CustomClass.php 8.1 KB

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