Fixed numeric fields to accept user native number format.
[fa-stable.git] / purchasing / supplier_payment.php
1 <?php
2
3 $path_to_root="..";
4 $page_security = 5;
5 include_once($path_to_root . "/includes/session.inc");
6
7 include_once($path_to_root . "/includes/date_functions.inc");
8 include_once($path_to_root . "/includes/ui.inc");
9 include_once($path_to_root . "/includes/banking.inc");
10 include_once($path_to_root . "/includes/data_checks.inc");
11
12 include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
13 $js = "";
14 if ($use_popup_windows)
15         $js .= get_js_open_window(900, 500);
16 if ($use_date_picker)
17         $js .= get_js_date_picker();
18 page(_("Supplier Payment Entry"), false, false, "", $js);
19
20
21 if (isset($_GET['supplier_id']))
22 {
23         $_POST['supplier_id'] = $_GET['supplier_id'];
24 }
25
26 //----------------------------------------------------------------------------------------
27
28 check_db_has_suppliers(_("There are no suppliers defined in the system."));
29
30 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
31
32 check_db_has_bank_trans_types(_("There are no bank payment types defined in the system."));
33
34 //----------------------------------------------------------------------------------------
35
36 if (isset($_GET['AddedID'])) 
37 {
38         $payment_id = $_GET['AddedID'];
39
40         display_notification_centered( _("Payment has been sucessfully entered"));
41
42     display_note(get_gl_view_str(22, $payment_id, _("View the GL Journal Entries for this Payment")));
43
44     hyperlink_params($path_to_root . "/purchasing/allocations/supplier_allocate.php", _("Allocate this Payment"), "trans_no=$payment_id&trans_type=22");
45
46         hyperlink_params($_SERVER['PHP_SELF'], _("Enter another supplier payment"), "supplier_id=" . $_POST['supplier_id']);
47
48         display_footer_exit();
49 }
50
51 //----------------------------------------------------------------------------------------
52
53 function display_controls()
54 {
55         global $table_style2;
56         start_form(false, true);
57
58         if (!isset($_POST['supplier_id']))
59                 $_POST['supplier_id'] = get_global_supplier(false);
60         if (!isset($_POST['DatePaid']))
61         {
62                 $_POST['DatePaid'] = Today();
63                 if (!is_date_in_fiscalyear($_POST['DatePaid']))
64                         $_POST['DatePaid'] = end_fiscalyear();
65         }               
66         start_table($table_style2, 5, 7);
67         echo "<tr><td valign=top>"; // outer table
68
69         echo "<table>";
70
71     bank_accounts_list_row(_("From Bank Account:"), 'bank_account', null, true);
72
73         amount_row(_("Amount of Payment:"), 'amount');
74         amount_row(_("Amount of Discount:"), 'discount');
75
76     date_row(_("Date Paid") . ":", 'DatePaid');
77
78         echo "</table>";
79         echo "</td><td valign=top class='tableseparator'>"; // outer table
80         echo "<table>";
81
82     supplier_list_row(_("Payment To:"), 'supplier_id', null, false, true);
83
84         set_global_supplier($_POST['supplier_id']);
85
86         $supplier_currency = get_supplier_currency($_POST['supplier_id']);
87         $bank_currency = get_bank_account_currency($_POST['bank_account']);
88         if ($bank_currency != $supplier_currency) 
89         {
90                 exchange_rate_display($bank_currency, $supplier_currency, $_POST['DatePaid']);
91         }
92
93         bank_trans_types_list_row(_("Payment Type:"), 'PaymentType', null);
94
95     ref_row(_("Reference:"), 'ref', references::get_next(22));
96
97     text_row(_("Memo:"), 'memo_', null, 52,50);
98
99         echo "</table>";
100
101         echo "</td></tr>";
102         end_table(1); // outer table
103
104         submit_center('ProcessSuppPayment',_("Enter Payment"));
105
106         if ($bank_currency != $supplier_currency) 
107         {
108                 display_note(_("The amount and discount are in the bank account's currency."), 2, 0);
109         }
110
111         end_form();
112 }
113
114 //----------------------------------------------------------------------------------------
115
116 function check_inputs()
117 {
118         if ($_POST['amount'] == "") 
119         {
120                 $_POST['amount'] = price_format(0);
121         }
122
123         if (!check_num('amount', 0))
124         {
125                 display_error(_("The entered amount is invalid or less than zero."));
126                 return false;
127         }
128
129         if ($_POST['discount'] == "") 
130         {
131                 $_POST['discount'] = 0;
132         }
133
134         if (!check_num('discount', 0))
135         {
136                 display_error(_("The entered discount is invalid or less than zero."));
137                 return false;
138         }
139
140         if (input_num('amount') - input_num('discount') <= 0) 
141         {
142                 display_error(_("The total of the amount and the discount negative. Please enter positive values."));
143                 return false;
144         }
145
146         if (!is_date($_POST['DatePaid']))
147         {
148                 display_error(_("The entered date is invalid."));
149                 return false;
150         } 
151         elseif (!is_date_in_fiscalyear($_POST['DatePaid'])) 
152         {
153                 display_error(_("The entered date is not in fiscal year."));
154                 return false;
155         }
156     if (!references::is_valid($_POST['ref'])) 
157     {
158                 display_error(_("You must enter a reference."));
159                 return false;
160         }
161
162         if (!is_new_reference($_POST['ref'], 22)) 
163         {
164                 display_error(_("The entered reference is already in use."));
165                 return false;
166         }
167
168         return true;
169 }
170
171 //----------------------------------------------------------------------------------------
172
173 function handle_add_payment()
174 {
175         $payment_id = add_supp_payment($_POST['supplier_id'], $_POST['DatePaid'],
176                 $_POST['PaymentType'], $_POST['bank_account'],
177                 input_num('amount'), input_num('discount'), $_POST['ref'], $_POST['memo_']);
178
179         //unset($_POST['supplier_id']);
180         unset($_POST['bank_account']);
181         unset($_POST['DatePaid']);
182         unset($_POST['PaymentType']);
183         unset($_POST['currency']);
184         unset($_POST['memo_']);
185         unset($_POST['amount']);
186         unset($_POST['discount']);
187         unset($_POST['ProcessSuppPayment']);
188
189         meta_forward($_SERVER['PHP_SELF'], "AddedID=$payment_id&supplier_id=".$_POST['supplier_id']);
190 }
191
192 //----------------------------------------------------------------------------------------
193
194 if (isset($_POST['ProcessSuppPayment']))
195 {
196          /*First off  check for valid inputs */
197     if (check_inputs() == true) 
198     {
199         handle_add_payment();
200         end_page();
201         exit;
202     }
203 }
204
205 display_controls();
206
207 end_page();
208 ?>