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