| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 9 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 10 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 11 | | using WinSolutions.Sveta.Common; |
| | | 12 | | |
| | | 13 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 14 | | { |
| | | 15 | | public class MovementItemService : SvetaServiceBase |
| | | 16 | | { |
| | | 17 | | private readonly SvetaDbContext _db; |
| | | 18 | | private readonly ILogger<MovementItemService> _logger; |
| | | 19 | | public MovementItemService(SvetaDbContext db, ILogger<MovementItemService> logger, IAuthenticationService authen |
| | 0 | 20 | | : base(authenticationService) |
| | 0 | 21 | | { |
| | 0 | 22 | | _db = db; |
| | 0 | 23 | | _logger = logger; |
| | 0 | 24 | | } |
| | 0 | 25 | | public async Task<MovementItem> GetMovementItem(long id) => await _db.MovementItems |
| | 0 | 26 | | .Include(d => d.Good) |
| | 0 | 27 | | .ThenInclude(Good => Good.UnitsKind) |
| | 0 | 28 | | .Include(d => d.Good) |
| | 0 | 29 | | .ThenInclude(Good => Good.VatsKind) |
| | 0 | 30 | | .Include(d => d.RecState) |
| | 0 | 31 | | .Where(e => !e.IsDeleted) |
| | 0 | 32 | | .FirstOrDefaultAsync(d => d.Id == id); |
| | 0 | 33 | | public async Task<List<MovementItem>> GetMovementItems(int page, int limit) => await _db.MovementItems |
| | 0 | 34 | | .AsNoTracking() |
| | 0 | 35 | | .Include(d => d.Good) |
| | 0 | 36 | | .ThenInclude(Good => Good.DefaultBarCode) |
| | 0 | 37 | | .Include(d => d.Good) |
| | 0 | 38 | | .ThenInclude(Good => Good.GoodBarcodes) |
| | 0 | 39 | | .ThenInclude(barcode => barcode.BarCode) |
| | 0 | 40 | | .Include(d => d.Good) |
| | 0 | 41 | | .ThenInclude(Good => Good.UnitsKind) |
| | 0 | 42 | | .Include(d => d.Good) |
| | 0 | 43 | | .ThenInclude(Good => Good.VatsKind) |
| | 0 | 44 | | .Include(d => d.RecState) |
| | 0 | 45 | | .Where(d => !d.IsDeleted) |
| | 0 | 46 | | .Skip(page * limit) |
| | 0 | 47 | | .Take(limit) |
| | 0 | 48 | | .ToListAsync(); |
| | | 49 | | public async Task CreateMovementItem(MovementItem item) |
| | 0 | 50 | | { |
| | 0 | 51 | | if (item.RecState != null) |
| | 0 | 52 | | _db.Entry(item.RecState).State = item.RecState.Id == 0 ? EntityState.Detached : EntityState.Unchanged; |
| | 0 | 53 | | await _db.MovementItems.AddAsync(item); |
| | 0 | 54 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 55 | | } |
| | | 56 | | public async Task UpdateMovementItem(MovementItem item) |
| | 0 | 57 | | { |
| | 0 | 58 | | _db.MovementItems.Update(item); |
| | 0 | 59 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 60 | | } |
| | | 61 | | public async Task DeleteMovementItem(MovementItem data) |
| | 0 | 62 | | { |
| | 0 | 63 | | if (!(await MovementItemExists(data.Id))) |
| | 0 | 64 | | { |
| | 0 | 65 | | throw new ArgumentException($"Record #{data.Id} not found"); |
| | | 66 | | } |
| | 0 | 67 | | _db.Entry(data).State = EntityState.Deleted; |
| | 0 | 68 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 69 | | } |
| | 0 | 70 | | public async Task<bool> MovementItemExists(long id) => await _db.MovementItems |
| | 0 | 71 | | .AsNoTracking() |
| | 0 | 72 | | .Where(e => !e.IsDeleted) |
| | 0 | 73 | | .Where(d => d.Id == id) |
| | 0 | 74 | | .AnyAsync(); |
| | | 75 | | } |
| | | 76 | | } |