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