| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723 |
- <?php
- /**
- * User: admin
- * Date Time: 2021/1/15 20:36
- */
- namespace ghs\controllers;
- use biz\admin\classes\AdminRoleClass;
- use biz\ghs\classes\GhsClass;
- use biz\item\classes\PtItemClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\item\classes\ItemClassClass;
- use bizGhs\merchant\classes\ShopClass;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\product\models\Product;
- use bizGhs\product\services\ProductService;
- use common\components\stringUtil;
- use common\components\util;
- use Yii;
- class ProductController extends BaseController
- {
- public $guestAccess = ['index'];
- //按分类列出所有花材
- public function actionIndex()
- {
- $py = Yii::$app->request->get('py', '');
- //默认显示上架商品
- $status = Yii::$app->request->get('status', 1);
- //默认显示未删除商品
- $delStatus = Yii::$app->request->get('delStatus', 0);
- //获取所有当前供应商的分类 (全部、常用)
- //根据分类组装分类下的花材信息
- //中央ID
- $classIds = ItemClassClass::getGhsItemClassAll($this->mainId);
- $where['mainId'] = $this->mainId;
- if ($py) {
- $pyName = strtolower($py);
- $where['py'] = $pyName;
- }
- if (!empty($status)) {
- $where['status'] = $status;
- }
- $where['delStatus'] = $delStatus;
- //客户等级
- $customId = Yii::$app->request->get('customId', 0);
- $level = 1;
- if (!empty($customId)) {
- $custom = CustomClass::getById($customId, true);
- $level = $custom->level ?? 1;
- }
- $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
- $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
- $data = ProductService::assembleData($classIds, $itemInfoData, true);
- util::success($data);
- }
- //所有花材分页和不分页
- public function actionList()
- {
- $where = [];
- $where['sjId'] = $this->sjId;
- $classId = Yii::$app->request->get('classId', 0);
- if (!empty($classId)) {
- $where['classId'] = $classId;
- }
- $name = Yii::$app->request->get('name', '');
- if (empty($name)) {
- $py = Yii::$app->request->get('py', '');
- $name = !empty($py) ? $py : '';
- }
- if (!empty($name)) {
- if (stringUtil::isLetter($name)) {
- $where['py'] = ['like', $name];
- } else {
- $where['name'] = ['like', $name];
- }
- }
- $status = Yii::$app->request->get('status', 0);
- if (!empty($status)) {
- $where['status'] = $status;
- }
- $delStatus = Yii::$app->request->get('delStatus', 0);
- if ($delStatus != 2) {
- $where['delStatus'] = $delStatus;
- }
- $shopId = Yii::$app->request->get('shopId', 0);
- if (!$shopId) {
- $shopId = $this->shopId;
- }
- $where['shopId'] = $shopId;
- $productIds = Yii::$app->request->get('ids', '');
- if (!empty($productIds)) {
- $productIdArr = explode(',', $productIds);
- if (!empty($productIdArr)) {
- $where['id'] = ['in', $productIdArr];
- }
- }
- //输出标准会员价,即花店价,默认显示的是花店价
- $level = 1;
- $type = Yii::$app->request->get('type', '');
- $showPage = Yii::$app->request->get('showPage', 0);
- if ($showPage) {
- if ($type == 'waring') {
- $pro = Product::find()->where("shopId={$shopId}")->andWhere('`stock`<`stockWarning`')->select('id')->asArray()->all();
- $lackIds = array_column($pro, 'id');
- if (empty($lackIds)) {
- util::success([]);
- }
- $lackIds = array_filter(array_unique($lackIds));
- $where['id'] = ['in', $lackIds];
- $respond = ProductClass::getList("*", $where, "actualSold desc");
- } else {
- $respond = ProductClass::getList("*", $where, "inTurn desc");
- }
- $respond['list'] = ProductClass::groupProductInfo($respond['list'], $level);
- $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
- util::success($respond);
- } else {
- if ($type == 'waring') {
- //库存预警 按库存从小到大排序
- $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
- } else {
- if (!empty($productIds)) {
- //优先按 $productIds 排序 "FIELD(`id`,4,5,6)"=>true
- $data = ProductClass::getAllByCondition($where, ["FIELD(`id`,$productIds)" => true, 'inTurn' => SORT_DESC], "*");
- } else {
- $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
- }
- }
- $data = ProductClass::groupProductInfo($data, $level);
- $data = ProductClass::groupClassName($this->sjId, $data);
- if ($type == 'waring') {
- //库存预警
- $newData = [];
- foreach ($data as $v) {
- if ($v['stock'] <= $v['stockWarning']) {
- $newData[] = $v;
- }
- }
- util::success(['list' => $newData]);
- }
- util::success(['list' => $data]);
- }
- }
- //所有花材
- public function actionAll()
- {
- $where = [];
- $where['sjId'] = $this->sjId;
- $where['shopId'] = $this->shopId;
- $classId = Yii::$app->request->get('classId', 0);
- if (!empty($classId)) {
- $where['classId'] = $classId;
- }
- $status = Yii::$app->request->get('status', 0);
- if (!empty($status)) {
- $where['status'] = $status;
- }
- $delStatus = Yii::$app->request->get('delStatus', 0);
- if ($delStatus != 2) {
- $where['delStatus'] = $delStatus;
- }
- //输出标准会员价,即花店价,默认显示的是花店价
- $level = 1;
- $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
- $data = ProductClass::groupProductInfo($data, $level);
- util::success(['list' => $data]);
- }
- //批量改价
- public function actionBatchChangePrice()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('超管才能修改');
- }
- //花材价格只能在首店修改,并同步到分店
- $shop = $this->shop;
- $default = $shop->default ?? 0;
- if ($default != 1) {
- util::fail('请在首店修改');
- }
- $data = Yii::$app->request->post('data', '');
- if (empty($data)) {
- util::fail('没有修改');
- }
- $arr = json_decode($data, true);
- if (empty($arr)) {
- util::fail('没有修改!');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $ids = array_column($arr, 'id');
- $ids = array_unique(array_filter($ids));
- if (empty($ids)) {
- util::fail('请选择花材');
- }
- $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
- foreach ($arr as $product) {
- if (isset($product['price']) == false) {
- util::fail('没有价格');
- }
- if (isset($product['id']) == false) {
- util::fail('没有花材');
- }
- $price = $product['price'];
- $productId = $product['id'];
- $product = $productList[$productId] ?? [];
- if (empty($product)) {
- util::fail('没有找到花材');
- }
- if (isset($product->shopId) == false || $product->shopId != $this->shopId) {
- util::fail('没有权限访问');
- }
- if (empty($price)) {
- continue;
- }
- ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
- }
- $transaction->commit();
- util::complete('修改成功');
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::info("修改失败原因:" . $e->getMessage());
- util::fail('修改失败');
- }
- }
- //改价
- public function actionChangePrice()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('超管才能修改');
- }
- //花材价格只能在首店修改,并同步到分店
- $shop = $this->shop;
- $default = $shop->default ?? 0;
- if ($default != 1) {
- util::fail('请在首店修改');
- }
- $productId = Yii::$app->request->post('productId', 0);
- $productInfo = ProductClass::getById($productId, true);
- if (empty($productInfo)) {
- util::fail("花材不存在");
- }
- if ($productInfo->shopId != $this->shopId) {
- util::fail('没有权限');
- }
- $price = Yii::$app->request->post('price', '');
- if (is_numeric($price) && $price > 0) {
- ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
- } else {
- $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
- ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
- }
- util::success(['price' => $price]);
- }
- //恢复自动调价
- public function actionAutoPrice()
- {
- $productId = Yii::$app->request->post('productId', 0);
- $productData = ProductClass::getById($productId);
- if (!$productData) {
- util::fail("花材不存在");
- }
- //2021.04.06改为用product中的成本价+加价
- $price = bcadd($productData['cost'], $productData['addPrice'], 2);
- ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
- util::complete("操作成功");
- }
- //修改加价
- public function actionChangeAddPrice()
- {
- $productId = Yii::$app->request->post('productId', 0);
- $addPrice = Yii::$app->request->post('addPrice', '');
- if ($addPrice < 0) {
- util::fail("加价不能小于0");
- }
- $productData = ProductClass::getProductData($productId, $this->shopId);
- if (!$productData) {
- util::fail("花材不存在");
- }
- ProductClass::changeAddPrice($productId, $addPrice);
- util::complete();
- }
- //批量修改更多,包括排序、重量和保质期 shish 20211211
- public function actionBatchChangeMore()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('超管才能操作');
- }
- $addData = Yii::$app->request->post('addData', '');
- if (empty($addData)) {
- util::fail('没有花材');
- }
- $itemData = json_decode($addData, true);
- if (is_array($itemData) == false) {
- util::fail('没有花材...');
- }
- $ids = array_column($itemData, 'id');
- $infoList = ProductClass::getByIds($ids);
- if (empty($infoList)) {
- util::fail('没有花材');
- }
- foreach ($infoList as $info) {
- if (isset($info['shopId']) == false || $info['shopId'] != $this->shopId) {
- util::fail('非常访问,不是你的花材不能修改');
- }
- }
- foreach ($itemData as $key => $data) {
- $id = $data['id'] ?? 0;
- ProductClass::updateById($id, $data);
- }
- util::complete('修改成功');
- }
- //批量修改加价 shish 20211211
- public function actionBatchChangeAddPrice()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('超管才能操作');
- }
- $addData = Yii::$app->request->post('addData', '');
- if (empty($addData)) {
- util::fail('没有花材');
- }
- $itemData = json_decode($addData, true);
- if (is_array($itemData) == false) {
- util::fail('没有花材...');
- }
- $ids = array_column($itemData, 'id');
- $infoList = ProductClass::getByIds($ids);
- if (empty($infoList)) {
- util::fail('没有花材');
- }
- foreach ($infoList as $info) {
- if (isset($info['shopId']) == false || $info['shopId'] != $this->shopId) {
- util::fail('非常访问,不是你的花材不能修改');
- }
- }
- foreach ($itemData as $key => $data) {
- $id = $data['id'] ?? 0;
- ProductClass::updateById($id, $data);
- }
- util::complete('修改成功');
- }
- //批量修改product 排序,成本价,加价
- public function actionBatchUpdate()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('超管才能修改');
- }
- //花材价格只能在首店修改,并同步到分店
- $shop = $this->shop;
- $default = $shop->default ?? 0;
- if ($default != 1) {
- util::fail('请在首店修改');
- }
- $data = Yii::$app->request->post('data', '');
- $type = Yii::$app->request->post('type', '');
- if (empty($data) || empty($type)) {
- util::fail("参数错误");
- }
- if (in_array($type, ['inTurn', 'addPrice', 'skAddPrice', 'hjAddPrice', 'zsAddPrice', 'cost', 'weight', 'ratio', 'ratio', 'price', 'stockWarning', 'shelfLife']) == false) {
- util::fail('非法数据修改');
- }
- $data = json_decode($data, true);
- foreach ($data as $v) {
- if ($type == 'status') {
- $val = $v[$type] ?? '';
- if (!in_array($val, [1, 2])) {
- util::fail("参数错误");
- }
- }
- }
- foreach ($data as $v) {
- $id = $v['id'];
- $val = $v[$type] ?? '';
- //shish 20210703
- if ($type == 'ratio') {
- if (preg_match("/^[1-9][0-9]*$/", $val) == false || $val <= 0) {
- util::fail('单位比请填写整数,并且要大于0');
- }
- }
- //修改售价 shish 20210703
- if ($type == 'price') {
- $product = ProductClass::getLockById($id);
- $currentName = $product->name ?? '';
- if (empty($product)) {
- continue;
- }
- if ($product->shopId != $this->shopId) {
- util::fail("不是您的产品");
- }
- if (empty($val)) {
- util::fail("没有填写价格:" . $currentName);
- }
- if (is_numeric($val) == false) {
- util::fail("价格不是数值:" . $currentName);
- }
- $currentItemId = $product->itemId ?? 0;
- if (empty($currentItemId)) {
- continue;
- }
- if (empty($val)) {
- continue;
- }
- //成本价需要修改的花材id
- $costChangeIds = Yii::$app->request->post('costChangeIds', []);
- if (!empty($costChangeIds)) {
- $arrIds = json_decode($costChangeIds, true);
- $costIds = is_array($arrIds) ? $arrIds : [];
- if (!empty($costIds) && in_array($id, $costIds)) {
- $currentAddPrice = bcsub($val, $product->cost, 2);
- $skDiff = bcsub($product->skAddPrice, $product->addPrice, 2);
- //大众会员价比标准会员价高,所以是相加
- $newSkAddPrice = bcadd($currentAddPrice, $skDiff, 2);
- $hjDiff = bcsub($product->addPrice, $product->hjAddPrice, 2);
- $newHjAddPrice = bcsub($currentAddPrice, $hjDiff, 2);
- $zsDiff = bcsub($product->addPrice, $product->zsAddPrice, 2);
- $newZsAddPrice = bcsub($currentAddPrice, $zsDiff, 2);
- $params = [];
- $params['skAddPrice'] = $newSkAddPrice;
- $params['addPrice'] = $currentAddPrice;
- $params['hjAddPrice'] = $newHjAddPrice;
- $params['zsAddPrice'] = $newZsAddPrice;
- $sjId = $product->sjId ?? 0;
- $shopAll = ShopClass::getAllShop($sjId);
- if ($shopAll) {
- foreach ($shopAll as $shop) {
- $currentId = $shop['id'] ?? 0;
- $itemId = $product->itemId ?? 0;
- ProductClass::updateByCondition(['shopId' => $currentId, 'itemId' => $itemId], $params);
- }
- }
- }
- }
- ProductClass::changePrice($product, $val, ProductClass::PRICE_LABEL_CHANGE);
- continue;
- }
- // $where = [];
- // $where['id'] = $id;
- // $where['shopId'] = $this->shopId;
- // $where['sjId'] = $this->sjId;
- // if (empty($val)) {
- // util::fail("参数错误!");
- // }
- // $data = [$type => $val];
- // ProductClass::updateByCondition($where, $data);
- $data = [$type => $val];
- $currentProduct = ProductClass::getById($id, true);
- if ($currentProduct->shopId != $this->shopId) {
- util::fail('非常操作');
- }
- $currentProduct->$type = $val;
- if ($type == 'addPrice') {
- $skAddPrice = $currentProduct->skAddPrice;
- if ($skAddPrice <= 0) {
- $currentProduct->skAddPrice = $val - 1 > 0 ? $val - 1 : 1;
- $data['skAddPrice'] = $currentProduct->skAddPrice;
- }
- $hjAddPrice = $currentProduct->hjAddPrice;
- if ($hjAddPrice <= 0) {
- $currentProduct->hjAddPrice = $val - 3 > 0 ? $val - 3 : 1;
- $data['hjAddPrice'] = $currentProduct->hjAddPrice;
- }
- $zsAddPrice = $currentProduct->zsAddPrice;
- if ($zsAddPrice <= 0) {
- $currentProduct->zsAddPrice = $val - 4 > 0 ? $val - 4 : 1;
- $data['zsAddPrice'] = $currentProduct->zsAddPrice;
- }
- }
- $currentProduct->save();
- //2021.7.6 linqh 如果是首店,则更新全部分店
- ProductClass::updateSyncProduct($id, $this->shopId, $data);
- }
- util::complete();
- }
- public function actionUpdate()
- {
- $post = Yii::$app->request->post();
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('超管才能修改');
- }
- $shop = $this->shop;
- $default = $shop->default ?? 0;
- if ($default != 1) {
- //除了上下架其它只能在首店修改
- if (isset($post['status']) == false) {
- util::fail('请在首店修改');
- }
- }
- $post['adminId'] = $this->adminId;
- ProductClass::updateProduct($post);
- util::complete();
- }
- //上下架状态控制 shish 20210713
- public function actionStatusUpdate()
- {
- $post = Yii::$app->request->post();
- $shopAdmin = $this->shopAdmin;
- $roleId = $shopAdmin['roleId'] ?? 0;
- $role = AdminRoleClass::getById($roleId);
- $roleName = $role['roleName'] ?? '';
- if ($roleName == '员工') {
- util::fail('没有权限操作哦');
- }
- $id = $post['id'] ?? 0;
- $status = $post['status'] ?? 1;
- $product = ProductClass::getById($id, true);
- if ($product->shopId != $this->shopId) {
- util::fail('没有权限操作');
- }
- $product->status = $status;
- if ($status == 2) {
- $product->discountPrice = 0;
- }
- $product->save();
- util::complete();
- }
- //花材展示状态控制 shish 20210804
- public function actionDelStatusUpdate()
- {
- $post = Yii::$app->request->post();
- $shopAdmin = $this->shopAdmin;
- $roleId = $shopAdmin['roleId'] ?? 0;
- $role = AdminRoleClass::getById($roleId);
- $roleName = $role['roleName'] ?? '';
- if ($roleName == '员工') {
- util::fail('没有权限操作');
- }
- $id = $post['id'] ?? 0;
- $delStatus = $post['delStatus'] ?? 0;
- $product = ProductClass::getById($id, true);
- if ($product->shopId != $this->shopId) {
- util::fail('没有权限操作');
- }
- $product->delStatus = $delStatus;
- $product->save();
- util::complete();
- }
- //详情 ruidian 2021.5.3
- public function actionDetail()
- {
- $id = Yii::$app->request->get('id', 0);
- $respond = ProductClass::getItemInfo($id);
- $ptItemId = $respond['itemId'] ?? 0;
- $ptItemInfo = PtItemClass::getById($ptItemId);
- $respond['ptItemInfo'] = $ptItemInfo;
- util::success($respond);
- }
- //新增花材 2021.6.21
- public function actionAdd()
- {
- $post = Yii::$app->request->post();
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('管理员才能添加花材');
- }
- $shop = $this->shop;
- $default = $shop->default ?? 0;
- if ($default != 1) {
- util::fail('请在总店添加');
- }
- $post['sjId'] = $this->sjId;
- $post['adminId'] = $this->adminId;
- $post['shopId'] = $this->shopId;
- $price = $post['price'] ?? 0;
- $addPrice = $post['addPrice'] ?? 0;
- if ($addPrice > $price) {
- util::fail('加价不能大于售价');
- }
- $cost = bcsub($price, $addPrice, 2);
- $post['cost'] = $cost;
- ProductClass::addProduct($post);
- util::complete('添加成功');
- }
- //获取拼音缩写 shish 20210707
- public function actionPy()
- {
- $name = Yii::$app->request->get('name');
- $py = stringUtil::py($name);
- util::success(['py' => $py]);
- }
- //复制花材
- public function actionCopy()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('超管才能添加花材');
- }
- $shop = $this->shop;
- $default = $shop->default ?? 0;
- if ($default != 1) {
- util::fail('请在首店添加');
- }
- $id = Yii::$app->request->get('id');
- $productData = ProductClass::getById($id);
- if (!$productData) {
- util::fail("花材不存在");
- }
- if ($productData['shopId'] != $this->shopId) {
- util::fail("无效花材");
- }
- if (isset($productData['variety']) && $productData['variety'] != 0) {
- util::fail('特殊花材不能复制');
- }
- //去除无用的字段
- unset($productData['stock']); //库存去掉
- unset($productData['viewNum']);
- unset($productData['actualSold']);
- unset($productData['itemId']);
- unset($productData['id']);
- unset($productData['delStatus']);
- $productData['copy'] = 1; //复制标识
- ProductClass::addProduct($productData);
- util::complete();
- }
- //下载价格表 shish 20210922
- public function actionGeneratePriceTable()
- {
- $level = Yii::$app->request->get('level', 0);
- $respond = ProductService::generatePriceTable($this->sjId, $this->shopId, $level,$this->mainId);
- $file = $respond['file'] ?? '';
- $shortFile = $respond['shortFile'] ?? '';
- $fileUrl = Yii::$app->params['ghsHost'] . $file;
- util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
- }
- //下载条形码 linqh 20211227
- public function actionGenerateBarcodeTable()
- {
- $respond = ProductService::generateBarcodeTable($this->sjId, $this->shopId,$this->mainId);
- $file = $respond['file'] ?? '';
- $shortFile = $respond['shortFile'] ?? '';
- $fileUrl = Yii::$app->params['ghsHost'] . $file;
- util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
- }
- }
|