| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1\store;
- use app\constants\OptionSetting;
- use app\models\Admin;
- use app\models\AttrLibrary;
- use app\models\Cat;
- use app\models\CloudGoodsBind;
- use app\models\ErpInventory;
- use app\models\ErpPurchasein;
- use app\models\ErpPurchaseinOrder;
- use app\models\Goods;
- use app\models\GoodsBook;
- use app\models\GoodsCat;
- use app\models\GoodsFullMinus;
- use app\models\GoodsPic;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\PostageRules;
- use app\utils\OrderNo;
- use yii\base\Model;
- use yii\helpers\Json;
- use app\models\MchCat;
- use app\modules\admin\models\MchForm;
- class StoreAdminGoodsForm extends Model
- {
- public $status = 0;//商品列表状态 /分类状态
- public $sort = 0;//时间排序
- public $model;
- public $id;//商品ID
- public $goods_name;//商品名称
- public $goods_num;//商品数量
- public $goods_price;//商品价格
- public $attr;//商品规格
- public $goods_info;//商品信息
- public $cat_name; //分类名称
- public $pic_url; //分类缩略图
- public $parent_id; //分类父级
- public $store_id;
- public $goods_no;
- public $purchase;
- public $mch_id;
- public function rules()
- {
- return [
- [['status', 'sort', 'goods_num', 'goods_price', 'parent_id', 'store_id'], 'integer'],
- [['goods_name', 'id', 'pic_url', 'cat_name', 'goods_no'], 'string'],
- [['mch_id'], 'safe'],
- [['attr', 'goods_info', 'purchase'], 'safe']
- ];
- }
- public function __construct($config = [])
- {
- parent::__construct($config);
- $this->model = Goods::find()->where(['store_id'=>get_store_id(), 'is_delete' => 0]);
- $whereMch = [];
- if($this->mch_id){
- $whereMch = ['mch_id' => $this->mch_id];
- $this->model->andWhere($whereMch);
- }
- }
- //商品列表
- public function goodsList(){
- try {
- $model = $this->model;
- $up_status = (clone $model)->andWhere(['status'=>1])->count();
- $down_status = (clone $model)->andWhere(['status'=>0])->count();
- //商品状态
- $model->andWhere(['status'=>$this->status]);
- //排序方式
- //创建时间
- if ($this->sort != 0) {
- $model->orderBy('created_at asc');
- } else {
- $model->orderBy('created_at desc');
- }
- $model->select('product_type, mch_id, id, name, price, goods_num, virtual_sales, cover_pic, created_at, status, is_delete, use_attr, attr, cost_price');
- //分页
- $pagination = pagination_make($model);
- $list = $pagination['list'];
- foreach ($list as &$item) {
- if(!empty($item['attr'])){
- $item['attr'] = json_decode($item['attr'], true);
- }
- $item['cost_price'] = $item['cost_price'] ?: $item['price'];
- }
- $is_add_goods = Option::get('is_add_goods', get_store_id(), 'store', 1)['value'];
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'list' => $list,
- 'up_status' => $up_status,
- 'down_status' => $down_status,
- 'totalCount' => $pagination['totalCount'],
- 'pageNo' => $pagination['pageNo'],
- 'is_add_goods' => (int)$is_add_goods
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //商品规格库存
- public function getGoodsAttr(){
- try {
- $id = $this->id;
- $model = $this->model;
- $goods = $model->andWhere(['id' => $id, 'use_attr' => 1])->select('id, attr')->one();
- if (empty($goods->attr) || empty($goods)) {
- throw new \Exception("获取商品规格库存失败");
- }
- $attr = !empty($goods->attr) ? json_decode($goods->attr, true) : [];
- $getAttrGroupList = $goods->getAttrGroupList();
- $getAttrGroupList = json_decode(json_encode($getAttrGroupList), true);
- $title = null;
- if (!empty($getAttrGroupList)) {
- $title = $getAttrGroupList[0]['attr_list'];
- }
- $arr = [];
- if (!empty($title) && !empty($attr)) {
- foreach ($title as $index =>$item) {
- foreach ($attr as $at) {
- if (!empty($at)) {
- foreach ($at['attr_list'] as $al) {
- if ($item['attr_id'] === $al['attr_id']) {
- $arr[$index]['list'][] = $at;
- }
- }
- }
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => [
- 'attr' => $attr,
- 'attr_group' => $arr,
- 'title' => $title
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //修改标题
- public function setGoodsName(){
- try {
- $model = $this->model;
- $goods = $model->andWhere(['id' => $this->id])->select('id, name')->one();
- if (empty($goods) || empty($goods->name)) {
- throw new \Exception("信息不存在");
- }
- $goods->name = $this->goods_name;
- if (!$goods->save()) {
- throw new \Exception("保存失败");
- }
- return [
- 'code' => 0,
- 'msg' => "修改成功"
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //删除
- public function goodsDel(){
- try {
- $id = $this->id;
- $id = explode(',',$id);
- $res = Goods::updateAll(['is_delete' => 1], ['id' => $id]);
- $cloud_store_token = get_merchant_token(0, get_store_id(), $message);
- if (!$cloud_store_token && $message) {
- debug_log('删除转单商品' . $message['msg']);
- }
- $mch_set_submit_order_url = "/cloud/mch/delTransGoods";
- $mch_set_submit_order_data = [];
- $mch_set_submit_order_data['access_token'] = $cloud_store_token;
- foreach ($id as $item) {
- $goods = Goods::findOne($item);
- if (!$goods->cloud_goods_id || intval($goods->is_wholesale)) {
- continue;
- }
- $mch_set_submit_order_data['goods_id'] = $goods->cloud_goods_id;
- $domain = (new OptionSetting)->getCloudDomainName();
- $mchSetSubmitOrderInfo = cloud_post( $domain . $mch_set_submit_order_url, $mch_set_submit_order_data);
- $mchSetSubmitOrderInfo = json_decode($mchSetSubmitOrderInfo,true);
- if($mchSetSubmitOrderInfo['code'] != 0){
- debug_log('删除转单商品' . $mchSetSubmitOrderInfo['msg']);
- } else {
- CloudGoodsBind::updateAll(['is_delete' => 1], ['store_id' => get_store_id(), 'cloud_goods_id' => $goods->cloud_goods_id, 'is_delete' => 0]);
- }
- }
- if ($res) {
- return [
- 'code' => 0,
- 'msg' => "删除成功"
- ];
- }
- throw new \Exception("删除失败");
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //上下架
- public function setGoodsStatus(){
- try {
- $id = $this->id;
- $id = explode(',',$id);
- if (empty($id) ) {
- throw new \Exception("没有传入有效数据");
- }
- $model = $this->model;
- if(!empty($this->status)){
- //判断库存
- $goods = $model->andWhere(['id' => $id])->select('mch_id, id, status, goods_num, name')->all();
- if (empty($goods)) {
- throw new \Exception("获取产品信息失败");
- }
- foreach ($goods as $item) {
- if (empty($item->goods_num) && $item->goods_num <= 0) {
- throw new \Exception('商品' . $item->name . "库存不足");
- }
- if($item->mch_id){
- $mchSetting = (new MchForm(['store_id' => $item->store_id]))->mchSetting();
- if($mchSetting['mch_goods_audit']){
- $item->mch_audit = 1;
- $item->mch_audit_time = time();
- $item->status = Goods::STATUS_DISABLE;
- $item->save();
- return [
- 'code' => 0,
- 'msg' => '已提交审核,请等待管理员审核'
- ];
- }
- }
- }
- }
- $status = empty($this->status) ? 0 : 1;
- //修改上下架状态
- $res = Goods::updateAll(['status' => $status], ['id' => $id]);
- if ($res) {
- return [
- 'code' => 0,
- 'msg' => "操作成功"
- ];
- }
- throw new \Exception('信息修改失败');
- } catch (\Exception $e){
- return [
- 'code' => 0,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //修改价格/数量 (规格库存价格)//未测试带规格的
- public function setPrice(){
- try {
- $model = $this->model;
- $goods = $model->andWhere(['id' => $this->id])->select('id, status, goods_num ,use_attr, attr, price')->one();
- if (empty($goods)) {
- throw new \Exception("获取产品信息失败");
- }
- //判断是否存在以及开启规格
- if ($goods->use_attr == 1 && !empty($this->attr)) {
- $attr = $this->attr;
- $arr = [];
- foreach ($attr as $item) {
- if ($item['num'] >=0 && $item['price'] >=0) {
- $arr[] = $item;
- } else {
- throw new \Exception("数据错误");
- }
- }
- $goods->attr = json_encode($arr, JSON_UNESCAPED_UNICODE);
- } elseif ($goods->use_attr == 0) {
- $this->goods_num > 0 && $goods->goods_num = $this->goods_num;
- $this->goods_price > 0 && $goods->price = $this->goods_price;
- //如果库存不足,则下架处理
- if ($this->goods_num == 0) {
- $goods->status = 0;
- }
- // 更新attr字段
- try {
- $attr = \json_decode($goods->attr, true);
- $attr[0]['num'] = $this->goods_num > 0 ? $this->goods_num : 0;
- $goods->attr = \json_encode($attr, JSON_UNESCAPED_UNICODE);
- } catch(\Exception $e) {}
- }
- if (!$goods->save()) {
- throw new \Exception('数据保存错误' . json_encode($goods->errors));
- }
- return [
- 'code' => 0,
- 'msg' => '修改成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //修改添加商品
- public function getGoodsEdit(){
- try {
- $id = $this->id;
- $model = $this->model;
- if ($id) {
- $goods = $model->where(['id'=>$id])->select('id, cat_id, product_type, name, attr, use_attr, freight, price, goods_num, detail, delivery_type, pieces, forehead, cover_pic, cost_price, is_front_delivery, is_front_centralize, agent_goods_status')->one();
- if (empty($goods)) {
- return [
- 'code' => 1,
- 'msg' => '商品获取失败'
- ];
- }
- $getAttrGroupList = $goods->getAttrGroupList();
- $goods = $goods->toArray();
- $cat_id = GoodsCat::find()
- ->where(['goods_id' => $goods['id'], 'is_delete' => 0])
- ->select(['cat_id'])->asArray()->all();
- $cat_id = array_column($cat_id, 'cat_id');
- $goods['cat_id'] = $cat_id;
- $goods['integral'] = json_decode($goods['integral'],true);
- $goods['goods_pic_list'] = GoodsPic::find()->select(['pic_url'])
- ->where(['goods_id' => $id, 'is_delete' => 0])->asArray()->all();
- foreach ($goods as &$val) {
- $val = is_int($val) ? (string)$val : $val;
- }
- $goods['attr'] = !empty($goods['attr']) ? json_decode($goods['attr'], true) : [];
- $goods['is_front_delivery'] = intval($goods['is_front_delivery']);
- $goods['is_front_centralize'] = intval($goods['is_front_centralize']);
- $goods['agent_goods_status'] = intval($goods['agent_goods_status']);
- $goods['use_attr'] = intval($goods['use_attr']);
- //unset($goods['attr']);
- } else {
- $goods = null;
- }
- //商品规格
- $AttrLibrary = AttrLibrary::find()->where(['is_delete' => 0, 'store_id' => get_store_id()])->select('id, name, parent_id')->asArray()->all();
- $new_AttrLibrary = $this->getmenu($AttrLibrary);
- $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'store')['value'];
- $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'pay', $send_type);
- $send_type = $send_type ? Json::decode($send_type['value']) : [];
- $send_type_arr = [];
- foreach ((array)$send_type as $key => $send) {
- if ($send['value'] == 1) {
- $send_type_arr[$key] = $send['text'];
- }
- }
- //物流方式
- $arr = empty($send_type_arr) ? ['express' => '快递', 'shop' => '自提'] : $send_type_arr;
- $goods_delivery_type = array_keys($arr);
- foreach ($goods_delivery_type as &$item) {
- switch ($item) {
- case "express":
- $value = '快递';
- break;
- case "shop":
- $value = '到店自提';
- break;
- case "delivery":
- $value = '同城配送';
- break;
- default:
- $value = '';
- break;
- }
- $item = [
- 'key' => $item,
- 'value' => $value
- ];
- }
- $goods['delivery_type'] = !empty($goods['delivery_type']) ? json_decode($goods['delivery_type'], true) : [];
- // unset($goods['goods_delivery_type']);
- // 获取运费规则
- $postage = PostageRules::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- 'mch_id' => 0,
- 'is_enable' => 1
- ])->select(['id', 'name', 'is_enable'])->asArray()->all();
- //分类列表
- $cat = Cat::find()->where(['is_delete' => 0, 'is_show' => 1, 'store_id' => get_store_id()])->select('id, name, parent_id')->asArray()->all();
- $cat_arr = $this->getmenu($cat);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'info' => $goods,
- 'postage' => $postage,
- 'cat' => $cat_arr,
- 'goods_delivery_type' => $goods_delivery_type,
- 'new_AttrLibrary' => $new_AttrLibrary,
- 'AttrGroupList' => $getAttrGroupList ?? [],
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //获取运费规则
- public function getPostageData()
- {
- try {
- // 获取运费规则
- $postage = PostageRules::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- 'mch_id' => 0,
- 'is_enable' => 1
- ])->select(['id', 'name', 'is_enable'])->asArray()->all();
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => [
- 'postage' => $postage
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //商品信息保存
- public function saveGoods(){
- try {
- $model = $this->model;
- $goods_info = $this->goods_info;
- if (empty($goods_info)) {
- throw new \Exception("数据错误");
- }
- $id = $goods_info['id'];
- if (!empty($id)) {
- $model = $model->where(['id' => $id])->select('id, cat_id, product_type, attr, use_attr, price, goods_num, freight, detail, delivery_type, pieces, forehead, cover_pic, original_price, goods_no, cost_price')->one();
- if (empty($model)) {
- throw new \Exception("获取产品信息失败");
- }
- } else {
- $model = new Goods();
- $model->store_id = get_store_id();
- }
- // if (($goods_info['product_type'] != 2 || $goods_info['product_type'] != 3) && $goods_info['use_attr'] == 1) {
- //
- // foreach ($goods_info['attr'] as $item) {
- // if ((empty($item['num']) || $item['num'] <= 0) || (empty($item['price']) || $item['price'] <= 0)) {
- // throw new \Exception("规格信息错误");
- // }
- // }
- //
- // } elseif (empty($id) || $goods_info['product_type'] == 2 || $goods_info['product_type'] == 3) {
- //
- // $goods_info['attr'] = [
- // [
- // "attr_list" => [
- // [
- // "attr_id"=>16,
- // "attr_name"=> "默认"
- // ]
- // ],
- // "num" => 0 ,
- // "price" => 0,
- // "no" => "",
- // "pic" => "",
- // "share_commission_first" => "",
- // "share_commission_second" => "",
- // "share_commission_third" => ""
- // ]
- // ];
- //
- // }
- $model->use_attr = $goods_info['use_attr'];
- $model->product_type = $goods_info['product_type'];
- // $model->cat_id = $goods_info['cat_id'];
- // $model->attr = json_encode($goods_info['attr']);
- $model->price = $goods_info['price'];
- $model->goods_num = $goods_info['goods_num'];
- $model->name = $goods_info['name'];
- // if ($goods_info['use_attr'] === 0) {
- //
- // }
- $model->original_price = $goods_info['original_price'];
- $model->goods_no = $goods_info['goods_no'];
- $model->detail = $goods_info['detail'];
- $model->delivery_type = json_encode($goods_info['delivery_type']);
- $model->pieces = $goods_info['pieces'];
- $model->forehead = $goods_info['forehead'];
- $model->cover_pic = $goods_info['cover_pic'];
- $model->freight = $goods_info['freight'];
- $model->cost_price = $goods_info['cost_price'] ?: 0;
- $goods_pic_list = [];
- if ($goods_info['goods_pic_list']) {
- $goods_pic_list = $goods_info['goods_pic_list'];
- }
- // if ($goods_info['cover_pic']) {
- // $goods_pic_list = array_merge($goods_pic_list, [['pic_url' => $goods_info['cover_pic']]]);
- // }
- $goods_pic_list = array_values($goods_pic_list);
- if($this->mch_id){
- $mchSetting = (new MchForm(['store_id' => $model->store_id]))->mchSetting();
- if($mchSetting['mch_goods_audit'] && !$id){
- $model->mch_audit = 1;
- $model->mch_audit_time = time();
- $model->status = Goods::STATUS_DISABLE;
- }
- $model->mch_id = $this->mch_id;
- }
- if ($model->save()) {
- if($this->mch_id && $goods_info['mch_cat_id']){
- $saveCat = \app\models\MchGoodsCat::saveCat($model->id, $goods_info['mch_cat_id']);
- if ($saveCat['code']) {
- $t->rollBack();
- return $saveCat;
- }
- }
- //判断如果规格发生改变就删除满减规则
- if (!empty($goods_info['is_change_attr'])) {
- $res = GoodsFullMinus::updateAll(['is_delete' => 1], ['goods_id' => $model->id]);
- \Yii::error($res, "规格发生改变就删除满减规则");
- }
- $goods_info['id'] = $model->id;
- $form = new StoreAdminAttrForm();
- $res = $form->goodsAttrBook($goods_info);
-
- if ($res['code'] != 0) {
- throw new \Exception($res['msg']);
- }
- //图片
- if (!empty($goods_pic_list)) {
- if ($id) {
- GoodsPic::updateAll(['is_delete' => 1], ['goods_id' => $id, 'is_delete' => 0]);
- }
- foreach ($goods_pic_list as $item) {
- $goods_pic = new GoodsPic();
- $goods_pic->goods_id = $model->id;
- $goods_pic->pic_url = $item['pic_url'];
- if (!$goods_pic->save()) {
- throw new \Exception(json_encode($goods_pic->errors));
- };
- }
- }
- //分类
- if (!empty($goods_info['cat_id'])) {
- $cat_id = is_string($goods_info['cat_id']) ? json_decode($goods_info['cat_id']) : $goods_info['cat_id'];
- if ($id) {
- GoodsCat::updateAll(['is_delete' => 1], ['goods_id' => $id, 'is_delete' => 0]);
- }
- foreach ((array)$cat_id as $item) {
- $goods_cat = new GoodsCat();
- $goods_cat->goods_id = $model->id;
- $goods_cat->store_id = get_store_id();
- $goods_cat->cat_id = $item;
- if (!$goods_cat->save()) {
- throw new \Exception(json_encode($goods_cat->errors));
- };
- }
- }
- return [
- 'code' => 0,
- 'msg' => "保存成功"
- ];
- }
- throw new \Exception(json_encode($model->errors));
- } catch (\Exception $e) {
- debug_log($e->getMessage().$e->getLine());
- return [
- 'code' => 1,
- 'msg' => $e->getMessage().$e->getLine()
- ];
- }
- }
- //展示商品详情
- public function goodsDetail(){
- try {
- $id = $this->id;
- $model = $this->model;
- if (empty($id)) {
- throw new \Exception("获取参数失败");
- }
- $goods = $model->where(['id' => $id])->select('id, detail, name, price, goods_num, virtual_sales, cat_id, attr, use_attr')->with('goodsPicList')->asArray()->one();
- if (empty($goods)) {
- throw new \Exception("获取产品信息失败");
- }
- //分类列表
- $cat = Cat::find()->where(['is_delete' => 0, 'is_show' => 1, 'store_id' => get_store_id()])->select('id, name, parent_id')->asArray()->all();
- $cat_arr = $this->getmenu($cat);
- //实际销量
- $actual_num = Order::find()->where(['and',
- ['o.is_delete' => 0, 'o.store_id' => get_store_id(), 'od.goods_id' => $id]])
- ->andWhere(['or', ['<>', 'o.trade_status', '-1'],['<>', 'o.trade_status', '1']])
- ->alias('o')->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id = o.id')
- ->count();
- $goods['virtual_sales'] += $actual_num;
- //规格
- $goods['attr'] = !empty($goods['attr']) ? json_decode($goods['attr'], true) : '';
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => [
- 'goods_info' => $goods,
- 'cat' => $cat_arr
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //分类列表
- public function catList(){
- $data = Cat::find()->where(['store_id' => get_store_id(), 'is_delete' => 0])->select("id, parent_id, name, pic_url, is_show")->asArray()->all();
- if($this->mch_id){
- $data = \app\models\MchCat::find()->where(['mch_id' => $this->mch_id, 'is_delete' => 0])->select("id, parent_id, name, pic_url, is_show")->asArray()->all();
- }
- $list = $this->getmenu($data, 0, 1);
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $list
- ];
- }
- //分类保存
- public function catAdd(){
- try {
- $id = $this->id;
- $cat_name = $this->cat_name;
- $pic_url = $this->pic_url;
- $parent_id = $this->parent_id;
- $status = $this->status;
- $model = Cat::findOne($id)? : new Cat();
- if($this->mch_id){
- $model = MchCat::findOne($id)? : new MchCat();
- $model->mch_id = $this->mch_id;
- }
- $model->parent_id = $parent_id;
- $model->name = $cat_name;
- $model->store_id = get_store_id();
- $model->pic_url = $pic_url;
- $model->is_show = $status;
- $model->created_at = time();
- $model->updated_at = time();
- if (!$model->save()) {
- throw new \Exception(json_encode($model->errors));
- }
- return [
- 'code' => 0,
- 'msg' => "保存成功"
- ];
- } catch (\Exception $e){
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //分类状态更改
- public function setCatStatus(){
- try {
- $id = $this->id;
- $status = $this->status;
- if (empty($id)) {
- throw new \Exception("找不到对应的数据信息");
- }
- $table = new Cat;
- if($this->mch_id){
- $table = new MchCat;
- }
- $model = $table::findOne($id);
- if (empty($model)) {
- throw new \Exception("找不到对应的数据信息");
- }
- switch ($status) {
- case 0:
- $res = $table::updateAll(['is_show' => 0], ['OR', ['id' => $id], ['parent_id' => $id]]);
- break;
- case 1:
- $res = $table::updateAll(['is_show' => 1], ['OR', ['id' => $id], ['parent_id' => $id]]);
- break;
- case 2:
- $res = $table::updateAll(['is_delete' => 1], ['OR', ['id' => $id], ['parent_id' => $id]]);
- break;
- }
- if (!$res) {
- throw new \Exception("失败");
- }
- return [
- 'code' => 0,
- 'msg' => "成功"
- ];
- } catch (\Exception $e){
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //erp扫码入库
- public function erpScanGetGoodsList() {
- $store_id = $this->store_id;
- $goods_no = $this->goods_no;
- try {
- $erp_open = intval(Option::get(OptionSetting::ERP_OPEN, $store_id, 'store', 0)['value']);
- if (!$erp_open) {
- throw new \Exception('未开启本地进销存');
- }
- if (empty($goods_no)) {
- return [
- 'code' => 0,
- 'msg' => '货号不能为空',
- 'data' => [
- 'list' => []
- ]
- ];
- }
- $goods_id = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0])
- ->andWhere(['LIKE', 'goods_no', $goods_no])->select('id')->column();
- $query = ErpInventory::find()->alias('ei')->where(['ei.is_delete' => 0, 'ei.store_id' => $store_id]);
- $query->leftJoin(['g' => Goods::tableName()], 'g.id = ei.goods_id');
- if ($goods_id) {
- $query->andWhere(['OR', ['ei.goods_no' => $goods_no], ['LIKE', 'ei.sku_id', $goods_no], ['ei.goods_id' => $goods_id]]);
- } else {
- $query->andWhere(['OR', ['ei.goods_no' => $goods_no], ['LIKE', 'ei.sku_id', $goods_no]]);
- }
- $query->orderBy('ei.goods_id DESC');
- $data = $query->select('ei.*, g.name goods_name, g.cover_pic')->asArray()->all();
- foreach ($data as &$item) {
- // $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $attr_info = json_decode($item['attr_info'], true);
- $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name'));
- $item['price'] = $attr_info['price'];
- if($attr_info['pic']){
- $item['cover_pic'] = $attr_info['pic'];
- }
- $item['num'] = 0;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $data
- ],
- // 'q' => $query->createCommand()->getRawSql(),
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //入库
- public function purchaseInSave ()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $store_id = $this->store_id;
- $erp_open = intval(Option::get(OptionSetting::ERP_OPEN, $store_id, 'store', 0)['value']);
- if (!$erp_open) {
- throw new \Exception('未开启本地进销存');
- }
- $admin = Admin::findOne(['type' => 'store', 'type_id' => $store_id, 'is_delete' => 0]);
- $model = new ErpPurchaseinOrder();
- $model->operator = $admin->name;
- $model->operator_id = $admin->id;
- $model->store_id = $this->store_id;
- $model->order_no = OrderNo::getOrderNo(OrderNo::ERP_PURCHASE_IN);
- $model->supplier_id = 0;
- $model->num = count($this->purchase);
- if (!$model->save()) {
- \Yii::error([__METHOD__, $model->attributes]);
- throw new \Exception(json_encode($model->errors, JSON_UNESCAPED_UNICODE));
- }
- ErpPurchasein::saveList($model, $this->purchase);
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //递归
- public function getmenu($menu, $id = 0, $show_parent = 0, $name = ""){
- $arr = [];
- foreach ($menu as $k => $v) {
- if($v['parent_id'] == $id){
- if ($show_parent == 1) {
- $v['parent_name'] = $name;
- $v['children'] = $this -> getmenu($menu, $v['id'], $show_parent, $v['name']);
- }else {
- $v['children'] = $this -> getmenu($menu, $v['id']);
- }
- $arr[] = $v;
- }
- }
- return $arr;
- }
- }
|