using Back.Data.Contracts; using Back.Data.Models.Warehouse; using Microsoft.EntityFrameworkCore; using Shared.DTOs.Warehouse; using Shared.Enums; namespace Back.Services.Warehouse { public class RemittanceService { private readonly IAsyncRepository _ReceiptRepo; public RemittanceService(IAsyncRepository remittanceRepo) { _ReceiptRepo = remittanceRepo; } public async Task ADD(RemittanceDto item) { var model = new Remittance() { Date = item.Date.Replace("/", ""), CODID = item.CODID, Count = item.Count, info = item.info, Type = item.Type, InvoiceID = item.InvoiceID, Deleted=false }; var returnmodel = await _ReceiptRepo.AddAsync(model); if (returnmodel != null) { item.ID = returnmodel.ID; return item; } else { return null; } } public async Task AddRange(List items) { await _ReceiptRepo.AddRangeAsync(items); } public async Task Update(RemittanceDto item) { var model = await _ReceiptRepo.Get(w => w.ID == item.ID).FirstOrDefaultAsync(); model.Date = item.Date.Replace("/", ""); model.CODID = item.CODID; model.Count = item.Count; model.info = item.info; model.Type = item.Type; var returnmodel = await _ReceiptRepo.UpdateAsync(model); if (returnmodel) { return item; } else { return null; } } public async Task Delete(int itemID, int CompanyID) { var model = await _ReceiptRepo.Get(w => w.ID == itemID && w.cODItem.CompanyID == CompanyID && !w.Deleted).FirstOrDefaultAsync(); model.Deleted = true; return await _ReceiptRepo.UpdateAsync(model); } public async Task DeleteByInvoiceID(int InvoiceID, int CompanyID) { var models = await _ReceiptRepo.Get(w => w.InvoiceID == InvoiceID && w.cODItem.CompanyID == CompanyID && !w.Deleted).ToListAsync(); foreach (var model in models) model.Deleted = true; return await _ReceiptRepo.UpdateRangeAsync(models); } public async Task DeleteByCODIDAndInvoiceID(int InvoiceID, int CODID, int CompanyID) { var model = await _ReceiptRepo.Get(w => w.InvoiceID == InvoiceID && w.CODID == CODID && w.cODItem.CompanyID == CompanyID && !w.Deleted).FirstOrDefaultAsync(); model.Deleted = true; return await _ReceiptRepo.UpdateAsync(model); } public async Task Get(int itemID, int CompanyID) { return await _ReceiptRepo.Get(w => w.ID == itemID && w.cODItem.CompanyID == CompanyID && !w.Deleted) .Include(inc => inc.cODItem) .Select(s => new RemittanceDto() { Date = s.Date, CODID = s.CODID, Count = s.Count, ID = s.ID, CODTitle = s.cODItem.Title, info = s.info, Type = s.Type }) .FirstOrDefaultAsync(); } public async Task?> GetAll(int CompanyID, TypeRemittance? Type = null, int CODID = 0) { var request = _ReceiptRepo.Get(w => w.cODItem.CompanyID == CompanyID && !w.Deleted); if (Type != null) request = request.Where(w => w.Type == Type); if (CODID != 0) request = request.Where(w => w.CODID == CODID); return await request .Include(inc => inc.cODItem) .Select(s => new RemittanceDto() { Date = s.Date, CODID = s.CODID, Count = s.Count, ID = s.ID, CODTitle = s.cODItem.Title, info = s.info, Type = s.Type }) .ToListAsync(); } public async Task HasaRemittance(int InvoiceID) { return await _ReceiptRepo.Get(w => w.InvoiceID == InvoiceID && !w.Deleted).AnyAsync(); } public async Task HasaRemittance(int InvoiceID,int CODID) { return await _ReceiptRepo.Get(w => w.InvoiceID == InvoiceID && w.CODID==CODID && !w.Deleted).AnyAsync(); } public async Task ChangeRemittance(int InvoiceID, int CODID,int newnvoceID) { var item= await _ReceiptRepo.Get(w => w.InvoiceID == InvoiceID && w.CODID == CODID && !w.Deleted).FirstOrDefaultAsync(); if (item!=null) { item.InvoiceID = newnvoceID; item.info = $"حواله خودکار از صورتحساب {newnvoceID}"; return await _ReceiptRepo.UpdateAsync(item); } return false; } public async Task DeleteByInvoiceIDandCODID(int InvoiceID, int CODID) { var model = await _ReceiptRepo.Get(w => w.InvoiceID == InvoiceID && w.CODID == CODID && !w.Deleted).FirstOrDefaultAsync(); model.Deleted = true; await _ReceiptRepo.UpdateAsync(model); } } }