From e8b686d9285adfc1be28fb06f982eebdbd380ebb Mon Sep 17 00:00:00 2001 From: mDeflotte Date: Mon, 9 Jul 2012 16:30:12 +0000 Subject: [PATCH] [-] BO : #PSCFV-3112 - Fix bug with payment by cheque and multishipping git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@16294 b9a71923-0436-4b27-9f14-aed3839534dd --- .../controllers/orders/_documents.tpl | 10 +-- classes/PaymentModule.php | 2 +- classes/order/Order.php | 35 +++++----- classes/order/OrderHistory.php | 5 +- classes/order/OrderInvoice.php | 67 ++++++++++++++++++- 5 files changed, 94 insertions(+), 25 deletions(-) diff --git a/admin-dev/themes/default/template/controllers/orders/_documents.tpl b/admin-dev/themes/default/template/controllers/orders/_documents.tpl index 23701026c..b6e87bcd9 100644 --- a/admin-dev/themes/default/template/controllers/orders/_documents.tpl +++ b/admin-dev/themes/default/template/controllers/orders/_documents.tpl @@ -82,12 +82,12 @@ -- {else} {displayPrice price=$document->total_paid_tax_incl currency=$currency->id}  - {if $document->getGlobalRestPaid()} + {if $document->getTotalPaid()} - {if $document->getGlobalRestPaid() >= 0} - ({displayPrice price=$document->getGlobalRestPaid() currency=$currency->id} {l s='not paid'}) - {else} - ({displayPrice price=-$document->getGlobalRestPaid() currency=$currency->id} {l s='overpaid'}) + {if $document->getRestPaid() > 0} + ({displayPrice price=$document->getRestPaid() currency=$currency->id} {l s='not paid'}) + {else if $document->getRestPaid() < 0} + ({displayPrice price=-$document->getRestPaid() currency=$currency->id} {l s='overpaid'}) {/if} {/if} diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index 697e2b462..40fe0f563 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -531,7 +531,7 @@ abstract class PaymentModuleCore extends Module // So you migth have two order states $new_history = new OrderHistory(); $new_history->id_order = (int)$order->id; - $new_history->changeIdOrderState((int)$id_order_state, (int)$order->id); + $new_history->changeIdOrderState((int)$id_order_state, (int)$order->id, true); $new_history->addWithemail(true, $extra_vars); unset($order_detail); diff --git a/classes/order/Order.php b/classes/order/Order.php index 32f6d8366..fa0faaa24 100644 --- a/classes/order/Order.php +++ b/classes/order/Order.php @@ -1070,8 +1070,8 @@ class OrderCore extends ObjectModel return false; } - public static function getLastInvoiceNumber() - { + public static function getLastInvoiceNumber() + { return Db::getInstance()->getValue(' SELECT MAX(`number`) FROM `'._DB_PREFIX_.'order_invoice` @@ -1081,7 +1081,7 @@ class OrderCore extends ObjectModel /** * This method allows to generate first invoice of the current order */ - public function setInvoice() + public function setInvoice($use_existing_payment = false) { if (!$this->hasInvoice()) { @@ -1129,19 +1129,22 @@ class OrderCore extends ObjectModel WHERE `id_order` = '.(int)$order_invoice->id_order); // Update order payment - $id_order_payment = Db::getInstance()->getValue(' - SELECT id_order_payment FROM `'._DB_PREFIX_.'order_payment` op - INNER JOIN `'._DB_PREFIX_.'orders` o - ON o.reference = op.order_reference - WHERE id_order = '.(int)$order_invoice->id_order); - - if ($id_order_payment) - Db::getInstance()->execute(' - INSERT INTO `'._DB_PREFIX_.'order_invoice_payment` - SET - `id_order_invoice` = '.(int)$order_invoice->id.', - `id_order_payment` = '.(int)$id_order_payment.', - `id_order` = '.(int)$order_invoice->id_order); + if ($use_existing_payment) + { + $id_order_payment = Db::getInstance()->getValue(' + SELECT MAX(id_order_payment) FROM `'._DB_PREFIX_.'order_payment` op + INNER JOIN `'._DB_PREFIX_.'orders` o + ON o.reference = op.order_reference + WHERE id_order = '.(int)$order_invoice->id_order); + + if ($id_order_payment) + Db::getInstance()->execute(' + INSERT INTO `'._DB_PREFIX_.'order_invoice_payment` + SET + `id_order_invoice` = '.(int)$order_invoice->id.', + `id_order_payment` = '.(int)$id_order_payment.', + `id_order` = '.(int)$order_invoice->id_order); + } // Update order cart rule Db::getInstance()->execute(' diff --git a/classes/order/OrderHistory.php b/classes/order/OrderHistory.php index a8ec80f85..147a9d856 100644 --- a/classes/order/OrderHistory.php +++ b/classes/order/OrderHistory.php @@ -72,8 +72,9 @@ class OrderHistoryCore extends ObjectModel * * @param int $new_order_state * @param int $id_order + * @param bool $use_existing_payment */ - public function changeIdOrderState($new_order_state, $id_order) + public function changeIdOrderState($new_order_state, $id_order, $use_existing_payment = false) { if (!$new_order_state || !$id_order) return; @@ -216,7 +217,7 @@ class OrderHistoryCore extends ObjectModel $order->update(); if ($new_os->invoice && !$order->invoice_number) - $order->setInvoice(); + $order->setInvoice($use_existing_payment); // set orders as paid if ($new_os->paid == 1) diff --git a/classes/order/OrderInvoice.php b/classes/order/OrderInvoice.php index 8304a6d56..7813dd294 100644 --- a/classes/order/OrderInvoice.php +++ b/classes/order/OrderInvoice.php @@ -27,6 +27,10 @@ class OrderInvoiceCore extends ObjectModel { + const TAX_EXCL = 0; + const TAX_INCL = 1; + const DETAIL = 2; + /** @var integer */ public $id_order; @@ -496,7 +500,68 @@ class OrderInvoiceCore extends ObjectModel */ public function getRestPaid() { - return round($this->total_paid_tax_incl - $this->getTotalPaid(), 2); + return round($this->total_paid_tax_incl + $this->getSiblingTotal() - $this->getTotalPaid(), 2); + } + + + /** + * Return collection of order invoice object linked to the payments of the current order invoice object + * + * @since 1.5.0.14 + */ + public function getSibling() + { + $query = new DbQuery(); + $query->select('oip2.id_order_invoice'); + $query->from('order_invoice_payment', 'oip1'); + $query->innerJoin('order_invoice_payment', 'oip2', + 'oip2.id_order_payment = oip1.id_order_payment AND oip2.id_order_invoice <> oip1.id_order_invoice'); + $query->where('oip1.id_order_invoice = '.$this->id); + + $invoices = Db::getInstance()->executeS($query); + if (!$invoices) + return array(); + + $invoice_list = array(); + foreach ($invoices as $invoice) + $invoice_list[] = $invoice['id_order_invoice']; + + $payments = new Collection('OrderInvoice'); + $payments->where('id_order_invoice', 'IN', $invoice_list); + + return $payments; + } + + + /** + * Return total to paid of sibling invoices + * + * @param int $mod TAX_EXCL, TAX_INCL, DETAIL + * + * @since 1.5.0.14 + */ + public function getSiblingTotal($mod = OrderInvoice::TAX_INCL) + { + $query = new DbQuery(); + $query->select('SUM(oi.total_paid_tax_incl) as total_paid_tax_incl, SUM(oi.total_paid_tax_excl) as total_paid_tax_excl'); + $query->from('order_invoice_payment', 'oip1'); + $query->innerJoin('order_invoice_payment', 'oip2', + 'oip2.id_order_payment = oip1.id_order_payment AND oip2.id_order_invoice <> oip1.id_order_invoice'); + $query->leftJoin('order_invoice', 'oi', + 'oi.id_order_invoice = oip2.id_order_invoice'); + $query->where('oip1.id_order_invoice = '.$this->id); + + $row = Db::getInstance()->getRow($query); + + switch ($mod) + { + case OrderInvoice::TAX_EXCL: + return $row['total_paid_tax_excl']; + case OrderInvoice::TAX_INCL: + return $row['total_paid_tax_incl']; + default: + return $row; + } } /**