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