| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- namespace bizGhs\order\classes;
- use biz\ghs\classes\GhsClass;
- use biz\shop\classes\ShopClass;
- use bizGhs\custom\classes\CustomClass;
- use common\components\dict;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\util;
- use bizGhs\base\classes\BaseClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\purchase\classes\PurchaseClearClass;
- class OrderClearClass extends BaseClass
- {
- public static $baseFile = '\bizGhs\order\models\OrderClear';
- //订单结算 ssh 2021.3.14
- public static function clear($post, $idText, $sjId, $shopId)
- {
- $customId = $post['customId'] ?? 0;
- $data = json_decode($idText, true);
- $ids = array_column($data, 'id');
- if (empty($ids)) {
- util::fail('请选择交易记录');
- }
- $list = OrderClass::getByIds($ids);
- if (empty($list)) {
- util::fail('请选择订单');
- }
- $amount = 0;
- $modifyPrice = $post['modifyPrice'] ?? 0.00;
- $purchaseArr = [];
- foreach ($list as $key => $val) {
- if ($val['sjId'] != $sjId) {
- util::fail('只能结算自己的订单哦');
- }
- if ($val['debt'] == 0) {
- util::fail('有订单已经结算过了');
- }
- if ($val['customId'] != $customId) {
- util::fail('每次只能结算一个客户的订单');
- }
- $amount = bcadd($amount, $val['remainDebtPrice'], 2);
- $purchaseId = $val['purchaseId'] ?? 0;
- if (!empty($purchaseId)) {
- $purchaseArr[] = $purchaseId;
- }
- }
- $purchaseString = '';
- if (!empty($purchaseArr)) {
- $purchaseString = implode(',', $purchaseArr);
- }
- $custom = CustomClass::getLockById($customId);
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- $ghsId = $custom->ghsId;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- $ghsShopId = $ghs['shopId'] ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- $ghsShopName = $ghsShop->shopName ?? '';
- $ghsShopName = $ghsShopName == '首店' ? '总店' : $ghsShopName;
- $orderSn = orderSn::getPurchaseClearSn();
- $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : dict::getDict('payWay', 'unPay');
- $clearData = [
- 'prePrice' => $amount,
- 'actPrice' => $modifyPrice,
- 'realPrice' => $modifyPrice,
- 'ghsId' => $ghsId,
- 'ghsName' => $ghs['name'] ?? '',
- 'ghsMobile' => $ghs['mobile'] ?? '',
- 'ghsAvatar' => $ghs['avatar'] ?? '',
- 'ghsAddress' => $ghs['address'] ?? '',
- 'ghsShopAdminId' => $post['ghsShopAdminId'],
- 'ghsShopId' => $post['ghsShopId'] ?? 0,
- 'ghsShopAdminName' => $post['ghsShopAdminName'],
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'orderSn' => $orderSn,
- 'saleIds' => implode(',', $ids),
- 'status' => 1,
- 'clearStyle' => dict::getDict('clearStyle', 'gys2Hd'),
- 'customId' => $customId,
- 'customName' => $custom['name'] ?? '',
- 'customShopId' => $custom['shopId'] ?? 0,
- 'customAvatar' => $custom['avatar'] ?? '',
- 'customMobile' => $custom['mobile'] ?? '',
- 'customAddress' => $custom['address'] ?? '',
- 'deadline' => date("Y-m-d H:i:s", time() + 10 * 60 * 60),
- 'purchaseIds' => $purchaseString,
- 'payWay' => $payWay,
- 'num' => count($ids),
- 'ghsShopName' => $ghsShopName,
- ];
- if ($modifyPrice < $amount) {
- $clearData['discountAmount'] = bcsub($amount, $modifyPrice, 2);
- $clearData['discountType'] = dict::getDict('discountType', 'discount');
- }
- if ($modifyPrice > $amount) {
- util::fail('结账金额大于总欠款金额');
- }
- $return = PurchaseClearClass::add($clearData, true);
- $complete = $post['complete'] ?? 0;
- if ($complete == 1) {
- self::confirmClear($return, $payWay);
- }
- return $return;
- }
- //确认结账 ssh 20220510
- public static function confirmClear($clear, $payWay)
- {
- if ($clear->status == 2) {
- util::fail('已结过账了');
- }
- if ($clear->status == 3) {
- util::fail('已取消了');
- }
- $current = time();
- if ($current >= strtotime($clear->deadline)) {
- util::fail('已过期,请手动取消');
- }
- $clear->payWay = $payWay;
- $clear->payTime = date("Y-m-d H:i:s");
- $clear->status = 2;
- $clear->save();
- $clearId = $clear->id ?? 0;
- $customId = $clear->customId ?? 0;
- $custom = CustomClass::getLockById($customId);
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- $ghsId = $custom->ghsId;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- $amount = $clear->prePrice ?? 0;
- $saleIds = $clear->saleIds ?? '';
- $ids = explode(',', trim($saleIds));
- if (empty($ids)) {
- util::fail('数据错误');
- }
- $purchaseIds = $clear->purchaseIds ?? '';
- $purchaseArr = explode(',', $purchaseIds);
- if (empty($purchaseArr)) {
- util::fail('数据错误');
- }
- //客户欠款减少
- CustomClass::clearDebtAmountReduce($custom, $ghs, $amount, $clear);
- // 销售单状态变更
- OrderClass::updateByIds($ids, ['debt' => 0, 'clearId' => $clearId, 'remainDebtPrice' => 0]);
- // 采购单状态变更
- PurchaseClass::updateByIds($purchaseArr, ['debt' => 2, 'clearId' => $clearId, 'remainDebtPrice' => 0]);
- //供货商结账欠款减少
- \bizHd\ghs\classes\GhsClass::clearDebtAmountReduce($ghs, $custom, $amount, $clear);
- }
- }
|