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