X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=sales%2Fincludes%2Fsales_db.inc;h=75fca059a1d98575c5077451b45ecbb0819e112c;hb=df0fd2e5ff26de79e7d86c4206582012833bcef5;hp=e3b3e5a34752fa304d8bef0e191487b6ded1b598;hpb=0c1bcd8ce3c089d7ddb3722a097f8fc8417f41e6;p=fa-stable.git diff --git a/sales/includes/sales_db.inc b/sales/includes/sales_db.inc index e3b3e5a3..75fca059 100644 --- a/sales/includes/sales_db.inc +++ b/sales/includes/sales_db.inc @@ -46,28 +46,67 @@ function add_gl_trans_customer($type, $type_no, $date_, $account, $dimension, $d //---------------------------------------------------------------------------------------- -function get_price ($stock_id, $debtor_no) +function get_price ($stock_id, $currency, $sales_type_id, $factor, $date=null) { + if ($date == null) + $date = Today(); + $sql = "SELECT ".TB_PREF."prices.price - FROM ".TB_PREF."prices, ".TB_PREF."debtors_master - WHERE ".TB_PREF."debtors_master.sales_type=".TB_PREF."prices.sales_type_id - AND ".TB_PREF."debtors_master.debtor_no='" . $debtor_no . "' - AND ".TB_PREF."prices.stock_id = '" . $stock_id . "' - AND ".TB_PREF."prices.curr_abrev = ".TB_PREF."debtors_master.curr_code"; + FROM ".TB_PREF."prices + WHERE ".TB_PREF."prices.stock_id = '" . $stock_id . "' " + ." AND ".TB_PREF."prices.sales_type_id = " . $sales_type_id + ." AND ".TB_PREF."prices.curr_abrev = '$currency'"; - $result = db_query($sql, "There was a problem retrieving the pricing information for the part $stock_id for customer"); + $msg = "There was a problem retrieving the pricing information for the part $stock_id for customer"; + $result = db_query($sql, $msg); - if (db_num_rows($result) != 0) + if (db_num_rows($result) != 0) { - /*There is a price from one of the above so return that */ $myrow = db_fetch_row($result); return $myrow[0]; } - else + if ($factor == 0) return 0; // auto price calculations off + + $base_id = get_base_sales_type(); + $home_curr = get_company_currency(); + + // get all prices which we can use to guess the price. + // alternative is make up to 2 additional sql queries + $sql = "SELECT ".TB_PREF."prices.price,".TB_PREF."prices.curr_abrev, + ".TB_PREF."prices.sales_type_id + FROM ".TB_PREF."prices + WHERE ".TB_PREF."prices.stock_id = '" . $stock_id . "' " + ." AND (".TB_PREF."prices.sales_type_id = " . $sales_type_id + ." OR ".TB_PREF."prices.sales_type_id = " . $base_id.")" + ." AND (".TB_PREF."prices.curr_abrev = '$currency'" + ." OR ".TB_PREF."prices.curr_abrev = '$home_curr')"; + + $result = db_query($sql, $msg); + + $prices = array(); + while($myrow = db_fetch($result)) { - return 0; + $prices[$myrow['sales_type_id']][$myrow['curr_abrev']] = $myrow['price']; } - + + $rate = round(get_exchange_rate_from_home_currency($currency, $date), + user_exrate_dec()); + $price = 0.00; + + if (isset($prices[$sales_type_id][$home_curr])) + { + $price = $prices[$sales_type_id][$home_curr] / $rate; + } + if (isset($prices[$base_id][$currency])) + { + $price =$prices[$base_id][$currency] * $factor; + } + if (isset($prices[$base_id][$home_curr])) + { + $price =$prices[$base_id][$home_curr] * $factor / $rate; + } + + return round($price, user_price_dec()); } //----------------------------------------------------------------------------- @@ -180,7 +219,7 @@ function read_sales_trans($doc_type, $trans_no, &$cart) else $cart->trans_no = array($trans_no[0]=>$myrow["version"]); - $cart->set_sales_type($myrow["tpe"], $myrow["sales_type"], $myrow["tax_included"]); + $cart->set_sales_type($myrow["tpe"], $myrow["sales_type"], $myrow["tax_included"],0); $cart->set_customer($myrow["debtor_no"], $myrow["DebtorName"], $myrow["curr_code"], $myrow["discount"]);