Added a Profit and Loss Drilldown page and changed menu links
[fa-stable.git] / gl / inquiry / journal_inquiry.php
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
13 $page_security = 'SA_GLANALYTIC';
14 $path_to_root="../..";
15
16 include($path_to_root . "/includes/db_pager.inc");
17 include_once($path_to_root . "/includes/session.inc");
18
19 include_once($path_to_root . "/includes/date_functions.inc");
20 include_once($path_to_root . "/includes/ui.inc");
21 $js = "";
22 if ($use_popup_windows)
23         $js .= get_js_open_window(800, 500);
24 if ($use_date_picker)
25         $js .= get_js_date_picker();
26
27 page(_($help_context = "Journal Inquiry"), false, false, "", $js);
28
29 //-----------------------------------------------------------------------------------
30 // Ajax updates
31 //
32 if (get_post('Search'))
33 {
34         $Ajax->activate('journal_tbl');
35 }
36 //--------------------------------------------------------------------------------------
37 if (!isset($_POST['filterType']))
38         $_POST['filterType'] = -1;
39
40 start_form();
41
42 start_table("class='tablestyle_noborder'");
43 start_row();
44
45 ref_cells(_("Reference:"), 'Ref', '',null, _('Enter reference fragment or leave empty'));
46
47 journal_types_list_cells(_("Type:"), "filterType");
48 date_cells(_("From:"), 'FromDate', '', null, 0, -1, 0);
49 date_cells(_("To:"), 'ToDate');
50
51 check_cells( _("Show closed:"), 'AlsoClosed', null);
52
53 submit_cells('Search', _("Search"), '', '', 'default');
54 end_row();
55 start_row();
56 ref_cells(_("Memo:"), 'Memo', '',null, _('Enter memo fragment or leave empty'));
57 end_row();
58 end_table();
59
60 function journal_pos($row)
61 {
62         return $row['gl_seq'] ? $row['gl_seq'] : '-';
63 }
64
65 function systype_name($dummy, $type)
66 {
67         global $systypes_array;
68         
69         return $systypes_array[$type];
70 }
71
72 function view_link($row) 
73 {
74         return get_trans_view_str($row["type"], $row["type_no"]);
75 }
76
77 function gl_link($row) 
78 {
79         return get_gl_view_str($row["type"], $row["type_no"]);
80 }
81
82 $editors = array(
83         0 => "/gl/gl_journal.php?ModifyGL=Yes&trans_no=%d&trans_type=%d",
84 //      1=> Bank Payment,
85 //      2=> Bank Deposit,
86 //      4=> Funds Transfer,
87    ST_SALESINVOICE => "/sales/customer_invoice.php?ModifyInvoice=%d",
88 //   11=>
89 // free hand (debtors_trans.order_==0)
90 //      "/sales/credit_note_entry.php?ModifyCredit=%d"
91 // credit invoice
92 //      "/sales/customer_credit_invoice.php?ModifyCredit=%d"
93 //       12=> Customer Payment,
94    ST_CUSTDELIVERY => "/sales/customer_delivery.php?ModifyDelivery=%d",
95 //   16=> Location Transfer,
96 //   17=> Inventory Adjustment,
97 //   20=> Supplier Invoice,
98 //   21=> Supplier Credit Note,
99 //   22=> Supplier Payment,
100 //   25=> Purchase Order Delivery,
101 //   28=> Work Order Issue,
102 //   29=> Work Order Production",
103 //   35=> Cost Update,
104 );
105
106 function edit_link($row)
107 {
108         global $editors;
109
110         return isset($editors[$row["type"]]) && !is_closed_trans($row["type"], $row["type_no"]) ? 
111                 pager_link(_("Edit"), 
112                         sprintf($editors[$row["type"]], $row["type_no"], $row["type"]),
113                         ICON_EDIT) : '';
114 }
115 /*
116 // Tom Hallman 11 Nov 2009
117 // IF(gl.type = 1... statement is for deposits/payments that may not actually result
118 // in a deposit, such as when a fix is made.  Without that statement (and the
119 // joining of the bank_trans table), the fix deposit/payment amount would show up 
120 // incorrectly as only the positive side of the fix.    
121 $sql = "SELECT  IF(ISNULL(a.gl_seq),0,a.gl_seq) as gl_seq,
122         gl.tran_date,
123         gl.type,
124         gl.type_no,
125         refs.reference,
126         IF(gl.type = 1 OR gl.type = 2,
127           bank_trans.amount,
128           SUM(IF(gl.amount>0, gl.amount,0))) as amount,
129         com.memo_,
130         IF(ISNULL(u.user_id),'',u.user_id) as user_id
131         FROM ".TB_PREF."gl_trans as gl
132          LEFT JOIN ".TB_PREF."audit_trail as a ON 
133                 (gl.type=a.type AND gl.type_no=a.trans_no)
134          LEFT JOIN ".TB_PREF."comments as com ON 
135                 (gl.type=com.type AND gl.type_no=com.id)
136          LEFT JOIN ".TB_PREF."refs as refs ON 
137                 (gl.type=refs.type AND gl.type_no=refs.id)
138          LEFT JOIN ".TB_PREF."users as u ON 
139                 a.user=u.id
140          LEFT JOIN ".TB_PREF."bank_trans as bank_trans ON 
141                 (gl.type=bank_trans.type AND gl.type_no=bank_trans.trans_no)            
142         WHERE gl.tran_date >= '" . date2sql($_POST['FromDate']) . "'
143         AND gl.tran_date <= '" . date2sql($_POST['ToDate']) . "'
144         AND gl.amount!=0";
145 if (isset($_POST['Ref']) && $_POST['Ref'] != "") {
146         $sql .= " AND reference LIKE '%". $_POST['Ref'] . "%'";
147 }       
148 if (get_post('filterType') != -1) {
149         $sql .= " AND gl.type=".get_post('filterType');
150 }       
151 if (!check_value('AlsoClosed')) {
152         $sql .= " AND gl_seq=0";
153 }
154 $sql .= " GROUP BY gl.type, gl.type_no";
155 */
156
157 $sql = "SELECT  IF(ISNULL(a.gl_seq),0,a.gl_seq) as gl_seq,
158         gl.tran_date,
159         gl.type,
160         gl.type_no,
161         refs.reference,
162         SUM(IF(gl.amount>0, gl.amount,0)) as amount,
163         com.memo_,
164         IF(ISNULL(u.user_id),'',u.user_id) as user_id
165         FROM ".TB_PREF."gl_trans as gl
166          LEFT JOIN ".TB_PREF."audit_trail as a ON
167                 (gl.type=a.type AND gl.type_no=a.trans_no)
168          LEFT JOIN ".TB_PREF."comments as com ON
169                 (gl.type=com.type AND gl.type_no=com.id)
170          LEFT JOIN ".TB_PREF."refs as refs ON
171                 (gl.type=refs.type AND gl.type_no=refs.id)
172          LEFT JOIN ".TB_PREF."users as u ON
173                 a.user=u.id
174         WHERE gl.tran_date >= '" . date2sql($_POST['FromDate']) . "'
175         AND gl.tran_date <= '" . date2sql($_POST['ToDate']) . "'
176         AND gl.amount!=0";
177 if (isset($_POST['Ref']) && $_POST['Ref'] != "") {
178         $sql .= " AND reference LIKE '%". $_POST['Ref'] . "%'";
179 }
180 if (isset($_POST['Memo']) && $_POST['Memo'] != "") {
181         $sql .= " AND com.memo_ LIKE '%". $_POST['Memo'] . "%'";
182 }
183 if (get_post('filterType') != -1) {
184         $sql .= " AND gl.type=".get_post('filterType');
185 }
186 if (!check_value('AlsoClosed')) {
187         $sql .= " AND gl_seq=0";
188 }
189 $sql .= " GROUP BY gl.type, gl.type_no";
190
191 $cols = array(
192         _("#") => array('fun'=>'journal_pos', 'align'=>'center'), 
193         _("Date") =>array('name'=>'tran_date','type'=>'date','ord'=>'desc'),
194         _("Type") => array('fun'=>'systype_name'), 
195         _("Trans #") => array('fun'=>'view_link'), 
196         _("Reference"), 
197         _("Amount") => array('type'=>'amount'),
198         _("Memo"),
199         _("User"),
200         _("View") => array('insert'=>true, 'fun'=>'gl_link'),
201         array('insert'=>true, 'fun'=>'edit_link')
202 );
203
204 if (!check_value('AlsoClosed')) {
205         $cols[_("#")] = 'skip';
206 }
207
208 $table =& new_db_pager('journal_tbl', $sql, $cols);
209
210 $table->width = "80%";
211
212 display_db_pager($table);
213
214 end_form();
215 end_page();
216
217 ?>