Merged changes from main CVS (up to 2.2.10)
[fa-stable.git] / sales / customer_credit_invoice.php
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 //
14 //      Entry/Modify Credit Note for selected Sales Invoice
15 //
16
17 $page_security = 'SA_SALESCREDITINV';
18 $path_to_root = "..";
19
20 include_once($path_to_root . "/sales/includes/cart_class.inc");
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/data_checks.inc");
23 include_once($path_to_root . "/includes/manufacturing.inc");
24 include_once($path_to_root . "/sales/includes/sales_db.inc");
25 include_once($path_to_root . "/sales/includes/sales_ui.inc");
26 include_once($path_to_root . "/reporting/includes/reporting.inc");
27
28 $js = "";
29 if ($use_popup_windows) {
30         $js .= get_js_open_window(900, 500);
31 }
32
33 if ($use_date_picker) {
34         $js .= get_js_date_picker();
35 }
36
37 if (isset($_GET['ModifyCredit'])) {
38         $_SESSION['page_title'] = sprintf(_("Modifying Credit Invoice # %d."), $_GET['ModifyCredit']);
39         $help_context = "Modifying Credit Invoice";
40         processing_start();
41 } elseif (isset($_GET['InvoiceNumber'])) {
42         $_SESSION['page_title'] = _($help_context = "Credit all or part of an Invoice");
43         processing_start();
44 }
45 page($_SESSION['page_title'], false, false, "", $js);
46
47 //-----------------------------------------------------------------------------
48
49 if (isset($_GET['AddedID'])) {
50         $credit_no = $_GET['AddedID'];
51         $trans_type = ST_CUSTCREDIT;
52
53         display_notification_centered(_("Credit Note has been processed"));
54
55         display_note(get_customer_trans_view_str($trans_type, $credit_no, _("&View This Credit Note")), 0, 0);
56
57         display_note(print_document_link($credit_no, _("&Print This Credit Note"), true, $trans_type),1);
58
59         display_note(get_gl_view_str($trans_type, $credit_no, _("View the GL &Journal Entries for this Credit Note")),1);
60
61         display_footer_exit();
62
63 } elseif (isset($_GET['UpdatedID'])) {
64         $credit_no = $_GET['UpdatedID'];
65         $trans_type = ST_CUSTCREDIT;
66
67         display_notification_centered(_("Credit Note has been updated"));
68
69         display_note(get_customer_trans_view_str($trans_type, $credit_no, _("&View This Credit Note")), 0, 0);
70
71         display_note(print_document_link($credit_no, _("&Print This Credit Note"), true, $trans_type),1);
72
73         display_note(get_gl_view_str($trans_type, $credit_no, _("View the GL &Journal Entries for this Credit Note")),1);
74
75         display_footer_exit();
76 } else
77         check_edit_conflicts();
78
79
80 //-----------------------------------------------------------------------------
81
82 function can_process()
83 {
84         global $Refs;
85
86         if (!is_date($_POST['CreditDate'])) {
87                 display_error(_("The entered date is invalid."));;
88                 set_focus('CreditDate');
89                 return false;
90         } elseif (!is_date_in_fiscalyear($_POST['CreditDate'])) {
91                 display_error(_("The entered date is not in fiscal year."));
92                 set_focus('CreditDate');
93                 return false;
94         }
95
96     if ($_SESSION['Items']->trans_no==0) {
97                 if (!$Refs->is_valid($_POST['ref'])) {
98                         display_error(_("You must enter a reference."));;
99                         set_focus('ref');
100                         return false;
101                 }
102
103                 if (!is_new_reference($_POST['ref'], ST_CUSTCREDIT)) {
104                         display_error(_("The entered reference is already in use."));;
105                         set_focus('ref');
106                         return false;
107                 }
108     }
109         if (!check_num('ChargeFreightCost', 0)) {
110                 display_error(_("The entered shipping cost is invalid or less than zero."));;
111                 set_focus('ChargeFreightCost');
112                 return false;
113         }
114         if (!check_quantities()) {
115                 display_error(_("Selected quantity cannot be less than zero nor more than quantity not credited yet."));
116                 return false;
117         }
118         return true;
119 }
120
121 //-----------------------------------------------------------------------------
122
123 if (isset($_GET['InvoiceNumber']) && $_GET['InvoiceNumber'] > 0) {
124
125     $ci = new Cart(ST_SALESINVOICE, $_GET['InvoiceNumber'], true);
126
127     $ci->trans_type = ST_CUSTCREDIT;
128     $ci->src_docs = $ci->trans_no;
129     $ci->src_date = $ci->document_date;
130     $ci->trans_no = 0;
131     $ci->document_date = new_doc_date();
132     $ci->reference = $Refs->get_next(ST_CUSTCREDIT);
133
134     for ($line_no=0; $line_no<count($ci->line_items); $line_no++) {
135         $ci->line_items[$line_no]->qty_dispatched = '0';
136     }
137
138     $_SESSION['Items'] = $ci;
139         copy_from_cart();
140
141 } elseif ( isset($_GET['ModifyCredit']) && $_GET['ModifyCredit']>0) {
142
143         $_SESSION['Items'] = new Cart(ST_CUSTCREDIT,$_GET['ModifyCredit']);
144         copy_from_cart();
145
146 } elseif (!processing_active()) {
147         /* This page can only be called with an invoice number for crediting*/
148         die (_("This page can only be opened if an invoice has been selected for crediting."));
149 } elseif (!check_quantities()) {
150         display_error(_("Selected quantity cannot be less than zero nor more than quantity not credited yet."));
151 }
152
153 function check_quantities()
154 {
155         $ok =1;
156         foreach ($_SESSION['Items']->line_items as $line_no=>$itm) {
157                 if ($itm->quantity == $itm->qty_done) {
158                         continue; // this line was fully credited/removed
159                 }
160                 if (isset($_POST['Line'.$line_no])) {
161                         if (check_num('Line'.$line_no, 0, $itm->quantity)) {
162                                 $_SESSION['Items']->line_items[$line_no]->qty_dispatched =
163                                   input_num('Line'.$line_no);
164                         }
165                 }
166                 else {
167                         $ok = 0;
168                 }
169
170                 if (isset($_POST['Line'.$line_no.'Desc'])) {
171                         $line_desc = $_POST['Line'.$line_no.'Desc'];
172                         if (strlen($line_desc) > 0) {
173                                 $_SESSION['Items']->line_items[$line_no]->item_description = $line_desc;
174                         }
175                 }
176         }
177         return $ok;
178 }
179 //-----------------------------------------------------------------------------
180
181 function copy_to_cart()
182 {
183         $cart = &$_SESSION['Items'];
184         $cart->ship_via = $_POST['ShipperID'];
185         $cart->freight_cost = input_num('ChargeFreightCost');
186         $cart->document_date =  $_POST['CreditDate'];
187         $cart->Location = $_POST['Location'];
188         $cart->Comments = $_POST['CreditText'];
189         if ($_SESSION['Items']->trans_no == 0)
190                 $cart->reference = $_POST['ref'];
191 }
192 //-----------------------------------------------------------------------------
193
194 function copy_from_cart()
195 {
196         $cart = &$_SESSION['Items'];
197         $_POST['ShipperID'] = $cart->ship_via;
198         $_POST['ChargeFreightCost'] = price_format($cart->freight_cost);
199         $_POST['CreditDate']= $cart->document_date;
200         $_POST['Location']= $cart->Location;
201         $_POST['CreditText']= $cart->Comments;
202         $_POST['cart_id'] = $cart->cart_id;
203         $_POST['ref'] = $cart->reference;
204 }
205 //-----------------------------------------------------------------------------
206
207 if (isset($_POST['ProcessCredit']) && can_process()) {
208
209     $new_credit = ($_SESSION['Items']->trans_no == 0);
210
211     if (!isset($_POST['WriteOffGLCode']))
212                 $_POST['WriteOffGLCode'] = 0;
213
214         copy_to_cart();
215         if ($new_credit) new_doc_date($_SESSION['Items']->document_date);
216     $credit_no = $_SESSION['Items']->write($_POST['WriteOffGLCode']);
217
218         processing_end();exit;
219         if ($new_credit) {
220                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
221         } else {
222                 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$credit_no");
223         }
224 }
225
226 //-----------------------------------------------------------------------------
227
228 if (isset($_POST['Location'])) {
229         $_SESSION['Items']->Location = $_POST['Location'];
230 }
231
232 //-----------------------------------------------------------------------------
233
234 function display_credit_items()
235 {
236     start_form();
237         hidden('cart_id');
238
239         start_table(TABLESTYLE2, "width=80%", 5);
240         echo "<tr><td>"; // outer table
241
242     start_table(TABLESTYLE, "width=100%");
243     start_row();
244     label_cells(_("Customer"), $_SESSION['Items']->customer_name, "class='tableheader2'");
245         label_cells(_("Branch"), get_branch_name($_SESSION['Items']->Branch), "class='tableheader2'");
246     label_cells(_("Currency"), $_SESSION['Items']->customer_currency, "class='tableheader2'");
247     end_row();
248     start_row();
249
250 //      if (!isset($_POST['ref']))
251 //              $_POST['ref'] = $Refs->get_next(11);
252
253     if ($_SESSION['Items']->trans_no==0) {
254                 ref_cells(_("Reference"), 'ref', '', null, "class='tableheader2'");
255         } else {
256                 label_cells(_("Reference"), $_SESSION['Items']->reference, "class='tableheader2'");
257         }
258     label_cells(_("Crediting Invoice"), get_customer_trans_view_str(ST_SALESINVOICE, array_keys($_SESSION['Items']->src_docs)), "class='tableheader2'");
259
260         if (!isset($_POST['ShipperID'])) {
261                 $_POST['ShipperID'] = $_SESSION['Items']->ship_via;
262         }
263         label_cell(_("Shipping Company"), "class='tableheader2'");
264         shippers_list_cells(null, 'ShipperID', $_POST['ShipperID']);
265 //      if (!isset($_POST['sales_type_id']))
266 //        $_POST['sales_type_id'] = $_SESSION['Items']->sales_type;
267 //      label_cell(_("Sales Type"), "class='tableheader2'");
268 //      sales_types_list_cells(null, 'sales_type_id', $_POST['sales_type_id']);
269
270         end_row();
271         end_table();
272
273     echo "</td><td>";// outer table
274
275     start_table(TABLESTYLE, "width=100%");
276
277     label_row(_("Invoice Date"), $_SESSION['Items']->src_date, "class='tableheader2'");
278
279     date_row(_("Credit Note Date"), 'CreditDate', '', $_SESSION['Items']->trans_no==0, 0, 0, 0, "class='tableheader2'");
280
281     end_table();
282
283         echo "</td></tr>";
284
285         end_table(1); // outer table
286
287         div_start('credit_items');
288     start_table(TABLESTYLE, "width=80%");
289     $th = array(_("Item Code"), _("Item Description"), _("Invoiced Quantity"), _("Units"),
290         _("Credit Quantity"), _("Price"), _("Discount %"), _("Total"));
291     table_header($th);
292
293     $k = 0; //row colour counter
294
295     foreach ($_SESSION['Items']->line_items as $line_no=>$ln_itm) {
296                 if ($ln_itm->quantity == $ln_itm->qty_done) {
297                         continue; // this line was fully credited/removed
298                 }
299                 alt_table_row_color($k);
300
301
302                 //      view_stock_status_cell($ln_itm->stock_id); alternative view
303         label_cell($ln_itm->stock_id);
304
305                 text_cells(null, 'Line'.$line_no.'Desc', $ln_itm->item_description, 30, 50);
306                 $dec = get_qty_dec($ln_itm->stock_id);
307         qty_cell($ln_itm->quantity, false, $dec);
308         label_cell($ln_itm->units);
309                 amount_cells(null, 'Line'.$line_no, number_format2($ln_itm->qty_dispatched, $dec),
310                         null, null, $dec);
311         $line_total =($ln_itm->qty_dispatched * $ln_itm->price * (1 - $ln_itm->discount_percent));
312
313         amount_cell($ln_itm->price);
314         percent_cell($ln_itm->discount_percent*100);
315         amount_cell($line_total);
316         end_row();
317     }
318
319     if (!check_num('ChargeFreightCost')) {
320         $_POST['ChargeFreightCost'] = price_format($_SESSION['Items']->freight_cost);
321     }
322         $colspan = 7;
323         start_row();
324         label_cell(_("Credit Shipping Cost"), "colspan=$colspan align=right");
325         small_amount_cells(null, "ChargeFreightCost", price_format(get_post('ChargeFreightCost',0)));
326         end_row();
327
328     $inv_items_total = $_SESSION['Items']->get_items_total_dispatch();
329
330     $display_sub_total = price_format($inv_items_total + input_num($_POST['ChargeFreightCost']));
331     label_row(_("Sub-total"), $display_sub_total, "colspan=$colspan align=right", "align=right");
332
333     $taxes = $_SESSION['Items']->get_taxes(input_num($_POST['ChargeFreightCost']));
334
335     $tax_total = display_edit_tax_items($taxes, $colspan, $_SESSION['Items']->tax_included);
336
337     $display_total = price_format(($inv_items_total + input_num('ChargeFreightCost') + $tax_total));
338
339     label_row(_("Credit Note Total"), $display_total, "colspan=$colspan align=right", "align=right");
340
341     end_table();
342         div_end();
343 }
344
345 //-----------------------------------------------------------------------------
346 function display_credit_options()
347 {
348         global $Ajax;
349         br();
350
351 i       f (isset($_POST['_CreditType_update']))
352                 $Ajax->activate('options');
353
354         div_start('options');
355         start_table(TABLESTYLE2);
356
357         credit_type_list_row(_("Credit Note Type"), 'CreditType', null, true);
358
359         if ($_POST['CreditType'] == "Return")
360         {
361
362                 /*if the credit note is a return of goods then need to know which location to receive them into */
363                 if (!isset($_POST['Location']))
364                         $_POST['Location'] = $_SESSION['Items']->Location;
365                 locations_list_row(_("Items Returned to Location"), 'Location', $_POST['Location']);
366         }
367         else
368         {
369                 /* the goods are to be written off to somewhere */
370                 gl_all_accounts_list_row(_("Write off the cost of the items to"), 'WriteOffGLCode', null);
371         }
372
373         textarea_row(_("Memo"), "CreditText", null, 51, 3);
374         echo "</table>";
375  div_end();
376 }
377
378 //-----------------------------------------------------------------------------
379 if (get_post('Update'))
380 {
381         $Ajax->activate('credit_items');
382 }
383 //-----------------------------------------------------------------------------
384
385 display_credit_items();
386 display_credit_options();
387
388 echo "<br><center>";
389 submit('Update', _("Update"), true, _('Update credit value for quantities entered'), true);
390 echo "&nbsp";
391 submit('ProcessCredit', _("Process Credit Note"), true, '', 'default');
392 echo "</center>";
393
394 end_form();
395
396
397 end_page();
398
399 ?>