Final rewriting of sales module
[fa-stable.git] / sales / includes / db / sales_credit_db.inc
1 <?php
2
3 //----------------------------------------------------------------------------------------
4 // if ($writeoff_acc==0) return goods into $cart->Location
5 // if src_docs!=0 => credit invoice else credit note
6 //
7 function write_credit_note($credit_note, $write_off_acc)
8 {
9         $credit_invoice = count($credit_note->src_docs) ? key($credit_note->src_docs) : 0;
10
11
12         $credit_date = $credit_note->document_date;
13         $tax_group_id = $credit_note->tax_group_id;
14
15         $trans_no = $credit_note->trans_no;
16         if (is_array($trans_no)) {
17                 $trans_no = key($trans_no);
18         }
19
20         $credit_type = $write_off_acc == 0 ? 'Return' : 'Writeoff';
21
22         begin_transaction();
23
24         $company_data = get_company_prefs();
25         $branch_data = get_branch_accounts($credit_note->Branch);
26
27         $credit_note_total = $credit_note->get_items_total_dispatch();
28         $freight_tax = $credit_note->get_shipping_tax();
29
30         $taxes = $credit_note->get_taxes();
31
32         $tax_total = 0;
33         foreach ($taxes as $taxitem) {
34                 $tax_total +=  $taxitem['Value'];
35         }
36
37         if ($credit_note->tax_included == 0) {
38             $items_added_tax = $tax_total-$freight_tax;
39             $freight_added_tax = $freight_tax;
40         } else {
41             $items_added_tax = 0;
42             $freight_added_tax = 0;
43         }
44
45 //      $sales_order=$invoice->order_no;        //?
46 //    if (is_array($sales_order)) $sales_order = $sales_order[0]; //?
47
48         /*Now insert the Credit Note into the debtor_trans table with the allocations as calculated above*/
49         // all amounts in debtor's currency
50         $credit_no = write_customer_trans(11, $trans_no, $credit_note->customer_id,
51                 $credit_note->Branch, $credit_date, $credit_note->reference,
52                 $credit_note_total, 0, $items_added_tax,
53                 $credit_note->freight_cost, $freight_added_tax,
54                 $credit_note->default_sales_type,
55                 $credit_note->order_no, $credit_invoice, $credit_note->ship_via
56                 //, $credit_note->due_date
57                 );
58
59         if ($trans_no==0) {
60                 $credit_note->trans_no = array($credit_no=>0);
61                 set_document_parent($credit_note);
62         } else {
63                 delete_comments(11, $credit_no);
64                 void_cust_allocations(11, $credit_no);
65                 void_gl_trans(11, $credit_no, true);
66                 void_stock_move(11, $credit_no);
67                 void_customer_trans_tax_details(11, $credit_no);
68         }
69
70         if ($credit_invoice) {
71                 $invoice_alloc_balance = get_DebtorTrans_allocation_balance(10, $credit_invoice);
72                 update_customer_trans_version(get_parent_type(11), $credit_note->src_docs );
73                 if ($invoice_alloc_balance > 0) {       /*the invoice is not already fully allocated */
74                         $total = $credit_note_total + $credit_note->freight_cost +
75                             $items_added_tax + $freight_added_tax;
76
77                         $allocate_amount = ($invoice_alloc_balance > $total) ? $total : $invoice_alloc_balance;
78                         /*Now insert the allocation record if > 0 */
79                         if ($allocate_amount != 0) {
80                                 update_debtor_trans_allocation(10, $credit_invoice, $allocate_amount);
81                                 update_debtor_trans_allocation(11, $credit_no, $allocate_amount); // ***
82                                 add_cust_allocation($allocate_amount, 11, $credit_no, 10, $credit_invoice);
83                         }
84                 }
85         }
86
87         foreach ($credit_note->line_items as $credit_line) {
88
89                 if ($credit_invoice && $credit_line->qty_dispatched!=$credit_line->qty_old ) {
90                         update_parent_line(11, $credit_line->id,($credit_line->qty_dispatched
91                                         -$credit_line->qty_old));
92                 }
93
94                 $line_taxfree_price = get_tax_free_price_for_item($credit_line->stock_id, $credit_line->price,
95                   0, $credit_note->tax_included, $credit_note->tax_group_array);
96
97                 $line_tax = get_full_price_for_item($credit_line->stock_id, $credit_line->price,
98                   0, $credit_note->tax_included, $credit_note->tax_group_array) - $line_taxfree_price;
99
100                 write_customer_trans_detail_item(11, $credit_no, $credit_line->stock_id,
101                         $credit_line->item_description, $credit_line->qty_dispatched,
102                         $credit_line->line_price(), $line_tax, $credit_line->discount_percent,
103                         $credit_line->standard_cost, $trans_no==0 ? 0:  $credit_line->id);
104
105                 add_credit_movements_item(&$credit_note, &$credit_line, $credit_date,
106                         $credit_type, $line_taxfree_price+$line_tax, $credit_invoice);
107
108                 add_gl_trans_credit_costs($credit_note, $credit_line, $credit_no,
109                     $credit_date, $credit_type, $write_off_acc, $branch_data);
110         } /*end of credit_line loop */
111
112         /*Post credit note transaction to GL credit debtors,
113         debit freight re-charged and debit sales */
114
115         if (($credit_note_total + $credit_note->freight_cost) != 0) {
116
117                 add_gl_trans_customer(11, $credit_no, $credit_date, $branch_data["receivables_account"], 0, 0,
118                         -($credit_note_total + $credit_note->freight_cost + $items_added_tax + $freight_added_tax),
119                         $credit_note->customer_id,
120                         "The total debtor GL posting for the credit note could not be inserted");
121         }
122
123         if ($credit_note->freight_cost !=0) {
124                 add_gl_trans_customer(11, $credit_no, $credit_date, $company_data["freight_act"], 0, 0,
125                         $credit_note->get_tax_free_shipping(), $credit_note->customer_id,
126                         "The freight GL posting for this credit note could not be inserted");
127         }
128
129         foreach ($taxes as $taxitem) {
130                 if ($taxitem['Value'] != 0) {
131
132                         add_customer_trans_tax_detail_item(11, $credit_no, $taxitem['tax_type_id'],
133                                 $taxitem['rate'], $credit_note->tax_included, $taxitem['Value']);
134
135                         add_gl_trans_customer(11, $credit_no, $date_, $taxitem['sales_gl_code'], 0, 0,
136                                 $taxitem['Value'], $credit_note->customer_id,
137                                 "A tax GL posting for this credit note could not be inserted");
138                 }
139         }
140
141         add_comments(11, $credit_no, $credit_date, $credit_note->Comments);
142
143         if ($trans_no == 0) {
144                 references::save_last($credit_note->reference, 11);
145         }
146
147         commit_transaction();
148
149         return $credit_no;
150 }
151
152 //----------------------------------------------------------------------------------------
153 // Insert a stock movement coming back in to show the credit note and
154 //      a reversing stock movement to show the write off
155 //
156 function add_credit_movements_item(&$credit_note, &$credit_line, $date_,
157         $credit_type, $price, $credited_invoice=0)
158 {
159
160         if ($credit_type == "Return") {
161
162                 $reference = "Return ";
163                 if ($credited_invoice) {
164                         $reference .= "Ex Inv: " . $credited_invoice;
165                 }
166
167         } elseif ($credit_type == "WriteOff") {
168
169                 $reference = "WriteOff ";
170                 if ($credited_invoice)
171                         $reference .= "Ex Inv: " . $credited_invoice;
172
173         add_stock_move_customer(11,  $credit_line->stock_id,
174                 key($credit_note->trans_no), $credit_note->Location,
175                 $credit_note->document_date, $reference, -$credit_line->qty_dispatched,
176                 $credit_line->standard_cost,  0, $price,
177                 $credit_line->discount_percent);
178
179                 }
180         add_stock_move_customer(11,  $credit_line->stock_id,
181                 key($credit_note->trans_no), $credit_note->Location,
182                 $credit_note->document_date, $reference, $credit_line->qty_dispatched,
183                 $credit_line->standard_cost,  0, $price,
184                 $credit_line->discount_percent);
185
186         }
187
188 //----------------------------------------------------------------------------------------
189
190 function add_gl_trans_credit_costs($order, $order_line, $credit_no, $date_,
191         $credit_type, $write_off_gl_code, &$branch_data)
192 {
193         $stock_gl_codes = get_stock_gl_code($order_line->stock_id);
194
195         /* insert gl_trans to credit stock and debit cost of sales at standard cost*/
196         if ($order_line->standard_cost != 0) {
197                 /*first the cost of sales entry*/
198
199                 add_gl_trans_std_cost(11, $credit_no, $date_, $stock_gl_codes["cogs_account"],
200                         $stock_gl_codes["dimension_id"], $stock_gl_codes["dimension2_id"], "",
201                         -($order_line->standard_cost * $order_line->qty_dispatched),
202                         payment_person_types::customer(), $order->customer_id,
203                         "The cost of sales GL posting could not be inserted");
204
205                 /*now the stock entry*/
206                 if ($credit_type == "WriteOff") {
207                         $stock_entry_account = $write_off_gl_code;
208                 } else {
209                         $stock_gl_code = get_stock_gl_code($order_line->stock_id);
210                         $stock_entry_account = $stock_gl_code["inventory_account"];
211                 }
212
213                 add_gl_trans_std_cost(11, $credit_no, $date_, $stock_entry_account, 0, 0,
214                         "", ($order_line->standard_cost * $order_line->qty_dispatched),
215                         payment_person_types::customer(), $order->customer_id,
216                         "The stock side (or write off) of the cost of sales GL posting could not be inserted");
217
218         } /* end of if GL and stock integrated and standard cost !=0 */
219
220         if ($order_line->line_price() != 0) {
221
222                 $line_taxfree_price =
223                 get_tax_free_price_for_item($order_line->stock_id, $order_line->price,
224                   0, $order->tax_included, $order->tax_group_array);
225
226                 $line_tax = get_full_price_for_item($order_line->stock_id, $order_line->price,
227                   0, $order->tax_included, $order->tax_group_array) - $line_taxfree_price;
228
229
230                 //Post sales transaction to GL credit sales
231
232                 add_gl_trans_customer(11, $credit_no, $date_, $stock_gl_codes["sales_account"], 0, 0,
233                         ($line_taxfree_price * $order_line->qty_dispatched), $order->customer_id,
234                         "The credit note GL posting could not be inserted");
235
236                 if ($order_line->discount_percent != 0) {
237
238                         add_gl_trans_customer(11, $credit_no, $date_, $branch_data["sales_discount_account"], 0, 0,
239                                 -($line_taxfree_price * $order_line->qty_dispatched * $order_line->discount_percent),
240                                 $order->customer_id,
241                                 "The credit note discount GL posting could not be inserted");
242
243                 } /*end of if discount !=0 */
244         } /*if line_price!=0 */
245 }
246
247 ?>