ProductController.php 39 KB

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