using CPRNIMS.Domain.Contracts.Receiving; using CPRNIMS.Infrastructure.Database; using CPRNIMS.Infrastructure.Dto.Items; using CPRNIMS.Infrastructure.Entities.Purchasing; using CPRNIMS.Infrastructure.Entities.Receiving; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CPRNIMS.Domain.Services.Receiving { public class Receiving : IReceiving { private readonly NonInventoryDbContext _dbContext; public Receiving(NonInventoryDbContext dbContext) { _dbContext = dbContext; } #region Get public async Task> GetRRReport(ItemDto itemDto) { if (!itemDto.IsSorting) { itemDto.DateFrom = DateTime.Today; itemDto.DateTo = DateTime.Today.AddDays(1).AddSeconds(-1); } var allItems = await _dbContext.RRReports .FromSqlRaw($"EXEC GetRRReport @DateFrom,@DateTo,@IsSorting", new SqlParameter("@DateFrom", itemDto.DateFrom), new SqlParameter("@DateTo", itemDto.DateTo), new SqlParameter("@IsSorting", itemDto.IsSorting)) .ToListAsync(); return allItems ?? new List(); } public async Task> GetForReceiving(ItemDto itemDto) { var allItems = await _dbContext.ForReceivings .FromSqlRaw($"EXEC GetForReceiving @UserId,@IsDenied", new SqlParameter("@UserId", itemDto.UserId), new SqlParameter("@IsDenied", itemDto.IsDenied)) .ToListAsync(); return allItems ?? new List(); } public async Task> GetRR(ItemDto itemDto) { var allItems = await _dbContext.RRs .FromSqlRaw($"EXEC GetRR @UserId", new SqlParameter("@UserId", itemDto.UserId)) .ToListAsync(); return allItems ?? new List(); } public async Task> GetLatestRRNo(ItemDto itemDto) { return await _dbContext.RRSeries.FromSqlRaw("EXEC GetLatestRRNo @UserId", new SqlParameter("@UserId", itemDto.UserId)).ToListAsync(); } public async Task> GetRRDetailByPO(ItemDto itemDto) { var allItems = await _dbContext.ReceivingDetails .FromSqlRaw($"EXEC GetRRDetailByPO @PONo,@POTypeId,@UserId", new SqlParameter("@PONo", itemDto.PONo), new SqlParameter("@POTypeId", itemDto.POTypeId), new SqlParameter("@UserId", itemDto.UserId)) .ToListAsync(); return allItems ?? new List(); } public async Task> GetRRDetail(ItemDto itemDto) { var allItems = await _dbContext.RRDetailss .FromSqlRaw("EXEC GetRRDetail @RRNo,@UserId", new SqlParameter("@RRNo", itemDto.RRNo), new SqlParameter("@UserId", itemDto.UserId)) .ToListAsync(); return allItems ?? new List(); } #endregion #region Post Put public async Task PostPutReceiving(ItemDto itemDto) { try { await _dbContext.Database .ExecuteSqlRawAsync($"EXEC PostPutReceiving @UserId, @PONo, @POTypeId, @EmailAddress, @DRNo, @DocTypeId, @QuantityReceived,@RRNo,@PRDetailsId,@Remarks,@ReceivedDate,@IsCompleted", new SqlParameter("@UserId", itemDto.UserId), new SqlParameter("@PONo", itemDto.PONo), new SqlParameter("@POTypeId", itemDto.POTypeId), new SqlParameter("@EmailAddress", itemDto.EmailAddress), new SqlParameter("@DRNo", itemDto.DRNo), new SqlParameter("@DocTypeId", itemDto.DocTypeId), new SqlParameter("@QuantityReceived", itemDto.QuantityReceived), new SqlParameter("@RRNo", itemDto.RRNo), new SqlParameter("@PRDetailsId", itemDto.PRDetailsId), new SqlParameter("@Remarks", itemDto.Remarks ?? "N/A"), new SqlParameter("@ReceivedDate", itemDto.ReceivedDate), new SqlParameter("@IsCompleted", itemDto.IsCompleted)); if (!isUpdated) { await UpdateRRNoSeriesAsync(itemDto.RRNo); } return new PRDetails(); } catch (SqlException ex) { ex.ToString(); throw; } } bool isUpdated=false; public async Task PutPOClose(ItemDto itemDto) { try { await _dbContext.Database .ExecuteSqlRawAsync("EXEC PutPOClose @UserId, @PONo, @POTypeId, @EmailAddress,@PRDetailsId,@DocTypeId,@PRNo,@Remarks", new SqlParameter("@UserId", itemDto.UserId), new SqlParameter("@PONo", itemDto.PONo), new SqlParameter("@POTypeId", itemDto.POTypeId), new SqlParameter("@EmailAddress", itemDto.EmailAddress), new SqlParameter("@DocTypeId", itemDto.DocTypeId), new SqlParameter("@PRDetailsId", itemDto.PRDetailsId), new SqlParameter("@PRNo", itemDto.PRNo), new SqlParameter("@Remarks", itemDto.Remarks ?? "N/A")); return new PRDetails(); } catch (SqlException ex) { ex.ToString(); throw; } } private async Task UpdateRRNoSeriesAsync(long rrNo) { try { var rrno = await _dbContext.RRSeries.FirstOrDefaultAsync(x => x.Id == 1); if (rrno == null) throw new Exception("RRSeries record with ID 1 not found."); rrno.RRNo = rrNo; await _dbContext.SaveChangesAsync(); isUpdated=true; } catch (Exception ex) { throw; } } public async Task PutRRNoSeries(ItemDto itemDto) { try { var rrno = await _dbContext.RRSeries.FirstOrDefaultAsync(x => x.Id == 1); rrno.RRNo=itemDto.RRNo; _dbContext.RRSeries.Update(rrno); await _dbContext.SaveChangesAsync(); return rrno; } catch (Exception) { throw; } } #endregion } }