engineering/app/adminapi/logic/contract/ProcurementContractLogic.php

259 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\ProcurementContract;
use app\common\model\contract\ProcurementContractDetail;
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;
/**
* 采购合同逻辑
* Class ProcurementContractLogic
* @package app\adminapi\logic\contract
*/
class ProcurementContractLogic extends BaseLogic
{
/**
* @notes 添加采购合同
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/12/04 22:08
*/
public static function add(array $params): bool
{
//采购合同明细
$procurement_contract_detail =$params['procurement_contract_detail'];// json_decode($params['procurement_contract_detail'],true);
//付款计划
$payment_plan =$params['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($procurement_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 {
$procurementContract = ProcurementContract::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('CGHT'),
'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' => $params['annex']? json_encode($params['annex']) : '',
]);
foreach ($procurement_contract_detail as $v)
{
ProcurementContractDetail::create([
'contract_id' => $procurementContract->id,
'material_purchase_request_detail_id' => $v['material_purchase_request_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'],
]);
}
foreach ($payment_plan as $v)
{
FinancePaymentPlan::create([
'supplier_id' => $params['supplier_id'],
'project_id' => $params['project_id'],
'contract_id' => $procurementContract->id,
'contract_type' => 1,
'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/02 17:19
*/
public static function edit(array $params): bool
{
//采购合同明细
$procurement_contract_detail = $params['procurement_contract_detail'];//json_decode($params['procurement_contract_detail'],true);
//付款计划
$payment_plan = $params['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($procurement_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 {
ProcurementContract::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' => $params['annex']? json_encode($params['annex']) : '',
]);
foreach ($procurement_contract_detail as $v)
{
if(isset($v['id']) && $v != ''){
ProcurementContractDetail::where('id',$v['id'])->update([
'contract_id' => $params['id'],
'material_purchase_request_detail_id' => $v['material_purchase_request_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'],
]);
}else{
ProcurementContractDetail::create([
'contract_id' => $params['id'],
'material_purchase_request_detail_id' => $v['material_purchase_request_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'],
]);
}
}
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' => 1,
'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' => 1,
'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/04 22:08
*/
public static function detail($params): array
{
$data = ProcurementContract::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();
}
}