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