<?php

function validate_import_stock_item_data($myrow,&$data)
{
        $category = "";
        
        $id = $myrow['id'];
        $data['id'] = $myrow['id'];
        $data['NewStockID']  = $myrow['item_code'];
        $data['description'] = $myrow['name'];
        $data['long_description'] = $myrow['long_description'];
        $data['minimum_price'] = 0;
        $data['high_value_item'] = 0;
        $data['sample_item'] = 0;
        $data['reference_cost'] = 0;
        $data['any_supplier'] = 0;
        $data['dimension_id'] = 0;
        $data['dimension2_id'] = 0;
        $data['editable'] = 0;
        $data['parameter1_id'] = 0;
        $data['country_of_origin'] = 0;
        $data['country_of_manufacture'] = 0;
        $data['supplier_id'] = 0;
        $data['local_agent'] = 0;
        $data['brand'] = 0;
        $data['parameter2_id'] = 0;
        $data['stock_control_method'] = 0;
        $data['upload_file'] = "not_uploading";
        $data['inactive'] = 0;
        $data['search_code'] = $myrow['search_code'];
        $data['part_no'] = $myrow['part_no'];
        
        $gci = get_category_id($myrow['category_name']);
        if($gci['passed'] == true){
            $data['category_id'] = $gci['message'];
            $sql = "SELECT * FROM ".TB_PREF."stock_category 
                                WHERE category_id =".db_escape($data['category_id']);
    
            $result = db_query($sql);
            $category = db_fetch($result);
        }
        else{
            return array('success' => false, 'message' => $gci['message']);
        }
        
        $itt = get_item_tax_type_id($myrow['item_tax_type_name']);
        if($itt['passed'] === true){
            $data['tax_type_id'] = $itt['message'];
        }
        else{
            return array('success' => false, 'message' => $itt['message']);
        }
        
        $itc = get_item_type_code($myrow['item_type_name']);
        if($itc['passed'] === true){
            $data['mb_flag'] = $itc['message'];
        }
        else{
            return array('success' => false, 'message' => $itc['message']);
        }
        
        $uc = get_units_code($myrow['units_of_measure_name']);
        if($uc['passed'] === true){
            $data['units'] = $uc['message'];
        }
        else{
            return array('success' => false, 'message' => $uc['message']);
        }
        
        $gscmc = get_stock_control_method_code($myrow['stock_control_method']);
        if($gscmc['passed'] === true){
            $data['stock_control_method'] = $gscmc['message'];
        }
        else{
            return array('success' => false, 'message' => $gscmc['message']);
        }
        
        $ynf = "";
        $ynf = get_yes_no_flag($myrow['editable_description']);
        if($ynf['passed'] === true){
            $data['editable_description'] = $ynf['message'];
        }
        else{
            return array('success' => false, 'message' => "editable_description - ".$ynf['message']);
        }        
        
        $ynf = "";
        $ynf = get_yes_no_flag($myrow['exclude_from_sales']);
        if($ynf['passed'] == true){
            $data['no_sale'] = $ynf['message'];
        }
        else{
            return array('success' => false, 'message' => "no_sale - ".$ynf['message']);
        }
        
        $ynf = "";
        $ynf = get_yes_no_flag($myrow['use_for_pos']);
        if($ynf['passed'] == true){
            $data['is_pos'] = $ynf['message'];
        }
        else{
            return array('success' => false, 'message' => "is_pos - ".$ynf['message']);
        }
        
        
        $ynf = "";
        $ynf = get_yes_no_flag($myrow['use_for_restaurant']);
        if($ynf['passed'] == true){
            $data['is_restaurant'] = $ynf['message'];
        }
        else{
            return array('success' => false, 'message' => "is_restaurant - ".$ynf['message']);
        }
        

        $ynf = "";
        $ynf = get_yes_no_flag($myrow['use_for_laundry']);
        if($ynf['passed'] == true){
            $data['is_laundry'] = $ynf['message'];
        }
        else{
            return array('success' => false, 'message' => "is_laundry - ".$ynf['message']);
        }
        
        $ynf = "";
        $ynf = get_yes_no_flag($myrow['high_value_item']);
        if($ynf['passed'] == true){
            $data['no_sale'] = $ynf['message'];
        }
        else{
            return array('success' => false, 'message' => "high_value_item - ".$ynf['message']);
        }
        
        $ynf = "";
        $ynf = get_yes_no_flag($myrow['sample_item']);
        if($ynf['passed'] == true){
            $data['no_sale'] = $ynf['message'];
        }
        else{
            return array('success' => false, 'message' => "sample_item - ".$ynf['message']);
        }
        
        $ynf = "";
        $ynf = get_yes_no_flag($myrow['any_supplier']);
        if($ynf['passed'] == true){
            $data['any_supplier'] = $ynf['message'];
        }
        else{
            return array('success' => false, 'message' => "any_supplier - ".$ynf['message']);
        }        
        
        //Item category name
        if($myrow['item_category'] != ""){
            $glvni = get_list_value_name_id("ITEM_PARAMETER_1", $myrow['item_category']);
            if($glvni['passed'] === true){
                $data['parameter1_id'] = $glvni['message'];
            }
            else{
                return array('success' => false, 'message' => "Item Category - ".$glvni['message']);
            }        
        }

        //Country of origin
        if($myrow['country_of_origin'] != ""){
            $glvni = "";
            $glvni = get_list_value_name_id("COUNTRIES", $myrow['country_of_origin']);
            if($glvni['passed'] === true){
                $data['parameter1_id'] = $glvni['message'];
            }
            else{
                return array('success' => false, 'message' => "Country of origin - ".$glvni['message']);
            }           
        }
        
        //Country of manufacture
        if($myrow['country_of_manuf'] != ""){
            $glvni = "";
            $glvni = get_list_value_name_id("COUNTRIES", $myrow['country_of_manuf']);
            if($glvni['passed'] === true){
                $data['country_of_manuf'] = $glvni['message'];
            }
            else{
                return array('success' => false, 'message' => "Country of manufacture - ".$glvni['message']);
            }   
        }

        if($myrow['local_agent'] != ""){
            $la = get_supplier_id_by_name($myrow['local_agent']);
            if($la['passed'] == true){
                $data['supplier_id'] = $la['message'];
            }
            else{
                return array('success' => false, 'message' => "Local agent - ".$la['message']);
            }         
        }

        //Brand
        if($myrow['brand'] != ""){
            $glvni = "";
            $glvni = get_list_value_name_id("ITEM_BRANDS", $myrow['brand']);
            if($glvni['passed'] === true){
                $data['brand'] = $glvni['message'];
            }
            else{
                return array('success' => false, 'message' => "Brand - ".$glvni['message']);
            }    
        }
        
        //Type
        if($myrow['type'] != ""){
            $glvni = "";
            $glvni = get_list_value_name_id("ITEM_PARAMETER_2", $myrow['type']);
            if($glvni['passed'] === true){
                $data['parameter2_id'] = $glvni['message'];
            }
            else{
                return array('success' => false, 'message' => "Type - ".$glvni['message']);
            }    
        }
                
        
        $ac = "";
        $ac = get_account_code($myrow['sales_account']);
        if($ac['passed'] == true){
            $data['sales_account'] = $ac['message'];
        }
        else{
            return array('success' => false, 'message' => "sales_account - ".$ac['message']);
        }  
        
        
        
        $ac = "";
        $ac = get_account_code($myrow['inventory_account']);
        if($ac['passed'] == true){
            $data['inventory_account'] = $ac['message'];
        }
        else{
            return array('success' => false, 'message' => "inventory_account - ".$ac['message']);
        }  
        
        
        $ac = "";
        $ac = get_account_code($myrow['cogs_account']);
        if($ac['passed'] == true){
            $data['cogs_account'] = $ac['message'];
        }
        else{
            return array('success' => false, 'message' => "cogs_account - ".$ac['message']);
        }  
        

        $ac = "";
        $ac = get_account_code($myrow['inventory_adjustments_account']);
        if($ac['passed'] == true){
            $data['adjustment_account'] = $ac['message'];
        }
        else{
            return array('success' => false, 'message' => "adjustment_account - ".$ac['message']);
        }  
        
        
        
        $ac = "";
        if($myrow['item_assembly_costs_account'] == ""){
            $myrow['item_assembly_costs_account'] = $category['dflt_assembly_act'];
        }
        $ac = get_account_code($myrow['item_assembly_costs_account']);
        if($ac['passed'] == true){
            $data['assembly_account'] = $ac['message'];
        }
        else{
            return array('success' => false, 'message' => "assembly_account - ".$ac['message']);
        }  

        
        $data['mb_flag'] = strtoupper($data['mb_flag']);
        
        $data['manual_entry'] = false;
        
        return array('success' => true, 'message' => "");
        
}

