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


function write_project_dispatch_request_header($pdr_no, $project_id, $debtor_no, $customer_branch, $reference,
                                                $tran_date, $site_id, $location_name, $pso_id, $tax_group_id)                      
                                             
{
    begin_transaction();
    
    if($pdr_no > 0){        
        update_project_dispatch_request_header($pdr_no, $project_id, $debtor_no, $customer_branch, $reference,
                                                $tran_date, $site_id, $location_name);
    }else{
        
        $pdr_no = add_project_dispatch_request_header($pdr_no, $project_id, $debtor_no, $customer_branch, $reference,
                                                        $tran_date, $site_id, $location_name, $pso_id, $tax_group_id);
    }
    
    if(!($pdr_no>0)){
        return;
    }
        
    commit_transaction();    
    return $pdr_no;
}


function write_project_dispatch_request_line($pdr_no, $pdr_line_id, $type, $description, 
                                                $item_id, $tran_unit, $tran_unit_rate, $tran_unit_qty, 
                                                $line_comment, $task_id, $conversion_factor, $quantity, $pso_detail_id)
{                
    if($pdr_line_id > 0){         
        update_project_dispatch_request_line($pdr_line_id, $type, $description, $item_id,
                                                $tran_unit, $tran_unit_rate, $tran_unit_qty, $line_comment, 
                                                $task_id, $conversion_factor, $quantity, $pso_detail_id);         
    }else{
        $pdr_line_id = add_project_dispatch_request_line($pdr_no, $type, $description, $item_id,
                                                            $tran_unit, $tran_unit_rate, $tran_unit_qty, $line_comment, 
                                                            $task_id, $conversion_factor, $quantity, $pso_detail_id);
    }
    
}



function update_project_dispatch_request_header($pdr_no, $project_id, $debtor_no, $customer_branch, 
                                                    $reference, $tran_date, $site_id, $location_name)
     
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
    $sql = "UPDATE ".TB_PREF."project_dispatch_requests
            SET 
                project_id=".db_escape($project_id).",
                debtor_no=".db_escape($debtor_no).",
                branch_code=".db_escape($customer_branch).", 
                reference=".db_escape($reference).",               
                tran_date=".db_escape(date2sql($tran_date)).",                             
                site_id=".db_escape($site_id).", 
                location=".db_escape($location_name).", 
                updated_by=".db_escape($updated_by).",
                updated_on=".db_escape($updated_on)."                  
            WHERE id=".db_escape($pdr_no);
    
    db_query($sql);
    return db_num_affected_rows();  
    
}


function add_project_dispatch_request_header($pdr_no, $project_id, $debtor_no, $customer_branch, $reference,
                                                $tran_date, $site_id, $location_name, $pso_id, $tax_group_id)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."project_dispatch_requests
                  ( project_id, debtor_no, 
                    branch_code, reference, 
                    tran_date, site_id, location, pso_id,
                    tax_group_id,
                    created_by, created_on )  
                          
                VALUES 
                
                (".db_escape($project_id).", ".db_escape($debtor_no).", 
                    ".db_escape($customer_branch).", ".db_escape($reference).",
                    ".db_escape(date2sql($tran_date)).",
                    ".db_escape($site_id).",
                    ".db_escape($location_name).", ".db_escape($pso_id).",
                    ".db_escape($tax_group_id).",
                    ".db_escape($created_by).", ".db_escape($created_on).")";
     
    db_query($sql);
    return db_insert_id();        
}


