Merged all main trunk bugfixes up to release 2.0.5
[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 //----------------------------------------------------------------------------------------
33 if ($ret = context_restore()) {
34         if(isset($ret['supplier_id']))
35                 $_POST['supplier_id'] = $ret['supplier_id'];
36 }
37 if (isset($_POST['_supplier_id_editor'])) {
38         context_call($path_to_root.'/purchasing/manage/suppliers.php?supplier_id='.$_POST['supplier_id'], 
39                 array( 'supplier_id', 'bank_account', 'DatePaid', 'ref', 'amount', 
40                         'discount', 'memo_') );
41 }
42 if (isset($_POST['_DatePaid_changed'])) {
43   $Ajax->activate('_ex_rate');
44 }
45 //----------------------------------------------------------------------------------------
46
47 if (isset($_GET['AddedID'])) 
48 {
49         $payment_id = $_GET['AddedID'];
50
51         display_notification_centered( _("Payment has been sucessfully entered"));
52
53     display_note(get_gl_view_str(22, $payment_id, _("View the GL &Journal Entries for this Payment")));
54
55     hyperlink_params($path_to_root . "/purchasing/allocations/supplier_allocate.php", _("&Allocate this Payment"), "trans_no=$payment_id&trans_type=22");
56
57         hyperlink_params($_SERVER['PHP_SELF'], _("Enter another supplier &payment"), "supplier_id=" . $_POST['supplier_id']);
58
59         display_footer_exit();
60 }
61
62 //----------------------------------------------------------------------------------------
63
64 function display_controls()
65 {
66         global $table_style2;
67         start_form(false, true);
68
69         if (!isset($_POST['supplier_id']))
70                 $_POST['supplier_id'] = get_global_supplier(false);
71         if (!isset($_POST['DatePaid']))
72         {
73                 $_POST['DatePaid'] = Today();
74                 if (!is_date_in_fiscalyear($_POST['DatePaid']))
75                         $_POST['DatePaid'] = end_fiscalyear();
76         }               
77         start_table($table_style2, 5, 7);
78         echo "<tr><td valign=top>"; // outer table
79
80         echo "<table>";
81
82     bank_accounts_list_row(_("From Bank Account:"), 'bank_account', null, true);
83
84         amount_row(_("Amount of Payment:"), 'amount');
85         amount_row(_("Amount of Discount:"), 'discount');
86
87     date_row(_("Date Paid") . ":", 'DatePaid', '', null, 0, 0, 0, null, true);
88
89         echo "</table>";
90         echo "</td><td valign=top class='tableseparator'>"; // outer table
91         echo "<table>";
92
93     supplier_list_row(_("Payment To:"), 'supplier_id', null, false, true);
94
95         set_global_supplier($_POST['supplier_id']);
96
97         $supplier_currency = get_supplier_currency($_POST['supplier_id']);
98         $bank_currency = get_bank_account_currency($_POST['bank_account']);
99         if ($bank_currency != $supplier_currency) 
100         {
101                 exchange_rate_display($bank_currency, $supplier_currency, $_POST['DatePaid'], true);
102         }
103
104     ref_row(_("Reference:"), 'ref', '', references::get_next(22));
105
106     text_row(_("Memo:"), 'memo_', null, 52,50);
107
108         echo "</table>";
109
110         echo "</td></tr>";
111         end_table(1); // outer table
112
113         submit_center('ProcessSuppPayment',_("Enter Payment"), true, '', true);
114
115         if ($bank_currency != $supplier_currency) 
116         {
117                 display_note(_("The amount and discount are in the bank account's currency."), 2, 0);
118         }
119
120         end_form();
121 }
122
123 //----------------------------------------------------------------------------------------
124
125 function check_inputs()
126 {
127         if ($_POST['amount'] == "") 
128         {
129                 $_POST['amount'] = price_format(0);
130         }
131
132         if (!check_num('amount', 0))
133         {
134                 display_error(_("The entered amount is invalid or less than zero."));
135                 set_focus('amount');
136                 return false;
137         }
138
139         if (isset($_POST['_ex_rate']) && !check_num('_ex_rate', 0.000001))
140         {
141                 display_error(_("The exchange rate must be numeric and greater than zero."));
142                 set_focus('_ex_rate');
143                 return false;
144         }
145
146         if ($_POST['discount'] == "") 
147         {
148                 $_POST['discount'] = 0;
149         }
150
151         if (!check_num('discount', 0))
152         {
153                 display_error(_("The entered discount is invalid or less than zero."));
154                 set_focus('amount');
155                 return false;
156         }
157
158         if (input_num('amount') - input_num('discount') <= 0) 
159         {
160                 display_error(_("The total of the amount and the discount is zero or negative. Please enter positive values."));
161                 set_focus('amount');
162                 return false;
163         }
164
165         if (!is_date($_POST['DatePaid']))
166         {
167                 display_error(_("The entered date is invalid."));
168                 set_focus('DatePaid');
169                 return false;
170         } 
171         elseif (!is_date_in_fiscalyear($_POST['DatePaid'])) 
172         {
173                 display_error(_("The entered date is not in fiscal year."));
174                 set_focus('DatePaid');
175                 return false;
176         }
177     if (!references::is_valid($_POST['ref'])) 
178     {
179                 display_error(_("You must enter a reference."));
180                 set_focus('ref');
181                 return false;
182         }
183
184         if (!is_new_reference($_POST['ref'], 22)) 
185         {
186                 display_error(_("The entered reference is already in use."));
187                 set_focus('ref');
188                 return false;
189         }
190
191         return true;
192 }
193
194 //----------------------------------------------------------------------------------------
195
196 function handle_add_payment()
197 {
198         $supp_currency = get_supplier_currency($_POST['supplier_id']);
199         $bank_currency = get_bank_account_currency($_POST['bank_account']);
200         $comp_currency = get_company_currency();
201         if ($comp_currency != $bank_currency && $bank_currency != $supp_currency)
202                 $rate = 0;
203         else
204                 $rate = input_num('_ex_rate');
205
206         $payment_id = add_supp_payment($_POST['supplier_id'], $_POST['DatePaid'],
207                 $_POST['bank_account'], input_num('amount'), input_num('discount'), 
208                 $_POST['ref'], $_POST['memo_'], $rate);
209
210         //unset($_POST['supplier_id']);
211         unset($_POST['bank_account']);
212         unset($_POST['DatePaid']);
213         unset($_POST['currency']);
214         unset($_POST['memo_']);
215         unset($_POST['amount']);
216         unset($_POST['discount']);
217         unset($_POST['ProcessSuppPayment']);
218
219         meta_forward($_SERVER['PHP_SELF'], "AddedID=$payment_id&supplier_id=".$_POST['supplier_id']);
220 }
221
222 //----------------------------------------------------------------------------------------
223
224 if (isset($_POST['ProcessSuppPayment']))
225 {
226          /*First off  check for valid inputs */
227     if (check_inputs() == true) 
228     {
229         handle_add_payment();
230         end_page();
231         exit;
232     }
233 }
234
235 display_controls();
236
237 end_page();
238 ?>