public function discounts() { $page = 1; if ($this->GetData('page')) { $page = $this->GetData('page'); } $user = Site::CurrentUser(); $id = mysql_real_escape_string($user->id); $discounts = LoyaltyDiscount::paginate("users.id = '{$id}'", "loyalty_discounts.id DESC", $page, 10); $this->assign('user', $user); $this->assign('discounts', $discounts); $this->title = 'Loyalty Points :: Discount Codes'; $this->render('loyalty_award/discounts.tpl'); }
public function convert_loyalty() { $balance = $this->loyalty_balance(); global $config; $expiry = strtotime($config['loyalty']['expiry'], time()); while ($balance > 0) { $voucher = $balance; if ($voucher > 1000) { $voucher = 1000; } $balance -= $voucher; $value = $voucher * $config['loyalty']['cash_value']; // Let's do this inside a transaction mysql_query('BEGIN'); $discount = new DiscountCode(); $discount->code = strtoupper(md5(uniqid() . mt_rand())); $discount->description = "Loyalty Discount for {$this->nickname}: {$voucher}pts"; $discount->type = "monetary"; $discount->quantity = 1; $discount->active = true; $discount->value = $value; $discount->expires = $expiry; $discount->full_cart_discount = true; $discount->loyalty = true; if (!$discount->save()) { mysql_query('ROLLBACK'); throw new Error500('Unable to save discount', $discount); } $ld = new LoyaltyDiscount(); $ld->discount_id = $discount->id; $ld->user_id = $this->id; $ld->discount_id = $discount->id; $ld->points_cash_value = $value; $ld->points = $voucher; if (!$ld->save()) { mysql_query('ROLLBACK'); throw new Error500('Unable to save LoyaltyDiscount', $discount); } } // Mark all points as redeemed $id = mysql_real_escape_string($this->id); $awards = LoyaltyAward::find_all("users.id = '{$id}' AND loyalty_awards.redeemed = 0"); foreach ($awards as $award) { $award->redeemed = true; if (!$award->save()) { mysql_query('ROLLBACK'); throw new Error500('Unable to save LoyaltyAward', $award); } } mysql_query('COMMIT'); }
public function mark_paid($payment = null, $note = null) { if ($note) { $this->note = $note; } foreach ($this->items() as $item) { $id = mysql_real_escape_string($this->id); $newitem = CartItem::find_by_id($id); $result = $item->object->mark_paid(true); $item->paid = true; $item->save(); } $this->paid = true; $this->save(); $id = mysql_real_escape_string($this->id); $redemptions = DiscountRedemption::find_all("discount_redemptions.cart_id = '{$id}'"); foreach ($redemptions as $redemption) { $discount_id = mysql_real_escape_string($redemption->discount_id); $user_id = mysql_real_escape_string($this->user_id); $loyalty_discounts = LoyaltyDiscount::find_all("loyalty_discounts.discount_id = '{$discount_id}' AND loyalty_discounts.user_id = '{$user_id}'"); foreach ($loyalty_discounts as $loyalty_discount) { $loyalty_discount->spent = true; $loyalty_discount->save(); } } }
* Loyalty Discount Module * * @author Steven Enten <*****@*****.**> * @copyright 2015 Steven Enten * @license http://opensource.org/licenses/MIT */ if (!defined('_PS_VERSION_')) { exit; } if (!defined('PS_MODULE_LOYALTYDISCOUNT')) { define('PS_MODULE_LOYALTYDISCOUNT', 'loyaltydiscount'); } if (!defined('PS_MODULE_LOYALTYDISCOUNT_PATH')) { define('PS_MODULE_LOYALTYDISCOUNT_PATH', _PS_MODULE_DIR_ . DIRECTORY_SEPARATOR . PS_MODULE_LOYALTYDISCOUNT); } include_once LoyaltyDiscount::buildFilePath('classes', 'LoyaltyDiscount.php'); class LoyaltyDiscount extends Module { const MODULE_NAME = PS_MODULE_LOYALTYDISCOUNT; const MODULE_TITLE = 'Loyalty Discount'; const MODULE_AUTHOR = 'Steven Enten'; const MODULE_VERSION = '0.0.1'; const PS_MIN_VERSION = '1.6'; const INSTALL_SQL_FILE = 'install.sql'; const UNINSTALL_SQL_FILE = 'uninstall.sql'; const ADMIN_CONTROLLER = 'AdminLoyaltyDiscount'; const ADMIN_TAB_PARENT = 'AdminPriceRule'; public function __construct() { $this->name = self::MODULE_NAME; $this->tab = 'pricing_promotion';
public function report() { $expiry_data = LoyaltyDiscount::retrieve_points_by_expiry(); $this->assign("expiry_data", $expiry_data); $this->title = "Loyalty Reporting"; $this->render("loyalty_award/report.tpl"); }
public function redeem($cart) { // Check the discount is valid if (!$this->is_valid()) { throw new Error500('Discount is not valid'); } // Check the cart hasn't been fully discounted already if ($cart->has_exclusive_discount()) { throw new Error500('This cart cannot have any more discounts'); } if ($this->exclusive && count($cart->discounts()) > 0) { throw new Error500('You cannot add this discount while there are other discounts'); } // Grab all cart items referenced by a discount $id = mysql_real_escape_string($cart->id); $discounts = DiscountRedemption::find_all("carts.id = '{$id}'"); foreach ($discounts as $discount) { if ($discount->discount->id == $this->id) { throw new Error500('That discount has already been added'); } } if ($this->loyalty) { $user_id = mysql_real_escape_string($cart->user_id); $id = mysql_real_escape_string($this->id); $ld = LoyaltyDiscount::find("users.id = '{$user_id}' AND discount_codes.id = '{$id}'"); if (!$ld) { throw new Error500('You are not allowed to use this discount'); } } // If this is a full cart discount, apply it if ($this->full_cart_discount) { $this->add_for_cart($cart); return true; } // Let's get some sorted discount items $services = array(); foreach ($this->services() as $item) { $services[] = $item->item->id; } $tickets = array(); foreach ($this->tickets() as $item) { $tickets[] = $item->item->id; } $discounted = false; $count = $this->count_redemptions(); $availableQuantity = $this->quantity - $count; foreach ($cart->items() as $item) { if ($availableQuantity <= 0) { break; } // We now need to check if the item can be discounted if (is_a($item->object, 'EventSignup')) { // We need to check the item's EventTicket if (!in_array($item->object->event_ticket_id, $tickets)) { continue; } } elseif (is_a($item->object, 'EventService')) { // Check that the Service is allowed if (!in_array($item->object->service_id, $services)) { continue; } } $this->add_for_cart($cart, $item); $discounted = true; $availableQuantity--; } if (!$discounted) { throw new Error500('There is nothing that can be discounted'); } return true; }