<?php
/** SMS - Selling Made Simple
* Copyright 2007 by Kevin Grandon (hide@address.com)
* This project's homepage is: http://sellingmadesimple.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* BUT withOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**/
class EventController extends ModuleCouponsAppController {
var $uses = null;
function utilize_coupon()
{
global $order;
if(empty($_POST['module_coupon_code']))
return;
loadModel('ModuleCoupon');
$this->ModuleCoupon =& new ModuleCoupon();
$coupon = $this->ModuleCoupon->find(array('code' => $_POST['module_coupon_code']));
// Check restrictions
if(count($order['OrderProduct']) < $coupon['ModuleCoupon']['min_product_count'])
$invalid_msg = 'Not enough products in cart for coupon. Requires: ' . $coupon['ModuleCoupon']['min_product_count'] . ' products.';
elseif(count($order['OrderProduct']) > $coupon['ModuleCoupon']['max_product_count'])
$invalid_msg = 'Too many products in cart for coupon. Requires less than: ' . $coupon['ModuleCoupon']['min_product_count'] . ' products.';
elseif($order['Order']['total'] < $coupon['ModuleCoupon']['min_order_total'])
$invalid_msg = 'Order total not enough. Requires at least: ' . $coupon['ModuleCoupon']['min_order_total'] . '.';
elseif($order['Order']['total'] > $coupon['ModuleCoupon']['max_order_total'])
$invalid_msg = 'Order total too high. Requires less than: ' . $coupon['ModuleCoupon']['min_order_total'] . '.';
if(isset($invalid_msg))
{
echo '<div class="error">' . $invalid_msg . '</div>';
return;
}
// Take off the discounts
$discount = 0;
if($coupon['ModuleCoupon']['percent_off_total'] > 0)
$discount = $discount - (($coupon['ModuleCoupon']['percent_off_total']/100)*$order['Order']['total']);
if($coupon['ModuleCoupon']['amount_off_total'] > 0)
$discount = $discount - $coupon['ModuleCoupon']['amount_off_total'];
if($coupon['ModuleCoupon']['free_shipping'] == 1)
$discount = $discount - $order['Order']['shipping'];
$coupon_product = array();
$coupon_product['OrderProduct']['order_id'] = $order['Order']['id'];
$coupon_product['OrderProduct']['order_id'] = $order['Order']['id'];
$coupon_product['OrderProduct']['name'] = 'Coupon: ' . $coupon['ModuleCoupon']['name'] . ' - ' . $coupon['ModuleCoupon']['code'];
$coupon_product['OrderProduct']['quantity'] = 1;
$coupon_product['OrderProduct']['price'] = $discount;
// Get the content_id for the new product
loadModel('Content');
$this->Content =& new Content();
$content_page = $this->Content->findByAlias('coupon-details');
$coupon_product['OrderProduct']['content_id'] = $content_page['Content']['id'];
// Load the OrderProduct model for saving and error checking
loadModel('OrderProduct');
$this->OrderProduct =& new OrderProduct();
// Make sure this coupon isn't already in our 'cart'
$coupon_count = $this->OrderProduct->findCount(array('order_id' => $order['Order']['id'], 'name' => $coupon_product['OrderProduct']['name']));
if($coupon_count > 0)
{
echo '<div class="error">Error: Coupon already used.</div>';
return;
}
// Save the new coupon as an order product
$this->OrderProduct->save($coupon_product);
// Save the new order totals
loadComponent('Order');
$this->Order =& new Order();
$order = $this->Order->read(null,$_SESSION['Customer']['order_id']);
$order['Order']['total'] = $order['Order']['total'] + $discount;
$this->Order->save($order);
}
}
?>