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