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