name = 'ant_refund_discount'; $this->tab = 'administration'; $this->author = 'Antadis'; $this->version = '1.0'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Refund discount'); $this->description = $this->l('Re-create discount when an order is refund'); } public function install() { // Register hooks if(!(parent::install() && $this->registerHook('orderSlip'))) { return false; } return true; } public function hookOrderSlip($params) { $orderParams = $params['order']; $productList = $params['productList']; $qtyList = $params['qtyList']; $shippingBack = isset($params['shippingBack']) ? $params['shippingBack'] : false; if ($this->debuglog) file_put_contents(_PS_ROOT_DIR_.'/log/ant_refund_discount.log', 'Discount : '.$orderParams->total_discounts."\n"); if ($this->debuglog) file_put_contents(_PS_ROOT_DIR_.'/log/ant_refund_discount.log', print_r($params, 1), FILE_APPEND); // Select if a discount is associated to the order $nbDiscount = Db::getInstance()->getValue("SELECT count(*) AS nb FROM `"._DB_PREFIX_."order_discount` od WHERE od.`id_order`=".(int)$orderParams->id); // If we have no discount if ($nbDiscount == 0) { return; } // If "full" value is set to true, never check the order details $full = false; if (isset($params['full']) && $params['full'] === true) { $full = true; } // shippingBack if (!$full && !$shippingBack) { return; } $order = new Order($orderParams->id); // Product qty if ($full === false) { $orderProducts = $order->getProductsDetail(); if ($this->debuglog) file_put_contents(_PS_ROOT_DIR_.'/log/ant_refund_discount.log', count($orderProducts).' != '.count($productList)."\n", FILE_APPEND); // Check product line difference if (count($orderProducts) != count($productList)) { return; } if ($this->debuglog) file_put_contents(_PS_ROOT_DIR_.'/log/ant_refund_discount.log', print_r($orderProducts, 1), FILE_APPEND); // Check product quantity foreach ($orderProducts as $p) { if ($p['product_quantity'] != $qtyList[$p['id_order_detail']]) { return; } } } // Discount $discountUse = $order->getDiscounts(); if (count($discountUse) > 0) { foreach($discountUse as $d) { $this->copyDiscountAfterRefund($order, $d); } } if ($this->debuglog) file_put_contents(_PS_ROOT_DIR_.'/log/ant_refund_discount.log', 'FIN', FILE_APPEND); } protected function copyDiscountAfterRefund($order, $discount) { $languages = Language::getLanguages(); $item = new Discount($discount['id_discount']); if ($this->debuglog) file_put_contents(_PS_ROOT_DIR_.'/log/ant_refund_discount.log', print_r($item, 1), FILE_APPEND); // Re Create discount $voucher = new Discount(); $voucher->id_discount_type = $item->id_discount_type; foreach ($languages as $language) { $voucher->description[$language['id_lang']] = $item->description[$language['id_lang']]; } $voucher->value = (float) $item->value; $voucher->name = 'R-'.(int)($order->id); $voucher->id_customer = (int)($order->id_customer); $voucher->id_currency = (int)($order->id_currency); $voucher->quantity = 1; $voucher->quantity_per_user = 1; $voucher->cumulable = $item->cumulable; $voucher->cumulable_reduction = $item->cumulable_reduction; $voucher->minimal = (float) $voucher->value; $voucher->active = 1; $voucher->cart_display = $item->cart_display; $start = new DateTime($item->date_from); $end = new DateTime($item->date_to); $interval = $start->diff($end); $newDate = new DateTime('now'); $voucher->date_from = $newDate->format('Y-m-d H:i:s'); $newDate->add($interval); $voucher->date_to = $newDate->format('Y-m-d H:i:s'); if (!$voucher->validateFieldsLang(false) OR !$voucher->add()) { return false; } // Ok discount created, send mail $customer = new Customer($order->id_customer); // Vars $params = array( '{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{voucher_num}' => $voucher->name, ); Mail::Send((int)$order->id_lang, 'voucher_after_refund', Mail::l("New voucher after refund"), $params, $customer->email, $customer->firstname.' '.$customer->lastname, null, null, null, null, _PS_MAIL_DIR_, true); return $voucher; } }