<?php
/**********************************************************************
***********************************************************************/



function get_batch_item_issue_selected_distinct_pr_transactions($type, $trans_no, $source_type_id=0)
{
     
    $sql = " SELECT DISTINCT biit.type,
                            biit.trans_no,
                            biit.source_type_id,
                            biit.source_type_no,
                            biit.movement_type,
                            biit.linked_field_type,
                            biit.linked_field_id,
                            biit.group_company_code,
                            pod.order_no po_no,                             
                            
                            biid.employee_company_code,
                            biid.employee_code 
            FROM ".TB_PREF."batch_item_issue_transactions biit 
                JOIN ".TB_PREF."batch_item_issue_details biid 
                    ON biid.id = biit.bii_line_id  
                JOIN ".TB_PREF."purch_order_details pod  
                    ON pod.po_detail_item = biit.po_line_id   
            WHERE biid.type = ".db_escape($type)." 
                AND biid.trans_no = ".db_escape($trans_no)." 
                AND biit.source_type_id = ".db_escape($source_type_id)." 
                AND biit.target_line_id = 0 ";
    
    
    $sql .= " ORDER BY biit.source_type_id, biit.source_type_no ";

    return db_query($sql, "get_batch_item_issue_selected_prs");

}



function get_batch_item_issue_pr_line_transaction_count($type_id, $trans_no, $source_type_id, $source_line_id, 
                    $movement_type, $linked_field_type, $linked_field_id, $group_company_code, $po_no,
                    $employee_company_code, $employee_code)
{
    $sql = "SELECT COUNT(biit.id) row_cont 
            FROM ".TB_PREF."batch_item_issue_transactions biit 
                JOIN ".TB_PREF."batch_item_issue_details biid 
                    ON biid.id = biit.bii_line_id  
                JOIN ".TB_PREF."purch_order_details pod  
                    ON pod.po_detail_item = biit.po_line_id   
            WHERE biit.type = ".db_escape($type_id)." 
                AND biit.trans_no = ".db_escape($trans_no)." 
                AND biit.source_type_id = ".db_escape($source_type_id)." 
                AND biit.source_line_id = ".db_escape($source_line_id)." 
                AND biit.movement_type = ".db_escape($movement_type)." 
                AND biit.linked_field_type = ".db_escape($linked_field_type)." 
                AND biit.linked_field_id = ".db_escape($linked_field_id)." 
                AND biit.group_company_code = ".db_escape($group_company_code)."  
                AND pod.order_no = ".db_escape($po_no)."     
                AND biid.employee_company_code = ".db_escape($employee_company_code)." 
                AND biid.employee_code = ".db_escape($employee_code); 
    
    $result = db_query($sql, "get_batch_item_issue_pr_line_transaction_count");
    
    return db_result_value($result, "row_cont");
}



function get_batch_item_issue_pr_line_transaction($type_id, $trans_no, $source_type_id, $source_line_id, 
                    $movement_type, $linked_field_type, $linked_field_id, $group_company_code, $po_no,
                    $employee_company_code, $employee_code)
{
    
    $sql = "SELECT biit.tran_unit, 
                    biit.tran_qty, 
                    biit.project_site_id 
            FROM ".TB_PREF."batch_item_issue_transactions biit 
                JOIN ".TB_PREF."batch_item_issue_details biid 
                    ON biid.id = biit.bii_line_id  
                JOIN ".TB_PREF."purch_order_details pod  
                    ON pod.po_detail_item = biit.po_line_id     
            WHERE biit.type = ".db_escape($type_id)." 
                AND biit.trans_no = ".db_escape($trans_no)." 
                AND biit.source_type_id = ".db_escape($source_type_id)." 
                AND biit.source_line_id = ".db_escape($source_line_id)." 
                AND biit.movement_type = ".db_escape($movement_type)." 
                AND biit.linked_field_type = ".db_escape($linked_field_type)." 
                AND biit.linked_field_id = ".db_escape($linked_field_id)." 
                AND biit.group_company_code = ".db_escape($group_company_code)."  
                AND pod.order_no = ".db_escape($po_no)."     
                AND biid.employee_company_code = ".db_escape($employee_company_code)." 
                AND biid.employee_code = ".db_escape($employee_code); 
    
    $result = db_query($sql, "get_batch_item_issue_pr_line_transaction_count");
    
    return db_fetch($result);
}


