Files
Backend-Api/ServiceHost/Areas/Admin/Controllers/institutionContractController.cs
MahanCh c1fcaa1726 add transaction controller and create api
edit institutionContractController.cs and add excel download
2025-08-06 11:37:06 +03:30

362 lines
14 KiB
C#
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.
using _0_Framework.Application;
using AccountManagement.Application.Contracts.Account;
using CompanyManagement.Infrastructure.Excel.InstitutionContract;
using CompanyManagment.App.Contracts.InstitutionContract;
using CompanyManagment.App.Contracts.InstitutionContractContactinfo;
using CompanyManagment.App.Contracts.PersonalContractingParty;
using Microsoft.AspNetCore.Mvc;
using ServiceHost.BaseControllers;
namespace ServiceHost.Areas.Admin.Controllers;
/// <summary>
/// کنترلر قرارداد های مالی موسسه
/// </summary>
public class institutionContractController : AdminBaseController
{
private readonly IInstitutionContractApplication _institutionContractApplication;
private readonly IPersonalContractingPartyApp _contractingPartyApplication;
private readonly IContactInfoApplication _contactInfoApplication;
private readonly IAccountApplication _accountApplication;
public institutionContractController(IInstitutionContractApplication institutionContractApplication,
IPersonalContractingPartyApp contractingPartyApplication, IContactInfoApplication contactInfoApplication,
IAccountApplication accountApplication)
{
_institutionContractApplication = institutionContractApplication;
_contractingPartyApplication = contractingPartyApplication;
_contactInfoApplication = contactInfoApplication;
_accountApplication = accountApplication;
}
/// <summary>
/// لیست قرارداد های مالی
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<ActionResult<PagedResult<GetInstitutionContractListItemsViewModel>>> GetList(
InstitutionContractListSearchModel searchModel)
{
return await _institutionContractApplication.GetList(searchModel);
}
/// <summary>
/// وضعیت تب ها
/// </summary>
/// <param name="searchModel"></param>
/// <returns></returns>
[HttpGet("stats")]
public async Task<ActionResult<GetInstitutionContractListStatsViewModel>> GetListStats(
InstitutionContractListSearchModel searchModel)
{
return await _institutionContractApplication.GetListStats(searchModel);
}
/// <summary>
/// ایجاد
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
[HttpPost]
public async Task<ActionResult<OperationResult>> Create([FromBody] CreateInstitutionContractRequest command)
{
var op = new OperationResult();
var counter = command.ContactInfos.Count;
//if (string.IsNullOrWhiteSpace(command.HasValueAddedTax))
// command.HasValueAddedTax = "false";
var phone = command.ContactInfos.FirstOrDefault(x =>
x.SendSmsString == "true" && x.Position == "طرف قرارداد" && x.PhoneType == "شماره همراه");
var conractingParty = _contractingPartyApplication.GetDetails(command.ContractingPartyId);
if (conractingParty.IsLegal == "حقیقی" && string.IsNullOrWhiteSpace(conractingParty.Nationalcode))
return new JsonResult(op.Failed("کد ملی طرف حساب وجود ندارد"));
if (conractingParty.IsLegal == "حقوقی" && string.IsNullOrWhiteSpace(conractingParty.NationalId))
return new JsonResult(op.Failed("شناسه ملی طرف حساب وجود ندارد"));
var result = await _institutionContractApplication.CreateAsync(command);
if (result.IsSuccedded && counter > 0)
{
for (var i = 0; i <= counter - 1; i++)
{
if (command.ContactInfos[i].PhoneNumber != null)
{
var contactinfo = new CreateContactInfo
{
InstitutionContractId = result.SendId,
PhoneType = command.ContactInfos[i].PhoneType,
Position = command.ContactInfos[i].Position,
PhoneNumber = command.ContactInfos[i].PhoneNumber,
FnameLname = command.ContactInfos[i].FnameLname,
SendSms = command.ContactInfos[i].SendSmsString == "true" ? true : false
};
_contactInfoApplication.Create(contactinfo);
}
Thread.Sleep(500);
}
if (phone != null)
{
var userPass = conractingParty.IsLegal == "حقیقی"
? conractingParty.Nationalcode
: conractingParty.NationalId;
var createAcc = new RegisterAccount
{
Fullname = conractingParty.LName,
Username = userPass,
Password = userPass,
Mobile = phone.PhoneNumber,
NationalCode = userPass
};
var res = _accountApplication.RegisterClient(createAcc);
if (res.IsSuccedded)
_institutionContractApplication.CreateContractingPartyAccount(command.ContractingPartyId,
res.SendId);
}
}
return new JsonResult(result);
}
/// <summary>
/// ویرایش
/// </summary>
/// <returns></returns>
[HttpPut]
public async Task<ActionResult<OperationResult>> Edit([FromBody] EditInstitutionContractRequest command)
{
var op = new OperationResult();
var phoneNumber = command.ContactInfos.FirstOrDefault(x =>
x.SendSmsString == "true" && x.Position == "طرف قرارداد" && x.PhoneType == "شماره همراه");
var conractingParty = _contractingPartyApplication.GetDetails(command.ContractingPartyId);
if (conractingParty.IsLegal == "حقیقی" && string.IsNullOrWhiteSpace(conractingParty.Nationalcode))
return new JsonResult(op.Failed("کد ملی طرف حساب وجود ندارد"));
if (conractingParty.IsLegal == "حقوقی" && string.IsNullOrWhiteSpace(conractingParty.NationalId))
return new JsonResult(op.Failed("شناسه ملی طرف حساب وجود ندارد"));
if (phoneNumber == null)
return new JsonResult(op.Failed("تعیین شماره همراه با سمت طرف قرارداد اجباریست"));
//if (string.IsNullOrWhiteSpace(command.HasValueAddedTax))
// command.HasValueAddedTax = "false";
var result = await _institutionContractApplication.EditAsync(command);
var contractingPartyId = _institutionContractApplication.GetDetails(result.SendId);
var counter = command.ContactInfos.Count;
var getOldContarct = _institutionContractApplication.NewSearch(new InstitutionContractSearchModel()
{ ContractingPartyId = contractingPartyId.ContractingPartyId, IsActiveString = "both" })
.Where(x => x.IsActiveString == "false" || x.IsActiveString == "blue").ToList();
if (result.IsSuccedded && counter > 0)
{
if (getOldContarct.Count > 0)
{
foreach (var item in getOldContarct)
{
_contactInfoApplication.RemoveContactInfo(item.Id);
foreach (var phone in command.ContactInfos)
{
if (phone.PhoneNumber != null)
{
var contactinfo = new CreateContactInfo
{
InstitutionContractId = item.Id,
PhoneType = phone.PhoneType,
Position = phone.Position,
PhoneNumber = phone.PhoneNumber,
FnameLname = phone.FnameLname,
SendSms = phone.SendSmsString == "true" ? true : false
};
_contactInfoApplication.Create(contactinfo);
}
Thread.Sleep(500);
}
}
}
_contactInfoApplication.RemoveContactInfo(command.Id);
foreach (var item in command.ContactInfos)
{
if (item.PhoneNumber != null)
{
var contactinfo = new CreateContactInfo
{
InstitutionContractId = result.SendId,
PhoneType = item.PhoneType,
Position = item.Position,
PhoneNumber = item.PhoneNumber,
FnameLname = item.FnameLname,
SendSms = item.SendSmsString == "true" ? true : false
};
_contactInfoApplication.Create(contactinfo);
}
Thread.Sleep(500);
}
//ساخت اکانت کلاینت
var userPass = conractingParty.IsLegal == "حقیقی"
? conractingParty.Nationalcode
: conractingParty.NationalId;
var checkExistAccount = _accountApplication.CheckExistClientAccount(userPass);
if (!checkExistAccount)
{
var createAcc = new RegisterAccount
{
Fullname = conractingParty.LName,
Username = userPass,
Password = userPass,
Mobile = phoneNumber.PhoneNumber,
NationalCode = userPass
};
var res = _accountApplication.RegisterClient(createAcc);
if (res.IsSuccedded)
_institutionContractApplication.CreateContractingPartyAccount(command.ContractingPartyId,
res.SendId);
}
}
//Thread.Sleep(500);
//for (int i = 0; i <= counter - 1; i++)
//{
// if (command.ContactInformationList[i].PhoneNumber != null)
// {
// var contactinfo = new CreateContactInfo()
// {
// InstitutionContractId = result.SendId,
// PhoneType = command.ContactInformationList[i].PhoneType,
// Position = command.ContactInformationList[i].Position,
// PhoneNumber = command.ContactInformationList[i].PhoneNumber,
// FnameLname = command.ContactInformationList[i].FnameLname,
// SendSms = command.ContactInformationList[i].SendSmsString == "true" ? true : false
// };
// _contactInfoApplication.Create(contactinfo);
// }
// Thread.Sleep(500);
//}
return new JsonResult(result);
}
[HttpPost("deActive")]
public ActionResult<OperationResult> DeActive(long id, string balance)
{
var result = new OperationResult();
if (balance == "0")
{
result = _institutionContractApplication.DeActive(id);
if (result.IsSuccedded) result = _institutionContractApplication.DeActiveAllConnections(id);
}
else
{
result = _institutionContractApplication.DeActiveBlue(id);
if (result.IsSuccedded)
result = _institutionContractApplication.DeActiveAllConnections(id);
}
return result;
}
/// <summary>
/// غیر فعال
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost("active")]
public ActionResult<OperationResult> IsActive(long id)
{
var result = _institutionContractApplication.Active(id);
if (result.IsSuccedded)
{
result = _institutionContractApplication.ReActiveAllConnections(id);
}
return result;
}
[HttpPost("sign")]
public ActionResult<OperationResult> Sign(long Id)
{
var result = _institutionContractApplication.Sign(Id);
if (result.IsSuccedded)
return new JsonResult(new
{
isSuccedded = true
});
return new JsonResult(new
{
isSuccedded = false
});
}
[HttpPost("unsign")]
public ActionResult<OperationResult> UnSign(long Id)
{
var id = Convert.ToInt64(Id);
var result = _institutionContractApplication.UnSign(Id);
if (result.IsSuccedded)
return new JsonResult(new
{
isSuccedded = true
});
return new JsonResult(new
{
isSuccedded = false
});
}
/// <summary>
/// بلاک طرف حساب
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost("block-contracting-party")]
public IActionResult OnPostBlockContractingParty(long id)
{
var result = _contractingPartyApplication.Block(id);
var afterActtion = _contractingPartyApplication.GetDetails(id);
return new JsonResult(new
{
isSuccedded = result.IsSuccedded,
isBlock = afterActtion.IsBlock,
blockTimes = afterActtion.BlockTimes
});
}
/// <summary>
/// آنبلاک طرف حساب
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost("unblock-contracting-party")]
public IActionResult OnPostUnBlockContractingParty(long id)
{
var result = _contractingPartyApplication.DisableBlock(id);
var afterActtion = _contractingPartyApplication.GetDetails(id);
return new JsonResult(new
{
isSuccedded = result.IsSuccedded,
isBlock = afterActtion.IsBlock,
blockTimes = afterActtion.BlockTimes
});
}
/// <summary>
/// دانلود اکسل
/// </summary>
/// <returns></returns>
[HttpGet("excel")]
public IActionResult OnGetDownloadExcel()
{
var institutionContractViewModels =
_institutionContractApplication.NewSearch(new() { IsActiveString = "both", TypeOfContract = "both" });
var bytes = InstitutionContractExcelGenerator.GenerateExcel(institutionContractViewModels);
return File(bytes,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
$"قرارداد های مالی.xlsx");
}
}