| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- <?php
- namespace bizGhs\custom\classes;
- use biz\ghs\classes\GhsClass;
- use biz\sj\classes\MerchantClass;
- use bizGhs\custom\models\Custom;
- use bizGhs\ghs\classes\GhsBalanceChangeClass;
- use bizGhs\shop\classes\MainClass;
- use bizGhs\shop\classes\TotalDebtChangeClass;
- use bizGhs\ghs\classes\GhsBalanceClass;
- use bizGhs\ghs\classes\GhsRechargeClass;
- use bizGhs\shop\classes\ShopClass;
- use common\components\business;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\util;
- use common\components\orderSn;
- use bizHd\base\classes\BaseClass;
- class CustomClass extends BaseClass
- {
- public static $baseFile = '\bizGhs\custom\models\Custom';
- const IS_DEBT_NO = 0;
- const IS_DEBT_YES = 1;
- const NOT_BLACK = 1;
- const IS_BLACK = 2;
- const LEVEL_SK = 0; //散客
- const LEVEL_PT = 1; //花店
- const LEVEL_HJ = 2; //同行
- const LEVEL_ZS = 3; //金店
- public static $levelMap = [
- self::LEVEL_SK => '散客',
- self::LEVEL_PT => '花店',
- self::LEVEL_HJ => '同行',
- self::LEVEL_ZS => '金店',
- ];
- public static $nickNameMap = [
- self::LEVEL_SK => '零售',
- self::LEVEL_PT => '批发',
- self::LEVEL_HJ => '同行',
- self::LEVEL_ZS => '金店',
- ];
- public static $levelPriceKeyMap = [
- self::LEVEL_SK => 'skPrice',
- self::LEVEL_PT => 'price',
- self::LEVEL_HJ => 'hjPrice',
- self::LEVEL_ZS => 'zsPrice',
- ];
- public static $levelAddPriceKeyMap = [
- self::LEVEL_SK => 'skMore',
- self::LEVEL_PT => 'addPrice',
- self::LEVEL_HJ => 'hjAddPrice',
- self::LEVEL_ZS => 'zsAddPrice',
- ];
- //消费余额 ssh 20240311
- public static function clearConsumeBalance($amount, $custom, $ghs, $shop, $staff, $clear)
- {
- $customId = $custom->id ?? 0;
- $ghsId = $ghs->id ?? 0;
- $relateId = $clear->id ?? 0;
- $staffId = $staff->id ?? 0;
- $staffName = $staff->name ?? '';
- $clearSn = $clear->orderSn;
- $mainId = $shop->mainId ?? 0;
- $shopId = $shop->id ?? 0;
- $sjId = $shop->sjId ?? 0;
- $newGhsBalance = bcsub($ghs->balance, $amount, 2);
- $ghs->balance = $newGhsBalance;
- $ghs->save();
- $newCustomBalance = bcsub($custom->balance, $amount, 2);
- $custom->balance = $newCustomBalance;
- $custom->save();
- if ($newGhsBalance < 0) {
- util::fail('余额有问题哦..');
- }
- if (floatval($newGhsBalance) != floatval($newCustomBalance)) {
- util::fail('余额有问题...');
- }
- $capitalType = dict::getDict('capitalType', 'ghsHelpCustomUseBalanceClear', 'id');
- $fromType = dict::getDict('fromType', 'shop');
- $cbData = [
- 'customId' => $customId,
- 'relateId' => $relateId,
- 'onlinePay' => 1,
- 'ptStyle' => 2,
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $newCustomBalance,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'io' => 0,
- 'side' => 0,
- 'payWay' => 0,
- 'fromType' => $fromType,
- 'event' => '结账 ' . $clearSn,
- 'mainId' => $mainId,
- 'shopId' => $shopId,
- 'sjId' => $sjId,
- 'remark' => '',
- ];
- $cbc = CustomBalanceChangeClass::add($cbData, true);
- $customShopId = $custom->shopId ?? 0;
- $customShop = ShopClass::getById($customShopId, true);
- if (empty($customShop)) {
- util::fail('没有找到客户门店呢..');
- }
- $customShopSjId = $customShop->sjId ?? 0;
- $customShopMainId = $customShop->mainId ?? 0;
- $capitalType = dict::getDict('capitalType', 'customAskGhsUseBalanceClear', 'id');
- $gbData = [
- 'ghsId' => $ghsId,
- 'relateId' => $relateId,
- 'ptStyle' => 2,
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $newGhsBalance,
- 'io' => 0,
- 'side' => 0,
- 'onlinePay' => 1,
- 'payWay' => 0,
- 'fromType' => $fromType,
- 'event' => '结账 ' . $clearSn,
- 'sjId' => $customShopSjId,
- 'mainId' => $customShopMainId,
- 'shopId' => $customShopId,
- 'remark' => '',
- ];
- $gbc = GhsBalanceChangeClass::add($gbData, true);
- $clear->customBalanceChangeId = $cbc->id ?? 0;
- $clear->ghsBalanceChangeId = $gbc->id ?? 0;
- $clear->save();
- }
- //充值金额,有多个地方要同步修改,请搜索关键词custom_ghs_recharge ssh 20240310
- public static function rechargeBalance($custom, $amount, $shop, $staff, $payWay, $params = [])
- {
- $ghsId = $custom->ghsId ?? 0;
- $ghsInfo = GhsClass::getLockById($ghsId);
- if (empty($ghsInfo)) {
- util::fail('没有找到供货商');
- }
- $ghsShopId = $ghsInfo->shopId ?? 0;
- $ghsName = $ghsInfo->name ?? '';
- $customId = $custom->id ?? 0;
- $customShopId = $custom->shopId ?? 0;
- $customName = $custom->name ?? '';
- $customShop = ShopClass::getById($customShopId, true);
- if (empty($customShop)) {
- util::fail('没有找到客户门店');
- }
- $payTime = date("Y-m-d H:i:s");
- $customShopSjId = $customShop->sjId ?? 0;
- $customShopMainId = $customShop->mainId ?? 0;
- $payMap = dict::getDict('payWayName');
- $payName = $payMap[$payWay] ?? '';
- $rechargeEvent = $payName . '充值';
- $custom->balance = bcadd($custom->balance, $amount, 2);
- $custom->save();
- $orderSn = orderSn::getGhsCustomRechargeSn();
- $shopId = $shop->id ?? 0;
- $sjId = $shop->sjId ?? 0;
- $mainId = $shop->mainId ?? 0;
- $staffId = $staff->id ?? 0;
- $staffName = $staff->name ?? '';
- $remark = $params['remark'] ?? '';
- $cData = [
- 'orderSn' => $orderSn,
- 'onlinePay' => 1,
- 'payWay' => $payWay,
- 'shopId' => $shopId,
- 'mainId' => $mainId,
- 'amount' => $amount,
- 'balance' => $custom->balance,
- 'side' => 0,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'payStatus' => 1,
- 'status' => 1,
- 'remark' => '',
- 'ghsId' => $ghsId,
- 'ghsShopId' => $ghsShopId,
- 'ghsName' => $ghsName,
- 'customId' => $customId,
- 'customShopId' => $customShopId,
- 'customName' => $customName,
- 'remark' => $remark,
- 'payTime' => $payTime,
- ];
- $cRecharge = CustomRechargeClass::add($cData, true);
- $cRechargeId = $cRecharge->id ?? 0;
- $capitalType = dict::getDict('capitalType', 'ghsHelpCustomRecharge', 'id');
- $fromType = dict::getDict('fromType', 'shop');
- $cbData = [
- 'customId' => $customId,
- 'relateId' => $cRechargeId,
- 'onlinePay' => 1,
- 'ptStyle' => 2,
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $custom->balance,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'io' => 1,
- 'side' => 0,
- 'payWay' => $payWay,
- 'fromType' => $fromType,
- 'event' => $rechargeEvent,
- 'mainId' => $mainId,
- 'shopId' => $shopId,
- 'sjId' => $sjId,
- 'remark' => $remark,
- ];
- CustomBalanceChangeClass::add($cbData, true);
- $ghsInfo->balance = bcadd($ghsInfo->balance, $amount, 2);
- $ghsInfo->save();
- $orderSn = orderSn::getGhsRechargeSn();
- $customMainId = $custom->mainId ?? 0;
- $gData = [
- 'orderSn' => $orderSn,
- 'onlinePay' => 1,
- 'payWay' => $payWay,
- 'shopId' => $customShopId,
- 'mainId' => $customMainId,
- 'amount' => $amount,
- 'balance' => $ghsInfo->balance,
- 'side' => 0,
- 'staffId' => 0,
- 'staffName' => '',
- 'payStatus' => 1,
- 'status' => 1,
- 'remark' => '',
- 'ghsId' => $ghsId,
- 'ghsShopId' => $ghsShopId,
- 'ghsName' => $ghsName,
- 'customId' => $customId,
- 'customShopId' => $customShopId,
- 'customName' => $customName,
- 'remark' => $remark,
- 'payTime' => $payTime,
- ];
- $gRecharge = GhsRechargeClass::add($gData, true);
- $gRechargeId = $gRecharge->id ?? 0;
- $capitalType = dict::getDict('capitalType', 'customAskGhsRecharge', 'id');
- $gbData = [
- 'ghsId' => $ghsId,
- 'relateId' => $gRechargeId,
- 'ptStyle' => 2,
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $ghsInfo->balance,
- 'io' => 1,
- 'side' => 0,
- 'onlinePay' => 1,
- 'payWay' => $payWay,
- 'fromType' => $fromType,
- 'event' => $rechargeEvent,
- 'sjId' => $customShopSjId,
- 'mainId' => $customShopMainId,
- 'shopId' => $customShopId,
- 'remark' => $remark,
- ];
- GhsBalanceChangeClass::add($gbData, true);
- return ['custom' => $custom, 'ghs' => $ghsInfo, 'customRecharge' => $cRecharge, 'ghsRecharge' => $gRecharge];
- }
- public static function createSeatSn($mainId)
- {
- $max = Custom::find(['mainId' => $mainId])->max('seatSn');
- $new = bcadd($max, 1);
- return $new;
- }
- //建立客户和供货商关系
- public static function build($ghsShopId, $hdShopId, $changeName = '')
- {
- return GhsClass::build($ghsShopId, $hdShopId, 2, $changeName);
- }
- //添加客户 ssh 2021.2.24
- public static function addCustom($data)
- {
- return self::add($data);
- }
- //获取客户信息 ssh 2021.2.4
- public static function getCustom($id)
- {
- $info = self::getById($id);
- if (empty($info)) {
- return $info;
- }
- $respond = self::groupBaseInfo([$info]);
- return current($respond);
- }
- public static function getCustomByIds($ids)
- {
- $list = self::getByIds($ids, null, 'id');
- return self::groupBaseInfo($list);
- }
- //整合头像等信息 ssh 2021.1.8
- public static function groupBaseInfo($list)
- {
- if (empty($list)) {
- return $list;
- }
- //获取客户信息
- $customSjIdList = array_unique(array_filter(array_column($list, 'sjId')));
- $sjInfo = MerchantClass::getByIds($customSjIdList, null, 'id');
- $levelMap = self::$levelMap;
- foreach ($list as $key => $val) {
- $customSjId = $val['sjId'];
- $currentInfo = isset($sjInfo[$customSjId]) ? $sjInfo[$customSjId] : [];
- $currentInfo = business::formatMerchantLogo($currentInfo);
- $smallAvatar = !empty($currentInfo['smallLogoUrl']) ? $currentInfo['smallLogoUrl'] : imgUtil::getPrefix() . '/hhb_small.png';
- $avatar = !empty($currentInfo['logoUrl']) ? $currentInfo['logoUrl'] : imgUtil::getPrefix() . '/hhb_small.png';
- $list[$key]['smallAvatar'] = $smallAvatar;
- $list[$key]['shortSmallAvatar'] = $currentInfo['shortSmallLogoUrl'];
- $list[$key]['avatar'] = $avatar;
- $list[$key]['shortAvatar'] = $currentInfo['shortLogoUrl'];
- $list[$key]['sn'] = $val['id'];
- $level = $val['level'] ?? 1;
- $levelName = $levelMap[$level] ?? '';
- $list[$key]['levelName'] = $levelName;
- $debtAmount = $val['debtAmount'] ?? 0;
- $balance = $val['balance'] ?? 0;
- $remainDebtAmount = bcsub($debtAmount, $balance, 2);
- $list[$key]['remainDebtAmount'] = $remainDebtAmount;
- $recentExpend = $val['recentExpend'] ?? '';
- $recentExpend = strtotime($recentExpend);
- $seven = bcmul(86400, 7);
- $addTime = bcadd($recentExpend, $seven);
- $list[$key]['overSevenUnExpend'] = 0;
- if ($addTime < time()) {
- //超过7天未下单
- $list[$key]['overSevenUnExpend'] = 1;
- }
- }
- return $list;
- }
- //欠款权限开关 ssh 2021.1.8
- public static function debt($custom, $debt)
- {
- $id = isset($custom['id']) ? $custom['id'] : 0;
- self::updateById($id, ['debt' => $debt]);
- return true;
- }
- //采购查看权限的黑白名单设置 shizhongqi 2021.08.08
- public static function black($custom, $black)
- {
- $id = isset($custom['id']) ? $custom['id'] : 0;
- self::updateById($id, ['black' => $black]);
- return true;
- }
- //权限判断 ssh 2021.1.8
- public static function valid($custom, $shopId)
- {
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- if ($custom['ownShopId'] != $shopId) {
- util::fail('您无法操作此客户');
- }
- }
- //客户采购欠款金额增加 ssh 20220306
- public static function cgDebtAmountAdd($custom, $order, $bookOrder = false)
- {
- $amount = $order->remainDebtPrice ?? 0;
- $custom->isDebt = CustomClass::IS_DEBT_YES;
- $debtLimit = $custom->debtLimit ?? 0;
- if ($bookOrder == false) {
- if (isset($order->shopAdminId) && $order->shopAdminId == 0) {
- //客户自己下单要受额度限制
- if ($custom->debtAmount > $debtLimit) {
- util::fail("已欠款{$custom->debtAmount},请先结账哦");
- }
- }
- }
- $balance = bcadd($custom->debtAmount, $amount, 2);
- $mustNotice = false;
- if (getenv('YII_ENV') == 'production') {
- //王品牛排 黄先生
- if ($custom->id == 11726) {
- $mustNotice = true;
- }
- } else {
- if ($custom->id == 577) {
- $mustNotice = true;
- }
- }
- if (getenv('YII_ENV', 'local') == 'production') {
- if ($mustNotice) {
- noticeUtil::push("【开单动作 | 客户】变化前欠款:{$custom->debtAmount} 变化金额:{$amount} 变化后欠款:{$balance}", '15280215347');
- }
- }
- $custom->debtAmount = $balance;
- $custom->save();
- $relateId = $order->id ?? 0;
- $customId = $custom->id ?? 0;
- $orderSn = $order->orderSn ?? '';
- $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
- $event = '购买花材,单号:' . $orderSn;
- if ($bookOrder) {
- $event = '预订单多退少补,单号:' . $orderSn;
- }
- $change = [
- 'relateId' => $relateId,
- 'customId' => $customId,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $balance,
- 'io' => 1,
- 'payWay' => $order->payWay,
- 'event' => $event,
- 'sjId' => $order->sjId,
- 'shopId' => $order->id,
- 'mainId' => $order->mainId,
- ];
- CustomDebtChangeClass::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);
- if (getenv('YII_ENV', 'local') == 'production') {
- if ($mustNotice) {
- noticeUtil::push("【开单动作 | 总欠款】变化前欠款:{$debt} 变化金额:{$amount} 变化后欠款:{$remain}", '15280215347');
- }
- }
- $main->debt = $remain;
- $main->save();
- $event = $custom->name . ' 购买花材,单号:' . $orderSn;
- $change['balance'] = $remain;
- $change['event'] = $event;
- TotalDebtChangeClass::addChange($change);
- }
- //客户结账欠款金额减少 ssh 20220306
- public static function clearDebtAmountReduce($custom, $ghs, $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', 'ghsXsClear', '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' => $ghs->sjId,
- 'shopId' => $ghs->id,
- 'mainId' => $ghs->mainId,
- ];
- CustomDebtChangeClass::addChange($change);
- $mainId = $ghs->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息');
- }
- $debt = $main->debt ?? 0;
- $remain = bcsub($debt, $amount, 2);
- $main->debt = $remain;
- $main->save();
- $event = $custom->name . " 结账,单号:{$orderSn}";
- $change['balance'] = $remain;
- $change['event'] = $event;
- TotalDebtChangeClass::addChange($change);
- }
- //客户退款欠款金额减少 ssh 20220306
- public static function refundDebtAmountReduce($custom, $refund, $order)
- {
- $refundPrice = $refund->refundPrice ?? 0;
- $balance = bcsub($custom->debtAmount, $refundPrice, 2);
- $custom->debtAmount = $balance;
- $custom->save();
- $relateId = $refund->id ?? 0;
- $customId = $custom->id ?? 0;
- $saleOrderSn = $order->orderSn ?? '';
- $capitalType = dict::getDict('capitalType', 'saleRefund', 'id');
- $change = [
- 'relateId' => $relateId,
- 'customId' => $customId,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'capitalType' => $capitalType,
- 'amount' => $refundPrice,
- 'balance' => $balance,
- 'io' => 0,
- 'payWay' => 0,
- 'event' => "订单号:{$saleOrderSn} 退款",
- 'sjId' => $refund->sjId,
- 'shopId' => $refund->id,
- 'mainId' => $order->mainId,
- ];
- CustomDebtChangeClass::addChange($change);
- $mainId = $order->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息');
- }
- $debt = $main->debt ?? 0;
- $remain = bcsub($debt, $refundPrice, 2);
- $main->debt = $remain;
- $main->save();
- $event = $custom->name . " 订单号:{$saleOrderSn} 退款";
- $change['balance'] = $remain;
- $change['event'] = $event;
- TotalDebtChangeClass::addChange($change);
- }
- }
|