[0000101] Roll back of yeasterday issues
[fa-stable.git] / sales / includes / db / sales_invoice_db.inc
1 <?php
2
3 //-----------------------------------------------------------------------------
4 //      Add or update Sales Invoice
5 //
6 function write_sales_invoice(&$invoice)
7 {
8
9         $trans_no = $invoice->trans_no;
10         if (is_array($trans_no))
11                 $trans_no = key($trans_no);
12
13         $date_ = $invoice->document_date;
14         $charge_shipping =$invoice->freight_cost;
15
16         begin_transaction();
17
18         $company_data = get_company_prefs();
19
20         $branch_data = get_branch_accounts($invoice->Branch);
21
22         $customer = get_customer($invoice->customer_id);
23
24         // offer price values without freight costs
25         $items_total = $invoice->get_items_total_dispatch();
26         $freight_tax = $invoice->get_shipping_tax();
27
28         $delivery_no = $invoice->src_docs;
29         if (is_array($delivery_no))
30                 $delivery_no = 0;
31
32         update_customer_trans_version(get_parent_type(10), $invoice->src_docs);
33
34         $ov_gst = 0;
35         $taxes = $invoice->get_taxes(); // all taxes with freight_tax
36
37         foreach ($taxes as $taxitem) {
38                 $taxitem['Value'] =  round2($taxitem['Value'], user_price_dec());
39                 $ov_gst +=  $taxitem['Value'];
40         }
41
42         if($invoice->tax_included==0) {
43             $items_added_tax = $ov_gst-$freight_tax;
44             $freight_added_tax = $freight_tax;
45         } else {
46             $items_added_tax = 0;
47             $freight_added_tax = 0;
48         }
49         // 2006-06-14. If the Customer Branch AR Account is set to a Bank Account,
50         // the transaction will be settled at once.
51         if (is_bank_account($branch_data['receivables_account']))
52                 $alloc = $items_total + $items_added_tax + $invoice->freight_cost + $freight_added_tax;
53         else
54                 $alloc = 0;
55
56         /* Insert/update the debtor_trans */
57         $sales_order = $invoice->order_no;
58         if (is_array($sales_order))
59                         $sales_order = $sales_order[0]; // assume all crucial SO data are same for every delivery
60
61         $invoice_no = write_customer_trans(10, $trans_no, $invoice->customer_id,
62                 $invoice->Branch, $date_, $invoice->reference, $items_total, 0,
63                 $items_added_tax, $invoice->freight_cost, $freight_added_tax,
64                 $invoice->sales_type, $sales_order, $delivery_no,
65                 $invoice->ship_via, $invoice->due_date, $alloc); // 2008-06-14 extra $alloc
66
67         if ($trans_no == 0) {
68                 $invoice->trans_no = array($invoice_no=>0);
69                 set_document_parent($invoice);
70         } else {
71                 delete_comments(10, $invoice_no);
72                 void_gl_trans(10, $invoice_no, true);
73                 void_cust_allocations(10, $invoice_no); // ?
74                 void_customer_trans_tax_details(10, $invoice_no);
75         }
76         $total = 0;
77         foreach ($invoice->line_items as $invoice_line) {
78
79                 $line_taxfree_price = get_tax_free_price_for_item($invoice_line->stock_id,
80                         $invoice_line->price, 0, $invoice->tax_included,
81                         $invoice->tax_group_array);
82
83                 $line_tax = get_full_price_for_item($invoice_line->stock_id,
84                         $invoice_line->price, 0, $invoice->tax_included,
85                         $invoice->tax_group_array) - $line_taxfree_price;
86
87                 write_customer_trans_detail_item(10, $invoice_no, $invoice_line->stock_id,
88                         $invoice_line->item_description, $invoice_line->qty_dispatched,
89                         $invoice_line->line_price(), $line_tax, $invoice_line->discount_percent,
90                         $invoice_line->standard_cost,
91                         $trans_no ? $invoice_line->id : 0);
92
93                 // Update delivery items for the quantity invoiced
94                 if ($invoice_line->qty_old != $invoice_line->qty_dispatched)
95                         update_parent_line(10, $invoice_line->src_id, ($invoice_line->qty_dispatched-$invoice_line->qty_old));
96
97                 if ($invoice_line->qty_dispatched != 0) {
98                         $stock_gl_code = get_stock_gl_code($invoice_line->stock_id);
99
100                         if ($invoice_line->line_price() != 0) {
101                                 //Post sales transaction to GL credit sales
102
103                                 // 2008-06-14. If there is a Branch Sales Account, then override with this,
104                                 // else take the Item Sales Account
105                                 $sales_account = ($branch_data['sales_account'] != "" ? $branch_data['sales_account'] : $stock_gl_code['sales_account']);
106                                 // 2008-08-01. If there is a Customer Dimension, then override with this,
107                                 // else take the Item Dimension (if any)
108                                 $dim = ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_code["dimension_id"]);
109                                 $dim2 = ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_code["dimension2_id"]);
110                                 $total += add_gl_trans_customer(10, $invoice_no, $date_, $sales_account, $dim, $dim2,
111                                         (-$line_taxfree_price * $invoice_line->qty_dispatched),
112                                         $invoice->customer_id, "The sales price GL posting could not be inserted");
113
114                                 if ($invoice_line->discount_percent != 0) {
115
116                                         $total += add_gl_trans_customer(10, $invoice_no, $date_,
117                                                 $branch_data["sales_discount_account"], $dim, $dim2,
118                                                 ($line_taxfree_price * $invoice_line->qty_dispatched * $invoice_line->discount_percent),
119                                                 $invoice->customer_id, "The sales discount GL posting could not be inserted");
120                                 } /*end of if discount !=0 */
121                         }
122                 } /*quantity dispatched is more than 0 */
123         } /*end of delivery_line loop */
124
125         if (($items_total + $charge_shipping) != 0) {
126                 $total += add_gl_trans_customer(10, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
127                         ($items_total + $charge_shipping + $items_added_tax + $freight_added_tax),
128                         $invoice->customer_id, "The total debtor GL posting could not be inserted");
129         }
130
131         if ($charge_shipping != 0) {
132                 $total += add_gl_trans_customer(10, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
133                         -$invoice->get_tax_free_shipping(), $invoice->customer_id,
134                         "The freight GL posting could not be inserted");
135         }
136         // post all taxes
137         foreach ($taxes as $taxitem) {
138                 if ($taxitem['Value'] != 0) {
139                         add_customer_trans_tax_detail_item(10, $invoice_no, $taxitem['tax_type_id'],
140                                 $taxitem['rate'], $invoice->tax_included, $taxitem['Value']);
141
142                         $total += add_gl_trans_customer(10, $invoice_no, $date_, $taxitem['sales_gl_code'], 0, 0,
143                                 (-$taxitem['Value']), $invoice->customer_id,
144                                 "A tax GL posting could not be inserted");
145                 }
146         }
147
148         /*Post a balance post if $total != 0 */
149         add_gl_balance(10, $invoice_no, $date_, -$total, payment_person_types::customer(), $invoice->customer_id);      
150
151         add_comments(10, $invoice_no, $date_, $invoice->Comments);
152
153         if ($trans_no == 0) {
154                 references::save_last($invoice->reference, 10);
155         }
156
157         commit_transaction();
158
159         return $invoice_no;
160 }
161
162 //--------------------------------------------------------------------------------------------------
163
164 function void_sales_invoice($type, $type_no)
165 {
166         begin_transaction();
167
168         void_bank_trans($type, $type_no, true);
169         void_gl_trans($type, $type_no, true);
170
171         // reverse all the changes in parent document(s)
172         $items_result = get_customer_trans_details($type, $type_no);
173
174         $deliveries = get_parent_trans($type, $type_no);
175
176         if ($deliveries !== 0) {
177                 $srcdetails = get_customer_trans_details(get_parent_type($type), $deliveries);
178                 while ($row = db_fetch($items_result)) {
179                         $src_line = db_fetch($srcdetails);
180                         update_parent_line($type, $src_line['id'], -$row['quantity']);
181                 }
182         }
183         // clear details after they've been reversed in the sales order
184         void_customer_trans_details($type, $type_no);
185
186         void_customer_trans_tax_details($type, $type_no);
187
188         void_cust_allocations($type, $type_no);
189
190         // do this last because other voidings can depend on it - especially voiding
191         // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
192         void_customer_trans($type, $type_no);
193
194         commit_transaction();
195 }
196
197 ?>