From 2650da3ea061306ef1f01bf4fe2ebb24cb1dd935 Mon Sep 17 00:00:00 2001 From: aahmetgaliev Date: Fri, 6 Sep 2019 16:56:46 +0300 Subject: [PATCH] new documents added --- src/CorreosConnector/CorreosConnector.php | 84 ++++++++++++++++- src/Factories/Shipment.php | 4 +- tests/CancelShipmentTest.php | 24 ++++- tests/InitTest.php | 21 ++--- tests/PrintLabelTest.php | 43 +++++++-- tests/UpdateShipmentTest.php | 109 +++++++++++++++++++--- 6 files changed, 244 insertions(+), 41 deletions(-) diff --git a/src/CorreosConnector/CorreosConnector.php b/src/CorreosConnector/CorreosConnector.php index ef83e28..4c154ed 100644 --- a/src/CorreosConnector/CorreosConnector.php +++ b/src/CorreosConnector/CorreosConnector.php @@ -2,6 +2,10 @@ namespace CorreosSdk\CorreosConnector; +use CorreosSdk\ServiceType\Documentacion; +use CorreosSdk\StructType\SolicitudDocumentacionAduanera; +use CorreosSdk\StructType\SolicitudDocumentacionAduaneraCN23CP71; +use CorreosSdk\StructType\SolicitudEtiquetaExp; use InvalidArgumentException; use CorreosSdk\ServiceType\Anular; use CorreosSdk\ServiceType\Modificar; @@ -114,13 +118,85 @@ public function printLabel(string $trackNumber, \DateTime $shipDateRequest = nul $response = $documentationService->SolicitudEtiquetaOp($labelData); - if($response->getResultado() == 1) { + if ($response->getResultado() == 1) { throw new CorreosException("Shipment with track number " . $trackNumber . " not found"); } return !empty($response->Bulto) ? $response->Bulto->Etiqueta->Etiqueta_pdf->Fichero : ""; } + /** + * @param string $trackNumber + * @param \DateTime $shipDateRequest + * @return string + * @throws CorreosException + */ + public function printExpeditionLabel(string $trackNumber, \DateTime $shipDateRequest = null): string + { + $documentationService = new Solicitud($this->correosConfig->getOptions()); + + $labelData = new SolicitudEtiquetaExp( + !empty($shipDateRequest) ? $shipDateRequest->format('d-m-y H:i:s') : null, + $this->correosConfig->getClientCode(), + $this->correosConfig->getClientContractNumber(), + $this->correosConfig->getClientNumber(), + $trackNumber, + $this->correosConfig->getCare(), + self::PDF_TYPE_REQUEST + ); + $response = $documentationService->SolicitudEtiquetaExpOp($labelData); + + return !empty($response->Bulto) ? $response->Bulto->Etiqueta->Etiqueta_pdf->Fichero : ""; + } + + + /** + * + * @param string $type + * @param string $countryIso + * @param string $numberOfShippings + * @param string $companyName + * @param string $province + * @return string|null + */ + public function printCustomsDocument(string $type, string $countryIso, string $numberOfShippings, string $companyName, string $province = ''): ?string + { + $documentationService = new Documentacion($this->correosConfig->getOptions()); + + $documentData = new SolicitudDocumentacionAduanera( + $type, + $this->correosConfig->getClientContractNumber(), + $this->correosConfig->getClientNumber(), + $this->correosConfig->getClientCode(), + $province, + $countryIso, + $companyName, + $numberOfShippings + ); + + $response = $documentationService->DocumentacionAduaneraOp($documentData); + + return !empty($response->Fichero) ? $response->Fichero : ""; + + } + + /** + * @param $trackNumber + * @return string + */ + public function printCustomDocumentCN23CP71(string $trackNumber) + { + $documentationService = new Documentacion($this->correosConfig->getOptions()); + + $documentData = new SolicitudDocumentacionAduaneraCN23CP71( + $trackNumber + ); + + $response = $documentationService->DocumentacionAduaneraCN23CP71Op($documentData); + + return !empty($response->Fichero) ? $response->Fichero : ""; + } + /** * @param string $trackNumber * @return bool @@ -144,15 +220,16 @@ public function cancelShipment(string $trackNumber): bool /** * @param Shipment $invoice + * @param string $trackNumber * @return Shipment * @throws CorreosException */ - public function updateShipment(Shipment $invoice): Shipment + public function updateShipment(Shipment $invoice, string $trackNumber): Shipment { try { $updateService = new Modificar($this->correosConfig->getOptions()); $updateData = new PeticionModificar( - $invoice->getClippedTrackNumber(), + $trackNumber, $invoice->getDateRequest()->format('d-m-y H:i:s'), $this->correosConfig->getClientCode(), $this->correosConfig->getClientContractNumber(), @@ -164,7 +241,6 @@ public function updateShipment(Shipment $invoice): Shipment $invoice->getReceiverUnitedIdentity()->buildUpdateReceiverIdentity(), $invoice->getSendingContent()->buildUpdateSendingContent() ); - $response = $updateService->ModificarOp($updateData); $invoice->setResponse($response); diff --git a/src/Factories/Shipment.php b/src/Factories/Shipment.php index cc1f584..72d8c95 100644 --- a/src/Factories/Shipment.php +++ b/src/Factories/Shipment.php @@ -107,7 +107,7 @@ public function setDateRequest(\DateTime $dateRequest): void { $preparedDateTime = $this->setZerosToSeconds($dateRequest); - $this->dateRequest = $preparedDateTime; + $this->dateRequest = $dateRequest; } /** @@ -127,7 +127,7 @@ private function setZerosToSeconds(\DateTime $dateTime) : \DateTime * For printing labels need cut last character * @return string */ - public function getClippedTrackNumber(): string + public function getClippedTrackNumber(): ?string { return substr($this->trackNumber, 0, -1); } diff --git a/tests/CancelShipmentTest.php b/tests/CancelShipmentTest.php index 35ea9c0..d4be6a8 100644 --- a/tests/CancelShipmentTest.php +++ b/tests/CancelShipmentTest.php @@ -4,8 +4,14 @@ namespace CorreosSdk\Tests; +use CorreosSdk\CorreosConnector\CorreosConfig; +use CorreosSdk\CorreosConnector\CorreosConnector; use CorreosSdk\Exceptions\CorreosException; +use CorreosSdk\Factories\Address; +use CorreosSdk\Factories\Identification; +use CorreosSdk\Factories\SenderUnitedIdentity; use CorreosSdk\StructType\PeticionAnular; +use Matomo\Ini\IniReader; class CancelShipmentTest extends InitTest { @@ -17,7 +23,7 @@ public function testCancelShipment() $shipment = $this->createShipment(); $trackNumber = $shipment->getClippedTrackNumber(); - + echo "track number =" . $trackNumber; $isCancelShipment = $this->client->cancelShipment($trackNumber); $this->assertTrue($isCancelShipment); @@ -35,4 +41,20 @@ public function testFailedCancel() $this->expectException(CorreosException::class); } + + public function testProdCancel() + { + +// $shipment = $this->createShipment(); + +// print_r($shipment->getResponse()); + +// echo $trackNumber = $shipment->getClippedTrackNumber(); + +// $isCancelShipment = $this->client->cancelShipment("LX504912338ES"); + +// $this->assertTrue($isCancelShipment); + + } + } \ No newline at end of file diff --git a/tests/InitTest.php b/tests/InitTest.php index a13e001..100e296 100644 --- a/tests/InitTest.php +++ b/tests/InitTest.php @@ -37,21 +37,20 @@ public function __construct($name = null, array $data = array(), $dataName = '') $config['sender_city_name'], $config['sender_street_name'], $config['sender_province_name'], - $config['sender_street_number'] + $config['sender_street_number'], + null, + "C", + null, + null, + $config['sender_street_floor'] ); $senderIdentification = new Identification( - $config['sender_name'], - $config['sender_first_name'], - $config['sender_second_name'], - $config['sender_company_name'], $config['sender_name'] ); $senderUnitedIdentity = new SenderUnitedIdentity( $senderAddress, $senderIdentification, - $config['sender_postcode'], - $config['sender_phone'], - $config['sender_email'] + $config['sender_post_code'] ); $this->client = new CorreosConnector($correosConfig, $senderUnitedIdentity); @@ -68,11 +67,7 @@ protected function createShipment() : Shipment "20" ); $receiverIdentity = new Identification( - "TEST TEST", - null, - null, - null, - null + "TEST TEST" ); $receiverUnitedIdentity = new ReceiverUnitedIdentity( $receiverAddress, diff --git a/tests/PrintLabelTest.php b/tests/PrintLabelTest.php index 6d2fc9c..4fb31a2 100644 --- a/tests/PrintLabelTest.php +++ b/tests/PrintLabelTest.php @@ -3,14 +3,7 @@ namespace CorreosSdk\Tests; - -use CorreosSdk\CorreosConnector\CorreosConfig; -use CorreosSdk\CorreosConnector\CorreosConnector; use CorreosSdk\Exceptions\CorreosException; -use CorreosSdk\Factories\Address; -use CorreosSdk\Factories\Identification; -use CorreosSdk\Factories\SenderUnitedIdentity; -use Matomo\Ini\IniReader; class PrintLabelTest extends InitTest { @@ -28,7 +21,6 @@ public function testPrinting() $labelPdfByteCode = $this->client->printLabel($trackNumber, null); - $this->assertTrue(strlen($labelPdfByteCode) > 3); } @@ -42,8 +34,6 @@ public function testfailedPrinting() $trackNumber = $shipment->getTrackNumber(); - $dateTime = $shipment->getDateRequest(); - $dateTime = new \DateTime('tomorrow'); $labelPdfByteCode = $this->client->printLabel($trackNumber, $dateTime); @@ -54,4 +44,37 @@ public function testfailedPrinting() } + public function testPrintCustomDocument() + { + + $typeDocument = "DDP"; // DCAF + $countryIso = "RU"; + $numberOfShipments = "2"; + $companyName = "test"; + + $documentCode = $this->client->printCustomsDocument($typeDocument, $countryIso, $numberOfShipments, $companyName); + + $this->assertTrue(strlen($documentCode) > 3); + } + + public function testCustomDocumentCN23CP71() + { + $invoice = $this->createShipment(); + + $trackNumber = $invoice->getClippedTrackNumber(); + + $documentCode = $this->client->printCustomDocumentCN23CP71($trackNumber); + + $this->assertTrue(strlen($documentCode) > 3); + + } + + public function testExpeditionPrint() + { + $invoice = $this->createShipment(); + + $trackNumber = $invoice->getTrackNumber(); + +// $labelPdfByteCode = $this->client->printExpeditionLabel($trackNumber, $invoice->getDateRequest()); + } } \ No newline at end of file diff --git a/tests/UpdateShipmentTest.php b/tests/UpdateShipmentTest.php index 2a7df25..ddddca3 100644 --- a/tests/UpdateShipmentTest.php +++ b/tests/UpdateShipmentTest.php @@ -3,7 +3,6 @@ namespace CorreosSdk\Tests; - use CorreosSdk\Factories\Address; use CorreosSdk\Factories\Identification; use CorreosSdk\Factories\Shipment; @@ -13,18 +12,47 @@ use CorreosSdk\Factories\ReceiverUnitedIdentity; use CorreosSdk\Factories\SendingContent; use CorreosSdk\Factories\SendingInsides; +use Matomo\Ini\IniReader; class UpdateShipmentTest extends InitTest { public function testUpdateShipment() { - $createdShipment = $this->createShipment(); - $totalPrice = 60000; +// $config = (new IniReader())->readFile(__DIR__ . '/config.ini'); +// $correosConfig = new CorreosConfig( +// "w81099174", +// "6pjUqAw4", +// "69RH", +// 'prod'); +// $correosConfig->setClientContractNumber("54034824"); +// $correosConfig->setClientNumber("81099174"); +// +// $senderAddress = new Address( +// $config['sender_city_name'], +// $config['sender_street_name'], +// $config['sender_province_name'], +// $config['sender_street_number'], +// null, +// "C", +// null, +// null, +// $config['sender_street_floor'] +// ); +// $senderIdentification = new Identification( +// $config['sender_name'] +// ); +// $senderUnitedIdentity = new SenderUnitedIdentity( +// $senderAddress, +// $senderIdentification, +// $config['sender_post_code'] +// ); +// +// $this->client = new CorreosConnector($correosConfig, $senderUnitedIdentity); $receiverAddress = new Address( - "TEST CITY NAME", + "TEST LAKEPORT", "TEST STREET NAME", - "MADRIDO", + "TEST NAME", "20" ); $receiverIdentity = new Identification( @@ -33,10 +61,69 @@ public function testUpdateShipment() $receiverUnitedIdentity = new ReceiverUnitedIdentity( $receiverAddress, $receiverIdentity, - "42300", // must be less than < 6 + "48059", // must be less than < 6 "US", - "ainur_ahmetgalie@mail.ru" + "ainur_ahmetgalie@mail.ru", + null, + null + ); + + $product = new ProductDescription( + '1', + '189', + '200', + '96000' + ); + $productList = new ProductList(); + $productList->addProduct($product); + + $sendingInsides = new SendingInsides( + SendingInsides::GOODS_CONTENT_TYPE, // GOODS, + SendingInsides::YES_CHOICE, // Y + SendingInsides::YES_CHOICE, + $productList, + SendingInsides::NO_CHOICE + ); + $packageSize = new PackageSize( + 15, + 15, + 15, + 500 + ); + + $sendingContent = new SendingContent( + SendingContent::PAQ_LIGHT_INTERNATIONAL_TARIFF, // PAQ LIGHT INTERNATIONAL(I) + SendingContent::POSTAGE_PAID_PAYMENT_TYPE, + SendingContent::STANDARD_DELIVERY_MODE, + $packageSize, + $sendingInsides + ); + + $sendingContent->setCustomerShipmentCode("test order: 123456"); + + $shipment = new Shipment($receiverUnitedIdentity, $sendingContent); + + $createdShipment = $this->client->createShipment($shipment); + + $totalPrice = 60000; + $receiverAddress = new Address( + "TEST LAKEPORT", + "TEST STREET NAME", + "TEST NAME", + "20" + ); + $receiverIdentity = new Identification( + "TEST TEST" + ); + $receiverUnitedIdentity = new ReceiverUnitedIdentity( + $receiverAddress, + $receiverIdentity, + "48059", // must be less than < 6 + "US", + "ainur_ahmetgalie@mail.ru", + null, + null ); $product = new ProductDescription( @@ -51,9 +138,9 @@ public function testUpdateShipment() $sendingInsides = new SendingInsides( SendingInsides::GOODS_CONTENT_TYPE, // GOODS, SendingInsides::YES_CHOICE, // Y - $totalPrice > 50000 ? SendingInsides::YES_CHOICE : null, + SendingInsides::YES_CHOICE, $productList, - $totalPrice > 50000 ? SendingInsides::NO_CHOICE : null + SendingInsides::NO_CHOICE ); $packageSize = new PackageSize( 15, @@ -71,10 +158,10 @@ public function testUpdateShipment() ); $shipment = new Shipment($receiverUnitedIdentity, $sendingContent); - $shipment->setTrackNumber($createdShipment->getTrackNumber()); + $shipment->setDateRequest($createdShipment->getDateRequest()); - $shipment = $this->client->updateShipment($shipment); + $shipment = $this->client->updateShipment($shipment, $createdShipment->getClippedTrackNumber()); $responseUpdate = $shipment->getResponse();