function add_project_dispatch_request_line($pdr_no, $type, $description, $item_id,
                                            $tran_unit, $tran_unit_rate, $tran_unit_qty, $line_comment, 
                                            $task_id, $conversion_factor, $quantity, $pso_detail_id)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
     $sql = "INSERT INTO ".TB_PREF."project_dispatch_request_details
              ( pdr_no, type, 
                description, item_id, task_id,
                tran_unit, tran_unit_rate, tran_unit_qty,
                line_comment, conversion_factor, quantity, pso_detail_id, 
                created_by, created_on )
            VALUES
                (".db_escape($pdr_no).", ".db_escape($type).", 
                    ".db_escape($description).", ".db_escape($item_id).", ".db_escape($task_id).",
                    ".db_escape($tran_unit).", ".db_escape($tran_unit_rate).", ".db_escape($tran_unit_qty).",
                    ".db_escape($line_comment).", ".db_escape($conversion_factor).",
                    ".db_escape($quantity).", ".db_escape($pso_detail_id).", 
                    ".db_escape($created_by).", ".db_escape($created_on).")";
            
    db_query($sql);
  
}


function update_project_dispatch_request_line($pi_detail_row_id, $type, $description, $item_id,
                                                $tran_unit, $tran_unit_rate, $tran_unit_qty, $line_comment, 
                                                $task_id, $conversion_factor, $quantity, $pso_detail_id)
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
     $sql = "UPDATE ".TB_PREF."project_dispatch_request_details
                SET 
                    task_id=".db_escape($task_id).", 
                    tran_unit=".db_escape($tran_unit).", 
                    tran_unit_rate=".db_escape($tran_unit_rate).", 
                    tran_unit_qty=".db_escape($tran_unit_qty).",  
                    line_comment=".db_escape($line_comment).",
                    conversion_factor=".db_escape($conversion_factor).",
                    quantity=".db_escape($quantity).",
                    pso_detail_id=".db_escape($pso_detail_id).",   
                    updated_by=".db_escape($updated_by).",
                    updated_on=".db_escape($updated_on)."                
                WHERE id=".db_escape($pi_detail_row_id);
     
    db_query($sql);
    return db_num_affected_rows();  
    
}

function get_project_dispatch_request_header($pi_no)
{
    $sql = "SELECT pso.*,
                    pm.description project_description,
                    tg.name tax_group_name 
            FROM ".TB_PREF."project_dispatch_requests pso 
                    INNER JOIN ".TB_PREF."projects_master pm ON 
                            pso.project_id = pm.project_id 
                    INNER JOIN ".TB_PREF."tax_groups tg ON 
                            pso.tax_group_id = tg.id 
                  
            WHERE pso.id=".db_escape($pi_no);
    
    $result = db_query($sql, "Could not get_project_dispatch_request_header");
    return db_fetch($result);   
    
}


function get_pdr_details_manual($order_no)
{
    $sql = "SELECT pdrd.*, u.name unit_name, pt.description budget
        
            FROM ".TB_PREF."project_dispatch_request_details pdrd 
                    LEFT JOIN ".TB_PREF."item_units u  ON 
                            pdrd.tran_unit = u.abbr 
                            
                    LEFT JOIN ".TB_PREF."project_tasks pt ON
                            pt.task_id = pdrd.task_id
                   
            WHERE pdrd.pdr_no=".db_escape($order_no)." AND 
                    pdrd.pso_detail_id = 0";
    
    return db_query($sql, "Could not get_project_dispatch_request_details");
    
}

function get_pdr_detail_record($selected_id)
{
     $sql = "SELECT * 
                    
             FROM ".TB_PREF."project_dispatch_request_details  
                    
             WHERE id=".db_escape($selected_id);
     
     $result = db_query($sql, "Could not get_pdr_detail_record");
     return db_fetch($result);
    
}

