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