getRow(' SELECT * FROM `'._DB_PREFIX_.'product_voucher` WHERE `id_product_voucher` = '.(int) $id_product_voucher.' '); if($full) { $languages = Language::getLanguages(FALSE); $languages_fields = array(); foreach($languages as $lang) { $result_lang = Db::getInstance()->getRow(' SELECT `mail`, `objet` FROM `'._DB_PREFIX_.'product_voucher_lang` WHERE `id_product_voucher` = '.(int) $id_product_voucher.' AND `id_lang` = '.(int) $lang['id_lang'].' '); $product_voucher['mail_object'][(int) $lang['id_lang']] = $result_lang['objet']; $product_voucher['mail'][(int) $lang['id_lang']] = $result_lang['mail']; } } return $product_voucher; } public static function createProductVoucher($name, $product_id, $active=0, $mail_object, $mail, $max_vouchers=-1) { $already_associate = self::testProductIdExists($product_id); if(!$already_associate) { $data = Db::getInstance()->autoExecute(_DB_PREFIX_.'product_voucher', array( 'name' => pSQL($name), 'product_id' => (int) $product_id, 'active' => (int) $active, 'max_vouchers' => (int) $max_vouchers, ), 'INSERT'); if($data) { $id_product_voucher = Db::getInstance()->Insert_ID(); $languages = Language::getLanguages(FALSE); foreach($languages as $lang) { Db::getInstance()->Execute(' INSERT INTO `'._DB_PREFIX_.'product_voucher_lang` (`id_product_voucher`,`objet`, `mail`, `id_lang`) VALUES( '.(int) $id_product_voucher.', "'.pSQL($mail_object[(int) $lang['id_lang']]).'", "'.pSQL($mail[(int) $lang['id_lang']], TRUE).'", '.(int) $lang['id_lang'].' ) '); } return $id_product_voucher; } } return FALSE; } public static function editProductVoucher($id_product_voucher, $name, $product_id, $active, $mail_object, $mail, $max_vouchers=-1) { Db::getInstance()->autoExecute(_DB_PREFIX_.'product_voucher', array( 'name' => pSQL($name), 'active' => (int) $active, 'product_id' => (int) $product_id, 'max_vouchers' => (int) $max_vouchers, ), 'UPDATE', 'id_product_voucher = '.(int) $id_product_voucher); $languages = Language::getLanguages(FALSE); foreach($languages as $key => $lang) { Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'product_voucher_lang` SET `objet` = "'.pSQL($mail_object[$lang['id_lang']]).'", `mail` = "'.pSQL($mail[$lang['id_lang']], TRUE).'" WHERE `id_product_voucher` = '.(int) $id_product_voucher.' AND `id_lang` = '.(int) $lang['id_lang'].' '); } return TRUE; } public static function testProductIdExists($product_id) { return (bool) Db::getInstance()->getValue(' SELECT `id_product_voucher` FROM `'._DB_PREFIX_.'product_voucher` WHERE `product_id` = '.(int) $product_id.' '); } public static function associateVouchers($id_product_voucher, $code) { return (bool) Db::getInstance()->autoExecute(_DB_PREFIX_.'product_voucher_item', array( 'id_product_voucher' => (int) $id_product_voucher, 'code' => pSQL($code), 'id_order' => 0, 'statut' => self::AVAILABLE, ), 'INSERT'); } public static function getProductVoucherItems($id_product_voucher, $total=FALSE, $statut=0) { if($total) { return Db::getInstance()->getValue(' SELECT COUNT(`code`) as `numb` FROM `'._DB_PREFIX_.'product_voucher_item` WHERE `id_product_voucher` = '.(int) $id_product_voucher.' '.($statut != 0? 'AND `statut` = '.(int) $statut: '').' '); } else { return Db::getInstance()->executeS(' SELECT * FROM `'._DB_PREFIX_.'product_voucher_item` WHERE `id_product_voucher` = '.(int) $id_product_voucher.' '.($statut != 0? 'AND `statut` = '.(int) $statut: '').' '); } } public static function getExportProduct($id_product_voucher, $statut) { $vouchers = ProductVouchers::getProductVoucherItems($id_product_voucher, FALSE, (int) $statut); foreach($vouchers as $key => $voucher) { if($voucher['id_order'] != 0) { $result = Db::getInstance()->getRow(' SELECT c.`id_customer`, c.`email`, o.`date_add` FROM `'._DB_PREFIX_.'customer` c LEFT JOIN `'._DB_PREFIX_.'orders` o ON c.`id_customer` = o.`id_customer` WHERE o.`id_order` = '.(int) $voucher['id_order'].' '); $vouchers[$key]['id_customer'] = $result['id_customer']; $vouchers[$key]['email'] = $result['email']; $vouchers[$key]['date_add'] = $result['date_add']; } else { $vouchers[$key]['id_customer'] = ''; $vouchers[$key]['email'] = ''; $vouchers[$key]['date_add'] = ''; } } return $vouchers; } public static function getVouchersCodeByOrder($id_order) { return Db::getInstance()->executeS(' SELECT i.`code`, i.`statut`, v.`product_id` FROM `'._DB_PREFIX_.'product_voucher_item` i LEFT JOIN `'._DB_PREFIX_.'product_voucher` v ON i.`id_product_voucher` = v.`id_product_voucher` WHERE `id_order` = '.(int) $id_order.' '); } public static function getProductEligibleForOrder($id_order) { return Db::getInstance()->executeS(' SELECT od.`product_id`, od.`product_quantity`, p.`id_product_voucher` FROM `'._DB_PREFIX_.'order_detail` od RIGHT JOIN `'._DB_PREFIX_.'product_voucher` p ON p.`product_id` = od.`product_id` WHERE p.`active` = 1 AND od.`id_order` ='.(int) $id_order.' '); } public static function associateCodeToOrder($id_product_voucher, $quantity, $id_order) { for($i = 1; $i <= (int) $quantity; $i++) { Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'product_voucher_item` SET `id_order` = '.(int) $id_order.', `statut` = '.(int) self::ACTIVE.' WHERE `id_order` = 0 AND `id_product_voucher` = '.(int) $id_product_voucher.' LIMIT 1 '); $code = self::getCodeToSend($id_product_voucher, $id_order); if (!empty($code)) { self::sendMail($id_product_voucher, $id_order, $code); } else { // ALERT ! = Send mail (aucun bon dispo) mail('thibault@antadis.com', 'bon produit indisponible', 'Aucun bon disponible pour une commande '. (int) $id_product_voucher); return FALSE; } } } public static function getCodeToSend($id_product_voucher, $id_order) { return Db::getInstance()->getValue(' SELECT `code` FROM `'._DB_PREFIX_.'product_voucher_item` i LEFT JOIN `'._DB_PREFIX_.'product_voucher` v ON v.`id_product_voucher` = i.`id_product_voucher` WHERE i.`id_product_voucher` = '.(int)$id_product_voucher.' AND `id_order` = '.(int)$id_order.' ORDER BY i.`id_product_voucher_item` DESC '); } public static function getCodeAvailable($id_product_voucher, $id_order=0) { return Db::getInstance()->getRow(' SELECT i.`id_product_voucher`, `code` FROM `'._DB_PREFIX_.'product_voucher_item` i LEFT JOIN `'._DB_PREFIX_.'product_voucher` v ON v.`id_product_voucher` = i.`id_product_voucher` WHERE i.`id_product_voucher` = '.(int) $id_product_voucher.' AND `id_order` = '.$id_order.' '); } public static function getQuantityRefundbyProduct($id_order) { return Db::getInstance()->executeS(' SELECT `product_id`, `product_quantity`, `product_quantity_refunded` FROM `'._DB_PREFIX_.'order_detail` WHERE `id_order` ='.(int) $id_order.' '); } public static function refundCode($product_id, $id_order, $quantity) { foreach(Db::getInstance()->executeS(' SELECT `id_product_voucher_item` as item FROM `'._DB_PREFIX_.'product_voucher_item` i LEFT JOIN `'._DB_PREFIX_.'product_voucher` v ON i.`id_product_voucher` = v.`id_product_voucher` WHERE i.`id_order` = '.(int) $id_order.' AND v.`product_id` = '.(int) $product_id.' LIMIT '.(int) $quantity.' ') as $item) { Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'product_voucher_item` SET `statut` = '.self::REFUND.' WHERE `id_product_voucher_item` = '.(int) $item['item'].' '); } } public static function refundOrder($id_order) { Db::getInstance()->Execute(' UPDATE `'._DB_PREFIX_.'product_voucher_item` SET `statut` = '.self::REFUND.' WHERE `id_order` = '.(int) $id_order.' '); } public static function deleteProductVoucher($id_product_voucher) { if(!Db::getInstance()->Execute(' DELETE FROM `ps_product_voucher` WHERE `id_product_voucher` = '.(int) $id_product_voucher.' ')) { return FALSE; } if(!Db::getInstance()->Execute(' DELETE FROM `ps_product_voucher_lang` WHERE `id_product_voucher` = '.(int) $id_product_voucher.' ')) { return FALSE; } if(!Db::getInstance()->Execute(' DELETE FROM `ps_product_voucher_item` WHERE `id_product_voucher` = '.(int) $id_product_voucher.' ')) { return FALSE; } return TRUE; } public static function getDataMailByProductVoucher($id_product_voucher, $id_lang) { return Db::getInstance()->getRow(' SELECT `objet`, `mail` FROM `'._DB_PREFIX_.'product_voucher_lang` WHERE `id_product_voucher` = '.(int) $id_product_voucher.' AND `id_lang` ='.(int) $id_lang.' '); } public static function sendMail($id_product_voucher, $id_order, $code) { global $cookie; $vouchers = self::getDataMailByProductVoucher($id_product_voucher, (int) $cookie->id_lang); if(!empty($vouchers)) { $order = new Order($id_order); $customer = new Customer((int) $order->id_customer); $contenu_mail = str_replace("%bon%", $code, $vouchers['mail']); $contenu_mail = str_replace("%nom%", $customer->lastname, $contenu_mail); $contenu_mail = str_replace("%prenom%", $customer->firstname, $contenu_mail); $data = self::getProductVoucher((int) $id_product_voucher, FALSE); $product = new Product($data['product_id'], FALSE, (int) $cookie->id_lang); $sale = Sale::getSaleFromCategory((int) $product->id_category_default); $send = Mail::Send( $cookie->id_lang, 'product_voucher', $vouchers['objet'], array( '{id_sale}' => (int) $sale->id, '{message}' => $contenu_mail ), $customer->email, $customer->firstname.' '.$customer->lastname, (string) Configuration::get('PS_SHOP_EMAIL'), (string) Configuration::get('PS_SHOP_NAME'), NULL, NULL, dirname(__FILE__).'/mails/' ); } } /** * Récupère le nombre de bons achetés * @param int $id_product_voucher : id du bon produit * @param int|Customer|null $customer : client * @return int */ public static function getVouchersByMember($id_product_voucher, $customer=NULL) { if(!$customer) { global $cookie; $customer = $cookie->id_customer; } $customer = ($customer instanceof Customer)? $customer->id: $customer; $orders = array(); foreach(Db::getInstance()->executeS(' SELECT `id_order` FROM `'._DB_PREFIX_.'orders` WHERE `id_customer` = '.(int) $customer.' ') as $row) { $orders[] = (int) $row['id_order']; } return (int) Db::getInstance()->getValue(' SELECT COUNT(`id_product_voucher_item`) FROM `'._DB_PREFIX_.'product_voucher_item` WHERE `id_product_voucher` = '.(int) $id_product_voucher.' AND `id_order` IN ('.implode(', ', $orders).') AND `statut` =' . self::ACTIVE ); } /** * Récupère le nomdre de bons produits maximum par utilisateur * @param int $id_product_voucher : id du bon produit * @return int */ public static function getMaxAllowedVouchers($id_product_voucher) { return (int) Db::getInstance()->getValue(' SELECT `max_vouchers` FROM `'._DB_PREFIX_.'product_voucher` WHERE `id_product_voucher` = '.(int) $id_product_voucher.' '); } /** * Teste si l'utilisateur peut commander un bon produit en effectuant les calculs suivants : * - Bons déjà commandés + quantité souhaitée <= Nombre de bons autorisés par client * - Bons déjà dans le panier + quantité souhaitée <= Nombre de bons autorisés par client * @param int $id_product_voucher : id du bon produit * @param int $quantity : quantité souhaitée * @param Cookie $cookie : objet cookie, permettant de récupérer panier et client * @return bool */ public static function canOrderVoucher($id_product_voucher, $quantity, $cookie) { $voucher = self::getProductVoucher($id_product_voucher); $customerVouchers = self::getVouchersByMember($id_product_voucher, (int) $cookie->id_customer); $maxVouchers = self::getMaxAllowedVouchers($id_product_voucher); $currentCart = new Cart((int) $cookie->id_cart); $cartProducts = $currentCart->containsProduct((int) $voucher['product_id'], 0, NULL); return ($maxVouchers === 0) || ( ($customerVouchers + $quantity + $cartProducts['quantity'] <= $maxVouchers) && ((int) $cartProducts['quantity'] + $quantity <= $maxVouchers) ); } /** * Récupère un bon à partir d'un id de produit * @param int $id_product * @return int */ public static function getVoucherFromProduct($id_product) { return (int) Db::getInstance()->getValue(' SELECT `id_product_voucher` FROM `'._DB_PREFIX_.'product_voucher` WHERE `product_id` = '.(int) $id_product.' '); } }