Merged latest changes from stable branch.
[fa-stable.git] / purchasing / includes / purchasing_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 include_once($path_to_root . "/purchasing/includes/supp_trans_class.inc");
13
14 include_once($path_to_root . "/includes/banking.inc");
15
16 include_once($path_to_root . "/includes/date_functions.inc");
17
18 include_once($path_to_root . "/includes/db/inventory_db.inc");
19 include_once($path_to_root . "/includes/db/allocations_db.inc");
20
21 include_once($path_to_root . "/purchasing/includes/db/supp_trans_db.inc");
22 include_once($path_to_root . "/purchasing/includes/db/po_db.inc");
23 include_once($path_to_root . "/purchasing/includes/db/grn_db.inc");
24 include_once($path_to_root . "/purchasing/includes/db/invoice_db.inc");
25 include_once($path_to_root . "/purchasing/includes/db/suppalloc_db.inc");
26 include_once($path_to_root . "/purchasing/includes/db/supp_payment_db.inc");
27 include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
28
29 //-------------------------------------------------------------------------------------------------------------
30
31 // add a supplier-related gl transaction
32 // $date_ is display date (non-sql)
33 // $amount is in SUPPLIERS'S currency
34
35 function add_gl_trans_supplier($type, $type_no, $date_, $account, $dimension, $dimension2,  
36         $amount, $supplier_id, $err_msg="", $rate=0, $memo="")
37 {
38         if ($err_msg == "")
39                 $err_msg = "The supplier GL transaction could not be inserted"; 
40                 
41         return add_gl_trans($type, $type_no, $date_, $account, $dimension, $dimension2, $memo, 
42                 $amount, get_supplier_currency($supplier_id), 
43                 PT_SUPPLIER, $supplier_id, $err_msg, $rate);
44 }
45
46 //----------------------------------------------------------------------------------------
47
48 function get_purchase_price($supplier_id, $stock_id)
49 {
50         $sql = "SELECT price, conversion_factor FROM ".TB_PREF."purch_data 
51                 WHERE supplier_id = ".db_escape($supplier_id) . " 
52                 AND stock_id = ".db_escape($stock_id);
53         $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
54
55         if (db_num_rows($result) == 1)
56         {
57                 $myrow = db_fetch($result);
58                 return $myrow["price"] / $myrow['conversion_factor'];
59         } 
60         else 
61         {
62                 return 0;
63         }       
64 }
65
66 function get_purchase_conversion_factor($supplier_id, $stock_id)
67 {
68         $sql = "SELECT conversion_factor FROM ".TB_PREF."purch_data 
69                 WHERE supplier_id = ".db_escape($supplier_id)." 
70                 AND stock_id = ".db_escape($stock_id);
71         $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
72
73         if (db_num_rows($result) == 1)
74         {
75                 $myrow = db_fetch($result);
76                 return $myrow['conversion_factor'];
77         } 
78         else 
79         {
80                 return 1;
81         }       
82 }
83 //----------------------------------------------------------------------------------------
84
85 function get_purchase_data($supplier_id, $stock_id)
86 {
87         $sql = "SELECT * FROM ".TB_PREF."purch_data 
88                 WHERE supplier_id = ".db_escape($supplier_id) . "
89                 AND stock_id = ".db_escape($stock_id);
90         $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
91
92         return db_fetch($result);
93 }
94
95 function add_or_update_purchase_data($supplier_id, $stock_id, $price, $description="", $uom="")
96 {
97         $data = get_purchase_data($supplier_id, $stock_id);
98         if ($data === false)
99         {
100                 $sql = "INSERT INTO ".TB_PREF."purch_data (supplier_id, stock_id, price, suppliers_uom,
101                         conversion_factor, supplier_description) VALUES (".db_escape($supplier_id)
102                         .", ".db_escape($stock_id).", ".db_escape($price).", "
103                         .db_escape($uom).", 1, ".db_escape($description).")";
104                 db_query($sql,"The supplier purchasing details could not be added");
105                 return;
106         }       
107         $price = round($price * $data['conversion_factor'], user_price_dec());
108         $sql = "UPDATE ".TB_PREF."purch_data SET price=".db_escape($price);
109         if ($uom != "")
110                 $sql .= ",suppliers_uom=".db_escape($uom);
111         if ($description != "") 
112                 $sql .= ",supplier_description=".db_escape($description);
113         $sql .= " WHERE stock_id=".db_escape($stock_id)." AND supplier_id=".db_escape($supplier_id);
114         db_query($sql,"The supplier purchasing details could not be updated");
115         return true;
116 }
117
118 function get_po_prepayments($supp_trans)
119 {
120         // collect purchase order line ids
121         $allocations = array();
122         $line_ids = array();
123         foreach($supp_trans->grn_items as $item)
124                 $line_ids[] = $item->po_detail_item;
125
126         if (!count($line_ids))
127                 return $allocations;
128
129         $sql = "SELECT DISTINCT allocs.*
130                 FROM ".TB_PREF."supp_allocations allocs 
131                         LEFT JOIN ".TB_PREF."purch_order_details line ON line.order_no=allocs.trans_no_to AND trans_type_to=".ST_PURCHORDER."
132                 WHERE line.po_detail_item IN(".implode(',', array_values($line_ids)).")";
133 //_vd($sql);
134         $result = db_query($sql, "Cannot retrieve po prepayments");
135
136         while($dat = db_fetch($result))
137         {
138                 $allocations[] = $dat;
139         }
140
141         return $allocations;
142 }
143 ?>