cffd2210d5320bf9d71bd1ebe0d79e7d4c4ffe60
[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 include_once($path_to_root . "/includes/inventory.inc");
16
17 include_once($path_to_root . "/includes/date_functions.inc");
18
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, $rate=0, $memo="")
37 {
38         return add_gl_trans($type, $type_no, $date_, $account, $dimension, $dimension2, $memo, 
39                 $amount, get_supplier_currency($supplier_id), 
40                 PT_SUPPLIER, $supplier_id, $rate);
41 }
42
43 //----------------------------------------------------------------------------------------
44
45 function get_purchase_price($supplier_id, $stock_id)
46 {
47         $sql = "SELECT price, conversion_factor FROM ".TB_PREF."purch_data 
48                 WHERE supplier_id = ".db_escape($supplier_id) . " 
49                 AND stock_id = ".db_escape($stock_id);
50         $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
51
52         if (db_num_rows($result) == 1)
53         {
54                 $myrow = db_fetch($result);
55                 return $myrow["price"] / $myrow['conversion_factor'];
56         } 
57         else 
58         {
59                 return 0;
60         }       
61 }
62
63 function get_purchase_conversion_factor($supplier_id, $stock_id)
64 {
65         $sql = "SELECT conversion_factor FROM ".TB_PREF."purch_data 
66                 WHERE supplier_id = ".db_escape($supplier_id)." 
67                 AND stock_id = ".db_escape($stock_id);
68         $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
69
70         if (db_num_rows($result) == 1)
71         {
72                 $myrow = db_fetch($result);
73                 return $myrow['conversion_factor'];
74         } 
75         else 
76         {
77                 return 1;
78         }       
79 }
80 //----------------------------------------------------------------------------------------
81
82 function get_purchase_data($supplier_id, $stock_id)
83 {
84         $sql = "SELECT * FROM ".TB_PREF."purch_data 
85                 WHERE supplier_id = ".db_escape($supplier_id) . "
86                 AND stock_id = ".db_escape($stock_id);
87         $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
88
89         return db_fetch($result);
90 }
91
92 function add_or_update_purchase_data($supplier_id, $stock_id, $price, $description="", $uom="")
93 {
94         $data = get_purchase_data($supplier_id, $stock_id);
95         if ($data === false)
96         {
97                 $sql = "INSERT INTO ".TB_PREF."purch_data (supplier_id, stock_id, price, suppliers_uom,
98                         conversion_factor, supplier_description) VALUES (".db_escape($supplier_id)
99                         .", ".db_escape($stock_id).", ".db_escape($price).", "
100                         .db_escape($uom).", 1, ".db_escape($description).")";
101                 db_query($sql,"The supplier purchasing details could not be added");
102                 return;
103         }       
104         $price = round($price * $data['conversion_factor'], user_price_dec());
105         $sql = "UPDATE ".TB_PREF."purch_data SET price=".db_escape($price);
106         if ($uom != "")
107                 $sql .= ",suppliers_uom=".db_escape($uom);
108         if ($description != "") 
109                 $sql .= ",supplier_description=".db_escape($description);
110         $sql .= " WHERE stock_id=".db_escape($stock_id)." AND supplier_id=".db_escape($supplier_id);
111         db_query($sql,"The supplier purchasing details could not be updated");
112         return true;
113 }
114
115 function get_po_prepayments($supp_trans)
116 {
117         // collect purchase order line ids
118         $allocations = array();
119         $line_ids = array();
120         foreach($supp_trans->grn_items as $item)
121                 if ($item->po_detail_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
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
142 //---------------------------------------------------------------------------------------------------
143 //
144 //      Add Purchase Order, GRN or Purchase Invoice with parent auto documents (if any)
145 //
146 function add_direct_supp_trans($cart)
147 {
148         global $Refs, $type_shortcuts;
149
150         if ($cart->trans_type != ST_PURCHORDER) {
151                 // for direct grn/invoice set same dates for lines as for whole document
152                 foreach ($cart->line_items as $line_no =>$line)
153                         $cart->line_items[$line_no]->req_del_date = $cart->tran_date;
154         }
155
156         $ref = $cart->reference;
157         if ($cart->trans_type != ST_PURCHORDER) {
158                 $cart->reference = 'auto';
159                 begin_transaction();    // all db changes as single transaction for direct document
160         }
161         $order_no = add_po($cart);
162         $cart->order_no = $order_no;
163
164         if ($cart->trans_type == ST_PURCHORDER)
165                 return $order_no;
166
167         //Direct GRN
168         if ($cart->trans_type == ST_SUPPRECEIVE)
169                 $cart->reference = $ref;
170         if ($cart->trans_type != ST_SUPPINVOICE)
171                 $cart->Comments = $cart->reference; //grn does not hold supp_ref
172
173         $grn_no = write_grn($cart);
174         if ($cart->trans_type == ST_SUPPRECEIVE) {
175                 commit_transaction(); // save PO+GRN
176                 return $grn_no;
177         }
178         //      Direct Purchase Invoice
179         $inv = new supp_trans(ST_SUPPINVOICE);
180         $inv->Comments = $cart->Comments;
181         $inv->supplier_id = $cart->supplier_id;
182         $inv->tran_date = $cart->tran_date;
183         $inv->due_date = $cart->due_date;
184         $inv->dimension = $cart->dimension;
185         $inv->dimension2 = $cart->dimension2;
186         $inv->reference = $ref;
187         $inv->supp_reference = $cart->supp_ref;
188         $inv->tax_included = $cart->tax_included;
189         $supp = get_supplier($cart->supplier_id);
190         $inv->tax_group_id = $supp['tax_group_id'];
191         $inv->ov_amount = $inv->ov_gst = $inv->ov_discount = 0;
192         $total = 0;
193                 foreach($cart->line_items as $key => $line) {
194                 $inv->add_grn_to_trans($line->grn_item_id, $line->po_detail_rec, $line->stock_id,
195                         $line->item_description, $line->quantity, 0, $line->quantity,
196                         $line->price, $line->price, true, get_unit_cost($line->stock_id), '');
197                 $total += round2(($line->quantity * $line->price), user_price_dec());
198         }
199         $inv->tax_overrides = $cart->tax_overrides;
200         if (!$inv->tax_included) {
201                 $taxes = $inv->get_taxes($inv->tax_group_id, 0, false);
202                 foreach( $taxes as $taxitem) {
203                         $total += isset($taxitem['Override']) ? $taxitem['Override'] : $taxitem['Value'];
204                 }
205         }
206         $inv->ex_rate = $cart->ex_rate;
207
208         $inv_no = add_supp_invoice($inv);
209
210         if ($cart->cash_account) {
211                 $pmt_no = write_supp_payment(0, $inv->supplier_id, $cart->cash_account, $inv->tran_date, $Refs->get_next(ST_SUPPAYMENT, null, $inv->tran_date), 
212                         $total, 0, _('Payment for:').$inv->supp_reference .' ('.$type_shortcuts[ST_SUPPINVOICE].$inv_no.')');
213                 add_supp_allocation($total, ST_SUPPAYMENT, $pmt_no, ST_SUPPINVOICE, $inv_no, $inv->supplier_id, $inv->tran_date);
214                 update_supp_trans_allocation(ST_SUPPINVOICE, $inv_no, $inv->supplier_id);
215                 update_supp_trans_allocation(ST_SUPPAYMENT, $pmt_no, $inv->supplier_id);
216         }
217         commit_transaction(); // save PO+GRN+PI(+SP)
218         return $inv_no;
219 }
220