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