Credit note can be posted without customer selected when search customer List is...
[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 ($SysPrefs->use_popup_windows) {
30         $js .= get_js_open_window(900, 500);
31 }
32 if (user_use_date_picker()) {
33         $js .= get_js_date_picker();
34 }
35
36 if(isset($_GET['NewCredit'])) {
37         $_SESSION['page_title'] = _($help_context = "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_context = "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."-".$trans_type, _("&Print This Credit Invoice"), true, ST_CUSTCREDIT),0, 1);
71         display_note(print_document_link($credit_no."-".$trans_type, _("&Email This Credit Invoice"), true, ST_CUSTCREDIT, false, "printlink", "", 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         hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$credit_no");
78
79         display_footer_exit();
80 } else
81         check_edit_conflicts(get_post('cart_id'));
82
83 //--------------------------------------------------------------------------------
84
85 function line_start_focus() {
86         global $Ajax;
87         $Ajax->activate('items_table');
88         set_focus('_stock_id_edit');
89 }
90
91 //-----------------------------------------------------------------------------
92
93 function copy_to_cn()
94 {
95         $cart = &$_SESSION['Items'];
96         $cart->Comments = $_POST['CreditText'];
97         $cart->document_date = $_POST['OrderDate'];
98         $cart->freight_cost = input_num('ChargeFreightCost');
99         $cart->Location = (isset($_POST["Location"]) ? $_POST["Location"] : "");
100         $cart->sales_type = $_POST['sales_type_id'];
101         if ($cart->trans_no == 0)
102                 $cart->reference = $_POST['ref'];
103         $cart->ship_via = $_POST['ShipperID'];
104         $cart->dimension_id = $_POST['dimension_id'];
105         $cart->dimension2_id = $_POST['dimension2_id'];
106 }
107
108 //-----------------------------------------------------------------------------
109
110 function copy_from_cn()
111 {
112         $cart = &$_SESSION['Items'];
113         $_POST['CreditText'] = $cart->Comments;
114         $_POST['OrderDate'] = $cart->document_date;
115         $_POST['ChargeFreightCost'] = price_format($cart->freight_cost);
116         $_POST['Location'] = $cart->Location;
117         $_POST['sales_type_id'] = $cart->sales_type;
118         if ($cart->trans_no == 0)
119                 $_POST['ref'] = $cart->reference;
120         $_POST['ShipperID'] = $cart->ship_via;
121         $_POST['dimension_id'] = $cart->dimension_id;
122         $_POST['dimension2_id'] = $cart->dimension2_id;
123         $_POST['cart_id'] = $cart->cart_id;
124 }
125
126 //-----------------------------------------------------------------------------
127
128 function handle_new_credit($trans_no)
129 {
130         processing_start();
131         $_SESSION['Items'] = new Cart(ST_CUSTCREDIT,$trans_no);
132         copy_from_cn();
133 }
134
135 //-----------------------------------------------------------------------------
136
137 function can_process()
138 {
139         global $Refs;
140
141         $input_error = 0;
142
143         if (!get_post('customer_id')) 
144         {
145                 display_error(_("There is no customer selected."));
146                 set_focus('customer_id');
147                 return false;
148         } 
149         
150         if (!get_post('branch_id')) 
151         {
152                 display_error(_("This customer has no branch defined."));
153                 set_focus('branch_id');
154                 return false;
155         } 
156         if ($_SESSION['Items']->count_items() == 0 && !input_num('ChargeFreightCost',0))
157         {
158                 display_error(_("You must enter at least one non empty item line."));
159                 set_focus('AddItem');
160                 return false;
161         }
162         if($_SESSION['Items']->trans_no == 0) {
163             if (!$Refs->is_valid($_POST['ref'], ST_CUSTCREDIT)) {
164                         display_error( _("You must enter a reference."));
165                         set_focus('ref');
166                         $input_error = 1;
167                 }
168         }
169
170         if (!is_date($_POST['OrderDate'])) {
171                 display_error(_("The entered date for the credit note is invalid."));
172                 set_focus('OrderDate');
173                 $input_error = 1;
174         } elseif (!is_date_in_fiscalyear($_POST['OrderDate'])) {
175                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
176                 set_focus('OrderDate');
177                 $input_error = 1;
178         }
179         return ($input_error == 0);
180 }
181
182 //-----------------------------------------------------------------------------
183
184 if (isset($_POST['ProcessCredit']) && can_process()) {
185         copy_to_cn();
186         if ($_POST['CreditType'] == "WriteOff" && (!isset($_POST['WriteOffGLCode']) ||
187                 $_POST['WriteOffGLCode'] == '')) {
188                 display_note(_("For credit notes created to write off the stock, a general ledger account is required to be selected."), 1, 0);
189                 display_note(_("Please select an account to write the cost of the stock off to, then click on Process again."), 1, 0);
190                 exit;
191         }
192         if (!isset($_POST['WriteOffGLCode'])) {
193                 $_POST['WriteOffGLCode'] = 0;
194         }
195         copy_to_cn();
196         $credit_no = $_SESSION['Items']->write($_POST['WriteOffGLCode']);
197         if ($credit_no == -1)
198         {
199                 display_error(_("The entered reference is already in use."));
200                 set_focus('ref');
201         }
202         else
203         {
204                 new_doc_date($_SESSION['Items']->document_date);
205                 processing_end();
206                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
207         }
208 } /*end of process credit note */
209
210   //-----------------------------------------------------------------------------
211
212 function check_item_data()
213 {
214         if (!check_num('qty',0)) {
215                 display_error(_("The quantity must be greater than zero."));
216                 set_focus('qty');
217                 return false;
218         }
219         if (!check_num('price',0)) {
220                 display_error(_("The entered price is negative or invalid."));
221                 set_focus('price');
222                 return false;
223         }
224         if (!check_num('Disc', 0, 100)) {
225                 display_error(_("The entered discount percent is negative, greater than 100 or invalid."));
226                 set_focus('Disc');
227                 return false;
228         }
229         return true;
230 }
231
232 //-----------------------------------------------------------------------------
233
234 function handle_update_item()
235 {
236         if ($_POST['UpdateItem'] != "" && check_item_data()) {
237                 $_SESSION['Items']->update_cart_item($_POST['line_no'], input_num('qty'),
238                         input_num('price'), input_num('Disc') / 100);
239         }
240     line_start_focus();
241 }
242
243 //-----------------------------------------------------------------------------
244
245 function handle_delete_item($line_no)
246 {
247         $_SESSION['Items']->remove_from_cart($line_no);
248     line_start_focus();
249 }
250
251 //-----------------------------------------------------------------------------
252
253 function handle_new_item()
254 {
255
256         if (!check_item_data())
257                 return;
258
259         add_to_order($_SESSION['Items'], $_POST['stock_id'], input_num('qty'),
260                 input_num('price'), input_num('Disc') / 100);
261     line_start_focus();
262 }
263 //-----------------------------------------------------------------------------
264 $id = find_submit('Delete');
265 if ($id!=-1)
266         handle_delete_item($id);
267
268 if (isset($_POST['AddItem']))
269         handle_new_item();
270
271 if (isset($_POST['UpdateItem']))
272         handle_update_item();
273
274 if (isset($_POST['CancelItemChanges']))
275         line_start_focus();
276
277 //-----------------------------------------------------------------------------
278
279 if (!processing_active()) {
280         handle_new_credit(0);
281 }
282
283 //-----------------------------------------------------------------------------
284
285 start_form();
286 hidden('cart_id');
287
288 $customer_error = display_credit_header($_SESSION['Items']);
289
290 if ($customer_error == "") {
291         start_table(TABLESTYLE, "width='80%'", 10);
292         echo "<tr><td>";
293         display_credit_items(_("Credit Note Items"), $_SESSION['Items']);
294         credit_options_controls($_SESSION['Items']);
295         echo "</td></tr>";
296         end_table();
297 } else {
298         display_error($customer_error);
299 }
300
301 echo "<br><center><table><tr>";
302 submit_cells('Update', _("Update"));
303 submit_cells('ProcessCredit', _("Process Credit Note"), '', false, 'default');
304 echo "</tr></table></center>";
305
306 end_form();
307 end_page();
308