| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace App\Http\Controllers;
- use App\DeliveryAppointmentCar;
- use App\Filters\ReceivingTaskFilters;
- use App\Http\Requests\Api\ReceivingTaskRequest;
- use App\ReceivingTask;
- use App\ReceivingTaskItem;
- use App\Services\OwnerService;
- use App\Services\ReceivingTaskService;
- use App\Warehouse;
- use Illuminate\Support\Facades\Gate;
- use Illuminate\Http\Request;
- class ReceivingTaskController extends Controller
- {
- public $service;
- public $ownerService;
- public function __construct(ReceivingTaskService $service, OwnerService $ownerService)
- {
- $this->service = $service;
- $this->ownerService = $ownerService;
- }
- public function index(Request $request, ReceivingTaskFilters $filter)
- {
- if (Gate::denies('入库管理-开单入库-查询')) {
- return redirect('/');
- }
- $receivingTasks = ReceivingTask::query()->with(['items', 'owner', 'wareHouse', 'file', 'deliveryAppointmentCar'])->filter($filter)->orderByDesc('created_at')->paginate(50);
- $owners = $this->ownerService->getQuery()->select('id', 'name')->get();
- $warehouses = Warehouse::query()->get();
- return view("store.receivingTasks.index", compact('receivingTasks', 'owners', 'warehouses'));
- }
- public function create()
- {
- if (Gate::denies('入库管理-开单入库-创建')) {
- return redirect('/');
- }
- $wareHouse = Warehouse::query()->get();
- $owners = $this->ownerService->getQuery()->select("id", "code", "name")->get();
- return view("store.receivingTasks.create", compact('wareHouse', 'owners'));
- }
- public function storeApi(ReceivingTaskRequest $request): array
- {
- if (Gate::denies('入库管理-开单入库-创建')) {
- return ['success' => false, 'message' => '没有对应权限'];
- }
- $appointment_number = $request->input('appointment_number', null);
- $delivery_appointment_car = DeliveryAppointmentCar::query()->with('deliveryAppointment')->where('appointment_number', $appointment_number)->first();
- if (!$delivery_appointment_car || !$delivery_appointment_car->deliveryAppointment) {
- return ['success' => false, 'errors' => ['appointment_number' => ['对应预约号未找到']]];
- }
- $ans_number_string = $delivery_appointment_car->deliveryAppointment->asn_number ?? '';
- $ans_numbers = array_filter(preg_split('/[,, ]+/is', $ans_number_string));
- if (count($ans_numbers) === 0 && count($request->input('asn_no',[])) == 0) {
- return ['success' => false, 'errors' => ['appointment_number' => ['预约号没有对应的Asn号']]];
- }
- $asn_nos = array_unique(array_merge($ans_numbers, $request->input('asn_nos') ?? []));
- if (count($asn_nos) == 0){
- return ['success' => false, 'errors' => ['appointment_number' => ['预约号没有对应的Asn号']]];
- }
- // $receiving_task_items = ReceivingTaskItem::query()->whereIn('asn_no',$request->input('asn_nos') ?? [])->get()->map(function($item){
- // return $item->asn_no;
- // })->toArray();
- // if (count($receiving_task_items) >0 ){
- // return [
- // 'success' => false,'errors' => ['appointment_number' => ['勾选的asn号'.json_encode($receiving_task_items).'已再其他开单入库中有关联']]
- // ];
- // }
- if ($delivery_appointment_car->deliveryAppointment->owner_id != $request->input('owner_id')) {
- return ['success' => false, 'errors' => ['appointment_number' => ['预约号与货主未必填项']]];
- }
- if (ReceivingTask::query()->where('delivery_appointment_car_id', $delivery_appointment_car->id)->exists()) {
- return ['success' => false, 'errors' => ['appointment_number' => ['预约号已有对应的任务']]];
- }
- try {
- $receiving_task = $this->service->createReceivingTask($delivery_appointment_car, $request->all());
- if (!$receiving_task->id) return ['success' => false, 'message' => '生成入库单任务失败,请重新尝试'];
- $receiving_task->loadMissing(['wareHouse', 'owner', 'deliveryAppointmentCar']);
- return ['success' => true, 'data' => $receiving_task];
- } catch (\Exception $e) {
- return ['success' => false, 'message' => '生成入库单任务失败,请重新尝试'];
- }
- }
- /**
- * 根据预约号获取Asn单号
- * @param Request $request
- * @return array
- */
- public function getAsnByAppointmentNumberApi(Request $request): array
- {
- $appointment_number = $request->input('appointment_number');
- if (!$appointment_number) {
- return ['success' => false, 'errors' => ['appointment_number' => '预约号不能为空']];
- }
- $delivery_appointment_car = DeliveryAppointmentCar::query()->with('deliveryAppointment')->where('appointment_number', $appointment_number)->first();
- if (ReceivingTask::query()->where('delivery_appointment_car_id', $delivery_appointment_car->id)->exists()) {
- return ['success' => false, 'errors' => ['appointment_number' => ['预约号已有对应的任务']]];
- }
- $ans_number_string = $delivery_appointment_car->deliveryAppointment->asn_number ?? '';
- $ans_numbers = array_filter(preg_split('/[,, ]+/is', $ans_number_string));
- return ['success' => true, 'data' => $ans_numbers];
- }
- }
|