function get_batch_item_issue_pr_line_transaction_summary($type_id, $trans_no, $source_type_id, $source_line_id, 
                    $movement_type, $linked_field_type, $linked_field_id, $group_company_code, $po_no,
                    $employee_company_code, $employee_code)
{

    $sql = "SELECT sm.units base_unit,
                    SUM(biit.qty) base_unit_qty_sum, 
                    biit.project_site_id 
            FROM ".TB_PREF."batch_item_issue_transactions biit 
                JOIN ".TB_PREF."batch_item_issue_details biid 
                    ON biid.id = biit.bii_line_id  
                JOIN ".TB_PREF."purch_order_details pod  
                    ON pod.po_detail_item = biit.po_line_id      
                JOIN ".TB_PREF."stock_master sm  
                    ON sm.stock_id = biid.stock_id 
            WHERE biit.type = ".db_escape($type_id)." 
                AND biit.trans_no = ".db_escape($trans_no)." 
                AND biit.source_type_id = ".db_escape($source_type_id)." 
                AND biit.source_line_id = ".db_escape($source_line_id)." 
                AND biit.movement_type = ".db_escape($movement_type)." 
                AND biit.linked_field_type = ".db_escape($linked_field_type)." 
                AND biit.linked_field_id = ".db_escape($linked_field_id)." 
                AND biit.group_company_code = ".db_escape($group_company_code)."   
                AND pod.order_no = ".db_escape($po_no)."     
                AND biid.employee_company_code = ".db_escape($employee_company_code)." 
                AND biid.employee_code = ".db_escape($employee_code)." 
            GROUP BY biit.qty ";
    
    $result = db_query($sql, "get_batch_item_issue_pr_line_transaction_count");
    
    return db_fetch($result);
}



function update_batch_item_issue_pr_transaction_target_details($type_id, $trans_no, $source_type_id, $source_line_id, 
                                                                $movement_type, $linked_field_type, $linked_field_id, $group_company_code, $po_no,                                                               
                                                                $employee_company_code, $employee_code, 
                                                                $target_type_id, $target_type_no, $target_line_id)
{
    $sql = "UPDATE ".TB_PREF."batch_item_issue_transactions biit  
                JOIN ".TB_PREF."batch_item_issue_details biid 
                    ON biid.id = biit.bii_line_id  
                JOIN ".TB_PREF."purch_order_details pod  
                    ON pod.po_detail_item = biit.po_line_id      
            SET biit.target_type_id = ".db_escape($target_type_id).", 
                biit.target_type_no = ".db_escape($target_type_no).", 
                biit.target_line_id = ".db_escape($target_line_id)."  
                    
            WHERE biit.type = ".db_escape($type_id)." 
                AND biit.trans_no = ".db_escape($trans_no)." 
                AND biit.source_type_id = ".db_escape($source_type_id)." 
                AND biit.source_line_id = ".db_escape($source_line_id)." 
                AND biit.movement_type = ".db_escape($movement_type)." 
                AND biit.linked_field_type = ".db_escape($linked_field_type)." 
                AND biit.linked_field_id = ".db_escape($linked_field_id)." 
                AND biit.group_company_code = ".db_escape($group_company_code)."  
                AND pod.order_no = ".db_escape($po_no)."  
                AND biid.employee_company_code = ".db_escape($employee_company_code)."  
                AND biid.employee_code = ".db_escape($employee_code)."  
                AND biit.target_line_id = 0 ";
    
    db_query($sql, "update_batch_item_issue_transaction_target_details");
    
    return db_num_affected_rows();
}