*** empty log message ***
[fa-stable.git] / purchasing / includes / purchasing_db.inc
1 <?php
2
3 include_once($path_to_root . "/purchasing/includes/supp_trans_class.inc");
4
5 include_once($path_to_root . "/includes/banking.inc");
6
7 include_once($path_to_root . "/includes/date_functions.inc");
8
9 include_once($path_to_root . "/includes/db/inventory_db.inc");
10
11 include_once($path_to_root . "/purchasing/includes/db/supp_trans_db.inc");
12 include_once($path_to_root . "/purchasing/includes/db/po_db.inc");
13 include_once($path_to_root . "/purchasing/includes/db/grn_db.inc");
14 include_once($path_to_root . "/purchasing/includes/db/invoice_db.inc");
15 include_once($path_to_root . "/purchasing/includes/db/suppalloc_db.inc");
16 include_once($path_to_root . "/purchasing/includes/db/supp_payment_db.inc");
17 include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
18
19 //-------------------------------------------------------------------------------------------------------------
20
21 // add a supplier-related gl transaction
22 // $date_ is display date (non-sql)
23 // $amount is in SUPPLIERS'S currency
24
25 function add_gl_trans_supplier($type, $type_no, $date_, $account, $dimension, $dimension2,  
26         $amount, $supplier_id, $err_msg="", $rate=0)
27 {
28         if ($err_msg == "")
29                 $err_msg = "The supplier GL transaction could not be inserted"; 
30                 
31         return add_gl_trans($type, $type_no, $date_, $account, $dimension, $dimension2, "", 
32                 $amount, get_supplier_currency($supplier_id), 
33                 payment_person_types::supplier(), $supplier_id, $err_msg, $rate);
34 }
35
36 //----------------------------------------------------------------------------------------
37
38 function get_purchase_price($supplier_id, $stock_id)
39 {
40         $sql = "SELECT price, conversion_factor FROM ".TB_PREF."purch_data 
41                 WHERE supplier_id = '" . $supplier_id . "' 
42                 AND stock_id = '". $stock_id . "'";
43         $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
44
45         if (db_num_rows($result) == 1)
46         {
47                 $myrow = db_fetch($result);
48                 return $myrow["price"] / $myrow['conversion_factor'];
49         } 
50         else 
51         {
52                 return 0;
53         }       
54 }
55
56 //----------------------------------------------------------------------------------------
57
58 function get_purchase_data($supplier_id, $stock_id)
59 {
60         $sql = "SELECT * FROM ".TB_PREF."purch_data 
61                 WHERE supplier_id = '" . $supplier_id . "' 
62                 AND stock_id = '". $stock_id . "'";
63         $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
64
65         return db_fetch($result);
66 }
67
68 function add_or_update_purchase_data($supplier_id, $stock_id, $price, $description="", $uom="")
69 {
70         $data = get_purchase_data($supplier_id, $stock_id);
71         if ($data === false)
72         {
73                 $sql = "INSERT INTO ".TB_PREF."purch_data (supplier_id, stock_id, price, suppliers_uom,
74                         conversion_factor, supplier_description) VALUES ('$supplier_id', '$stock_id', 
75                         $price, '$uom', 1, '$description')";
76                 db_query($sql,"The supplier purchasing details could not be added");
77                 return;
78         }       
79         $price = round($price * $data['conversion_factor'], user_price_dec());  
80         $sql = "UPDATE ".TB_PREF."purch_data SET price=$price";
81         if ($uom != "")
82                 $sql .= ",suppliers_uom='$uom'";
83         if ($description != "") 
84                 $sql .= ",supplier_description='$description'";
85         $sql .= " WHERE stock_id='$stock_id' AND supplier_id='$supplier_id'";
86         db_query($sql,"The supplier purchasing details could not be updated");
87         return true;
88 }
89
90 ?>