2 //---------------------------------------------------------------------------
4 // Entry/Modify Credit Note for selected Sales Invoice
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");
19 if ($use_popup_windows) {
20 $js .= get_js_open_window(900, 500);
23 if ($use_date_picker) {
24 $js .= get_js_date_picker();
27 if (isset($_GET['ModifyCredit'])) {
28 $_SESSION['page_title'] = sprintf(_("Modifying Credit Invoice # %d."), $_GET['ModifyCredit']);
29 $help_page_title =_("Modifying Credit Invoice");
31 } elseif (isset($_GET['InvoiceNumber'])) {
32 $_SESSION['page_title'] = _("Credit all or part of an Invoice");
35 page($_SESSION['page_title'], false, false, "", $js);
37 //-----------------------------------------------------------------------------
39 if (isset($_GET['AddedID'])) {
40 $credit_no = $_GET['AddedID'];
43 display_notification_centered(_("Credit Note has been processed"));
45 display_note(get_customer_trans_view_str($trans_type, $credit_no, _("&View This Credit Note")), 0, 0);
47 display_note(print_document_link($credit_no, _("&Print This Credit Note"), true, 11),1);
49 display_note(get_gl_view_str($trans_type, $credit_no, _("View the GL &Journal Entries for this Credit Note")),1);
51 display_footer_exit();
53 } elseif (isset($_GET['UpdatedID'])) {
54 $credit_no = $_GET['UpdatedID'];
57 display_notification_centered(_("Credit Note has been updated"));
59 display_note(get_customer_trans_view_str($trans_type, $credit_no, _("&View This Credit Note")), 0, 0);
61 display_note(print_document_link($credit_no, _("&Print This Credit Note"), true, 11),1);
63 display_note(get_gl_view_str($trans_type, $credit_no, _("View the GL &Journal Entries for this Credit Note")),1);
65 display_footer_exit();
67 check_edit_conflicts();
70 //-----------------------------------------------------------------------------
72 function can_process()
74 if (!is_date($_POST['CreditDate'])) {
75 display_error(_("The entered date is invalid."));;
76 set_focus('CreditDate');
78 } elseif (!is_date_in_fiscalyear($_POST['CreditDate'])) {
79 display_error(_("The entered date is not in fiscal year."));
80 set_focus('CreditDate');
84 if ($_SESSION['Items']->trans_no==0) {
85 if (!references::is_valid($_POST['ref'])) {
86 display_error(_("You must enter a reference."));;
91 if (!is_new_reference($_POST['ref'], 11)) {
92 display_error(_("The entered reference is already in use."));;
97 if (!check_num('ChargeFreightCost', 0)) {
98 display_error(_("The entered shipping cost is invalid or less than zero."));;
99 set_focus('ChargeFreightCost');
102 if (!check_quantities()) {
103 display_error(_("Selected quantity cannot be less than zero nor more than quantity not credited yet."));
109 //-----------------------------------------------------------------------------
111 if (isset($_GET['InvoiceNumber']) && $_GET['InvoiceNumber'] > 0) {
113 $ci = new Cart(10, $_GET['InvoiceNumber'], true);
115 $ci->trans_type = 11;
116 $ci->src_docs = $ci->trans_no;
117 $ci->src_date = $ci->document_date;
119 $ci->document_date = Today();
120 $ci->reference = references::get_next(11);
122 for ($line_no=0; $line_no<count($ci->line_items); $line_no++) {
123 $ci->line_items[$line_no]->qty_dispatched = '0';
126 $_SESSION['Items'] = $ci;
129 } elseif ( isset($_GET['ModifyCredit']) && $_GET['ModifyCredit']>0) {
131 $_SESSION['Items'] = new Cart(11,$_GET['ModifyCredit']);
134 } elseif (!processing_active()) {
135 /* This page can only be called with an invoice number for crediting*/
136 die (_("This page can only be opened if an invoice has been selected for crediting."));
137 } elseif (!check_quantities()) {
138 display_error(_("Selected quantity cannot be less than zero nor more than quantity not credited yet."));
141 function check_quantities()
144 foreach ($_SESSION['Items']->line_items as $line_no=>$itm) {
145 if (isset($_POST['Line'.$line_no])) {
146 if (check_num('Line'.$line_no, 0, $itm->quantity)) {
147 $_SESSION['Items']->line_items[$line_no]->qty_dispatched =
148 input_num('Line'.$line_no);
155 if (isset($_POST['Line'.$line_no.'Desc'])) {
156 $line_desc = $_POST['Line'.$line_no.'Desc'];
157 if (strlen($line_desc) > 0) {
158 $_SESSION['Items']->line_items[$line_no]->item_description = $line_desc;
164 //-----------------------------------------------------------------------------
166 function copy_to_cart()
168 $cart = &$_SESSION['Items'];
169 $cart->ship_via = $_POST['ShipperID'];
170 $cart->freight_cost = input_num('ChargeFreightCost');
171 $cart->document_date = $_POST['CreditDate'];
172 $cart->Location = $_POST['Location'];
173 $cart->Comments = $_POST['CreditText'];
175 //-----------------------------------------------------------------------------
177 function copy_from_cart()
179 $cart = &$_SESSION['Items'];
180 $_POST['ShipperID'] = $cart->ship_via;
181 $_POST['ChargeFreightCost'] = price_format($cart->freight_cost);
182 $_POST['CreditDate']= $cart->document_date;
183 $_POST['Location']= $cart->Location;
184 $_POST['CreditText']= $cart->Comments;
185 $_POST['cart_id'] = $cart->cart_id;
187 //-----------------------------------------------------------------------------
189 if (isset($_POST['ProcessCredit']) && can_process()) {
191 $newcredit = ($_SESSION['Items']->trans_no == 0);
193 if (!isset($_POST['WriteOffGLCode']))
194 $_POST['WriteOffGLCode'] = 0;
197 $credit_no = $_SESSION['Items']->write($_POST['WriteOffGLCode']);
201 meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
203 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$credit_no");
207 //-----------------------------------------------------------------------------
209 if (isset($_POST['Location'])) {
210 $_SESSION['Items']->Location = $_POST['Location'];
213 //-----------------------------------------------------------------------------
215 function display_credit_items()
217 global $table_style, $table_style2;
219 start_form(false, true);
222 start_table("$table_style2 width=80%", 5);
223 echo "<tr><td>"; // outer table
225 start_table("$table_style width=100%");
227 label_cells(_("Customer"), $_SESSION['Items']->customer_name, "class='tableheader2'");
228 label_cells(_("Branch"), get_branch_name($_SESSION['Items']->Branch), "class='tableheader2'");
229 label_cells(_("Currency"), $_SESSION['Items']->customer_currency, "class='tableheader2'");
233 // if (!isset($_POST['ref']))
234 // $_POST['ref'] = references::get_next(11);
236 if ($_SESSION['Items']->trans_no==0) {
237 ref_cells(_("Reference"), 'ref', '', $_SESSION['Items']->reference, "class='tableheader2'");
239 label_cells(_("Reference"), $_SESSION['Items']->reference, "class='tableheader2'");
241 // label_cells(_("Crediting Invoice"), get_customer_trans_view_str(10, $_SESSION['InvoiceToCredit']), "class='tableheader2'");
242 label_cells(_("Crediting Invoice"), get_customer_trans_view_str(10, array_keys($_SESSION['Items']->src_docs)), "class='tableheader2'");
244 if (!isset($_POST['ShipperID'])) {
245 $_POST['ShipperID'] = $_SESSION['Items']->ship_via;
247 label_cell(_("Shipping Company"), "class='tableheader2'");
248 shippers_list_cells(null, 'ShipperID', $_POST['ShipperID']);
249 // if (!isset($_POST['sales_type_id']))
250 // $_POST['sales_type_id'] = $_SESSION['Items']->sales_type;
251 // label_cell(_("Sales Type"), "class='tableheader2'");
252 // sales_types_list_cells(null, 'sales_type_id', $_POST['sales_type_id']);
257 echo "</td><td>";// outer table
259 start_table("$table_style width=100%");
261 label_row(_("Invoice Date"), $_SESSION['Items']->src_date, "class='tableheader2'");
263 date_row(_("Credit Note Date"), 'CreditDate', '', null, 0, 0, 0, "class='tableheader2'");
269 end_table(1); // outer table
271 div_start('credit_items');
272 start_table("$table_style width=80%");
273 $th = array(_("Item Code"), _("Item Description"), _("Invoiced Quantity"), _("Units"),
274 _("Credit Quantity"), _("Price"), _("Discount %"), _("Total"));
277 $k = 0; //row colour counter
279 foreach ($_SESSION['Items']->line_items as $line_no=>$ln_itm) {
280 if ($ln_itm->quantity==$ln_itm->qty_done) {
281 continue; // this line was fully credited
283 alt_table_row_color($k);
286 // view_stock_status_cell($ln_itm->stock_id); alternative view
287 label_cell($ln_itm->stock_id);
289 text_cells(null, 'Line'.$line_no.'Desc', $ln_itm->item_description, 30, 50);
290 $dec = get_qty_dec($ln_itm->stock_id);
291 qty_cell($ln_itm->quantity, false, $dec);
292 label_cell($ln_itm->units);
293 amount_cells(null, 'Line'.$line_no, number_format2($ln_itm->qty_dispatched, $dec),
295 $line_total =($ln_itm->qty_dispatched * $ln_itm->price * (1 - $ln_itm->discount_percent));
297 amount_cell($ln_itm->price);
298 percent_cell($ln_itm->discount_percent*100);
299 amount_cell($line_total);
303 if (!check_num('ChargeFreightCost')) {
304 $_POST['ChargeFreightCost'] = price_format($_SESSION['Items']->freight_cost);
308 label_cell(_("Credit Shipping Cost"), "colspan=7 align=right");
309 amount_cells_ex(null, "ChargeFreightCost", 6, 8, $_POST['ChargeFreightCost']);
312 $inv_items_total = $_SESSION['Items']->get_items_total_dispatch();
314 $display_sub_total = price_format($inv_items_total + input_num($_POST['ChargeFreightCost']));
315 label_row(_("Sub-total"), $display_sub_total, "colspan=7 align=right", "align=right");
317 $taxes = $_SESSION['Items']->get_taxes(input_num($_POST['ChargeFreightCost']));
319 $tax_total = display_edit_tax_items($taxes, 7, $_SESSION['Items']->tax_included);
321 $display_total = price_format(($inv_items_total + input_num('ChargeFreightCost') + $tax_total));
323 label_row(_("Credit Note Total"), $display_total, "colspan=7 align=right", "align=right");
329 //-----------------------------------------------------------------------------
330 function display_credit_options()
332 global $table_style2, $Ajax;
335 if (isset($_POST['_CreditType_update']))
336 $Ajax->activate('options');
338 div_start('options');
339 start_table("$table_style2");
341 credit_type_list_row(_("Credit Note Type"), 'CreditType', null, true);
343 if ($_POST['CreditType'] == "Return")
346 /*if the credit note is a return of goods then need to know which location to receive them into */
347 if (!isset($_POST['Location']))
348 $_POST['Location'] = $_SESSION['Items']->Location;
349 locations_list_row(_("Items Returned to Location"), 'Location', $_POST['Location']);
353 /* the goods are to be written off to somewhere */
354 gl_all_accounts_list_row(_("Write off the cost of the items to"), 'WriteOffGLCode', null);
357 textarea_row(_("Memo"), "CreditText", null, 51, 3);
362 //-----------------------------------------------------------------------------
363 if (get_post('Update'))
365 $Ajax->activate('credit_items');
367 //-----------------------------------------------------------------------------
369 display_credit_items();
370 display_credit_options();
373 submit('Update', _("Update"), true, _('Update credit value for quantities entered'), true);
375 submit('ProcessCredit', _("Process Credit Note"), true, '', true);