diff --git a/admin/controllers/base/DataController.php b/admin/controllers/base/DataController.php index 7a73df8..5edfdeb 100644 --- a/admin/controllers/base/DataController.php +++ b/admin/controllers/base/DataController.php @@ -7,22 +7,26 @@ use common\events\PrescriptionEvent; use common\events\ProductOrderEvent; use common\forms\ProductCancelForm; use common\forms\ProductRefundForm; +use common\helpers\FuncHelper; use common\jobs\Sms\WaitApprovalMessageJob; use common\jobs\templateMessage\PrescriptionCreated; use common\models\oldDb\Prescription; use common\models\oldDb\ProductOrder; +use common\models\oldDb\ProductOrderLog; use common\models\oldDb\Reconciliation; use common\models\oldDb\Register; use common\models\oldDb\Store; use common\models\oldDb\User; +use common\services\JacErpService; use yii\db\ActiveRecord; use yii\db\Exception; +use yii\helpers\Json; class DataController extends BaseAdminController { - public $optional = ['count', 'top-drugs', 'trend', 'store-number', 'notify-order-generation']; + public $optional = ['count', 'top-drugs', 'trend', 'store-number', 'notify-order-generation', 'send-order']; /** * 获取首页统计数据 @@ -290,4 +294,93 @@ class DataController extends BaseAdminController } return ['ok']; } + + public function actionSendOrder() + { + $this->orderId = 117; + $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'] != 0){ + $processContent = '煎服,每天'.$recipe['deployment'].'次。'.$recipe['process_rule']; + } elseif ($recipe['package_method_id'] != 0) { + $processContent = '外配,每天'.$recipe['deployment'].'次。'. $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' => $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(); + + $t->commit(); + } 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()); + return; + } + } } \ No newline at end of file