3 function get_qoh_on_date($stock_id, $location=null, $date_=null, $exclude=0)
8 $date = date2sql($date_);
10 $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
11 WHERE stock_id='$stock_id'
12 AND tran_date <= '$date'";
14 if ($location != null)
15 $sql .= " AND loc_code = '$location'";
17 $result = db_query($sql, "QOH calulcation failed");
19 $myrow = db_fetch_row($result);
22 $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
23 WHERE stock_id='$stock_id'
25 AND tran_date = '$date'";
27 $result = db_query($sql, "QOH calulcation failed");
28 $myrow2 = db_fetch_row($result);
29 if ($myrow2 !== false)
30 $myrow[0] -= $myrow2[0];
36 //--------------------------------------------------------------------------------------
38 function get_item_edit_info($stock_id)
40 $sql = "SELECT material_cost + labour_cost + overhead_cost AS standard_cost, units
41 FROM ".TB_PREF."stock_master WHERE stock_id='$stock_id'";
42 $result = db_query($sql, "The standard cost cannot be retrieved");
44 return db_fetch($result);
47 //--------------------------------------------------------------------------------------
49 function get_standard_cost($stock_id)
51 $sql = "SELECT material_cost + labour_cost + overhead_cost AS std_cost
52 FROM ".TB_PREF."stock_master WHERE stock_id='$stock_id'";
53 $result = db_query($sql, "The standard cost cannot be retrieved");
55 $myrow = db_fetch_row($result);
60 //--------------------------------------------------------------------------------------
62 function is_inventory_item($stock_id)
64 $sql = "SELECT stock_id FROM ".TB_PREF."stock_master
65 WHERE stock_id='$stock_id' AND mb_flag <> 'D'";
66 $result = db_query($sql, "Cannot query is inventory item or not");
68 return db_num_rows($result) > 0;
71 //-------------------------------------------------------------------
73 Function get_stock_gl_code($stock_id)
75 /*Gets the GL Codes relevant to the item account */
77 $sql = "SELECT inventory_account, cogs_account,
78 adjustment_account, sales_account, assembly_account, dimension_id, dimension2_id FROM
79 ".TB_PREF."stock_master WHERE stock_id = '$stock_id'";
81 $get = db_query($sql,"retreive stock gl code");
82 return db_fetch($get);
85 //--------------------------------------------------------------------------------------
87 // $date_ - display / non-sql date
88 // $std_cost - in HOME currency
89 // $show_or_hide - wil this move be visible in reports, etc
90 // $price - in $person_id's currency
92 function add_stock_move($type, $stock_id, $trans_no, $location,
93 $date_, $reference, $quantity, $std_cost, $person_id=0, $show_or_hide=1,
94 $price=0, $discount_percent=0, $error_msg="")
96 // do not add a stock move if it's a non-inventory item
97 if (!is_inventory_item($stock_id))
100 $date = date2sql($date_);
102 $sql = "INSERT INTO ".TB_PREF."stock_moves (stock_id, trans_no, type, loc_code,
103 tran_date, person_id, reference, qty, standard_cost, visible, price,
104 discount_percent) VALUES ('$stock_id', $trans_no, $type,
105 ".db_escape($location).", '$date', '$person_id', ".db_escape($reference).", $quantity, $std_cost,
106 $show_or_hide, $price, $discount_percent)";
108 if ($error_msg == "")
109 $error_msg = "The stock movement record cannot be inserted";
111 db_query($sql, $error_msg);
113 return db_insert_id();
116 function update_stock_move_pid($type, $stock_id, $from, $to, $pid, $cost)
118 $from = date2sql($from);
120 $sql = "UPDATE ".TB_PREF."stock_moves SET standard_cost=$cost WHERE type=$type
121 AND stock_id='$stock_id' AND tran_date>='$from' AND tran_date<='$to' AND person_id = $pid";
122 db_query($sql, "The stock movement standard_cost cannot be updated");
125 //--------------------------------------------------------------------------------------------------
127 function get_stock_moves($type, $type_no, $visible=false)
129 $sql = "SELECT ".TB_PREF."stock_moves.*, ".TB_PREF."stock_master.description, ".TB_PREF."stock_master.units,
130 ".TB_PREF."locations.location_name,
131 ".TB_PREF."stock_master.material_cost + ".TB_PREF."stock_master.labour_cost + ".TB_PREF."stock_master.overhead_cost AS FixedStandardCost
132 FROM ".TB_PREF."stock_moves,".TB_PREF."locations,".TB_PREF."stock_master
133 WHERE ".TB_PREF."stock_moves.stock_id = ".TB_PREF."stock_master.stock_id
134 AND ".TB_PREF."locations.loc_code=".TB_PREF."stock_moves.loc_code
135 AND type=$type AND trans_no=$type_no ORDER BY trans_id";
137 $sql .= " AND ".TB_PREF."stock_moves.visible=1";
139 return db_query($sql, "Could not get stock moves");
142 //--------------------------------------------------------------------------------------------------
144 function void_stock_move($type, $type_no)
146 $sql = "UPDATE ".TB_PREF."stock_moves SET qty=0, price=0, discount_percent=0,
147 standard_cost=0 WHERE type=$type AND trans_no=$type_no";
149 db_query($sql, "Could not void stock moves");
152 //--------------------------------------------------------------------------------------------------
154 function get_location_name($loc_code)
156 $sql = "SELECT location_name FROM ".TB_PREF."locations WHERE loc_code='$loc_code'";
158 $result = db_query($sql, "could not retreive the location name for $loc_code");
160 if (db_num_rows($result) == 1)
162 $row = db_fetch_row($result);
166 display_db_error("could not retreive the location name for $loc_code", $sql, true);
169 //--------------------------------------------------------------------------------------------------