type = ShopBill::TYPE_INCR; $bill->seller_id = 0; $bill->shop_id = $shopId; $bill->money = $money; $bill->begin_balance = $beforeShop->balance; $bill->begin_balance_withdraw = $beforeShop->balance_withdraw; $bill->begin_balance_cash = $beforeShop->balance_cash; $bill->end_balance = $endShop->balance; $bill->end_balance_withdraw = $endShop->balance_withdraw; $bill->end_balance_cash = $endShop->balance_cash; $bill->begin_balance_freeze = $beforeShop->balance_freeze; $bill->end_balance_freeze = $endShop->balance_freeze; $bill->ie = '+'; $bill->title = '系统加款'; $bill->remark = '管理员['.$adminId.']给店铺加款'; $bill->created_date = date('Ymd'); $bill->save(); DB::commit(); } catch (\Exception $exception) { DB::rollBack(); throw $exception; } } public static function recharge(ShopRecharge $recharge) { ThrowException::isTrue(!$recharge->isSuccess(), '充值单未成功'); // 增加余额 $beforeShop = Shop::find($recharge->shop_id); Shop::balanceRecharge($recharge->shop_id, $recharge->pay_money); // 账单 $endShop = Shop::find($recharge->shop_id); $bill = new ShopBill(); $bill->type = ShopBill::TYPE_RECHARGE; $bill->seller_id = $recharge->seller_id; $bill->shop_id = $beforeShop->id; $bill->money = $recharge->pay_money; $bill->begin_balance = $beforeShop->balance; $bill->begin_balance_withdraw = $beforeShop->balance_withdraw; $bill->begin_balance_cash = $beforeShop->balance_cash; $bill->end_balance = $endShop->balance; $bill->end_balance_withdraw = $endShop->balance_withdraw; $bill->end_balance_cash = $endShop->balance_cash; $bill->begin_balance_freeze = $beforeShop->balance_freeze; $bill->end_balance_freeze = $endShop->balance_freeze; $bill->ie = '+'; $bill->title = '充值'; $bill->created_date = date('Ymd'); $bill->save(); } /** * 出票相关金额和账单 * @param Order $order * @param Seller $seller * @throws \Exception */ public static function ticket(Order $order, Seller $seller) { DB::beginTransaction(); try { /** @var Customer $customer */ $customer = Customer::find($order->customer_id); $agentor = $customer->agentor; // 代理用户的佣金 $agentBrokerage = 0; // 如果用户有上级代理,并且设置了佣金 if ($agentor && $agentor->agent && $agentor->agent_brokerage > 0) { $agentBrokerage = $order->money * ($agentor->agent_brokerage / 100); } // 出票服务费 $feeMoney = $order->money * Config::platformFeeLv(); $allReduceMoney = 0 + $feeMoney; /** @var Shop $shop */ $shop = Shop::find($seller->shop_id); if ($shop->balanceActive() < $allReduceMoney) { ThrowException::run('余额不足,无法出票'); } // 如果是合作出票,需要扣除合作余额 $isUnion = false; if ($order->shop_id != $seller->shop_id) { $isUnion = true; self::cooperateTicket($seller, $order); } // 扣除出票服务费 self::ticketFee($order, $seller, $feeMoney, $isUnion); // 代理佣金 if ($agentor && $agentor->agent) { // 代理用户佣金 if ($agentBrokerage) { // 扣除代理用户佣金 // $shopBill = self::customerAgentBrokerage($order, $seller, $agentBrokerage); // 代理用户增加佣金 // CustomerWalletService::agentBrokerage($order, $agentor->id, $seller->id, $shopBill->id, $agentBrokerage); AgentSale::log($agentor, $order, $agentBrokerage); } } // 如果是合买 if ($order->type == OrderType::UNION) { // 有需要退款的 if ($order->union_piece_keep > 0) { // 保底份数 - 未出售份数 $remainPiece = $order->union_piece_total - $order->union_piece_buy; $backPiece = $order->union_piece_keep - $remainPiece; // 如果超额保底,则退款多余保底金额 if ($backPiece > 0) { $backPayMoney = $backPiece * $order->union_piece_money; $order->union_piece_keep = $remainPiece; $order->union_money -= $backPayMoney; $order->save(); CustomerWalletService::backUnionKeepMoney($order, $backPayMoney); } } // 代理佣金 $unionOrders = Order::where('pid', $order->id)->get(); foreach ($unionOrders as $unionOrder) { /** @var Customer $customer */ $unionCustomer = Customer::find($unionOrder->customer_id); $unionAgentor = $unionCustomer->agentor; // 代理用户的佣金 $unionAgentBrokerage = 0; // 如果用户有上级代理,并且设置了佣金 if ($unionAgentor && $unionAgentor->agent && $unionAgentor->agent_brokerage > 0) { $unionAgentBrokerage = $unionOrder->union_money * ($unionAgentor->agent_brokerage / 100); if ($unionAgentBrokerage) { // 扣除代理用户佣金 // $shopBill = self::customerAgentBrokerage($order, $seller, $unionAgentBrokerage); // 代理用户增加佣金 // CustomerWalletService::agentBrokerage($order, $unionAgentor->id, $seller->id, $shopBill->id, $unionAgentBrokerage); AgentSale::log($unionAgentor, $unionOrder, $unionAgentBrokerage); } } } } DB::commit(); } catch (JingCaiException $exception) { DB::rollBack(); throw $exception; } catch (\Exception $exception) { DB::rollBack(); throw $exception; } } /** * 出票服务费 * @param Order $order * @param Seller $seller * @param $feeMoney * @return ShopBill * @throws \Exception */ public static function ticketFee(Order $order, Seller $seller, $feeMoney, $isUnion = false) { $beforeShop = Shop::find($seller->shop_id); Shop::balanceReduce($seller->shop_id, $feeMoney); $endShop = Shop::find($seller->shop_id); $bill = new ShopBill(); $bill->type = ShopBill::TYPE_FEE; $bill->seller_id = $seller->id; $bill->cooped = $isUnion; $bill->order_id = $order->id; $bill->shop_id = $beforeShop->id; $bill->money = $feeMoney; $bill->begin_balance = $beforeShop->balance; $bill->begin_balance_withdraw = $beforeShop->balance_withdraw; $bill->begin_balance_cash = $beforeShop->balance_cash; $bill->end_balance = $endShop->balance; $bill->end_balance_withdraw = $endShop->balance_withdraw; $bill->end_balance_cash = $endShop->balance_cash; $bill->begin_balance_freeze = $beforeShop->balance_freeze; $bill->end_balance_freeze = $endShop->balance_freeze; $bill->ie = '-'; $bill->title = sprintf('出票基础服务费(订单%d元)', $order->money); $bill->created_date = date('Ymd'); $bill->save(); return $bill; } private static function customerAgentBrokerage(Order $order, Seller $seller, $agentBrokerage) { $beforeShop = Shop::find($seller->shop_id);; // 店主余额减少 Shop::balanceReduce($seller->shop_id, $agentBrokerage); $endShop = Shop::find($beforeShop->id); $money = $order->money; if ($order->type == OrderType::UNION) { $money = $order->union_money; } $bill = new ShopBill(); $bill->type = ShopBill::TYPE_AGENT; $bill->seller_id = $seller->id; $bill->order_id = $order->id; $bill->shop_id = $beforeShop->id; $bill->money = $agentBrokerage; $bill->begin_balance = $beforeShop->balance; $bill->begin_balance_withdraw = $beforeShop->balance_withdraw; $bill->begin_balance_cash = $beforeShop->balance_cash; $bill->end_balance = $endShop->balance; $bill->end_balance_withdraw = $endShop->balance_withdraw; $bill->end_balance_cash = $endShop->balance_cash; $bill->begin_balance_freeze = $beforeShop->balance_freeze; $bill->end_balance_freeze = $endShop->balance_freeze; $bill->ie = '-'; $bill->title = sprintf('用户销售佣金(订单%d元)', $money); $bill->created_date = date('Ymd'); $bill->save(); return $bill; } public static function gendanShopReduceAllBrokerage(Seller $seller, Order $order, $sendPrize) { $fadanBrokerage = $order->lottery_gendan_brokerage_all; if ($fadanBrokerage <= 0) { return; } $beforeShop = Shop::find($order->shop_id); // 店主余额减少 Shop::balanceReduce($order->shop_id, $fadanBrokerage); $endShop = Shop::find($order->shop_id); $bill = new ShopBill(); $bill->type = ShopBill::TYPE_FADAN_ALL; $bill->seller_id = $seller->id; $bill->order_id = $order->id; $bill->shop_id = $beforeShop->id; $bill->money = $fadanBrokerage; $bill->begin_balance = $beforeShop->balance; $bill->begin_balance_withdraw = $beforeShop->balance_withdraw; $bill->begin_balance_cash = $beforeShop->balance_cash; $bill->end_balance = $endShop->balance; $bill->end_balance_withdraw = $endShop->balance_withdraw; $bill->end_balance_cash = $endShop->balance_cash; $bill->begin_balance_freeze = $beforeShop->balance_freeze; $bill->end_balance_freeze = $endShop->balance_freeze; $bill->ie = '-'; $bill->title = sprintf('平台扣除跟单佣金(订单%d元)', $order->money); $bill->remark = '跟单人店铺被扣除10%'; $bill->created_date = date('Ymd'); $bill->save(); } public static function gendanShopIncrBrokerage(Seller $seller, Order $order, $sendPrize) { $fadanBrokerage = $order->lottery_gendan_brokerage_all * Config::fadanShopFeeLv(); if ($fadanBrokerage <= 0) { return; } $beforeShop = Shop::find($order->shop_id); // 店主余额增加 Shop::balanceIncr($order->shop_id, $fadanBrokerage); $endShop = Shop::find($order->shop_id); $bill = new ShopBill(); $bill->type = ShopBill::TYPE_FADAN_ALL; $bill->seller_id = $seller->id; $bill->order_id = $order->id; $bill->shop_id = $beforeShop->id; $bill->money = $fadanBrokerage; $bill->begin_balance = $beforeShop->balance; $bill->begin_balance_withdraw = $beforeShop->balance_withdraw; $bill->begin_balance_cash = $beforeShop->balance_cash; $bill->end_balance = $endShop->balance; $bill->end_balance_withdraw = $endShop->balance_withdraw; $bill->end_balance_cash = $endShop->balance_cash; $bill->begin_balance_freeze = $beforeShop->balance_freeze; $bill->end_balance_freeze = $endShop->balance_freeze; $bill->ie = '+'; $bill->title = sprintf('平台派发跟单佣金(订单%d元)', $order->money); $bill->remark = '跟单人店铺获得2%'; $bill->created_date = date('Ymd'); $bill->save(); } /** * 发单人店铺 获取部分发单佣金 * @param Order $order * @param $shopId * @param $sendPrize */ public static function fadanShopIncrBrokerage(Order $order, $shopId, $sendPrize) { $allFadanBrokerage = $order->lottery_gendan_brokerage_all; $fadanBrokerage = $allFadanBrokerage * Config::fadanUserFeeLv(); if ($fadanBrokerage <= 0) { return; } $beforeShop = Shop::find($shopId); // 店主余额增加 Shop::balanceIncr($shopId, $fadanBrokerage); $endShop = Shop::find($shopId); $bill = new ShopBill(); $bill->type = ShopBill::TYPE_FADAN_SHOP; $bill->seller_id = 0; $bill->order_id = $order->id; $bill->shop_id = $shopId; $bill->money = $fadanBrokerage; $bill->begin_balance = $beforeShop->balance; $bill->begin_balance_withdraw = $beforeShop->balance_withdraw; $bill->begin_balance_cash = $beforeShop->balance_cash; $bill->end_balance = $endShop->balance; $bill->end_balance_withdraw = $endShop->balance_withdraw; $bill->end_balance_cash = $endShop->balance_cash; $bill->begin_balance_freeze = $beforeShop->balance_freeze; $bill->end_balance_freeze = $endShop->balance_freeze; $bill->ie = '+'; $bill->title = sprintf('平台派发发单佣金(订单%d元)', $order->money); $bill->remark = '发单人店铺余额增加6%'; $bill->created_date = date('Ymd'); $bill->save(); } public function withdrawMoney(Seller $seller, $data) { ThrowException::isTrue(!$seller->real_identity || !$seller->real_name, '请先完成实名认证'); $type = Arr::get($data, 'type', 0); $money = Arr::get($data, 'money', 0); $ali_account = Arr::get($data, 'ali_account', ''); $bank_no = Arr::get($data, 'bank_no', ''); $bank_area = Arr::get($data, 'bank_area', ''); $bank_master = Arr::get($data, 'bank_master', ''); $bank_branch = Arr::get($data, 'bank_branch', ''); $password_pay = Arr::get($data, 'password_pay', ''); ThrowException::isTrue($type != WithdrawType::BANK && $type != WithdrawType::ALI, '不支持该方式'); ThrowException::isTrue($money > Config::shopWithdrawMax(), '超过限额'); $seller = Seller::find($seller->id); ThrowException::isTrue($seller->level != SellerLevel::MASTER, '无权操作'); ThrowException::isTrue(!$seller->password_pay, '请设置支付密码'); ThrowException::isTrue(!Seller::checkPassword($password_pay, $seller->password_pay), '支付密码错误'); if ($type == WithdrawType::BANK) { $errors = BankValidator::hasErrors($data); ThrowException::isTrue($errors, $errors); } DB::beginTransaction(); try { $shop = Shop::lockForUpdate()->find($seller->shop_id); ThrowException::isTrue($shop->balance_withdraw < $money, '可提现金额不足'); $shop->balance_withdraw -= $money; $shop->balance_freeze += $money; $shop->save(); $freeze = new ShopFreeze(); $freeze->shop_id = $shop->id; $freeze->type = FreezeType::WITHDRAW; $freeze->money = $money; $freeze->state = BoolEnum::YES; $freeze->description = ''; $freeze->save(); $withdraw = new ShopWithdraw(); $withdraw->shop_id = $seller->shop_id; $withdraw->seller_id = $seller->id; $withdraw->freeze_id = $freeze->id; $withdraw->type = $type; $withdraw->state = WithdrawState::PENDING; $withdraw->money = $money; $withdraw->ali_account = $ali_account; $withdraw->bank_no = $bank_no; $withdraw->bank_area = $bank_area; $withdraw->bank_master = $bank_master; $withdraw->bank_branch = $bank_branch; $withdraw->save(); DB::commit(); } catch (JingCaiException $exception) { DB::rollBack(); throw $exception; } catch (\Exception $exception) { DB::rollBack(); throw $exception; } } // TODO 此处Seller需要更换为Admin表 public function withdrawAudit(Seller $admin, $data) { $id = Arr::get($data, 'id'); $state = Arr::get($data, 'state'); $remark = Arr::get($data, 'remark'); ThrowException::isTrue($id < 1, '参数错误'); ThrowException::isTrue(!in_array($state, [WithdrawState::SUCCESS, WithdrawState::FAIL]), '状态错误'); DB::beginTransaction(); try { $withdraw = ShopWithdraw::lockForUpdate()->find($id); ThrowException::isTrue(!$withdraw, '数据不存在'); ThrowException::isTrue($withdraw->state != WithdrawState::PENDING, '数据状态异常,不可操作'); $freeze = ShopFreeze::lockForUpdate()->find($withdraw->freeze_id); ThrowException::isTrue($freeze->state == BoolEnum::NO, '数据已被处理'); $shop = Shop::lockForUpdate()->find($withdraw->shop_id); ThrowException::isTrue($shop->balance_freeze < $withdraw->money, '冻结金额不足'); $freeze->state = BoolEnum::NO; $freeze->save(); if ($state == WithdrawState::SUCCESS) { $shop->balance_freeze -= $withdraw->money; $shop->save(); $endShop = Shop::find($shop->id); $bill = new ShopBill(); $bill->type = BillType::WITHDRAW; $bill->title = '提现'; $bill->shop_id = $shop->id; $bill->recharge_id = 0; $bill->order_id = 0; $bill->ie = '-'; $bill->begin_balance = $shop->balance; $bill->begin_balance_withdraw = $shop->balance_withdraw; $bill->begin_balance_cash = $shop->balance_cash; $bill->end_balance = $endShop->balance; $bill->end_balance_withdraw = $endShop->balance_withdraw; $bill->end_balance_cash = $endShop->balance_cash; $bill->begin_balance_freeze = $shop->balance_freeze; $bill->end_balance_freeze = $endShop->balance_freeze; $bill->money = $withdraw->money; $bill->admin_id = $admin->id; $bill->created_date = date('Ymd'); $bill->save(); } else if ($state == WithdrawState::FAIL){ $shop->balance_freeze -= $withdraw->money; $shop->balance_withdraw += $withdraw->money; $shop->save(); } $withdraw->state = $state; $withdraw->admin_id = $admin->id; $withdraw->admin_remark = strval($remark); $withdraw->remark_at = date('Y-m-d H:i:s'); $withdraw->save(); DB::commit(); } catch (JingCaiException $exception) { DB::rollBack(); throw $exception; } catch (\Exception $exception) { DB::rollBack(); throw $exception; } } /** * 合作出票 * @param Seller $seller * @param $data * @return void * @throws \Exception */ public static function cooperateTicket(Seller $seller, Order $order) { /** @var Lottery $lottery */ $lottery = $order->lottery; /** @var ShopCooperateLottery $scl */ $scl = $lottery->shopCooperateLottery; ThrowException::isTrue(!$scl || $scl->cooperate_id != $seller->shop_id, '非合作店铺,无法出票'); DB::beginTransaction(); try { $sc = ShopCooperate::lockForUpdate()->find($scl->shop_cooperate_id); ThrowException::isTrue(!$sc || $sc->cooperate_id != $seller->shop_id, '非合作店铺,无法出票!'); $payMoney = $order->getPayMoney(); $brokerageMoney = $scl->getBrokerageMoney($payMoney); $money = $payMoney - $brokerageMoney; ThrowException::isTrue(bc_lt($sc->balance, $order->getPayMoney()), '合作店铺余额不足,无法出票!'); $beginBalance = $sc->balance; $sc->balance -= $money; $sc->save(); $scb = new ShopCooperateBill(); $scb->order_id = $order->id; $scb->lottery_type_id = $order->lottery_type_id; $scb->type = ShopCooperateBill::TYPE_TICKET; $scb->title = $scb->getTypeTitle(); $scb->seller_id = $seller->id; $scb->seller_shop_id = $seller->shop_id; $scb->shop_cooperate_id = $sc->id; $scb->shop_id = $order->shop_id; $scb->money = $money; $scb->begin_balance = $beginBalance; $scb->end_balance = $sc->balance; $scb->cooperate_brokerage = $brokerageMoney; $scb->ie = '-'; $scb->remark = strval(''); $scb->created_date = date('Ymd'); $scb->save(); DB::commit(); } catch (JingCaiException $exception) { DB::rollBack(); throw $exception; } catch (\Exception $exception) { DB::rollBack(); throw $exception; } } public function cooperateIncr(Seller $seller, $data) { $money = Arr::get($data, 'money'); $remark = Arr::get($data, 'remark'); $shopId = Arr::get($data, 'shop_id'); ThrowException::isTrue($money <= 0, '金额不能小于0'); DB::beginTransaction(); try { $sc = ShopCooperate::where('cooperate_id', $seller->shop_id) ->where('shop_id', $shopId) ->lockForUpdate() ->first(); ThrowException::isTrue(!$sc, '非合作关系'); ThrowException::isTrue($sc->audit_state != BoolEnum::YES, '审核未成功'); $beginBalance = $sc->balance; $sc->balance += $money; $sc->save(); $scb = new ShopCooperateBill(); $scb->type = ShopCooperateBill::TYPE_INCR; $scb->shop_cooperate_id = $sc->id; $scb->title = $scb->getTypeTitle(); $scb->seller_id = $seller->id; $scb->seller_shop_id = $seller->shop_id; $scb->shop_id = $shopId; $scb->money = $money; $scb->begin_balance = $beginBalance; $scb->end_balance = $sc->balance; $scb->ie = '+'; $scb->remark = strval($remark); $scb->created_date = date('Ymd'); $scb->save(); DB::commit(); } catch (JingCaiException $exception) { DB::rollBack(); throw $exception; } catch (\Exception $exception) { DB::rollBack(); throw $exception; } } public static function cooperateOrderWined(Seller $seller, $order) { if ($order->cooperate_id == 0 && $order->cooperate_show != BoolEnum::NO) { return; } if ($seller->shop_id == $order->shop_id || $seller->shop_id != $order->cooperate_id) { return; } $money = $order->lottery_send_prize; $shopId = $order->shop_id; DB::beginTransaction(); try { $sc = ShopCooperate::where('cooperate_id', $seller->shop_id) ->where('shop_id', $shopId) ->lockForUpdate() ->first(); ThrowException::isTrue(!$sc, '非合作关系'); $beginBalance = $sc->balance; $sc->balance += $money; $sc->save(); $scb = new ShopCooperateBill(); $scb->type = ShopCooperateBill::TYPE_WINED; $scb->order_id = $order->id; $scb->lottery_type_id = $order->lottery_type_id; $scb->shop_cooperate_id = $sc->id; $scb->title = $scb->getTypeTitle(); $scb->seller_id = $seller->id; $scb->seller_shop_id = $seller->shop_id; $scb->shop_id = $shopId; $scb->money = $money; $scb->begin_balance = $beginBalance; $scb->end_balance = $sc->balance; $scb->ie = '+'; $scb->remark = strval(''); $scb->created_date = date('Ymd'); $scb->save(); DB::commit(); } catch (JingCaiException $exception) { DB::rollBack(); throw $exception; } catch (\Exception $exception) { DB::rollBack(); throw $exception; } } public function cooperateReduce(Seller $seller, $data) { $money = Arr::get($data, 'money'); $remark = Arr::get($data, 'remark'); $shopId = Arr::get($data, 'shop_id'); ThrowException::isTrue($money <= 0, '金额不能小于0'); DB::beginTransaction(); try { $sc = ShopCooperate::where('cooperate_id', $seller->shop_id) ->where('shop_id', $shopId) ->lockForUpdate() ->first(); ThrowException::isTrue(!$sc, '非合作关系'); ThrowException::isTrue($sc->audit_state != BoolEnum::YES, '审核未成功'); ThrowException::isTrue(bc_gt($money, $sc->balance), '余额不足,无法扣款'); $beginBalance = $sc->balance; $sc->balance -= $money; $sc->save(); $scb = new ShopCooperateBill(); $scb->type = ShopCooperateBill::TYPE_REDUCE; $scb->title = $scb->getTypeTitle(); $scb->seller_id = $seller->id; $scb->seller_shop_id = $seller->shop_id; $scb->shop_cooperate_id = $sc->id; $scb->shop_id = $shopId; $scb->money = $money; $scb->begin_balance = $beginBalance; $scb->end_balance = $sc->balance; $scb->ie = '-'; $scb->remark = strval($remark); $scb->created_date = date('Ymd'); $scb->save(); DB::commit(); } catch (JingCaiException $exception) { DB::rollBack(); throw $exception; } catch (\Exception $exception) { DB::rollBack(); throw $exception; } } }