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