<?php

namespace Goods\Lib;

use Dever;

class Sku
{
    # 设置sku特殊字段
    private $col = array
    (
        'code' => '商品条码号',
        's_price' => '市场价',
        'f_price' => '门店采购价',
        'c_price' => '成本价',
        'price' => '销售价',
        'min' => '采购起订数',
        'unit' => '计量单位',
        'weight' => '重量(kg)',
    );

    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) . '<br />';
                    }

                    $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 = '<input type="hidden" name="goods_id" value="'.$info['id'].'"/><input type="hidden" name="'.$other_col.'" value="'.$other_id.'"/>';
        }

        $sku = Dever::db('goods/info_sku')->getDataByKeys(array('info_id' => $info['id']));

        $group_attr = $single_attr = $input_attr = array();
        $head = $single_head = $input_head = '<thead><tr>';
        $html = '请先选择属性';

        if ($parent_goods) {
            $head .= '<th>商品名称</th>';
            $single_head .= '<th>商品名称</th>';
            $input_head .= '<th>商品名称</th>';
        }

        if (isset($info['group_attr']) && $info['group_attr']) {
            foreach ($info['group_attr'] as $k => $v) {
                if (isset($info['attr'][$v]['option'])) {
                    $group_attr[$k] = $info['attr'][$v]['option'];
                    $head .= '<th>'.$info['attr'][$v]['name'].'</th>';
                }
            }
        }

        if (isset($info['single_attr']) && $info['single_attr']) {
            $single_head .= '<th>属性名称</th>';
            foreach ($info['single_attr'] as $k => $v) {
                if (isset($info['attr'][$v]['option'])) {
                    $single_attr = array_merge($single_attr, $info['attr'][$v]['option']);
                }
            }
        }

        if (isset($info['input_attr']) && $info['input_attr']) {
            $input_head .= '<th>属性名称</th>';
            $input_head .= '<th>起算</th>';
            foreach ($info['input_attr'] as $k => $v) {
                if (isset($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 .= '<th>'.$v.'</th>';
            $single_head .= '<th>'.$v.'</th>';
            $input_head .= '<th>'.$v.'</th>';
        }
        
        $head .= '</tr></thead>';
        $single_head .= '</tr></thead>';
        $input_head .= '</tr></thead>';

        $body = '<tbody>';
        $single_body = '<tbody>';
        $input_body = '<tbody>';

        if (!$group_attr) {

            $prefix = '[单一规格]:';

            if (!$update && $col_num <= 1) {
                $head = '';
            }
            if ($update) {
                $input .= '<input type="hidden" name="key" value="-1"/>';
            }

            $body .= '<tr>';

            if ($parent_goods) {
                $body .= '<td width="10">'.$parent_goods['name'].'</td>';
            }

            foreach ($set['col'] as $k1 => $v1) {
                if (!$update && strstr($v1, '^')) {
                    continue;
                } 
                $value = $this->getValue($k1, -1, $sku_value, $sku);
                
                if ($update) {
                    if (strstr($v1, '|')) {
                        $v1 = str_replace('|', '', $v1);
                        $body .= '<td width="30">'.$value.'</td>';
                    } elseif ($k1 == 'unit') {
                        $select = '';
                        foreach ($this->unit as $k9 => $v9) {
                            if ($value == $v9) {
                                $selected = 'selected';
                            } else {
                                $selected = '';
                            }
                            $k9 = $v9;
                            if ($v9 == '请选择') {
                                $k9 = '';
                            }
                            $select .= '<option value="'.$k9.'" '.$selected.'>'.$v9.'</option>';
                        }
                        $body .= '<td width="30"><select lay-ignore  name="'.$k1.'" >'.$select.'</select></td>';
                    } else {
                        $body .= '<td width="30"><input type="text" class="layui-input" name="'.$k1.'"  value="'.$value.'"/></td>';
                    }
                    
                } elseif ($col_num > 1) {
                    $body .= '<td width="30">'.$value.'</td>';
                } else {
                    $body .= $value;
                }
            }

            $body .= '</tr>';

        } else {
            
            $prefix = '[组合规格]:';

            $group_attr = Dever::cartesian($group_attr);

            if ($sku_value) {
                foreach ($group_attr as $k => $v) {
                    $key = array();
                    foreach ($v['name'] as $k1 => $v1) {
                        $key[] = $v['info_id'][$k1] . '-' . $v['id'][$k1];
                    }
                    $key = implode('_', $key);
                    if (!isset($sku[$key])) {
                        unset($group_attr[$k]);
                    } elseif (isset($sku[$key]) && !isset($sku_value[$sku[$key]['id']])) {
                        unset($group_attr[$k]);
                    }
                }
            }

            $show = false;

            if ($group_attr) {
                foreach ($group_attr as $k => $v) {
                    $body .= '<tr data-row="' .($k+1). '">';

                    if ($parent_goods) {
                        $body .= '<td>'.$parent_goods['name'].'</td>';
                    }

                    $key = $id = array();
                    foreach ($v['name'] as $k1 => $v1) {
                        $rows = 1;
                        $body .= '<td width="50" rowspan="'.$rows.'">'.$v1.'</td>';
                        $id[] = array
                        (
                            'id' => $v['id'][$k1],
                            'attr_id' => $v['info_id'][$k1],
                        );
                        $key[] = $v['info_id'][$k1] . '-' . $v['id'][$k1];
                    }
                    $key = implode('_', $key);

                    foreach ($set['col'] as $k1 => $v1) {
                        if (!$update && strstr($v1, '^')) {
                            continue;
                        } 
                        $value = $this->getValue($k1, $key, $sku_value, $sku);
                        
                        if ($update) {
                            if (strstr($v1, '|')) {
                                $v1 = str_replace('|', '', $v1);
                                $body .= '<td width="30">'.$value.'</td>';
                            } elseif ($k1 == 'unit') {
                                $select = '';
                                foreach ($this->unit as $k9 => $v9) {
                                    if ($value == $v9) {
                                        $selected = 'selected';
                                    } else {
                                        $selected = '';
                                    }
                                    $k9 = $v9;
                                    if ($v9 == '请选择') {
                                        $k9 = '';
                                    }
                                    $select .= '<option value="'.$k9.'" '.$selected.'>'.$v9.'</option>';
                                }
                                $body .= '<td width="30"><select lay-ignore  name="'.$k1.'['.$k.']" >'.$select.'</select></td>';
                            } else {
                                $body .= '<td width="30"><input type="text" class="layui-input" name="'.$k1.'['.$k.']"  value="'.$value.'"/></td>';
                            }
                            
                        } else {
                            $body .= '<td width="30">'.$value.'</td>';
                        }
                    }

                    if ($update) {
                        $body .= '<input type="hidden" name="key['.$k.']" value="'.$key.'"/><textarea style="display:none;" name="attr['.$k.']">'.json_encode($id).'</textarea>';
                    }
                    
                    $body .= '</tr>';
                }
            } else {
                $prefix = $head = $body = '';
            }
        }

        $body .= $input;
                
        $html = $prefix . $head . $body;

        if ($update) {
            $html = '<table class="layui-table goods_table" style="width: 100%;">' . $html . '</table>';
        } else {
            $html = '<table class="layui-table goods_table" style="width: 100%;">' . $html . '</table>';
        }

        if ($single_attr && $sku_value) {
            foreach ($single_attr as $k => $v) {
                $key = $v['info_id'] . '-' . $v['id'];
                if (!isset($sku[$key])) {
                    unset($single_attr[$k]);
                } elseif (isset($sku[$key]) && !isset($sku_value[$sku[$key]['id']])) {
                    unset($single_attr[$k]);
                }
            }
        }

        if ($single_attr) {
            $html = $html;
            foreach ($single_attr as $k => $v) {

                $id = array();
                $single_body .= '<tr data-row="' .($k+1). '">';

                if ($parent_goods) {
                    $single_body .= '<td>'.$parent_goods['name'].'</td>';
                }

                $rows = 1;
                $single_body .= '<td width="100" rowspan="'.$rows.'">'. $v['parent_name'].'->'. $v['name'].'</td>';
                $id[] = array
                (
                    'id' => $v['id'],
                    'attr_id' => $v['info_id'],
                );
                $key = $v['info_id'] . '-' . $v['id'];

                foreach ($set['col'] as $k1 => $v1) {
                    if (!$update && strstr($v1, '^')) {
                        continue;
                    } 
                    $value = $this->getValue($k1, $key, $sku_value, $sku);
                    
                    if ($update) {
                        if (strstr($v1, '|')) {
                            $v1 = str_replace('|', '', $v1);
                            $single_body .= '<td width="30">'.$value.'</td>';
                        } elseif ($k1 == 'unit') {
                        $select = '';
                        foreach ($this->unit as $k9 => $v9) {
                            if ($value == $v9) {
                                $selected = 'selected';
                            } else {
                                $selected = '';
                            }
                            $k9 = $v9;
                            if ($v9 == '请选择') {
                                $k9 = '';
                            }
                            $select .= '<option value="'.$k9.'" '.$selected.'>'.$v9.'</option>';
                        }
                        $body .= '<td width="30"><select lay-ignore name="single_'.$k1.'['.$k.']" >'.$select.'</select></td>';
                    } else {
                            $single_body .= '<td width="30"><input type="text" class="layui-input" name="single_'.$k1.'['.$k.']"  value="'.$value.'"/></td>';
                        }
                        
                    } else {
                        $single_body .= '<td width="30">'.$value.'</td>';
                    }
                }

                if ($update) {
                    $single_body .= '<input type="hidden" name="single_key['.$k.']" value="'.$key.'"/><textarea style="display:none;" name="single_attr['.$k.']">'.json_encode($id).'</textarea>';
                }
                
                $single_body .= '</tr>';
            }

            $html = $html . '[单选规格]:<table class="layui-table">' . $single_head . $single_body . '</table>';
        }

        if ($input_attr && $sku_value) {

            foreach ($input_attr as $k => $v) {
                $key = array();
                foreach ($v['option'] as $k1 => $v1) {
                    $key[] = $v1['price_key'];
                }
                $key = implode('_', $key);
                if (!isset($sku[$key])) {
                    unset($input_attr[$k]);
                } elseif (isset($sku[$key]) && !isset($sku_value[$sku[$key]['id']])) {
                    unset($input_attr[$k]);
                }
            }
        }

        if ($input_attr) {
            foreach ($input_attr as $k => $v) {

                if ($v['sell_compute'] == 2) {
                    $v['sell_compute_type'] = '+';
                } elseif ($v['sell_compute'] == 3) {
                    $v['sell_compute_type'] = '-';
                } else {
                    $v['sell_compute_type'] = '*';
                }

                $id = array();
                $input_body .= '<tr data-row="' .($k+1). '">';

                if ($parent_goods) {
                    $input_body .= '<td>'.$parent_goods['name'].'</td>';
                }

                $rows = 1;
                $key = $id = $name = array();
                foreach ($v['option'] as $k1 => $v1) {
                    $id[] = array
                    (
                        'id' => $v1['id'],
                        'attr_id' => $v1['info_id'],
                    );
                    $key[] = $v1['price_key'];
                    $name[] = $v1['name'];
                }
                $key = implode('_', $key);
                $input_body .= '<td width="100" rowspan="'.$rows.'">'. $v['name'].'->'. implode($v['sell_compute_type'], $name).'</td>';
                $input_body .= '<td width="5"> >='.$v['sell_value'].'</td>';
                foreach ($set['col'] as $k1 => $v1) {
                    if (!$update && strstr($v1, '^')) {
                        continue;
                    } 
                    $value = $this->getValue($k1, $key, $sku_value, $sku);
                    if ($update) {
                        if (strstr($v1, '|')) {
                            $v1 = str_replace('|', '', $v1);
                            $input_body .= '<td width="30">'.$value.'</td>';
                        } elseif ($k1 == 'unit') {
                                $select = '';
                                foreach ($this->unit as $k9 => $v9) {
                                    if ($value == $v9) {
                                        $selected = 'selected';
                                    } else {
                                        $selected = '';
                                    }
                                    $k9 = $v9;
                                    if ($v9 == '请选择') {
                                        $k9 = '';
                                    }
                                    $select .= '<option value="'.$k9.'" '.$selected.'>'.$v9.'</option>';
                                }
                                $body .= '<td width="30"><select lay-ignore  name="input_'.$k1.'['.$k.']" >'.$select.'</select></td>';
                        } else {
                            $input_body .= '<td width="30"><input type="text" class="layui-input" name="input_'.$k1.'['.$k.']"  value="'.$value.'"/></td>';
                        }
                        
                    } else {
                        $input_body .= '<td width="30">'.$value.'</td>';
                    }
                }

                if ($update) {
                    $input_body .= '<input type="hidden" name="input_key['.$k.']" value="'.$key.'"/><textarea style="display:none;" name="input_attr['.$k.']">'.json_encode($id).'</textarea>';
                }
                
                $input_body .= '</tr>';
            }

            $html = $html . '[计算价]:<table class="layui-table">' . $input_head . $input_body . '</table>';
        }
        

        return $html;
    }

    private function create_table($head, $body)
    {
        $html = '<table class="layui-table">';
    }

    private function getValue($k1, $key, $sku_value, $sku)
    {
        $value = '';
        if ($k1 == 'unit' || $k1 == 'weight') {
            $value = '';
        } elseif ($k1 == 'min') {
            $value = 1;
        } else {
            $value = 0;
        }
        
        if ($key == -1) {
            /*
            if (isset($sku_value[$sku[$key]['id']]) && isset($sku_value[$sku[$key]['id']][$k1])) {
                $value = $sku_value[$sku[$key]['id']][$k1];
            } 
            */
            if (isset($sku_value[-1]) && isset($sku_value[-1][$k1])) {
                $value = $sku_value[-1][$k1];
            } elseif (isset($sku[-1]) && isset($sku[-1][$k1])) {
                $value = $sku[-1][$k1];
            }
        } else {
            if (isset($sku[$key])) {
                if (isset($sku_value[$sku[$key]['id']][$k1])) {
                    $value = $sku_value[$sku[$key]['id']][$k1];
                } elseif (isset($sku[$key][$k1])) {
                    $value = $sku[$key][$k1];
                }
            }
        }
        
        return $value;       
    }
}