CustomClass.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace bizGhs\custom\classes;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\sj\classes\MerchantClass;
  5. use bizGhs\shop\classes\MainClass;
  6. use bizGhs\shop\classes\TotalDebtChangeClass;
  7. use common\components\business;
  8. use common\components\dict;
  9. use common\components\imgUtil;
  10. use common\components\noticeUtil;
  11. use common\components\util;
  12. use bizHd\base\classes\BaseClass;
  13. class CustomClass extends BaseClass
  14. {
  15. public static $baseFile = '\bizGhs\custom\models\Custom';
  16. const IS_DEBT_NO = 0;
  17. const IS_DEBT_YES = 1;
  18. const NOT_BLACK = 1;
  19. const IS_BLACK = 2;
  20. const LEVEL_SK = 0; //散客
  21. const LEVEL_PT = 1; //花店
  22. const LEVEL_HJ = 2; //同行
  23. const LEVEL_ZS = 3; //金店
  24. public static $levelMap = [
  25. self::LEVEL_SK => '散客',
  26. self::LEVEL_PT => '花店',
  27. self::LEVEL_HJ => '同行',
  28. self::LEVEL_ZS => '金店',
  29. ];
  30. public static $nickNameMap = [
  31. self::LEVEL_SK => '散客',
  32. self::LEVEL_PT => '花店',
  33. self::LEVEL_HJ => '同行',
  34. self::LEVEL_ZS => '金店',
  35. ];
  36. public static $levelPriceKeyMap = [
  37. self::LEVEL_SK => 'skPrice',
  38. self::LEVEL_PT => 'price',
  39. self::LEVEL_HJ => 'hjPrice',
  40. self::LEVEL_ZS => 'zsPrice',
  41. ];
  42. public static $levelAddPriceKeyMap = [
  43. self::LEVEL_SK => 'skAddPrice',
  44. self::LEVEL_PT => 'addPrice',
  45. self::LEVEL_HJ => 'hjAddPrice',
  46. self::LEVEL_ZS => 'zsAddPrice',
  47. ];
  48. //建立客户和供货商关系
  49. public static function build($ghsShopId, $hdShopId, $changeName = '')
  50. {
  51. return GhsClass::build($ghsShopId, $hdShopId, 2, $changeName);
  52. }
  53. //添加客户 ssh 2021.2.24
  54. public static function addCustom($data)
  55. {
  56. return self::add($data);
  57. }
  58. //获取客户信息 ssh 2021.2.4
  59. public static function getCustom($id)
  60. {
  61. $info = self::getById($id);
  62. if (empty($info)) {
  63. return $info;
  64. }
  65. $respond = self::groupBaseInfo([$info]);
  66. return current($respond);
  67. }
  68. public static function getCustomByIds($ids)
  69. {
  70. $list = self::getByIds($ids, null, 'id');
  71. return self::groupBaseInfo($list);
  72. }
  73. //整合头像等信息 ssh 2021.1.8
  74. public static function groupBaseInfo($list)
  75. {
  76. if (empty($list)) {
  77. return $list;
  78. }
  79. //获取客户信息
  80. $customSjIdList = array_unique(array_filter(array_column($list, 'sjId')));
  81. $sjInfo = MerchantClass::getByIds($customSjIdList, null, 'id');
  82. $levelMap = self::$levelMap;
  83. foreach ($list as $key => $val) {
  84. $customSjId = $val['sjId'];
  85. $currentInfo = isset($sjInfo[$customSjId]) ? $sjInfo[$customSjId] : [];
  86. $currentInfo = business::formatMerchantLogo($currentInfo);
  87. $smallAvatar = !empty($currentInfo['smallLogoUrl']) ? $currentInfo['smallLogoUrl'] : imgUtil::getPrefix() . '/hhb_small.png';
  88. $avatar = !empty($currentInfo['logoUrl']) ? $currentInfo['logoUrl'] : imgUtil::getPrefix() . '/hhb_small.png';
  89. $list[$key]['smallAvatar'] = $smallAvatar;
  90. $list[$key]['shortSmallAvatar'] = $currentInfo['shortSmallLogoUrl'];
  91. $list[$key]['avatar'] = $avatar;
  92. $list[$key]['shortAvatar'] = $currentInfo['shortLogoUrl'];
  93. $list[$key]['sn'] = $val['id'];
  94. $level = $val['level'] ?? 1;
  95. $levelName = $levelMap[$level] ?? '';
  96. $list[$key]['levelName'] = $levelName;
  97. }
  98. return $list;
  99. }
  100. //欠款权限开关 ssh 2021.1.8
  101. public static function debt($custom, $debt)
  102. {
  103. $id = isset($custom['id']) ? $custom['id'] : 0;
  104. self::updateById($id, ['debt' => $debt]);
  105. return true;
  106. }
  107. //采购查看权限的黑白名单设置 shizhongqi 2021.08.08
  108. public static function black($custom, $black)
  109. {
  110. $id = isset($custom['id']) ? $custom['id'] : 0;
  111. self::updateById($id, ['black' => $black]);
  112. return true;
  113. }
  114. //权限判断 ssh 2021.1.8
  115. public static function valid($custom, $shopId)
  116. {
  117. if (empty($custom)) {
  118. util::fail('没有找到客户');
  119. }
  120. if ($custom['ownShopId'] != $shopId) {
  121. util::fail('您没有权限操作此客户');
  122. }
  123. }
  124. //客户采购欠款金额增加 ssh 20220306
  125. public static function cgDebtAmountAdd($custom, $order, $bookOrder = false)
  126. {
  127. $amount = $order->remainDebtPrice ?? 0;
  128. $custom->isDebt = CustomClass::IS_DEBT_YES;
  129. $debtLimit = $custom->debtLimit ?? 0;
  130. if ($bookOrder == false) {
  131. if ($custom->debtAmount > $debtLimit) {
  132. util::fail("您已欠款{$custom->debtAmount},请先结账哦");
  133. }
  134. }
  135. $balance = bcadd($custom->debtAmount, $amount, 2);
  136. $custom->debtAmount = $balance;
  137. $custom->save();
  138. $relateId = $order->id ?? 0;
  139. $customId = $custom->id ?? 0;
  140. $orderSn = $order->orderSn ?? '';
  141. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  142. $event = '购买花材,单号:' . $orderSn;
  143. if ($bookOrder) {
  144. $event = '预订单多退少补,单号:' . $orderSn;
  145. }
  146. $change = [
  147. 'relateId' => $relateId,
  148. 'customId' => $customId,
  149. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  150. 'capitalType' => $capitalType,
  151. 'amount' => $amount,
  152. 'balance' => $balance,
  153. 'io' => 1,
  154. 'payWay' => $order->payWay,
  155. 'event' => $event,
  156. 'sjId' => $order->sjId,
  157. 'shopId' => $order->id,
  158. 'mainId' => $order->mainId,
  159. ];
  160. CustomDebtChangeClass::addChange($change);
  161. $mainId = $order->mainId ?? 0;
  162. $main = MainClass::getLockById($mainId);
  163. if (empty($main)) {
  164. util::fail('没有main信息');
  165. }
  166. $debt = $main->debt ?? 0;
  167. $remain = bcadd($debt, $amount, 2);
  168. $main->debt = $remain;
  169. $main->save();
  170. $event = $custom->name.' 购买花材,单号:' . $orderSn;
  171. $change['balance'] = $remain;
  172. $change['event'] = $event;
  173. TotalDebtChangeClass::addChange($change);
  174. }
  175. //客户结账欠款金额减少 ssh 20220306
  176. public static function clearDebtAmountReduce($custom, $ghs, $amount, $clear)
  177. {
  178. $debtAmount = $custom->debtAmount ?? 0.00;
  179. $remain = bcsub($debtAmount, $amount, 2);
  180. if ($remain < 0) {
  181. noticeUtil::push("客户欠款总金额:{$custom->debtAmount} 本次结账金额:{$amount} 客户ID:{$custom->id} 【里外金额有问题】", '15280215347');
  182. util::fail('收款失败,结账后欠款金额会出现负数');
  183. }
  184. $custom->isDebt = $remain <= 0 ? 0 : 1;
  185. $custom->debtAmount = $remain;
  186. $custom->save();
  187. $relateId = $clear->id ?? 0;
  188. $customId = $custom->id ?? 0;
  189. $capitalType = dict::getDict('capitalType', 'ghsXsClear', 'id');
  190. $orderSn = $clear->orderSn ?? '';
  191. $change = [
  192. 'relateId' => $relateId,
  193. 'customId' => $customId,
  194. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  195. 'capitalType' => $capitalType,
  196. 'amount' => $amount,
  197. 'balance' => $remain,
  198. 'io' => 0,
  199. 'payWay' => 0,
  200. 'event' => "结账,单号:{$orderSn}",
  201. 'sjId' => $ghs->sjId,
  202. 'shopId' => $ghs->id,
  203. 'mainId' => $ghs->mainId,
  204. ];
  205. CustomDebtChangeClass::addChange($change);
  206. $mainId = $ghs->mainId ?? 0;
  207. $main = MainClass::getLockById($mainId);
  208. if (empty($main)) {
  209. util::fail('没有main信息');
  210. }
  211. $debt = $main->debt ?? 0;
  212. $remain = bcsub($debt, $amount, 2);
  213. $main->debt = $remain;
  214. $main->save();
  215. $event = $custom->name." 结账,单号:{$orderSn}";
  216. $change['balance'] = $remain;
  217. $change['event'] = $event;
  218. TotalDebtChangeClass::addChange($change);
  219. }
  220. //客户退款欠款金额减少 ssh 20220306
  221. public static function refundDebtAmountReduce($custom, $refund, $order)
  222. {
  223. $refundPrice = $refund->refundPrice ?? 0;
  224. $balance = bcsub($custom->debtAmount, $refundPrice, 2);
  225. $custom->debtAmount = $balance;
  226. $custom->save();
  227. $relateId = $refund->id ?? 0;
  228. $customId = $custom->id ?? 0;
  229. $saleOrderSn = $order->orderSn ?? '';
  230. $capitalType = dict::getDict('capitalType', 'saleRefund', 'id');
  231. $change = [
  232. 'relateId' => $relateId,
  233. 'customId' => $customId,
  234. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  235. 'capitalType' => $capitalType,
  236. 'amount' => $refundPrice,
  237. 'balance' => $balance,
  238. 'io' => 0,
  239. 'payWay' => 0,
  240. 'event' => "订单号:{$saleOrderSn} 退款",
  241. 'sjId' => $refund->sjId,
  242. 'shopId' => $refund->id,
  243. 'mainId' => $order->mainId,
  244. ];
  245. CustomDebtChangeClass::addChange($change);
  246. $mainId = $order->mainId ?? 0;
  247. $main = MainClass::getLockById($mainId);
  248. if (empty($main)) {
  249. util::fail('没有main信息');
  250. }
  251. $debt = $main->debt ?? 0;
  252. $remain = bcsub($debt, $refundPrice, 2);
  253. $main->debt = $remain;
  254. $main->save();
  255. $event = $custom->name." 订单号:{$saleOrderSn} 退款";
  256. $change['balance'] = $remain;
  257. $change['event'] = $event;
  258. TotalDebtChangeClass::addChange($change);
  259. }
  260. }