diff --git a/Back/Controllers/InvoicePaymentController.cs b/Back/Controllers/InvoicePaymentController.cs new file mode 100644 index 0000000..c62cc22 --- /dev/null +++ b/Back/Controllers/InvoicePaymentController.cs @@ -0,0 +1,169 @@ +using Back.Common; +using Back.Data.Models; +using Back.Services; +using Back.Validations; +using FluentValidation; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Shared.DTOs; +using System.Net; +using System.Runtime.ConstrainedExecution; +using System.Security.Cryptography; + +namespace Back.Controllers +{ + [Route("api/[controller]")] + [Authorize] + [ApiController] + public class InvoicePaymentController : ControllerBase + { + private readonly servInvoicePayment _servpay; + private readonly servUser _servUser; + private readonly AUInvoicePayValidation _validation; + private readonly servInvoice _servInvoice; + public InvoicePaymentController(servInvoicePayment servpay, AUInvoicePayValidation validation + , servUser servUser, servInvoice servInvoice) + { + _servpay = servpay; + _validation = validation; + _servUser = servUser; + _servInvoice = servInvoice; + + } + [HttpPost("AddPay")] + public async Task> AddItem([FromBody] InvoiceItemAction 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, model.invoiceID, model.item, 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, model.invoiceID); + if (invoice == null) + return BadRequest(new List { "invoice notFound..." }); + + + invoice.LastChangeUserID = Convert.ToInt32(UserID); + + if (await _servInvoice.UpdateInvoice(invoice)) + { + return Ok(await _servpay.Add(new Data.Models.InvoicePayment + { + InvoiceID = model.invoiceID, + acn = model.item.acn, + iinn = model.item.iinn, + PaymentDateTime = model.item.PaymentDateTime.Replace("/", ""), + pcn = model.item.pcn, + pid = model.item.pid, + pmt = model.item.pmt, + pv = model.item.pv, + trmn = model.item.trmn, + trn = model.item.trn, + + + })); + } + else return BadRequest(new List { "خطایی رخ داده" }); + + + } + [HttpPut("UpdatePay")] + public async Task> UpdateItem([FromBody] InvoiceItemAction model) + { + //-----GetUserAndCompany + var claim = HttpContext.User.Claims.First(c => c.Type == "UserID"); + var UserID = claim.Value; + var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID)); + + + //-----Get invoice + Invoice invoice = await _servInvoice.GetInvoiceByInvoiceID(user.RolUsers.First().CompanyID, model.invoiceID); + if (invoice == null) + return BadRequest(new List { "invoice notFound..." }); + + + + //-----Validaton + //var resultValidationmodel = await _validationInvoiceItem.ValidateAsync(Tuple.Create(user.RolUsers.First().CompanyID, model.invoiceID, model.item, eActionValidation.update)); + //if (!resultValidationmodel.IsValid) + //{ + // if (invoice.invoiceType != InvoiceType.BackFrmSale || (invoice.invoiceType != InvoiceType.BackFrmSale + // && resultValidationmodel.Errors.Count > 1)) + // { + // return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList()); + // } + + //} + + + + + + var item = await _servpay.GetInvoicePayByInvoicePayID(user.RolUsers.First().CompanyID, model.invoiceID, model.item.ID.Value); + if (item == null) + return BadRequest(new List { "invoice Pay notFound..." }); + + + invoice.LastChangeUserID = Convert.ToInt32(UserID); + + item.acn = model.item.acn; + item.iinn = model.item.iinn; + item.PaymentDateTime = model.item.PaymentDateTime.Replace("/", ""); + item.pcn = model.item.pcn; + item.pid = model.item.pid; + item.pmt = model.item.pmt; + item.pv = model.item.pv; + item.trmn = model.item.trmn; + item.trn = model.item.trn; + + if (await _servInvoice.UpdateInvoice(invoice)) + return Ok(await _servpay.Update(item)); + + else + return BadRequest(new List { "خطایی رخ داده" }); + + + } + [HttpDelete("DeletePay/{InvoicePayID}")] + public async Task> DeleteItem(int InvoicePayID) + { + + //-----GetUserAndCompany + var claim = HttpContext.User.Claims.First(c => c.Type == "UserID"); + var UserID = claim.Value; + var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID)); + + //-----Get invoicePay + var payitem = await _servpay.GetinvoicePay(user.RolUsers.First().CompanyID, InvoicePayID); + if (payitem == null) + return NotFound(new List { "invoice pay notFound..." }); + + ////-----Validaton + //var resultValidationmodel = await _validationInvoiceItem.ValidateAsync(Tuple.Create(user.RolUsers.First().CompanyID, invoiceitem.InvoiceID, new InvoiceItemDTO(), eActionValidation.delete)); + //if (!resultValidationmodel.IsValid) + // return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList()); + + + + payitem.invoice.LastChangeUserID = Convert.ToInt32(UserID); + + + + if (await _servInvoice.UpdateInvoice(payitem.invoice)) + return Ok(await _servpay.Delete(payitem)); + + else return BadRequest(new List { "خطایی رخ داده" }); + + + } + + + } +} diff --git a/Back/Controllers/TaxPayerController.cs b/Back/Controllers/TaxPayerController.cs index e5ca57a..2a489d3 100644 --- a/Back/Controllers/TaxPayerController.cs +++ b/Back/Controllers/TaxPayerController.cs @@ -236,7 +236,7 @@ namespace Back.Controllers //شماره کدتاژ اظهارنامه گمرکی Cdcn = level == 10 ? result.cdcn : null, //تاریخ کوتاژ اظهارنامه گمرکی - // Cdcd = level == 10 ? result.cdcd : null, + Cdcd = level == 10 ? result.cdcd : null, //شناسه یکتای ثبت قزارداد فروشنده Crn = (level == 3 || level == 4 || level == 5 || level == 6) && !string.IsNullOrEmpty(result.crn) ? result.crn : null, //شماره اشتراک/شناسه قبض بهره بردار @@ -358,26 +358,30 @@ namespace Back.Controllers //Pay List InvoicePay = new List(); - if(level!=10) - foreach (var pitem in result.payments) + if (result.setm==1 || result.setm == 3) { - PaymentDto payment = new PaymentDto(); - payment = new PaymentDto + if (level != 10) + foreach (var pitem in result.payments) { - Iinn=pitem.iinn, - Acn=pitem.acn, - Trmn=pitem.trmn, - Pmt=pitem.pmt, - Trn=pitem.trn, - Pcn=pitem.pcn, - Pid=pitem.pid, - Pdt=pitem.pdt, - Pv=pitem.pv, - - }; + PaymentDto payment = new PaymentDto(); + payment = new PaymentDto + { + Iinn = pitem.iinn, + Acn = pitem.acn, + Trmn = pitem.trmn, + Pmt = pitem.pmt, + Trn = pitem.trn, + Pcn = pitem.pcn, + Pid = pitem.pid, + Pdt = pitem.pdt, + Pv = pitem.pv, - InvoicePay.Add(payment); + }; + + InvoicePay.Add(payment); + } } + diff --git a/Back/Data/Models/Invoice.cs b/Back/Data/Models/Invoice.cs index ec25869..6da3514 100644 --- a/Back/Data/Models/Invoice.cs +++ b/Back/Data/Models/Invoice.cs @@ -71,7 +71,10 @@ namespace Back.Data.Models //اریخ کوتاژ اظهارنامه گمرکی // Unix Time => from fild CottageDateOfCustomsDeclaration [MaxLength(5)] - public long? cdcd { get { return string.IsNullOrEmpty(CottageDateOfCustomsDeclaration) ? null : new DateTimeOffset(CottageDateOfCustomsDeclaration.Trim().ToMiladi()).ToUnixTimeMilliseconds(); } } + public int? cdcd { get { + return string.IsNullOrEmpty(CottageDateOfCustomsDeclaration) ? null : (int)DateTime.UtcNow.Subtract(CottageDateOfCustomsDeclaration.Trim().ToMiladi()).TotalSeconds; + // return string.IsNullOrEmpty(CottageDateOfCustomsDeclaration) ? null : new DateTimeOffset(CottageDateOfCustomsDeclaration.Trim().ToMiladi()).ToUnixTimeMilliseconds(); + } } //کد پستی خریدار [MaxLength(10)] public string? bpc { get { return Customer.ZipCode; } } @@ -89,7 +92,10 @@ namespace Back.Data.Models public string? sbc { get { return company?.BranchID; } } //مبلغ پرداختی نقدی [MaxLength(18)] - public decimal? cap { get { return tbill- insp- tvam- todam; } } + public decimal? cap { get { + return + setm == 1 ? tbill : setm == 2 ? 0 : tbill - insp; + } } //موضوع صورتحساب [MaxLength(1)] public int? ins { get { return (int)invoiceType; } } diff --git a/Back/Program.cs b/Back/Program.cs index 79c2d39..682d7f7 100644 --- a/Back/Program.cs +++ b/Back/Program.cs @@ -67,6 +67,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/Back/Services/servInvoice.cs b/Back/Services/servInvoice.cs index 534b8ce..3628985 100644 --- a/Back/Services/servInvoice.cs +++ b/Back/Services/servInvoice.cs @@ -99,7 +99,6 @@ namespace Back.Services acn = x.acn, iinn = x.acn, pcn = x.acn, - pdt = x.pdt, PaymentDateTime = x.PaymentDateTime, pid = x.pid, pmt = x.pmt, @@ -233,6 +232,7 @@ namespace Back.Services .Include(inc => inc.sentTax) .Include(inc => inc.pattern) .Include(inc => inc.invoice) + .Include(inc=>inc.payments) .FirstOrDefaultAsync(); } public async Task DeleteInvoice(Invoice item) diff --git a/Back/Services/servInvoicePayment.cs b/Back/Services/servInvoicePayment.cs new file mode 100644 index 0000000..beaeaea --- /dev/null +++ b/Back/Services/servInvoicePayment.cs @@ -0,0 +1,45 @@ +using Back.Data.Contracts; +using Back.Data.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Hosting; +using Shared.DTOs; + +namespace Back.Services +{ + public class servInvoicePayment + { + private readonly IAsyncRepository _Repo; + public servInvoicePayment(IAsyncRepository Repo) + { + _Repo = Repo; + } + public async Task Add(InvoicePayment item) + { + return await _Repo.AddBoolResultAsync(item); + } + public async Task Update(InvoicePayment item) + { + return await _Repo.UpdateAsync(item); + } + public async Task Delete(InvoicePayment item) + { + return await _Repo.DeleteAsync(item); + } + public async Task Exist(int companyID, int invoiceID, int ID) + { + return await _Repo.Get(w => w.InvoiceID == invoiceID && w.ID == ID && w.invoice.CompanyID == companyID).AnyAsync(); + } + public async Task GetinvoicePay(int CompanyID, int ID) + { + return await _Repo + .Get(w => w.ID == ID && w.invoice.CompanyID == CompanyID && !w.invoice.IsDeleted) + .Include(s=>s.invoice) + .FirstOrDefaultAsync(); + } + public async Task GetInvoicePayByInvoicePayID (int companyID, int invoiceID, int ID) + { + return await _Repo.Get(w => w.InvoiceID == invoiceID && w.ID == ID && w.invoice.CompanyID == companyID).FirstOrDefaultAsync(); + } + + } +} diff --git a/Back/Validations/AUInvoicePayValidation.cs b/Back/Validations/AUInvoicePayValidation.cs new file mode 100644 index 0000000..24a0ce4 --- /dev/null +++ b/Back/Validations/AUInvoicePayValidation.cs @@ -0,0 +1,89 @@ +using Back.Common; +using Back.Services; +using FluentValidation; +using Shared.DTOs; + +namespace Back.Validations +{ + // companyid invoiceid model + public class AUInvoicePayValidation : AbstractValidator> + { + public AUInvoicePayValidation(servInvoice servInvoice, servTaxPayer servTaxPayer, servInvoicePayment servInvoicepay) + { + When(m => m.Item4 == eActionValidation.update, () => + { + RuleFor(r => r) + .Custom((model, context) => + { + if (!model.Item3.ID.HasValue) + context.AddFailure("شناسه در حالت ویرایش نمی تواند خالی باشد"); + if (!servInvoicepay.Exist(model.Item1, model.Item2, model.Item3.ID.Value).Result) + context.AddFailure("شناسه یافت نشد"); + + }); + + }); + When(m => m.Item4 == eActionValidation.update || m.Item4 == eActionValidation.add, () => + { + + + + RuleFor(r => r.Item3).Custom((model, context) => + { + if (model.pmt.HasValue && model.pmt.Value > 0 && model.pmt.Value < 9) + { + + } + else context.AddFailure("روش پرداخت مشخص نشده"); + }); + + //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("مبلغ تخفیف نمی تواند از (تعداد * مبلغ واحد) بیشتر باشد"); + + + //}); + }); + 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 (model.Item4 == eActionValidation.update || model.Item4 == eActionValidation.add) + { + var totalpay = invoice.payments.Sum(s => s.pv).GetValueOrDefault() + model.Item3.pv; + if (totalpay > invoice.tbill) context.AddFailure("مجموع مبلغ پرداختی میبایست از مجموع صورتحساب کوچکتر باشد."); + + } + + if (servTaxPayer.ExistSuccessfulorSendorpendingInvoice(invoice).Result) + context.AddFailure("این صورتحساب به سازمان ارسال شده" + '\n' + + "برای تغییر ،صورتحساب را ابطال/اصلاح یا برگشت بزنید"); + else if ((model.Item4 == eActionValidation.add || model.Item4 == eActionValidation.delete) + && invoice.invoiceType == InvoiceType.BackFrmSale) + context.AddFailure("پرداخت های صورتحساب در وضعیت برگشت از فروش نمی تواند اضافه یا کم شود"); + else if (invoice.invoiceType != InvoiceType.Bidding + && invoice.invoiceType != InvoiceType.Sale && invoice.invoiceType != InvoiceType.Repair) + { + context.AddFailure($"صورتحساب در حالت {invoice.invoiceType.GetEnumDisplayName()} نمی تواند ویرایش شود" + '\n' + + $"فقط در حالت پیش نویس ، فاکتور و اصلاح (که به سازمان ارسال نشده باشد) میتوان سند را ویرایش کرد"); + } + } + } + + else context.AddFailure("صورتحساب صحیح نمی باشد"); + + }); + + } + } +} diff --git a/Shared/DTOs/InvoiceDtos.cs b/Shared/DTOs/InvoiceDtos.cs index 10c6858..79bf618 100644 --- a/Shared/DTOs/InvoiceDtos.cs +++ b/Shared/DTOs/InvoiceDtos.cs @@ -141,12 +141,10 @@ namespace Shared.DTOs [MaxLength(12)] public string? pid { get; set; } //تاریخ و زمان پرداخت - [MaxLength(13)] - public long? pdt { get; set; } public string? PaymentDateTime { get; set; } //مبلغ پرداختی [MaxLength(18)] - public decimal? pv { get; set; } + public long? pv { get; set; } } //------------------------------ diff --git a/TaxPayerFull/Program.cs b/TaxPayerFull/Program.cs index ea0270a..b3c5965 100644 --- a/TaxPayerFull/Program.cs +++ b/TaxPayerFull/Program.cs @@ -23,8 +23,7 @@ builder.Services.AddScoped(sp => new UserAuthenticationDTO() IsAdmin = false, Logo = "", Name = "" - } - , + }, FullName = "", Photo="", Token="", @@ -37,10 +36,10 @@ builder.Services.AddScoped(sp => new UserAuthenticationDTO() //builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("http://195.88.208.142:7075/api/") }); //Home -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/") }); //farzan -//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");