3d0a97239f8d17048477bce42f83ceada78067ca
[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 $table_style2, $Ajax;
20
21         $qes = has_quick_entries(QE_JOURNAL);
22         $new = $Order->order_id==0;
23         start_outer_table("$table_style2 width=90%");
24         table_section(1);
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, "50%");
45                 start_row();
46                 quick_entries_list_cells(_("Quick Entry").":", 'person_id', null, QE_JOURNAL, true);
47                 $qid = get_quick_entry(get_post('person_id'));
48                 if (list_updated('person_id')) {
49                         unset($_POST['totamount']); // enable default
50                 $Ajax->activate('totamount');
51                 }
52                 amount_cells($qid['base_desc'].":", 'totamount', price_format($qid['base_amount']),
53                  null, "&nbsp;&nbsp;".submit('go', _("Go"), false, false, true));
54                 end_row();
55
56         }
57                 
58         end_outer_table(1);
59 }
60
61 //---------------------------------------------------------------------------------
62
63 function display_gl_items($title, &$order)
64 {
65         global $table_style, $path_to_root;
66
67         display_heading($title);
68
69         $dim = get_company_pref('use_dimension');
70
71     div_start('items_table');
72         start_table("$table_style colspan=7 width=95%");
73         if ($dim == 2)
74                 $th = array(_("Account Code"), _("Account Description"), _("Dimension")." 1",
75                         _("Dimension")." 2", _("Debit"), _("Credit"), _("Memo"), "");
76         else if ($dim == 1)
77                 $th = array(_("Account Code"), _("Account Description"), _("Dimension"),
78                         _("Debit"), _("Credit"), _("Memo"), "");
79         else
80                 $th = array(_("Account Code"), _("Account Description"),
81                         _("Debit"), _("Credit"), _("Memo"), "");
82
83         if (count($order->gl_items)) $th[] = '';
84
85         table_header($th);      
86
87         $k = 0;
88
89         $id = find_submit('Edit');
90         foreach ($order->gl_items as $line => $item) 
91         {
92                 if ($id != $line)
93                 {
94                 alt_table_row_color($k);
95
96                         label_cells($item->code_id, $item->description);
97                 if ($dim >= 1)
98                                 label_cell(get_dimension_string($item->dimension_id, true));
99                 if ($dim > 1)
100                                 label_cell(get_dimension_string($item->dimension2_id, true));
101                 if ($item->amount > 0)
102                 {
103                         amount_cell(abs($item->amount));
104                         label_cell("");
105                 }       
106                 else
107                 {
108                         label_cell("");
109                         amount_cell(abs($item->amount));
110                 }       
111                         label_cell($item->reference);
112
113                         edit_button_cell("Edit$line", _("Edit"),
114                                 _('Edit journal line'));
115                         delete_button_cell("Delete$line", _("Delete"),
116                                 _('Remove line from journal'));
117                 end_row();
118                 } 
119                 else 
120                 {
121                         gl_edit_item_controls($order, $dim, $line);
122                 }
123         }
124
125         if ($id == -1)
126                 gl_edit_item_controls($order, $dim);
127
128         if ($order->count_gl_items()) 
129         {
130                 $colspan = ($dim == 2 ? "4" : ($dim == 1 ? "3" : "2"));
131                 start_row();
132                 label_cell(_("Total"), "align=right colspan=" . $colspan);
133                 amount_cell($order->gl_items_total_debit());
134                 amount_cell(abs($order->gl_items_total_credit()));
135                 label_cell('', "colspan=3");
136                 end_row();
137         }
138
139     end_table();
140         div_end();
141 }
142
143 //---------------------------------------------------------------------------------
144
145 function gl_edit_item_controls(&$order, $dim, $Index=null)
146 {
147         global $Ajax;
148         start_row();
149
150         $id = find_submit('Edit');
151         if ($Index != -1 && $Index == $id)
152         {
153             // Modifying an existing row
154                 $item = $order->gl_items[$Index];
155                 $_POST['code_id'] = $item->code_id;
156                 $_POST['dimension_id'] = $item->dimension_id;
157                 $_POST['dimension2_id'] = $item->dimension2_id;
158                 if ($item->amount > 0)
159                 {
160                         $_POST['AmountDebit'] = price_format($item->amount);
161                         $_POST['AmountCredit'] = "";
162                 }
163                 else
164                 {
165                         $_POST['AmountDebit'] = "";
166                         $_POST['AmountCredit'] = price_format(abs($item->amount));
167                 }       
168                 $_POST['description'] = $item->description;
169                 $_POST['LineMemo'] = $item->reference;
170
171                 hidden('Index', $id);
172                 $skip_bank = !$_SESSION["wa_current_user"]->can_access('SA_BANKJOURNAL');
173                 echo gl_all_accounts_list('code_id', null, $skip_bank, true);
174                 if ($dim >= 1) 
175                         dimensions_list_cells(null, 'dimension_id', null, true, " ", false, 1);
176                 if ($dim > 1) 
177                         dimensions_list_cells(null, 'dimension2_id', null, true, " ", false, 2);
178             $Ajax->activate('items_table');
179         }
180         else
181         {
182             // Adding a new row
183                 $_POST['AmountDebit'] = '';  //price_format(0);
184                 $_POST['AmountCredit'] = ''; //price_format(0);
185                 $_POST['dimension_id'] = 0;
186                 $_POST['dimension2_id'] = 0;
187                 $_POST['LineMemo'] = "";
188                 $_POST['_code_id_edit'] = "";
189                 $_POST['code_id'] = "";
190                 if(isset($_POST['_code_id_update'])) {
191                             $Ajax->activate('code_id');
192                 }
193                 
194                 $skip_bank = !$_SESSION["wa_current_user"]->can_access('SA_BANKJOURNAL');
195                 echo gl_all_accounts_list('code_id', null, $skip_bank, true);
196                 if ($dim >= 1)
197                         dimensions_list_cells(null, 'dimension_id', null, true, " ", false, 1);
198                 if ($dim > 1)
199                         dimensions_list_cells(null, 'dimension2_id', null, true, " ", false, 2);
200         }
201         if ($dim < 1)
202                 hidden('dimension_id', 0);
203         if ($dim < 2)
204                 hidden('dimension2_id', 0);
205
206         small_amount_cells(null, 'AmountDebit');
207         small_amount_cells(null, 'AmountCredit');
208         text_cells_ex(null, 'LineMemo', 35, 255);
209
210         if ($id != -1)
211         {
212                 button_cell('UpdateItem', _("Update"),
213                                 _('Confirm changes'), ICON_UPDATE);
214                 button_cell('CancelItemChanges', _("Cancel"),
215                                 _('Cancel changes'), ICON_CANCEL);
216                 set_focus('amount');
217         } 
218         else 
219                 submit_cells('AddItem', _("Add Item"), "colspan=2",
220                     _('Add new line to journal'), true);
221
222         end_row();
223 }
224
225 //---------------------------------------------------------------------------------
226
227 function gl_options_controls()
228 {
229           echo "<br><table align='center'>";
230
231           textarea_row(_("Memo"), 'memo_', null, 50, 3);
232
233           echo "</table>";
234 }
235
236
237 //---------------------------------------------------------------------------------
238
239 ?>