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

function add_supp_invoice_item($supp_trans_type, $supp_trans_no, $stock_id, $description,
                                $gl_code, $unit_price, $unit_tax, $quantity, 
                                $grn_item_id, $po_detail_item_id, $memo_,
                                $err_msg="", $sti_id = 0, 
                                $po_unit = '', $po_unit_qty = 0, $po_unit_price = 0, 
                                $po_unit_price_discount = 0, $po_unit_conv_factor = 1)
{
	$sql = "INSERT INTO ".TB_PREF."supp_invoice_items (
                        supp_trans_type, supp_trans_no, 
                        stock_id, description, 
                        gl_code, unit_price, 
                        unit_tax, quantity,
                        grn_item_id, po_detail_item_id, 
                        memo_, transport_invoice_id, 
                        po_unit, po_unit_qty, 
                        po_unit_price, po_unit_price_discount,
                        po_unit_conv_factor) ";
	$sql .= "VALUES (".db_escape($supp_trans_type).", ".db_escape($supp_trans_no).", "
                        .db_escape($stock_id).", ".db_escape($description).", "
                        .db_escape($gl_code).", ".db_escape($unit_price).", "
                        .db_escape($unit_tax).", ".db_escape($quantity).", "
                        .db_escape($grn_item_id).", ".db_escape($po_detail_item_id).", "
                        .db_escape($memo_).",".db_escape($sti_id).", "
                        .db_escape($po_unit).", ".db_escape($po_unit_qty).", "
                        .db_escape($po_unit_price).",".db_escape($po_unit_price_discount).","
                        .db_escape($po_unit_conv_factor).")";

	if ($err_msg == "")
		$err_msg = "Cannot insert a supplier transaction detail record";

	db_query($sql, $err_msg);

	return db_insert_id();
}

//-------------------------------------------------------------------------------------------------------------

function add_supp_invoice_gl_item($supp_trans_type, $supp_trans_no, $gl_code, $amount, $memo_, $err_msg="")
{
	return add_supp_invoice_item($supp_trans_type, $supp_trans_no,  "", "", $gl_code, $amount,
		0, 0, /*$grn_item_id*/0, /*$po_detail_item_id*/0, $memo_, $err_msg);
}


//-------------------------------------------------------------------------------------------------------------

function add_supp_invoice_sti_item($supp_trans_type, $supp_trans_no, $gl_code, $amount, $memo_, $sti_id)
{
	return add_supp_invoice_item($supp_trans_type, $supp_trans_no,  "" /*$stock_id*/, "" /*$description*/, $gl_code, $amount,
		0, 0, /*$grn_item_id*/0, /*$po_detail_item_id*/0, $memo_, "", $sti_id);
}


//----------------------------------------------------------------------------------------

function get_supp_invoice_items($supp_trans_type, $supp_trans_no)
{
	$sql = "SELECT sii.*, 
                    sii.unit_price AS FullUnitPrice,
                    iu.name po_unit_name 
            FROM ".TB_PREF."supp_invoice_items sii 
                    LEFT JOIN ".TB_PREF."item_units iu ON 
                            sii.po_unit = iu.abbr 
            WHERE supp_trans_type = ".db_escape($supp_trans_type)."
                    AND supp_trans_no = ".db_escape($supp_trans_no)." 
            ORDER BY id";
	return db_query($sql, "Cannot retreive supplier transaction detail records");
}

//----------------------------------------------------------------------------------------

function void_supp_invoice_items($type, $type_no)
{
	$sql = "UPDATE ".TB_PREF."supp_invoice_items SET quantity=0, unit_price=0
		WHERE supp_trans_type = ".db_escape($type)." AND supp_trans_no=".db_escape($type_no);
	db_query($sql, "could not void supptrans details");
}

?>