orderId,'开始同步订单'); // 开始数据库事务 $t = \Yii::$app->db->beginTransaction(); try { // 设置请求参数 $this->setRequest(); // 查询符合条件的订单 $ProductOrder = ProductOrder::find()->with(['prescription','store'])->where([ 'id' => $this->orderId, 'is_sync_erp' => 0, 'is_pay' => 1, 'is_online' => 0, 'status' => 1 //已付款待发货 ])->one(); // 检查订单是否存在且未被取消 if (!$ProductOrder || !$ProductOrder->prescription || $ProductOrder->cancel_status == 1 || !$ProductOrder->store) { throw new \Exception('订单不存在或已取消'); } // 检查处方是否已审核 if($ProductOrder->prescription->status != 1){ throw new \Exception('处方未审核'); } // 解析处方内容 $prescriptionContent = Json::decode($ProductOrder->prescription->content); $recipe = $prescriptionContent['repice'][0]; $recipeContent = Json::decode($recipe['content']); // 根据煎服或外配规则生成加工内容 $processContent = ''; if($recipe['process_rule_id'] != 0){ $processContent = '煎服,每天'.$recipe['deployment'].'次。'.$recipe['process_rule']; } elseif ($recipe['package_method_id'] != 0) { $processContent = '外配,每天'.$recipe['deployment'].'次。'. $recipe['process_rule']; } // 准备同步到ERP的参数 $params = [ // 生成聊天ID,格式为 'XK_' 加用户ID、上级ID和注册ID 'cht_id' => 'XK_'.$ProductOrder->user_id.'_'.$ProductOrder->up_id.'_'.$ProductOrder->prescription->register_id, // 处方序列号 'ps_seq' => $ProductOrder->prescription->prescription_no, // 患者姓名 'pat_name' => $prescriptionContent['patient']['name'], // 性别,根据患者性别代码决定 'sex' => $prescriptionContent['patient']['sex'] == 1 ? '男':'女', // 年龄,通过身份证号码计算得出 // 'age' => FuncHelper::getAgeFromIdNo($prescriptionContent['patient']['id_card']), 'age' => $prescriptionContent['patient']['age'], // 总剂量 'tot_posts' => $recipe['dosage'], // 单剂量 'tisane_posts' => $recipe['dosage'], // 用法说明 'usage' => $processContent,//`, 每次'.$recipe['volume'].'ml // 医生姓名 'doctor' => $prescriptionContent['doctor']['name'], // 科室名称 'department' => $prescriptionContent['doctor']['depart']['name'], // 剂量重量,即加工内容的条目数 'post_weight' => count($recipeContent), // 单剂量金额,总价格除以总剂量并四舍五入到两位小数 // 'post_amount' => round($recipe['total_price']/$recipe['dosage'],2), 'post_amount' => $recipe['total_price'], // 诊断信息 'diagnose' => $prescriptionContent['clinical_diagnose'], // 业务类型代码 'yw_type' => '1', // 提交日期,格式为年-月-日 'submit_date' => date('Y-m-d'), // 领取编号,由用户ID、随机数和上级ID组合后取前10位 'get_no' => substr($ProductOrder->user_id.rand(1000,999).$ProductOrder->up_id,0,10) ]; // 根据配送方式添加详细信息 if($ProductOrder->delivery_method){ //到店取货 // 设置配送方式为默认值'0' $params['send_way'] = '0'; // 使用患者移动电话作为联系方式 $params['telphone'] = $prescriptionContent['patient']['mobile']; // 配送地址设置为门店位置 $params['addr'] = $ProductOrder->store->position; } else { //快递到家 // 设置发货方式为1,此处假设1代表某种特定的发货方式,例如快递 $params['send_way'] = '1'; // 将订单的联系电话设置为(express_mobile字段的值) $params['telphone'] = $ProductOrder->express_mobile; // 组合订单的地址信息,包括地区和详细地址 $params['addr'] = $ProductOrder->express_region.$ProductOrder->express_address; } // 准备订单详情 $details = []; foreach ($recipeContent as $value) { $unitName = !empty($value['unit'])? $value['unit']['name'] : 'g'; $detail = [ // 药品序列号 'drug_seq' => $value['drug_id'], // 历史采购代码,拼接'XK'前缀以区分 'his_prc_cod' => 'XK'.$value['id'], // 药品名称 'drug_name' => $value['name'], // 规格,拼接数量和单位名称 'spec' => $value['number'].$unitName, // 投标数量,即药品的数量 'post_quantity' => $value['number'], // 备注,根据是否有订单选择对应的使用方式名称或'无' 'note' => $value['order']?$value['useWay']['name']:'无', // 药品价格 'drug_price' => $value['price'], // 批发价格 'wholesale_price' => $value['buy_price'], // 单位名称 'unit' => $unitName, // 汤剂配方代码 'tisane_prc_code' => $value['drug_number'] ]; $details[] = $detail; } $params['details'] = $details; // 记录日志 // \Yii::info(__METHOD__."——同步订单至江奥川erp(".$ProductOrder->store->erp_id.")原始数据: ".Json::encode($params)); // ProductOrderLog::saveLog($this->orderId,__METHOD__."——同步订单至江奥川erp(".$ProductOrder->store->erp_id.")原始数据: ".Json::encode($params)); // 调用ERP服务同步订单 $result = JacErpService::getInstance($ProductOrder->store->erp_id)->syncOrder($params); if(!$result){ throw new \Exception('订单同步江奥川ERP失败:'.$this->getErrorMsg()); } // 订单更新为已同步erp $ProductOrder->is_sync_erp = 1; $ProductOrder->save(); // 提交事务 $t->commit(); ProductOrderLog::saveLog($this->orderId,'同步成功'); } catch (\Exception $exception) { // 回滚事务 $t->rollBack(); // 记录异常日志 ProductOrderLog::saveLog($this->orderId,'订单自动同步江奥川ERP('.$ProductOrder->store->erp_id.')异常:'.$exception->getMessage()); ProductOrderLog::saveLog($this->orderId,'订单自动同步江奥川ERP('.$ProductOrder->store->erp_id.')异常:'.$exception->getLine()); ProductOrderLog::saveLog($this->orderId,'订单自动同步江奥川ERP('.$ProductOrder->store->erp_id.')异常:'.$exception->getFile()); return; } } }