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

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

function add_inventory_adjustment_request_header($date_, $reference, $location, $project_id, 
                        $increase, $type, $memo, $movement_type, 
                        $sample_items, $linked_field_type, $linked_field_id)
{
    $ia_date = date2sql($date_);
    $created_on = date('Y-m-d H:i:s');
    $next_approval_group_id = 0;
    $created_by = $_SESSION['wa_current_user']->user;
        
	$sql = "INSERT INTO ".TB_PREF."stock_adjustment_requests(ai_date, reference,
                        location, project_id,  
                        type, sample_items,
                        increase, comments,
                        movement_type, linked_field_type, 
                        linked_field_id, 
                        created_by, created_on) 
                VALUES (".db_escape($ia_date).", ".db_escape($reference).", "
                        .db_escape($location).", ".db_escape($project_id).", "
                        .db_escape($type).", ".db_escape($sample_items).", "
                        .db_escape($increase).", ".db_escape($memo).", "
                        .db_escape($movement_type).", ".db_escape($linked_field_type).", "
                        .db_escape($linked_field_id).", "
                        .db_escape($created_by).", ".db_escape($created_on).")";

    $error_msg = "add_inventory_adjustment_request_header";
   

	db_query($sql, $error_msg);

	return db_insert_id();
}

function update_inventory_adjustment_request_header($heder_id, $date_, $reference, $location, $project_id, $increase, $tran_type, 
                          $memo, $movement_type, $sample_item, $linked_field_type, $linked_field_id)
{
    $ia_date = date2sql($date_);
    $updated_on = date('Y-m-d H:i:s');
    $updated_by = $_SESSION['wa_current_user']->user;
    
    $sql = "UPDATE ".TB_PREF."stock_adjustment_requests  
            SET ai_date = ".db_escape($ia_date).", 
                reference = ".db_escape($reference).",
                location = ".db_escape($location).",
                project_id = ".db_escape($project_id).",
                increase = ".db_escape($increase).",
                type = ".db_escape($tran_type).",
                comments = ".db_escape($memo).",
                movement_type = ".db_escape($movement_type).",
                sample_items = ".db_escape($sample_item).",
                linked_field_type = ".db_escape($linked_field_type).",
                linked_field_id = ".db_escape($linked_field_id).",
                updated_by = ".db_escape($updated_by).",
                updated_on = ".db_escape($updated_on)."
                        
            WHERE id = ".db_escape($heder_id)."";
    
    $error_msg = "update_inventory_adjustment_request_header";

    db_query($sql, $error_msg);
    return $heder_id;
}


function add_existing_stock_adjustment_request_details($trans_id, $type, $stock_id, $trans_no, $location,
                                                       $date_, $reference, $quantity, $std_cost, 
                                                       $person_id=0, $show_or_hide=1, $price=0, $discount_percent=0, 
                                                       $tran_unit = "", $tran_qty = 0, $convertion_factor = 1,
                                                       $task_id = 0, $project_site_id = 0,
                                                       $created_by, $created_on)
{
    $updated_on = date('Y-m-d H:i:s');
    $updated_by = $_SESSION['wa_current_user']->user;
	$date = date2sql($date_);

	$sql = "INSERT INTO ".TB_PREF."stock_adjustment_request_details(trans_id, stock_id, trans_no, 
                    type, loc_code,
                    tran_date, person_id, 
                    reference, qty, 
                    standard_cost, visible, 
                    price, discount_percent, 
                    tran_unit, tran_qty, 
                    convertion_factor, task_id,
                    project_site_id,
                    created_by, created_on,
                    updated_by, updated_on) 
                    
            VALUES (".db_escape($trans_id).", ".db_escape($stock_id).", ".db_escape($trans_no).", "
                     .db_escape($type).",".db_escape($location).", "
                     .db_escape($date).", ".db_escape($person_id).", "
                     .db_escape($reference).", ".db_escape($quantity).", "
                     .db_escape($std_cost).",".db_escape($show_or_hide).", "
                     .db_escape($price).", ".db_escape($discount_percent).", "
                     .db_escape($tran_unit).", ".db_escape($tran_qty).", "
                     .db_escape($convertion_factor).", ".db_escape($task_id).", "
                     .db_escape($project_site_id).","
                     .db_escape($created_by).",".db_escape($created_on).", "
                     .db_escape($updated_by).",".db_escape($updated_on).")";

	$error_msg = "add_existing_stock_adjustment_request_details";

	db_query($sql, $error_msg);

	return db_insert_id();
}


function get_stock_adjustment_request_header($trans_no)
{
    $sql = "SELECT sa.*,
                l.location_name issuing_location_name,
                pm.description project_description,
                mt.type transaction_type, 
                mt.id movement_type, 
                mt.name movement_type_name, 
               
                u.real_name requestor_name 

               
            FROM ".TB_PREF."stock_adjustment_requests sa 
                INNER JOIN ".TB_PREF."projects_master pm ON 
                    sa.project_id = pm.project_id 
                INNER JOIN ".TB_PREF."locations l ON 
                    sa.location = l.loc_code 
                INNER JOIN ".TB_PREF."users u ON 
                     sa.created_by = u.id 
                LEFT JOIN ".TB_PREF."movement_types mt ON 
                     sa.movement_type = mt.id   

  
            WHERE sa.id = ".  db_escape($trans_no);
    
    $result = db_query($sql,"The adjustment transfer record could not be retreived");

    return db_fetch($result);
}

