<?php

function dashboard_get_petty_cash_i_owe_yous_details($from_date, $to_date, $approval_status_array=null, $not_ap_st_array=null, $rec_count=false) {
    $sql = "SELECT DISTINCT iou.*, pm.description project_name 
            FROM ".TB_PREF."i_owe_you iou 
                JOIN ".TB_PREF."projects_master pm 
                    ON iou.project_id = pm.project_id 
            WHERE iou.tran_date >= ".db_escape($from_date)." 
                AND iou.tran_date <= ".db_escape($to_date);
    
        if (is_array($approval_status_array)) {
            $sql .= " AND iou.approval_status IN (".implode(',', $approval_status_array).")";
        }
        if (is_array($not_ap_st_array)) {
            $sql .= " AND iou.approval_status NOT IN (".implode(',', $not_ap_st_array).")";
        }
        
        $result = db_query($sql, "dashboard_get_petty_cash_i_owe_yous_details");
        
        if ($rec_count === true) {
            return db_num_rows($result);
        } else {
            return $result;
        }
}

function dashboard_get_petty_cash_i_owe_yous_expenses_entered_details($from_date, $to_date, $rec_count=false) {
    $sql = "SELECT DISTINCT iou.*, pm.description project_name 
            FROM ".TB_PREF."i_owe_you iou 
                JOIN ".TB_PREF."projects_master pm 
                    ON iou.project_id = pm.project_id 
            WHERE iou.tran_date >= ".db_escape($from_date)." 
                AND iou.tran_date <= ".db_escape($to_date)." 
                AND iou.batch_no > 0";
    
        $result = db_query($sql, "dashboard_get_petty_cash_i_owe_yous_expenses_entered_details");
        
        if ($rec_count === true) {
            return db_num_rows($result);
        } else {
            return $result;
        }
}

function dashboard_get_petty_cash_i_owe_yous_completed_details($from_date, $to_date, $rec_count=false) {
    $sql = "SELECT DISTINCT iou.*, pm.description project_name 
            FROM ".TB_PREF."i_owe_you iou 
                JOIN ".TB_PREF."projects_master pm 
                    ON iou.project_id = pm.project_id 
            WHERE iou.tran_date >= ".db_escape($from_date)." 
                AND iou.tran_date <= ".db_escape($to_date)." 
                AND iou.batch_complete = 1";
    
        $result = db_query($sql, "dashboard_get_petty_cash_i_owe_yous_completed_details");
        
        if ($rec_count === true) {
            return db_num_rows($result);
        } else {
            return $result;
        }
}

function dashboard_get_petty_cash_expenses_details($from_date, $to_date, $approval_status_array=null, $not_ap_st_array=null, $rec_count=false) {
    $sql = "SELECT DISTINCT pch.*, pm.description project_name, bt.approval_status 
            FROM ".TB_PREF."petty_cash_header pch  
                JOIN ".TB_PREF."i_owe_you_batches bt 
                    ON pch.batch_no = bt.batch_no  
                JOIN ".TB_PREF."projects_master pm 
                    ON pch.project_id = pm.project_id 
            WHERE pch.tran_date >= ".db_escape($from_date)." 
                AND pch.tran_date <= ".db_escape($to_date);
    
        if (is_array($approval_status_array)) {
            $sql .= " AND bt.approval_status IN (".implode(',', $approval_status_array).")";
        }
        if (is_array($not_ap_st_array)) {
            $sql .= " AND bt.approval_status NOT IN (".implode(',', $not_ap_st_array).")";
        }
        
        $result = db_query($sql, "dashboard_get_petty_cash_expenses_details");
        
        if ($rec_count === true) {
            return db_num_rows($result);
        } else {
            return $result;
        }
}

function dashboard_get_petty_cash_expenses_completed_details($from_date, $to_date, $rec_count=false) {
    $sql = "SELECT DISTINCT pch.*, pm.description project_name, bt.approval_status 
            FROM ".TB_PREF."petty_cash_header pch  
                JOIN ".TB_PREF."i_owe_you_batches bt 
                    ON pch.batch_no = bt.batch_no  
                JOIN ".TB_PREF."projects_master pm 
                    ON pch.project_id = pm.project_id 
            WHERE pch.tran_date >= ".db_escape($from_date)." 
                AND pch.tran_date <= ".db_escape($to_date)." 
                AND pch.batch_complete = 1 ";
    
        $result = db_query($sql, "dashboard_get_petty_cash_expenses_completed_details");
        
        if ($rec_count === true) {
            return db_num_rows($result);
        } else {
            return $result;
        }
}

function dashboard_get_petty_cash_expense_batches_details($from_date, $to_date, $approval_status_array=null, $not_ap_st_array=null, $rec_count=false) {
    $sql = "SELECT DISTINCT bt.*, pm.description project_name, bt.batch_no id 
            FROM ".TB_PREF."i_owe_you_batches bt 
                JOIN ".TB_PREF."projects_master pm 
                    ON bt.project_id = pm.project_id 
            WHERE bt.tran_date >= ".db_escape($from_date)." 
                AND bt.tran_date <= ".db_escape($to_date);
    
        if (is_array($approval_status_array)) {
            $sql .= " AND bt.approval_status IN (".implode(',', $approval_status_array).")";
        }
        if (is_array($not_ap_st_array)) {
            $sql .= " AND bt.approval_status NOT IN (".implode(',', $not_ap_st_array).")";
        }
        
        $result = db_query($sql, "dashboard_get_petty_cash_expense_batches_details");
        
        if ($rec_count === true) {
            return db_num_rows($result);
        } else {
            return $result;
        }
}

function dashboard_get_petty_cash_expense_batches_checked_details($from_date, $to_date, $rec_count=false) {
    $sql = "SELECT DISTINCT bt.*, pm.description project_name, bt.batch_no id  
            FROM ".TB_PREF."i_owe_you_batches bt 
                JOIN ".TB_PREF."projects_master pm 
                    ON bt.project_id = pm.project_id 
            WHERE bt.tran_date >= ".db_escape($from_date)." 
                AND bt.tran_date <= ".db_escape($to_date)." 
                AND bt.checked = 1 ";
        
        $result = db_query($sql, "dashboard_get_petty_cash_expense_batches_checked_details");
        
        if ($rec_count === true) {
            return db_num_rows($result);
        } else {
            return $result;
        }
}

function dashboard_get_petty_cash_expense_batches_completed_details($from_date, $to_date, $rec_count=false) {
    $sql = "SELECT DISTINCT bt.*, pm.description project_name, bt.batch_no id  
            FROM ".TB_PREF."i_owe_you_batches bt 
                JOIN ".TB_PREF."projects_master pm 
                    ON bt.project_id = pm.project_id 
            WHERE bt.tran_date >= ".db_escape($from_date)." 
                AND bt.tran_date <= ".db_escape($to_date)." 
                AND bt.complete = 1 ";
        
        $result = db_query($sql, "dashboard_get_petty_cash_expense_batches_completed_details");
        
        if ($rec_count === true) {
            return db_num_rows($result);
        } else {
            return $result;
        }
}

?>