14ce24ab1084732fd0f943929bc0084937fb91e6
[fa-stable.git] / dimensions / inquiry / search_dimensions.php
1 <?php
2
3 $page_security = 2;
4 $path_to_root="../..";
5
6 include_once($path_to_root . "/includes/session.inc");
7
8 include_once($path_to_root . "/includes/date_functions.inc");
9 include_once($path_to_root . "/includes/ui.inc");
10 $js = "";
11 if ($use_popup_windows)
12         $js .= get_js_open_window(800, 500);
13 if ($use_date_picker)
14         $js .= get_js_date_picker();
15
16 if (isset($_GET['outstanding_only']) && $_GET['outstanding_only'])
17 {
18         $outstanding_only = 1;
19         page(_("Search Outstanding Dimensions"), false, false, "", $js);
20 }
21 else
22 {
23         $outstanding_only = 0;
24         page(_("Search Dimensions"), false, false, "", $js);
25 }
26 //-----------------------------------------------------------------------------------
27 // Ajax updates
28 //
29 if (get_post('SearchOrders'))
30 {
31         $Ajax->activate('dim_table');
32 } elseif (get_post('_OrderNumber_changed'))
33 {
34         $disable = get_post('OrderNumber') !== '';
35
36         $Ajax->addDisable(true, 'FromDate', $disable);
37         $Ajax->addDisable(true, 'ToDate', $disable);
38         $Ajax->addDisable(true, 'type_', $disable);
39         $Ajax->addDisable(true, 'OverdueOnly', $disable);
40         $Ajax->addDisable(true, 'OpenOnly', $disable);
41
42         if ($disable) {
43 //              $Ajax->addFocus(true, 'OrderNumber');
44                 set_focus('OrderNumber');
45         } else
46 //              $Ajax->addFocus(true, 'StockLocation');
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, 0, 2);
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 $sql = "SELECT * FROM ".TB_PREF."dimensions WHERE id > 0";
89
90 if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
91 {
92         $sql .= " AND reference LIKE '%". $_POST['OrderNumber'] . "%'";
93 } else {
94
95         if ($dim == 1)
96                 $sql .= " AND type_=1";
97
98         if (isset($_POST['OpenOnly']))
99         {
100                 $sql .= " AND closed=0";
101         }
102
103         if (isset($_POST['type_']) && ($_POST['type_'] > 0))
104         {
105                 $sql .= " AND type_=" . $_POST['type_'];
106         }
107
108         if (isset($_POST['OverdueOnly']))
109         {
110                 $today = date2sql(Today());
111
112                 $sql .= " AND due_date < '$today' ";
113         }
114
115         $sql .= " AND date_ >= '" . date2sql($_POST['FromDate']) . "'
116                 AND date_ <= '" . date2sql($_POST['ToDate']) . "'";
117 }
118 $sql .= " ORDER BY due_date";
119
120 $result = db_query($sql,"could not query dimensions");
121
122 div_start('dim_table');
123 start_table("$table_style width=80%");
124
125 if (!$outstanding_only)
126         $th = array(_("#"), _("Reference"), _("Name"), _("Type"), _("Date"),
127                 _("Due Date"), _("Closed"), _("Balance"));
128 else
129         $th = array(_("#"), _("Reference"), _("Name"), _("Type"), _("Date"),
130                 _("Due Date"), _("Balance"));
131 table_header($th);
132 $j = 1;
133 $k = 0;
134
135 while ($myrow = db_fetch($result))
136 {
137         $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans WHERE tran_date >= '" .
138                 date2sql($_POST['FromDate']) . "' AND
139                 tran_date <= '" . date2sql($_POST['ToDate']) . "' AND (dimension_id = " .
140                 $myrow['id']." OR dimension2_id = " .$myrow['id'].")";
141         $res = db_query($sql, "Transactions could not be calculated");
142         $row = db_fetch_row($res);
143
144         if ($k == 1)
145         {
146                 $row_text = "class='oddrow'";
147                 $k = 0;
148         }
149         else
150         {
151                 $row_text = "class='evenrow'";
152                 $k++;
153         }
154
155         // check if it's an overdue work order
156         if (date_diff(Today(), sql2date($myrow["due_date"]), "d") > 0)
157         {
158                 $row_text = "class='overduebg'";
159         }
160
161         start_row($row_text);
162
163         $mpage = $path_to_root . "/dimensions/dimension_entry.php?" . SID . "trans_no=" . $myrow["id"];
164
165         label_cell(get_dimensions_trans_view_str(systypes::dimension(), $myrow["id"]));
166         label_cell(get_dimensions_trans_view_str(systypes::dimension(), $myrow["id"], $myrow["reference"]));
167         label_cell($myrow["name"]);
168         label_cell($myrow["type_"]);
169         label_cell(sql2date($myrow["date_"]));
170         label_cell(sql2date($myrow["due_date"]));
171         if (!$outstanding_only)
172                 label_cell(($myrow["closed"] ? _("Yes") : _("No")));
173         amount_cell($row[0]);
174
175         label_cell($myrow["closed"] == 0 ? ("<a href='$mpage'>" . _("Edit") . "</a>") :'');
176         end_row();
177
178         $j++;
179         If ($j == 12)
180         {
181                 $j = 1;
182                 table_header($th);
183         }
184         //end of page full new headings if
185 }
186 //end of while loop
187
188 end_table(1);
189 div_end();
190 //---------------------------------------------------------------------------------
191
192 end_page();
193
194 ?>