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