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