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