201 lines
7.0 KiB
PHP
201 lines
7.0 KiB
PHP
<?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\financial;
|
||
|
||
|
||
use app\common\logic\BaseLogic;
|
||
use app\common\model\auth\Admin;
|
||
use app\common\model\financial\FinancialInvoice;
|
||
use app\common\model\financial\FinancialRefund;
|
||
use app\common\model\marketing\MarketingContract;
|
||
use app\common\model\marketing\MarketingCustom;
|
||
use think\facade\Db;
|
||
use ZipArchive;
|
||
|
||
|
||
/**
|
||
* 财务管理--开票台账逻辑
|
||
* Class FinancialInvoiceLogic
|
||
* @package app\adminapi\logic\financial
|
||
*/
|
||
class FinancialInvoiceLogic extends BaseLogic
|
||
{
|
||
|
||
|
||
/**
|
||
* @notes 添加财务管理--开票台账
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2024/03/25 14:09
|
||
*/
|
||
public static function add(array $params): bool
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
FinancialInvoice::create([
|
||
'contract_id' => $params['contract_id'],
|
||
'invoice_code' => data_unique_code('HTKP'),
|
||
'invoice_type' => $params['invoice_type'],
|
||
'apply_amount' => $params['apply_amount'],
|
||
'apply_company' => $params['apply_company'],
|
||
'apply_company_number' => $params['apply_company_number'],
|
||
'apply_company_address' => $params['apply_company_address'] ?? '',
|
||
'apply_company_telephone' => $params['apply_company_telephone'] ?? '',
|
||
'apply_company_bank' => $params['apply_company_bank'] ?? '',
|
||
'apply_company_account' => $params['apply_company_account'] ?? '',
|
||
'apply_contact' => $params['apply_contact'] ?? '',
|
||
'apply_email' => $params['apply_email'] ?? '',
|
||
'pay_type' => $params['pay_type'] ?? '',
|
||
'invoice_content' => $params['invoice_content'] ?? '',
|
||
'annex' => $params['annex'] ? json_encode($params['annex']) : null,
|
||
'create_user' => $params['create_user'],
|
||
'create_time' => !empty($params['create_time']) ? strtotime($params['create_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 2024/03/25 14:09
|
||
*/
|
||
public static function edit(array $params): bool
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
FinancialInvoice::where('id', $params['id'])->update([
|
||
'contract_id' => $params['contract_id'],
|
||
'invoice_type' => $params['invoice_type'],
|
||
'apply_amount' => $params['apply_amount'],
|
||
'apply_company' => $params['apply_company'],
|
||
'apply_company_number' => $params['apply_company_number'],
|
||
'apply_company_address' => $params['apply_company_address'] ?? '',
|
||
'apply_company_telephone' => $params['apply_company_telephone'] ?? '',
|
||
'apply_company_bank' => $params['apply_company_bank'] ?? '',
|
||
'apply_company_account' => $params['apply_company_account'] ?? '',
|
||
'apply_contact' => $params['apply_contact'] ?? '',
|
||
'apply_email' => $params['apply_email'] ?? '',
|
||
'pay_type' => $params['pay_type'] ?? '',
|
||
'invoice_content' => $params['invoice_content'] ?? '',
|
||
'annex' => $params['annex'] ? json_encode($params['annex']) : null,
|
||
'create_user' => $params['create_user'],
|
||
'create_time' => !empty($params['create_time']) ? strtotime($params['create_time']) : time(),
|
||
'update_time' => time()
|
||
]);
|
||
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public static function download(array $params): bool|string
|
||
{
|
||
try {
|
||
$data = FinancialInvoice::field('annex')->where('id', $params['id'])->findOrEmpty();
|
||
if ($data->isEmpty() || !$data['annex']) {
|
||
self::setError("文件为空");
|
||
return false;
|
||
}
|
||
$zipname = 'uploads/financial_invoice_' . $params['id'] . '.zip';
|
||
if (file_exists($zipname)) {
|
||
return $zipname;
|
||
}
|
||
$files = [];
|
||
foreach ($data['annex'] as $v) {
|
||
if (!empty($v['uri'])) {
|
||
$file = explode('uploads', $v['uri']);
|
||
$files[] = 'uploads' . $file[1];
|
||
}
|
||
}
|
||
$zip = new ZipArchive();
|
||
$res = $zip->open($zipname, ZipArchive::CREATE);
|
||
if ($res === TRUE) {
|
||
foreach ($files as $file) {
|
||
if (file_exists($file)) {
|
||
$new_filename = substr($file, strrpos($file, '/') + 1);
|
||
$zip->addFile($file, $new_filename);
|
||
}
|
||
}
|
||
}
|
||
$zip->close();
|
||
return $zipname;
|
||
} catch (\Exception $e) {
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 删除财务管理--开票台账
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2024/03/25 14:09
|
||
*/
|
||
public static function delete(array $params): bool
|
||
{
|
||
return FinancialInvoice::destroy($params['id']);
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 获取财务管理--开票台账详情
|
||
* @param $params
|
||
* @return array
|
||
* @author likeadmin
|
||
* @date 2024/03/25 14:09
|
||
*/
|
||
public static function detail($params): array
|
||
{
|
||
$data = FinancialInvoice::withoutField('update_time,delete_time')->findOrEmpty($params['id']);
|
||
$contract = MarketingContract::field('contract_name,contract_type,part_a,part_b,signed_amount,create_time')->where('id', $data['contract_id'])->findOrEmpty();
|
||
$custom = MarketingCustom::field('name')->where('id', $contract['part_a'])->findOrEmpty();
|
||
$data['invoice_type_text'] = $data->invoice_type_text;
|
||
$data['contract_name'] = $contract['contract_name'];
|
||
$data['contract_type'] = !$contract->isEmpty() ? $contract->contract_type_text : '';
|
||
$data['part_a'] = $custom['name'];
|
||
$data['part_b'] = $contract['part_b'];
|
||
$data['sign_money'] = $contract['signed_amount'];
|
||
$data['sign_time'] = $contract['create_time'];
|
||
$data['total_invoice_amount'] = FinancialInvoice::where('contract_id', $data['contract_id'])->sum('apply_amount');
|
||
$data['total_refund_amount'] = FinancialRefund::where('contract_id', $data['contract_id'])->sum('amount');
|
||
$admin = Admin::field('name')->where('id', $data['apply_contact'])->findOrEmpty();
|
||
$data['apply_contact_name'] = $admin?->name;
|
||
return $data->toArray();
|
||
}
|
||
|
||
public static function datas()
|
||
{
|
||
return FinancialInvoice::field(['id', 'invoice_code'])->order(['id' => 'desc'])->select()->each(function ($data) {
|
||
$data['projectinfo'] = 'ID:' . $data['id'] . ' / 编号:' . $data['invoice_code'];
|
||
})->toArray();
|
||
}
|
||
} |