Removed obsolete submit_on_change option from sales_credit_note dimension lists.
[fa-stable.git] / sales / credit_note_entry.php
1 <?php
2 //---------------------------------------------------------------------------
3 //
4 //      Entry/Modify free hand Credit Note
5 //
6 $page_security = 3;
7 $path_to_root="..";
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");
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 if ($use_date_picker) {
23         $js .= get_js_date_picker();
24 }
25
26 if(isset($_GET['NewCredit'])) {
27         $_SESSION['page_title'] = _("Customer Credit Note");
28         handle_new_credit(0);
29 } elseif (isset($_GET['ModifyCredit'])) {
30         $_SESSION['page_title'] = sprintf(_("Modifying Customer Credit Note #%d"), $_GET['ModifyCredit']);
31         handle_new_credit($_GET['ModifyCredit']);
32         $help_page_title = _("Modifying Customer Credit Note");
33 }
34
35 page($_SESSION['page_title'],false, false, "", $js);
36
37 //-----------------------------------------------------------------------------
38
39 check_db_has_stock_items(_("There are no items defined in the system."));
40
41 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
42
43 //-----------------------------------------------------------------------------
44 if ($ret = context_restore()) {
45  // return from new customer add
46         copy_from_cn();
47         if(isset($ret['customer_id']))
48                 $_POST['customer_id'] = $ret['customer_id'];
49         if(isset($ret['branch_id']))
50                 $_POST['branch_id'] = $ret['branch_id'];
51 }
52 if (isset($_POST['_customer_id_editor'])) {
53         copy_to_cn(); //store context
54         context_call($path_to_root.'/sales/manage/customers.php?debtor_no='.$_POST['customer_id'], 'Items');
55 }
56
57 if (isset($_GET['AddedID'])) {
58         $credit_no = $_GET['AddedID'];
59         $trans_type = 11;
60
61         display_notification_centered(sprintf(_("Credit Note # %d has been processed"),$credit_no));
62
63         display_note(get_customer_trans_view_str($trans_type, $credit_no, _("&View this credit note")), 0, 1);
64
65         display_note(print_document_link($credit_no, _("&Print This Credit Invoice"), true, 11),0, 1);
66
67         display_note(get_gl_view_str($trans_type, $credit_no, _("View the GL &Journal Entries for this Credit Note")));
68
69         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another &Credit Note"), "NewCredit=yes");
70
71         display_footer_exit();
72 }
73 //--------------------------------------------------------------------------------
74
75 function line_start_focus() {
76   global $Ajax;
77   $Ajax->activate('items_table');
78   set_focus('_stock_id_edit');
79 }
80
81 //-----------------------------------------------------------------------------
82
83 function copy_to_cn()
84 {
85         $_SESSION['Items']->Comments = $_POST['CreditText'];
86         $_SESSION['Items']->document_date = $_POST['OrderDate'];
87         $_SESSION['Items']->freight_cost = input_num('ChargeFreightCost');
88         $_SESSION['Items']->Location = $_POST["Location"];
89         $_SESSION['Items']->sales_type = $_POST['sales_type_id'];
90         $_SESSION['Items']->reference = $_POST['ref'];
91         $_SESSION['Items']->ship_via = $_POST['ShipperID'];
92         $_SESSION['Items']->dimension_id = $_POST['dimension_id'];
93         $_SESSION['Items']->dimension2_id = $_POST['dimension2_id'];
94 }
95
96 //-----------------------------------------------------------------------------
97
98 function copy_from_cn()
99 {
100         $_POST['CreditText'] = $_SESSION['Items']->Comments;
101         $_POST['OrderDate'] = $_SESSION['Items']->document_date;
102         $_POST['ChargeFreightCost'] = price_format($_SESSION['Items']->freight_cost);
103         $_POST['Location'] = $_SESSION['Items']->Location;
104         $_POST['sales_type_id'] = $_SESSION['Items']->sales_type;
105         $_POST['ref'] = $_SESSION['Items']->reference;
106         $_POST['ShipperID'] = $_SESSION['Items']->ship_via;
107         $_POST['dimension_id'] = $_SESSION['Items']->dimension_id;
108         $_POST['dimension2_id'] = $_SESSION['Items']->dimension2_id;
109 }
110
111 //-----------------------------------------------------------------------------
112
113 function handle_new_credit($trans_no)
114 {
115         processing_start();
116         $_SESSION['Items'] = new Cart(11,$trans_no);
117         copy_from_cn();
118 }
119
120 //-----------------------------------------------------------------------------
121
122 function can_process()
123 {
124
125         $input_error = 0;
126
127         if ($_SESSION['Items']->count_items() == 0 && (!check_num('ChargeFreightCost',0)))
128                 return false;
129         if($_SESSION['Items']->trans_no == 0) {
130             if (!references::is_valid($_POST['ref'])) {
131                 display_error( _("You must enter a reference."));
132                 set_focus('ref');
133                 $input_error = 1;
134             } elseif (!is_new_reference($_POST['ref'], 11))     {
135                 display_error( _("The entered reference is already in use."));
136                 set_focus('ref');
137                 $input_error = 1;
138             }
139         }
140         if (!is_date($_POST['OrderDate'])) {
141                 display_error(_("The entered date for the credit note is invalid."));
142                 set_focus('OrderDate');
143                 $input_error = 1;
144         } elseif (!is_date_in_fiscalyear($_POST['OrderDate'])) {
145                 display_error(_("The entered date is not in fiscal year."));
146                 set_focus('OrderDate');
147                 $input_error = 1;
148         }
149         return ($input_error == 0);
150 }
151
152 //-----------------------------------------------------------------------------
153
154 if (isset($_POST['ProcessCredit']) && can_process()) {
155         copy_to_cn();
156         if ($_POST['CreditType'] == "WriteOff" && (!isset($_POST['WriteOffGLCode']) ||
157                 $_POST['WriteOffGLCode'] == '')) {
158                 display_note(_("For credit notes created to write off the stock, a general ledger account is required to be selected."), 1, 0);
159                 display_note(_("Please select an account to write the cost of the stock off to, then click on Process again."), 1, 0);
160                 exit;
161                 
162         }
163         if (!isset($_POST['WriteOffGLCode'])) {
164                 $_POST['WriteOffGLCode'] = 0;
165         }
166         $credit_no = $_SESSION['Items']->write($_POST['WriteOffGLCode']);
167         processing_end();
168         meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
169
170 } /*end of process credit note */
171
172   //-----------------------------------------------------------------------------
173
174 function check_item_data()
175 {
176         if (!check_num('qty',0)) {
177                 display_error(_("The quantity must be greater than zero."));
178                 set_focus('qty');
179                 return false;
180         }
181         if (!check_num('price',0)) {
182                 display_error(_("The entered price is negative or invalid."));
183                 set_focus('price');
184                 return false;
185         }
186         if (!check_num('Disc', 0, 100)) {
187                 display_error(_("The entered discount percent is negative, greater than 100 or invalid."));
188                 set_focus('Disc');
189                 return false;
190         }
191         return true;
192 }
193
194 //-----------------------------------------------------------------------------
195
196 function handle_update_item()
197 {
198         if ($_POST['UpdateItem'] != "" && check_item_data()) {
199                 $_SESSION['Items']->update_cart_item($_POST['line_no'], input_num('qty'),
200                         input_num('price'), input_num('Disc') / 100);
201         }
202     line_start_focus();
203 }
204
205 //-----------------------------------------------------------------------------
206
207 function handle_delete_item($line_no)
208 {
209         $_SESSION['Items']->remove_from_cart($line_no);
210     line_start_focus();
211 }
212
213 //-----------------------------------------------------------------------------
214
215 function handle_new_item()
216 {
217
218         if (!check_item_data())
219                 return;
220
221         add_to_order($_SESSION['Items'], $_POST['stock_id'], input_num('qty'),
222                 input_num('price'), input_num('Disc') / 100);
223     line_start_focus();
224 }
225 //-----------------------------------------------------------------------------
226 $id = find_submit('Delete');
227 if ($id!=-1)
228         handle_delete_item($id);
229
230 if (isset($_POST['AddItem']))
231         handle_new_item();
232
233 if (isset($_POST['UpdateItem']))
234         handle_update_item();
235
236 if (isset($_POST['CancelItemChanges']))
237         line_start_focus();
238
239 //-----------------------------------------------------------------------------
240
241 if (!processing_active()) {
242         handle_new_credit();
243 }
244
245 //-----------------------------------------------------------------------------
246
247 start_form(false, true);
248
249 $customer_error = display_credit_header($_SESSION['Items']);
250
251 if ($customer_error == "") {
252         start_table("$table_style width=80%", 10);
253         echo "<tr><td>";
254         display_credit_items(_("Credit Note Items"), $_SESSION['Items']);
255         credit_options_controls();
256         echo "</td></tr>";
257         end_table();
258 } else {
259         display_error($customer_error);
260 }
261
262 echo "<br><center><table><tr>";
263 submit_cells('Update', _("Update"));
264 submit_cells('ProcessCredit', _("Process Credit Note"));
265 echo "</tr></table></center>";
266
267 end_form();
268 end_page();
269
270 ?>