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