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