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