| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace bizHd\purchase\classes;
- use biz\ghs\classes\GhsClass;
- use biz\shop\classes\ShopClass;
- use biz\shop\classes\ShopYeChangeClass;
- use biz\wx\classes\WxMessageClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\order\classes\OrderClass;
- use bizHd\base\classes\BaseClass;
- use bizHd\shop\classes\MainClass;
- use common\components\dict;
- use common\components\orderSn;
- use common\components\util;
- use Yii;
- class PurchaseClearClass extends BaseClass
- {
- public static $baseFile = '\bizHd\purchase\models\PurchaseClear';
- const STATUS_AWAIT_PAY = 1;
- const STATUS_HAS_PAY = 2;
- const STATUS_EXPIRE = 3;
- public static function addOrder($data, $ghs)
- {
- $ghsId = $ghs->id;
- $customId = $ghs->customId;
- $custom = CustomClass::getById($customId, true);
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- $ghsShopId = $ghs->shopId ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- $ghsShopName = $ghsShop->shopName ?? '';
- $ghsShopName = $ghsShopName == '首店' ? '总店' : $ghsShopName;
- $orderSn = orderSn::getPurchaseClearSn();
- $purchaseIds = $data['purchaseIds'];
- $arr = explode(',', $purchaseIds);
- if (empty($arr)) {
- util::fail('请选择采购单');
- }
- //订单太多数据库会报错
- if (count($arr) > 100) {
- util::fail('订单太多,请选月份再结');
- }
- $purchaseList = PurchaseClass::getAllByCondition(['id' => ['in', $arr]], null, '*', null, true);
- if (empty($purchaseList)) {
- util::fail('请选择采购单');
- }
- $totalPrice = 0;
- $currentGhsId = $data['ghsId'] ?? 0;
- $saleArr = [];
- foreach ($purchaseList as $purchase) {
- if ($purchase->debt != PurchaseClass::DEBT_YES) {
- util::fail('请选择有欠款的采购单');
- }
- if ($purchase->ghsId != $currentGhsId) {
- util::fail('请选择同个供货商的订单');
- }
- $totalPrice = bcadd($totalPrice, $purchase->remainDebtPrice, 2);
- $saleId = $purchase->saleId;
- $saleArr[] = $saleId;
- }
- $saleString = implode(',', $saleArr);
- $data['saleIds'] = $saleString;
- $data['prePrice'] = $totalPrice;
- $data['actPrice'] = $totalPrice;
- $data['realPrice'] = $totalPrice;
- $data['orderSn'] = $orderSn;
- $validTime = dict::getDict('order_pay_has_time');
- $deadTime = time() + $validTime;
- $data['deadline'] = date("Y-m-d H:i:s", $deadTime);
- $params = [
- 'ghsId' => $ghsId,
- 'ghsName' => $ghs['name'] ?? '',
- 'ghsMobile' => $ghs['mobile'] ?? '',
- 'ghsAvatar' => $ghs['avatar'] ?? '',
- 'ghsAddress' => $ghs['address'] ?? '',
- 'customId' => $customId,
- 'customName' => $custom['name'] ?? '',
- 'customAvatar' => $custom['avatar'] ?? '',
- 'customMobile' => $custom['mobile'] ?? '',
- 'customAddress' => $custom['address'] ?? '',
- 'num' => count($arr),
- 'ghsShopName' => $ghsShopName,
- ];
- $data = array_merge($data, $params);
- return self::add($data);
- }
- //第三方支付回调 ssh 2021.4.28
- public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId)
- {
- $info = self::getByCondition(['orderSn' => $orderSn], true);
- if (empty($info)) {
- $msg = "没有找到清算订单 orderSn:{$orderSn}";
- Yii::info($msg);
- util::fail($msg);
- }
- if ($info->actPrice != $totalFee) {
- $msg = "清算订单金额与回调通知金额不一致 orderSn:{$orderSn} {$info->actPrice} {$totalFee}";
- Yii::info($msg);
- util::fail($msg);
- }
- //临时解决微信一秒内通知二次问题
- $id = $info->id;
- $info = self::getLockById($id);
- $info->onlinePay = dict::getDict('onlinePay', 'yes');
- $info->thirdNo = $transactionId;
- $info->save();
- self::complete($info, $payWay);
- }
- //支付成功后的流程 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->payTime = date("Y-m-d H:i:s");
- $order->save();
- $clearId = $order->id;
- $orderSn = $order->orderSn;
- $purchaseIds = $order->purchaseIds;
- $arr = explode(',', $purchaseIds);
- //供货商欠款变更
- $ghsId = $order->ghsId;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商信息');
- }
- $customId = $ghs->customId;
- $custom = CustomClass::getLockById($customId);
- if (empty($custom)) {
- util::fail('没有找到客户信息');
- }
- \bizHd\ghs\classes\GhsClass::clearDebtAmountReduce($ghs, $custom, $order->actPrice, $order);
- $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信息');
- }
- if ($payWay == dict::getDict('payWay', 'wxPay')) {
- //供货商门店余额增加
- ShopClass::customClearAddBalance($main, $shop, $order);
- }
- //供货商的门店应收客户款减少
- $currentGathering = bcsub($main->mayGathering, $order->actPrice, 2);
- $main->mayGathering = $currentGathering;
- $main->save();
- if (empty($arr)) {
- $msg = "结算订单 orderSn:{$orderSn} 支付成功,回调处理,没有找到采购订单";
- util::fail($msg);
- }
- $list = PurchaseClass::getAllByCondition(['id' => ['in', $arr]], null, '*', null, true);
- if (empty($list)) {
- $msg = "结算订单 orderSn:{$orderSn} 支付成功,回调处理,没有找到采购订单!";
- util::fail($msg);
- }
- //采购单状态变更
- $saleIds = [];
- foreach ($list as $purchase) {
- $purchase->debt = PurchaseClass::DEBT_NO;
- $purchase->payWay = $payWay;
- $purchase->clearId = $clearId;
- $purchase->remainDebtPrice = 0;
- $purchase->save();
- $saleIds[] = $purchase->saleId;
- }
- //销售单状态变化
- OrderClass::updateByIds($saleIds, ['debt' => OrderClass::DEBT_NO, 'clearId' => $clearId, 'remainDebtPrice' => 0]);
- //客户付供货商款减少
- CustomClass::clearDebtAmountReduce($custom, $ghs, $order->actPrice, $order);
- WxMessageClass::clearInform($shop, $order, $custom);
- }
- }
|