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