ProductController.php 56 KB

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