function validate_stock_item_data(&$item_data)
{
	if ($item_data['upload_file'] == 'No'){
        $error_message = 'Upload file not selected';
        if($item_data['manual_entry'] == true){
            display_error($error_message);
        }        
        return(array('success' => false, 'message' => $error_message));
    }
    
    
	if (strlen($item_data['description']) == 0) 
	{
        $error_message = 'The item name must be entered.';
        if($item_data['manual_entry'] == true){
            display_error($error_message);
            set_focus('description');
        }
        return(array('success' => false, 'message' => $error_message));
	} 
    
	if (strlen($item_data['NewStockID']) == 0) 
	{
		$error_message =  _('The item code cannot be empty');
        if($item_data['manual_entry'] == true){
            display_error($error_message);
            set_focus('NewStockID');
        }
        return(array('success' => false, 'message' => $error_message));
	}
    
    if (strstr($item_data['NewStockID'], " ") || strstr($item_data['NewStockID'],"'") || 
        strstr($item_data['NewStockID'], "+") || strstr($item_data['NewStockID'], "\"") || 
        strstr($item_data['NewStockID'], "&") || strstr($item_data['NewStockID'], "\t")||
        strstr($item_data['NewStockID'], "%"))    
    {
		$error_message = 'The item code cannot contain any of the following characters -  & + % OR a space OR quotes';
        if($item_data['manual_entry'] == true){
            display_error($error_message);
            set_focus('NewStockID');
        }
        return(array('success' => false, 'message' => $error_message));
	}
    
	if ($item_data['new_item'] && db_num_rows(get_item_kit($item_data['NewStockID'])))
	{
        $error_message = "This item code is already assigned to stock item or sale kit.";
        if($item_data['manual_entry'] == true){
            display_error($error_message);
            set_focus('NewStockID');
        }
        return(array('success' => false, 'message' => $error_message));
	}
    
    $unit = $item_data['units'] ;
    
    if($unit == '0' || $unit == '' || $unit == null){
    //if($item_data['units'] < 0){
        $error_message = "Please select a Base Unit.";
        if($item_data['manual_entry'] == true){
            display_error($error_message);
            set_focus('units');
        }
        return(array('success' => false, 'message' => $error_message));
    }
    
    if ($item_data['parameter1_id'] == -1) 
	{
		$item_data['parameter1_id'] = 0;
	}
    
    if ($item_data['country_of_origin'] == -1) 
	{
		$item_data['country_of_origin'] = 0;
	}
    
    if ($item_data['country_of_manufacture'] == -1) 
	{
		$item_data['country_of_manufacture'] = 0;
	}
    
    if ($item_data['local_agent'] == -1) 
	{
		$item_data['local_agent'] = 0;
	}
    
    if ($item_data['brand'] == -1) 
	{
		$item_data['brand'] = 0;
	}
    
    if ($item_data['parameter2_id'] == -1) 
	{
		$item_data['parameter2_id'] = 0;
	}
    
    return(array('success' => true, 'message' => ''));
    
}

