Skip to content

Commit abc8347

Browse files
authored
#41 update getCity method, added param $warehouseDescription to explicitly get a city if there are many cities with the same name in one area (#42)
1 parent 930245b commit abc8347

1 file changed

Lines changed: 35 additions & 24 deletions

File tree

src/Delivery/NovaPoshtaApi2.php

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,8 @@ public function getWarehouse($cityRef, $description = '')
412412
$error = array();
413413
$data = array();
414414
if (is_array($warehouses['data'])) {
415-
if (1 === count($warehouses['data']) or !$description) {
416-
$data = $warehouses['data'][0];
417-
} elseif (count($warehouses['data']) > 1) {
415+
$data = $warehouses['data'][0];
416+
if (count($warehouses['data']) > 1 && $description) {
418417
foreach ($warehouses['data'] as $warehouse) {
419418
if (false !== mb_stripos($warehouse['Description'], $description)
420419
or false !== mb_stripos($warehouse['DescriptionRu'], $description)) {
@@ -563,10 +562,11 @@ protected function findCityByRegion($cities, $areaName)
563562
*
564563
* @param string $cityName City's name
565564
* @param string $areaName Region's name
565+
* @param string $warehouseDescription Warehouse description to identiry needed city (if it more than 1 in the area)
566566
*
567-
* @return array City's data
567+
* @return array Cities's data Can be returned more than 1 city with the same name
568568
*/
569-
public function getCity($cityName, $areaName = '')
569+
public function getCity($cityName, $areaName = '', $warehouseDescription = '')
570570
{
571571
// Get cities by name
572572
$cities = $this->getCities(0, $cityName);
@@ -577,6 +577,16 @@ public function getCity($cityName, $areaName = '')
577577
? $this->findCityByRegion($cities, $areaName)
578578
: array($cities['data'][0]);
579579
}
580+
// Try to identify city by one of warehouses descriptions
581+
if (count($data) > 1 && $warehouseDescription) {
582+
foreach ($data as $cityData) {
583+
$warehouseData = $this->getWarehouse($cityData['Ref'], $warehouseDescription);
584+
if (!$warehouseData) {
585+
break;
586+
}
587+
$data = array($cityData);
588+
}
589+
}
580590
// Error
581591
$error = array();
582592
(!$data) and $error = array('City was not found');
@@ -877,7 +887,7 @@ protected function checkInternetDocumentRecipient(array &$counterparty)
877887
}
878888

879889
// Set defaults
880-
if (!$counterparty['CounterpartyType']) {
890+
if (empty($counterparty['CounterpartyType'])) {
881891
$counterparty['CounterpartyType'] = 'PrivatePerson';
882892
}
883893
}
@@ -898,13 +908,14 @@ protected function checkInternetDocumentParams(array &$params)
898908
if (!$params['Cost']) {
899909
throw new \Exception('Cost is required filed for new Internet document');
900910
}
901-
(!$params['DateTime']) and $params['DateTime'] = date('d.m.Y');
902-
(!$params['ServiceType']) and $params['ServiceType'] = 'WarehouseWarehouse';
903-
(!$params['PaymentMethod']) and $params['PaymentMethod'] = 'Cash';
904-
(!$params['PayerType']) and $params['PayerType'] = 'Recipient';
905-
(!$params['SeatsAmount']) and $params['SeatsAmount'] = '1';
906-
(!$params['CargoType']) and $params['CargoType'] = 'Cargo';
907-
(!$params['VolumeGeneral']) and $params['VolumeGeneral'] = '0.0004';
911+
empty($params['DateTime']) and $params['DateTime'] = date('d.m.Y');
912+
empty($params['ServiceType']) and $params['ServiceType'] = 'WarehouseWarehouse';
913+
empty($params['PaymentMethod']) and $params['PaymentMethod'] = 'Cash';
914+
empty($params['PayerType']) and $params['PayerType'] = 'Recipient';
915+
empty($params['SeatsAmount']) and $params['SeatsAmount'] = '1';
916+
empty($params['CargoType']) and $params['CargoType'] = 'Cargo';
917+
empty($params['VolumeGeneral']) and $params['VolumeGeneral'] = '0.0004';
918+
empty($params['VolumeWeight']) and $params['VolumeWeight'] = $params['Weight'];
908919
}
909920

910921
/**
@@ -934,47 +945,47 @@ public function newInternetDocument($sender, $recipient, $params)
934945
// Check for required params and set defaults
935946
$this->checkInternetDocumentRecipient($recipient);
936947
$this->checkInternetDocumentParams($params);
937-
if (!$sender['CitySender']) {
938-
$senderCity = $this->getCity($sender['City'], $sender['Region']);
948+
if (empty($sender['CitySender'])) {
949+
$senderCity = $this->getCity($sender['City'], $sender['Region'], $sender['Warehouse']);
939950
$sender['CitySender'] = $senderCity['data'][0]['Ref'];
940951
}
941952
$sender['CityRef'] = $sender['CitySender'];
942-
if (!$sender['SenderAddress'] and $sender['CitySender'] and $sender['Warehouse']) {
953+
if (empty($sender['SenderAddress']) and $sender['CitySender'] and $sender['Warehouse']) {
943954
$senderWarehouse = $this->getWarehouse($sender['CitySender'], $sender['Warehouse']);
944955
$sender['SenderAddress'] = $senderWarehouse['data'][0]['Ref'];
945956
}
946-
if (!$sender['Sender']) {
957+
if (empty($sender['Sender'])) {
947958
$sender['CounterpartyProperty'] = 'Sender';
948959
$fullName = trim($sender['LastName'].' '.$sender['FirstName'].' '.$sender['MiddleName']);
949960
// Set full name to Description if is not set
950-
if (!$sender['Description']) {
961+
if (empty($sender['Description'])) {
951962
$sender['Description'] = $fullName;
952963
}
953964
// Check for existing sender
954965
$senderCounterpartyExisting = $this->getCounterparties('Sender', 1, $fullName, $sender['CityRef']);
955966
// Copy user to the selected city if user doesn't exists there
956-
if ($senderCounterpartyExisting['data'][0]['Ref']) {
967+
if (isset($senderCounterpartyExisting['data'][0]['Ref'])) {
957968
// Counterparty exists
958969
$sender['Sender'] = $senderCounterpartyExisting['data'][0]['Ref'];
959970
$contactSender = $this->getCounterpartyContactPersons($sender['Sender']);
960971
$sender['ContactSender'] = $contactSender['data'][0]['Ref'];
961-
$sender['SendersPhone'] = $sender['Phone'] ? $sender['Phone'] : $contactSender['data'][0]['Phones'];
972+
$sender['SendersPhone'] = isset($sender['Phone']) ? $sender['Phone'] : $contactSender['data'][0]['Phones'];
962973
}
963974
}
964975

965976
// Prepare recipient data
966977
$recipient['CounterpartyProperty'] = 'Recipient';
967978
$recipient['RecipientsPhone'] = $recipient['Phone'];
968-
if (!$recipient['CityRecipient']) {
969-
$recipientCity = $this->getCity($recipient['City'], $recipient['Region']);
979+
if (empty($recipient['CityRecipient'])) {
980+
$recipientCity = $this->getCity($recipient['City'], $recipient['Region'], $recipient['Warehouse']);
970981
$recipient['CityRecipient'] = $recipientCity['data'][0]['Ref'];
971982
}
972983
$recipient['CityRef'] = $recipient['CityRecipient'];
973-
if (!$recipient['RecipientAddress']) {
984+
if (empty($recipient['RecipientAddress'])) {
974985
$recipientWarehouse = $this->getWarehouse($recipient['CityRecipient'], $recipient['Warehouse']);
975986
$recipient['RecipientAddress'] = $recipientWarehouse['data'][0]['Ref'];
976987
}
977-
if (!$recipient['Recipient']) {
988+
if (empty($recipient['Recipient'])) {
978989
$recipientCounterparty = $this->model('Counterparty')->save($recipient);
979990
$recipient['Recipient'] = $recipientCounterparty['data'][0]['Ref'];
980991
$recipient['ContactRecipient'] = $recipientCounterparty['data'][0]['ContactPerson']['data'][0]['Ref'];

0 commit comments

Comments
 (0)