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