944cb437b3c95ae1b501f8bfa20de6002a553080
[fa-stable.git] / inventory / inquiry / stock_movements.php
1 <?php
2
3
4 $page_security = 2;
5 $path_to_root="../..";
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/banking.inc");
10 include_once($path_to_root . "/sales/includes/sales_db.inc");
11
12 include_once($path_to_root . "/includes/ui.inc");
13 $js = "";
14 if ($use_popup_windows)
15         $js .= get_js_open_window(800, 500);
16 if ($use_date_picker)
17         $js .= get_js_date_picker();
18
19 page(_("Inventory Item Movement"), false, false, "", $js);
20
21 if (isset($_GET['stock_id']))
22 {
23         $_POST['stock_id'] = $_GET['stock_id'];
24 }
25
26 start_form(false, true);
27
28 if (!isset($_POST['stock_id']))
29         $_POST['stock_id'] = get_global_stock_item();
30
31 start_table("class='tablestyle_noborder'");
32
33 stock_items_list_cells(_("Item:"), 'stock_id', $_POST['stock_id']);
34
35 locations_list_cells(_("From Location:"), 'StockLocation', null);
36
37 date_cells(_("From:"), 'AfterDate', '', null, -30);
38 date_cells(_("To:"), 'BeforeDate');
39
40 submit_cells('ShowMoves',_("Show Movements"));
41 end_table();
42 end_form();
43
44 set_global_stock_item($_POST['stock_id']);
45
46 $before_date = date2sql($_POST['BeforeDate']);
47 $after_date = date2sql($_POST['AfterDate']);
48
49 $sql = "SELECT type, trans_no, tran_date, person_id, qty, reference
50         FROM ".TB_PREF."stock_moves
51         WHERE loc_code='" . $_POST['StockLocation'] . "'
52         AND tran_date >= '". $after_date . "'
53         AND tran_date <= '" . $before_date . "'
54         AND stock_id = '" . $_POST['stock_id'] . "' ORDER BY tran_date,trans_id";
55 $result = db_query($sql, "could not query stock moves");
56
57 check_db_error("The stock movements for the selected criteria could not be retrieved",$sql);
58
59 start_table("$table_style width=70%");
60 $th = array(_("Type"), _("#"), _("Reference"), _("Date"), _("Detail"),
61         _("Quantity In"), _("Quantity Out"), _("Quantity On Hand"));
62
63 table_header($th);
64
65 $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves WHERE stock_id='" . $_POST['stock_id'] . "'
66         AND loc_code='" . $_POST['StockLocation'] . "'
67         AND tran_date < '" . $after_date . "'";
68 $before_qty = db_query($sql, "The starting quantity on hand could not be calculated");
69
70 $before_qty_row = db_fetch_row($before_qty);
71 $after_qty = $before_qty = $before_qty_row[0];
72
73 if (!isset($before_qty_row[0]))
74 {
75         $after_qty = $before_qty = 0;
76 }
77
78 start_row("class='inquirybg'");
79 label_cell("<b>"._("Quantity on hand before") . " " . $_POST['AfterDate']."</b>", "align=center colspan=5");
80 label_cell("&nbsp;", "colspan=2");
81 $dec = get_qty_dec($_POST['stock_id']);
82 qty_cell($before_qty, false, $dec);
83 end_row();
84
85 $j = 1;
86 $k = 0; //row colour counter
87
88 $total_in = 0;
89 $total_out = 0;
90
91 while ($myrow = db_fetch($result))
92 {
93
94         alt_table_row_color($k);
95
96         $trandate = sql2date($myrow["tran_date"]);
97
98         $type_name = systypes::name($myrow["type"]);
99
100         if ($myrow["qty"] > 0)
101         {
102                 $quantity_formatted = number_format2($myrow["qty"], $dec);
103                 $total_in += $myrow["qty"];
104         }
105         else
106         {
107                 $quantity_formatted = number_format2(-$myrow["qty"], $dec);
108                 $total_out += -$myrow["qty"];
109         }
110         $after_qty += $myrow["qty"];
111
112         label_cell($type_name);
113
114         label_cell(get_trans_view_str($myrow["type"], $myrow["trans_no"]));
115
116         label_cell(get_trans_view_str($myrow["type"], $myrow["trans_no"], $myrow["reference"]));
117
118         label_cell($trandate);
119
120         $person = $myrow["person_id"];
121         $gl_posting = "";
122
123         if (($myrow["type"] == 13) || ($myrow["type"] == 11))
124         {
125                 $cust_row = get_customer_details_from_trans($myrow["type"], $myrow["trans_no"]);
126
127                 if (strlen($cust_row['name']) > 0)
128                         $person = $cust_row['name'] . " (" . $cust_row['br_name'] . ")";
129
130         }
131         elseif ($myrow["type"] == 25)
132         {
133                 // get the supplier name
134                 $sql = "SELECT supp_name FROM ".TB_PREF."suppliers WHERE supplier_id = '" . $myrow["person_id"] . "'";
135                 $supp_result = db_query($sql,"check failed");
136
137                 $supp_row = db_fetch($supp_result);
138
139                 if (strlen($supp_row['supp_name']) > 0)
140                         $person = $supp_row['supp_name'];
141         }
142         elseif ($myrow["type"] == systypes::location_transfer() || $myrow["type"] == systypes::inventory_adjustment())
143         {
144                 // get the adjustment type
145                 $movement_type = get_movement_type($myrow["person_id"]);
146                 $person = $movement_type["name"];
147         }
148         elseif ($myrow["type"]==systypes::work_order() || $myrow["type"] == 28  ||
149                 $myrow["type"] == 29)
150         {
151                 $person = "";
152         }
153
154         label_cell($person);
155
156         label_cell((($myrow["qty"] >= 0) ? $quantity_formatted : ""), "nowrap align=right");
157         label_cell((($myrow["qty"] < 0) ? $quantity_formatted : ""), "nowrap align=right");
158         qty_cell($after_qty, false, $dec);
159         end_row();
160         $j++;
161         If ($j == 12)
162         {
163                 $j = 1;
164                 table_header($th);
165         }
166 //end of page full new headings if
167 }
168 //end of while loop
169
170 // 2008-06-14. Always write this.
171 //if ($total_in != 0 || $total_out != 0)
172 //{
173         start_row("class='inquirybg'");
174     label_cell("<b>"._("Quantity on hand after") . " " . $_POST['BeforeDate']."</b>", "align=center colspan=5");
175     qty_cell($total_in, false, $dec);
176     qty_cell($total_out, false, $dec);
177     qty_cell($after_qty, false, $dec);
178     end_row();
179 //}
180
181 end_table(1);
182
183 end_page();
184
185 ?>