ProductController.php 40 KB

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