[ Index ] |
|
Code source de Zen Cart E-Commerce Shopping Cart 1.3.7.1 |
1 <?php 2 /** 3 * @package orderTotal 4 * @copyright Copyright 2003-2006 Zen Cart Development Team 5 * @copyright Portions Copyright 2003 osCommerce 6 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0 7 * @version $Id: ot_gv.php 4975 2006-11-20 22:11:21Z wilt $ 8 */ 9 10 class ot_gv { 11 var $title, $output; 12 13 function ot_gv() { 14 global $currencies; 15 $this->code = 'ot_gv'; 16 $this->title = MODULE_ORDER_TOTAL_GV_TITLE; 17 $this->header = MODULE_ORDER_TOTAL_GV_HEADER; 18 $this->description = MODULE_ORDER_TOTAL_GV_DESCRIPTION; 19 $this->user_prompt = MODULE_ORDER_TOTAL_GV_USER_PROMPT; 20 $this->sort_order = MODULE_ORDER_TOTAL_GV_SORT_ORDER; 21 $this->include_shipping = MODULE_ORDER_TOTAL_GV_INC_SHIPPING; 22 $this->include_tax = MODULE_ORDER_TOTAL_GV_INC_TAX; 23 $this->calculate_tax = MODULE_ORDER_TOTAL_GV_CALC_TAX; 24 $this->credit_tax = MODULE_ORDER_TOTAL_GV_CREDIT_TAX; 25 $this->tax_class = MODULE_ORDER_TOTAL_GV_TAX_CLASS; 26 $this->show_redeem_box = MODULE_ORDER_TOTAL_GV_REDEEM_BOX; 27 $this->credit_class = true; 28 if (!zen_not_null(ltrim($_SESSION['cot_gv'], ' 0')) || $_SESSION['cot_gv'] == '0') $_SESSION['cot_gv'] = '0.00'; 29 $this->checkbox = $this->user_prompt . '<input type="text" size="6" onchange="submitFunction()" name="cot_gv" value="' . number_format($_SESSION['cot_gv'], 2) . '" onfocus="if (this.value == \'' . number_format($_SESSION['cot_gv'], 2) . '\') this.value = \'\';" />' . ($this->user_has_gv_account($_SESSION['customer_id']) > 0 ? '<br />' . MODULE_ORDER_TOTAL_GV_USER_BALANCE . $currencies->format($this->user_has_gv_account($_SESSION['customer_id'])) : ''); 30 $this->output = array(); 31 } 32 33 function process() { 34 global $order, $currencies; 35 if ($_SESSION['cot_gv']) { 36 $order_total = $this->get_order_total(); 37 $od_amount = $this->calculate_credit($order_total); 38 if ($this->calculate_tax != "none") { 39 $tod_amount = zen_round($this->calculate_tax_deduction($order_total, $od_amount, $this->calculate_tax, true), 2); 40 $od_amount = $this->calculate_credit($order_total); 41 } 42 $this->deduction = $od_amount + $tod_amount; 43 $order->info['total'] = zen_round($order->info['total'] - $this->deduction, 2); 44 if ($od_amount > 0) { 45 if (DISPLAY_PRICE_WITH_TAX == 'true') { 46 $this->deduction += zen_calculate_tax($this->deduction, $tax); 47 } 48 49 $this->output[] = array('title' => $this->title . ':', 50 'text' => '-' . $currencies->format($this->deduction), 51 'value' => $this->deduction); 52 } 53 } 54 } 55 56 function clear_posts() { 57 unset($_SESSION['cot_gv']); 58 } 59 function selection_test() { 60 if ($this->user_has_gv_account($_SESSION['customer_id'])) { 61 return true; 62 } else { 63 return false; 64 } 65 } 66 67 function pre_confirmation_check($order_total) { 68 global $order, $currencies; 69 // clean out negative values and strip common currency symbols 70 $_SESSION['cot_gv'] = preg_replace('/[^0-9.%]/', '', $_SESSION['cot_gv']); 71 $_SESSION['cot_gv'] = abs($_SESSION['cot_gv']); 72 73 if ($_SESSION['cot_gv'] > 0) { 74 if ($this->include_shipping == 'false') $order_total -= $order->info['shipping_cost']; 75 if ($this->include_tax == 'false') $order_total -= $order->info['tax']; 76 if (ereg('[^0-9/.]', trim($_SESSION['cot_gv']))) { 77 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)); 78 } 79 if ($_SESSION['cot_gv'] > $currencies->value($this->user_has_gv_account($_SESSION['customer_id']))) { 80 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)); 81 } 82 $od_amount = $this->calculate_credit($order_total); 83 if ($this->calculate_tax != "none") { 84 $tod_amount = $this->calculate_tax_deduction($order_total, $od_amount, $this->calculate_tax); 85 $od_amount = $this->calculate_credit($order_total)+$tod_amount; 86 } 87 if ($od_amount >= $order->info['total'] && MODULE_ORDER_TOTAL_GV_ORDER_STATUS_ID != 0) $order->info['order_status'] = MODULE_ORDER_TOTAL_GV_ORDER_STATUS_ID; 88 } 89 return $od_amount + $tod_amount; 90 } 91 92 function use_credit_amount() { 93 if ($this->selection_test()) { 94 $output_string = $this->checkbox; 95 } 96 return $output_string; 97 } 98 99 function update_credit_account($i) { 100 global $db, $order, $insert_id; 101 if (ereg('^GIFT', addslashes($order->products[$i]['model']))) { 102 $gv_order_amount = ($order->products[$i]['final_price'] * $order->products[$i]['qty']); 103 if ($this->credit_tax=='true') $gv_order_amount = $gv_order_amount * (100 + $order->products[$i]['tax']) / 100; 104 $gv_order_amount = $gv_order_amount * 100 / 100; 105 if (MODULE_ORDER_TOTAL_GV_QUEUE == 'false') { 106 // GV_QUEUE is false so release amount to account immediately 107 $gv_result = $this->user_has_gv_account($_SESSION['customer_id']); 108 $customer_gv = false; 109 $total_gv_amount = 0; 110 if ($gv_result) { 111 $total_gv_amount = $gv_result; 112 $customer_gv = true; 113 } 114 $total_gv_amount = $total_gv_amount + $gv_order_amount; 115 if ($customer_gv) { 116 $db->Execute("update " . TABLE_COUPON_GV_CUSTOMER . " set amount = '" . $total_gv_amount . "' where customer_id = '" . (int)$_SESSION['customer_id'] . "'"); 117 } else { 118 $db->Execute("insert into " . TABLE_COUPON_GV_CUSTOMER . " (customer_id, amount) values ('" . (int)$_SESSION['customer_id'] . "', '" . $total_gv_amount . "')"); 119 } 120 } else { 121 // GV_QUEUE is true - so queue the gv for release by store owner 122 $db->Execute("insert into " . TABLE_COUPON_GV_QUEUE . " (customer_id, order_id, amount, date_created, ipaddr) values ('" . (int)$_SESSION['customer_id'] . "', '" . (int)$insert_id . "', '" . $gv_order_amount . "', NOW(), '" . $_SERVER['REMOTE_ADDR'] . "')"); 123 } 124 } 125 } 126 127 function credit_selection() { 128 global $db, $currencies; 129 $gv_query = $db->Execute("select coupon_id from " . TABLE_COUPONS . " where coupon_type = 'G' and coupon_active='Y'"); 130 if ($gv_query->RecordCount() > 0 || $this->use_credit_amount()) { 131 $selection = array('id' => $this->code, 132 'module' => $this->title, 133 'redeem_instructions' => MODULE_ORDER_TOTAL_GV_REDEEM_INSTRUCTIONS, 134 'checkbox' => $this->use_credit_amount(), 135 'fields' => array(array('title' => MODULE_ORDER_TOTAL_GV_TEXT_ENTER_CODE, 136 'field' => zen_draw_input_field('gv_redeem_code', '', 'id="disc-'.$this->code.'" onchange="submitFunction(0,0)"'), 137 'tag' => 'disc-'.$this->code 138 ))); 139 140 } 141 return $selection; 142 } 143 144 function apply_credit() { 145 global $db, $order, $messageStack; 146 if ($_SESSION['cot_gv'] != 0) { 147 $gv_result = $db->Execute("select amount from " . TABLE_COUPON_GV_CUSTOMER . " where customer_id = '" . (int)$_SESSION['customer_id'] . "'"); 148 $gv_payment_amount = $this->deduction; 149 $gv_amount = $gv_result->fields['amount'] - $gv_payment_amount; 150 $db->Execute("update " . TABLE_COUPON_GV_CUSTOMER . " set amount = '" . $gv_amount . "' where customer_id = '" . (int)$_SESSION['customer_id'] . "'"); 151 } 152 $_SESSION['cot_gv'] = false; 153 return $gv_payment_amount; 154 } 155 156 157 function collect_posts() { 158 global $db, $currencies, $messageStack; 159 if (!$_POST['cot_gv']) $_SESSION['cot_gv'] = '0.00'; 160 if ($_POST['gv_redeem_code']) { 161 $gv_result = $db->Execute("select coupon_id, coupon_type, coupon_amount from " . TABLE_COUPONS . " where coupon_code = '" . zen_db_prepare_input($_POST['gv_redeem_code']) . "'"); 162 if ($gv_result->RecordCount() > 0) { 163 $redeem_query = $db->Execute("select * from " . TABLE_COUPON_REDEEM_TRACK . " where coupon_id = '" . (int)$gv_result->fields['coupon_id'] . "'"); 164 if ( ($redeem_query->RecordCount() > 0) && ($gv_result->fields['coupon_type'] == 'G') ) { 165 $messageStack->add_session('checkout_payment', ERROR_NO_INVALID_REDEEM_GV, error); 166 zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL')); 167 } 168 } else { 169 $messageStack->add_session('checkout_payment', ERROR_NO_INVALID_REDEEM_GV, error); 170 zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL')); 171 } 172 if ($gv_result->fields['coupon_type'] == 'G') { 173 $gv_amount = $gv_result->fields['coupon_amount']; 174 // Things to set 175 // ip address of claimant 176 // customer id of claimant 177 // date 178 // redemption flag 179 // now update customer account with gv_amount 180 $gv_amount_result=$db->Execute("select amount from " . TABLE_COUPON_GV_CUSTOMER . " where customer_id = '" . (int)$_SESSION['customer_id'] . "'"); 181 $customer_gv = false; 182 $total_gv_amount = $gv_amount;; 183 if ($gv_amount_result->RecordCount() > 0) { 184 $total_gv_amount = $gv_amount_result->fields['amount'] + $gv_amount; 185 $customer_gv = true; 186 } 187 $db->Execute("update " . TABLE_COUPONS . " set coupon_active = 'N' where coupon_id = '" . $gv_result->fields['coupon_id'] . "'"); 188 $db->Execute("insert into " . TABLE_COUPON_REDEEM_TRACK . " (coupon_id, customer_id, redeem_date, redeem_ip) values ('" . $gv_result->fields['coupon_id'] . "', '" . (int)$_SESSION['customer_id'] . "', now(),'" . $_SERVER['REMOTE_ADDR'] . "')"); 189 if ($customer_gv) { 190 // already has gv_amount so update 191 $db->Execute("update " . TABLE_COUPON_GV_CUSTOMER . " set amount = '" . $total_gv_amount . "' where customer_id = '" . (int)$_SESSION['customer_id'] . "'"); 192 } else { 193 // no gv_amount so insert 194 $db->Execute("insert into " . TABLE_COUPON_GV_CUSTOMER . " (customer_id, amount) values ('" . (int)$_SESSION['customer_id'] . "', '" . $total_gv_amount . "')"); 195 } 196 // zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_REDEEMED_AMOUNT. $currencies->format($gv_amount)), 'SSL')); 197 $messageStack->add_session('redemptions',ERROR_REDEEMED_AMOUNT. $currencies->format($gv_amount), 'success' ); 198 } 199 } 200 if ($_POST['submit_redeem_x'] && $gv_result->fields['coupon_type'] == 'G') zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_NO_REDEEM_CODE), 'SSL')); 201 } 202 203 function calculate_credit($amount) { 204 global $db, $order, $currencies; 205 $gv_payment_amount = $currencies->value($_SESSION['cot_gv'], true, DEFAULT_CURRENCY); 206 $gv_amount = $gv_payment_amount; 207 $save_total_cost = $amount; 208 $full_cost = $save_total_cost - $gv_payment_amount; 209 if ($full_cost < 0) { 210 $full_cost = 0; 211 $gv_payment_amount = $save_total_cost; 212 } 213 return zen_round($gv_payment_amount,2); 214 } 215 216 function calculate_tax_deduction($amount, $od_amount, $method, $finalise = false) { 217 global $order; 218 $tax_address = zen_get_tax_locations(); 219 switch ($method) { 220 case 'Standard': 221 $ratio1 = zen_round($od_amount / $amount,2); 222 $tod_amount = 0; 223 reset($order->info['tax_groups']); 224 while (list($key, $value) = each($order->info['tax_groups'])) { 225 $tax_rate = zen_get_tax_rate_from_desc($key, $tax_address['country_id'], $tax_address['zone_id']); 226 $total_net += $tax_rate * $value; 227 } 228 if ($od_amount > $total_net) $od_amount = $total_net; 229 reset($order->info['tax_groups']); 230 while (list($key, $value) = each($order->info['tax_groups'])) { 231 $tax_rate = zen_get_tax_rate_from_desc($key, $tax_address['country_id'], $tax_address['zone_id']); 232 $net = $tax_rate * $value; 233 if ($net > 0) { 234 $god_amount = $value * $ratio1; 235 $tod_amount += $god_amount; 236 if ($finalise) $order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount; 237 } 238 } 239 if ($finalise) $order->info['tax'] -= $tod_amount; 240 if ($finalise) $order->info['total'] -= $tod_amount; 241 break; 242 case 'Credit Note': 243 $tax_rate = zen_get_tax_rate($this->tax_class, $tax_address['country_id'], $tax_address['zone_id']); 244 $tax_desc = zen_get_tax_description($this->tax_class, $tax_address['country_id'], $tax_address['zone_id']); 245 $tod_amount = $this->deduction / (100 + $tax_rate)* $tax_rate; 246 if ($finalise) $order->info['tax_groups'][$tax_desc] -= $tod_amount; 247 if ($finalise) $order->info['tax'] -= $tod_amount; 248 if ($finalise) $order->info['total'] -= $tod_amount; 249 break; 250 default: 251 } 252 return $tod_amount; 253 } 254 255 function user_has_gv_account($c_id) { 256 global $db; 257 $gv_result = $db->Execute("select amount from " . TABLE_COUPON_GV_CUSTOMER . " where customer_id = '" . (int)$c_id . "'"); 258 if ($gv_result->RecordCount() > 0) { 259 return $gv_result->fields['amount']; 260 } 261 // return false; 262 return 0; // was preventing checkout_payment from continuing 263 } 264 265 function get_order_total() { 266 global $order; 267 $order_total = $order->info['total']; 268 if ($this->include_tax == 'false') $order_total = $order_total - $order->info['tax']; 269 if ($this->include_shipping == 'false') $order_total = $order_total - $order->info['shipping_cost']; 270 271 return $order_total; 272 } 273 274 function check() { 275 global $db; 276 if (!isset($this->check)) { 277 $check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_ORDER_TOTAL_GV_STATUS'"); 278 $this->check = $check_query->RecordCount(); 279 } 280 281 return $this->check; 282 } 283 284 function keys() { 285 return array('MODULE_ORDER_TOTAL_GV_STATUS', 'MODULE_ORDER_TOTAL_GV_SORT_ORDER', 'MODULE_ORDER_TOTAL_GV_QUEUE', 'MODULE_ORDER_TOTAL_GV_INC_SHIPPING', 'MODULE_ORDER_TOTAL_GV_INC_TAX', 'MODULE_ORDER_TOTAL_GV_CALC_TAX', 'MODULE_ORDER_TOTAL_GV_TAX_CLASS', 'MODULE_ORDER_TOTAL_GV_CREDIT_TAX', 'MODULE_ORDER_TOTAL_GV_ORDER_STATUS_ID'); 286 } 287 288 function install() { 289 global $db; 290 $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_GV_STATUS', 'true', '', '6', '1','zen_cfg_select_option(array(\'true\'), ', now())"); 291 $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_GV_SORT_ORDER', '840', 'Sort order of display.', '6', '2', now())"); 292 $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Queue Purchases', 'MODULE_ORDER_TOTAL_GV_QUEUE', 'true', 'Do you want to queue purchases of the Gift Voucher?', '6', '3','zen_cfg_select_option(array(\'true\', \'false\'), ', now())"); 293 $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Include Shipping', 'MODULE_ORDER_TOTAL_GV_INC_SHIPPING', 'true', 'Include Shipping in calculation', '6', '5', 'zen_cfg_select_option(array(\'true\', \'false\'), ', now())"); 294 $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Include Tax', 'MODULE_ORDER_TOTAL_GV_INC_TAX', 'false', 'Include Tax in calculation.', '6', '6','zen_cfg_select_option(array(\'true\', \'false\'), ', now())"); 295 $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Re-calculate Tax', 'MODULE_ORDER_TOTAL_GV_CALC_TAX', 'None', 'Re-Calculate Tax', '6', '7','zen_cfg_select_option(array(\'None\', \'Standard\', \'Credit Note\'), ', now())"); 296 $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_ORDER_TOTAL_GV_TAX_CLASS', '0', 'Use the following tax class when treating Gift Voucher as Credit Note.', '6', '0', 'zen_get_tax_class_title', 'zen_cfg_pull_down_tax_classes(', now())"); 297 $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Credit including Tax', 'MODULE_ORDER_TOTAL_GV_CREDIT_TAX', 'false', 'Add tax to purchased Gift Voucher when crediting to Account', '6', '8','zen_cfg_select_option(array(\'true\', \'false\'), ', now())"); 298 $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_GV_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())"); 299 } 300 301 function remove() { 302 global $db; 303 $db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')"); 304 } 305 } 306 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 16:45:43 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |