cab8c3dc810a6a7edbf64284187fdf9878c76bcd
[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 } else
73         check_edit_conflicts();
74
75 //--------------------------------------------------------------------------------
76
77 function line_start_focus() {
78   global $Ajax;
79   $Ajax->activate('items_table');
80   set_focus('_stock_id_edit');
81 }
82
83 //-----------------------------------------------------------------------------
84
85 function copy_to_cn()
86 {
87         $cart = &$_SESSION['Items'];
88         $cart->Comments = $_POST['CreditText'];
89         $cart->document_date = $_POST['OrderDate'];
90         $cart->freight_cost = input_num('ChargeFreightCost');
91         $cart->Location = $_POST["Location"];
92         $cart->sales_type = $_POST['sales_type_id'];
93         $cart->reference = $_POST['ref'];
94         $cart->ship_via = $_POST['ShipperID'];
95         $cart->dimension_id = $_POST['dimension_id'];
96         $cart->dimension2_id = $_POST['dimension2_id'];
97 }
98
99 //-----------------------------------------------------------------------------
100
101 function copy_from_cn()
102 {
103         $cart = &$_SESSION['Items'];
104         $_POST['CreditText'] = $cart->Comments;
105         $_POST['OrderDate'] = $cart->document_date;
106         $_POST['ChargeFreightCost'] = price_format($cart->freight_cost);
107         $_POST['Location'] = $cart->Location;
108         $_POST['sales_type_id'] = $cart->sales_type;
109         $_POST['ref'] = $cart->reference;
110         $_POST['ShipperID'] = $cart->ship_via;
111         $_POST['dimension_id'] = $cart->dimension_id;
112         $_POST['dimension2_id'] = $cart->dimension2_id;
113         $_POST['cart_id'] = $cart->cart_id;
114 }
115
116 //-----------------------------------------------------------------------------
117
118 function handle_new_credit($trans_no)
119 {
120         processing_start();
121         $_SESSION['Items'] = new Cart(11,$trans_no);
122         copy_from_cn();
123 }
124
125 //-----------------------------------------------------------------------------
126
127 function can_process()
128 {
129
130         $input_error = 0;
131
132         if ($_SESSION['Items']->count_items() == 0 && (!check_num('ChargeFreightCost',0)))
133                 return false;
134         if($_SESSION['Items']->trans_no == 0) {
135             if (!references::is_valid($_POST['ref'])) {
136                 display_error( _("You must enter a reference."));
137                 set_focus('ref');
138                 $input_error = 1;
139             } elseif (!is_new_reference($_POST['ref'], 11))     {
140                 display_error( _("The entered reference is already in use."));
141                 set_focus('ref');
142                 $input_error = 1;
143             }
144         }
145         if (!is_date($_POST['OrderDate'])) {
146                 display_error(_("The entered date for the credit note is invalid."));
147                 set_focus('OrderDate');
148                 $input_error = 1;
149         } elseif (!is_date_in_fiscalyear($_POST['OrderDate'])) {
150                 display_error(_("The entered date is not in fiscal year."));
151                 set_focus('OrderDate');
152                 $input_error = 1;
153         }
154         return ($input_error == 0);
155 }
156
157 //-----------------------------------------------------------------------------
158
159 if (isset($_POST['ProcessCredit']) && can_process()) {
160         copy_to_cn();
161         if ($_POST['CreditType'] == "WriteOff" && (!isset($_POST['WriteOffGLCode']) ||
162                 $_POST['WriteOffGLCode'] == '')) {
163                 display_note(_("For credit notes created to write off the stock, a general ledger account is required to be selected."), 1, 0);
164                 display_note(_("Please select an account to write the cost of the stock off to, then click on Process again."), 1, 0);
165                 exit;
166                 
167         }
168         if (!isset($_POST['WriteOffGLCode'])) {
169                 $_POST['WriteOffGLCode'] = 0;
170         }
171         $credit_no = $_SESSION['Items']->write($_POST['WriteOffGLCode']);
172         processing_end();
173         meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
174
175 } /*end of process credit note */
176
177   //-----------------------------------------------------------------------------
178
179 function check_item_data()
180 {
181         if (!check_num('qty',0)) {
182                 display_error(_("The quantity must be greater than zero."));
183                 set_focus('qty');
184                 return false;
185         }
186         if (!check_num('price',0)) {
187                 display_error(_("The entered price is negative or invalid."));
188                 set_focus('price');
189                 return false;
190         }
191         if (!check_num('Disc', 0, 100)) {
192                 display_error(_("The entered discount percent is negative, greater than 100 or invalid."));
193                 set_focus('Disc');
194                 return false;
195         }
196         return true;
197 }
198
199 //-----------------------------------------------------------------------------
200
201 function handle_update_item()
202 {
203         if ($_POST['UpdateItem'] != "" && check_item_data()) {
204                 $_SESSION['Items']->update_cart_item($_POST['line_no'], input_num('qty'),
205                         input_num('price'), input_num('Disc') / 100);
206         }
207     line_start_focus();
208 }
209
210 //-----------------------------------------------------------------------------
211
212 function handle_delete_item($line_no)
213 {
214         $_SESSION['Items']->remove_from_cart($line_no);
215     line_start_focus();
216 }
217
218 //-----------------------------------------------------------------------------
219
220 function handle_new_item()
221 {
222
223         if (!check_item_data())
224                 return;
225
226         add_to_order($_SESSION['Items'], $_POST['stock_id'], input_num('qty'),
227                 input_num('price'), input_num('Disc') / 100);
228     line_start_focus();
229 }
230 //-----------------------------------------------------------------------------
231 $id = find_submit('Delete');
232 if ($id!=-1)
233         handle_delete_item($id);
234
235 if (isset($_POST['AddItem']))
236         handle_new_item();
237
238 if (isset($_POST['UpdateItem']))
239         handle_update_item();
240
241 if (isset($_POST['CancelItemChanges']))
242         line_start_focus();
243
244 //-----------------------------------------------------------------------------
245
246 if (!processing_active()) {
247         handle_new_credit();
248 }
249
250 //-----------------------------------------------------------------------------
251
252 start_form(false, true);
253 hidden('cart_id');
254
255 $customer_error = display_credit_header($_SESSION['Items']);
256
257 if ($customer_error == "") {
258         start_table("$table_style width=80%", 10);
259         echo "<tr><td>";
260         display_credit_items(_("Credit Note Items"), $_SESSION['Items']);
261         credit_options_controls($_SESSION['Items']);
262         echo "</td></tr>";
263         end_table();
264 } else {
265         display_error($customer_error);
266 }
267
268 echo "<br><center><table><tr>";
269 submit_cells('Update', _("Update"));
270 submit_cells('ProcessCredit', _("Process Credit Note"));
271 echo "</tr></table></center>";
272
273 end_form();
274 end_page();
275
276 ?>