<?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){
    global $systypes_array;
    $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 = "could not add_inventory_adjustment_request_header";
    db_query($sql, $error_msg);
    
    if(!(db_insert_id() > 0)){
        return array("success" => false, "message" => "Could not Add $systypes_array[$type] header.", "trans_no" => 0);
    }else{
        return array("success" => true, "message" => "", "trans_no" => 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, $row_state){
    global $systypes_array;
    $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).",
                row_state = ".db_new_row_state($row_state)."
                        
            WHERE id = ".db_escape($heder_id)." 
                AND row_state = ".db_escape($row_state);
    
    $error_msg = "could not update_inventory_adjustment_request_header";
    db_query($sql, $error_msg);
    
    if(!(db_num_affected_rows() > 0)){
        return array("success" => false, "message" => "Could not Update $systypes_array[$tran_type] header.", "trans_no" => $heder_id);
    }else{
        return array("success" => true, "message" => "", "trans_no" => $heder_id);
    }
}

function update_existing_stock_adjustment_request_details($trans_id, $type, $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, $conversion_factor = 1,
                                                          $task_id = 0, $project_site_id = 0){
    global $systypes_array;
    $updated_on = date('Y-m-d H:i:s');
    $updated_by = $_SESSION['wa_current_user']->user;
    $date = date2sql($date_);
        
        $sql = "UPDATE ".TB_PREF."stock_adjustment_request_details   
                SET type = ".db_escape($type).",
                    loc_code = ".db_escape($location).",
                    tran_date = ".db_escape($date).",
                    person_id = ".db_escape($person_id).",
                    reference = ".db_escape($reference).",
                    qty = ".db_escape($quantity).",
                    standard_cost = ".db_escape($std_cost).",
                    visible = ".db_escape($show_or_hide).",
                    price = ".db_escape($price).",
                    discount_percent = ".db_escape($discount_percent).",
                    tran_unit = ".db_escape($tran_unit).",
                    tran_qty = ".db_escape($tran_qty).",
                    convertion_factor = ".db_escape($conversion_factor).",
                    task_id = ".db_escape($task_id).",
                    project_site_id = ".db_escape($project_site_id).",
                    updated_by = ".db_escape($updated_by).",
                    updated_on = ".db_escape($updated_on)."

                WHERE trans_id = ".db_escape($trans_id)." 
                    AND trans_no = ".db_escape($trans_no);

    $error_msg = "could not add_existing_stock_adjustment_request_details";
    db_query($sql, $error_msg);
    if(!(db_num_affected_rows() > 0)){
        return array("success" => false, "trans_no"=>$trans_no, "message" => "Could not Add $systypes_array[$type] details.");
    }else{
        return array("success" => true, "trans_no"=>$trans_no, "message" => "");
    }
}

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 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, $conversion_factor = 1, $task_id = 0, $project_site_id = 0){
    global $systypes_array;
    $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($conversion_factor).", ".db_escape($task_id).", "
                     .db_escape($project_site_id).", "
                     .db_escape($created_by).", ".db_escape($created_on).")";

    $error_msg = "could not add_stock_adjustment_request_detail";
    db_query($sql, $error_msg);
    
    if(!(db_insert_id() > 0)){
        return array("success" => false, "trans_no" => $trans_no, "message" => "Could not Add $systypes_array[$type] details.");
    }else{
        return array("success" => true, "trans_no" => $trans_no, "message" => "");
    }
}

function delete_stock_adjustment_request_details_record($trans_id, $mv_type){
    global $systypes_array;
    $sql = "DELETE FROM ".TB_PREF."stock_adjustment_request_details 
            WHERE trans_id = ".db_escape($trans_id);
    
    db_query($sql, "Could not delete_stock_adjustment_request_details_record");
    if(!(db_num_affected_rows() > 0)){
        return array("success" => false, "message" => "Could not Delete $systypes_array[$mv_type] record #$trans_id");
    }else{
        return array("success" => true, "message" => "$systypes_array[$mv_type] line #$trans_id has been deleted.");
    }
}

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);
}

?>