Added fixed assets module
[fa-stable.git] / sales / includes / db / sales_delivery_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 //-----------------------------------------------------------------------------
13 // insert/update sales delivery
14 //
15 function write_sales_delivery(&$delivery,$bo_policy)
16 {
17         global $Refs;
18
19         $trans_no = $delivery->trans_no;
20         if (is_array($trans_no)) $trans_no = key($trans_no);
21
22         begin_transaction();
23         $delivery->bo_policy = $bo_policy;
24         hook_db_prewrite($delivery, ST_CUSTDELIVERY);
25
26         $customer = get_customer($delivery->customer_id);
27
28         add_new_exchange_rate($customer['curr_code'], $delivery->document_date, $delivery->ex_rate);
29
30         $delivery_items_total = $delivery->get_items_total_dispatch();
31         $freight_tax = $delivery->get_shipping_tax();
32
33 // mark sales order for concurrency conflicts check
34         update_sales_order_version($delivery->src_docs);
35
36         $tax_total = 0;
37         $taxes = $delivery->get_taxes(); // all taxes with freight_tax
38
39         foreach ($taxes as $taxitem) {
40                 $taxitem['Value'] =  round2($taxitem['Value'], user_price_dec());
41                 $tax_total +=  $taxitem['Value'];
42         }
43         /* Insert/update the debtor_trans */
44         $delivery_no = write_customer_trans(ST_CUSTDELIVERY, $trans_no, $delivery->customer_id,
45                 $delivery->Branch, $delivery->document_date, $delivery->reference,
46                 $delivery_items_total, 0,
47                 $delivery->tax_included ? 0 : $tax_total-$freight_tax,
48                 $delivery->freight_cost,
49                 $delivery->tax_included ? 0 : $freight_tax,
50                 $delivery->sales_type, $delivery->order_no,
51                 $delivery->ship_via, $delivery->due_date, 0, 0, $delivery->dimension_id, 
52                 $delivery->dimension2_id, $delivery->payment, $delivery->tax_included, $delivery->prep_amount);
53
54         if ($trans_no == 0) {
55                 $delivery->trans_no = array($delivery_no=>0);
56         } else {
57                 move_trans_attachments(ST_CUSTDELIVERY, $trans_no, $delivery_no);
58                 void_gl_trans(ST_CUSTDELIVERY, $delivery_no, true);
59                 void_stock_move(ST_CUSTDELIVERY, $delivery_no);
60                 void_trans_tax_details(ST_CUSTDELIVERY, $delivery_no);
61                 delete_comments(ST_CUSTDELIVERY, $delivery_no);
62         }
63
64
65         // for prepaid delivery repost income from deffered account
66         if ($delivery->is_prepaid())
67         {
68                 $branch_data = get_branch_accounts($delivery->Branch);
69                 $total = 0;
70         }
71         foreach ($delivery->line_items as $line_no => $delivery_line) {
72
73                 $line_price = $delivery_line->line_price();
74                 $line_taxfree_price = get_tax_free_price_for_item($delivery_line->stock_id,
75                         $delivery_line->price, 0, $delivery->tax_included,
76                         $delivery->tax_group_array);
77
78                 $line_tax = get_full_price_for_item($delivery_line->stock_id, $delivery_line->price,
79                         0, $delivery->tax_included, $delivery->tax_group_array) - $line_taxfree_price;
80
81                 $delivery_line->standard_cost = get_standard_cost($delivery_line->stock_id);
82
83                 /* add delivery details for all lines */
84                 write_customer_trans_detail_item(ST_CUSTDELIVERY, $delivery_no, $delivery_line->stock_id,
85                         $delivery_line->item_description, $delivery_line->qty_dispatched,
86                         $delivery_line->line_price(), $line_tax,
87                         $delivery_line->discount_percent, $delivery_line->standard_cost, $delivery_line->src_id,
88                         $trans_no ? $delivery_line->id : 0);
89
90         // Now update sales_order_details for the quantity delivered
91                 if ($delivery_line->qty_old != $delivery_line->qty_dispatched)
92                         update_parent_line(ST_CUSTDELIVERY, $delivery_line->src_id,
93                                 $delivery_line->qty_dispatched-$delivery_line->qty_old);
94
95                 if ($delivery_line->qty_dispatched != 0) {
96                         add_stock_move(ST_CUSTDELIVERY, $delivery_line->stock_id, $delivery_no,
97                                 $delivery->Location, $delivery->document_date, $delivery->reference,
98                                 -$delivery_line->qty_dispatched, $delivery_line->standard_cost,
99                                  $line_price*(1-$delivery_line->discount_percent));
100
101                         $mb_flag = get_mb_flag($delivery_line->stock_id);
102
103                         if (is_fixed_asset($mb_flag)) {
104                                 $sql = "UPDATE ".TB_PREF."stock_master SET inactive=1
105                                         WHERE stock_id=".db_escape($delivery_line->stock_id);
106                                 db_query($sql,"The cost details for the fixed asset could not be updated");
107                         }
108
109                         $stock_gl_code = get_stock_gl_code($delivery_line->stock_id);
110
111                         // If there is a Customer Dimension, then override with this,
112                         // else take the Item Dimension (if any)
113                         $dim = ($delivery->dimension_id != $customer['dimension_id'] ? $delivery->dimension_id : 
114                                 ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_code["dimension_id"]));
115                         $dim2 = ($delivery->dimension2_id != $customer['dimension2_id'] ? $delivery->dimension2_id : 
116                                 ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_code["dimension2_id"]));
117                         if ($delivery->is_prepaid())
118                         {
119                                 $sales_account = ($branch_data['sales_account'] != "" ? $branch_data['sales_account'] : $stock_gl_code['sales_account']);
120
121                                 $total += add_gl_trans_customer(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date, $sales_account, $dim, $dim2,
122                                         -$line_taxfree_price*$delivery_line->qty_dispatched, // FIXME - calculation order should be exactly like in invoice to minimalize roundings
123                                         $delivery->customer_id, "The sales price GL posting could not be inserted");
124                         }
125                         /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
126                         if (is_inventory_item($delivery_line->stock_id) && $delivery_line->standard_cost != 0) {
127
128                                 /*first the cost of sales entry*/
129
130                                 $delivery_gl_code = $stock_gl_code["cogs_account"];
131                                 if ($delivery->fixed_asset)
132                                         $delivery_gl_code = $stock_gl_code["adjustment_account"];
133
134                                 add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no,
135                                         $delivery->document_date, $delivery_gl_code, $dim, $dim2, "",
136                                         $delivery_line->standard_cost * $delivery_line->qty_dispatched,
137                                         PT_CUSTOMER, $delivery->customer_id,
138                                         "The cost of sales GL posting could not be inserted");
139
140                                 /*now the stock entry*/
141
142                                 add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date,
143                                         $stock_gl_code["inventory_account"], 0, 0, "",
144                                         (-$delivery_line->standard_cost * $delivery_line->qty_dispatched),
145                                         PT_CUSTOMER, $delivery->customer_id,
146                                         "The stock side of the cost of sales GL posting could not be inserted");
147
148                         } /* end of if GL and stock integrated and standard cost !=0 */
149
150                 } /*quantity dispatched is more than 0 */
151         } /*end of order_line loop */
152
153         if ($delivery->is_prepaid())
154         {
155                 $deferred_act = get_company_pref('deferred_income_act');
156                 add_gl_trans_customer(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date, $deferred_act, 0, 0,
157                         -$total, $delivery->customer_id, "The deferred income GL posting could not be inserted");
158         }
159
160         if ($bo_policy == 0) {
161                 // if cancelling any remaining quantities
162                 close_sales_order($delivery->order_no);
163         }
164
165         // taxes - this is for printing purposes
166         foreach ($taxes as $taxitem) {
167                 if ($taxitem['Net'] != 0) {
168                                 $ex_rate = get_exchange_rate_from_home_currency(get_customer_currency($delivery->customer_id), $delivery->document_date);
169                     add_trans_tax_details(ST_CUSTDELIVERY, $delivery_no, $taxitem['tax_type_id'],
170                                 $taxitem['rate'], $delivery->tax_included, $taxitem['Value'],
171                                 $taxitem['Net'], $ex_rate, $delivery->document_date, $delivery->reference, null);
172                 }
173         }
174
175         add_comments(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date, $delivery->Comments);
176
177         if ($trans_no == 0) {
178                         $Refs->save(ST_CUSTDELIVERY, $delivery_no, $delivery->reference);
179         }
180         hook_db_postwrite($delivery, ST_CUSTDELIVERY);
181         commit_transaction();
182
183         return $delivery_no;
184 }
185
186 //--------------------------------------------------------------------------------------------------
187 function adjust_shipping_charge(&$delivery, $trans_no)
188 {
189         $sql = "SELECT sum(ov_freight) as freight FROM ".TB_PREF."debtor_trans WHERE order_ = $trans_no AND type = " . ST_CUSTDELIVERY . " AND debtor_no = " . $delivery->customer_id;
190         $result = db_query($sql, "Can not find delivery notes");
191         $row = db_fetch_row($result);
192         if (!$row[0]) $freight = 0;
193         else $freight = $row[0];
194         if ($freight < $delivery->freight_cost) $delivery->freight_cost = $delivery->freight_cost - $freight;
195         else $delivery->freight_cost = 0;
196 }
197
198 //--------------------------------------------------------------------------------------------------
199
200 function void_sales_delivery($type, $type_no, $transactions=true)
201 {
202         if ($transactions)
203                 begin_transaction();
204
205         hook_db_prevoid($type, $type_no);
206         
207         void_gl_trans($type, $type_no, true);
208
209         // reverse all the changes in the sales order
210         $items_result = get_customer_trans_details($type, $type_no);
211
212         $order = get_customer_trans_order($type, $type_no);
213
214         if ($order) {
215                 $auto = (get_reference(ST_SALESORDER, $order) == "auto");
216                 while ($row = db_fetch($items_result)) {
217                         update_parent_line(ST_CUSTDELIVERY, $row['src_id'], -$row['quantity'], $auto);
218                 }
219         }
220
221         // clear details after they've been reversed in the sales order
222         void_customer_trans_details($type, $type_no);
223
224         void_trans_tax_details($type, $type_no);
225
226         void_cust_allocations($type, $type_no);
227
228         // do this last because other voidings can depend on it
229         // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
230         void_customer_trans($type, $type_no);
231
232         if ($transactions)
233                 commit_transaction();
234 }
235