Final rewriting of sales module
[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
60 function copy_to_cn()
61 {
62         $_SESSION['Items']->Comments = $_POST['CreditText'];
63         $_SESSION['Items']->document_date = $_POST['OrderDate'];
64         $_SESSION['Items']->freight_cost = $_POST['ChargeFreightCost'];
65         $_SESSION['Items']->Location = $_POST["Location"];
66         $_SESSION['Items']->default_sales_type = $_POST['sales_type_id'];
67         $_SESSION['Items']->reference = $_POST['ref'];
68         $_SESSION['Items']->ship_via = $_POST['ShipperID'];
69 }
70
71 //-----------------------------------------------------------------------------
72
73 function copy_from_cn()
74 {
75         $_POST['CreditText'] = $_SESSION['Items']->Comments;
76         $_POST['OrderDate'] = $_SESSION['Items']->document_date;
77         $_POST['ChargeFreightCost'] = $_SESSION['Items']->freight_cost;
78         $_POST['Location'] = $_SESSION['Items']->Location;
79         $_POST['sales_type_id'] = $_SESSION['Items']->default_sales_type;
80         $_POST['ref'] = $_SESSION['Items']->reference;
81         $_POST['ShipperID'] = $_SESSION['Items']->ship_via;
82 }
83
84 //-----------------------------------------------------------------------------
85
86 function handle_new_credit($trans_no)
87 {
88         processing_start();
89         $_SESSION['Items'] = new Cart(11,$trans_no);
90         copy_from_cn();
91 }
92
93 //-----------------------------------------------------------------------------
94
95 function can_process()
96 {
97
98         $input_error = 0;
99
100         if ($_SESSION['Items']->count_items() == 0
101                 && (!isset($_POST['ChargeFreightCost']) || $_POST['ChargeFreightCost'] <= 0))
102                 return false;
103
104         if (!references::is_valid($_POST['ref'])) {
105                 display_error( _("You must enter a reference."));
106                 $input_error = 1;
107         } elseif (!is_new_reference($_POST['ref'], 11)) {
108                 display_error( _("The entered reference is already in use."));
109                 $input_error = 1;
110         } elseif (!is_date($_POST['OrderDate'])) {
111                 display_error(_("The entered date for the credit note is invalid."));
112                 $input_error = 1;
113         } elseif (!is_date_in_fiscalyear($_POST['OrderDate'])) {
114                 display_error(_("The entered date is not in fiscal year."));
115                 $input_error = 1;
116         }
117         return ($input_error == 0);
118 }
119
120 //-----------------------------------------------------------------------------
121
122 if (isset($_POST['ProcessCredit']) && can_process()) {
123         if ($_POST['CreditType'] == "WriteOff" && (!isset($_POST['WriteOffGLCode']) ||
124                 $_POST['WriteOffGLCode'] == '')) {
125                 display_note(_("For credit notes created to write off the stock, a general ledger account is required to be selected."), 1, 0);
126                 display_note(_("Please select an account to write the cost of the stock off to, then click on Process again."), 1, 0);
127                 exit;
128         }
129         if (!isset($_POST['WriteOffGLCode'])) {
130                 $_POST['WriteOffGLCode'] = 0;
131         }
132         $credit_no = $_SESSION['Items']->write($_POST['WriteOffGLCode']);
133         processing_end();
134         meta_forward($_SERVER['PHP_SELF'], "AddedID=$credit_no");
135
136 } /*end of process credit note */
137
138   //-----------------------------------------------------------------------------
139
140 function check_item_data()
141 {
142         if ($_POST['qty'] <= 0) {
143                 display_error(_("The quantity must be greater than zero."));
144                 return false;
145         }
146         if (!is_numeric($_POST['price']) || $_POST['price'] < 0) {
147                 display_error(_("The entered price is negative or invalid."));
148                 return false;
149         }
150         if (!is_numeric($_POST['Disc']) || $_POST['Disc'] > 100 || $_POST['Disc'] < 0) {
151                 display_error(_("The entered discount percent is negative, greater than 100 or invalid."));
152                 return false;
153         }
154         return true;
155 }
156
157 //-----------------------------------------------------------------------------
158
159 function handle_update_item()
160 {
161         if ($_POST['UpdateItem'] != "" && check_item_data()) {
162                 $_SESSION['Items']->update_cart_item($_POST['line_no'], $_POST['qty'],
163                         $_POST['price'], ($_POST['Disc'] / 100));
164         }
165 }
166
167 //-----------------------------------------------------------------------------
168
169 function handle_delete_item()
170 {
171         $_SESSION['Items']->remove_from_cart($_GET['Delete']);
172 }
173
174 //-----------------------------------------------------------------------------
175
176 function handle_new_item()
177 {
178
179         if (!check_item_data())
180                 return;
181
182         add_to_order($_SESSION['Items'], $_POST['stock_id'], $_POST['qty'],
183                 $_POST['price'], $_POST['Disc'] / 100);
184 }
185 //-----------------------------------------------------------------------------
186 if (isset($_GET['Delete']) || isset($_GET['Edit']))
187         copy_from_cn();
188
189 if (isset($_GET['Delete']))
190         handle_delete_item();
191
192 if (isset($_POST['AddItem']) || isset($_POST['UpdateItem']))
193         copy_to_cn();
194
195 if (isset($_POST['AddItem']))
196         handle_new_item();
197
198 if (isset($_POST['UpdateItem']))
199         handle_update_item();
200
201 //-----------------------------------------------------------------------------
202
203 if (!processing_active()) {
204         handle_new_credit();
205 } else {
206         if (!isset($_POST['customer_id']))
207                 $_POST['customer_id'] = $_SESSION['Items']->customer_id;
208         if (!isset($_POST['branch_id']))
209                 $_POST['branch_id'] = $_SESSION['Items']->Branch;
210 }
211
212 //-----------------------------------------------------------------------------
213
214 start_form(false, true);
215
216 $customer_error = display_credit_header($_SESSION['Items']);
217
218 if ($customer_error == "") {
219         start_table("$table_style width=80%", 10);
220         echo "<tr><td>";
221         display_credit_items(_("Credit Note Items"), $_SESSION['Items']);
222         credit_options_controls();
223         echo "</td></tr>";
224         end_table();
225 } else {
226         display_error($customer_error);
227 }
228
229 echo "<br><center><table><tr>";
230 submit_cells('Update', _("Update"));
231 submit_cells('ProcessCredit', _("Process Credit Note"));
232 echo "</tr></table>";
233
234 end_form();
235 end_page();
236
237 ?>