Moving 2.0 development version to main trunk.
[fa-stable.git] / sales / credit_note_entry.php
1 <?php
2 //---------------------------------------------------------------------------
3 //
4 //      Entry/Modify free hand Credit Note
5 //
6 $page_security = 3;
7 $path_to_root="..";
8 include_once($path_to_root . "/sales/includes/cart_class.inc");
9 include_once($path_to_root . "/includes/session.inc");
10 include_once($path_to_root . "/includes/data_checks.inc");
11 include_once($path_to_root . "/sales/includes/sales_db.inc");
12 include_once($path_to_root . "/sales/includes/sales_ui.inc");
13 include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
14 include_once($path_to_root . "/sales/includes/ui/sales_credit_ui.inc");
15 include_once($path_to_root . "/sales/includes/ui/sales_order_ui.inc");
16
17 $js = "";
18 if ($use_popup_windows) {
19         $js .= get_js_open_window(900, 500);
20 }
21 if ($use_date_picker) {
22         $js .= get_js_date_picker();
23 }
24
25 if(isset($_GET['NewCredit'])) {
26         $_SESSION['page_title'] = _("Customer Credit Note");
27         handle_new_credit(0);
28 } elseif (isset($_GET['ModifyCredit'])) {
29         $_SESSION['page_title'] = sprintf(_("Modifying Customer Credit Note #%d"), $_GET['ModifyCredit']);
30         handle_new_credit($_GET['ModifyCredit']);
31         $help_page_title = _("Modifying Customer Credit Note");
32 }
33
34 page($_SESSION['page_title'],false, false, "", $js);
35
36 //-----------------------------------------------------------------------------
37
38 check_db_has_stock_items(_("There are no items defined in the system."));
39
40 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
41
42 //-----------------------------------------------------------------------------
43
44 if (isset($_GET['AddedID'])) {
45         $credit_no = $_GET['AddedID'];
46         $trans_type = 11;
47
48         display_notification_centered(sprintf(_("Credit Note # %d has been processed"),$credit_no));
49
50         display_note(get_customer_trans_view_str($trans_type, $credit_no, _("View this credit note")), 0, 1);
51
52         display_note(get_gl_view_str($trans_type, $credit_no, _("View the GL Journal Entries for this Credit Note")));
53
54         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Credit Note"), "NewCredit=yes");
55
56         display_footer_exit();
57 }
58 //--------------------------------------------------------------------------------
59
60 function line_start_focus() {
61   global $Ajax;
62   $Ajax->activate('items_table');
63   set_focus('_stock_id_edit');
64 }
65
66 //-----------------------------------------------------------------------------
67
68 function copy_to_cn()
69 {
70         $_SESSION['Items']->Comments = $_POST['CreditText'];
71         $_SESSION['Items']->document_date = $_POST['OrderDate'];
72         $_SESSION['Items']->freight_cost = input_num('ChargeFreightCost');
73         $_SESSION['Items']->Location = $_POST["Location"];
74         $_SESSION['Items']->sales_type = $_POST['sales_type_id'];
75         $_SESSION['Items']->reference = $_POST['ref'];
76         $_SESSION['Items']->ship_via = $_POST['ShipperID'];
77 }
78
79 //-----------------------------------------------------------------------------
80
81 function copy_from_cn()
82 {
83         $_POST['CreditText'] = $_SESSION['Items']->Comments;
84         $_POST['OrderDate'] = $_SESSION['Items']->document_date;
85         $_POST['ChargeFreightCost'] = price_format($_SESSION['Items']->freight_cost);
86         $_POST['Location'] = $_SESSION['Items']->Location;
87         $_POST['sales_type_id'] = $_SESSION['Items']->sales_type;
88         $_POST['ref'] = $_SESSION['Items']->reference;
89         $_POST['ShipperID'] = $_SESSION['Items']->ship_via;
90 }
91
92 //-----------------------------------------------------------------------------
93
94 function handle_new_credit($trans_no)
95 {
96         processing_start();
97         $_SESSION['Items'] = new Cart(11,$trans_no);
98         copy_from_cn();
99     line_start_focus();
100 }
101
102 //-----------------------------------------------------------------------------
103
104 function can_process()
105 {
106
107         $input_error = 0;
108
109         if ($_SESSION['Items']->count_items() == 0 && (!check_num('ChargeFreightCost',0)))
110                 return false;
111         if($_SESSION['Items']->trans_no == 0) {
112             if (!references::is_valid($_POST['ref'])) {
113                 display_error( _("You must enter a reference."));
114                 set_focus('ref');
115                 $input_error = 1;
116             } elseif (!is_new_reference($_POST['ref'], 11))     {
117                 display_error( _("The entered reference is already in use."));
118                 set_focus('ref');
119                 $input_error = 1;
120             }
121         }
122         if (!is_date($_POST['OrderDate'])) {
123                 display_error(_("The entered date for the credit note is invalid."));
124                 set_focus('OrderDate');
125                 $input_error = 1;
126         } elseif (!is_date_in_fiscalyear($_POST['OrderDate'])) {
127                 display_error(_("The entered date is not in fiscal year."));
128                 set_focus('OrderDate');
129                 $input_error = 1;
130         }
131         return ($input_error == 0);
132 }
133
134 //-----------------------------------------------------------------------------
135
136 if (isset($_POST['ProcessCredit']) && can_process()) {
137         if ($_POST['CreditType'] == "WriteOff" && (!isset($_POST['WriteOffGLCode']) ||
138                 $_POST['WriteOffGLCode'] == '')) {
139                 display_note(_("For credit notes created to write off the stock, a general ledger account is required to be selected."), 1, 0);
140                 display_note(_("Please select an account to write the cost of the stock off to, then click on Process again."), 1, 0);
141                 exit;
142                 
143         }
144         if (!isset($_POST['WriteOffGLCode'])) {
145                 $_POST['WriteOffGLCode'] = 0;
146         }
147         $credit_no = $_SESSION['Items']->write($_POST['WriteOffGLCode']);
148         processing_end();
149         meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
150
151 } /*end of process credit note */
152
153   //-----------------------------------------------------------------------------
154
155 function check_item_data()
156 {
157         if (!check_num('qty',0)) {
158                 display_error(_("The quantity must be greater than zero."));
159                 set_focus('qty');
160                 return false;
161         }
162         if (!check_num('price',0)) {
163                 display_error(_("The entered price is negative or invalid."));
164                 set_focus('price');
165                 return false;
166         }
167         if (!check_num('Disc', 0, 100)) {
168                 display_error(_("The entered discount percent is negative, greater than 100 or invalid."));
169                 set_focus('Disc');
170                 return false;
171         }
172         return true;
173 }
174
175 //-----------------------------------------------------------------------------
176
177 function handle_update_item()
178 {
179         if ($_POST['UpdateItem'] != "" && check_item_data()) {
180                 $_SESSION['Items']->update_cart_item($_POST['line_no'], input_num('qty'),
181                         input_num('price'), input_num('Disc') / 100);
182         }
183     line_start_focus();
184 }
185
186 //-----------------------------------------------------------------------------
187
188 function handle_delete_item($line_no)
189 {
190         $_SESSION['Items']->remove_from_cart($line_no);
191     line_start_focus();
192 }
193
194 //-----------------------------------------------------------------------------
195
196 function handle_new_item()
197 {
198
199         if (!check_item_data())
200                 return;
201
202         add_to_order($_SESSION['Items'], $_POST['stock_id'], input_num('qty'),
203                 input_num('price'), input_num('Disc') / 100);
204     line_start_focus();
205 }
206 //-----------------------------------------------------------------------------
207 $id = find_submit('Delete');
208 if ($id!=-1)
209         handle_delete_item($id);
210
211 if (isset($_POST['AddItem']))
212         handle_new_item();
213
214 if (isset($_POST['UpdateItem']))
215         handle_update_item();
216
217 if (isset($_POST['CancelItemChanges']))
218         line_start_focus();
219
220 //-----------------------------------------------------------------------------
221
222 if (!processing_active()) {
223         handle_new_credit();
224 }
225
226 //-----------------------------------------------------------------------------
227
228 start_form(false, true);
229
230 $customer_error = display_credit_header($_SESSION['Items']);
231
232 if ($customer_error == "") {
233         start_table("$table_style width=80%", 10);
234         echo "<tr><td>";
235         display_credit_items(_("Credit Note Items"), $_SESSION['Items']);
236         credit_options_controls();
237         echo "</td></tr>";
238         end_table();
239 } else {
240         display_error($customer_error);
241 }
242
243 echo "<br><center><table><tr>";
244 submit_cells('Update', _("Update"));
245 submit_cells('ProcessCredit', _("Process Credit Note"));
246 echo "</tr></table></center>";
247
248 end_form();
249 end_page();
250
251 ?>