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