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