Merge pull request 'update' (#143) from zhangwei into dev

Reviewed-on: #143
This commit is contained in:
weiz 2024-01-10 13:38:43 +08:00
commit 38305d9d99
7 changed files with 318 additions and 256 deletions

View File

@ -59,38 +59,6 @@ class ProcurementContractController extends BaseAdminController
return $this->fail(ProcurementContractLogic::getError());
}
/**
* @notes 编辑采购合同
* @return \think\response\Json
* @author likeadmin
* @date 2023/12/04 22:08
*/
public function edit()
{
$params = (new ProcurementContractValidate())->post()->goCheck('edit');
$result = ProcurementContractLogic::edit($params);
if (true === $result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(ProcurementContractLogic::getError());
}
/**
* @notes 删除采购合同
* @return \think\response\Json
* @author likeadmin
* @date 2023/12/04 22:08
*/
public function delete()
{
$params = (new ProcurementContractValidate())->post()->goCheck('delete');
ProcurementContractLogic::delete($params);
return $this->success('删除成功', [], 1, 1);
}
/**
* @notes 获取采购合同详情
* @return \think\response\Json

View File

@ -18,6 +18,9 @@ namespace app\adminapi\lists\contract;
use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\contract\ProcurementContract;
use app\common\lists\ListsSearchInterface;
use app\common\model\contract\ProcurementContractDetail;
use app\common\model\project\Project;
use app\common\model\supplier\Supplier;
use think\facade\Db;
/**
@ -38,20 +41,11 @@ class ProcurementContractLists extends BaseAdminDataLists implements ListsSearch
public function setSearch(): array
{
return [
'=' => ['pc.supplier_id', 'pc.project_id', 'pc.contract_no'],
'=' => ['supplier_id', 'project_id'],
'%like%' => ['contract_no','']
];
}
public function querySearch(): array
{
$queryWhere = [];
$queryWhere['pc.contract_cate'] = 1;
if (!empty($this->params['all'])) {
unset($queryWhere['contract_cate']);
}
return $queryWhere;
}
/**
* @notes 获取采购合同列表
* @return array
@ -63,26 +57,35 @@ class ProcurementContractLists extends BaseAdminDataLists implements ListsSearch
*/
public function lists(): array
{
return Db::name('ProcurementContract')->alias('pc')
->where($this->searchWhere)->where($this->querySearch())
->whereNull('pc.delete_time')
->leftJoin('project p','p.id = pc.project_id')
->field('pc.*, p.name as project_name, p.project_code')
return ProcurementContract::where($this->searchWhere)
->field('id,project_id,supplier_id,contract_no,contract_name,contract_type,signing_date,contract_amount')
->limit($this->limitOffset, $this->limitLength)
->order(['pc.id' => 'desc'])
->select()->each(function($item, $key){
//关联数据后续添加
$item['supplier_name'] = '供应商名称';
$item['approve_no'] = '付款单号';
$item['approve_step'] = '流程步骤';
$item['approve_settle_status'] = 1;
if (!empty($item['pay_plan'])) {
$planArray = json_decode($item['pay_plan'], true);
if (is_array($planArray)) {
$item['pay_plan'] = $planArray;
}
}
return $item;
->order(['id' => 'desc'])
->select()->each(function($data){
$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['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'] = $data->contract_type_text;
//已付款金额
$data['has_pay_amount'] = 0;
//未付款金额
$data['not_pay_amount'] = $data['contract_amount'] - $data['has_pay_amount'];
//已开票金额
$data['has_invoice_amount'] = 0;
//未开票金额
$data['not_invoice_amount'] = $data['contract_amount'] - $data['has_invoice_amount'];
//退款金额
$data['refund_amount'] = 0;
//数量
$data['num'] = ProcurementContractDetail::where('contract_id',$data['id'])->sum('num');
//已入库数量
$data['has_storage_num'] = 0;
//未入库数量
$data['not_storage_num'] = $data['num'] - $data['has_storage_num'];
return $data;
})
->toArray();
}
@ -96,10 +99,7 @@ class ProcurementContractLists extends BaseAdminDataLists implements ListsSearch
*/
public function count(): int
{
return Db::name('ProcurementContract')->alias('pc')
->where($this->searchWhere)->where($this->querySearch())
->whereNull('pc.delete_time')
->leftJoin('project p','p.id = pc.project_id')->count();
return ProcurementContract::where($this->searchWhere)->count();
}
}

View File

@ -19,6 +19,12 @@ 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;
@ -40,42 +46,71 @@ class ProcurementContractLogic extends BaseLogic
*/
public static function add(array $params): bool
{
//采购合同明细
$procurement_contract_detail = json_decode($params['procurement_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($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'] ?? 0,
'dept_id' => $params['dept_id'] ?? 0,
'supplier_id' => $params['supplier_id'] ?? 0,
'approve_id' => $params['approve_id'] ?? 0,
'project_id' => $params['project_id'] ?? 0,
'contract_cate' => 1,
'contract_no' => $params['contract_no'] ?? '',
'contract_type' => $params['contract_type'] ?? 0,
'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'] ?? 0,
'account_period' => $params['account_period'] ?? 0,
'amount' => $params['amount'] ?? 0,
'amount_excluding_tax' => $params['amount_excluding_tax'] ?? 0,
'amount_daxie' => $params['amount_daxie'] ?? '',
'retention_money_rate' => $params['retention_money_rate'] ?? '',
'retention_money' => $params['retention_money'] ?? 0,
'pay_plan' => empty($params['pay_plan']) ? [] : json_encode($params['pay_plan']),
'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'] ?? ''
'annex' => !empty($params['annex']) ? $params['annex'] : null
]);
foreach ($params['material'] ?? [] as $item)
foreach ($procurement_contract_detail as $v)
{
ProcurementContractDetail::create([
'procurement_contract_id' => $procurementContract->id,
'material_procurement_application_id' => $item['material_procurement_application_id'] ?? 0,
'num' => $item['num'] ?? 0,
'unit_price_including_tax' => $item['unit_price_including_tax'] ?? 0,
'tax_rate' => $item['tax_rate'] ?? 0,
'amount_excluding_tax' => $item['amount_excluding_tax'] ?? 0,
'amount_including_tax' => $item['amount_including_tax'] ?? 0,
'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) {
@ -84,78 +119,7 @@ class ProcurementContractLogic extends BaseLogic
return false;
}
}
/**
* @notes 编辑采购合同
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/12/04 22:08
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
ProcurementContract::where('id', $params['id'])->update([
'org_id' => $params['org_id'] ?? 0,
'dept_id' => $params['dept_id'] ?? 0,
'supplier_id' => $params['supplier_id'] ?? 0,
'approve_id' => $params['approve_id'] ?? 0,
'project_id' => $params['project_id'] ?? 0,
'contract_no' => $params['contract_no'] ?? '',
'supplier_contract_no' => $params['supplier_contract_no'] ?? '',
'contract_cate' => 1,
'contract_type' => $params['contract_type'] ?? 0,
'signing_date' => strtotime($params['signing_date']),
'pay_type' => $params['pay_type'] ?? 0,
'account_period' => $params['account_period'] ?? 0,
'amount' => $params['amount'] ?? 0,
'amount_excluding_tax' => $params['amount_excluding_tax'] ?? 0,
'amount_daxie' => $params['amount_daxie'] ?? '',
'retention_money_rate' => $params['retention_money_rate'] ?? '',
'retention_money' => $params['retention_money'] ?? 0,
'pay_plan' => empty($params['pay_plan']) ? [] : json_encode($params['pay_plan']),
'remark' => $params['remark'] ?? '',
'annex' => $params['annex'] ?? ''
]);
ProcurementContractDetail::where('procurement_contract_id', $params['id'])->update(['delete_time' =>time()]);
foreach ($params['material'] ?? [] as $item)
{
ProcurementContractDetail::create([
'procurement_contract_id' => $params['id'],
'material_procurement_application_id' => $item['material_procurement_application_id'] ?? 0,
'num' => $item['num'] ?? 0,
'unit_price_including_tax' => $item['unit_price_including_tax'] ?? 0,
'tax_rate' => $item['tax_rate'] ?? 0,
'amount_excluding_tax' => $item['amount_excluding_tax'] ?? 0,
'amount_including_tax' => $item['amount_including_tax'] ?? 0,
]);
}
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/04 22:08
*/
public static function delete(array $params): bool
{
ProcurementContractDetail::where('procurement_contract_id', $params['id'])->update(['delete_time' =>time()]);
return ProcurementContract::destroy($params['id']);
}
/**
* @notes 获取采购合同详情
@ -166,18 +130,20 @@ class ProcurementContractLogic extends BaseLogic
*/
public static function detail($params): array
{
$procurementContract = ProcurementContract::findOrEmpty($params['id']);
$procurementContract->supplier = null;
$procurementContract->custom = null;
if (!empty($procurementContract->project->custom_id)) {
$procurementContract->custom = Custom::findOrEmpty($procurementContract->project->custom_id);
}
$procurementContract->pay_plan = json_decode($procurementContract->pay_plan, true);
$procurementContract->project;
$procurementContract->material;
$procurementContract->org;
$procurementContract->dept;
$procurementContract->annex = json_decode($procurementContract->annex, true);
return $procurementContract->toArray();
$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'] = $data->contract_type_text;
$data['pay_type'] = $data->pay_type_text;
$data['account_period'] = $data->account_period_text;
return $data->toArray();
}
}

