id."', '$cross_item_id')");
}
function add_grouping($grouping_id)
{
return(Database::ado_execute_query("UPDATE ".Config::$db_tables['inventory_items']." SET grouping_id='$grouping_id' WHERE id='".$this->id."' LIMIT 1"));
}
function delete_cross_sell($cross_item_id)
{
Database::ado_execute_query("DELETE FROM ".Config::$db_tables['inventory_cross_sell']." WHERE item_id='".$this->id."' AND cross_item_id='$cross_item_id' LIMIT 1");
}
function delete_all_cross_sells()
{
Database::ado_execute_query("DELETE FROM ".Config::$db_tables['inventory_cross_sell']." WHERE item_id='".$this->id."'");
}
function sizes_list($Sizes)
{
// not used yet
if(!isset($this->sizes))
{
$this->set_sizes($Sizes);
}
$list = '';
foreach($this->sizes as $size)
{
if($list != '')
{
$list .= ', ';
}
$list .= $size;
}
return($list);
}
function delete_files_for_image($Items, $filename)
{
$image_files_to_delete = array();
foreach($Items->images as $image_size=>$image_size_info)
{
$image_files_to_delete[$image_size] = $image_size_info['path'].$filename;
}
foreach($image_files_to_delete as $path_file)
{
Utilities::delete_file($path_file);
}
}
function delete_image_files($Items, $which='all')
{
// by default this removes all files for all images associated with this item ($this->images)
// $which can be sent in as an array of image orders to delete
if(!isset($this->images))
{
$this->set_images($Items);
}
foreach($this->images as $image_order=>$image_info)
{
if($which == 'all' || in_array($image_order, $which))
{
$this->delete_files_for_image($Items, $image_info['filename']);
}
}
}
function has_orders($OrdersAdmin)
{
if($OrdersAdmin->get_item_orders($this->id)->RecordCount() > 0)
{
return(TRUE);
}
return(FALSE);
}
function get_avg_cost($ShipmentsAdmin)
{
if($RS = $ShipmentsAdmin->get_item_shipments($this->id))
{
$total_cost = 0;
$total_qty = 0;
while($row = $RS->FetchRow())
{
$total_cost += $row['unit_cost'];
$total_qty += $row['quantity'];
}
$avg_cost = round($total_cost / $total_qty, 2);
return($avg_cost);
}
else
{
return(FALSE);
}
}
function has_shipments($ShipmentsAdmin)
{
if($ShipmentsAdmin->get_item_shipments($this->id)->RecordCount() > 0)
{
return(TRUE);
}
return(FALSE);
}
function delete_image_records($which='all')
{
$del = "DELETE FROM ".Config::$db_tables['inventory_item_images']." WHERE item_id='".$this->id."'";
if($which != 'all')
{
foreach($which as $image_order)
{
$del .= " AND imageorder='$image_order'";
}
}
return(Database::ado_execute_query($del));
}
function has_inventory()
{
if(!isset($this->breakdown))
{
$this->set_breakdown();
}
foreach($this->breakdown as $bd_id=>$bd_info)
{
if($bd_info['qty_instock'] > 0)
{
return(TRUE);
}
}
return(FALSE);
}
function has_adjustments()
{
if(!isset($this->breakdown))
{
$this->set_breakdown();
}
foreach($this->breakdown as $bd_id=>$bd_info)
{
if(Database::ado_get_record_count("SELECT * FROM ".Config::$db_tables['inventory_adjustments']." WHERE item_breakdown_id='$bd_id'", "Item::has_adjustments") > 0)
{
return(TRUE);
}
}
return(FALSE);
}
function delete_cross_sell_records()
{
$del = "DELETE FROM ".Config::$db_tables['inventory_cross_sell']." WHERE item_id='".$this->id."' OR cross_item_id='".$this->data['id']."'";
return(Database::ado_execute_query($del, "Item::delete_cross_sell_records"));
}
function delete_category_records()
{
return(Database::ado_execute_query("DELETE FROM ".Config::$db_tables['inventory_item_categories']." WHERE item_id='".$this->id."'"));
}
function delete_breakdown_records()
{
return(Database::ado_execute_query("DELETE FROM ".Config::$db_tables['inventory_item_breakdown']." WHERE item_id='".$this->id."'"));
}
function delete_color($item_color_id, $Items)
{
// return false if color exists in a bd record
if(!isset($this->breakdown))
{
$this->set_breakdown();
}
if(!isset($this->colors))
{
$this->set_colors();
}
foreach($this->breakdown as $bd_id=>$bd_info)
{
if($bd_info['item_color_id'] == $item_color_id)
{
return(FALSE);
}
}
// delete swatch + swatch thumb images if they exist
if($this->colors[$item_color_id]['swatch'] != '')
{
Utilities::delete_file($Items->color_swatch_images['normal']['web_location'].$this->colors[$item_color_id]['swatch']);
}
if($this->colors[$item_color_id]['swatch_thumb'] != '')
{
Utilities::delete_file($Items->color_swatch_images['thumb']['web_location'].$this->colors[$item_color_id]['swatch_thumb']);
}
Database::ado_execute_query("DELETE FROM ".Config::$db_tables['inventory_item_colors']." WHERE id='$item_color_id' LIMIT 1", "Item::delete_color");
return(TRUE);
}
function delete_colors($Items)
{
if(!isset($this->colors))
{
$this->set_colors();
}
foreach($this->colors as $item_color_id=>$color_info)
{
$this->delete_color($item_color_id, $Items);
}
return($this->delete_color_records());
}
function delete_color_records()
{
return(Database::ado_execute_query("DELETE FROM ".Config::$db_tables['inventory_item_colors']." WHERE item_id='".$this->id."'"));
}
function delete_news()
{
return(Database::ado_execute_query("DELETE FROM ".Config::$db_tables['news']." WHERE item_id='".$this->id."'"));
}
function delete_record()
{
return(Database::ado_execute_query("DELETE FROM ".Config::$db_tables['inventory_items']." WHERE id='".$this->id."' LIMIT 1"));
}
function is_featured()
{
return(Database::ado_get_row("SELECT * FROM ".Config::$db_tables['vendor_designers_featured_images']." WHERE item_id='".$this->id."'", "Item::is_featured"));
}
function _delete ($Items, $OrdersAdmin, $Shipments)
{
if($this->has_inventory())
{
return(FALSE);
}
if($this->has_orders($OrdersAdmin))
{
return(FALSE);
}
if($this->has_shipments($Shipments))
{
return(FALSE);
}
if($this->has_adjustments())
{
return(FALSE);
}
if($this->is_featured())
{
return(FALSE);
}
$this->delete_image_files($Items);
$this->delete_image_records();
$this->delete_cross_sell_records();
$this->delete_category_records();
$this->delete_breakdown_records();
$this->delete_colors($Items);
$this->delete_news();
$this->delete_record();
return(TRUE);
}
function add_category ($category_id)
{
// make sure not already there
if(Database::ado_get_record_count("SELECT * FROM ".Config::$db_tables['inventory_item_categories']." WHERE item_id='".$this->id."' AND category_id='$category_id'", "m_item::add_category") > 0)
{
return(FALSE);
}
$ins = "INSERT into ".Config::$db_tables['inventory_item_categories']." VALUES(NULL, '".$this->id."', '$category_id')";
return(Database::ado_execute_query($ins, 'Item::add_category'));
}
function initialize_upload_images()
{
$this->upload_images = array(); // holds the random names of the images
}
function set_fields($Forms, $Items, $Designers, $Sizes, $form_type, $db_default=FALSE)
{
if(!isset($this->colors))
{
$this->set_colors();
}
// db_default var if true sets default field vals to db val
// form type of edit means that form has not yet been submitted
switch($form_type)
{
case 'newconfirm': // AFTER INITIAL UPLOAD FORM
$submitname = "confirmsubmit";
$submitvalue = "e n t e r i t e m";
$cancelname = 'confirmcancel';
break;
case 'edit':
$submitname = "editsubmit";
$submitvalue = "s u b m i t c h a n g e s";
$cancelname = 'editcancel';
break;
case 'editconfirm':
$submitname = "confirmsubmit";
$submitvalue = "c o n f i r m c h a n g e s";
$cancelname = 'confirmcancel';
break;
} // switch
$this->fields = array();
// images, captions
$has_images = FALSE;
for($i=1;$i<=$Items->max_images;++$i)
{
if(isset($this->upload_images['image'.$i]) || isset($_POST['image'.$i])) // would come from the posted hidden field in the case of newconfirm form that is submitted but has errors
{
$default = (isset($this->upload_images['image'.$i])) ? $this->upload_images['image'.$i] : $_POST['image'.$i];
$has_images = TRUE;
// uploaded images get put into hidden form fields
$this->fields['image'.$i] = array(
'type' => array(
'tag'=>'input_hidden'
),
'default' => $default
);
$this->fields['caption'.$i] = array(
'label' => 'IMAGE'.$i.'
Caption',
'label_css' => 'black_11_bold',
'label_valign' => 'bottom',
'type' => array(
'tag'=>'input_text',
'input_size' => 40,
'char_max' => 255
),
'html_before' => '
',
);
$this->fields['show_xlarge'.$i] = array(
'label' => 'Show xlarge?',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=>'select',
'options' => array('yes'=>'yes','no'=>'no'),
),
'html_after' => '
',
);
}
elseif(isset($this->images[$i])) // would only be true on edit page, not add page
{
$has_images = TRUE;
$html_after = ($form_type == 'edit') ? 'delete image' : ''; // only can delete image during edit process - not editconfirm
$this->fields['caption'.$i] = array(
'label' => 'IMAGE'.$i.'
Caption',
'label_css' => 'black_11_bold',
'label_valign' => 'bottom',
'type' => array(
'tag'=>'input_text',
'input_size' => 40,
'char_max' => 255
),
'html_before' => ''.$this->img_html($Items, 'medium', $i, '', $this->data['name']).'
',
'html_after' => $html_after,
);
$this->fields['show_xlarge'.$i] = array(
'label' => 'Show xlarge?',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=>'select',
'options' => array('yes'=>'yes','no'=>'no'),
),
'html_after' => '
',
);
}
}
$this->fields['enter_date'] = array(
'type' => array(
'tag'=> 'input_hidden'
)
); // only used for inserting and will get set to NOW() in _insert
$this->fields['style_number'] = array(
'blank_space_vertical' => 10,
'label' => 'Style #',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 30,
'char_max' => 50
)
);
$season_options = array();
$i = 0;
$sRS = Database::ado_execute_query("SELECT * FROM ".Config::$db_tables['inventory_seasons']." ORDER BY id DESC");
while($row = $sRS->FetchRow())
{
++$i;
$season_options[$row['id']] = $row['season'];
if($i == 1)
{
$so_def = $row['id'];
}
}
// 0 option
$season_options[0] = 'n/a - AIS';
$this->fields['season_id'] = array(
'label' => 'Season',
'label_css' => 'black_11_bold',
'type' => array(
'tag' => 'select',
'options' => $season_options,
'top_option' => array('' => ' -- select -- '),
),
'validation' => array('required'),
'default' => $so_def
);
$this->fields['name'] = array(
'label' => 'Name',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 30,
'char_max' => 255
),
'validation' => array('required')
);
$this->fields['short_name'] = array(
'label' => 'Short Name',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 21,
'char_max' => 21
)
);
$this->fields['description'] = array(
'label' => 'Description',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'textarea',
'cols' => 50,
'rows' => 4
)
);
$this->fields['keywords'] = array(
'label' => 'Keywords',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 30,
'char_max' => 255
)
);
$des_def = (isset($_GET['designer_id'])) ? $_GET['designer_id'] : '';
$this->fields['designer_id'] = array(
'label' => 'Designer',
'label_css' => 'black_11_bold',
'type' => array(
'tag' => 'predefined',
'class' => 'Designers',
'function' => 'generate_select',
'args' => array('Forms'=>$Forms, 'name'=>'designer_id', 'RS'=>$Designers->get_RecordSet('zzm_item::set_fields', 'all', '', 'name', ''), 'onchange'=>'', 'top_option'=>array(''=>' -- select -- '), 'selected'=>$des_def, 'css'=>'') // hokey
),
'validation' => array('required')
);
// colors
$i = 0;
foreach($this->colors as $item_color_id=>$color_info)
{
//var_dump($color_info);
++$i;
$this->fields['color['.$item_color_id.']'] = array(
'label' => 'Color '.$i,
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 20,
'char_max' => 50
),
'html_after' => 'delete [do not use this field to add a new color!]',
);
$this->fields['color_hex['.$item_color_id.']'] = array(
'label' => 'Color '.$i.' Hex',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 6,
'char_max' => 6
),
'html_after' => '[ie CCCCCC]',
'validation' => array("regex"=>"/[a-zA-Z0-9]{6}/")
);
$this->fields['color_description['.$item_color_id.']'] = array(
'label' => 'Color '.$i.' Descrip',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 30,
'char_max' => 255
)
);
$html_before = ($this->colors[$item_color_id]['swatch'] != '') ? '
' : '';
$this->fields['color_swatch['.$item_color_id.']'] = array(
'label' => 'Color '.$i.' Swatch',
'label_css' => 'black_11_bold',
'type' => array(
'tag' => 'input_file'
),
'html_before' => $html_before
);
$html_before = ($this->colors[$item_color_id]['swatch_thumb'] != '') ? '
' : '';
$this->fields['color_swatch_thumb['.$item_color_id.']'] = array(
'label' => 'Color '.$i.' Swatch Thumbnail',
'label_css' => 'black_11_bold',
'type' => array(
'tag' => 'input_file'
),
'html_before' => $html_before
);
$this->fields['color_ais_web['.$item_color_id.']'] = array(
'label' => 'Color '.$i.' AIS Web',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'select',
'options' => array('no'=>'no','yes'=>'yes'),
'top_option' => array(),
)
);
$this->fields['color_ais_store['.$item_color_id.']'] = array(
'label' => 'Color '.$i.' AIS Store',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'select',
'options' => array('no'=>'no','yes'=>'yes'),
'top_option' => array(),
'onchange' => ''
)
);
$this->fields['color_available_date['.$item_color_id.']'] = array(
'label' => 'Color '.$i.' Available Date',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 10,
'char_max' => 10
),
'html_after' => '[YYYY-MM-DD]',
'validation' => array('mysql_date')
);
} // end existing color edit fields
$colors_label = (!isset($this->data)) ? 'Color(s)' : 'Add Color(s)';
$this->fields['colors'] = array(
'label' => $colors_label,
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'textarea',
'cols' => 50,
'rows' => 2
),
'html_after' => '[ie white,black,green]'
);
$size_scale_options = array();
$RS = $Sizes->size_scales_RS();
while($row = $RS->FetchRow())
{
$sizes_comma_sep = '';
$sizes_for_scale = $Sizes->get_sizes_for_scale($row['id']);
foreach($sizes_for_scale as $size)
{
if($sizes_comma_sep != '')
{
$sizes_comma_sep .= ',';
}
$sizes_comma_sep .= $size;
}
$size_scale_options[$row['id']] = $sizes_comma_sep;
}
$this->fields['size_scale_id'] = array(
'label' => 'Size Scale',
'label_css' => 'black_11_bold',
'type' => array(
'tag' => 'select',
'options' => $size_scale_options,
'top_option' => array('' => ' -- select -- '),
'onchange' => ''
)
);
$this->fields['fabrication'] = array(
'label' => 'Fabrication',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 30,
'char_max' => 255
)
);
$this->fields['measurements'] = array(
'label' => 'Measurements',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'textarea',
'cols' => 50,
'rows' => 4
)
);
$this->fields['care'] = array(
'label' => 'Care',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 30,
'char_max' => 255
)
);
$this->fields['price'] = array(
'label' => 'Price',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 8,
'char_max' => 10
),
'html_before' => '$',
'validation' => array('required','numeric')
);
$this->fields['sale_price'] = array(
'label' => 'Sale Price',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 8,
'char_max' => 10
),
'html_before' => '$',
'validation' => array('numeric')
);
$this->fields['price_web'] = array(
'label' => 'Web Price',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 8,
'char_max' => 10
),
'html_before' => '$',
'validation' => array('numeric')
);
$this->fields['sale_price_web'] = array(
'label' => 'Web Sale Price',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 8,
'char_max' => 10
),
'html_before' => '$',
'validation' => array('numeric')
);
$this->fields['preorder_cutoff_date'] = array(
'label' => 'Order Cutoff Date [for preorders]',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 10,
'char_max' => 10
),
'html_after' => '[YYYY-MM-DD]',
'validation' => array('mysql_date')
);
$this->fields['available_date'] = array(
'label' => 'Available Date [for preorders]',
'label_css' => 'black_11_bold',
'type' => array(
'tag'=> 'input_text',
'input_size' => 10,
'char_max' => 10
),
'html_after' => '[YYYY-MM-DD]',
'validation' => array('mysql_date')
);
// status_web
// only allow active status if there are images
$status_web_options = array('inactive'=>'inactive');
if($has_images)
{
$status_web_options['active'] = 'active';
}
$this->fields['status_web'] = array(
'label' => 'Web Status',
'label_css' => 'black_11_bold',
'type' => array(
'tag' => 'select',
'options' => $status_web_options,
'top_option' => array(),
),
'validation' => array('required')
);
$this->fields[$submitname] = array(
'blank_space_vertical' => 10,
'type' => array(
'tag'=> 'input_submit',
),
'input_css' => 'blue_submit',
'default' => $submitvalue,
'html_after' => 'or',
'end_row' => FALSE
);
$this->fields[$cancelname] = array(
'start_row' => FALSE,
'type' => array(
'tag'=> 'input_submit',
),
'input_css' => 'red_cancel',
'default' => 'c a n c e l',
);
if($db_default)
{
// for some reason the status_web field was not getting included in the loop below (????)
$this->fields['status_web']['default'] = $this->data['status_web'];
// sets default from db for all fields
foreach($this->fields as $fieldName=>$fieldInfo)
{
$default = '';
$exclude_fields = array('colors');
if(!in_array($fieldName, $exclude_fields) && $fieldInfo['type']['tag'] != 'input_submit' && $fieldInfo['type']['tag'] != 'input_hidden')
{
// first do fields that are not part of $this->data
if(strstr($fieldName, 'caption'))
{
$default = (isset($this->images[substr($fieldName, 7)]['caption'])) ? $this->images[substr($fieldName, 7)]['caption'] : '';
}
elseif(strstr($fieldName, 'show_xlarge'))
{
$default = (isset($this->images[substr($fieldName, 11)]['show_xlarge'])) ? $this->images[substr($fieldName, 11)]['show_xlarge'] : 'yes';
}
elseif(strstr($fieldName, 'color') && strstr($fieldName, '['))
{
foreach($this->colors as $item_color_id=>$color_info)
{
if($fieldName == 'color['.$item_color_id.']')
{
$default = $color_info['name'];
break;
}
elseif($fieldName == 'color_hex['.$item_color_id.']')
{
$default = $color_info['hex'];
break;
}
elseif($fieldName == 'color_description['.$item_color_id.']')
{
$default = $color_info['description'];
break;
}
elseif($fieldName == 'color_swatch['.$item_color_id.']')
{
$default = $color_info['swatch'];
break;
}
elseif($fieldName == 'color_swatch_thumb['.$item_color_id.']')
{
$default = $color_info['swatch_thumb'];
break;
}
elseif($fieldName == 'color_ais_web['.$item_color_id.']')
{
$default = $color_info['ais_web'];
break;
}
elseif($fieldName == 'color_ais_store['.$item_color_id.']')
{
$default = $color_info['ais_store'];
break;
}
elseif($fieldName == 'color_available_date['.$item_color_id.']')
{
$default = ($color_info['available_date'] != '0000-00-00') ? $color_info['available_date'] : '';
break;
}
}
}
// special cases for sale pricing
elseif($fieldName == 'sale_price' || $fieldName == 'sale_price_web')
{
$default = ($this->data[$fieldName] > 0) ? $this->data[$fieldName] : '';
}
elseif($fieldName == 'preorder_cutoff_date')
{
//echo 'here: '.$this->data[$fieldName];
$default = ($this->data[$fieldName] != '0000-00-00') ? $this->data[$fieldName] : '';
}
elseif($fieldName == 'available_date')
{
//echo 'here: '.$this->data[$fieldName];
$default = ($this->data[$fieldName] != '0000-00-00') ? $this->data[$fieldName] : '';
}
else
{
$default = $this->data[$fieldName];
}
$this->fields[$fieldName]['default'] = $default;
//echo 'h: '.$fieldName.': '.$default.'
';
}
}
}
}
function _validate($ItemForm, $Sizes='')
{
if(!isset($this->breakdown))
{
$this->set_breakdown();
}
// unique validation for this page
// style number must be unique within designer
if(trim($ItemForm->get_field_value('style_number')) != '')
{
$style_sel = "SELECT * FROM ".Config::$db_tables['inventory_items']." WHERE style_number = '".trim(addslashes($ItemForm->get_field_value('style_number')))."' AND designer_id='".$ItemForm->get_field_value('designer_id')."'";
if($this->id > 0)
{
// for edits don't validate against itself for uniqueness
$style_sel .= "AND id != '".$this->id."'";
}
if(Database::ado_get_row($style_sel))
{
$ItemForm->set_field_error('style_number', 'already exists for this designer');
$ItemForm->set_error(TRUE);
return(FALSE);
}
}
// if putting in an available date must be in future and email site manager
if($ItemForm->get_field_value('available_date') != '' && $ItemForm->get_field_value('available_date') != '0000-00-00')
{
//echo Utilities::date_compare($ItemForm->get_field_value('available_date'), Utilities::date_convert_to_mysql(date("m/d/y"))) . '
' . Utilities::convert_time_to_sec($min=0,$hr=0,$day=14);exit();
if(Utilities::date_compare($ItemForm->get_field_value('available_date'), Utilities::date_convert_to_mysql(date("m/d/y"))) < Utilities::convert_time_to_sec($min=0,$hr=0,$day=14) )
{
$ItemForm->set_field_error('available_date', 'date must be at least 2 weeks away');
$ItemForm->set_error(TRUE);
return(FALSE);
}
Utilities::notify(Config::$site_manager_email, 'ITEM AVAILABILITY DATE SET', Config::$url."/admin/items/edit.hello?id=".$this->id);
}
// if setting to active must have a web price
if($ItemForm->get_field_value('status_web') == 'active' && $ItemForm->get_field_value('price_web') == '')
{
$ItemForm->set_field_error('price_web', 'required for active web item');
$ItemForm->set_error(TRUE);
return(FALSE);
}
if($this->id > 0)
{
// this is an item edit
// if changing size scale make sure that current bd sizes are a subset of new scale
if($ItemForm->get_field_value('size_scale_id') != $this->data['size_scale_id'])
{
$new_sizes = $Sizes->get_sizes_for_scale($ItemForm->get_field_value('size_scale_id')); // size_id=>size
foreach($this->breakdown as $bd_id=>$bd_info)
{
if(array_key_exists($bd_info['size_id'], $this->sizes) && !array_key_exists($bd_info['size_id'], $new_sizes))
{
$ItemForm->set_field_error('size_scale_id', 'inventory exists for old sizes');
$ItemForm->set_error(TRUE);
return(FALSE);
}
}
}
// new colors must be unique
$new_colors = explode(",", $_POST['colors']);
foreach($new_colors as $color)
{
$ok = TRUE;
foreach($this->colors as $item_color_id=>$color_info)
{
if(strtoupper($color_info['name']) == strtoupper($color))
{
$ok = FALSE;
}
}
if(!$ok)
{
$ItemForm->set_field_error('colors', 'duplicate exists for '.$color);
$ItemForm->set_error(TRUE);
return(FALSE);
}
}
}
}
function _insert($Items, $ItemForm)
{
$ins = "INSERT INTO ".Config::$db_tables['inventory_items']." (";
$i = 0;
$ins_val = " VALUES(";
foreach($this->fields as $field_name => $field_info)
{
++$i;
if($field_name != 'colors' && !strstr($field_name, "cancel") && !strstr($field_name, "submit") && !strstr($field_name, "caption") && !strstr($field_name, "show_xlarge") && !strstr($field_name, "image") && !strstr($field_name, "category")) // caption and image fields go into item_images table, color and size and qty fields go into item_breakdown table, category into item_categories
{
$ins .= $field_name.',';
switch($field_name)
{
case 'enter_date': $ins_val .= "NOW(),"; break;
default: $ins_val .= "'".trim(addslashes($ItemForm->get_field_value($field_name)))."',";
}
}
}
$ins = substr($ins, 0, strlen($ins)-1) . ')';
$ins_val = substr($ins_val, 0, strlen($ins_val)-1) . ')';
$insert = $ins.$ins_val;
//echo $insert;
if(!$item_id = Database::ado_execute_query($insert, 'Item::_insert'))
{
return(FALSE);
}
// images and captions
for($i = 1; $i <= $Items->max_images; ++$i)
{
if(isset($_POST['image'.$i]) && $_POST['image'.$i] != '')
{
$item_images_ins = "INSERT INTO ".Config::$db_tables['inventory_item_images']." VALUES(NULL, '$item_id', '".$_POST['image'.$i]."', '".trim(addslashes($_POST['caption'.$i]))."', '".$_POST['show_xlarge'.$i]."', '$i')";
//echo '
'.$item_images_ins;
if(!Database::ado_execute_query($item_images_ins, 'Item::_insert'))
{
return(FALSE);
}
else
{
//Utilities::notify(Config::tech_email, 'Item Image Inserted', $item_images_ins);
}
}
}
// end images and captions
// colors
$new_colors = explode(",", $_POST['colors']);
foreach($new_colors as $color)
{
if(trim($color) != '')
{
if(!$this->insert_color($item_id, $color, '', '', '', '', 'no', 'no', ''))
{
return(FALSE);
}
}
}
return($item_id);
}
function insert_color($item_id, $color, $color_hex='', $color_description='', $swatch='', $swatch_thumb='', $ais_web='no', $ais_store='no', $available_date='')
{
$ins = "INSERT INTO ".Config::$db_tables['inventory_item_colors']." VALUES(NULL, '$item_id', '".trim(addslashes($color))."', '$color_hex', '".trim(addslashes($color_description))."', '$swatch', '$swatch_thumb', '$ais_web', '$ais_store', '$available_date')";
return(Database::ado_execute_query($ins, 'Item::insert_color'));
}
function _update($ItemForm, $Items, $Sizes)
{
$items_upd = "UPDATE ".Config::$db_tables['inventory_items']." SET";
$i = 0;
foreach($this->fields as $field_name => $field_info)
{
++$i;
if(!strstr($field_name, "color") && !strstr($field_name, "cancel") && !strstr($field_name, "submit") && !strstr($field_name, "caption") && !strstr($field_name, "show_xlarge") && !strstr($field_name, "image") && !strstr($field_name, "category")) // caption and image fields go into item_images table, color and size and qty fields go into item_breakdown table, category into item_categories
{
switch($field_name)
{
case 'enter_date': break;
default: $items_upd .= " $field_name='".trim(addslashes($ItemForm->get_field_value($field_name)))."',";
}
}
}
$items_upd = substr($items_upd, 0, strlen($items_upd)-1) . " WHERE id='".$this->id."' LIMIT 1";
//echo $items_upd;
$affected_rows = Database::ado_execute_query($items_upd, 'Item::_update');
if($affected_rows === FALSE)
{
return(FALSE);
}
// images and captions
for($i = 1; $i <= $Items->max_images; ++$i)
{
if(isset($_POST['image'.$i]) && $_POST['image'.$i] != '')
{
// either inserting or update
if(isset($this->images[$i]))
{
// update so delete old image
$images_to_delete = array($i);
$this->delete_image_files($Items, $images_to_delete);
$item_images_upd = "UPDATE ".Config::$db_tables['inventory_item_images']." SET imagefile='".$_POST['image'.$i]."', caption='".trim(addslashes($_POST['caption'.$i]))."', show_xlarge='".$_POST['show_xlarge'.$i]."' WHERE item_id='".$this->data['id']."' AND imageorder = '$i' LIMIT 1";
//echo '
'.$item_images_upd;
$affected_rows = Database::ado_execute_query($item_images_upd, 'Item::_update');
if($affected_rows === FALSE)
{
return(FALSE);
}
else
{
Utilities::notify(Config::$tech_email, 'Item Image Updated', $item_images_upd);
}
}
else
{
$item_images_ins = "INSERT INTO ".Config::$db_tables['inventory_item_images']." VALUES(NULL, '".$this->id."', '".$_POST['image'.$i]."', '".trim(addslashes($_POST['caption'.$i]))."', '".$_POST['show_xlarge'.$i]."', '$i')";
//echo '
'.$item_images_ins;
if(!Database::ado_execute_query($item_images_ins, 'Item::_update'))
{
return(FALSE);
}
else
{
Utilities::notify(Config::$tech_email, 'Item Image Inserted', $item_images_ins);
}
}
}
elseif(isset($_POST['caption'.$i]))
{
// update just the caption for existing images if it has changed
if($_POST['caption'.$i] != $this->images[$i]['caption'] || $_POST['show_xlarge'.$i] != $this->images[$i]['show_xlarge'])
{
$item_images_upd = "UPDATE ".Config::$db_tables['inventory_item_images']." SET caption='".trim(addslashes($_POST['caption'.$i]))."', show_xlarge='".$_POST['show_xlarge'.$i]."' WHERE item_id='".$this->id."' AND imageorder = '$i' LIMIT 1";
//echo '
'.$item_images_upd;
$affected_rows = Database::ado_execute_query($item_images_upd, 'Item::_update');
if($affected_rows === FALSE)
{
return(FALSE);
}
}
}
}
// end images and captions
// colors
// existing
if(isset($_POST['color']))
{
foreach($_POST['color'] as $item_color_id=>$color_name)
{
// do not overwrite swatch images
$swatch_upd = ($ItemForm->get_field_value('color_swatch['.$item_color_id.']') != '') ? " swatch='".$ItemForm->get_field_value('color_swatch['.$item_color_id.']')."'," : "";
$swatch_thumb_upd = ($ItemForm->get_field_value('color_swatch_thumb['.$item_color_id.']') != '') ? " swatch_thumb='".$ItemForm->get_field_value('color_swatch_thumb['.$item_color_id.']')."'," : "";
$item_colors_upd = "UPDATE ".Config::$db_tables['inventory_item_colors']." SET color='".trim(addslashes($color_name))."', color_hex='".trim(addslashes($_POST['color_hex'][$item_color_id]))."', color_description='".trim(addslashes($_POST['color_description'][$item_color_id]))."', $swatch_upd $swatch_thumb_upd ais_web='".$_POST['color_ais_web'][$item_color_id]."', ais_store='".$_POST['color_ais_store'][$item_color_id]."', available_date='".$_POST['color_available_date'][$item_color_id]."' WHERE id='$item_color_id' LIMIT 1";
Database::ado_execute_query($item_colors_upd, 'Item::_update');
}
}
// new
$new_colors = explode(",", $_POST['colors']);
foreach($new_colors as $color)
{
if(trim($color) != '')
{
// insert if not there
$ok = TRUE;
foreach($this->colors as $item_color_id=>$color_info)
{
if(strtoupper($color_info['name']) == strtoupper($color))
{
$ok = FALSE;
}
}
if($ok)
{
$item_colors_ins = "INSERT INTO ".Config::$db_tables['inventory_item_colors']." VALUES(NULL, '".$this->id."', '".trim(addslashes($color))."', '', '', '', '', 'no', 'no', '')";
if(!Database::ado_execute_query($item_colors_ins, 'Item::_update'))
{
return(FALSE);
}
}
}
}
return(TRUE);
}
function set_upload_fields($Items)
{
$this->upload_fields = array();
$this->upload_fields['MAX_FILE_SIZE'] = array(
'label' => '',
'label_css' => '',
'type' => array(
'tag'=>'input_hidden'
),
'input_css' => '',
'default' => '10000000',
'required' => FALSE
);
for($i=1;$i<=$Items->max_images;++$i)
{
$this->upload_fields['image'.$i] = array(
'label' => 'Image '.$i,
'label_css' => 'black_11_bold',
'type' => array(
'tag'=>'input_file'
),
'input_css' => '',
'required' => FALSE
);
if($i == 1)
{
$this->upload_fields['image'.$i]['input_cell_css'] = 'red_11_bold';
$this->upload_fields['image'.$i]['html_after'] = '[Main Image]';
}
}
$this->upload_fields['upload'] = array(
'label' => '',
'label_css' => '',
'type' => array('tag'=>'input_submit'),
'input_css' => 'blue_submit',
'default' => ' u p l o a d ',
'required' => FALSE
);
}
function set_upload_form($type='insert', $submitStatus='')
{
$qs = (isset($this->data['id'])) ? '?id='.$this->data['id'] : '';
$top_text = ($type == 'insert') ? 'Upload Item image(s) skip images' : 'Replace / Add New Item image(s)';
$this->UploadForm = new Form('itemImages', 'multipart/form-data', 'post', $_SERVER['PHP_SELF'].$qs, $this->upload_fields, $submitStatus);
}
function add_upload_images($field, $image_name)
{
$this->upload_images[$field] = $image_name;
}
function get_web_popup_link()
{
// force qs var forces page to open even if no images or inventory
$web_popup_link = 'javascript:window.open(\'/dressing_room/?id='.$this->id.'&force\',\'popup\', \'width=800,height=600,scrollbars=yes,resizable=yes\');void 0';
return($web_popup_link);
}
function is_dummy()
{
if(!isset($this->data))
{
$this->set_data();
}
return ($this->data['name'] == 'dummy item') ? TRUE : FALSE;
}
function insert_bd($qty, $item_color_id, $size_id)
{
// make sure this is not a dummy item
if($this->is_dummy())
{
return(TRUE);
}
$ins = "INSERT INTO ".Config::$db_tables['inventory_item_breakdown']." VALUES(NULL, '".$this->id."', '$item_color_id', '$size_id', '$qty')";
//echo $ins;
return(Database::ado_execute_query($ins, 'Items::insert_bd')); // returns bd_id
}
function is_gc()
{
if(!isset($this->data))
{
$this->set_data();
}
return ($this->data['name'] == 'Gift Certificate') ? TRUE : FALSE;
}
function notify_negative_bd($item_color_id, $size_id, $bd_id='')
{
// get color and size text
if(!isset($this->colors))
{
$this->set_colors();
}
if(!isset($this->colors))
{
$this->set_sizes();
}
if($bd_id == '')
{
$color = $this->colors[$item_color_id]['name'];
$size = $this->sizes[$size_id];
}
else
{
if(!isset($this->breakdown))
{
$this->set_breakdown();
}
$color = $this->colors[$this->breakdown[$bd_id]['item_color_id']];
$size = $this->sizes[$this->breakdown[$bd_id]['size_id']];
}
Utilities::notify(Config::$tech_email, "Item bd adjusted to negative", "https://".Config::$domain."/admin/items/?item_id=".$this->id."\ncolor: ".$color."\nsize: ".$size);
}
function adjust_bd_qty($adjust_qty, $item_color_id, $size_id, $bd_id='')
{
// adds the adjust qty to current bd qty - adjust_qty can be positive or negative
// inputs can be both item_color_id and size_id or just bd_id
if($adjust_qty == 0)
{
return(TRUE);
}
$adjust_sign = ($adjust_qty > 0) ? '+' : '-';
$where = " WHERE ";
$where .= ($bd_id != '') ? "id='$bd_id'" : "item_id='".$this->id."' AND item_color_id='$item_color_id' AND size_id='$size_id'";
$upd = "UPDATE ".Config::$db_tables['inventory_item_breakdown']." SET qty_instock = qty_instock $adjust_sign ".abs($adjust_qty)." $where LIMIT 1";
//echo $upd;
// send alert if bd qty < 0
$this->set_breakdown();// refresh after update
if($this->get_breakdown_qty_instock($item_color_id, $size_id, $bd_id) < 0)
{
$this->notify_negative_bd($item_color_id, $size_id, $bd_id='');
}
return(Database::ado_execute_query($upd, 'Item::adjust_bd_qty'));
}
function update_bd_qty($qty, $item_color_id, $size_id, $bd_id='')
{
// inputs can be both item_color_id and size_id or just bd_id
$where = " WHERE ";
$where .= ($bd_id != '') ? "id='$bd_id'" : "item_id='".$this->id."' AND item_color_id='$item_color_id' AND size_id='$size_id'";
$upd = "UPDATE ".Config::$db_tables['inventory_item_breakdown']." SET qty_instock='$qty' $where LIMIT 1";
//echo $upd;
// send alert if bd qty < 0
if($this->get_breakdown_qty_instock($item_color_id, $size_id, $bd_id) < 0)
{
$this->notify_negative_bd($item_color_id, $size_id, $bd_id='');
}
return(Database::ado_execute_query($upd, 'Item::adjust_breakdown_qty'));
}
function get_bd_id($item_color_id, $size_id)
{
return(Database::ado_get_one("SELECT id FROM ".Config::$db_tables['inventory_item_breakdown']." WHERE item_id='".$this->id."' AND item_color_id='$item_color_id' AND size_id='$size_id'", "Item:: breakdown_exists"));
}
function insert_inventory_adjustment($item_color_id, $size_id, $bd_id, $qty, $cost, $action, $reason)
{
// into inventory_adjustments table for manual adjustment
// inputs can be both item_color_id and size_id or just bd_id
if($bd_id == '')
{
if(!$bd_id = $this->get_bd_id($item_color_id, $size_id))
{
return(FALSE);
}
}
$ins = "INSERT INTO ".Config::$db_tables['inventory_adjustments']." VALUES(NULL, '$bd_id', '$qty', '$cost', '$action', '".trim(addslashes($reason))."', NOW())";
return(Database::ado_execute_query($ins, 'Item::insert_inventory_adjustment'));
}
function get_item_color_id($color_name)
{
if(!isset($this->colors))
{
$this->set_colors();
}
foreach($this->colors as $item_color_id=>$item_color_info)
{
if($item_color_info['name'] == $color_name)
{
return($item_color_id);
}
}
return(FALSE);
}
function get_size_id($size_text, $Sizes)
{
if(!isset($this->sizes))
{
$this->set_sizes($Sizes);
}
foreach($this->sizes as $size_id=>$size)
{
if($size == $size_text)
{
return($size_id);
}
}
return(FALSE);
}
}
?>