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', 'actPrice', 'realPrice', 'debt', 'ghsId'], 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('请选择同个供货商的订单'); } $totalPrice = bcadd($totalPrice, $purchase->actPrice, 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; $data['purchaseIds'] = implode(',', $arr); $deadTime = time() + 120; $data['deadline'] = date("Y-m-d H:i:s", $deadTime); $data['status'] = self::STATUS_AWAIT_PAY; $order = self::add($data, true); $payWay = $data['payWay'] ?? dict::getDict('payWay', 'wxPay'); self::complete($order, $payWay); return $order; } //支付成功后的流程 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; $purchaseIds = $order->purchaseIds; $arr = explode(',', $purchaseIds); 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("系统繁忙,请重试"); } $currentAmount = bcsub($ghs->debtAmount, $order->prePrice, 2); $ghs->debtAmount = $currentAmount; $ghs->debtNum -= count($arr); $ghs->debt = $currentAmount > 0 ? 2 : 1; $ghs->save(); //减少欠款变动记录 $capitalType = dict::getDict('capitalType', 'ghsCgOrderClear', 'id'); $debtData = [ 'ghsId' => $ghsId, 'relateId' => $order->id, 'ptStyle' => 2, 'capitalType' => $capitalType, 'amount' => $order->prePrice, 'staffId' => $order->customShopAdminId, 'staffName' => $order->customShopAdminName, 'balance' => $ghs->debtAmount, 'io' => 0, 'event' => '采购单结账 ' . $order->orderSn, 'sjId' => $order->sjId, 'shopId' => $order->shopId, ]; GhsDebtRecordClass::add($debtData); 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->payWay = $payWay; $purchase->clearId = $clearId; $purchase->save(); } //客户欠款变化 $customId = $ghs->customId; $custom = CustomClass::getLockById($customId); if (empty($custom)) { util::fail('没有找到客户信息'); } $remain = bcsub($custom->debtAmount, $order->actPrice, 2); $custom->debtAmount = $remain; $custom->debtNum -= count($arr); $custom->isDebt = $remain > 0 ? 1 : 0; $custom->save(); if ($custom->debtNum == 0) { $main->mayGatheringNum -= 1; $main->save(); } //应付供货商款减少 $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) { $purchaseIds = $info['purchaseIds'] ?? []; $cgArr = explode(',', $purchaseIds); $cgList = CgClass::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'] = PurchaseClearClass::STATUS_EXPIRE; self::updateById($id, ['status' => PurchaseClearClass::STATUS_EXPIRE]); } $purchaseIds = $info['purchaseIds'] ?? []; $cgArr = explode(',', $purchaseIds); $cgList = PurchaseClass::getAllByCondition(['id' => ['in', $cgArr]], null, '*', null); $info['orderList'] = $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) { $saleIds = $info['saleIds'] ?? []; $saleArr = explode(',', $saleIds); $saleList = OrderClass::getAllByCondition(['id' => ['in', $saleArr]], null, '*', null); $info['orderList'] = $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; } }