function get_pdr_detail_line($selected_id, $type)
{
    switch ($type) {
        case PSOIT_STOCK_ITEM:
            $sql = "SELECT pdrd.*,
                           iu.name unit_name,
                           sm.description item_description 
                    FROM ".TB_PREF."project_dispatch_request_details pdrd 
                            JOIN ".TB_PREF."stock_master sm ON 
                                   pdrd.item_id = sm.stock_id  
                            JOIN ".TB_PREF."item_units iu ON 
                                   pdrd.tran_unit = iu.abbr ";
                break;
        case PSOIT_BOQ_ITEM:      
            $sql = "SELECT pdrd.*,
                               iu.name unit_name,
                               boqi.description item_description 
                    FROM ".TB_PREF."project_dispatch_request_details pdrd 
                            JOIN ".TB_PREF."project_boq_items boqi ON 
                                   pdrd.item_id = boqi.id  
                            JOIN ".TB_PREF."item_units iu ON 
                                   pdrd.tran_unit = iu.abbr ";
        break;
    
        case PSOIT_TEXT:      
        default:
            $sql = "SELECT pdrd.*,
                            iu.name unit_name,
                            pdrd.description item_description 
                    FROM ".TB_PREF."project_dispatch_request_details pdrd 
                            JOIN ".TB_PREF."item_units iu ON 
                                       pdrd.tran_unit = iu.abbr ";
        break;
    }
    
    $sql .= " WHERE pdrd.id=".db_escape($selected_id);
     
    $result = db_query($sql, "Could not get_project_dispatch_request_line");
    return db_fetch($result);
    
}


function delete_project_dispatch_request_line($pdr_line_id)
{
    begin_transaction();
      
    $sql = "DELETE FROM ".TB_PREF."project_dispatch_request_details WHERE id=".db_escape($pdr_line_id);
   
    commit_transaction();
    
    db_query($sql);
    
    return db_num_affected_rows();
    
}


function get_existing_pdr_details_id($pso_detail_id, $pdr_no)
{
    $sql = "SELECT id
            FROM ".TB_PREF."project_dispatch_request_details
            WHERE pdr_no=".db_escape($pdr_no)." AND 
                pso_detail_id=".db_escape($pso_detail_id);
       
    $result = db_query($sql);
    $row = db_fetch($result);   
    if($row){
        return $row['id'];
    }
    else{
        return 0;
    }
}


function get_applicable_quantity($pso_detail_id, $pdr_no)
{
    $sql = "SELECT pdrd.quantity pdrd_quantity 
                    
            FROM ".TB_PREF."project_sales_order_details psod 
                LEFT JOIN ".TB_PREF."project_dispatch_request_details pdrd ON
                    psod.id = pdrd.pso_detail_id
                
            WHERE pdrd.pso_detail_id=".db_escape($pso_detail_id)."AND  
                pdrd.pdr_no=".db_escape($pdr_no);
            
$result = db_query($sql);
return db_fetch($result);

}


function get_pdr_details_pso($pso_no, $pdr_no)
{
    $sql = "SELECT psod.*, 
                    u.name unit_name,
                    pdrd.id pdrd_id,
                    pdrd.tran_unit_qty pdrd_tran_unit_qty,
                    pdrd.line_comment pdrd_line_comment,
                    pdrd.tran_unit_rate pdrd_tran_unit_rate
            FROM ".TB_PREF."project_sales_order_details psod 
                    LEFT JOIN ".TB_PREF."item_units u  ON 
                            psod.order_unit = u.abbr 
                    LEFT JOIN ".TB_PREF."project_dispatch_request_details pdrd ON 
                            psod.id = pdrd.pso_detail_id AND 
                            pdrd.pdr_no = ".db_escape($pdr_no)." 
            WHERE psod.order_no=".db_escape($pso_no);
    
    return db_query($sql, "Could not get_project_customer_purchase_order_details");
    
}


function get_pdr_tax_type_amount($pdr_no, $tax_type_id)
{
    $sql = "SELECT amount 
            FROM ".TB_PREF."project_dispatch_request_taxes 
            WHERE pdr_no=".db_escape($pdr_no)." AND 
                tax_type_id = ".db_escape($tax_type_id);
    
    $result = db_query($sql);
    $amt =  db_fetch($result);   
    $amount = 0;
    if(isset($amt['amount']) && $amt['amount'] > 0){
        $amount = $amt['amount'];
    }
    
    return $amount;
}

?>