ProductController.php 53 KB

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