<?php //
/**********************************************************************
***********************************************************************/
function add_project_invoicing_item($project_id, $total_only, $deduction, $code, $description, $billing_type, $rate, $unit, $max_quantity, 
                                    $long_description, $tax_group_id, $type, $display_order, $parent_id,
                                    $print_long_description)
{
                $id_user = $_SESSION['wa_current_user']->user;
                $datetime = date('Y-m-d H:i:s');
                
        $sql = "INSERT INTO ".TB_PREF."project_invoicing_items  
                  (project_id, total_only, deduction, code, description, billing_type,
                   rate, unit, max_quantity, long_description, print_long_description,
                   tax_group_id, type, display_order, parent_id, created_by, created_on) 
                  
                VALUES (".db_escape($project_id).", ".db_escape($total_only).", ".db_escape($deduction).", ".db_escape($code)
                       .", ".db_escape($description).", ".db_escape($billing_type).", ".db_escape($rate).", "
                       .db_escape($unit).", ".db_escape($max_quantity).", "
                        .db_escape($long_description).", ".db_escape($print_long_description).", "
                       .db_escape($tax_group_id).", ".db_escape($type).", ".db_escape($display_order).", "
                       .db_escape($parent_id).", ".db_escape($id_user).", ".db_escape($datetime).")";

        db_query($sql,"Project Billing Item could not be added");		
}

function update_project_invoicing_item($schedule_id, $total_only, $deduction, $code, $description, $billing_type, $rate, $unit, $max_quantity,
                                       $long_description,$tax_group_id, $type, $display_order, $parent_id,
                                        $print_long_description)
{
                $id_user = $_SESSION['wa_current_user']->user;
                $datetime = date('Y-m-d H:i:s');
        $sql = "UPDATE ".TB_PREF."project_invoicing_items 
                SET 
                    total_only =".db_escape($total_only).", 
                    deduction=".db_escape($deduction).", 
                    code=".db_escape($code).", 
                    description=".db_escape($description).",       
                    rate=".db_escape($rate).",
                    unit=".db_escape($unit).",    
                    max_quantity=".db_escape($max_quantity).", 
                    long_description=".db_escape($long_description).", 
                    billing_type=".db_escape($billing_type).", 
                    print_long_description=".db_escape($print_long_description).",  
                    tax_group_id=".db_escape($tax_group_id).", 
                    type=".db_escape($type).",
                    display_order=".db_escape($display_order).",
                    parent_id=".db_escape($parent_id).",
                    updated_by=".db_escape($id_user).",
                    updated_on=".db_escape($datetime)."
                        
                WHERE id=".db_escape($schedule_id);

        db_query($sql,"Billing item could not be updated");		
}

function delete_project_invoicing_item($schedule_id)
{
	$sql="DELETE FROM ".TB_PREF."project_invoicing_items  WHERE id= ".db_escape($schedule_id);
	db_query($sql,"Project Billing item could not be deleted");			
}

function get_project_invoicing_items($project_id)
{
	$sql = "SELECT pbi.*,
                       pt.description parent_description,
                       tg.name
                       
                FROM ".TB_PREF."project_invoicing_items pbi 
                    LEFT JOIN ".TB_PREF."tax_groups tg ON 
                	pbi.tax_group_id = tg.id 
                    LEFT JOIN ".TB_PREF."project_invoicing_items pt ON 
                        pt.id = pbi.parent_id
                    
                WHERE pbi.project_id=".db_escape($project_id)." 
                ORDER BY display_order, type, code";
                     
	return db_query($sql,"Project billing items could not be retreived");
}


function get_project_invoicing_item_details($id)
{
	$sql = "SELECT * 
                     
                FROM ".TB_PREF."project_invoicing_items   
                                         
                WHERE  id=".db_escape($id);
	
	$result = db_query($sql,"price could not be retreived");
	
	return db_fetch($result);
}


function get_top_level_invoicing_items($project_id, $order_no)
{
    $sql = "SELECT pii.*, pid.id invoice_line_id, pid.description invoice_item_description   
                     
                FROM ".TB_PREF."project_invoicing_items pii 
                        LEFT JOIN ".TB_PREF."project_invoice_details pid ON 
                                pii.id = pid.invoicing_item_id 
                                AND pid.order_no = ".db_escape($order_no)."  
                WHERE  pii.project_id=".db_escape($project_id)." 
                    AND pii.parent_id = 0 
                ORDER BY pii.display_order";
	
	return db_query($sql,"Could not get_top_level_billing_items");
}

function get_child_invoicing_items($parent_id, $order_no)
{
    $sql = "SELECT pii.*, pid.id invoice_line_id, pid.description invoice_item_description   
                FROM ".TB_PREF."project_invoicing_items  pii 
                        LEFT JOIN ".TB_PREF."project_invoice_details pid ON 
                                pii.id = pid.invoicing_item_id 
                                AND pid.order_no = ".db_escape($order_no)."  
                WHERE  pii.parent_id=".db_escape($parent_id)." 
                ORDER BY pii.display_order";
	
	return db_query($sql,"Could not get_child_billing_items");
}

?>