Files
moadiran/Back/Services/Warehouse/ReceiptService.cs

158 lines
5.2 KiB
C#
Raw Normal View History

2025-01-11 10:10:47 +03:30
using Back.Data.Contracts;
using Back.Data.Models.Warehouse;
using Microsoft.EntityFrameworkCore;
2025-01-20 14:10:15 +03:30
using Microsoft.Identity.Client;
2025-01-11 10:10:47 +03:30
using Shared.DTOs.Warehouse;
using Shared.Enums;
namespace Back.Services.Warehouse
{
public class ReceiptService
{
private readonly IAsyncRepository<Receipt> _ReceiptRepo;
2025-01-19 17:44:20 +03:30
private readonly CheckPermission _checkPermission;
2025-01-11 10:10:47 +03:30
2025-01-19 17:44:20 +03:30
public ReceiptService(IAsyncRepository<Receipt> receiptRepo, CheckPermission checkPermission)
2025-01-11 10:10:47 +03:30
{
2025-01-19 17:44:20 +03:30
_ReceiptRepo = receiptRepo;
_checkPermission = checkPermission;
2025-01-11 10:10:47 +03:30
}
2025-01-20 14:10:15 +03:30
public async Task<ReceiptDto?> ADD(ReceiptDto item, int CompanyID,bool IneffectiveinAccounting=false)
2025-01-11 10:10:47 +03:30
{
2025-01-19 17:44:20 +03:30
try
2025-01-11 10:10:47 +03:30
{
2025-01-20 14:10:15 +03:30
if (IneffectiveinAccounting || await _checkPermission.ExtensionofAccess(CompanyID, 18, "-1"))
2025-01-19 17:44:20 +03:30
{
var model = new Receipt()
{
2025-01-23 02:44:11 +03:30
Date = item.Date.Replace("/",""),
2025-01-19 17:44:20 +03:30
CODID = item.CODID,
Count = item.Count,
ForSale = item.ForSale,
info = item.info,
Type = item.Type,
2025-01-25 12:57:07 +03:30
InvoiceID=item.InvoiceID,
2025-01-19 17:44:20 +03:30
Deleted = false
};
var returnmodel = await _ReceiptRepo.AddAsync(model);
if (returnmodel != null)
{
item.ID = returnmodel.ID;
return item;
}
else
{
return null;
}
}
else
{
return null;
}
2025-01-11 10:10:47 +03:30
}
2025-01-19 17:44:20 +03:30
catch (Exception)
2025-01-11 10:10:47 +03:30
{
return null;
}
2025-01-19 17:44:20 +03:30
2025-01-11 10:10:47 +03:30
}
public async Task<ReceiptDto?> Update(ReceiptDto item)
{
var model= await _ReceiptRepo.Get(w => w.ID == item.ID).FirstOrDefaultAsync();
2025-01-23 02:44:11 +03:30
model.Date= item.Date.Replace("/", "");
2025-01-11 10:10:47 +03:30
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<bool> Delete(int itemID,int CompanyID)
{
var model = await _ReceiptRepo.Get(w => w.ID == itemID && w.cODItem.CompanyID==CompanyID).FirstOrDefaultAsync();
2025-01-19 17:44:20 +03:30
model.Deleted= true;
return await _ReceiptRepo.UpdateAsync(model);
2025-01-11 10:10:47 +03:30
}
2025-01-24 19:18:17 +03:30
public async Task<bool> 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;
2025-01-25 12:57:07 +03:30
if (models.Any())
return await _ReceiptRepo.UpdateRangeAsync(models);
return true;
2025-01-24 19:18:17 +03:30
}
//public async Task<bool> 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);
//}
2025-01-11 10:10:47 +03:30
public async Task<ReceiptDto?> Get(int itemID, int CompanyID)
{
2025-01-19 17:44:20 +03:30
return await _ReceiptRepo.Get(w => w.ID == itemID && w.cODItem.CompanyID == CompanyID && !w.Deleted)
2025-01-11 10:10:47 +03:30
.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<List<ReceiptDto>?> GetAll(int CompanyID, TypeReceipt? Type=null, int CODID=0)
{
2025-01-19 17:44:20 +03:30
var request = _ReceiptRepo.Get(w => w.cODItem.CompanyID == CompanyID && !w.Deleted);
2025-01-11 10:10:47 +03:30
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();
}
}
}