View File

@ -45,8 +45,8 @@ class QuotationLogic extends BaseLogic
{
$amount_including_tax = 0;
$quotation_detail = json_decode($params['quotation_detail'],true);
$tax_rate_dict = DictData::where('type_value','tax_rate')->column('name','value');
foreach($quotation_detail as &$v){
$tax_rate_dict = DictData::where('type_value','tax_rate')->column('name','value');
$amount_including_tax += $v['num'] * $v['tax_inclusive_price'];
$v['tax_inclusive_amount'] = $v['num'] * $v['tax_inclusive_price'];
$v['tax_exclusive_amount'] = $v['num'] * $v['tax_inclusive_price'] * (1- $tax_rate_dict[$v['tax_rate']] / 100);

View File

@ -229,7 +229,7 @@ class ContractValidate extends BaseValidate
{
$detail = json_decode($value,true);
if(empty($detail) || !is_array($detail)){
return '回款计划数据格式错误';
return '回款计划数据格式错误';
}
foreach($detail as $v) {
if(empty($v['period'])){
@ -243,7 +243,6 @@ class ContractValidate extends BaseValidate
if(empty($v['return_date'])){
return '请选择计划回款日期';
}
if(empty($v['amount'])){
return '金额不能为空';
}else{

View File

@ -15,6 +15,12 @@
namespace app\adminapi\validate\contract;
use app\common\model\dept\Dept;
use app\common\model\dept\Orgs;
use app\common\model\dict\DictData;
use app\common\model\material\MaterialPurchaseRequestDetail;
use app\common\model\project\Project;
use app\common\model\supplier\Supplier;
use app\common\validate\BaseValidate;
@ -32,11 +38,19 @@ class ProcurementContractValidate extends BaseValidate
*/
protected $rule = [
'id' => 'require',
'supplier_id' => 'require',
'approve_id' => 'require',
'project_id' => 'require',
'material' => 'require|array|checkMaterial',
'pay_plan' => 'require|array|checkPlan',
'org_id' => 'require|checkOrg',
'dept_id' => 'require|checkDept',
'supplier_id' => 'require|checkSupplier',
'project_id' => 'require|checkProject',
'contract_name' => 'require',
'contract_type' => 'require|checkContractType',
'signing_date' => 'require|dateFormat:Y-m-d',
'pay_type' => 'require|checkPayType',
'account_period' => 'require|checkAccountPeriod',
'retention_money_rate' => 'require|float|egt:0',
'annex' => 'checkAnnex',
'procurement_contract_detail' => 'require|checkProcurementContractDetail',
'payment_plan' => 'require|checkPaymentPlan'
];
@ -44,8 +58,23 @@ class ProcurementContractValidate extends BaseValidate
* 参数描述
* @var string[]
*/
protected $field = [
'id' => 'id',
protected $message = [
'id.require' => '缺少必要参数',
'org_id.require' => '请选择组织',
'dept_id.require' => '请选择部门',
'supplier_id.require' => '请选择供应商',
'project_id.require' => '请选择项目',
'contract_name.require' => '请填写合同名称',
'contract_type.require' => '请选择合同类型',
'signing_date.require' => '请选择签约日期',
'signing_date.dateFormat' => '签约日期数据格式错误',
'pay_type.require' => '请选择付款方式',
'account_period.require' => '请选择账期',
'retention_money_rate.require' => '请填写质保金比例',
'retention_money_rate.float' => '质保金比例值必须是数字',
'retention_money_rate.egt' => '质保金比例值必须大于等于0',
'procurement_contract_detail.require' => '采购合同明细不能为空',
'payment_plan.require' => '付款计划不能为空'
];
@ -68,9 +97,7 @@ class ProcurementContractValidate extends BaseValidate
* @date 2023/12/04 22:08
*/
public function sceneEdit()
{
return $this->only(['id', 'supplier_id', 'approve_id', 'project_id', 'material', 'pay_plan']);
}
{}
/**
@ -95,44 +122,151 @@ class ProcurementContractValidate extends BaseValidate
{
return $this->only(['id']);
}
public function checkOrg($value): bool|string
{
$org = Orgs::where('id',$value)->findOrEmpty();
if($org->isEmpty()){
return '组织不存在';
}
return true;
}
public function checkDept($value,$rule,$data): bool|string
{
$dept = Dept::where('id',$value)->findOrEmpty();
if($dept->isEmpty()){
return '部门不存在';
}
if($dept['org_id'] != $data['org_id']){
return '部门无效';
}
return true;
}
public function checkSupplier($value): bool|string
{
$supplier = Supplier::where('id',$value)->findOrEmpty();
if($supplier->isEmpty()){
return '供应商信息不存在';
}
return true;
}
public function checkProject($value): bool|string
{
$project = Project::where('id',$value)->findOrEmpty();
if($project->isEmpty()){
return '项目信息不存在';
}
return true;
}
public function checkContractType($value): bool|string
{
$dict = DictData::where('type_value','procurement_contract_type')->column('value');
if(!in_array($value,$dict)){
return '合同类型无效';
}
return true;
}
public function checkPayType($value): bool|string
{
$dict = DictData::where('type_value','pay_type')->column('value');
if(!in_array($value,$dict)){
return '付款方式无效';
}
return true;
}
public function checkAccountPeriod($value): bool|string
{
$dict = DictData::where('type_value','account_period')->column('value');
if(!in_array($value,$dict)){
return '账期无效';
}
return true;
}
public function checkAnnex($value): bool|string
{
if(!empty($value) && $value != ''){
$annex = json_decode($value,true);
if(empty($annex) || !is_array($annex)){
return '附件格式错误';
}
}
return true;
}
public function checkMaterial($value, $rule, $data)
{
$firstData = $value[0] ?? [];
if (empty($firstData['material_procurement_application_id'])) {
return '材料采购申请id不能为空';
}
if (empty($firstData['num'])) {
return '材料数量不能为空!';
}
if (empty($firstData['unit_price_including_tax'])) {
return '材料含税单价不能为空!';
}
if (empty($firstData['tax_rate'])) {
return '材料税率不能为空!';
}
if (empty($firstData['amount_excluding_tax'])) {
return '材料不含税金额不能为空!';
}
if (empty($firstData['amount_including_tax'])) {
return '材料含税金额不能为空!';
}
return true;
}
public function checkPlan($value, $rule, $data)
{
$firstData = $value[0] ?? [];
if (empty($firstData['period'])) {
return '付款期次不能为空!';
}
if (empty($firstData['pay_date'])) {
return '付款日期不能为空!';
}
if (empty($firstData['pay_amount'])) {
return '付款金额不能为空!';
}
return true;
}
public function checkProcurementContractDetail($value): bool|string
{
$detail = json_decode($value,true);
if(empty($detail) || !is_array($detail)){
return '采购合同明细数据格式错误';
}
foreach($detail as $v) {
if(empty($v['material_purchase_request_detail_id'])){
return '请选择采购申请明细信息';
}else{
$material_purchase_request_detail = MaterialPurchaseRequestDetail::where('id',$v['material_purchase_request_detail_id'])->findOrEmpty();
if($material_purchase_request_detail->isEmpty()){
return '采购申请明细信息不存在';
}
}
if(empty($v['num'])){
return '数量不能为空';
}else{
if(!is_numeric($v['num']) || $v['num'] <= 0){
return '数量必须是大于0的数字';
}
}
if(empty($v['price'])){
return '含税单价不能为空';
}else{
if(!is_numeric($v['price']) || $v['price'] <= 0){
return '含税单价必须是大于0的数字';
}
}
if(empty($v['tax_rate'])){
return '请选择税率';
}else{
$dict = DictData::where('type_value','tax_rate')->column('value');
if(!in_array($v['tax_rate'],$dict)){
return '税率无效';
}
}
}
return true;
}
public function checkPaymentPlan($value): bool|string
{
$detail = json_decode($value,true);
if(empty($detail) || !is_array($detail)){
return '付款计划据格式错误';
}
foreach($detail as $v) {
if(empty($v['period'])){
return '请选择期次';
}else{
$dict = DictData::where('type_value','pay_period')->column('value');
if(!in_array($v['period'],$dict)){
return '期次无效';
}
}
if(empty($v['pay_date'])){
return '请选择计划付款日期';
}
if(empty($v['amount'])){
return '金额不能为空';
}else{
if(!is_numeric($v['amount']) || $v['amount'] <= 0){
return '金额必须是大于0的数字';
}
}
}
return true;
}
}

View File

@ -16,6 +16,7 @@ namespace app\common\model\contract;
use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use think\model\concern\SoftDelete;
@ -29,35 +30,29 @@ class ProcurementContract extends BaseModel
use SoftDelete;
protected $name = 'procurement_contract';
protected $deleteTime = 'delete_time';
public function project()
{
return $this->belongsTo(\app\common\model\project\Project::class, 'project_id');
}
public function material()
{
return $this->hasMany(\app\common\model\contract\ProcurementContractDetail::class, 'procurement_contract_id', 'id');
}
public function work()
{
return $this->hasMany(\app\common\model\contract\SubcontractingContractDetail::class, 'subcontracting_contract_id', 'id');
}
public function org()
{
return $this->hasOne(\app\common\model\dept\Orgs::class, 'id', 'org_id');
}
public function dept()
{
return $this->hasOne(\app\common\model\dept\Dept::class, 'id', 'dept_id');
}
public function getSigningDateAttr($value)
public function getSigningDateAttr($value): string
{
return empty($value) ? '' : date('Y-m-d H:i:s', $value);
}
public function getAnnexAttr($value){
return !empty($value) ? json_decode($value) : '';
}
public function getContractTypeTextAttr($value,$data){
$dict = DictData::where('type_value','procurement_contract_type')->column('name','value');
return $dict[$data['contract_type']];
}
public function getPayTypeTextAttr($value,$data){
$dict = DictData::where('type_value','pay_type')->column('name','value');
return $dict[$data['pay_type']];
}
public function getAccountPeriodTextAttr($value,$data){
$dict = DictData::where('type_value','account_period')->column('name','value');
return $dict[$data['account_period']];
}
}