NonInventPurchasingSystem/CPRNIMS.Domain/Services/Receiving/Receiving.cs
2026-01-20 07:44:30 +08:00

228 lines
8.0 KiB
C#

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<List<Infrastructure.Entities.Receiving.RRReport>> GetRRReport(ItemDto itemDto)
{
try
{
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<Infrastructure.Entities.Receiving.RRReport>();
}
catch (SqlException ex)
{
ex.ToString();
throw;
}
}
public async Task<List<ForReceiving>> GetForReceiving(ItemDto itemDto)
{
try
{
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<ForReceiving>();
}
catch (SqlException ex)
{
ex.ToString();
throw;
}
}
public async Task<List<RR>> GetRR(ItemDto itemDto)
{
try
{
var allItems = await _dbContext.RRs
.FromSqlRaw($"EXEC GetRR @UserId",
new SqlParameter("@UserId", itemDto.UserId))
.ToListAsync();
return allItems ?? new List<RR>();
}
catch (SqlException ex)
{
ex.ToString();
throw;
}
}
public async Task<List<RRSeries>> GetLatestRRNo(ItemDto itemDto)
{
try
{
return await _dbContext.RRSeries.FromSqlRaw("EXEC GetLatestRRNo @UserId",
new SqlParameter("@UserId",itemDto.UserId)).ToListAsync();
}
catch (Exception)
{
throw;
}
}
public async Task<List<ReceivingDetail>> GetRRDetailByPO(ItemDto itemDto)
{
try
{
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<ReceivingDetail>();
}
catch (SqlException ex)
{
ex.ToString();
throw;
}
}
public async Task<List<RRDetail>> GetRRDetail(ItemDto itemDto)
{
try
{
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<RRDetail>();
}
catch (SqlException ex)
{
ex.ToString();
throw;
}
}
#endregion
#region Post Put
public async Task<PRDetails> 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<PRDetails> 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<RRSeries> 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
}
}