MusicianWithdrawService.php
7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace App\Services;
use App\Helper\CacheKeyTools;
use App\Helper\ErrorCode;
use App\Helper\RedisClient;
use App\Helper\Response;
use App\Models\Legal\Company;
use App\Models\Legal\Contract;
use App\Models\Legal\StakeholderBalanceByPayer;
use App\Models\Legal\StakeholderBalanceDetail;
use App\Models\Legal\StakeholderIncomeSyncApp;
use App\Models\Legal\StakeholderIncomeSyncAppDetails;
use App\Models\Legal\StakeholderPrepaid;
use App\Models\Legal\StakeholderSongCollate;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
* Class MusicianAgreementService
* @package App\Services
*/
class MusicianWithdrawService extends Service
{
/**
* 返回账单详情
* @return \Illuminate\Http\JsonResponse
*/
public function walletDetail()
{
$income_by_payer = StakeholderIncomeSyncAppDetails::query()->where(['serial_no'=>$this->request->input('busi_id'), 'sync_status'=>1])->first();
if (empty($income_by_payer)) return Response::error(ErrorCode::ORDER_NO_FOUND);
$collate = StakeholderSongCollate::query()->with('song:id,name,singer')->where([
'bills_id'=>$income_by_payer->related_id, 'subject_no'=>$income_by_payer->subject_no])
->select(['song_id', 'role', 'cost_amount', 'deduct_amount', 'real_share_amount'])->paginate($this->pageSize);
foreach ($collate as &$item) {
$item->setAttribute('role', Contract::transformRole($item->role));
}
return Response::success($collate);
}
/**
* 发票信息 - 通过名字
* @return \Illuminate\Http\JsonResponse|mixed
*/
public function receiptInfoByName()
{
if ($receipt = Company::query()->where(['receipt_name'=>$this->request->input('name')])->first()) {
return Response::success([
'receipt_type' => $receipt->receipt_type,
'receipt_name' => $receipt->receipt_name,
'receipt_no' => $receipt->receipt_no,
'receipt_tel' => $receipt->receipt_tel,
'receipt_address' => $receipt->receipt_address,
'receipt_bank' => $receipt->receipt_bank,
'receipt_bank_no' => $receipt->receipt_bank_no,
'receipt_consignee' => $receipt->post_consignee, //收件人
'post_address' => $receipt->post_address, //邮寄地址
'post_phone' => $receipt->post_phone, //邮寄电话
]);
} else {
return Response::error();
}
}
/**
* 修改提现状态
* @return \Illuminate\Http\JsonResponse|mixed
*/
public function changeStatus()
{
$withdraw_status = '';
$query = StakeholderIncomeSyncAppDetails::query()->where(['sync_status'=>1, 'identifier'=>$this->identifier->identifier])
->whereIn('serial_no', $this->request->input('busi_id'));
switch ($this->request->input('type')) {
case 'confirm': //已确认
$withdraw_status = 1;
$query = $query->where(['withdraw_status'=>0]);
break;
case 'fail': //提现失败 -> 待提现
$withdraw_status = 1;
$query = $query->where(['withdraw_status'=>2]);
break;
case 'advance': //提现中
$withdraw_status = 2;
$query = $query->where(['withdraw_status'=>1]);
break;
case 'success': //提现完成
$withdraw_status = 3;
$query = $query->where(['withdraw_status'=>2]);
break;
}
if ($query->update(['withdraw_status'=>$withdraw_status])) {
return Response::success();
} else {
return Response::error();
}
}
/**
* 确认账单
* @return \Illuminate\Http\JsonResponse
*/
public function billConfirm()
{
if (RedisClient::instance('bills')->set(CacheKeyTools::billsConfirm($this->request->serial_no), true, 'EX', 3, 'NX')) {
$app = StakeholderIncomeSyncApp::query()->where(['serial_no'=>$this->request->serial_no, 'sync_status'=>0, 'identifier'=>$this->identifier->identifier])->select(['id', 'subject_no', 'company_id', 'stakeholder_id', 'related_id'])->first();
DB::beginTransaction();
try {
if (empty($app)) throw new \Exception('找不到同步到分贝收益记录');
$condition = "{$app->stakeholder_id}|{$app->subject_no}";
$balance_detail = StakeholderBalanceDetail::query()->where(['status'=>0, 'company_id'=>$app->company_id, 'stakeholder_id'=>$app->stakeholder_id, 'bills_id'=>$app->related_id, 'subject_no'=>$app->subject_no])->first();
if (empty($app)) throw new \Exception('找不到相关入账记录');
//权益人-付款主体账户
StakeholderBalanceByPayer::query()->where(['condition'=>$condition])->update([
'account' => DB::raw("account + {$balance_detail->real_share_amount}"), //总收入
'balance' => DB::raw("balance + {$balance_detail->real_share_amount}"), //剩余提现余额
'confirmed_amount' => DB::raw("confirmed_amount - {$balance_detail->real_share_amount}"), //待确认收益
'deduct_prepaid' => DB::raw("deduct_prepaid + {$balance_detail->deduct_prepaid}"), //核销预付
'freeze_prepaid' => DB::raw("freeze_prepaid - {$balance_detail->deduct_prepaid}"), //冻结预付
]);
//权益人-付款主体预付账本
if (!empty($balance_detail['prepaid_detail'])) {
foreach ($balance_detail['prepaid_detail'] as $prepaid=>$prepaid_detail) {
StakeholderPrepaid::query()->where(['id'=>$prepaid])->update([
'freeze_prepaid' => DB::raw("freeze_prepaid - {$prepaid_detail['freeze_prepaid']}"),
'deduct_prepaid' => DB::raw("deduct_prepaid + {$prepaid_detail['freeze_prepaid']}"),
]);
}
}
//改变状态 - 已入账
StakeholderBalanceDetail::query()->where(['id'=>$balance_detail->id, 'status'=>0])->update(['status'=>1]);
//更新同步分贝记录
StakeholderIncomeSyncApp::query()->where(['id'=>$app->id, 'withdraw_status'=>0])->update([
'withdraw_status' => 1,
]);
DB::commit();
return Response::success();
} catch (\Exception $e) {
DB::rollBack();
Log::info(__METHOD__.':确认账单失败', ['msg' => $e->getMessage(), 'params'=>$this->request->all()]);
return Response::error(ErrorCode::WITHDRAW_CONFIRM_BILLS_FAIL);
}
} else {
return Response::error(ErrorCode::REPEAT_SUBMIT);
}
}
}