Rerun of Supplier Inquiry Aging. Optimizing.
[fa-stable.git] / purchasing / includes / db / suppliers_db.inc
index d66c89fc7941fb20c3912d1175eb0413b366fb2a..7c97b1c1c4d10b9f60eeb181227e6c349aefd136 100644 (file)
@@ -73,8 +73,12 @@ function update_supplier($supplier_id, $supp_name, $supp_ref, $address, $supp_ad
 
 function delete_supplier($supplier_id)
 {
+       begin_transaction();
+       delete_entity_contacts('supplier', $supplier_id);
        $sql="DELETE FROM ".TB_PREF."suppliers WHERE supplier_id=".db_escape($supplier_id);
+
        db_query($sql,"check failed");
+       commit_transaction();
 }
 
 function get_supplier_details($supplier_id, $to=null, $all=true)
@@ -91,9 +95,9 @@ function get_supplier_details($supplier_id, $to=null, $all=true)
        if ($all)
        $value = "(trans.ov_amount + trans.ov_gst + trans.ov_discount)";
     else       
-       $value = "IF (trans.type=".ST_SUPPINVOICE." OR trans.type=".ST_BANKDEPOSIT."
-       OR (trans.type=".ST_JOURNAL." AND (trans.ov_amount + trans.ov_gst + trans.ov_discount)>0), 1, -1)*
-               ABS(trans.ov_amount + trans.ov_gst + trans.ov_discount) - trans.alloc";
+       $value = "IF (trans.type=".ST_SUPPINVOICE." OR trans.type=".ST_BANKDEPOSIT." OR (trans.type=".ST_JOURNAL." AND (trans.ov_amount + trans.ov_gst + trans.ov_discount)>0), 
+               (trans.ov_amount + trans.ov_gst + trans.ov_discount - trans.alloc),
+               (trans.ov_amount + trans.ov_gst + trans.ov_discount + trans.alloc))";
 
        $due = "IF (trans.type=".ST_SUPPINVOICE." OR trans.type=".ST_SUPPCREDIT.",trans.due_date,trans.tran_date)";
     $sql = "SELECT supp.supp_name, supp.curr_code, ".TB_PREF."payment_terms.terms,
@@ -115,7 +119,7 @@ function get_supplier_details($supplier_id, $to=null, $all=true)
                         supp.payment_terms = ".TB_PREF."payment_terms.terms_indicator
                         AND supp.supplier_id = $supplier_id ";
        if (!$all)
-               $sql .= "AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_discount) - trans.alloc > ".FLOAT_COMP_DELTA." ";  
+               $sql .= "AND $value <> 0 ";  
        $sql .= "GROUP BY
                          supp.supp_name,
                          ".TB_PREF."payment_terms.terms,
@@ -144,7 +148,7 @@ function get_supplier_name($supplier_id)
 
        $row = db_fetch_row($result);
 
-       return $row[0];
+       return is_array($row) ? $row[0] : false;
 }
 
 function get_supplier_accounts($supplier_id)
@@ -187,7 +191,7 @@ function get_supplier_currency($supplier_id)
        $result = db_query($sql, "Retreive currency of supplier $supplier_id");
 
        $myrow=db_fetch_row($result);
-       return $myrow[0];
+       return $myrow ? $myrow[0] : get_company_currency();
 }
 
 function get_suppliers_search($supplier)