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


function get_sql_for_pr_issues($date_after_, $date_before_, $pri_number, $pr_number,
                                $location, $project_id, $originator_id)
{
    $date_after = date2sql($date_after_); 
    $date_before = date2sql($date_before_);
        
	$sql = "SELECT
                    pri.pri_no,
                    pri.reference,  
                    pri.source_pr_no,                 
                    l.location_name location,
                    p.description, 
                    pri.created_on,
                    u.real_name originator_name,                   
                    pri.po_no,
                    pri.status,
                    pri.pri_date 

		FROM  ".TB_PREF."purchase_requisition_issues as pri,                     
                        ".TB_PREF."projects_master as p, 
                        ".TB_PREF."locations l,
                        ".TB_PREF."users as u
                    
		WHERE pri.project_id = p.project_id AND 
                        l.loc_code = pri.issuing_location AND                      
                        pri.created_by = u.id AND                       
                        pri.pri_date >= ".db_escape($date_after). " AND 
                        pri.pri_date <= ".db_escape($date_before);


        if (isset($pri_number) && $pri_number > 0){
            $sql .= "AND pri.pri_no = ".db_escape($pri_number);
        }
        
        if (isset($pr_number) && $pr_number > 0){
            $sql .= "AND pri.source_pr_no = ".db_escape($pr_number);
        }
   

		if (isset($location) && $location != ALL_TEXT){
			$sql .= " AND pri.issuing_location = ".db_escape($location);
		}
		
        if (isset($project_id) && $project_id > 0){
			$sql .= " AND pri.project_id =".db_escape($project_id);
		}
        
        if (isset($originator_id) && $originator_id > 0){
			$sql .= " AND pri.created_by =".db_escape($originator_id);
		}


     //   $sql .= " GROUP BY pri.source_pr_no";
        return $sql;
    
}


function view_pr_issue($trans_no)
{
    $sql = "SELECT pri.pri_no,
                    pri.po_no,
                    pri.reference,
                    pri.source_pr_no,     
                    pri.created_on,
                    lo.location_name,
                    pm.description project_description,
                    us.real_name originator_name,
                    mt.name movement_type_name, 
                    pri.*  

            FROM ".TB_PREF."purchase_requisition_issues pri 
                    JOIN ".TB_PREF."locations lo 
                        ON lo.loc_code = pri.issuing_location
                    JOIN ".TB_PREF."projects_master pm 
                        ON pm.project_id = pri.project_id 
                    JOIN ".TB_PREF."users us 
                        ON us.id = pri.created_by 
                    LEFT JOIN ".TB_PREF."movement_types mt 
                        ON pri.movement_type = mt.id              
            WHERE  pri.pri_no=".db_escape($trans_no);
    
    $result = db_query($sql, "view_pr_issue");   
    
    return db_fetch($result);
    
}


function view_pr_issue_details($trans_no)
{
    $sql = "SELECT prid.description,                    
                    prid.item_code,
                    prid.tran_unit, 
                    prid.tran_qty,
                    prid.project_site_id,
                    prid.po_line_id,
                    prid.unit_cost,
                    prid.conversion_factor,
                    ps.description site_name,
                    pt.description task_description,
                    pt.task_code,
                    mov.qty,
                    mov.standard_cost,
                    sm.mb_flag 

            FROM ".TB_PREF."purchase_requistion_issue_details prid 
                JOIN ".TB_PREF."project_tasks pt ON 
                    prid.task_id = pt.task_id 
                JOIN ".TB_PREF."stock_master sm ON 
                    prid.item_code = sm.stock_id 
                LEFT JOIN ".TB_PREF."stock_moves mov ON
                    mov.type = ".db_escape(ST_PR_ISSUE)." AND 
                    mov.stock_id = prid.item_code AND 
                    mov.trans_no = prid.pri_no AND 
                    mov.ref_line_id = prid.id  
                LEFT JOIN ".TB_PREF."project_sites ps ON
                    prid.project_site_id = ps.id 
                
                
            WHERE prid.pri_no=".db_escape($trans_no);
    
      return db_query($sql);    
    
}


?>