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