'商品条码号',
's_price' => '市场价',
'f_price' => '门店采购价',
'c_price' => '成本价',
'price' => '销售价',
'min' => '采购起订数',
'unit' => '计量单位',
'weight' => '重量(kg)',
'cunhuo_code' => '存货编码',
);
private $unit = array
(
'请选择','件','箱','盒','袋','个','桶','台','套','瓶','罐','支','条','kg','g','L','ML'
);
# 主要价格字段
private $main = 'price';
public function __construct()
{
# 设置权限
Dever::load('manage/auth.init');
}
public function config()
{
$data['action'] = Dever::url('goods/lib/sku.update');
$data['goods_id'] = Dever::input('goods_id');
$data['other'] = Dever::input('other');
$data['other_id'] = Dever::input('other_id');
if ($data['other']) {
$data['other_col'] = $data['other'] . '_id';
}
$data['attr'] = Dever::input('attr');
$data['key'] = Dever::input('key');
$data['single_attr'] = Dever::input('single_attr');
$data['single_key'] = Dever::input('single_key');
$data['input_attr'] = Dever::input('input_attr');
$data['input_key'] = Dever::input('input_key');
$data['hook'] = Dever::input('hook');
$data['set'] = $this->getSet($data['other']);
return $data;
}
private function getSet($other)
{
if ($other) {
if (Dever::project($other)) {
$set = Dever::load($other . '/lib/sku')->set;
if (!isset($set['price_type'])) {
$set['price_type'] = false;
}
} else {
Dever::alert($other . '不存在');
}
} else {
$set = array('col' => $this->col, 'price_type' => false);
}
return $set;
}
public function show()
{
$config = Dever::load('goods/lib/sku')->config();
return $this->table($config['goods_id'], $config['other'], $config['other_id'], $config['set'], true);
}
public function update_api()
{
$config = Dever::load('goods/lib/sku')->config();
$where = array();
if ($config['other']) {
$where[$config['other_col']] = $config['other_id'];
$where['goods_id'] = $config['goods_id'];
$key = 'sku_id';
$table = $config['other'] . '/goods';
} else {
$where['info_id'] = $config['goods_id'];
$key = 'key';
$table = 'goods/info';
$w['option_info_id'] = $config['goods_id'];
$w['set_state'] = 2;
Dever::db('goods/info_sku')->updates($w);
}
$sku_table = $table . '_sku';
$code = array();
if ($config['key'] == -1) {
if ($config['other']) {
if ($config['other'] == 'price') {
$info = Dever::db($table)->one($config['other_id']);
} else {
$info = Dever::db($table)->one($where);
}
} else {
$info = Dever::db($table)->one($config['goods_id']);
}
$update = $where;
foreach ($config['set']['col'] as $k => $v) {
$update[$k] = Dever::input($k);
if (!$update[$k] && strstr($k, '_price')) {
$update[$k] = 0;
}
if ($k == 'code') {
if (!$update[$k]) {
Dever::alert('商品条码号不能为空');
}
if (in_array($update[$k], $code)) {
Dever::alert('商品条码号已重复');
}
$code[] = $update[$k];
}
}
if ($info && $update) {
$update['where_id'] = $info['id'];
Dever::db($table)->update($update);
unset($update['where_id']);
}
$where[$key] = -1;
/*
if ($config['other']) {
$info_sku = Dever::db('goods/info_sku')->one(array('goods_id' => $config['goods_id'], 'key' => -1));
$where[$key] = $info_sku['id'];
}
*/
$info = Dever::db($sku_table)->one($where);
$update[$key] = $where[$key];
Dever::config('base')->hook = false;
if (!$info) {
$state = Dever::db($sku_table)->insert($update);
} else {
$update['where_id'] = $info['id'];
$update['state'] = 1;
$state = Dever::db($sku_table)->update($update);
}
} elseif ($config['key']) {
if (!is_array($config['key'])) {
$config['key'] = explode(',', $config['key']);
}
$min = array();
$cur_sku = false;
$sku_id = false;
foreach ($config['key'] as $k => $v) {
$type = 1;
if (strstr($v, '||')) {
$temp = explode('||', $v);
$v = $temp[0];
$type = $temp[1];
}
if (isset($config['attr'][$k])) {
$data = $where;
if ($config['other']) {
$sku = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
if (!$sku) {
continue;
}
$data[$key] = $sku['id'];
$info = Dever::db($sku_table)->one($data);
} else {
$info = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
$data['attr'] = $config['attr'][$k];
$data[$key] = $v;
}
$data['type'] = $type;
foreach ($config['set']['col'] as $k1 => $v1) {
$value = Dever::input($k1);
$cur_sku = false;
if (isset($value[$k])) {
$data[$k1] = $value[$k];
if ($k1 == 'code') {
if (!$data[$k1]) {
Dever::alert('商品条码号不能为空');
}
if (in_array($data[$k1], $code)) {
Dever::alert('商品条码号已重复');
}
$code[] = $data[$k1];
}
if ($k1 == $this->main) {
if (!isset($min[$k1])) {
$min = $data;
$cur_sku = true;
} elseif ($min[$k1] > $data[$k1]) {
$min = $data;
$cur_sku = true;
}
}
}
}
Dever::config('base')->hook = false;
if (!$info) {
$temp = Dever::db($sku_table)->insert($data);
if ($cur_sku) {
$sku_id = $temp;
}
} else {
$data['where_id'] = $info['id'];
if ($cur_sku) {
$sku_id = $data['where_id'];
}
$data['state'] = 1;
Dever::db($sku_table)->update($data);
}
}
}
if (!$config['other'] && $min) {
$update = array();
if ($sku_id) {
$update['sku_id'] = $sku_id;
}
foreach ($config['set']['col'] as $k => $v) {
if (isset($min[$k])) {
$update[$k] = $min[$k];
}
}
$update['where_id'] = $config['goods_id'];
Dever::db($table)->update($update);
}
}
if ($config['single_key']) {
if (!is_array($config['single_key'])) {
$config['single_key'] = explode(',', $config['single_key']);
}
foreach ($config['single_key'] as $k => $v) {
$type = 2;
if (strstr($v, '||')) {
$temp = explode('||', $v);
$v = $temp[0];
$type = $temp[1];
}
if (isset($config['single_attr'][$k])) {
$data = $where;
if ($config['other']) {
$sku = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
if (!$sku) {
continue;
}
$data[$key] = $sku['id'];
$info = Dever::db($sku_table)->one($data);
} else {
$info = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
$data['attr'] = $config['single_attr'][$k];
$data[$key] = $v;
}
$data['type'] = $type;
foreach ($config['set']['col'] as $k1 => $v1) {
$value = Dever::input('single_' . $k1);
if (isset($value[$k])) {
$data[$k1] = $value[$k];
}
}
Dever::config('base')->hook = false;
if (!$info) {
$temp = Dever::db($sku_table)->insert($data);
} else {
$data['where_id'] = $info['id'];
$data['state'] = 1;
Dever::db($sku_table)->update($data);
}
}
}
}
if ($config['input_key']) {
if (!is_array($config['input_key'])) {
$config['input_key'] = explode(',', $config['input_key']);
}
foreach ($config['input_key'] as $k => $v) {
$type = 3;
if (strstr($v, '||')) {
$temp = explode('||', $v);
$v = $temp[0];
$type = $temp[1];
}
if (isset($config['input_attr'][$k])) {
$data = $where;
if ($config['other']) {
$sku = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
if (!$sku) {
continue;
}
$data[$key] = $sku['id'];
$info = Dever::db($sku_table)->one($data);
} else {
$info = Dever::db('goods/info_sku')->one(array('key' => $v, 'info_id' => $config['goods_id']));
$data['attr'] = $config['input_attr'][$k];
$data[$key] = $v;
}
$data['type'] = $type;
foreach ($config['set']['col'] as $k1 => $v1) {
$value = Dever::input('input_' . $k1);
if (isset($value[$k])) {
$data[$k1] = $value[$k];
}
}
Dever::config('base')->hook = false;
if (!$info) {
$temp = Dever::db($sku_table)->insert($data);
} else {
$data['where_id'] = $info['id'];
$data['state'] = 1;
Dever::db($sku_table)->update($data);
}
}
}
}
Dever::config('base')->hook = true;
Dever::out('yes');
}
public function table($goods, $other = false, $other_id = false, $set = false, $update = true, $parent_goods = false)
{
if (!$set) {
$set = $this->getSet($other);
}
$info = is_array($goods) ? $goods : Dever::db('goods/info')->one($goods);
$info = Info::init(-1)->info($info, 'view_reorder');
if ($info && $other && isset($set['price_type']) && $set['price_type']) {
$price_type = explode(',', $set['price_type']);
if (in_array($info['price_type'], $price_type)) {
if ($update) {
return '组合商品无法设置库存';
}
if ($info['goods'] && is_array($info['goods'])) {
$html = '';
foreach ($info['goods'] as $k => $v) {
$html .= $this->table($v['id'], $other, $other_id, $set, $update, $v) . '
';
}
$html .= '*组合商品的库存为以上商品中的最低库存为准';
return $html;
} else {
return '';
}
}
}
$input = '';
$sku_value = array();
if ($other) {
$other_col = $other . '_id';
$sku_value = Dever::db($other . '/goods_sku')->getData(array('goods_id' => $info['id'], $other_col => $other_id, 'state_1' => 1));
if ($other == 'price') {
$other_col = 'id';
}
$input = '';
}
$sku = Dever::db('goods/info_sku')->getDataByKeys(array('info_id' => $info['id']));
$group_attr = $single_attr = $input_attr = array();
$head = $single_head = $input_head = '';
$html = '请先选择属性';
if ($parent_goods) {
$head .= ' ';
$single_head .= '';
$input_head .= '';
$body = '商品名称 ';
$single_head .= '商品名称 ';
$input_head .= '商品名称 ';
}
if (isset($info['group_attr']) && $info['group_attr']) {
foreach ($info['group_attr'] as $k => $v) {
if (isset($info['attr'][$v]['option']) && $info['attr'][$v]['option']) {
$group_attr[$k] = $info['attr'][$v]['option'];
$head .= ''.$info['attr'][$v]['name'].' ';
}
}
}
if (isset($info['single_attr']) && $info['single_attr']) {
$single_head .= '属性名称 ';
foreach ($info['single_attr'] as $k => $v) {
if (isset($info['attr'][$v]['option']) && $info['attr'][$v]['option']) {
$single_attr = array_merge($single_attr, $info['attr'][$v]['option']);
}
}
}
if (isset($info['input_attr']) && $info['input_attr']) {
$input_head .= '属性名称 ';
$input_head .= '起算 ';
foreach ($info['input_attr'] as $k => $v) {
if (isset($info['attr'][$v]['option']) && $info['attr'][$v]['option']) {
$input_attr[$k] = $info['attr'][$v];
}
}
}
$col_num = 0;
foreach ($set['col'] as $k => $v) {
if (!$update && strstr($v, '^')) {
continue;
}
$col_num++;
if (strstr($v, '^')) {
$v = str_replace('^', '', $v);
}
if (strstr($v, '|')) {
$v = str_replace('|', '', $v);
}
$head .= ''.$v.' ';
$single_head .= ''.$v.' ';
$input_head .= ''.$v.' ';
}
$head .= '