2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU Affero General Public License,
5 AGPL, as published by the Free Software Foundation, either version
6 3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 function get_qoh_on_date($stock_id, $location=null, $date_=null, $exclude=0)
17 $date = date2sql($date_);
19 $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
20 WHERE stock_id='$stock_id'
21 AND tran_date <= '$date'";
23 if ($location != null)
24 $sql .= " AND loc_code = '$location'";
26 $result = db_query($sql, "QOH calulcation failed");
28 $myrow = db_fetch_row($result);
31 $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
32 WHERE stock_id='$stock_id'
34 AND tran_date = '$date'";
36 $result = db_query($sql, "QOH calulcation failed");
37 $myrow2 = db_fetch_row($result);
38 if ($myrow2 !== false)
39 $myrow[0] -= $myrow2[0];
45 //--------------------------------------------------------------------------------------
47 function get_item_edit_info($stock_id)
49 $sql = "SELECT material_cost + labour_cost + overhead_cost AS standard_cost, units, decimals
50 FROM ".TB_PREF."stock_master,".TB_PREF."item_units
51 WHERE stock_id='$stock_id'
52 AND ".TB_PREF."stock_master.units=".TB_PREF."item_units.abbr";
53 $result = db_query($sql, "The standard cost cannot be retrieved");
55 return db_fetch($result);
58 //--------------------------------------------------------------------------------------
60 function get_standard_cost($stock_id)
62 $sql = "SELECT material_cost + labour_cost + overhead_cost AS std_cost
63 FROM ".TB_PREF."stock_master WHERE stock_id='$stock_id'";
64 $result = db_query($sql, "The standard cost cannot be retrieved");
66 $myrow = db_fetch_row($result);
71 //--------------------------------------------------------------------------------------
73 function is_inventory_item($stock_id)
75 $sql = "SELECT stock_id FROM ".TB_PREF."stock_master
76 WHERE stock_id='$stock_id' AND mb_flag <> 'D'";
77 $result = db_query($sql, "Cannot query is inventory item or not");
79 return db_num_rows($result) > 0;
82 //-------------------------------------------------------------------
84 Function get_stock_gl_code($stock_id)
86 /*Gets the GL Codes relevant to the item account */
88 $sql = "SELECT inventory_account, cogs_account,
89 adjustment_account, sales_account, assembly_account, dimension_id, dimension2_id FROM
90 ".TB_PREF."stock_master WHERE stock_id = '$stock_id'";
92 $get = db_query($sql,"retreive stock gl code");
93 return db_fetch($get);
96 //--------------------------------------------------------------------------------------
98 // $date_ - display / non-sql date
99 // $std_cost - in HOME currency
100 // $show_or_hide - wil this move be visible in reports, etc
101 // $price - in $person_id's currency
103 function add_stock_move($type, $stock_id, $trans_no, $location,
104 $date_, $reference, $quantity, $std_cost, $person_id=0, $show_or_hide=1,
105 $price=0, $discount_percent=0, $error_msg="")
107 // do not add a stock move if it's a non-inventory item
108 if (!is_inventory_item($stock_id))
111 $date = date2sql($date_);
113 $sql = "INSERT INTO ".TB_PREF."stock_moves (stock_id, trans_no, type, loc_code,
114 tran_date, person_id, reference, qty, standard_cost, visible, price,
115 discount_percent) VALUES ('$stock_id', $trans_no, $type,
116 ".db_escape($location).", '$date', '$person_id', ".db_escape($reference).", $quantity, $std_cost,
117 $show_or_hide, $price, $discount_percent)";
119 if ($error_msg == "")
120 $error_msg = "The stock movement record cannot be inserted";
122 db_query($sql, $error_msg);
124 return db_insert_id();
127 function update_stock_move_pid($type, $stock_id, $from, $to, $pid, $cost)
129 $from = date2sql($from);
131 $sql = "UPDATE ".TB_PREF."stock_moves SET standard_cost=$cost WHERE type=$type
132 AND stock_id='$stock_id' AND tran_date>='$from' AND tran_date<='$to' AND person_id = $pid";
133 db_query($sql, "The stock movement standard_cost cannot be updated");
136 //--------------------------------------------------------------------------------------------------
138 function get_stock_moves($type, $type_no, $visible=false)
140 $sql = "SELECT ".TB_PREF."stock_moves.*, ".TB_PREF."stock_master.description, ".TB_PREF."stock_master.units,
141 ".TB_PREF."locations.location_name,
142 ".TB_PREF."stock_master.material_cost + ".TB_PREF."stock_master.labour_cost + ".TB_PREF."stock_master.overhead_cost AS FixedStandardCost
143 FROM ".TB_PREF."stock_moves,".TB_PREF."locations,".TB_PREF."stock_master
144 WHERE ".TB_PREF."stock_moves.stock_id = ".TB_PREF."stock_master.stock_id
145 AND ".TB_PREF."locations.loc_code=".TB_PREF."stock_moves.loc_code
146 AND type=$type AND trans_no=$type_no ORDER BY trans_id";
148 $sql .= " AND ".TB_PREF."stock_moves.visible=1";
150 return db_query($sql, "Could not get stock moves");
153 //--------------------------------------------------------------------------------------------------
155 function void_stock_move($type, $type_no)
157 $sql = "UPDATE ".TB_PREF."stock_moves SET qty=0, price=0, discount_percent=0,
158 standard_cost=0 WHERE type=$type AND trans_no=$type_no";
160 db_query($sql, "Could not void stock moves");
163 //--------------------------------------------------------------------------------------------------
165 function get_location_name($loc_code)
167 $sql = "SELECT location_name FROM ".TB_PREF."locations WHERE loc_code='$loc_code'";
169 $result = db_query($sql, "could not retreive the location name for $loc_code");
171 if (db_num_rows($result) == 1)
173 $row = db_fetch_row($result);
177 display_db_error("could not retreive the location name for $loc_code", $sql, true);
180 //--------------------------------------------------------------------------------------------------