| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace bizHd\ghs\classes;
- /**
- * 花店侧 xhGhs 关系账户(与 xhGhsCustom 成对,custom.ghsId 关联)
- *
- * 金额与 bizGhs\custom\classes\AccountMoneyClass 一致:写前 ensureGhsMoneyReady(仅行锁);
- * 挂账=balance 减少,结账/退款=balance 增加;只记 GhsBalanceChange,不再新增 GhsDebtChange(历史仍可查)。
- */
- use bizGhs\custom\classes\AccountMoneyClass;
- use common\components\dict;
- use common\components\noticeUtil;
- use common\components\util;
- use Yii;
- use bizHd\base\classes\BaseClass;
- class GhsClass extends BaseClass
- {
- public static $baseFile = '\bizHd\ghs\models\Ghs';
- /** 采购挂账开单:balance 减少(原 debtAmount 增加) */
- public static function cgDebtAmountAdd($ghs, $order, $bookOrder = false)
- {
- // hd 采购挂账:净 balance 减少
- AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
- // 开单余额抵挂账后 remainDebtPrice 变小,balance 仍按整单 debtPrice 扣减
- $amount = bcadd((string)($order->debtPrice ?? '0'), '0', 2);
- if (bccomp($amount, '0', 2) <= 0) {
- $amount = bcadd((string)($order->remainDebtPrice ?? '0'), '0', 2);
- }
- if (bccomp($amount, '0', 2) <= 0) {
- return;
- }
- $beforeBalance = $ghs->balance ?? '0.00';
- $ghs->debt = 2;
- $newBalance = bcsub($beforeBalance, $amount, 2);
- $ghs->balance = $newBalance;
- $ghs->save(false, ['balance', 'debt']);
- $relateId = $order->id ?? 0;
- $orderSn = $order->orderSn ?? '';
- $ghsId = $ghs->id ?? 0;
- $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
- $event = '购买花材,采购单号:' . $orderSn;
- if ($bookOrder) {
- $event = '预订单多退少补,单号' . $orderSn;
- }
- $change = [
- 'relateId' => $relateId,
- 'ghsId' => $ghsId,
- 'ptStyle' => dict::getDict('ptStyle', 'hd'),
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $newBalance,
- 'io' => 0,
- 'payWay' => $order->payWay,
- 'event' => $event,
- 'sjId' => $order->sjId ?? 0,
- 'shopId' => $order->shopId ?? 0,
- ];
- GhsBalanceChangeClass::add($change, true);
- }
- /** 采购结账:balance 增加(原 debtAmount 减少) */
- public static function clearDebtAmountReduce($ghs, $custom, $amount, $clear)
- {
- // hd 采购结账:净 balance 增加
- AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
- $beforeBalance = $ghs->balance ?? '0.00';
- $newBalance = bcadd($beforeBalance, $amount, 2);
- if (bccomp($beforeBalance, '0', 2) < 0 && bccomp($newBalance, '0', 2) > 0) {
- noticeUtil::push("结账没有成功,账户余额:{$beforeBalance} 本次结账金额:{$amount} 供货商ID:{$ghs->id}", '15280215347');
- util::fail('结账没有成功');
- }
- $ghs->balance = $newBalance;
- $ghs->debt = bccomp($newBalance, '0', 2) < 0 ? 2 : 1;
- $ghs->save(false, ['balance', 'debt']);
- $relateId = $clear->id ?? 0;
- $ghsId = $ghs->id ?? 0;
- $orderSn = $clear->orderSn ?? '';
- $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
- $change = [
- 'relateId' => $relateId,
- 'ghsId' => $ghsId,
- 'ptStyle' => dict::getDict('ptStyle', 'hd'),
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $newBalance,
- 'io' => 1,
- 'payWay' => 1,
- 'event' => "结账,单号:{$orderSn}",
- 'sjId' => $custom->sjId ?? 0,
- 'shopId' => $custom->shopId ?? 0,
- ];
- GhsBalanceChangeClass::add($change, true);
- }
- /** 采购退款:balance 增加 */
- public static function refundDebtAmountReduce($ghs, $refund, $cg)
- {
- // hd 采购退款:净 balance 增加
- AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
- $refundPrice = $refund->refundPrice ?? 0;
- if (bccomp($refundPrice, '0', 2) <= 0) {
- return;
- }
- $newBalance = bcadd($ghs->balance ?? '0.00', $refundPrice, 2);
- $ghs->balance = $newBalance;
- $ghs->save(false, ['balance']);
- $relateId = $refund->id ?? 0;
- $ghsId = $ghs->id ?? 0;
- $capitalType = dict::getDict('capitalType', 'cgRefund', 'id');
- $cgOrderSn = $cg->orderSn ?? '';
- $change = [
- 'relateId' => $relateId,
- 'ghsId' => $ghsId,
- 'ptStyle' => dict::getDict('ptStyle', 'hd'),
- 'capitalType' => $capitalType,
- 'amount' => $refundPrice,
- 'balance' => $newBalance,
- 'io' => 1,
- 'payWay' => 0,
- 'event' => "采购单:{$cgOrderSn} 退款",
- 'sjId' => $refund->sjId ?? 0,
- 'shopId' => $refund->shopId ?? 0,
- ];
- GhsBalanceChangeClass::add($change, true);
- }
- //验证供货商 ssh 20220307
- public static function valid($ghs, $shopId)
- {
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- if ($ghs->ownShopId != $shopId) {
- util::fail('不是您的供货商');
- }
- return true;
- }
- }
|