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

function add_supplier($supp_name, $supp_ref, $address, $supp_address, $gst_no, $svat_no,
	              $website, $supp_account_no, $supplier_type_id, 
                        $bank_account, $bank_account_no, $bank_id, $bank_branch_id, 
                        $credit_limit, $dimension_id, $dimension2_id, 
	              $curr_code, $payment_terms, $payable_account, $purchase_account, $payment_discount_account, 
	              $notes, $tax_group_id, $tax_included)
{
    $user_id = $_SESSION['wa_current_user']->user;
    $date_time = date('Y-m-d H:i:s');  
        
	$sql = "INSERT INTO ".TB_PREF."suppliers (supp_name, supp_ref, address, supp_address, gst_no, svat_no,
                website, supp_account_no, supplier_type, 
                bank_account, bank_account_no, bank_id, bank_branch_id, 
                credit_limit, dimension_id, dimension2_id, curr_code,
		payment_terms, payable_account, purchase_account, payment_discount_account, notes, 
		tax_group_id, tax_included, created_by, created_on)
		VALUES (".db_escape($supp_name). ", "
		.db_escape($supp_ref). ", "
		.db_escape($address) . ", "
		.db_escape($supp_address) . ", "
		.db_escape($gst_no). ", "
		.db_escape($svat_no). ", "
		.db_escape($website). ", "
		.db_escape($supp_account_no). ", "
                .db_escape($supplier_type_id). ", "
		.db_escape($bank_account). ", "
                .db_escape($bank_account_no). ", "
		.db_escape($bank_id). ", "
		.db_escape($bank_branch_id). ", " 
		.db_escape($credit_limit). ", "
		.db_escape($dimension_id). ", "
		.db_escape($dimension2_id). ", "
		.db_escape($curr_code). ", "
		.db_escape($payment_terms). ", "
		.db_escape($payable_account). ", "
		.db_escape($purchase_account). ", "
		.db_escape($payment_discount_account). ", "
		.db_escape($notes). ", "
		.db_escape($tax_group_id). ", "
		.db_escape($tax_included). ", "
		.db_escape($user_id). ", "
		.db_escape($date_time). ")";

	db_query($sql,"The supplier could not be added");
}

function update_supplier($supplier_id, $supp_name, $supp_ref, $address, $supp_address, $gst_no, $svat_no,
	                 $website, $supp_account_no, $supplier_type_id, 
                        $bank_account, $bank_account_no, $bank_id, $bank_branch_id,  
                        $credit_limit, $dimension_id, $dimension2_id, 
	                 $curr_code, $payment_terms, $payable_account, $purchase_account, $payment_discount_account, 
	                 $notes, $tax_group_id, $tax_included)
{
    $user_id = $_SESSION['wa_current_user']->user;
    $date_time = date('Y-m-d H:i:s'); 
    
	$sql = "UPDATE ".TB_PREF."suppliers SET supp_name=".db_escape($supp_name) . ",
                        supp_ref=".db_escape($supp_ref) . ",
                        address=".db_escape($address) . ",
                        supp_address=".db_escape($supp_address) . ",
                        supplier_type=".db_escape($supplier_type_id) . ",
                        gst_no=".db_escape($gst_no) . ",
                        svat_no=".db_escape($svat_no) . ",
                        website=".db_escape($website) . ",
                        supp_account_no=".db_escape($supp_account_no) . ",
                        bank_account=".db_escape($bank_account) . ",
                        bank_account_no=".db_escape($bank_account_no) . ",
                        bank_id=".db_escape($bank_id) . ",
                        bank_branch_id=".db_escape($bank_branch_id) . ",
                        credit_limit=".$credit_limit . ",
                        dimension_id=".db_escape($dimension_id) . ",
                        dimension2_id=".db_escape($dimension2_id) . ",
                        curr_code=".db_escape($curr_code).",
                        payment_terms=".db_escape($payment_terms) . ",
                        payable_account=".db_escape($payable_account) . ",
                        purchase_account=".db_escape($purchase_account) . ",
                        payment_discount_account=".db_escape($payment_discount_account) . ",
                        notes=".db_escape($notes) . ",
                        tax_group_id=".db_escape($tax_group_id). ",
                        tax_included=".db_escape($tax_included)." ,
                        updated_by=".db_escape($user_id). ",
                        updated_on=".db_escape($date_time)." 
                WHERE supplier_id = ".db_escape($supplier_id);

	db_query($sql,"The supplier could not be updated");
}

function delete_supplier($supplier_id)
{
	$sql="DELETE FROM ".TB_PREF."suppliers WHERE supplier_id=".db_escape($supplier_id);
	db_query($sql,"check failed");
}

