orderId,'订单自动同步江奥川ERP start'); $t = \Yii::$app->db->beginTransaction(); try { $this->setRequest(); $ProductOrder = ProductOrder::find()->with(['prescription','store'])->where([ 'id' => $this->orderId, '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']){ $processContent = $recipe['process_rule']; } $params = [ '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']), 'tot_posts' => $recipe['dosage'], 'tisane_posts' => $recipe['dosage'], 'usage' => '煎服,每天'.$recipe['deployment'].'次。'.$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), 'diagnose' => $prescriptionContent['clinical_diagnose'], 'yw_type' => '1', 'submit_date' => date('Y-m-d'), 'get_no' => substr($ProductOrder->user_id.rand(1000,999).$ProductOrder->up_id,0,10) ]; if($ProductOrder->delivery_method){ //到店取货 $params['send_way'] = '0'; $params['telphone'] = $prescriptionContent['patient']['mobile']; $params['addr'] = $ProductOrder->store->position; } else { //快递到家 $params['send_way'] = '1'; $params['telphone'] = $ProductOrder->express_mobile; $params['addr'] = $ProductOrder->express_region.$ProductOrder->express_address; } $details = []; foreach ($recipeContent as $value) { $detail = [ 'drug_seq' => $value['drug_id'], 'his_prc_cod' => 'XK'.$value['id'], 'drug_name' => $value['name'], 'spec' => $value['number'].$value['unit']['name'], 'post_quantity' => $value['number'], 'note' => $value['order']?$value['useWay']['name']:'无', 'drug_price' => $value['price'], 'wholesale_price' => $value['buy_price'], 'unit' => $value['unit']['name'], 'tisane_prc_code' => $value['drug_number'] ]; $details[] = $detail; } $params['details'] = $details; \Yii::info(__METHOD__."——同步订单至江奥川erp(".$ProductOrder->store->erp_id.")原始数据: ".Json::encode($params)); $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(); ProductOrderLog::saveLog($this->orderId,'订单自动同步江奥川ERP('.$ProductOrder->store->erp_id.') end'); $t->commit(); } catch (\Exception $exception) { $t->rollBack(); ProductOrderLog::saveLog($this->orderId,'订单自动同步江奥川ERP('.$ProductOrder->store->erp_id.')异常:'.$exception->getMessage()); return; } } }