110 lines
5.3 KiB
PHP
110 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace common\jobs;
|
|
|
|
use common\helpers\FuncHelper;
|
|
use common\models\oldDb\ProductOrder;
|
|
use common\models\oldDb\ProductOrderLog;
|
|
use common\services\JacErpService;
|
|
use yii\helpers\Json;
|
|
use yii\queue\JobInterface;
|
|
use yii\queue\Queue;
|
|
|
|
//产品订单同步
|
|
class ProductOrderSyncJob extends BaseJob implements JobInterface
|
|
{
|
|
public $orderId;
|
|
|
|
/**
|
|
* @param Queue $queue which pushed and is handling the job
|
|
*/
|
|
public function execute($queue)
|
|
{
|
|
$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) {
|
|
$unitName = !empty($value['unit'])? $value['unit']['name'] : 'g';
|
|
$detail = [
|
|
'drug_seq' => $value['drug_id'],
|
|
'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));
|
|
$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());
|
|
ProductOrderLog::saveLog($this->orderId,'订单自动同步江奥川ERP('.$ProductOrder->store->erp_id.')异常:'.$exception->getFile());
|
|
return;
|
|
}
|
|
}
|
|
} |