Update from usntable branch.
[fa-stable.git] / dimensions / inquiry / search_dimensions.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 $page_security = 'SA_DIMTRANSVIEW';
13 $path_to_root="../..";
14
15 include($path_to_root . "/includes/db_pager.inc");
16 include_once($path_to_root . "/includes/session.inc");
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/ui.inc");
20 $js = "";
21 if ($use_popup_windows)
22         $js .= get_js_open_window(800, 500);
23 if ($use_date_picker)
24         $js .= get_js_date_picker();
25
26 if (isset($_GET['outstanding_only']) && $_GET['outstanding_only'])
27 {
28         $outstanding_only = 1;
29         page(_($help_context = "Search Outstanding Dimensions"), false, false, "", $js);
30 }
31 else
32 {
33         $outstanding_only = 0;
34         page(_($help_context = "Search Dimensions"), false, false, "", $js);
35 }
36 //-----------------------------------------------------------------------------------
37 // Ajax updates
38 //
39 if (get_post('SearchOrders'))
40 {
41         $Ajax->activate('dim_table');
42 } elseif (get_post('_OrderNumber_changed'))
43 {
44         $disable = get_post('OrderNumber') !== '';
45
46         $Ajax->addDisable(true, 'FromDate', $disable);
47         $Ajax->addDisable(true, 'ToDate', $disable);
48         $Ajax->addDisable(true, 'type_', $disable);
49         $Ajax->addDisable(true, 'OverdueOnly', $disable);
50         $Ajax->addDisable(true, 'OpenOnly', $disable);
51
52         if ($disable) {
53 //              $Ajax->addFocus(true, 'OrderNumber');
54                 set_focus('OrderNumber');
55         } else
56                 set_focus('type_');
57
58         $Ajax->activate('dim_table');
59 }
60
61 //--------------------------------------------------------------------------------------
62
63 if (isset($_GET["stock_id"]))
64         $_POST['SelectedStockItem'] = $_GET["stock_id"];
65
66 //--------------------------------------------------------------------------------------
67
68 start_form(false, false, $_SERVER['PHP_SELF'] ."?outstanding_only=$outstanding_only");
69
70 start_table("class='tablestyle_noborder'");
71 start_row();
72
73 ref_cells(_("Reference:"), 'OrderNumber', '',null, '', true);
74
75 number_list_cells(_("Type"), 'type_', null, 1, 2, _("All"));
76 date_cells(_("From:"), 'FromDate', '', null, 0, 0, -5);
77 date_cells(_("To:"), 'ToDate');
78
79 check_cells( _("Only Overdue:"), 'OverdueOnly', null);
80
81 if (!$outstanding_only)
82 {
83         check_cells( _("Only Open:"), 'OpenOnly', null);
84 }
85 else
86         $_POST['OpenOnly'] = 1;
87
88 submit_cells('SearchOrders', _("Search"), '', '', 'default');
89
90 end_row();
91 end_table();
92
93 $dim = get_company_pref('use_dimension');
94
95 function view_link($row) 
96 {
97         return get_dimensions_trans_view_str(ST_DIMENSION, $row["id"]);
98 }
99
100 function is_closed($row)
101 {
102         return $row['closed'] ? _('Yes') : _('No');
103 }
104
105 function sum_dimension($row) 
106 {
107         $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans WHERE tran_date >= '" .
108                 date2sql($_POST['FromDate']) . "' AND
109                 tran_date <= '" . date2sql($_POST['ToDate']) . "' AND (dimension_id = " .
110                 $row['id']." OR dimension2_id = " .$row['id'].")";
111         $res = db_query($sql, "Sum of transactions could not be calculated");
112         $row = db_fetch_row($res);
113
114         return $row[0];
115 }
116
117 function is_overdue($row)
118 {
119         return date_diff2(Today(), sql2date($row["due_date"]), "d") > 0;
120 }
121
122 function edit_link($row)
123 {
124         //return $row["closed"] ?  '' :
125         //      pager_link(_("Edit"),
126         //              "/dimensions/dimension_entry.php?trans_no=" . $row["id"], ICON_EDIT);
127         return pager_link(_("Edit"),
128                         "/dimensions/dimension_entry.php?trans_no=" . $row["id"], ICON_EDIT);
129 }
130
131 $sql = "SELECT dim.id,
132         dim.reference,
133         dim.name,
134         dim.type_,
135         dim.date_,
136         dim.due_date,
137         dim.closed
138         FROM ".TB_PREF."dimensions as dim WHERE id > 0";
139
140 if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
141 {
142         $sql .= " AND reference LIKE ".db_escape("%". $_POST['OrderNumber'] . "%");
143 } else {
144
145         if ($dim == 1)
146                 $sql .= " AND type_=1";
147
148         if (isset($_POST['OpenOnly']))
149         {
150                 $sql .= " AND closed=0";
151         }
152
153         if (isset($_POST['type_']) && ($_POST['type_'] > 0))
154         {
155                 $sql .= " AND type_=".db_escape($_POST['type_']);
156         }
157
158         if (isset($_POST['OverdueOnly']))
159         {
160                 $today = date2sql(Today());
161
162                 $sql .= " AND due_date < '$today'";
163         }
164
165         $sql .= " AND date_ >= '" . date2sql($_POST['FromDate']) . "'
166                 AND date_ <= '" . date2sql($_POST['ToDate']) . "'";
167 }
168
169 $cols = array(
170         _("#") => array('fun'=>'view_link'), 
171         _("Reference"), 
172         _("Name"), 
173         _("Type"), 
174         _("Date") =>'date',
175         _("Due Date") => array('name'=>'due_date', 'type'=>'date', 'ord'=>'asc'), 
176         _("Closed") => array('fun'=>'is_closed'),
177         _("Balance") => array('type'=>'amount', 'insert'=>true, 'fun'=>'sum_dimension'),
178         array('insert'=>true, 'fun'=>'edit_link')
179 );
180
181 if ($outstanding_only) {
182         $cols[_("Closed")] = 'skip';
183 }
184
185 $table =& new_db_pager('dim_tbl', $sql, $cols);
186 $table->set_marker('is_overdue', _("Marked dimensions are overdue."));
187
188 $table->width = "80%";
189
190 display_db_pager($table);
191
192 end_form();
193 end_page();
194
195 ?>