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