function getAverageCost($stock_id, $to_date) { if ($to_date == null) { $to_date = Today(); } $to_date = date2sql($to_date); $sql = "SELECT standard_cost, price, tran_date, type, trans_no, qty, person_id FROM " . TB_PREF . "stock_moves\n\t\tWHERE stock_id=" . db_escape($stock_id) . "\n\t\tAND tran_date <= '{$to_date}' AND standard_cost > 0.001 AND qty <> 0 AND type <> " . ST_LOCTRANSFER; $sql .= " ORDER BY tran_date"; $result = db_query($sql, "No standard cost transactions were returned"); if ($result == false) { return 0; } $qty = $old_qty = $count = $old_std_cost = $tot_cost = 0; while ($row = db_fetch($result)) { $qty += $row['qty']; $price = get_domestic_price($row, $stock_id, $qty, $old_std_cost, $old_qty); $old_std_cost = $row['standard_cost']; $tot_cost += $price; $count++; $old_qty = $qty; } if ($count == 0) { return 0; } return $tot_cost / $count; }
function trans_qty_unit_cost($stock_id, $location = null, $from_date, $to_date, $inward = true) { if ($from_date == null) { $from_date = Today(); } $from_date = date2sql($from_date); if ($to_date == null) { $to_date = Today(); } $to_date = date2sql($to_date); $sql = "SELECT standard_cost, price, tran_date, type, trans_no, qty, person_id FROM " . TB_PREF . "stock_moves\n\t\tWHERE stock_id=" . db_escape($stock_id) . "\n\t\tAND tran_date <= '{$to_date}' AND standard_cost > 0.001 AND qty <> 0 AND type <> " . ST_LOCTRANSFER; if ($location != '') { $sql .= " AND loc_code = " . db_escape($location); } if ($inward) { $sql .= " AND qty > 0 "; } else { $sql .= " AND qty < 0 "; } $sql .= " ORDER BY tran_date"; $result = db_query($sql, "No standard cost transactions were returned"); if ($result == false) { return 0; } $qty = $count = $old_qty = $old_std_cost = $tot_cost = 0; while ($row = db_fetch($result)) { $qty += $row['qty']; $price = get_domestic_price($row, $stock_id, $qty, $old_std_cost, $old_qty); if (strncmp($row['tran_date'], $from_date, 10) >= 0) { $tot_cost += $price; $count++; } $old_std_cost = $row['standard_cost']; $old_qty = $qty; } if ($count == 0) { return 0; } return $tot_cost / $count; }