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