Additional sort options.
[fa-stable.git] / sales / includes / sales_db.inc
index e3b3e5a34752fa304d8bef0e191487b6ded1b598..4f98c78fc6ab5df0774ae38db8edd43026f9c213 100644 (file)
@@ -6,6 +6,8 @@ include_once($path_to_root . "/sales/includes/db/sales_order_db.inc");
 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");
@@ -46,28 +48,73 @@ 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=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());
 }
 
 //-----------------------------------------------------------------------------
@@ -79,7 +126,7 @@ function set_document_parent($cart)
        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 ;
@@ -123,7 +170,7 @@ function update_parent_line($doc_type, $line_id, $qty_dispatched)
 {
        $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 {
@@ -180,7 +227,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"]);