| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 | <?phpnamespace Scm_product\Lib;use Dever;class Info{    # 获取商品列表    public function getList()    {    	    }    # 获取商品详情    public function getInfo($id, $type = 1)    {        # type == 1 是销售商品 2是采购商品        $info = is_array($id) ? $id : Dever::db('scm_product/info')->getOne($id);        if (!$info) {            Dever::alert('商品信息不存在');        }        $info = $this->get($info, $type);        return $info;    }    private function get($info, $type = 1)    {        # 基本信息        $info['udate'] = date('Y-m-d H:i', $info['udate']);        $info['cdate'] = date('Y-m-d H:i', $info['cdate']);        # 获取分类        $info['category_info'] = Dever::load('scm_product/lib/category')->getInfo($info['category']);        # 获取属性        $attr = Dever::db('scm_product/info_attr')->select(array('info_id' => $info['id']));        if ($attr) {            foreach ($attr as $k => $v) {                $attr_info = Dever::load('scm_product/lib/attr')->getInfo($v['attr_id'], $v['attr_value']);                if ($attr_info) {                    $info['attr'][] = $attr_info;                }            }        }        # 获取规格        $info['spec'] = Dever::load('scm_product/lib/spec')->getData($info['id'], 'getData');        # 获取价格        $info['sku'] = Dever::load('scm_product/lib/sku')->getData($info['id'], false, $type);        # 获取组合商品        if ($info['type'] > 10) {            $info['goods'] = Dever::json_decode($info['goods']);        } else {            unset($info['goods']);        }        # 获取单位信息        if ($info['unit_id'] > 0) {            $info['unit'] = Dever::db('scm/unit')->find($info['unit_id']);        }        return $info;    }    # 获取商品及其属性列表    public function getSetList($table, $other_where, $cate_id = false, $col = 'goods_id', $set = array(), $price_id = -1, $source = '')    {        $price = Dever::input('price', 0);        $buy_price = Dever::input('buy_price', 0);        $total = Dever::input('total', 1);        if ($cate_id) {            $where['top_category_id'] = $cate_id;        }        $where['status'] = 1;        if (!$source) {            $source = 'scm_product/info';        }        $data = Dever::db($source)->getData($where);        $result = array();        if ($data) {            $i = 0;            foreach ($data as $k => $v) {                                $sku = Dever::db('scm_product/info_sku')->select(array('info_id' => $v['id']));                if ($sku) {                    $other = array();                    if ($table) {                        $other_where[$col] = $v['id'];                        $other_where['sku_id'] = $sku[0]['id'];                        $other = Dever::db($table)->find($other_where);                    }                                        if ($set) {                        $result[$i]['set'] = $set;                    }                                        $result[$i]['id'] = $v['id'];                    $result[$i]['name'] = $v['name'];                    $result[$i]['type'] = $v['type'];                    $result[$i]['stock'] = $v['stock'];                    $result[$i]['commission'] = $v['commission'];                    $result[$i]['sell_type'] = $v['sell_type'];                    $result[$i]['buy_type'] = $v['buy_type'];                    $result[$i]['old_price'] = $result[$i]['price'] = $price ? $price : $sku[0]['price'];                    $result[$i]['old_buy_price'] = $result[$i]['buy_price'] = $buy_price ? $buy_price : $sku[0]['buy_price'];                    $result[$i]['old_cost_price'] = $result[$i]['cost_price'] = $sku[0]['cost_price'];                    $result[$i]['select'] = 2;                    $result[$i]['del'] = 1;                    $result[$i]['total'] = isset($v['total']) ? $v['total'] : 0;                    if ($price_id > 0 && $sku[0]['key'] == -1) {                        list($result[$i]['price'], $result[$i]['buy_price']) = Dever::load('scm_product/lib/price')->get($price_id, $v['id'], $sku[0]['id'], $result[$i]['price'], $result[$i]['buy_price']);                    }                                        if ($other) {                        if (isset($other['commission']) && $other['commission']) {                            $result[$i]['commission'] = $other['commission'];                        }                        if (isset($other['price']) && $other['price']) {                            $result[$i]['price'] = $other['price'];                        }                        if (isset($other['buy_price']) && $other['buy_price']) {                            $result[$i]['buy_price'] = $other['buy_price'];                        }                        if (isset($other['cost_price']) && $other['cost_price']) {                            $result[$i]['cost_price'] = $other['cost_price'];                        }                        if (isset($other['sell_min']) && $other['sell_min']) {                            $result[$i]['sell_min'] = $other['sell_min'];                        }                        if (isset($other['buy_min']) && $other['buy_min']) {                            $result[$i]['buy_min'] = $other['buy_min'];                        }                        if (isset($other['source_id']) && $other['source_id']) {                            $result[$i]['source_id'] = $other['source_id'];                        }                        if (isset($other['in_num'])) {                            if (isset($other['out_num'])) {                                $result[$i]['total'] = $other['in_num'] - $other['out_num'];                            } else {                                $result[$i]['total'] = $other['in_num'];                            }                                                    } elseif (isset($other['num'])) {                            $result[$i]['total'] = $other['num'];                        }                                                $result[$i]['select'] = $other['state'];                    }                    $result[$i]['total'] = $result[$i]['total'] * $total;                    $result[$i]['children'] = array();                    $num = count($sku);                    if ($sku[0]['key'] == -1) {                        $result[$i]['id'] .= '-' . $sku[0]['id'];                        $result[$i]['old_price'] = $sku[0]['price'];                        $result[$i]['old_buy_price'] = $sku[0]['buy_price'];                        if (!$price && $result[$i]['price'] <= 0) {                            $result[$i]['price'] = $sku[0]['price'];                        }                        if (!$buy_price && $result[$i]['buy_price'] <= 0) {                            $result[$i]['buy_price'] = $sku[0]['buy_price'];                        }                                                //$result[$i]['id'] .=  '-' . $sku[0]['id'];                        $result[$i]['end'] = true;                    } else {                        foreach ($sku as $k1 => $v1) {                            $v1['sku_name'] = '';                            if ($v1['key']) {                                $key = str_replace('-', ',', $v1['key']);                                $spec = Dever::db('scm_product/info_spec_value')->getGroupData(array('ids' => $key));                                $v1['sku_name'] = $spec['name'];                            }                            $name = $v['name'];                            if ($v1['sku_name']) {                                $name .= '-' . $v1['sku_name'];                            }                            $children = array                            (                                'id' => $v['id'] . '-' . $v1['id'],                                'name' => $name,                                'stock' => $v['stock'],                                'commission' => $v['commission'],                                'sell_type' => $v['sell_type'],                                'buy_type' => $v['buy_type'],                                'old_price' => $v1['price'],                                'old_buy_price' => $v1['buy_price'],                                'old_cost_price' => $v1['cost_price'],                                'price' => $price ? $price : $v1['price'],                                'buy_price' => $buy_price ? $buy_price : $v1['buy_price'],                                'cost_price' => $v1['cost_price'],                                'type' => $v['type'],                                'del' => 1,                                'end' => true,                                'total' => 0,                            );                            $other_sku = array();                            if ($table) {                                $other_where['sku_id'] = $v1['id'];                                $other_sku = Dever::db($table)->find($other_where);                            }                            $children['select'] = 2;                            $children['total'] = 0;                            if ($set) {                                $children['set'] = $set;                            }                            if ($price_id > 0) {                                list($children['price'], $children['buy_price']) = Dever::load('scm_product/lib/price')->get($price_id, $v['id'], $v1['id'], $children['price'], $children['buy_price']);                            }                            if ($other_sku) {                                if (isset($other_sku['commission']) && $other_sku['commission']) {                                    $children['commission'] = $other_sku['commission'];                                }                                if (isset($other_sku['price']) && $other_sku['price']) {                                    $children['price'] = $other_sku['price'];                                }                                if (isset($other_sku['buy_price']) && $other_sku['buy_price']) {                                    $children['buy_price'] = $other_sku['buy_price'];                                }                                if (isset($other_sku['cost_price']) && $other_sku['cost_price']) {                                    $children['cost_price'] = $other_sku['cost_price'];                                }                                if (isset($other_sku['sell_min']) && $other_sku['sell_min']) {                                    $children['sell_min'] = $other_sku['sell_min'];                                }                                if (isset($other_sku['buy_min']) && $other_sku['buy_min']) {                                    $children['buy_min'] = $other_sku['buy_min'];                                }                                if (isset($other_sku['source_id']) && $other_sku['source_id']) {                                    $children['source_id'] = $other_sku['source_id'];                                }                                if (isset($other_sku['in_num'])) {                                    if (isset($other_sku['out_num'])) {                                        $children['total'] = $other_sku['in_num'] - $other_sku['out_num'];                                    } else {                                        $children['total'] = $other_sku['in_num'];                                    }                                } elseif (isset($other_sku['num'])) {                                    $children['total'] = $other_sku['num'];                                }                                                                $children['select'] = $other_sku['state'];                            }                            $children['total'] = $children['total']*$total;                            $result[$i]['children'][] = $children;                        }                    }                    $i++;                }            }        }        return $result;    }    # 获取支付所需要的信息    public function getPayInfo($id, $sku = 0, $seller_id = false)    {        $info = is_array($id) ? $id : Dever::db('scm_product/info')->one($id);        if ($info) {            # 最小购买数1            $info['min'] = 1;            $sku = $sku > 0 ? $sku : -1;            $info['sku_id'] = $sku;            if ($sku > 0) {                $where['info_id'] = $info['id'];                $where['id'] = $sku;                $sku = Dever::db('scm_product/info_sku')->find($where);                if ($sku) {                    if ($seller_id) {                        $seller = Dever::db('scm_role/seller')->find($seller_id);                        if ($seller) {                            list($sku['price'], $sku['buy_price']) = Dever::load('scm_product/lib/price')->get($seller['price_id'], $info['id'], $sku['id'], $sku['price'], $sku['buy_price']);                        }                    }                    $info['price'] = $sku['price'];                    $info['buy_price'] = $sku['buy_price'];                }            }            /*            $info['freight_id'] = 0;            $info['freight_price'] = 0;            $freight = $this->freight($info, $info['price'], $user, $num);            if ($freight) {                $info['freight_id'] = $freight['id'];                $info['freight_price'] = $freight['price'];            }            */            # 佣金计算            //$info['reward'] = $this->reward($info, $info['price']*$num);        }        return $info;    }    # 获取商品的基本信息,只包括名称-价格等信息    public function getBaseInfo($info, $sku = false, $unit_id = false)    {        $info = is_array($info) ? $info : Dever::db('scm_product/info')->one($info);        $where = array('info_id' => $info['id']);        if ($sku && $sku > 0) {            $where['id'] = $sku;        }        $info['base_unit_id'] = $info['unit_id'];        if ($unit_id) {            $info['unit_id'] = $unit_id;        }        $info['unit'] = '';        if ($info['unit_id']) {            $unit = Dever::db('scm/unit')->find($info['unit_id']);            if ($unit) {                $info['unit'] = $unit['name'];            }        }        if (!$info['unit']) {            $info['unit'] = '个';        }        $info['aname'] = $info['name'];        $info['spec_name'] = array();        $sku = Dever::db('scm_product/info_sku')->select($where);        if ($sku) {            foreach ($sku as $k => $v) {                $key = str_replace('-', ',', $v['key']);                $spec = Dever::db('scm_product/info_spec_value')->getGroupData(array('ids' => $key));                if ($spec) {                    $info['aname'] .= '-' . $spec['name'];                    $info['spec_name'][] = $spec['name'];                }                if ($sku && $sku > 0) {                    $info['code'] = $v['code'];                    $info['price'] = $v['price'];                    $info['buy_price'] = $v['buy_price'];                    $info['cost_price'] = $v['cost_price'];                }            }        }        if (!$info['spec_name']) {            $info['spec_name'] = '无';        } else {            $info['spec_name'] = implode(',', $info['spec_name']);        }        $pic = explode(',', $info['pic']);        $info['cover'] = $pic[0];        $this->getGroup($info);        return $info;    }    # 获取组合商品    public function getGroup(&$info)    {        $info['goods'] = array();        if ($info['type'] == 11 || $info['type'] == 12) {            $goods = Dever::db('scm_product/info_goods')->select(array('info_id' => $info['id']));            $info['goods'] = array();            foreach ($goods as $k => $v) {                $goods_info = $this->getBaseInfo($v['goods_id'], $v['sku_id']);                if ($goods_info) {                    $info['goods'][] = array                    (                        'id' => $goods_info['id'],                        'code' => $goods_info['code'],                        'name' => $goods_info['aname'],                        'price' => $goods_info['price'],                        'buy_price' => $goods_info['buy_price'],                        'cover' => $goods_info['cover'],                        'unit_id' => $goods_info['unit_id'],                        'num' => $v['num'],                    );                }            }        }    }}
 |