<?php

//function complete_stock_count(&$order)
//{
// 
//        update_stock_count_header_status($order->order_no, SC_COMPLETING);
//
//	foreach ($order->line_items as $line)
//	{
//                $qoh = get_qoh_on_date($line->stock_id, $order->from_loc);
//                $standard_cost = get_standard_cost($line->stock_id);
//                $status_id = SC_COMPLETED;
//           
//                $sql = "UPDATE ".TB_PREF."stock_count_details SET system_qty=" . db_escape($qoh). " ,
//                                status_id =" . db_escape($status_id). " ,
//                                standard_cost =" . db_escape($standard_cost). "
//                                WHERE id = ".db_escape($line->row_id);
//                
//		db_query($sql, "Stock count cannot be updated");
//
//        }
//
//        $result = check_stock_count_complete($order->order_no);
//        $check_stock_complete = db_fetch($result);
//        
//        if(empty($check_stock_complete)){
//                update_stock_count_header_status($order->order_no, 25);
//                return array("passed"=>true, "message"=>"Stock count $order->order_no completed");
//        }
//        else{
//                return array("passed"=>false, "message"=>"Could not complete all stock count items");
//        }
//}


function complete_stock_count($stock_count_id)
{
    $total_adjustment_value = 0;
    
    $sc_header = get_stock_count_header($stock_count_id);
    $n = update_stock_count_header_status($stock_count_id, SC_COMPLETING, SC_COUNTED);
    if($n != 1){
        return array("success"=>false, "message"=>"Another user has updated Stock count $stock_count_id");
    }

    $sc_sheets_result = get_stock_count_sheets($stock_count_id);    
    while ($sc_sheet = db_fetch($sc_sheets_result)){
        
        $sc_line_result = get_stock_count_counted_details($stock_count_id, $sc_sheet['sheet_no']); 
        while ($sc_line = db_fetch($sc_line_result)){
            $good_qty = $sc_line['good_qty'];
            $damaged_qty = $sc_line['damaged_qty'];
            $counted_qty = $good_qty + $damaged_qty;
            
//            $qoh = get_qoh_on_date($sc_line['stock_id'], $sc_header['loc_code'], sql2date($sc_header['tran_date']));
            $counting_standard_cost = $sc_line['standard_cost'];
            $qoh = $sc_line['system_qty'];
            
            $status_id = SC_COMPLETED;

            $sql = "UPDATE ".TB_PREF."stock_count_details 
                    SET status_id =" . db_escape($status_id). "  
                    WHERE id = ".db_escape($sc_line['id'])." AND 
                          status_id =" . db_escape(SC_COUNTED);

            db_query($sql, "Could not update item ".$sc_line['stock_id']);
            
            $adj_value = ($counted_qty - $qoh) * $counting_standard_cost;
            
            $total_adjustment_value += $adj_value;
        }

    }
    
    $error_item_count = check_stock_count_complete($stock_count_id);
    if($error_item_count > 0){
        return array("success"=>false, "message"=>"Could not complete $error_item_count stock count items");
    }

    $n2 = update_stock_count_completed($stock_count_id, SC_COMPLETED, SC_COMPLETING, $total_adjustment_value);
    if($n2 == 1){
        return array("success"=>true, "message"=>"Counting completed for stock count $stock_count_id");
    }
    else{
        return array("success"=>false, "message"=>"Could not complete stock count");
    }
}


function check_stock_count_complete($stock_count_id)
{
    $sql = "SELECT COUNT(scd.id) count 
            FROM ".TB_PREF."stock_count_details as scd
            WHERE scd.stock_count_no = $stock_count_id 
                      AND scd.status_id <> ".SC_COMPLETED;

    $result = db_query($sql, "Could not check_stock_count_complete");
    $sc = db_fetch($result);
    if(isset($sc['count']) && $sc['count'] > 0){
        return $sc['count'];
    }
    else{
        return 0;
    }
}

function check_stock_count_processed($stock_count_id)
{
        $sql = "SELECT COUNT(status_id) rec_count  
                   FROM ".TB_PREF."stock_count_details
                   WHERE  stock_count_no = $stock_count_id
                   AND status_id <> ".SC_PROCESSED." 
                        LIMIT 1";
     
        $result = db_query($sql, "Could not retreive not processed stock count lines");
        $row = db_fetch($result);
        if(isset($row['rec_count']) && $row['rec_count'] > 0){
            return false;
        }
        else{
            return true;
        }

}

function update_stock_count_detail_status($row_id, $status_id)
{
        $sql = "UPDATE ".TB_PREF."stock_count_details SET status_id = $status_id
                WHERE id = ".db_escape($row_id);

     db_query($sql,"Stock count details cannot be updated");
   
}

function update_stock_count_item_processed($row_id, $stock_id){

        $sql = "UPDATE ".TB_PREF."stock_count_details 
                SET 
                    status_id = ".SC_PROCESSED." 
		WHERE stock_id = ".db_escape($stock_id)." AND 
                    id = ".db_escape($row_id);

	db_query($sql,"update_stock_count_item_processed");
        
}

function update_stock_count_item_ignored($row_id, $stock_id, $qoh_at_processing, $comment){

        $sql = "UPDATE ".TB_PREF."stock_count_details 
                SET status_id = ".SC_IGNORED." 
                    system_qty_at_processing = ".db_escape($qoh_at_processing)." 
                    comment = ".db_escape($comment)." 
		WHERE stock_id = ".db_escape($stock_id)." AND 
                    id = ".db_escape($row_id);

	db_query($sql,"update_stock_count_item_ignored");
        
}

function update_stock_count_joq_request_id($stock_count_no, $jobq_id)
{
    $sql = "UPDATE ".TB_PREF."stock_counts 
            SET jobq_request_id=".db_escape($jobq_id)." 
            WHERE stock_count_no = ".db_escape($stock_count_no);
    
    db_query($sql,"Could not update stock count jobq id");
}

?>
