0000593,0001093: Prepayments made against orders implemented.
[fa-stable.git] / sales / includes / sales_db.inc
index 4dff712f2ae1b2c729b8099f5294670d3ce3776d..08175b186e7fddf9db0dc560491e654f1c67f918 100644 (file)
@@ -11,6 +11,7 @@
 ***********************************************************************/
 include_once($path_to_root . "/includes/banking.inc");
 include_once($path_to_root . "/includes/db/inventory_db.inc");
+include_once($path_to_root . "/includes/db/allocations_db.inc");
 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");
@@ -174,7 +175,7 @@ function get_kit_price($item_code, $currency, $sales_type_id, $factor=null,
                $kit_price = get_price( $item_code, $currency, $sales_type_id, 
                        $factor, $date);
 
-               if ($kit_price !== false) {
+               if ($kit_price !== 0) {
                        return $kit_price;
                }
        }       
@@ -201,18 +202,20 @@ function update_parent_line($doc_type, $line_id, $qty_dispatched, $auto=false)
 {
        $doc_type = get_parent_type($doc_type);
 
+        $qty_dispatched = (float)$qty_dispatched;
+
 //     echo "update line: $line_id, $doc_type, $qty_dispatched";
-       if ($doc_type==0)
+       if ($doc_type == 0)
                return false;
        else {
-               if ($doc_type==ST_SALESORDER)
+               if ($doc_type==ST_SALESORDER || $doc_type==ST_SALESQUOTE)
                {
                        $sql = "UPDATE ".TB_PREF."sales_order_details
                                SET qty_sent = qty_sent + $qty_dispatched";
                        if ($auto)
                                $sql .= ", quantity = quantity + $qty_dispatched";
                        $sql .= " WHERE id=".db_escape($line_id);
-               }               
+               }
                else
                        $sql = "UPDATE ".TB_PREF."debtor_trans_details
                                SET qty_done = qty_done + $qty_dispatched
@@ -271,6 +274,7 @@ function read_sales_trans($doc_type, $trans_no, &$cart)
                        $myrow["tax_group_name"]);
 
                $cart->reference = $myrow["reference"];
+               $cart->prepaid = $myrow["prepaid"];
                $cart->order_no = $myrow["order_"];
                $cart->due_date = sql2date($myrow["due_date"]);
                $cart->document_date = sql2date($myrow["tran_date"]);
@@ -300,9 +304,12 @@ function read_sales_trans($doc_type, $trans_no, &$cart)
                                        $myrow["stock_id"],$myrow["quantity"],
                                        $myrow["unit_price"], $myrow["discount_percent"],
                                        $myrow["qty_done"], $myrow["standard_cost"],
-                                       $myrow["StockDescription"],$myrow["id"], $myrow["debtor_trans_no"]);
+                                       $myrow["StockDescription"],$myrow["id"], $myrow["debtor_trans_no"],
+                                       @$myrow["src_id"]);
                        }
                }
+               $cart->prepayments = get_payments_for($trans_no, $doc_type);
+
        } // !newdoc
 
        return true;
@@ -316,10 +323,13 @@ function get_sales_child_lines($trans_type, $trans_no, $lines=true)
        if (!is_array($trans_no)) {
                $trans_no = array($trans_no);
        }
-       
+
        $par_tbl = $trans_type == ST_SALESORDER ? "sales_order_details" : "debtor_trans_details";
        $par_no = $trans_type == ST_SALESORDER ? "trans.order_no" : "trans.debtor_trans_no";
 
+       foreach($trans_no as $n => $trans) {
+               $trans_no[$n] = db_escape($trans);
+       }
        $sql = "SELECT child.*
                        FROM
                                ".TB_PREF."debtor_trans_details child
@@ -360,7 +370,8 @@ function get_sales_parent_lines($trans_type, $trans_no, $lines=true)
                        LEFT JOIN ".TB_PREF."debtor_trans_details trans 
                                ON trans.src_id=parent.id
                        WHERE
-                               trans.debtor_trans_type=$trans_type AND trans.debtor_trans_no=$trans_no";
+                               trans.debtor_trans_type=".db_escape($trans_type)
+                               ." AND trans.debtor_trans_no=".db_escape($trans_no);
        if (!$lines)
                $sql .= " GROUP BY $par_no";
        
@@ -398,5 +409,4 @@ function get_sales_child_documents($trans_type, $trans_no)
        return db_query($sql,"The related credit notes could not be retreived");
 }
 
-
 ?>
\ No newline at end of file