3 /*The credit selection screen uses the Cart class used for the making up orders
4 some of the variable names refer to order - please think credit when you read order */
8 include_once($path_to_root . "/sales/includes/cart_class.inc");
10 include_once($path_to_root . "/includes/session.inc");
12 include_once($path_to_root . "/includes/data_checks.inc");
14 include_once($path_to_root . "/sales/includes/sales_db.inc");
16 include_once($path_to_root . "/sales/includes/sales_ui.inc");
18 include_once($path_to_root . "/sales/includes/ui/sales_credit_ui.inc");
19 include_once($path_to_root . "/sales/includes/ui/sales_order_ui.inc");
22 if ($use_popup_windows)
23 $js .= get_js_open_window(900, 500);
25 $js .= get_js_date_picker();
26 page(_("Customer Credit Note"), false, false, "", $js);
28 //-----------------------------------------------------------------------------------------------
30 check_db_has_stock_items(_("There are no items defined in the system."));
32 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
34 //---------------------------------------------------------------------------------------------------------------
36 if (isset($_GET['AddedID']))
38 $credit_no = $_GET['AddedID'];
41 display_notification_centered(_("Credit Note has been processed"));
42 display_note(get_customer_trans_view_str($trans_type, $credit_no, _("View this credit note")), 0, 1);
44 display_note(get_gl_view_str($trans_type, $credit_no, _("View the GL Journal Entries for this Credit Note")));
46 hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Credit Note"), "NewCredit=yes");
48 display_footer_exit();
51 //--------------------------------------------------------------------------------------------------
55 $_SESSION['credit_items']->memo_ = $_POST['CreditText'];
57 $_SESSION['credit_items']->orig_order_date = $_POST['OrderDate'];
58 $_SESSION['credit_items']->freight_cost = $_POST['ChargeFreightCost'];
60 $_SESSION['credit_items']->Location = $_POST["Location"];
62 $_SESSION['credit_items']->default_sales_type = $_POST['sales_type_id'];
63 $_SESSION['credit_items']->tax_group_id = $_POST["tax_group_id"];
66 //--------------------------------------------------------------------------------------------------
68 function copy_from_cn()
70 $_POST['CreditText'] = $_SESSION['credit_items']->memo_;
72 $_POST['OrderDate'] = $_SESSION['credit_items']->orig_order_date;
73 $_POST['ChargeFreightCost'] = $_SESSION['credit_items']->freight_cost;
75 $_POST["Location"] = $_SESSION['credit_items']->Location;
77 $_POST['sales_type_id'] = $_SESSION['credit_items']->default_sales_type;
78 $_POST["tax_group_id"] = $_SESSION['credit_items']->tax_group_id;
81 //-----------------------------------------------------------------------------------------------
83 function handle_new_order()
85 if (isset($_SESSION['credit_items']))
87 unset ($_SESSION['credit_items']->line_items);
88 unset ($_SESSION['credit_items']);
91 session_register("credit_items");
93 $_SESSION['credit_items'] = new cart;
94 $_POST['OrderDate'] = Today();
95 if (!is_date_in_fiscalyear($_POST['OrderDate']))
96 $_POST['OrderDate'] = end_fiscalyear();
97 $_SESSION['credit_items']->orig_order_date = $_POST['OrderDate'];
101 //-----------------------------------------------------------------------------------------------
103 if (isset($_POST['ProcessCredit']))
108 if (!references::is_valid($_POST['ref']))
110 display_error( _("You must enter a reference."));
113 elseif (!is_new_reference($_POST['ref'], 11))
115 display_error( _("The entered reference is already in use."));
118 elseif (!is_date($_POST['OrderDate']))
120 display_error(_("The entered date for the credit note is invalid."));
123 elseif (!is_date_in_fiscalyear($_POST['OrderDate']))
125 display_error(_("The entered date is not in fiscal year."));
128 if ($input_error == 1)
129 unset($_POST['ProcessCredit']);
132 //------------------------------------------------------------------------------------
134 if (isset($_POST['ProcessCredit']))
136 //alert("WriteOffGLCode = ".$_POST['WriteOffGLCode'].", CreditType = ".$_POST['CreditType']);
137 if ($_POST['CreditType'] == "WriteOff" && (!isset($_POST['WriteOffGLCode']) ||
138 $_POST['WriteOffGLCode'] == ''))
140 display_note(_("For credit notes created to write off the stock, a general ledger account is required to be selected."), 1, 0);
141 display_note(_("Please select an account to write the cost of the stock off to, then click on Process again."), 1, 0);
144 if (!isset($_POST['WriteOffGLCode']))
145 $_POST['WriteOffGLCode'] = "";
146 if (!isset($_POST['ShipperID']))
147 $_POST['ShipperID'] = 0;
148 $credit_no = add_credit_note($_SESSION['credit_items'], $_POST['OrderDate'],
149 $_POST['CreditType'], $_POST['tax_group_id'],
150 $_POST['ChargeFreightCost'], $_POST['sales_type_id'], $_POST['ShipperID'],
151 $_POST['ref'], $_POST['CreditText'], $_POST['WriteOffGLCode']);
153 unset($_SESSION['credit_items']->line_items);
154 unset($_SESSION['credit_items']);
156 meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
158 } /*end of process credit note */
160 //---------------------------------------------------------------------------------------------------
162 function get_details_from_customer()
164 return get_customer_details_to_order($_SESSION['credit_items'], $_POST['customer_id'], $_POST['branch_id']);
167 //-----------------------------------------------------------------------------------------------
169 function check_item_data()
171 if ($_POST['qty'] <= 0)
173 display_error(_("The quantity must be greater than zero."));
176 if (!is_numeric($_POST['price']) || $_POST['price'] < 0)
178 display_error(_("The entered price is negative or invalid."));
181 if (!is_numeric($_POST['Disc']) || $_POST['Disc'] > 100 || $_POST['Disc'] < 0)
183 display_error(_("The entered discount percent is negative, greater than 100 or invalid."));
189 //-----------------------------------------------------------------------------------------------
191 function handle_update_item()
193 if($_POST['UpdateItem'] != "" && check_item_data())
195 $_SESSION['credit_items']->update_cart_item($_POST['line_no'], $_POST['qty'],
196 $_POST['price'], ($_POST['Disc'] / 100));
200 //-----------------------------------------------------------------------------------------------
202 function handle_delete_item()
204 $_SESSION['credit_items']->remove_from_cart($_GET['Delete']);
207 //-----------------------------------------------------------------------------------------------
209 function handle_new_item()
211 if (!check_item_data())
214 add_to_order($_SESSION['credit_items'], $_POST['line_no'], $_POST['stock_id'], $_POST['qty'],
215 $_POST['price'], $_POST['Disc'] / 100);
218 //-----------------------------------------------------------------------------------------------
219 if (isset($_GET['Delete']) || isset($_GET['Edit']))
222 if (isset($_GET['Delete']))
223 handle_delete_item();
225 if (isset($_POST['AddItem']) || isset($_POST['UpdateItem']))
228 if (isset($_POST['AddItem']))
231 if (isset($_POST['UpdateItem']))
232 handle_update_item();
234 //-----------------------------------------------------------------------------------------------
236 if (isset($_GET['NewCredit']) || !isset($_SESSION['credit_items']))
242 if (!isset($_POST['customer_id']))
243 $_POST['customer_id'] = $_SESSION['credit_items']->customer_id;
244 if (!isset($_POST['branch_id']))
245 $_POST['branch_id'] = $_SESSION['credit_items']->Branch;
248 //-----------------------------------------------------------------------------------------------
250 start_form(false, true);
252 $customer_error = display_credit_header($_SESSION['credit_items']);
254 if ($customer_error == "")
256 start_table("$table_style width=80%", 10);
258 display_credit_items(_("Credit Note Items"), $_SESSION['credit_items']);
259 credit_options_controls();
265 display_error($customer_error);
268 if (!isset($_POST['ProcessCredit']))
270 echo "<br><center><table><tr>";
271 submit_cells('Update', _("Update"));
272 if ($_SESSION['credit_items']->count_items() >= 1
273 /*OR $_POST['ChargeTax'] > 0*/ || (isset($_POST['ChargeFreightCost']) && $_POST['ChargeFreightCost'] > 0))
275 submit_cells('ProcessCredit', _("Process Credit Note"));
277 echo "</tr></table>";