?php
/**
* @package storeCredit
* @copyright Copyright 2007 Kath Chapman
* @copyright Portions Copyright 2003-2006 Zen Cart Development Team
* @copyright Portions Copyright 2003 osCommerce
* @license
http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
*/
class ot_sc {
var $title, $output;
function ot_sc() {
global $currencies;
$this->code = 'ot_sc';
$this->title = MODULE_ORDER_TOTAL_SC_TITLE;
$this->header = MODULE_ORDER_TOTAL_SC_HEADER;
$this->description = MODULE_ORDER_TOTAL_SC_DESCRIPTION;
$this->user_prompt = MODULE_ORDER_TOTAL_SC_USER_PROMPT;
$this->sort_order = MODULE_ORDER_TOTAL_SC_SORT_ORDER;
$this->credit_class = true;
$_SESSION['cot_sc'] = $this->get_user_balance($_SESSION['customer_id']);
$this->checkbox = $this->user_prompt . '<input type="textfield" size="6" onchange="submitFunction()" name="cot_sc" value="' . number_format($_SESSION['cot_sc'], 2) . '" onfocus="if (this.value == \'' . number_format($_SESSION['cot_sc'], 2) . '\') this.value = \'\';" />' . ($_SESSION['cot_sc'] > 0 ? '<br />' . MODULE_ORDER_TOTAL_SC_USER_BALANCE . $currencies->format($_SESSION['cot_sc']) : '');
$this->output = array();
}
function process() {
global $order, $currencies;
if ($_SESSION['cot_sc']) {
$order_total = $this->get_order_total();
$od_amount = $this->calculate_credit($order_total);
$this->deduction = $od_amount;
$order->info['total'] = zen_round($order->info['total'] - $this->deduction, 2);
if ($od_amount > 0) {
$this->output[] = array('title' => $this->title . ':',
'text' => '-' . $currencies->format($this->deduction),
'value' => $this->deduction);
}
}
}
function clear_posts() {
unset($_SESSION['cot_sc']);
}
function selection_test() {
if ($this->get_user_balance($_SESSION['customer_id']) > 0) {
return true;
} else {
return false;
}
}
function pre_confirmation_check($order_total) {
global $order;
// clean out negative values and strip common currency symbols
$_SESSION['cot_sc'] = preg_replace('/[^0-9.%]/', '', $_SESSION['cot_sc']);
$_SESSION['cot_sc'] = abs($_SESSION['cot_sc']);
if ($_SESSION['cot_sc'] > 0) {
if (ereg('[^0-9/.]', trim($_SESSION['cot_sc']))) {
zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, 'credit_class_error_code=' . $this->code . '&credit_class_error=' . urlencode(TEXT_INVALID_REDEEM_AMOUNT), 'SSL',true, false));
}
if ($_SESSION['cot_sc'] > $this->get_user_balance($_SESSION['customer_id'])) {
zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, 'credit_class_error_code=' . $this->code . '&credit_class_error=' . urlencode(TEXT_INVALID_REDEEM_AMOUNT), 'SSL',true, false));
}
$od_amount = $this->calculate_credit($order_total);
if ($od_amount >= $order->info['total'] && MODULE_ORDER_TOTAL_SC_ORDER_STATUS_ID != 0) $order->info['order_status'] = MODULE_ORDER_TOTAL_SC_ORDER_STATUS_ID;
}
return $od_amount;
}
function use_credit_amount() {
if ($this->selection_test()) {
$output_string = $this->checkbox;
}
return $output_string;
}
function credit_selection() {
global $db, $currencies;
$gv_query = $db->Execute("select amount from " . TABLE_STORE_CREDIT . " where customer_id = ".$_SESSION['customer_id']);
if ($gv_query->$amount > 0 || $this->use_credit_amount()) {
$selection = array('id' => $this->code,
'module' => $this->title,
'redeem_instructions' => MODULE_ORDER_TOTAL_SC_INSTRUCTIONS,
'checkbox' => $this->use_credit_amount(),
'fields' => array(array(
'tag' => $this->code
)));
}
return $selection;
}
function apply_credit() {
global $db;
if(is_numeric($this->deduction)){
$db->execute("update " . TABLE_STORE_CREDIT . " set amount = amount - " .$this->deduction. " where customer_id=".$_SESSION['customer_id']);
}
}
function calculate_credit($amount) {
global $db, $order;
$gv_payment_amount = $_SESSION['cot_sc'];
$gv_amount = $gv_payment_amount;
$save_total_cost = $amount;
$full_cost = $save_total_cost - $gv_payment_amount;
if ($full_cost < 0) {
$full_cost = 0;
$gv_payment_amount = $save_total_cost;
}
return zen_round($gv_payment_amount,2);
}
function get_order_total() {
global $order;
$order_total = $order->info['total'];
return $order_total;
}
function collect_posts() {
}
function update_credit_account($i) {
}
function get_user_balance(){
global $db;
if(isset($_SESSION['customer_id'])){
$check_query = $db->Execute("select amount from " . TABLE_STORE_CREDIT . " where customer_id = ".$_SESSION['customer_id']);
return $check_query->fields['amount'];
}else{
return 0;
}
}
function check() {
global $db;
if (!isset($this->check)) {
$check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_ORDER_TOTAL_SC_STATUS'");
$this->check = $check_query->RecordCount();
}
return $this->check;
}
function keys() {
return array('MODULE_ORDER_TOTAL_SC_STATUS', 'MODULE_ORDER_TOTAL_SC_SORT_ORDER', 'MODULE_ORDER_TOTAL_SC_ORDER_STATUS_ID');
}
function install() {
global $db;
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('This module is installed', 'MODULE_ORDER_TOTAL_SC_STATUS', 'true', '', '6', '1','zen_cfg_select_option(array(\'true\'), ', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_SC_SORT_ORDER', '840', 'Sort order of display.', '6', '2', now())");
$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('Set Order Status', 'MODULE_ORDER_TOTAL_SC_ORDER_STATUS_ID', '0', 'Set the status of orders made where GV covers full payment', '6', '0', 'zen_cfg_pull_down_order_statuses(', 'zen_get_order_status_name', now())");
}
function remove() {
global $db;
$db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
}
}
?>