Stable merged into unstable again (due to failure on binary file during previous...
[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         $delivery_items_total = $delivery->get_items_total_dispatch();
28         $freight_tax = $delivery->get_shipping_tax();
29
30 // mark sales order for concurrency conflicts check
31         update_sales_order_version($delivery->src_docs);
32
33         $tax_total = 0;
34         $taxes = $delivery->get_taxes(); // all taxes with freight_tax
35
36         foreach ($taxes as $taxitem) {
37                 $taxitem['Value'] =  round2($taxitem['Value'], user_price_dec());
38                 $tax_total +=  $taxitem['Value'];
39         }
40         /* Insert/update the debtor_trans */
41         $delivery_no = write_customer_trans(ST_CUSTDELIVERY, $trans_no, $delivery->customer_id,
42                 $delivery->Branch, $delivery->document_date, $delivery->reference,
43                 $delivery_items_total, 0,
44                 $delivery->tax_included ? 0 : $tax_total-$freight_tax,
45                 $delivery->freight_cost,
46                 $delivery->tax_included ? 0 : $freight_tax,
47                 $delivery->sales_type, $delivery->order_no,
48                 $delivery->ship_via, $delivery->due_date, 0, 0, $delivery->dimension_id, 
49                 $delivery->dimension2_id, $delivery->payment, $delivery->tax_included);
50
51         if ($trans_no == 0) {
52                 $delivery->trans_no = array($delivery_no=>0);
53         } else {
54                 void_gl_trans(ST_CUSTDELIVERY, $delivery_no, true);
55                 void_stock_move(ST_CUSTDELIVERY, $delivery_no);
56                 void_trans_tax_details(ST_CUSTDELIVERY, $delivery_no);
57                 delete_comments(ST_CUSTDELIVERY, $delivery_no);
58         }
59
60         foreach ($delivery->line_items as $line_no => $delivery_line) {
61
62                 $line_price = $delivery_line->line_price();
63                 $line_taxfree_price = get_tax_free_price_for_item($delivery_line->stock_id,
64                         $delivery_line->price, 0, $delivery->tax_included,
65                         $delivery->tax_group_array);
66
67                 $line_tax = get_full_price_for_item($delivery_line->stock_id, $delivery_line->price,
68                         0, $delivery->tax_included, $delivery->tax_group_array) - $line_taxfree_price;
69
70                 //if ($trans_no != 0) // Inserted 2008-09-25 Joe Hunt. This condition is removed after experience by Chaitanya
71                         $delivery_line->standard_cost = get_standard_cost($delivery_line->stock_id);
72
73                 /* add delivery details for all lines */
74                 write_customer_trans_detail_item(ST_CUSTDELIVERY, $delivery_no, $delivery_line->stock_id,
75                         $delivery_line->item_description, $delivery_line->qty_dispatched,
76                         $delivery_line->line_price(), $line_tax,
77                         $delivery_line->discount_percent, $delivery_line->standard_cost, $delivery_line->src_id,
78                         $trans_no ? $delivery_line->id : 0);
79
80         // Now update sales_order_details for the quantity delivered
81                 if ($delivery_line->qty_old != $delivery_line->qty_dispatched)
82                         update_parent_line(ST_CUSTDELIVERY, $delivery_line->src_id,
83                                 $delivery_line->qty_dispatched-$delivery_line->qty_old);
84
85                 if ($delivery_line->qty_dispatched != 0) {
86                         add_stock_move_customer(ST_CUSTDELIVERY, $delivery_line->stock_id, $delivery_no,
87                                 $delivery->Location, $delivery->document_date, $delivery->reference,
88                                 -$delivery_line->qty_dispatched, $delivery_line->standard_cost,1,
89                                 $line_price, $delivery_line->discount_percent);
90
91
92                         $stock_gl_code = get_stock_gl_code($delivery_line->stock_id);
93
94                         /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
95                         if (is_inventory_item($delivery_line->stock_id) && $delivery_line->standard_cost != 0) {
96
97                                 /*first the cost of sales entry*/
98                                 // 2008-08-01. If there is a Customer Dimension, then override with this,
99                                 // else take the Item Dimension (if any)
100                                 $dim = ($delivery->dimension_id != $customer['dimension_id'] ? $delivery->dimension_id : 
101                                         ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_code["dimension_id"]));
102                                 $dim2 = ($delivery->dimension2_id != $customer['dimension2_id'] ? $delivery->dimension2_id : 
103                                         ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_code["dimension2_id"]));
104
105                                 add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no,
106                                         $delivery->document_date, $stock_gl_code["cogs_account"], $dim, $dim2, "",
107                                         $delivery_line->standard_cost * $delivery_line->qty_dispatched,
108                                         PT_CUSTOMER, $delivery->customer_id,
109                                         "The cost of sales GL posting could not be inserted");
110
111                                 /*now the stock entry*/
112
113                                 add_gl_trans_std_cost(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date,
114                                         $stock_gl_code["inventory_account"], 0, 0, "",
115                                         (-$delivery_line->standard_cost * $delivery_line->qty_dispatched),
116                                         PT_CUSTOMER, $delivery->customer_id,
117                                         "The stock side of the cost of sales GL posting could not be inserted");
118
119                         } /* end of if GL and stock integrated and standard cost !=0 */
120
121                 } /*quantity dispatched is more than 0 */
122         } /*end of order_line loop */
123
124         if ($bo_policy == 0) {
125                 // if cancelling any remaining quantities
126                 close_sales_order($delivery->order_no);
127         }
128
129         // taxes - this is for printing purposes
130         foreach ($taxes as $taxitem) {
131                 if ($taxitem['Net'] != 0) {
132                                 $ex_rate = get_exchange_rate_from_home_currency(get_customer_currency($delivery->customer_id), $delivery->document_date);
133                     add_trans_tax_details(ST_CUSTDELIVERY, $delivery_no, $taxitem['tax_type_id'],
134                                 $taxitem['rate'], $delivery->tax_included, $taxitem['Value'],
135                                 $taxitem['Net'], $ex_rate, $delivery->document_date, $delivery->reference );
136                 }
137         }
138
139         add_comments(ST_CUSTDELIVERY, $delivery_no, $delivery->document_date, $delivery->Comments);
140
141         if ($trans_no == 0) {
142                         $Refs->save(ST_CUSTDELIVERY, $delivery_no, $delivery->reference);
143         }
144
145         hook_db_postwrite($delivery, ST_CUSTDELIVERY);
146         commit_transaction();
147
148         return $delivery_no;
149 }
150
151 //--------------------------------------------------------------------------------------------------
152 function adjust_shipping_charge(&$delivery, $trans_no)
153 {
154         $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;
155         $result = db_query($sql, "Can not find delivery notes");
156         $row = db_fetch_row($result);
157         if (!$row[0]) $freight = 0;
158         else $freight = $row[0];
159         if ($freight < $delivery->freight_cost) $delivery->freight_cost = $delivery->freight_cost - $freight;
160         else $delivery->freight_cost = 0;
161 }
162
163 //--------------------------------------------------------------------------------------------------
164
165 function void_sales_delivery($type, $type_no, $transactions=true)
166 {
167         if ($transactions)
168                 begin_transaction();
169
170         hook_db_prevoid($type, $type_no);
171         
172         void_gl_trans($type, $type_no, true);
173
174         // reverse all the changes in the sales order
175         $items_result = get_customer_trans_details($type, $type_no);
176
177         $order = get_customer_trans_order($type, $type_no);
178
179         if ($order) {
180                 $auto = (get_reference(ST_SALESORDER, $order) == "auto");
181                 while ($row = db_fetch($items_result)) {
182                         update_parent_line(ST_CUSTDELIVERY, $row['src_id'], -$row['quantity'], $auto);
183                 }
184         }
185
186         // clear details after they've been reversed in the sales order
187         void_customer_trans_details($type, $type_no);
188
189         void_trans_tax_details($type, $type_no);
190
191         void_cust_allocations($type, $type_no);
192
193         // do this last because other voidings can depend on it
194         // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
195         void_customer_trans($type, $type_no);
196
197         if ($transactions)
198                 commit_transaction();
199 }
200 ?>