//function get_stock_adjustment_header($trans_no)
//{
//    $sql = "SELECT 
//                sa.*,
//                l.location_name issuing_location_name,
//                pm.description project_description,
//                apn.value_name task_description,
//                ap.approval_path_name approval_path_description,
//                u.real_name requestor_name, 
//                s.supp_name supplier_name, 
//                dm.name customer_name 
//            FROM ".TB_PREF."stock_adjustments sa 
//                INNER JOIN ".TB_PREF."projects_master pm ON 
//                    sa.project_id = pm.project_id 
//                INNER JOIN ".TB_PREF."project_tasks pt ON 
//                    sa.task_id = pt.task_id 
//                INNER JOIN ".TB_PREF."locations l ON 
//                    sa.location = l.loc_code 
//                INNER JOIN ".TB_PREF."users u ON 
//                     sa.created_by = u.id 
//                LEFT JOIN ".TB_PREF."approval_path ap ON 
//                    pt.approval_path_id = ap.approval_path_id 
//                LEFT JOIN ".TB_PREF."list_values apn ON 
//                    pt.type_id = apn.id 
//                LEFT JOIN ".TB_PREF."suppliers s ON 
//                    sa.supplier_id = s.supplier_id  
//                LEFT JOIN ".TB_PREF."debtors_master dm ON 
//                    sa.customer_id = dm.debtor_no     
//                
//            WHERE  
//                sa.id = ".  db_escape($trans_no);
//    
//    $result = db_query($sql,"The adjustment transfer record could not be retreived");
//
//    return db_fetch($result);
//}


function add_stock_adjustment_request_detail($type, $stock_id, $trans_no, $location,
                                              $date_, $reference, $quantity, $std_cost, 
                                              $person_id=0, $show_or_hide=1, $price=0, $discount_percent=0, 
                                              $tran_unit = "", $tran_qty = 0, $convertion_factor = 1,
                                              $task_id = 0, $project_site_id = 0)
{
	$date = date2sql($date_);
        $created_on = date('Y-m-d H:i:s');
        $created_by = $_SESSION['wa_current_user']->user;

	$sql = "INSERT INTO ".TB_PREF."stock_adjustment_request_details(stock_id, trans_no, 
                    type, loc_code,
                    tran_date, person_id, 
                    reference, qty, 
                    standard_cost, visible, 
                    price, discount_percent, 
                    tran_unit, tran_qty, 
                    convertion_factor, task_id,
                    project_site_id,
                    created_by, created_on)
                    
            VALUES (".db_escape($stock_id).", ".db_escape($trans_no).", "
                    .db_escape($type).",".db_escape($location).", "
                    .db_escape($date).", ".db_escape($person_id).", "
                    .db_escape($reference).", ".db_escape($quantity).", "
                    .db_escape($std_cost).",".db_escape($show_or_hide).", "
                    .db_escape($price).", ".db_escape($discount_percent).","
                    .db_escape($tran_unit).", ".db_escape($tran_qty).","
                    .db_escape($convertion_factor).", ".db_escape($task_id).", "
                    .db_escape($project_site_id).", "
                    .db_escape($created_by).", ".db_escape($created_on).")";

	$error_msg = "add_stock_adjustment_request_detail";

	db_query($sql, $error_msg);

	return db_insert_id();
}


function delete_stock_adjustment_request_details($trans_no)
{
    $sql = "DELETE FROM ".TB_PREF."stock_adjustment_request_details 
            WHERE trans_no = ".db_escape($trans_no);
    
    db_query($sql, "Could not delete_stock_adjustment_request_details");
}


function get_stock_adjustment_request_items_details($trans_no)
{
    $sql = "SELECT sad.*,
                    pt.description task_description,
                    ps.description project_description 
                    
            FROM ".TB_PREF."stock_adjustment_request_details sad 
                JOIN ".TB_PREF."stock_adjustment_requests sa  
                    ON sad.trans_no = sa.id  
                LEFT JOIN ".TB_PREF."project_tasks pt 
                    ON sad.task_id = pt.task_id 
                LEFT JOIN ".TB_PREF."project_sites ps  
                    ON sad.project_site_id = ps.id 
            WHERE sad.trans_no = ".db_escape($trans_no)."
            ORDER BY sad.trans_id";
    
    return db_query($sql, "get_stock_adjustment_request_items_details");
}


function get_stock_adjustment_request_header_record($trans_no)
{
    $sql = "SELECT sa.* 
            FROM ".TB_PREF."stock_adjustment_requests sa 
            WHERE sa.id = ".  db_escape($trans_no);
    
    $result = db_query($sql,"get_stock_adjustment_request_header_record");

    return db_fetch($result);
}

?>