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