using cylsg.Model.ECTEModel;
|
using cylsg.Model.utilityViewModel;
|
using ECTESTOA;
|
using Furion.LinqBuilder;
|
using Models;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Linq.Expressions;
|
using System.Text;
|
using System.Threading.Tasks;
|
using static SKIT.FlurlHttpClient.Wechat.Api.Models.UnionShopTargetAddPlanRequest.Types;
|
|
namespace cylsg.Application.CyOS
|
{
|
/// <summary>
|
/// 财务报销控制器
|
/// </summary>
|
[Authorize]
|
public class CyOSBaoXiaoCaiWuController: IDynamicApiController
|
{
|
private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1);
|
private readonly IOAServices _OAServices;
|
private readonly ISqlSugarClient _SugarClient;
|
private ECTESTOAPermissions Permissions;
|
public CyOSBaoXiaoCaiWuController(IOAServices oaservices, ISqlSugarClient sugarClient)
|
{
|
_OAServices = oaservices;
|
_SugarClient = sugarClient.AsTenant().GetConnection("ECTESTOADB");
|
}
|
|
|
/// <summary>
|
/// 查询
|
/// </summary>
|
[HttpPost]
|
public async Task<WorekPlandto> getplanelist(SearchOaWorekPlan param)
|
{
|
if (!await CheckRols())
|
{
|
throw Oops.Oh("没有权限");
|
}
|
|
Expression<Func<OaWorkPlan, bool>> SearchList = (x) => true;
|
SearchList = SearchList.And(x => x.ApprovalStatus != PlanStatusType.Uncommitted);
|
//川印工作单
|
SearchList = SearchList.And(x => x.MemberId == _OAServices.firmId);
|
SearchList = SearchList.And(x => x.PlanType == 3);
|
SearchList = SearchList.And(x => x.PlanDataType == 17);
|
//必须是审批完成后的单
|
SearchList = SearchList.And(x=>x.ApprovalStatus== PlanStatusType.Yes);
|
if (!string.IsNullOrEmpty(param.StaffName))
|
{
|
SearchList = SearchList.And(x => x.SentStaff.Name.Contains(param.StaffName));
|
}
|
if (param.StaffMemberIdId != null)
|
{
|
SearchList = SearchList.And(x => x.SentStaffId == param.StaffMemberIdId);
|
}
|
if (param.PlanDataType > 0)
|
{
|
SearchList = SearchList.And(x => x.PlanDataType == param.PlanDataType);
|
}
|
if (param.PlanStartTimeStart != null)
|
{
|
SearchList = SearchList.And(x => x.PlanStartTime > param.PlanStartTimeStart);
|
}
|
if (param.PlanStartTimeEnd != null)
|
{
|
SearchList = SearchList.And(x => x.PlanStartTime <= param.PlanStartTimeEnd.Value.AddDays(1));
|
}
|
////审批状态
|
//if (param.ApprovalStatus != null)
|
//{
|
|
// SearchList = SearchList.And(x => x.ApprovalStatus == param.ApprovalStatus);
|
|
|
//}
|
|
//报销状态
|
if (param.EvaluationStatus != null)
|
{
|
SearchList = SearchList.And(x => x.EvaluationStatus == param.EvaluationStatus);
|
}
|
//强制增加员工过滤
|
RefAsync<int> totle = 0;
|
var data = await _SugarClient.Queryable<OaWorkPlan>().Includes(x => x.OaWorkPlanShenpis).Includes(x=>x.SentStaff)
|
.Where(SearchList).OrderByDescending(x => x.MemberId).ToPageListAsync(param.page.PageIndex, param.page.PageSize, totle);
|
|
param.page.TotalCount = totle;
|
var sum = await _SugarClient.Queryable<OaWorkPlan>()
|
.Where(SearchList).SumAsync(x => x.PlanMoney);
|
return new WorekPlandto
|
|
{
|
list = data,
|
page = param.page,
|
TotalMoney = sum ?? 0
|
};
|
|
|
}
|
|
/// <summary>
|
/// 获取报销单详情
|
/// </summary>
|
/// <param name="id"></param>
|
/// <returns></returns>
|
[HttpGet]
|
public async Task<OaWorkPlan> GetPlanInfo(int id)
|
{
|
if (!await CheckRols())
|
{
|
throw Oops.Oh("没有权限");
|
}
|
return await _SugarClient.Queryable<OaWorkPlan>().Includes(x => x.OaWorkPlanShenpis,y=>y.ApprovalStaff).Includes(x => x.SentStaff).Includes(x => x.Depart).Includes(x=>x.WorkPlanAttachments).Where(x => x.Keyid == id).FirstAsync();
|
}
|
|
/// <summary>
|
/// 报销
|
/// </summary>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<bool> ApprovalPlan(OaWorkPlan plan)
|
{
|
|
|
// 等待信号量
|
await _semaphore.WaitAsync();
|
try
|
{
|
if (plan.Keyid == 0)
|
throw Oops.Oh("ID错误");
|
if (plan.FirmAccountId == null)
|
throw Oops.Oh("必须选择银行账号");
|
|
if (!await CheckRols())
|
{
|
throw Oops.Oh("没有权限");
|
}
|
var FirmAccount = await _SugarClient.Queryable<OA_FirmAccount>().Where(x => x.FirmId == _OAServices.firmId && x.Keyid == plan.FirmAccountId && x.Status == true).FirstAsync();
|
if (FirmAccount == null)
|
throw Oops.Oh("账号错误");
|
var data = await _SugarClient.Queryable<OaWorkPlan>().Includes(x => x.SentStaff).Where(x => x.Keyid == plan.Keyid).FirstAsync();
|
if (data == null)
|
throw Oops.Oh("没有找到报销单");
|
|
if (data.ApprovalStatus != PlanStatusType.Yes)
|
throw Oops.Oh("未审批完成,不可报销");
|
|
|
|
|
if (data.EvaluationStatus != BaoxiaoStausType.Reimbursed)
|
{
|
await _SugarClient.AsTenant().BeginTranAsync();
|
try
|
{
|
data.EvaluationStatus = BaoxiaoStausType.Reimbursed;
|
//data.PlanContent = plan.PlanContent;
|
data.LastUpdateTime = DateTime.Now;
|
await _SugarClient.Updateable(data).ExecuteCommandAsync();
|
|
FirmAccount.LastUpdateTime = DateTime.Now;
|
FirmAccount.Balance -= (Math.Round(data.PlanMoney ?? 0, 2));
|
FirmAccount.AllExpenses += (Math.Round(data.PlanMoney ?? 0, 2));
|
|
await _SugarClient.Updateable(FirmAccount).ExecuteCommandAsync();
|
OA_FirmAccountRecord rec = new OA_FirmAccountRecord
|
{
|
AccountId = FirmAccount.Keyid,
|
Money = data.PlanMoney ?? 0,
|
LastUpdateTime = DateTime.Now,
|
Department = data.PlanContent,
|
RecordTypeId = 2,
|
SubjectId = 3,//报销必须为3
|
Operator = Permissions.Name,
|
PaymentUnit = data.SentStaff.Name,
|
ResidualAmount = FirmAccount.Balance,
|
OperationalMatters = data.PlanContent,
|
|
|
};
|
await _SugarClient.Insertable(rec).ExecuteCommandAsync();
|
await _SugarClient.Updateable(data).ExecuteCommandAsync();
|
await _SugarClient.AsTenant().CommitTranAsync();
|
return true;
|
}
|
catch (Exception)
|
{
|
await _SugarClient.AsTenant().RollbackTranAsync();
|
throw;
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
}
|
catch(Exception e)
|
{
|
throw;
|
}
|
finally
|
{
|
// 释放信号量
|
_semaphore.Release();
|
}
|
|
|
|
|
}
|
/// <summary>
|
/// 获取账户列表
|
/// </summary>
|
/// <returns></returns>
|
[HttpGet]
|
public async Task<List<OA_FirmAccount>> GetFirmAccountList()
|
{
|
return await _SugarClient.Queryable<OA_FirmAccount>().Where(x => x.Status == true && x.FirmId == _OAServices.firmId).Select(x => new OA_FirmAccount
|
{
|
AccountName = x.AccountName,
|
AccountType = x.AccountType,
|
UserName = x.UserName,
|
Keyid=x.Keyid,
|
|
|
}).WithCache(3600).ToListAsync();
|
}
|
|
/// <summary>
|
/// 判断是否具有权限
|
/// </summary>
|
/// <returns></returns>
|
private async Task<bool> CheckRols()
|
{
|
var pr = await _OAServices.GetOAPermissions();
|
Permissions = pr;
|
if (pr != null && pr.MemberId != null)
|
{
|
|
if (pr.BF_IsCaiWu)
|
return true;
|
else return false;
|
|
}
|
else
|
return false;
|
}
|
}
|
}
|