function save_item($data)
{

    if(check_value('high_value_item') == 1){
        $data['reference_cost'] = 1000000;
    }
    else{
        $data['reference_cost'] = 0;
    }

            
    if ($data['new_item'] == 0) 
    { /*so its an existing one */
        begin_transaction();

        $item_info_before_update = get_item_edit_info($data['NewStockID']);

        update_stock_item($data);

        update_record_status($data['NewStockID'], $data['inactive'],
            'stock_master', 'stock_id');
        update_record_status($data['NewStockID'], $data['inactive'],
            'item_codes', 'item_code');

        $alternative_units = check_alternative_units($data['NewStockID'], true);
        if(!$alternative_units){
            // No alternate units found. Not even default alternate unit record created.
            if($data['units'] == ''){
            // No change in the units. Add default alternate unit record 
                add_alternate_unit($data['NewStockID'], $item_info_before_update['units'], $item_info_before_update['units'], 1, 0);
                commit_transaction();
                return(array('success' => true, 'message' => 'Item has been updated.'));
            }
            else{
            // This is a fresh item. 
                if($data['units'] != $item_info_before_update['units']){
                    //Base unit may have been changed. Add default alternate unit record with new selected unit.
                    add_alternate_unit($data['NewStockID'], $data['units'], $data['units'], 1, 0);
                    commit_transaction();
                    return(array('success' => true, 'message' => 'Item has been updated.'));
                }
                else{
                    cancel_transaction();
                }
            }
        }        
        else{
            // There is default alternate unit record.
            if($data['units'] == ''){
                // Not a fresh item. So no change in the base unit.
                commit_transaction();
                return(array('success' => true, 'message' => 'Item has been updated.'));
            }
            else{
                // Fresh item                
                if($data['units'] != $item_info_before_update['units']){
                    // Base unit has been changed.
                    $number_of_records = delete_alternate_units($data['NewStockID'], $item_info_before_update['units']);
                    if($number_of_records == 1){
                        add_alternate_unit($data['NewStockID'], $data['units'], $data['units'], 1, 0);
                        commit_transaction();
                        return(array('success' => true, 'message' => 'Item has been updated.'));
                    }
                    else{
                        cancel_transaction();
                    }
                }
                else{
                    // No change in the base unit.
                    commit_transaction();
                    return(array('success' => true, 'message' => 'Item has been updated.'));
                }
            }
        }
        return(array('success' => false, 'message' => 'Could not update the item.'));
    } 
    else 
    { //it is a NEW part
        begin_transaction();

        add_stock_item($data);

        add_alternate_unit($data['NewStockID'], $data['units'], $data['units'], 1, 0);
        commit_transaction();
        return(array('success' => true, 'message' => 'Item has been added.'));

    }
}

