ProductClass.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. namespace bizHd\product\classes;
  3. use biz\item\classes\PtItemClass;
  4. use bizHd\base\classes\BaseClass;
  5. use common\components\imgUtil;
  6. use common\components\noticeUtil;
  7. use common\components\sms;
  8. use common\components\util;
  9. use Yii;
  10. class ProductClass extends BaseClass
  11. {
  12. public static $baseFile = '\bizHd\product\models\Product';
  13. //花材列表用到的数据组合 ssh 20221225
  14. public static function itemListGroup($list, $level = 0)
  15. {
  16. //各个等级对应的price addPrice字段
  17. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  18. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  19. foreach ($list as $k => $v) {
  20. $shortCover = $v['cover'] ?? '';
  21. $cover = imgUtil::getPrefix() . 'hhb_small.png';
  22. if (!empty($shortCover)) {
  23. $parse = parse_url(self::groupImg($shortCover));
  24. if (empty($parse['query'])) {
  25. $cover = self::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_100,w_100";
  26. } else {
  27. $cover = self::groupImg($shortCover);
  28. }
  29. }
  30. $list[$k]['cover'] = $cover;
  31. //今日特价、会员价
  32. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($v, $level, $priceMap, $addPriceMap);
  33. if ($level == 0) {
  34. //零售做特价时显示的原价,不能删除
  35. if (isset($v['skPrice'])) {
  36. $list[$k]['prePrice'] = $v['skPrice'];
  37. }
  38. }
  39. if (isset($v['discountPrice']) && $v['discountPrice'] > 0) {
  40. //做特价花材时,花材列表和改价列表显示的会员价和零售价,不能删除
  41. $list[$k]['hjPrice'] = floatval($v['hjDiscountPrice']);
  42. $list[$k]['skPrice'] = floatval($v['skDiscountPrice']);
  43. }
  44. $list[$k]['price'] = $price;
  45. $cost = $v['cost'] ?? 0;
  46. if ($price > 0) {
  47. $profit = bcsub($price, $cost, 2);
  48. $mll = bcdiv($profit, $price, 3);
  49. $mll = bcmul($mll, 100);
  50. $mll = round($mll, 1);
  51. } else {
  52. $mll = 0;
  53. }
  54. $list[$k]['mll'] = $mll;
  55. }
  56. return $list;
  57. }
  58. public static function kdItemGroup($list, $level = 0)
  59. {
  60. //各个等级对应的price addPrice字段
  61. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  62. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  63. foreach ($list as $k => $v) {
  64. $shortCover = $v['cover'] ?? '';
  65. $cover = imgUtil::getPrefix() . 'hhb_small.png';
  66. if (!empty($shortCover)) {
  67. $parse = parse_url(self::groupImg($shortCover));
  68. if (empty($parse['query'])) {
  69. $cover = self::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_100,w_100";
  70. } else {
  71. $cover = self::groupImg($shortCover);
  72. }
  73. }
  74. $ratio = isset($v['ratio']) && $v['ratio'] > 0 ? $v['ratio'] : 1;
  75. $list[$k]['cover'] = $cover;
  76. $stockInfo = self::formatStock($v['stock'], $ratio);
  77. $list[$k]['bigNum'] = $stockInfo['bigNum'];
  78. $list[$k]['smallNum'] = $stockInfo['smallNum'];
  79. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($v, $level, $priceMap, $addPriceMap);
  80. $list[$k]['price'] = $price;
  81. $list[$k]['bigPrice'] = $price;
  82. $smallRatio = $v['smallRatio'] ?? 0;
  83. $smallPrice = bcdiv($price, $ratio, 2);
  84. $smallPrice = bcmul($smallPrice, $smallRatio, 2);
  85. $list[$k]['smallPrice'] = $smallPrice;
  86. }
  87. return $list;
  88. }
  89. //花店的花材转成批发店花材 ssh 20230308
  90. public static function hdToggleGhs($list, $ghsShop)
  91. {
  92. $ghsMainId = $ghsShop->mainId ?? 0;
  93. $ptItemIds = array_column($list, 'itemId');
  94. $ghsHasProduct = ProductClass::getAllByCondition(['mainId' => $ghsMainId, 'itemId' => ['in', $ptItemIds]], null, '*', 'itemId');
  95. $ghsSjId = $ghsShop->sjId ?? 0;
  96. $ghsShopId = $ghsShop->id ?? 0;
  97. $defaultClassId = \bizGhs\item\classes\ItemClassClass::getDefaultClassId($ghsMainId);
  98. if (empty($defaultClassId)) {
  99. util::fail('没有找到花材的默认分类');
  100. }
  101. foreach ($list as $k => $v) {
  102. $ptItemId = $v['itemId'] ?? 0;
  103. $hdProductId = $v['productId'] ?? 0;
  104. if (isset($ghsHasProduct[$ptItemId]) == false) {
  105. //不存在,则从平台新增
  106. $ptItem = PtItemClass::getById($ptItemId);
  107. unset($ptItem['addTime']);
  108. unset($ptItem['updateTime']);
  109. $productData = $ptItem;
  110. $productData['sjId'] = $ghsSjId;
  111. $productData['shopId'] = $ghsShopId;
  112. $productData['mainId'] = $ghsMainId;
  113. $productData['itemId'] = $ptItemId;
  114. $productData['classId'] = $defaultClassId;
  115. unset($productData['id']);
  116. $productData['rank'] = '';
  117. $cost = $v['cost'] ?? 0;
  118. $addPrice = $v['addPrice'] ?? 0;
  119. $productData['price'] = bcadd($cost, $addPrice, 2);
  120. $addProduct = ProductClass::addBaseData($productData);
  121. $ghsProductId = $addProduct->id ?? 0;
  122. } else {
  123. $ghsProductId = $ghsHasProduct[$ptItemId]['id'] ?? 0;
  124. }
  125. $list[$k]['productId'] = $ghsProductId;
  126. $list[$k]['hdProductId'] = $hdProductId;
  127. }
  128. return $list;
  129. }
  130. public static function addBaseData($data)
  131. {
  132. $respond = self::add($data, true);
  133. return $respond;
  134. }
  135. public static function adStockCheck($shop, $productList)
  136. {
  137. //阿东开5支老是会出现5扎情况跟踪
  138. $mainId = $shop->mainId ?? 0;
  139. Yii::info("mainId:" . $mainId);
  140. if (getenv('YII_ENV') == 'production') {
  141. if ($mainId != 742 && $mainId != 50) {
  142. return false;
  143. }
  144. } else {
  145. if ($mainId != 644) {
  146. return false;
  147. }
  148. }
  149. Yii::info('productList:' . json_encode($productList));
  150. if (!empty($productList)) {
  151. $adIds = [];
  152. foreach ($productList as $adItem) {
  153. if (isset($adItem['property']) && $adItem['property'] == 1 && $adItem['unitType'] == 0 && $adItem['num'] == 5) {
  154. $adIds[] = $adItem['productId'];
  155. Yii::info('productId:' . $adItem['productId']);
  156. }
  157. }
  158. if (!empty($adIds)) {
  159. $arr = self::getByIds($adIds);
  160. if (!empty($arr)) {
  161. foreach ($arr as $it) {
  162. Yii::info("num:" . $it['scanNum']);
  163. if (isset($it['scanNum']) && $it['scanNum'] > 0) {
  164. $name = $it['name'] ?? '';
  165. if (getenv('YII_ENV') == 'production') {
  166. if ($mainId == 742) {
  167. sms::freeSend('13531700113,' . $name, '刚刚,花材:{$var},5支被开成了5扎,请注意!!!');
  168. sms::freeSend('15220670540,' . $name, '刚刚,花材:{$var},5支被开成了5扎,请注意!!!');
  169. sms::freeSend('13531704009,' . $name, '刚刚,花材:{$var},5支被开成了5扎,请注意!!!');
  170. sms::freeSend('15766689130,' . $name, '刚刚,花材:{$var},5支被开成了5扎,请注意!!!');
  171. sms::freeSend('15280215347,' . $name, '刚刚,花材:{$var},5支被开成了5扎,请注意!!!');
  172. }
  173. if ($mainId == 50) {
  174. sms::freeSend('15280215347,' . $name, '刚刚,花材:{$var},5支被开成了5扎,请注意!!!');
  175. }
  176. } else {
  177. noticeUtil::push('刚刚,花材:' . $name . ',5支被开成了5扎,请注意!!!', '15280215347');
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. //花材的库存转换: 库存是小数,根据各个花材的unitNum, 计算出多少扎,多少支
  186. public static function formatStock($stock, $unitNum)
  187. {
  188. $pn = true; //正数
  189. if ($stock < 0) {
  190. $pn = false;
  191. $stock = abs($stock);
  192. }
  193. $bigNum = floor($stock); // 扎
  194. $stockArr = explode('.', $stock);
  195. $smallNum = 0; //支
  196. if (count($stockArr) === 2) {
  197. $smallNum = bcmul(($stock - $bigNum), $unitNum);
  198. }
  199. if (!$pn) {
  200. $bigNum *= -1;
  201. $smallNum *= -1;
  202. }
  203. return [
  204. 'bigNum' => $bigNum,
  205. 'smallNum' => $smallNum,
  206. ];
  207. }
  208. //通用花材列表数组组合
  209. public static function changePriceGroup($list, $level = 0)
  210. {
  211. //各个等级对应的price addPrice字段
  212. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  213. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  214. foreach ($list as $k => $v) {
  215. $shortCover = $v['cover'] ?? '';
  216. $cover = imgUtil::getPrefix() . 'hhb_small.png';
  217. if (!empty($shortCover)) {
  218. $parse = parse_url(self::groupImg($shortCover));
  219. if (empty($parse['query'])) {
  220. $cover = self::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_100,w_100";
  221. } else {
  222. $cover = self::groupImg($shortCover);
  223. }
  224. }
  225. $list[$k]['cover'] = $cover;
  226. $list[$k]['prePrice'] = $v['price'] ?? 0;
  227. //今日特价、会员价
  228. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($v, $level, $priceMap, $addPriceMap);
  229. if ($level == 0) {
  230. //零售做特价时显示的原价,不能删除
  231. if (isset($v['skPrice'])) {
  232. $list[$k]['prePrice'] = $v['skPrice'];
  233. }
  234. }
  235. if (isset($v['discountPrice']) && $v['discountPrice'] > 0) {
  236. //做特价花材时,花材列表和改价列表显示的会员价和零售价,不能删除
  237. $list[$k]['hjPrice'] = floatval($v['hjDiscountPrice']);
  238. $list[$k]['skPrice'] = floatval($v['skDiscountPrice']);
  239. }
  240. $list[$k]['price'] = $price;
  241. $cost = $v['cost'] ?? 0;
  242. $ml = 0;
  243. if ($price > 0) {
  244. $profit = bcsub($price, $cost, 2);
  245. $ml = $profit;
  246. $mll = bcdiv($profit, $price, 3);
  247. $mll = bcmul($mll, 100);
  248. $mll = round($mll, 1);
  249. } else {
  250. $mll = 0;
  251. }
  252. $list[$k]['mll'] = $mll;
  253. $list[$k]['ml'] = $ml;
  254. }
  255. return $list;
  256. }
  257. public static function breakItemGroup($list, $level = 0)
  258. {
  259. foreach ($list as $k => $v) {
  260. $shortCover = $v['cover'] ?? '';
  261. $ratio = isset($v['ratio']) && $v['ratio'] > 0 ? $v['ratio'] : 20;
  262. $stockInfo = self::formatStock($v['stock'], $ratio);
  263. $list[$k]['bigNum'] = $stockInfo['bigNum'];
  264. $list[$k]['smallNum'] = $stockInfo['smallNum'];
  265. $list[$k]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_100,w_100";
  266. }
  267. return $list;
  268. }
  269. public static function partItemGroup($list, $level = 0)
  270. {
  271. foreach ($list as $k => $v) {
  272. $shortCover = $v['cover'] ?? '';
  273. $ratio = isset($v['ratio']) && $v['ratio'] > 0 ? $v['ratio'] : 20;
  274. $stockInfo = self::formatStock($v['stock'], $ratio);
  275. $list[$k]['bigNum'] = $stockInfo['bigNum'];
  276. $list[$k]['smallNum'] = $stockInfo['smallNum'];
  277. $list[$k]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_100,w_100";
  278. }
  279. return $list;
  280. }
  281. public static function checkItemGroup($list, $level = 0)
  282. {
  283. foreach ($list as $k => $v) {
  284. $shortCover = $v['cover'] ?? '';
  285. $list[$k]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_100,w_100";
  286. $ratio = isset($v['ratio']) && $v['ratio'] > 0 ? $v['ratio'] : 1;
  287. $stockInfo = self::formatStock($v['stock'], $ratio);
  288. $list[$k]['bigNum'] = $stockInfo['bigNum'];
  289. $list[$k]['smallNum'] = $stockInfo['smallNum'];
  290. }
  291. return $list;
  292. }
  293. public static function cgItemGroup($list, $level = 0)
  294. {
  295. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  296. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  297. foreach ($list as $k => $v) {
  298. $shortCover = $v['cover'] ?? '';
  299. $list[$k]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_100,w_100";
  300. $ratio = isset($v['ratio']) && $v['ratio'] > 0 ? $v['ratio'] : 1;
  301. $stockInfo = self::formatStock($v['stock'], $ratio);
  302. $list[$k]['bigNum'] = $stockInfo['bigNum'];
  303. $list[$k]['smallNum'] = $stockInfo['smallNum'];
  304. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($v, $level, $priceMap, $addPriceMap);
  305. $list[$k]['price'] = $price;
  306. $list[$k]['bigPrice'] = $price;
  307. $smallRatio = $v['smallRatio'] ?? 0;
  308. $smallPrice = bcdiv($price, $ratio, 2);
  309. $smallPrice = bcmul($smallPrice, $smallRatio, 2);
  310. $list[$k]['smallPrice'] = $smallPrice;
  311. }
  312. return $list;
  313. }
  314. //通用花材列表数组组合
  315. public static function changeStockGroup($list, $level = 0)
  316. {
  317. //各个等级对应的price addPrice字段
  318. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  319. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  320. foreach ($list as $k => $v) {
  321. $shortCover = $v['cover'] ?? '';
  322. $cover = imgUtil::getPrefix() . 'hhb_small.png';
  323. if (!empty($shortCover)) {
  324. $parse = parse_url(self::groupImg($shortCover));
  325. if (empty($parse['query'])) {
  326. $cover = self::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_100,w_100";
  327. } else {
  328. $cover = self::groupImg($shortCover);
  329. }
  330. }
  331. $list[$k]['cover'] = $cover;
  332. $list[$k]['prePrice'] = $v['price'] ?? 0;
  333. //今日特价、会员价
  334. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($v, $level, $priceMap, $addPriceMap);
  335. //零售做特价时的原价
  336. if ($level == 0) {
  337. $currentAddPrice = $v['skMore'] ?? 0;
  338. $list[$k]['prePrice'] = bcadd($v['price'], $currentAddPrice, 2);
  339. }
  340. if (isset($v['discountPrice']) && $v['discountPrice'] > 0) {
  341. $currentAddPrice = $v['skMore'] ?? 0;
  342. $list[$k]['skPrice'] = bcadd($v['discountPrice'], $currentAddPrice, 2);
  343. }
  344. $list[$k]['price'] = $price;
  345. $cost = $v['cost'] ?? 0;
  346. $ml = 0;
  347. if ($price > 0) {
  348. $profit = bcsub($price, $cost, 2);
  349. $ml = $profit;
  350. $mll = bcdiv($profit, $price, 3);
  351. $mll = bcmul($mll, 100);
  352. $mll = round($mll, 1);
  353. } else {
  354. $mll = 0;
  355. }
  356. $list[$k]['mll'] = $mll;
  357. $list[$k]['ml'] = $ml;
  358. }
  359. return $list;
  360. }
  361. public static function stockOutGroup($list, $level = 0)
  362. {
  363. //各个等级对应的price addPrice字段
  364. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  365. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  366. foreach ($list as $k => $v) {
  367. $shortCover = $v['cover'] ?? '';
  368. $list[$k]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_100,w_100";
  369. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($v, $level, $priceMap, $addPriceMap);
  370. $list[$k]['price'] = $price;
  371. $list[$k]['bigPrice'] = $price;
  372. $smallRatio = $v['smallRatio'] ?? 0;
  373. $ratio = isset($v['ratio']) && $v['ratio'] > 0 ? $v['ratio'] : 20;
  374. $smallPrice = bcdiv($price, $ratio, 2);
  375. $smallPrice = bcmul($smallPrice, $smallRatio, 2);
  376. $list[$k]['smallPrice'] = $smallPrice;
  377. }
  378. return $list;
  379. }
  380. }