52b2aa1fb7dd75e76f197b8d4e5a21602d444d16
[fa-stable.git] / inventory / inquiry / stock_movements.php
1 <?php
2
3
4 $page_security = 2;
5 $path_to_root="../..";
6 include($path_to_root . "/includes/db_pager.inc");
7 include_once($path_to_root . "/includes/session.inc");
8
9 include_once($path_to_root . "/includes/date_functions.inc");
10 include_once($path_to_root . "/includes/banking.inc");
11 include_once($path_to_root . "/sales/includes/sales_db.inc");
12
13 include_once($path_to_root . "/includes/ui.inc");
14 $js = "";
15 if ($use_popup_windows)
16         $js .= get_js_open_window(800, 500);
17 if ($use_date_picker)
18         $js .= get_js_date_picker();
19
20 page(_("Inventory Item Movement"), false, false, "", $js);
21 //------------------------------------------------------------------------------------------------
22
23 check_db_has_stock_items(_("There are no items defined in the system."));
24
25 if (isset($_GET['stock_id']))
26 {
27         $_POST['stock_id'] = $_GET['stock_id'];
28 }
29
30 start_form(false, true);
31
32 if (!isset($_POST['stock_id']))
33         $_POST['stock_id'] = get_global_stock_item();
34
35 start_table("class='tablestyle_noborder'");
36
37 stock_items_list_cells(_("Item:"), 'stock_id', $_POST['stock_id']);
38
39 locations_list_cells(_("From Location:"), 'StockLocation', null);
40
41 date_cells(_("From:"), 'AfterDate', '', null, -30);
42 date_cells(_("To:"), 'BeforeDate');
43
44 submit_cells('ShowMoves',_("Show Movements"),'',_('Refresh Inquiry'), true);
45 end_table();
46 end_form();
47
48 set_global_stock_item($_POST['stock_id']);
49 $item_dec = get_qty_dec($_POST['stock_id']);
50 //
51 //      Get summary displayed in headewr and footer.
52 //
53 function get_summary(&$table)
54 {
55         global $Ajax, $item_dec;
56
57         $sql = "SELECT
58                 Sum(qty) as sum,
59                 Sum(IF(qty>0, qty, 0)) as in_qty,
60                 Sum(IF(qty<0, -qty, 0)) as out_qty
61         FROM ".TB_PREF."stock_moves
62         WHERE loc_code='" . $_POST['StockLocation'] . "'
63         AND tran_date >= '". date2sql($_POST['AfterDate']) . "'
64         AND tran_date <= '" . date2sql($_POST['BeforeDate']) . "'
65         AND stock_id = '" . $_POST['stock_id'] . "'";
66         " GROUP BY rec";
67         $result = db_query($sql, "cannot retrieve stock moves");
68
69         $qty = db_fetch($result);
70
71         $sum['beg'] = get_qoh_on_date($_POST['stock_id'], $_POST['StockLocation'],
72                 add_days($_POST['AfterDate'], -1));
73         $sum['in'] = $qty['in_qty'];
74         $sum['out'] = $qty['out_qty'];
75         $sum['end'] = $sum['beg'] + $qty['sum'];
76         $sum['dec'] = $item_dec = get_qty_dec($_POST['stock_id']);
77
78         $table->sum = $sum;
79         $Ajax->activate('summary');
80  }
81 //-----------------------------------------------------------------------------
82
83 function systype_name($row)
84 {
85         return systypes::name($row["type"]);
86 }
87
88 function trans_view($row)
89 {
90         return  get_trans_view_str($row["type"], $row["trans_no"]);
91 }
92
93 function show_details($row)
94 {
95         $person = $row["person_id"];
96         $gl_posting = "";
97
98         if (($row["type"] == 13) || ($row["type"] == 11))
99         {
100                 $cust_row = get_customer_details_from_trans($row["type"], $row["trans_no"]);
101
102                 if (strlen($cust_row['name']) > 0)
103                         $person = $cust_row['name'] . " (" . $cust_row['br_name'] . ")";
104
105         }
106         elseif ($row["type"] == 25 || $row['type'] == 21)
107         {
108                 // get the supplier name
109                 $sql = "SELECT supp_name FROM ".TB_PREF."suppliers WHERE supplier_id = '" . $row["person_id"] . "'";
110                 $supp_result = db_query($sql,"check failed");
111
112                 $supp_row = db_fetch($supp_result);
113
114                 if (strlen($supp_row['supp_name']) > 0)
115                         $person = $supp_row['supp_name'];
116         }
117         elseif ($row["type"] == systypes::location_transfer() || $row["type"] == systypes::inventory_adjustment())
118         {
119                 // get the adjustment type
120                 $movement_type = get_movement_type($row["person_id"]);
121                 $person = $movement_type["name"];
122         }
123         elseif ($row["type"]==systypes::work_order() || $row["type"] == 28  ||
124                 $row["type"] == 29)
125         {
126                 $person = "";
127         }
128         return $person;
129 }
130
131 $total_out = 0;
132 $total_in = 0;
133
134 function qty_in($row)
135 {
136         $q = $row["qty"];
137         return $q <= 0 ? '' : $q;
138 }
139
140 function qty_out($row)
141 {
142         $q = -$row["qty"];
143         return $q <= 0 ? '' : $q;
144 }
145 /*
146 function show_qoh($row)
147 {
148         $qoh =& $_SESSION['qoh'];
149         $qoh += $row['qty'];
150         return $qoh;
151 }
152 */
153 function before_status($pager)
154 {
155         $r[] =
156                 array( "<b>"._("Quantity on hand before") . " " . $_POST['AfterDate']
157                 .':'."</b>", "align='right' colspan=5");
158         if($pager->sum['beg']>=0) {
159                 $r[] = array (number_format2($pager->sum['beg'], $pager->sum['dec']),
160                 "align='right'");
161                 $r[] = array("&nbsp;");
162         } else {
163                 $r[] = array("&nbsp;");
164                 $r[] = array (number_format2($pager->sum['beg'], $pager->sum['dec']),
165                 "align='right'");
166         }
167         return $r;
168 }
169
170 function after_status($pager)
171 {
172         $r[] =
173                 array( "<b>"._("Quantity on hand after") . " " . $_POST['BeforeDate']
174                 .':'."</b>", "align='right' colspan=5");
175         if($pager->sum['end']>=0) {
176                 $r[] = array (number_format2($pager->sum['end'], $pager->sum['dec']),
177                 "align='right'");
178                 $r[] = array("&nbsp;", "colspan=2");
179         } else {
180                 $r[] = array("&nbsp;", "colspan=2");
181                 $r[] = array (number_format2($pager->sum['end'], $pager->sum['dec']),
182                 "align='right'");
183         }
184         return $r;
185 }
186 //-----------------------------------------------------------------------------
187
188 $before_date = date2sql($_POST['BeforeDate']);
189 $after_date = date2sql($_POST['AfterDate']);
190
191 $sql = "SELECT 
192         type, 
193         trans_no, 
194         reference,
195         tran_date, 
196         person_id, 
197         qty
198         FROM ".TB_PREF."stock_moves
199         WHERE loc_code='" . $_POST['StockLocation'] . "'
200         AND tran_date >= '". $after_date . "'
201         AND tran_date <= '" . $before_date . "'
202         AND stock_id = '" . $_POST['stock_id'] . "'";
203
204 $cols = array(
205         _("Type") => array('fun'=>'systype_name' ), 
206         _("#") => array('fun'=>'trans_view' ), 
207         _("Reference"), 
208         _("Date") => array('date', 'ord'=>'desc'), 
209         _("Detail") => array('fun'=>'show_details' ), 
210         _("Quantity In") => array('type'=>'amount', 'dec'=> $item_dec, 'insert'=>true,'fun'=>'qty_in' ),
211         _("Quantity Out") => array('type'=>'amount', 'dec'=> $item_dec,'insert'=>true,'fun'=>'qty_out' ), 
212 //      _("Quantity On Hand") => array('insert'=>true,'type'=>'amount', 'fun'=>'show_qoh' )
213 );
214
215 $table =& new_db_pager('doc_tbl', $sql, $cols);
216 $table->set_header('before_status');
217 $table->set_footer('after_status');
218
219 if (!$table->ready)  // new sql query - update summary
220         get_summary(&$table);
221
222 start_form();
223
224 display_db_pager($table);
225
226 end_form();
227 end_page();
228 ?>