<?php



function fi_get_remaining_transactions_count($type, $cutoff_date, $batch_size=100000000)
{
    return fi_get_remaining_transactions_result($type, $cutoff_date, $batch_size, true);
}

function fi_get_remaining_transactions_details($type, $cutoff_date, $batch_size=100000000)
{
    return fi_get_remaining_transactions_result($type, $cutoff_date, $batch_size, false);
}


function fi_get_remaining_transactions_result($type, $cutoff_date, $batch_size, $count)
{
    global $path_to_root;
     
        switch ($type) {
            // Inventory
            case ST_MATERIAL_ISSUE:
                return fi_check_material_issues($cutoff_date, $batch_size, $count);

            case ST_MATERIAL_RETURN:
                return fi_check_material_returns($cutoff_date, $batch_size, $count);

            case ST_SITE_MATERIAL_RETURN:
                return fi_check_site_material_returns($cutoff_date, $batch_size, $count);
                
            case ST_INVENTORY_RECEIPT:
            case ST_INVENTORY_ISSUE:
            case ST_INVADJUST:
                return fi_check_inventory_adjustments($cutoff_date, $type, $batch_size, $count);
                
            case ST_STOCK_COUNT:
                return fi_check_stock_counts($cutoff_date, $type, $batch_size, $count);
                
            case ST_ITEM_COSTUPDATE:
                return fi_check_stock_item_cost_updates($cutoff_date, $batch_size, $count);       
                
            case ST_DAILY_ITEM_ISSUE:
            case ST_DAILY_ITEM_RETURN:
                return fi_check_daily_item_issues($cutoff_date, $type, $batch_size, $count);                        
                    
            case ST_PETTY_CASH_ITEM_RECEIPT:
                return fi_check_petty_cash_item_receipts($cutoff_date, $batch_size, $count);            
            
                
            // GL
            case ST_UTILITY_BILLS:
                return fi_check_utility_bills($cutoff_date, $batch_size, $count);
                
            case ST_UTILITY_BILL_CREDIT_NOTE:
                return fi_check_utility_bill_credit_notes($cutoff_date, $batch_size, $count);
            
            
            case ST_PROJECT_INVOICE:
                return fi_check_project_invoices($cutoff_date, $batch_size, $count);
                
            case ST_PROJECT_CREDIT_NOTE:
                return fi_check_project_credit_notes($cutoff_date, $batch_size, $count);

            case ST_SUBCONTRACTOR_INVOICE:
                return fi_check_subcontractor_invoices($cutoff_date, $batch_size, $count);
                
            case ST_SUBCONTRACTOR_CREDIT_NOTE:
                return fi_check_subcontractor_credit_notes($cutoff_date, $batch_size, $count);
                
            case ST_MISCELLANEOUS_PROJECT_COST:
                return fi_check_miscellaneous_project_costs($cutoff_date, $batch_size, $count); 

            
            
            case ST_SALESINVOICE:
                return fi_check_sales_invoices($cutoff_date, $type, $batch_size, $count);
                
            case ST_CUSTDELIVERY:
                return fi_check_sales_deliveries($cutoff_date, $type, $batch_size, $count);
                
            case ST_CUSTOMER_PAYMENT_RETURN:  
                return fi_check_sales_invoice_returns($cutoff_date, $type, $batch_size, $count);
                
            case ST_CUSTCREDIT:
                return fi_check_customer_credit_notes($cutoff_date, $type, $batch_size, $count);
                
            case ST_CUSTPAYMENT:        
                return fi_check_customer_payments($cutoff_date, $type, $batch_size, $count);
            
            
            
            case ST_SUPPRECEIVE:
                return fi_check_goods_received_notes($cutoff_date, $batch_size, $count);

            case ST_SUPPINVOICE:
                return fi_check_supplier_invoices($cutoff_date, $batch_size, $count);
                
            case ST_SUPPCREDIT:
                return fi_check_supplier_credit_notes($cutoff_date, $batch_size, $count);
                
            case ST_SUPPAYMENT: 
                return fi_check_supplier_payments($cutoff_date, $type, $batch_size, $count);
                
            case ST_PR_ISSUE:   
                return fi_check_pr_issues($cutoff_date, $batch_size, $count);
                
            case ST_PR_RETURN: 
                return fi_check_pr_returns($cutoff_date, $batch_size, $count);
                
            
            
            
            case ST_PETTY_CASH_FLOAT_ADJ:
                return fi_check_petty_cash_float_adjustments($cutoff_date, $batch_size, $count);
                
            case ST_PETTY_CASH_FLOAT_REIMBUR:              
                return fi_check_petty_cash_float_reimbursements($cutoff_date, $batch_size, $count);
                
            case ST_I_OWE_YOU_BATCH:              
                return fi_check_i_owe_you_expense_batches($cutoff_date, $batch_size, $count);
                
            case ST_PETTY_CASH_DIRECT_PAYMENTS:              
                return fi_check_petty_cash_direct_payments($cutoff_date, $batch_size, $count);
                
            case ST_I_OWE_YOU_PAYMENT:
            case ST_PETTY_CASH_PAYMENT:
            case ST_I_OWE_YOU_ADVANCES:              
                return fi_check_fi_i_owe_you_payments($cutoff_date, $type, $batch_size, $count);
            
            
            // Assets
            case ST_ASSET_QUANTITY_ADJUSTMENT:              
                return fi_check_asset_quantity_adjustments($cutoff_date, $batch_size, $count);
            case ST_STOCK_ASSET_TRANSFER:              
                return fi_check_stock_asset_transfers($cutoff_date, $batch_size, $count);
                
                
            // Transport
            case ST_SUPPLIER_TRANSPORT_INVOICES:              
                return fi_check_supplier_transport_invoices($cutoff_date, $batch_size, $count);
                
            
                        
            case ST_COMP_LOAN_REPAYMENT:             
                return fi_check_company_loan_repayments($cutoff_date, $batch_size, $count);
                
            
            case ST_PAYROLL:
                return 2222;
                return fi_check_payroll_summaries($cutoff_date, $batch_size, $count);
                
            
                        

//            case ST_TOOL_USAGE_CHARGE:
//                fi_tool_usage_charges($batch_no, $cutoff_date_);
//                break;
//            case ST_ASSET_DEPRECIATION:
//                fi_asset_depreciation($batch_no, $cutoff_date_);
//                break;
//            case ST_ASSET_QUANTITY_ADJUSTMENT:
//                fi_asset_quantity_adjustments($batch_no, $cutoff_date_);
//                break;
//            case ST_STOCK_ASSET_TRANSFER:
//                fi_stock_asset_transfers($batch_no, $cutoff_date_);
//                break;

            
            
            default:
                break;
        }
        
        return 0;
    
}


