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