<?php

//C:\xampp\htdocs\saw\admin\db\list_values_db.inc
//C:\xampp\htdocs\saw\projects\includes\db\project_boq_items_db.inc
        
function cs_get_non_stock_to_stock_grn_details($stock_id){
    
    $sql = "SELECT d.*,
                h.loc_code, h.delivery_date, h.reference, h.project_id  
            FROM ".TB_PREF."grn_batch h 
                JOIN ".TB_PREF."grn_items d 
                    ON d.grn_batch_id = h.id 
            WHERE d.item_code = ".db_escape($stock_id)." 
            ORDER BY h.id 
            LIMIT 200";
                
    return db_query($sql,"Could not cs_get_non_stock_to_stock_grn_details");
}

function cs_grn_rec_exists_stock_move($stock_id, $trans_type, $grn_batch_no, $po_detail_item_id){
    
    $sql = "SELECT  IF(trans_id > 0, trans_id, 0) id   
            FROM ".TB_PREF."stock_moves 
            WHERE stock_id = ".db_escape($stock_id)." 
                    AND type = ".db_escape($trans_type)." 
                    AND trans_no = ".db_escape($grn_batch_no)." 
                    AND ref_line_id = ".db_escape($po_detail_item_id);
    
    $result = db_query($sql,"Could not cs_get_non_stock_to_stock_grn_details");
    
    return db_result_value($result, 'id'); 
}



        
function cs_custom_script_non_stock_to_stock_grn_details()
{
    write_custom_script_log('Start');
    
    $stock_id = 8888;

    write_custom_script_log('Item: '.$stock_id);
    
    $q = cs_get_non_stock_to_stock_grn_details($stock_id);

    if(!(db_num_rows($q) > 0)){
        write_custom_script_log('No items found');
        return;
    }
    while ($row = db_fetch($q)) {

        
        $type = ST_SUPPRECEIVE;
        $trans_no = $row['grn_batch_id'];
        $location = $row['loc_code'];
        $date_ = sql2date($row['delivery_date']);
        $reference = $row['reference'];
        $quantity = $row['qty_recd'];
        $std_cost = $row['unit_cost'];
        $price = $row['unit_cost'];
        $tran_unit = $row['tran_unit'];
        $tran_qty = $row['tran_qty'];
        $convertion_factor = $row['convertion_factor'];
        $reference_line_id = $row['po_detail_item'];
        $project_id = $row['project_id'];
        
        $trans_id = cs_grn_rec_exists_stock_move($stock_id, $type, $trans_no, $reference_line_id);
        
        if($trans_id > 0){
            write_custom_script_log("stock_moves rec: trans_id=".$trans_id.", stock_id=".$stock_id.", type=".$type.", trans_no=".$trans_no.", po_detail_item=".$reference_line_id);
        }else{
            $material_cost = update_average_material_cost(null, $stock_id, $std_cost, $quantity, $date_);
            
            add_stock_move($type, $stock_id, $trans_no, $location,
                            $date_, $reference, $quantity, $material_cost, 
                            $person_id=0, $show_or_hide=1, $price, $discount_percent=0, 
                            $error_msg="", $tran_unit, $tran_qty, $convertion_factor,
                            $task_id = 0, $project_site_id = 0, $reference_line_id, $project_id);
        } 
    }
        
        //return;
    
}

