From 52812c0398b00ae35e1c12bb8bb736fd09c40f13 Mon Sep 17 00:00:00 2001 From: aahmetgaliev Date: Wed, 24 Jul 2019 14:20:30 +0300 Subject: [PATCH] added new methods --- README.md | 71 +++++----- src/Contracts/DeliveryConnector.php | 20 ++- src/PickPoint/PickPointConnector.php | 202 ++++++++++++++++++++++----- tests/CheckInvoiceStatusTest.php | 53 +++++++ tests/PrintLabelsAndReestrsTest.php | 80 +++++++++++ 5 files changed, 357 insertions(+), 69 deletions(-) create mode 100644 tests/CheckInvoiceStatusTest.php create mode 100644 tests/PrintLabelsAndReestrsTest.php diff --git a/README.md b/README.md index 6367057..ae96195 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,9 @@ API PICKPOINT CONNECTOR ```bash composer require sch-group/pickpoint ``` -Example +Примеры: +``` $config = [ 'host' => '', 'login' => '', @@ -20,8 +21,10 @@ $client = new PickPointConnector(new PickPointConf($config), $senderDestination, $points = $client->getPoints(); // получить массив поставматов -// Цены за доставку +``` +Цены за доставку +``` $receiverDestination = new ReceiverDestination('Санкт-Петербург', 'Ленинградская обл.'); $prices = $client->calculatePrices($receiverDestination); // вернет массив с ценами и тарифами @@ -29,56 +32,60 @@ $prices = $client->calculatePrices($receiverDestination); // вернет мас $tariffPrice = $client->calculateObjectedPrices($receiverDestination); // Вернет объект с ценами $commonStandardPrice = $tariffPrice->getStandardCommonPrice(); // получить общую цену с тарифом стандарт - -// Создание отправления - +``` +Создание отправления +``` $invoice = new Invoice(); - $invoice->setSenderCode('order: 123456'); - $invoice->setPostamatNumber('5602-009'); - $invoice->setDescription('Custom zakaz'); - $invoice->setRecipientName('Айнур'); - $invoice->setMobilePhone('+79274269594'); - $invoice->setEmail('ainur_ahmetgalie@mail.ru'); - -$invoice->setPostageType('unpiad'); - -$invoice->setGettingType('sc'); - +$invoice->setPostageType('unpiad'); // paid or unpaid +$invoice->setGettingType('sc'); // courier or sc $invoice->setSum(500.00); - -$invoice->setDeliveryMode('standard'); - +$invoice->setDeliveryMode('standard'); // stanadard or priority $packageSize = new PackageSize(20, 20, 20); - $invoice->setPackageSize($packageSize); - $product = new Product(); - $product->setDescription('Test product'); - $product->setPrice(200); - $product->setQuantity(1); - $product->setName('Tovar 1'); - $product->setProductCode('1231'); - $invoice->setProducts([$product]); - $address = new Address(); - $address->setCityName('Казань'); - $address->setPhoneNumber('+79274269594'); - $invoice->setClientReturnAddress($address); +$response = $client->createShipment($invoice); +``` +Печать наклейки +``` +$invoice = $client->createShipmentWithInvoice($invoice); +$invoiceNumber = $invoice->getInvoiceNumber(); +$pdfByteCode = $client->printLabel(array($invoiceNumber)); +``` +Создание реестра и печать +``` +$invoice = $client->createShipmentWithInvoice($invoice); +$invoiceNumber = $invoice->getInvoiceNumber(); + +$reestr = $client->makeReceipt(array($invoiceNumber)); +$pdfByteCode = $client->rintReceipt($reestr[0]); -$response = $this->client->createShipment($invoice); +``` +Одновременное создание реестра и печать +``` +$invoice = $client->createShipmentWithInvoice($invoice); +$invoiceNumber = $invoice->getInvoiceNumber(); +$pdfByteCode = $this->client->makeReceiptAndPrint(array($invoiceNumber)); +``` +Проверка статуса отправлений + +``` +$invoiceNumber = $invoice->getInvoiceNumber(); +$status = $client->getStatus($invoiceNumber); +``` \ No newline at end of file diff --git a/src/Contracts/DeliveryConnector.php b/src/Contracts/DeliveryConnector.php index 508b85e..7c02838 100644 --- a/src/Contracts/DeliveryConnector.php +++ b/src/Contracts/DeliveryConnector.php @@ -19,16 +19,23 @@ public function getPoints(); /** * Returns invoice data and create shipment/order in delivery service * @param Invoice $invoice + * @param bool $returnInvoiceNumberOnly * @return mixed */ public function createShipment(Invoice $invoice); + /** + * @param Invoice $invoice + * @return mixed + */ + public function createShipmentWithInvoice(Invoice $invoice) : Invoice; /** * Returns current delivery status * @param string $invoiceNumber + * @param string $orderNumber * @return mixed */ - public function getStatus(string $invoiceNumber); + public function getStatus(string $invoiceNumber, string $orderNumber = ''); /** * @param string $invoiceNumber @@ -58,6 +65,17 @@ public function calculateObjectedPrices(ReceiverDestination $receiverDestination */ public function printLabel(array $invoiceNumbers); + /** + * @param array $invoiceNumbers + * @return mixed + */ + public function makeReceipt(array $invoiceNumbers); + + /** + * @param array $invoiceNumbers + * @return mixed + */ + public function makeReceiptAndPrint(array $invoiceNumbers); /** * Print reestr/receipt * @param string $identifier diff --git a/src/PickPoint/PickPointConnector.php b/src/PickPoint/PickPointConnector.php index 1464c2e..4437d73 100644 --- a/src/PickPoint/PickPointConnector.php +++ b/src/PickPoint/PickPointConnector.php @@ -106,7 +106,7 @@ public function getPoints() * @return mixed * @throws PickPointMethodCallException */ - public function calculatePrices(ReceiverDestination $receiverDestination, SenderDestination $senderDestination = null, PackageSize $packageSize = null) : array + public function calculatePrices(ReceiverDestination $receiverDestination, SenderDestination $senderDestination = null, PackageSize $packageSize = null): array { $url = $this->pickPointConf->getHost() . '/calctariff'; /** @@ -119,17 +119,17 @@ public function calculatePrices(ReceiverDestination $receiverDestination, Sender $packageSize = $packageSize ?? $this->defaultPackageSize; $requestArray = [ - 'SessionId' => $this->auth(), - "IKN" => $this->pickPointConf->getIKN(), - "FromCity" => $senderDestination != null ? $senderDestination->getCity() : '', - "FromRegion" => $senderDestination != null ? $senderDestination->getRegion() : '', - "ToCity" => $receiverDestination->getCity(), - "ToRegion" => $receiverDestination->getRegion(), - "PtNumber" => $receiverDestination->getPostamatNumber(), - "Length" => $packageSize != null ? $packageSize->getLength() : '', - "Depth" => $packageSize != null ? $packageSize->getDepth() : '', - "Width" => $packageSize != null ? $packageSize->getWidth() : '', - "Weight" => $packageSize != null ? $packageSize->getWeight() : '' + 'SessionId' => $this->auth(), + "IKN" => $this->pickPointConf->getIKN(), + "FromCity" => $senderDestination != null ? $senderDestination->getCity() : '', + "FromRegion" => $senderDestination != null ? $senderDestination->getRegion() : '', + "ToCity" => $receiverDestination->getCity(), + "ToRegion" => $receiverDestination->getRegion(), + "PtNumber" => $receiverDestination->getPostamatNumber(), + "Length" => $packageSize != null ? $packageSize->getLength() : '', + "Depth" => $packageSize != null ? $packageSize->getDepth() : '', + "Width" => $packageSize != null ? $packageSize->getWidth() : '', + "Weight" => $packageSize != null ? $packageSize->getWeight() : '' ]; $request = $this->client->post($url, [ @@ -158,28 +158,13 @@ public function calculateObjectedPrices(ReceiverDestination $receiverDestination } - /** - * @param $response - * @param $urlCall - * @return mixed - * @throws PickPointMethodCallException - */ - private function checkMethodException($response, $urlCall) - { - if (!empty($response['ErrorCode'])) { - $errorCode = $response['ErrorCode']; - $errorMessage = $response['Error'] ?? ""; - throw new PickPointMethodCallException($urlCall, $errorMessage, $errorCode); - } - } - - /** * Returns invoice data and create shipment/order in delivery service * @param Invoice $invoice + * @param bool $returnInvoiceNumberOnly * @return mixed * @throws PickPointMethodCallException - * @throws \PickPointSdk\Exceptions\ValidateException + * @throws ValidateException */ public function createShipment(Invoice $invoice) { @@ -220,7 +205,7 @@ public function createShipment(Invoice $invoice) [ "Width" => isset($packageSize) ? $packageSize->getWidth() : 0, "Height" => isset($packageSize) ? $packageSize->getLength() : 0, - "Depth" => isset($packageSize) ? $packageSize->getDepth() : 0, + "Depth" => isset($packageSize) ? $packageSize->getDepth() : 0, "Weight" => isset($packageSize) ? $packageSize->getWeight() : 1, "GSBarCode" => $invoice->getGcBarCode() ?? '', "CellStorageType" => 0, @@ -245,43 +230,188 @@ public function createShipment(Invoice $invoice) return $response; } + /** + * @param Invoice $invoice + * @return mixed|void + * @throws PickPointMethodCallException + * @throws ValidateException + */ + public function createShipmentWithInvoice(Invoice $invoice): Invoice + { + $response = $this->createShipment($invoice); + + if (!empty($response['CreatedSendings'])) { + $invoice->setInvoiceNumber($response['CreatedSendings'][0]['InvoiceNumber']); + $invoice->setBarCode($response['CreatedSendings'][0]['Barcode']); + } + return $invoice; + } + /** * Returns current delivery status * @param string $invoiceNumber + * @param string $orderNumber * @return mixed + * @throws PickPointMethodCallException */ - public function getStatus(string $invoiceNumber) + public function getStatus(string $invoiceNumber, string $orderNumber = '') { - // TODO: Implement getStatus() method. + + $url = $this->pickPointConf->getHost() . '/tracksending'; + $request = $this->client->post($url, [ + 'json' => [ + 'SessionId' => $this->auth(), + "InvoiceNumber" => $invoiceNumber, + "SenderInvoiceNumber" => $orderNumber + ], + ]); + + $response = json_decode($request->getBody()->getContents(), true); + + $this->checkMethodException($response, $url); + + return $response[0] ?? []; } /** * @param string $invoiceNumber * @return mixed + * @throws PickPointMethodCallException */ public function cancelInvoice(string $invoiceNumber) { - // TODO: Implement cancelInvoice() method. + $url = $this->pickPointConf->getHost() . '/cancelInvoice'; + $request = $this->client->post($url, [ + 'json' => [ + 'SessionId' => $this->auth(), + "IKN" => $this->pickPointConf->getIKN(), + "InvoiceNumber" => $invoiceNumber + ], + ]); + $response = $request->getBody()->getContents(); + + $this->checkMethodException($response, $url); + + return $response; } /** * Marks on packages * @param array $invoiceNumbers * @return mixed + * @throws PickPointMethodCallException */ public function printLabel(array $invoiceNumbers) { - // TODO: Implement printLabel() method. + $invoices = !empty($invoices) ? $invoices : []; + + $url = $this->pickPointConf->getHost() . '/makelabel'; + $request = $this->client->post($url, [ + 'json' => [ + 'SessionId' => $this->auth(), + "Invoices" => $invoices, + ], + ]); + $response = $request->getBody()->getContents(); + + $this->checkMethodException($response, $url); + + return $response; + } + + + /** + * @param array $invoiceNumbers + * @return mixed + * @throws PickPointMethodCallException + */ + public function makeReceipt(array $invoiceNumbers) + { + + $url = $this->pickPointConf->getHost() . '/makereestrnumber'; + $array = [ + 'SessionId' => $this->auth(), + "CityName" => $this->senderDestination->getCity(), + "RegionName" => $this->senderDestination->getRegion(), + "DeliveryPoint" => $this->senderDestination->getPostamatNumber(), + "Invoices" => $invoiceNumbers, + ]; + $request = $this->client->post($url, [ + 'json' => $array, + ]); + + $response = json_decode($request->getBody()->getContents(), true); + + $this->checkMethodException($response, $url); + + if (!empty($response['ErrorMessage'])) { + throw new PickPointMethodCallException($url, $response['ErrorMessage']); + } + return $response['Numbers'] ?? []; + } /** - * Print reestr/receipt + * Returns byte code pdf * @param string $identifier * @return mixed + * @throws PickPointMethodCallException */ public function printReceipt(string $identifier) { - // TODO: Implement printReceipt() method. + $url = $this->pickPointConf->getHost() . '/getreestr'; + $array = [ + 'SessionId' => $this->auth(), + "ReestrNumber" => $identifier + ]; + $request = $this->client->post($url, [ + 'json' => $array, + ]); + $response = $request->getBody()->getContents(); + + $this->checkMethodException($response, $url); + + return $response; } + /** + * @param array $invoiceNumbers + * @return mixed + * @throws PickPointMethodCallException + */ + public function makeReceiptAndPrint(array $invoiceNumbers) + { + $url = $this->pickPointConf->getHost() . '/makereestr'; + $array = [ + 'SessionId' => $this->auth(), + "CityName" => $this->senderDestination->getCity(), + "RegionName" => $this->senderDestination->getRegion(), + "DeliveryPoint" => $this->senderDestination->getPostamatNumber(), + "Invoices" => $invoiceNumbers, + ]; + $request = $this->client->post($url, [ + 'json' => $array, + ]); + + $response = $request->getBody()->getContents(); + + $this->checkMethodException($response, $url); + + return $response; + } + + /** + * @param $response + * @param $urlCall + * @return mixed + * @throws PickPointMethodCallException + */ + private function checkMethodException($response, $urlCall) + { + if (!empty($response['ErrorCode'])) { + $errorCode = $response['ErrorCode']; + $errorMessage = $response['Error'] ?? ""; + throw new PickPointMethodCallException($urlCall, $errorMessage, $errorCode); + } + } } \ No newline at end of file diff --git a/tests/CheckInvoiceStatusTest.php b/tests/CheckInvoiceStatusTest.php new file mode 100644 index 0000000..b23f669 --- /dev/null +++ b/tests/CheckInvoiceStatusTest.php @@ -0,0 +1,53 @@ +createInvoice(); + $invoiceNumber = $invoice->getInvoiceNumber(); + $status = $this->client->getStatus($invoiceNumber); + $this->assertNotEmpty($status['State']); + $this->assertNotEmpty($status['StateMessage']); + } + + private function createInvoice() + { + $senderCode = 'order:' .(new \DateTime('now'))->getTimestamp(); + $invoice = new Invoice(); + $invoice->setSenderCode($senderCode); + $invoice->setPostamatNumber('5602-009'); + $invoice->setDescription('Custom zakaz'); + $invoice->setRecipientName('Айнур'); + $invoice->setMobilePhone('+79274269594'); + + $invoice->setEmail('ainur_ahmetgalie@mail.ru'); + $invoice->setPostageType('unpiad'); + $invoice->setGettingType('sc'); + $invoice->setSum(500.00); + $invoice->setDeliveryMode('priority'); + + $packageSize = new PackageSize(20, 20, 20); + $invoice->setPackageSize($packageSize); + + $product = new Product(); + $product->setDescription('Test product'); + $product->setPrice(200); + $product->setQuantity(1); + $product->setName('Tovar 1'); + $product->setProductCode('1231'); + + $invoice->setProducts([$product]); + + return $this->client->createShipmentWithInvoice($invoice); + + } +} \ No newline at end of file diff --git a/tests/PrintLabelsAndReestrsTest.php b/tests/PrintLabelsAndReestrsTest.php new file mode 100644 index 0000000..fae06f2 --- /dev/null +++ b/tests/PrintLabelsAndReestrsTest.php @@ -0,0 +1,80 @@ +createInvoice(); + $invoiceNumber = $invoice->getInvoiceNumber(); + $pdfByteCode = $this->client->printLabel(array($invoiceNumber)); + $this->assertTrue(is_string($pdfByteCode)); + $this->assertNotEmpty($pdfByteCode); + } + + public function testMakeReestr() + { + /** + * Invoice $invoice + */ + $invoice = $this->createInvoice(); + $invoiceNumber = $invoice->getInvoiceNumber(); + + $reestr = $this->client->makeReceipt(array($invoiceNumber)); + $this->assertTrue(count($reestr) > 0); + + $pdfByteCode = $this->client->printReceipt($reestr[0]); + $this->assertTrue(is_string($pdfByteCode)); + $this->assertNotEmpty($pdfByteCode); + + $invoice = $this->createInvoice(); + $invoiceNumber = $invoice->getInvoiceNumber(); + $pdfByteCode = $this->client->makeReceiptAndPrint(array($invoiceNumber)); + + $this->assertTrue(is_string($pdfByteCode)); + $this->assertNotEmpty($pdfByteCode); + + } + + private function createInvoice() + { + $senderCode = 'order:' .(new \DateTime('now'))->getTimestamp(); + $invoice = new Invoice(); + $invoice->setSenderCode($senderCode); + $invoice->setPostamatNumber('5602-009'); + $invoice->setDescription('Custom zakaz'); + $invoice->setRecipientName('Айнур'); + $invoice->setMobilePhone('+79274269594'); + + $invoice->setEmail('ainur_ahmetgalie@mail.ru'); + $invoice->setPostageType('unpiad'); + $invoice->setGettingType('sc'); + $invoice->setSum(500.00); + $invoice->setDeliveryMode('priority'); + + $packageSize = new PackageSize(20, 20, 20); + $invoice->setPackageSize($packageSize); + + $product = new Product(); + $product->setDescription('Test product'); + $product->setPrice(200); + $product->setQuantity(1); + $product->setName('Tovar 1'); + $product->setProductCode('1231'); + + $invoice->setProducts([$product]); + + return $this->client->createShipmentWithInvoice($invoice); + + } +} \ No newline at end of file