From e4cd267beb1ae70e2219471b7290d16074c21050 Mon Sep 17 00:00:00 2001 From: mmrbnjd Date: Sat, 25 May 2024 17:52:40 +0330 Subject: [PATCH] ... --- Back/Back.csproj | 2 +- Back/Controllers/InvoiceItemController.cs | 105 ++++++++++++++++++++ Back/Program.cs | 2 + Back/Services/ServCOD.cs | 2 +- Back/Services/servInvoiceItem.cs | 32 ++++++ Back/Services/servTaxPayer.cs | 2 +- Back/Validations/AUInvoiceItemValidation.cs | 78 +++++++++++++++ Back/Validations/AddOrCodValidation.cs | 2 +- TaxPayerFull/Program.cs | 4 +- 9 files changed, 223 insertions(+), 6 deletions(-) create mode 100644 Back/Controllers/InvoiceItemController.cs create mode 100644 Back/Services/servInvoiceItem.cs create mode 100644 Back/Validations/AUInvoiceItemValidation.cs diff --git a/Back/Back.csproj b/Back/Back.csproj index 52705f9..a6bcdd4 100644 --- a/Back/Back.csproj +++ b/Back/Back.csproj @@ -35,7 +35,7 @@ - ..\..\Dlls\Service.dll + ..\..\..\LocalGit\TaxPayerTools\Service\bin\Debug\Service.dll diff --git a/Back/Controllers/InvoiceItemController.cs b/Back/Controllers/InvoiceItemController.cs new file mode 100644 index 0000000..cc78882 --- /dev/null +++ b/Back/Controllers/InvoiceItemController.cs @@ -0,0 +1,105 @@ +using Back.Common; +using Back.Data.Models; +using Back.Services; +using Back.Validations; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Shared.DTOs; +using System.Net; + +namespace Back.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class InvoiceItemController : ControllerBase + { + private readonly servInvoiceItem _servInvoiceItem; + private readonly servUser _servUser; + private readonly AUInvoiceItemValidation _validationInvoiceItem; + private readonly servInvoice _servInvoice; + public InvoiceItemController(servInvoiceItem servInvoiceItem, AUInvoiceItemValidation validationInvoiceItem + , servUser servUser, servInvoice servInvoice) + { + _servInvoiceItem = servInvoiceItem; + _validationInvoiceItem = validationInvoiceItem; + _servUser = servUser; + _servInvoice = servInvoice; + + } + [HttpPost("AddItem/{invoiceID}")] + public async Task AddItem([FromRoute] int invoiceID, [FromBody] InvoiceItemDTO model) + { + //-----GetUserAndCompany + var claim = HttpContext.User.Claims.First(c => c.Type == "UserID"); + var UserID = claim.Value; + var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID)); + + //-----Validaton + var resultValidationmodel = await _validationInvoiceItem.ValidateAsync(Tuple.Create(user.RolUsers.First().CompanyID, invoiceID, model, eActionValidation.add)); + if (!resultValidationmodel.IsValid) + return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList()); + + //-----Get invoice + Invoice invoice = await _servInvoice.GetInvoiceByInvoiceID(user.RolUsers.First().CompanyID, invoiceID); + if (invoice == null) + return BadRequest(new List { "invoice notFound..." }); + + + invoice.LastChangeUserID = Convert.ToInt32(UserID); + + if (await _servInvoice.UpdateInvoice(invoice)) + { + return Ok(await _servInvoiceItem.Add(new InvoiceItem + { + am=model.am, + fee=model.fee, + dis=model.dis, + CODID=model.CODID, + InvoiceID=invoiceID, + + })); + } + else return BadRequest(new List { "خطایی رخ داده" }); + + + } + [HttpPost("UpdateItem/{invoiceID}")] + public async Task UpdateItem([FromRoute] int invoiceID, [FromBody] InvoiceItemDTO model) + { + //کالا نمی تواند + //-----GetUserAndCompany + var claim = HttpContext.User.Claims.First(c => c.Type == "UserID"); + var UserID = claim.Value; + var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID)); + + //-----Validaton + var resultValidationmodel = await _validationInvoiceItem.ValidateAsync(Tuple.Create(user.RolUsers.First().CompanyID, invoiceID, model, eActionValidation.update)); + if (!resultValidationmodel.IsValid) + return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList()); + + //-----Get invoice + Invoice invoice = await _servInvoice.GetInvoiceByInvoiceID(user.RolUsers.First().CompanyID, invoiceID); + if (invoice == null) + return BadRequest(new List { "invoice notFound..." }); + + + var invoiceitem=await _servInvoiceItem.Getinvoiceitem(user.RolUsers.First().CompanyID, invoiceID, model.ID.Value); + if (invoiceitem == null) + return BadRequest(new List { "invoice Item notFound..." }); + + invoice.LastChangeUserID = Convert.ToInt32(UserID); + + invoiceitem.am = model.am; + invoiceitem.fee = model.fee; + invoiceitem.dis = model.dis; + // invoiceitem.CODID = model.CODID; + + if (await _servInvoice.UpdateInvoice(invoice)) + return Ok(await _servInvoiceItem.Update(invoiceitem)); + + else return BadRequest(new List { "خطایی رخ داده" }); + + + } + } +} diff --git a/Back/Program.cs b/Back/Program.cs index b4c21fe..f8be6c3 100644 --- a/Back/Program.cs +++ b/Back/Program.cs @@ -44,6 +44,8 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(c => new mpNuget.RestClient("09119660045", "C54S2")); string origins = "OriginTaxPayer"; diff --git a/Back/Services/ServCOD.cs b/Back/Services/ServCOD.cs index b98f887..8ea2d2b 100644 --- a/Back/Services/ServCOD.cs +++ b/Back/Services/ServCOD.cs @@ -80,7 +80,7 @@ namespace Back.Services return await _CODRepo .Get(w => w.ID == CodID && w.CompanyID == CompanyID && !w.IsDeleted).FirstOrDefaultAsync(); } - public async Task ExistCodByCustomerID(int CodID, int CompanyID) + public async Task ExistCodByCompanyID(int CodID, int CompanyID) { return await _CODRepo .Get(w => w.ID == CodID && w.CompanyID == CompanyID && !w.IsDeleted).AnyAsync(); diff --git a/Back/Services/servInvoiceItem.cs b/Back/Services/servInvoiceItem.cs new file mode 100644 index 0000000..782cf08 --- /dev/null +++ b/Back/Services/servInvoiceItem.cs @@ -0,0 +1,32 @@ +using Back.Data.Contracts; +using Back.Data.Models; +using Microsoft.EntityFrameworkCore; + +namespace Back.Services +{ + public class servInvoiceItem + { + private readonly IAsyncRepository _invoiceitemRepo; + public servInvoiceItem(IAsyncRepository invoiceitemRepo) + { + _invoiceitemRepo = invoiceitemRepo; + } + public async Task Add(InvoiceItem item) + { + return await _invoiceitemRepo.AddBoolResultAsync(item); + } + public async Task Update(InvoiceItem item) + { + return await _invoiceitemRepo.UpdateAsync(item); + } + public async Task Exist(int companyID,int invoiceID,int invoiceitemID) + { + return await _invoiceitemRepo.Get(w => w.InvoiceID == invoiceID && w.ID == invoiceitemID && w.invoice.CompanyID == companyID).AnyAsync(); + } + + public async Task Getinvoiceitem(int companyID, int invoiceID, int invoiceitemID) + { + return await _invoiceitemRepo.Get(w => w.InvoiceID == invoiceID && w.ID == invoiceitemID && w.invoice.CompanyID == companyID).FirstOrDefaultAsync(); + } + } +} diff --git a/Back/Services/servTaxPayer.cs b/Back/Services/servTaxPayer.cs index 28f63c8..c362a7b 100644 --- a/Back/Services/servTaxPayer.cs +++ b/Back/Services/servTaxPayer.cs @@ -20,7 +20,7 @@ namespace Back.Services } public async Task ExistSuccessfulorSendorpendingInvoice(Invoice invoice) { - return _repoSentTax.Get(w => w.InvoiceType == invoice.invoiceType && w.InvoiceID == invoice.ID && + return _repoSentTax.Get(w => /*w.InvoiceType == invoice.invoiceType &&*/ w.InvoiceID == invoice.ID && (w.SentStatus == SentStatus.Successful || w.SentStatus == SentStatus.Send || w.SentStatus == SentStatus.pending)).Any(); } public async Task CheckingTheCompanyKeyInformation(int CompanyID, string UniqeMemory, string PrivateKey, string EconomicCode) diff --git a/Back/Validations/AUInvoiceItemValidation.cs b/Back/Validations/AUInvoiceItemValidation.cs new file mode 100644 index 0000000..3cf2ae8 --- /dev/null +++ b/Back/Validations/AUInvoiceItemValidation.cs @@ -0,0 +1,78 @@ +using Back.Common; +using Back.Services; +using FluentValidation; +using Shared.DTOs; + +namespace Back.Validations +{ + // com ,invoice,model,action + public class AUInvoiceItemValidation : AbstractValidator> + { + public AUInvoiceItemValidation(servInvoice servInvoice,ServCOD servCOD, servTaxPayer servTaxPayer, servInvoiceItem servInvoiceItem) + { + When(m => m.Item4 == eActionValidation.update, () => + { + RuleFor(r => r) + .Custom((model, context) => + { + if (!model.Item3.ID.HasValue) + context.AddFailure("شناسه در حالت ویرایش نمی تواند خالی باشد"); + if (!servInvoiceItem.Exist(model.Item1,model.Item2,model.Item3.ID.Value).Result) + context.AddFailure("شناسه یافت نشد"); + + }); + + }); + + + RuleFor(r => r) + .Custom( (model, context) => + { + if (model.Item3.CODID != null && model.Item3.CODID > 0) + { + if (! servCOD.ExistCodByCompanyID(model.Item3.CODID, model.Item1).Result) + context.AddFailure("کالا یافت نشد"); + } + + else context.AddFailure("کالا صحیح نمی باشد"); + + }); + RuleFor(r => r) + .Custom((model, context) => + { + if (model.Item2!=null && model.Item2 > 0) + { + var invoice = servInvoice.GetInvoiceByInvoiceID(model.Item1,model.Item2).Result; + if (invoice==null) + context.AddFailure("صورتحساب یافت نشد"); + else + { + if ( servTaxPayer.ExistSuccessfulorSendorpendingInvoice(invoice).Result) + context.AddFailure("این صورتحساب به سازمان ارسال شده"+'\n'+ + "برای تغییر ،صورتحساب را ابطال/اصلاح یا برگشت بزنید"); + } + } + + else context.AddFailure("صورتحساب صحیح نمی باشد"); + + }); + + RuleFor(r => r.Item3.am) + .NotEmpty().WithMessage("تعداد مشخص نشده") + .NotNull().WithMessage("تعداد مشخص نشده"); + + RuleFor(r => r.Item3.fee) + .NotEmpty().WithMessage("مبلغ واحد مشخص نشده") + .NotNull().WithMessage("مبلغ واحد مشخص نشده"); + + RuleFor(r => r).Custom(async (model, context) => + { + if (model.Item3.dis!=null && model.Item3.dis > 0) + if (model.Item3.dis > model.Item3.am * model.Item3.fee) + context.AddFailure("مبلغ تخفیف نمی تواند از (تعداد * مبلغ واحد) بیشتر باشد"); + + + }); + } + } +} diff --git a/Back/Validations/AddOrCodValidation.cs b/Back/Validations/AddOrCodValidation.cs index 066aca1..574708e 100644 --- a/Back/Validations/AddOrCodValidation.cs +++ b/Back/Validations/AddOrCodValidation.cs @@ -32,7 +32,7 @@ namespace Back.Validations else { var customerid = model.Item2.ID.Value; - if (!servCod.ExistCodByCustomerID(customerid, companyid).Result) + if (!servCod.ExistCodByCompanyID(customerid, companyid).Result) context.AddFailure("کالا با این شناسه یافت نشد"); } }); diff --git a/TaxPayerFull/Program.cs b/TaxPayerFull/Program.cs index 628e6c3..f8805c2 100644 --- a/TaxPayerFull/Program.cs +++ b/TaxPayerFull/Program.cs @@ -34,9 +34,9 @@ builder.Services.AddScoped(sp => new UserAuthenticationDTO() }) ; -builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("https://localhost:7075/api/") }); +//builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("https://localhost:7075/api/") }); -//builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("http://localhost:5271/api/") }); +builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("http://localhost:5271/api/") }); CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("fa-Ir");