model = Goods::find()->where(['store_id'=>$this->store_id, 'is_delete' => 0]); } //商品列表 public function goodsList(){ try { $store_id = $this->store_id; $this->initGoods(); $model = $this->model; $up_status = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0])->andWhere(['status'=>1])->count(); $down_status = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0])->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('id, name, price, goods_num, virtual_sales, cover_pic, created_at, status, is_delete, use_attr, attr, cloud_goods_id, cloud_supplier_id, cost_price'); //分页 $pagination = pagination_make($model); $list = $pagination['list']; foreach ($list as $i => &$item) { if(!empty($item['attr'])){ $item['attr'] = is_array($item['attr'])? $item['attr'] : json_decode($item['attr'], true); } $item['cost_price'] = $item['cost_price'] ?: $item['price']; // $form = new PlatformForm(); // $form->id = $item['cloud_goods_id']; // $glist = $form->goodsInfo(); // $supplier_name = ''; // if($glist['code'] == 0 && $glist['data']['count'] == 1){ // $supplier_name = $glist['data']['list'][0]['supplier']['name']; // } // if (!$supplier_name) { // continue; // } $supplier = Supplier::findOne(['cloud_supplier_id' => $item['cloud_supplier_id'], 'is_delete' => 0]); $supplier_name = ''; if ($supplier) { $supplier_name = $supplier->supplier_name; } $item['cloud_goods_supplier_name'] = $supplier_name; } $is_add_goods = Option::get('is_add_goods', $store_id, 'store', 1)['value']; return [ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'sql' => $model->createCommand()->getRawSql(), '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; $this->initGoods(); $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 { $this->initGoods(); $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, $this->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); $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' => $this->store_id, 'cloud_goods_id' => $goods->cloud_goods_id, 'is_delete' => 0]); } } if ($res) { if (count($id) === 1) { $goods = Goods::findOne($id[0]); (new DiyCommon)->JobBehaviors($goods->store_id, StoreSyncExtLog::TYPE_PRODUCT, $id); } 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("没有传入有效数据"); } $this->initGoods(); $model = $this->model; if(!empty($this->status)){ //判断库存 $goods = $model->andWhere(['id' => $id])->select('id, status, goods_num, name, cloud_goods_id, is_wholesale')->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->cloud_goods_id > 0) { $goods_url = "/goods/getAttrParams"; $param = [ 'id' => $item->cloud_goods_id ]; //请求接口 $domain = (new OptionSetting)->getCloudDomainName(); $goodsInfo = cloud_post($domain.$goods_url,$param); $goodsInfo = json_decode($goodsInfo, true); $goodsInfo = $goodsInfo['data']; //如果没有上架或审核未通过则不允许购买 if ((int)$goodsInfo['status'] === 0 || (int)$goodsInfo['is_audit'] !== 2) { throw new \Exception("供货商商品状态异常"); } //判断是否可以上架 供货商是否更新产品 $CloudGoodsUpdateLog = CloudGoodsUpdateLog::findOne(['cloud_goods_id' => $item->cloud_goods_id]); //存在记录并且不为批发订单 if ($CloudGoodsUpdateLog && !$item->is_wholesale) { if (!$CloudGoodsUpdateLog->is_audit) { return [ 'code' => 1, 'msg' => '供货商商品暂未审核通过' ]; } if (!$CloudGoodsUpdateLog->is_update) { return [ 'code' => 1, 'msg' => '商品信息需要更新' ]; } } } } } $status = empty($this->status) ? 0 : 1; //修改上下架状态 $res = Goods::updateAll(['status' => $status], ['id' => $id]); if ($res) { if (count($id) === 1) { $goods = Goods::findOne($id[0]); (new DiyCommon)->JobBehaviors($goods->store_id, StoreSyncExtLog::TYPE_PRODUCT, $id); } return [ 'code' => 0, 'msg' => "操作成功" ]; } throw new \Exception('信息修改失败'); } catch (\Exception $e){ return [ 'code' => 0, 'msg' => $e->getMessage() ]; } } //修改价格/数量 (规格库存价格) public function setPrice(){ try { $this->initGoods(); $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 ($goods->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; $store_id = $this->store_id; $this->initGoods(); $model = $this->model; if ($id) { $goods = $model->where(['id'=>$id])->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' => $store_id])->select('id, name, parent_id')->asArray()->all(); $new_AttrLibrary = $this->getmenu($AttrLibrary); $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, $store_id, 'store')['value']; $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, $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' => $store_id, 'is_delete' => 0, 'mch_id' => 0 ])->select(['id', 'name', 'is_enable'])->asArray()->all(); //分类列表 $cat = Cat::find()->where(['is_delete' => 0, 'is_show' => 1, 'store_id' => $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 { $admin = Admin::findOne(['saas_user_id' => get_saas_user_id(), 'is_delete' => 0, 'type' => 'store']); // 获取运费规则 $postage = PostageRules::find()->where([ 'store_id' => $admin->type_id, 'is_delete' => 0, 'mch_id' => 0 ])->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(){ $t = \Yii::$app->db->beginTransaction(); try { $this->initGoods(); $model = $this->model; $goods_info = $this->goods_info; $store_id = $this->store_id; 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 = $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 ((int)$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'] ?: 0; $model->cost_price = $goods_info['cost_price'] ?: 0; if ($model->save()) { $goods_info['id'] = $model->id; //分类处理 if ($goods_info['cat_id']) { if (!empty($id)) { GoodsCat::deleteAll(['goods_id' => $id]); } if (!is_array($goods_info['cat_id'])) { $goods_info['cat_id'] = [$goods_info['cat_id']]; } foreach ($goods_info['cat_id'] as $item) { if (empty($item)) { continue; } $goods_cat = new GoodsCat(); $goods_cat->goods_id = $model->id; $goods_cat->store_id = $this->store_id; $goods_cat->cat_id = (int)$item; if (!$goods_cat->save()) { throw new \Exception(json_encode($goods_cat->errors)); } } } if ($goods_info['goods_pic_list']) { if (!empty($id)) { GoodsPic::updateAll(['is_delete' => 1], ['goods_id' => $id, 'is_delete' => 0]); } foreach ($goods_info['goods_pic_list'] as $item) { if(!isset($item['pic_url']) || empty($item['pic_url'])){ continue; } $goods_pic = new GoodsPic(); $goods_pic->pic_url = $item['pic_url']; $goods_pic->goods_id = $model->id; if (!$goods_pic->save()) { throw new \Exception(json_encode($goods_pic->errors)); } } } //判断如果规格发生改变就删除满减规则 if (!empty($goods_info['is_change_attr'])) { $res = GoodsFullMinus::updateAll(['is_delete' => 1], ['goods_id' => $model->id]); \Yii::error($res, "规格发生改变就删除满减规则"); } $form = new StoreAdminAttrForm(); $form->store_id = $store_id; $res = $form->goodsAttrBook($goods_info); if ($res['code'] != 0) { throw new \Exception($res['msg']); } $t->commit(); return [ 'code' => 0, 'msg' => "保存成功" ]; } throw new \Exception(json_encode($model->errors)); } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage().$e->getLine() ]; } } //展示商品详情 public function goodsDetail(){ try { $id = $this->id; $this->initGoods(); $model = $this->model; $store_id = $this->store_id; 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' => $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' => $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(){ $store_id = $this->store_id; $data = Cat::find()->where(['store_id' => $store_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; $store_id = $this->store_id; $model = Cat::findOne($id)? : new Cat(); $model->parent_id = $parent_id; $model->name = $cat_name; $model->store_id = $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 catAddBatch($name_1, $name_2, $name_3){ try { $store_id = $this->store_id; if(empty($name_1)){ return [ 'code' => 1, 'msg' => '缺少参数' ]; } $data = []; $model = Cat::findOne(['store_id' => $store_id, 'is_delete' => 0, 'parent_id' => 0, 'name' => $name_1]); if(!$model){ $model = new Cat(); $model->parent_id = 0; $model->name = $name_1; $model->store_id = $store_id; $model->created_at = time(); $model->updated_at = time(); if (!$model->save()) { throw new \Exception(json_encode($model->errors)); } } $data[] = $model; if($name_2){ $model2 = Cat::findOne(['store_id' => $store_id, 'is_delete' => 0, 'parent_id' => $model->id, 'name' => $name_2]); if(!$model2){ $model2 = new Cat(); $model2->parent_id = $model->id; $model2->name = $name_2; $model2->store_id = $store_id; $model2->created_at = time(); $model2->updated_at = time(); if (!$model2->save()) { throw new \Exception(json_encode($model2->errors)); } } $data[] = $model2; } if($name_2 && $name_3){ $model3 = Cat::findOne(['store_id' => $store_id, 'is_delete' => 0, 'parent_id' => $model2->id, 'name' => $name_3]); if(!$model3){ $model3 = new Cat(); $model3->parent_id = $model2->id; $model3->name = $name_3; $model3->store_id = $store_id; $model3->created_at = time(); $model3->updated_at = time(); if (!$model3->save()) { throw new \Exception(json_encode($model3->errors)); } } $data[] = $model3; } return [ 'code' => 0, 'msg' => "保存成功", 'data' => $data, ]; } 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("找不到对应的数据信息"); } $model = Cat::findOne($id); if (empty($model)) { throw new \Exception("找不到对应的数据信息"); } switch ($status) { case 0: $res = Cat::updateAll(['is_show' => 0], ['OR', ['id' => $id], ['parent_id' => $id]]); break; case 1: $res = Cat::updateAll(['is_show' => 1], ['OR', ['id' => $id], ['parent_id' => $id]]); break; case 2: $res = Cat::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 { $erp_open = intval(Option::get(OptionSetting::ERP_OPEN, $this->store_id, 'store', 0)['value']); if (!$erp_open) { throw new \Exception('未开启本地进销存'); } $admin = Admin::findOne(['type' => 'store', 'type_id' => $this->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; } }