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