Fixed numeric fields to accept user native number format.
[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 check_db_has_bank_trans_types(_("There are no bank payment types defined in the system."));
28
29 //----------------------------------------------------------------------------------------
30
31 if (isset($_GET['AddedID'])) {
32         $payment_no = $_GET['AddedID'];
33
34         display_notification_centered(_("The customer payment has been successfully entered."));
35
36         display_note(get_gl_view_str(12, $payment_no, _("View the GL Journal Entries for this Customer Payment")));
37
38         hyperlink_params($path_to_root . "/sales/allocations/customer_allocate.php", _("Allocate this Customer Payment"), "trans_no=$payment_no&trans_type=12");
39
40         hyperlink_no_params($path_to_root . "/sales/customer_payments.php", _("Enter Another Customer Payment"));
41         br(1);
42         end_page();
43         exit;
44 }
45
46 //----------------------------------------------------------------------------------------------
47
48 function can_process()
49 {
50         if (!isset($_POST['DateBanked']) || !is_date($_POST['DateBanked'])) {
51                 display_error(_("The entered date is invalid. Please enter a valid date for the payment."));
52                 return false;
53         } elseif (!is_date_in_fiscalyear($_POST['DateBanked'])) {
54                 display_error(_("The entered date is not in fiscal year."));
55                 return false;
56         }
57
58         if (!references::is_valid($_POST['ref'])) {
59                 display_error(_("You must enter a reference."));
60                 return false;
61         }
62
63         if (!is_new_reference($_POST['ref'], 12)) {
64                 display_error(_("The entered reference is already in use."));
65                 return false;
66         }
67
68         if (!check_num('amount', 0)) {
69                 display_error(_("The entered amount is invalid or negative and cannot be processed."));
70                 return false;
71         }
72
73         if (!check_num('discount')) {
74                 display_error(_("The entered discount is not a valid number."));
75                 return false;
76         }
77
78         if ((input_num('amount') - input_num('discount') <= 0)) {
79                 display_error(_("The balance of the amount and discout is zero or negative. Please enter valid amounts."));
80                 return false;
81         }
82
83         return true;
84 }
85
86 //----------------------------------------------------------------------------------------------
87
88 // validate inputs
89 if (isset($_POST['AddPaymentItem'])) {
90
91         if (!can_process()) {
92                 unset($_POST['AddPaymentItem']);
93         }
94 }
95
96 //----------------------------------------------------------------------------------------------
97
98 if (isset($_POST['AddPaymentItem'])) {
99         $payment_no = write_customer_payment(0, $_POST['customer_id'], $_POST['BranchID'],
100                 $_POST['bank_account'], $_POST['DateBanked'], $_POST['ReceiptType'], $_POST['ref'],
101                 input_num('amount'), input_num('discount'), $_POST['memo_']);
102
103         meta_forward($_SERVER['PHP_SELF'], "AddedID=$payment_no");
104 }
105
106 //----------------------------------------------------------------------------------------------
107
108 function read_customer_data()
109 {
110         $sql = "SELECT ".TB_PREF."debtors_master.pymt_discount,
111                 ".TB_PREF."credit_status.dissallow_invoices
112                 FROM ".TB_PREF."debtors_master, ".TB_PREF."credit_status
113                 WHERE ".TB_PREF."debtors_master.credit_status = ".TB_PREF."credit_status.id
114                         AND ".TB_PREF."debtors_master.debtor_no = '" . $_POST['customer_id'] . "'";
115
116         $result = db_query($sql, "could not query customers");
117
118         $myrow = db_fetch($result);
119
120         $_POST['HoldAccount'] = $myrow["dissallow_invoices"];
121         $_POST['pymt_discount'] = $myrow["pymt_discount"];
122         $_POST['ref'] = references::get_next(12);
123 }
124
125 //-------------------------------------------------------------------------------------------------
126
127 function display_item_form()
128 {
129         global $table_style2;
130         start_table($table_style2, 5, 7);
131         echo "<tr><td valign=top>"; // outer table
132
133         echo "<table>";
134
135         if (!isset($_POST['customer_id']))
136                 $_POST['customer_id'] = get_global_customer(false);
137         if (!isset($_POST['DateBanked'])) {
138                 $_POST['DateBanked'] = Today();
139                 if (!is_date_in_fiscalyear($_POST['DateBanked'])) {
140                         $_POST['DateBanked'] = end_fiscalyear();
141                 }
142         }
143         customer_list_row(_("From Customer:"), 'customer_id', null, false, true);
144
145         if (db_customer_has_branches($_POST['customer_id'])) {
146                 customer_branches_list_row(_("Branch:"), $_POST['customer_id'], 'BranchID', null, false, true, true);
147         } else {
148                 hidden('BranchID', reserved_words::get_any_numeric());
149         }
150
151         read_customer_data();
152
153         set_global_customer($_POST['customer_id']);
154
155         if (isset($_POST['HoldAccount']) && $_POST['HoldAccount'] != 0) {
156                 echo "</table></table>";
157                 display_note(_("This customer account is on hold."), 0, 0, "class='redfb'");
158         } else {
159                 $display_discount_percent = percent_format($_POST['pymt_discount']*100) . "%";
160
161                 amount_row(_("Amount:"), 'amount');
162
163                 amount_row(_("Amount of Discount:"), 'discount');
164
165                 label_row(_("Customer prompt payment discount :"), $display_discount_percent);
166
167                 date_row(_("Date of Deposit:"), 'DateBanked');
168
169                 echo "</table>";
170                 echo "</td><td valign=top class='tableseparator'>"; // outer table
171                 echo "<table>";
172
173                 bank_accounts_list_row(_("Into Bank Account:"), 'bank_account', null, true);
174
175                 $cust_currency = get_customer_currency($_POST['customer_id']);
176                 $bank_currency = get_bank_account_currency($_POST['bank_account']);
177
178                 if ($cust_currency != $bank_currency) {
179                         exchange_rate_display($cust_currency, $bank_currency, $_POST['DateBanked']);
180                 }
181
182                 bank_trans_types_list_row(_("Type:"), 'ReceiptType', null);
183
184                 text_row(_("Reference:"), 'ref', null, 20, 40);
185
186                 textarea_row(_("Memo:"), 'memo_', null, 22, 4);
187
188                 echo "</table>";
189
190                 echo "</td></tr>";
191                 end_table(); // outer table
192
193                 if ($cust_currency != $bank_currency)
194                         display_note(_("Amount and discount are in customer's currency."));
195
196                 echo"<br>";
197
198                 submit_center('AddPaymentItem', _("Add Payment"));
199         }
200
201         echo "<br>";
202 }
203
204 //----------------------------------------------------------------------------------------------
205
206 start_form();
207
208 display_item_form();
209
210 end_form();
211 end_page();
212 ?>