include_once($path_to_root . "/sales/includes/db/sales_credit_db.inc");
include_once($path_to_root . "/sales/includes/db/sales_invoice_db.inc");
include_once($path_to_root . "/sales/includes/db/sales_delivery_db.inc");
+include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
+include_once($path_to_root . "/sales/includes/db/sales_points_db.inc");
include_once($path_to_root . "/sales/includes/db/custalloc_db.inc");
include_once($path_to_root . "/sales/includes/db/cust_trans_db.inc");
include_once($path_to_root . "/sales/includes/db/cust_trans_details_db.inc");
//----------------------------------------------------------------------------------------
-function get_price ($stock_id, $debtor_no)
+function get_price ($stock_id, $currency, $sales_type_id, $factor=null, $date=null)
{
+ if ($date == null)
+ $date = Today();
+
+ if ($factor === null)
+ {
+ $myrow = get_sales_type($sales_type_id);
+ $factor = $myrow['factor'];
+ }
+
$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());
}
//-----------------------------------------------------------------------------
if (count($cart->src_docs) == 1) {
// if this child document has only one parent - update child link
- $del_no = key($cart->src_docs);
+ $del_no = reset(array_keys($cart->src_docs));
$sql = 'UPDATE '.TB_PREF.'debtor_trans SET trans_link = ' . $del_no .
' WHERE type='.$cart->trans_type.' AND trans_no='. $inv_no ;
{
$doc_type = get_parent_type($doc_type);
- //echo "update line: $line_id, $doc_type, $qty_dispatch";
+// echo "update line: $line_id, $doc_type, $qty_dispatched";
if ($doc_type==0)
return false;
else {
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"]);