<?php //
/**********************************************************************
***********************************************************************/
function add_project_billing_schedule_detail($project_id, $billing_type_id, $date, $amount, $percentage, $comment, $invoice_no, $credit_note_no)
{
                $id_user = $_SESSION['wa_current_user']->user;
                $datetime = date('Y-m-d H:i:s');
                
        $sql = "INSERT INTO ".TB_PREF."projects_billing_schedule (
                        project_id, billing_type_id, date,
                        amount, percentage, comment,
                        created_by, created_on, 
                        invoice_no, credit_note_no) 
                VALUES (".db_escape($project_id).", ".db_escape($billing_type_id).", ".db_escape(date2sql($date))
                        .", ".db_escape($amount).", ".db_escape($percentage).", ".db_escape($comment).", "
                        .db_escape($id_user).", ".db_escape($datetime).", "
                        .db_escape($invoice_no).", ".db_escape($credit_note_no).")";

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

function update_project_billing_schedule_detail($id, $billing_type_id, $date, $amount, $percentage, $comment, $invoice_no, $credit_note_no)
{
                $id_user = $_SESSION['wa_current_user']->user;
                $datetime = date('Y-m-d H:i:s');
        $sql = "UPDATE ".TB_PREF."projects_billing_schedule SET 
                    billing_type_id=".db_escape($billing_type_id).",       
                    date=".db_escape(date2sql($date)).", 
                    amount=".db_escape($amount).",       
                    comment=".db_escape($comment).",
                    updated_by=".db_escape($id_user).",
                    percentage=".db_escape($percentage).",
                    invoice_no=".db_escape($invoice_no).",
                    credit_note_no=".db_escape($credit_note_no).",
                    updated_on=".db_escape($datetime)."
                          WHERE id=".db_escape($id);

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

function delete_project_billing_schedule_detail($id)
{
	$sql="DELETE FROM ".TB_PREF."projects_billing_schedule WHERE id= ".db_escape($id);
	db_query($sql,"an item price could not be deleted");			
}

function get_project_billing_schedule($project_id)
{
	$sql = "SELECT pbs.*,
                        pbsn.value_name billing_type_name 
                FROM ".TB_PREF."projects_billing_schedule pbs 
                    LEFT JOIN ".TB_PREF."list_values pbsn 
                        ON pbs.billing_type_id = pbsn.id 
                WHERE pbs.project_id=".db_escape($project_id)." 
                ORDER BY date";
	return db_query($sql,"Project billing schedule could not be retreived");
}

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

function get_project_billing_schedule_summary($project_id, $start_date, $end_date)
{
	$sql = "SELECT SUM(amount) total_amount 
                FROM ".TB_PREF."projects_billing_schedule 
                WHERE project_id =".db_escape($project_id)." 
                    AND date >=".db_escape($start_date)." 
                    AND date <=".db_escape($end_date);
	
	$result = db_query($sql,"price could not be retreived");
	
	return db_result_value($result, "total_amount");
}

?>