Recommended Posts

Hello,

I am trying to find a discount module based on the real margin and the total order amount.

I have installed margin report, I have also quantity price breaking but I would like to make or find a contribution that :

- calculate the total margin of the customer basket (OK thanks to margin report)

- and then with the order total adjust a discount amount in order to be sure to ever have a minimum margin

For example the customer has choosen more than 1 product

Case 1 :

let say for example margin = 250€ and order total amount = 500 € + VAT 19% = 595€

I want to give a discount to the customer that let me have 30% margin on the total amount for Order total < 1000€

The addon must verify Order_total < 1000 € then ==> Calculate :Discount amount = Margin - (30% * Order_total) for this sample : = 250€ - (30%*595) = 71.50€ and perhaps round this amount to 70€ in order the customer can not calculate what happens behind.

I would like to define the minimum margin I want for each values of order_total

if order total <1000€ mimimum margin = 30%

if order total <2000€ minimum margin = 25%

If ...................<........minimum margin = x%

and so on

I have found so much discount addons but not this kind.

I would like some help in order to make this contribution.

Thanks!

Share on other sites

Hello,

I am looking inside the codes for shipping and weight calculations. (Yes, I am not so good in doing all myself, and far to be perfect in php)

the idea is to calculate total margin the same tilme is calculated the total weight.

Is someone having ideas ?

Thanks

Share on other sites

Hello,

Well I've tryed out all those week end to "re-touch" some existing codes to obtain my goal,

results are very various and sometimes the results were surprising, I've retouched ot_lev_discount code to obtain the code hereunder.

To figure out the results

to view were the mistake is I have changed the result value withe the 2 parts of the result in function calculate_credit()

\$od_amount = \$b; /* this is to view order cost value , replace with \$a for the order total value

As I have also "quantity price break" and "separate pricing per customer" something is wrong but I must first check this step.

The problem is order cost ( amount I Pay is not correct value, missing the half part of products) AND (order total is not correct (also missing a part of product as I have tried with a product with quantity break (this one is missing)

For both order cost and order total I want to have only the part of products without attributes.

Any help is appreciated.

Thanks

```<?php
/*
<<<<<<< ot_margin_discount.php
\$Id: ot_margin_discount.php,v 1.0 2002/04/08 01:13:43 hpdl Exp \$
=======
\$Id: ot_margin_discount.php,v 1.3 2002/09/04 22:49:11 wilt Exp \$
\$Id: ot_margin_discount.php,v 2.4 2006/02/28 12:10:01 maniac101 Exp \$
modified to calc discount correctly when tax is included in discount
>>>>>>> 2.4

osCommerce, Open Source E-Commerce Solutions
http://www.oscommerce.com

Released under the GNU General Public License
*/

