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

function add_issued_pdc($supplier_id, 
                                $cheque_no, $amount,
                                $bank_account_id, 
                                $cheque_date, $comment)
{
        $created_by = $_SESSION['wa_current_user']->user;
        $created_on = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO ".TB_PREF."pdcs_issued (
                        supplier_id, 
                        cheque_no, amount,
                        bank_account_id,  
                        cheque_date, comment, 
                        created_by, created_on) 
                VALUES (".db_escape($supplier_id) .", "
                        .db_escape($cheque_no).", ".db_escape($amount).", "
                        .db_escape($bank_account_id).", "
                        .db_escape($cheque_date).", ".db_escape($comment) . ", "
                        .db_escape($created_by).", ".db_escape($created_on).")";
        
   	db_query($sql,"Post dated cheque could not be added");
}

function update_issued_pdc($selected_id, $supplier_id, 
                                $cheque_no, $amount,
                                $bank_account_id, 
                                $cheque_date, $comment)
{
        $updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
        
        $sql = "UPDATE ".TB_PREF."pdcs_issued SET 
                        supplier_id=".db_escape($supplier_id).", 
                        cheque_no=".db_escape($cheque_no).", 
                        amount=".db_escape($amount).",
                        bank_account_id=".db_escape($bank_account_id).",
                        cheque_date=".db_escape($cheque_date).", 
                        comment=".db_escape($comment).", 
                        updated_by=".db_escape($updated_by).", 
                        updated_on=".db_escape($updated_on)."  
		WHERE id = ".db_escape($selected_id);
   	db_query($sql,"Post dated cheque could not be updated");
}

function update_issued_pdc_payment_id($selected_id, $payment_id)
{
        $updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
        
        $sql = "UPDATE ".TB_PREF."pdcs_issued SET 
                        payment_id=".db_escape($payment_id).",  
                        updated_by=".db_escape($updated_by).", 
                        updated_on=".db_escape($updated_on)."  
		WHERE id = ".db_escape($selected_id);
   	db_query($sql,"Payment id of the post dated cheque could not be updated");
}

function delete_issued_pdc($id)
{
	//$date = date2sql($date);
	$sql = "DELETE FROM ".TB_PREF."pdcs_issued WHERE id = ".$id; 
   	
        db_query($sql,"Issued PDC could not be deleted");
        
}

function get_issued_pdcs($supplier_id, $from_date, $to_date, $pdc_status = 0)
{
	//$date = date2sql($date);
	$sql = "SELECT pdcs.*, s.supp_name, ba.bank_account_name     
                FROM ".TB_PREF."pdcs_issued pdcs,  
                        ".TB_PREF."suppliers s,
                        ".TB_PREF."bank_accounts ba 
                WHERE pdcs.supplier_id = s.supplier_id AND 
                        pdcs.bank_account_id = ba.id AND 
                        cheque_date >= ".db_escape(date2sql($from_date))." AND 
                        cheque_date <= ".db_escape(date2sql($to_date));
        
        if($supplier_id <> ""){
            $sql .= " AND pdcs.supplier_id = ".db_escape($supplier_id);
        }
        
        if($pdc_status == 1){
            $sql .= " AND pdcs.payment_id = 0 ";
        }
        elseif($pdc_status == 2){
            $sql .= " AND pdcs.payment_id > 0 ";
        }
   	
        $result = db_query($sql,"PDCs issued could not be retrieved");
        
        return $result;
}

function get_issued_pdc_details($id)
{
	//$date = date2sql($date);
	$sql = "SELECT pdcs.*, s.supp_name, ba.bank_account_name     
                FROM ".TB_PREF."pdcs_issued pdcs,  
                        ".TB_PREF."suppliers s,
                        ".TB_PREF."bank_accounts ba 
                WHERE pdcs.supplier_id = s.supplier_id AND 
                        pdcs.bank_account_id = ba.id AND 
                        pdcs.id = ".  db_escape($id); 
   	
        $result = db_query($sql,"Issued post dated cheque details could not be retrieved");
        
        return db_fetch($result);

}

?>