ProductController.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/1/15 20:36
  5. */
  6. namespace ghs\controllers;
  7. use biz\admin\classes\AdminRoleClass;
  8. use biz\item\classes\PtItemClass;
  9. use bizGhs\custom\classes\CustomClass;
  10. use bizGhs\item\classes\ItemClass;
  11. use bizGhs\item\classes\ItemClassClass;
  12. use bizGhs\merchant\classes\ShopClass;
  13. use bizGhs\order\classes\PurchaseOrderClass;
  14. use bizGhs\product\classes\ProductClass;
  15. use bizGhs\product\models\Product;
  16. use bizGhs\product\services\ProductService;
  17. use bizGhs\shop\classes\ShopAdminClass;
  18. use bizGhs\stock\classes\StockInDayClass;
  19. use common\components\printUtil;
  20. use common\components\stringUtil;
  21. use common\components\util;
  22. use Yii;
  23. class ProductController extends BaseController
  24. {
  25. public $guestAccess = ['index'];
  26. //停止预售 ssh 20230224
  27. public function actionCancelPreSell()
  28. {
  29. $get = Yii::$app->request->get();
  30. $id = $get['id'] ?? 0;
  31. $info = ProductClass::getById($id, true);
  32. ProductClass::check($info, $this->mainId);
  33. if ($info->stock > 0) {
  34. util::fail('没有库存才能停止预售');
  35. }
  36. $info->presell = 0;
  37. $info->presellDate = '';
  38. $info->save();
  39. util::complete();
  40. }
  41. //修改预售 ssh 20221214
  42. public function actionChangePresell()
  43. {
  44. $post = Yii::$app->request->post();
  45. $id = $post['id'] ?? 0;
  46. $info = ProductClass::getById($id, true);
  47. ProductClass::check($info, $this->mainId);
  48. $str = $post['date'] ?? '';
  49. $presell = $post['presell'] ?? 0;
  50. $newStr = '';
  51. if ($presell == 1) {
  52. if (empty($str)) {
  53. util::fail('请填写预售日期');
  54. }
  55. $arr = json_decode($str, true);
  56. if (empty($arr)) {
  57. util::fail('请填写预售日期');
  58. }
  59. $new = [];
  60. foreach ($arr as $date) {
  61. $new[] = strtotime($date);
  62. }
  63. $new = array_unique(array_filter($new));
  64. asort($new);
  65. $newStr = implode(',', $new);
  66. }
  67. $info->presell = $presell;
  68. $info->presellDate = $newStr;
  69. $info->discountPrice = 0;
  70. $info->save();
  71. util::complete();
  72. }
  73. //取消特价 ssh 20220901
  74. public function actionCancelDiscount()
  75. {
  76. $get = Yii::$app->request->get();
  77. $id = $get['id'] ?? 0;
  78. $info = ProductClass::getById($id, true);
  79. ProductClass::check($info, $this->mainId);
  80. $info->discountPrice = 0;
  81. $info->save();
  82. util::complete();
  83. }
  84. //打印花材的标签 ssh 20220602
  85. public function actionPrint()
  86. {
  87. $get = Yii::$app->request->get();
  88. $id = $get['id'] ?? 0;
  89. $num = $get['num'] ?? 1;
  90. $type = $get['type'] ?? 0;
  91. $info = ProductClass::getById($id, true);
  92. if (empty($info)) {
  93. util::fail('没有找到花材');
  94. }
  95. $name = $info->name ?? '';
  96. $ptItemId = $info->itemId ?? 0;
  97. $mainId = $info->mainId ?? 0;
  98. $ext = $this->shopExt;
  99. $printLabelSn = $ext->printLabelSn ?? '';
  100. if (empty($printLabelSn)) {
  101. util::fail('请设置标签打印机');
  102. }
  103. $p = new printUtil($printLabelSn);
  104. $unit = $info->ratio . $info->smallUnit . '/' . $info->bigUnit;
  105. $p->times = $num;
  106. if (stringUtil::getWordNum($name) > 7) {
  107. $content = '<TEXT x="30" y="30" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
  108. } else {
  109. $content = '<TEXT x="30" y="30" font="12" w="2" h="2" r="0">' . $name . '</TEXT>';
  110. }
  111. //杭州斗南批发店
  112. if (isset($get['type']) == false && $this->shopId == 4608) {
  113. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $id . '</QR>';
  114. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  115. } else {
  116. if ($type == 0) {
  117. //打标签
  118. $content .= '<BC128 x="30" y="115" h="75" s="1" r="0" n="3" w="10">' . $ptItemId . '</BC128>';
  119. if (in_array($mainId, [52, 1459])) {
  120. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">惠雅鲜花</TEXT>';
  121. } else {
  122. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $unit . '</TEXT>';
  123. }
  124. } else {
  125. //打价码
  126. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $id . '</QR>';
  127. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  128. }
  129. }
  130. $p->printLabelMsg($content);
  131. util::complete();
  132. }
  133. //取出今天有入库的花材 ssh 20221024
  134. public function actionStockIn()
  135. {
  136. $py = Yii::$app->request->get('py', '');
  137. $version = Yii::$app->request->get('version', 0);
  138. if ($version == 0) {
  139. util::fail('请先升级版本到1.0.70以上');
  140. }
  141. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  142. $mainId = $this->mainId ?? 0;
  143. $where['mainId'] = $mainId;
  144. if ($py) {
  145. $pyName = strtolower($py);
  146. $where['py'] = $pyName;
  147. }
  148. $arr = StockInDayClass::getAllByCondition(['mainId' => $mainId], null, '*');
  149. if (empty($arr)) {
  150. util::fail('没有花材需要盘点');
  151. }
  152. $ids = array_column($arr, 'itemId');
  153. $ids = array_unique($ids);
  154. $where['id'] = ['in', $ids];
  155. $level = 1;
  156. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell,presellDate';
  157. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  158. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  159. $data = ProductService::assembleData($classInfo, $itemInfoData, true);
  160. util::success($data);
  161. }
  162. //按分类列出所有花材
  163. public function actionShow()
  164. {
  165. $py = Yii::$app->request->get('py', '');
  166. $requestType = Yii::$app->request->get('requestType', 'common');
  167. //默认显示上架商品
  168. $status = Yii::$app->request->get('status', 1);
  169. //默认显示未删除商品
  170. $delStatus = Yii::$app->request->get('delStatus', 0);
  171. //获取所有当前供货商的分类 (全部、常用)
  172. //根据分类组装分类下的花材信息
  173. //中央ID
  174. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  175. $where['mainId'] = $this->mainId;
  176. if ($py) {
  177. $pyName = strtolower($py);
  178. $where['py'] = $pyName;
  179. }
  180. //改价和全部有库存花材列表
  181. if ($requestType == 'changePrice' || $requestType == 'hasStockList') {
  182. $where['stock>'] = 0;
  183. }
  184. if (!empty($status)) {
  185. $where['status'] = $status;
  186. }
  187. $where['delStatus'] = $delStatus;
  188. //客户等级
  189. $customId = Yii::$app->request->get('customId', 0);
  190. $level = 1;
  191. if (!empty($customId)) {
  192. $custom = CustomClass::getById($customId, true);
  193. $level = $custom->level ?? 1;
  194. }
  195. $itemInfoData = [];
  196. if ($requestType == 'hasStockList') {
  197. //有库存的花材列表
  198. $field = 'id,py,name,classId,itemId,price,skPrice,stock';
  199. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  200. } elseif ($requestType == 'changePrice') {
  201. //修改价格列表
  202. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,discountPrice,skMore,variety,price,stock,presell';
  203. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  204. $itemInfoData = ProductClass::changePriceGroup($itemInfoData, $level);
  205. } elseif ($requestType == 'kd') {
  206. //开单的花材列表
  207. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,smallRatio,bigUnit,ratio';
  208. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  209. $itemInfoData = ProductClass::kdItemGroup($itemInfoData, $level);
  210. } elseif ($requestType == 'itemList') {
  211. //花材列表
  212. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,discountPrice,skMore,stock,actualSold,stockWarning,status,presell';
  213. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  214. $itemInfoData = ProductClass::itemListGroup($itemInfoData, $level);
  215. } elseif ($requestType == 'outList') {
  216. //下架花材列表
  217. $where['status'] = 2;
  218. $where['delStatus'] = 0;
  219. $where['removeStatus'] = 0;
  220. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,discountPrice,skMore,variety,price,stock,presell,status';
  221. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  222. $itemInfoData = ProductClass::simpleGroup($itemInfoData, $level);
  223. } elseif ($requestType == 'stopList') {
  224. //停用花材列表
  225. $where['status'] = 2;
  226. $where['delStatus'] = 1;
  227. $where['removeStatus'] = 0;
  228. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,discountPrice,skMore,variety,price,stock,presell,status';
  229. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  230. $itemInfoData = ProductClass::simpleGroup($itemInfoData, $level);
  231. } elseif ($requestType == 'removeList') {
  232. //删除花材列表
  233. $where['status'] = 2;
  234. $where['delStatus'] = 1;
  235. $where['removeStatus'] = 1;
  236. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,discountPrice,skMore,variety,price,stock,presell,status';
  237. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  238. $itemInfoData = ProductClass::simpleGroup($itemInfoData, $level);
  239. } elseif ($requestType == 'check') {
  240. //盘点花材列表
  241. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio';
  242. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  243. $itemInfoData = ProductClass::checkItemGroup($itemInfoData, $level);
  244. } elseif ($requestType == 'break') {
  245. //报损花材
  246. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio';
  247. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  248. $itemInfoData = ProductClass::breakItemGroup($itemInfoData, $level);
  249. } elseif ($requestType == 'part') {
  250. //报损花材
  251. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio';
  252. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  253. $itemInfoData = ProductClass::partItemGroup($itemInfoData, $level);
  254. } elseif ($requestType == 'stockOut') {
  255. //出库花材
  256. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio,smallRatio';
  257. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  258. $itemInfoData = ProductClass::stockOutGroup($itemInfoData, $level);
  259. } elseif ($requestType == 'cg') {
  260. //采购花材
  261. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,presell,ratioType,smallUnit,bigUnit,ratio,weight';
  262. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  263. $itemInfoData = ProductClass::cgItemGroup($itemInfoData, $level);
  264. } elseif ($requestType == 'warning') {
  265. //库存预警
  266. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,discountPrice,stock,actualSold,presell,bigUnit,stockWarning,status';
  267. $itemInfoData = Product::find()->where("mainId={$this->mainId}")
  268. ->andWhere('`stock`<`stockWarning`')
  269. ->andWhere("delStatus=0")
  270. ->select($field)->asArray()->all();
  271. $itemInfoData = ProductClass::warningGroup($itemInfoData, $level);
  272. } else {
  273. util::fail('没有数据');
  274. }
  275. $data = ProductService::assembleItemData($classInfo, $itemInfoData);
  276. util::success($data);
  277. }
  278. //取出今天有入库的花材 ssh 20221224
  279. public function actionGetStockIn()
  280. {
  281. $py = Yii::$app->request->get('py', '');
  282. $version = Yii::$app->request->get('version', 0);
  283. if ($version == 0) {
  284. util::fail('请先升级版本到1.0.70以上');
  285. }
  286. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  287. $mainId = $this->mainId ?? 0;
  288. $where['mainId'] = $mainId;
  289. if ($py) {
  290. $pyName = strtolower($py);
  291. $where['py'] = $pyName;
  292. }
  293. $arr = StockInDayClass::getAllByCondition(['mainId' => $mainId], null, '*');
  294. if (empty($arr)) {
  295. util::fail('没有花材需要盘点');
  296. }
  297. $ids = array_column($arr, 'itemId');
  298. $ids = array_unique($ids);
  299. $where['id'] = ['in', $ids];
  300. $level = 1;
  301. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio';
  302. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  303. $itemInfoData = ProductClass::checkItemGroup($itemInfoData, $level);
  304. $data = ProductService::assembleItemData($classInfo, $itemInfoData);
  305. util::success($data);
  306. }
  307. //按分类列出所有花材
  308. public function actionIndex()
  309. {
  310. $py = Yii::$app->request->get('py', '');
  311. $requestType = Yii::$app->request->get('requestType', 'common');
  312. //默认显示上架商品
  313. $status = Yii::$app->request->get('status', 1);
  314. //默认显示未删除商品
  315. $delStatus = Yii::$app->request->get('delStatus', 0);
  316. $warning = Yii::$app->request->get('warning', 0);
  317. //获取所有当前供货商的分类 (全部、常用)
  318. //根据分类组装分类下的花材信息
  319. //中央ID
  320. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  321. $where['mainId'] = $this->mainId;
  322. if ($py) {
  323. $pyName = strtolower($py);
  324. $where['py'] = $pyName;
  325. }
  326. if ($requestType == 'hasStock') {
  327. //改价只显示库存大于0的
  328. $where['stock>'] = 0;
  329. }
  330. if (!empty($status)) {
  331. $where['status'] = $status;
  332. }
  333. $where['delStatus'] = $delStatus;
  334. //客户等级
  335. $customId = Yii::$app->request->get('customId', 0);
  336. $level = 1;
  337. if (!empty($customId)) {
  338. $custom = CustomClass::getById($customId, true);
  339. $level = $custom->level ?? 1;
  340. }
  341. if ($warning == 1) {
  342. //库存预警
  343. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell,presellDate';
  344. $itemInfoData = Product::find()->where("mainId={$this->mainId}")
  345. ->andWhere('`stock`<`stockWarning`')
  346. ->andWhere("delStatus=0")
  347. ->select($field)->asArray()->all();
  348. } else {
  349. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell,presellDate';
  350. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  351. }
  352. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  353. $data = ProductService::assembleData($classInfo, $itemInfoData, true);
  354. util::success($data);
  355. }
  356. //所有花材分页和不分页
  357. public function actionList()
  358. {
  359. $where = [];
  360. $classId = Yii::$app->request->get('classId', 0);
  361. if (!empty($classId)) {
  362. $where['classId'] = $classId;
  363. }
  364. $name = Yii::$app->request->get('name', '');
  365. if (empty($name)) {
  366. $py = Yii::$app->request->get('py', '');
  367. $name = !empty($py) ? $py : '';
  368. }
  369. if (!empty($name)) {
  370. if (stringUtil::isLetter($name)) {
  371. $where['py'] = ['like', $name];
  372. } else {
  373. $where['name'] = ['like', $name];
  374. }
  375. }
  376. $status = Yii::$app->request->get('status', 0);
  377. if (!empty($status)) {
  378. $where['status'] = $status;
  379. }
  380. $delStatus = Yii::$app->request->get('delStatus', 0);
  381. if ($delStatus != 2) {
  382. $where['delStatus'] = $delStatus;
  383. }
  384. $where['mainId'] = $this->mainId;
  385. $productIds = Yii::$app->request->get('ids', '');
  386. if (!empty($productIds)) {
  387. $productIdArr = explode(',', $productIds);
  388. if (!empty($productIdArr)) {
  389. $where['id'] = ['in', $productIdArr];
  390. }
  391. }
  392. //输出标准会员价,即花店价,默认显示的是花店价
  393. $level = 1;
  394. $type = Yii::$app->request->get('type', '');
  395. $showPage = Yii::$app->request->get('showPage', 0);
  396. if ($showPage) {
  397. if ($type == 'waring') {
  398. $pro = Product::find()->where("mainId={$this->mainId}")->andWhere('`stock`<`stockWarning`')->select('id')->asArray()->all();
  399. $lackIds = array_column($pro, 'id');
  400. if (empty($lackIds)) {
  401. util::success([]);
  402. }
  403. $lackIds = array_filter(array_unique($lackIds));
  404. $where['id'] = ['in', $lackIds];
  405. $respond = ProductClass::getList("*", $where, "actualSold desc");
  406. } else {
  407. $respond = ProductClass::getList("*", $where, "inTurn desc");
  408. }
  409. $respond['list'] = ProductClass::groupProductInfo($respond['list'], $level);
  410. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  411. util::success($respond);
  412. } else {
  413. if ($type == 'waring') {
  414. //库存预警 按库存从小到大排序
  415. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  416. } else {
  417. if (!empty($productIds)) {
  418. //优先按 $productIds 排序 "FIELD(`id`,4,5,6)"=>true
  419. $data = ProductClass::getAllByCondition($where, ["FIELD(`id`,$productIds)" => true, 'inTurn' => SORT_DESC], "*");
  420. } else {
  421. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  422. }
  423. }
  424. $data = ProductClass::groupProductInfo($data, $level);
  425. $data = ProductClass::groupClassName($this->sjId, $data);
  426. if ($type == 'waring') {
  427. //库存预警
  428. $newData = [];
  429. foreach ($data as $v) {
  430. if ($v['stock'] <= $v['stockWarning']) {
  431. $newData[] = $v;
  432. }
  433. }
  434. util::success(['list' => $newData]);
  435. }
  436. util::success(['list' => $data]);
  437. }
  438. }
  439. //所有花材
  440. public function actionAll()
  441. {
  442. $where = [];
  443. $where['mainId'] = $this->mainId;
  444. $classId = Yii::$app->request->get('classId', 0);
  445. if (!empty($classId)) {
  446. $where['classId'] = $classId;
  447. }
  448. $status = Yii::$app->request->get('status', 0);
  449. if (!empty($status)) {
  450. $where['status'] = $status;
  451. }
  452. $delStatus = Yii::$app->request->get('delStatus', 0);
  453. if ($delStatus != 2) {
  454. $where['delStatus'] = $delStatus;
  455. }
  456. //输出标准会员价,即花店价,默认显示的是花店价
  457. $level = 1;
  458. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  459. $data = ProductClass::groupProductInfo($data, $level);
  460. util::success(['list' => $data]);
  461. }
  462. //批量改价
  463. public function actionBatchChangePrice()
  464. {
  465. $shop = $this->shop;
  466. $join = $shop->join ?? 0;
  467. $default = $shop->default ?? 0;
  468. if ($join == 0 && $default == 0) {
  469. util::fail('请在默认门店修改');
  470. }
  471. $shopAdmin = $this->shopAdmin;
  472. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  473. util::fail('超管才能修改');
  474. }
  475. $shop = $this->shop;
  476. $default = $shop->default ?? 0;
  477. $adminId = $this->adminId ?? 0;
  478. //normal常规改价,cg采购改价
  479. $changeType = Yii::$app->request->post('changeType', 'normal');
  480. $targetId = Yii::$app->request->post('targetId', 0);
  481. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  482. $data = Yii::$app->request->post('data', '');
  483. if (empty($data)) {
  484. util::fail('没有修改');
  485. }
  486. $arr = json_decode($data, true);
  487. if (empty($arr)) {
  488. util::fail('没有修改哦');
  489. }
  490. $connection = Yii::$app->db;
  491. $transaction = $connection->beginTransaction();
  492. try {
  493. $cg = PurchaseOrderClass::getLockById($targetId);
  494. if (!empty($cg)) {
  495. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  496. util::fail('不是您的采购单');
  497. }
  498. //批量改价同时入库
  499. if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_SENDING) {
  500. $staff = $this->shopAdmin;
  501. $data = [];
  502. $data['staffId'] = $staff->id ?? 0;
  503. $data['staffName'] = $staff->name ?? '';
  504. $data['adminId'] = $this->adminId ?? 0;
  505. PurchaseOrderClass::putIn($cg, $data);
  506. }
  507. }
  508. $ids = array_column($arr, 'id');
  509. $ids = array_unique(array_filter($ids));
  510. if (empty($ids)) {
  511. util::fail('请选择花材');
  512. }
  513. $sjId = $shop->sjId ?? 0;
  514. $chainShopList = [];
  515. if ($default == 1) {
  516. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  517. }
  518. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  519. foreach ($arr as $product) {
  520. $productId = $product['id'];
  521. $productInfo = $productList[$productId] ?? [];
  522. if (empty($productInfo)) {
  523. util::fail('没有找到花材');
  524. }
  525. $productName = $productInfo->name ?? '';
  526. if (isset($product['id']) == false) {
  527. util::fail('没有花材');
  528. }
  529. if (isset($product['price']) == false || is_numeric($product['price']) == false || $product['price'] < 0) {
  530. util::fail('请填写' . $productName . '的花店价');
  531. }
  532. $price = $product['price'];
  533. if (isset($product['skPrice']) == false || is_numeric($product['skPrice']) == false || $product['skPrice'] < 0) {
  534. util::fail('请填写' . $productName . '的散客价');
  535. }
  536. $skPrice = $product['skPrice'];
  537. if (isset($productInfo->mainId) == false || $productInfo->mainId != $this->mainId) {
  538. util::fail('没有权限访问');
  539. }
  540. if (empty($price)) {
  541. continue;
  542. }
  543. $ptItemId = $productInfo->itemId;
  544. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $changeParams);
  545. //在首店修改需要同步到所有直营店
  546. if ($default == 1) {
  547. foreach ($chainShopList as $chain) {
  548. if ($chain->id == $shop->id) {
  549. continue;
  550. }
  551. $chainMainId = $chain->mainId ?? 0;
  552. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  553. $changeParams2 = ['staffId' => $staff->id, 'staffName' => $staff->name, 'changeType' => $changeType, 'targetId' => $targetId];
  554. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $changeParams2);
  555. }
  556. }
  557. }
  558. $mainId = $shop->mainId ?? 0;
  559. if (!empty($mainId)) {
  560. //提醒零售收银台界面要刷新
  561. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  562. if (!empty($staffList)) {
  563. foreach ($staffList as $staff) {
  564. $staffId = $staff->id ?? 0;
  565. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  566. }
  567. }
  568. }
  569. $transaction->commit();
  570. util::complete('操作成功');
  571. } catch (\Exception $e) {
  572. $transaction->rollBack();
  573. Yii::info("修改失败原因:" . $e->getMessage());
  574. util::fail('修改失败');
  575. }
  576. }
  577. //改价
  578. public function actionChangePrice()
  579. {
  580. $shopAdmin = $this->shopAdmin;
  581. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  582. util::fail('超管才能修改');
  583. }
  584. $productId = Yii::$app->request->post('productId', 0);
  585. $productInfo = ProductClass::getById($productId, true);
  586. if (empty($productInfo)) {
  587. util::fail("花材不存在");
  588. }
  589. if ($productInfo->mainId != $this->mainId) {
  590. util::fail('没有权限');
  591. }
  592. $price = Yii::$app->request->post('price', '');
  593. if (is_numeric($price) && $price > 0) {
  594. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  595. } else {
  596. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  597. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  598. }
  599. util::success(['price' => $price]);
  600. }
  601. //恢复自动调价
  602. public function actionAutoPrice()
  603. {
  604. $productId = Yii::$app->request->post('productId', 0);
  605. $productData = ProductClass::getById($productId);
  606. if (!$productData) {
  607. util::fail("花材不存在");
  608. }
  609. //2021.04.06改为用product中的成本价+加价
  610. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  611. ProductClass::changeSinglePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  612. util::complete("操作成功");
  613. }
  614. //修改加价
  615. public function actionChangeAddPrice()
  616. {
  617. $productId = Yii::$app->request->post('productId', 0);
  618. $addPrice = Yii::$app->request->post('addPrice', '');
  619. if ($addPrice < 0) {
  620. util::fail("加价不能小于0");
  621. }
  622. $productData = ProductClass::getProductData($productId, $this->mainId);
  623. if (!$productData) {
  624. util::fail("花材不存在");
  625. }
  626. ProductClass::changeAddPrice($productId, $addPrice);
  627. util::complete();
  628. }
  629. //批量修改更多,包括排序、重量和保质期 ssh 20211211
  630. public function actionBatchChangeMore()
  631. {
  632. $shop = $this->shop;
  633. $default = $shop->default ?? 0;
  634. if ($default == 0) {
  635. util::fail('请在首店修改');
  636. }
  637. $shopAdmin = $this->shopAdmin;
  638. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  639. util::fail('超管才能操作');
  640. }
  641. $addData = Yii::$app->request->post('addData', '');
  642. if (empty($addData)) {
  643. util::fail('没有花材');
  644. }
  645. $itemData = json_decode($addData, true);
  646. if (is_array($itemData) == false) {
  647. util::fail('没有花材...');
  648. }
  649. $ids = array_column($itemData, 'id');
  650. $infoList = ProductClass::getByIds($ids, null, 'id');
  651. if (empty($infoList)) {
  652. util::fail('没有花材');
  653. }
  654. foreach ($infoList as $info) {
  655. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  656. util::fail('不是你的花材不能修改');
  657. }
  658. }
  659. //首店修改同步到所有直营店
  660. $sjId = $shop->sjId ?? 0;
  661. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  662. foreach ($itemData as $key => $data) {
  663. $id = $data['id'] ?? 0;
  664. unset($data['id']);
  665. ProductClass::updateById($id, $data);
  666. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  667. if (empty($ptItemId)) {
  668. continue;
  669. }
  670. if (isset($shop->default) && $shop->default == 1) {
  671. foreach ($chainShopList as $chainShop) {
  672. $currentMainId = $chainShop->mainId ?? 0;
  673. if ($currentMainId == $shop->mainId) {
  674. //前面已经修改过了,不需要重复修改
  675. continue;
  676. }
  677. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  678. }
  679. }
  680. }
  681. util::complete('修改成功');
  682. }
  683. //批量修改加价 ssh 20211211
  684. public function actionBatchChangeAddPrice()
  685. {
  686. $shop = $this->shop;
  687. $join = $shop->join ?? 0;
  688. $default = $shop->default ?? 0;
  689. if ($join == 0 && $default == 0) {
  690. util::fail('请在首店操作');
  691. }
  692. $shopAdmin = $this->shopAdmin;
  693. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  694. util::fail('超管才能操作');
  695. }
  696. $addData = Yii::$app->request->post('addData', '');
  697. if (empty($addData)) {
  698. util::fail('没有花材');
  699. }
  700. $itemData = json_decode($addData, true);
  701. if (is_array($itemData) == false) {
  702. util::fail('没有花材...');
  703. }
  704. $ids = array_column($itemData, 'id');
  705. $infoList = ProductClass::getByIds($ids, null, 'id');
  706. if (empty($infoList)) {
  707. util::fail('没有花材');
  708. }
  709. foreach ($infoList as $info) {
  710. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  711. util::fail('不能修改别人的花材');
  712. }
  713. }
  714. //首店修改同步到所有直营店
  715. $sjId = $shop->sjId ?? 0;
  716. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  717. foreach ($itemData as $key => $data) {
  718. $id = $data['id'] ?? 0;
  719. unset($data['id']);
  720. ProductClass::updateById($id, $data);
  721. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  722. if (empty($ptItemId)) {
  723. continue;
  724. }
  725. if (isset($shop->default) && $shop->default == 1) {
  726. foreach ($chainShopList as $chainShop) {
  727. if (isset($chainShop->join) && $chainShop->join == 1) {
  728. continue;
  729. }
  730. $currentMainId = $chainShop->mainId ?? 0;
  731. if ($currentMainId == $shop->mainId) {
  732. //前面已经修改过了,不需要重复修改
  733. continue;
  734. }
  735. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  736. }
  737. }
  738. }
  739. util::complete('修改成功');
  740. }
  741. public function actionUpdate()
  742. {
  743. $shopAdmin = $this->shopAdmin;
  744. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  745. util::fail('超管才能操作');
  746. }
  747. $shop = $this->shop;
  748. $join = $shop->join ?? 0;
  749. $default = $shop->default ?? 0;
  750. if ($join == 0 && $default == 0) {
  751. //因为盘点不能将花材盘成0,所以这边放出入口允许创始人在分店修改分店的花材库存
  752. if ($shopAdmin->super != 1) {
  753. util::fail('请先开通超管权限');
  754. }
  755. }
  756. $post = Yii::$app->request->post();
  757. $post['adminId'] = $this->adminId;
  758. $post['staffId'] = $this->shopAdmin->id;
  759. $post['staffName'] = $this->shopAdmin->name;
  760. ProductClass::updateProduct($post, $this->shop);
  761. util::complete();
  762. }
  763. //上下架状态控制 ssh 20210713
  764. public function actionStatusUpdate()
  765. {
  766. $post = Yii::$app->request->post();
  767. $shopAdmin = $this->shopAdmin;
  768. $roleId = $shopAdmin['roleId'] ?? 0;
  769. $role = AdminRoleClass::getById($roleId);
  770. $roleName = $role['roleName'] ?? '';
  771. if ($roleName == '员工') {
  772. util::fail('没有权限操作哦');
  773. }
  774. $id = $post['id'] ?? 0;
  775. $status = $post['status'] ?? 1;
  776. $product = ProductClass::getById($id, true);
  777. if ($product->mainId != $this->mainId) {
  778. util::fail('没有权限操作');
  779. }
  780. $product->status = $status;
  781. if ($status == 2) {
  782. $product->discountPrice = 0;
  783. }
  784. $product->save();
  785. util::complete();
  786. }
  787. //花材展示状态控制 ssh 20210804
  788. public function actionDelStatusUpdate()
  789. {
  790. $post = Yii::$app->request->post();
  791. $shop = $this->shop;
  792. $join = $shop->join ?? 0;
  793. $default = $shop->default ?? 0;
  794. if ($join == 0 && $default == 0) {
  795. util::fail('请在首店操作');
  796. }
  797. $shopAdmin = $this->shopAdmin;
  798. $roleId = $shopAdmin['roleId'] ?? 0;
  799. $role = AdminRoleClass::getById($roleId);
  800. $roleName = $role['roleName'] ?? '';
  801. if ($roleName == '员工') {
  802. util::fail('没有权限操作');
  803. }
  804. $id = $post['id'] ?? 0;
  805. $delStatus = $post['delStatus'] ?? 0;
  806. $product = ProductClass::getById($id, true);
  807. if ($product->mainId != $this->mainId) {
  808. util::fail('没有权限操作');
  809. }
  810. $product->delStatus = $delStatus;
  811. $product->save();
  812. $ptItemId = $product->itemId ?? 0;
  813. if (isset($shop->default) && $shop->default == 1) {
  814. //首店修改同步到所有直营店
  815. $sjId = $shop->sjId ?? 0;
  816. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  817. foreach ($chainShopList as $chainShop) {
  818. $chainShopId = $chainShop->id ?? 0;
  819. if (isset($chainShop->join) && $chainShop->join == 1) {
  820. continue;
  821. }
  822. if ($chainShopId == $shop->id) {
  823. continue;
  824. }
  825. $chainMainId = $chainShop->mainId ?? 0;
  826. $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  827. if (!empty($chainProduct)) {
  828. $chainProduct->delStatus = $delStatus;
  829. $chainProduct->save();
  830. }
  831. }
  832. }
  833. util::complete();
  834. }
  835. //详情 ruidian 2021.5.3
  836. public function actionDetail()
  837. {
  838. $id = Yii::$app->request->get('id', 0);
  839. $respond = ProductClass::getItemInfo($id);
  840. $ptItemId = $respond['itemId'] ?? 0;
  841. $ptItemInfo = PtItemClass::getById($ptItemId);
  842. $respond['ptItemInfo'] = $ptItemInfo;
  843. util::success($respond);
  844. }
  845. //新增花材 2021.6.21
  846. public function actionAdd()
  847. {
  848. $post = Yii::$app->request->post();
  849. $shop = $this->shop;
  850. $default = $shop->default ?? 0;
  851. if ($default == 0) {
  852. util::fail('请在默认门店添加花材,分店只能修改花材');
  853. }
  854. $shopAdmin = $this->shopAdmin;
  855. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  856. util::fail('管理员才能添加花材');
  857. }
  858. $post['sjId'] = $this->sjId;
  859. $post['adminId'] = $this->adminId;
  860. $post['mainId'] = $this->mainId;
  861. $price = $post['price'] ?? 0;
  862. $addPrice = $post['addPrice'] ?? 0;
  863. if ($addPrice > $price) {
  864. util::fail('加价不能大于售价');
  865. }
  866. $weight = isset($post['weight']) && is_numeric($post['weight']) ? $post['weight'] : 0;
  867. if ($weight <= 0) {
  868. util::fail('请填写重量,最小0.1');
  869. }
  870. $post['staffName'] = $shopAdmin->name ?? '';
  871. ProductClass::addProduct($post, $this->shop);
  872. util::complete('添加成功');
  873. }
  874. //获取拼音缩写 ssh 20210707
  875. public function actionPy()
  876. {
  877. $name = Yii::$app->request->get('name');
  878. $py = stringUtil::py($name);
  879. util::success(['py' => $py]);
  880. }
  881. //(客户端-批发商)复制花材
  882. public function actionClone()
  883. {
  884. $post = Yii::$app->request->post();
  885. $shop = $this->shop;
  886. $join = $shop->join ?? 0;
  887. $default = $shop->default ?? 0;
  888. if ($join == 0 && $default == 0) {
  889. util::fail('请在首店操作');
  890. }
  891. $shopAdmin = $this->shopAdmin;
  892. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  893. util::fail('管理员才能添加花材');
  894. }
  895. $shop = $this->shop;
  896. $default = $shop->default ?? 0;
  897. if ($default != 1) {
  898. util::fail('请在总店添加');
  899. }
  900. $post['sjId'] = $this->sjId;
  901. $post['adminId'] = $this->adminId;
  902. $post['mainId'] = $this->mainId;
  903. $post['staffName'] = $shopAdmin->name ?? '';
  904. $price = $post['price'] ?? 0;
  905. $addPrice = $post['addPrice'] ?? 0;
  906. if ($addPrice > $price) {
  907. util::fail('加价不能大于售价');
  908. }
  909. //复制标识
  910. $post['copy'] = 1;
  911. //去除无用的字段
  912. unset($post['viewNum']);
  913. unset($post['actualSold']);
  914. unset($post['itemId']);
  915. unset($post['id']);
  916. unset($post['delStatus']);
  917. ProductClass::addProduct($post, $this->shop);
  918. util::complete('复制成功');
  919. }
  920. //下载价格表 ssh 20210922
  921. public function actionGeneratePriceTable()
  922. {
  923. $level = Yii::$app->request->get('level', 0);
  924. $respond = ProductService::generatePriceTable($this->sjId, $level, $this->mainId);
  925. $file = $respond['file'] ?? '';
  926. $shortFile = $respond['shortFile'] ?? '';
  927. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  928. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  929. }
  930. public function actionGenerateItemTable()
  931. {
  932. $level = Yii::$app->request->get('level', 0);
  933. $respond = ProductService::generateItemTable($this->sjId, $level, $this->mainId);
  934. $file = $respond['file'] ?? '';
  935. $shortFile = $respond['shortFile'] ?? '';
  936. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  937. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  938. }
  939. //下载条形码 lqh 20211227
  940. public function actionGenerateBarcodeTable()
  941. {
  942. $respond = ProductService::generateBarcodeTable($this->sjId, $this->shopId, $this->mainId);
  943. $file = $respond['file'] ?? '';
  944. $shortFile = $respond['shortFile'] ?? '';
  945. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  946. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  947. }
  948. //批量修改花材的分类 ssh 20230405
  949. public function actionBatchModifyClass()
  950. {
  951. $post = Yii::$app->request->post();
  952. $classId = $post['classId'] ?? 0;
  953. $class = ItemClassClass::getById($classId, true);
  954. if (empty($class) || $class->mainId != $this->mainId) {
  955. util::fail('请选择您自己的分类');
  956. }
  957. $idsList = $post['ids'] ?? '';
  958. $ids = json_decode($idsList, true);
  959. if (empty($ids)) {
  960. util::fail('请选择花材');
  961. }
  962. $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  963. if (empty($list)) {
  964. util::fail('请选择花材');
  965. }
  966. foreach ($list as $item) {
  967. $name = $item->name ?? '';
  968. if ($item->mainId != $this->mainId) {
  969. util::fail($name . '不是您的花材');
  970. }
  971. $item->classId = $classId;
  972. $item->save();
  973. }
  974. util::complete();
  975. }
  976. }