[-] Project : #PSCFV-2633 #PSCFV-2622 - Fix problems with multishipping and order payment & invoice
git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@15797 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
@@ -84,6 +84,7 @@ class PaymentCCCore extends OrderPayment
|
||||
public static function getByOrderId($id_order)
|
||||
{
|
||||
Tools::displayAsDeprecated();
|
||||
return OrderPayment::getByOrderId($id_order);
|
||||
$order = new Order($id_order);
|
||||
return OrderPayment::getByOrderReference($order->reference);
|
||||
}
|
||||
}
|
||||
|
||||
+18
-12
@@ -143,6 +143,10 @@ abstract class PaymentModuleCore extends Module
|
||||
$cart = new Cart($id_cart);
|
||||
$this->context->cart = $cart;
|
||||
|
||||
$order_status = new OrderState((int)$id_order_state, (int)$cart->id_lang);
|
||||
if (!Validate::isLoadedObject($order_status))
|
||||
throw new PrestaShopException('Can\'t load Order state status');
|
||||
|
||||
if (!$this->active)
|
||||
die(Tools::displayError());
|
||||
// Does order already exists ?
|
||||
@@ -183,10 +187,6 @@ abstract class PaymentModuleCore extends Module
|
||||
die($error);
|
||||
}
|
||||
|
||||
$order_status = new OrderState((int)$id_order_state, (int)$cart->id_lang);
|
||||
if (!Validate::isLoadedObject($order_status))
|
||||
throw new PrestaShopException('Can\'t load Order state status');
|
||||
|
||||
foreach ($cart_delivery_option as $id_address => $key_carriers)
|
||||
foreach ($delivery_option_list[$id_address][$key_carriers]['carrier_list'] as $id_carrier => $data)
|
||||
foreach ($data['package_list'] as $id_package)
|
||||
@@ -270,18 +270,14 @@ abstract class PaymentModuleCore extends Module
|
||||
// Creating order
|
||||
$result = $order->add();
|
||||
|
||||
// Register Payment only if the order status validate the order
|
||||
if ($result && $order_status->logable)
|
||||
{
|
||||
if (!$order->addOrderPayment($amount_paid))
|
||||
throw new PrestaShopException('Can\'t save Order Payment');
|
||||
}
|
||||
if (!$result)
|
||||
throw new PrestaShopException('Can\'t save Order');
|
||||
|
||||
// Amount paid by customer is not the right one -> Status = payment error
|
||||
// We don't use the following condition to avoid the float precision issues : http://www.php.net/manual/en/language.types.float.php
|
||||
// if ($order->total_paid != $order->total_paid_real)
|
||||
// We use number_format in order to compare two string
|
||||
if ($order_status->logable && number_format($cart_total_paid, 2) != number_format($order->total_paid_real, 2))
|
||||
if ($order_status->logable && number_format($cart_total_paid, 2) != number_format($amount_paid, 2))
|
||||
$id_order_state = Configuration::get('PS_OS_ERROR');
|
||||
|
||||
$order_list[] = $order;
|
||||
@@ -304,6 +300,16 @@ abstract class PaymentModuleCore extends Module
|
||||
}
|
||||
}
|
||||
|
||||
// Register Payment only if the order status validate the order
|
||||
if ($order_status->logable)
|
||||
{
|
||||
// $order is the last order loop in the foreach
|
||||
// The method addOrderPayment of the class Order make a create a paymentOrder
|
||||
// linked to the order reference and not to the order id
|
||||
if (!$order->addOrderPayment($amount_paid))
|
||||
throw new PrestaShopException('Can\'t save Order Payment');
|
||||
}
|
||||
|
||||
// Next !
|
||||
$only_one_gift = false;
|
||||
$cart_rule_used = array();
|
||||
@@ -617,7 +623,7 @@ abstract class PaymentModuleCore extends Module
|
||||
Logger::addLog($error, 4, '0000002', 'Cart', intval($order->id_cart));
|
||||
die($error);
|
||||
}
|
||||
}
|
||||
} // End foreach $order_detail_list
|
||||
// Use the last order as currentOrder
|
||||
$this->currentOrder = (int)$order->id;
|
||||
return true;
|
||||
|
||||
+48
-7
@@ -1116,9 +1116,15 @@ class OrderCore extends ObjectModel
|
||||
|
||||
// Update order payment
|
||||
Db::getInstance()->execute('
|
||||
UPDATE `'._DB_PREFIX_.'order_payment`
|
||||
SET `id_order_invoice` = '.(int)$order_invoice->id.'
|
||||
WHERE `id_order` = '.(int)$order_invoice->id_order);
|
||||
INSERT INTO `'._DB_PREFIX_.'order_invoice_payment`
|
||||
SET
|
||||
`id_order_invoice` = '.(int)$order_invoice->id.',
|
||||
`id_order_payment` = (
|
||||
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.' LIMIT 1),
|
||||
`id_order` = '.(int)$order_invoice->id_order);
|
||||
|
||||
// Update order cart rule
|
||||
Db::getInstance()->execute('
|
||||
@@ -1357,7 +1363,7 @@ class OrderCore extends ObjectModel
|
||||
public function getOrderPaymentCollection()
|
||||
{
|
||||
$order_payments = new Collection('OrderPayment');
|
||||
$order_payments->where('id_order', '=', $this->id);
|
||||
$order_payments->where('order_reference', '=', $this->reference);
|
||||
return $order_payments;
|
||||
}
|
||||
|
||||
@@ -1376,8 +1382,7 @@ class OrderCore extends ObjectModel
|
||||
public function addOrderPayment($amount_paid, $payment_method = null, $payment_transaction_id = null, $currency = null, $date = null, $order_invoice = null)
|
||||
{
|
||||
$order_payment = new OrderPayment();
|
||||
$order_payment->id_order = $this->id;
|
||||
$order_payment->id_order_invoice = (!is_null($order_invoice) ? $order_invoice->id : null);
|
||||
$order_payment->order_reference = $this->reference;
|
||||
$order_payment->id_currency = ($currency ? $currency->id : $this->id_currency);
|
||||
// we kept the currency rate for historization reasons
|
||||
$order_payment->conversion_rate = ($currency ? $currency->conversion_rate : 1);
|
||||
@@ -1394,7 +1399,19 @@ class OrderCore extends ObjectModel
|
||||
$this->total_paid_real += Tools::ps_round(Tools::convertPrice($order_payment->amount, $order_payment->id_currency, false), 2);
|
||||
|
||||
// We put autodate parameter of add method to true if date_add field is null
|
||||
return $order_payment->add(is_null($order_payment->date_add)) && $this->update();
|
||||
$res = $order_payment->add(is_null($order_payment->date_add)) && $this->update();
|
||||
|
||||
if (!$res)
|
||||
return false;
|
||||
|
||||
if (!is_null($order_invoice))
|
||||
{
|
||||
$res = Db::getInstance()->execute('
|
||||
INSERT INTO `'._DB_PREFIX_.'order_invoice_payment`
|
||||
VALUES('.(int)$order_invoice->id.', '.(int)$order_payment->id.', '.(int)$this->id.')');
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1745,5 +1762,29 @@ class OrderCore extends ObjectModel
|
||||
$sql_filter .= Shop::addSqlRestriction(Shop::SHARE_ORDER, 'main');
|
||||
return parent::getWebserviceObjectList($sql_join, $sql_filter, $sql_sort, $sql_limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all other orders with the same reference
|
||||
*
|
||||
* @since 1.5.0.13
|
||||
*/
|
||||
public function getBrother()
|
||||
{
|
||||
$collection = new Collection('order');
|
||||
$collection->where('reference', '=', $this->reference);
|
||||
$collection->where('id_order', '<>', $this->id);
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a collection of order payments
|
||||
*
|
||||
* @since 1.5.0.13
|
||||
*/
|
||||
public function getOrderPayments()
|
||||
{
|
||||
return OrderPayment::getByOrderReference($this->reference);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -226,16 +226,19 @@ class OrderHistoryCore extends ObjectModel
|
||||
foreach ($invoices as $invoice)
|
||||
{
|
||||
$rest_paid = $invoice->getRestPaid();
|
||||
if ($rest_paid)
|
||||
if ($rest_paid > 0)
|
||||
{
|
||||
$payment = new OrderPayment();
|
||||
$payment->id_order = $order->id;
|
||||
$payment->id_order_invoice = $invoice->id;
|
||||
$payment->order_reference = $order->reference;
|
||||
$payment->id_currency = $order->id_currency;
|
||||
$payment->amount = $rest_paid;
|
||||
$payment->payment_method = $payment_method->displayName;
|
||||
$payment->conversion_rate = 1;
|
||||
$payment->save();
|
||||
|
||||
Db::getInstance()->execute('
|
||||
INSERT INTO `'._DB_PREFIX_.'order_invoice_payment`
|
||||
VALUES('.(int)$invoice->id.', '.(int)$payment->id.', '.(int)$order->id.')');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -442,11 +442,11 @@ class OrderInvoiceCore extends ObjectModel
|
||||
return $carrier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.5
|
||||
* @static
|
||||
* @param $id_order_invoice
|
||||
*/
|
||||
/**
|
||||
* @since 1.5
|
||||
* @static
|
||||
* @param $id_order_invoice
|
||||
*/
|
||||
public static function getCarrierId($id_order_invoice)
|
||||
{
|
||||
$sql = 'SELECT `id_carrier`
|
||||
@@ -496,6 +496,39 @@ class OrderInvoiceCore extends ObjectModel
|
||||
return round($this->total_paid_tax_incl - $this->getTotalPaid(), 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get global rest to paid
|
||||
* This method will return something different of the method getRestPaid if
|
||||
* there is an other invoice linked to the payments of the current invoice
|
||||
* @since 1.5.0.13
|
||||
*/
|
||||
public function getGlobalRestPaid()
|
||||
{
|
||||
static $cache;
|
||||
|
||||
if (!isset($cache[$this->id]))
|
||||
{
|
||||
$res = Db::getInstance()->getRow('
|
||||
SELECT SUM(sub.paid) paid, SUM(sub.to_paid) to_paid
|
||||
FROM (
|
||||
SELECT
|
||||
op.amount as paid, SUM(oi.total_paid_tax_incl) to_paid
|
||||
FROM `'._DB_PREFIX_.'order_invoice_payment` oip1
|
||||
INNER JOIN `'._DB_PREFIX_.'order_invoice_payment` oip2
|
||||
ON oip2.id_order_payment = oip1.id_order_payment
|
||||
INNER JOIN `'._DB_PREFIX_.'order_invoice` oi
|
||||
ON oi.id_order_invoice = oip2.id_order_invoice
|
||||
INNER JOIN `'._DB_PREFIX_.'order_payment` op
|
||||
ON op.id_order_payment = oip2.id_order_payment
|
||||
WHERE oip1.id_order_invoice = '.(int)$this->id.'
|
||||
GROUP BY op.id_order_payment
|
||||
) sub');
|
||||
$cache[$this->id] = round($res['to_paid'] - $res['paid'], 2);
|
||||
}
|
||||
|
||||
return $cache[$this->id];
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.5.0.2
|
||||
* @return bool Is paid ?
|
||||
@@ -511,9 +544,7 @@ class OrderInvoiceCore extends ObjectModel
|
||||
*/
|
||||
public function getOrderPaymentCollection()
|
||||
{
|
||||
$order_payments = new Collection('OrderPayment');
|
||||
$order_payments->where('id_order_invoice', '=', $this->id);
|
||||
return $order_payments;
|
||||
return OrderPayment::getByInvoiceId($this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,9 +27,8 @@
|
||||
|
||||
class OrderPaymentCore extends ObjectModel
|
||||
{
|
||||
public $id_order;
|
||||
public $order_reference;
|
||||
public $id_currency;
|
||||
public $id_order_invoice;
|
||||
public $amount;
|
||||
public $payment_method;
|
||||
public $conversion_rate;
|
||||
@@ -47,9 +46,8 @@ class OrderPaymentCore extends ObjectModel
|
||||
'table' => 'order_payment',
|
||||
'primary' => 'id_order_payment',
|
||||
'fields' => array(
|
||||
'id_order' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
|
||||
'order_reference' => array('type' => self::TYPE_STRING, 'validate' => 'isAnything', 'size' => 9),
|
||||
'id_currency' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
|
||||
'id_order_invoice' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
|
||||
'amount' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
|
||||
'payment_method' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
|
||||
'conversion_rate' => array('type' => self::TYPE_INT, 'validate' => 'isFloat'),
|
||||
@@ -79,12 +77,27 @@ class OrderPaymentCore extends ObjectModel
|
||||
*/
|
||||
public static function getByOrderId($id_order)
|
||||
{
|
||||
return Db::getInstance()->executeS('
|
||||
SELECT *
|
||||
FROM `'._DB_PREFIX_.'order_payment`
|
||||
WHERE `id_order` = '.(int)$id_order);
|
||||
Tools::displayAsDeprecated();
|
||||
$order = new Order($id_order);
|
||||
return OrderPayment::getByOrderReference($order->reference);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the detailed payment of an order
|
||||
* @param int $order_reference
|
||||
* @return array
|
||||
* @since 1.5.0.13
|
||||
*/
|
||||
public static function getByOrderReference($order_reference)
|
||||
{
|
||||
return ObjectModel::hydrateCollection('OrderPayment',
|
||||
Db::getInstance()->executeS('
|
||||
SELECT *
|
||||
FROM `'._DB_PREFIX_.'order_payment`
|
||||
WHERE `order_reference` = \''.pSQL($order_reference).'\'')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Order Payments By Invoice ID
|
||||
* @static
|
||||
@@ -93,9 +106,38 @@ class OrderPaymentCore extends ObjectModel
|
||||
*/
|
||||
public static function getByInvoiceId($id_invoice)
|
||||
{
|
||||
$payments = Db::getInstance()->executeS('SELECT id_order_payment FROM `'._DB_PREFIX_.'order_invoice_payment` WHERE id_order_invoice = '.(int)$id_invoice);
|
||||
if (!$payments)
|
||||
return array();
|
||||
|
||||
$payment_list = array();
|
||||
foreach ($payments as $payment)
|
||||
$payment_list[] = $payment['id_order_payment'];
|
||||
|
||||
$payments = new Collection('OrderPayment');
|
||||
$payments->where('id_order_invoice', '=', $id_invoice);
|
||||
$payments->where('id_order_payment', 'IN', $payment_list);
|
||||
return $payments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return order invoice object linked to the payment
|
||||
*
|
||||
* @param int $id_order Order Id
|
||||
*
|
||||
* @since 1.5.0.13
|
||||
*/
|
||||
public function getOrderInvoice($id_order)
|
||||
{
|
||||
$res = Db::getInstance()->getValue('
|
||||
SELECT id_order_invoice
|
||||
FROM `'._DB_PREFIX_.'order_invoice_payment`
|
||||
WHERE id_order_payment = '.(int)$this->id.'
|
||||
AND id_order = '.(int)$id_order);
|
||||
|
||||
if (!$res)
|
||||
return false;
|
||||
|
||||
return new OrderInvoice($res['id_order_invoice']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user