Changed all numeric constants to the new defined constants. A huge task.
[fa-stable.git] / sales / credit_note_entry.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //---------------------------------------------------------------------------
13 //
14 //      Entry/Modify free hand Credit Note
15 //
16 $page_security = 'SA_SALESCREDIT';
17 $path_to_root = "..";
18 include_once($path_to_root . "/sales/includes/cart_class.inc");
19 include_once($path_to_root . "/includes/session.inc");
20 include_once($path_to_root . "/includes/data_checks.inc");
21 include_once($path_to_root . "/sales/includes/sales_db.inc");
22 include_once($path_to_root . "/sales/includes/sales_ui.inc");
23 include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
24 include_once($path_to_root . "/sales/includes/ui/sales_credit_ui.inc");
25 include_once($path_to_root . "/sales/includes/ui/sales_order_ui.inc");
26 include_once($path_to_root . "/reporting/includes/reporting.inc");
27
28 $js = "";
29 if ($use_popup_windows) {
30         $js .= get_js_open_window(900, 500);
31 }
32 if ($use_date_picker) {
33         $js .= get_js_date_picker();
34 }
35
36 if(isset($_GET['NewCredit'])) {
37         $_SESSION['page_title'] = _("Customer Credit Note");
38         handle_new_credit(0);
39 } elseif (isset($_GET['ModifyCredit'])) {
40         $_SESSION['page_title'] = sprintf(_("Modifying Customer Credit Note #%d"), $_GET['ModifyCredit']);
41         handle_new_credit($_GET['ModifyCredit']);
42         $help_page_title = _("Modifying Customer Credit Note");
43 }
44
45 page($_SESSION['page_title'],false, false, "", $js);
46
47 //-----------------------------------------------------------------------------
48
49 check_db_has_stock_items(_("There are no items defined in the system."));
50
51 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
52
53 //-----------------------------------------------------------------------------
54
55 if (list_updated('branch_id')) {
56         // when branch is selected via external editor also customer can change
57         $br = get_branch(get_post('branch_id'));
58         $_POST['customer_id'] = $br['debtor_no'];
59         $Ajax->activate('customer_id');
60 }
61
62 if (isset($_GET['AddedID'])) {
63         $credit_no = $_GET['AddedID'];
64         $trans_type = ST_CUSTCREDIT;
65
66         display_notification_centered(sprintf(_("Credit Note # %d has been processed"),$credit_no));
67
68         display_note(get_customer_trans_view_str($trans_type, $credit_no, _("&View this credit note")), 0, 1);
69
70         display_note(print_document_link($credit_no, _("&Print This Credit Invoice"), true, ST_CUSTCREDIT),0, 1);
71         display_note(print_document_link($credit_no, _("&Email This Credit Invoice"), true, ST_CUSTCREDIT, false, "", "", 1),0, 1);
72
73         display_note(get_gl_view_str($trans_type, $credit_no, _("View the GL &Journal Entries for this Credit Note")));
74
75         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another &Credit Note"), "NewCredit=yes");
76
77         display_footer_exit();
78 } else
79         check_edit_conflicts();
80
81 //--------------------------------------------------------------------------------
82
83 function line_start_focus() {
84   global $Ajax;
85   $Ajax->activate('items_table');
86   set_focus('_stock_id_edit');
87 }
88
89 //-----------------------------------------------------------------------------
90
91 function copy_to_cn()
92 {
93         $cart = &$_SESSION['Items'];
94         $cart->Comments = $_POST['CreditText'];
95         $cart->document_date = $_POST['OrderDate'];
96         $cart->freight_cost = input_num('ChargeFreightCost');
97         $cart->Location = $_POST["Location"];
98         $cart->sales_type = $_POST['sales_type_id'];
99         $cart->reference = $_POST['ref'];
100         $cart->ship_via = $_POST['ShipperID'];
101         $cart->dimension_id = $_POST['dimension_id'];
102         $cart->dimension2_id = $_POST['dimension2_id'];
103 }
104
105 //-----------------------------------------------------------------------------
106
107 function copy_from_cn()
108 {
109         $cart = &$_SESSION['Items'];
110         $_POST['CreditText'] = $cart->Comments;
111         $_POST['OrderDate'] = $cart->document_date;
112         $_POST['ChargeFreightCost'] = price_format($cart->freight_cost);
113         $_POST['Location'] = $cart->Location;
114         $_POST['sales_type_id'] = $cart->sales_type;
115         $_POST['ref'] = $cart->reference;
116         $_POST['ShipperID'] = $cart->ship_via;
117         $_POST['dimension_id'] = $cart->dimension_id;
118         $_POST['dimension2_id'] = $cart->dimension2_id;
119         $_POST['cart_id'] = $cart->cart_id;
120 }
121
122 //-----------------------------------------------------------------------------
123
124 function handle_new_credit($trans_no)
125 {
126         processing_start();
127         $_SESSION['Items'] = new Cart(11,$trans_no);
128         copy_from_cn();
129 }
130
131 //-----------------------------------------------------------------------------
132
133 function can_process()
134 {
135         global $Refs;
136
137         $input_error = 0;
138
139         if ($_SESSION['Items']->count_items() == 0 && (!check_num('ChargeFreightCost',0)))
140                 return false;
141         if($_SESSION['Items']->trans_no == 0) {
142             if (!$Refs->is_valid($_POST['ref'])) {
143                 display_error( _("You must enter a reference."));
144                 set_focus('ref');
145                 $input_error = 1;
146             } elseif (!is_new_reference($_POST['ref'], ST_CUSTCREDIT))  {
147                 display_error( _("The entered reference is already in use."));
148                 set_focus('ref');
149                 $input_error = 1;
150             }
151         }
152         if (!is_date($_POST['OrderDate'])) {
153                 display_error(_("The entered date for the credit note is invalid."));
154                 set_focus('OrderDate');
155                 $input_error = 1;
156         } elseif (!is_date_in_fiscalyear($_POST['OrderDate'])) {
157                 display_error(_("The entered date is not in fiscal year."));
158                 set_focus('OrderDate');
159                 $input_error = 1;
160         }
161         return ($input_error == 0);
162 }
163
164 //-----------------------------------------------------------------------------
165
166 if (isset($_POST['ProcessCredit']) && can_process()) {
167         copy_to_cn();
168         if ($_POST['CreditType'] == "WriteOff" && (!isset($_POST['WriteOffGLCode']) ||
169                 $_POST['WriteOffGLCode'] == '')) {
170                 display_note(_("For credit notes created to write off the stock, a general ledger account is required to be selected."), 1, 0);
171                 display_note(_("Please select an account to write the cost of the stock off to, then click on Process again."), 1, 0);
172                 exit;
173                 
174         }
175         if (!isset($_POST['WriteOffGLCode'])) {
176                 $_POST['WriteOffGLCode'] = 0;
177         }
178         copy_to_cn();
179         $credit_no = $_SESSION['Items']->write($_POST['WriteOffGLCode']);
180         new_doc_date($_SESSION['Items']->document_date);
181         processing_end();
182         meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
183
184 } /*end of process credit note */
185
186   //-----------------------------------------------------------------------------
187
188 function check_item_data()
189 {
190         if (!check_num('qty',0)) {
191                 display_error(_("The quantity must be greater than zero."));
192                 set_focus('qty');
193                 return false;
194         }
195         if (!check_num('price',0)) {
196                 display_error(_("The entered price is negative or invalid."));
197                 set_focus('price');
198                 return false;
199         }
200         if (!check_num('Disc', 0, 100)) {
201                 display_error(_("The entered discount percent is negative, greater than 100 or invalid."));
202                 set_focus('Disc');
203                 return false;
204         }
205         return true;
206 }
207
208 //-----------------------------------------------------------------------------
209
210 function handle_update_item()
211 {
212         if ($_POST['UpdateItem'] != "" && check_item_data()) {
213                 $_SESSION['Items']->update_cart_item($_POST['line_no'], input_num('qty'),
214                         input_num('price'), input_num('Disc') / 100);
215         }
216     line_start_focus();
217 }
218
219 //-----------------------------------------------------------------------------
220
221 function handle_delete_item($line_no)
222 {
223         $_SESSION['Items']->remove_from_cart($line_no);
224     line_start_focus();
225 }
226
227 //-----------------------------------------------------------------------------
228
229 function handle_new_item()
230 {
231
232         if (!check_item_data())
233                 return;
234
235         add_to_order($_SESSION['Items'], $_POST['stock_id'], input_num('qty'),
236                 input_num('price'), input_num('Disc') / 100);
237     line_start_focus();
238 }
239 //-----------------------------------------------------------------------------
240 $id = find_submit('Delete');
241 if ($id!=-1)
242         handle_delete_item($id);
243
244 if (isset($_POST['AddItem']))
245         handle_new_item();
246
247 if (isset($_POST['UpdateItem']))
248         handle_update_item();
249
250 if (isset($_POST['CancelItemChanges']))
251         line_start_focus();
252
253 //-----------------------------------------------------------------------------
254
255 if (!processing_active()) {
256         handle_new_credit();
257 }
258
259 //-----------------------------------------------------------------------------
260
261 start_form();
262 hidden('cart_id');
263
264 $customer_error = display_credit_header($_SESSION['Items']);
265
266 if ($customer_error == "") {
267         start_table("$table_style width=80%", 10);
268         echo "<tr><td>";
269         display_credit_items(_("Credit Note Items"), $_SESSION['Items']);
270         credit_options_controls($_SESSION['Items']);
271         echo "</td></tr>";
272         end_table();
273 } else {
274         display_error($customer_error);
275 }
276
277 echo "<br><center><table><tr>";
278 submit_cells('Update', _("Update"));
279 submit_cells('ProcessCredit', _("Process Credit Note"), '', false, 'default');
280 echo "</tr></table></center>";
281
282 end_form();
283 end_page();
284
285 ?>