Changed license type to GPLv3 in top of files
[fa-stable.git] / sales / includes / db / sales_invoice_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //-----------------------------------------------------------------------------
13 //      Add or update Sales Invoice
14 //
15 function write_sales_invoice(&$invoice)
16 {
17
18         $trans_no = $invoice->trans_no;
19         if (is_array($trans_no))
20                 $trans_no = key($trans_no);
21
22         $date_ = $invoice->document_date;
23         $charge_shipping =$invoice->freight_cost;
24
25         begin_transaction();
26
27         $company_data = get_company_prefs();
28
29         $branch_data = get_branch_accounts($invoice->Branch);
30
31         $customer = get_customer($invoice->customer_id);
32
33         // offer price values without freight costs
34         $items_total = $invoice->get_items_total_dispatch();
35         $freight_tax = $invoice->get_shipping_tax();
36
37         $delivery_no = $invoice->src_docs;
38         if (is_array($delivery_no))
39                 $delivery_no = 0;
40
41         update_customer_trans_version(get_parent_type(10), $invoice->src_docs);
42
43         $ov_gst = 0;
44         $taxes = $invoice->get_taxes(); // all taxes with freight_tax
45
46         foreach ($taxes as $taxitem) {
47                 $taxitem['Value'] =  round2($taxitem['Value'], user_price_dec());
48                 $ov_gst +=  $taxitem['Value'];
49         }
50
51         if($invoice->tax_included==0) {
52             $items_added_tax = $ov_gst-$freight_tax;
53             $freight_added_tax = $freight_tax;
54         } else {
55             $items_added_tax = 0;
56             $freight_added_tax = 0;
57         }
58         // 2006-06-14. If the Customer Branch AR Account is set to a Bank Account,
59         // the transaction will be settled at once.
60         if (is_bank_account($branch_data['receivables_account']))
61                 $alloc = $items_total + $items_added_tax + $invoice->freight_cost + $freight_added_tax;
62         else
63                 $alloc = 0;
64
65         /* Insert/update the debtor_trans */
66         $sales_order = $invoice->order_no;
67         if (is_array($sales_order))
68                         $sales_order = $sales_order[0]; // assume all crucial SO data are same for every delivery
69
70         $invoice_no = write_customer_trans(10, $trans_no, $invoice->customer_id,
71                 $invoice->Branch, $date_, $invoice->reference, $items_total, 0,
72                 $items_added_tax, $invoice->freight_cost, $freight_added_tax,
73                 $invoice->sales_type, $sales_order, $delivery_no,
74                 $invoice->ship_via, $invoice->due_date, $alloc, 0, $invoice->dimension_id, $invoice->dimension2_id); 
75                 // 2008-06-14 extra $alloc, 2008-11-12 added dimension_id Joe Hunt
76
77         if ($trans_no == 0) {
78                 $invoice->trans_no = array($invoice_no=>0);
79                 set_document_parent($invoice);
80         } else {
81                 delete_comments(10, $invoice_no);
82                 void_gl_trans(10, $invoice_no, true);
83                 void_cust_allocations(10, $invoice_no); // ?
84                 void_trans_tax_details(10, $invoice_no);
85         }
86         $total = 0;
87         foreach ($invoice->line_items as $invoice_line) {
88
89                 $line_taxfree_price = get_tax_free_price_for_item($invoice_line->stock_id,
90                         $invoice_line->price, 0, $invoice->tax_included,
91                         $invoice->tax_group_array);
92
93                 $line_tax = get_full_price_for_item($invoice_line->stock_id,
94                         $invoice_line->price, 0, $invoice->tax_included,
95                         $invoice->tax_group_array) - $line_taxfree_price;
96
97                 write_customer_trans_detail_item(10, $invoice_no, $invoice_line->stock_id,
98                         $invoice_line->item_description, $invoice_line->qty_dispatched,
99                         $invoice_line->line_price(), $line_tax, $invoice_line->discount_percent,
100                         $invoice_line->standard_cost,
101                         $trans_no ? $invoice_line->id : 0);
102
103                 // Update delivery items for the quantity invoiced
104                 if ($invoice_line->qty_old != $invoice_line->qty_dispatched)
105                         update_parent_line(10, $invoice_line->src_id, ($invoice_line->qty_dispatched-$invoice_line->qty_old));
106
107                 if ($invoice_line->qty_dispatched != 0) {
108                         $stock_gl_code = get_stock_gl_code($invoice_line->stock_id);
109
110                         if ($invoice_line->line_price() != 0) {
111                                 //Post sales transaction to GL credit sales
112
113                                 // 2008-06-14. If there is a Branch Sales Account, then override with this,
114                                 // else take the Item Sales Account
115                                 $sales_account = ($branch_data['sales_account'] != "" ? $branch_data['sales_account'] : $stock_gl_code['sales_account']);
116                                 // 2008-08-01. If there is a Customer Dimension, then override with this,
117                                 // else take the Item Dimension (if any)
118                                 $dim = ($invoice->dimension_id != $customer['dimension_id'] ? $invoice->dimension_id : 
119                                         ($customer['dimension_id'] != 0 ? $customer["dimension_id"] : $stock_gl_code["dimension_id"]));
120                                 $dim2 = ($invoice->dimension2_id != $customer['dimension2_id'] ? $invoice->dimension2_id : 
121                                         ($customer['dimension2_id'] != 0 ? $customer["dimension2_id"] : $stock_gl_code["dimension2_id"]));
122                                 $total += add_gl_trans_customer(10, $invoice_no, $date_, $sales_account, $dim, $dim2,
123                                         (-$line_taxfree_price * $invoice_line->qty_dispatched),
124                                         $invoice->customer_id, "The sales price GL posting could not be inserted");
125
126                                 if ($invoice_line->discount_percent != 0) {
127
128                                         $total += add_gl_trans_customer(10, $invoice_no, $date_,
129                                                 $branch_data["sales_discount_account"], $dim, $dim2,
130                                                 ($line_taxfree_price * $invoice_line->qty_dispatched * $invoice_line->discount_percent),
131                                                 $invoice->customer_id, "The sales discount GL posting could not be inserted");
132                                 } /*end of if discount !=0 */
133                         }
134                 } /*quantity dispatched is more than 0 */
135         } /*end of delivery_line loop */
136
137         if (($items_total + $charge_shipping) != 0) {
138                 $total += add_gl_trans_customer(10, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
139                         ($items_total + $charge_shipping + $items_added_tax + $freight_added_tax),
140                         $invoice->customer_id, "The total debtor GL posting could not be inserted");
141         }
142
143         if ($charge_shipping != 0) {
144                 $total += add_gl_trans_customer(10, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
145                         -$invoice->get_tax_free_shipping(), $invoice->customer_id,
146                         "The freight GL posting could not be inserted");
147         }
148         // post all taxes
149         foreach ($taxes as $taxitem) {
150
151                 if ($taxitem['Net'] != 0) {
152                         $ex_rate = get_exchange_rate_from_home_currency(get_customer_currency($invoice->customer_id), $date_);
153                         add_trans_tax_details(10, $invoice_no, $taxitem['tax_type_id'],
154                                 $taxitem['rate'], $invoice->tax_included, $taxitem['Value'],
155                                  $taxitem['Net'], $ex_rate, $date_, $invoice->reference);
156
157                         $total += add_gl_trans_customer(10, $invoice_no, $date_, $taxitem['sales_gl_code'], 0, 0,
158                                 (-$taxitem['Value']), $invoice->customer_id,
159                                 "A tax GL posting could not be inserted");
160                 }
161         }
162
163         /*Post a balance post if $total != 0 */
164         add_gl_balance(10, $invoice_no, $date_, -$total, payment_person_types::customer(), $invoice->customer_id);      
165
166         add_comments(10, $invoice_no, $date_, $invoice->Comments);
167
168         if ($trans_no == 0) {
169                 references::save_last($invoice->reference, 10);
170                 if ($invoice->cash) {
171                         $amount = $items_total + $items_added_tax + $invoice->freight_cost 
172                                 + $freight_added_tax;
173
174                         // to use debtors.pmt_discount on cash sale:
175                         // extend invoice entry page with final amount after discount 
176                         // and change line below.
177                         $discount = 0; // $invoice->cash_discount*$amount;
178                         $pmtno = write_customer_payment(0, $invoice->customer_id, 
179                                 $invoice->Branch, $invoice->cash_account, $date_,
180                                 references::get_next(12), $amount-$discount, $discount,
181                                 _('Cash invoice').' '.$invoice->trans_no);
182                         add_cust_allocation($amount, 12, $pmtno, 10, $invoice_no);
183
184                         update_debtor_trans_allocation(10, $invoice_no, $amount);
185                         update_debtor_trans_allocation(12, $pmtno, $amount);
186                 }
187         }
188         commit_transaction();
189
190         return $invoice_no;
191 }
192
193 //--------------------------------------------------------------------------------------------------
194
195 function void_sales_invoice($type, $type_no)
196 {
197         begin_transaction();
198
199         void_bank_trans($type, $type_no, true);
200         void_gl_trans($type, $type_no, true);
201
202         // reverse all the changes in parent document(s)
203         $items_result = get_customer_trans_details($type, $type_no);
204
205         $deliveries = get_parent_trans($type, $type_no);
206
207         if ($deliveries !== 0) {
208                 $srcdetails = get_customer_trans_details(get_parent_type($type), $deliveries);
209                 while ($row = db_fetch($items_result)) {
210                         $src_line = db_fetch($srcdetails);
211                         update_parent_line($type, $src_line['id'], -$row['quantity']);
212                 }
213         }
214         // clear details after they've been reversed in the sales order
215         void_customer_trans_details($type, $type_no);
216
217         void_trans_tax_details($type, $type_no);
218
219         void_cust_allocations($type, $type_no);
220
221         // do this last because other voidings can depend on it - especially voiding
222         // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
223         void_customer_trans($type, $type_no);
224
225         commit_transaction();
226 }
227
228 ?>