function fi_check_fi_i_owe_you_payments($cutoff_date, $tran_type, $batch_size, $count=true){

    if($tran_type == ST_I_OWE_YOU_PAYMENT){
        $result = get_fi_i_owe_you_payments($cutoff_date, $batch_size);
    }
    elseif($tran_type == ST_PETTY_CASH_PAYMENT){
        $result = get_fi_petty_cash_payments($cutoff_date, $batch_size);
    }
    elseif($tran_type == ST_I_OWE_YOU_ADVANCES){
        $result = get_fi_i_owe_you_advances($cutoff_date, $batch_size);
    }
    
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_payroll_summaries($cutoff_date, $batch_size, $count=true){

    $payroll_type_code = 'MAIN';
    
    $company_code = 'SAW';
    $db_name = get_group_company_db_name($company_code);    
    $result =  get_fi_group_company_processed_payroll_runs_by_payroll_type($db_name, $payroll_type_code, $cutoff_date, $batch_size);
    $n = db_num_rows($result);
    
    $company_code = 'SAWC';
    $db_name = get_group_company_db_name($company_code);   
    $result =  get_fi_group_company_processed_payroll_runs_by_payroll_type($db_name, $payroll_type_code, $cutoff_date, $batch_size);
    $nSAWC = db_num_rows($result);
    if($n < $nSAWC){
        $n = $nSAWC;
    }
    
    $company_code = 'LPS';
    $db_name = get_group_company_db_name($company_code);   
    $result =  get_fi_group_company_processed_payroll_runs_by_payroll_type($db_name, $payroll_type_code, $cutoff_date, $batch_size);
    $nLPS = db_num_rows($result);
    if($n < $nLPS){
        $n = $nLPS;
    }
    
    return $n;
    
}


function fi_check_company_loan_repayments($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_company_loan_repayments($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_supplier_transport_invoices($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_supplier_transport_invoices($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_petty_cash_direct_payments($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_petty_cash_direct_payments($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_i_owe_you_expense_batches($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_i_owe_you_expense_batches($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_petty_cash_float_reimbursements($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_petty_cash_float_reimbursements($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_petty_cash_float_adjustments($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_petty_cash_float_adjustments($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_pr_returns($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_pr_returns($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_pr_issues($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_pr_issues($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_supplier_payments($cutoff_date, $trans_type, $batch_size, $count=true){

    $result =  get_fi_supplier_payments($trans_type, $cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_supplier_credit_notes($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_supplier_credit_notes($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_supplier_invoices($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_supplier_invoices($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_goods_received_notes($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_goods_received_notes($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_customer_payments($cutoff_date, $trans_type, $batch_size, $count=true){

    $result =  get_fi_customer_payments($trans_type, $cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_customer_credit_notes($cutoff_date, $trans_type, $batch_size, $count=true){

    $result =  get_fi_customer_credit_notes($trans_type, $cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_sales_invoice_returns($cutoff_date, $trans_type, $batch_size, $count=true){

    $result =  get_fi_sales_invoice_returns($trans_type, $cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_sales_deliveries($cutoff_date, $trans_type, $batch_size, $count=true){

    $result =  get_fi_sales_deliveries($trans_type, $cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_sales_invoices($cutoff_date, $trans_type, $batch_size, $count=true){

    $result =  get_fi_sales_invoices($trans_type, $cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_subcontractor_credit_notes($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_subcontractor_credit_notes($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_subcontractor_invoices($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_subcontractor_invoices($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_project_credit_notes($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_project_credit_notes($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_project_invoices($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_project_invoices($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_miscellaneous_project_costs($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_miscellaneous_project_costs($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_utility_bill_credit_notes($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_utility_bill_credit_notes($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_utility_bills($cutoff_date, $batch_size, $count=true){

    $result =  get_fi_utility_bills($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_stock_item_cost_updates($cutoff_date, $batch_size, $count=true){

    $result = get_fi_stock_item_cost_updates($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_stock_counts($cutoff_date, $trans_type, $batch_size, $count=true){

    $result = get_fi_stock_counts($cutoff_date, $trans_type, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_inventory_adjustments($cutoff_date, $tran_type, $batch_size, $count=true){

    $result = get_fi_inventory_adjustments($cutoff_date, $tran_type, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}

function fi_check_site_material_returns($cutoff_date, $batch_size, $count=true){

    $result = get_fi_site_material_returns($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}

function fi_check_material_returns($cutoff_date, $batch_size, $count=true){

    $result = get_fi_material_returns($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_material_issues($cutoff_date, $batch_size, $count=true){

    $result = get_fi_material_issues($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
    
}


function fi_check_daily_item_issues($cutoff_date, $type, $batch_size, $count=true){

    $result = get_fi_daily_item_issues($cutoff_date, $type, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}


function fi_check_petty_cash_item_receipts($cutoff_date, $batch_size, $count=true){

    $result = get_fi_petty_cash_item_receipts($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}




function fi_check_asset_quantity_adjustments($cutoff_date, $batch_size, $count=true){

    $result = get_fi_asset_quantity_adjustment_headers($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}

function fi_check_stock_asset_transfers($cutoff_date, $batch_size, $count=true){

    $result = get_fi_stock_asset_transfer_headers($cutoff_date, $batch_size);
    if($count){
        return db_num_rows($result);
    }else{
        return $result;
    }
    
}