CustomClass.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. namespace bizHd\custom\classes;
  3. use biz\wx\classes\WxMessageClass;
  4. use bizGhs\shop\classes\TotalDebtChangeClass;
  5. use bizHd\shop\classes\MainClass;
  6. use bizMall\user\classes\UserClass;
  7. use common\components\dict;
  8. use common\components\imgUtil;
  9. use common\components\noticeUtil;
  10. use common\components\stringUtil;
  11. use common\components\util;
  12. use Yii;
  13. use bizHd\base\classes\BaseClass;
  14. class CustomClass extends BaseClass
  15. {
  16. public static $baseFile = '\bizHd\custom\models\Custom';
  17. //客户采购欠款金额增加 ssh 20220908
  18. public static function cgDebtAmountAdd($custom, $order)
  19. {
  20. $amount = $order->remainDebtPrice ?? 0;
  21. $custom->isDebt = 1;
  22. $lastDebt = bcadd($custom->debtAmount, $amount, 2);
  23. $custom->debtAmount = $lastDebt;
  24. $custom->save();
  25. $relateId = $order->id ?? 0;
  26. $customId = $custom->id ?? 0;
  27. $orderSn = $order->orderSn ?? '';
  28. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  29. $event = '购买花材,订单号:' . $orderSn;
  30. $change = [
  31. 'relateId' => $relateId,
  32. 'customId' => $customId,
  33. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  34. 'capitalType' => $capitalType,
  35. 'amount' => $amount,
  36. 'balance' => $lastDebt,
  37. 'io' => 1,
  38. 'payWay' => $order->payWay,
  39. 'event' => $event,
  40. 'sjId' => $order->sjId,
  41. 'shopId' => $order->id,
  42. 'mainId' => $order->mainId,
  43. ];
  44. CustomDebtRecordClass::addChange($change);
  45. $mainId = $order->mainId ?? 0;
  46. $main = MainClass::getLockById($mainId);
  47. if (empty($main)) {
  48. util::fail('没有main信息');
  49. }
  50. $debt = $main->debt ?? 0;
  51. $remain = bcadd($debt, $amount, 2);
  52. $main->debt = $remain;
  53. $main->save();
  54. $event = $custom->name . ' 购买花材,订单号:' . $orderSn;
  55. $change['balance'] = $remain;
  56. $change['event'] = $event;
  57. TotalDebtChangeClass::addChange($change);
  58. }
  59. //散客申请退款,零售门店欠款金额减少
  60. public static function refundDebtAmountReduce($custom, $amount, $refund)
  61. {
  62. $debtAmount = $custom->debtAmount ?? 0.00;
  63. $remain = bcsub($debtAmount, $amount, 2);
  64. if ($remain < 0) {
  65. noticeUtil::push("零售客户欠款总金额:{$custom->debtAmount} 本次退款金额:{$amount} 客户ID:{$custom->id} 超出了", '15280215347');
  66. util::fail('操作失败,欠款金额会出现负数');
  67. }
  68. $custom->isDebt = $remain <= 0 ? 0 : 1;
  69. $custom->debtAmount = $remain;
  70. $custom->save();
  71. $relateId = $refund->id ?? 0;
  72. $customId = $custom->id ?? 0;
  73. $capitalType = dict::getDict('capitalType', 'hdRefund', 'id');
  74. $refundSn = $refund->refundSn ?? '';
  75. $change = [
  76. 'relateId' => $relateId,
  77. 'customId' => $customId,
  78. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  79. 'capitalType' => $capitalType,
  80. 'amount' => $amount,
  81. 'balance' => $remain,
  82. 'io' => 0,
  83. 'payWay' => 0,
  84. 'event' => "客户申请退款,退款单号:{$refundSn}",
  85. 'sjId' => $refund->sjId ?? 0,
  86. 'shopId' => $refund->shopId ?? 0,
  87. 'mainId' => $refund->mainId ?? 0,
  88. ];
  89. CustomDebtRecordClass::addChange($change);
  90. $mainId = $refund->mainId ?? 0;
  91. $main = MainClass::getLockById($mainId);
  92. if (empty($main)) {
  93. util::fail('没有main信息');
  94. }
  95. $debt = $main->debt ?? 0;
  96. $totalRemain = bcsub($debt, $amount, 2);
  97. $main->debt = $totalRemain;
  98. $main->save();
  99. $event = $custom->name . "申请退款,退款单号:{$refundSn}";
  100. $change['balance'] = $totalRemain;
  101. $change['event'] = $event;
  102. TotalDebtChangeClass::addChange($change);
  103. }
  104. //客户结账欠款金额减少 ssh 20220908
  105. public static function clearDebtAmountReduce($custom, $amount, $clear)
  106. {
  107. $debtAmount = $custom->debtAmount ?? 0.00;
  108. $remain = bcsub($debtAmount, $amount, 2);
  109. if ($remain < 0) {
  110. noticeUtil::push("零售客户欠款总金额:{$custom->debtAmount} 本次结账金额:{$amount} 客户ID:{$custom->id} 超出了", '15280215347');
  111. util::fail('操作失败,结账后欠款金额会出现负数');
  112. }
  113. $custom->isDebt = $remain <= 0 ? 0 : 1;
  114. $custom->debtAmount = $remain;
  115. $custom->save();
  116. $relateId = $clear->id ?? 0;
  117. $customId = $custom->id ?? 0;
  118. $capitalType = dict::getDict('capitalType', 'hdXsClear', 'id');
  119. $orderSn = $clear->orderSn ?? '';
  120. $change = [
  121. 'relateId' => $relateId,
  122. 'customId' => $customId,
  123. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  124. 'capitalType' => $capitalType,
  125. 'amount' => $amount,
  126. 'balance' => $remain,
  127. 'io' => 0,
  128. 'payWay' => 0,
  129. 'event' => "结账,单号:{$orderSn}",
  130. 'sjId' => $clear->sjId,
  131. 'shopId' => $clear->id,
  132. 'mainId' => $clear->mainId,
  133. ];
  134. CustomDebtRecordClass::addChange($change);
  135. $mainId = $clear->mainId ?? 0;
  136. $main = MainClass::getLockById($mainId);
  137. if (empty($main)) {
  138. util::fail('没有main信息');
  139. }
  140. $debt = $main->debt ?? 0;
  141. $totalRemain = bcsub($debt, $amount, 2);
  142. $main->debt = $totalRemain;
  143. $main->save();
  144. $event = $custom->name . " 结账,单号:{$orderSn}";
  145. $change['balance'] = $totalRemain;
  146. $change['event'] = $event;
  147. TotalDebtChangeClass::addChange($change);
  148. }
  149. public static function getCustomCacheKey($sjId, $shopId, $userId)
  150. {
  151. return 'hd_custom_key_' . $sjId . '_' . $shopId . '_' . $userId;
  152. }
  153. //添加客户 ssh 2021.2.28
  154. public static function addCustom($data)
  155. {
  156. return self::add($data, true);
  157. }
  158. //创建或获取客户信息 ssh 2021.3.19
  159. public static function buildRelation($shop, $user)
  160. {
  161. if (empty($user)) {
  162. return false;
  163. }
  164. if (empty($shop)) {
  165. return false;
  166. }
  167. $isNewCustom = 0;
  168. $userId = $user->id;
  169. $name = $user->name ?? '';
  170. $mobile = $user->mobile ?? '';
  171. $shopId = $shop->id ?? 0;
  172. $hdAvatar = $shop->avatar ?? '';
  173. $sjId = $shop->sjId ?? 0;
  174. $sjName = $shop->merchantName ?? '';
  175. $shopName = $shop->shopName ?? '';
  176. $hdMobile = $shop->mobile ?? '';
  177. $hdName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  178. $hdPy = stringUtil::py($hdName);
  179. $custom = self::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
  180. if (empty($custom)) {
  181. $py = stringUtil::py($name);
  182. $visitTime = date("Y-m-d H:i:s");
  183. $data = [
  184. 'userId' => $userId,
  185. 'sjId' => $sjId,
  186. 'shopId' => $shopId,
  187. 'name' => $name,
  188. 'originName' => $name,
  189. 'py' => $py,
  190. 'mobile' => $mobile,
  191. 'visitTime' => $visitTime,
  192. ];
  193. $custom = self::addCustom($data);
  194. $isNewCustom = 1;
  195. }
  196. $customId = $custom->id ?? 0;
  197. $hd = HdClass::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
  198. if (empty($hd)) {
  199. $hdData = [
  200. 'shopId' => $shopId,
  201. 'userId' => $userId,
  202. 'name' => $hdName,
  203. 'sjId' => $sjId,
  204. 'customId' => $customId,
  205. 'avatar' => $hdAvatar,
  206. 'py' => $hdPy,
  207. 'mobile' => $hdMobile,
  208. 'province' => $shop->province ?? '',
  209. 'city' => $shop->city ?? '',
  210. 'dist' => $shop->dist ?? '',
  211. 'address' => $shop->address ?? '',
  212. 'floor' => $shop->floor ?? '',
  213. 'fullAddress' => $shop->fullAddress ?? '',
  214. 'showAddress' => $shop->showAddress ?? '',
  215. 'lat' => $shop->lat ?? '',
  216. 'long' => $shop->long ?? '',
  217. ];
  218. $hd = HdClass::addData($hdData);
  219. $isNewCustom = 1;
  220. }
  221. $hdId = $hd->id ?? 0;
  222. $custom->hdId = $hdId;
  223. $custom->save();
  224. if ($isNewCustom == 1) {
  225. WxMessageClass::newHdCustomInform($shop, $custom);
  226. }
  227. return ['custom' => $custom, 'hd' => $hd];
  228. }
  229. //获取客户信息
  230. public static function getCustom($data)
  231. {
  232. $custom = self::getByCondition($data);
  233. if (empty($custom)) {
  234. $custom = self::add($data);
  235. }
  236. return $custom;
  237. }
  238. //获取客户信息 ssh 2021.2.1
  239. public static function getCustomInfo($id)
  240. {
  241. $custom = self::getById($id);
  242. if (empty($custom)) {
  243. return $custom;
  244. }
  245. $list = self::groupBaseInfo([$custom]);
  246. return current($list);
  247. }
  248. //获取客户信息 ssh 2021.3.19
  249. public static function getCustomByIds($ids)
  250. {
  251. $list = self::getByIds($ids);
  252. $list = self::groupBaseInfo($list);
  253. return $list;
  254. }
  255. public static function valid($info, $shopId)
  256. {
  257. if (isset($info['shopId']) && $info['shopId'] == $shopId) {
  258. return true;
  259. }
  260. util::fail('客户信息无效');
  261. }
  262. //整合基本信息 ssh 2021.1.31
  263. public static function groupBaseInfo($list)
  264. {
  265. if (empty($list)) {
  266. return $list;
  267. }
  268. foreach ($list as $key => $val) {
  269. $list[$key]['avatar'] = imgUtil::groupImg($val['avatar']);
  270. }
  271. return $list;
  272. }
  273. //更新客户信息 ssh 2021.3.23
  274. public static function updateCustomInfo($custom, $data)
  275. {
  276. $userId = $custom['userId'];
  277. $userData = [];
  278. if (isset($data['mobile']) && !empty($data['mobile'])) {
  279. $userData['mobile'] = $data['mobile'];
  280. }
  281. if (!empty($userData)) {
  282. UserClass::updateById($userId, $userData);
  283. }
  284. $id = $custom['id'];
  285. self::updateById($id, $data);
  286. }
  287. }