177 lines
5.5 KiB
PHP
177 lines
5.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* GiftVoucherModule class, GiftVoucherModule.php
|
|
* Gift voucher module
|
|
* @category modules
|
|
*
|
|
* @author dids <prestashop@dids.fr>
|
|
* @copyright dids - des idées et des sites - 2009-2011
|
|
* @version 1.1.5
|
|
*
|
|
*/
|
|
|
|
class GiftVoucherModule extends ObjectModel
|
|
{
|
|
public $id_gift_voucher;
|
|
public $id_cart;
|
|
public $id_product;
|
|
public $id_order_detail;
|
|
public $id_discount;
|
|
public $value;
|
|
public $customize;
|
|
public $email;
|
|
public $name;
|
|
public $comment;
|
|
public $date_add;
|
|
|
|
protected $fieldsRequired = array('value', 'customize');
|
|
protected $fieldsSize = array(
|
|
/*'id_gift_voucher' => 8,
|
|
'id_cart' => 8,
|
|
'id_product' => 8,
|
|
'id_order_detail' => 8,
|
|
'id_discount' => 8,*/
|
|
'customize' => 1,
|
|
'email' => 128,
|
|
'name' => 64,
|
|
'comment' => 65535
|
|
);
|
|
protected $fieldsValidate = array(
|
|
'id_gift_voucher' => 'isUnsignedId',
|
|
'id_cart' => 'isUnsignedId',
|
|
'id_product' => 'isUnsignedId',
|
|
'id_order_detail' => 'isUnsignedId',
|
|
'id_discount' => 'isUnsignedId',
|
|
'value' => 'isFloat',
|
|
'customize' => 'isUnsignedId',
|
|
'email' => 'isEmail',
|
|
'name' => 'isName',
|
|
'comment' => 'isMessage',
|
|
'date_add' => 'isDate'
|
|
);
|
|
|
|
protected $table = 'gift_voucher';
|
|
protected $identifier = 'id_gift_voucher';
|
|
|
|
public function getFields()
|
|
{
|
|
parent::validateFields();
|
|
$fields['id_cart'] = $this->id_cart ? intval($this->id_cart) : null;
|
|
$fields['id_product'] = $this->id_product ? intval($this->id_product) : null;
|
|
$fields['id_order_detail'] = $this->id_order_detail ? intval($this->id_order_detail) : null;
|
|
$fields['id_discount'] = $this->id_discount ? intval($this->id_discount) : null;
|
|
$fields['value'] = floatval($this->value);
|
|
$fields['customize'] = intval($this->customize);
|
|
$fields['email'] = pSQL($this->email);
|
|
$fields['name'] = pSQL($this->name);
|
|
$fields['comment'] = pSQL($this->comment);
|
|
$fields['date_add'] = pSQL($this->date_add);
|
|
return $fields;
|
|
}
|
|
|
|
public function save($nullValues=true, $autodate=true)
|
|
{
|
|
return parent::save($nullValues, $autodate);
|
|
}
|
|
|
|
public function registerDiscount($order)
|
|
{
|
|
if ($this->id_discount) return false;
|
|
|
|
$configurations = Configuration::getMultiple(array(
|
|
'GIFT_VOUCHER_DISCOUNT_DESC',
|
|
'GIFT_VOUCHER_DISCOUNT_DURATION'
|
|
));
|
|
|
|
$discount = new Discount();
|
|
$discount->id_discount_type = 2; // Discount on order (amount)
|
|
$discount->value = $this->value;
|
|
if (property_exists(discount, "id_currency")) { // Prestashop v1.3.x
|
|
$discount->id_currency = $order->id_currency;
|
|
}
|
|
$discount->quantity = 1;
|
|
$discount->quantity_per_user = 1;
|
|
$discount->cumulable = 1;
|
|
$discount->cumulable_reduction = 1;
|
|
$discount->date_from = date('Y-m-d H:i:s', time());
|
|
$discount->date_to = date('Y-m-d H:i:s', mktime(0, 0, 0, date('m') + $configurations['GIFT_VOUCHER_DISCOUNT_DURATION'], date('d'), date('Y'))); // + n months
|
|
$discount->name = Tools::passwdGen(6);
|
|
$discount->description = Configuration::getInt('GIFT_VOUCHER_DISCOUNT_DESC');
|
|
if ($discount->add()) {
|
|
$this->id_discount = $discount->id;
|
|
return $this->save();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static public function getGiftVouchersFromCart($id_cart, $lang_id = 1, $all = true) {
|
|
if ($all) {
|
|
$query = '
|
|
SELECT gv.*, pl.name AS product_name
|
|
FROM `'._DB_PREFIX_.'gift_voucher` gv
|
|
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.id_product = gv.id_product)
|
|
WHERE gv.`id_cart` = '.intval($id_cart).' AND pl.`id_lang` = '.intval($lang_id);
|
|
} else {
|
|
$query = '
|
|
SELECT gv.*, pl.name AS product_name
|
|
FROM `'._DB_PREFIX_.'gift_voucher` gv
|
|
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.id_product = gv.id_product)
|
|
WHERE gv.`id_cart` = '.intval($id_cart).' AND gv.`customize` = 2 AND pl.`id_lang` = '.intval($lang_id);
|
|
}
|
|
|
|
return Db::getInstance()->ExecuteS($query);
|
|
}
|
|
|
|
static public function getGiftVouchersFromCartAndProduct($id_cart, $id_product) {
|
|
$query = '
|
|
SELECT gv.*
|
|
FROM `'._DB_PREFIX_.'gift_voucher` gv
|
|
WHERE gv.`id_cart` = '.intval($id_cart).' AND gv.`id_product` = '.intval($id_product);
|
|
|
|
return Db::getInstance()->ExecuteS($query);
|
|
}
|
|
|
|
static public function setNoInfoToGiftVouchersFromCart($id_cart) {
|
|
$query = '
|
|
UPDATE `'._DB_PREFIX_.'gift_voucher`
|
|
SET `customize` = 0
|
|
WHERE `customize` = 2 AND `id_cart` = '.intval($id_cart);
|
|
|
|
return Db::getInstance()->Execute($query);
|
|
}
|
|
|
|
static public function deleteGiftVouchersFromCartAndProduct($nb, $id_cart, $id_product) {
|
|
$query = '
|
|
DELETE FROM `'._DB_PREFIX_.'gift_voucher`
|
|
WHERE `id_cart` = '.intval($id_cart).' AND `id_product` = '.intval($id_product).'
|
|
ORDER BY `id_gift_voucher` DESC LIMIT '.intval($nb);
|
|
|
|
return Db::getInstance()->Execute($query);
|
|
}
|
|
|
|
static public function linkGiftVouchersToOrder($order) {
|
|
$gift_voucher_products_ids = preg_split("/,/", Configuration::get('GIFT_VOUCHER_PRODUCTS'));
|
|
$product_details = $order->getProductsDetail();
|
|
$result = true;
|
|
|
|
foreach ($product_details as $product_detail) {
|
|
if (in_array($product_detail["product_id"], $gift_voucher_products_ids)) {
|
|
$query = '
|
|
UPDATE `'._DB_PREFIX_.'gift_voucher`
|
|
SET `id_order_detail` = '.intval($product_detail["id_order_detail"]).'
|
|
WHERE
|
|
`id_order_detail` IS NULL
|
|
AND `id_cart` = '.intval($order->id_cart).'
|
|
AND `id_product` = '.intval($product_detail["product_id"]);
|
|
$result &= Db::getInstance()->Execute($query);
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
?>
|