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 . "/includes/banking.inc");
13
14 include_once($path_to_root . "/includes/date_functions.inc");
15
16 include_once($path_to_root . "/includes/db/inventory_db.inc");
17 include_once($path_to_root . "/includes/db/allocations_db.inc");
18
19 include_once($path_to_root . "/purchasing/includes/db/supp_trans_db.inc");
20 include_once($path_to_root . "/purchasing/includes/db/po_db.inc");
21 include_once($path_to_root . "/purchasing/includes/db/grn_db.inc");
22 include_once($path_to_root . "/purchasing/includes/db/invoice_db.inc");
23 include_once($path_to_root . "/purchasing/includes/db/suppalloc_db.inc");
24 include_once($path_to_root . "/purchasing/includes/db/supp_payment_db.inc");
25 include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
26
27 //-------------------------------------------------------------------------------------------------------------
28
29 // add a supplier-related gl transaction
30 // $date_ is display date (non-sql)
31 // $amount is in SUPPLIERS'S currency
32
33 function add_gl_trans_supplier($type, $type_no, $date_, $account, $dimension, $dimension2,  
34         $amount, $supplier_id, $err_msg="", $rate=0, $memo="")
35 {
36         if ($err_msg == "")
37                 $err_msg = "The supplier GL transaction could not be inserted"; 
38                 
39         return add_gl_trans($type, $type_no, $date_, $account, $dimension, $dimension2, $memo, 
40                 $amount, get_supplier_currency($supplier_id), 
41                 PT_SUPPLIER, $supplier_id, $err_msg, $rate);
42 }
43
44 //----------------------------------------------------------------------------------------
45
46 function get_purchase_price($supplier_id, $stock_id)
47 {
48         $sql = "SELECT price, conversion_factor FROM ".TB_PREF."purch_data 
49                 WHERE supplier_id = ".db_escape($supplier_id) . " 
50                 AND stock_id = ".db_escape($stock_id);
51         $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
52
53         if (db_num_rows($result) == 1)
54         {
55                 $myrow = db_fetch($result);
56                 return $myrow["price"] / $myrow['conversion_factor'];
57         } 
58         else 
59         {
60                 return 0;
61         }       
62 }
63
64 function get_purchase_conversion_factor($supplier_id, $stock_id)
65 {
66         $sql = "SELECT conversion_factor FROM ".TB_PREF."purch_data 
67                 WHERE supplier_id = ".db_escape($supplier_id)." 
68                 AND stock_id = ".db_escape($stock_id);
69         $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
70
71         if (db_num_rows($result) == 1)
72         {
73                 $myrow = db_fetch($result);
74                 return $myrow['conversion_factor'];
75         } 
76         else 
77         {
78                 return 1;
79         }       
80 }
81 //----------------------------------------------------------------------------------------
82
83 function get_purchase_data($supplier_id, $stock_id)
84 {
85         $sql = "SELECT * FROM ".TB_PREF."purch_data 
86                 WHERE supplier_id = ".db_escape($supplier_id) . "
87                 AND stock_id = ".db_escape($stock_id);
88         $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
89
90         return db_fetch($result);
91 }
92
93 function add_or_update_purchase_data($supplier_id, $stock_id, $price, $description="", $uom="")
94 {
95         $data = get_purchase_data($supplier_id, $stock_id);
96         if ($data === false)
97         {
98                 $sql = "INSERT INTO ".TB_PREF."purch_data (supplier_id, stock_id, price, suppliers_uom,
99                         conversion_factor, supplier_description) VALUES (".db_escape($supplier_id)
100                         .", ".db_escape($stock_id).", ".db_escape($price).", "
101                         .db_escape($uom).", 1, ".db_escape($description).")";
102                 db_query($sql,"The supplier purchasing details could not be added");
103                 return;
104         }       
105         $price = round($price * $data['conversion_factor'], user_price_dec());
106         $sql = "UPDATE ".TB_PREF."purch_data SET price=".db_escape($price);
107         if ($uom != "")
108                 $sql .= ",suppliers_uom=".db_escape($uom);
109         if ($description != "") 
110                 $sql .= ",supplier_description=".db_escape($description);
111         $sql .= " WHERE stock_id=".db_escape($stock_id)." AND supplier_id=".db_escape($supplier_id);
112         db_query($sql,"The supplier purchasing details could not be updated");
113         return true;
114 }
115
116 function get_po_prepayments($supp_trans)
117 {
118         // collect purchase order line ids
119         $allocations = array();
120         $line_ids = array();
121         foreach($supp_trans->grn_items as $item)
122                 $line_ids[] = $item->po_detail_item;
123
124         if (!count($line_ids))
125                 return $allocations;
126
127         $sql = "SELECT DISTINCT allocs.*
128                 FROM ".TB_PREF."supp_allocations allocs 
129                         LEFT JOIN ".TB_PREF."purch_order_details line ON line.order_no=allocs.trans_no_to AND trans_type_to=".ST_PURCHORDER."
130                 WHERE line.po_detail_item IN(".implode(',', array_values($line_ids)).")";
131 //_vd($sql);
132         $result = db_query($sql, "Cannot retrieve po prepayments");
133
134         while($dat = db_fetch($result))
135         {
136                 $allocations[] = $dat;
137         }
138
139         return $allocations;
140 }
141 ?>