using Back.Data.Contracts; using Back.Data.Models.Warehouse; using Microsoft.EntityFrameworkCore; using Microsoft.Identity.Client; using Shared.DTOs.Warehouse; using Shared.Enums; namespace Back.Services.Warehouse { public class ReceiptService { private readonly IAsyncRepository _ReceiptRepo; private readonly CheckPermission _checkPermission; public ReceiptService(IAsyncRepository receiptRepo, CheckPermission checkPermission) { _ReceiptRepo = receiptRepo; _checkPermission = checkPermission; } public async Task ADD(ReceiptDto item, int CompanyID,bool IneffectiveinAccounting=false) { try { if (IneffectiveinAccounting || await _checkPermission.ExtensionofAccess(CompanyID, 18, "-1")) { var model = new Receipt() { Date = item.Date.Replace("/",""), CODID = item.CODID, Count = item.Count, ForSale = item.ForSale, 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; } } else { return null; } } catch (Exception) { return null; } } public async Task Update(ReceiptDto 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.ForSale= item.ForSale; 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).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; if (models.Any()) return await _ReceiptRepo.UpdateRangeAsync(models); return true; } //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 ReceiptDto() { Date = s.Date, CODID=s.CODID, Count = s.Count, ForSale= s.ForSale, ID=s.ID, CODTitle=s.cODItem.Title, info = s.info, Type=s.Type }) .FirstOrDefaultAsync(); } public async Task?> GetAll(int CompanyID, TypeReceipt? 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 ReceiptDto() { Date = s.Date, CODID = s.CODID, Count = s.Count, ForSale = s.ForSale, ID = s.ID, CODTitle = s.cODItem.Title, info = s.info, Type = s.Type }) .ToListAsync(); } } }