Copyright notes at top op every source file
[fa-stable.git] / sales / credit_note_entry.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-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 if ($ret = context_restore()) {
55  // return from new customer add
56         copy_from_cn();
57         if(isset($ret['customer_id']))
58                 $_POST['customer_id'] = $ret['customer_id'];
59         if(isset($ret['branch_id']))
60                 $_POST['branch_id'] = $ret['branch_id'];
61 }
62 if (isset($_POST['_customer_id_editor'])) {
63         copy_to_cn(); //store context
64         context_call($path_to_root.'/sales/manage/customers.php?debtor_no='.$_POST['customer_id'], 'Items');
65 }
66
67 if (isset($_GET['AddedID'])) {
68         $credit_no = $_GET['AddedID'];
69         $trans_type = 11;
70
71         display_notification_centered(sprintf(_("Credit Note # %d has been processed"),$credit_no));
72
73         display_note(get_customer_trans_view_str($trans_type, $credit_no, _("&View this credit note")), 0, 1);
74
75         display_note(print_document_link($credit_no, _("&Print This Credit Invoice"), true, 11),0, 1);
76
77         display_note(get_gl_view_str($trans_type, $credit_no, _("View the GL &Journal Entries for this Credit Note")));
78
79         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another &Credit Note"), "NewCredit=yes");
80
81         display_footer_exit();
82 } else
83         check_edit_conflicts();
84
85 //--------------------------------------------------------------------------------
86
87 function line_start_focus() {
88   global $Ajax;
89   $Ajax->activate('items_table');
90   set_focus('_stock_id_edit');
91 }
92
93 //-----------------------------------------------------------------------------
94
95 function copy_to_cn()
96 {
97         $cart = &$_SESSION['Items'];
98         $cart->Comments = $_POST['CreditText'];
99         $cart->document_date = $_POST['OrderDate'];
100         $cart->freight_cost = input_num('ChargeFreightCost');
101         $cart->Location = $_POST["Location"];
102         $cart->sales_type = $_POST['sales_type_id'];
103         $cart->reference = $_POST['ref'];
104         $cart->ship_via = $_POST['ShipperID'];
105         $cart->dimension_id = $_POST['dimension_id'];
106         $cart->dimension2_id = $_POST['dimension2_id'];
107 }
108
109 //-----------------------------------------------------------------------------
110
111 function copy_from_cn()
112 {
113         $cart = &$_SESSION['Items'];
114         $_POST['CreditText'] = $cart->Comments;
115         $_POST['OrderDate'] = $cart->document_date;
116         $_POST['ChargeFreightCost'] = price_format($cart->freight_cost);
117         $_POST['Location'] = $cart->Location;
118         $_POST['sales_type_id'] = $cart->sales_type;
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(11,$trans_no);
132         copy_from_cn();
133 }
134
135 //-----------------------------------------------------------------------------
136
137 function can_process()
138 {
139
140         $input_error = 0;
141
142         if ($_SESSION['Items']->count_items() == 0 && (!check_num('ChargeFreightCost',0)))
143                 return false;
144         if($_SESSION['Items']->trans_no == 0) {
145             if (!references::is_valid($_POST['ref'])) {
146                 display_error( _("You must enter a reference."));
147                 set_focus('ref');
148                 $input_error = 1;
149             } elseif (!is_new_reference($_POST['ref'], 11))     {
150                 display_error( _("The entered reference is already in use."));
151                 set_focus('ref');
152                 $input_error = 1;
153             }
154         }
155         if (!is_date($_POST['OrderDate'])) {
156                 display_error(_("The entered date for the credit note is invalid."));
157                 set_focus('OrderDate');
158                 $input_error = 1;
159         } elseif (!is_date_in_fiscalyear($_POST['OrderDate'])) {
160                 display_error(_("The entered date is not in fiscal year."));
161                 set_focus('OrderDate');
162                 $input_error = 1;
163         }
164         return ($input_error == 0);
165 }
166
167 //-----------------------------------------------------------------------------
168
169 if (isset($_POST['ProcessCredit']) && can_process()) {
170         copy_to_cn();
171         if ($_POST['CreditType'] == "WriteOff" && (!isset($_POST['WriteOffGLCode']) ||
172                 $_POST['WriteOffGLCode'] == '')) {
173                 display_note(_("For credit notes created to write off the stock, a general ledger account is required to be selected."), 1, 0);
174                 display_note(_("Please select an account to write the cost of the stock off to, then click on Process again."), 1, 0);
175                 exit;
176                 
177         }
178         if (!isset($_POST['WriteOffGLCode'])) {
179                 $_POST['WriteOffGLCode'] = 0;
180         }
181         $credit_no = $_SESSION['Items']->write($_POST['WriteOffGLCode']);
182         processing_end();
183         meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
184
185 } /*end of process credit note */
186
187   //-----------------------------------------------------------------------------
188
189 function check_item_data()
190 {
191         if (!check_num('qty',0)) {
192                 display_error(_("The quantity must be greater than zero."));
193                 set_focus('qty');
194                 return false;
195         }
196         if (!check_num('price',0)) {
197                 display_error(_("The entered price is negative or invalid."));
198                 set_focus('price');
199                 return false;
200         }
201         if (!check_num('Disc', 0, 100)) {
202                 display_error(_("The entered discount percent is negative, greater than 100 or invalid."));
203                 set_focus('Disc');
204                 return false;
205         }
206         return true;
207 }
208
209 //-----------------------------------------------------------------------------
210
211 function handle_update_item()
212 {
213         if ($_POST['UpdateItem'] != "" && check_item_data()) {
214                 $_SESSION['Items']->update_cart_item($_POST['line_no'], input_num('qty'),
215                         input_num('price'), input_num('Disc') / 100);
216         }
217     line_start_focus();
218 }
219
220 //-----------------------------------------------------------------------------
221
222 function handle_delete_item($line_no)
223 {
224         $_SESSION['Items']->remove_from_cart($line_no);
225     line_start_focus();
226 }
227
228 //-----------------------------------------------------------------------------
229
230 function handle_new_item()
231 {
232
233         if (!check_item_data())
234                 return;
235
236         add_to_order($_SESSION['Items'], $_POST['stock_id'], input_num('qty'),
237                 input_num('price'), input_num('Disc') / 100);
238     line_start_focus();
239 }
240 //-----------------------------------------------------------------------------
241 $id = find_submit('Delete');
242 if ($id!=-1)
243         handle_delete_item($id);
244
245 if (isset($_POST['AddItem']))
246         handle_new_item();
247
248 if (isset($_POST['UpdateItem']))
249         handle_update_item();
250
251 if (isset($_POST['CancelItemChanges']))
252         line_start_focus();
253
254 //-----------------------------------------------------------------------------
255
256 if (!processing_active()) {
257         handle_new_credit();
258 }
259
260 //-----------------------------------------------------------------------------
261
262 start_form(false, true);
263 hidden('cart_id');
264
265 $customer_error = display_credit_header($_SESSION['Items']);
266
267 if ($customer_error == "") {
268         start_table("$table_style width=80%", 10);
269         echo "<tr><td>";
270         display_credit_items(_("Credit Note Items"), $_SESSION['Items']);
271         credit_options_controls($_SESSION['Items']);
272         echo "</td></tr>";
273         end_table();
274 } else {
275         display_error($customer_error);
276 }
277
278 echo "<br><center><table><tr>";
279 submit_cells('Update', _("Update"));
280 submit_cells('ProcessCredit', _("Process Credit Note"));
281 echo "</tr></table></center>";
282
283 end_form();
284 end_page();
285
286 ?>