From bf92a5a7bc475b5be3340fe9b04764afecb68699 Mon Sep 17 00:00:00 2001 From: lLefevre Date: Tue, 10 Jul 2012 10:03:35 +0000 Subject: [PATCH] [*] BO : #PSCFV-3007 : Now you can create a voucher when you create a partial refund on the BO --- .../controllers/orders/helpers/view/view.tpl | 1 + controllers/admin/AdminOrdersController.php | 60 +++++++++++++++++-- mails/en/lang.php | 2 +- mails/es/lang.php | 1 - 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/admin-dev/themes/default/template/controllers/orders/helpers/view/view.tpl b/admin-dev/themes/default/template/controllers/orders/helpers/view/view.tpl index 13ddf7b23..153885679 100755 --- a/admin-dev/themes/default/template/controllers/orders/helpers/view/view.tpl +++ b/admin-dev/themes/default/template/controllers/orders/helpers/view/view.tpl @@ -730,6 +730,7 @@ diff --git a/controllers/admin/AdminOrdersController.php b/controllers/admin/AdminOrdersController.php index d5567da69..4a2529eb6 100755 --- a/controllers/admin/AdminOrdersController.php +++ b/controllers/admin/AdminOrdersController.php @@ -459,24 +459,76 @@ class AdminOrdersControllerCore extends AdminController foreach ($_POST['partialRefundProduct'] as $id_order_detail => $amount_detail) { $order_detail_list[$id_order_detail]['quantity'] = (int)$_POST['partialRefundProductQuantity'][$id_order_detail]; - + if (empty($amount_detail)) { - $orderDetail = new OrderDetail($id_order_detail); - $order_detail_list[$id_order_detail]['amount'] = $orderDetail->unit_price_tax_incl * $order_detail_list[$id_order_detail]['quantity']; + $order_detail = new OrderDetail((int)$id_order_detail); + $order_detail_list[$id_order_detail]['amount'] = $order_detail->unit_price_tax_incl * $order_detail_list[$id_order_detail]['quantity']; } else $order_detail_list[$id_order_detail]['amount'] = (float)$amount_detail; + $amount += $order_detail_list[$id_order_detail]['amount']; } + $shipping_cost_amount = (float)Tools::getValue('partialRefundShippingCost'); if ($shipping_cost_amount > 0) $amount += $shipping_cost_amount; - + if ($amount > 0) { if (!OrderSlip::createPartialOrderSlip($order, $amount, $shipping_cost_amount, $order_detail_list)) $this->errors[] = Tools::displayError('Cannot generate partial credit slip'); + + // Generate voucher + if (Tools::isSubmit('generateDiscountRefund') && !count($this->errors)) + { + $cart_rule = new CartRule(); + $cart_rule->description = sprintf($this->l('Credit Slip for order #%d'), $order->id); + $languages = Language::getLanguages(false); + foreach ($languages as $language) + // Define a temporary name + $cart_rule->name[$language['id_lang']] = sprintf('V0C%1$dO%2$d', $order->id_customer, $order->id); + + // Define a temporary code + $cart_rule->code = sprintf('V0C%1$dO%2$d', $order->id_customer, $order->id); + $cart_rule->quantity = 1; + $cart_rule->quantity_per_user = 1; + + // Specific to the customer + $cart_rule->id_customer = $order->id_customer; + $now = time(); + $cart_rule->date_from = date('Y-m-d H:i:s', $now); + $cart_rule->date_to = date('Y-m-d H:i:s', $now + (3600 * 24 * 365.25)); /* 1 year */ + $cart_rule->active = 1; + + $cart_rule->reduction_amount = $amount; + $cart_rule->minimum_amount_currency = $order->id_currency; + $cart_rule->reduction_currency = $order->id_currency; + + if (!$cart_rule->add()) + $this->errors[] = Tools::displayError('Cannot generate voucher'); + else + { + // Update the voucher code and name + foreach ($languages as $language) + $cart_rule->name[$language['id_lang']] = sprintf('V%1$dC%2$dO%3$d', $cart_rule->id, $order->id_customer, $order->id); + $cart_rule->code = sprintf('V%1$dC%2$dO%3$d', $cart_rule->id, $order->id_customer, $order->id); + + if (!$cart_rule->update()) + $this->errors[] = Tools::displayError('Cannot generate voucher'); + else + { + $currency = $this->context->currency; + $params['{voucher_amount}'] = Tools::displayPrice($cart_rule->reduction_amount, $currency, false); + $params['{voucher_num}'] = $cart_rule->code; + $customer = new Customer((int)$order->id_customer); + @Mail::Send((int)$order->id_lang, 'voucher', sprintf(Mail::l('New voucher regarding your order %s', (int)$order->id_lang), $order->reference), + $params, $customer->email, $customer->firstname.' '.$customer->lastname, null, null, null, + null, _PS_MAIL_DIR_, true); + } + } + } } else $this->errors[] = Tools::displayError('You have to write an amount if you want to do a partial credit slip'); diff --git a/mails/en/lang.php b/mails/en/lang.php index 8755208e4..4b14bedc6 100644 --- a/mails/en/lang.php +++ b/mails/en/lang.php @@ -43,7 +43,7 @@ $_LANGMAIL['Your order return state has changed'] = 'Your order return status ha $_LANGMAIL['Your password'] = 'Your password has been reset'; $_LANGMAIL['Password query confirmation'] = 'Forgot password'; $_LANGMAIL['An answer to your message is available'] = 'New response to your inquiry'; -$_LANGMAIL['New voucher regarding your order'] = 'New voucher for your order'; +$_LANGMAIL['New voucher regarding your order %s'] = 'New voucher for your order %s'; $_LANGMAIL['Happy birthday!'] = 'Happy birthday!'; $_LANGMAIL['Newsletter confirmation'] = 'Newsletter subscription confirmation'; $_LANGMAIL['Newsletter voucher'] = 'Newsletter voucher'; diff --git a/mails/es/lang.php b/mails/es/lang.php index 2c71868a5..478373822 100644 --- a/mails/es/lang.php +++ b/mails/es/lang.php @@ -18,7 +18,6 @@ $_LANGMAIL['New message regarding your order'] = ''; $_LANGMAIL['Your order return state has changed'] = 'El estado de retorno de su orden ha cambiado'; $_LANGMAIL['Your new admin password'] = 'Su nueva clave de administrador'; $_LANGMAIL['Password query confirmation'] = 'Confirmación de password'; -$_LANGMAIL['New voucher regarding your order '] = 'Nuevo bono con respecto a su orden'; $_LANGMAIL['An answer to your message is available #ct%1$s #tc%2$s'] = ''; $_LANGMAIL['New voucher regarding your order %s'] = 'Nuevo bono con respecto a su orden %s'; $_LANGMAIL['Newsletter confirmation'] = '';