| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614 |
- <?php
- namespace bizGhs\clear\classes;
- use biz\ghs\classes\GhsClass;
- use biz\shop\classes\ShopClass;
- use bizGhs\base\classes\BaseClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\clear\classes\OrderCgClearClass;
- use bizGhs\ghs\classes\GhsBalanceChangeClass;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\order\classes\PurchaseOrderClass;
- use bizGhs\shop\classes\MainClass;
- use bizGhs\shop\classes\ShopMoneyClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\purchase\classes\PurchaseClearClass;
- use common\components\dict;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\util;
- use Yii;
- use common\components\lakala\Lakala;
- class ClearClass extends BaseClass
- {
- public static $baseFile = '\bizGhs\clear\models\Clear';
- const STATUS_AWAIT_PAY = 1;
- const STATUS_HAS_PAY = 2;
- const STATUS_EXPIRE = 3;
- public static function refundMoney($shop, $clear)
- {
- $refundOrderSn = orderSn::getClearRefundSn();
- $refundPrice = $clear->actPrice ?? 0;
- $orderSn = $clear->orderSn ?? '';
- $refundFee = bcmul($refundPrice, 100);
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $shop->lklSjNo,
- 'term_no' => $shop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $refundReason = '';
- $thirdNo = $clear->thirdNo ?? '';
- $backParams = [
- 'refundSn' => $refundOrderSn,
- 'thirdNo' => $thirdNo,
- 'refundAmount' => $refundFee,
- 'refundReason' => $refundReason,
- ];
- $response = $laResource->refund($backParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $errMsg = $response['msg'] ?? '';
- return ['status' => 0, 'msg' => $errMsg];
- } else {
- $sjName = $shop->merchantName;
- $shopName = $shop->shopName;
- $name = $shopName == '首店' ? $sjName : $sjName . '-' . $shopName;
- noticeUtil::push("取消的结账单,客户的钱已原路退回 success {$orderSn} {$name}", '15280215347');
- return ['status' => 1, 'msg' => '退款成功'];
- }
- }
- public static function addOrder($data)
- {
- $orderSn = orderSn::getPurchaseClearSn();
- $idJson = $data['id'];
- $idArr = json_decode($idJson, true);
- $arr = array_column($idArr, 'id');
- if (empty($arr)) {
- util::fail('请选择采购单');
- }
- $purchaseList = PurchaseOrderClass::getAllByCondition(['id' => ['in', $arr]], null, ['id', 'orderSn', 'actPrice', 'realPrice', 'debt', 'ghsId', 'debtPrice', 'remainDebtPrice'], null, true);
- if (empty($purchaseList)) {
- util::fail('请选择采购单');
- }
- $totalPrice = 0;
- $currentGhsId = $data['ghsId'] ?? 0;
- foreach ($purchaseList as $purchase) {
- if ($purchase->debt != PurchaseOrderClass::DEBT_YES) {
- util::fail('请选择有欠款的采购单');
- }
- if ($purchase->ghsId != $currentGhsId) {
- util::fail('请选择同个供货商的订单');
- }
- // 结账口径:剩余待结;历史单未回填时回落 actPrice
- $remain = bcadd((string)($purchase->remainDebtPrice ?? '0'), '0', 2);
- if (bccomp($remain, '0', 2) <= 0) {
- $remain = bcadd((string)($purchase->actPrice ?? '0'), '0', 2);
- }
- $totalPrice = bcadd($totalPrice, $remain, 2);
- }
- $ghs = GhsClass::getById($currentGhsId, true);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- GhsClass::valid($ghs, $data['shopId']);
- $ghsName = $ghs->name ?? '';
- $ghsAvatar = $ghs->avatar ?? '';
- $ghsMobile = $ghs->mobile ?? '';
- $customId = $ghs->customId ?? 0;
- $custom = CustomClass::getById($customId, true);
- $customName = $custom->name ?? '';
- $customAvatar = $custom->avatar ?? '';
- $customMobile = $custom->mobile ?? '';
- $data['ghsName'] = $ghsName;
- $data['ghsAvatar'] = $ghsAvatar;
- $data['ghsMobile'] = $ghsMobile;
- $data['customName'] = $customName;
- $data['customAvatar'] = $customAvatar;
- $data['customMobile'] = $customMobile;
- $data['prePrice'] = $totalPrice;
- $realPrice = $totalPrice;
- if (isset($data['modifyPrice']) && !empty($data['modifyPrice'])) {
- if (is_numeric($data['modifyPrice']) == false) {
- util::fail('实收金额错误');
- }
- $modifyPrice = $data['modifyPrice'] ?? 0.00;
- if ($modifyPrice > $totalPrice) {
- util::fail('实收金额不能大于订单金额');
- }
- if ($modifyPrice < $totalPrice) {
- $data['discountAmount'] = bcsub($totalPrice, $modifyPrice, 2);
- $data['discountType'] = dict::getDict('discountType', 'discount');
- }
- $realPrice = $modifyPrice;
- }
- $data['actPrice'] = $realPrice;
- $data['realPrice'] = $realPrice;
- $data['orderSn'] = $orderSn;
- // 生成并保存 salt 字段,用于结账单的验签和防篡改分享链接
- $data['salt'] = $data['salt'] ?? \common\components\stringUtil::charsShuffleLowerCase(10);
- $deadTime = time() + 120;
- $data['deadline'] = date("Y-m-d H:i:s", $deadTime);
- $data['status'] = self::STATUS_AWAIT_PAY;
- $order = self::add($data, true);
- $pairs = OrderCgClearClass::buildPairsFromGhsCgOrders($purchaseList);
- OrderCgClearClass::bindRelations($order, $pairs);
- $payWay = $data['payWay'] ?? dict::getDict('payWay', 'wxPay');
- self::complete($order, $payWay);
- OrderCgClearClass::markDoneByClearId($order->id);
- return $order;
- }
- /**
- * 【用途】作废该供货商下待付的 gys2KmGys 采购结账单(新建自动销欠前调用)
- */
- public static function expireAwaitPayPurchaseClears($ghsId, $shopId = 0)
- {
- $ghsId = intval($ghsId);
- if ($ghsId <= 0) {
- return;
- }
- $where = [
- 'ghsId' => $ghsId,
- 'clearStyle' => dict::getDict('clearStyle', 'gys2KmGys'),
- 'status' => self::STATUS_AWAIT_PAY,
- ];
- if (intval($shopId) > 0) {
- $where['shopId'] = intval($shopId);
- }
- $list = self::getAllByCondition($where, null, '*', null, true);
- if (empty($list)) {
- return;
- }
- foreach ($list as $item) {
- $item->status = self::STATUS_EXPIRE;
- $item->remark = ($item->remark ?? '') . ';售后返充自动销欠前作废待付结账单';
- $item->save(false, ['status', 'remark']);
- OrderCgClearClass::markExpireByClearId($item->id ?? 0);
- }
- }
- /**
- * 【用途】采购售后返充余额后 FIFO 销欠:建结账单并减 remain,不再加余额(钱已入账)。
- * 【为什么】对齐销售侧 afterCreditFifoClear,余额流水可挂 clearId/clearSn/clearAmount。
- *
- * @param object $ghs 买方供货商(已锁)
- * @param array $plan [['cgId'=>int,'clearAmount'=>string,'orderSn'=>string], ...]
- * @param array $meta shopId/sjId/staffId/staffName/payWay/remark
- * @return object|null 结账单;无计划时 null
- */
- public static function settlePurchaseFifoAfterCredit($ghs, array $plan, array $meta = [])
- {
- if (empty($ghs) || empty($plan)) {
- return null;
- }
- $planTotal = '0.00';
- $pairs = [];
- foreach ($plan as $row) {
- $cgId = intval($row['cgId'] ?? 0);
- $cut = bcadd((string)($row['clearAmount'] ?? '0'), '0', 2);
- if ($cgId <= 0 || bccomp($cut, '0', 2) <= 0) {
- continue;
- }
- $planTotal = bcadd($planTotal, $cut, 2);
- $pairs[] = [
- 'orderId' => 0,
- 'orderSn' => '',
- 'cgId' => $cgId,
- 'cgSn' => (string)($row['orderSn'] ?? ''),
- 'amount' => $cut,
- ];
- }
- if (empty($pairs) || bccomp($planTotal, '0', 2) <= 0) {
- return null;
- }
- $ghsId = intval($ghs->id ?? 0);
- $shopId = intval($meta['shopId'] ?? ($ghs->shopId ?? 0));
- $sjId = intval($meta['sjId'] ?? 0);
- self::expireAwaitPayPurchaseClears($ghsId, $shopId);
- $customId = intval($ghs->customId ?? 0);
- $custom = $customId > 0 ? CustomClass::getById($customId, true) : null;
- $payWay = isset($meta['payWay']) && is_numeric($meta['payWay'])
- ? intval($meta['payWay'])
- : intval(dict::getDict('payWay', 'balancePay'));
- $orderSn = orderSn::getPurchaseClearSn();
- $data = [
- 'ghsId' => $ghsId,
- 'ghsName' => $ghs->name ?? '',
- 'ghsAvatar' => $ghs->avatar ?? '',
- 'ghsMobile' => $ghs->mobile ?? '',
- 'customId' => $customId,
- 'customName' => $custom->name ?? '',
- 'customAvatar' => $custom->avatar ?? '',
- 'customMobile' => $custom->mobile ?? '',
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'clearStyle' => dict::getDict('clearStyle', 'gys2KmGys'),
- 'customShopAdminId' => intval($meta['staffId'] ?? 0),
- 'customShopId' => $shopId,
- 'customShopAdminName' => (string)($meta['staffName'] ?? ''),
- 'payWay' => $payWay,
- 'prePrice' => $planTotal,
- 'actPrice' => $planTotal,
- 'realPrice' => $planTotal,
- 'orderSn' => $orderSn,
- 'salt' => \common\components\stringUtil::charsShuffleLowerCase(10),
- 'deadline' => date('Y-m-d H:i:s', time() + 120),
- 'status' => self::STATUS_AWAIT_PAY,
- 'remark' => (string)($meta['remark'] ?? '采购售后返充余额自动销欠'),
- ];
- $clear = self::add($data, true);
- OrderCgClearClass::bindRelations($clear, $pairs, OrderCgClearClass::STATUS_HAS_PAY);
- // 只减采购单待结 / 更新 debtNum,不再加余额
- self::applyPurchasePaymentSettleOnly($clear, $pairs, $payWay);
- return $clear;
- }
- /**
- * 【用途】结账单已建好后:按明细减采购 remain,足额写真实 clearId;不加 xhGhs 余额。
- */
- protected static function applyPurchasePaymentSettleOnly($clear, array $pairs, $payWay)
- {
- if (empty($clear)) {
- util::fail('没有结账单');
- }
- if (intval($clear->status ?? 0) === self::STATUS_HAS_PAY) {
- // bind 时可能已标 HAS_PAY,仍允许走减 remain
- } else {
- $clear->status = self::STATUS_HAS_PAY;
- }
- $clear->payWay = $payWay;
- $clear->payTime = date('Y-m-d H:i:s');
- $clear->save(false);
- $clearId = intval($clear->id ?? 0);
- $clearedCount = 0;
- foreach ($pairs as $pair) {
- $cgId = intval($pair['cgId'] ?? 0);
- $cut = bcadd((string)($pair['amount'] ?? '0'), '0', 2);
- if ($cgId <= 0 || bccomp($cut, '0', 2) <= 0) {
- continue;
- }
- $purchase = PurchaseOrderClass::getById($cgId, true);
- if (empty($purchase) || intval($purchase->debt ?? 0) !== PurchaseOrderClass::DEBT_YES) {
- continue;
- }
- $remain = bcadd((string)($purchase->remainDebtPrice ?? '0'), '0', 2);
- if (bccomp($remain, '0', 2) <= 0) {
- $remain = bcadd((string)($purchase->actPrice ?? '0'), '0', 2);
- }
- $newRemain = bcsub($remain, $cut, 2);
- if (bccomp($newRemain, '0', 2) < 0) {
- $newRemain = '0.00';
- }
- $purchase->remainDebtPrice = $newRemain;
- $saveAttrs = ['remainDebtPrice', 'debt', 'clearId'];
- if (bccomp($newRemain, '0', 2) <= 0) {
- $purchase->debt = PurchaseOrderClass::DEBT_NO;
- $purchase->clearId = $clearId;
- $clearedCount++;
- } elseif (intval($purchase->clearId ?? 0) === 0) {
- $purchase->clearId = 1;
- }
- $purchase->save(false, $saveAttrs);
- }
- if ($clearedCount > 0) {
- $ghsId = intval($clear->ghsId ?? 0);
- $ghs = GhsClass::getLockById($ghsId);
- if (!empty($ghs)) {
- $ghs->debtNum = max(0, intval($ghs->debtNum ?? 0) - $clearedCount);
- $ghs->debt = bccomp((string)($ghs->balance ?? '0'), '0', 2) < 0
- ? GhsClass::DEBT_YES
- : (intval($ghs->debtNum) > 0 ? GhsClass::DEBT_YES : GhsClass::DEBT_NO);
- $ghs->save(false, ['debtNum', 'debt']);
- $customId = intval($ghs->customId ?? 0);
- if ($customId > 0) {
- $custom = CustomClass::getLockById($customId);
- if (!empty($custom)) {
- $custom->debtNum = max(0, intval($custom->debtNum ?? 0) - $clearedCount);
- $custom->isDebt = bccomp((string)($custom->balance ?? '0'), '0', 2) < 0
- ? CustomClass::IS_DEBT_YES
- : (intval($custom->debtNum) > 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO);
- $custom->save(false, ['debtNum', 'isDebt']);
- }
- }
- }
- }
- }
- /**
- * 【用途】售后返充流水回写结账审计字段(供 ghs 余额变动展示「本单销」)
- * 【注意】getByCondition 签名为 (condition, returnObject, order, field),
- * 勿把「id DESC」传到第 4 参,否则会当成 SELECT 字段导致查不到 clearId。
- */
- public static function patchGhsBalanceChangeClearAudit($ghsId, $relateId, $clear, $clearAmount = null)
- {
- $ghsId = intval($ghsId);
- $relateId = intval($relateId);
- $clearId = intval(is_object($clear) ? ($clear->id ?? 0) : 0);
- if ($ghsId <= 0 || $relateId <= 0 || $clearId <= 0) {
- return;
- }
- // 第 3 参 order、第 4 参 field;取最新一条入账流水挂结账单
- $row = GhsBalanceChangeClass::getByCondition([
- 'ghsId' => $ghsId,
- 'relateId' => $relateId,
- 'io' => 1,
- ], true, 'id DESC', '*');
- if (empty($row) || !method_exists($row, 'hasAttribute') || !$row->hasAttribute('clearId')) {
- return;
- }
- $row->clearId = $clearId;
- $row->clearSn = (string)(is_object($clear) ? ($clear->orderSn ?? '') : '');
- $row->clearAmount = bcadd(
- (string)($clearAmount !== null ? $clearAmount : (is_object($clear) ? ($clear->actPrice ?? 0) : 0)),
- '0',
- 2
- );
- $row->save(false, ['clearId', 'clearSn', 'clearAmount']);
- }
- //支付成功后的流程 ssh 2021.4.28
- public static function complete($order, $payWay)
- {
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- if (isset($order->status) == false || $order->status != self::STATUS_AWAIT_PAY) {
- util::fail('订单不是待付款状态');
- }
- $order->status = self::STATUS_HAS_PAY;
- $order->save();
- $clearId = $order->id;
- $arr = OrderCgClearClass::getCgIdsByClearId($clearId);
- if (empty($arr)) {
- $msg = "没有找到采购订单";
- util::fail($msg);
- }
- $list = PurchaseOrderClass::getAllByCondition(['id' => ['in', $arr]], null, '*', null, true);
- if (empty($list)) {
- $msg = "没有找到采购订单!";
- util::fail($msg);
- }
- //供货商欠款变更
- $ghsId = $order->ghsId;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商信息');
- }
- $ghsAddDebtKey = 'ghs_change_debt_' . $ghsId;
- $lock = util::lock($ghsAddDebtKey);
- if (!$lock) {
- util::fail("系统繁忙,请重试");
- }
- PurchaseOrderClass::applyPurchaseClearOnBuyerGhs($ghs, $order->prePrice, $order, count($arr));
- util::unlock($ghsAddDebtKey);
- $shopId = $ghs->shopId;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到供货商门店');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息19');
- }
- //门店应收客户款减少
- $currentGathering = bcsub($main->mayGathering, $order->actPrice, 2);
- $main->mayGathering = $currentGathering;
- $main->save();
- //采购单状态变更:结清剩余待结
- foreach ($list as $purchase) {
- $purchase->debt = PurchaseOrderClass::DEBT_NO;
- $purchase->remainDebtPrice = '0.00';
- $purchase->payWay = $payWay;
- $purchase->clearId = $clearId;
- $purchase->save(false, ['debt', 'remainDebtPrice', 'payWay', 'clearId']);
- }
- $custom = PurchaseOrderClass::applyPurchaseClearOnSupplierCustom($ghs, $order->actPrice, count($arr), $main);
- //应付供货商款减少
- $customShopId = $custom->shopId;
- $customShop = ShopClass::getLockById($customShopId);
- if (empty($customShop)) {
- util::fail('没有找到客户门店');
- }
- $customMainId = $customShop->mainId ?? 0;
- $customMain = MainClass::getLockById($customMainId);
- if (empty($customMain)) {
- util::fail('没有main信息20');
- }
- $currentMayPay = bcsub($customMain->mayPay, $order->actPrice, 2);
- $customMain->mayPay = $currentMayPay;
- if ($ghs->debtNum == 0) {
- $customMain->mayPayNum -= 1;
- }
- $customMain->save();
- //扣除现金
- if ($payWay == dict::getDict('payWay', 'cash')) {
- $balance = bcsub($customMain->money, $order->actPrice, 2);
- if ($balance < 0) {
- util::fail('现金不足');
- }
- $customMain->money = $balance;
- $customMain->save();
- $staffId = $order->customShopAdminId ?? 0;
- $staffName = $order->customShopAdminName ?? '';
- $event = $staffName . '买花结账支出' . $order->actPrice . '元,单号:' . $order->orderSn;
- $capitalType = dict::getDict('capitalType', 'ghsClearExpend', 'id');
- $data = [];
- $data['balance'] = $balance;
- $data['mainId'] = $customMainId;
- $data['sjId'] = 0;
- $data['amount'] = $order->actPrice ?? 0;
- $data['staffId'] = $staffId;
- $data['staffName'] = $staffName;
- $data['ptStyle'] = dict::getDict('ptStyle', 'ghs');
- $data['capitalType'] = $capitalType;
- $data['event'] = $event;
- $data['remark'] = '';
- ShopMoneyClass::addData($data);
- }
- }
- //供货商的采购信息 ssh 20210625
- public static function getGhsCgInfo($info)
- {
- $clearId = intval($info['id'] ?? 0);
- $cgArr = OrderCgClearClass::getCgIdsByClearId($clearId);
- $cgList = empty($cgArr) ? [] : PurchaseOrderClass::getAllByCondition(['id' => ['in', $cgArr]], null, 'id,orderSn,bigNum,smallNum,realPrice,addTime', null);
- $info['orderList'] = $cgList;
- $info['orderNum'] = count($cgArr);
- $clearStyle = $info['clearStyle'] ?? dict::getDict('clearStyle', 'gys2KmGys');
- $operator = $info['customShopAdminName'] ?? '';
- if ($clearStyle == dict::getDict('clearStyle', 'kmGys2gys')) {
- $operator = '商家';
- }
- $info['operator'] = $operator;
- return $info;
- }
- //零售的采购信息 ssh 20210625
- public static function getCgInfo($info)
- {
- $id = $info['id'] ?? 0;
- $deadline = $info['deadline'] ?? '';
- $deadTime = strtotime($deadline);
- $current = time();
- // 仅待结清且已过期时才取消,避免已结清账单被误改状态
- if ($current >= $deadTime && ($info['status'] ?? 0) == PurchaseClearClass::STATUS_AWAIT_PAY) {
- $n = date("Y-m-d H:i:s");
- $info['status'] = PurchaseClearClass::STATUS_EXPIRE;
- self::updateById($id, [
- 'status' => PurchaseClearClass::STATUS_EXPIRE,
- 'remark' => "账单过期,已自动取消,参数:{$deadline} {$n}",
- ]);
- }
- $clearId = intval($info['id'] ?? 0);
- $cgArr = OrderCgClearClass::getCgIdsByClearId($clearId);
- $cgList = empty($cgArr) ? [] : PurchaseClass::getAllByCondition(['id' => ['in', $cgArr]], null, '*', null);
- $info['orderList'] = self::buildPartialClearLinksForPurchaseOrders($cgList);
- $info['orderNum'] = count($cgArr);
- $clearStyle = $info['clearStyle'] ?? dict::getDict('clearStyle', 'hd2Gys');
- $operator = $info['customShopAdminName'] ?? '';
- if ($clearStyle == dict::getDict('clearStyle', 'gys2Hd')) {
- $operator = '商家';
- }
- $info['operator'] = $operator;
- return $info;
- }
- //完整信息 ssh 20210625
- public static function getSaleInfo($info)
- {
- $clearId = intval($info['id'] ?? 0);
- $saleArr = OrderCgClearClass::getOrderIdsByClearId($clearId);
- $saleList = empty($saleArr) ? [] : OrderClass::getAllByCondition(['id' => ['in', $saleArr]], null, '*', null);
- $info['orderList'] = self::buildPartialClearLinksForSaleOrders($saleList);
- $info['orderNum'] = count($saleArr);
- $type = $info['clearStyle'] ?? dict::getDict('clearStyle', 'gys2KmGys');
- $operator = $info['ghsShopAdminName'] ?? '';
- if ($type == dict::getDict('clearStyle', 'hd2Gys')) {
- $operator = '客户';
- }
- $info['operator'] = $operator;
- return $info;
- }
- /**
- * 结账单详情(供货商销售单):附加 orderClearList,与订单详情 OrderService 一致。
- */
- protected static function buildPartialClearLinksForSaleOrders($saleList)
- {
- if (empty($saleList)) {
- return [];
- }
- $out = [];
- foreach ($saleList as $order) {
- $row = self::normalizeOrderRow($order);
- $orderId = intval($row['id'] ?? 0);
- $row['remainDebtPrice'] = bcadd((string)($row['remainDebtPrice'] ?? '0'), '0', 2);
- $row['orderClearList'] = $orderId > 0
- ? OrderCgClearClass::listPaidClearRecordsForOrder($orderId)
- : [];
- $out[] = $row;
- }
- return $out;
- }
- /**
- * 结账单详情(花店采购单):附加 orderClearList;待结以批发销售单为准(采购单可能滞后)。
- */
- protected static function buildPartialClearLinksForPurchaseOrders($cgList)
- {
- if (empty($cgList)) {
- return [];
- }
- $out = [];
- foreach ($cgList as $purchase) {
- $row = self::normalizeOrderRow($purchase);
- $cgId = intval($row['id'] ?? 0);
- $saleId = intval($row['saleId'] ?? 0);
- $row['remainDebtPrice'] = self::resolvePurchaseRemainDebtPrice($row);
- $row['orderClearList'] = OrderCgClearClass::listPaidClearRecordsForPurchase($cgId, $saleId);
- $out[] = $row;
- }
- return $out;
- }
- /** 采购单待结:优先对齐 xhOrder.remainDebtPrice */
- protected static function resolvePurchaseRemainDebtPrice(array $row)
- {
- $remain = bcadd((string)($row['remainDebtPrice'] ?? '0'), '0', 2);
- $saleId = intval($row['saleId'] ?? 0);
- if ($saleId <= 0) {
- return $remain;
- }
- $sale = OrderClass::getById($saleId, true);
- if (empty($sale)) {
- return $remain;
- }
- return bcadd((string)($sale->remainDebtPrice ?? '0'), '0', 2);
- }
- /** 统一订单行为数组,便于前端附加 orderClearList */
- protected static function normalizeOrderRow($row)
- {
- if (is_array($row)) {
- return $row;
- }
- if (is_object($row) && method_exists($row, 'getAttributes')) {
- return $row->getAttributes();
- }
- return (array)$row;
- }
- }
|