Commit 4b472f54 4b472f54100dd9a0f4d02d8957f65631e51dd77f by lemon

发票

1 parent bf612098
......@@ -6,7 +6,8 @@ use App\Helper\CacheKeyTools;
use App\Helper\RedisClient;
use App\Models\Legal\Bills;
use App\Models\Legal\Company;
use App\Models\Legal\StakeholderIncomeByPayer;
use App\Models\Legal\StakeholderIncomeSyncApp;
use App\Models\Legal\StakeholderIncomeSyncAppDetails;
use App\Services\ApiService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
......@@ -18,6 +19,8 @@ use Illuminate\Support\Facades\Log;
*/
class StakeholderIncomeSyncCommand extends Command
{
use \App\Traits\Bills;
/**
* The name and signature of the console command.
*
......@@ -36,8 +39,8 @@ class StakeholderIncomeSyncCommand extends Command
const COUNT = 1;
const BLOCK_TIME = 1000;
const TYPE_BILLS = 1001601; //账单分成
const TYPE_ROYALTY = 1001602; //合作伙伴打款 type = 1 版税分成
const TYPE_BILLS = "1001601"; //账单分成
const TYPE_ROYALTY = "1001602"; //合作伙伴打款 type = 1 版税分成
/**
* Create a new command instance.
......@@ -66,60 +69,104 @@ class StakeholderIncomeSyncCommand extends Command
//处理消息体
if($msg = $redis->xread([$key=>$id], self::COUNT, self::BLOCK_TIME)) {
$id = key($msg[$key]);
$income_item = $msg[$key][$id];
$id = key($msg[$key]);
$serial_no = $msg[$key][$id]['serial_no'];
Log::channel('api')->warning(__METHOD__."streamid:{$id}-即将处理任务", ['income_item'=>$income_item]);
Log::channel('api')->warning(__METHOD__."streamid:{$id}-即将处理任务", ['item'=>$msg[$key][$id]]);
$company = Company::query()->find($income_item['company_id']);
$bills = Bills::query()->find($income_item['related_id']);
$channel = 'TME';
//获取记录
if ($http_data = $this->resolveSyncData($serial_no)) {
//http
$http_res = ApiService::walletAddIncome($http_data);
$http_data = [
'busiId'=>$income_item['serial_no'],
'cardNo'=>$income_item['identifier'],
'faxMoney'=>$income_item['fax_money'],
'money'=>$income_item['money'],
'totalMoney'=>$income_item['total_money'],
'paymentCompany'=>$company->receipt_name,
'type'=>self::TYPE_BILLS,
'title'=>"{$bills->bill_section_start}-{$bills->bill_section_end}账单/{$channel}",
];
if (empty($http_res)) {
//重试 401/403/403
Log::channel('api')->warning(__METHOD__."streamid:{$id}-api请求失败", ['income_item'=>$income_item, 'http_data'=>$http_data]);
} else {
//成功
if ($res = StakeholderIncomeByPayer::query()->where(['serial_no'=>$income_item['serial_no']])->first()) {
}
if ($res['sync_status'] == 0) {
} else {
Log::channel('api')->info(__METHOD__, ['msg'=>'找不到需要同步的对应的收益记录', 'serial_no'=>$serial_no]);
$this->consumeTask($redis, $key, $id);
}
}
}
}
//直接未同步的数据才请求api
$http_res = ApiService::walletAddIncome($http_data);
/**
* 消费任务
* @param $redis
* @param $key
* @param $id
* @return mixed
*/
private function consumeTask($redis, $key, $id)
{
return $redis->xdel($key, [$id]);
}
if (empty($http_res)) {
//重试 401/403/403
Log::channel('api')->warning(__METHOD__."streamid:{$id}-api请求失败", ['income_item'=>$income_item, 'http_data'=>$http_data]);
} else {
/**
* 生成http请求数据
* @param $serial_no
* @return array
*/
private function resolveSyncData($serial_no)
{
if ($income = StakeholderIncomeSyncApp::query()->where(['serial_no'=>$serial_no, 'sync_status'=>0])->first()) {
$income_detail = StakeholderIncomeSyncAppDetails::query()->where(['track_serial_no'=>$serial_no])->orderBy('month')->get();
$update = [
'http_log'=>json_encode($http_res, JSON_UNESCAPED_UNICODE),
'busi_id'=>$http_res['data']['busiId'],
];
if ($income_detail->isNotEmpty()) {
return $this->formatHttpBody($income, $income_detail);
}
}
if ($http_res['code'] == 0) {
$update['sync_status'] = 1;
} else {
$update['sync_status'] = 2;
}
return [];
}
//记录请求api返回体并修改状态
if (StakeholderIncomeByPayer::query()->where(['serial_no'=>$income_item['serial_no']])->update($update)) {
$redis->xdel($key, [$id]);
}
}
/**
* @param $income
* @param $income_detail
* @return array
*/
private function formatHttpBody($income, $income_detail)
{
} else {
$redis->xdel($key, [$id]);
}
}
}
$company = Company::query()->find($income->company_id);
$bills = Bills::query()->find($income->related_id);
$body = [
'busiId' => $income->serial_no,
'cardNo' => $income->identifier,
'cost' => $income->cost_amount,
'deductAmount' => $income->deduct_prepaid,
'faxMoney' => $income->fax_money,
'money' => $income->money,
'totalMoney' => $income->total_money,
'type' => self::TYPE_BILLS,
'title' => $this->billsTitle($bills),//账单标题
'paymentCompany'=> $company->receipt_name, //付款公司营业执照全称
'addLevelTwoIncomeRequests' => [],
];
foreach ($income_detail as $item) {
$body['addLevelTwoIncomeRequests'][] = [
'actualTime' => (string)$item->created_at,
'busiId' => $item->serial_no,
'faxMoney' => $item->fax_money,
'money' => $item->money,
'totalMoney' => $item->total_money,
'paymentCompany' => $company->receipt_name, //付款公司营业执照全称
'songNum' => $item->song_num,
'title' => $this->billsSubTitle($item->month),
'type' => self::TYPE_BILLS,
];
}
return $body;
}
}
......
......@@ -17,4 +17,13 @@ class CacheKeyTools
{
return config('cache.key')['bills_sync'];
}
/**
* @return mixed
*/
public static function channelname()
{
return config('cache.key')['channelname'];
}
}
......
<?php
namespace App\Models\Legal;
use App\Helper\CacheKeyTools;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
/**
* Class Channel
* @package App\Models
*/
class Channel extends BaseModel
{
use SoftDeletes;
const TIME = 86400;
/**
* @return mixed
*/
public static function getChannelName()
{
if (!$res = Cache::get(CacheKeyTools::channelname())) {
$res = Channel::query()->where(['pid'=>0])->pluck('name', 'id')->toArray();
Cache::put(CacheKeyTools::channelname(), $res, self::TIME);
}
return $res;
}
}
<?php
namespace App\Models\Legal;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* 进账收益流水
* Class StakeholderIncomeByPayer
* @package App\Models\Legal
*/
class StakeholderIncomeByPayer extends BaseModel
{
use HasFactory;
const TYPE_INCOME = 1;
const TYPE_PAY = 2;
public $table = 'stakeholder_income_by_payer';
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function company()
{
return $this->belongsTo(Company::class, 'company_id', 'company_id');
}
}
<?php
namespace App\Models\Legal;
use App\Models\BaseModel;
use Illuminate\Support\Facades\DB;
/**
* Class stakeholderIncomeSyncApp
* @package App\Models
*/
class StakeholderIncomeSyncApp extends BaseModel
{
const TYPE_INCOME = 1; //收入
const TYPE_PAY = 2; //支出
/**
* @var string
*/
protected $table = 'stakeholder_income_sync_app';
/**
* @var array
*/
protected $guarded = [];
/**
* 付款方
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function subject()
{
return $this->belongsTo(Subject::class, 'subject_no', 'no')->where('attr', 'like', '%1%');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function stakeholder()
{
return $this->belongsTo(Stakeholder::class, 'stakeholder_id');
}
}
<?php
namespace App\Models\Legal;
use App\Models\BaseModel;
use Illuminate\Support\Facades\DB;
/**
* Class stakeholderIncomeSyncApp
* @package App\Models
*/
class StakeholderIncomeSyncAppDetails extends BaseModel
{
const TYPE_INCOME = 1; //收入
const TYPE_PAY = 2; //支出
/**
* @var string
*/
protected $table = 'stakeholder_income_sync_app_details';
/**
* @var array
*/
protected $guarded = [];
/**
* 付款方
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function subject()
{
return $this->belongsTo(Subject::class, 'subject_no', 'no')->where('attr', 'like', '%1%');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function stakeholder()
{
return $this->belongsTo(Stakeholder::class, 'stakeholder_id');
}
}
<?php
namespace App\Models\Legal;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
/**
* Class StakeholderIncomeSyncLogs
* @package App\Models\Legal
*/
class StakeholderIncomeSyncLogs extends BaseModel
{
public $table = 'stakeholder_income_sync_logs';
}
......@@ -15,7 +15,7 @@ use App\Models\Legal\StakeholderIncomeByPayer;
class ApiService
{
protected static $uri = [
'walletAddIncome'=> '/api/wallet/addIncome', //外部系统入账-外部可调用
'walletAddIncome'=> '/api/income/addIncomeAndDetail', //外部系统入账-外部可调用
];
/**
......
......@@ -41,22 +41,18 @@ class MusicianWithdrawService extends Service
*/
public function receiptInfoByName()
{
/*
if (!StakeholderIncomeByPayer::query()->where(['sync_status'=>1, 'withdraw_status'=>0, 'identifier'=>$this->identifier->identifier])->count()) {
return Response::error(ErrorCode::WITHDRAW_NO_RECORD);
}
*/
if ($receipt = Company::query()->where(['receipt_name'=>$this->request->input('name')])->first()) {
return Response::success([
'receipt_type'=>$receipt->receipt_type,
'receipt_consignee'=>$receipt->receipt_consignee,
'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_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();
......
<?php
namespace App\Traits;
use App\Models\Legal\Channel;
/**
* 账单
* Class Bills
* @package App\Traits
*/
trait Bills
{
/**
* 获取账单名字
* @param \App\Models\Legal\Bills $bills
*/
public function billsTitle(\App\Models\Legal\Bills $bills)
{
$start_month = date('m', strtotime($bills->bill_section_start));
$end_month = date('m', strtotime($bills->bill_section_end));
$channel = Channel::getChannelName()[$bills->channel] ?? '';
return "{$start_month}-{$end_month}{$channel}版税账单";
}
/**
* @param $month
* @return string
*/
public function billsSubTitle($month)
{
return $month . "月账单";
}
}
......@@ -109,7 +109,8 @@ return [
//cache_key
'key'=> [
'bills_sync'=>"bills:sync",
'bills_sync' =>'bills:sync',
'channelname'=>'channelname',
]
];
......
......@@ -38,7 +38,7 @@ Route::group([], function (){
//提现发票抬头
Route::post('withdraw/receipt', 'MusicianWithdrawController@receipt');
//提现发票抬头 - 通过公司中文
Route::post('withdraw/receipt_by_name', 'MusicianWithdrawController@receiptByName');
Route::get('withdraw/receipt_by_name', 'MusicianWithdrawController@receiptByName');
//账单状态修改
Route::post('withdraw/status', 'MusicianWithdrawController@status');
......