800 lines
31 KiB
PHP
800 lines
31 KiB
PHP
<?php
|
|
/**
|
|
* 2014-2023 Presta-Mod.pl Rafał Zontek
|
|
*
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* Poniższy kod jest kodem płatnym, rozpowszechanie bez pisemnej zgody autora zabronione
|
|
* Moduł można zakupić na stronie Presta-Mod.pl. Modyfikacja kodu jest zabroniona,
|
|
* wszelkie modyfikacje powodują utratę gwarancji
|
|
*
|
|
* http://presta-mod.pl
|
|
*
|
|
* DISCLAIMER
|
|
*
|
|
*
|
|
* @author Presta-Mod.pl Rafał Zontek <biuro@presta-mod.pl>
|
|
* @copyright 2014-2023 Presta-Mod.pl
|
|
* @license Licecnja na jedną domenę
|
|
* Presta-Mod.pl Rafał Zontek
|
|
*/
|
|
|
|
class PmInpostPaczkomatyOrderController extends ModuleAdminController
|
|
{
|
|
public $api;
|
|
public $module_pm;
|
|
public $version_ps;
|
|
public $_errors;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->context = Context::getContext();
|
|
if (Configuration::get('PMINPOSTPACZKOMATY_SHIPX')) {
|
|
$this->api = InpostApiShipx::getInstance();
|
|
} else {
|
|
$this->api = InpostApi::getInstance();
|
|
}
|
|
$this->module_pm = Module::getInstanceByName('pminpostpaczkomaty');
|
|
if (Tools::version_compare(_PS_VERSION_, '1.7.0.0', '<=')) {
|
|
$this->version_ps = '1.6';
|
|
} else {
|
|
$this->version_ps = '1.7';
|
|
}
|
|
|
|
if (Tools::isSubmit('printlabel')) {
|
|
$this->printLabel();
|
|
} elseif (Tools::isSubmit('send')) {
|
|
$this->sendDataFromOrder();
|
|
} elseif (Tools::isSubmit('send2')) {
|
|
$this->sendDataFromController();
|
|
} elseif (Tools::isSubmit('sendandprint')) {
|
|
$this->sendAndPrint();
|
|
} elseif (Tools::isSubmit('refreshstatus')) {
|
|
$this->refreshStatus();
|
|
} elseif (Tools::isSubmit('createnew')) {
|
|
$this->createNewList();
|
|
}
|
|
}
|
|
|
|
public function initContent()
|
|
{
|
|
}
|
|
|
|
private function createList($params)
|
|
{
|
|
$customer_email = isset($params['customer_email']) ? $params['customer_email'] : '';
|
|
$customer_phone = isset($params['customer_phone']) ? $params['customer_phone'] : '';
|
|
$selected_machine = isset($params['selected_machine']) ? $params['selected_machine'] : '';
|
|
$package_size = isset($params['package_size']) ? $params['package_size'] : '';
|
|
$insurance_ammount = isset($params['insurance_ammount']) ? $params['insurance_ammount'] : '';
|
|
$cod_ammount = isset($params['cod_ammount']) ? $params['cod_ammount'] : '';
|
|
$sender_machine = isset($params['sender_machine']) ? $params['sender_machine'] : '';
|
|
$package_reference = isset($params['reference']) ? $params['reference'] : '';
|
|
$end_of_week_col = isset($params['end_of_week_collection']) ? (bool)$params['end_of_week_collection'] : false;
|
|
$machine = isset($params['pminpostorder_machine']) ? $params['pminpostorder_machine'] :
|
|
Configuration::get('PMINPOSTPACZKOMATY_SHIPPING_METHOD');
|
|
|
|
if (Configuration::get('PMINPOSTPACZKOMATY_SHIPX')) {
|
|
if ($sender_machine == '') {
|
|
$sender_machine = null;
|
|
}
|
|
}
|
|
|
|
$list_params = array();
|
|
$list_params['pminpostorder_machine'] = $machine;
|
|
$list_params['customer_email'] = $customer_email;
|
|
$list_params['customer_phone'] = $customer_phone;
|
|
$list_params['selected_machine'] = $selected_machine;
|
|
$list_params['package_size'] = $package_size;
|
|
$list_params['insurance_ammount'] = $insurance_ammount;
|
|
$list_params['cod_ammount'] = $cod_ammount;
|
|
$list_params['sender_machine'] = $sender_machine;
|
|
$list_params['package_reference'] = $package_reference;
|
|
$list_params['end_of_week_collection'] = $end_of_week_col;
|
|
$result = $this->api->createList($list_params);
|
|
if (!Configuration::get('PMINPOSTPACZKOMATY_SHIPX')) {
|
|
if ($result) {
|
|
$packCode = (string)$result->pack->packcode;
|
|
} else {
|
|
$this->_errors = $this->api->getErrors();
|
|
return false;
|
|
}
|
|
return $packCode;
|
|
}
|
|
if ($result && $result['status'] != 400 && $result['status'] != 401) {
|
|
sleep(0.500);
|
|
$trackingNumber = $this->api->getTrackingNumber($result['id']);
|
|
if ($trackingNumber === false) {
|
|
return false;
|
|
}
|
|
return array($result['id'], $trackingNumber);
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function payforpack()
|
|
{
|
|
$order = new Order(Tools::getValue('id_order'));
|
|
$id_cart = (int)$order->id_cart;
|
|
$paczkomatyList = PaczkomatyList::getByIdCart($id_cart);
|
|
if ($paczkomatyList->status == 'Opłacona') {
|
|
return true;
|
|
}
|
|
|
|
$shippingNumber = $paczkomatyList->nr_listu;
|
|
$order = new Order(Tools::getValue('id_order'));
|
|
$id_order_carrier = (int)$order->getIdOrderCarrier();
|
|
$order_carrier = new OrderCarrier($id_order_carrier);
|
|
|
|
$order->shipping_number = $shippingNumber;
|
|
$order->update();
|
|
|
|
$order_carrier->tracking_number = pSQL($shippingNumber);
|
|
if ($order_carrier->update()) {
|
|
if ($this->version_ps == '1.7') {
|
|
try {
|
|
$order_carrier->sendInTransitEmail($order);
|
|
} catch (Exception $exp) {
|
|
$exp->getMessage();
|
|
}
|
|
} else {
|
|
$customer = new Customer((int)$order->id_customer);
|
|
$carrier = new Carrier((int)$order->id_carrier, $order->id_lang);
|
|
|
|
$templateVars = array(
|
|
'{followup}' => str_replace('@', $order->shipping_number, $carrier->url),
|
|
'{firstname}' => $customer->firstname,
|
|
'{lastname}' => $customer->lastname,
|
|
'{id_order}' => $order->id,
|
|
'{shipping_number}' => $order->shipping_number,
|
|
'{order_name}' => $order->getUniqReference(),
|
|
'meta_products' => ''
|
|
);
|
|
if (@Mail::Send(
|
|
(int)$order->id_lang,
|
|
'in_transit',
|
|
Mail::l('Package in transit', (int)$order->id_lang),
|
|
$templateVars,
|
|
$customer->email,
|
|
$customer->firstname.' '.$customer->lastname,
|
|
null,
|
|
null,
|
|
null,
|
|
null,
|
|
_PS_MAIL_DIR_,
|
|
true,
|
|
(int)$order->id_shop
|
|
)) {
|
|
Hook::exec(
|
|
'actionAdminOrdersTrackingNumberUpdate',
|
|
array(
|
|
'order' => $order,
|
|
'customer' => $customer,
|
|
'carrier' => $carrier
|
|
),
|
|
null,
|
|
false,
|
|
true,
|
|
false,
|
|
$order->id_shop
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (Configuration::get('PMINPOSTPACZKOMATY_OS')) {
|
|
$status = (int)Configuration::get('PMINPOSTPACZKOMATY_STATUS');
|
|
if ($status != $order->getCurrentState()) {
|
|
$order->setCurrentState($status);
|
|
}
|
|
}
|
|
|
|
$sql = '
|
|
UPDATE `'._DB_PREFIX_.'order_carrier`
|
|
SET tracking_number = "'.pSQL($shippingNumber).'"
|
|
WHERE id_order ='.(int)$order->id;
|
|
Db::getInstance()->execute($sql);
|
|
|
|
$paczkomatyList->status = 'Opłacona';
|
|
$paczkomatyList->save();
|
|
return true;
|
|
}
|
|
|
|
public function genereateReference()
|
|
{
|
|
$order = new Order(Tools::getValue('id_order'));
|
|
$referenceGen = Configuration::get('PMINPOSTPACZKOMATY_REFERENCE');
|
|
$id_order = $order->id;
|
|
$address = new Address($order->id_address_delivery);
|
|
$firstname = $address->firstname;
|
|
$lastname = $address->lastname;
|
|
$order_reference = $order->reference;
|
|
$products = $order->getProducts();
|
|
$productsreference = array();
|
|
foreach ($products as $product) {
|
|
if (trim($product['product_reference']) != '') {
|
|
$productsreference[] = $product['product_reference'];
|
|
}
|
|
}
|
|
$productsreference = implode(' ', $productsreference);
|
|
$reference = str_replace(
|
|
array(
|
|
'{id_order}',
|
|
'{reference}',
|
|
'{productsreference}'
|
|
),
|
|
array(
|
|
$id_order,
|
|
$order_reference,
|
|
$productsreference
|
|
),
|
|
$referenceGen
|
|
);
|
|
$reference = str_replace(
|
|
array(
|
|
'{firstname}',
|
|
'{lastname}'
|
|
),
|
|
array(
|
|
$firstname,
|
|
$lastname
|
|
),
|
|
$reference
|
|
);
|
|
|
|
$reference = trim(Tools::strtoupper($reference));
|
|
if (Tools::strlen($reference)>100) {
|
|
$reference = mb_substr($reference, 0, 100).'@';
|
|
}
|
|
return $reference;
|
|
}
|
|
|
|
public function checkAccess()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function translate(&$error)
|
|
{
|
|
return $this->module_pm->translateText($error);
|
|
}
|
|
|
|
public function parseErrors(&$errors)
|
|
{
|
|
if (is_array($errors)) {
|
|
foreach ($errors as &$item) {
|
|
$this->translate($item);
|
|
}
|
|
} else {
|
|
$this->translate($errors);
|
|
}
|
|
return $errors;
|
|
}
|
|
|
|
public function printLabel()
|
|
{
|
|
$id_order = (int)Tools::getValue('id_order');
|
|
|
|
$order = new Order($id_order);
|
|
$id_cart = (int)$order->id_cart;
|
|
$this->payforpack();
|
|
$shippingNumber = PaczkomatyList::getNrListuByIdCart((int)$id_cart);
|
|
$size_format = PaczkomatyList::getSizeFormat();
|
|
$cache = dirname(__FILE__).'/../../cache/'.$shippingNumber.$size_format;
|
|
if (file_exists($cache)) {
|
|
$file = Tools::file_get_contents($cache);
|
|
} else {
|
|
$file = $this->api->getSticker($shippingNumber);
|
|
if ($file) {
|
|
file_put_contents($cache, $file);
|
|
}
|
|
}
|
|
if (Tools::isSubmit('check')) {
|
|
if ($file == false) {
|
|
$this->displayAjaxError($this->api->getErrors());
|
|
} else {
|
|
$result = json_encode(
|
|
array(
|
|
'error' => false,
|
|
'confirmation' => $this->l('Downloading label')
|
|
)
|
|
);
|
|
die($result);
|
|
}
|
|
}
|
|
$format = Configuration::get('PMINPOSTPACZKOMATY_LABEL_FORMAT');
|
|
if (Tools::isSubmit('format')) {
|
|
$format = Tools::getValue('format');
|
|
}
|
|
header('Content-Description: File Transfer');
|
|
header('Content-Type: application/octet-stream');
|
|
if ($shippingNumber == '') {
|
|
$shippingNumber = $id_order;
|
|
}
|
|
|
|
$paczkomatyList = PaczkomatyList::getByIdCart($id_cart);
|
|
|
|
$status_label = 'Prepared';
|
|
if ($paczkomatyList->package_status == $this->module_pm->translateText($status_label)) {
|
|
$paczkomatyList->status_date = Date('Y-m-d H:i:s');
|
|
}
|
|
$status_label = 'Printed';
|
|
$paczkomatyList->package_status = $this->module_pm->translateText($status_label);
|
|
$paczkomatyList->save();
|
|
|
|
header('Content-Disposition: attachment; filename='.$shippingNumber.'.'.$format);
|
|
header('Expires: 0');
|
|
header('Cache-Control: must-revalidate');
|
|
header('Pragma: public');
|
|
die($file);
|
|
}
|
|
|
|
public function sendDataFromOrder()
|
|
{
|
|
$cod_ammount = 0;
|
|
$insurance_ammount = '0';
|
|
|
|
$id_order = (int)Tools::getValue('id_order');
|
|
$order = new Order($id_order);
|
|
$id_cart = (int)$order->id_cart;
|
|
|
|
$customer_email = Tools::getValue('pminpostorder_email');
|
|
$customer_phone = $this->module_pm->trimPhone(Tools::getValue('pminpostorder_phone'));
|
|
$selected_machine = Tools::getValue('pminpostorder_selected');
|
|
$package_size = Tools::getValue('pminpostorder_size');
|
|
$package_reference = Tools::getValue('pminpostorder_reference');
|
|
|
|
if (in_array((string)Tools::getValue('pminpostorder_machine'), array('1','2'))) {
|
|
$sender_machine = Tools::getValue('pminpostorder_selected_from');
|
|
} else {
|
|
$sender_machine = '';
|
|
}
|
|
$cod = (bool)Tools::getValue('pminpostorder_pobranie');
|
|
|
|
if ($cod) {
|
|
$cod_ammount = (float)$order->total_paid_tax_incl;
|
|
if (Tools::isSubmit('pminpostorder_pobranie_value')) {
|
|
if ((float)Tools::getValue('pminpostorder_pobranie_value') == 0) {
|
|
$result = json_encode(
|
|
array(
|
|
'error' => $this->l('Please check COD value')
|
|
)
|
|
);
|
|
die($result);
|
|
}
|
|
$cod_ammount = (float)Tools::getValue('pminpostorder_pobranie_value');
|
|
} else {
|
|
$cod_ammount = (float)Tools::getValue('pminpostorder_pobranie_value');
|
|
}
|
|
}
|
|
|
|
$insurance_ammount = 0;
|
|
if (Tools::getValue('pminpostorder_ubezpieczenie')) {
|
|
$insurance_ammount = $this->module_pm->replaceComma(
|
|
(float)Tools::getValue('pminpostorder_ubezpieczenie_value')
|
|
);
|
|
if ($insurance_ammount < $cod_ammount) {
|
|
$insurance_ammount = $cod_ammount;
|
|
}
|
|
}
|
|
|
|
$serialized_post = pSql(serialize($_POST));
|
|
|
|
if ($cod && Configuration::get('PMINPOSTPACZKOMATY_ADD_COD')) {
|
|
$package_reference .= ' P:'.$cod_ammount;
|
|
}
|
|
$end_of_week_col = Tools::getValue('pminpostorder_eof') ? true : false;
|
|
$params = array();
|
|
$params['pminpostorder_machine'] = (int)Tools::getValue('pminpostorder_machine');
|
|
$params['customer_email'] = $customer_email;
|
|
$params['customer_phone'] = $customer_phone;
|
|
$params['selected_machine'] = $selected_machine;
|
|
$params['package_size'] = $package_size;
|
|
$params['insurance_ammount'] = $insurance_ammount;
|
|
$params['cod_ammount'] = $cod_ammount;
|
|
$params['sender_machine'] = $sender_machine;
|
|
$params['reference'] = $package_reference;
|
|
$params['end_of_week_collection'] = $end_of_week_col;
|
|
|
|
$packCode = $this->createList($params);
|
|
if (!$packCode) {
|
|
$this->displayAjaxError($this->api->getErrors());
|
|
}
|
|
$paczkomatyList = PaczkomatyList::getByIdCart($id_cart);
|
|
|
|
if ($packCode !== false && is_array($packCode)) {
|
|
$id_pack = pSql($packCode[0]);
|
|
$packCode = $packCode[1];
|
|
$paczkomatyList->id_pack = $id_pack;
|
|
$paczkomatyList->nr_listu = $packCode;
|
|
$paczkomatyList->machine = Tools::getValue('pminpostorder_selected');
|
|
$paczkomatyList->post_info = $serialized_post;
|
|
} else {
|
|
$paczkomatyList->nr_listu = $packCode;
|
|
$paczkomatyList->machine = Tools::getValue('pminpostorder_selected');
|
|
$paczkomatyList->post_info = $serialized_post;
|
|
}
|
|
$status_label = 'Prepared';
|
|
$paczkomatyList->package_status = $this->module_pm->translateText($status_label);
|
|
$paczkomatyList->status_date = Date('Y-m-d H:i:s');
|
|
$paczkomatyList->save();
|
|
$paczkomaty_list_id = $paczkomatyList->id;
|
|
$result = json_encode(
|
|
array(
|
|
'error' => false,
|
|
'packcode' => $packCode,
|
|
'link' => $this->context->link->getAdminLink('PmInpostPaczkomatyList').'&id_orders='.$paczkomaty_list_id.'&vieworders',
|
|
'link2' => $this->context->link->getAdminLink('PmInpostPaczkomatyOrder').
|
|
'&printlabel=1&id_order='.$order->id.'&id='.$paczkomaty_list_id,
|
|
'confirmation' => $this->l('Delivery created')
|
|
)
|
|
);
|
|
die($result);
|
|
}
|
|
|
|
public function sendDataFromController()
|
|
{
|
|
$order = new Order(Tools::getValue('id_order'));
|
|
$id_cart = $order->id_cart;
|
|
$config = array();
|
|
$customer = new Customer($order->id_customer);
|
|
$address = new Address($order->id_address_delivery);
|
|
$config['pminpostorder_email'] = $customer->email;
|
|
$config['pminpostorder_phone'] = $address->phone_mobile;
|
|
if ($config['pminpostorder_phone'] == '') {
|
|
$config['pminpostorder_phone'] = $address->phone;
|
|
}
|
|
$config['pminpostorder_phone'] = $this->module_pm->trimPhone($config['pminpostorder_phone']);
|
|
$module = Module::getInstanceByName('pminpostpaczkomaty');
|
|
|
|
if ($module->isSelectedCarrierBwEof($order->id_carrier)) {
|
|
$config['pminpostorder_eof'] = 1;
|
|
$config['pminpostorder_pobranie'] = 0;
|
|
$config['pminpostorder_size'] = Configuration::get('PMINPOSTPACZKOMATY_SIZE_EOF');
|
|
$config['pminpostorder_ubezpieczenie'] = Configuration::get('PMINPOSTPACZKOMATY_ISSURANCE_EOF');
|
|
} elseif ($module->isSelectedCarrierCodEof($order->id_carrier)) {
|
|
$config['pminpostorder_pobranie'] = 1;
|
|
$config['pminpostorder_size'] = Configuration::get('PMINPOSTPACZKOMATY_SIZE_EOF_CODEOF');
|
|
$config['pminpostorder_ubezpieczenie'] = Configuration::get('PMINPOSTPACZKOMATY_ISSURANCE_EOF_COD');
|
|
$config['pminpostorder_eof'] = 1;
|
|
} elseif ($module->isSelectedCarrierCod($order->id_carrier)) {
|
|
$config['pminpostorder_size'] = Configuration::get('PMINPOSTPACZKOMATY_SIZE_COD');
|
|
$config['pminpostorder_pobranie'] = 1;
|
|
$config['pminpostorder_ubezpieczenie'] = Configuration::get('PMINPOSTPACZKOMATY_ISSURANCE_COD');
|
|
$config['pminpostorder_eof'] = 0;
|
|
} elseif ($module->isSelectedCarrierCod($order->id_carrier)) {
|
|
$config['pminpostorder_pobranie'] = 0;
|
|
$config['pminpostorder_size'] = Configuration::get('PMINPOSTPACZKOMATY_SIZE_BW');
|
|
$config['pminpostorder_ubezpieczenie'] = Configuration::get('PMINPOSTPACZKOMATY_ISSURANCE_BW');
|
|
$config['pminpostorder_eof'] = 0;
|
|
} else {
|
|
$config['pminpostorder_pobranie'] = 0;
|
|
$config['pminpostorder_size'] = Configuration::get('PMINPOSTPACZKOMATY_SIZE_BW');
|
|
$config['pminpostorder_ubezpieczenie'] = Configuration::get('PMINPOSTPACZKOMATY_ISSURANCE_BW');
|
|
$config['pminpostorder_eof'] = 0;
|
|
}
|
|
|
|
if ($order->module == 'pm_cashondelivery') {
|
|
$config['pminpostorder_pobranie'] = 1;
|
|
$config['pminpostorder_ubezpieczenie'] = Configuration::get('PMINPOSTPACZKOMATY_ISSURANCE_COD');
|
|
}
|
|
$cod = $config['pminpostorder_pobranie'];
|
|
$config['pminpostorder_selected_from'] = Configuration::get('PMINPOSTPACZKOMATY_MACHINE');
|
|
$config['pminpostorder_machine'] = Configuration::get('PMINPOSTPACZKOMATY_SHIPPING_METHOD');
|
|
$config['pminpostorder_reference'] = $this->genereateReference();
|
|
|
|
$size = Tools::getValue('pminpostorder_size');
|
|
$insurance_ammount = 0;
|
|
|
|
$machine = PaczkomatyList::getSelectedMachine($id_cart);
|
|
if ($machine) {
|
|
$config['pminpostorder_selected'] = $machine;
|
|
} else {
|
|
$config['pminpostorder_selected'] = '';
|
|
}
|
|
|
|
$post_info = PaczkomatyList::getPostInfoByIdCart($id_cart);
|
|
$post_info = stripslashes($post_info); // Tools::stripslashes($string) { return $string; } PS 1782
|
|
if ($post_info && trim($post_info) != '') {
|
|
$post_info = unserialize($post_info);
|
|
$config = array_merge($config, $post_info);
|
|
}
|
|
$const_insurance = (float)$this->module_pm->replaceComma(Configuration::get('PMINPOSTPACZKOMATY_INSC'));
|
|
$spost = pSql(serialize($config));
|
|
|
|
if ($config['pminpostorder_ubezpieczenie'] == 1) {
|
|
$insurance_ammount = (float)$order->total_paid_tax_incl;
|
|
if ($const_insurance) {
|
|
$insurance_ammount = $const_insurance;
|
|
}
|
|
if ($insurance_ammount >= 20000) {
|
|
$insurance_ammount = 20000;
|
|
}
|
|
}
|
|
if ($config['pminpostorder_machine']) {
|
|
$paczkomat_nadania = $config['pminpostorder_selected_from'];
|
|
} else {
|
|
$paczkomat_nadania = '';
|
|
}
|
|
|
|
if ($cod) {
|
|
$cod = (float)$order->total_paid_tax_incl;
|
|
}
|
|
|
|
$reference = $config['pminpostorder_reference'];
|
|
|
|
$params = array();
|
|
$params['customer_email'] = $config['pminpostorder_email'];
|
|
$params['customer_phone'] = $config['pminpostorder_phone'];
|
|
$params['selected_machine'] = $config['pminpostorder_selected'];
|
|
$params['package_size'] = $size;
|
|
$params['insurance_ammount'] = $insurance_ammount;
|
|
$params['cod_ammount'] = $cod;
|
|
$params['sender_machine'] = $paczkomat_nadania;
|
|
$params['reference'] = $reference;
|
|
$params['end_of_week_collection'] = $config['pminpostorder_eof'];
|
|
|
|
$packCode = $this->createList($params);
|
|
|
|
if (!$packCode) {
|
|
$this->displayAjaxError($this->api->getErrors());
|
|
}
|
|
$paczkomatyList = PaczkomatyList::getByIdCart($id_cart);
|
|
if ($packCode !== false && is_array($packCode)) {
|
|
$id_pack_code = pSql($packCode[0]);
|
|
$packCode = $packCode[1];
|
|
if ($paczkomatyList->id) {
|
|
$paczkomatyList->id_pack = $id_pack_code;
|
|
$paczkomatyList->nr_listu = $packCode;
|
|
$paczkomatyList->post_info = $spost;
|
|
} else {
|
|
$paczkomatyList->id_pack = $id_pack_code;
|
|
$paczkomatyList->nr_listu = $packCode;
|
|
$paczkomatyList->machine = pSql(Tools::getValue('machine'));
|
|
$paczkomatyList->post_info = $spost;
|
|
}
|
|
} else {
|
|
if ($paczkomatyList->id) {
|
|
$paczkomatyList->nr_listu = $packCode;
|
|
$paczkomatyList->post_info = $spost;
|
|
} else {
|
|
$paczkomatyList->nr_listu = $packCode;
|
|
$paczkomatyList->machine = pSql(Tools::getValue('machine'));
|
|
$paczkomatyList->post_info = $spost;
|
|
}
|
|
}
|
|
$status_label = 'Prepared';
|
|
$paczkomatyList->package_status = $this->module_pm->translateText($status_label);
|
|
$paczkomatyList->status_date = Date('Y-m-d H:i:s');
|
|
|
|
$paczkomatyList->save();
|
|
$id_paczkomaty_list = $paczkomatyList->id;
|
|
$result = json_encode(
|
|
array(
|
|
'error' => false,
|
|
'packcode' => $packCode,
|
|
'confirmation' => $this->l('Delivery created'),
|
|
'link' => $this->context->link->getAdminLink('PmInpostPaczkomatyList').'&id_orders='.$id_paczkomaty_list.'&vieworders',
|
|
'link2' => $this->context->link->getAdminLink('PmInpostPaczkomatyOrder').
|
|
'&printlabel=1&id_order='.$order->id.'&id='.$id_paczkomaty_list,
|
|
'id_order' => $order->id,
|
|
)
|
|
);
|
|
die($result);
|
|
}
|
|
|
|
public function sendAndPrint()
|
|
{
|
|
$order = new Order(Tools::getValue('id_order'));
|
|
|
|
$id_cart = $order->id_cart;
|
|
$spost = pSql(serialize($_POST));
|
|
|
|
$size = Tools::getValue('pminpostorder_size');
|
|
$insurance_ammount = '';
|
|
$cod = 0;
|
|
if (Tools::isSubmit('pminpostorder_pobranie_value') && Tools::getValue('pminpostorder_pobranie')) {
|
|
$cod = Tools::getValue('pminpostorder_pobranie_value');
|
|
if ((float)Tools::getValue('pminpostorder_pobranie_value') == 0) {
|
|
$result = json_encode(
|
|
array(
|
|
'error' => $this->l('Please check COD value')
|
|
)
|
|
);
|
|
die($result);
|
|
}
|
|
} elseif (Tools::getValue('pminpostorder_pobranie')) {
|
|
$cod = (float)Tools::getValue('pminpostorder_pobranie_value');
|
|
}
|
|
if (!$cod) {
|
|
if (Configuration::get('PMINPOSTPACZKOMATY_ISSURANCE_BW')) {
|
|
$insurance_ammount = (float)$order->total_paid_tax_incl;
|
|
if ($cod > $insurance_ammount) {
|
|
$insurance_ammount = $cod;
|
|
}
|
|
if ($insurance_ammount >= 20000) {
|
|
$insurance_ammount = 20000;
|
|
}
|
|
}
|
|
} else {
|
|
if (Configuration::get('PMINPOSTPACZKOMATY_ISSURANCE_COD')) {
|
|
$insurance_ammount = (float)$order->total_paid_tax_incl;
|
|
if ($cod > $insurance_ammount) {
|
|
$insurance_ammount = $cod;
|
|
}
|
|
if ($insurance_ammount >= 20000) {
|
|
$insurance_ammount = 20000;
|
|
}
|
|
}
|
|
}
|
|
if (in_array((string)Tools::getValue('pminpostorder_machine'), array('1','2'))) {
|
|
$sender_machine = Tools::getValue('pminpostorder_selected_from');
|
|
} else {
|
|
$sender_machine = '';
|
|
}
|
|
$reference = Tools::getValue('pminpostorder_reference');
|
|
if ($cod && Configuration::get('PMINPOSTPACZKOMATY_ADD_COD')) {
|
|
$reference .= $cod;
|
|
}
|
|
$end_of_week = Tools::getValue('pminpostorder_eof') ? true : false;
|
|
|
|
$params = array();
|
|
$params['pminpostorder_machine'] = (int)Tools::getValue('pminpostorder_machine');
|
|
$params['customer_email'] = Tools::getValue('pminpostorder_email');
|
|
$params['customer_phone'] = Tools::getValue('pminpostorder_phone');
|
|
$params['selected_machine'] = Tools::getValue('pminpostorder_selected');
|
|
$params['package_size'] = $size;
|
|
;
|
|
$params['insurance_ammount'] = $insurance_ammount;
|
|
$params['cod_ammount'] = $cod;
|
|
$params['sender_machine'] = $sender_machine;
|
|
$params['reference'] = $reference;
|
|
$params['end_of_week_collection'] = $end_of_week;
|
|
|
|
$packCode = $this->createList($params);
|
|
if (!$packCode) {
|
|
$this->displayAjaxError($this->api->getErrors());
|
|
}
|
|
|
|
$paczkomatyList = PaczkomatyList::getByIdCart($id_cart);
|
|
|
|
if ($packCode !== false && is_array($packCode)) {
|
|
$id_pack_code = pSql($packCode[0]);
|
|
$packCode = $packCode[1];
|
|
if ($paczkomatyList->id) {
|
|
$paczkomatyList->id_pack = $id_pack_code;
|
|
$paczkomatyList->nr_listu = $packCode;
|
|
$paczkomatyList->post_info = $spost;
|
|
} else {
|
|
$paczkomatyList->id_pack = $id_pack_code;
|
|
$paczkomatyList->nr_listu = $packCode;
|
|
$paczkomatyList->post_info = $spost;
|
|
$paczkomatyList->machine = pSql(Tools::getValue('machine'));
|
|
}
|
|
} else {
|
|
if ($paczkomatyList->id) {
|
|
$paczkomatyList->nr_listu = $packCode;
|
|
$paczkomatyList->post_info = $spost;
|
|
} else {
|
|
$paczkomatyList->nr_listu = $packCode;
|
|
$paczkomatyList->machine = pSql(Tools::getValue('machine'));
|
|
$paczkomatyList->post_info = $spost;
|
|
}
|
|
$status_label = 'Printed';
|
|
$paczkomatyList->package_status = $this->module_pm->translateText($status_label);
|
|
$paczkomatyList->status_date = Date('Y-m-d H:i:s');
|
|
}
|
|
$paczkomatyList->save();
|
|
$this->payforpack();
|
|
$id_paczkomaty_list = $paczkomatyList->id;
|
|
$result = json_encode(
|
|
array(
|
|
'error' => false,
|
|
'packcode' => $packCode,
|
|
'confirmation' => $this->l('Delivery created'),
|
|
'link' => $this->context->link->getAdminLink('PmInpostPaczkomatyList').'&id_orders='.$id_paczkomaty_list.'&vieworders',
|
|
'link2' => $this->context->link->getAdminLink('PmInpostPaczkomatyOrder').
|
|
'&printlabel=1&id_order='.$order->id.'&id='.$id_paczkomaty_list,
|
|
)
|
|
);
|
|
die($result);
|
|
}
|
|
|
|
public function displayAjaxError($message)
|
|
{
|
|
$result = json_encode(
|
|
array(
|
|
'error' => $this->parseErrors($message)
|
|
)
|
|
);
|
|
die($result);
|
|
}
|
|
|
|
public function refreshStatus()
|
|
{
|
|
$shipping_number = Tools::getValue('nr_listu');
|
|
$pack_status = $this->api->getPackStatus($shipping_number);
|
|
if ($pack_status === false) {
|
|
$this->displayAjaxError($this->api->getErrors());
|
|
}
|
|
if (!Configuration::get('PMINPOSTPACZKOMATY_SHIPX')) {
|
|
$data = strtotime((string)$pack_status->statusDate);
|
|
$data = Date('Y-m-d H:i:s', $data);
|
|
$status = (string)$pack_status->status;
|
|
} else {
|
|
$pack_status = (json_decode($pack_status));
|
|
if (!isset($pack_status->tracking_details)) {
|
|
$this->displayAjaxError($pack_status->message);
|
|
}
|
|
$data = strtotime((string)$pack_status->tracking_details[0]->datetime);
|
|
$data = Date('Y-m-d H:i:s', $data);
|
|
$status = (string)$pack_status->status;
|
|
}
|
|
|
|
if (isset($pack_status->status) && $pack_status->status == '404') {
|
|
$this->displayAjaxError($pack_status->message);
|
|
}
|
|
|
|
$paczkomatyList = PaczkomatyList::getObjectByShippingNumber($shipping_number);
|
|
$avstates = explode(',', Configuration::get('PMINPOSTPACZKOMATY_STATUS_AV'));
|
|
if (Configuration::get('PMINPOSTPACZKOMATY_OS') &&
|
|
Tools::isSubmit('change_order_state')
|
|
) {
|
|
if ($status == 'Delivered') {
|
|
$state = (int)Configuration::get('PMINPOSTPACZKOMATY_STATUS_DEL');
|
|
} elseif ($status == 'Prepared') {
|
|
$state = (int)Configuration::get('PMINPOSTPACZKOMATY_STATUS');
|
|
} elseif ($status == 'Stored') {
|
|
$state = (int)Configuration::get('PMINPOSTPACZKOMATY_STATUS_PIC');
|
|
} elseif ($status == 'InTransit' || $status == 'sent_from_source_branch'
|
|
|| $status == 'adopted_at_source_branch' || $status == 'collected_from_sender'
|
|
) {
|
|
$state = (int)Configuration::get('PMINPOSTPACZKOMATY_STATUS_PIC');
|
|
}
|
|
|
|
$id_cart = $paczkomatyList->id_cart;
|
|
if (isset($state) && $state && $id_cart) {
|
|
$order = new Order(Order::getOrderByCartId($id_cart));
|
|
$order_current_sate = $order->getCurrentState();
|
|
if (in_array($order_current_sate, $avstates)) {
|
|
if ($state != $order_current_sate) {
|
|
$order->setCurrentState($state);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$status = $data.' '.$this->translate($status);
|
|
if ($paczkomatyList) {
|
|
$paczkomatyList->pack_status = $status;
|
|
$paczkomatyList->save();
|
|
}
|
|
$result = json_encode(
|
|
array(
|
|
'error' => false,
|
|
'status' => $status
|
|
)
|
|
);
|
|
die($result);
|
|
}
|
|
|
|
public function createNewList()
|
|
{
|
|
$order = new Order(Tools::getValue('id_order'));
|
|
$id_cart = (int)$order->id_cart;
|
|
PaczkomatyList::createEmpty($id_cart);
|
|
$status = true;
|
|
$result = json_encode(
|
|
array(
|
|
'error' => false,
|
|
'status' => $status,
|
|
'confirmation' => $this->l('Please check all fields before create new label')
|
|
)
|
|
);
|
|
|
|
die($result);
|
|
}
|
|
}
|