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


function save_lot_adjustment($data)
{
    if($data['type'] == 2){
        $data['tran_qty'] = -$data['tran_qty'];
    }
    
    update_lot($data['lot_id'], $data['row_state'], $data['tran_qty']);
          
    add_lot_adjustment($data['tran_no'], $data['tran_reference'],
                        $data['lot_id'],$data['stock_id'],
                        $data['tran_date'],
                        $data['tran_qty'], $data['tran_type'], $data['location']);

    if(strlen($data['lot_tran_comment']) > 0){
        add_comments($data['tran_type'], $data['tran_no'], $_POST['tran_date'], $_POST['comment']);   
    }
}

function add_lot_adjustment($tran_no, $tran_reference, $lot_id, $stock_id, $tran_date, $qty, $type, $location)
{
//    $id_user = $_SESSION['wa_current_user']->user;
//    $datetime = date('Y-m-d H:i:s');
    //$date = date2sql($tran_date);
        $cost = 0;
        
        $sql = "INSERT INTO ".TB_PREF."lot_moves
                        (tran_no, lot_id,
                        stock_id,tran_date,
                        reference,qty,
                        cost,type,loc_code) 
                VALUES
                        (".db_escape($tran_no).",".db_escape($lot_id).","
                            .db_escape($stock_id).",".db_escape(date2sql($tran_date)).","
                            .db_escape($tran_reference)."," .db_escape($qty).","
                            .db_escape($cost).",".db_escape($type).","
                            .db_escape($location).")";
        
        db_query($sql, "New lot adjustment could not be added");
        //return db_insert_id();
   
    
}

function update_lot($lot_id, &$row_state, $tran_qty)
{
    $new_row_state = $row_state + 1;

    $sql = "UPDATE ".TB_PREF."lots 
            SET
                qty = qty + ".$tran_qty.", 
                row_state = ".db_escape($new_row_state)." 
            WHERE 
                id = ".db_escape($lot_id);
    //." AND                 row_state = ".db_escape($row_state) ;
        
        db_query($sql, "Selected lot could not be updated ");
        
        $row_state = $new_row_state;
        
//        return db_num_affected_rows();
}

function get_lots_header_details($id)
{
	$sql = "SELECT l.*, 
                sm.description
        FROM ".TB_PREF."lots l,
                ".TB_PREF."stock_master sm
                WHERE 
                    l.stock_id = sm.stock_id AND 
                    id=".db_escape($id);

	$result = db_query($sql,"Details of the selected lot could not be retrieved");

    return db_fetch($result);
}

function get_lot_details_for_view($id)
{
	$sql="SELECT lm.*, 
                        l.reference lot_description,
                            lo.location_name,
                                sm.description, units
                    
            FROM ".TB_PREF."lot_moves lm,
                    ".TB_PREF."lots l,
                        ".TB_PREF."locations lo,
                            ".TB_PREF."stock_master sm
                
                WHERE 
                    lm.tran_id=".db_escape($id)." AND
                        lm.lot_id = l.id AND
                            lm.stock_id = sm.stock_id AND
                                lm.loc_code = lo.loc_code
                            
                        ";

	return db_query($sql,"Selected lotdetails could not be retrieved");
}

function get_lot_moves_details($id, $stock_id)
{
    $sql = "SELECT l.reference
            FROM ".TB_PREF."lot_moves lm,
                    ".TB_PREF."lots l
             WHERE 
                    lm.tran_no=".db_escape($id)." AND
                        lm.stock_id=".db_escape($stock_id)." AND
                        lm.lot_id = l.id ";

	$result = db_query($sql,"Selected lot move details could not be retrieved");
        
        return db_fetch($result);
}
?>