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