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

function add_pdc($debtor_no, $branch_code, 
                                $cheque_no, $amount,
                                $bank_id, $bank_branch, 
                                $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_in_hand (
                        debtor_no, branch_code, 
                        cheque_no, amount,
                        bank_id, bank_branch, 
                        cheque_date, comment, 
                        created_by, created_on) 
                VALUES (".db_escape($debtor_no) . ", ".db_escape($branch_code).", "
                        .db_escape($cheque_no).", ".db_escape($amount).", "
                        .db_escape($bank_id) . ", ".db_escape($bank_branch).", ".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_pdc($selected_id, $debtor_no, $branch_code, 
                                $cheque_no, $amount,
                                $bank_id, $bank_branch, 
                                $cheque_date, $comment)
{
        $updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
        
        $sql = "UPDATE ".TB_PREF."pdcs_in_hand SET 
                        debtor_no=".db_escape($debtor_no).", 
                        branch_code=".db_escape($branch_code).", 
                        cheque_no=".db_escape($cheque_no).", 
                        amount=".db_escape($amount).",
                        bank_id=".db_escape($bank_id).",
                        bank_branch=".db_escape($bank_branch).", 
                        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_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_in_hand 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_pdc($id)
{
	//$date = date2sql($date);
	$sql = "DELETE FROM ".TB_PREF."pdcs_in_hand WHERE id = ".$id; 
   	
        db_query($sql,"PDCs in hand could not be deleted");
        
}

function get_pdcs($customer_id, $from_date, $to_date, $pdc_status = 0)
{
	//$date = date2sql($date);
	$sql = "SELECT pdcs.*, dm.name customer_name, lv.value_name bank_name, cb.br_name   
                FROM ".TB_PREF."pdcs_in_hand pdcs, " 
                        .TB_PREF."debtors_master dm, "
                        .TB_PREF."list_values lv, "
                        .TB_PREF."cust_branch cb 
                WHERE pdcs.debtor_no = dm.debtor_no AND 
                        pdcs.branch_code = cb.branch_code AND 
                        pdcs.bank_id = lv.id AND 
                        cheque_date >= ".db_escape(date2sql($from_date))." AND 
                        cheque_date <= ".db_escape(date2sql($to_date));
        
        if($customer_id <> ""){
            $sql .= " AND pdcs.debtor_no = ".db_escape($customer_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 in hand could not be retrieved");
        
        return $result;
}

function get_pdc_details($id)
{
	//$date = date2sql($date);
	$sql = "SELECT pdcs.*, dm.name customer_name, lv.value_name bank_name, cb.br_name   
                FROM ".TB_PREF."pdcs_in_hand pdcs, " 
                        .TB_PREF."debtors_master dm, "
                        .TB_PREF."list_values lv, "
                        .TB_PREF."cust_branch cb 
                WHERE pdcs.debtor_no = dm.debtor_no AND 
                        pdcs.branch_code = cb.branch_code AND 
                        pdcs.bank_id = lv.id AND 
                        pdcs.id = ".  db_escape($id); 
   	
        $result = db_query($sql,"Post dated cheque details could not be retrieved");
        
        return db_fetch($result);

}

?>