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