| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- namespace bizHd\custom\classes;
- use biz\wx\classes\WxMessageClass;
- use bizGhs\shop\classes\TotalDebtChangeClass;
- use bizHd\shop\classes\MainClass;
- use bizMall\user\classes\UserClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\stringUtil;
- use common\components\util;
- use Yii;
- use bizHd\base\classes\BaseClass;
- class CustomClass extends BaseClass
- {
- public static $baseFile = '\bizHd\custom\models\Custom';
- //客户采购欠款金额增加 ssh 20220908
- public static function cgDebtAmountAdd($custom, $order)
- {
- $amount = $order->remainDebtPrice ?? 0;
- $custom->isDebt = 1;
- $lastDebt = bcadd($custom->debtAmount, $amount, 2);
- $custom->debtAmount = $lastDebt;
- $custom->save();
- $relateId = $order->id ?? 0;
- $customId = $custom->id ?? 0;
- $orderSn = $order->orderSn ?? '';
- $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
- $event = '购买花材,订单号:' . $orderSn;
- $change = [
- 'relateId' => $relateId,
- 'customId' => $customId,
- 'ptStyle' => dict::getDict('ptStyle', 'hd'),
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $lastDebt,
- 'io' => 1,
- 'payWay' => $order->payWay,
- 'event' => $event,
- 'sjId' => $order->sjId,
- 'shopId' => $order->id,
- 'mainId' => $order->mainId,
- ];
- CustomDebtRecordClass::addChange($change);
- $mainId = $order->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息');
- }
- $debt = $main->debt ?? 0;
- $remain = bcadd($debt, $amount, 2);
- $main->debt = $remain;
- $main->save();
- $event = $custom->name . ' 购买花材,订单号:' . $orderSn;
- $change['balance'] = $remain;
- $change['event'] = $event;
- TotalDebtChangeClass::addChange($change);
- }
- //散客申请退款,零售门店欠款金额减少
- public static function refundDebtAmountReduce($custom, $amount, $refund)
- {
- $debtAmount = $custom->debtAmount ?? 0.00;
- $remain = bcsub($debtAmount, $amount, 2);
- if ($remain < 0) {
- noticeUtil::push("零售客户欠款总金额:{$custom->debtAmount} 本次退款金额:{$amount} 客户ID:{$custom->id} 超出了", '15280215347');
- util::fail('操作失败,欠款金额会出现负数');
- }
- $custom->isDebt = $remain <= 0 ? 0 : 1;
- $custom->debtAmount = $remain;
- $custom->save();
- $relateId = $refund->id ?? 0;
- $customId = $custom->id ?? 0;
- $capitalType = dict::getDict('capitalType', 'hdRefund', 'id');
- $refundSn = $refund->refundSn ?? '';
- $change = [
- 'relateId' => $relateId,
- 'customId' => $customId,
- 'ptStyle' => dict::getDict('ptStyle', 'hd'),
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $remain,
- 'io' => 0,
- 'payWay' => 0,
- 'event' => "客户申请退款,退款单号:{$refundSn}",
- 'sjId' => $refund->sjId ?? 0,
- 'shopId' => $refund->shopId ?? 0,
- 'mainId' => $refund->mainId ?? 0,
- ];
- CustomDebtRecordClass::addChange($change);
- $mainId = $refund->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息');
- }
- $debt = $main->debt ?? 0;
- $totalRemain = bcsub($debt, $amount, 2);
- $main->debt = $totalRemain;
- $main->save();
- $event = $custom->name . "申请退款,退款单号:{$refundSn}";
- $change['balance'] = $totalRemain;
- $change['event'] = $event;
- TotalDebtChangeClass::addChange($change);
- }
- //客户结账欠款金额减少 ssh 20220908
- public static function clearDebtAmountReduce($custom, $amount, $clear)
- {
- $debtAmount = $custom->debtAmount ?? 0.00;
- $remain = bcsub($debtAmount, $amount, 2);
- if ($remain < 0) {
- noticeUtil::push("零售客户欠款总金额:{$custom->debtAmount} 本次结账金额:{$amount} 客户ID:{$custom->id} 超出了", '15280215347');
- util::fail('操作失败,结账后欠款金额会出现负数');
- }
- $custom->isDebt = $remain <= 0 ? 0 : 1;
- $custom->debtAmount = $remain;
- $custom->save();
- $relateId = $clear->id ?? 0;
- $customId = $custom->id ?? 0;
- $capitalType = dict::getDict('capitalType', 'hdXsClear', 'id');
- $orderSn = $clear->orderSn ?? '';
- $change = [
- 'relateId' => $relateId,
- 'customId' => $customId,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $remain,
- 'io' => 0,
- 'payWay' => 0,
- 'event' => "结账,单号:{$orderSn}",
- 'sjId' => $clear->sjId,
- 'shopId' => $clear->id,
- 'mainId' => $clear->mainId,
- ];
- CustomDebtRecordClass::addChange($change);
- $mainId = $clear->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息');
- }
- $debt = $main->debt ?? 0;
- $totalRemain = bcsub($debt, $amount, 2);
- $main->debt = $totalRemain;
- $main->save();
- $event = $custom->name . " 结账,单号:{$orderSn}";
- $change['balance'] = $totalRemain;
- $change['event'] = $event;
- TotalDebtChangeClass::addChange($change);
- }
- public static function getCustomCacheKey($sjId, $shopId, $userId)
- {
- return 'hd_custom_key_' . $sjId . '_' . $shopId . '_' . $userId;
- }
- //添加客户 ssh 2021.2.28
- public static function addCustom($data)
- {
- return self::add($data, true);
- }
- //创建或获取客户信息 ssh 2021.3.19
- public static function buildRelation($shop, $user)
- {
- if (empty($user)) {
- return false;
- }
- if (empty($shop)) {
- return false;
- }
- $isNewCustom = 0;
- $userId = $user->id;
- $name = $user->name ?? '';
- $mobile = $user->mobile ?? '';
- $shopId = $shop->id ?? 0;
- $hdAvatar = $shop->avatar ?? '';
- $sjId = $shop->sjId ?? 0;
- $sjName = $shop->merchantName ?? '';
- $shopName = $shop->shopName ?? '';
- $hdMobile = $shop->mobile ?? '';
- $hdName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
- $hdPy = stringUtil::py($hdName);
- $custom = self::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
- if (empty($custom)) {
- $py = stringUtil::py($name);
- $visitTime = date("Y-m-d H:i:s");
- $data = [
- 'userId' => $userId,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'name' => $name,
- 'originName' => $name,
- 'py' => $py,
- 'mobile' => $mobile,
- 'visitTime' => $visitTime,
- ];
- $custom = self::addCustom($data);
- $isNewCustom = 1;
- }
- $customId = $custom->id ?? 0;
- $hd = HdClass::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
- if (empty($hd)) {
- $hdData = [
- 'shopId' => $shopId,
- 'userId' => $userId,
- 'name' => $hdName,
- 'sjId' => $sjId,
- 'customId' => $customId,
- 'avatar' => $hdAvatar,
- 'py' => $hdPy,
- 'mobile' => $hdMobile,
- 'province' => $shop->province ?? '',
- 'city' => $shop->city ?? '',
- 'dist' => $shop->dist ?? '',
- 'address' => $shop->address ?? '',
- 'floor' => $shop->floor ?? '',
- 'fullAddress' => $shop->fullAddress ?? '',
- 'showAddress' => $shop->showAddress ?? '',
- 'lat' => $shop->lat ?? '',
- 'long' => $shop->long ?? '',
- ];
- $hd = HdClass::addData($hdData);
- $isNewCustom = 1;
- }
- $hdId = $hd->id ?? 0;
- $custom->hdId = $hdId;
- $custom->save();
- if ($isNewCustom == 1) {
- WxMessageClass::newHdCustomInform($shop, $custom);
- }
- return ['custom' => $custom, 'hd' => $hd];
- }
- //获取客户信息
- public static function getCustom($data)
- {
- $custom = self::getByCondition($data);
- if (empty($custom)) {
- $custom = self::add($data);
- }
- return $custom;
- }
- //获取客户信息 ssh 2021.2.1
- public static function getCustomInfo($id)
- {
- $custom = self::getById($id);
- if (empty($custom)) {
- return $custom;
- }
- $list = self::groupBaseInfo([$custom]);
- return current($list);
- }
- //获取客户信息 ssh 2021.3.19
- public static function getCustomByIds($ids)
- {
- $list = self::getByIds($ids);
- $list = self::groupBaseInfo($list);
- return $list;
- }
- public static function valid($info, $shopId)
- {
- if (isset($info['shopId']) && $info['shopId'] == $shopId) {
- return true;
- }
- util::fail('客户信息无效');
- }
- //整合基本信息 ssh 2021.1.31
- public static function groupBaseInfo($list)
- {
- if (empty($list)) {
- return $list;
- }
- foreach ($list as $key => $val) {
- $list[$key]['avatar'] = imgUtil::groupImg($val['avatar']);
- }
- return $list;
- }
- //更新客户信息 ssh 2021.3.23
- public static function updateCustomInfo($custom, $data)
- {
- $userId = $custom['userId'];
- $userData = [];
- if (isset($data['mobile']) && !empty($data['mobile'])) {
- $userData['mobile'] = $data['mobile'];
- }
- if (!empty($userData)) {
- UserClass::updateById($userId, $userData);
- }
- $id = $custom['id'];
- self::updateById($id, $data);
- }
- }
|