Merged changes up to 2.3.16 into unstable
[fa-stable.git] / gl / includes / ui / gl_journal_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 include_once($path_to_root . "/includes/ui.inc");
13 include_once($path_to_root . "/includes/ui/items_cart.inc");
14
15 //--------------------------------------------------------------------------------
16
17 function display_order_header(&$Order)
18 {
19         global $Ajax;
20
21         $qes = has_quick_entries(QE_JOURNAL);
22         $new = $Order->order_id==0;
23         start_outer_table(TABLESTYLE2, "width=90%");
24         table_section(1, $qes ? "20%" : "");
25
26         start_row();
27     date_cells(_("Date:"), 'date_', '', $new);
28
29         table_section(2, $qes ? "20%" : "50%");
30         ref_cells(_("Reference:"), 'ref', '');
31         hidden('ref_original');
32         end_row();
33
34
35         if ($new) {
36                 table_section(3, "20%");
37                 start_row();
38                 check_cells(_("Reverse Transaction:"), 'Reverse', null);
39                 end_row();
40         }
41
42         if ($qes !== false)
43         {
44                 table_section(3, "40%");
45                 start_row();
46                 quick_entries_list_cells(_("Quick Entry").":", 'quick', null, QE_JOURNAL, true);
47                 $qid = get_quick_entry(get_post('quick'));
48                 if (list_updated('quick')) {
49                         unset($_POST['totamount']); // enable default
50                 $Ajax->activate('totamount');
51                 }
52                 if ($qid['bal_type'] == 1)
53                 {
54                         end_row();
55                         start_row();
56                         $accname = get_gl_account_name($qid['base_desc']);
57                         label_cell(($qid['base_amount'] == 0 ? _("Yearly") : _("Monthly")) . " ". _("balance from account")." ".
58                                 $qid['base_desc']." ".$accname."&nbsp;&nbsp;".submit('go', _("Go"), false, false, true), "colspan=2");
59                 }
60                 else
61                         amount_cells($qid['base_desc'].":", 'totamount', price_format($qid['base_amount']),
62                                 null, "&nbsp;&nbsp;".submit('go', _("Go"), false, false, true));
63                 end_row();
64
65         }
66
67         end_outer_table(1);
68 }
69
70 //---------------------------------------------------------------------------------
71
72 function display_gl_items($title, &$order)
73 {
74         global $path_to_root;
75
76         display_heading($title);
77
78         $dim = get_company_pref('use_dimension');
79
80         $sub_type = is_subledger_account(get_post('code_id'));
81         $has_subaccounts = $order->has_sub_accounts();
82
83     div_start('items_table');
84         start_table(TABLESTYLE, "colspan=7 width=95%");
85         if ($dim == 2)
86                 $th = array(_("Account Code"), _("Account Description"), _("Dimension")." 1",
87                         _("Dimension")." 2", _("Debit"), _("Credit"), _("Memo"), "");
88         else if ($dim == 1)
89                 $th = array(_("Account Code"), _("Account Description"), _("Dimension"),
90                         _("Debit"), _("Credit"), _("Memo"), "");
91         else
92                 $th = array(_("Account Code"), _("Account Description"),
93                         _("Debit"), _("Credit"), _("Memo"), "");
94
95         if (count($order->gl_items)) $th[] = '';
96         if ($sub_type || $has_subaccounts)
97                 array_insert($th, 2, _("Counterparty"));
98
99         table_header($th);
100
101         $k = 0;
102
103         $id = find_submit('Edit');
104         if ($id == -1) 
105                 $id = get_post('Index', -1);
106
107         foreach ($order->gl_items as $line => $item) 
108         {
109                 if ($id != $line)
110                 {
111                 alt_table_row_color($k);
112
113                         $acc = $item->code_id . ($item->person_id ? sprintf(' %05d', $item->person_id) : '');
114                         label_cells($acc, $item->description);
115
116                         label_cell($item->person_name);
117
118                 if ($dim >= 1)
119                                 label_cell(get_dimension_string($item->dimension_id, true));
120                 if ($dim > 1)
121                                 label_cell(get_dimension_string($item->dimension2_id, true));
122                 if ($item->amount > 0)
123                 {
124                         amount_cell(abs($item->amount));
125                         label_cell("");
126                 }
127                 else
128                 {
129                         label_cell("");
130                         amount_cell(abs($item->amount));
131                 }       
132                         label_cell($item->reference);
133
134                         edit_button_cell("Edit$line", _("Edit"),
135                                 _('Edit journal line'));
136                         delete_button_cell("Delete$line", _("Delete"),
137                                 _('Remove line from journal'));
138                 end_row();
139                 } 
140                 else 
141                 {
142                         gl_edit_item_controls($order, $dim, $item->person_id, $line);
143                 }
144         }
145
146         if ($id == -1)
147                 gl_edit_item_controls($order, $dim, $sub_type);
148
149         if ($order->count_gl_items()) 
150         {
151                 $colspan = ($dim == 2 ? "4" : ($dim == 1 ? "3" : "2"));
152                 start_row();
153                 label_cell(_("Total"), "align=right colspan=" . $colspan);
154                 amount_cell($order->gl_items_total_debit());
155                 amount_cell(abs($order->gl_items_total_credit()));
156                 label_cell('', "colspan=3");
157                 end_row();
158         }
159
160     end_table();
161         div_end();
162 }
163
164 //---------------------------------------------------------------------------------
165
166 function gl_edit_item_controls(&$order, $dim, $sub_accounts, $Index=null)
167 {
168         global $Ajax;
169         start_row();
170
171         $id = find_submit('Edit');
172         if ($id == -1) 
173                 $continue = $id = get_post('Index', -1);
174
175         if ($Index != -1 && $Index == $id)
176         {
177             // Modifying an existing row
178                 $item = $order->gl_items[$Index];
179                 if (!isset($continue))
180                 {
181                         $_POST['code_id'] = $item->code_id;
182                         $_POST['dimension_id'] = $item->dimension_id;
183                         $_POST['dimension2_id'] = $item->dimension2_id;
184                         $_POST['person_id'] = $item->person_id;
185                         if ($item->amount > 0)
186                         {
187                                 $_POST['AmountDebit'] = price_format($item->amount);
188                                 $_POST['AmountCredit'] = "";
189                         }
190                         else
191                         {
192                                 $_POST['AmountDebit'] = "";
193                                 $_POST['AmountCredit'] = price_format(abs($item->amount));
194                         }
195                         $_POST['description'] = $item->description;
196                         $_POST['LineMemo'] = $item->reference;
197                 }
198                 hidden('Index', $id);
199                 $skip_bank = !$_SESSION["wa_current_user"]->can_access('SA_BANKJOURNAL');
200                 echo gl_all_accounts_list('code_id', null, $skip_bank, true, _('[Select account]'), true, false, false);
201
202                 if (is_subledger_account(get_post('code_id')))
203                 {
204                         subledger_list_cells(null, 'person_id', get_post('code_id'));
205                 } elseif ($order->has_sub_accounts())
206                         label_cell('');
207                 if ($dim >= 1) 
208                         dimensions_list_cells(null, 'dimension_id', null, true, " ", false, 1);
209                 if ($dim > 1) 
210                         dimensions_list_cells(null, 'dimension2_id', null, true, " ", false, 2);
211             $Ajax->activate('items_table');
212         }
213         else
214         {
215
216                 if (!list_updated('code_id'))
217                 {
218                     // Adding a new row
219                         $_POST['AmountDebit'] = '';  //price_format(0);
220                         $_POST['AmountCredit'] = ''; //price_format(0);
221                         $_POST['dimension_id'] = 0;
222                         $_POST['dimension2_id'] = 0;
223                         //$_POST['LineMemo'] = ""; // let memo go to next line Joe Hunt 2010-05-30
224                         $_POST['_code_id_edit'] = "";
225                         $_POST['code_id'] = "";
226                 }
227                 if(isset($_POST['_code_id_update'])) {
228                     $Ajax->activate('code_id');
229                 }
230
231                 $skip_bank = !$_SESSION["wa_current_user"]->can_access('SA_BANKJOURNAL');
232                 echo gl_all_accounts_list('code_id', null, $skip_bank, true, _('[Select account]'), true, false, false);
233
234                 if ($sub_accounts)
235                 {
236                         subledger_list_cells(null, 'person_id', get_post('code_id'));
237                 }
238                 elseif ($order->has_sub_accounts())
239                         label_cell('');
240
241                 if ($dim >= 1)
242                         dimensions_list_cells(null, 'dimension_id', null, true, " ", false, 1);
243                 if ($dim > 1)
244                         dimensions_list_cells(null, 'dimension2_id', null, true, " ", false, 2);
245         }
246         if ($dim < 1)
247                 hidden('dimension_id', 0);
248         if ($dim < 2)
249                 hidden('dimension2_id', 0);
250
251         small_amount_cells(null, 'AmountDebit');
252         small_amount_cells(null, 'AmountCredit');
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                 submit_cells('AddItem', _("Add Item"), "colspan=2",
265                     _('Add new line to journal'), true);
266
267         end_row();
268 }
269
270 //---------------------------------------------------------------------------------
271
272 function gl_options_controls()
273 {
274           echo "<br><table align='center'>";
275
276           textarea_row(_("Memo"), 'memo_', null, 50, 3);
277
278           echo "</table>";
279 }