*** empty log message ***
[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, $invoice->dimension_id, $invoice->dimension2_id); 
65                 // 2008-06-14 extra $alloc, 2008-11-12 added dimension_id Joe Hunt
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
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 = ($invoice->dimension_id != $customer['dimension_id'] ? $invoice->dimension_id : 
109                                         ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_code["dimension_id"]));
110                                 $dim2 = ($invoice->dimension2_id != $customer['dimension2_id'] ? $invoice->dimension2_id : 
111                                         ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_code["dimension2_id"]));
112                                 add_gl_trans_customer(10, $invoice_no, $date_, $sales_account, $dim, $dim2,
113                                         (-$line_taxfree_price * $invoice_line->qty_dispatched),
114                                         $invoice->customer_id, "The sales price GL posting could not be inserted");
115
116                                 if ($invoice_line->discount_percent != 0) {
117
118                                         add_gl_trans_customer(10, $invoice_no, $date_,
119                                                 $branch_data["sales_discount_account"], $dim, $dim2,
120                                                 ($line_taxfree_price * $invoice_line->qty_dispatched * $invoice_line->discount_percent),
121                                                 $invoice->customer_id, "The sales discount GL posting could not be inserted");
122                                 } /*end of if discount !=0 */
123                         }
124                 } /*quantity dispatched is more than 0 */
125         } /*end of delivery_line loop */
126
127         /*Post debtors transaction to GL debit debtors, credit freight re-charged and credit sales */
128         if (($items_total + $charge_shipping) != 0) {
129                 add_gl_trans_customer(10, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
130                         ($items_total + $charge_shipping + $items_added_tax + $freight_added_tax),
131                         $invoice->customer_id, "The total debtor GL posting could not be inserted");
132         }
133         if ($charge_shipping != 0) {
134                 add_gl_trans_customer(10, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
135                         -$invoice->get_tax_free_shipping(), $invoice->customer_id,
136                         "The freight GL posting could not be inserted");
137         }
138         // post all taxes
139         foreach ($taxes as $taxitem) {
140                 if ($taxitem['Value'] != 0) {
141                         add_customer_trans_tax_detail_item(10, $invoice_no, $taxitem['tax_type_id'],
142                                 $taxitem['rate'], $invoice->tax_included, $taxitem['Value']);
143
144                         add_gl_trans_customer(10, $invoice_no, $date_, $taxitem['sales_gl_code'], 0, 0,
145                                 (-$taxitem['Value']), $invoice->customer_id,
146                                 "A tax GL posting could not be inserted");
147                 }
148         }
149
150         add_comments(10, $invoice_no, $date_, $invoice->Comments);
151
152         if ($trans_no == 0) {
153                 references::save_last($invoice->reference, 10);
154                 if ($invoice->cash) {
155                         $amount = $items_total + $items_added_tax + $invoice->freight_cost 
156                                 + $freight_added_tax;
157
158                         // to use debtors.pmt_discount on cash sale:
159                         // extend invoice entry page with final amount after discount 
160                         // and change line below.
161                         $discount = 0; // $invoice->cash_discount*$amount;
162                         $pmtno = write_customer_payment(0, $invoice->customer_id, 
163                                 $invoice->Branch, $invoice->cash_account, $date_,
164                                 references::get_next(12), $amount-$discount, $discount,
165                                 _('Cash invoice').' '.$invoice->trans_no);
166                         add_cust_allocation($amount, 12, $pmtno, 10, $invoice_no);
167                 }
168         }
169         commit_transaction();
170
171         return $invoice_no;
172 }
173
174 //--------------------------------------------------------------------------------------------------
175
176 function void_sales_invoice($type, $type_no)
177 {
178         begin_transaction();
179
180         void_bank_trans($type, $type_no, true);
181         void_gl_trans($type, $type_no, true);
182
183         // reverse all the changes in parent document(s)
184         $items_result = get_customer_trans_details($type, $type_no);
185
186         $deliveries = get_parent_trans($type, $type_no);
187
188         if (count($deliveries)) {
189                 $srcdetails = get_customer_trans_details(get_parent_type($type), $deliveries);
190                 while ($row = db_fetch($items_result)) {
191                         $src_line = db_fetch($srcdetails);
192                         update_parent_line(10, $src_line['id'], -$row['quantity']);
193                 }
194         }
195         // clear details after they've been reversed in the sales order
196         void_customer_trans_details($type, $type_no);
197
198         void_customer_trans_tax_details($type, $type_no);
199
200         void_cust_allocations($type, $type_no);
201
202         // do this last because other voidings can depend on it - especially voiding
203         // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
204         void_customer_trans($type, $type_no);
205
206         commit_transaction();
207 }
208
209 ?>