Removed redundant form_types functions and the file /reporting/includes/form_types...
[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
47         /* Insert/update the debtor_trans */
48         $sales_order = $invoice->order_no;
49         if (is_array($sales_order))
50                         $sales_order = $sales_order[0]; // assume all crucial SO data are same for every delivery
51
52         $invoice_no = write_customer_trans(10, $trans_no, $invoice->customer_id,
53                 $invoice->Branch, $date_, $invoice->reference, $items_total, 0,
54                 $items_added_tax, $invoice->freight_cost, $freight_added_tax,
55                 $invoice->default_sales_type, $sales_order, $delivery_no,
56                 $invoice->ship_via, $invoice->due_date);
57
58         if ($trans_no == 0) {
59                 $invoice->trans_no = array($invoice_no=>0);
60                 set_document_parent($invoice);
61         } else {
62                 delete_comments(10, $invoice_no);
63                 void_gl_trans(10, $invoice_no, true);
64                 void_cust_allocations(10, $invoice_no); // ?
65                 void_customer_trans_tax_details(10, $invoice_no);
66         }
67
68         foreach ($invoice->line_items as $invoice_line) {
69
70                 $line_taxfree_price = get_tax_free_price_for_item($invoice_line->stock_id,
71                         $invoice_line->price, 0, $invoice->tax_included,
72                         $invoice->tax_group_array);
73
74                 $line_tax = get_full_price_for_item($invoice_line->stock_id,
75                         $invoice_line->price, 0, $invoice->tax_included,
76                         $invoice->tax_group_array) - $line_taxfree_price;
77
78                 write_customer_trans_detail_item(10, $invoice_no, $invoice_line->stock_id,
79                         $invoice_line->item_description, $invoice_line->qty_dispatched,
80                         $invoice_line->line_price(), $line_tax, $invoice_line->discount_percent,
81                         $invoice_line->standard_cost,
82                         $trans_no ? $invoice_line->id : 0);
83
84                 // Update delivery items for the quantity invoiced
85                 if ($invoice_line->qty_old != $invoice_line->qty_dispatched)
86                         update_parent_line(10, $invoice_line->id, ($invoice_line->qty_dispatched-$invoice_line->qty_old));
87
88                 if ($invoice_line->qty_dispatched != 0) {
89                         $stock_gl_code = get_stock_gl_code($invoice_line->stock_id);
90
91                         if ($invoice_line->line_price() != 0) {
92                                 //Post sales transaction to GL credit sales
93
94                                         add_gl_trans_customer(10, $invoice_no, $date_, $stock_gl_code["sales_account"],
95                                                 $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"],
96                                                 (-$line_taxfree_price * $invoice_line->qty_dispatched),
97                                                 $invoice->customer_id, "The sales price GL posting could not be inserted");
98
99                                 if ($invoice_line->discount_percent != 0) {
100
101                                         add_gl_trans_customer(10, $invoice_no, $date_,
102                                                 $branch_data["sales_discount_account"], $stock_gl_code["dimension_id"],
103                                                 $stock_gl_code["dimension2_id"],
104                                                 ($line_taxfree_price * $invoice_line->qty_dispatched * $invoice_line->discount_percent),
105                                                 $invoice->customer_id, "The sales discount GL posting could not be inserted");
106                                 } /*end of if discount !=0 */
107                         }
108                 } /*quantity dispatched is more than 0 */
109         } /*end of delivery_line loop */
110
111         /*Post debtors transaction to GL debit debtors, credit freight re-charged and credit sales */
112         if (($items_total + $charge_shipping) != 0) {
113                 add_gl_trans_customer(10, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
114                         ($items_total + $charge_shipping + $items_added_tax + $freight_added_tax),
115                         $invoice->customer_id, "The total debtor GL posting could not be inserted");
116         }
117         if ($charge_shipping != 0) {
118                 add_gl_trans_customer(10, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
119                         -$invoice->get_tax_free_shipping(), $invoice->customer_id,
120                         "The freight GL posting could not be inserted");
121         }
122         // post all taxes
123         foreach ($taxes as $taxitem) {
124                 if ($taxitem['Value'] != 0) {
125                         add_customer_trans_tax_detail_item(10, $invoice_no, $taxitem['tax_type_id'],
126                                 $taxitem['rate'], $invoice->tax_included, $taxitem['Value']);
127
128                         add_gl_trans_customer(10, $invoice_no, $date_, $taxitem['sales_gl_code'], 0, 0,
129                                 (-$taxitem['Value']), $invoice->customer_id,
130                                 "A tax GL posting could not be inserted");
131                 }
132         }
133
134         add_comments(10, $invoice_no, $date_, $invoice->Comments);
135
136         if ($trans_no == 0) {
137                 references::save_last($invoice->reference, 10);
138         }
139
140         commit_transaction();
141
142         return $invoice_no;
143 }
144
145 //--------------------------------------------------------------------------------------------------
146
147 function void_sales_invoice($type, $type_no)
148 {
149         begin_transaction();
150
151         void_bank_trans($type, $type_no, true);
152         void_gl_trans($type, $type_no, true);
153
154         // for invoices and credits related to invoices,
155         // reverse all the changes in the sales order
156         $items_result = get_customer_trans_details($type, $type_no);
157
158         $delivery = get_customer_trans_link($type, $type_no);
159
160         if ($delivery) {
161                 while ($row = db_fetch($items_result)) {
162                         update_parent_line(10, $row['id'], -$row['quantity']);
163                 }
164         }
165
166         // clear details after they've been reversed in the sales order
167         void_customer_trans_details($type, $type_no);
168
169         void_customer_trans_tax_details($type, $type_no);
170
171         void_cust_allocations($type, $type_no);
172
173         // do this last because other voidings can depend on it - especially voiding
174         // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
175         void_customer_trans($type, $type_no);
176
177         commit_transaction();
178 }
179
180 ?>