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

function get_ious_for_reopen($iou_id = null, $iou_type = null, $project_id = null, $float_id = null, $employee_code = null, 
                    $date_after = null, $date_before = null)
{

    /*
     SELECT iou.id 
     FROM 0_i_owe_you iou 
            LEFT JOIN 0_i_owe_you_expenses ex on 
               iou.batch_no = ex.iou_batch_no 
            LEFT JOIN 0_i_owe_you_payments pmnt ON 
                    iou.id = pmnt.iou_id 
     WHERE iou.batch_complete = 0 AND 
            iou.advances_batch_no = 0 
     GROUP BY iou.id
     */
    $sql = "SELECT 
                iou.id, 
                iou.batch_no, 
                iou.company_code, 
                iou.employee_code, 
                pm.description project_description,
                iou.float_id,
                fl.name float_name,
                iou.type,
                tn.value_name type_name,
                iou.amount,
                iou.tran_date,
                iou.comments  
		FROM ".TB_PREF."vw_reopenable_ious as re_ious 
                INNER JOIN ".TB_PREF."i_owe_you as iou ON 
                        re_ious.id = iou.id 
                INNER JOIN ".TB_PREF."projects_master as pm ON 
                        iou.project_id = pm.project_id 
                INNER JOIN ".TB_PREF."petty_cash_floats as fl ON 
                        iou.float_id = fl.id 
                INNER JOIN ".TB_PREF."list_values as tn ON 
                        iou.type = tn.id 
		WHERE iou.approval_status =". db_escape(AS_APPROVED);
    
    
        if ($iou_id != null && $iou_id > 0){
                $sql .= "AND re_ious.id=". db_escape($iou_id)." ";
        }
        
        if ($iou_type != null && $iou_type > 0){
            $sql .= "AND iou.type = ".db_escape($iou_type);
        }
        
        if ($project_id != null && $project_id != ''){
            $sql .= "AND iou.project_id = ".db_escape($project_id);
        }
        
        if ($float_id != null && $float_id > 0){
            $sql .= "AND iou.float_id = ".db_escape($float_id);
        }
        
        if ($employee_code != null && $employee_code != ''){
            $sql .= "AND iou.employee_code = ".db_escape($employee_code);
        }
        
        if ($date_after != null){
            $sql .= " AND iou.tran_date >= ".db_escape(date2sql($date_after));
        }

        if ($date_before != null){
            $sql .= " AND iou.tran_date <= ".db_escape(date2sql($date_before));
        }

        $sql .= " ORDER BY iou.id ";
        
        return db_query($sql,"Could not get_ious_for_reopen");
	
  }	

//----------------------------------------------------------------------------------------

function get_sales_order_work_orders($so_no)
{
    $sql = "SELECT wo.* 
            FROM ".TB_PREF."sales_order_details sod,  
                    ".TB_PREF."manufacturing_requirements mr, 
                    ".TB_PREF."workorders wo 
            WHERE sod.id = mr.source_line_id AND 
                    mr.source_type = ". db_escape(ST_SALESORDER)." AND 
                    mr.order_type =  ". db_escape(ST_WORKORDER)." AND 
                    mr.order_no = wo.id AND 
                    sod.order_no = ".db_escape($so_no);
    
    return db_query($sql,"Could not get_sales_order_work_orders");
    
}
  
?>