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'];
42 print_hidden_script(11);
44 display_notification_centered(_("Credit Note has been processed"));
46 display_note(get_customer_trans_view_str($trans_type, $credit_no, _("View This Credit Note")), 0, 0);
48 display_note(print_document_link($credit_no, _("Print This Credit Note"), true, 11),1);
50 display_note(get_gl_view_str($trans_type, $credit_no, _("View the GL Journal Entries for this Credit Note")),1);
52 display_footer_exit();
54 } elseif (isset($_GET['UpdatedID'])) {
55 $credit_no = $_GET['UpdatedID'];
57 print_hidden_script(11);
59 display_notification_centered(_("Credit Note has been updated"));
61 display_note(get_customer_trans_view_str($trans_type, $credit_no, _("View This Credit Note")), 0, 0);
63 display_note(print_document_link($credit_no, _("Print This Credit Note"), true, 11),1);
65 display_note(get_gl_view_str($trans_type, $credit_no, _("View the GL Journal Entries for this Credit Note")),1);
67 display_footer_exit();
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');
106 //-----------------------------------------------------------------------------
108 if (isset($_GET['InvoiceNumber']) && $_GET['InvoiceNumber'] > 0) {
110 $ci = new Cart(10, $_GET['InvoiceNumber'], true);
113 display_error(_("This invoice can not be credited using the automatic facility."));
114 display_error("Please report that a duplicate debtor_trans header record was found for invoice " . key($ci->src_docs));
119 //valid invoice record returned from the entered invoice number
121 $ci->trans_type = 11;
122 $ci->src_docs = $ci->trans_no;
123 $ci->src_date = $ci->document_date;
125 $ci->document_date = Today();
126 $ci->reference = references::get_next(11);
128 for ($line_no=0; $line_no<count($ci->line_items); $line_no++) {
129 $ci->line_items[$line_no]->qty_dispatched = '0';
132 $_SESSION['Items'] = $ci;
135 } elseif ( isset($_GET['ModifyCredit']) && $_GET['ModifyCredit']>0) {
137 $_SESSION['Items'] = new Cart(11,$_GET['ModifyCredit']);
140 } elseif (!processing_active()) {
141 /* This page can only be called with an invoice number for crediting*/
142 die (_("This page can only be opened if an invoice has been selected for crediting."));
144 foreach ($_SESSION['Items']->line_items as $line_no=>$itm) {
145 if (isset($_POST['Line'.$line_no])) {
146 if (check_num('Line'.$line_no, ($itm->quantity - $itm->qty_done))) {
147 $_SESSION['Items']->line_items[$line_no]->qty_dispatched =
148 input_num('Line'.$line_no);
152 if (isset($_POST['Line'.$line_no.'Desc'])) {
153 $line_desc = $_POST['Line'.$line_no.'Desc'];
154 if (strlen($line_desc) > 0) {
155 $_SESSION['Items']->line_items[$line_no]->item_description = $line_desc;
160 //-----------------------------------------------------------------------------
162 function copy_to_cart()
164 $cart = &$_SESSION['Items'];
165 $cart->ship_via = $_POST['ShipperID'];
166 $cart->freight_cost = input_num('ChargeFreightCost');
167 $cart->document_date = $_POST['CreditDate'];
168 $cart->Location = $_POST['Location'];
169 $cart->Comments = $_POST['CreditText'];
171 //-----------------------------------------------------------------------------
173 function copy_from_cart()
175 $cart = &$_SESSION['Items'];
176 $_POST['ShipperID'] = $cart->ship_via;
177 $_POST['ChargeFreightCost'] = price_format($cart->freight_cost);
178 $_POST['CreditDate']= $cart->document_date;
179 $_POST['Location']= $cart->Location;
180 $_POST['CreditText']= $cart->Comments;
182 //-----------------------------------------------------------------------------
184 if (isset($_POST['ProcessCredit']) && can_process()) {
186 $newcredit = ($_SESSION['Items']->trans_no == 0);
188 if (!isset($_POST['WriteOffGLCode']))
189 $_POST['WriteOffGLCode'] = 0;
192 $credit_no = $_SESSION['Items']->write($_POST['WriteOffGLCode']);
196 meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
198 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$credit_no");
202 //-----------------------------------------------------------------------------
204 if (isset($_POST['Location'])) {
205 $_SESSION['Items']->Location = $_POST['Location'];
208 //-----------------------------------------------------------------------------
210 function display_credit_items()
212 global $table_style, $table_style2;
214 start_form(false, true);
216 start_table("$table_style2 width=80%", 5);
217 echo "<tr><td>"; // outer table
219 start_table("$table_style width=100%");
221 label_cells(_("Customer"), $_SESSION['Items']->customer_name, "class='tableheader2'");
222 label_cells(_("Branch"), get_branch_name($_SESSION['Items']->Branch), "class='tableheader2'");
223 label_cells(_("Currency"), $_SESSION['Items']->customer_currency, "class='tableheader2'");
227 // if (!isset($_POST['ref']))
228 // $_POST['ref'] = references::get_next(11);
230 if ($_SESSION['Items']->trans_no==0) {
231 ref_cells(_("Reference"), 'ref', '', $_SESSION['Items']->reference, "class='tableheader2'");
233 label_cells(_("Reference"), $_SESSION['Items']->reference, "class='tableheader2'");
235 // label_cells(_("Crediting Invoice"), get_customer_trans_view_str(10, $_SESSION['InvoiceToCredit']), "class='tableheader2'");
236 label_cells(_("Crediting Invoice"), get_customer_trans_view_str(10, array_keys($_SESSION['Items']->src_docs)), "class='tableheader2'");
238 if (!isset($_POST['ShipperID'])) {
239 $_POST['ShipperID'] = $_SESSION['Items']->ship_via;
241 label_cell(_("Shipping Company"), "class='tableheader2'");
242 shippers_list_cells(null, 'ShipperID', $_POST['ShipperID']);
243 // if (!isset($_POST['sales_type_id']))
244 // $_POST['sales_type_id'] = $_SESSION['Items']->sales_type;
245 // label_cell(_("Sales Type"), "class='tableheader2'");
246 // sales_types_list_cells(null, 'sales_type_id', $_POST['sales_type_id']);
251 echo "</td><td>";// outer table
253 start_table("$table_style width=100%");
255 label_row(_("Invoice Date"), $_SESSION['Items']->src_date, "class='tableheader2'");
257 date_row(_("Credit Note Date"), 'CreditDate', '', null, 0, 0, 0, "class='tableheader2'");
263 end_table(1); // outer table
265 start_table("$table_style width=80%");
266 $th = array(_("Item Code"), _("Item Description"), _("Invoiced Quantity"), _("Units"),
267 _("Credit Quantity"), _("Price"), _("Discount %"), _("Total"));
270 $k = 0; //row colour counter
272 foreach ($_SESSION['Items']->line_items as $line_no=>$ln_itm) {
273 if ($ln_itm->quantity==$ln_itm->qty_done) {
274 continue; // this line was fully credited
276 alt_table_row_color($k);
279 // view_stock_status_cell($ln_itm->stock_id); alternative view
280 label_cell($ln_itm->stock_id);
282 text_cells(null, 'Line'.$line_no.'Desc', $ln_itm->item_description, 30, 50);
283 $dec = get_qty_dec($ln_itm->stock_id);
284 qty_cell($ln_itm->quantity, false, $dec);
285 label_cell($ln_itm->units);
287 amount_cells(null, 'Line'.$line_no, number_format2($ln_itm->qty_dispatched, $dec));
289 $line_total =($ln_itm->qty_dispatched * $ln_itm->price * (1 - $ln_itm->discount_percent));
291 amount_cell($ln_itm->price);
292 percent_cell($ln_itm->discount_percent*100);
293 amount_cell($line_total);
297 if (!check_num('ChargeFreightCost')) {
298 $_POST['ChargeFreightCost'] = price_format($_SESSION['Items']->freight_cost);
302 label_cell(_("Credit Shipping Cost"), "colspan=7 align=right");
303 amount_cells(null, "ChargeFreightCost", $_POST['ChargeFreightCost'], 6, 6);
306 $inv_items_total = $_SESSION['Items']->get_items_total_dispatch();
308 $display_sub_total = price_format($inv_items_total + input_num($_POST['ChargeFreightCost']));
309 label_row(_("Sub-total"), $display_sub_total, "colspan=7 align=right", "align=right");
311 $taxes = $_SESSION['Items']->get_taxes(input_num($_POST['ChargeFreightCost']));
313 $tax_total = display_edit_tax_items($taxes, 7, $_SESSION['Items']->tax_included);
315 $display_total = price_format(($inv_items_total + input_num('ChargeFreightCost') + $tax_total));
317 label_row(_("Credit Note Total"), $display_total, "colspan=7 align=right", "align=right");
322 //-----------------------------------------------------------------------------
324 function display_credit_options()
326 global $table_style2;
329 start_table($table_style2);
331 echo "<tr><td>" . _("Credit Note Type") . "</td>";
332 echo "<td><select name='CreditType' onchange='this.form.submit();'>";
333 if (!isset($_POST['CreditType']) || $_POST['CreditType'] == "Return") {
334 echo "<option value='WriteOff'>" . _("Items Written Off") . "</option>";
335 echo "<option selected value='Return'>" . _("Items Returned to Inventory Location") . "</option>";
337 echo "<option selected value='WriteOff'>" . _("Items Written Off") . "</option>";
338 echo "<option value='Return'>" . _("Items Returned to Inventory Location") . "</option>";
343 if (!isset($_POST['CreditType']) || $_POST['CreditType'] == "Return") {
345 /*if the credit note is a return of goods then need to know which location to receive them into */
346 if (!isset($_POST['Location'])) {
347 $_POST['Location'] = $_SESSION['Items']->Location;
350 locations_list_row(_("Items Returned to Inventory Location"), 'Location', $_POST['Location']);
351 } else { /* the goods are to be written off to somewhere */
352 gl_all_accounts_list_row(_("Write Off the Cost of the Items to"), 'WriteOffGLCode', $_POST['WriteOffGLCode']);
354 textarea_row(_("Memo"), "CreditText", null, 45, 3);
358 //-----------------------------------------------------------------------------
360 display_credit_items();
361 display_credit_options();
364 submit('Update', _("Update"));
366 submit('ProcessCredit', _("Process Credit Note"));