engineering/app/adminapi/logic/finance/FinanceInvoiceApplyLogic.php
2024-01-15 14:08:56 +08:00

183 lines
7.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\adminapi\logic\finance;
use app\common\model\contract\Contract;
use app\common\model\contract\ContractNegotiation;
use app\common\model\dict\DictData;
use app\common\model\finance\FinanceInvoiceApply;
use app\common\logic\BaseLogic;
use app\common\model\finance\FinanceReturnedRecord;
use app\common\model\project\Project;
use app\common\model\custom\Custom;
use app\common\model\bid\BidBuyBiddingDocument;
use think\facade\Db;
/**
* FinanceInvoiceApply逻辑
* Class FinanceInvoiceApplyLogic
* @package app\adminapi\logic\finance
*/
class FinanceInvoiceApplyLogic extends BaseLogic
{
/**
* @notes 添加
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/12/13 15:01
*/
public static function add(array $params): bool
{
$contract = Contract::field('project_id')->where('id',$params['contract_id'])->findOrEmpty();
$tax_rate = DictData::where('type_value','tax_rate')->column('name','value');
Db::startTrans();
try {
FinanceInvoiceApply::create([
'project_id' => $contract['project_id'],
'contract_id' => $params['contract_id'],
'invoicing_code' => data_unique_code('开票申请'),
'invoicing_date' => strtotime($params['invoicing_date']),
'period' => $params['period'],
'tax_rate' => $params['tax_rate'],
'invoice_type' => $params['invoice_type'],
'invoicing_amount' => $params['invoicing_amount'],
'tax_amount' => ($tax_rate[$params['tax_rate']] / 100) * $params['invoicing_amount'],
'amount_including_tax' => (1 - $tax_rate[$params['tax_rate']] / 100) * $params['invoicing_amount'],
'content' => $params['content'] ?? '',
'invoice_no' => $params['invoice_no'] ?? '',
'remark' => $params['remark'] ?? '',
'annex' => $params['annex']? json_encode($params['annex']) : null,
'invoicing_company_name' => $params['invoicing_company_name'] ?? '',
'taxpayer_identification_number' => $params['taxpayer_identification_number'] ?? '',
'deposit_bank' => $params['deposit_bank'] ?? '',
'bank_accnout' => $params['bank_accnout'] ?? '',
'address_phone' => $params['address_phone'] ?? '',
'receiving_address' => $params['receiving_address'] ?? '',
'contacts' => $params['contacts'] ?? '',
'phone' => $params['phone'] ?? '',
'mailing_time' => !empty($params['mailing_time']) ? strtotime($params['mailing_time']) : 0,
'mailing_type' => $params['mailing_type'] ?? '',
'mailing_no' => $params['mailing_no'] ?? '',
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 编辑
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/12/13 15:01
*/
public static function edit(array $params): bool
{
$contract = Contract::field('project_id')->where('id',$params['contract_id'])->findOrEmpty();
$tax_rate = DictData::where('type_value','tax_rate')->column('name','value');
Db::startTrans();
try {
FinanceInvoiceApply::where('id', $params['id'])->update([
'project_id' => $contract['project_id'],
'contract_id' => $params['contract_id'],
'invoicing_date' => strtotime($params['invoicing_date']),
'period' => $params['period'],
'tax_rate' => $params['tax_rate'],
'invoice_type' => $params['invoice_type'],
'invoicing_amount' => $params['invoicing_amount'],
'tax_amount' => ($tax_rate[$params['tax_rate']] / 100) * $params['invoicing_amount'],
'amount_including_tax' => (1 - $tax_rate[$params['tax_rate']] / 100) * $params['invoicing_amount'],
'content' => $params['content'] ?? '',
'invoice_no' => $params['invoice_no'] ?? '',
'remark' => $params['remark'] ?? '',
'annex' => $params['annex']? json_encode($params['annex']) : null,
'invoicing_company_name' => $params['invoicing_company_name'] ?? '',
'taxpayer_identification_number' => $params['taxpayer_identification_number'] ?? '',
'deposit_bank' => $params['deposit_bank'] ?? '',
'bank_accnout' => $params['bank_accnout'] ?? '',
'address_phone' => $params['address_phone'] ?? '',
'receiving_address' => $params['receiving_address'] ?? '',
'contacts' => $params['contacts'] ?? '',
'phone' => $params['phone'] ?? '',
'mailing_time' => !empty($params['mailing_time']) ? strtotime($params['mailing_time']) : 0,
'mailing_type' => $params['mailing_type'] ?? '',
'mailing_no' => $params['mailing_no'] ?? '',
'update_time' => time(),
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/12/13 15:01
*/
public static function delete(array $params): bool
{
return FinanceInvoiceApply::destroy($params['id']);
}
/**
* @notes 获取详情
* @param $params
* @return array
* @author likeadmin
* @date 2023/12/13 15:01
*/
public static function detail($params): array
{
$data = FinanceInvoiceApply::findOrEmpty($params['id']);
$project = Project::field('custom_id,name,project_code')->where('id',$data['project_id'])->findOrEmpty();
$custom = Custom::field('name')->where('id',$project['custom_id'])->findOrEmpty();
$contract = Contract::field('contract_code,contract_name,amount')->where('id',$data['contract_id'])->findOrEmpty();
$data['project_name'] = $project['name'];
$data['project_code'] = $project['project_code'];
$data['custom_name'] = $custom['name'];
$data['contract_code'] = $contract['contract_code'];
$data['contract_name'] = $contract['contract_name'];
//合同洽商金额
$contract_negotiation_amount = ContractNegotiation::where('contract_id',$data['contract_id'])->sum('negotiation_amount');
//合同金额 = 合同金额 + 合同洽商金额
$data['contract_amount'] = $contract['amount'] + $contract_negotiation_amount;
//回款金额
$data['returned_amount'] = FinanceReturnedRecord::where('contract_id',$data['contract_id'])->sum('amount');
//累计开票金额
$data['total_invoicing_amount'] = FinanceInvoiceApply::where('contract_id',$data['contract_id'])->where('id','<>',$data['id'])->sum('invoicing_amount');
$data['period_text'] = $data->period_text;
$data['tax_rate_text'] = $data->tax_rate_text;
$data['invoice_type_text'] = $data->invoice_type_text;
return $data->toArray();
}
}