Final rewriting of sales module
[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                 return false;
77         } elseif (!is_date_in_fiscalyear($_POST['CreditDate'])) {
78                 display_error(_("The entered date is not in fiscal year."));
79                 return false;
80         }
81
82     if ($_SESSION['Items']->trans_no==0) {
83                 if (!references::is_valid($_POST['ref'])) {
84                         display_error(_("You must enter a reference."));;
85                         return false;
86                 }
87
88                 if (!is_new_reference($_POST['ref'], 11)) {
89                         display_error(_("The entered reference is already in use."));;
90                         return false;
91                 }
92     }
93         if (!is_numeric($_POST['ChargeFreightCost']) || $_POST['ChargeFreightCost'] < 0) {
94                 display_error(_("The entered shipping cost is invalid or less than zero."));;
95                 return false;
96         }
97
98         return true;
99 }
100
101 //-----------------------------------------------------------------------------
102
103 if (isset($_GET['InvoiceNumber']) && $_GET['InvoiceNumber'] > 0) {
104
105     $ci = new Cart(10, $_GET['InvoiceNumber'], true);
106
107     if ($ci==null) {
108                 display_error(_("This invoice can not be credited using the automatic facility."));
109                 display_error("Please report that a duplicate debtor_trans header record was found for invoice " . key($ci->src_docs));
110                 echo "<br><br>";
111                 processing_end();
112                 exit;
113     }
114     //valid invoice record returned from the entered invoice number
115
116     $ci->trans_type = 11;
117     $ci->src_docs = $ci->trans_no;
118     $ci->src_date = $ci->document_date;
119     $ci->trans_no = 0;
120     $ci->document_date = Today();
121     $ci->reference = references::get_next(11);
122
123     for ($line_no=0; $line_no<count($ci->line_items); $line_no++) {
124         $ci->line_items[$line_no]->qty_dispatched = '0';
125     }
126
127     $_SESSION['Items'] = $ci;
128         copy_from_cart();
129
130 } elseif ( isset($_GET['ModifyCredit']) && $_GET['ModifyCredit']>0) {
131
132         $_SESSION['Items'] = new Cart(11,$_GET['ModifyCredit']);
133         copy_from_cart();
134
135 } elseif (!processing_active()) {
136         /* This page can only be called with an invoice number for crediting*/
137         die (_("This page can only be opened if an invoice has been selected for crediting."));
138 } else {
139         foreach ($_SESSION['Items']->line_items as $line_no=>$itm) {
140                 if (isset($_POST['Line'.$line_no])) {
141                         $line_qty = $_POST['Line'.$line_no];
142                         if (is_numeric($line_qty) && $_POST['Line'.$line_no] <= ($itm->quantity - $itm->qty_done)) {
143                                 $_SESSION['Items']->line_items[$line_no]->qty_dispatched = $line_qty;
144                         }
145                 }
146
147                 if (isset($_POST['Line'.$line_no.'Desc'])) {
148                         $line_desc = $_POST['Line'.$line_no.'Desc'];
149                         if (strlen($line_desc) > 0) {
150                                 $_SESSION['Items']->line_items[$line_no]->item_description = $line_desc;
151                         }
152                 }
153         }
154 }
155 //-----------------------------------------------------------------------------
156
157 function copy_to_cart()
158 {
159   $cart = &$_SESSION['Items'];
160   $cart->ship_via = $_POST['ShipperID'];
161   $cart->freight_cost = $_POST['ChargeFreightCost'];
162   $cart->document_date =  $_POST['CreditDate'];
163   $cart->Location = $_POST['Location'];
164   $cart->Comments = $_POST['CreditText'];
165 }
166 //-----------------------------------------------------------------------------
167
168 function copy_from_cart()
169 {
170   $cart = &$_SESSION['Items'];
171   $_POST['ShipperID'] = $cart->ship_via;
172   $_POST['ChargeFreightCost'] = $cart->freight_cost;
173   $_POST['CreditDate']= $cart->document_date;
174   $_POST['Location']= $cart->Location;
175   $_POST['CreditText']= $cart->Comments;
176 }
177 //-----------------------------------------------------------------------------
178
179 if (isset($_POST['ProcessCredit']) && can_process()) {
180
181     $newcredit = ($_SESSION['Items']->trans_no == 0);
182
183     if (!isset($_POST['WriteOffGLCode']))
184                 $_POST['WriteOffGLCode'] = 0;
185
186         copy_to_cart();
187     $credit_no = $_SESSION['Items']->write($_POST['WriteOffGLCode']);
188
189         processing_end();
190         if ($newcredit) {
191                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
192         } else {
193                 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$credit_no");
194         }
195 }
196
197 //-----------------------------------------------------------------------------
198
199 if (isset($_POST['Location'])) {
200         $_SESSION['Items']->Location = $_POST['Location'];
201 }
202
203 //-----------------------------------------------------------------------------
204
205 function display_credit_items()
206 {
207         global $table_style, $table_style2;
208
209     start_form(false, true);
210
211         start_table("$table_style2 width=80%", 5);
212         echo "<tr><td>"; // outer table
213
214     start_table("$table_style width=100%");
215     start_row();
216     label_cells(_("Customer"), $_SESSION['Items']->customer_name, "class='tableheader2'");
217         label_cells(_("Branch"), get_branch_name($_SESSION['Items']->Branch), "class='tableheader2'");
218     label_cells(_("Currency"), $_SESSION['Items']->customer_currency, "class='tableheader2'");
219     end_row();
220     start_row();
221
222 //      if (!isset($_POST['ref']))
223 //              $_POST['ref'] = references::get_next(11);
224
225     if ($_SESSION['Items']->trans_no==0) {
226                 ref_cells(_("Reference"), 'ref', $_SESSION['Items']->reference, "class='tableheader2'");
227         } else {
228                 label_cells(_("Reference"), $_SESSION['Items']->reference, "class='tableheader2'");
229         }
230 //    label_cells(_("Crediting Invoice"), get_customer_trans_view_str(10, $_SESSION['InvoiceToCredit']), "class='tableheader2'");
231     label_cells(_("Crediting Invoice"), get_customer_trans_view_str(10, array_keys($_SESSION['Items']->src_docs)), "class='tableheader2'");
232
233         if (!isset($_POST['ShipperID'])) {
234                 $_POST['ShipperID'] = $_SESSION['Items']->ship_via;
235         }
236         label_cell(_("Shipping Company"), "class='tableheader2'");
237         shippers_list_cells(null, 'ShipperID', $_POST['ShipperID']);
238 //      if (!isset($_POST['sales_type_id']))
239 //        $_POST['sales_type_id'] = $_SESSION['Items']->default_sales_type;
240 //      label_cell(_("Sales Type"), "class='tableheader2'");
241 //      sales_types_list_cells(null, 'sales_type_id', $_POST['sales_type_id']);
242
243         end_row();
244         end_table();
245
246     echo "</td><td>";// outer table
247
248     start_table("$table_style width=100%");
249
250     label_row(_("Invoice Date"), $_SESSION['Items']->src_date, "class='tableheader2'");
251
252     date_row(_("Credit Note Date"), 'CreditDate', null, 0, 0, 0, "class='tableheader2'");
253
254     end_table();
255
256         echo "</td></tr>";
257
258         end_table(1); // outer table
259
260     start_table("$table_style width=80%");
261     $th = array(_("Item Code"), _("Item Description"), _("Invoiced Quantity"), _("Units"),
262         _("Credit Quantity"), _("Price"), _("Discount %"), _("Total"));
263     table_header($th);
264
265     $k = 0; //row colour counter
266
267     foreach ($_SESSION['Items']->line_items as $line_no=>$ln_itm) {
268                 if ($ln_itm->quantity==$ln_itm->qty_done) {
269                         continue; // this line was fully credited
270                 }
271                 alt_table_row_color($k);
272
273
274                 //      view_stock_status_cell($ln_itm->stock_id); alternative view
275         label_cell($ln_itm->stock_id);
276
277                 text_cells(null, 'Line'.$line_no.'Desc', $ln_itm->item_description, 30, 50);
278
279         qty_cell($ln_itm->quantity);
280         label_cell($ln_itm->units);
281             text_cells(null, 'Line'.$line_no, $ln_itm->qty_dispatched, 13, 15);
282
283         $line_total =($ln_itm->qty_dispatched * $ln_itm->price * (1 - $ln_itm->discount_percent));
284
285         amount_cell($ln_itm->price);
286         amount_cell($ln_itm->discount_percent*100);
287         amount_cell($line_total);
288         end_row();
289     }
290
291     if (!isset($_POST['ChargeFreightCost']) || ($_POST['ChargeFreightCost'] == "")) {
292         $_POST['ChargeFreightCost'] = $_SESSION['Items']->freight_cost;
293     }
294
295     if (!is_numeric($_POST['ChargeFreightCost']))
296     {
297         $_POST['ChargeFreightCost'] = 0;
298     }
299         start_row();
300         label_cell(_("Credit Shipping Cost"), "colspan=7 align=right");
301     text_cells(null, "ChargeFreightCost", $_POST['ChargeFreightCost'], 6, 6);
302         end_row();
303
304     $inv_items_total = $_SESSION['Items']->get_items_total_dispatch();
305
306     $display_sub_total = number_format2($inv_items_total + $_POST['ChargeFreightCost'],user_price_dec());
307     label_row(_("Sub-total"), $display_sub_total, "colspan=7 align=right", "align=right");
308
309     $taxes = $_SESSION['Items']->get_taxes($_POST['ChargeFreightCost']);
310
311     $tax_total = display_edit_tax_items($taxes, 7, $_SESSION['Items']->tax_included);
312
313     $display_total = number_format2(($inv_items_total + $_POST['ChargeFreightCost'] + $tax_total), user_price_dec());
314
315     label_row(_("Credit Note Total"), $display_total, "colspan=7 align=right", "align=right");
316
317     end_table();
318 }
319
320 //-----------------------------------------------------------------------------
321
322 function display_credit_options()
323 {
324         global $table_style2;
325
326     echo "<br>";
327     start_table($table_style2);
328
329     echo "<tr><td>" . _("Credit Note Type") . "</td>";
330     echo "<td><select name='CreditType' onchange='this.form.submit();'>";
331     if (!isset($_POST['CreditType']) || $_POST['CreditType'] == "Return") {
332         echo "<option value='WriteOff'>" . _("Items Written Off") . "</option>";
333         echo "<option selected value='Return'>" . _("Items Returned to Inventory Location") . "</option>";
334     } else {
335         echo "<option selected value='WriteOff'>" . _("Items Written Off") . "</option>";
336         echo "<option value='Return'>" . _("Items Returned to Inventory Location") . "</option>";
337     }
338     echo "</select>";
339     echo"</td></tr>";
340
341     if (!isset($_POST['CreditType']) || $_POST['CreditType'] == "Return") {
342
343         /*if the credit note is a return of goods then need to know which location to receive them into */
344         if (!isset($_POST['Location'])) {
345                 $_POST['Location'] = $_SESSION['Items']->Location;
346         }
347
348         locations_list_row(_("Items Returned to Inventory Location"), 'Location', $_POST['Location']);
349     } else {    /* the goods are to be written off to somewhere */
350         gl_all_accounts_list_row(_("Write Off the Cost of the Items to"), 'WriteOffGLCode', $_POST['WriteOffGLCode']);
351     }
352     textarea_row(_("Memo"), "CreditText", null, 45, 3);
353     end_table();
354 }
355
356 //-----------------------------------------------------------------------------
357
358 display_credit_items();
359 display_credit_options();
360
361 echo "<br><center>";
362 submit('Update', _("Update"));
363 echo "&nbsp";
364 submit('ProcessCredit', _("Process Credit Note"));
365 echo "</center>";
366
367 end_form();
368
369
370 end_page();
371
372 ?>