ProductController.php 58 KB

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