ProductController.php 59 KB

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