DeliveryAppointmentController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\CarType;
  4. use App\CommodityBarcode;
  5. use App\Components\AsyncResponse;
  6. use App\DeliveryAppointment;
  7. use App\DeliveryAppointmentCar;
  8. use App\DeliveryAppointmentDetail;
  9. use App\Imports\AppointmentDetail;
  10. use App\Warehouse;
  11. use Carbon\Carbon;
  12. use Carbon\CarbonPeriod;
  13. use Illuminate\Database\Eloquent\Builder;
  14. use Illuminate\Support\Facades\Auth;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Validator;
  17. class DeliveryAppointmentController extends Controller
  18. {
  19. use AsyncResponse;
  20. public function appointment()
  21. {
  22. $owners = app("OwnerService")->getIntersectPermitting();
  23. $cars = CarType::query()->get();
  24. $warehouses = Warehouse::query()->select("id","name")->get();
  25. return view("store.deliveryAppointment.appointment",compact("owners","cars","warehouses"));
  26. }
  27. public function import()
  28. {
  29. $this->importExcel(new AppointmentDetail());
  30. }
  31. /**
  32. * 获取产能
  33. *
  34. */
  35. public function getCapacity()
  36. {
  37. $model = request("model");
  38. $errors = $this->appointmentValidator($model)->errors();
  39. if (count($errors)>0)$this->success(["errors"=>$errors]);
  40. /** @var \stdClass $warehouse */
  41. $warehouse = Warehouse::query()->find(request("warehouse_id"));
  42. $tonne = $model["tonne"];
  43. $cubicMeter = $model["cubic_meter"];
  44. $amount = request("detail_amount");
  45. $need = app("DeliveryAppointmentService")->calculateCapacity($tonne,$cubicMeter,$amount,$warehouse->reduced_production_capacity_coefficient);//所需产能
  46. $start = Carbon::tomorrow();
  47. $end = Carbon::today()->addDays(7);
  48. $map = [];
  49. DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
  50. ->whereBetween("appointment_date",[$start->toDateString(),$end->toDateString()])
  51. ->where("status",0)
  52. ->where("warehouse_id",$warehouse->id)
  53. ->groupBy(["appointment_date","date_period"])->get()
  54. ->each(function ($appointment)use(&$map){
  55. $map[$appointment->appointment_date."-".$appointment->date_period] = $appointment->capacity;
  56. });
  57. $list = [];
  58. $capacity = $warehouse->production_capacity;
  59. foreach (CarbonPeriod::create($start,$end) as $date){
  60. /** @var $date Carbon */
  61. $date = $date->format("Y-m-d");
  62. $periods = [];
  63. foreach (DeliveryAppointment::PERIOD as $key=>$period){
  64. $total = $capacity*DeliveryAppointment::HOUR[$key];//仓库该时段产能总量
  65. $used = $map[$date."-".$key] ?? 0; //已使用产能
  66. $available = $total-$used; //可用产能
  67. if ($available < $need)$periods[] = ["time"=>$period,"index"=>$key,"isAvailable"=>false];
  68. else $periods[] = ["time"=>$period,"index"=>$key,"isAvailable"=>true];
  69. }
  70. $list[] = ["date"=>$date,"period"=>$periods];
  71. }
  72. $this->success($list);
  73. }
  74. /**
  75. * 确定预约
  76. */
  77. public function submitAppointment()
  78. {
  79. $model = request("model");
  80. $selectDate = request("selectDate");
  81. $details = request("details");
  82. $errors = $this->appointmentValidator($model)->errors();
  83. if (count($errors)>0)$this->success(["errors"=>$errors]);
  84. $errors = Validator::make($selectDate,[
  85. "date" => ["required","date","after:today"],
  86. "time" => ["required","integer"],
  87. ])->errors();
  88. if (count($errors)>0)$this->error("未选定预约日期");
  89. DB::transaction(function ()use($model,$selectDate,$details){
  90. $result = DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
  91. ->where("appointment_date",$selectDate["date"])
  92. ->where("date_period",$selectDate["time"])
  93. ->where("warehouse_id",$model["warehouse_id"])
  94. ->where("status",0)
  95. ->groupBy(["appointment_date","date_period"])
  96. ->lockForUpdate()->first();
  97. /** @var \stdClass $warehouse */
  98. $warehouse = Warehouse::query()->find($model["warehouse_id"]);
  99. $need = app("DeliveryAppointmentService")->
  100. calculateCapacity($model["tonne"],$model["cubic_meter"],count($details),
  101. $warehouse->reduced_production_capacity_coefficient);
  102. if ($result){
  103. $total = $warehouse->production_capacity*DeliveryAppointment::HOUR[$selectDate["time"]];
  104. $available = $total-$result->capacity;
  105. if ($available < $need)$this->success(["isFail"=>true]);
  106. }
  107. /** @var \stdClass $appointment */
  108. $appointment = DeliveryAppointment::query()->create([
  109. "user_id" => Auth::id(),
  110. "owner_id" => $model["owner_id"],
  111. "procurement_number" => $model["procurement_number"],
  112. "asn_number" => $model["asn_number"],
  113. "warehouse_id" => $model["warehouse_id"],
  114. "tonne" => $model["tonne"],
  115. "cubic_meter" => $model["cubic_meter"],
  116. "box_amount" => $model["box_amount"],
  117. "capacity" => $need,
  118. "appointment_date" => $selectDate["date"],
  119. "date_period" => $selectDate["time"],
  120. ]);
  121. if ($details)app("DeliveryAppointmentService")->insertDetails($appointment,$details);
  122. $insert = [];
  123. foreach ($model["cars"] as $index=>$car){
  124. $rand = mt_rand(0,9);
  125. $len = strlen($appointment->id);
  126. $ten = $len < 2 ? "0" : substr($appointment->id,$len-2,1);
  127. $one = substr($appointment->id,$len-1,1);
  128. //唯一码 随机数+十位+当前下标+个位+日期
  129. $number = $rand.$ten.$index.$one.date("d");
  130. $insert[] = [
  131. "delivery_appointment_id" => $appointment->id,
  132. "license_plate_number" => $car["license_plate_number"],
  133. "car_id" => $car["car_id"],
  134. "driver_name" => $car["driver_name"],
  135. "driver_phone" => $car["driver_phone"],
  136. "appointment_number" => $number,
  137. ];
  138. }
  139. DeliveryAppointmentCar::query()->insert($insert);
  140. //md5加密在密文第五位后插入
  141. $md5 = substr_replace(md5(date("m-d")),$appointment->id,5,0);
  142. $this->success(["key"=>$md5]);
  143. });
  144. }
  145. private function appointmentValidator(array $model)
  146. {
  147. return Validator::make($model,[
  148. "owner_id" => ["required","integer"],
  149. "warehouse_id" => ["required","integer"],
  150. "tonne" => ["required_without:cubic_meter","numeric"],
  151. "cubic_meter" => ["required_without:tonne","numeric"],
  152. "box_amount" => ["nullable","integer"],
  153. "cars.*.license_plate_number" => ["required","size:7"],
  154. "cars.*.car_id" => ["nullable","integer"],
  155. "cars.*.driver_phone" => ["nullable"],
  156. "cars.*.driver_name" => ["nullable"],
  157. ],[
  158. 'required'=>':attribute 不应为空',
  159. 'integer'=>':attribute 应为数值',
  160. 'required_without'=>':attribute 不应为空',
  161. 'numeric'=>':attribute 必须为数字',
  162. 'size'=>':attribute 非法',
  163. ],[
  164. 'owner_id'=>'货主',
  165. 'warehouse_id'=>'仓库',
  166. 'tonne'=>'吨',
  167. 'cubic_meter'=>'立方',
  168. 'cars.*.license_plate_number'=>'车牌号',
  169. 'cars.*.car_id'=>'车型',
  170. 'cars.*.driver_phone'=>'司机电话',
  171. 'cars.*.driver_name'=>'司机姓名',
  172. ]);
  173. }
  174. /**
  175. * 根据key取id 鉴权数据
  176. */
  177. public function showAppointmentInfo()
  178. {
  179. $key = request("k");
  180. $len = strlen($key);
  181. $id = substr($key,5,$len-32);
  182. $md5 = substr($id,0,5).substr($id,5+$len-32);
  183. if ($md5!==md5(date("m-d")))return view("exception.404");
  184. /** @var \stdClass $appointment */
  185. $appointment = DeliveryAppointment::query()->with("cars")->find($id);
  186. if (!$appointment || $appointment->user_id != Auth::id())return view("exception.404");
  187. return view("store.deliveryAppointment.success",compact("appointment"));
  188. }
  189. }