ProductController.php 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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\cp\classes\CpClass;
  10. use bizGhs\cp\classes\CpItemClass;
  11. use bizGhs\custom\classes\CustomClass;
  12. use bizGhs\item\classes\ItemClass;
  13. use bizGhs\item\classes\ItemClassClass;
  14. use bizGhs\merchant\classes\ShopClass;
  15. use bizGhs\notify\classes\NotifyClass;
  16. use bizGhs\order\classes\PurchaseOrderClass;
  17. use bizGhs\product\classes\ProductClass;
  18. use bizGhs\product\models\Product;
  19. use bizGhs\product\services\ProductService;
  20. use bizGhs\shop\classes\ShopAdminClass;
  21. use bizGhs\stock\classes\StockInDayClass;
  22. use common\components\printUtil;
  23. use common\components\stringUtil;
  24. use common\components\util;
  25. use bizGhs\stock\classes\OnStockRecordClass;
  26. use Yii;
  27. class ProductController extends BaseController
  28. {
  29. public $guestAccess = ['index'];
  30. //修改路上库存 ssh 20231219
  31. public function actionModifyOnStock()
  32. {
  33. $get = Yii::$app->request->get();
  34. $id = $get['id'] ?? 0;
  35. $onStock = $get['onStock'] ?? 0;
  36. $info = ProductClass::getLockById($id);
  37. ProductClass::check($info, $this->mainId);
  38. $oldOnStock = $info->onStock ?? 0;
  39. $newOnStock = $onStock;
  40. $num = bcsub($newOnStock, $oldOnStock);
  41. $num = abs($num);
  42. $staff = $this->shopAdmin;
  43. $staffName = $staff->name ?? '';
  44. $onRecord = [];
  45. $onRecord['sjId'] = $this->sjId;
  46. $onRecord['shopId'] = $this->shopId;
  47. $onRecord['mainId'] = $this->mainId;
  48. $onRecord['orderSn'] = '';
  49. $onRecord['productId'] = $id;
  50. $onRecord['itemId'] = $info->itemId ?? 0;
  51. $onRecord['relateName'] = $staffName;
  52. $onRecord['itemNum'] = $num;
  53. $onRecord['oldStock'] = $oldOnStock;
  54. $onRecord['newStock'] = $newOnStock;
  55. $onRecord['ptStyle'] = 2;
  56. OnStockRecordClass::modify($onRecord);
  57. $info->onStock = $newOnStock;
  58. $info->save();
  59. util::complete('修改成功');
  60. }
  61. //停止预售 ssh 20230224
  62. public function actionCancelPreSell()
  63. {
  64. $get = Yii::$app->request->get();
  65. $id = $get['id'] ?? 0;
  66. $info = ProductClass::getById($id, true);
  67. ProductClass::check($info, $this->mainId);
  68. if ($info->stock > 0) {
  69. util::fail('没有库存才能停止预售');
  70. }
  71. $info->presell = 0;
  72. $info->presellDate = '';
  73. $info->save();
  74. util::complete();
  75. }
  76. //修改预售 ssh 20221214
  77. public function actionChangePresell()
  78. {
  79. $post = Yii::$app->request->post();
  80. $id = $post['id'] ?? 0;
  81. $info = ProductClass::getById($id, true);
  82. ProductClass::check($info, $this->mainId);
  83. $str = $post['date'] ?? '';
  84. $presell = $post['presell'] ?? 0;
  85. $newStr = '';
  86. if ($presell == 1) {
  87. if (empty($str)) {
  88. util::fail('请填写预售日期');
  89. }
  90. $arr = json_decode($str, true);
  91. if (empty($arr)) {
  92. util::fail('请填写预售日期');
  93. }
  94. $new = [];
  95. foreach ($arr as $date) {
  96. $new[] = strtotime($date);
  97. }
  98. $new = array_unique(array_filter($new));
  99. asort($new);
  100. $newStr = implode(',', $new);
  101. }
  102. $info->presell = $presell;
  103. $info->presellDate = $newStr;
  104. $info->discountPrice = 0;
  105. $info->save();
  106. util::complete();
  107. }
  108. //取消满减 ssh 20231216
  109. public function actionCancelFullOff()
  110. {
  111. $get = Yii::$app->request->get();
  112. $id = $get['id'] ?? 0;
  113. $info = ProductClass::getById($id, true);
  114. ProductClass::check($info, $this->mainId);
  115. $info->reachNum = 0;
  116. $info->reachNumDiscount = 0;
  117. $info->save();
  118. util::complete('取消成功');
  119. }
  120. //取消特价 ssh 20220901
  121. public function actionCancelDiscount()
  122. {
  123. $get = Yii::$app->request->get();
  124. $id = $get['id'] ?? 0;
  125. $info = ProductClass::getById($id, true);
  126. ProductClass::check($info, $this->mainId);
  127. $info->discountPrice = 0;
  128. $info->skDiscountPrice = 0;
  129. $info->hjDiscountPrice = 0;
  130. $info->save();
  131. util::complete('取消成功');
  132. }
  133. //打印花材的标签 ssh 20220602
  134. public function actionPrint()
  135. {
  136. $get = Yii::$app->request->get();
  137. $id = $get['id'] ?? 0;
  138. $num = $get['num'] ?? 1;
  139. $type = $get['type'] ?? 0;
  140. $info = ProductClass::getById($id, true);
  141. if (empty($info)) {
  142. util::fail('没有找到花材');
  143. }
  144. $name = $info->name ?? '';
  145. $ptItemId = $info->itemId ?? 0;
  146. $mainId = $info->mainId ?? 0;
  147. $ext = $this->shopExt;
  148. $printLabelSn = $ext->printLabelSn ?? '';
  149. if (empty($printLabelSn)) {
  150. util::fail('请设置标签打印机');
  151. }
  152. $p = new printUtil($printLabelSn);
  153. $unit = $info->ratio . $info->smallUnit . '/' . $info->bigUnit;
  154. if (isset($info->ratioType) && $info->ratioType == 1) {
  155. $unit = '若干' . $info->smallUnit . '/' . $info->bigUnit;
  156. }
  157. $p->times = $num;
  158. if (stringUtil::getWordNum($name) > 7) {
  159. $content = '<TEXT x="30" y="30" font="12" w="1" h="1" r="0">' . $name . '</TEXT>';
  160. } else {
  161. $content = '<TEXT x="30" y="30" font="12" w="2" h="2" r="0">' . $name . '</TEXT>';
  162. }
  163. //杭州斗南批发店
  164. if (isset($get['type']) == false && $this->shopId == 4608) {
  165. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $id . '</QR>';
  166. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  167. } else {
  168. if ($type == 0) {
  169. //打标签
  170. $content .= '<BC128 x="30" y="115" h="75" s="1" r="0" n="3" w="10">' . $ptItemId . '</BC128>';
  171. if (in_array($mainId, [52, 1459])) {
  172. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">惠雅鲜花</TEXT>';
  173. } else {
  174. $content .= '<TEXT x="30" y="250" font="12" w="2" h="2" r="0">' . $unit . '</TEXT>';
  175. }
  176. } else {
  177. //打价码
  178. $content .= '<QR x="35" y="100" e="L" w="5">' . Yii::$app->params['mallHost'] . "/item/show-info?id=" . $id . '</QR>';
  179. $content .= '<TEXT x="40" y="270" font="12" w="1" h="1" r="0">扫码看价格</TEXT>';
  180. }
  181. }
  182. $p->printLabelMsg($content);
  183. util::complete();
  184. }
  185. //取出今天有入库的花材 ssh 20221024
  186. public function actionStockIn()
  187. {
  188. $py = Yii::$app->request->get('py', '');
  189. $version = Yii::$app->request->get('version', 0);
  190. if ($version == 0) {
  191. util::fail('请先升级版本到1.0.70以上');
  192. }
  193. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  194. $mainId = $this->mainId ?? 0;
  195. $where['mainId'] = $mainId;
  196. if ($py) {
  197. $pyName = strtolower($py);
  198. $where['py'] = $pyName;
  199. }
  200. $arr = StockInDayClass::getAllByCondition(['mainId' => $mainId], null, '*');
  201. if (empty($arr)) {
  202. util::fail('没有花材需要盘点');
  203. }
  204. $ids = array_column($arr, 'itemId');
  205. $ids = array_unique($ids);
  206. $where['id'] = ['in', $ids];
  207. $level = 1;
  208. $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';
  209. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  210. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  211. $data = ProductService::assembleData($classInfo, $itemInfoData, true);
  212. util::success($data);
  213. }
  214. //按分类列出所有花材
  215. public function actionShow()
  216. {
  217. $py = Yii::$app->request->get('py', '');
  218. $requestType = Yii::$app->request->get('requestType', 'common');
  219. //默认显示上架商品
  220. $status = Yii::$app->request->get('status', 1);
  221. //默认显示未删除商品
  222. $delStatus = Yii::$app->request->get('delStatus', 0);
  223. $book = Yii::$app->request->get('book', 0);
  224. //获取所有当前供货商的分类 (全部、常用)
  225. //根据分类组装分类下的花材信息
  226. //中央ID
  227. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  228. $where['mainId'] = $this->mainId;
  229. if ($py) {
  230. $pyName = strtolower($py);
  231. $where['py'] = $pyName;
  232. }
  233. //改价和全部有库存花材列表
  234. if ($requestType == 'changePrice' || $requestType == 'hasStockList') {
  235. $where['stock>'] = 0;
  236. }
  237. if (!empty($status)) {
  238. $where['status'] = $status;
  239. }
  240. if ($requestType == 'book') {
  241. unset($where['status']);
  242. }
  243. $where['delStatus'] = $delStatus;
  244. //客户等级
  245. $customId = Yii::$app->request->get('customId', 0);
  246. $level = 1;
  247. if (!empty($customId)) {
  248. $custom = CustomClass::getById($customId, true);
  249. $level = $custom->level ?? 1;
  250. }
  251. $itemInfoData = [];
  252. if ($requestType == 'hasStockList') {
  253. //有库存的花材列表
  254. $field = 'id,py,name,classId,itemId,price,skPrice,hjPrice,stock';
  255. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  256. } elseif ($requestType == 'changePrice') {
  257. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,hjPrice,discountPrice,hjDiscountPrice,skDiscountPrice,skMore,hjAddPrice,addPrice,variety,price,hjPrice,stock,onStock,presell,frontHide,reachNum,reachNumDiscount';
  258. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  259. $itemInfoData = ProductClass::changePriceGroup($itemInfoData, $level);
  260. } elseif ($requestType == 'changeStock') {
  261. //修改库存列表
  262. $field = 'id,py,cover,name,cost,itemId,status,classId,addPrice,skPrice,discountPrice,skMore,variety,price,stock,frontHide';
  263. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  264. $itemInfoData = ProductClass::changeStockGroup($itemInfoData, $level);
  265. } elseif ($requestType == 'kd') {
  266. //开单的花材列表
  267. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,hjPrice,hjDiscountPrice,skDiscountPrice,discountPrice,stock,weight,stockWarning,presell,ratioType,smallUnit,smallRatio,bigUnit,ratio,frontHide,reachNum,reachNumDiscount';
  268. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  269. $itemInfoData = ProductClass::kdItemGroup($itemInfoData, $level);
  270. } elseif ($requestType == 'book') {
  271. //预订花材列表
  272. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,hjPrice,hjDiscountPrice,skDiscountPrice,discountPrice,stock,weight,stockWarning,presell,ratioType,smallUnit,smallRatio,bigUnit,ratio,frontHide';
  273. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  274. $itemInfoData = ProductClass::bookItemGroup($itemInfoData, $level);
  275. } elseif ($requestType == 'itemList') {
  276. //花材列表
  277. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,hjPrice,discountPrice,hjDiscountPrice,skDiscountPrice,stock,onStock,actualSold,status,ratio,ratioType,presell,frontHide,auth,reachNum,reachNumDiscount';
  278. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  279. $itemInfoData = ProductClass::itemListGroup($itemInfoData, $level);
  280. } elseif ($requestType == 'outList') {
  281. //下架花材列表
  282. $where['status'] = 2;
  283. $where['delStatus'] = 0;
  284. $where['removeStatus'] = 0;
  285. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,bigUnit,smallUnit,ratio,ratioType,discountPrice,skMore,variety,price,stock,presell,status';
  286. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  287. $itemInfoData = ProductClass::simpleGroup($itemInfoData, $level);
  288. } elseif ($requestType == 'stopList') {
  289. //停用花材列表
  290. unset($where['status']);
  291. $where['delStatus'] = 1;
  292. $where['removeStatus'] = 0;
  293. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,bigUnit,smallUnit,ratio,ratioType,discountPrice,skMore,variety,price,stock,presell,status';
  294. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  295. $itemInfoData = ProductClass::simpleGroup($itemInfoData, $level);
  296. } elseif ($requestType == 'removeList') {
  297. //删除花材列表
  298. unset($where['status']);
  299. $where['delStatus'] = 1;
  300. $where['removeStatus'] = 1;
  301. $field = 'id,py,cover,name,cost,itemId,classId,addPrice,skPrice,bigUnit,smallUnit,ratio,ratioType,discountPrice,skMore,variety,price,stock,presell,status';
  302. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  303. $itemInfoData = ProductClass::simpleGroup($itemInfoData, $level);
  304. } elseif ($requestType == 'check') {
  305. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio,frontHide';
  306. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  307. $itemInfoData = ProductClass::checkItemGroup($itemInfoData, $level);
  308. } elseif ($requestType == 'break') {
  309. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio,frontHide';
  310. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  311. $itemInfoData = ProductClass::breakItemGroup($itemInfoData, $level);
  312. } elseif ($requestType == 'part') {
  313. //拆散花材
  314. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio';
  315. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  316. $itemInfoData = ProductClass::partItemGroup($itemInfoData, $level);
  317. } elseif ($requestType == 'stockOut') {
  318. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio,smallRatio,frontHide';
  319. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  320. $itemInfoData = ProductClass::stockOutGroup($itemInfoData, $level);
  321. } elseif ($requestType == 'cg') {
  322. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,presell,ratioType,smallUnit,bigUnit,ratio,weight,frontHide';
  323. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  324. $itemInfoData = ProductClass::cgItemGroup($itemInfoData, $level);
  325. } elseif ($requestType == 'warning') {
  326. $field = 'id,py,cover,name,cost,itemId,classId,skPrice,variety,price,discountPrice,stock,onStock,actualSold,presell,bigUnit,stockWarning,status,frontHide';
  327. $itemInfoData = Product::find()->where("mainId={$this->mainId}")
  328. ->andWhere('`stock`+`onStock`<`stockWarning`')
  329. ->andWhere("delStatus=0")
  330. ->select($field)->asArray()->all();
  331. $itemInfoData = ProductClass::warningGroup($itemInfoData, $level);
  332. } else {
  333. util::fail('没有数据');
  334. }
  335. $data = ProductService::assembleItemData($classInfo, $itemInfoData);
  336. util::success($data);
  337. }
  338. //取出今天有入库的花材 ssh 20221224
  339. public function actionGetStockIn()
  340. {
  341. $py = Yii::$app->request->get('py', '');
  342. $version = Yii::$app->request->get('version', 0);
  343. if ($version == 0) {
  344. util::fail('请先升级版本到1.0.70以上');
  345. }
  346. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  347. $mainId = $this->mainId ?? 0;
  348. $where['mainId'] = $mainId;
  349. if ($py) {
  350. $pyName = strtolower($py);
  351. $where['py'] = $pyName;
  352. }
  353. $arr = StockInDayClass::getAllByCondition(['mainId' => $mainId], null, '*');
  354. if (empty($arr)) {
  355. util::fail('没有花材需要盘点');
  356. }
  357. $ids = array_column($arr, 'itemId');
  358. $ids = array_unique($ids);
  359. $where['id'] = ['in', $ids];
  360. $level = 1;
  361. $field = 'id,py,cover,name,itemId,classId,variety,price,discountPrice,stock,stockWarning,presell,ratioType,smallUnit,bigUnit,ratio';
  362. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  363. $itemInfoData = ProductClass::checkItemGroup($itemInfoData, $level);
  364. $data = ProductService::assembleItemData($classInfo, $itemInfoData);
  365. util::success($data);
  366. }
  367. //按分类列出所有花材
  368. public function actionIndex()
  369. {
  370. $py = Yii::$app->request->get('py', '');
  371. $requestType = Yii::$app->request->get('requestType', 'common');
  372. //默认显示上架商品
  373. $status = Yii::$app->request->get('status', 1);
  374. //默认显示未删除商品
  375. $delStatus = Yii::$app->request->get('delStatus', 0);
  376. $warning = Yii::$app->request->get('warning', 0);
  377. //获取所有当前供货商的分类 (全部、常用)
  378. //根据分类组装分类下的花材信息
  379. //中央ID
  380. $classInfo = ItemClassClass::getGhsItemClassAll($this->mainId);
  381. $where['mainId'] = $this->mainId;
  382. if ($py) {
  383. $pyName = strtolower($py);
  384. $where['py'] = $pyName;
  385. }
  386. if ($requestType == 'hasStock') {
  387. //改价只显示库存大于0的
  388. $where['stock>'] = 0;
  389. }
  390. if (!empty($status)) {
  391. $where['status'] = $status;
  392. }
  393. $where['delStatus'] = $delStatus;
  394. //客户等级
  395. $customId = Yii::$app->request->get('customId', 0);
  396. $level = 1;
  397. if (!empty($customId)) {
  398. $custom = CustomClass::getById($customId, true);
  399. $level = $custom->level ?? 1;
  400. }
  401. if ($warning == 1) {
  402. //库存预警
  403. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell';
  404. $itemInfoData = Product::find()->where("mainId={$this->mainId}")
  405. ->andWhere('`stock`<`stockWarning`')
  406. ->andWhere("delStatus=0")
  407. ->select($field)->asArray()->all();
  408. } else {
  409. $field = 'id,py,cover,name,ratio,cost,addPrice,classId,skMore,skPrice,itemId,smallUnit,smallRatio,presellDate,bigUnit,smallUnit,ratioType,variety,price,stock,stockWarning,discountPrice,presell';
  410. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], $field);
  411. }
  412. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  413. $data = ProductService::assembleData($classInfo, $itemInfoData, true);
  414. util::success($data);
  415. }
  416. //所有花材分页和不分页
  417. public function actionList()
  418. {
  419. $where = [];
  420. $classId = Yii::$app->request->get('classId', 0);
  421. if (!empty($classId)) {
  422. $where['classId'] = $classId;
  423. }
  424. $name = Yii::$app->request->get('name', '');
  425. if (empty($name)) {
  426. $py = Yii::$app->request->get('py', '');
  427. $name = !empty($py) ? $py : '';
  428. }
  429. if (!empty($name)) {
  430. if (stringUtil::isLetter($name)) {
  431. $where['py'] = ['like', $name];
  432. } else {
  433. $where['name'] = ['like', $name];
  434. }
  435. }
  436. $status = Yii::$app->request->get('status', 0);
  437. if (!empty($status)) {
  438. $where['status'] = $status;
  439. }
  440. $delStatus = Yii::$app->request->get('delStatus', 0);
  441. if ($delStatus != 2) {
  442. $where['delStatus'] = $delStatus;
  443. }
  444. $where['mainId'] = $this->mainId;
  445. $productIds = Yii::$app->request->get('ids', '');
  446. if (!empty($productIds)) {
  447. $productIdArr = explode(',', $productIds);
  448. if (!empty($productIdArr)) {
  449. $where['id'] = ['in', $productIdArr];
  450. }
  451. }
  452. //输出标准会员价,即批发价,默认显示的是批发价
  453. $level = 1;
  454. $type = Yii::$app->request->get('type', '');
  455. $showPage = Yii::$app->request->get('showPage', 0);
  456. if ($showPage) {
  457. if ($type == 'waring') {
  458. $pro = Product::find()->where("mainId={$this->mainId}")->andWhere('`stock`<`stockWarning`')->select('id')->asArray()->all();
  459. $lackIds = array_column($pro, 'id');
  460. if (empty($lackIds)) {
  461. util::success([]);
  462. }
  463. $lackIds = array_filter(array_unique($lackIds));
  464. $where['id'] = ['in', $lackIds];
  465. $respond = ProductClass::getList("*", $where, "actualSold desc");
  466. } else {
  467. $respond = ProductClass::getList("*", $where, "inTurn desc");
  468. }
  469. $respond['list'] = ProductClass::groupProductInfo($respond['list'], $level);
  470. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  471. util::success($respond);
  472. } else {
  473. if ($type == 'waring') {
  474. //库存预警 按库存从小到大排序
  475. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  476. } else {
  477. if (!empty($productIds)) {
  478. //优先按 $productIds 排序 "FIELD(`id`,4,5,6)"=>true
  479. $data = ProductClass::getAllByCondition($where, ["FIELD(`id`,$productIds)" => true, 'inTurn' => SORT_DESC], "*");
  480. } else {
  481. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  482. }
  483. }
  484. $data = ProductClass::groupProductInfo($data, $level);
  485. $data = ProductClass::groupClassName($this->sjId, $data);
  486. if ($type == 'waring') {
  487. //库存预警
  488. $newData = [];
  489. foreach ($data as $v) {
  490. if ($v['stock'] <= $v['stockWarning']) {
  491. $newData[] = $v;
  492. }
  493. }
  494. util::success(['list' => $newData]);
  495. }
  496. util::success(['list' => $data]);
  497. }
  498. }
  499. //所有花材
  500. public function actionAll()
  501. {
  502. $where = [];
  503. $where['mainId'] = $this->mainId;
  504. $classId = Yii::$app->request->get('classId', 0);
  505. if (!empty($classId)) {
  506. $where['classId'] = $classId;
  507. }
  508. $status = Yii::$app->request->get('status', 0);
  509. if (!empty($status)) {
  510. $where['status'] = $status;
  511. }
  512. $delStatus = Yii::$app->request->get('delStatus', 0);
  513. if ($delStatus != 2) {
  514. $where['delStatus'] = $delStatus;
  515. }
  516. //输出标准会员价,即批发价,默认显示的是批发价
  517. $level = 1;
  518. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  519. $data = ProductClass::groupProductInfo($data, $level);
  520. util::success(['list' => $data]);
  521. }
  522. //批量改价
  523. public function actionBatchChangePrice()
  524. {
  525. $shop = $this->shop;
  526. $join = $shop->join ?? 0;
  527. $default = $shop->default ?? 0;
  528. if ($join == 0 && $default == 0) {
  529. util::fail('当前直营分店,只能回总店修改价格');
  530. }
  531. $shopAdmin = $this->shopAdmin;
  532. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  533. util::fail('超管才能修改');
  534. }
  535. $shop = $this->shop;
  536. $default = $shop->default ?? 0;
  537. $adminId = $this->adminId ?? 0;
  538. //normal常规改价,cg采购改价
  539. $changeType = Yii::$app->request->post('changeType', 'normal');
  540. $targetId = Yii::$app->request->post('targetId', 0);
  541. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  542. $post = Yii::$app->request->post();
  543. $data = $post['data'] ?? '';
  544. if (empty($data)) {
  545. util::fail('没有修改');
  546. }
  547. $arr = json_decode($data, true);
  548. if (empty($arr)) {
  549. util::fail('没有修改哦');
  550. }
  551. $appVersion = $post['appVersion'] ?? 0;
  552. if ($appVersion != 1) {
  553. util::fail('请先升级应用');
  554. }
  555. $connection = Yii::$app->db;
  556. $transaction = $connection->beginTransaction();
  557. try {
  558. $cg = PurchaseOrderClass::getLockById($targetId);
  559. if (!empty($cg)) {
  560. if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
  561. util::fail('不是您的采购单');
  562. }
  563. //批量改价同时入库
  564. if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_SENDING) {
  565. $staff = $this->shopAdmin;
  566. $data = [];
  567. $data['staffId'] = $staff->id ?? 0;
  568. $data['staffName'] = $staff->name ?? '';
  569. $data['adminId'] = $this->adminId ?? 0;
  570. PurchaseOrderClass::putIn($cg, $data);
  571. }
  572. }
  573. $ids = array_column($arr, 'id');
  574. $ids = array_unique(array_filter($ids));
  575. if (empty($ids)) {
  576. util::fail('请选择花材');
  577. }
  578. $sjId = $shop->sjId ?? 0;
  579. $chainShopList = [];
  580. if ($default == 1) {
  581. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  582. }
  583. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  584. foreach ($arr as $product) {
  585. $productId = $product['id'];
  586. $productInfo = $productList[$productId] ?? [];
  587. if (empty($productInfo)) {
  588. util::fail('没有找到花材');
  589. }
  590. $productName = $productInfo->name ?? '';
  591. if (isset($product['id']) == false) {
  592. util::fail('没有花材');
  593. }
  594. if (isset($product['price']) == false || is_numeric($product['price']) == false || $product['price'] < 0) {
  595. util::fail('请填写' . $productName . '的批发价');
  596. }
  597. $price = $product['price'];
  598. if (isset($product['skPrice']) == false || is_numeric($product['skPrice']) == false || $product['skPrice'] < 0) {
  599. util::fail('请填写' . $productName . '的零售价');
  600. }
  601. $skPrice = $product['skPrice'];
  602. if (isset($product['hjPrice']) == false || is_numeric($product['hjPrice']) == false || $product['hjPrice'] < 0) {
  603. util::fail('请填写' . $productName . '的会员价');
  604. }
  605. $hjPrice = $product['hjPrice'] ?? 0;
  606. if (isset($productInfo->mainId) == false || $productInfo->mainId != $this->mainId) {
  607. util::fail('无法访问');
  608. }
  609. if ($hjPrice > $price) {
  610. util::fail($productName . ' 批发价要大于会员价');
  611. }
  612. if ($price > $skPrice) {
  613. util::fail($productName . ' 零售价要大于批发价');
  614. }
  615. $ptItemId = $productInfo->itemId;
  616. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $hjPrice, $changeParams);
  617. //在首店修改需要同步到所有直营店
  618. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  619. foreach ($chainShopList as $chain) {
  620. if ($chain->id == $shop->id) {
  621. continue;
  622. }
  623. $chainMainId = $chain->mainId ?? 0;
  624. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  625. $changeParams2 = ['staffId' => $staff->id, 'staffName' => $staff->name, 'changeType' => $changeType, 'targetId' => $targetId];
  626. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $hjPrice, $changeParams2);
  627. }
  628. }
  629. }
  630. $mainId = $shop->mainId ?? 0;
  631. if (!empty($mainId)) {
  632. //提醒零售收银台界面要刷新
  633. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  634. if (!empty($staffList)) {
  635. foreach ($staffList as $staff) {
  636. $staffId = $staff->id ?? 0;
  637. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  638. Yii::$app->redis->executeCommand('SET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  639. }
  640. }
  641. }
  642. $transaction->commit();
  643. util::complete('操作成功');
  644. } catch (\Exception $e) {
  645. $transaction->rollBack();
  646. Yii::info("修改失败原因:" . $e->getMessage());
  647. util::fail('修改失败');
  648. }
  649. }
  650. //多个花材同时改成一个价 ssh 20231003
  651. public function actionManyItemChangeOnePrice()
  652. {
  653. $shop = $this->shop;
  654. $join = $shop->join ?? 0;
  655. $default = $shop->default ?? 0;
  656. if ($join == 0 && $default == 0) {
  657. util::fail('当前直营分店,只能回总店修改价格');
  658. }
  659. $shopAdmin = $this->shopAdmin;
  660. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  661. util::fail('超管才能修改');
  662. }
  663. $post = Yii::$app->request->post();
  664. $idsList = $post['ids'] ?? '';
  665. $ids = json_decode($idsList, true);
  666. if (empty($ids)) {
  667. util::fail('请选择花材');
  668. }
  669. $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  670. if (empty($list)) {
  671. util::fail('请选择花材哦');
  672. }
  673. $mainId = $this->mainId;
  674. $adminId = $this->adminId ?? 0;
  675. $shop = $this->shop;
  676. $default = $shop->default ?? 0;
  677. $shopAdmin = $this->shopAdmin;
  678. $price = $post['price'] ?? 0;
  679. if (is_numeric($price) == false) {
  680. util::fail('批发价填写错误');
  681. }
  682. $skPrice = $post['skPrice'] ?? 0;
  683. if (is_numeric($skPrice) == false) {
  684. util::fail('散客价填写错误');
  685. }
  686. $hjPrice = $post['hjPrice'] ?? 0;
  687. if (is_numeric($hjPrice) == false) {
  688. util::fail('会员价填写错误');
  689. }
  690. if ($hjPrice > $price) {
  691. util::fail('会员价不能大于批发价');
  692. }
  693. if ($price > $skPrice) {
  694. util::fail('批发价不能大于零售价');
  695. }
  696. $sjId = $shop->sjId ?? 0;
  697. $chainShopList = [];
  698. if ($default == 1) {
  699. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  700. }
  701. //normal常规改价,cg采购改价
  702. $changeType = Yii::$app->request->post('changeType', 'normal');
  703. $targetId = 0;
  704. $changeParams = ['staffId' => $shopAdmin->id, 'staffName' => $shopAdmin->name, 'changeType' => $changeType, 'targetId' => $targetId];
  705. $connection = Yii::$app->db;
  706. $transaction = $connection->beginTransaction();
  707. try {
  708. foreach ($list as $product) {
  709. if ($product->mainId != $mainId) {
  710. util::fail('修改失败,不是你的花材');
  711. }
  712. $ptItemId = $product->itemId;
  713. ProductClass::changeSinglePrice($shop, $ptItemId, $price, $skPrice, $hjPrice, $changeParams);
  714. //在首店修改需要同步到所有直营店
  715. if ($default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  716. if (!empty($chainShopList)) {
  717. foreach ($chainShopList as $chain) {
  718. if ($chain->id == $shop->id) {
  719. continue;
  720. }
  721. $chainMainId = $chain->mainId ?? 0;
  722. $staff = ShopAdminClass::getByCondition(['mainId' => $chainMainId, 'adminId' => $adminId], true);
  723. $changeParams2 = ['staffId' => $staff->id, 'staffName' => $staff->name, 'changeType' => $changeType, 'targetId' => $targetId];
  724. ProductClass::changeSinglePrice($chain, $ptItemId, $price, $skPrice, $hjPrice, $changeParams2);
  725. }
  726. }
  727. }
  728. }
  729. if (!empty($mainId)) {
  730. //提醒零售收银台界面要刷新
  731. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $mainId], null, '*', null, true);
  732. if (!empty($staffList)) {
  733. foreach ($staffList as $staff) {
  734. $staffId = $staff->id ?? 0;
  735. Yii::$app->redis->executeCommand('SET', ['cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  736. Yii::$app->redis->executeCommand('SET', ['ghs_cashier_' . $mainId . '_' . $staffId . '_may_refresh', 'yes']);
  737. }
  738. }
  739. }
  740. $transaction->commit();
  741. util::complete('修改成功');
  742. } catch (\Exception $e) {
  743. $transaction->rollBack();
  744. Yii::info("修改失败原因:" . $e->getMessage());
  745. util::fail('修改失败');
  746. }
  747. }
  748. //改价
  749. public function actionChangePrice()
  750. {
  751. $shopAdmin = $this->shopAdmin;
  752. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  753. util::fail('超管才能修改');
  754. }
  755. $productId = Yii::$app->request->post('productId', 0);
  756. $productInfo = ProductClass::getById($productId, true);
  757. if (empty($productInfo)) {
  758. util::fail("花材不存在");
  759. }
  760. if ($productInfo->mainId != $this->mainId) {
  761. util::fail('没有权限');
  762. }
  763. $price = Yii::$app->request->post('price', '');
  764. if (is_numeric($price) && $price > 0) {
  765. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  766. } else {
  767. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  768. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  769. }
  770. util::success(['price' => $price]);
  771. }
  772. //恢复自动调价
  773. public function actionAutoPrice()
  774. {
  775. $productId = Yii::$app->request->post('productId', 0);
  776. $productData = ProductClass::getById($productId);
  777. if (!$productData) {
  778. util::fail("花材不存在");
  779. }
  780. //2021.04.06改为用product中的成本价+加价
  781. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  782. ProductClass::changeSinglePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  783. util::complete("操作成功");
  784. }
  785. //修改加价
  786. public function actionChangeAddPrice()
  787. {
  788. $productId = Yii::$app->request->post('productId', 0);
  789. $addPrice = Yii::$app->request->post('addPrice', '');
  790. if ($addPrice < 0) {
  791. util::fail("加价不能小于0");
  792. }
  793. $productData = ProductClass::getProductData($productId, $this->mainId);
  794. if (!$productData) {
  795. util::fail("花材不存在");
  796. }
  797. ProductClass::changeAddPrice($productId, $addPrice);
  798. util::complete();
  799. }
  800. //批量修改更多,包括排序、重量和保质期 ssh 20211211
  801. public function actionBatchChangeMore()
  802. {
  803. $shop = $this->shop;
  804. $default = $shop->default ?? 0;
  805. if ($default == 0) {
  806. util::fail('当前直营分店,请返回总店修改');
  807. }
  808. $shopAdmin = $this->shopAdmin;
  809. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  810. util::fail('超管才能操作');
  811. }
  812. $addData = Yii::$app->request->post('addData', '');
  813. if (empty($addData)) {
  814. util::fail('没有花材');
  815. }
  816. $itemData = json_decode($addData, true);
  817. if (is_array($itemData) == false) {
  818. util::fail('没有花材...');
  819. }
  820. $ids = array_column($itemData, 'id');
  821. $infoList = ProductClass::getByIds($ids, null, 'id');
  822. if (empty($infoList)) {
  823. util::fail('没有花材');
  824. }
  825. foreach ($infoList as $info) {
  826. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  827. util::fail('不是你的花材不能修改');
  828. }
  829. }
  830. //总店修改同步到所有门店
  831. $sjId = $shop->sjId ?? 0;
  832. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  833. foreach ($itemData as $key => $data) {
  834. $id = $data['id'] ?? 0;
  835. unset($data['id']);
  836. ProductClass::updateById($id, $data);
  837. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  838. if (empty($ptItemId)) {
  839. continue;
  840. }
  841. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  842. foreach ($chainShopList as $chainShop) {
  843. $currentMainId = $chainShop->mainId ?? 0;
  844. if ($currentMainId == $shop->mainId) {
  845. //前面已经修改过了,不需要重复修改
  846. continue;
  847. }
  848. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  849. }
  850. }
  851. }
  852. util::complete('修改成功');
  853. }
  854. //批量修改加价 ssh 20211211
  855. public function actionBatchChangeAddPrice()
  856. {
  857. $shop = $this->shop;
  858. $join = $shop->join ?? 0;
  859. $default = $shop->default ?? 0;
  860. if ($join == 0 && $default == 0) {
  861. util::fail('当前直营分店,请切回总店操作');
  862. }
  863. $shopAdmin = $this->shopAdmin;
  864. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  865. util::fail('超管才能操作');
  866. }
  867. $post = Yii::$app->request->post();
  868. $addData = $post['addData'] ?? '';
  869. if (empty($addData)) {
  870. util::fail('没有花材');
  871. }
  872. $itemData = json_decode($addData, true);
  873. if (is_array($itemData) == false) {
  874. util::fail('没有花材...');
  875. }
  876. $ids = array_column($itemData, 'id');
  877. $infoList = ProductClass::getByIds($ids, null, 'id');
  878. if (empty($infoList)) {
  879. util::fail('没有花材');
  880. }
  881. foreach ($infoList as $info) {
  882. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  883. util::fail('不能修改别人的花材');
  884. }
  885. }
  886. $appVersion = $post['appVersion'] ?? 0;
  887. if ($appVersion != 1) {
  888. util::fail('请先升级应用');
  889. }
  890. //总店修改同步到所有门店
  891. $sjId = $shop->sjId ?? 0;
  892. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  893. foreach ($itemData as $key => $data) {
  894. $id = $data['id'] ?? 0;
  895. unset($data['id']);
  896. ProductClass::updateById($id, $data);
  897. $ptItemId = $infoList[$id]['itemId'] ?? 0;
  898. if (empty($ptItemId)) {
  899. continue;
  900. }
  901. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  902. foreach ($chainShopList as $chainShop) {
  903. if (isset($chainShop->join) && $chainShop->join == 1) {
  904. continue;
  905. }
  906. $currentMainId = $chainShop->mainId ?? 0;
  907. if ($currentMainId == $shop->mainId) {
  908. //前面已经修改过了,不需要重复修改
  909. continue;
  910. }
  911. ProductClass::updateByCondition(['mainId' => $currentMainId, 'itemId' => $ptItemId], $data);
  912. }
  913. }
  914. }
  915. util::complete('修改成功');
  916. }
  917. public function actionUpdate()
  918. {
  919. $shopAdmin = $this->shopAdmin;
  920. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  921. util::fail('超管才能操作');
  922. }
  923. $shop = $this->shop;
  924. $join = $shop->join ?? 0;
  925. $default = $shop->default ?? 0;
  926. if ($join == 0 && $default == 0) {
  927. //因为盘点不能将花材盘成0,所以这边放出入口允许创始人在分店修改分店的花材库存
  928. if ($shopAdmin->super != 1) {
  929. util::fail('请先开通超管权限');
  930. }
  931. }
  932. $post = Yii::$app->request->post();
  933. $discountPrice = $post['discountPrice'] ?? 0;
  934. if ($discountPrice > 0) {
  935. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  936. $addPrice = $post['addPrice'] ?? 0;
  937. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  938. $skMore = $post['skMore'] ?? 0;
  939. $diff = bcsub($addPrice, $hjAddPrice, 2);
  940. if ($diff > 0 && $discountPrice > $diff) {
  941. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  942. } else {
  943. $hjDiscountPrice = $discountPrice;
  944. }
  945. $diff = bcsub($skMore, $addPrice, 2);
  946. if ($diff > 0) {
  947. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  948. } else {
  949. $skDiscountPrice = $discountPrice;
  950. }
  951. $post['skDiscountPrice'] = $skDiscountPrice;
  952. $post['hjDiscountPrice'] = $hjDiscountPrice;
  953. }
  954. $reachNum = $post['reachNum'] ?? 0;
  955. $reachNumDiscount = $post['reachNumDiscount'] ?? 0;
  956. if ($reachNum > 0 && $reachNumDiscount > 0) {
  957. util::fail('特价和满减不能同时进行');
  958. }
  959. }
  960. $post['adminId'] = $this->adminId;
  961. $post['staffId'] = $this->shopAdmin->id;
  962. $post['staffName'] = $this->shopAdmin->name;
  963. ProductClass::updateProduct($post, $this->shop);
  964. util::complete();
  965. }
  966. //上下架状态控制 ssh 20210713
  967. public function actionStatusUpdate()
  968. {
  969. $post = Yii::$app->request->post();
  970. $shopAdmin = $this->shopAdmin;
  971. $roleId = $shopAdmin['roleId'] ?? 0;
  972. $role = AdminRoleClass::getById($roleId);
  973. $roleName = $role['roleName'] ?? '';
  974. if ($roleName == '员工') {
  975. util::fail('无法操作哦');
  976. }
  977. $id = $post['id'] ?? 0;
  978. $status = $post['status'] ?? 1;
  979. $product = ProductClass::getById($id, true);
  980. if ($product->mainId != $this->mainId) {
  981. util::fail('无法操作');
  982. }
  983. $product->status = $status;
  984. if ($status == 2) {
  985. $product->discountPrice = 0;
  986. }
  987. $product->save();
  988. util::complete();
  989. }
  990. //花材展示状态控制 ssh 20231113
  991. public function actionDelStatusUpdate()
  992. {
  993. $post = Yii::$app->request->post();
  994. $shop = $this->shop;
  995. $join = $shop->join ?? 0;
  996. $default = $shop->default ?? 0;
  997. if ($join == 0 && $default == 0) {
  998. util::fail('请在首店操作');
  999. }
  1000. $id = $post['id'] ?? 0;
  1001. $delStatus = $post['delStatus'] ?? 0;
  1002. $product = ProductClass::getById($id, true);
  1003. if ($product->mainId != $this->mainId) {
  1004. util::fail('不是你的花材哦!!');
  1005. }
  1006. $product->delStatus = $delStatus;
  1007. if ($delStatus == 1) {
  1008. $product->stock = 0;
  1009. $product->status = 2;
  1010. }
  1011. $product->save();
  1012. $ptItemId = $product->itemId ?? 0;
  1013. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1014. //总店修改同步到所有门店
  1015. $sjId = $shop->sjId ?? 0;
  1016. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1017. foreach ($chainShopList as $chainShop) {
  1018. $chainShopId = $chainShop->id ?? 0;
  1019. if (isset($chainShop->join) && $chainShop->join == 1) {
  1020. continue;
  1021. }
  1022. if ($chainShopId == $shop->id) {
  1023. continue;
  1024. }
  1025. $chainMainId = $chainShop->mainId ?? 0;
  1026. $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
  1027. if (!empty($chainProduct)) {
  1028. $chainProduct->delStatus = $delStatus;
  1029. if ($delStatus == 1) {
  1030. $chainProduct->stock = 0;
  1031. $chainProduct->status = 2;
  1032. }
  1033. $chainProduct->save();
  1034. }
  1035. }
  1036. }
  1037. util::complete();
  1038. }
  1039. //详情 shish 20240115
  1040. public function actionDetail()
  1041. {
  1042. $id = Yii::$app->request->get('id', 0);
  1043. $source = Yii::$app->request->get('source', '');
  1044. $respond = ProductClass::getItemInfo($id);
  1045. $ptItemId = $respond['itemId'] ?? 0;
  1046. $ptItemInfo = PtItemClass::getById($ptItemId);
  1047. $respond['ptItemInfo'] = $ptItemInfo;
  1048. //通知提醒抹除
  1049. if ($source == 'notify' && $respond['mainId'] == $this->mainId) {
  1050. $staff = $this->shopAdmin;
  1051. $staffId = $this->shopAdminId;
  1052. $notify = NotifyClass::getByCondition(['staffId' => $staffId, 'type' => 1, 'targetId' => $id], true);
  1053. if (!empty($notify)) {
  1054. if ($notify->read == 0) {
  1055. $notify->read = 1;
  1056. $notify->save();
  1057. $notifyNum = $staff->notifyNum;
  1058. if ($notifyNum > 0) {
  1059. $notifyNum--;
  1060. $staff->notifyNum = $notifyNum;
  1061. $staff->save();
  1062. }
  1063. }
  1064. }
  1065. }
  1066. //重复花材
  1067. $repeatList = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'itemId' => $ptItemId, 'delStatus' => 0], null, '*', null, true);
  1068. $repeatId = 0;
  1069. if (!empty($repeatList)) {
  1070. foreach ($repeatList as $repeat) {
  1071. if ($repeat->id != $id) {
  1072. $repeatId = $repeat->id;
  1073. }
  1074. }
  1075. }
  1076. $respond['repeatId'] = $repeatId;
  1077. $cpItemList = CpItemClass::getAllByCondition(['productId' => $id], null, '*', 'cpId');
  1078. $cpInfo = [];
  1079. if (!empty($cpItemList)) {
  1080. $cpIds = array_column($cpItemList, 'cpId');
  1081. $cpList = CpClass::getByIds($cpIds);
  1082. if (!empty($cpList)) {
  1083. foreach ($cpList as $key => $val) {
  1084. $id = $val['id'] ?? 0;
  1085. $name = $val['name'] ?? '';
  1086. $cpInfo[] = ['id' => $id, 'name' => $name];
  1087. }
  1088. }
  1089. }
  1090. $respond['cpInfo'] = $cpInfo;
  1091. util::success($respond);
  1092. }
  1093. //新增花材 2021.6.21
  1094. public function actionAdd()
  1095. {
  1096. $post = Yii::$app->request->post();
  1097. //临时兼容,2023.7.1后可以删除
  1098. $appVersion = $post['appVersion'] ?? 0;
  1099. if ($appVersion != 1) {
  1100. util::fail('请先升级应用');
  1101. }
  1102. $discountPrice = $post['discountPrice'] ?? 0;
  1103. if ($discountPrice > 0) {
  1104. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  1105. $addPrice = $post['addPrice'] ?? 0;
  1106. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  1107. $skMore = $post['skMore'] ?? 0;
  1108. $diff = bcsub($addPrice, $hjAddPrice, 2);
  1109. if ($diff > 0 && $discountPrice > $diff) {
  1110. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  1111. } else {
  1112. $hjDiscountPrice = $discountPrice;
  1113. }
  1114. $diff = bcsub($skMore, $addPrice, 2);
  1115. if ($diff > 0) {
  1116. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  1117. } else {
  1118. $skDiscountPrice = $discountPrice;
  1119. }
  1120. $post['skDiscountPrice'] = $skDiscountPrice;
  1121. $post['hjDiscountPrice'] = $hjDiscountPrice;
  1122. }
  1123. }
  1124. $shop = $this->shop;
  1125. $join = $shop->join ?? 0;
  1126. $default = $shop->default ?? 0;
  1127. if ($join == 0 && $default == 0) {
  1128. util::fail('当前直营分店,请切回总店添加花材');
  1129. }
  1130. $shopAdmin = $this->shopAdmin;
  1131. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1132. util::fail('管理员才能添加花材');
  1133. }
  1134. $post['sjId'] = $this->sjId;
  1135. $post['adminId'] = $this->adminId;
  1136. $post['mainId'] = $this->mainId;
  1137. $price = $post['price'] ?? 0;
  1138. $addPrice = $post['addPrice'] ?? 0;
  1139. if ($addPrice > $price) {
  1140. util::fail('加价不能大于售价');
  1141. }
  1142. $weight = isset($post['weight']) && is_numeric($post['weight']) ? $post['weight'] : 0;
  1143. if ($weight <= 0) {
  1144. util::fail('请填写重量,最小0.1');
  1145. }
  1146. $post['staffName'] = $shopAdmin->name ?? '';
  1147. ProductClass::addProduct($post, $this->shop);
  1148. util::complete('添加成功');
  1149. }
  1150. //获取拼音缩写 ssh 20210707
  1151. public function actionPy()
  1152. {
  1153. $name = Yii::$app->request->get('name');
  1154. $py = stringUtil::py($name);
  1155. util::success(['py' => $py]);
  1156. }
  1157. //(客户端-批发商)复制花材
  1158. public function actionClone()
  1159. {
  1160. $post = Yii::$app->request->post();
  1161. $appVersion = $post['appVersion'] ?? 0;
  1162. if ($appVersion != 1) {
  1163. util::fail('请先升级应用');
  1164. }
  1165. $discountPrice = $post['discountPrice'] ?? 0;
  1166. if ($discountPrice > 0) {
  1167. if (isset($post['hjDiscountPrice']) == false || empty($post['hjDiscountPrice'])) {
  1168. $addPrice = $post['addPrice'] ?? 0;
  1169. $hjAddPrice = $post['hjAddPrice'] ?? 0;
  1170. $skMore = $post['skMore'] ?? 0;
  1171. $diff = bcsub($addPrice, $hjAddPrice, 2);
  1172. if ($diff > 0 && $discountPrice > $diff) {
  1173. $hjDiscountPrice = bcsub($discountPrice, $diff, 2);
  1174. } else {
  1175. $hjDiscountPrice = $discountPrice;
  1176. }
  1177. $diff = bcsub($skMore, $addPrice, 2);
  1178. if ($diff > 0) {
  1179. $skDiscountPrice = bcadd($diff, $discountPrice, 2);
  1180. } else {
  1181. $skDiscountPrice = $discountPrice;
  1182. }
  1183. $post['skDiscountPrice'] = $skDiscountPrice;
  1184. $post['hjDiscountPrice'] = $hjDiscountPrice;
  1185. }
  1186. }
  1187. $shop = $this->shop;
  1188. $join = $shop->join ?? 0;
  1189. $default = $shop->default ?? 0;
  1190. if ($join == 0 && $default == 0) {
  1191. util::fail('请在首店操作');
  1192. }
  1193. $shopAdmin = $this->shopAdmin;
  1194. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1195. util::fail('管理员才能添加花材');
  1196. }
  1197. $post['sjId'] = $this->sjId;
  1198. $post['adminId'] = $this->adminId;
  1199. $post['mainId'] = $this->mainId;
  1200. $post['staffName'] = $shopAdmin->name ?? '';
  1201. $price = $post['price'] ?? 0;
  1202. if (empty($price) || $price <= 0) {
  1203. util::fail('请填写批发价');
  1204. }
  1205. $skPrice = $post['skPrice'] ?? 0;
  1206. if (empty($skPrice) || $skPrice <= 0) {
  1207. util::fail('请填写零售价');
  1208. }
  1209. $hjPrice = $post['hjPrice'] ?? 0;
  1210. if (empty($hjPrice) || $hjPrice <= 0) {
  1211. util::fail('请填写会员价');
  1212. }
  1213. $addPrice = $post['addPrice'] ?? 0;
  1214. if ($addPrice > $price) {
  1215. util::fail('批发价的加价不能大于售价');
  1216. }
  1217. //复制标识
  1218. $post['copy'] = 1;
  1219. //去除无用的字段
  1220. unset($post['viewNum']);
  1221. unset($post['actualSold']);
  1222. unset($post['itemId']);
  1223. unset($post['id']);
  1224. unset($post['delStatus']);
  1225. ProductClass::addProduct($post, $this->shop);
  1226. util::complete('复制成功');
  1227. }
  1228. //下载价格表 ssh 20210922
  1229. public function actionGeneratePriceTable()
  1230. {
  1231. $level = Yii::$app->request->get('level', 0);
  1232. $respond = ProductService::generatePriceTable($this->sjId, $level, $this->mainId);
  1233. $file = $respond['file'] ?? '';
  1234. $shortFile = $respond['shortFile'] ?? '';
  1235. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1236. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1237. }
  1238. public function actionGenerateItemTable()
  1239. {
  1240. $level = Yii::$app->request->get('level', 0);
  1241. $respond = ProductService::generateItemTable($this->sjId, $level, $this->mainId);
  1242. $file = $respond['file'] ?? '';
  1243. $shortFile = $respond['shortFile'] ?? '';
  1244. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1245. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1246. }
  1247. //下载条形码 lqh 20211227
  1248. public function actionGenerateBarcodeTable()
  1249. {
  1250. $respond = ProductService::generateBarcodeTable($this->sjId, $this->shopId, $this->mainId);
  1251. $file = $respond['file'] ?? '';
  1252. $shortFile = $respond['shortFile'] ?? '';
  1253. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  1254. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  1255. }
  1256. //批量修改花材的分类 ssh 20230405
  1257. public function actionBatchModifyClass()
  1258. {
  1259. $post = Yii::$app->request->post();
  1260. $shop = $this->shop;
  1261. $join = $shop->join ?? 0;
  1262. $default = $shop->default ?? 0;
  1263. if ($join == 0 && $default == 0) {
  1264. util::fail('当前直营分店,请切回总店操作');
  1265. }
  1266. $classId = $post['classId'] ?? 0;
  1267. $class = ItemClassClass::getById($classId, true);
  1268. if (empty($class) || $class->mainId != $this->mainId) {
  1269. util::fail('请选择您自己的分类');
  1270. }
  1271. $connection = Yii::$app->db;
  1272. $transaction = $connection->beginTransaction();
  1273. try {
  1274. $className = $class->name ?? '';
  1275. $idsList = $post['ids'] ?? '';
  1276. $ids = json_decode($idsList, true);
  1277. if (empty($ids)) {
  1278. util::fail('请选择花材');
  1279. }
  1280. $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
  1281. if (empty($list)) {
  1282. util::fail('请选择花材');
  1283. }
  1284. $ptItemList = [];
  1285. foreach ($list as $item) {
  1286. $name = $item->name ?? '';
  1287. if ($item->mainId != $this->mainId) {
  1288. util::fail($name . '不是您的花材');
  1289. }
  1290. $item->classId = $classId;
  1291. $item->save();
  1292. $ptItemId = $item->itemId ?? 0;
  1293. $ptItemList[] = $ptItemId;
  1294. }
  1295. $shop = $this->shop;
  1296. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1297. //总店修改同步到所有门店
  1298. $sjId = $shop->sjId ?? 0;
  1299. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1300. foreach ($chainShopList as $chainShop) {
  1301. $chainShopId = $chainShop->id ?? 0;
  1302. if ($chainShopId == $shop->id) {
  1303. continue;
  1304. }
  1305. $chainManId = $chainShop->mainId ?? 0;
  1306. $chainAllClassList = ItemClassClass::getAllByCondition(['mainId' => $chainManId], null, '*', null, true);
  1307. if (!empty($chainAllClassList)) {
  1308. foreach ($chainAllClassList as $chainClass) {
  1309. $chainName = $chainClass->name ?? '';
  1310. $chainClassId = $chainClass->id ?? 0;
  1311. if ($chainName == $className) {
  1312. $chainProductList = Productclass::getAllByCondition(['mainId' => $chainManId, 'itemId' => ['in', $ptItemList]], null, '*', null, true);
  1313. if (!empty($chainProductList)) {
  1314. foreach ($chainProductList as $currentProduct) {
  1315. $currentProduct->classId = $chainClassId;
  1316. $currentProduct->save();
  1317. }
  1318. }
  1319. }
  1320. }
  1321. }
  1322. }
  1323. }
  1324. $transaction->commit();
  1325. util::complete('修改成功');
  1326. } catch (\Exception $e) {
  1327. $transaction->rollBack();
  1328. Yii::info("修改失败原因:" . $e->getMessage());
  1329. util::fail('修改失败');
  1330. }
  1331. }
  1332. }