MusicianWithdrawService.php 4.97 KB
<?php

namespace App\Services;

use App\Helper\ErrorCode;
use App\Helper\Response;
use App\Models\Legal\Company;
use App\Models\Legal\Contract;
use App\Models\Legal\StakeholderIncomeByPayer;
use App\Models\Legal\StakeholderSongCollate;

/**
 * Class MusicianAgreementService
 * @package App\Services
 */
class MusicianWithdrawService extends Service
{
    /**
     * 返回账单详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function walletDetail()
    {
        $income_by_payer = StakeholderIncomeByPayer::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
     */
    public function receiptInfo()
    {
        $receipt = [];

        StakeholderIncomeByPayer::query()->with('company:company_id,receipt_type,receipt_name,receipt_no,receipt_tel,receipt_address,receipt_bank,receipt_bank_no')
            ->where(['identifier'=>$this->identifier->identifier, 'sync_status'=>1])->whereIn('serial_no', $this->request->input('serial_no'))->select(['id', 'company_id', 'serial_no', 'busi_id'])->get()->map(function ($item) use (&$receipt)  {

                        if (!empty($item->company->company_id)) {
                            $receipt[$item->company->company_id] = [
                                'receipt_type'=>$item->company->receipt_type,
                                'receipt_name'=>$item->company->receipt_name,
                                'receipt_no'  =>$item->company->receipt_no,
                                'receipt_tel' =>$item->company->receipt_tel,
                                'receipt_address'=>$item->company->receipt_address,
                                'receipt_bank'=>$item->company->receipt_bank,
                                'receipt_bank_no'=>$item->company->receipt_bank_no,
                            ];
                        }

                        return $receipt;
                    });

        return Response::success(array_values($receipt));
    }

    /**
     * 修改提现状态
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function changeStatus()
    {
        $withdraw_status = '';

        $query = StakeholderIncomeByPayer::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();
        }
    }

}