967 lines
34 KiB
PHP
967 lines
34 KiB
PHP
<?php
|
|
|
|
class stPaczkomatyBackendActions extends autostPaczkomatyBackendActions
|
|
{
|
|
public function preExecute()
|
|
{
|
|
parent::preExecute();
|
|
|
|
if (!in_array($this->getActionName(), ['config', 'easypackWidget']) && !stConfig::getInstance('stPaczkomatyBackend')->get('enabled'))
|
|
{
|
|
$this->setFlash('error', $this->getContext()->getI18N()->__('app.requires.configuration'), false);
|
|
return $this->forward('stPaczkomatyBackend', 'config');
|
|
}
|
|
}
|
|
|
|
public function executeDispatchOrderPrintOut()
|
|
{
|
|
$i18n = $this->getContext()->getI18N();
|
|
$id = $this->getRequestParameter('id');
|
|
$dispatchOrder = PaczkomatyDispatchOrderPeer::retrieveByPK($id);
|
|
$api = stInPostApi::getInstance();
|
|
|
|
if (null === $dispatchOrder)
|
|
{
|
|
$this->setFlash('error', $i18n->__('Wybrane zlecenie odbioru nie istnieje.'));
|
|
return $this->redirect($this->getRequest()->getReferer());
|
|
}
|
|
|
|
try
|
|
{
|
|
$response = $api->getDispatchOrderPrintOut($dispatchOrder->getDispatchOrderId());
|
|
}
|
|
catch (stInPostApiException $e)
|
|
{
|
|
$message = [$i18n->__('Wystąpił błąd podczas drukowania zlecenia odbioru') . ':'];
|
|
|
|
if (!empty($api->getLastError()->details))
|
|
{
|
|
$error = $api->getLastError()->details;
|
|
}
|
|
elseif (!empty($api->getLastError()->message))
|
|
{
|
|
$error = $api->getLastError()->message;
|
|
}
|
|
else
|
|
{
|
|
$error = $e->getMessage();
|
|
}
|
|
|
|
$message[] = $error;
|
|
$this->setFlash('error', implode('<br>', $message));
|
|
return $this->redirect($this->getRequest()->getReferer());
|
|
}
|
|
|
|
return $this->downloadDocument($response, stPropelSeoUrlBehavior::makeSeoFriendly($i18n->__('Zlecenie odbioru')).'-'.$dispatchOrder->getDispatchOrderExternalId());
|
|
}
|
|
|
|
public function executeDispatchOrder()
|
|
{
|
|
if (!stDeliveryTypeConfiguration::has('inpostk'))
|
|
{
|
|
return $this->redirect($this->getRequest()->getReferer());
|
|
}
|
|
|
|
$i18n = $this->getContext()->getI18N();
|
|
|
|
if ($this->getRequest()->getMethod() == sfRequest::POST)
|
|
{
|
|
$ids = $this->getRequestParameter('paczkomaty_pack[selected]');
|
|
}
|
|
else
|
|
{
|
|
$ids = [$this->getRequestParameter('id')];
|
|
}
|
|
|
|
$shipmentIds = [];
|
|
|
|
$packs = PaczkomatyPackPeer::retrieveByPKsOrder(array_values($ids));
|
|
|
|
if (empty($packs))
|
|
{
|
|
$this->setFlash('error', $i18n->__('Przesyłki o id %%id%% nie istnieją.', [
|
|
'%%id%%' => implode(', ', $ids),
|
|
]));
|
|
|
|
return $this->redirect($this->getRequest()->getReferer());
|
|
}
|
|
|
|
$api = stInPostApi::getInstance();
|
|
$errors = [];
|
|
|
|
foreach ($packs as $pack)
|
|
{
|
|
if (null !== $pack->getDispatchOrderId())
|
|
{
|
|
$errors[] = $i18n->__('Dla przesyłki <b>%%shipment%%</b> zostało już utworzone zlecenie odbioru.', [
|
|
'%%shipment%%' => $pack->getTrackingNumber(),
|
|
]);
|
|
|
|
continue;
|
|
}
|
|
|
|
if ($pack->getSendingMethod() != 'dispatch_order')
|
|
{
|
|
$errors[] = $i18n->__('Przesyłka <b>%%shipment%%</b> ma nieprawidłową metodę wysyłki', [
|
|
'%%shipment%%' => $pack->getTrackingNumber(),
|
|
]);
|
|
|
|
continue;
|
|
}
|
|
|
|
$shipmentIds[] = $pack->getInpostShipmentId();
|
|
}
|
|
|
|
if (!empty($errors))
|
|
{
|
|
$errors = array_merge([$i18n->__('Wystąpił błąd podczas tworzenia zlecenia odbioru') . ':'], $errors);
|
|
$this->setFlash('error', implode('<br>', $errors));
|
|
return $this->redirect($this->getRequest()->getReferer());
|
|
}
|
|
|
|
try
|
|
{
|
|
$response = $api->createDispatchOrders($shipmentIds);
|
|
}
|
|
catch (stInPostApiException $e)
|
|
{
|
|
$error = $api->getLastError();
|
|
|
|
$message = [$i18n->__('Wystąpił błąd podczas tworzenia zlecenia odbioru') . ':'];
|
|
|
|
if (isset($error->details->shipments))
|
|
{
|
|
foreach ($error->details->shipments as $index => $shipmentError)
|
|
{
|
|
$message[] = stInPostApi::translateErrorMessage($shipmentError, [
|
|
'%%subject%%' => $i18n->__('Przesyłka'),
|
|
'%%subject_id%%' => $packs[$index]->getTrackingNumber(),
|
|
]);
|
|
}
|
|
|
|
$this->setFlash('error', implode('<br>', $message));
|
|
}
|
|
else
|
|
{
|
|
$this->setFlash('error', $e->getMessage());
|
|
}
|
|
|
|
return $this->redirect($this->getRequest()->getReferer());
|
|
}
|
|
|
|
while (!isset($response->external_id))
|
|
{
|
|
sleep(5);
|
|
|
|
$response = stInPostApi::getInstance()->getDispatchOrder($response->id);
|
|
}
|
|
|
|
|
|
$dispatchOrder = new PaczkomatyDispatchOrder();
|
|
$dispatchOrder->setDispatchOrderId($response->id);
|
|
$dispatchOrder->setDispatchOrderExternalId($response->external_id);
|
|
$dispatchOrder->setStatus($response->status);
|
|
$dispatchOrder->save();
|
|
|
|
foreach ($packs as $pack)
|
|
{
|
|
$pack->setPaczkomatyDispatchOrder($dispatchOrder);
|
|
$pack->save();
|
|
}
|
|
|
|
return $this->redirect($this->getRequest()->getReferer());
|
|
}
|
|
|
|
/**
|
|
* Undocumented function
|
|
*
|
|
* @param PaczkomatyPack $paczkomaty_pack
|
|
* @return void
|
|
*/
|
|
protected function deletePaczkomatyPack($paczkomaty_pack)
|
|
{
|
|
$api = stInPostApi::getInstance();
|
|
$i18n = $this->getContext()->getI18N();
|
|
|
|
try
|
|
{
|
|
$api->deleteShipment($paczkomaty_pack->getInpostShipmentId());
|
|
}
|
|
catch (stInPostApiException $e)
|
|
{
|
|
throw new Exception($i18n->__('Przesyłka dla zamówienia "%%order%%": %%message%%', [
|
|
'%%order%%' => $paczkomaty_pack->getOrder()->getNumber(),
|
|
'%%message%%' => $e->getMessage(),
|
|
]));
|
|
}
|
|
|
|
parent::deletePaczkomatyPack($paczkomaty_pack);
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
* @return PaczkomatyPack
|
|
*/
|
|
protected function getPaczkomatyPackOrCreate($id = 'id')
|
|
{
|
|
if (!isset($this->paczkomaty_pack))
|
|
{
|
|
$pack = parent::getPaczkomatyPackOrCreate($id);
|
|
$order = $pack->getOrder();
|
|
|
|
if ($pack->hasCourierService() && !stDeliveryTypeConfiguration::has('inpostk'))
|
|
{
|
|
return $this->redirect('@stOrder?action=edit&id=' . $order->getId());
|
|
}
|
|
|
|
if ($pack->isNew())
|
|
{
|
|
$packs = $order->getPaczkomatyPacks();
|
|
|
|
if ($packs)
|
|
{
|
|
return $this->redirect('@stPaczkomatyPlugin?action=edit&id=' . $packs[0]->getId() . '&order_id=' . $order->getId());
|
|
}
|
|
}
|
|
|
|
$i18n = $this->getContext()->getI18N();
|
|
|
|
if ($pack->isNew())
|
|
{
|
|
$config = stConfig::getInstance('stPaczkomatyBackend');
|
|
$pack->setSendingMethod($config->get('sending_method'));
|
|
$pack->setDropOffPoint($config->get('dropoff_point'));
|
|
$pack->setDescription($i18n->__('Zamówienie ' . $pack->getOrder()->getNumber()));
|
|
|
|
if ($pack->hasCourierService() && $pack->hasCashOnDelivery())
|
|
{
|
|
$pack->setInsurance($pack->getCashOnDelivery(true));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$api = stInPostApi::getInstance();
|
|
|
|
try
|
|
{
|
|
$response = $api->getShipment($pack->getInpostShipmentId());
|
|
|
|
$pack->setSendingMethod($response->custom_attributes->sending_method);
|
|
|
|
if (isset($response->custom_attributes->target_point))
|
|
{
|
|
$pack->setCustomerPickupPoint($response->custom_attributes->target_point);
|
|
}
|
|
|
|
if (isset($response->custom_attributes->dropoff_point))
|
|
{
|
|
$pack->setDropOffPoint($response->custom_attributes->dropoff_point);
|
|
}
|
|
|
|
if (!$pack->getTrackingNumber())
|
|
{
|
|
$pack->setTrackingNumber($response->tracking_number);
|
|
}
|
|
|
|
if (isset($response->parcels[0]->template))
|
|
{
|
|
$pack->setParcelTemplate($response->parcels[0]->template);
|
|
}
|
|
|
|
$pack->setStatus($response->status);
|
|
$pack->setEndOfWeekCollection($response->end_of_week_collection);
|
|
|
|
$pack->setCashOnDelivery(isset($response->cod) && $response->cod && $response->cod->amount ? $response->cod->amount : 0);
|
|
$pack->setInsurance(isset($response->insurance) && $response->insurance && $response->insurance->amount ? $response->insurance->amount : 0);
|
|
}
|
|
catch (stInPostApiException $e)
|
|
{
|
|
$this->getRequest()->setError('api', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
if ($pack->hasCourierService())
|
|
{
|
|
$this->removeField('customer_pickup_point');
|
|
$this->removeField('pack_type');
|
|
}
|
|
else
|
|
{
|
|
$this->removeField('parcels');
|
|
$this->removeField('customer_country_code');
|
|
$this->removeField('customer_name');
|
|
$this->removeField('customer_company_name');
|
|
$this->removeField('customer_post_code');
|
|
$this->removeField('customer_city');
|
|
$this->removeField('customer_street');
|
|
$this->removeField('customer_building_number');
|
|
}
|
|
|
|
if (!$pack->isAdminGeneratorPlainField())
|
|
{
|
|
$this->hideAction('download_sticker');
|
|
$this->removeField('status_label');
|
|
$this->removeField('tracking_number');
|
|
}
|
|
else
|
|
{
|
|
$this->hideAction('_save');
|
|
}
|
|
}
|
|
|
|
|
|
return $this->paczkomaty_pack;
|
|
}
|
|
|
|
public function executeList()
|
|
{
|
|
if (!stDeliveryTypeConfiguration::has('inpostk'))
|
|
{
|
|
$this->hideField('dispatch_order_id');
|
|
}
|
|
|
|
if ($this->getRequestParameter('order_id'))
|
|
{
|
|
return $this->redirect('@stOrder?action=edit&id=' . $this->getRequestParameter('order_id'));
|
|
}
|
|
|
|
try
|
|
{
|
|
$result = parent::executeList();
|
|
$this->pager->getResults();
|
|
|
|
return $result;
|
|
}
|
|
catch (stInPostApiException $e)
|
|
{
|
|
$i18n = $this->getContext()->getI18N();
|
|
$this->setFlash('error', $i18n->__('Wystąpił podczas próby nawiązania połączenia z API (błąd: %%error%%)', [
|
|
'%%error%%' =>$e->getMessage()
|
|
]));
|
|
|
|
return $this->redirect('@stPaczkomatyPlugin?action=config');
|
|
}
|
|
}
|
|
|
|
public function executeDownloadLabel()
|
|
{
|
|
$pack = PaczkomatyPackPeer::retrieveByPK($this->getRequestParameter('id'));
|
|
$api = stInPostApi::getInstance();
|
|
$i18n = $this->getContext()->getI18N();
|
|
|
|
try
|
|
{
|
|
if ($pack->getInpostShipmentId())
|
|
{
|
|
$id = $pack->getInpostShipmentId();
|
|
}
|
|
else
|
|
{
|
|
$shipment = $api->getShipmentByTrackingNumber($pack->getTrackingNumber());
|
|
|
|
$id = $shipment->id;
|
|
}
|
|
|
|
$response = $api->downloadLabel($id);
|
|
|
|
return $this->downloadDocument($response, $i18n->__('etykieta').'-'.$pack->getTrackingNumber(), stConfig::getInstance('stPaczkomatyBackend')->get('label_format'));
|
|
}
|
|
catch (stInPostApiException $e)
|
|
{
|
|
$i18n = $this->getContext()->getI18N();
|
|
|
|
if (!empty($api->getLastError()->details))
|
|
{
|
|
$error = $api->getLastError()->details;
|
|
}
|
|
elseif (!empty($api->getLastError()->message))
|
|
{
|
|
$error = $api->getLastError()->message;
|
|
}
|
|
else
|
|
{
|
|
$error = $e->getMessage();
|
|
}
|
|
|
|
$this->getRequest()->setError('api', $i18n->__('Wystąpił bład podczas pobierania etykiety (Błąd: "%error%")', array('%error%' => $error)));
|
|
|
|
return $this->forward('stPaczkomatyBackend', 'edit');
|
|
}
|
|
}
|
|
|
|
public function validateEdit()
|
|
{
|
|
$request = $this->getRequest();
|
|
|
|
if ($request->getMethod() == sfRequest::POST)
|
|
{
|
|
$this->related_object = OrderPeer::retrieveByPK($this->getRequestParameter('order_id'));
|
|
$pack = $this->getPaczkomatyPackOrCreate();
|
|
|
|
$this->updatePaczkomatyPackFromRequest();
|
|
|
|
$api = stInPostApi::getInstance();
|
|
$i18n = $this->getContext()->getI18N();
|
|
$config = stConfig::getInstance('stPaczkomatyBackend');
|
|
$required = ['customer_email', 'customer_phone', 'customer_pickup_point'];
|
|
$data = $this->getRequestParameter('paczkomaty_pack');
|
|
|
|
if ($pack->hasCourierService())
|
|
{
|
|
$required = ['customer_street', 'customer_building_number', 'customer_post_code', 'customer_city', 'customer_country_code', 'customer_email', 'customer_phone', 'parcels'];
|
|
}
|
|
|
|
if (in_array($pack->getSendingMethod(), array('parcel_locker')))
|
|
{
|
|
$required[] = 'dropoff_point';
|
|
}
|
|
|
|
foreach ($required as $fieldName)
|
|
{
|
|
if (!isset($data[$fieldName]) || empty($data[$fieldName]))
|
|
{
|
|
$request->setError('paczkomaty_pack{' . $fieldName . '}', $i18n->__('Proszę uzupełnić pole.'));
|
|
}
|
|
}
|
|
|
|
if ($pack->hasCourierService() && empty($data['customer_name']) && empty($data['customer_company_name']))
|
|
{
|
|
$request->setError('paczkomaty_pack{customer_name}', $i18n->__('Musisz podać nazwę firmy lub imię i nazwisko odbiorcy'));
|
|
$request->setError('paczkomaty_pack{customer_company_name}', $i18n->__('Musisz podać nazwę firmy lub imię i nazwisko odbiorcy'));
|
|
}
|
|
|
|
if ($pack->hasCourierService() && isset($data['parcels']))
|
|
{
|
|
$this->validateParcelSize($pack, $data['parcels']);
|
|
}
|
|
|
|
if ($request->hasErrors())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
$shipmentRequest = array(
|
|
'receiver' => array(
|
|
'email' => $pack->getCustomerEmail(),
|
|
'phone' => $pack->getCustomerPhone(),
|
|
),
|
|
'sender' => array(
|
|
"company_name" => !empty($config->get('sender_company')) ? $config->get('sender_company') : null,
|
|
"first_name" => !empty($config->get('sender_name')) ? $config->get('sender_name') : null,
|
|
"last_name" => !empty($config->get('sender_surname')) ? $config->get('sender_surname') : null,
|
|
"email" => trim($config->get('sender_email')),
|
|
"phone" => str_replace(array('+48', ' ', '-'), '', $config->get('sender_phone')),
|
|
"address" => array(
|
|
"street" => $config->get('sender_street'),
|
|
"building_number" => $config->get('sender_building'),
|
|
"city" => $config->get('sender_city'),
|
|
"post_code" => $config->get('sender_post_code'),
|
|
"country_code" => $config->get('sender_country_code'),
|
|
),
|
|
),
|
|
'custom_attributes' => array(
|
|
'target_point' => $pack->getCustomerPickupPoint(),
|
|
'sending_method' => $pack->getSendingMethod(),
|
|
),
|
|
'service' => $pack->getService(),
|
|
'reference' => $pack->getDescription(),
|
|
'only_choice_of_offer' => false,
|
|
);
|
|
|
|
if (!$pack->hasCourierService())
|
|
{
|
|
$shipmentRequest['parcels'] = [
|
|
['template' => $pack->getParcelTemplate()],
|
|
];
|
|
}
|
|
else
|
|
{
|
|
$parcels = [];
|
|
|
|
list($firstName, $lastName) = explode(' ', $pack->getCustomerName(), 2);
|
|
|
|
foreach (array_values($pack->getParcels()) as $index => $parcel)
|
|
{
|
|
$parcels[] = [
|
|
'id' => $index + 1,
|
|
'dimensions' => [
|
|
'width' => $parcel['width'] * 10,
|
|
'height' => $parcel['height'] * 10,
|
|
'length' => $parcel['length'] * 10,
|
|
'unit' => 'mm',
|
|
],
|
|
'weight' => [
|
|
'amount' => $parcel['weight'],
|
|
'unit' => 'kg',
|
|
]
|
|
];
|
|
}
|
|
|
|
$shipmentRequest['parcels'] = $parcels;
|
|
$shipmentRequest['receiver'] = array_merge($shipmentRequest['receiver'], [
|
|
'company_name' => !empty($pack->getCustomerCompanyName()) ? $pack->getCustomerCompanyName() : null,
|
|
'first_name' => !empty($firstName) ? $firstName : null,
|
|
'last_name' => !empty($lastName) ? $lastName : null,
|
|
'address' => [
|
|
'street' => $pack->getCustomerStreet(),
|
|
'building_number' => $pack->getCustomerBuildingNumber(),
|
|
'post_code' => $pack->getCustomerPostCode(),
|
|
'city' => $pack->getCustomerCity(),
|
|
'country_code' => $pack->getCustomerCountryCode(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
if ($pack->getInsurance() > 0)
|
|
{
|
|
$shipmentRequest['insurance'] = array(
|
|
'amount' => $pack->getInsurance(),
|
|
'currency' => 'PLN',
|
|
);
|
|
}
|
|
|
|
if ($pack->getEndOfWeekCollection())
|
|
{
|
|
$shipmentRequest['end_of_week_collection'] = true;
|
|
}
|
|
|
|
if ($pack->getCashOnDelivery() > 0)
|
|
{
|
|
$shipmentRequest['cod'] = array(
|
|
'amount' => $pack->getCashOnDelivery(),
|
|
'currency' => 'PLN',
|
|
);
|
|
}
|
|
|
|
if (in_array($pack->getSendingMethod(), array('parcel_locker')))
|
|
{
|
|
$shipmentRequest['custom_attributes']['dropoff_point'] = $pack->getDropOffPoint();
|
|
}
|
|
|
|
if (!$request->hasErrors())
|
|
{
|
|
|
|
try
|
|
{
|
|
$response = $api->createShipment($shipmentRequest);
|
|
|
|
$count = 0;
|
|
|
|
while ((null === $response->tracking_number || $response->status != 'confirmed') && $count <= 5)
|
|
{
|
|
sleep(5);
|
|
|
|
$result = $api->getShipment($response->id);
|
|
|
|
if ($result && is_object($result))
|
|
{
|
|
$response = $result;
|
|
}
|
|
|
|
if (!empty($response->offers) && $response->offers[0]->status == 'unavailable')
|
|
{
|
|
$request->setError('api', stInPostApi::formatUnavailableError($response->offers[0]->unavailability_reasons));
|
|
break;
|
|
}
|
|
|
|
$count++;
|
|
}
|
|
|
|
$request->setParameter('create_shipment', $response, 'soteshop/stInPostApi');
|
|
}
|
|
catch (stInPostApiException $e)
|
|
{
|
|
$error = $api->getLastError();
|
|
|
|
if ($error && $error->error == stInPostApi::VALIDATION_FAILED)
|
|
{
|
|
if (isset($error->details->cod))
|
|
{
|
|
$request->setError('paczkomaty_pack{cash_on_delivery}', stInPostApi::formatDetailError($error->details->cod));
|
|
}
|
|
|
|
if (isset($error->details->insurance))
|
|
{
|
|
$request->setError('paczkomaty_pack{insurance}', stInPostApi::formatDetailError($error->details->insurance));
|
|
}
|
|
|
|
if (isset($error->details->custom_attributes))
|
|
{
|
|
foreach ($error->details->custom_attributes as $item)
|
|
{
|
|
if (isset($item->dropoff_point) && $item->dropoff_point[0] == "dropoff_and_target_points_must_be_different")
|
|
{
|
|
$request->setError('paczkomaty_pack{dropoff_point}', $i18n->__('Paczkomat nadawcy nie może być taki sam jak paczkomat odbiorcy'));
|
|
}
|
|
}
|
|
}
|
|
|
|
if (isset($error->details->receiver[0]->email))
|
|
{
|
|
$request->setError('paczkomaty_pack{customer_email}', stInPostApi::formatDetailError($error->details->receiver[0]->email));
|
|
}
|
|
|
|
if (isset($error->details->receiver[0]->address[0]->post_code))
|
|
{
|
|
$request->setError('paczkomaty_pack{customer_post_code}', stInPostApi::formatDetailError($error->details->receiver[0]->address[0]->post_code));
|
|
}
|
|
|
|
if (isset($error->details->sender[0]->phone))
|
|
{
|
|
sfLoader::loadHelpers(array('Helper', 'stUrl'));
|
|
$request->setError('api', $i18n->__('Numer nadawcy jest nieprawidłowy. Numer musi być podany w formacie 9 cyfrowym bez spacji oraz myślników. Przejdź do %link%, aby skorygować problem.', array(
|
|
'%link%' => st_link_to($i18n->__('konfiguracji'), '@stPaczkomatyPlugin?action=config', array('target' => '_blank')),
|
|
)));
|
|
}
|
|
|
|
if (isset($error->details->sender[0]->address[0]->post_code))
|
|
{
|
|
sfLoader::loadHelpers(array('Helper', 'stUrl'));
|
|
$request->setError('api', $i18n->__('Nieprawidłowy kod pocztowy nadawcy. Przejdź do %link%, aby skorygować problem.', array(
|
|
'%link%' => st_link_to($i18n->__('konfiguracji'), '@stPaczkomatyPlugin?action=config', array('target' => '_blank')),
|
|
)));
|
|
}
|
|
|
|
if (isset($error->details->service))
|
|
{
|
|
$request->setError('api', stInPostApi::formatDetailError($error->details->service));
|
|
}
|
|
|
|
if (!$request->hasErrors())
|
|
{
|
|
$request->setError('api', $i18n->__("Wystąpił nieobsługiwany wyjątek (błąd: %error%)", array('%error%' => json_encode($error->details))));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$request->setError('api', $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return !$request->hasErrors();
|
|
}
|
|
|
|
/**
|
|
* Zapisuje model PaczkomatyPack
|
|
*
|
|
* @param PaczkomatyPack $pack
|
|
* @return void
|
|
*/
|
|
protected function savePaczkomatyPack($pack)
|
|
{
|
|
$response = $this->getRequest()->getParameter('create_shipment', null, 'soteshop/stInPostApi');
|
|
|
|
if (!$pack->isNew() && $pack->getInpostShipmentId())
|
|
{
|
|
$api = stInPostApi::getInstance();
|
|
$api->deleteShipment($pack->getInpostShipmentId());
|
|
}
|
|
|
|
$pack->setInpostShipmentId($response->id);
|
|
$pack->setStatus($response->status);
|
|
$pack->setTrackingNumber($response->tracking_number);
|
|
|
|
$parcels = $pack->getParcels();
|
|
|
|
foreach ($response->parcels as $index => $parcel)
|
|
{
|
|
$parcels[$index]['number'] = $parcel->tracking_number;
|
|
}
|
|
|
|
$pack->setParcels($parcels);
|
|
|
|
$isNew = $pack->isNew();
|
|
|
|
$result = parent::savePaczkomatyPack($pack);
|
|
|
|
if ($isNew)
|
|
{
|
|
$this->updateOrderStatus($pack->getOrder());
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function executeEasypackWidget()
|
|
{
|
|
$this->setLayout(false);
|
|
|
|
if (!$this->getRequestParameter('sandbox'))
|
|
{
|
|
$this->endpoint = 'https://api-pl-points.easypack24.net/v1';
|
|
}
|
|
else
|
|
{
|
|
$this->endpoint = 'https://stage-api-pl-points.easypack24.net/v1';
|
|
}
|
|
|
|
$this->scope = $this->getRequestParameter('scope');
|
|
}
|
|
|
|
public function validateConfig()
|
|
{
|
|
$request = $this->getRequest();
|
|
|
|
$this->config = stConfig::getInstance('stPaczkomatyBackend');
|
|
|
|
$api = stInPostApi::getInstance();
|
|
|
|
$i18n = $this->getContext()->getI18N();
|
|
|
|
if ($request->getMethod() == sfRequest::POST)
|
|
{
|
|
$this->updateConfigFromRequest();
|
|
|
|
if ($this->config->get('enabled'))
|
|
{
|
|
if (!$this->config->get('token'))
|
|
{
|
|
$request->setError('config{token}', $i18n->__('Proszę uzupełnić pole.'));
|
|
}
|
|
|
|
if (!$request->hasErrors())
|
|
{
|
|
if ($api->isValid())
|
|
{
|
|
if (!$this->config->get('sending_method'))
|
|
{
|
|
$request->setError('config{sending_method}', $i18n->__('Proszę uzupełnić pole.'));
|
|
}
|
|
elseif ($this->config->get('sending_method') == 'parcel_locker' && !$this->config->get('dropoff_point'))
|
|
{
|
|
$request->setError('config{dropoff_point}', $i18n->__('Proszę uzupełnić pole.'));
|
|
}
|
|
|
|
$required = array('sender_email', 'sender_country_code', 'sender_street', 'sender_building', 'sender_city', 'sender_post_code');
|
|
|
|
if (!$this->config->get('sender_company') && (!$this->config->get('sender_company') || !$this->config->get('sender_company')))
|
|
{
|
|
$required = array_merge(array('sender_name', 'sender_surname'), $required);
|
|
}
|
|
|
|
foreach ($required as $field)
|
|
{
|
|
if (!$this->config->get($field))
|
|
{
|
|
$request->setError('config{' . $field . '}', $i18n->__('Proszę uzupełnić pole.'));
|
|
}
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
$organizations = $api->getOrganizations();
|
|
|
|
$request->setParameter('organizations', $organizations, 'soteshop/stInPostApi');
|
|
}
|
|
catch (stInPostApiException $e)
|
|
{
|
|
$request->setError('api', $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if ($this->config->get('enabled') && $this->config->get('token') && !$request->hasErrors() && !$api->isValid())
|
|
{
|
|
$request->setError('api', $api->getLastErrorMessage());
|
|
}
|
|
|
|
return !$request->hasErrors();
|
|
}
|
|
|
|
protected function updateConfigFromRequest()
|
|
{
|
|
parent::updateConfigFromRequest();
|
|
|
|
$this->updateSenderFromOrganization();
|
|
}
|
|
|
|
protected function updateOrderStatus(Order $order)
|
|
{
|
|
$config = stConfig::getInstance('stPaczkomatyBackend');
|
|
|
|
if ($config->get('order_status') && $order->getOrderStatusId() != $config->get('order_status'))
|
|
{
|
|
$order->setOrderStatusId($config->get('order_status'));
|
|
$order->save();
|
|
|
|
if ($order->getOrderStatus()->getHasMailNotification())
|
|
{
|
|
$orderNotification = new stOrderMailNotification($this->getContext());
|
|
$orderNotification->sendStatusChangeNotification($order);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function saveConfig()
|
|
{
|
|
$ret = parent::saveConfig();
|
|
stTheme::clearSmartyCache(true);
|
|
|
|
return $ret;
|
|
}
|
|
|
|
|
|
protected function updateSenderFromOrganization()
|
|
{
|
|
$organizations = $this->getRequest()->getParameter('organizations', null, 'soteshop/stInPostApi');
|
|
|
|
if ($organizations && !$this->config->get('organization'))
|
|
{
|
|
$organization = $organizations->items[0];
|
|
|
|
$this->config->set('organization', $organization->id);
|
|
$this->config->set('sender_company', $organization->name);
|
|
$this->config->set('sender_street', $organization->address->street);
|
|
$this->config->set('sender_building', $organization->address->building_number);
|
|
$this->config->set('sender_post_code', $organization->address->post_code);
|
|
$this->config->set('sender_country_code', $organization->address->country_code);
|
|
$this->config->set('sender_city', $organization->address->city);
|
|
$this->config->set('sender_email', $organization->contact_person->email);
|
|
$this->config->set('sender_phone', $organization->contact_person->phone);
|
|
$this->config->set('sender_name', $organization->contact_person->first_name);
|
|
$this->config->set('sender_surname', $organization->contact_person->last_name);
|
|
$this->config->set('services', $organization->services);
|
|
}
|
|
}
|
|
|
|
protected function updatePaczkomatyPackFromRequest()
|
|
{
|
|
parent::updatePaczkomatyPackFromRequest();
|
|
|
|
$data = $this->getRequestParameter('paczkomaty_pack');
|
|
|
|
foreach (array('customer_email', 'customer_phone', 'customer_paczkomat', 'use_sender_paczkomat', 'sender_paczkomat', 'pack_type', 'insurance', 'cash_on_delivery', 'description') as $v)
|
|
{
|
|
if (isset($data[$v]))
|
|
{
|
|
call_user_func(array($this->paczkomaty_pack, sfInflector::camelize('set_' . $v)), $data[$v]);
|
|
}
|
|
}
|
|
|
|
if (!isset($data['insurance']))
|
|
{
|
|
$this->paczkomaty_pack->setInsurance(null);
|
|
}
|
|
|
|
if (!isset($data['cash_on_delivery']))
|
|
{
|
|
$this->paczkomaty_pack->setCashOnDelivery(null);
|
|
}
|
|
}
|
|
|
|
protected function getLabels()
|
|
{
|
|
$labels = parent::getLabels();
|
|
|
|
$labels['api'] = 'InPost';
|
|
|
|
return $labels;
|
|
}
|
|
|
|
protected function getConfigLabels()
|
|
{
|
|
$labels = parent::getConfigLabels();
|
|
|
|
$labels['api'] = 'InPost';
|
|
|
|
return $labels;
|
|
}
|
|
|
|
protected function addFiltersCriteria($c)
|
|
{
|
|
parent::addFiltersCriteria($c);
|
|
|
|
if (isset($this->filters['dispatch_order']) && !empty($this->filters['dispatch_order']))
|
|
{
|
|
$c->add(PaczkomatyDispatchOrderPeer::DISPATCH_ORDER_EXTERNAL_ID, $this->filters['dispatch_order']);
|
|
}
|
|
|
|
if (isset($this->filters['service_type']) && !empty($this->filters['service_type']))
|
|
{
|
|
$c->addJoin(OrderPeer::ORDER_DELIVERY_ID, OrderDeliveryPeer::ID);
|
|
$c->addJoin(OrderDeliveryPeer::DELIVERY_ID, DeliveryPeer::ID);
|
|
$c->addJoin(DeliveryPeer::TYPE_ID, DeliveryTypePeer::ID);
|
|
$c->add(DeliveryTypePeer::TYPE, $this->filters['service_type']);
|
|
}
|
|
}
|
|
|
|
protected function deleteDispatchOrderPaczkomatyDispatchOrder($dispatchOrder)
|
|
{
|
|
$i18n = $this->getContext()->getI18N();
|
|
|
|
if (!in_array($dispatchOrder->getStatus(), ['sent', 'new']))
|
|
{
|
|
throw new Exception($i18n->__('Zlecenie odbioru %%dispatch_order%% posiada nieprawidłowy status.', ['%%dispatch_order%%' => $dispatchOrder->getDispatchOrderExternalId()]));
|
|
}
|
|
|
|
$api = stInPostApi::getInstance();
|
|
|
|
try
|
|
{
|
|
$api->deleteDispatchOrder($dispatchOrder->getDispatchOrderId());
|
|
}
|
|
catch (stInPostApiException $e)
|
|
{
|
|
throw new Exception($i18n->__('Zlecenie odbioru "%%dispatch_order%%": %%message%%', [
|
|
'%%dispatch_order%%' => $dispatchOrder->getDispatchOrderExternalId(),
|
|
'%%message%%' => $e->getMessage(),
|
|
]));
|
|
}
|
|
|
|
return parent::deleteDispatchOrderPaczkomatyDispatchOrder($dispatchOrder);
|
|
}
|
|
|
|
protected function downloadDocument(string $content, string $filename, string $type = 'Pdf')
|
|
{
|
|
$types = [
|
|
'Pdf' => 'application/pdf',
|
|
'Zpl' => 'application/octet-stream',
|
|
'Epl' => 'application/octet-stream',
|
|
];
|
|
|
|
if (!isset($types[$type]))
|
|
{
|
|
throw new Exception(sprintf('Przekazany typ plik nie jest obsługiwany (dostępne: %s)', implode(', ', array_keys($types))));
|
|
}
|
|
|
|
$this->getResponse()->clearHttpHeaders();
|
|
$this->getResponse()->setHttpHeader('Content-Type', $types[$type]);
|
|
$this->getResponse()->setHttpHeader('Content-Disposition', 'attachment;filename=' . $filename . '.' . lcfirst($type));
|
|
|
|
return $this->renderText($content);
|
|
}
|
|
|
|
protected function validateParcelSize(PaczkomatyPack $pack, array $parcels)
|
|
{
|
|
$errors = [];
|
|
$validator = new stInPostParcelSizeValidator();
|
|
$validator->initialize($this->getContext(), [
|
|
'service' => $pack->getService(),
|
|
]);
|
|
|
|
if (!$validator->execute($parcels, $errors))
|
|
{
|
|
$this->getRequest()->setError('paczkomaty_pack{parcels}', implode('<br>', $errors));
|
|
}
|
|
|
|
}
|
|
|
|
protected function getActionSelectControlOptions()
|
|
{
|
|
$options = parent::getActionSelectControlOptions();
|
|
|
|
if (!stDeliveryTypeConfiguration::has('inpostk'))
|
|
{
|
|
array_pop($options['NONE']);
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
}
|