1192cd0685c29f602080f2a3124cd12b08f094d4
[fa-stable.git] / inventory / inquiry / stock_movements.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 2;
13 $path_to_root="../..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 include_once($path_to_root . "/includes/date_functions.inc");
17 include_once($path_to_root . "/includes/banking.inc");
18 include_once($path_to_root . "/sales/includes/sales_db.inc");
19
20 include_once($path_to_root . "/includes/ui.inc");
21 $js = "";
22 if ($use_popup_windows)
23         $js .= get_js_open_window(800, 500);
24 if ($use_date_picker)
25         $js .= get_js_date_picker();
26
27 page(_("Inventory Item Movement"), false, false, "", $js);
28 //------------------------------------------------------------------------------------------------
29
30 check_db_has_stock_items(_("There are no items defined in the system."));
31
32 if(get_post('ShowMoves'))
33 {
34         $Ajax->activate('doc_tbl');
35 }
36
37 if (isset($_GET['stock_id']))
38 {
39         $_POST['stock_id'] = $_GET['stock_id'];
40 }
41
42 start_form();
43
44 if (!isset($_POST['stock_id']))
45         $_POST['stock_id'] = get_global_stock_item();
46
47 start_table("class='tablestyle_noborder'");
48
49 stock_items_list_cells(_("Item:"), 'stock_id', $_POST['stock_id']);
50
51 locations_list_cells(_("From Location:"), 'StockLocation', null);
52
53 date_cells(_("From:"), 'AfterDate', '', null, -30);
54 date_cells(_("To:"), 'BeforeDate');
55
56 submit_cells('ShowMoves',_("Show Movements"),'',_('Refresh Inquiry'), 'default');
57 end_table();
58 end_form();
59
60 set_global_stock_item($_POST['stock_id']);
61
62 $before_date = date2sql($_POST['BeforeDate']);
63 $after_date = date2sql($_POST['AfterDate']);
64
65 $sql = "SELECT type, trans_no, tran_date, person_id, qty, reference
66         FROM ".TB_PREF."stock_moves
67         WHERE loc_code='" . $_POST['StockLocation'] . "'
68         AND tran_date >= '". $after_date . "'
69         AND tran_date <= '" . $before_date . "'
70         AND stock_id = '" . $_POST['stock_id'] . "' ORDER BY tran_date,trans_id";
71 $result = db_query($sql, "could not query stock moves");
72
73 check_db_error("The stock movements for the selected criteria could not be retrieved",$sql);
74
75 div_start('doc_tbl');
76 start_table($table_style);
77 $th = array(_("Type"), _("#"), _("Reference"), _("Date"), _("Detail"),
78         _("Quantity In"), _("Quantity Out"), _("Quantity On Hand"));
79
80 table_header($th);
81
82 $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves WHERE stock_id='" . $_POST['stock_id'] . "'
83         AND loc_code='" . $_POST['StockLocation'] . "'
84         AND tran_date < '" . $after_date . "'";
85 $before_qty = db_query($sql, "The starting quantity on hand could not be calculated");
86
87 $before_qty_row = db_fetch_row($before_qty);
88 $after_qty = $before_qty = $before_qty_row[0];
89
90 if (!isset($before_qty_row[0]))
91 {
92         $after_qty = $before_qty = 0;
93 }
94
95 start_row("class='inquirybg'");
96 label_cell("<b>"._("Quantity on hand before") . " " . $_POST['AfterDate']."</b>", "align=center colspan=5");
97 label_cell("&nbsp;", "colspan=2");
98 $dec = get_qty_dec($_POST['stock_id']);
99 qty_cell($before_qty, false, $dec);
100 end_row();
101
102 $j = 1;
103 $k = 0; //row colour counter
104
105 $total_in = 0;
106 $total_out = 0;
107
108 while ($myrow = db_fetch($result))
109 {
110
111         alt_table_row_color($k);
112
113         $trandate = sql2date($myrow["tran_date"]);
114
115         $type_name = systypes::name($myrow["type"]);
116
117         if ($myrow["qty"] > 0)
118         {
119                 $quantity_formatted = number_format2($myrow["qty"], $dec);
120                 $total_in += $myrow["qty"];
121         }
122         else
123         {
124                 $quantity_formatted = number_format2(-$myrow["qty"], $dec);
125                 $total_out += -$myrow["qty"];
126         }
127         $after_qty += $myrow["qty"];
128
129         label_cell($type_name);
130
131         label_cell(get_trans_view_str($myrow["type"], $myrow["trans_no"]));
132
133         label_cell(get_trans_view_str($myrow["type"], $myrow["trans_no"], $myrow["reference"]));
134         label_cell($trandate);
135
136         $person = $myrow["person_id"];
137         $gl_posting = "";
138
139         if (($myrow["type"] == 13) || ($myrow["type"] == 11))
140         {
141                 $cust_row = get_customer_details_from_trans($myrow["type"], $myrow["trans_no"]);
142
143                 if (strlen($cust_row['name']) > 0)
144                         $person = $cust_row['name'] . " (" . $cust_row['br_name'] . ")";
145
146         }
147         elseif ($myrow["type"] == 25 || $myrow['type'] == 21)
148         {
149                 // get the supplier name
150                 $sql = "SELECT supp_name FROM ".TB_PREF."suppliers WHERE supplier_id = '" . $myrow["person_id"] . "'";
151                 $supp_result = db_query($sql,"check failed");
152
153                 $supp_row = db_fetch($supp_result);
154
155                 if (strlen($supp_row['supp_name']) > 0)
156                         $person = $supp_row['supp_name'];
157         }
158         elseif ($myrow["type"] == systypes::location_transfer() || $myrow["type"] == systypes::inventory_adjustment())
159         {
160                 // get the adjustment type
161                 $movement_type = get_movement_type($myrow["person_id"]);
162                 $person = $movement_type["name"];
163         }
164         elseif ($myrow["type"]==systypes::work_order() || $myrow["type"] == 28  ||
165                 $myrow["type"] == 29)
166         {
167                 $person = "";
168         }
169
170         label_cell($person);
171
172         label_cell((($myrow["qty"] >= 0) ? $quantity_formatted : ""), "nowrap align=right");
173         label_cell((($myrow["qty"] < 0) ? $quantity_formatted : ""), "nowrap align=right");
174         qty_cell($after_qty, false, $dec);
175         end_row();
176         $j++;
177         If ($j == 12)
178         {
179                 $j = 1;
180                 table_header($th);
181         }
182 //end of page full new headings if
183 }
184 //end of while loop
185
186 start_row("class='inquirybg'");
187 label_cell("<b>"._("Quantity on hand after") . " " . $_POST['BeforeDate']."</b>", "align=center colspan=5");
188 qty_cell($total_in, false, $dec);
189 qty_cell($total_out, false, $dec);
190 qty_cell($after_qty, false, $dec);
191 end_row();
192
193 end_table(1);
194 div_end();
195 end_page();
196
197 ?>