Mysqli errors: Trying to access array offset on value of type bool. Fixed. Please...
[fa-stable.git] / gl / includes / ui / gl_bank_ui.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 function display_bank_header(&$order)
13 {
14         global $Ajax;
15         $payment = $order->trans_type == ST_BANKPAYMENT;
16
17         $customer_error = false;
18         div_start('pmt_header');
19
20         start_outer_table(TABLESTYLE2, "width='90%'"); // outer table
21
22         table_section(1);
23         
24     date_row(_("Date:"), 'date_', '', true, 0, 0, 0, null, true);
25
26         ref_row(_("Reference:"), 'ref', '', $order->reference, false, $order->trans_type, get_post('date_'));
27
28         table_section(2, "33%");
29
30         if (!isset($_POST['PayType']))
31         {
32                 if (isset($_GET['PayType']))
33                         $_POST['PayType'] = $_GET['PayType'];
34                 else
35                         $_POST['PayType'] = "";
36         }
37         if (!isset($_POST['person_id']))
38         {
39                 if (isset($_GET['PayPerson']))
40                         $_POST['person_id'] = $_GET['PayPerson'];
41                 else
42                         $_POST['person_id'] = "";
43         }
44         if (isset($_POST['_PayType_update'])) {
45                 $_POST['person_id'] = '';
46                 $Ajax->activate('pmt_header');
47                 $Ajax->activate('code_id');
48                 $Ajax->activate('pagehelp');
49                 $Ajax->activate('editors');
50                 $Ajax->activate('footer');
51         }
52     payment_person_types_list_row( $payment ? _("Pay To:"):_("From:"),
53                  'PayType', $_POST['PayType'], true);
54     switch ($_POST['PayType'])
55     {
56                 case PT_MISC :
57                 text_row_ex($payment ?_("To the Order of:"):_("Name:"),
58                                  'person_id', 40, 50);
59                 break;
60                 case PT_SUPPLIER :
61                 supplier_list_row(_("Supplier:"), 'person_id', null, false, true, false, true);
62                 break;
63                 case PT_CUSTOMER :
64                 customer_list_row(_("Customer:"), 'person_id', null, false, true, false, true);
65
66                 if (db_customer_has_branches($_POST['person_id']))
67                 {
68                         customer_branches_list_row(_("Branch:"), $_POST['person_id'], 
69                                         'PersonDetailID', null, false, true, true, true);
70                 }
71                 else
72                 {
73                                 $_POST['PersonDetailID'] = ANY_NUMERIC;
74                         hidden('PersonDetailID');
75                 }
76                 $trans = get_customer_habit($_POST['person_id']); // take care of customers on hold
77                 if (is_array($trans) && $trans['dissallow_invoices'] != 0)
78                 {
79                         if ($payment)
80                         {
81                                 $customer_error = true;
82                                         display_error(_("This customer account is on hold."));
83                         }
84                         else
85                                         display_warning(_("This customer account is on hold."));
86                 }
87                 break;
88
89                 case PT_QUICKENTRY :
90                         quick_entries_list_row(_("Type").":", 'person_id', null, ($payment ? QE_PAYMENT : QE_DEPOSIT), true);
91                         $qid = get_quick_entry(get_post('person_id'));
92                         if (list_updated('person_id')) {
93                                 unset($_POST['totamount']); // enable default
94                                 $Ajax->activate('footer');
95                                 $Ajax->activate('totamount');
96                         }
97                         amount_row($qid['base_desc'].":", 'totamount', price_format($qid['base_amount']),
98                                  null, "&nbsp;&nbsp;".submit('go', _("Go"), false, false, true));
99                         break;  
100     }
101
102         table_section(3, "33%");
103
104         if (!$order->order_id && !get_post('bank_account'))
105         {
106                 if ($_POST['PayType'] == PT_CUSTOMER)
107                         $_POST['bank_account'] = get_default_customer_bank_account($_POST['person_id']);
108                 elseif ($_POST['PayType'] == PT_SUPPLIER)       
109                         $_POST['bank_account'] = get_default_supplier_bank_account($_POST['person_id']);
110                 else
111                         unset($_POST['bank_account']);
112         }               
113     
114     bank_accounts_list_row( $payment ? _("From:") : _("Into:"), 'bank_account', null, true);
115     if ($payment)
116                 bank_balance_row($_POST['bank_account']);
117
118         $bank_currency = get_bank_account_currency($_POST['bank_account']);
119
120         exchange_rate_display(get_company_currency(), $bank_currency, $_POST['date_']);
121
122         end_outer_table(1); // outer table
123
124         div_end();
125         if ($customer_error)
126         {
127                 end_form();
128                 end_page();
129                 exit;
130         }
131 }
132 //---------------------------------------------------------------------------------
133
134 function display_gl_items($title, &$order)
135 {
136         global $path_to_root;
137
138         $dim = get_company_pref('use_dimension');
139         $colspan = ($dim == 2 ? 4 : ($dim == 1 ? 3 : 2));
140         display_heading($title);
141
142     div_start('items_table');
143         start_table(TABLESTYLE, "width='95%'");
144
145         if ($dim == 2)
146                 $th = array(_("Account Code"), _("Account Description"), _("Dimension")." 1",
147                         _("Dimension")." 2", _("Amount"), _("Memo"), "");
148         elseif ($dim == 1)
149                 $th = array(_("Account Code"), _("Account Description"), _("Dimension"),
150                         _("Amount"), _("Memo"), "");
151         else
152                 $th = array(_("Account Code"), _("Account Description"),
153                         _("Amount"), _("Memo"), "");
154
155         if (count($order->gl_items)) $th[] = '';
156
157         table_header($th);
158         $k = 0;  //row colour counter
159
160         $id = find_submit('Edit');
161         foreach ($order->gl_items as $line => $item)
162         {
163                 if ($id != $line)
164                 {
165                 alt_table_row_color($k);
166
167                         label_cell($item->code_id);
168                         label_cell($item->description);
169                 if ($dim >= 1)
170                                 label_cell(get_dimension_string($item->dimension_id, true));
171                 if ($dim > 1)
172                                 label_cell(get_dimension_string($item->dimension2_id, true));
173                         if ($order->trans_type == ST_BANKDEPOSIT)
174                                 amount_cell(-$item->amount);
175                         else            
176                                 amount_cell($item->amount);
177                         label_cell($item->reference);
178
179                         edit_button_cell("Edit$line", _("Edit"),
180                                 _('Edit document line'));
181                         delete_button_cell("Delete$line", _("Delete"),
182                                 _('Remove line from document'));
183                 end_row();
184                 }
185                 else
186                 {
187                         gl_edit_item_controls($order, $dim, $line);
188                 }
189         }
190
191         if ($id == -1)
192                 gl_edit_item_controls($order, $dim);
193
194         if ($order->count_gl_items())
195                 label_row(_("Total"), number_format2(abs($order->gl_items_total()), user_price_dec()),"colspan=" . $colspan . " align=right", "align=right",3);
196
197     end_table();
198         div_end();
199 }
200
201 //---------------------------------------------------------------------------------
202
203 function gl_edit_item_controls(&$order, $dim, $Index=null)
204 {
205         global $Ajax;
206         $payment = $order->trans_type == ST_BANKPAYMENT;
207
208         start_row();
209         $id = find_submit('Edit');
210         if ($Index != -1 && $Index == $id)
211         {
212                 $item = $order->gl_items[$Index];
213                 $_POST['code_id'] = $item->code_id;
214                 $_POST['dimension_id'] = $item->dimension_id;
215                 $_POST['dimension2_id'] = $item->dimension2_id;
216                 $_POST['amount'] = price_format(abs($item->amount));
217                 $_POST['description'] = $item->description;
218                 $_POST['LineMemo'] = $item->reference;
219
220                 hidden('Index', $id);
221             $Ajax->activate('items_table');
222         }
223         else
224         {
225                 if(isset($_POST['_code_id_update'])) {
226                             $Ajax->activate('code_id');
227                 }
228                 if (list_updated('PayType') || list_updated('person_id'))
229                 {
230                         if ($_POST['PayType'] == PT_CUSTOMER)
231                         {
232                                 $acc = get_branch_accounts($_POST['PersonDetailID']);
233                                 $_POST['code_id'] = is_array($acc) ? $acc['receivables_account'] : '';
234                         }
235                         elseif ($_POST['PayType'] == PT_SUPPLIER)
236                         {
237                                 $acc = get_supplier_accounts($_POST['person_id']);
238                                 $_POST['code_id'] = is_array($acc) ? $acc['payable_account'] : '';
239                                 $_POST['dimension_id'] = is_array($acc) ? $acc['dimension_id'] : '';
240                                 $_POST['dimension2_id'] = is_array($acc) ? $acc['dimension2_id'] : '';
241                         }
242                         else {
243                                 $_POST['code_id'] =
244                                         get_company_pref($payment ? 'default_cogs_act':'default_inv_sales_act');
245                         }
246                         $Ajax->activate('dimension_id');
247                         $Ajax->activate('dimension2_id');
248                 }
249         }
250         echo gl_all_accounts_list('code_id', null, true, true);
251         if ($dim >= 1)
252                 dimensions_list_cells(null, 'dimension_id', null, true, " ", false, 1);
253         else
254                 hidden('dimension_id', 0);
255         if ($dim > 1)
256                 dimensions_list_cells(null, 'dimension2_id', null, true, " ", false, 2);
257         else
258                 hidden('dimension2_id', 0);
259
260         amount_cells(null, 'amount');
261         text_cells_ex(null, 'LineMemo', 35, 255);
262
263         if ($id != -1)
264         {
265                 button_cell('UpdateItem', _("Update"),
266                                 _('Confirm changes'), ICON_UPDATE);
267                 button_cell('CancelItemChanges', _("Cancel"),
268                                 _('Cancel changes'), ICON_CANCEL);
269                 set_focus('amount');
270         }
271         else
272         {
273                 submit_cells('AddItem', _("Add Item"), "colspan=2",
274                     _('Add new item to document'), true);
275         }
276
277         end_row();
278 }
279
280
281 //---------------------------------------------------------------------------------
282
283 function gl_options_controls($order)
284 {
285         div_start('footer');
286         echo "<br><table align='center'>";
287
288         $type = get_post('PayType');
289         $bank_curr = get_bank_account_currency(get_post('bank_account'));
290         $person_curr = $type == PT_CUSTOMER ? get_customer_currency(get_post('person_id')) 
291                 : ($type == PT_SUPPLIER ? get_supplier_currency(get_post('person_id')) : $bank_curr);
292
293         if ($person_curr != $bank_curr)
294         {
295                 $_POST['settled_amount'] =
296                         price_format(abs($order->gl_items_total() / get_exchange_rate_from_to($bank_curr, $person_curr, get_post('date_'))));
297                 amount_row($type == PT_CUSTOMER ? _("Settled AR Amount:") :  _("Settled AP Amount:"),
298                          'settled_amount', null, null, $person_curr, user_price_dec());
299         }
300         textarea_row(_("Memo"), 'memo_', null, 50, 3);
301
302         echo "</table>";
303         div_end();
304 }
305
306
307 //---------------------------------------------------------------------------------
308