all();
$org_id = Admin::user()->org_id;
$timeInfo = TimePeriod::where(['id'=>$data['timer']])->first()->toArray();
$patientInfo = Patient::where(['id'=>$data['user']])->first()->toArray();
$vaccineInfo = Vaccine::where(['id'=>$data['vaccine']])->first()->toArray();
$orgVaccine = OrganizationVaccine::where(['vaccine_id'=>$data['vaccine'],'org_id'=>$org_id])->first()->toArray();
DB::beginTransaction();
try {
$orderInfo = [
'user_id'=>$patientInfo['user_id'],
'organization_id'=>$org_id,
'order_sn'=>$patientInfo['user_id'],
'product_type'=>4,
'total_amount'=>$orgVaccine['price'],
'is_faster'=>1,
'order_status'=>2,
'payment_status'=>2,
];
if($data['date'] == date('Y-m-d',time())){
$orderInfo['order_status'] = 3;
// $orderInfo['order_notes'] = '快速预约';
$orderInfo['receiving_time'] = time();
}
$order = Order::create($orderInfo);
$order_sn = build_sn($order['id']);
Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
//保存订单患者信息
$addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'info', 'card_type', 'card_number', 'card_img_url', 'card_back_img_url', 'email', 'phone as patient_phone', 'social_card_number', 'born_hospital', 'guardian_name', 'address'])->where('id', $data['user'])->first()->getOriginal();
$addPatient['order_id'] = $order['id'];
$addPatient['patient_id'] = $data['user'];
$addPatient['organization_id'] = $org_id;
$addPatient['time_period_id'] = $data['timer'];
$addPatient['appoint_start_time'] = strtotime($data['date'].' '.$timeInfo['start_time_period'].':00');
$addPatient['appoint_end_time'] = strtotime($data['date'].' '.$timeInfo['end_time_period'].':00');
$patienter = OrderPatient::create($addPatient);
// todo 需要完善疫苗订单
$vaccine = [
'order_id'=>$order['id'],
'order_patient_id'=>$patienter['id'],
'vaccine_id'=>$orgVaccine['vaccine_id'],
'vaccine_type'=>$orgVaccine['type'],
'vaccine_price'=>$orgVaccine['price'],
'vaccine_name'=>$vaccineInfo['name'],
'vaccine_remark'=>$orgVaccine['remark'],
'vaccine_supplier'=>$orgVaccine['supplier'],
];
OrderVaccine::create($vaccine);
OrganizationVaccine::where(['vaccine_id'=>$data['vaccine'],'org_id'=>$org_id])->decrement('stock');
DB::commit();
} catch ( Exception $e){
DB::rollBack();
return $this->response()->error('添加订单失败');
}
return $this->response()->success('导入完成!')->refresh();
}
public function form()
{
Admin::script($this->script());
Admin::js('select2.js');
$patient = Patient::pluck('name','id');
$org_id = Admin::user()->org_id;
$ids = OrganizationVaccine::where(['org_id'=>$org_id])->where('stock','>',0)->pluck('vaccine_id');
$vaccine = Vaccine::whereIn('id',$ids)->pluck('name','id');
// todo 需要搜索
$this->select('user', '接种人')->options($patient)->rules('required',['required'=>'请选择接种患者']);
$this->select('vaccine', '疫苗')->options($vaccine)->rules('required',['required'=>'请选择疫苗']);
$this->date('date','时间')->rules('required',['required'=>'请选择时间']);
$this->select('timer','时间段')->rules('required',['required'=>'请选择时间段']);
}
public function html()
{
return << 快速预约
HTML;
}
protected function script()
{
return <<'+data[key]+'';
}
$('select[name="timer"]').empty();
$('select[name="timer"]').append(html);
});
});
EOT;
}
}