'Created Order', 'message' => 'You have successfully created a new Order' ]; } /** @var FetchesCompany */ private $fetchesCompany; /** @var fetchesAddress */ private $fetchesAddress; /** @var CreateOrderProcessor */ private $createOrderProcessor; /** @var FetchesCompanyModule */ private $fetchesCompanyModule; /** @var GeneratesOrderNumber */ private $generatesOrderNumber; /** @var CreatesAddress */ private $createsAddress; /** @var CanCreateAddress */ private $canCreateAddress; /** @var NewLeadTaskToPerfexCRMProcessor */ private $newLeadTaskToPerfexCRMProcessor; /** @var ConnectOrderToExchangeBooking */ private $connectOrderToExchangeBooking; /** @var ConnectOrderToExchangeBookingOnExchangeProcessor */ private $connectOrderToExchangeBookingOnExchangeProcessor; /** @var CreatesKeyValuePair */ private $createsKeyValuePair; /** * CreateOrderLogic constructor. * @param FetchesCompany $fetchesCompany * @param FetchesAddress $fetchesAddress * @param CreateOrderProcessor $createOrderProcessor * @param FetchesCompanyModule $fetchesCompanyModule * @param GeneratesOrderNumber $generatesOrderNumber * @param NewLeadTaskToPerfexCRMProcessor $newLeadTaskToPerfexCRMProcessor * @param ConnectOrderToExchangeBooking $connectOrderToExchangeBooking * @param ConnectOrderToExchangeBookingOnExchangeProcessor $connectOrderToExchangeBookingOnExchangeProcessor */ public function __construct(FetchesCompany $fetchesCompany, FetchesAddress $fetchesAddress, CreateOrderProcessor $createOrderProcessor, FetchesCompanyModule $fetchesCompanyModule, GeneratesOrderNumber $generatesOrderNumber, NewLeadTaskToPerfexCRMProcessor $newLeadTaskToPerfexCRMProcessor, ConnectOrderToExchangeBooking $connectOrderToExchangeBooking, ConnectOrderToExchangeBookingOnExchangeProcessor $connectOrderToExchangeBookingOnExchangeProcessor, CreatesKeyValuePair $createsKeyValuePair) { $this->fetchesCompany = $fetchesCompany; $this->fetchesAddress = $fetchesAddress; $this->createOrderProcessor = $createOrderProcessor; $this->fetchesCompanyModule = $fetchesCompanyModule; $this->generatesOrderNumber = $generatesOrderNumber; $this->newLeadTaskToPerfexCRMProcessor = $newLeadTaskToPerfexCRMProcessor; $this->connectOrderToExchangeBooking = $connectOrderToExchangeBooking; $this->connectOrderToExchangeBookingOnExchangeProcessor = $connectOrderToExchangeBookingOnExchangeProcessor; $this->createsKeyValuePair = $createsKeyValuePair; } public function logic(Request $request): JsonResponse { $isTermsAgreed = $request->input('is_terms_agree'); if(!$isTermsAgreed){ throw new RequestValidationException('You must read and agree to our terms and conditions to proceed'); } $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]); $address = $this->fetchesAddress->execute(['id' => $request->input('address_id')]); // todo-new: fix this // if select warehouse foshan if ($request->input('warehouse_id') === 'foshan') { // 13 - sabah, 14 - sarawak if (in_array($address->state->id, [13, 14])) { $originWarehouse = $this->fetchesCompanyModule->execute(['reference' => WarehouseReferences::YD_FS_WEST_MALAYSIA]); } else { $originWarehouse = $this->fetchesCompanyModule->execute(['reference' => WarehouseReferences::YD_FS_WEST_MALAYSIA]); } } else { $originWarehouse = $this->fetchesCompanyModule->execute(['id' => $request->input('warehouse_id')]); $originWarehouse = $this->fetchesCompanyModule->execute(['reference' => $originWarehouse->reference === WarehouseReferences::VT_GUANG_ZHOU ? WarehouseReferences::YD_GUANG_ZHOU : WarehouseReferences::YD_YIWU]); // if (!in_array($company->companyModules()->importers()->first()->id, WarehouseReferences::YD_EXEMPT_LIST)) } if($originWarehouse->reference === WarehouseReferences::YD_YIWU && in_array($address->state_id, [5, 13, 14])) { throw new RequestValidationException('Our Yiwu warehouse is unable to ship goods to Sabah & Sarawak at the moment. you can select our Guangzhou warehouse as an alternative.'); } /** @var Order $order */ $order = $this->createOrderProcessor->execute($company, $originWarehouse, $address, $this->generatesOrderNumber->execute()); $keyValuePairObject = new KeyValuePairObject( "TERMS_AND_CONDITIONS", $isTermsAgreed ); $this->createsKeyValuePair->execute($order, $keyValuePairObject); if(config('perfexcrm.is_enabled') == 'true'){ $this->newLeadTaskToPerfexCRMProcessor->execute($company); } $token = explode('.', $request->bearerToken())[1]; $decodedToken = base64_decode($token); $exchangeUser = json_decode($decodedToken, true)['user']; $exchangeBookingId = null; if (isset($exchangeUser['exchange_booking_id']) && $exchangeUser['exchange_booking_id']) { $exchangeBookingId = $exchangeUser['exchange_booking_id']; } if ($exchangeBookingId) { $this->connectOrderToExchangeBooking->execute($order, $exchangeBookingId); $this->connectOrderToExchangeBookingOnExchangeProcessor->execute($request, $order->id, $exchangeBookingId); } return $this->resourceResponse(new OrderResource($order)); } }