| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683 |
- <?php
- namespace bizHd\purchase\services;
- use biz\ghs\classes\GhsClass;
- use biz\shop\classes\ShopCapitalClass;
- use biz\shop\classes\ShopClass;
- use biz\shop\classes\ShopCouponClass;
- use biz\stat\classes\StatCgClass;
- use biz\stat\classes\StatCgGhsClass;
- use biz\stat\classes\StatOutClass;
- use bizGhs\custom\classes\AccountMoneyClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\custom\services\GhsRechargeSettleService;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\order\services\OrderService;
- use bizHd\base\services\BaseService;
- use bizHd\cg\classes\CgRefundClass;
- use bizGhs\clear\classes\OrderCgClearClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\purchase\classes\PurchaseItemClass;
- use bizHd\purchase\classes\PurchaseSnClass;
- use Yii;
- use bizHd\shop\classes\MainClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\util;
- class PurchaseService extends BaseService
- {
- public static $baseFile = '\bizHd\purchase\classes\PurchaseClass';
- public static function createPurchase($data, $ghs)
- {
- //零售采购单
- $respond = PurchaseClass::addPurchase($data, $ghs);
- //供货商销售单
- $ghsId = $data['ghsId'] ?? 0;
- $currentShopId = $ghs['shopId'] ?? 0;
- $currentSjId = $ghs['sjId'] ?? 0;
- $product = $data['product'] ?? [];
- $sendCost = $data['sendCost'] ?? 0;
- $packCost = $data['packCost'] ?? 0;
- $localOrder = $data['localOrder'] ?? 0;
- $customId = $ghs['customId'] ?? 0;
- $live = isset($ghs['live']) ? $ghs['live'] : 1;
- $custom = CustomClass::getById($customId);
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- //欠款超额,是否允许使用现金下单
- if ($custom['debtLimitPay'] == 1) {
- //欠款超额,不允许使用现金下单
- $debtLimit = $custom['debtLimit'] ?? 0;
- $debtAmount = $custom['debtAmount'] ?? 0;
- $actPrice = $respond->actPrice ?? 0;
- $finalDebtAmount = bcadd($debtAmount, $actPrice, 2);
- if ($finalDebtAmount > $debtLimit && $debtLimit > 0) {
- util::fail('请结清欠款再下单');
- }
- }
- $customName = $custom['name'] ?? '';
- $customNamePy = $custom['py'] ?? '';
- $customMobile = $custom['mobile'] ?? '';
- $purchaseId = $respond->id ?? 0;
- $shopAdminId = $data['shopAdminId'] ?? 0;
- $shopAdminName = $data['shopAdminName'] ?? '';
- $sendType = $data['sendType'] ?? 1;
- $transType = $data['transType'] ?? 0;
- $couponId = $data['couponId'] ?? 0;
- $wlName = $data['wlName'] ?? '';
- $discountType = $respond->discountType ?? dict::getDict('discountType', 'noDiscount');
- $discountAmount = $respond->discountAmount ?? 0;
- $discount = $respond->discount ?? 0;
- $remark = $data['remark'] ?? '';
- $xj = $data['xj'] ?? '';
- $sendTimeWant = isset($data['sendTimeWant']) && !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
- $orderType = 2;
- $book = $data['book'] ?? 0;
- $preSell = $data['presell'] ?? 0;
- $sendDistance = $data['sendDistance'] ?? 0;
- $mainId = $custom['ownMainId'] ?? 0;
- $fromType = dict::getDict('fromType', 'mall');
- $arr = [
- 'mainId' => $mainId,
- 'book' => $book,
- 'product' => $product,
- 'sjId' => $currentSjId,
- 'shopId' => $currentShopId,
- 'sendCost' => $sendCost,
- 'packCost' => $packCost,
- 'customId' => $customId,
- 'customName' => $customName,
- 'customNamePy' => $customNamePy,
- 'customMobile' => $customMobile,
- 'province' => $custom['province'] ?? '',
- 'city' => $custom['city'] ?? '',
- 'dist' => $custom['dist'] ?? '',
- 'address' => $custom['address'] ?? '',
- 'floor' => $custom['floor'] ?? '',
- 'fullAddress' => $custom['fullAddress'] ?? '',
- 'showAddress' => $custom['showAddress'] ?? '',
- 'long' => $custom['long'] ?? '',
- 'lat' => $custom['lat'] ?? '',
- 'purchaseId' => $purchaseId,
- 'sendType' => $sendType,
- 'customShopAdminId' => $shopAdminId,
- 'customShopAdminName' => $shopAdminName,
- 'ghsId' => $ghsId,
- 'cgStyle' => dict::getDict('cgStyle', 'hd'),
- 'couponId' => $couponId,
- 'discountType' => $discountType,
- 'discountAmount' => $discountAmount,
- 'remark' => $remark,
- 'xj' => $xj,
- 'sendTimeWant' => $sendTimeWant,
- 'discount' => $discount,
- 'orderType' => $orderType,
- 'fromType' => $fromType,
- 'presell' => $preSell,
- 'wlName' => $wlName,
- 'live' => $live,
- 'localOrder' => $localOrder,
- 'transType' => $transType,
- 'sendDistance' => $sendDistance,
- ];
- $result = OrderClass::addOrder($arr, $custom);
- $respond->saleId = $result['id'];
- $respond->save();
- return $respond;
- }
- //获取采购单信息 ssh 2021.1.24
- public static function getInfo($id, $showButton = false, $showProduct = false)
- {
- $purchase = PurchaseClass::getById($id, true);
- if (empty($purchase)) {
- util::fail('没有找到采购单');
- }
- $info = $purchase->attributes;
- $respond = PurchaseClass::groupPurchase([$info], $showButton, $showProduct);
- $result = current($respond);
- $purchaseId = intval($result['id'] ?? 0);
- $saleId = intval($result['saleId'] ?? 0);
- $result['orderClearList'] = OrderCgClearClass::listPaidClearRecordsForPurchase($purchaseId, $saleId);
- return $result;
- }
- //检查采购单
- public static function valid($info, $shopId)
- {
- if (empty($info)) {
- util::fail('没有找到采购单');
- }
- if (isset($info->shopId) && $info->shopId == $shopId) {
- return true;
- }
- util::fail('无法操作');
- }
- //采购记录 ssh 2021.1.24
- public static function getPurchaseList($where)
- {
- $data = PurchaseClass::getList('*', $where, 'addTime DESC');
- $data['list'] = self::groupInfo($data['list']);
- return $data;
- }
- //组合信息 ssh 2021.1.25
- public static function groupInfo($list)
- {
- if (empty($list)) {
- return $list;
- }
- foreach ($list as $key => $val) {
- $list[$key]['ghsAvatar'] = imgUtil::groupImg($val['ghsAvatar']);
- }
- return $list;
- }
- //所有的欠款采购单 ssh 2021.1.26
- public static function getDebtList($where)
- {
- $get = Yii::$app->request->get();
- $allowedPageSizes = [100, 200, 300];
- $pageSize = isset($get['pageSize']) && in_array((int)$get['pageSize'], $allowedPageSizes, true)
- ? (int)$get['pageSize'] : 100;
- $page = max(1, (int)($get['page'] ?? 1));
- $list = PurchaseClass::getAllList('*', $where, 'addTime DESC');
- $synced = [];
- $clearedSum = '0.00';
- $remainSum = '0.00';
- foreach ($list as $val) {
- $row = is_array($val) ? $val : (array)$val;
- $row = self::syncPurchaseDebtFromGhsOrder($row);
- if (intval($row['debt'] ?? 0) != PurchaseClass::DEBT_YES) {
- continue;
- }
- $row = self::enrichPurchaseDebtRow($row);
- $synced[] = $row;
- $clearedSum = bcadd($clearedSum, (string)($row['hasPayDebt'] ?? 0), 2);
- $remainSum = bcadd($remainSum, (string)($row['remainDebtPrice'] ?? 0), 2);
- }
- $totalNum = count($synced);
- $totalPage = max(1, (int)ceil($totalNum / $pageSize));
- if ($page > $totalPage) {
- $page = $totalPage;
- }
- $offset = ($page - 1) * $pageSize;
- return [
- 'list' => array_slice($synced, $offset, $pageSize),
- 'debtNum' => $totalNum,
- 'totalNum' => $totalNum,
- 'totalPage' => $totalPage,
- 'moreData' => $page < $totalPage ? 1 : 0,
- 'clearedAmount' => floatval($clearedSum),
- 'remainDebtAmount' => floatval($remainSum),
- ];
- }
- /** 列表展示:与 ghs order/debt-list 一致,按 debtPrice - remainDebtPrice 算已清 */
- public static function enrichPurchaseDebtRow(array $row)
- {
- $debtPrice = bcadd((string)($row['debtPrice'] ?? '0'), '0', 2);
- $remain = bcadd((string)($row['remainDebtPrice'] ?? '0'), '0', 2);
- $hasPayDebt = '0.00';
- if (bccomp($debtPrice, $remain, 2) > 0) {
- $hasPayDebt = bcsub($debtPrice, $remain, 2);
- }
- $row['hasPayDebt'] = floatval($hasPayDebt);
- return $row;
- }
- /**
- * 充值销账后批发订单已更新,采购单可能滞后:按 saleId 对齐 remainDebtPrice / debt。
- */
- public static function syncPurchaseDebtFromGhsOrder(array $row)
- {
- $saleId = intval($row['saleId'] ?? 0);
- if ($saleId <= 0) {
- return $row;
- }
- $order = OrderClass::getById($saleId, true);
- if (empty($order)) {
- return $row;
- }
- $purchaseId = intval($row['id'] ?? 0);
- if ($purchaseId <= 0) {
- return $row;
- }
- $purchase = PurchaseClass::getById($purchaseId, true);
- if (empty($purchase)) {
- return $row;
- }
- $orderRemain = bcadd((string)($order->remainDebtPrice ?? '0'), '0', 2);
- $orderDebt = bcadd((string)($order->debtPrice ?? $order->orderPrice ?? '0'), '0', 2);
- $needSave = false;
- if (bccomp((string)($purchase->remainDebtPrice ?? '0'), $orderRemain, 2) !== 0) {
- $purchase->remainDebtPrice = bccomp($orderRemain, '0', 2) > 0 ? $orderRemain : '0.00';
- $needSave = true;
- }
- if (intval($order->debt ?? 0) === 0 && intval($purchase->debt) === PurchaseClass::DEBT_YES) {
- $purchase->debt = PurchaseClass::DEBT_NO;
- if (!empty($order->clearId)) {
- $purchase->clearId = $order->clearId;
- }
- $needSave = true;
- }
- if (bccomp((string)($purchase->debtPrice ?? '0'), '0', 2) <= 0 && bccomp($orderDebt, '0', 2) > 0) {
- $purchase->debtPrice = $orderDebt;
- $needSave = true;
- }
- if ($needSave) {
- $purchase->save(false);
- }
- $row['remainDebtPrice'] = $purchase->remainDebtPrice;
- $row['debtPrice'] = $purchase->debtPrice;
- $row['debt'] = $purchase->debt;
- return $row;
- }
- //采购单失效 ssh 2021.5.14
- public static function expire($purchase)
- {
- if ($purchase->status != PurchaseClass::STATUS_UN_PAY) {
- util::fail('不能取消');
- }
- //采购单失效
- $id = $purchase->id;
- $purchase = PurchaseClass::cancel($id);
- $saleId = $purchase->saleId;
- //销售单失效
- OrderClass::cancel($saleId);
- $couponId = $purchase->hbId;
- if (!empty($couponId)) {
- $coupon = ShopCouponClass::getLockById($couponId);
- ShopCouponClass::rollback($coupon);
- }
- // OrderClass::cancel($saleId) 中已经清理了
- //清空限购缓存
- // $order = OrderClass::getById($saleId, true);
- // if (!empty($order)) {
- // $customId = $order->customId ?? 0;
- // $orderSn = $order->orderSn;
- // if (!empty($customId)) {
- // $orderItemList = OrderItemClass::getOrderItem($orderSn);
- // if (!empty($orderItemList)) {
- // foreach ($orderItemList as $item) {
- // $productId = $item['productId'] ?? 0;
- // $num = floor($item['num'] ?? 0);
- // if (!empty($productId) && $num > 0) {
- // ProductClass::baseClearLimitBuy($productId, $customId, $num);
- // }
- // }
- // }
- // }
- // }
- return $purchase;
- }
- //订单延期支付 ssh 2021.1.24
- public static function debt($info)
- {
- $payWay = dict::getDict('payWay', 'debtPay');
- $saleId = $info->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- //批发店开的单,客户付款了,不需要打印也不需要微信通知
- if ($info->cgStyle == dict::getDict('cgStyle', 'ghs')) {
- $order->needPrint = dict::getDict('needPrint', 'noNeed');
- $order->save();
- Yii::$app->params['noNeedWxNotice'] = 1;
- }
- // 成对账户预检:两个 payAfter 顺序不变,先确认 xhGhsCustom/xhGhs 净余额一致再动单
- $customId = intval($order->customId ?? 0);
- if ($customId > 0) {
- GhsRechargeSettleService::lockAccountPair(['id' => $customId]);
- }
- self::payAfter($info, $payWay);
- OrderService::payAfter($order, $payWay, true);
- }
- /**
- * 采购单余额支付(超管密码入口等)。
- * 批发-客户资金只在 xhGhs / xhGhsCustom,不扣 xhShop / xhMain.balance。
- */
- public static function balancePay($info)
- {
- $result = self::payByGhsCustomBalance($info);
- return $result['purchase'] ?? $info;
- }
- /**
- * 花店采购余额付:批发商-客户资金只在 xhGhs/xhGhsCustom,先销售单 payAfter 扣净余额,再采购单 payAfter 改状态。
- * xhShop/xhMain 仅为花店门店属性,不参与本关系余额扣减。须在事务内调用;并发由外层重试 + 行锁保证。
- */
- public static function payByGhsCustomBalance($purchase)
- {
- if (empty($purchase)) {
- util::fail('没有找到采购单');
- }
- if (intval($purchase->status ?? 0) == PurchaseClass::STATUS_CANCEL) {
- util::fail('订单已取消');
- }
- if (intval($purchase->status ?? 0) != PurchaseClass::STATUS_UN_PAY) {
- util::fail('订单不是待付款状态');
- }
- if (intval($purchase->payStatus ?? 0) === 1) {
- util::fail('订单已经付过款了');
- }
- $saleId = intval($purchase->saleId ?? 0);
- if ($saleId <= 0) {
- util::fail('订单无法余额支付');
- }
- $purchaseId = intval($purchase->id ?? 0);
- $purchase = PurchaseClass::getLockById($purchaseId);
- $order = OrderClass::getLockById($saleId);
- if (empty($order)) {
- util::fail('没有找到销售单');
- }
- if (intval($order->status ?? 0) != OrderClass::ORDER_STATUS_UN_PAY) {
- util::fail('重复操作');
- }
- $customId = intval($order->customId ?? 0);
- $pair = GhsRechargeSettleService::lockAccountPair(['id' => $customId]);
- $custom = $pair['custom'];
- if (empty($custom)) {
- util::fail('没有找到客户信息');
- }
- AccountMoneyClass::ensureCustomMoneyReady($custom, true);
- // 与待支付页 realPrice 一致,避免 actPrice 与 realPrice 不一致时误报余额不足
- $payAmount = bcadd((string)($purchase->realPrice ?? $order->realPrice ?? $order->actPrice ?? '0'), '0', 2);
- $beforeBalance = bcadd((string)($custom->balance ?? '0'), '0', 2);
- if (bccomp($beforeBalance, $payAmount, 2) < 0) {
- util::fail('余额不足');
- }
- $payWay = dict::getDict('payWay', 'balancePay');
- OrderService::payAfter($order, $payWay, false);
- $purchase = PurchaseClass::getLockById($purchaseId);
- $saleOrder = OrderClass::getById($saleId, true);
- if (!empty($purchase) && !empty($saleOrder) && intval($saleOrder->payStatus ?? 0) === 1) {
- $purchase->remainDebtPrice = $saleOrder->remainDebtPrice ?? '0.00';
- $purchase->debtPrice = $saleOrder->debtPrice ?? '0.00';
- $purchase->debt = ($saleOrder->debt ?? 0) == OrderClass::DEBT_YES
- ? PurchaseClass::DEBT_YES : PurchaseClass::DEBT_NO;
- $purchase->save(false, ['remainDebtPrice', 'debtPrice', 'debt']);
- }
- self::payAfter($purchase, $payWay);
- return [
- 'purchase' => $purchase,
- 'saleOrder' => OrderClass::getById($saleId, true),
- ];
- }
- //付款码支付处理流程 ssh 20211231
- public static function codePay($payWay, $orderSn, $transactionId = '', $openId = '', $aliUserId = '')
- {
- //4秒内同个订单不允许重复请求
- $cacheKey = 'code_pay_f2f_' . $orderSn;
- $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
- if (!empty($has)) {
- Yii::info("付款码支付,重复查询支付结果:orderSn:{$orderSn}");
- noticeUtil::push("付款码支付,重复查询支付结果:orderSn:{$orderSn}", '15280215347');
- util::fail('请稍等...');
- }
- Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
- $info = self::getByCondition(['orderSn' => $orderSn], true);
- if (empty($info)) {
- $msg = "没有找到订单 orderSn:{$orderSn}";
- Yii::info($msg);
- util::fail($msg);
- }
- //解决并发问题
- $id = $info->id;
- $info = PurchaseClass::getLockById($id);
- $info->onlinePay = dict::getDict('onlinePay', 'yes');
- $info->codePay = 1;
- $info->thirdNo = $transactionId;
- $info->payOpenId = $openId;
- $info->aliUserId = $aliUserId;
- //自取订单
- $info->getType = PurchaseClass::GET_TYPE_SELF_GET;
- $info->save();
- self::payAfter($info, $payWay);
- $saleId = $info->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- $order->onlinePay = dict::getDict('onlinePay', 'yes');
- $order->codePay = 1;
- //自取订单
- $order->sendType = OrderClass::SEND_TYPE_NO;
- //不需要小票
- $order->needPrint = dict::getDict('needPrint', 'noNeed');
- $order->save();
- OrderService::payAfter($order, $payWay, true);
- }
- //采购订单支付成功后回调处理流程
- public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId = '')
- {
- $info = self::getByCondition(['orderSn' => $orderSn], true);
- if (empty($info)) {
- $ghsName = '无供货商';
- preg_match('/\d+/', $orderSn, $m);
- if ($m[0]) {
- $cgSnId = $m[0];
- $snData = PurchaseSnClass::getById($cgSnId, true);
- $ghsId = $snData->ghsId ?? 0;
- if (!empty($ghsId)) {
- $ghs = \bizHd\ghs\classes\GhsClass::getById($ghsId, true);
- $ghsName = $ghs->name;
- }
- }
- noticeUtil::push('花店采购,付款成功,支付回调,单号:' . $orderSn . ',没有找到订单 ' . $ghsName . ',' . $attach, '15280215347');
- $msg = "没有找到订单 orderSn:{$orderSn}";
- util::fail($msg);
- }
- if ($info->actPrice != $totalFee) {
- noticeUtil::push("花店采购,付款成功,支付回调,单号:{$orderSn},订单金额与回调通知金额不一致{$info->actPrice} {$totalFee} {$attach}", '15280215347');
- $msg = "订单金额与回调通知金额不一致 orderSn:{$orderSn} {$info->actPrice} {$totalFee}";
- Yii::info($msg);
- util::fail($msg);
- }
- //临时解决微信一秒内通知二次问题
- $id = $info->id;
- $info = PurchaseClass::getLockById($id);
- //由批发店发起的采购单,零售付款时不需要微信通知
- if ($info->cgStyle == dict::getDict('cgStyle', 'ghs')) {
- Yii::$app->params['noNeedWxNotice'] = 1;
- }
- $info->onlinePay = dict::getDict('onlinePay', 'yes');
- $info->thirdNo = $transactionId;
- $info->save();
- self::payAfter($info, $payWay);
- $saleId = $info->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- $order->onlinePay = dict::getDict('onlinePay', 'yes');
- $order->save();
- OrderService::payAfter($order, $payWay, true);
- }
- //第三方支付、余额支付和欠款支付后调用的流程 ssh 2021.4.27
- public static function payAfter($info, $payWay)
- {
- if (empty($info)) {
- util::fail('没有找到订单');
- }
- $deadline = $info->deadline;
- $book = $info->book ?? 0;
- $orderSn = $info->orderSn ?? '';
- $current = date("Y-m-d H:i:s");
- if (isset($info->status) == false) {
- noticeUtil::push('重点注意,采购单:' . $orderSn . ',客户付钱了但没有找到订单的状态', '15280215347');
- util::fail('没有找到订单状态');
- }
- if ($info->status == PurchaseClass::STATUS_CANCEL) {
- noticeUtil::push('注意花店采购单:' . $orderSn . ',客户付款了但订单已取消,关键词 cancel_get_money', '15280215347');
- $saleId = $info->saleId;
- $sale = OrderClass::getById($saleId, true);
- if (!empty($sale)) {
- $ghsShopId = $sale->shopId;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- if (!empty($ghsShop)) {
- CgRefundClass::onlyRefundMoney($ghsShop, $info);
- }
- }
- util::stop("SUCCESS");
- }
- if (isset($info->payStatus) && $info->payStatus == 1) {
- util::fail('订单已经付过款了');
- }
- if ($deadline <= $current) {
- util::fail('订单已过期了哦');
- }
- //零售采购单状态变化
- $info->payWay = $payWay;
- $info->status = PurchaseClass::STATUS_UN_SEND;
- $info->payStatus = 1;
- $payTime = date("Y-m-d H:i:s");
- if (empty($info->payTime) || $info->payTime == '0000-00-00 00:00:00') {
- //如果外面没有设置支付时间,即账单时间,则赋值支付时间
- $info->payTime = $payTime;
- }
- if ($payWay == dict::getDict('payWay', 'debtPay')) {
- //欠款支付(批发开单若销售单已 payAfter,沿用其待结,支持开单余额抵挂账)
- $saleId = intval($info->saleId ?? 0);
- $saleOrder = $saleId > 0 ? \bizGhs\order\classes\OrderClass::getById($saleId, true) : null;
- if (!empty($saleOrder) && intval($saleOrder->payStatus ?? 0) === 1) {
- $info->debt = ($saleOrder->debt ?? 0) == 1 ? 1 : 0;
- $info->remainDebtPrice = $saleOrder->remainDebtPrice ?? 0;
- $info->debtPrice = $saleOrder->debtPrice ?? ($info->orderPrice ?? 0);
- } else {
- if (isset($info->actPrice) && $info->actPrice == 0) {
- $info->debt = 0;
- } else {
- $info->debt = 1;
- }
- $info->remainDebtPrice = $info->orderPrice ?? 0;
- $info->debtPrice = $info->orderPrice ?? 0;
- }
- }
- $info->save();
- $orderSn = $info->orderSn ?? '';
- PurchaseItemClass::updateByCondition(['orderSn' => $orderSn], ['payTime' => $payTime]);
- //零售门店资产变化
- $shopId = $info['shopId'] ?? 0;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店63');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息16');
- }
- $realPrice = $info->realPrice;
- $actPrice = $info->actPrice;
- // 余额支付:批发商-客户资金在 xhGhs/xhGhsCustom(销售单 payAfter 扣款),不扣 xhMain.balance
- $main->totalPurchase = bcadd($main->totalPurchase, $actPrice, 2);
- $currentExpend = bcadd($main->totalExpend, $actPrice, 2);
- $main->totalExpend = $currentExpend;
- $main->cgUnPay -= 1;
- if ($book == 1) {
- $main->cgUnSend += 1;
- } else {
- $main->cgUnSend += 1;
- }
- if ($payWay == dict::getDict('payWay', 'debtPay')) {
- //应付供货商款增加
- $currentMayPay = bcadd($main->mayPay, $realPrice, 2);
- $main->mayPay = $currentMayPay;
- }
- $main->save();
- //支出统计
- StatOutClass::updateOrInsert($main, $shop, $info->orderPrice);
- //支出流水
- $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
- $sjId = $info->sjId ?? 0;
- $shopId = $info->shopId ?? 0;
- $event = '采购';
- $capitalData = [
- 'capitalType' => $capitalType,
- 'io' => 0,
- 'totalExpend' => $currentExpend,
- 'payWay' => $payWay,
- 'amount' => $actPrice,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => $event,
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capitalData);
- //采购总量统计
- $cgNum = $info->bigNum ?? 0;
- StatCgClass::replace($main, $shop, $info->orderPrice, $cgNum);
- //按供货商分别统计采购量
- $num = $info->bigNum ?? 0;
- $amount = $info->orderPrice ?? 0;
- StatCgGhsClass::hdCgGhsReplace(true, $info, $amount, $num);
- //供货商资产变化
- $ghsId = $info->ghsId ?? 0;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- // 挂账净余额:有联动销售单时由 OrderService::payAfter 成对扣,hd 侧不再扣 xhGhs.balance(同余额支付)
- if ($payWay == dict::getDict('payWay', 'debtPay')) {
- $saleId = intval($info->saleId ?? 0);
- if ($saleId <= 0) {
- \bizHd\ghs\classes\GhsClass::cgDebtAmountAdd($ghs, $info);
- }
- }
- $ghs->expendAmount = bcadd($ghs->expendAmount, $actPrice, 2);
- $ghs->expendNum += 1;
- $ghs->save();
- PurchaseClass::updateMergeCg($info, 0);
- }
- }
|