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