function update_stock_item($data)
{

    $stock_item = get_stock_item_edit_info($data['NewStockID']);
    //$data['units'] = $stock_item['units'];
    $data['mb_flag'] = $stock_item['mb_flag'];
    
        update_item($data['NewStockID'], $data['description'], 
                $data['long_description'], $data['category_id'], 
                $data['tax_type_id'], $data['units'], 
                $data['mb_flag'], $data['sales_account'], $data['inventory_account'], 
                $data['cogs_account'], 	$data['adjustment_account'], 
                $data['assembly_account'], $data['dimension_id'],
                $data['dimension2_id'], $data['no_sale'], $data['editable'],
                $data['is_pos'], $data['is_restaurant'],$data['is_laundry'], 
                $data['minimum_price'], $data['high_value_item'],$data['sample_item'], 
                $data['reference_cost'], $data['any_supplier'], 
                $data['parameter1_id'], $data['country_of_origin'], 
                $data['country_of_manufacture'], $data['local_agent'], 
                $data['brand'], $data['parameter2_id'], $data['stock_control_method'],
                $data['search_code'], $data['part_no']);

}
        
        
function add_stock_item($data){
    
        add_item($data['NewStockID'], $data['description'], 
                $data['long_description'], $data['category_id'], 
                $data['tax_type_id'], $data['units'], 
                $data['mb_flag'], $data['sales_account'], $data['inventory_account'], 
                $data['cogs_account'], 	$data['adjustment_account'], $data['assembly_account'], $data['dimension_id'],
                $data['dimension2_id'], $data['no_sale'], $data['editable'],
                $data['is_pos'], $data['is_restaurant'],$data['is_laundry'], 
                $data['minimum_price'], $data['high_value_item'],$data['sample_item'], 
                $data['reference_cost'], $data['any_supplier'], 
                $data['parameter1_id'], $data['country_of_origin'], 
                $data['country_of_manufacture'], $data['local_agent'], 
                $data['brand'], $data['parameter2_id'], $data['stock_control_method'],
                $data['search_code'], $data['part_no']);
    
}

function can_process(&$data)
{
    return true;
    
}

?>