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