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