123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 |
- <?php
- namespace App\Modules\Finance\Services;
- use App\Modules\Channel\Services\ChannelService;
- use App\Modules\Finance\Models\FinancialPayMerchantBalance;
- use App\Modules\Finance\Models\FinancialStat;
- use App\Modules\Finance\Models\WithdrawCash;
- use App\Modules\Manage\Services\ManageService;
- use App\Modules\User\Services\UserService;
- use DB;
- class WithdrawCashService
- {
-
- public static function auto_add_withdrawCash($distribution_channel_id, $amount, $remark)
- {
- $distribution_channel_name = '';
- if (!is_numeric($amount)) {
- return response()->error("PARAM_ERROR");
- }
- if ($amount < 100) {
-
- if ($distribution_channel_id == 14 || $distribution_channel_id = "14" || $distribution_channel_id = '14') {
- if ($amount < 1) {
- return response()->error("WITHDRAW_CASH_AMOUNT");
- }
- } else {
- return response()->error("WITHDRAW_CASH_AMOUNT");
- }
- }
- if ($amount >= (20 * 10000)) {
-
-
- }
- $financialStat = FinancialStatService::getFinancialStatSingle($distribution_channel_id);
- if (empty($financialStat) || $financialStat['enable_withdrawal_amount'] < $amount) {
-
- return response()->error("WITHDRAW_CASH_AMOUNT_INSUFFICIEN");
- }
- if (FinancialConfigService::isFrozenDistributionChannel($distribution_channel_id)) {
-
- return response()->error("WITHDRAW_CASH_AMOUNT_FROZEN");
- }
-
- if (!CashAccountService::isCashAccountExits($distribution_channel_id)) {
- return response()->error("WITHDRAW_CASH_AMOUNT_ACCOUNT");
- }
-
- if (WithdrawCashService::isWithdrawCashChannelToToday($distribution_channel_id)) {
- return response()->error("WITHDRAW_CASH_TODAY_USE");
- }
- $is_company = 0;
- $cashAccount = CashAccountService::getCashAccountSingle($distribution_channel_id);
- if (!empty($cashAccount)) {
- $is_company = $cashAccount['is_company'];
- }
-
-
- $enable_by_companys = FinancialPayMerchantBalanceService::getChannelEnableWithdrawByCompanyId($distribution_channel_id);
- \Log::info('distribution_channel_id:' . $distribution_channel_id . 'amount:' . $amount);
- \Log::info('distribution_channel_id:' . $distribution_channel_id . 'enable:' . json_encode($enable_by_companys));
- DB::beginTransaction();
- try {
- foreach ($enable_by_companys as $enable_by_company) {
- $with_draw_amount = ($enable_by_company->enable_withdrawal_amount >= $amount) ? $amount : $enable_by_company->enable_withdrawal_amount;
- $pay_company_id = $enable_by_company->pay_merchant_company_id;
- WithdrawCashService::addWithdrawCash($distribution_channel_id, $with_draw_amount, $remark, $pay_company_id);
- $financialStatUp = FinancialStatService::updateFinancialStatByWithdraw($distribution_channel_id, $with_draw_amount);
- FinancialPayMerchantBalanceService::updateBalanceByWithdraw($with_draw_amount, $distribution_channel_id, $pay_company_id);
- $amount = $amount - $with_draw_amount;
-
- if ($is_company == 0 && in_array($pay_company_id, explode(',', env('ZW_COMPANY_IDS')))) {
- DB::rollBack();
- return response()->error('NOT_ALLOWED_PRIVATE_ACCOUNT');
- }
- if ($amount <= 0) {
- break;
- }
- }
- } catch (\Exception $e) {
- DB::rollBack();
- \Log::error('withdraw error !' . ($e->getMessage()));
- return response()->error('WITHDRAW_CASHES_FAILED');
- }
- DB::commit();
-
- $enable_amount = $financialStatUp['enable_withdrawal_amount'];
-
- $withdraw_pending_amount = $financialStatUp['withdraw_pending_amount'];
- return response()->success(compact('enable_amount', 'withdraw_pending_amount'));
- }
-
- public static function addWithdrawCash($channelId, $amount, $remark, $pay_company_id)
- {
- $tallage = FinanceService::getWithdrawCashTallage($channelId, $amount);
- $is_company = 0;
- $channelName = ChannelService::getChannelNicknameById($channelId);
- $cashAccount = CashAccountService::getCashAccountSingle($channelId);
- if (!empty($cashAccount)) {
- $is_company = $cashAccount['is_company'];
- $dataWithdrawCash['account_bank'] = $cashAccount->account_bank;
- $dataWithdrawCash['bank_account'] = $cashAccount->card_number;
- $dataWithdrawCash['account_name'] = $cashAccount->account_name;
- }
- $dataWithdrawCash['distribution_channel_id'] = $channelId;
- $dataWithdrawCash['distribution_channel_name'] = $channelName;
- $dataWithdrawCash['amount'] = $amount;
- $dataWithdrawCash['tallage'] = $tallage;
- $dataWithdrawCash['status'] = self::getWithdrawCashStatusStr(0);
- $dataWithdrawCash['remark'] = $remark;
- $dataWithdrawCash['is_company'] = $is_company;
- $dataWithdrawCash['pay_merchant_company_id'] = $pay_company_id;
- $withdrawCash = WithdrawCash::create($dataWithdrawCash);
- return $withdrawCash;
- }
-
- public static function updateWithdrawCashStatus($id, $userId = '', $statusIn, $remark = '', $serialNumber = '')
- {
-
-
- $withdrawCash = WithdrawCash::where('id', $id)->first();
- if (!empty($withdrawCash) && is_numeric($statusIn)) {
- if (WithdrawCashService::isWithdrawCashStatusPaymentIngStr($withdrawCash['status'])) {
-
- } else {
- if (WithdrawCashService::isEditWithdrawCashStatus($id)) {
- return $withdrawCash;
- }
- }
- $userName = "";
- if ($userId) {
- $manage = ManageService::getById($userId);
- if (!empty($manage)) {
- $userName = $manage['nickname'];
- }
- }
- $statusStr = WithdrawCashService::getWithdrawCashStatusStr($statusIn);
- if (empty($statusStr)) {
- } else {
- if ($statusStr) {
- $withdrawCash['status'] = $statusStr;
- }
- }
- if (WithdrawCashService::isWithdrawCashStatusCodeSuccess($statusIn)) {
-
- $financialStat = FinancialStat::getByDistributionChannel($withdrawCash['distribution_channel_id']);
-
- $financialStat['accumulative_withdrawal_amount'] = (float)$financialStat['accumulative_withdrawal_amount'] + (float)$withdrawCash['amount'];
-
- $financialStat['withdraw_pending_amount'] = (float)$financialStat['withdraw_pending_amount'] - (float)$withdrawCash['amount'];
- $financialStat->save();
-
- $financePayMerchantBalanceInfo = FinancialPayMerchantBalance::getOne($withdrawCash['distribution_channel_id'], $withdrawCash['pay_merchant_company_id']);
- \Log::info('financePayMerchantBalanceInfo:channel_id:' . $withdrawCash['distribution_channel_id'] . 'pay_merchant_company_id:' . $withdrawCash['pay_merchant_company_id'] . json_encode($financePayMerchantBalanceInfo));
-
- $financePayMerchantBalanceInfo->accumulative_withdrawal_amount = (float)$financePayMerchantBalanceInfo->accumulative_withdrawal_amount + (float)$withdrawCash['amount'];
-
- $financePayMerchantBalanceInfo->withdraw_pending_amount = (float)$financePayMerchantBalanceInfo->withdraw_pending_amount - (float)$withdrawCash['amount'];
- $financePayMerchantBalanceInfo->save();
- }
- if ($userId) {
- $withdrawCash['check_user_id'] = $userId;
- }
- if ($userName) {
- $withdrawCash['check_user_name'] = $userName;
- }
- if ($remark) {
- $withdrawCash['remark'] = $remark;
- }
- if ($serialNumber) {
- if (strpos($withdrawCash['serial_number'], $serialNumber) !== false) {
-
- } else {
- $serialNumber = $withdrawCash['serial_number'] . "," . $serialNumber;
- }
- $withdrawCash['serial_number'] = $serialNumber;
- }
- $withdrawCash->save();
- }
- return $withdrawCash;
- }
-
- public static function getWithdrawCash($id)
- {
- $withdrawCash = WithdrawCash::where('id', $id)->first();
- return $withdrawCash;
- }
-
- public static function getList($channel_id = '', $channel_name = '', $start_date = '', $end_date = '', $statusIn = '', $is_all = false)
- {
- $status = false;
- if (is_numeric($statusIn)) {
- $status = self::getWithdrawCashStatusStrList($statusIn);
- }
- return WithdrawCash::getListByDistributionChannel($channel_id, $channel_name, $start_date, $end_date, $status, $is_all);
- }
-
- public static function getFinancialCounting($params = [], $statusIn = '', $isAll = '')
- {
- if (is_numeric($statusIn)) {
- $params['status'] = self::getWithdrawCashStatusStrList($statusIn);
- }
- return WithdrawCash::getFinancialCountingParam($params, $isAll);
- }
-
- public static function getFinancialAudit($params = [], $statusIn = '', $isAll = false)
- {
- if (is_numeric($statusIn)) {
- $params['status'] = self::getWithdrawCashStatusStrList($statusIn);
- }
- return WithdrawCash::getFinancialAuditParam($params, $isAll);
- }
-
- public static function isWithdrawCashChannelToToday($channelId)
- {
- $withdrawCash = WithdrawCash::getWithdrawCashLastRecord($channelId);
- if (empty($withdrawCash)) {
- return false;
- }
- $todayStart = strtotime(date('Y-m-d', time()) . ' 00:00:00');
- $createTime = strtotime($withdrawCash['created_at']);
- if ($createTime > $todayStart) {
- return true;
- }
- return false;
- }
-
- public static function isEditWithdrawCashStatus($withdrawCashId)
- {
- $withdrawCash = WithdrawCash::where('id', $withdrawCashId)->first();
- if (empty($withdrawCash)) {
- return false;
- }
- if (
- $withdrawCash['status'] == "打款中"
- || $withdrawCash['status'] == "自动打款中"
- || $withdrawCash['status'] == "人工打款中"
- || $withdrawCash['status'] == "打款成功"
- || $withdrawCash['status'] == "自动打款成功"
- || $withdrawCash['status'] == "人工打款成功"
- ) {
- return true;
- }
- return false;
- }
-
- public static function isWithdrawCashStatusSuccess($withdrawCashId)
- {
- $withdrawCash = WithdrawCash::where('id', $withdrawCashId)->first();
- if (empty($withdrawCash)) {
- return false;
- }
- if (
- $withdrawCash['status'] == "打款成功"
- || $withdrawCash['status'] == "自动打款成功"
- || $withdrawCash['status'] == "人工打款成功"
- ) {
- return true;
- }
- return false;
- }
-
- public static function isWithdrawCashStatusPaymentWaitStr($statusStr = '')
- {
- if (
- $statusStr == "待打款"
- || $statusStr == "自动待打款"
- || $statusStr == "人工待打款"
- ) {
- return true;
- }
- return false;
- }
-
- public static function isWithdrawCashStatusPaymentIngStr($statusStr = '')
- {
- if (
- $statusStr == "打款中"
- || $statusStr == "自动打款中"
- || $statusStr == "人工打款中"
- ) {
- return true;
- }
- return false;
- }
-
- public static function isWithdrawCashStatusCodeSuccess($statusIn = '')
- {
- if ($statusIn == 30 || $statusIn == 31 || $statusIn == 32) {
- return true;
- }
- return false;
- }
-
- public static function isWithdrawCashStatusStrSuccess($statusStr = '')
- {
- if (
- $statusStr == "打款成功"
- || $statusStr == "自动打款成功"
- || $statusStr == "人工打款成功"
- ) {
- return true;
- }
- return false;
- }
-
- public static function getWithdrawCashStatusStrSuccessAndFailList()
- {
- $status = [];
- $status[] = "打款成功";
- $status[] = "自动打款成功";
- $status[] = "人工打款成功";
- $status[] = "打款失败";
- $status[] = "自动打款失败";
- $status[] = "人工打款失败";
- return $status;
- }
-
- public static function getWithdrawCashStatusStrSuccessFailCheckedSuccessList()
- {
- $status = [];
- $status[] = "审核通过";
- $status[] = "审核不通过";
- $status[] = "待打款";
- $status[] = "自动待打款";
- $status[] = "人工待打款";
- $status[] = "打款中";
- $status[] = "自动打款中";
- $status[] = "人工打款中";
- $status[] = "打款成功";
- $status[] = "自动打款成功";
- $status[] = "人工打款成功";
- $status[] = "打款失败";
- $status[] = "自动打款失败";
- $status[] = "人工打款失败";
- return $status;
- }
- public static function getWithdrawCashStatusList($type)
- {
- if ($type == 10) {
-
- $params = [
- ['code' => 0, 'name' => "待审核", 'show' => 1],
-
-
- ['code' => 9, 'name' => "审核不通过", 'show' => 1],
- ['code' => 10, 'name' => "待打款", 'show' => 1],
-
-
- ['code' => 20, 'name' => "打款中", 'show' => 1],
-
-
- ['code' => 30, 'name' => "打款成功", 'show' => 1],
-
-
- ['code' => 40, 'name' => "打款失败", 'show' => 1],
-
-
-
- ];
- return $params;
- }
- if ($type == 20) {
-
- $params = [
- ['code' => 0, 'name' => "待审核", 'show' => 1],
- ['code' => 1, 'name' => "审核中", 'show' => 0],
- ['code' => 2, 'name' => "审核通过", 'show' => 0],
- ['code' => 9, 'name' => "审核不通过", 'show' => 1],
- ['code' => 10, 'name' => "待打款", 'show' => 1],
- ['code' => 11, 'name' => "自动待打款", 'show' => 0],
- ['code' => 13, 'name' => "人工待打款", 'show' => 0],
- ['code' => 20, 'name' => "打款中", 'show' => 1],
- ['code' => 21, 'name' => "自动打款中", 'show' => 0],
- ['code' => 22, 'name' => "人工打款中", 'show' => 0],
- ['code' => 30, 'name' => "打款成功", 'show' => 1],
- ['code' => 31, 'name' => "自动打款成功", 'show' => 0],
- ['code' => 32, 'name' => "人工打款成功", 'show' => 0],
- ['code' => 40, 'name' => "打款失败", 'show' => 1],
- ['code' => 41, 'name' => "自动打款失败", 'show' => 0],
- ['code' => 42, 'name' => "人工打款失败", 'show' => 0],
- ['code' => -1, 'name' => "其他错误", 'show' => 0],
- ];
- return $params;
- }
- }
- public static function getWithdrawCashStatusStr($statusIn = '')
- {
- $status = '';
- if (is_numeric($statusIn)) {
- if ($statusIn == 0) {
- $status = "待审核";
- } else if ($statusIn == 1) {
- $status = "审核中";
- } else if ($statusIn == 2) {
- $status = "审核通过";
- } else if ($statusIn == 9) {
- $status = "审核不通过";
- } else if ($statusIn == 10) {
- $status = "待打款";
- } else if ($statusIn == 11) {
- $status = "自动待打款";
- } else if ($statusIn == 12) {
- $status = "人工待打款";
- } else if ($statusIn == 20) {
- $status = "打款中";
- } else if ($statusIn == 21) {
- $status = "自动打款中";
- } else if ($statusIn == 22) {
- $status = "人工打款中";
- } else if ($statusIn == 30) {
- $status = "打款成功";
- } else if ($statusIn == 31) {
- $status = "自动打款成功";
- } else if ($statusIn == 32) {
- $status = "人工打款成功";
- } else if ($statusIn == 40) {
- $status = "打款失败";
- } else if ($statusIn == 41) {
- $status = "自动打款失败";
- } else if ($statusIn == 42) {
- $status = "人工打款失败";
- } else if ($statusIn == -1) {
- $status = "其他错误";
- } else {
- $status = "";
- }
- }
- return $status;
- }
- public static function getWithdrawCashStatusStrList($statusIn = '')
- {
- $status = '';
- if (is_numeric($statusIn)) {
- $status = [];
- if ($statusIn == 0) {
- $status[] = "待审核";
- } else if ($statusIn == 1) {
- $status[] = "审核中";
- } else if ($statusIn == 2) {
- $status[] = "审核通过";
- } else if ($statusIn == 9) {
- $status[] = "审核不通过";
- } else if ($statusIn == 10) {
- $status[] = "待打款";
- $status[] = "自动待打款";
- $status[] = "人工待打款";
- } else if ($statusIn == 11) {
- $status[] = "自动待打款";
- } else if ($statusIn == 12) {
- $status[] = "人工待打款";
- } else if ($statusIn == 20) {
- $status[] = "打款中";
- $status[] = "自动打款中";
- $status[] = "人工打款中";
- } else if ($statusIn == 21) {
- $status[] = "自动打款中";
- } else if ($statusIn == 22) {
- $status[] = "人工打款中";
- } else if ($statusIn == 30) {
- $status[] = "打款成功";
- $status[] = "自动打款成功";
- $status[] = "人工打款成功";
- } else if ($statusIn == 31) {
- $status[] = "自动打款成功";
- } else if ($statusIn == 32) {
- $status[] = "人工打款成功";
- } else if ($statusIn == 40) {
- $status[] = "打款失败";
- $status[] = "自动打款失败";
- $status[] = "人工打款失败";
- } else if ($statusIn == 41) {
- $status[] = "自动打款失败";
- } else if ($statusIn == 42) {
- $status[] = "人工打款失败";
- } else if ($statusIn == -1) {
- $status[] = "其他错误";
- } else {
- $status[] = "未知错误码";
- }
- }
- return $status;
- }
- public static function updateWithdrawCashType($id, $param)
- {
- return WithdrawCash::where('id', $id)->update($param);
- }
- }
|