Current Module

This commit is contained in:
2026-07-10 11:56:56 +02:00
parent 1c52d03cbf
commit 74583d512c
67 changed files with 8964 additions and 0 deletions
@@ -0,0 +1,897 @@
<?php
/**
* Module allow edit existing orders and set rules for automatically generating numbers for orders and other documents
*
* @author Keshva <keshvathakur47@gmail.com>
* @copyright 2021 Keshva
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
if (!defined('_PS_VERSION_'))
exit;
}
class AdminEditOrderController extends ModuleAdminController
{
public function __construct() {
parent::__construct();
}
// ORDER
public function ajaxProcessSetOrderDate() {
$order = new Order((int)Tools::getValue('id_order'));
$date = Tools::getValue('value');
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$order->date_add = $date;
$order->date_upd = $date;
if($order->update()) {
if (version_compare(_PS_VERSION_, '1.7.7', '>=')) {
$dateTime = Tools::displayDate($order->date_add, Context::getContext()->language->id, true);
} else {
$dateTime = Tools::displayDate($order->date_add);
}
die(json_encode(array('errors' => false, 'value' => $dateTime, 'result' => $this->l('Order date stored'))));
}
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessSetOrderNumber() {
$order = new Order((int)Tools::getValue('id_order'));
$reference = Tools::getValue('value');
$updateOrderReference = false;
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
if (Tools::strlen($reference) > 9)
die(json_encode(array(
'result' => $this->l('Order number has more than 9 characters'),
'errors' => true
)));
if (!Order::getByReference($reference)->count()) {
if ($updateOrderReference = Db::getInstance()->update(
"order_payment",
array(
"order_reference" => pSQL($reference)
),
"order_reference = '".pSQL($order->reference)."'"
))
$order->reference = $reference;
} else
die(json_encode(array(
'errors' => true,
'result' => $this->l('Order number already exist')
)));
if($order->update() && $updateOrderReference)
die(json_encode(array('errors' => false, 'result' => $this->l('Order number stored'))));
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
// DOCUMENTS
public function ajaxProcessSetInvoiceDate() {
$order = new Order((int)Tools::getValue('id_order'));
$date = Tools::getValue('value');
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$orderInvoice = new OrderInvoice((int)Tools::getValue('id'));
if (!Validate::isLoadedObject($orderInvoice))
die(json_encode(array(
'result' => $this->l('Order invoice cannot be loaded'),
'errors' => true
)));
$orderInvoice->date_add = $date;
if (!$orderInvoice->update())
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
$order->invoice_date = $date;
if ($order->update())
die(json_encode(array('errors' => false, 'value' => Tools::displayDate($order->invoice_date), 'result' => $this->l('Invoice date stored'))));
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessSetInvoiceNumber() {
DocumentReferences::addMissingReferences((int)Tools::getValue('id_order'));
if (Db::getInstance()->update(
"document_references",
array(
'reference' => pSQL(Tools::getValue("value"))
),
_DB_PREFIX_."document_references.id_document = ".(int)Tools::getValue('id')." AND "._DB_PREFIX_."document_references.document_type = 'invoice'"
)) {
$order = new Order((int)Tools::getValue('id_order'));
$invoice_collection = $order->getInvoicesCollection();
$invoices = array();
foreach ($invoice_collection as $invoice) {
$invoice->name = $invoice->getInvoiceNumberFormatted(Context::getContext()->language->id, (int)$order->id_shop);
$invoices[] = $invoice;
}
die(json_encode(array(
'errors' => false,
'documents' => json_encode(DocumentReferences::getAllDocuments()),
'invoices' => $invoices,
'result' => $this->l('Invoice number stored')
)));
}
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessSetDeliveryDate() {
$order = new Order((int)Tools::getValue('id_order'));
$date = Tools::getValue('value');
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$orderInvoice = new OrderInvoice((int)Tools::getValue('id'));
if (!Validate::isLoadedObject($orderInvoice))
die(json_encode(array(
'result' => $this->l('Order invoice cannot be loaded'),
'errors' => true
)));
$orderInvoice->delivery_date = $date;
if (!$orderInvoice->update())
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
$order->delivery_date = $date;
if ($order->update())
die(json_encode(array('errors' => false, 'value' => Tools::displayDate($order->delivery_date), 'result' => $this->l('Delivery date stored'))));
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessSetDeliveryNumber() {
if (Db::getInstance()->update(
"document_references",
array(
'reference' => pSQL(Tools::getValue("value"))
),
_DB_PREFIX_."document_references.id_document = ".(int)Tools::getValue('id')." AND "._DB_PREFIX_."document_references.document_type = 'delivery_slip'"
))
die(json_encode(array('errors' => false, 'result' => $this->l('Delivery number stored'))));
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessSetCreditslipDate() {
$orderSlip = new OrderSlip((int)Tools::getValue('id'));
if (!Validate::isLoadedObject($orderSlip))
die(json_encode(array(
'result' => $this->l('Credit slip cannot be loaded'),
'errors' => true
)));
$orderSlip->date_add = Tools::getValue('value');
if ($orderSlip->update())
die(json_encode(array('errors' => false, 'value' => Tools::displayDate($orderSlip->date_add), 'result' => $this->l('Credit slip stored'))));
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessSetCreditslipNumber() {
if (Db::getInstance()->update(
"document_references",
array(
'reference' => pSQL(Tools::getValue("value"))
),
_DB_PREFIX_."document_references.id_document = ".(int)Tools::getValue('id')." AND "._DB_PREFIX_."document_references.document_type = 'credit_slip'"
))
die(json_encode(array('errors' => false, 'result' => $this->l('Credit slip number stored'))));
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessDeleteInvoice() {
$order = new Order((int)Tools::getValue('id_order'));
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$idOrderInvoice = (int)Tools::getValue('number');
$orderInvoice = new OrderInvoice($idOrderInvoice);
if (!Validate::isLoadedObject($orderInvoice))
die(json_encode(array(
'result' => $this->l('Order invoice cannot be loaded'),
'errors' => true
)));
if ((int)$order->invoice_number == (int)$orderInvoice->number) {
$newInvoiceNumber = 0;
$newInvoiceDate = "0000-00-00 00:00:00";
$invoiceNumbers = self::getOrderInvoiceNumbers($order->id, 'number');
if (count($invoiceNumbers) > 1) {
$newInvoiceNumber = self::getNextDocumentField($order->id, $orderInvoice->number, 'number', 'number');
$newInvoiceDate = self::getNextDocumentField($order->id, $orderInvoice->number, 'date_add', 'number');
}
$order->invoice_number = $newInvoiceNumber;
$order->invoice_date = $newInvoiceDate;
$order->update();
}
Db::getInstance()->execute('
UPDATE '._DB_PREFIX_.'order_carrier SET id_order_invoice = 0
WHERE id_order = '.(int)$order->id.' AND id_order_invoice = '.(int)$idOrderInvoice
);
Db::getInstance()->execute('
UPDATE '._DB_PREFIX_.'order_detail SET id_order_invoice = 0
WHERE id_order = '.(int)$order->id.' AND id_order_invoice = '.(int)$idOrderInvoice
);
Db::getInstance()->execute('
UPDATE '._DB_PREFIX_.'order_cart_rule SET id_order_invoice = 0
WHERE id_order = '.(int)$order->id
);
$this->deleteDeliverySlip((int)Tools::getValue('id_order'), (int)Tools::getValue('number'));
Db::getInstance()->delete('document_references', "`id_document` = ".(int)$idOrderInvoice." AND `document_type` = 'invoice'");
$orderInvoice->delete();
$invoice_collection = $order->getInvoicesCollection();
$invoices = array();
foreach ($invoice_collection as $invoice) {
$invoice->name = $invoice->getInvoiceNumberFormatted(Context::getContext()->language->id, (int)$order->id_shop);
$invoices[] = $invoice;
}
die(json_encode(array('errors' => false, 'invoices' => $invoices, 'documentsCount' => count($order->getDocuments()), 'result' => $this->l('Invoice successfully deleted'))));
}
public function ajaxProcessDeleteDeliverySlip() {
$this->deleteDeliverySlip((int)Tools::getValue('id_order'), (int)Tools::getValue('number'));
$order = new Order((int)Tools::getValue('id_order'));
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
die(json_encode(array('errors' => false, 'documentsCount' => count($order->getDocuments()), 'result' => $this->l('Delivery slip successfully deleted'))));
}
public function deleteDeliverySlip($idOrder, $idOrderInvoice) {
$order = new Order((int)$idOrder);
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$orderInvoice = new OrderInvoice($idOrderInvoice);
if (!Validate::isLoadedObject($orderInvoice))
die(json_encode(array(
'result' => $this->l('Order invoice cannot be loaded'),
'errors' => true
)));
if ((int)$order->delivery_number == (int)$orderInvoice->delivery_number) {
$newDeliveryNumber = 0;
$newDeliveryDate = "0000-00-00 00:00:00";
$deliveryNumbers = self::getOrderInvoiceNumbers($order->id, 'delivery_number');
if (count($deliveryNumbers) > 1) {
$newDeliveryNumber = self::getNextDocumentField($order->id, $orderInvoice->delivery_number, 'delivery_number', 'delivery_number');
$newDeliveryDate = self::getNextDocumentField($order->id, $orderInvoice->delivery_number, 'delivery_date', 'delivery_number');
}
$order->delivery_number = $newDeliveryNumber;
$order->delivery_date = $newDeliveryDate;
$order->update();
}
$orderInvoice->delivery_date = "0000-00-00 00:00:00";
$orderInvoice->delivery_number = 0;
$orderInvoice->update();
Db::getInstance()->delete('document_references', "`id_document` = ".(int)$idOrderInvoice." AND `document_type` = 'delivery_slip'");
}
public function ajaxProcessDeleteCreditSlip() {
$order = new Order((int)Tools::getValue('id_order'));
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$orderSlip = new OrderSlip((int)Tools::getValue('number'));
if (!Validate::isLoadedObject($orderSlip))
die(json_encode(array(
'result' => $this->l('Order slip cannot be loaded'),
'errors' => true
)));
Db::getInstance()->delete('order_slip', '`id_order_slip` = '.(int)$orderSlip->id);
Db::getInstance()->delete('order_slip_detail', '`id_order_slip` = '.(int)$orderSlip->id);
Db::getInstance()->delete('document_references', "`id_document` = ".(int)$orderSlip->id." AND `document_type` = 'credit_slip'");
die(json_encode(array('errors' => false, 'documentsCount' => count($order->getDocuments()), 'result' => $this->l('Order slip successfully deleted'))));
}
// ORDER NOTE
public function ajaxProcessSetOrderNote() {
$order = new Order((int)Tools::getValue('id_order'));
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$data = array(
'id_order' => $order->id,
'note' => pSQL(Tools::getValue('note'), true),
'show_on_invoice' => (int)Tools::getValue('show_on_invoice'),
'date_upd' => date('Y-m-d H:i:s')
);
if (Db::getInstance()->getRow('SELECT * FROM '._DB_PREFIX_.'order_note WHERE id_order = '.(int)$order->id)) {
$result = Db::getInstance()->update('order_note', $data, '`id_order` = '.(int)$order->id);
} else {
$result = Db::getInstance()->insert('order_note', array_merge($data, array('date_add' => date('Y-m-d H:i:s'))));
}
die(json_encode(array('errors' => !$result, 'result' => $this->l('Order note stored'))));
}
//STATUSES
public function ajaxProcessDeleteStatus() {
$sql = 'SELECT * FROM '._DB_PREFIX_.'order_history WHERE `id_order` = '.(int)Tools::getValue('id_order').' ORDER BY date_add, id_order_history ASC';
if ($results = Db::getInstance()->ExecuteS($sql))
if (count($results) > 1) { //vymaze zaznam len pokial existuje viac ako jeden dalsi, nesmu byt zmazane vsetky statusy
$orderHistoryCount = 0;
foreach ($results as $row) {
$orderHistoryCount++;
if ((int)Tools::getValue('number') == $orderHistoryCount)
if (Db::getInstance()->delete('order_history', '`id_order_history` = '.(int)$row['id_order_history'])) {
$currentOrderStateID = 0;
foreach ($results as $rowInner)
if ((int)$row['id_order_history'] != (int)$rowInner['id_order_history'])
$currentOrderStateID = (int)$rowInner['id_order_state'];
if ($currentOrderStateID > 0) {
$sqlUpdate = 'UPDATE '._DB_PREFIX_.'orders SET current_state = '.(int)$currentOrderStateID.' WHERE id_order = '.(int)Tools::getValue('id_order');
Db::getInstance()->execute($sqlUpdate);
}
die(json_encode(array('errors' => false, 'result' => $this->l('Order status removed'))));
}
else
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be removed')
)));
}
}
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be removed')
)));
}
// SHIPPING
public function ajaxProcessSetShippingDate() {
$orderCarrier = new OrderCarrier((int)Tools::getValue('id'));
if (!Validate::isLoadedObject($orderCarrier))
die(json_encode(array(
'result' => $this->l('Order carrier cannot be loaded'),
'errors' => true
)));
$orderCarrier->date_add = Tools::getValue('value');
if ($orderCarrier->update())
die(json_encode(array('errors' => false, 'value' => Tools::displayDate($orderCarrier->date_add, null, true), 'result' => $this->l('Shipping date stored'))));
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessSetShippingCarrier() {
error_reporting( E_ALL );
ini_set('display_errors', 1);
$order = new Order((int)Tools::getValue('id_order'));
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$order->id_carrier = (int)Tools::getValue('value');
$orderStore = $order->update();
$orderCarrier = new OrderCarrier((int)Tools::getValue('id'));
if (!Validate::isLoadedObject($orderCarrier))
die(json_encode(array(
'result' => $this->l('Order carrier cannot be loaded'),
'errors' => true
)));
$orderCarrier->id_carrier = (int)Tools::getValue('value');
$orderCarrierStore = $orderCarrier->update();
$order = self::recalculateShippingCostAndTotalPaid((int)Tools::getValue('id_order'));
if ($orderCarrierStore && $orderStore) {
Context::getContext()->smarty->assign(array(
'order' => $order,
'currency' => new Currency($order->id_currency),
'link' => Context::getContext()->link
));
if (version_compare(_PS_VERSION_, '1.7', '>')) {
smartyRegisterFunction(Context::getContext()->smarty, 'function', 'displayPrice', array('Tools', 'displayPriceSmarty'));
}
die(json_encode(array('errors' => false, 'order' => $order, 'shipping' => $order->getShipping(), 'shipping_html' => Context::getContext()->smarty->createTemplate(Tools::getValue('adminThemeDir').'/default/template/controllers/orders/_shipping.tpl', Context::getContext()->smarty)->fetch(), 'result' => $this->l('Shipping carrier stored'))));
}
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessSetShippingTrackingNumber() {
$orderCarrier = new OrderCarrier((int)Tools::getValue('id'));
if (!Validate::isLoadedObject($orderCarrier))
die(json_encode(array(
'result' => $this->l('Order carrier cannot be loaded'),
'errors' => true
)));
$orderCarrier->tracking_number = Tools::getValue('value');
if ($orderCarrier->update())
die(json_encode(array('errors' => false, 'result' => $this->l('Shipping tracking number stored'))));
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessSetShippingCostData() {
$orderCarrier = new OrderCarrier((int)Tools::getValue('id_order_carrier'));
if (!Validate::isLoadedObject($orderCarrier))
die(json_encode(array(
'result' => $this->l('Order carrier cannot be loaded'),
'errors' => true
)));
$orderCarrier->shipping_cost_tax_excl = (float)Tools::getValue('priceTaxExcl');
$orderCarrier->shipping_cost_tax_incl = (float)Tools::getValue('priceTaxIncl');
if (!$orderCarrier->update())
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
$order = new Order((int)Tools::getValue('id_order'));
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$order->total_shipping = (float)Tools::getValue('priceTaxIncl');
$order->total_shipping_tax_incl = (float)Tools::getValue('priceTaxIncl');
$order->total_shipping_tax_excl = (float)Tools::getValue('priceTaxExcl');
$order->carrier_tax_rate = (float)Tools::getValue('tax');
$order->total_paid = $order->total_products_wt + $order->total_shipping_tax_incl + $order->total_wrapping_tax_incl - $order->total_discounts_tax_incl;
$order->total_paid_tax_excl = $order->total_products + $order->total_shipping_tax_excl + $order->total_wrapping_tax_excl - $order->total_discounts_tax_excl;
$order->total_paid_tax_incl = $order->total_paid;
if (!$order->update())
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
$order = self::recalculateShippingCostAndTotalPaid((int)Tools::getValue('id_order'));
//pokial napr. nie je vygenerovana faktura pre objednavku tak neexistje zaznam na $orderCarrier->id_order_invoice
if ((int)$orderCarrier->id_order_invoice > 0) {
$orderInvoice = new OrderInvoice((int)$orderCarrier->id_order_invoice);
if (!Validate::isLoadedObject($orderInvoice))
die(json_encode(array(
'result' => $this->l('Order invoice cannot be loaded'),
'errors' => true
)));
else {
$orderInvoice->total_shipping_tax_incl = (float)Tools::getValue('priceTaxIncl');
$orderInvoice->total_shipping_tax_excl = (float)Tools::getValue('priceTaxExcl');
$orderInvoice->total_paid_tax_excl = $order->total_paid_tax_excl;
$orderInvoice->total_paid_tax_incl = $order->total_paid;
if(!$orderInvoice->update())
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
}
Context::getContext()->smarty->assign(array(
'order' => $order,
'currency' => new Currency($order->id_currency),
'link' => Context::getContext()->link
));
if (version_compare(_PS_VERSION_, '1.7', '>')) {
smartyRegisterFunction(Context::getContext()->smarty, 'function', 'displayPrice', array('Tools', 'displayPriceSmarty'));
}
$shippingHtml = '';
if (version_compare(_PS_VERSION_, '1.7.7', '<')) {
$shippingHtml = Context::getContext()->smarty->createTemplate(Tools::getValue('adminThemeDir').'/default/template/controllers/orders/_shipping.tpl', Context::getContext()->smarty)->fetch();
}
die(json_encode(array(
'errors' => false,
'order' => $order,
'shipping' => $order->getShipping(),
'shipping_html' => $shippingHtml,
'result' => $this->l('Shipping cost stored')
)));
}
// PAYMENTS
public function ajaxProcessSetPaymentDate() {
$order = new Order((int)Tools::getValue('id_order'));
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$orderPayments = new PrestaShopCollection('OrderPayment');
$orderPayments->where('order_reference', '=', $order->reference);
$paymentIndex = 0;
foreach ($orderPayments as $key => $value) {
if ($paymentIndex == (int)Tools::getValue('id')) {
$orderPayment = new OrderPayment((int)$value->id);
$orderPayment->date_add = Tools::getValue('value');
if($orderPayment->update())
die(json_encode(array('errors' => false, 'value' => Tools::displayDate($orderPayment->date_add, null, true), 'result' => $this->l('Payment date stored'))));
}
$paymentIndex++;
}
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessSetPaymentMethod() {
$order = new Order((int)Tools::getValue('id_order'));
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$orderPayments = new PrestaShopCollection('OrderPayment');
$orderPayments->where('order_reference', '=', $order->reference);
$paymentIndex = 0;
foreach ($orderPayments as $key => $value) {
if ($paymentIndex == (int)Tools::getValue('id')) {
$orderPayment = new OrderPayment((int)$value->id);
$orderPayment->payment_method = Tools::getValue('value');
if($orderPayment->update())
die(json_encode(array('errors' => false, 'result' => $this->l('Payment method stored'))));
}
$paymentIndex++;
}
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessSetPaymentTransactionID() {
$order = new Order((int)Tools::getValue('id_order'));
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$orderPayments = new PrestaShopCollection('OrderPayment');
$orderPayments->where('order_reference', '=', $order->reference);
$paymentIndex = 0;
foreach ($orderPayments as $key => $value) {
if ($paymentIndex == (int)Tools::getValue('id')) {
$orderPayment = new OrderPayment((int)$value->id);
$orderPayment->transaction_id = Tools::getValue('value');
if ($orderPayment->update())
die(json_encode(array('errors' => false, 'result' => $this->l('Payment transaction ID stored'))));
}
$paymentIndex++;
}
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessSetPaymentAmount() {
$order = new Order((int)Tools::getValue('id_order'));
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$orderPayments = new PrestaShopCollection('OrderPayment');
$orderPayments->where('order_reference', '=', $order->reference);
$paymentIndex = 0;
foreach ($orderPayments as $key => $value) {
if ($paymentIndex == (int)Tools::getValue('id')) {
$orderPayment = new OrderPayment((int)$value->id);
$order->total_paid_real -= (float)$orderPayment->amount;
$newOrderPaymentAmount = (float)str_replace(",", ".", Tools::getValue('value'));
if ($orderPayment->id_currency == $order->id_currency)
$order->total_paid_real += (float)$newOrderPaymentAmount;
else
$order->total_paid_real += Tools::ps_round(Tools::convertPrice((float)$newOrderPaymentAmount, $orderPayment->id_currency, false), 2);
$orderPayment->amount = (float)$newOrderPaymentAmount;
if($orderPayment->update() && $order->update())
die(json_encode(array('errors' => false, 'result' => $this->l('Payment amount stored'))));
}
$paymentIndex++;
}
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be stored')
)));
}
public function ajaxProcessDeletePayment() {
$order = new Order((int)Tools::getValue('id_order'));
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$orderPayments = new PrestaShopCollection('OrderPayment');
$orderPayments->where('order_reference', '=', $order->reference);
$paymentIndex = 0;
foreach ($orderPayments as $key => $value) {
if ($paymentIndex == (int)Tools::getValue('number')) {
$orderPayment = new OrderPayment((int)$value->id);
$order->total_paid_real -= (float)$orderPayment->amount;
if ($orderPayment->delete() && $order->update())
die(json_encode(array('errors' => false, 'result' => $this->l('Order payment removed'))));
}
$paymentIndex++;
}
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be removed')
)));
}
//PRODUCTS
public function ajaxProcessChangeProductName() {
if (Tools::strlen(Tools::getValue('product_name')) > 0) {
$orderDetail = new OrderDetail((int)Tools::getValue('product_id_order_detail'));
if (!Validate::isLoadedObject($orderDetail)) {
die(json_encode(array(
'result' => $this->l('The Order Detail object cannot be loaded'),
'errors' => true
)));
}
$orderDetail->product_name = Tools::getValue('product_name');
if ($orderDetail->update())
die(json_encode(array('errors' => false, 'result' => $this->l('Product name changed'))));
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be changed')
)));
}
}
public function ajaxProcessSetProductName() {
if (Tools::strlen(Tools::getValue('value')) > 0) {
$orderDetail = new OrderDetail((int)Tools::getValue('id'));
if (!Validate::isLoadedObject($orderDetail)) {
die(json_encode(array(
'result' => $this->l('The Order Detail object cannot be loaded'),
'errors' => true
)));
}
$orderDetail->product_name = Tools::getValue('value');
if ($orderDetail->update())
die(json_encode(array('errors' => false, 'result' => $this->l('Product name changed'))));
die(json_encode(array(
'errors' => true,
'result' => $this->l('Can not be changed')
)));
}
}
public function ajaxProcessResendEmail() {
$order = new Order((int)Tools::getValue('id_order'));
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => $this->l('Order cannot be loaded'),
'errors' => true
)));
$customer = new Customer($order->id_customer);
$invoiceAddress = new Address($order->id_address_invoice);
$deliveryAddress = new Address($order->id_address_delivery);
$currency = new Currency($order->id_currency);
$productsOrder = $order->getProducts();
$discounts = $order->getCartRules();
$productsByInvoices = array();
foreach ($productsOrder as $product) {
if (!array_key_exists((int)$product['id_order_invoice'], $productsByInvoices)) {
$productsByInvoices[(int)$product['id_order_invoice']] = array();
}
array_push($productsByInvoices[$product['id_order_invoice']], $product);
}
$virtualProduct = true;
$carrierObj = new Carrier($order->id_carrier);
if (count($productsByInvoices)) {
$customizationQuantities = Customization::countQuantityByCart($order->id_cart);
$productsList = '';
$cartRulesList = '';
foreach ($productsByInvoices as $products) {
foreach ($products as $key => $product) {
$customizedDatas = Product::getAllCustomizedDatas((int)$order->id_cart);
$customizationQuantity = 0;
if (isset($customizedDatas[$product['product_id']][$product['product_attribute_id']])) {
if (array_key_exists($product['product_id'], $customizationQuantities)
&& array_key_exists($product['product_attribute_id'], $customizationQuantities[$product['product_id']])) {
$customizationQuantity = (int)$customizationQuantities[$product['product_id']][$product['product_attribute_id']];
}
$customizationText = '';
foreach ($customizedDatas[$product['product_id']][$product['product_attribute_id']][$order->id_address_delivery] as $customization) {
if (isset($customization['datas'][Product::CUSTOMIZE_TEXTFIELD])) {
foreach ($customization['datas'][Product::CUSTOMIZE_TEXTFIELD] as $text) {
$customizationText .= $text['name'].': '.$text['value'].'<br />';
}
}
if (isset($customization['datas'][Product::CUSTOMIZE_FILE])) {
$customizationText .= sprintf($this->l('%d image(s)'), count($customization['datas'][Product::CUSTOMIZE_FILE])).'<br />';
}
$customizationText .= '---<br />';
}
$customizationText = rtrim($customizationText, '---<br />');
$productsList .= '<tr style="background-color: '.($key % 2 ? '#DDE2E6' : '#EBECEE').';text-align: center;">
<td style="padding: 0.6em 0.4em;width: 15%;">'.$product['product_reference'].'</td>
<td style="padding: 0.6em 0.4em;width: 30%;"><strong>'.$product['product_name'].' - '.$this->l('Customized').(!empty($customizationText) ? ' - '.$customizationText : '').'</strong></td>
<td style="padding: 0.6em 0.4em; width: 20%;">'.Tools::displayPrice(Product::getTaxCalculationMethod() == PS_TAX_EXC ?Tools::ps_round($product['unit_price_tax_excl'], 2) : $product['unit_price_tax_incl'], $currency, false).'</td>
<td style="padding: 0.6em 0.4em; width: 15%;">'.$customizationQuantity.'</td>
<td style="padding: 0.6em 0.4em; width: 20%;">'.Tools::displayPrice($customizationQuantity * (Product::getTaxCalculationMethod() == PS_TAX_EXC ? Tools::ps_round($product['unit_price_tax_excl'], 2) : $product['unit_price_tax_incl']), $currency, false).'</td>
</tr>';
}
if (!$customizationQuantity || (int)$product['product_quantity'] > $customizationQuantity) {
$productsList .= '<tr style="background-color: '.($key % 2 ? '#DDE2E6' : '#EBECEE').';text-align: center;">
<td style="padding: 0.6em 0.4em;width: 15%;">'.$product['product_reference'].'</td>
<td style="padding: 0.6em 0.4em;width: 30%;"><strong>'.$product['product_name'].'</strong></td>
<td style="padding: 0.6em 0.4em; width: 20%;">'.Tools::displayPrice(Product::getTaxCalculationMethod() == PS_TAX_EXC ? Tools::ps_round($product['unit_price_tax_excl'], 2) : $product['unit_price_tax_incl'], $currency, false).'</td>
<td style="padding: 0.6em 0.4em; width: 15%;">'.((int)$product['product_quantity'] - $customizationQuantity).'</td>
<td style="padding: 0.6em 0.4em; width: 20%;">'.Tools::displayPrice(((int)$product['product_quantity'] - $customizationQuantity) * (Product::getTaxCalculationMethod() == PS_TAX_EXC ? Tools::ps_round($product['unit_price_tax_excl'], 2) : $product['unit_price_tax_incl']), $currency, false).'</td>
</tr>';
}
if ($product['download_hash'] == '') {
$virtualProduct &= false;
}
}
}
if ($discounts && count($discounts)) {
foreach ($discounts as $cartRule) {
$cartRulesList .= '<tr>
<td colspan="4" style="padding:0.6em 0.4em;text-align:right">'.$this->l('Voucher name:').' '.$cartRule['name'].'</td>
<td style="padding:0.6em 0.4em;text-align:right">'.($cartRule['value'] != 0.00 ? '-' : '').Tools::displayPrice($cartRule['value'], $currency, false).'</td>
</tr>';
}
}
}
$data = array(
'{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname,
'{email}' => $customer->email,
'{delivery_block_txt}' => AddressFormat::generateAddress($deliveryAddress, array('avoid' => array()), "\n", ' '),
'{invoice_block_txt}' => AddressFormat::generateAddress($invoiceAddress, array('avoid' => array()), "\n", ' '),
'{delivery_block_html}' => AddressFormat::generateAddress($deliveryAddress, array('avoid' => array()), "\n", ' ', array(
'firstname' => '<span style="font-weight:bold;">%s</span>',
'lastname' => '<span style="font-weight:bold;">%s</span>'
)),
'{invoice_block_html}' => AddressFormat::generateAddress($invoiceAddress, array('avoid' => array()), "\n", ' ', array(
'firstname' => '<span style="font-weight:bold;">%s</span>',
'lastname' => '<span style="font-weight:bold;">%s</span>'
)),
'{delivery_company}' => $deliveryAddress->company,
'{delivery_firstname}' => $deliveryAddress->firstname,
'{delivery_lastname}' => $deliveryAddress->lastname,
'{delivery_address1}' => $deliveryAddress->address1,
'{delivery_address2}' => $deliveryAddress->address2,
'{delivery_city}' => $deliveryAddress->city,
'{delivery_postal_code}' => $deliveryAddress->postcode,
'{delivery_country}' => $deliveryAddress->country,
'{delivery_state}' => $deliveryAddress->id_state ?'' : '',
'{delivery_phone}' => ($deliveryAddress->phone) ? $deliveryAddress->phone : $deliveryAddress->phone_mobile,
'{delivery_other}' => $deliveryAddress->other,
'{invoice_company}' => $invoiceAddress->company,
'{invoice_vat_number}' => $invoiceAddress->vat_number,
'{invoice_firstname}' => $invoiceAddress->firstname,
'{invoice_lastname}' => $invoiceAddress->lastname,
'{invoice_address2}' => $invoiceAddress->address2,
'{invoice_address1}' => $invoiceAddress->address1,
'{invoice_city}' => $invoiceAddress->city,
'{invoice_postal_code}' => $invoiceAddress->postcode,
'{invoice_country}' => $invoiceAddress->country,
'{invoice_state}' => $invoiceAddress->id_state ? /*$invoice_state->name*/'' : '',
'{invoice_phone}' => ($invoiceAddress->phone) ? $invoiceAddress->phone : $invoiceAddress->phone_mobile,
'{invoice_other}' => $invoiceAddress->other,
'{order_name}' => $order->getUniqReference(),
'{date}' => Tools::displayDate(date('Y-m-d H:i:s'), null, 1),
'{carrier}' => $virtualProduct ? $this->l('No carrier') : $carrierObj->name,
'{payment}' => Tools::substr($order->payment, 0, 32),
'{products}' => $productsList,
'{discounts}' => $cartRulesList,
'{total_paid}' => Tools::displayPrice($order->total_paid, $currency, false),
'{total_products}' => Tools::displayPrice($order->total_paid - $order->total_shipping - $order->total_wrapping + $order->total_discounts, $currency, false),
'{total_discounts}' => Tools::displayPrice($order->total_discounts, $currency, false),
'{total_shipping}' => Tools::displayPrice($order->total_shipping, $currency, false),
'{total_wrapping}' => Tools::displayPrice($order->total_wrapping, $currency, false)
);
if (Validate::isEmail($customer->email)) {
if (Mail::Send(
(int)Context::getContext()->language->id,
'order_change',
Mail::l('Order changed', (int)$order->id_lang),
$data,
$customer->email,
$customer->firstname.' '.$customer->lastname,
null,
null,
null,
null,
_PS_MODULE_DIR_.'modrefchange/mails/',
false,
(int)$order->id_shop
))
die(json_encode(array('errors' => false, 'result' => $this->l('Mail has been sended to customer'))));
}
die(json_encode(array(
'result' => $this->l('Mail can not be send'),
'errors' => true
)));
}
public function l($string, $class = NULL, $addslashes = false, $htmlentities = true) {
return Translate::getModuleTranslation('modrefchange', $string, 'admineditordercontroller');
}
//STATIC
// funkcia na preratanie celkovej hodnoty shippingu na objednavke a zaroven preratanie celkovej sumy objednavky
// ma vyznam zavolat len pokial objednavka obsahuje viac ako jeden shipping
public static function recalculateShippingCostAndTotalPaid($idOrder) {
$order = new Order((int)$idOrder);
if (!Validate::isLoadedObject($order))
die(json_encode(array(
'result' => Translate::getModuleTranslation('modrefchange', 'Order cannot be loaded', 'admineditordercontroller'),
'errors' => true
)));
$shippings = $order->getShipping();
if (count($shippings) > 0) {
$order_total_shipping_tax_incl = 0;
$order_total_shipping_tax_excl = 0;
foreach ($shippings as $key => $value) {
$order_total_shipping_tax_incl += $value['shipping_cost_tax_incl'];
$order_total_shipping_tax_excl += $value['shipping_cost_tax_excl'];
}
$order->total_shipping = (float)$order_total_shipping_tax_incl;
$order->total_shipping_tax_incl = (float)$order_total_shipping_tax_incl;
$order->total_shipping_tax_excl = (float)$order_total_shipping_tax_excl;
//vypocet dane na zaklade sumy s danou a sumy bez dane -> nepouzivam, pouzivam zadanu dan uzivatelom
//$order->carrier_tax_rate = ((float)$order_total_shipping_tax_excl <= 0 ? 0 : Tools::ps_round((($order_total_shipping_tax_incl - $order_total_shipping_tax_excl) / $order_total_shipping_tax_excl) * 100, 3));
$order->total_paid = (float)$order->total_products_wt + $order->total_shipping_tax_incl + $order->total_wrapping_tax_incl - $order->total_discounts_tax_incl;
$order->total_paid_tax_excl = (float)$order->total_products + $order->total_shipping_tax_excl + $order->total_wrapping_tax_excl - $order->total_discounts_tax_excl;
$order->total_paid_tax_incl = (float)$order->total_paid;
if(!$order->update())
die(json_encode(array(
'errors' => true,
'result' => Translate::getModuleTranslation('modrefchange', 'Can not be stored', 'admineditordercontroller')
)));
}
return $order;
}
public static function getOrderInvoiceNumbers($idOrder, $selectedField) {
$sql = 'SELECT '.pSQL($selectedField).' FROM '._DB_PREFIX_.'order_invoice WHERE id_order = '.(int)$idOrder;
return Db::getInstance()->ExecuteS($sql);
}
public static function getNextDocumentField($idOrder, $documentNumber, $fieldName, $oldNumberName) {
$sql = 'SELECT '.pSQL($fieldName).' FROM '._DB_PREFIX_.'order_invoice WHERE id_order = '.(int)$idOrder.' AND '.pSQL($oldNumberName).' != '.(int)$documentNumber.' ORDER BY date_add ASC';
return Db::getInstance()->getValue($sql);
}
}