ProductController.php 57 KB

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