engineering/app/adminapi/logic/contract/SubcontractingContractLogic.php
2024-01-11 13:59:14 +08:00

264 lines
9.6 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\contract;
use app\common\model\contract\SubcontractingContract;
use app\common\model\contract\SubcontractingContractDetail;
use app\common\model\custom\Custom;
use app\common\logic\BaseLogic;
use app\common\model\dept\Dept;
use app\common\model\dept\Orgs;
use app\common\model\dict\DictData;
use app\common\model\finance\FinancePaymentPlan;
use app\common\model\project\Project;
use app\common\model\supplier\Supplier;
use think\facade\Db;
/**
* SubcontractingContract逻辑
* Class SubcontractingContractLogic
* @package app\adminapi\logic\contract
*/
class SubcontractingContractLogic extends BaseLogic
{
/**
* @notes 添加
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/12/12 17:15
*/
public static function add(array $params): bool
{
//分包合同明细
$subcontracting_contract_detail = json_decode($params['subcontracting_contract_detail'],true);
//付款计划
$payment_plan = json_decode($params['payment_plan'],true);
//税率字典值
$tax_rate_dict = DictData::where('type_value','tax_rate')->column('name','value');
//合同金额\不含税金额
$contract_amount = 0;
$amount_excluding_tax = 0;
foreach($subcontracting_contract_detail as $v){
$contract_amount += $v['price']*$v['num'];
$amount_excluding_tax += $v['num'] * $v['price'] * (1- $tax_rate_dict[$v['tax_rate']] / 100);
}
Db::startTrans();
try {
$res = SubcontractingContract::create([
'org_id' => $params['org_id'],
'dept_id' => $params['dept_id'],
'supplier_id' => $params['supplier_id'],
'project_id' => $params['project_id'],
'contract_no' => data_unique_code('FBHT'),
'contract_name' => $params['contract_name'],
'contract_type' => $params['contract_type'],
'signing_date' => strtotime($params['signing_date']),
'pay_type' => $params['pay_type'],
'account_period' => $params['account_period'],
//合同金额
'contract_amount' => $contract_amount,
//不含税金额
'amount_excluding_tax' => $amount_excluding_tax,
//质保金
'retention_money' => $contract_amount * ($params['retention_money_rate'] / 100),
//质保金比例
'retention_money_rate' => $params['retention_money_rate'],
'remark' => $params['remark'] ?? '',
'annex' => !empty($params['annex']) ? $params['annex'] : null
]);
foreach ($subcontracting_contract_detail as $v)
{
SubcontractingContractDetail::create([
'contract_id' => $res->id,
'subpackage_budget_detail_id' => $v['subpackage_budget_detail_id'],
'num' => $v['num'],
'price' => $v['price'],
'tax_rate' => $v['tax_rate'],
//不含税金额
'amount_excluding_tax' => $v['num'] * $v['price'] * (1- $tax_rate_dict[$v['tax_rate']] / 100),
//含税金额
'amount_including_tax' => $v['num'] * $v['price'],
'remark' => $v['remark'] ?? '',
]);
}
foreach ($payment_plan as $v)
{
FinancePaymentPlan::create([
'supplier_id' => $params['supplier_id'],
'project_id' => $params['project_id'],
'contract_id' => $res->id,
'contract_type' => 2,
'period' => $v['period'],
'pay_date' => strtotime($v['pay_date']),
'amount' => $v['amount'],
'status' => 1,
'remark' => $v['remark'] ?? '',
]);
}
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/12 17:15
*/
public static function edit(array $params): bool
{
//分包合同明细
$subcontracting_contract_detail = json_decode($params['subcontracting_contract_detail'],true);
//付款计划
$payment_plan = json_decode($params['payment_plan'],true);
//税率字典值
$tax_rate_dict = DictData::where('type_value','tax_rate')->column('name','value');
//合同金额\不含税金额
$contract_amount = 0;
$amount_excluding_tax = 0;
foreach($subcontracting_contract_detail as $v){
$contract_amount += $v['price']*$v['num'];
$amount_excluding_tax += $v['num'] * $v['price'] * (1- $tax_rate_dict[$v['tax_rate']] / 100);
}
Db::startTrans();
try {
SubcontractingContract::where('id',$params['id'])->update([
'org_id' => $params['org_id'],
'dept_id' => $params['dept_id'],
'supplier_id' => $params['supplier_id'],
'project_id' => $params['project_id'],
'contract_name' => $params['contract_name'],
'contract_type' => $params['contract_type'],
'signing_date' => strtotime($params['signing_date']),
'pay_type' => $params['pay_type'],
'account_period' => $params['account_period'],
//合同金额
'contract_amount' => $contract_amount,
//不含税金额
'amount_excluding_tax' => $amount_excluding_tax,
//质保金
'retention_money' => $contract_amount * ($params['retention_money_rate'] / 100),
//质保金比例
'retention_money_rate' => $params['retention_money_rate'],
'remark' => $params['remark'] ?? '',
'annex' => !empty($params['annex']) ? $params['annex'] : null
]);
foreach ($subcontracting_contract_detail as $v)
{
if(isset($v['id']) && $v != ''){
SubcontractingContractDetail::where('id',$v['id'])->update([
'contract_id' => $params['id'],
'subpackage_budget_detail_id' => $v['subpackage_budget_detail_id'],
'num' => $v['num'],
'price' => $v['price'],
'tax_rate' => $v['tax_rate'],
//不含税金额
'amount_excluding_tax' => $v['num'] * $v['price'] * (1- $tax_rate_dict[$v['tax_rate']] / 100),
//含税金额
'amount_including_tax' => $v['num'] * $v['price'],
'remark' => $v['remark'] ?? '',
]);
}else{
SubcontractingContractDetail::create([
'contract_id' => $params['id'],
'subpackage_budget_detail_id' => $v['subpackage_budget_detail_id'],
'num' => $v['num'],
'price' => $v['price'],
'tax_rate' => $v['tax_rate'],
//不含税金额
'amount_excluding_tax' => $v['num'] * $v['price'] * (1- $tax_rate_dict[$v['tax_rate']] / 100),
//含税金额
'amount_including_tax' => $v['num'] * $v['price'],
'remark' => $v['remark'] ?? '',
]);
}
}
foreach ($payment_plan as $v)
{
if(isset($v['id']) && $v != ''){
FinancePaymentPlan::where('id',$v['id'])->update([
'supplier_id' => $params['supplier_id'],
'project_id' => $params['project_id'],
'contract_id' => $params['id'],
'contract_type' => 2,
'period' => $v['period'],
'pay_date' => strtotime($v['pay_date']),
'amount' => $v['amount'],
'status' => 1,
'remark' => $v['remark'] ?? '',
]);
}else{
FinancePaymentPlan::create([
'supplier_id' => $params['supplier_id'],
'project_id' => $params['project_id'],
'contract_id' => $params['id'],
'contract_type' => 2,
'period' => $v['period'],
'pay_date' => strtotime($v['pay_date']),
'amount' => $v['amount'],
'status' => 1,
'remark' => $v['remark'] ?? '',
]);
}
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 获取详情
* @param $params
* @return array
* @author likeadmin
* @date 2023/12/12 17:15
*/
public static function detail($params): array
{
$data = SubcontractingContract::findOrEmpty($params['id']);
$org = Orgs::field('name')->where('id',$data['org_id'])->findOrEmpty();
$dept = Dept::field('name')->where('id',$data['dept_id'])->findOrEmpty();
$project = Project::field('name,project_code')->where('id',$data['project_id'])->findOrEmpty();
$supplier = Supplier::field('supplier_name,supplier_code')->where('id',$data['supplier_id'])->findOrEmpty();
$data['org_name'] = $org['name'];
$data['dept_name'] = $dept['name'];
$data['project_name'] = $project['name'];
$data['project_code'] = $project['project_code'];
$data['supplier_name'] = $supplier['supplier_name'];
$data['supplier_code'] = $supplier['supplier_code'];
$data['contract_type_text'] = $data->contract_type_text;
$data['pay_type_text'] = $data->pay_type_text;
$data['account_period_text'] = $data->account_period_text;
return $data->toArray();
}
}