Huge sales module changes toward delivery and invoicing separation. Includes some...
[fa-stable.git] / sales / includes / db / sales_credit_db.inc
1 <?php
2
3 //----------------------------------------------------------------------------------------
4
5 function add_credit_note($credit_items, $credit_date, $credit_type, $tax_group_id,
6         $charge_freight, $sales_type, $shipper, $reference, $memo_, $write_off_gl_code)
7 {
8         begin_transaction();
9
10         $branch_data = get_branch_accounts($credit_items->Branch);
11
12         $credit_items_total = $credit_items->get_items_total($tax_group_id);
13
14     $tax_total = 0;
15     $taxes = $credit_items->get_taxes($tax_group_id, $charge_freight);
16     foreach ($taxes as $taxitem)
17         $tax_total +=  $taxitem['Value'];
18
19         $credit_no = add_customer_trans(11, $credit_items->customer_id, $credit_items->Branch,
20         $credit_date, $reference, -($credit_items_total), 0, -$tax_total, -$charge_freight,
21         $sales_type, 0, $credit_items->ship_via);
22
23         foreach ($credit_items->line_items as $credit_line)
24         {
25
26                 if ($credit_line->quantity > 0)
27                 {
28                         // the functions use qty_dispatched - so until that's fixed make sure it's set
29                         // or KABOOOOM
30                         $credit_line->qty_dispatched = $credit_line->quantity;
31
32                         add_gl_trans_credit_order_item($credit_items, $credit_line, $credit_no, $credit_date,
33                         $tax_group_id, $credit_type);
34
35                 add_gl_trans_credit_costs($credit_items, $credit_line, $credit_no, $credit_date,
36                         $tax_group_id, $credit_type, $write_off_gl_code, $branch_data);
37
38                   } /*quantity credited is more than 0 */
39          } /*end of credit_line loop */
40
41         /*Post credit note transaction to GL credit debtors,
42         debit freight re-charged and debit sales */
43
44         add_g_trans_credit_common($credit_items, $credit_no, $credit_date,
45                 $credit_items_total, $charge_freight, $taxes, $tax_total, $branch_data);
46
47         add_comments(11, $credit_no, $credit_date, $memo_);
48
49         add_forms_for_sys_type(11, $credit_no, $credit_items->Location);
50
51         references::save_last($reference, 11);
52
53         commit_transaction();
54
55         return $credit_no;
56 }
57
58 //----------------------------------------------------------------------------------------
59
60 function credit_invoice($credit_items, $invoice_no, $order_no,
61         $default_dispatch_date, $credit_type, $tax_group_id,
62         $charge_freight, $reference, $memo_, $write_off_gl_code)
63 {
64         begin_transaction();
65
66         $branch_data = get_branch_accounts($credit_items->Branch);
67
68         $invoice_alloc_balance = get_DebtorTrans_allocation_balance(10, $invoice_no);
69
70         $invoice_items_total = $credit_items->get_items_total_dispatch($tax_group_id);
71
72     $tax_total = 0;
73     $taxes = $credit_items->get_taxes($tax_group_id, $charge_freight);
74     foreach ($taxes as $taxitem)
75         $tax_total +=  $taxitem['Value'];
76
77         $allocate_amount = 0;
78
79         if ($invoice_alloc_balance > 0)
80         {       /*the invoice is not already fully allocated */
81
82                 if ($invoice_alloc_balance > ($invoice_items_total + $charge_freight + $tax_total))
83                 {
84
85                         $allocate_amount = $invoice_items_total + $charge_freight + $tax_total;
86                 }
87                 else
88                 {       /*the balance left to allocate is less than the credit note value */
89                         $allocate_amount = $invoice_alloc_balance;
90                 }
91
92                 update_debtor_trans_allocation(10, $invoice_no, $allocate_amount);
93         }
94
95         /*Now insert the Credit Note into the debtor_trans table with the allocations as calculated above*/
96         // all amounts in debtor's currency
97
98         $credit_no = add_customer_trans(11, $credit_items->customer_id, $credit_items->Branch,
99         $default_dispatch_date, $reference,     -($invoice_items_total), 0, -$tax_total,
100         -$charge_freight, $credit_items->default_sales_type,    $order_no,
101         $credit_items->ship_via, "", $allocate_amount);
102
103         /*Now insert the allocation record if > 0 */
104         if ($allocate_amount != 0)
105         {
106                 add_cust_allocation($allocate_amount, 11, $credit_no, 10, $invoice_no);
107         }
108
109         /* Update sales order details quantity invoiced less this credit quantity. */
110         foreach ($credit_items->line_items as $order_line)
111         {
112
113                 if ($order_line->qty_dispatched >0)
114                 {
115
116                         // always deduct the credited/returned quantities from the sales order
117                         dispatch_sales_order_item($order_no, $order_line->stock_id, -$order_line->qty_dispatched, $order_line->id);
118                         
119                         //if (($_POST['credit_type']=="Return") OR ($_POST['ItemsAction']==1))
120                         /*{
121                                 $sql = "UPDATE ".TB_PREF."sales_order_details 
122                                                         SET qty_sent = qty_sent - " . $order_line->qty_dispatched . "
123                                                 WHERE order_no = " . $order_no . " 
124                                                 AND stk_code = '" . $order_line->stock_id . "'";
125                                 $result = db_query($sql,"The sales order detail record could not be updated for the reduced quantity invoiced");
126                         }*/
127
128                         /*Now update sales_order_details for the quantity invoiced and the actual dispatch dates. */
129
130                         add_gl_trans_credit_order_item($credit_items, $order_line, $credit_no,
131                                 $default_dispatch_date, $tax_group_id, $credit_type, $invoice_no);
132
133                 add_gl_trans_credit_costs($credit_items, $order_line, $credit_no, $default_dispatch_date,
134                         $tax_group_id, $credit_type, $write_off_gl_code, $branch_data);
135
136                 } /*quantity dispatched is more than 0 */
137         } /*end of order_line loop */
138
139         add_g_trans_credit_common($credit_items, $credit_no, $default_dispatch_date,
140                 $invoice_items_total, $charge_freight, $taxes, $tax_total, $branch_data);
141
142         add_comments(11, $credit_no, $default_dispatch_date, $memo_);
143
144         add_forms_for_sys_type(11, $credit_no);
145
146         references::save_last($reference, 11);
147
148         commit_transaction();
149
150         return $credit_no;
151 }
152
153 //----------------------------------------------------------------------------------------
154
155 function add_gl_trans_credit_order_item(&$order, &$order_line, $credit_no, $date_, $tax_group_id,
156         $credit_type, $credited_invoice=0)
157 {
158         $line_taxfree_price = $order_line->taxfree_price($tax_group_id);
159         $line_tax = $order_line->full_price() - $line_taxfree_price;
160
161         if ($credit_type == "Return")
162         {
163
164                 /* Insert stock movements for the stock coming back in - with unit cost */
165
166                 $reference = "";
167                 if ($credited_invoice)
168                         $reference .= "Ex Inv: " . $credited_invoice;
169
170                 add_customer_trans_detail_item(11, $credit_no, $order_line->stock_id,
171                         $order_line->item_description, $order->Location, $date_,
172                         $order_line->qty_dispatched, $line_taxfree_price, $line_tax,
173                         $order_line->discount_percent, $reference, $order_line->standard_cost);
174
175         }
176         elseif ($credit_type == "WriteOff")
177         {
178            /*Insert a stock movement coming back in to show the credit note and
179                 a reversing stock movement to show the write off
180                 no mods to location stock records*/
181
182            $reference = "";
183            if ($credited_invoice)
184                         $reference .= "Ex Inv: " . $credited_invoice;
185
186                 add_customer_trans_detail_item_writeoff(11, $credit_no, $order_line->stock_id,
187                         $order_line->item_description, $order->Location, $date_,
188                         $order_line->qty_dispatched, $line_taxfree_price, $line_tax,
189                         $order_line->discount_percent, $reference, $order_line->standard_cost);
190         }
191 }
192
193 //----------------------------------------------------------------------------------------
194
195 function add_gl_trans_credit_costs($order, $order_line, $credit_no, $date_, $tax_group_id,
196         $credit_type, $write_off_gl_code, &$branch_data)
197 {
198         $stock_gl_codes = get_stock_gl_code($order_line->stock_id);
199
200         /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
201         if ($order_line->standard_cost != 0)
202         {
203
204                 /*first the cost of sales entry*/
205
206                 add_gl_trans_std_cost(11, $credit_no, $date_, $stock_gl_codes["cogs_account"],
207                         $stock_gl_codes["dimension_id"], $stock_gl_codes["dimension2_id"], "",
208                         -($order_line->standard_cost * $order_line->qty_dispatched),
209                         payment_person_types::customer(), $order->customer_id,
210                         "The cost of sales GL posting could not be inserted");
211
212                 /*now the stock entry*/
213                 if ($credit_type == "WriteOff")
214                 {
215                         $stock_entry_account = $write_off_gl_code;
216                 }
217                 else
218                 {
219                         $stock_gl_code = get_stock_gl_code($order_line->stock_id);
220                         $stock_entry_account = $stock_gl_code["inventory_account"];
221                 }
222
223                 add_gl_trans_std_cost(11, $credit_no, $date_, $stock_entry_account, 0, 0,
224                         "", ($order_line->standard_cost * $order_line->qty_dispatched),
225                         payment_person_types::customer(), $order->customer_id,
226                         "The stock side (or write off) of the cost of sales GL posting could not be inserted");
227
228         } /* end of if GL and stock integrated and standard cost !=0 */
229
230         if ($order_line->price != 0)
231         {
232
233         $line_taxfree_price = $order_line->taxfree_price($tax_group_id);
234                 $line_tax = $order_line->full_price() - $line_taxfree_price;
235
236                 //Post sales transaction to GL credit sales
237
238                 add_gl_trans_customer(11, $credit_no, $date_, $stock_gl_codes["sales_account"], 0, 0,
239                         ($line_taxfree_price * $order_line->qty_dispatched), $order->customer_id,
240                         "The credit note GL posting could not be inserted");
241
242                 if ($order_line->discount_percent != 0)
243                 {
244
245                         add_gl_trans_customer(11, $credit_no, $date_, $branch_data["sales_discount_account"], 0, 0,
246                                 -($line_taxfree_price * $order_line->qty_dispatched * $order_line->discount_percent),
247                                 $order->customer_id,
248                                 "The credit note discount GL posting could not be inserted");
249
250                 } /*end of if discount !=0 */
251         } /*end of if sales integrated with debtors */
252 }
253
254 //----------------------------------------------------------------------------------------
255
256 function add_g_trans_credit_common($order, $credit_no, $date_,
257         $credit_items_total, $freight, $taxes, $tax_total, &$branch_data)
258 {
259         $company_data = get_company_prefs();
260
261         /*Post credit note transaction to GL credit debtors,
262         debit freight re-charged and debit sales */
263
264         if (($credit_items_total + $freight + $tax_total) != 0)
265         {
266
267                 add_gl_trans_customer(11, $credit_no, $date_, $branch_data["receivables_account"], 0, 0,
268                         -($credit_items_total + $freight + $tax_total), $order->customer_id,
269                         "The total debtor GL posting for the credit note could not be inserted");
270         }
271
272         if ($freight !=0)
273         {
274                 add_gl_trans_customer(11, $credit_no, $date_, $company_data["freight_act"], 0, 0,
275                         $freight, $order->customer_id,
276                         "The freight GL posting for this credit note could not be inserted");
277         }
278
279     foreach ($taxes as $taxitem)
280     {
281         if ($taxitem['Value'] != 0)
282         {
283
284                         add_customer_trans_tax_detail_item(11, $credit_no, $taxitem['tax_type_id'],
285                                 $taxitem['rate'], $taxitem['included_in_price'], $taxitem['Value']);
286
287                 add_gl_trans_customer(11, $credit_no, $date_, $taxitem['sales_gl_code'], 0, 0,
288                         $taxitem['Value'], $order->customer_id,
289                         "A tax GL posting for this credit note could not be inserted");
290         }
291     }
292 }
293
294 //----------------------------------------------------------------------------------------
295
296 ?>