function get_supplier_details($supplier_id, $to=null, $all=true)
{

	if ($to == null)
		$todate = date("Y-m-d");
	else
		$todate = date2sql($to);
	$past1 = get_company_pref('past_due_days');
	$past2 = 2 * $past1;
	// removed - supp_trans.alloc from all summations

	if ($all)
    	$value = "(trans.ov_amount + trans.ov_gst + trans.ov_discount)";
    else	
    	$value = "IF (trans.type=".ST_SUPPINVOICE." OR trans.type=".ST_BANKDEPOSIT.",
    		(trans.ov_amount + trans.ov_gst + trans.ov_discount - trans.alloc),
    		(trans.ov_amount + trans.ov_gst + trans.ov_discount + trans.alloc))";
	$due = "IF (trans.type=".ST_SUPPINVOICE." OR trans.type=".ST_SUPPCREDIT.",trans.due_date,trans.tran_date)";
    $sql = "SELECT supp.supp_name, supp.curr_code, ".TB_PREF."payment_terms.terms,

		Sum(IFNULL($value,0)) AS Balance,

		Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= 0,$value,0)) AS Due,
		Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $past1,$value,0)) AS Overdue1,
		Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $past2,$value,0)) AS Overdue2,
		supp.credit_limit - Sum(IFNULL(IF(trans.type=".ST_SUPPCREDIT.", -1, 1) 
			* (ov_amount + ov_gst + ov_discount),0)) as cur_credit,
		supp.tax_group_id

		FROM ".TB_PREF."suppliers supp
			 LEFT JOIN ".TB_PREF."supp_trans trans ON supp.supplier_id = trans.supplier_id AND trans.tran_date <= '$todate',
			 ".TB_PREF."payment_terms

		WHERE
			 supp.payment_terms = ".TB_PREF."payment_terms.terms_indicator
			 AND supp.supplier_id = $supplier_id ";
	if (!$all)
		$sql .= "AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_discount) - trans.alloc > ".FLOAT_COMP_DELTA." ";  
	$sql .= "GROUP BY
			  supp.supp_name,
			  ".TB_PREF."payment_terms.terms,
			  ".TB_PREF."payment_terms.days_before_due,
			  ".TB_PREF."payment_terms.day_in_following_month";

    $result = db_query($sql,"The customer details could not be retrieved");
    $supp = db_fetch($result);

    return $supp;
}

function get_supplier($supplier_id)
{
	$sql = "SELECT * FROM ".TB_PREF."suppliers WHERE supplier_id=".db_escape($supplier_id);

	$result = db_query($sql, "could not get supplier");

	return db_fetch($result);
}

function get_supplier_name($supplier_id)
{
	$sql = "SELECT supp_name AS name FROM ".TB_PREF."suppliers WHERE supplier_id=".db_escape($supplier_id);

	$result = db_query($sql, "could not get supplier");

	$row = db_fetch_row($result);
        if (isset($row[0]) && strlen($row[0]) > 0) {
            return $row[0];
        }  else {
            return '';
        }
}

function get_supplier_accounts($supplier_id)
{
	$sql = "SELECT payable_account,purchase_account,payment_discount_account FROM ".TB_PREF."suppliers WHERE supplier_id=".db_escape($supplier_id);

	$result = db_query($sql, "could not get supplier");

	return db_fetch($result);
}

function get_supplier_contacts($supplier_id, $action=null)
{
	$results = array();
	$res = get_crm_persons('supplier', $action, $supplier_id);
	while($contact = db_fetch($res))
		$results[] = $contact;

	return $results;
}

function get_current_supp_credit($supplier_id)
{
	$suppdet = get_supplier_details($supplier_id);
	return $suppdet['cur_credit'];

}

function is_new_supplier($id)
{
	$tables = array('supp_trans', 'grn_batch', 'purch_orders', 'purch_data');

	return !key_in_foreign_table($id, $tables, 'supplier_id');
}

function get_existing_supp_ref($supp_ref, $supplier_id)
{
	$sql = "SELECT supplier_id FROM ".TB_PREF."suppliers WHERE supp_ref=".db_escape($supp_ref);
        
        if($supplier_id > 0){
            $sql .= " AND supplier_id <> ".db_escape($supplier_id);
        }

	$result = db_query($sql, "could not get supplier reference");

	return db_fetch($result);
}



function get_supplier_by_short_name($supplier_short_name)
{
	$sql = "SELECT * FROM ".TB_PREF."suppliers WHERE supp_ref=".db_escape($supplier_short_name);
        
	$result = db_query($sql, "could not get supplier reference");

	return db_fetch($result);
}


function get_supplier_by_name($supplier_name)
{
	$sql = "SELECT * FROM ".TB_PREF."suppliers WHERE supp_name=".db_escape($supplier_name);

	$result = db_query($sql, "could not get supplier reference");

	return db_fetch($result);
}

function get_supplier_payment_terms_details($supplier_id)
{
    $sql = "SELECT terms.terms_indicator, terms.terms, terms.days_before_due, terms.day_in_following_month
            FROM ".TB_PREF."suppliers supp
                    LEFT JOIN ".TB_PREF."payment_terms terms ON 
                            supp.payment_terms=terms.terms_indicator
            WHERE supp.supplier_id = ".db_escape($supplier_id);

	$result = db_query($sql, "Could not get_supplier_payment_terms_details");

	return db_fetch($result);
}

?>