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