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 ReceiptService { private readonly IAsyncRepository _ReceiptRepo; public ReceiptService(IAsyncRepository ReceiptRepo) { _ReceiptRepo = ReceiptRepo; } public async Task ADD(ReceiptDto item) { var model = new Receipt() { Date = item.Date, CODID = item.CODID, Count = item.Count, ForSale = item.ForSale, info = item.info, Type = item.Type, }; var returnmodel = await _ReceiptRepo.AddAsync(model); if (returnmodel!=null) { item.ID= returnmodel.ID; return item; } else { return null; } } public async Task Update(ReceiptDto item) { var model= await _ReceiptRepo.Get(w => w.ID == item.ID).FirstOrDefaultAsync(); model.Date= item.Date; 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(); return await _ReceiptRepo.DeleteAsync(model); } public async Task Get(int itemID, int CompanyID) { return await _ReceiptRepo.Get(w => w.ID == itemID && w.cODItem.CompanyID == CompanyID) .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); 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(); } } }