* @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'].'
';
}
}
if (isset($customization['datas'][Product::CUSTOMIZE_FILE])) {
$customizationText .= sprintf($this->l('%d image(s)'), count($customization['datas'][Product::CUSTOMIZE_FILE])).'
';
}
$customizationText .= '---
';
}
$customizationText = rtrim($customizationText, '---
');
$productsList .= '