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