2 //---------------------------------------------------------------------------
4 // Entry/Modify free hand Credit Note
8 include_once($path_to_root . "/sales/includes/cart_class.inc");
9 include_once($path_to_root . "/includes/session.inc");
10 include_once($path_to_root . "/includes/data_checks.inc");
11 include_once($path_to_root . "/sales/includes/sales_db.inc");
12 include_once($path_to_root . "/sales/includes/sales_ui.inc");
13 include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
14 include_once($path_to_root . "/sales/includes/ui/sales_credit_ui.inc");
15 include_once($path_to_root . "/sales/includes/ui/sales_order_ui.inc");
18 if ($use_popup_windows) {
19 $js .= get_js_open_window(900, 500);
21 if ($use_date_picker) {
22 $js .= get_js_date_picker();
25 if(isset($_GET['NewCredit'])) {
26 $_SESSION['page_title'] = _("Customer Credit Note");
28 } elseif (isset($_GET['ModifyCredit'])) {
29 $_SESSION['page_title'] = sprintf(_("Modifying Customer Credit Note #%d"), $_GET['ModifyCredit']);
30 handle_new_credit($_GET['ModifyCredit']);
31 $help_page_title = _("Modifying Customer Credit Note");
34 page($_SESSION['page_title'],false, false, "", $js);
36 //-----------------------------------------------------------------------------
38 check_db_has_stock_items(_("There are no items defined in the system."));
40 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
42 //-----------------------------------------------------------------------------
43 if ($ret = context_restore()) {
44 // return from new customer add
46 if(isset($ret['customer_id']))
47 $_POST['customer_id'] = $ret['customer_id'];
48 if(isset($ret['branch_id']))
49 $_POST['branch_id'] = $ret['branch_id'];
51 if (isset($_POST['_customer_id_editor'])) {
52 copy_to_cn(); //store context
53 context_call($path_to_root.'/sales/manage/customers.php?debtor_no='.$_POST['customer_id'], 'Items');
56 if (isset($_GET['AddedID'])) {
57 $credit_no = $_GET['AddedID'];
60 display_notification_centered(sprintf(_("Credit Note # %d has been processed"),$credit_no));
62 display_note(get_customer_trans_view_str($trans_type, $credit_no, _("&View this credit note")), 0, 1);
64 display_note(print_document_link($credit_no, _("&Print This Credit Invoice"), true, 11),1);
66 display_note(get_gl_view_str($trans_type, $credit_no, _("View the GL &Journal Entries for this Credit Note")));
68 hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another &Credit Note"), "NewCredit=yes");
70 display_footer_exit();
72 //--------------------------------------------------------------------------------
74 function line_start_focus() {
76 $Ajax->activate('items_table');
77 set_focus('_stock_id_edit');
80 //-----------------------------------------------------------------------------
84 $_SESSION['Items']->Comments = $_POST['CreditText'];
85 $_SESSION['Items']->document_date = $_POST['OrderDate'];
86 $_SESSION['Items']->freight_cost = input_num('ChargeFreightCost');
87 $_SESSION['Items']->Location = $_POST["Location"];
88 $_SESSION['Items']->sales_type = $_POST['sales_type_id'];
89 $_SESSION['Items']->reference = $_POST['ref'];
90 $_SESSION['Items']->ship_via = $_POST['ShipperID'];
93 //-----------------------------------------------------------------------------
95 function copy_from_cn()
97 $_POST['CreditText'] = $_SESSION['Items']->Comments;
98 $_POST['OrderDate'] = $_SESSION['Items']->document_date;
99 $_POST['ChargeFreightCost'] = price_format($_SESSION['Items']->freight_cost);
100 $_POST['Location'] = $_SESSION['Items']->Location;
101 $_POST['sales_type_id'] = $_SESSION['Items']->sales_type;
102 $_POST['ref'] = $_SESSION['Items']->reference;
103 $_POST['ShipperID'] = $_SESSION['Items']->ship_via;
106 //-----------------------------------------------------------------------------
108 function handle_new_credit($trans_no)
111 $_SESSION['Items'] = new Cart(11,$trans_no);
115 //-----------------------------------------------------------------------------
117 function can_process()
122 if ($_SESSION['Items']->count_items() == 0 && (!check_num('ChargeFreightCost',0)))
124 if($_SESSION['Items']->trans_no == 0) {
125 if (!references::is_valid($_POST['ref'])) {
126 display_error( _("You must enter a reference."));
129 } elseif (!is_new_reference($_POST['ref'], 11)) {
130 display_error( _("The entered reference is already in use."));
135 if (!is_date($_POST['OrderDate'])) {
136 display_error(_("The entered date for the credit note is invalid."));
137 set_focus('OrderDate');
139 } elseif (!is_date_in_fiscalyear($_POST['OrderDate'])) {
140 display_error(_("The entered date is not in fiscal year."));
141 set_focus('OrderDate');
144 return ($input_error == 0);
147 //-----------------------------------------------------------------------------
149 if (isset($_POST['ProcessCredit']) && can_process()) {
150 if ($_POST['CreditType'] == "WriteOff" && (!isset($_POST['WriteOffGLCode']) ||
151 $_POST['WriteOffGLCode'] == '')) {
152 display_note(_("For credit notes created to write off the stock, a general ledger account is required to be selected."), 1, 0);
153 display_note(_("Please select an account to write the cost of the stock off to, then click on Process again."), 1, 0);
157 if (!isset($_POST['WriteOffGLCode'])) {
158 $_POST['WriteOffGLCode'] = 0;
160 $credit_no = $_SESSION['Items']->write($_POST['WriteOffGLCode']);
162 meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
164 } /*end of process credit note */
166 //-----------------------------------------------------------------------------
168 function check_item_data()
170 if (!check_num('qty',0)) {
171 display_error(_("The quantity must be greater than zero."));
175 if (!check_num('price',0)) {
176 display_error(_("The entered price is negative or invalid."));
180 if (!check_num('Disc', 0, 100)) {
181 display_error(_("The entered discount percent is negative, greater than 100 or invalid."));
188 //-----------------------------------------------------------------------------
190 function handle_update_item()
192 if ($_POST['UpdateItem'] != "" && check_item_data()) {
193 $_SESSION['Items']->update_cart_item($_POST['line_no'], input_num('qty'),
194 input_num('price'), input_num('Disc') / 100);
199 //-----------------------------------------------------------------------------
201 function handle_delete_item($line_no)
203 $_SESSION['Items']->remove_from_cart($line_no);
207 //-----------------------------------------------------------------------------
209 function handle_new_item()
212 if (!check_item_data())
215 add_to_order($_SESSION['Items'], $_POST['stock_id'], input_num('qty'),
216 input_num('price'), input_num('Disc') / 100);
219 //-----------------------------------------------------------------------------
220 $id = find_submit('Delete');
222 handle_delete_item($id);
224 if (isset($_POST['AddItem']))
227 if (isset($_POST['UpdateItem']))
228 handle_update_item();
230 if (isset($_POST['CancelItemChanges']))
233 //-----------------------------------------------------------------------------
235 if (!processing_active()) {
239 //-----------------------------------------------------------------------------
241 start_form(false, true);
243 $customer_error = display_credit_header($_SESSION['Items']);
245 if ($customer_error == "") {
246 start_table("$table_style width=80%", 10);
248 display_credit_items(_("Credit Note Items"), $_SESSION['Items']);
249 credit_options_controls();
253 display_error($customer_error);
256 echo "<br><center><table><tr>";
257 submit_cells('Update', _("Update"));
258 submit_cells('ProcessCredit', _("Process Credit Note"));
259 echo "</tr></table></center>";