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