class ot_margin_discount {
var \$title, \$output;

function ot_margin_discount() {
\$this->code = 'ot_margin_discount';
\$this->title = MODULE_MARGIN_DISCOUNT_TITLE;
\$this->description = MODULE_MARGIN_DISCOUNT_DESCRIPTION;
\$this->enabled = MODULE_MARGIN_DISCOUNT_STATUS;
\$this->sort_order = MODULE_MARGIN_DISCOUNT_SORT_ORDER;
\$this->include_shipping = MODULE_MARGIN_DISCOUNT_INC_SHIPPING;
\$this->include_tax = MODULE_MARGIN_DISCOUNT_INC_TAX;
\$this->calculate_tax = MODULE_MARGIN_DISCOUNT_CALC_TAX;
\$this->table = MODULE_MARGIN_DISCOUNT_TABLE;
//	  \$this->credit_class = true;
\$this->output = array();
}

function process() {
global \$order, \$ot_subtotal, \$currencies;
\$od_amount = \$this->calculate_credit(\$this->get_order_total());
if (\$od_amount>0) {
\$this->deduction = \$od_amount;
\$this->output[] = array('title' => \$this->title . ':',
'text' => '<b><red>' . \$currencies->format(\$od_amount) . '</red></b>',
'value' => \$od_amount);
\$order->info['total'] = \$order->info['total'] - \$od_amount;
if (\$this->sort_order < \$ot_subtotal->sort_order) {
\$order->info['subtotal'] = \$order->info['subtotal'] - \$od_amount;
}
}
}

function calculate_credit(\$amount) {
global \$order;
\$od_amount=0;

\$table_cost = split("[:,]" , MODULE_MARGIN_DISCOUNT_TABLE);
for (\$i = 0; \$i < count(\$table_cost); \$i+=2) {
if (\$amount >= \$table_cost[\$i]) {
\$od_pc = \$table_cost[\$i+1];
}
}
\$b =  \$this->get_order_total();
\$order_total = \$b;
\$a = \$this->get_order_cost();
\$order_cost =\$a;

if ((\$order_total) < ((\$order_cost) / (1 - \$od_pc/100))){
\$od_amount=0;
} elseif (\$od_pc < 10){
\$od_amount=0;
}
else {
\$od_amount = \$order_total-((\$order_cost) / (1 - \$od_pc/100));
}
//\$od_amount = round(\$od_amount*10)/10;
//\$od_amount = tep_round(\$od_amount/1000,2)*10; ---------> THIS ONE SHOULD BE RIGHT BUT ISN'T
//\$od_amount =
\$od_amount = \$b; /* this is to view order cost value , replace with \$a for the order total value
return \$od_amount;
}

/*
function get_order_total() {
global \$order;

\$order_total = \$order->info['total'];
if (\$this->include_tax == 'false') \$order_total = (\$order_total - \$order->info['tax']);
if (\$this->include_shipping == 'false') \$order_total = (\$order_total - \$order->info['shipping_cost']);
return \$order_total;
}
*/

function get_order_cost() {
global \$cart;
\$order_cost = 0;
\$products = \$cart->get_products();
for (\$k=0; \$k<sizeof(\$products)+0; \$k++) {
\$c_prid = tep_get_prid(\$products[\$k]['id']);
\$pd_query = tep_db_query("select products_price, products_cost, products_tax_class_id from " . TABLE_PRODUCTS . " where products_id = '" . \$c_prid . "'");
\$pd_result = tep_db_fetch_array(\$pd_query);
\$qty = \$cart->get_quantity(\$c_prid);
if (\$pd_result['products_cost'] > 0) {

\$pd_amount = \$pd_result['products_cost'] * \$qty;
} else {
\$pd_amount = \$pd_result['products_price'] * \$qty;
}

\$order_cost = \$order_cost + \$pd_amount;
}
return \$order_cost;
}
function get_order_total() {
global \$cart;
\$order_total = 0;
\$products = \$cart->get_products();
for (\$j=0; \$j<sizeof(\$products)+0; \$j++) {
\$special_price=0;
\$t_prid = tep_get_prid(\$products[\$j]['id']);
\$gv_query = tep_db_query("select products_price, products_tax_class_id, products_model from " . TABLE_PRODUCTS . " where products_id = '" . \$t_prid . "'");
\$gv_result = tep_db_fetch_array(\$gv_query);
\$qty = \$cart->get_quantity(\$t_prid);
\$special_price = tep_get_products_special_price(\$t_prid);
if (tep_not_null(\$specials_price)) {
\$gv_amount= \$specials_price * \$qty;
} else {
\$gv_amount = \$gv_result['products_price'] * \$qty;
}
\$order_total = \$order_total + \$gv_amount;
}

return \$order_total;
}
/*
function attributes_price(\$products_id) {
\$attributes_price = 0;
\$products_id_tmp = \$products_id;
if (isset(\$this->contents[\$products_id]['attributes'])) {
reset(\$this->contents[\$products_id]['attributes']);
while (list(\$option, \$value) = each(\$this->contents[\$products_id]['attributes'])) {
// BOM - Options Catagories
if(tep_subproducts_parent(\$products_id_tmp)){
\$products_id_query = tep_subproducts_parent(\$products_id_tmp);
}else{
\$products_id_query = \$products_id_tmp;
}

if ( !is_array(\$value) ) {
\$attribute_price_query = tep_db_query("select options_values_price as price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)\$products_id_query . "' and options_id = '" . (int)\$option . "' and options_values_id = '" . (int)\$value . "'");
\$attribute_price = tep_db_fetch_array(\$attribute_price_query);
if (\$attribute_price['price_prefix'] == '+') {
\$attributes_price += \$attribute_price['price'];
} else {
\$attributes_price -= \$attribute_price['price'];
}
} elseif ( isset(\$value['t']) ) {
\$attribute_price_query = tep_db_query("select options_values_price as price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)\$products_id_query . "' and options_id = '" . (int)\$option . "' and options_values_id = '0'");
\$attribute_price = tep_db_fetch_array(\$attribute_price_query);
if (\$attribute_price['price_prefix'] == '+') {
\$attributes_price += \$attribute_price['price'];
} else {
\$attributes_price -= \$attribute_price['price'];
}
} elseif ( isset(\$value['c']) ) {
foreach ( \$value['c'] as \$v ) {
\$attribute_price_query = tep_db_query("select options_values_price as price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)\$products_id_query . "' and options_id = '" . (int)\$option . "' and options_values_id = '" . (int)\$v . "'");
\$attribute_price = tep_db_fetch_array(\$attribute_price_query);
if (\$attribute_price['price_prefix'] == '+') {
\$attributes_price += \$attribute_price['price'];
} else {
\$attributes_price -= \$attribute_price['price'];
}
}
}
// EOM - Options Catagories
}
}

return \$attributes_price;
}
*/

function check() {
if (!isset(\$this->check)) {
\$check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_MARGIN_DISCOUNT_STATUS'");
\$this->check = tep_db_num_rows(\$check_query);
}

return \$this->check;
}

function keys() {
return array('MODULE_MARGIN_DISCOUNT_STATUS', 'MODULE_MARGIN_DISCOUNT_SORT_ORDER','MODULE_MARGIN_DISCOUNT_TABLE', 'MODULE_MARGIN_DISCOUNT_INC_SHIPPING', 'MODULE_MARGIN_DISCOUNT_INC_TAX','MODULE_MARGIN_DISCOUNT_CALC_TAX');
}

function install() {
tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Display Total', 'MODULE_MARGIN_DISCOUNT_STATUS', 'false', 'Do you want to enable the Order Discount?', '6', '1','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_MARGIN_DISCOUNT_SORT_ORDER', '15', 'Sort order of display.', '6', '2', now())");
tep_db_query("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_MARGIN_DISCOUNT_INC_SHIPPING', 'false', 'Include Shipping in calculation', '6', '3', 'tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
tep_db_query("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_MARGIN_DISCOUNT_INC_TAX', 'false', 'Include Tax in calculation.', '6', '4','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function ,date_added) values ('Calculate Tax', 'MODULE_MARGIN_DISCOUNT_CALC_TAX', 'true', 'Re-calculate Tax on discounted amount.', '6', '5','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Discount Percentage', 'MODULE_MARGIN_DISCOUNT_TABLE', '9000:16', 'Set the price breaks and discount percentages', '6', '6', now())");
}

function remove() {
\$keys = '';
\$keys_array = \$this->keys();
for (\$i=0; \$i<sizeof(\$keys_array); \$i++) {
\$keys .= "'" . \$keys_array[\$i] . "',";
}
\$keys = substr(\$keys, 0, -1);

tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in (" . \$keys . ")");
}
}
?>```

Share on other sites

Hello every body,

Specilly I would like to thank MYSELF , he did all the code for me.

It took me a great deal of time to analyse numerous replies I never received.

It is not necessary any more to answer this comment, because resolved...

A real headache with Crel*o*aded. But success after 6 good days of lost time !!! But it was worth it, it was worth it, I made a success. Beautiful exploit for beginner in php.

Good luck to the others who wish the contrib, I worked 6 long days, to do odd jobs in my code, I don't remember anymore what I've modified to say it to you

Hoping that it will not crash in production.

Thanks