FasteOrder.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Community\Actions\Vaccine;
  3. use App\Models\Order;
  4. use App\Models\OrderPatient;
  5. use App\Models\OrderVaccine;
  6. use App\Models\OrganizationVaccine;
  7. use App\Models\Patient;
  8. use App\Models\TimePeriod;
  9. use App\Models\Vaccine;
  10. use Encore\Admin\Actions\Action;
  11. use Encore\Admin\Actions\RowAction;
  12. use Encore\Admin\Grid\Tools\AbstractTool;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Request;
  15. use Encore\Admin\Facades\Admin;
  16. class FasteOrder extends Action
  17. {
  18. protected $selector = '.import-tenant';
  19. public function handle()
  20. {
  21. $data = request()->all();
  22. $org_id = Admin::user()->org_id;
  23. $timeInfo = TimePeriod::where(['id'=>$data['timer']])->first()->toArray();
  24. $patientInfo = Patient::where(['id'=>$data['user']])->first()->toArray();
  25. $vaccineInfo = Vaccine::where(['id'=>$data['vaccine']])->first()->toArray();
  26. $orgVaccine = OrganizationVaccine::where(['vaccine_id'=>$data['vaccine'],'org_id'=>$org_id])->first()->toArray();
  27. DB::beginTransaction();
  28. try {
  29. $orderInfo = [
  30. 'user_id'=>$patientInfo['user_id'],
  31. 'organization_id'=>$org_id,
  32. 'order_sn'=>$patientInfo['user_id'],
  33. 'product_type'=>4,
  34. 'total_amount'=>$orgVaccine['price'],
  35. 'order_status'=>2,
  36. 'payment_status'=>2,
  37. ];
  38. if($data['date'] == date('Y-m-d',time())){
  39. $orderInfo['order_status'] = 3;
  40. // $orderInfo['order_notes'] = '快速预约';
  41. $orderInfo['receiving_time'] = time();
  42. }
  43. $order = Order::create($orderInfo);
  44. $order_sn = build_sn($order['id']);
  45. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  46. //保存订单患者信息
  47. $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();
  48. $addPatient['order_id'] = $order['id'];
  49. $addPatient['patient_id'] = $data['user'];
  50. $addPatient['organization_id'] = $org_id;
  51. $addPatient['time_period_id'] = $data['timer'];
  52. $addPatient['appoint_start_time'] = strtotime($data['date'].' '.$timeInfo['start_time_period'].':00');
  53. $addPatient['appoint_end_time'] = strtotime($data['date'].' '.$timeInfo['end_time_period'].':00');
  54. $patienter = OrderPatient::create($addPatient);
  55. // todo 需要完善疫苗订单
  56. $vaccine = [
  57. 'order_id'=>$order['id'],
  58. 'order_patient_id'=>$patienter['id'],
  59. 'vaccine_id'=>$orgVaccine['vaccine_id'],
  60. 'vaccine_type'=>$orgVaccine['type'],
  61. 'vaccine_price'=>$orgVaccine['price'],
  62. 'vaccine_name'=>$vaccineInfo['name'],
  63. 'vaccine_remark'=>$orgVaccine['remark'],
  64. 'vaccine_supplier'=>$orgVaccine['supplier'],
  65. ];
  66. OrderVaccine::create($vaccine);
  67. OrganizationVaccine::where(['vaccine_id'=>$data['vaccine'],'org_id'=>$org_id])->decrement('stock');
  68. DB::commit();
  69. } catch ( Exception $e){
  70. DB::rollBack();
  71. return $this->response()->error('添加订单失败');
  72. }
  73. return $this->response()->success('导入完成!')->refresh();
  74. }
  75. public function form()
  76. {
  77. Admin::script($this->script());
  78. Admin::js('select2.js');
  79. $patient = Patient::pluck('name','id');
  80. $org_id = Admin::user()->org_id;
  81. $ids = OrganizationVaccine::where(['org_id'=>$org_id])->where('stock','>',0)->pluck('vaccine_id');
  82. $vaccine = Vaccine::whereIn('id',$ids)->pluck('name','id');
  83. // todo 需要搜索
  84. $this->select('user', '接种人')->options($patient)->rules('required',['required'=>'请选择接种患者']);
  85. $this->select('vaccine', '疫苗')->options($vaccine)->rules('required',['required'=>'请选择疫苗']);
  86. $this->date('date','时间')->rules('required',['required'=>'请选择时间']);
  87. $this->select('timer','时间段')->rules('required',['required'=>'请选择时间段']);
  88. }
  89. public function html()
  90. {
  91. return <<<HTML
  92. <a class="btn btn-sm btn-default import-tenant"><i class="fa fa-calendar-check-o"></i> 快速预约</a>
  93. HTML;
  94. }
  95. protected function script()
  96. {
  97. return <<<EOT
  98. $("select[name='user']").select2({"allowClear":true,"placeholder":{"id":"","text":"\u75ab\u82d7"}});
  99. $('#date').blur(function () {
  100. var url = '';
  101. var date = $('#date').val();
  102. console.log(date);
  103. $.get("/cdms/api/getDateVaccine",{date: this.value}, function (data) {
  104. var html = '<option value="">请选择时间段</option>';
  105. for(let key in data){
  106. console.log(key + '---' + data[key])
  107. html += '<option value="'+key+'">'+data[key]+'</option>';
  108. }
  109. $('select[name="timer"]').empty();
  110. $('select[name="timer"]').append(html);
  111. });
  112. });
  113. EOT;
  114. }
  115. }