Merged changes form stable branch up to 2.3.13
[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, $Refs;
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', '');
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         }
51     payment_person_types_list_row( $payment ? _("Pay To:"):_("From:"),
52                  'PayType', $_POST['PayType'], true);
53     switch ($_POST['PayType'])
54     {
55                 case PT_MISC :
56                 text_row_ex($payment ?_("To the Order of:"):_("Name:"),
57                                  'person_id', 40, 50);
58                 break;
59                 //case PT_WORKORDER :
60         //      workorders_list_row(_("Work Order:"), 'person_id', null);
61         //      break;
62                 case PT_SUPPLIER :
63                 supplier_list_row(_("Supplier:"), 'person_id', null, false, true, false, true);
64                 break;
65                 case PT_CUSTOMER :
66                 customer_list_row(_("Customer:"), 'person_id', null, false, true, false, true);
67
68                 if (db_customer_has_branches($_POST['person_id']))
69                 {
70                         customer_branches_list_row(_("Branch:"), $_POST['person_id'], 
71                                         'PersonDetailID', null, false, true, true, true);
72                 }
73                 else
74                 {
75                                 $_POST['PersonDetailID'] = ANY_NUMERIC;
76                         hidden('PersonDetailID');
77                 }
78                 $trans = get_customer_habit($_POST['person_id']); // take care of customers on hold
79                 if ($trans['dissallow_invoices'] != 0)
80                 {
81                         if ($payment)
82                         {
83                                 $customer_error = true;
84                                         display_error(_("This customer account is on hold."));
85                         }
86                         else
87                                         display_warning(_("This customer account is on hold."));
88                 }
89                 break;
90
91                 case PT_QUICKENTRY :
92                         quick_entries_list_row(_("Type").":", 'person_id', null, ($payment ? QE_PAYMENT : QE_DEPOSIT), true);
93                         $qid = get_quick_entry(get_post('person_id'));
94                         if (list_updated('person_id')) {
95                                 unset($_POST['totamount']); // enable default
96                                 $Ajax->activate('totamount');
97                         }
98                         amount_row($qid['base_desc'].":", 'totamount', price_format($qid['base_amount']),
99                                  null, "&nbsp;&nbsp;".submit('go', _("Go"), false, false, true));
100                         break;  
101                 //case payment_person_types::Project() :
102         //      dimensions_list_row(_("Dimension:"), 'person_id', $_POST['person_id'], false, null, true);
103         //      break;
104     }
105
106         table_section(3, "33%");
107
108         if (!$order->order_id && !list_updated('bank_account'))
109         {
110                 if ($_POST['PayType'] == PT_CUSTOMER)
111                         $_POST['bank_account'] = get_default_customer_bank_account($_POST['person_id']);
112                 elseif ($_POST['PayType'] == PT_SUPPLIER)       
113                         $_POST['bank_account'] = get_default_supplier_bank_account($_POST['person_id']);
114                 else
115                         unset($_POST['bank_account']);
116         }               
117     bank_accounts_list_row( $payment ? _("From:") : _("To:"), 'bank_account', null, true);
118     if ($payment)
119                 bank_balance_row($_POST['bank_account']);
120         
121         $person_currency = payment_person_currency($_POST['PayType'], $_POST['person_id']);
122         $bank_currency = get_bank_account_currency($_POST['bank_account']);
123
124         exchange_rate_display($bank_currency, $person_currency, $_POST['date_']);
125
126         end_outer_table(1); // outer table
127
128         div_end();
129         if ($customer_error)
130         {
131                 end_form();
132                 end_page();
133                 exit;
134         }
135 }
136 //---------------------------------------------------------------------------------
137
138 function display_gl_items($title, &$order)
139 {
140         global $path_to_root;
141
142         $dim = get_company_pref('use_dimension');
143         $colspan = ($dim == 2 ? 4 : ($dim == 1 ? 3 : 2));
144         display_heading($title);
145
146     div_start('items_table');
147         start_table(TABLESTYLE, "colspan=7 width=95%");
148
149         if ($dim == 2)
150                 $th = array(_("Account Code"), _("Account Description"), _("Dimension")." 1",
151                         _("Dimension")." 2", _("Amount"), _("Memo"), "");
152         else if ($dim == 1)
153                 $th = array(_("Account Code"), _("Account Description"), _("Dimension"),
154                         _("Amount"), _("Memo"), "");
155         else
156                 $th = array(_("Account Code"), _("Account Description"),
157                         _("Amount"), _("Memo"), "");
158
159         if (count($order->gl_items)) $th[] = '';
160
161         table_header($th);
162         $k = 0;  //row colour counter
163
164         $id = find_submit('Edit');
165         foreach ($order->gl_items as $line => $item)
166         {
167                 if ($id != $line)
168                 {
169                 alt_table_row_color($k);
170
171                         label_cell($item->code_id);
172                         label_cell($item->description);
173                 if ($dim >= 1)
174                                 label_cell(get_dimension_string($item->dimension_id, true));
175                 if ($dim > 1)
176                                 label_cell(get_dimension_string($item->dimension2_id, true));
177                         //amount_cell(abs($item->amount));
178                         if ($order->trans_type == ST_BANKDEPOSIT)
179                                 amount_cell(-$item->amount);
180                         else            
181                                 amount_cell($item->amount);
182                         label_cell($item->reference);
183
184                         edit_button_cell("Edit$line", _("Edit"),
185                                 _('Edit document line'));
186                         delete_button_cell("Delete$line", _("Delete"),
187                                 _('Remove line from document'));
188                 end_row();
189                 }
190                 else
191                 {
192                         gl_edit_item_controls($order, $dim, $line);
193                 }
194         }
195
196         if ($id == -1)
197                 gl_edit_item_controls($order, $dim);
198
199         if ($order->count_gl_items())
200                 label_row(_("Total"), number_format2(abs($order->gl_items_total()), user_price_dec()),"colspan=" . $colspan . " align=right", "align=right",3);
201
202     end_table();
203         div_end();
204 }
205
206 //---------------------------------------------------------------------------------
207
208 function gl_edit_item_controls(&$order, $dim, $Index=null)
209 {
210         global $Ajax;
211         $payment = $order->trans_type == ST_BANKPAYMENT;
212
213         start_row();
214         $id = find_submit('Edit');
215         if ($Index != -1 && $Index == $id)
216         {
217                 $item = $order->gl_items[$Index];
218                 $_POST['code_id'] = $item->code_id;
219                 $_POST['dimension_id'] = $item->dimension_id;
220                 $_POST['dimension2_id'] = $item->dimension2_id;
221                 $_POST['amount'] = price_format(abs($item->amount));
222                 $_POST['description'] = $item->description;
223                 $_POST['LineMemo'] = $item->reference;
224
225                 hidden('Index', $id);
226                 echo gl_all_accounts_list('code_id', null, true, true);
227                 if ($dim >= 1)
228                         dimensions_list_cells(null, 'dimension_id', null, true, " ", false, 1);
229                 if ($dim > 1)
230                         dimensions_list_cells(null, 'dimension2_id', null, true, " ", false, 2);
231             $Ajax->activate('items_table');
232         }
233         else
234         {
235                 $_POST['amount'] = price_format(0);
236                 $_POST['dimension_id'] = 0;
237                 $_POST['dimension2_id'] = 0;
238                 //$_POST['LineMemo'] = ""; // let memo go to next line Joe Hunt 2010-05-30
239                 if(isset($_POST['_code_id_update'])) {
240                             $Ajax->activate('code_id');
241                 }
242
243                 if ($_POST['PayType'] == PT_CUSTOMER)
244                 {
245                         $acc = get_branch_accounts($_POST['PersonDetailID']);
246                         $_POST['code_id'] = $acc['receivables_account'];
247                 }
248                 elseif ($_POST['PayType'] == PT_SUPPLIER)
249                 {
250                         $acc = get_supplier_accounts($_POST['person_id']);
251                         $_POST['code_id'] = $acc['payable_account'];
252                 }
253                 //elseif ($_POST['PayType'] == PT_WORKORDER)
254                 //      $_POST['code_id'] = get_company_pref('default_assembly_act');
255                 else {
256                         $_POST['code_id'] =
257                                 get_company_pref($payment ? 'default_cogs_act':'default_inv_sales_act');
258                 }
259                 echo gl_all_accounts_list('code_id', null, true, true);
260                 if ($dim >= 1)
261                         dimensions_list_cells(null, 'dimension_id', null, true, " ", false, 1);
262                 if ($dim > 1)
263                         dimensions_list_cells(null, 'dimension2_id', null, true, " ", false, 2);
264         }
265         if ($dim < 1)
266                 hidden('dimension_id', 0);
267         if ($dim < 2)
268                 hidden('dimension2_id', 0);
269
270         amount_cells(null, 'amount');
271         text_cells_ex(null, 'LineMemo', 35, 255);
272
273         if ($id != -1)
274         {
275                 button_cell('UpdateItem', _("Update"),
276                                 _('Confirm changes'), ICON_UPDATE);
277                 button_cell('CancelItemChanges', _("Cancel"),
278                                 _('Cancel changes'), ICON_CANCEL);
279                 set_focus('amount');
280         }
281         else
282         {
283                 submit_cells('AddItem', _("Add Item"), "colspan=2",
284                     _('Add new item to document'), true);
285         }
286
287         end_row();
288 }
289
290
291 //---------------------------------------------------------------------------------
292
293 function gl_options_controls()
294 {
295         echo "<br><table align='center'>";
296
297         textarea_row(_("Memo"), 'memo_', null, 50, 3);
298
299         echo "</table>";
300 }
301
302
303 //---------------------------------------------------------------------------------
304
305 ?>