| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417 |
- <?php
- namespace bizGhs\custom\classes;
- use biz\ghs\classes\GhsClass;
- use biz\sj\classes\SjClass;
- use bizGhs\admin\classes\AdminClass;
- use bizGhs\book\classes\BookCustomClass;
- use bizGhs\book\classes\BookItemCustomClass;
- use bizGhs\custom\models\Custom;
- use bizGhs\ghs\classes\GhsBalanceChangeClass;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\shop\classes\MainClass;
- use bizGhs\shop\classes\ShopMoneyClass;
- use bizGhs\shop\classes\TotalDebtChangeClass;
- use bizGhs\ghs\classes\GhsBalanceClass;
- use bizGhs\ghs\classes\GhsRechargeClass;
- use bizGhs\order\classes\OrderClearClass;
- use bizGhs\shop\classes\ShopClass;
- use bizHd\member\classes\MemberLevelClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\saas\classes\ApplyClass;
- use bizHd\saas\services\ApplyService;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\stringUtil;
- use common\components\util;
- use common\components\orderSn;
- use bizHd\base\classes\BaseClass;
- use Yii;
- 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',
- ];
- public static function checkBalance($custom)
- {
- $ghsId = $custom->ghsId ?? 0;
- $customId = $custom->id ?? 0;
- $ghs = GhsClass::getLockById($ghsId);
- $ghsName = $ghs->name ?? '';
- $customName = $custom->name ?? '';
- if (empty($ghs)) {
- util::fail('没有供货商信息');
- }
- if ($custom->balance != $ghs->balance) {
- noticeUtil::push("余额有问题哦,{$ghsName} {$customName}", '15280215347');
- util::fail('余额有问题哦');
- }
- if ($custom->balance < 0) {
- $remainDebt = OrderClass::sum(['customId' => $customId, 'debt' => 1], 'remainDebtPrice');
- if (abs($custom->balance) != $remainDebt) {
- noticeUtil::push("余额有问题呢,{$ghsName} {$customName}", '15280215347');
- util::fail('余额有问题呢');
- }
- }
- }
- public static function pullOtherShopCustom($data)
- {
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $toShopId = $data['toShopId'] ?? 0;
- $fromShopId = $data['fromShopId'] ?? 0;
- //指定当前环境为批发商,重要!!!
- Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
- $fromCustomList = CustomClass::getAllByCondition(['ownShopId' => $fromShopId], null, '*');
- if (!empty($fromCustomList)) {
- foreach ($fromCustomList as $fromCustomer) {
- $exist = CustomClass::exists(['ownShopId' => $toShopId, 'shopId' => $fromCustomer['shopId']]);
- if (!$exist) {
- CustomClass::build($toShopId, $fromCustomer['shopId'], $fromCustomer['name']);
- }
- }
- }
- $transaction->commit();
- return true;
- } catch (\Exception $exception) {
- $transaction->rollBack();
- noticeUtil::push("批量拉取客户时报错了,fromShopId:{$fromShopId} toShopId:{$toShopId},报错内容:" . $exception->getMessage(), '15280215347');
- }
- return true;
- }
- public static function generateCustom($data)
- {
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- // 组织表单数据
- $shopName = $data['name'] ?? '';
- $mobile = $data['mobile'] ?? '';
- $post = [];
- $post['introSjId'] = $data['sjId'] ?? '';
- $post['introShopId'] = $data['shopId'] ?? '';
- $post['introSjName'] = $data['sjName'] ?? '';
- $post['introStyle'] = SjClass::STYLE_SUPPLIER;
- $post['name'] = $shopName;
- if (empty($shopName)) {
- noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称没有填写", '15280215347');
- $transaction->rollBack();
- return true;
- }
- $post['mobile'] = $mobile;
- if (!stringUtil::isMobile($mobile)) {
- noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号填写错误", '15280215347');
- $transaction->rollBack();
- return true;
- }
- if (stringUtil::getWordNum($shopName) > 15) {
- noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,名称超过了15个汉字", '15280215347');
- $transaction->rollBack();
- return true;
- }
- $has = ApplyClass::getByCondition(['mobile' => $mobile]);
- if (!empty($has)) {
- noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号已经存在了哦", '15280215347');
- $transaction->rollBack();
- return true;
- }
- $hasShop = \biz\shop\classes\ShopClass::getByCondition(['mobile' => $mobile]);
- if (!empty($hasShop)) {
- noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,手机号已经存在了", '15280215347');
- $transaction->rollBack();
- return true;
- }
- $adminName = $post['adminName'] ?? '';
- $miniOpenId = $post['miniOpenId'] ?? '';
- $adminInfo = ['name' => $adminName, 'mobile' => $mobile, 'miniOpenId' => $miniOpenId];
- $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
- $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
- $adminId = $admin['id'] ?? 0;
- $post['adminId'] = $adminId;
- $post['adminName'] = $admin['name'] ?? '';
- $post['long'] = isset($post['longitude']) ? $post['longitude'] : '';
- $post['lat'] = isset($post['latitude']) ? $post['latitude'] : '';
- $post['from'] = ApplyClass::FROM_RETAIL;
- $post['style'] = SjClass::STYLE_RETAIL;
- //增加或更新申请
- $respond = ApplyService::replaceRetail($post);
- $id = $respond['id'] ?? 0;
- //后面的代码,要用到此参数,重要
- Yii::$app->params['ptStyle'] = 2;
- ApplyService::pass($id, '批发商手动添加');
- $transaction->commit();
- //只要手动添加的都标记为花店,任何情况都不会标记为散客,任何情况都是审核通过的状态
- $customList = CustomClass::getAllByCondition(['mobile' => $mobile], null, '*', null, true);
- if (!empty($customList)) {
- foreach ($customList as $custom) {
- $custom->isHd = 1;
- $custom->passStatus = 1;
- $custom->save();
- $ghsId = $custom->ghsId;
- GhsClass::updateById($ghsId, ['passStatus' => 1]);
- }
- }
- //noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 由批发商添加成功");
- return true;
- } catch (\Exception $exception) {
- $transaction->rollBack();
- noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 添加失败,原因:" . $exception->getMessage(), '15280215347');
- }
- return true;
- }
- //预订相关表同步更新货位 ssh 20240729
- public static function syncBookSeat($shop, $custom, $seatSn)
- {
- $bookSn = $shop->bookSn ?? 0;
- if (empty($bookSn)) {
- return true;
- }
- $customId = $custom->id ?? 0;
- BookItemCustomClass::updateByCondition(['bookSn' => $bookSn, 'customId' => $customId], ['seatSn' => $seatSn]);
- BookCustomClass::updateByCondition(['bookSn' => $bookSn, 'customId' => $customId], ['seatSn' => $seatSn]);
- }
- /**
- * 用账面正余额支付挂账:扣减净 balance 并记「结账」流水。
- * 来款充值后的自动销单不要调本方法(对照 hd:来款已入账,销单只改订单)。
- * 调用方:GhsRechargeSettleService::consumePositiveBalanceFifo。
- */
- public static function clearConsumeBalance($amount, $custom, $ghs, $shop, $staff, $clear, $options = [])
- {
- $skipBalanceChangeRecord = !empty($options['skipBalanceChangeRecord']);
- // 先合并历史挂账到 balance,再扣减(只记余额变动)
- AccountMoneyClass::ensureCustomMoneyReady($custom, true);
- AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
- $customId = $custom->id ?? 0;
- $customName = $custom->name ?? '';
- $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;
- $amount = bcadd((string)$amount, '0', 2);
- if (bccomp($amount, '0', 2) <= 0) {
- util::fail('销账扣款金额须大于0');
- }
- $newGhsBalance = bcsub((string)($ghs->balance ?? '0'), $amount, 2);
- $newCustomBalance = bcsub((string)($custom->balance ?? '0'), $amount, 2);
- // 合并后净余额可为负(待结),勿再因扣款后 <0 报错;金额由 CustomPaymentAllocateService 按来款池控制
- if (bccomp($newGhsBalance, $newCustomBalance, 2) !== 0) {
- util::fail('客户与供货商余额不一致,无法结账,请联系管理员');
- }
- $ghs->balance = $newGhsBalance;
- $ghs->save();
- $custom->balance = $newCustomBalance;
- $custom->save();
- // 充值销账:只记一条充值流水(附 clearAmount),不另记「结账 -X」行(对照 hd settleAmount)
- if ($skipBalanceChangeRecord) {
- return;
- }
- $capitalType = dict::getDict('capitalType', 'ghsHelpCustomUseBalanceClear', 'id');
- $fromType = dict::getDict('fromType', 'shop');
- $cbData = [
- 'customId' => $customId,
- 'customName' => $customName,
- '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();
- }
- /** 供货商帮充与花店线上充:成对充值单互相关联,供余额明细展示结账单 */
- protected static function linkCustomAndGhsRecharge($customRecharge, $ghsRecharge)
- {
- if (empty($customRecharge) || empty($ghsRecharge)) {
- return;
- }
- $customRechargeId = intval(is_object($customRecharge) ? ($customRecharge->id ?? 0) : 0);
- $ghsRechargeId = intval(is_object($ghsRecharge) ? ($ghsRecharge->id ?? 0) : 0);
- if ($customRechargeId <= 0 || $ghsRechargeId <= 0) {
- return;
- }
- $cProbe = new \bizGhs\custom\models\CustomRecharge();
- if (method_exists($cProbe, 'hasAttribute') && $cProbe->hasAttribute('ghsRechargeId')) {
- $customRecharge->ghsRechargeId = $ghsRechargeId;
- $customRecharge->save(false, ['ghsRechargeId']);
- }
- $gProbe = new \bizGhs\ghs\models\GhsRecharge();
- if (method_exists($gProbe, 'hasAttribute') && $gProbe->hasAttribute('customRechargeId')) {
- $ghsRecharge->customRechargeId = $customRechargeId;
- $ghsRecharge->save(false, ['customRechargeId']);
- }
- }
- // 充值/返充(增加净 balance);有多个地方要同步修改,请搜索关键词 custom_ghs_recharge ssh 20240310
- public static function rechargeBalance($custom, $amount, $shop, $staff, $payWay, $params = [])
- {
- // 充值前合并;之后对 balance 做 bcadd(只记余额变动)
- AccountMoneyClass::ensureCustomMoneyReady($custom, true);
- $ghsId = $custom->ghsId ?? 0;
- $ghsInfo = GhsClass::getLockById($ghsId);
- AccountMoneyClass::ensureGhsMoneyReady($ghsInfo, true);
- 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;
- $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'] ?? '';
- $rechargeType = $params['rechargeType'] ?? 0;
- $payMap = dict::getDict('payWayName');
- $payName = $payMap[$payWay] ?? '';
- $rechargeEvent = $payName . '充值';
- if ($rechargeType == 1) {
- $rechargeEvent = '售后返充';
- }
- $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,
- 'ghsId' => $ghsId,
- 'ghsShopId' => $ghsShopId,
- 'ghsName' => $ghsName,
- 'customId' => $customId,
- 'customShopId' => $customShopId,
- 'customName' => $customName,
- 'remark' => $remark,
- 'payTime' => $payTime,
- 'rechargeType' => $rechargeType,
- ];
- if (!empty($params['clearAudit']) && is_array($params['clearAudit'])) {
- $cData = CustomBalanceChangeClass::mergeClearAuditIntoRechargeData($cData, $params['clearAudit']);
- }
- $cRecharge = CustomRechargeClass::add($cData, true);
- $cRechargeId = $cRecharge->id ?? 0;
- $capitalType = dict::getDict('capitalType', 'ghsHelpCustomRecharge', 'id');
- if ($rechargeType == 1) {
- //售后返充
- $capitalType = dict::getDict('capitalType', 'ghsHelpCustomRechargeReturn', 'id');
- }
- $fromType = dict::getDict('fromType', 'shop');
- $cbData = [
- 'customId' => $customId,
- 'customName' => $customName,
- '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,
- ];
- if (!empty($params['clearAudit']) && is_array($params['clearAudit'])) {
- $cbData = CustomBalanceChangeClass::mergeClearAuditIntoRowData($cbData, $params['clearAudit']);
- }
- 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,
- 'ghsId' => $ghsId,
- 'ghsShopId' => $ghsShopId,
- 'ghsName' => $ghsName,
- 'customId' => $customId,
- 'customShopId' => $customShopId,
- 'customName' => $customName,
- 'remark' => $remark,
- 'payTime' => $payTime,
- 'rechargeType' => $rechargeType,
- ];
- $gRecharge = GhsRechargeClass::add($gData, true);
- $gRechargeId = $gRecharge->id ?? 0;
- self::linkCustomAndGhsRecharge($cRecharge, $gRecharge);
- $capitalType = dict::getDict('capitalType', 'customAskGhsRecharge', 'id');
- if ($rechargeType == 1) {
- //售后返充
- $capitalType = dict::getDict('capitalType', 'customAskGhsRechargeReturn', 'id');
- }
- $gbData = [
- 'ghsId' => $ghsId,
- 'ghsName' => $ghsName,
- '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);
- // 现金充值,同步更新表 xhMain 的 money 字段
- if ($payWay == 4) {
- $m = \bizGhs\shop\classes\MainClass::getById($mainId, true, 'id, money');
- $balance = $m->money;
- $m->money = bcadd($m->money, $amount);
- $m->save();
- $event = '客户(' . $customName . ')充值' . $amount . '元';
- $data = [
- 'amount' => $amount,
- 'balance' => $balance,
- 'io' => 1,
- 'mainId' => $mainId,
- 'capitalType' => $capitalType,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'event' => $event,
- 'remark' => $remark,
- ];
- ShopMoneyClass::addData($data);
- }
- 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, $mainId = 0)
- {
- if (empty($list)) {
- return $list;
- }
- //片区划分
- $distMap = [];
- $custom = current($list);
- if (!empty($custom) && !empty($custom['ownMainId'])) {
- $distMap = DistClass::getAllByCondition(['mainId' => $custom['ownMainId']], null, '*', 'id');
- }
- $levelMap = self::$levelMap;
- foreach ($list as $key => $val) {
- $smallAvatar = imgUtil::getPrefix() . (!empty($val['avatar']) ? $val['avatar'] : '/hhb_small.png') . '?x-oss-process=image/resize,m_fill,h_130,w_130';
- $avatar = imgUtil::getPrefix() . (!empty($val['avatar']) ? $val['avatar'] : '/hhb_small.png');
- $list[$key]['smallAvatar'] = $smallAvatar;
- $list[$key]['avatar'] = $avatar;
- $list[$key]['sn'] = $val['id'];
- $level = $val['level'] ?? 1;
- $levelName = $levelMap[$level] ?? '';
- $list[$key]['levelName'] = $levelName;
- // 按 appVersion 输出净 balance 或旧版待结+余额字段(合并仅由控制台脚本执行)
- $list[$key] = AccountMoneyClass::formatMoneyForClient($list[$key]);
- //前端使用,走播,预订客户添加新客户
- $list[$key]['zbNum'] = '';
- $list[$key]['zbPrice'] = '';
- //片区划分
- $distId = $val['distId'] ?? 0;
- $distInfo = $distMap[$distId] ?? [];
- $distName = $distInfo['name'] ?? '';
- //注意区别 dist 字段,dist字段是地图里的区
- $list[$key]['distName'] = $distName;
- //超时未下单
- $recentExpend = $val['recentExpend'] ?? '';
- $list[$key]['overTimeUnExpend'] = 0;
- if ($recentExpend == '0000-00-00 00:00:00') {
- $list[$key]['overTimeUnExpend'] = 10000;
- } else {
- $recentExpendTime = strtotime($recentExpend);
- $seven = bcmul(86400, 7);
- $sevenTime = bcadd($recentExpendTime, $seven);
- if ($sevenTime < time()) {
- $list[$key]['overTimeUnExpend'] = 7;
- }
- $month = bcmul(86400, 30);
- $monthTime = bcadd($recentExpendTime, $month);
- if ($monthTime < time()) {
- $list[$key]['overTimeUnExpend'] = 30;
- }
- $halfYear = bcmul(86400, 180);
- $halfYearTime = bcadd($recentExpendTime, $halfYear);
- if ($halfYearTime < time()) {
- $list[$key]['overTimeUnExpend'] = 180;
- }
- $year = bcmul(86400, 365);
- $yearTime = bcmul($recentExpendTime, $year);
- if ($yearTime < time()) {
- $list[$key]['overTimeUnExpend'] = 365;
- }
- }
- }
- return $list;
- }
- //欠款权限开关 ssh 2021.1.8
- public static function debt($custom, $debt)
- {
- $id = $custom['id'] ?? 0;
- self::updateById($id, ['debt' => $debt]);
- return true;
- }
- //采购查看权限的黑白名单设置 shizhongqi 2021.08.08
- public static function black($custom, $black)
- {
- $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('您无法操作此客户');
- }
- }
- /**
- * 调用方已 lockAccountPair 时,校验 custom/ghs 净余额一致。
- */
- protected static function assertPairBalanceEqual($custom, $ghs)
- {
- if (empty($ghs)) {
- return;
- }
- $customBal = bcadd((string)($custom->balance ?? '0'), '0', 2);
- $ghsBal = bcadd((string)($ghs->balance ?? '0'), '0', 2);
- if (bccomp($customBal, $ghsBal, 2) !== 0) {
- util::fail('余额不一致,请联系管理员');
- }
- }
- /**
- * 开单扣减净 balance:写 custom(及成对 ghs),调用方须已 FOR UPDATE 锁行。
- */
- protected static function savePairBalanceAfterDeduct($custom, $ghs, $newBalance)
- {
- $newBalance = bcadd((string)$newBalance, '0', 2);
- $custom->balance = $newBalance;
- $custom->isDebt = bccomp($newBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
- $custom->save(false, ['balance', 'isDebt']);
- if (empty($ghs)) {
- return;
- }
- AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
- $ghs->balance = $newBalance;
- $ghs->debt = bccomp($newBalance, '0', 2) < 0 ? 2 : 1;
- $ghs->save(false, ['balance', 'debt']);
- self::assertPairBalanceEqual($custom, $ghs);
- }
- /**
- * hdApp 供货商余额明细:事项单号优先用 xhCg.orderSn,无采购单时再退回销售单号。
- *
- * @param object|array $order 供货商销售单
- * @return string
- */
- protected static function resolveXhCgOrderSnForHdEvent($order)
- {
- $saleOrderSn = is_object($order) ? ($order->orderSn ?? '') : ($order['orderSn'] ?? '');
- $purchaseId = intval(is_object($order) ? ($order->purchaseId ?? 0) : ($order['purchaseId'] ?? 0));
- if ($purchaseId <= 0) {
- return $saleOrderSn;
- }
- $cg = PurchaseClass::getById($purchaseId, true);
- if (empty($cg)) {
- return $saleOrderSn;
- }
- $cgOrderSn = $cg->orderSn ?? '';
- return $cgOrderSn !== '' ? $cgOrderSn : $saleOrderSn;
- }
- /**
- * 花店采购余额付售后:成对账户原路退回(与 payToChangeBalance 对称)。
- * 调用方须已 lockAccountPair;同时写客户余额明细与 xhGhs 余额明细(hdApp 可查)。
- */
- public static function refundBalancePayToPair($custom, $ghs, $refund, $order)
- {
- AccountMoneyClass::ensureCustomMoneyReady($custom, true);
- if (!empty($ghs)) {
- AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
- self::assertPairBalanceEqual($custom, $ghs);
- }
- $refundPrice = bcadd((string)($refund->refundPrice ?? '0'), '0', 2);
- if (bccomp($refundPrice, '0', 2) <= 0) {
- return;
- }
- $newBalance = bcadd((string)($custom->balance ?? '0'), $refundPrice, 2);
- self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
- $relateId = $refund->id ?? 0;
- $customId = $custom->id ?? 0;
- $customName = $custom->name ?? '';
- $saleOrderSn = $order->orderSn ?? '';
- $capitalType = dict::getDict('capitalType', 'saleRefund', 'id');
- $payTime = $order->payTime ?? '';
- $change = [
- 'relateId' => $relateId,
- 'customId' => $customId,
- 'customName' => $customName,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'capitalType' => $capitalType,
- 'amount' => $refundPrice,
- 'balance' => $newBalance,
- 'io' => 1,
- 'payWay' => dict::getDict('payWay', 'balancePay'),
- 'event' => "订单售后退款,单号:{$saleOrderSn}",
- 'sjId' => $refund->sjId ?? 0,
- 'shopId' => $refund->shopId ?? 0,
- 'mainId' => $order->mainId ?? 0,
- 'payTime' => $payTime,
- 'staffId' => 0,
- 'staffName' => '',
- 'side' => 0,
- 'fromType' => dict::getDict('fromType', 'shop'),
- 'remark' => '',
- ];
- CustomBalanceChangeClass::add($change, true);
- self::addGhsRefundBalanceChange($custom, $ghs, $refund, $order, $refundPrice, $newBalance);
- }
- /** 花店采购余额退款:记 xhGhs 余额变动(与 addGhsOrderBalanceChange 对称,hdApp 余额明细展示) */
- protected static function addGhsRefundBalanceChange($custom, $ghs, $refund, $order, $amount, $newBalance)
- {
- if (empty($ghs)) {
- return;
- }
- // hd 端明细展示采购单号 xhCg.orderSn
- $orderSn = self::resolveXhCgOrderSnForHdEvent($order);
- $customShopId = $custom->shopId ?? 0;
- $customShop = ShopClass::getById($customShopId, true);
- $gbData = [
- 'ghsId' => $ghs->id ?? 0,
- 'relateId' => $refund->id ?? 0,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'capitalType' => dict::getDict('capitalType', 'saleRefund', 'id'),
- 'amount' => $amount,
- 'balance' => $newBalance,
- 'io' => 1,
- 'side' => 0,
- 'onlinePay' => 1,
- 'payWay' => dict::getDict('payWay', 'balancePay'),
- 'fromType' => dict::getDict('fromType', 'shop'),
- 'event' => "订单售后退款,单号:{$orderSn}",
- 'sjId' => $customShop->sjId ?? ($custom->sjId ?? 0),
- 'mainId' => $customShop->mainId ?? 0,
- 'shopId' => $customShopId,
- 'remark' => '',
- ];
- GhsBalanceChangeClass::add($gbData, true);
- }
- protected static function addGhsOrderBalanceChange($custom, $ghs, $order, $amount, $newBalance, $bookOrder = false)
- {
- if (empty($ghs)) {
- return;
- }
- // hd 端明细展示采购单号 xhCg.orderSn
- $orderSn = self::resolveXhCgOrderSnForHdEvent($order);
- $event = '购买花材,单号:' . $orderSn;
- if ($bookOrder) {
- $event = '预订单多退少补,单号:' . $orderSn;
- }
- $customShopId = $custom->shopId ?? 0;
- $customShop = ShopClass::getById($customShopId, true);
- $gbData = [
- 'ghsId' => $ghs->id ?? 0,
- 'relateId' => $order->id ?? 0,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'capitalType' => dict::getDict('capitalType', 'xhGhsOrder', 'id'),
- 'amount' => $amount,
- 'balance' => $newBalance,
- 'io' => 0,
- 'side' => 0,
- 'onlinePay' => 1,
- 'payWay' => $order->payWay ?? 0,
- 'fromType' => dict::getDict('fromType', 'shop'),
- 'event' => $event,
- 'sjId' => $customShop->sjId ?? ($custom->sjId ?? 0),
- 'mainId' => $customShop->mainId ?? 0,
- 'shopId' => $customShopId,
- 'remark' => '',
- ];
- GhsBalanceChangeClass::add($gbData, true);
- }
- /**
- * 充值退款:扣减成对客户/供货商净余额(调用方须已 lockAccountPair)。
- *
- * @return string 扣减后的净余额
- */
- public static function deductPairBalanceForRefund($custom, $ghs, $amount)
- {
- AccountMoneyClass::ensureCustomMoneyReady($custom, true);
- if (!empty($ghs)) {
- AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
- self::assertPairBalanceEqual($custom, $ghs);
- }
- $amount = bcadd((string)$amount, '0', 2);
- if (bccomp($amount, '0', 2) <= 0) {
- util::fail('退款金额有误');
- }
- $beforeBalance = bcadd((string)($custom->balance ?? '0'), '0', 2);
- if (bccomp($beforeBalance, $amount, 2) < 0) {
- util::fail('客户余额不足,本笔充值可能已用于销账或消费');
- }
- $newBalance = bcsub($beforeBalance, $amount, 2);
- self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
- return $newBalance;
- }
- /**
- * 后台手动减少客户净余额(只减正余额部分,不增加待结)。
- * 调用方须已通过 lockAccountPair 锁定 custom/ghs。
- */
- public static function manualDeductBalance($shop, $custom, $ghs, $amount, $params = [])
- {
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- AccountMoneyClass::ensureCustomMoneyReady($custom, true);
- if (!empty($ghs)) {
- AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
- self::assertPairBalanceEqual($custom, $ghs);
- }
- $amount = bcadd((string)$amount, '0', 2);
- if (bccomp($amount, '0', 2) <= 0) {
- util::fail('金额要大于0');
- }
- $beforeBalance = bcadd((string)($custom->balance ?? '0'), '0', 2);
- if (bccomp($beforeBalance, '0', 2) <= 0) {
- util::fail('没有可减余额');
- }
- if (bccomp($beforeBalance, $amount, 2) < 0) {
- util::fail('余额不够扣');
- }
- $newBalance = bcsub($beforeBalance, $amount, 2);
- self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
- $remark = $params['remark'] ?? '';
- $staffId = intval($params['staffId'] ?? 0);
- $staffName = $params['staffName'] ?? '';
- $mainId = $shop->mainId ?? 0;
- $shopId = $shop->id ?? 0;
- $sjId = $shop->sjId ?? 0;
- $customId = $custom->id ?? 0;
- $customName = $custom->name ?? '';
- $capitalType = dict::getDict('capitalType', 'deduct', 'id');
- $fromType = dict::getDict('fromType', 'shop');
- $event = '手动减少';
- CustomBalanceChangeClass::add([
- 'customId' => $customId,
- 'customName' => $customName,
- 'relateId' => 0,
- 'onlinePay' => 1,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $newBalance,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'io' => 0,
- 'side' => 0,
- 'payWay' => 0,
- 'fromType' => $fromType,
- 'event' => $event,
- 'mainId' => $mainId,
- 'shopId' => $shopId,
- 'sjId' => $sjId,
- 'remark' => $remark,
- ], true);
- if (!empty($ghs)) {
- $customShopId = $custom->shopId ?? 0;
- $customShop = ShopClass::getById($customShopId, true);
- GhsBalanceChangeClass::add([
- 'ghsId' => $ghs->id ?? 0,
- 'relateId' => 0,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $newBalance,
- 'io' => 0,
- 'side' => 0,
- 'onlinePay' => 1,
- 'payWay' => 0,
- 'fromType' => $fromType,
- 'event' => $event,
- 'sjId' => $customShop->sjId ?? 0,
- 'mainId' => $customShop->mainId ?? 0,
- 'shopId' => $customShopId,
- 'remark' => $remark,
- ], true);
- }
- }
- /**
- * 开单余额支付:扣减净 balance 并记购买流水(须传入 payAfter 已 lockAccountPair 的 custom/ghs)。
- */
- public static function payToChangeBalance($order, $custom, $ghs = null)
- {
- $actPrice = bcadd((string)($order->actPrice ?? '0'), '0', 2);
- if (bccomp($actPrice, '0', 2) <= 0) {
- return;
- }
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- AccountMoneyClass::ensureCustomMoneyReady($custom, true);
- if (!empty($ghs)) {
- AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
- self::assertPairBalanceEqual($custom, $ghs);
- }
- $beforeBalance = bcadd((string)($custom->balance ?? '0'), '0', 2);
- if (bccomp($beforeBalance, $actPrice, 2) < 0) {
- util::fail('余额不足');
- }
- $newBalance = bcsub($beforeBalance, $actPrice, 2);
- self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
- $orderSn = $order->orderSn ?? '';
- $customId = $custom->id ?? 0;
- $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
- $change = [
- 'relateId' => $order->id ?? 0,
- 'customId' => $customId,
- 'customName' => $custom->name ?? '',
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'capitalType' => $capitalType,
- 'amount' => $actPrice,
- 'balance' => $newBalance,
- 'io' => 0,
- 'payWay' => dict::getDict('payWay', 'balancePay'),
- 'event' => '购买花材,单号:' . $orderSn,
- 'sjId' => $order->sjId ?? 0,
- 'shopId' => $order->shopId ?? 0,
- 'mainId' => $order->mainId ?? 0,
- 'staffId' => $order->shopAdminId ?? 0,
- 'staffName' => $order->shopAdminName ?? '',
- 'side' => 0,
- 'fromType' => dict::getDict('fromType', 'shop'),
- 'remark' => '',
- ];
- CustomBalanceChangeClass::add($change, true);
- self::addGhsOrderBalanceChange($custom, $ghs, $order, $actPrice, $newBalance, false);
- }
- /**
- * 挂账开单时若账户有正余额且不足本单:用余额先结本单一部分(只改 remainDebtPrice,不另扣 balance)。
- */
- protected static function inlineOrderBalancePartialClear($custom, $order, $clearAmount)
- {
- $clearAmount = bcadd((string)$clearAmount, '0', 2);
- if (bccomp($clearAmount, '0', 2) <= 0) {
- return;
- }
- $orderId = intval($order->id ?? 0);
- if ($orderId <= 0) {
- return;
- }
- $shopId = intval($order->shopId ?? 0);
- $shop = ShopClass::getById($shopId, true);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $staffId = intval($order->shopAdminId ?? 0);
- $staffName = $order->shopAdminName ?? '';
- $sjId = $shop->sjId ?? 0;
- $orderSn = $order->orderSn ?? '';
- OrderClearClass::expireAwaitPayClears(intval($custom->id ?? 0));
- $clear = OrderClearClass::clearWithAmountMap([
- 'customId' => $custom->id ?? 0,
- 'ghsShopAdminId' => $staffId,
- 'ghsShopId' => $shopId,
- 'ghsShopAdminName' => $staffName,
- 'modifyPrice' => $clearAmount,
- 'payWay' => dict::getDict('payWay', 'balancePay'),
- 'complete' => 0,
- 'remark' => '开单余额抵挂账',
- ], [
- ['orderId' => $orderId, 'clearAmount' => $clearAmount, 'orderSn' => $orderSn],
- ], $sjId, $shopId);
- OrderClearClass::applyNetBalanceClear($clear, dict::getDict('payWay', 'balancePay'), ['skipCashMoney' => true]);
- }
- /**
- * 挂账开单:增加客户待结(改造后 balance 减少,只记余额变动,不再记挂账变动)
- * ssh 20220306
- */
- public static function cgDebtAmountAdd($custom, $order, $bookOrder = false, $ghs = null)
- {
- // 挂账开单:合并后 balance 减少,不再写 CustomDebtChange
- AccountMoneyClass::ensureCustomMoneyReady($custom, true);
- if (!empty($ghs)) {
- AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
- self::assertPairBalanceEqual($custom, $ghs);
- }
- $amount = bcadd((string)($order->remainDebtPrice ?? '0'), '0', 2);
- if (bccomp($amount, '0', 2) <= 0) {
- return;
- }
- $beforeBalance = bcadd((string)($custom->balance ?? '0'), '0', 2);
- $positiveBalance = bccomp($beforeBalance, '0', 2) > 0 ? $beforeBalance : '0.00';
- if (bccomp($positiveBalance, $amount, 2) >= 0) {
- util::fail('余额充足,请用余额支付');
- }
- if (bccomp($positiveBalance, '0', 2) > 0) {
- self::inlineOrderBalancePartialClear($custom, $order, $positiveBalance);
- }
- $custom->isDebt = CustomClass::IS_DEBT_YES;
- $debtLimit = $custom->debtLimit ?? 0;
- if (!$bookOrder) {
- if (isset($order->shopAdminId) && $order->shopAdminId == 0) {
- $outstanding = AccountMoneyClass::getOutstandingDebt($custom);
- if (bccomp($outstanding, $debtLimit, 2) > 0) {
- if ($custom->live == 1) {
- util::fail("已欠款{$outstanding},请先结账哦");
- }
- }
- }
- }
- $newBalance = bcsub($beforeBalance, $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("【开单动作 | 客户】变化前余额:{$beforeBalance} 变化金额:{$amount} 变化后余额:{$newBalance}", '15280215347');
- }
- }
- self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
- $relateId = $order->id ?? 0;
- $customId = $custom->id ?? 0;
- $customName = $custom->name ?? '';
- $orderSn = $order->orderSn ?? '';
- $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
- $event = '购买花材,单号:' . $orderSn;
- if ($bookOrder) {
- $event = '预订单多退少补,单号:' . $orderSn;
- }
- $change = [
- 'relateId' => $relateId,
- 'customId' => $customId,
- 'customName' => $customName,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $newBalance,
- 'io' => 0,
- 'payWay' => $order->payWay,
- 'event' => $event,
- 'sjId' => $order->sjId,
- 'shopId' => $order->shopId,
- 'mainId' => $order->mainId,
- 'staffId' => 0,
- 'staffName' => '',
- 'side' => 0,
- 'fromType' => dict::getDict('fromType', 'shop'),
- 'remark' => '',
- ];
- CustomBalanceChangeClass::add($change, true);
- self::addGhsOrderBalanceChange($custom, $ghs, $order, $amount, $newBalance, $bookOrder);
- $mainId = $order->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息8');
- }
- $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);
- }
- /**
- * 结账收回挂账:balance 增加(原 debtAmount 减少逻辑)
- * ssh 20220306
- */
- public static function clearDebtAmountReduce($custom, $ghs, $amount, $clear)
- {
- // 结账收回挂账:balance 增加,只记余额变动
- AccountMoneyClass::ensureCustomMoneyReady($custom, true);
- $beforeBalance = $custom->balance ?? '0.00';
- $newBalance = bcadd($beforeBalance, $amount, 2);
- if (bccomp($beforeBalance, '0', 2) < 0 && bccomp($newBalance, '0', 2) > 0) {
- noticeUtil::push("客户账户余额:{$beforeBalance} 本次结账金额:{$amount} 客户ID:{$custom->id} 【里外金额有问题】", '15280215347');
- util::fail('收款失败,结账后欠款金额会出现负数');
- }
- $custom->isDebt = bccomp($newBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
- $custom->balance = $newBalance;
- $custom->save(false, ['balance', 'isDebt']);
- $relateId = $clear->id ?? 0;
- $customId = $custom->id ?? 0;
- $customName = $custom->name ?? '';
- $capitalType = dict::getDict('capitalType', 'ghsXsClear', 'id');
- $orderSn = $clear->orderSn ?? '';
- $change = [
- 'relateId' => $relateId,
- 'customId' => $customId,
- 'customName' => $customName,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $newBalance,
- 'io' => 1,
- 'payWay' => 0,
- 'event' => "结账,单号:{$orderSn}",
- 'sjId' => $ghs->sjId,
- 'shopId' => $ghs->shopId,
- 'mainId' => $ghs->mainId,
- 'staffId' => 0,
- 'staffName' => '',
- 'side' => 0,
- 'fromType' => dict::getDict('fromType', 'shop'),
- 'remark' => '',
- ];
- CustomBalanceChangeClass::add($change, true);
- $mainId = $ghs->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息9');
- }
- $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);
- }
- /**
- * 售后退款:减少待结(balance 增加),只记余额变动
- * ssh 20220306
- */
- public static function refundDebtAmountReduce($custom, $refund, $order)
- {
- // 退款减待结:balance 增加,只记余额变动
- AccountMoneyClass::ensureCustomMoneyReady($custom, true);
- $refundPrice = $refund->refundPrice ?? 0;
- if (bccomp($refundPrice, '0', 2) <= 0) {
- return;
- }
- $newBalance = bcadd($custom->balance ?? '0.00', $refundPrice, 2);
- $custom->balance = $newBalance;
- $custom->isDebt = bccomp($newBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
- $custom->save(false, ['balance', 'isDebt']);
- $relateId = $refund->id ?? 0;
- $customId = $custom->id ?? 0;
- $customName = $custom->name ?? '';
- $saleOrderSn = $order->orderSn ?? '';
- $capitalType = dict::getDict('capitalType', 'saleRefund', 'id');
- $payTime = $order->payTime;
- $change = [
- 'relateId' => $relateId,
- 'customId' => $customId,
- 'customName' => $customName,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'capitalType' => $capitalType,
- 'amount' => $refundPrice,
- 'balance' => $newBalance,
- 'io' => 1,
- 'payWay' => 0,
- 'event' => "订单号:{$saleOrderSn} 退款",
- 'sjId' => $refund->sjId,
- 'shopId' => $refund->shopId,
- 'mainId' => $order->mainId,
- 'payTime' => $payTime,
- 'staffId' => 0,
- 'staffName' => '',
- 'side' => 0,
- 'fromType' => dict::getDict('fromType', 'shop'),
- 'remark' => '',
- ];
- CustomBalanceChangeClass::add($change, true);
- $mainId = $order->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息10');
- }
- $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);
- }
- public static function exportCustom($where, $sort, $mainId)
- {
- $list = self::getAllByCondition($where, $sort, '*', null);
- if (empty($list)) {
- util::fail('没有找到客户');
- }
- $list = self::groupBaseInfo($list);
- $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
- require_once($phpExcelFile . 'Classes/PHPExcel.php');
- $objPHPExcel = new \PHPExcel();
- $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
- ->setLastModifiedBy("Maarten Balliauw")
- ->setTitle("Office 2007 XLSX Document")
- ->setSubject("Office 2007 XLSX Document")
- ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
- ->setKeywords("office 2007 openxml php")
- ->setCategory("file");
- $ghsTitle = '供货商';
- $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
- $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
- $objPHPExcel->getActiveSheet()->setCellValue('B1', '客户');
- $objPHPExcel->getActiveSheet()->setCellValue('C1', '手机号');
- $objPHPExcel->getActiveSheet()->setCellValue('D1', '欠款');
- $objPHPExcel->getActiveSheet()->setCellValue('E1', '余额');
- $objPHPExcel->getActiveSheet()->setCellValue('F1', '待结金额');
- $objPHPExcel->getActiveSheet()->setCellValue('G1', '最近下单');
- $objPHPExcel->getActiveSheet()->setCellValue('H1', '累计消费');
- $objPHPExcel->getActiveSheet()->setCellValue('I1', '累计消费次数');
- //设置宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
- $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
- $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
- $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
- $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
- $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);
- $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
- $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20);
- //加粗
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('G1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('H1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('I1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $baseRow = 2;
- foreach ($list as $key => $custom) {
- $i = $baseRow + $key;
- $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['id']);
- $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['name']);
- $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['mobile'], \PHPExcel_Cell_DataType::TYPE_STRING);
- $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['debtAmount']);
- $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['balance']);
- $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['remainDebtAmount']);
- if ($custom['recentExpend'] == '0000-00-00 00:00:00') {
- $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, '没有下过单');
- } else {
- $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $custom['recentExpend']);
- }
- $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $custom['buyAmount']);
- $objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $custom['buyNum']);
- //居左
- $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('G' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('H' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('I' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- }
- $fileName = '客户-' . date("m-d");
- $objPHPExcel->getActiveSheet()->setTitle($fileName);
- $objPHPExcel->setActiveSheetIndex(0);
- $dir = './priceTable/' . $mainId;
- if (file_exists($dir) == false) {
- mkdir($dir, 0777, true);
- }
- $date = $fileName;
- $file = $date . '.xls';
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- if (file_exists($dir . '/' . $file)) {
- unlink($dir . '/' . $file);
- }
- $objWriter->save($dir . '/' . $file);
- $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
- util::success(['file' => $fileUrl, 'shortFile' => $file]);
- }
- public static function showTotalBalance($pfShopId = 0, $lsShopId = 0)
- {
- ini_set('memory_limit', '2045M');
- set_time_limit(0);
- $pfAmount = 0;
- $lsAmount = 0;
- if (!empty($pfShopId)) {
- $custom = CustomClass::getAllByCondition(['ownShopId' => $pfShopId], null, '*', null, true);
- if (!empty($custom)) {
- foreach ($custom as $c) {
- $new = AccountMoneyClass::getNetBalanceFromRow($c);
- $pfAmount = bcadd($pfAmount, $new, 2);
- }
- }
- }
- if (!empty($lsShopId)) {
- $lsCustom = \bizHd\custom\classes\CustomClass::getAllByCondition(['shopId' => $lsShopId], null, '*', null, true);
- if (!empty($lsCustom)) {
- foreach ($lsCustom as $lc) {
- $balance = $lc->balance ?? 0;
- $lsAmount = bcadd($balance, $lsAmount, 2);
- }
- }
- }
- return ['pfAmount' => $pfAmount, 'lsAmount' => $lsAmount];
- }
- }
|