DeliveryAppointmentController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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\Services\common\ExportService;
  11. use App\Services\common\QueryService;
  12. use App\Warehouse;
  13. use Carbon\Carbon;
  14. use Carbon\CarbonPeriod;
  15. use Illuminate\Database\Eloquent\Builder;
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Gate;
  19. use Illuminate\Support\Facades\Validator;
  20. class DeliveryAppointmentController extends Controller
  21. {
  22. use AsyncResponse;
  23. public function list()
  24. {
  25. if(!Gate::allows('入库管理-客户预约-预约管理')){ return view("exception.authority"); }
  26. $list = app("DeliveryAppointmentService")->query(request()->input())
  27. ->paginate(request("paginate") ?? 50);
  28. $warehouses = Warehouse::query()->select("id","name")->get();
  29. $owners = app("OwnerService")->getIntersectPermitting();
  30. return view("store.deliveryAppointment.list",compact("list","warehouses","owners"));
  31. }
  32. public function appointment()
  33. {
  34. if(!Gate::allows('入库管理-客户预约-预约')){ return view("exception.authority"); }
  35. $owners = app("OwnerService")->getIntersectPermitting();
  36. $cars = CarType::query()->get();
  37. $warehouses = Warehouse::query()->select("id","name")->get();
  38. return view("store.deliveryAppointment.appointment",compact("owners","cars","warehouses"));
  39. }
  40. public function import()
  41. {
  42. $this->importExcel(new AppointmentDetail());
  43. }
  44. /**
  45. * 获取产能
  46. *
  47. */
  48. public function getCapacity()
  49. {
  50. $this->gate("入库管理-客户预约-预约");
  51. $model = request("model");
  52. $errors = $this->appointmentValidator($model)->errors();
  53. if (count($errors)>0)$this->success(["errors"=>$errors]);
  54. /** @var \stdClass $warehouse */
  55. $warehouse = Warehouse::query()->find($model["warehouse_id"]);
  56. $tonne = $model["tonne"] ?? 0;
  57. $cubicMeter = $model["cubic_meter"] ?? 0;
  58. $amount = request("detail_amount");
  59. $need = app("DeliveryAppointmentService")->calculateCapacity($tonne,$cubicMeter,$amount,$warehouse->reduced_production_capacity_coefficient);//所需产能
  60. $start = Carbon::tomorrow();
  61. $end = Carbon::today()->addDays(7);
  62. $map = [];
  63. DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
  64. ->whereBetween("appointment_date",[$start->toDateString(),$end->toDateString()])
  65. ->where("status",0)
  66. ->where("warehouse_id",$warehouse->id)
  67. ->groupBy(["appointment_date","date_period"])->get()
  68. ->each(function ($appointment)use(&$map){
  69. $map[$appointment->appointment_date."-".$appointment->date_period] = $appointment->capacity;
  70. });
  71. $list = [];
  72. $capacity = $warehouse->production_capacity;
  73. foreach (CarbonPeriod::create($start,$end) as $date){
  74. /** @var $date Carbon */
  75. $date = $date->format("Y-m-d");
  76. $periods = [];
  77. foreach (DeliveryAppointment::PERIOD as $key=>$period){
  78. $total = $capacity*DeliveryAppointment::HOUR[$key];//仓库该时段产能总量
  79. $used = $map[$date."-".$key] ?? 0; //已使用产能
  80. $available = $total-$used; //可用产能
  81. if ($available < $need)$periods[] = ["time"=>$period,"index"=>$key,"isAvailable"=>false];
  82. else $periods[] = ["time"=>$period,"index"=>$key,"isAvailable"=>true];
  83. }
  84. $list[] = ["date"=>$date,"period"=>$periods];
  85. }
  86. $this->success($list);
  87. }
  88. /**
  89. * 确定预约
  90. */
  91. public function submitAppointment()
  92. {
  93. $this->gate("入库管理-客户预约-预约");
  94. $model = request("model");
  95. $selectDate = request("date");
  96. $details = request("details");
  97. $errors = $this->appointmentValidator($model)->errors();
  98. if (count($errors)>0)$this->success(["errors"=>$errors]);
  99. $errors = Validator::make($selectDate,[
  100. "date" => ["required","date","after:today"],
  101. "time" => ["required","integer"],
  102. ])->errors();
  103. if (count($errors)>0)$this->error("未选定预约日期");
  104. DB::transaction(function ()use($model,$selectDate,$details,&$appointment){
  105. $result = DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
  106. ->where("appointment_date",$selectDate["date"])
  107. ->where("date_period",$selectDate["time"])
  108. ->where("warehouse_id",$model["warehouse_id"])
  109. ->where("status",0)
  110. ->groupBy(["appointment_date","date_period"])
  111. ->lockForUpdate()->first();
  112. /** @var \stdClass $warehouse */
  113. $warehouse = Warehouse::query()->find($model["warehouse_id"]);
  114. $need = app("DeliveryAppointmentService")->
  115. calculateCapacity($model["tonne"] ?? 0,$model["cubic_meter"] ?? 0,count($details),
  116. $warehouse->reduced_production_capacity_coefficient);
  117. if ($result){
  118. $total = $warehouse->production_capacity*DeliveryAppointment::HOUR[$selectDate["time"]];
  119. $available = $total-$result->capacity;
  120. if ($available < $need)$this->success(["isFail"=>true]);
  121. }
  122. /** @var \stdClass $appointment */
  123. $appointment = DeliveryAppointment::query()->create([
  124. "user_id" => Auth::id(),
  125. "owner_id" => $model["owner_id"],
  126. "procurement_number" => $model["procurement_number"] ?? null,
  127. "asn_number" => $model["asn_number"] ?? null,
  128. "warehouse_id" => $model["warehouse_id"],
  129. "tonne" => $model["tonne"] ?? 0,
  130. "cubic_meter" => $model["cubic_meter"] ?? 0,
  131. "box_amount" => $model["box_amount"] ?? 0,
  132. "capacity" => $need,
  133. "appointment_date" => $selectDate["date"],
  134. "date_period" => $selectDate["time"],
  135. ]);
  136. if ($details)app("DeliveryAppointmentService")->insertDetails($appointment,$details);
  137. $insert = [];
  138. foreach ($model["cars"] as $index=>$car){
  139. $rand = mt_rand(0,9);
  140. $len = strlen($appointment->id);
  141. $ten = $len < 2 ? "0" : substr($appointment->id,$len-2,1);
  142. $one = substr($appointment->id,$len-1,1);
  143. //唯一码 随机数+十位+当前下标+个位+日期
  144. $number = $rand.$ten.$index.$one.date("d");
  145. $insert[] = [
  146. "delivery_appointment_id" => $appointment->id,
  147. "license_plate_number" => $car["license_plate_number"],
  148. "car_id" => $car["car_id"],
  149. "driver_name" => $car["driver_name"],
  150. "driver_phone" => $car["driver_phone"],
  151. "appointment_number" => $number,
  152. ];
  153. }
  154. DeliveryAppointmentCar::query()->insert($insert);
  155. });
  156. //md5加密在密文第五位后插入
  157. $md5 = substr_replace(md5(date("m-d")),$appointment->id,5,0);
  158. $this->success(["key"=>$md5]);
  159. }
  160. private function appointmentValidator(array $model)
  161. {
  162. return Validator::make($model,[
  163. "owner_id" => ["required","integer"],
  164. "warehouse_id" => ["required","integer"],
  165. "tonne" => ["required_without:cubic_meter","numeric"],
  166. "cubic_meter" => ["required_without:tonne","numeric"],
  167. "box_amount" => ["nullable","integer"],
  168. "cars.*.license_plate_number" => ["required","size:7"],
  169. "cars.*.car_id" => ["nullable","integer"],
  170. "cars.*.driver_phone" => ["nullable"],
  171. "cars.*.driver_name" => ["nullable"],
  172. ],[
  173. 'required'=>':attribute 不应为空',
  174. 'integer'=>':attribute 应为数值',
  175. 'required_without'=>':attribute 不应为空',
  176. 'numeric'=>':attribute 必须为数字',
  177. 'size'=>':attribute 非法',
  178. ],[
  179. 'owner_id'=>'货主',
  180. 'warehouse_id'=>'仓库',
  181. 'tonne'=>'吨',
  182. 'cubic_meter'=>'立方',
  183. 'cars.*.license_plate_number'=>'车牌号',
  184. 'cars.*.car_id'=>'车型',
  185. 'cars.*.driver_phone'=>'司机电话',
  186. 'cars.*.driver_name'=>'司机姓名',
  187. ]);
  188. }
  189. /**
  190. * 根据key取id 鉴权数据
  191. */
  192. public function showAppointmentInfo()
  193. {
  194. if(!Gate::allows('入库管理-客户预约-预约')){ return view("exception.authority"); }
  195. $key = request("k");
  196. $len = strlen($key);
  197. $id = substr($key,5,$len-32);
  198. $md5 = substr($key,0,5).substr($key,5+$len-32);
  199. if ($md5!==md5(date("m-d")))return view("exception.404");
  200. /** @var \stdClass $appointment */
  201. $appointment = DeliveryAppointment::query()->with("cars")->find($id);
  202. if (!$appointment || $appointment->user_id != Auth::id())return view("exception.404");
  203. return view("store.deliveryAppointment.success",compact("appointment"));
  204. }
  205. /**
  206. * 取消预约
  207. */
  208. public function cancel()
  209. {
  210. $this->gate("入库管理-客户预约-预约");
  211. $id = request("id");
  212. if (!$id)$this->error("非法参数");
  213. DeliveryAppointment::query()->where("status",0)->where("id",$id)->update(["status"=>1]);
  214. $this->success(1);
  215. }
  216. /**
  217. * 导出
  218. */
  219. public function export()
  220. {
  221. if(!Gate::allows('入库管理-客户预约-预约管理')){ return view("exception.authority"); }
  222. if (request("checkAllSign")){
  223. $params = request()->input();
  224. unset($params["checkAllSign"]);
  225. $query = app("DeliveryAppointmentService")->query($params);
  226. }else $query = app("DeliveryAppointmentService")->query(["id"=>request("id")]);
  227. /** @var Builder $query */
  228. $list = $query->with(["owner","warehouse"])->get();
  229. $row = ["状态","货主","预约时间","仓库","预约号","车牌号","车型",
  230. "司机姓名","司机电话","吨","立方","箱数","采购单号","ASN单号","商品名称","条码","数量","创建时间"];
  231. foreach ($list as &$data){
  232. $appointment = "";
  233. $number = "";
  234. $carType = "";
  235. $driverName = "";
  236. $driverPhone = "";
  237. $commodityName = "";
  238. $commodityCode = "";
  239. $amount = "";
  240. foreach ($data->cars as $car){
  241. $appointment .= $car->appointment_number."\r\n";
  242. $number .= $car->license_plate_number."\r\n";
  243. $carType .= ($car->car->name ?? '')."\r\n";
  244. $driverName .= ($car->driver_name ?? '')."\r\n";
  245. $driverPhone .= ($car->driver_phone ?? '')."\r\n";
  246. }
  247. foreach ($data->details as $detail){
  248. $commodityName .= ($detail->commodity->name ?? $detail->name)."\r\n";
  249. $commodityCode .= ($detail->commodity->barcodes->code ?? $detail->bar_code)."\r\n";
  250. $amount .= $detail->amount."\r\n";
  251. }
  252. $data = [
  253. DeliveryAppointment::STATUS[$data->status],
  254. $data->owner->name ?? '',
  255. $data->appointment_date,
  256. $data->warehouse->name ?? '',
  257. $appointment,
  258. $number,
  259. $carType,
  260. $driverName,
  261. $driverPhone,
  262. $data->tonne,
  263. $data->cubic_meter,
  264. $data->box_amount,
  265. $data->procurement_number,
  266. $data->asn_number,
  267. $commodityName,
  268. $commodityCode,
  269. $amount,
  270. $data->created_at
  271. ];
  272. }
  273. return app(ExportService::class)->json($row,$list,"预约记录");
  274. }
  275. }