<?php

function get_fi_daily_item_issues($cutoff_date, $type, $batch_size)
{
    $sql = "SELECT h.trans_no, 
                    h.tran_date,
                    h.project_id,
                    pm.type_id project_type_id 
            FROM ".TB_PREF."daily_item_issues h     
                JOIN ".TB_PREF."projects_master pm ON 
                    h.project_id = pm.project_id  
            WHERE h.type = ".db_escape($type)."  
                    AND h.tran_date <= ".db_escape($cutoff_date)."  
                    AND h.approval_status = ".db_escape(AS_APPROVED)."  
                    AND h.fi_batch_no = 0 
            ORDER BY h.trans_no LIMIT ".$batch_size;

    return db_query($sql);
}

function get_fi_daily_item_issue_details($tran_type, $im_no)
{
    $sql = "SELECT d.trans_id detail_line_id,
                    d.trans_no,
                    d.stock_id,
                    d.qty quantity, 
                    d.standard_cost unit_cost, 
                    d.task_id,    
                    d.item_description trans_item_description,
                    sm.mb_flag, 
                    sm.inventory_account, 
                    sm.cost_account,
                    sm.adjustment_account, 
                    sm.coc_account, 
                    sm.assembly_account,
                    sm.description 
            FROM ".TB_PREF."daily_item_issue_details d 
                JOIN ".TB_PREF."stock_master sm ON 
                    d.stock_id = sm.stock_id 
            WHERE d.type = ".db_escape($tran_type)." 
                AND d.trans_no = ".db_escape($im_no);
                    

    return db_query($sql);
}

function get_fi_daily_item_issue_total($tran_type, $im_no)
{
    $sql = "SELECT SUM(d.qty * d.standard_cost) total 
            FROM ".TB_PREF."daily_item_issue_details d  
            WHERE d.type = ".db_escape($tran_type)." 
                AND d.trans_no = ".db_escape($im_no);
                    

    $result = db_query($sql);
    return db_result_value($result, "total");
}
