*** empty log message ***
[fa-stable.git] / sales / credit_note_entry.php
1 <?php
2
3 /*The credit selection screen uses the Cart class used for the making up orders
4 some of the variable names refer to order - please think credit when you read order */
5
6 $page_security = 3;
7 $path_to_root="..";
8 include_once($path_to_root . "/sales/includes/cart_class.inc");
9
10 include_once($path_to_root . "/includes/session.inc");
11
12 include_once($path_to_root . "/includes/data_checks.inc");
13
14 include_once($path_to_root . "/sales/includes/sales_db.inc");
15
16 include_once($path_to_root . "/sales/includes/sales_ui.inc");
17
18 include_once($path_to_root . "/sales/includes/ui/sales_credit_ui.inc");
19 include_once($path_to_root . "/sales/includes/ui/sales_order_ui.inc");
20
21 $js = "";
22 if ($use_popup_windows)
23         $js .= get_js_open_window(900, 500);
24 page(_("Customer Credit Note"), false, false, "", $js);
25
26 //-----------------------------------------------------------------------------------------------
27
28 check_db_has_stock_items(_("There are no items defined in the system."));
29
30 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
31
32 //---------------------------------------------------------------------------------------------------------------
33
34 if (isset($_GET['AddedID'])) 
35 {
36         $credit_no = $_GET['AddedID'];
37         $trans_type = 11;
38
39         display_notification_centered(_("Credit Note has been processed"));
40         display_note(get_customer_trans_view_str($trans_type, $credit_no, _("View this credit note")), 0, 1);
41         
42         display_note(get_gl_view_str($trans_type, $credit_no, _("View the GL Journal Entries for this Credit Note")));
43
44         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Credit Note"), "NewCredit=yes");
45
46         display_footer_exit();
47 }
48
49 //--------------------------------------------------------------------------------------------------
50
51 function copy_to_cn()
52 {
53         $_SESSION['credit_items']->memo_ = $_POST['CreditText'];
54
55         $_SESSION['credit_items']->orig_order_date = $_POST['OrderDate'];
56         $_SESSION['credit_items']->freight_cost = $_POST['ChargeFreightCost'];
57
58         $_SESSION['credit_items']->Location = $_POST["Location"];
59
60         $_SESSION['credit_items']->default_sales_type = $_POST['sales_type_id'];
61         $_SESSION['credit_items']->tax_group_id = $_POST["tax_group_id"];
62 }
63
64 //--------------------------------------------------------------------------------------------------
65
66 function copy_from_cn()
67 {
68         $_POST['CreditText'] = $_SESSION['credit_items']->memo_;
69
70         $_POST['OrderDate'] = $_SESSION['credit_items']->orig_order_date;
71         $_POST['ChargeFreightCost'] = $_SESSION['credit_items']->freight_cost;
72
73         $_POST["Location"] = $_SESSION['credit_items']->Location;
74
75         $_POST['sales_type_id'] = $_SESSION['credit_items']->default_sales_type;
76         $_POST["tax_group_id"] = $_SESSION['credit_items']->tax_group_id;
77 }
78
79 //-----------------------------------------------------------------------------------------------
80
81 function handle_new_order()
82 {
83         if (isset($_SESSION['credit_items']))
84         {
85                 unset ($_SESSION['credit_items']->line_items);
86                 unset ($_SESSION['credit_items']);
87         }
88
89     session_register("credit_items");
90
91     $_SESSION['credit_items'] = new cart;
92         $_POST['OrderDate'] = Today();
93         if (!is_date_in_fiscalyear($_POST['OrderDate']))
94                 $_POST['OrderDate'] = end_fiscalyear();
95         $_SESSION['credit_items']->orig_order_date = $_POST['OrderDate'];       
96     
97 }
98
99 //-----------------------------------------------------------------------------------------------
100
101 if (isset($_POST['ProcessCredit']))
102 {
103
104         $input_error = 0;
105
106         if (!references::is_valid($_POST['ref'])) 
107         {
108                 display_error( _("You must enter a reference."));
109                 $input_error = 1;
110         } 
111         elseif (!is_new_reference($_POST['ref'], 11)) 
112         {
113                 display_error( _("The entered reference is already in use."));
114                 $input_error = 1;
115         } 
116         elseif (!is_date($_POST['OrderDate'])) 
117         {
118                 display_error(_("The entered date for the credit note is invalid."));
119                 $input_error = 1;
120         } 
121         elseif (!is_date_in_fiscalyear($_POST['OrderDate'])) 
122         {
123                 display_error(_("The entered date is not in fiscal year."));
124                 $input_error = 1;
125         }
126         if ($input_error == 1)
127                 unset($_POST['ProcessCredit']);
128 }
129
130 //------------------------------------------------------------------------------------
131
132 if (isset($_POST['ProcessCredit']))
133 {
134         //alert("WriteOffGLCode = ".$_POST['WriteOffGLCode'].", CreditType = ".$_POST['CreditType']);
135         if ($_POST['CreditType'] == "WriteOff" && (!isset($_POST['WriteOffGLCode']) || 
136                 $_POST['WriteOffGLCode'] == ''))
137         {
138                 display_note(_("For credit notes created to write off the stock, a general ledger account is required to be selected."), 1, 0);
139                 display_note(_("Please select an account to write the cost of the stock off to, then click on Process again."), 1, 0);
140                 exit;
141         }
142         if (!isset($_POST['WriteOffGLCode']))
143                 $_POST['WriteOffGLCode'] = "";
144         if (!isset($_POST['ShipperID']))
145                 $_POST['ShipperID'] = 0;
146         $credit_no = add_credit_note($_SESSION['credit_items'], $_POST['OrderDate'],
147                 $_POST['CreditType'], $_POST['tax_group_id'],
148                 $_POST['ChargeFreightCost'], $_POST['sales_type_id'], $_POST['ShipperID'],
149                 $_POST['ref'], $_POST['CreditText'], $_POST['WriteOffGLCode']);
150
151         unset($_SESSION['credit_items']->line_items);
152         unset($_SESSION['credit_items']);
153
154         meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
155
156 } /*end of process credit note */
157
158 //---------------------------------------------------------------------------------------------------
159
160 function get_details_from_customer()
161 {
162         return get_customer_details_to_order($_SESSION['credit_items'], $_POST['customer_id'], $_POST['branch_id']);
163 }
164
165 //-----------------------------------------------------------------------------------------------
166
167 function check_item_data()
168 {
169         if ($_POST['qty'] <= 0)
170         {
171                 display_error(_("The quantity must be greater than zero."));
172                 return false;
173         }
174         if (!is_numeric($_POST['price']) || $_POST['price'] < 0)
175         {
176                 display_error(_("The entered price is negative or invalid."));
177                 return false;
178         }
179         if (!is_numeric($_POST['Disc']) || $_POST['Disc'] > 100 || $_POST['Disc'] < 0)
180         {
181                 display_error(_("The entered discount percent is negative, greater than 100 or invalid."));
182                 return false;
183         }
184         return true;
185 }
186
187 //-----------------------------------------------------------------------------------------------
188
189 function handle_update_item()
190 {
191     if($_POST['UpdateItem'] != "" && check_item_data())
192     {
193         $_SESSION['credit_items']->update_cart_item($_POST['stock_id'], $_POST['qty'], 
194                 $_POST['price'], ($_POST['Disc'] / 100));
195     }
196 }
197
198 //-----------------------------------------------------------------------------------------------
199
200 function handle_delete_item()
201 {
202         $_SESSION['credit_items']->remove_from_cart($_GET['Delete']);
203 }
204
205 //-----------------------------------------------------------------------------------------------
206
207 function handle_new_item()
208 {
209         if (!check_item_data())
210                 return;
211
212         add_to_order($_SESSION['credit_items'], $_POST['stock_id'], $_POST['qty'],
213                 $_POST['price'], $_POST['Disc'] / 100);
214 }
215
216 //-----------------------------------------------------------------------------------------------
217 if (isset($_GET['Delete']) || isset($_GET['Edit']))
218         copy_from_cn();
219
220 if (isset($_GET['Delete']))
221         handle_delete_item();
222
223 if (isset($_POST['AddItem']) || isset($_POST['UpdateItem']))
224         copy_to_cn();
225
226 if (isset($_POST['AddItem']))
227         handle_new_item();
228
229 if (isset($_POST['UpdateItem']))
230         handle_update_item();
231
232 //-----------------------------------------------------------------------------------------------
233
234 if (isset($_GET['NewCredit']) || !isset($_SESSION['credit_items']))
235 {
236         handle_new_order();
237
238 else 
239 {
240         if (!isset($_POST['customer_id']))
241                 $_POST['customer_id'] = $_SESSION['credit_items']->customer_id;
242         if (!isset($_POST['branch_id']))
243                 $_POST['branch_id'] = $_SESSION['credit_items']->Branch;
244 }
245
246 //-----------------------------------------------------------------------------------------------
247
248 start_form(false, true);
249
250 $customer_error = display_credit_header($_SESSION['credit_items']);
251
252 if ($customer_error == "")
253 {
254         start_table("$table_style width=80%", 10);
255         echo "<tr><td>";
256     display_credit_items(_("Credit Note Items"), $_SESSION['credit_items']);
257     credit_options_controls();
258     echo "</td></tr>";
259     end_table();
260
261 else
262 {
263         display_error($customer_error);
264 }
265
266 if (!isset($_POST['ProcessCredit']))
267 {
268     echo "<br><center><table><tr>";
269     submit_cells('Update', _("Update"));
270         if ($_SESSION['credit_items']->count_items() >= 1
271                         /*OR $_POST['ChargeTax'] > 0*/ || (isset($_POST['ChargeFreightCost']) && $_POST['ChargeFreightCost'] > 0))
272         {
273             submit_cells('ProcessCredit', _("Process Credit Note"));
274         }
275         echo "</tr></table>";
276 }
277
278 end_form();
279 end_page();
280
281 ?>