Merged all main trunk bugfixes up to release 2.0.5
[fa-stable.git] / sales / customer_payments.php
1 <?php
2
3 $path_to_root="..";
4 $page_security = 3;
5 include_once($path_to_root . "/includes/session.inc");
6 include_once($path_to_root . "/includes/date_functions.inc");
7 include_once($path_to_root . "/includes/ui.inc");
8 include_once($path_to_root . "/includes/banking.inc");
9 include_once($path_to_root . "/includes/data_checks.inc");
10 include_once($path_to_root . "/sales/includes/sales_db.inc");
11
12 $js = "";
13 if ($use_popup_windows) {
14         $js .= get_js_open_window(900, 500);
15 }
16 if ($use_date_picker) {
17         $js .= get_js_date_picker();
18 }
19 page(_("Customer Payment Entry"), false, false, "", $js);
20
21 //----------------------------------------------------------------------------------------------
22
23 check_db_has_customers(_("There are no customers defined in the system."));
24
25 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
26
27 //----------------------------------------------------------------------------------------
28 if ($ret = context_restore()) {
29         if(isset($ret['customer_id']))
30                 $_POST['customer_id'] = $ret['customer_id'];
31         if(isset($ret['branch_id']))
32                 $_POST['BranchID'] = $ret['branch_id'];
33 }
34 if (isset($_POST['_customer_id_editor'])) {
35         context_call($path_to_root.'/sales/manage/customers.php?debtor_no='.$_POST['customer_id'], 
36                 array( 'customer_id', 'BranchID', 'bank_account', 'DateBanked', 
37                         'ref', 'amount', 'discount', 'memo_') );
38 }
39
40 if (isset($_GET['AddedID'])) {
41         $payment_no = $_GET['AddedID'];
42
43         display_notification_centered(_("The customer payment has been successfully entered."));
44
45         display_note(get_gl_view_str(12, $payment_no, _("&View the GL Journal Entries for this Customer Payment")));
46
47         hyperlink_params($path_to_root . "/sales/allocations/customer_allocate.php", _("&Allocate this Customer Payment"), "trans_no=$payment_no&trans_type=12");
48
49         hyperlink_no_params($path_to_root . "/sales/customer_payments.php", _("Enter Another &Customer Payment"));
50         br(1);
51         end_page();
52         exit;
53 }
54
55 //----------------------------------------------------------------------------------------------
56
57 function can_process()
58 {
59         if (!isset($_POST['DateBanked']) || !is_date($_POST['DateBanked'])) {
60                 display_error(_("The entered date is invalid. Please enter a valid date for the payment."));
61                 set_focus('DateBanked');
62                 return false;
63         } elseif (!is_date_in_fiscalyear($_POST['DateBanked'])) {
64                 display_error(_("The entered date is not in fiscal year."));
65                 set_focus('DateBanked');
66                 return false;
67         }
68
69         if (!references::is_valid($_POST['ref'])) {
70                 display_error(_("You must enter a reference."));
71                 set_focus('ref');
72                 return false;
73         }
74
75         if (!is_new_reference($_POST['ref'], 12)) {
76                 display_error(_("The entered reference is already in use."));
77                 set_focus('ref');
78                 return false;
79         }
80
81         if (!check_num('amount', 0)) {
82                 display_error(_("The entered amount is invalid or negative and cannot be processed."));
83                 set_focus('amount');
84                 return false;
85         }
86
87         if (isset($_POST['_ex_rate']) && !check_num('_ex_rate', 0.000001))
88         {
89                 display_error(_("The exchange rate must be numeric and greater than zero."));
90                 set_focus('_ex_rate');
91                 return false;
92         }
93
94         if ($_POST['discount'] == "") 
95         {
96                 $_POST['discount'] = 0;
97         }
98
99         if (!check_num('discount')) {
100                 display_error(_("The entered discount is not a valid number."));
101                 set_focus('discount');
102                 return false;
103         }
104
105         if ((input_num('amount') - input_num('discount') <= 0)) {
106                 display_error(_("The balance of the amount and discout is zero or negative. Please enter valid amounts."));
107                 set_focus('discount');
108                 return false;
109         }
110
111         return true;
112 }
113
114 //----------------------------------------------------------------------------------------------
115
116 // validate inputs
117 if (isset($_POST['AddPaymentItem'])) {
118
119         if (!can_process()) {
120                 unset($_POST['AddPaymentItem']);
121         }
122 }
123 if (isset($_POST['_customer_id_button'])) {
124 //      unset($_POST['branch_id']);
125         $Ajax->activate('BranchID');
126 }
127 if (isset($_POST['_DateBanked_changed'])) {
128   $Ajax->activate('_ex_rate');
129 }
130 //----------------------------------------------------------------------------------------------
131
132 if (isset($_POST['AddPaymentItem'])) {
133         
134         $cust_currency = get_customer_currency($_POST['customer_id']);
135         $bank_currency = get_bank_account_currency($_POST['bank_account']);
136         $comp_currency = get_company_currency();
137         if ($comp_currency != $bank_currency && $bank_currency != $cust_currency)
138                 $rate = 0;
139         else
140                 $rate = input_num('_ex_rate');
141
142         $payment_no = write_customer_payment(0, $_POST['customer_id'], $_POST['BranchID'],
143                 $_POST['bank_account'], $_POST['DateBanked'], $_POST['ref'],
144                 input_num('amount'), input_num('discount'), $_POST['memo_'], $rate);
145         meta_forward($_SERVER['PHP_SELF'], "AddedID=$payment_no");
146 }
147
148 //----------------------------------------------------------------------------------------------
149
150 function read_customer_data()
151 {
152         $sql = "SELECT ".TB_PREF."debtors_master.pymt_discount,
153                 ".TB_PREF."credit_status.dissallow_invoices
154                 FROM ".TB_PREF."debtors_master, ".TB_PREF."credit_status
155                 WHERE ".TB_PREF."debtors_master.credit_status = ".TB_PREF."credit_status.id
156                         AND ".TB_PREF."debtors_master.debtor_no = '" . $_POST['customer_id'] . "'";
157
158         $result = db_query($sql, "could not query customers");
159
160         $myrow = db_fetch($result);
161
162         $_POST['HoldAccount'] = $myrow["dissallow_invoices"];
163         $_POST['pymt_discount'] = $myrow["pymt_discount"];
164         $_POST['ref'] = references::get_next(12);
165 }
166
167 //-------------------------------------------------------------------------------------------------
168
169 function display_item_form()
170 {
171         global $table_style2;
172         start_table($table_style2, 5, 7);
173         echo "<tr><td valign=top>"; // outer table
174
175         echo "<table>";
176
177         if (!isset($_POST['customer_id']))
178                 $_POST['customer_id'] = get_global_customer(false);
179         if (!isset($_POST['DateBanked'])) {
180                 $_POST['DateBanked'] = Today();
181                 if (!is_date_in_fiscalyear($_POST['DateBanked'])) {
182                         $_POST['DateBanked'] = end_fiscalyear();
183                 }
184         }
185         customer_list_row(_("From Customer:"), 'customer_id', null, false, true);
186         if (db_customer_has_branches($_POST['customer_id'])) {
187                 customer_branches_list_row(_("Branch:"), $_POST['customer_id'], 'BranchID', null, false, true, true);
188         } else {
189                 hidden('BranchID', reserved_words::get_any_numeric());
190         }
191
192         read_customer_data();
193
194         set_global_customer($_POST['customer_id']);
195         if (isset($_POST['HoldAccount']) && $_POST['HoldAccount'] != 0) {
196                 echo "</table></table>";
197                 display_note(_("This customer account is on hold."), 0, 0, "class='redfb'");
198         } else {
199                 $display_discount_percent = percent_format($_POST['pymt_discount']*100) . "%";
200
201                 amount_row(_("Amount:"), 'amount');
202
203                 amount_row(_("Amount of Discount:"), 'discount');
204
205                 label_row(_("Customer prompt payment discount :"), $display_discount_percent);
206
207                 date_row(_("Date of Deposit:"), 'DateBanked','',null, 0, 0, 0, null, true);
208
209                 echo "</table>";
210                 echo "</td><td valign=top class='tableseparator'>"; // outer table
211                 echo "<table>";
212
213                 bank_accounts_list_row(_("Into Bank Account:"), 'bank_account', null, true);
214
215                 $cust_currency = get_customer_currency($_POST['customer_id']);
216                 $bank_currency = get_bank_account_currency($_POST['bank_account']);
217
218                 if ($cust_currency != $bank_currency) {
219                         exchange_rate_display($bank_currency, $cust_currency, $_POST['DateBanked'], true);
220                 }
221
222                 text_row(_("Reference:"), 'ref', null, 20, 40);
223
224                 textarea_row(_("Memo:"), 'memo_', null, 22, 4);
225
226                 echo "</table>";
227
228                 echo "</td></tr>";
229                 end_table(); // outer table
230
231                 if ($cust_currency != $bank_currency)
232                         display_note(_("Amount and discount are in customer's currency."));
233
234                 echo"<br>";
235
236                 submit_center('AddPaymentItem', _("Add Payment"), true, '', true);
237         }
238
239         echo "<br>";
240 }
241
242 //----------------------------------------------------------------------------------------------
243
244 start_form();
245
246 display_item_form();
247
248 end_form();
249 end_page();
250 ?>