From f36bb9d09092976d0105cc1f8a7ce48761a460cf Mon Sep 17 00:00:00 2001 From: omair saleh Date: Wed, 29 Dec 2021 00:43:20 +0800 Subject: [PATCH] YD curl request --- .../FetchOrderListsFromYdPortalProcessor.php | 269 +++++++++--------- 1 file changed, 137 insertions(+), 132 deletions(-) diff --git a/app/Classes/Modules/PackingLists/Processors/FetchOrderListsFromYdPortalProcessor.php b/app/Classes/Modules/PackingLists/Processors/FetchOrderListsFromYdPortalProcessor.php index 4821d4a7..cb36c1d7 100644 --- a/app/Classes/Modules/PackingLists/Processors/FetchOrderListsFromYdPortalProcessor.php +++ b/app/Classes/Modules/PackingLists/Processors/FetchOrderListsFromYdPortalProcessor.php @@ -137,164 +137,169 @@ class FetchOrderListsFromYdPortalProcessor */ public function execute(?Carbon $start = null, ?Carbon $end = null) { - db::beginTransaction(); + try { + db::beginTransaction(); - $start = $start ? $start : Carbon::now()->subMonths(2); + $start = $start ? $start : Carbon::now()->subMonths(2); - $startLimit = Carbon::parse('01-12-2021'); + $startLimit = Carbon::parse('01-12-2021'); - if($start->isBefore($startLimit)){ - $start = $startLimit; - } - - $end = $end ? $end : Carbon::now(); - - $orderRequest = $this->fetchesDataFRomYDPortal->clientRequest('http://www.yd-wl.com/api/GetOrderList.ashx', 'GET', [ - 'begintime' => $start->timestamp, - 'endtime' => $end->timestamp, - ]); - - $rows = $this->fetchesDataFRomYDPortal->getResponseBody($orderRequest); - - foreach($rows->data as $row){ - $containerReference = null; - $loadingDate = null; - $unstuffingDate = null; - $etd = null; - $eta = null; - - $trackingRequest = $this->fetchesDataFRomYDPortal->clientRequest('http://www.yd-wl.com/api/ApiTracking.ashx', 'GET', [ - 'trakingno' => $row->expressno - ]); - $rows = $this->fetchesDataFRomYDPortal->getResponseBody($trackingRequest); - foreach ($rows->data as $trackingRow) { - if($trackingRow->tracking === '货物已送达仓库准备入库中'){ - $receiveDate = Carbon::parse($trackingRow->trackingtime); - } - - if (strpos($trackingRow->tracking, '货物装柜完成。') !== false) { - $tracking = explode(':', $trackingRow->tracking); - $containerReference = explode('预计到港时间', $tracking[1])[0]; - $loadingDate = Carbon::parse($trackingRow->trackingtime); - $etd = Carbon::parse($tracking[2])->subDays(5); - $eta = Carbon::parse($tracking[2]); - } - - if($trackingRow->tracking === '货物已进目的港仓库'){ - $unstuffingDate = Carbon::parse($trackingRow->trackingtime); - } + if($start->isBefore($startLimit)){ + $start = $startLimit; } - $customerno = preg_split('/(-|\/)/', $row->customerno); + $end = $end ? $end : Carbon::now(); - if (!array_key_exists(2, $customerno)){ - if (!array_key_exists(1, $customerno)){ + $orderRequest = $this->fetchesDataFRomYDPortal->clientRequest('http://www.yd-wl.com/api/GetOrderList.ashx', 'GET', [ + 'begintime' => $start->timestamp, + 'endtime' => $end->timestamp, + ]); + + $rows = $this->fetchesDataFRomYDPortal->getResponseBody($orderRequest); + + foreach($rows->data as $row){ + $containerReference = null; + $loadingDate = null; + $unstuffingDate = null; + $etd = null; + $eta = null; + + $trackingRequest = $this->fetchesDataFRomYDPortal->clientRequest('http://www.yd-wl.com/api/ApiTracking.ashx', 'GET', [ + 'trakingno' => $row->expressno + ]); + $rows = $this->fetchesDataFRomYDPortal->getResponseBody($trackingRequest); + foreach ($rows->data as $trackingRow) { + if($trackingRow->tracking === '货物已送达仓库准备入库中'){ + $receiveDate = Carbon::parse($trackingRow->trackingtime); + } + + if (strpos($trackingRow->tracking, '货物装柜完成。') !== false) { + $tracking = explode(':', $trackingRow->tracking); + $containerReference = explode('预计到港时间', $tracking[1])[0]; + $loadingDate = Carbon::parse($trackingRow->trackingtime); + $etd = Carbon::parse($tracking[2])->subDays(5); + $eta = Carbon::parse($tracking[2]); + } + + if($trackingRow->tracking === '货物已进目的港仓库'){ + $unstuffingDate = Carbon::parse($trackingRow->trackingtime); + } + } + + $customerno = preg_split('/(-|\/)/', $row->customerno); + + if (!array_key_exists(2, $customerno)){ + if (!array_key_exists(1, $customerno)){ + continue; + } + $customerno[2] = substr($customerno[1], -9); + } + + + $orderNumber = $customerno[2]; + + try { + $order = $this->fetchesOrder->execute(['reference' => $orderNumber]); + } catch (ResourceNotFoundException $exception) { continue; } - $customerno[2] = substr($customerno[1], -9); - } + $packingListReference = $row->expressno; - $orderNumber = $customerno[2]; - - try { - $order = $this->fetchesOrder->execute(['reference' => $orderNumber]); - } catch (ResourceNotFoundException $exception) { - continue; - } - - $packingListReference = $row->expressno; - - try{ - $packingList = $this->fetchesPackingList->execute(['reference' => $row->expressno]); - } catch (ResourceNotFoundException $exception){ - - $warehouseReceiveObject = new PackingListObject($packingListReference, $order->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_FREIGHT_FORWARDER)->first()->appointee->id, PackingListType::WAREHOUSE_RECEIVE_LIST, ApprovalStatus::APPROVED); - /** @var PackingList $warehouseReceiveList */ - $warehouseReceiveList = $this->createPackingListProcessor->execute($warehouseReceiveObject, $order); - $transportObject = new TransportObject(TransportType::LAND, null, $row->kuaidilist, Carbon::parse($receiveDate), Carbon::parse($receiveDate), ApprovalStatus::APPROVED); - $this->createsTransport->execute($transportObject, $warehouseReceiveList); - - $contract = $this->unityCreateContract->execute(); - $contractReference = $contract->hash_id; - $contractObligations = $contract->contract_obligation_list; - - $this->unityActivateContract->execute($contractReference); - - $supervisorHashId = $order->orderRoles()->where('role_id', '=', OrderRoleTypes::SUPERVISOR)->first()->appointee->unity_hash_id; - - $supervisorContractEntity = $this->unityCreateContractEntity->execute($contractReference, $supervisorHashId); - $order->orderRoles()->where('role_id', '=', OrderRoleTypes::SUPERVISOR)->first()->update(['entity_hash_id' => $supervisorContractEntity->hash_id, 'entity_signature' => $supervisorContractEntity->entity_signature_hash_id]); - - $importerContractEntity = $this->unityCreateContractEntity->execute($contractReference, $order->orderRoles()->where('role_id', '=', OrderRoleTypes::IMPORTER)->first()->appointee->unity_hash_id); - $order->orderRoles()->where('role_id', '=', OrderRoleTypes::IMPORTER)->first()->update(['entity_hash_id' => $importerContractEntity->hash_id, 'entity_signature' => $importerContractEntity->entity_signature_hash_id]); - - $this->unityAssignContractEntity->execute($supervisorContractEntity->hash_id, $contractObligations); - - $packingListObject = new PackingListObject($packingListReference, $order->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_FREIGHT_FORWARDER)->first()->appointee->id, PackingListType::SHIPPING_PACKING_LIST, ApprovalStatus::PENDING_VERIFICATION , $contractReference); - - /** @var PackingList $packingList */ - $packingList = $this->createPackingListProcessor->execute($packingListObject, $order); - - foreach($contractObligations as $obligation) { - $stepObject = new StepsObject($order->orderRoles()->where('role_id', '=', OrderRoleTypes::SUPERVISOR)->first()->appointee->id, $obligation->reference, $obligation->sequence, $obligation->hash_id); - $this->createsStep->execute($packingList, $stepObject); - } - - foreach($row->deliverysize as $package) { - $packageObject = new PackageObject(PackageType::CARTON, $row->goodname, (float) $package->width, (float) $package->height, (float) $package->length, 0, (float) $package->num, ApprovalStatus::APPROVED); - $this->createPackageProcessor->execute($packageObject, $warehouseReceiveList); - $this->createPackageProcessor->execute($packageObject, $packingList); - } - } - - if($containerReference) { - try { - $container = $this->fetchesContainer->execute(['reference' => $containerReference]); + try{ + $packingList = $this->fetchesPackingList->execute(['reference' => $row->expressno]); } catch (ResourceNotFoundException $exception){ - $originWarehouse = $this->fetchesCompanyModule->execute(['id' => $order->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_WAREHOUSE)->first()->appointee->id]); - $containerObject = new ContainerObject($containerReference, '', '', ContainerTypes::FORTY_FEET_DRY_CONTAINER, $loadingDate, ApprovalStatus::PENDING_VERIFICATION); - /** @var Container $container */ - $container = $this->createContainerProcessor->execute($containerObject, $originWarehouse); - $container->packingLists()->attach($packingList); + $warehouseReceiveObject = new PackingListObject($packingListReference, $order->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_FREIGHT_FORWARDER)->first()->appointee->id, PackingListType::WAREHOUSE_RECEIVE_LIST, ApprovalStatus::APPROVED); + /** @var PackingList $warehouseReceiveList */ + $warehouseReceiveList = $this->createPackingListProcessor->execute($warehouseReceiveObject, $order); + $transportObject = new TransportObject(TransportType::LAND, null, $row->kuaidilist, Carbon::parse($receiveDate), Carbon::parse($receiveDate), ApprovalStatus::APPROVED); + $this->createsTransport->execute($transportObject, $warehouseReceiveList); - $transport = $container->transports()->first(); + $contract = $this->unityCreateContract->execute(); + $contractReference = $contract->hash_id; + $contractObligations = $contract->contract_obligation_list; - if(!$transport){ - $transportObject = new TransportObject(TransportType::SEA, null, null, $etd, null, ApprovalStatus::APPROVED); - /** @var Transport $transport */ - $transport = $this->createsTransport->execute($transportObject, $container); - $this->createsSchedule->execute($transport, new ScheduleObject($etd, $eta, ApprovalStatus::APPROVED)); - } - } + $this->unityActivateContract->execute($contractReference); - if($unstuffingDate){ - $container->update(['status' => ApprovalStatus::COMPLETED]); - $container->transports()->first()->update(['drop_date' => $unstuffingDate, 'status' => ApprovalStatus::COMPLETED]); + $supervisorHashId = $order->orderRoles()->where('role_id', '=', OrderRoleTypes::SUPERVISOR)->first()->appointee->unity_hash_id; + + $supervisorContractEntity = $this->unityCreateContractEntity->execute($contractReference, $supervisorHashId); + $order->orderRoles()->where('role_id', '=', OrderRoleTypes::SUPERVISOR)->first()->update(['entity_hash_id' => $supervisorContractEntity->hash_id, 'entity_signature' => $supervisorContractEntity->entity_signature_hash_id]); + + $importerContractEntity = $this->unityCreateContractEntity->execute($contractReference, $order->orderRoles()->where('role_id', '=', OrderRoleTypes::IMPORTER)->first()->appointee->unity_hash_id); + $order->orderRoles()->where('role_id', '=', OrderRoleTypes::IMPORTER)->first()->update(['entity_hash_id' => $importerContractEntity->hash_id, 'entity_signature' => $importerContractEntity->entity_signature_hash_id]); + + $this->unityAssignContractEntity->execute($supervisorContractEntity->hash_id, $contractObligations); + + $packingListObject = new PackingListObject($packingListReference, $order->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_FREIGHT_FORWARDER)->first()->appointee->id, PackingListType::SHIPPING_PACKING_LIST, ApprovalStatus::PENDING_VERIFICATION , $contractReference); /** @var PackingList $packingList */ - foreach($container->packingLists as $packingList){ + $packingList = $this->createPackingListProcessor->execute($packingListObject, $order); - if($packingList->status === ApprovalStatus::PENDING_VERIFICATION){ - $packingList->status = ApprovalStatus::APPROVED; - $packingList->save(); - } + foreach($contractObligations as $obligation) { + $stepObject = new StepsObject($order->orderRoles()->where('role_id', '=', OrderRoleTypes::SUPERVISOR)->first()->appointee->id, $obligation->reference, $obligation->sequence, $obligation->hash_id); + $this->createsStep->execute($packingList, $stepObject); + } - $signature = $packingList->owner->orderRoles()->where('role_id', '=', OrderRoleTypes::SUPERVISOR)->first()->appointee->entity_sigiture; - foreach($packingList->steps()->where('reference', '!=', 'DELIVERY')->get() as $step){ - $this->updatesContractObligations->execute($signature, $step->obligation_hash_id); - $step->update(['status' => ApprovalStatus::COMPLETED]); + foreach($row->deliverysize as $package) { + $packageObject = new PackageObject(PackageType::CARTON, $row->goodname, (float) $package->width, (float) $package->height, (float) $package->length, 0, (float) $package->num, ApprovalStatus::APPROVED); + $this->createPackageProcessor->execute($packageObject, $warehouseReceiveList); + $this->createPackageProcessor->execute($packageObject, $packingList); + } + } + + if($containerReference) { + try { + $container = $this->fetchesContainer->execute(['reference' => $containerReference]); + } catch (ResourceNotFoundException $exception){ + $originWarehouse = $this->fetchesCompanyModule->execute(['id' => $order->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_WAREHOUSE)->first()->appointee->id]); + $containerObject = new ContainerObject($containerReference, '', '', ContainerTypes::FORTY_FEET_DRY_CONTAINER, $loadingDate, ApprovalStatus::PENDING_VERIFICATION); + /** @var Container $container */ + $container = $this->createContainerProcessor->execute($containerObject, $originWarehouse); + + $container->packingLists()->attach($packingList); + + $transport = $container->transports()->first(); + + if(!$transport){ + $transportObject = new TransportObject(TransportType::SEA, null, null, $etd, null, ApprovalStatus::APPROVED); + /** @var Transport $transport */ + $transport = $this->createsTransport->execute($transportObject, $container); + $this->createsSchedule->execute($transport, new ScheduleObject($etd, $eta, ApprovalStatus::APPROVED)); } } + + if($unstuffingDate){ + $container->update(['status' => ApprovalStatus::COMPLETED]); + $container->transports()->first()->update(['drop_date' => $unstuffingDate, 'status' => ApprovalStatus::COMPLETED]); + + /** @var PackingList $packingList */ + foreach($container->packingLists as $packingList){ + + if($packingList->status === ApprovalStatus::PENDING_VERIFICATION){ + $packingList->status = ApprovalStatus::APPROVED; + $packingList->save(); + } + + $signature = $packingList->owner->orderRoles()->where('role_id', '=', OrderRoleTypes::SUPERVISOR)->first()->appointee->entity_sigiture; + foreach($packingList->steps()->where('reference', '!=', 'DELIVERY')->get() as $step){ + $this->updatesContractObligations->execute($signature, $step->obligation_hash_id); + $step->update(['status' => ApprovalStatus::COMPLETED]); + } + } + } + + } } - - + db::commit(); + } catch (\Exception $exception) { + dd($exception); } - db::commit(); + } }