Merging latest changes from stable branch up to 2.3.24
[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 = 'SA_ITEMSTRANSVIEW';
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 ($SysPrefs->use_popup_windows)
23         $js .= get_js_open_window(800, 500);
24 if (user_use_date_picker())
25         $js .= get_js_date_picker();
26 page(_($help_context = "Inventory Item Movement"), @$_GET['popup'], false, "", $js);
27 //------------------------------------------------------------------------------------------------
28
29 check_db_has_stock_items(_("There are no items defined in the system."));
30
31 if(get_post('ShowMoves'))
32 {
33         $Ajax->activate('doc_tbl');
34 }
35
36 if (isset($_GET['stock_id']))
37 {
38         $_POST['stock_id'] = $_GET['stock_id'];
39 }
40
41 if (!@$_GET['popup'])
42         start_form();
43
44 if (!isset($_POST['stock_id']))
45         $_POST['stock_id'] = get_global_stock_item();
46
47 start_table(TABLESTYLE_NOBORDER);
48 start_row();
49 if (!$page_nested)
50         stock_costable_items_list_cells(_("Item:"), 'stock_id', $_POST['stock_id']);
51 end_row();
52 end_table();
53
54 start_table(TABLESTYLE_NOBORDER);
55 start_row();
56
57 locations_list_cells(_("From Location:"), 'StockLocation', null, true);
58
59 date_cells(_("From:"), 'AfterDate', '', null, -user_transaction_days());
60 date_cells(_("To:"), 'BeforeDate');
61
62 submit_cells('ShowMoves',_("Show Movements"),'',_('Refresh Inquiry'), 'default');
63 end_row();
64 end_table();
65 if (!@$_GET['popup'])
66         end_form();
67
68 set_global_stock_item($_POST['stock_id']);
69
70 $before_date = date2sql($_POST['BeforeDate']);
71 $after_date = date2sql($_POST['AfterDate']);
72 $display_location = !$_POST['StockLocation'];
73
74 $result = get_stock_movements($_POST['stock_id'], $_POST['StockLocation'],
75         $_POST['BeforeDate'], $_POST['AfterDate']);
76
77 div_start('doc_tbl');
78 start_table(TABLESTYLE);
79 $th = array(_("Type"), _("#"), _("Reference"));
80
81 if ($display_location)
82         array_push($th, _("Location"));
83
84 array_push($th, _("Date"), _("Detail"), _("Quantity In"), _("Quantity Out"), _("Quantity On Hand"));
85
86 table_header($th);
87
88 $before_qty = get_qoh_on_date($_POST['stock_id'], $_POST['StockLocation'], add_days($_POST['AfterDate'], -1));
89
90 $after_qty = $before_qty;
91
92 /*
93 if (!isset($before_qty_row[0]))
94 {
95         $after_qty = $before_qty = 0;
96 }
97 */
98 start_row("class='inquirybg'");
99 $header_span = $display_location ? 6 : 5;
100 label_cell("<b>"._("Quantity on hand before") . " " . $_POST['AfterDate']."</b>", "align=center colspan=$header_span");
101 label_cell("&nbsp;", "colspan=2");
102 $dec = get_qty_dec($_POST['stock_id']);
103 qty_cell($before_qty, false, $dec);
104 end_row();
105
106 $j = 1;
107 $k = 0; //row colour counter
108
109 $total_in = 0;
110 $total_out = 0;
111
112 while ($myrow = db_fetch($result))
113 {
114
115         alt_table_row_color($k);
116
117         $trandate = sql2date($myrow["tran_date"]);
118
119         $type_name = $systypes_array[$myrow["type"]];
120
121         if ($myrow["qty"] > 0)
122         {
123                 $quantity_formatted = number_format2($myrow["qty"], $dec);
124                 $total_in += $myrow["qty"];
125         }
126         else
127         {
128                 $quantity_formatted = number_format2(-$myrow["qty"], $dec);
129                 $total_out += -$myrow["qty"];
130         }
131         $after_qty += $myrow["qty"];
132
133         label_cell($type_name);
134
135         label_cell(get_trans_view_str($myrow["type"], $myrow["trans_no"]));
136
137         label_cell(get_trans_view_str($myrow["type"], $myrow["trans_no"], $myrow["reference"]));
138
139         if($display_location) {
140                 label_cell($myrow['loc_code']);
141         }
142         label_cell($trandate);
143
144         $gl_posting = "";
145
146         label_cell($myrow['name']);
147
148         label_cell((($myrow["qty"] >= 0) ? $quantity_formatted : ""), "nowrap align=right");
149         label_cell((($myrow["qty"] < 0) ? $quantity_formatted : ""), "nowrap align=right");
150         qty_cell($after_qty, false, $dec);
151         end_row();
152
153         $j++;
154         if ($j == 12)
155         {
156                 $j = 1;
157                 table_header($th);
158         }
159 }
160
161 start_row("class='inquirybg'");
162 label_cell("<b>"._("Quantity on hand after") . " " . $_POST['BeforeDate']."</b>", "align=center colspan=$header_span");
163 qty_cell($total_in, false, $dec);
164 qty_cell($total_out, false, $dec);
165 qty_cell($after_qty, false, $dec);
166 end_row();
167
168 end_table(1);
169 div_end();
170 end_page();
171