Unstable release 2.
[fa-stable.git] / sales / includes / db / sales_invoice_db.inc
1 <?php
2
3 // $bo_policy = 0, cancel remaining quantities on order
4 // = 1, backorder remaining quantities
5
6 function add_sales_invoice(&$invoice, $date_, $due_date, $sales_order, $tax_group_id,
7         $charge_shipping, $location, $ship_via, $sales_type, $reference, $memo_, $bo_policy)
8 {
9         begin_transaction();
10
11         $company_data = get_company_prefs();
12
13         $branch_data = get_branch_accounts($invoice->Branch);
14
15         $invoice_items_total = $invoice->get_items_total_dispatch($tax_group_id);
16
17     $tax_total = 0;
18     $taxes = $invoice->get_taxes($tax_group_id, $charge_shipping);
19     foreach ($taxes as $taxitem)
20         $tax_total +=  $taxitem['Value'];
21
22         /*Now insert the debtor_trans */
23
24         $invoice_no = add_customer_trans(10, $invoice->customer_id, $invoice->Branch, $date_,
25                 $reference, $invoice_items_total, 0, $tax_total, $charge_shipping,
26         $sales_type, $sales_order, $ship_via, $due_date);
27
28         // If balance of the order cancelled update sales order details quantity.
29         foreach ($invoice->line_items as $order_line)
30         {
31
32                 if ($order_line->qty_dispatched != 0)
33                 {
34
35                         $line_taxfree_price = $order_line->taxfree_price($tax_group_id);
36                         $line_tax = $order_line->full_price() - $line_taxfree_price;
37
38                         // Now update sales_order_details for the quantity invoiced
39                         dispatch_sales_order_item($sales_order, $order_line->id,
40                                 $order_line->qty_dispatched);
41
42                         /* add invoice details and stock movements */
43                         $memo_ = "";
44
45                         add_customer_trans_detail_item(10, $invoice_no, $order_line->stock_id,
46                                 $order_line->item_description, $location, $date_,
47                                 -$order_line->qty_dispatched, $line_taxfree_price, $line_tax,
48                                 $order_line->discount_percent, $memo_, $order_line->standard_cost);
49
50                         $stock_gl_code = get_stock_gl_code($order_line->stock_id);
51
52                         /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
53                         if ($order_line->standard_cost != 0)
54                         {
55
56                                 /*first the cost of sales entry*/
57                 add_gl_trans_std_cost(10, $invoice_no, $date_, $stock_gl_code["cogs_account"],
58                         $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"], "",
59                         $order_line->standard_cost * $order_line->qty_dispatched,
60                         payment_person_types::customer(), $invoice->customer_id,
61                                         "The cost of sales GL posting could not be inserted");
62
63                                 /*now the stock entry*/
64                                 //$stock_gl_code = get_stock_gl_code($order_line->stock_id);
65
66                 add_gl_trans_std_cost(10, $invoice_no, $date_, $stock_gl_code["inventory_account"], 0, 0, "",
67                         (-$order_line->standard_cost * $order_line->qty_dispatched),
68                         payment_person_types::customer(), $invoice->customer_id,
69                                         "The stock side of the cost of sales GL posting could not be inserted");
70
71                         } /* end of if GL and stock integrated and standard cost !=0 */
72
73                         if ($order_line->price != 0)
74                         {
75
76                                 //Post sales transaction to GL credit sales
77
78                 add_gl_trans_customer(10, $invoice_no, $date_, $stock_gl_code["sales_account"],
79                         $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"],
80                                 (-$line_taxfree_price * $order_line->qty_dispatched),
81                                 $invoice->customer_id, "The sales price GL posting could not be inserted");
82
83                                 if ($order_line->discount_percent != 0)
84                                 {
85
86                         add_gl_trans_customer(10, $invoice_no, $date_,
87                                         $branch_data["sales_discount_account"], $stock_gl_code["dimension_id"],
88                                         $stock_gl_code["dimension2_id"],
89                                         ($line_taxfree_price * $order_line->qty_dispatched * $order_line->discount_percent),
90                                         $invoice->customer_id, "The sales discount GL posting could not be inserted");
91                                 } /*end of if discount !=0 */
92                         } /*end of if sales integrated with debtors */
93
94                 } /*quantity dispatched is more than 0 */
95         } /*end of order_line loop */
96
97
98         if ($bo_policy == 0)
99         {
100                 // if cancelling any remaining quantities
101                 close_sales_order($sales_order);
102         }
103
104         /*Post debtors transaction to GL debit debtors, credit freight re-charged and credit sales */
105         if (($invoice_items_total + $charge_shipping + $tax_total) != 0)
106         {
107
108         add_gl_trans_customer(10, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
109                         ($invoice_items_total + $charge_shipping + $tax_total),
110                         $invoice->customer_id, "The total debtor GL posting could not be inserted");
111         }
112
113         if ($charge_shipping != 0)
114         {
115
116         add_gl_trans_customer(10, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
117                         (-$charge_shipping), $invoice->customer_id,
118                         "The freight GL posting could not be inserted");
119         }
120
121     foreach ($taxes as $taxitem)
122     {
123         if ($taxitem['Value'] != 0)
124         {
125                         add_customer_trans_tax_detail_item(10, $invoice_no, $taxitem['tax_type_id'],
126                                 $taxitem['rate'], $taxitem['included_in_price'], $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_, $memo_);
135
136         add_forms_for_sys_type(10, $invoice_no, $location);
137
138         references::save_last($reference, 10);
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         $order = get_customer_trans_order($type, $type_no);
159
160         if ($order)
161         {
162                 while ($row = db_fetch($items_result))
163                 {
164                         dispatch_sales_order_item($order, $row["id"], $row["quantity"]);
165                 }
166         }
167
168         // clear details after they've been reversed in the sales order
169         void_customer_trans_details($type, $type_no);
170
171         void_customer_trans_tax_details($type, $type_no);
172
173         void_cust_allocations($type, $type_no);
174
175         // do this last because other voidings can depend on it - especially voiding
176         // DO NOT MOVE THIS ABOVE VOIDING or we can end up with trans with alloc < 0
177         void_customer_trans($type, $type_no);
178
179         commit_transaction();
180 }
181
182 //--------------------------------------------------------------------------------------------------
183
184
185
186
187
188 ?>