<?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_grn_details(){
    
    $sql = "SELECT 
                h.grn_batch_id trans_no,
                h.loc_code trans_no,
                h.delivery_date tran_date,
                h.reference,
                h.project_id,
                h.supplier_id person_id,
                h.reference,
                d.item_code stock_id,
                d.qty_recd quantity,
                d.unit_cost,
                d.tran_unit,
                d.tran_qty,
                d.convertion_factor conversion_factor,
                d.id reference_line_id,
                d.project_site_id,
                d.description item_description,
                0 task_id 
            FROM ".TB_PREF."grn_batch h 
                JOIN ".TB_PREF."grn_items d 
                    ON d.grn_batch_id = h.id 
                JOIN ".TB_PREF."stock_master sm  
                    ON sm.stock_id = d.item_code 
            WHERE sm.mb_flag = 'D'  
            LIMIT 2";
                
    return db_query($sql,"cs_get_non_stock_grn_details");
}

        
function cs_custom_script_create_non_stock_moves()
{
    write_custom_script_log('Start');
    
    $q = cs_get_non_stock_grn_details();

    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['trans_no'];
        $location = $row['loc_code'];
        $date_ = sql2date($row['tran_date']);
        $reference = $row['reference'];
        $stock_id = $row['stock_id'];
        $quantity = $row['quantity'];
        $std_cost = $row['unit_cost'];
        $price = $row['unit_cost'];
        $tran_unit = $row['tran_unit'];
        $tran_qty = $row['tran_qty'];
        $conversion_factor = $row['conversion_factor'];
        $reference_line_id = $row['reference_line_id'];
        $project_id = $row['project_id'];
        $person_id = $row['person_id'];
        $task_id = $row['task_id'];
        $project_site_id = $row['project_site_id'];
        $item_description = $row['item_description'];


            
//            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);
            
            add_non_stock_move($type, $stock_id, $trans_no, $location, $date_, $reference, $quantity, $std_cost, 
                                $person_id, 1, $price, 0, $tran_unit, $tran_qty, $conversion_factor, $task_id, 
                                $project_site_id, $reference_line_id, $item_description, $project_id);
        
    }
        
        //return;
    
}

