ReceivingTaskController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\DeliveryAppointmentCar;
  4. use App\Filters\ReceivingTaskFilters;
  5. use App\Http\Requests\Api\ReceivingTaskRequest;
  6. use App\ReceivingTask;
  7. use App\ReceivingTaskItem;
  8. use App\Services\OwnerService;
  9. use App\Services\ReceivingTaskService;
  10. use App\Warehouse;
  11. use Illuminate\Support\Facades\Gate;
  12. use Illuminate\Http\Request;
  13. class ReceivingTaskController extends Controller
  14. {
  15. public $service;
  16. public $ownerService;
  17. public function __construct(ReceivingTaskService $service, OwnerService $ownerService)
  18. {
  19. $this->service = $service;
  20. $this->ownerService = $ownerService;
  21. }
  22. public function index(Request $request, ReceivingTaskFilters $filter)
  23. {
  24. if (Gate::denies('入库管理-开单入库-查询')) {
  25. return redirect('/');
  26. }
  27. $receivingTasks = ReceivingTask::query()->with(['items', 'owner', 'wareHouse', 'file', 'deliveryAppointmentCar'])->filter($filter)->orderByDesc('created_at')->paginate(50);
  28. $owners = $this->ownerService->getQuery()->select('id', 'name')->get();
  29. $warehouses = Warehouse::query()->get();
  30. return view("store.receivingTasks.index", compact('receivingTasks', 'owners', 'warehouses'));
  31. }
  32. public function create()
  33. {
  34. if (Gate::denies('入库管理-开单入库-创建')) {
  35. return redirect('/');
  36. }
  37. $wareHouse = Warehouse::query()->get();
  38. $owners = $this->ownerService->getQuery()->select("id", "code", "name")->get();
  39. return view("store.receivingTasks.create", compact('wareHouse', 'owners'));
  40. }
  41. public function storeApi(ReceivingTaskRequest $request): array
  42. {
  43. if (Gate::denies('入库管理-开单入库-创建')) {
  44. return ['success' => false, 'message' => '没有对应权限'];
  45. }
  46. $appointment_number = $request->input('appointment_number', null);
  47. $delivery_appointment_car = DeliveryAppointmentCar::query()->with('deliveryAppointment')->where('appointment_number', $appointment_number)->first();
  48. if (!$delivery_appointment_car || !$delivery_appointment_car->deliveryAppointment) {
  49. return ['success' => false, 'errors' => ['appointment_number' => ['对应预约号未找到']]];
  50. }
  51. $ans_number_string = $delivery_appointment_car->deliveryAppointment->asn_number ?? '';
  52. $ans_numbers = array_filter(preg_split('/[,, ]+/is', $ans_number_string));
  53. if (count($ans_numbers) === 0 && count($request->input('asn_no',[])) == 0) {
  54. return ['success' => false, 'errors' => ['appointment_number' => ['预约号没有对应的Asn号']]];
  55. }
  56. $asn_nos = array_unique(array_merge($ans_numbers, $request->input('asn_nos') ?? []));
  57. if (count($asn_nos) == 0){
  58. return ['success' => false, 'errors' => ['appointment_number' => ['预约号没有对应的Asn号']]];
  59. }
  60. // $receiving_task_items = ReceivingTaskItem::query()->whereIn('asn_no',$request->input('asn_nos') ?? [])->get()->map(function($item){
  61. // return $item->asn_no;
  62. // })->toArray();
  63. // if (count($receiving_task_items) >0 ){
  64. // return [
  65. // 'success' => false,'errors' => ['appointment_number' => ['勾选的asn号'.json_encode($receiving_task_items).'已再其他开单入库中有关联']]
  66. // ];
  67. // }
  68. if ($delivery_appointment_car->deliveryAppointment->owner_id != $request->input('owner_id')) {
  69. return ['success' => false, 'errors' => ['appointment_number' => ['预约号与货主未必填项']]];
  70. }
  71. if (ReceivingTask::query()->where('delivery_appointment_car_id', $delivery_appointment_car->id)->exists()) {
  72. return ['success' => false, 'errors' => ['appointment_number' => ['预约号已有对应的任务']]];
  73. }
  74. try {
  75. $receiving_task = $this->service->createReceivingTask($delivery_appointment_car, $request->all());
  76. if (!$receiving_task->id) return ['success' => false, 'message' => '生成入库单任务失败,请重新尝试'];
  77. $receiving_task->loadMissing(['wareHouse', 'owner', 'deliveryAppointmentCar']);
  78. return ['success' => true, 'data' => $receiving_task];
  79. } catch (\Exception $e) {
  80. return ['success' => false, 'message' => '生成入库单任务失败,请重新尝试'];
  81. }
  82. }
  83. /**
  84. * 根据预约号获取Asn单号
  85. * @param Request $request
  86. * @return array
  87. */
  88. public function getAsnByAppointmentNumberApi(Request $request): array
  89. {
  90. $appointment_number = $request->input('appointment_number');
  91. if (!$appointment_number) {
  92. return ['success' => false, 'errors' => ['appointment_number' => '预约号不能为空']];
  93. }
  94. $delivery_appointment_car = DeliveryAppointmentCar::query()->with('deliveryAppointment')->where('appointment_number', $appointment_number)->first();
  95. if (ReceivingTask::query()->where('delivery_appointment_car_id', $delivery_appointment_car->id)->exists()) {
  96. return ['success' => false, 'errors' => ['appointment_number' => ['预约号已有对应的任务']]];
  97. }
  98. $ans_number_string = $delivery_appointment_car->deliveryAppointment->asn_number ?? '';
  99. $ans_numbers = array_filter(preg_split('/[,, ]+/is', $ans_number_string));
  100. return ['success' => true, 'data' => $ans_numbers];
  101. }
  102. }