DeliveryAppointmentController.php 8.7 KB

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