792 lines
36 KiB
C#
792 lines
36 KiB
C#
using AutoMapper;
|
|
using CPRNIMS.Domain.Services.ICanvass;
|
|
using CPRNIMS.Infrastructure.Database;
|
|
using CPRNIMS.Infrastructure.Dto.Canvass;
|
|
using CPRNIMS.Infrastructure.Dto.Canvass.Request;
|
|
using CPRNIMS.Infrastructure.Dto.Canvass.Response;
|
|
using CPRNIMS.Infrastructure.Dto.Common;
|
|
using CPRNIMS.Infrastructure.Entities.Canvass;
|
|
using CPRNIMS.Infrastructure.Entities.Purchasing;
|
|
using CPRNIMS.Infrastructure.Helper;
|
|
using CPRNIMS.Infrastructure.ViewModel.Canvass;
|
|
using CPRNIMS.Infrastructure.ViewModel.Common;
|
|
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Data;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static CPRNIMS.Domain.Services.OutputParamMessage;
|
|
|
|
namespace CPRNIMS.Domain.Services.Canvass
|
|
{
|
|
public class Canvass : Contracts.Canvass.ICanvass
|
|
{
|
|
private readonly NonInventoryDbContext _dbContext;
|
|
private readonly SMTPHelper _smptHelper;
|
|
private readonly IMapper _mapper;
|
|
public Canvass(NonInventoryDbContext dbContext, SMTPHelper smptHelper,IMapper mapper)
|
|
{
|
|
_dbContext = dbContext;
|
|
_smptHelper = smptHelper;
|
|
_mapper = mapper;
|
|
}
|
|
#region Get
|
|
public async Task<List<Suppliers>> GetSuppliers(CancellationToken ct)
|
|
{
|
|
|
|
return await _dbContext.Suppliers
|
|
.AsNoTracking()
|
|
.Where(s => s.IsActive)
|
|
.ToListAsync(ct);
|
|
}
|
|
public async Task<List<ItemListWOEmail>> GetItemSupplierWOEmail(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.ItemListWOEmails
|
|
.FromSqlRaw($"EXEC GetItemSupplierWOEmail @PRNo = '{CanvassDto.PRNo}'")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<ItemListWOEmail>();
|
|
}
|
|
public async Task<List<BiddingItem>> GetSupplierBid(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.BiddingItems
|
|
.FromSqlRaw($"EXEC GetSupplierBid @UserId,@IsHistory",
|
|
new SqlParameter("@UserId", CanvassDto.UserId),
|
|
new SqlParameter("@IsHistory", CanvassDto.IsHistory))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<BiddingItem>();
|
|
}
|
|
public async Task<PagedResult<ItemsForTagging>> GetItemsForTagging(CanvassDto dto)
|
|
{
|
|
var parameters = new[]
|
|
{
|
|
new SqlParameter("@UserId", dto.UserId),
|
|
new SqlParameter("@SearchPRNo", dto.SearchPRNo ?? ""),
|
|
new SqlParameter("@SearchItemNo", dto.SearchItemNo ?? ""),
|
|
new SqlParameter("@SearchItemName", dto.SearchItemName ?? ""),
|
|
new SqlParameter("@SearchDepartment", dto.SearchDepartment ?? ""),
|
|
new SqlParameter("@PageNumber", dto.PageNumber),
|
|
new SqlParameter("@PageSize", dto.PageSize)
|
|
};
|
|
|
|
int totalCount = 0;
|
|
var departmentList = new List<string>();
|
|
var items = new List<ItemsForTagging>();
|
|
|
|
var conn = _dbContext.Database.GetDbConnection();
|
|
await conn.OpenAsync();
|
|
|
|
using var cmd = conn.CreateCommand();
|
|
cmd.CommandText = @"EXEC GetItemsForTagging @UserId,
|
|
@SearchPRNo,@SearchItemNo,@SearchItemName,@SearchDepartment,
|
|
@PageNumber, @PageSize";
|
|
foreach (var p in parameters) cmd.Parameters.Add(p);
|
|
cmd.CommandTimeout = 60;
|
|
|
|
using var reader = await cmd.ExecuteReaderAsync();
|
|
|
|
// Result set 1 — distinct supplier list
|
|
while (await reader.ReadAsync())
|
|
departmentList.Add(reader.GetString(0));
|
|
|
|
// Result set 2 — total count
|
|
await reader.NextResultAsync();
|
|
if (await reader.ReadAsync())
|
|
totalCount = reader.GetInt32(0);
|
|
|
|
// Result set 3 — paged rows
|
|
await reader.NextResultAsync();
|
|
while (await reader.ReadAsync())
|
|
{
|
|
items.Add(new ItemsForTagging
|
|
{
|
|
PRDetailsId = Convert.ToInt64(reader["PRDetailsId"]),
|
|
PRNo = Convert.ToInt64(reader["PRNo"]),
|
|
ItemNo = Convert.ToInt64(reader["ItemNo"]),
|
|
ItemName = reader["ItemName"]?.ToString(),
|
|
ItemDescription = reader["ItemDescription"]?.ToString(),
|
|
Qty = Convert.ToDecimal(reader["Qty"]),
|
|
Department = reader["Department"]?.ToString(),
|
|
CreatedBy = reader["CreatedBy"]?.ToString(),
|
|
CreatedDate =Convert.ToDateTime(reader["CreatedDate"]),
|
|
DateNeeded = Convert.ToDateTime(reader["DateNeeded"]),
|
|
});
|
|
}
|
|
|
|
await conn.CloseAsync();
|
|
|
|
return new PagedResult<ItemsForTagging>
|
|
{
|
|
Data = items,
|
|
TotalCount = totalCount,
|
|
PageNumber = dto.PageNumber,
|
|
PageSize = dto.PageSize,
|
|
DepartmentList = departmentList
|
|
};
|
|
}
|
|
public async Task<PagedResult<PerSupplier>> GetCanvassPerSupplier(CanvassDto dto)
|
|
{
|
|
var parameters = new[]
|
|
{
|
|
new SqlParameter("@UserId", dto.UserId),
|
|
new SqlParameter("@SearchPRNo", dto.SearchPRNo ?? ""),
|
|
new SqlParameter("@SearchItemNo", dto.SearchItemNo ?? ""),
|
|
new SqlParameter("@SearchItemName", dto.SearchItemName ?? ""),
|
|
new SqlParameter("@SearchSupplier", dto.SearchSupplier ?? ""),
|
|
new SqlParameter("@PageNumber", dto.PageNumber),
|
|
new SqlParameter("@PageSize", dto.PageSize)
|
|
};
|
|
|
|
var supplierList = new List<string>();
|
|
int totalCount = 0;
|
|
var items = new List<PerSupplier>();
|
|
|
|
var conn = _dbContext.Database.GetDbConnection();
|
|
await conn.OpenAsync();
|
|
|
|
using var cmd = conn.CreateCommand();
|
|
cmd.CommandText = @"EXEC GetCanvassPerSupplier @UserId,
|
|
@SearchPRNo,@SearchItemNo,@SearchItemName,@SearchSupplier,
|
|
@PageNumber, @PageSize";
|
|
foreach (var p in parameters) cmd.Parameters.Add(p);
|
|
cmd.CommandTimeout = 60;
|
|
|
|
using var reader = await cmd.ExecuteReaderAsync();
|
|
|
|
// Result set 1 — distinct supplier list
|
|
while (await reader.ReadAsync())
|
|
supplierList.Add(reader.GetString(0));
|
|
|
|
// Result set 2 — total count
|
|
await reader.NextResultAsync();
|
|
if (await reader.ReadAsync())
|
|
totalCount = reader.GetInt32(0);
|
|
|
|
// Result set 3 — paged rows
|
|
await reader.NextResultAsync();
|
|
while (await reader.ReadAsync())
|
|
{
|
|
items.Add(new PerSupplier
|
|
{
|
|
SupplierId = reader["SupplierId"] as int? ?? 0,
|
|
SupplierName = reader["SupplierName"]?.ToString(),
|
|
EmailAddress = reader["EmailAddress"]?.ToString(),
|
|
AggreItemName = reader["AggreItemName"]?.ToString(),
|
|
AggreItemNo = reader["AggreItemNo"]?.ToString(),
|
|
AggrePRNo = reader["AggrePRNo"]?.ToString(),
|
|
});
|
|
}
|
|
|
|
await conn.CloseAsync();
|
|
|
|
return new PagedResult<PerSupplier>
|
|
{
|
|
Data = items,
|
|
TotalCount = totalCount,
|
|
PageNumber = dto.PageNumber,
|
|
PageSize = dto.PageSize,
|
|
SupplierList = supplierList
|
|
};
|
|
}
|
|
public async Task<List<RFQPerSupplier>> GetSupplierBidByItem(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.RFQPerSuppliers
|
|
.FromSqlRaw($"EXEC GetSupplierBidByItem @UserId,@Status,@ItemNo,@CanvassId,@IsHistory,@PRDetailsId",
|
|
new SqlParameter("@UserId", CanvassDto.UserId),
|
|
new SqlParameter("@Status", CanvassDto.Status),
|
|
new SqlParameter("@ItemNo", CanvassDto.ItemNo),
|
|
new SqlParameter("@CanvassId", CanvassDto.CanvassId),
|
|
new SqlParameter("@IsHistory", CanvassDto.IsHistory),
|
|
new SqlParameter("@PRDetailsId", CanvassDto.PRDetailsId))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<RFQPerSupplier>();
|
|
}
|
|
public async Task<List<PRCanvassDetail>> GetCanvassById(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.PRCanvassDetails
|
|
.FromSqlRaw("EXEC GetCanvassById @UserId",
|
|
new SqlParameter("@UserId", CanvassDto.UserId))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<PRCanvassDetail>();
|
|
}
|
|
public async Task<List<PRCanvassDetail>> GetCanvassByItemNo(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.PRCanvassDetails
|
|
.FromSqlRaw($"EXEC GetCanvassByItemNo @UserId, @ItemNo",
|
|
new SqlParameter("@UserId", CanvassDto.UserId),
|
|
new SqlParameter("@ItemNo", CanvassDto.ItemNo))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<PRCanvassDetail>();
|
|
}
|
|
public async Task<List<PRCanvassDetail>> GetCanvassByPRNo(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.PRCanvassDetails
|
|
.FromSqlRaw("EXEC GetCanvassByPRNo @UserId, @PRNo",
|
|
new SqlParameter("@UserId", CanvassDto.UserId),
|
|
new SqlParameter("@PRNo", CanvassDto.PRNo))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<PRCanvassDetail>();
|
|
}
|
|
public async Task<List<CanvassGroupByPRNo>> GetCanvassGroupByPRNo(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.CanvassGroupByPRNos
|
|
.FromSqlRaw("EXEC GetCanvassGroupByPRNo @UserId,@PRNo,@AggreItemNo,@IsTagging",
|
|
new SqlParameter("@UserId", CanvassDto.UserId),
|
|
new SqlParameter("@PRNo", CanvassDto.PRNo),
|
|
new SqlParameter("@AggreItemNo", CanvassDto.AggreItemNo ?? "N/A"),
|
|
new SqlParameter("@IsTagging", CanvassDto.IsTagging))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<CanvassGroupByPRNo>();
|
|
}
|
|
public async Task<List<SupplierResponseDto>> GetSupplierItemWOEmail(CanvassDto canvassDto)
|
|
{
|
|
|
|
var suppliers = await _dbContext.SupplierResponses
|
|
.FromSqlInterpolated($"EXEC GetSupplierItemWOEmail @ItemNo = {canvassDto.ItemNo}")
|
|
.ToListAsync();
|
|
|
|
return suppliers ?? new List<SupplierResponseDto>();
|
|
}
|
|
|
|
public async Task<List<SupplierResponseDto>> GetSupplierById(CanvassDto CanvassDto)
|
|
{
|
|
var items = await _dbContext.SupplierResponses
|
|
.FromSqlRaw($"EXEC GetSupplierById @SupplierId",
|
|
new SqlParameter("@SupplierId", CanvassDto.SupplierId))
|
|
.ToListAsync();
|
|
|
|
return items ?? new List<SupplierResponseDto>();
|
|
}
|
|
public async Task<List<RFQReference>> GetRFQ(ForCanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.RFQReferences
|
|
.FromSqlRaw($"EXEC GetRFQPerSupplier @SupplierId,@UserId",
|
|
new SqlParameter("@SupplierId", CanvassDto.SupplierId),
|
|
new SqlParameter("@UserId", CanvassDto.UserId))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<RFQReference>();
|
|
}
|
|
public async Task<List<PRCanvassDetail>> GetPRItemList(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.PRCanvassDetails
|
|
.FromSqlRaw($"EXEC GetPRItemList @UserId = '{CanvassDto.UserId}'")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<PRCanvassDetail>();
|
|
}
|
|
public async Task<List<PRCanvassDetail>> GetCanvassPerSupplierEmail(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.PRCanvassDetails
|
|
.FromSqlRaw($"EXEC GetCanvassPerSupplierEmail @UserId = '{CanvassDto.UserId}', @EmailAddress = '{CanvassDto.EmailAddress}'")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<PRCanvassDetail>();
|
|
}
|
|
public async Task<List<PRCanvassDetail>> GetPRItem(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.PRCanvassDetails
|
|
.FromSqlRaw($"EXEC GetPRItem @UserId = '{CanvassDto.UserId}', @ItemNo = {CanvassDto.ItemNo}")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<PRCanvassDetail>();
|
|
}
|
|
public async Task<List<WOResponse>> GetCanvassWOResponse(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.WOResponses
|
|
.FromSqlRaw($"EXEC GetCanvassWOResponse @UserId = '{CanvassDto.UserId}'")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<WOResponse>();
|
|
}
|
|
public async Task<int> GetCanvassNo()
|
|
{
|
|
var latestCanvassNo = await _dbContext.Canvasses
|
|
.OrderByDescending(ic => ic.CanvassNo)
|
|
.FirstOrDefaultAsync();
|
|
|
|
if (latestCanvassNo != null)
|
|
{
|
|
return latestCanvassNo.CanvassNo;
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
public async Task<List<ForAISearchingTagging>> GetForAISearchingTagging(CancellationToken ct)
|
|
{
|
|
var allItems = await _dbContext.ForAISearchingTaggings
|
|
.AsNoTracking()
|
|
.Take(1)
|
|
.ToListAsync(ct);
|
|
|
|
return allItems ?? new List<ForAISearchingTagging>();
|
|
}
|
|
public async Task<List<ItemWithoutSupplier>> GetItemWithoutSupplier(CancellationToken ct)
|
|
{
|
|
var allItems = await _dbContext.ItemWithoutSuppliers
|
|
.FromSqlRaw("EXEC GetItemWithoutSupplier")
|
|
.ToListAsync(ct);
|
|
|
|
return allItems ?? new List<ItemWithoutSupplier>();
|
|
}
|
|
public async Task<List<SupplierForCanvass>> GetSupplierForCanvass(int supplierId,string userName,
|
|
CancellationToken ct)
|
|
{
|
|
var allItems = await _dbContext.SupplierForCanvass
|
|
.FromSqlRaw("EXEC GetSupplierForCanvass @UserName,@SupplierId",
|
|
new SqlParameter("@UserName", userName),
|
|
new SqlParameter("@SupplierId", supplierId)
|
|
).AsNoTracking().ToListAsync(ct);
|
|
|
|
return allItems ?? new List<SupplierForCanvass>();
|
|
}
|
|
public async Task<List<WOResponseById>> GetWOResponseBySuppId(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.WOResponseByIds
|
|
.FromSqlRaw($"EXEC GetWOResponseBySuppId @UserId = '{CanvassDto.UserId}',@SupplierId = '{CanvassDto.SupplierId}'")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<WOResponseById>();
|
|
}
|
|
public async Task<RFQ> PostPerSupplierToken(ForCanvassDto CanvassDto)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PostPerSupplierToken @UserId,@SupplierId,@PRNo,@ItemNo,@CanvassNo,@PRDetailsId",
|
|
new SqlParameter("@UserId", CanvassDto.UserId),
|
|
new SqlParameter("@SupplierId", CanvassDto.SupplierId),
|
|
new SqlParameter("@PRNo", CanvassDto.PRNo),
|
|
new SqlParameter("@ItemNo", CanvassDto.ItemNo),
|
|
new SqlParameter("@CanvassNo", CanvassDto.CanvassNo),
|
|
new SqlParameter("@PRDetailsId", CanvassDto.PRDetailsId));
|
|
|
|
return new RFQ();
|
|
}
|
|
public async Task<List<SupplierBidById>> GetSupplierBidById(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.SupplierBidByIds
|
|
.FromSqlRaw($"EXEC GetSupplierBidById @CanvassDetailId",
|
|
new SqlParameter("@CanvassDetailId", CanvassDto.CanvassDetailId))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<SupplierBidById>();
|
|
}
|
|
public async Task<List<SupplierResponseDto>> GetMySuppliers(CanvassDto CanvassDto)
|
|
{
|
|
var items = await _dbContext.SupplierResponses
|
|
.FromSqlRaw($"EXEC GetMySuppliers @UserId,@SupplierId",
|
|
new SqlParameter("@UserId", CanvassDto.UserId),
|
|
new SqlParameter("@SupplierId", CanvassDto.SupplierId))
|
|
.ToListAsync();
|
|
|
|
return items ?? new List<SupplierResponseDto>();
|
|
}
|
|
public async Task<List<MyPRWOCanvass>> GetMyPRWOCanvass(CanvassDto itemDto)
|
|
{
|
|
var allItems = await _dbContext.MyPRWOCanvass
|
|
.FromSqlRaw($"EXEC GetMyPRWOCanvass @UserId,@SupplierId,@Query",
|
|
new SqlParameter("@UserId", itemDto.UserId),
|
|
new SqlParameter("@SupplierId", itemDto.SupplierId),
|
|
new SqlParameter("@Query", itemDto.Query))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<MyPRWOCanvass>();
|
|
}
|
|
public async Task<List<Infrastructure.Entities.Canvass.PRList>>
|
|
GetPRListByPRNo(CanvassDto canvassDto)
|
|
{
|
|
var allItems = await _dbContext.PRItemList
|
|
.FromSqlRaw($"EXEC GetPRListByPRNo @PRNo,@UserId",
|
|
new SqlParameter("@UserId", canvassDto.UserId),
|
|
new SqlParameter("@PRNo", canvassDto.PRNo))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<Infrastructure.Entities.Canvass.PRList>();
|
|
}
|
|
|
|
public async Task<List<PRCanvassDetail>> GetCanvassPerSupplierId(CanvassDto itemCodeDto)
|
|
{
|
|
var allItems = await _dbContext.PRCanvassDetails
|
|
.FromSqlRaw($"EXEC GetCanvassPerSupplierId @UserId,@SupplierId",
|
|
new SqlParameter("@UserId", itemCodeDto.UserId),
|
|
new SqlParameter("@SupplierId", itemCodeDto.SupplierId))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<PRCanvassDetail>();
|
|
}
|
|
|
|
public async Task<List<AlternativeOfferDetails>>
|
|
GetAlternativeOfferByPRDetailId(CanvassDto canvassDto)
|
|
{
|
|
var allItems = await _dbContext.AlternativeOfferDetails
|
|
.FromSqlRaw($"EXEC GetAlternativeOfferByPRDetailId @UserId,@PRDetailsId",
|
|
new SqlParameter("@UserId", canvassDto.UserId),
|
|
new SqlParameter("@PRDetailsId", canvassDto.PRDetailsId))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<AlternativeOfferDetails>();
|
|
}
|
|
public async Task<List<ForCanvass>> GetForCanvassPerItem(CanvassDto CanvassDto)
|
|
{
|
|
var allItems = await _dbContext.ForCanvasses
|
|
.FromSqlRaw($"EXEC GetForCanvassPerItem @UserId,@ItemNo",
|
|
new SqlParameter("@UserId", CanvassDto.UserId),
|
|
new SqlParameter("@ItemNo", CanvassDto.ItemNo))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<ForCanvass>();
|
|
}
|
|
public async Task<List<ForCanvassFollowUp>> GetCanvassForFollowUp(CanvassDto canvassDto)
|
|
{
|
|
var allItems = await _dbContext.ForCanvassFollowUps
|
|
.FromSqlRaw($"EXEC GetCanvassForFollowUp")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<ForCanvassFollowUp>();
|
|
}
|
|
public async Task<List<AllForCanvass>> GetAllForCanvass()
|
|
{
|
|
var allForCanvass = await _dbContext.AllForCanvasses
|
|
.FromSqlRaw("EXEC GetAllForCanvass").ToListAsync();
|
|
return allForCanvass ?? new List<AllForCanvass>();
|
|
}
|
|
#endregion
|
|
#region Post Put
|
|
public async Task<Result<SupplierResponse>> PostPutSupplierAsync(SupplierRequest request, CancellationToken ct)
|
|
{
|
|
var strategy = _dbContext.Database.CreateExecutionStrategy();
|
|
|
|
return await strategy.ExecuteAsync(async () =>
|
|
{
|
|
await using var transaction = await _dbContext.Database.BeginTransactionAsync(ct);
|
|
|
|
try
|
|
{
|
|
var supplier = await _dbContext.Suppliers
|
|
.FirstOrDefaultAsync(s => s.SupplierId == request.SupplierId, ct);
|
|
|
|
if (supplier == null)
|
|
{
|
|
supplier = _mapper.Map<Suppliers>(request);
|
|
await _dbContext.AddAsync(supplier, ct);
|
|
await _dbContext.SaveChangesAsync(ct);
|
|
}
|
|
|
|
await PostSupplierItems(supplier.SupplierId, request.ItemNo, ct);
|
|
|
|
await _dbContext.SaveChangesAsync(ct);
|
|
|
|
await transaction.CommitAsync(ct);
|
|
|
|
return Result<SupplierResponse>.Success(_mapper.Map<SupplierResponse>(supplier));
|
|
}
|
|
catch (DbUpdateException ex)
|
|
{
|
|
await transaction.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
});
|
|
}
|
|
private async Task PostSupplierItems(int supplierId, long itemNo, CancellationToken ct)
|
|
{
|
|
var exists = await _dbContext.SupplierItems.AnyAsync(
|
|
s => s.SupplierId == supplierId && s.ItemNo == itemNo && s.IsActive,
|
|
ct);
|
|
|
|
if (!exists)
|
|
{
|
|
await _dbContext.SupplierItems.AddAsync(new SupplierItems
|
|
{
|
|
SupplierId = supplierId,
|
|
ItemNo = itemNo,
|
|
IsActive = true
|
|
}, ct);
|
|
}
|
|
}
|
|
public async Task<SupplierResponse> PostApprovedSupp(CanvassDto CanvassDto)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PostApprovedSupp @UserId,@CanvassId,@ItemNo",
|
|
new SqlParameter("@UserId", CanvassDto.UserId),
|
|
new SqlParameter("@CanvassId", CanvassDto.CanvassId != null ? CanvassDto.CanvassId : 0L),
|
|
new SqlParameter("@ItemNo", CanvassDto.ItemNo != null ? CanvassDto.ItemNo : 0L));
|
|
|
|
return new SupplierResponse();
|
|
}
|
|
public async Task<CanvassDetail> PostSuggestedSupp(CanvassDto CanvassDto)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PostSuggestedSupp @UserId,@CanvassDetailId,@ItemNo,@SupplierId,@CanvassId",
|
|
new SqlParameter("@UserId", CanvassDto.UserId),
|
|
new SqlParameter("@CanvassDetailId", CanvassDto.CanvassDetailId),
|
|
new SqlParameter("@ItemNo", CanvassDto.ItemNo),
|
|
new SqlParameter("@SupplierId", CanvassDto.SupplierId),
|
|
new SqlParameter("@CanvassId", CanvassDto.CanvassId));
|
|
return new CanvassDetail();
|
|
}
|
|
public async Task<SupplierResponse> PostTaggingSupplier(CanvassDto CanvassDto)
|
|
{
|
|
var (messCode, message) = CreateOutputParams();
|
|
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync($"EXEC PostPutSupplier @SupplierId, @ItemNo, @IsActive, @UserId, @SupplierName, " +
|
|
"@EmailAddress, @Address, @ContactNo, @ContactPerson,@IsTagging, @MessCode OUTPUT, @Message OUTPUT",
|
|
new SqlParameter("@SupplierId", CanvassDto.SupplierId != null ? CanvassDto.SupplierId : 0L),
|
|
new SqlParameter("@ItemNo", CanvassDto.ItemNo),
|
|
new SqlParameter("@IsActive", CanvassDto.IsActive),
|
|
new SqlParameter("@UserId", CanvassDto.UserId),
|
|
new SqlParameter("@SupplierName", CanvassDto.SupplierName),
|
|
new SqlParameter("@EmailAddress", CanvassDto.EmailAddress),
|
|
new SqlParameter("@Address", CanvassDto.Address ?? "NONE"),
|
|
new SqlParameter("@ContactNo", CanvassDto.ContactNo ?? "NONE"),
|
|
new SqlParameter("@ContactPerson", CanvassDto.ContactPerson ?? "NONE"),
|
|
new SqlParameter("@IsTagging", CanvassDto.IsTagging),
|
|
messCode,
|
|
message);
|
|
|
|
if ((byte)messCode.Value == 0)
|
|
{
|
|
throw new Exception(message.Value.ToString());
|
|
}
|
|
return new SupplierResponse();
|
|
}
|
|
public async Task<SupplierResponse> PostPutItemTagging(CanvassDto canvassDto)
|
|
{
|
|
var (messCode, message) = CreateOutputParams();
|
|
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync($"EXEC PostPutItemTagging @SupplierId, @ItemNo, @IsActive, @UserId," +
|
|
"@MessCode OUTPUT, @Message OUTPUT",
|
|
new SqlParameter("@SupplierId", canvassDto.SupplierId != null ? canvassDto.SupplierId : 0L),
|
|
new SqlParameter("@ItemNo", canvassDto.ItemNo),
|
|
new SqlParameter("@IsActive", canvassDto.IsActive),
|
|
new SqlParameter("@UserId", canvassDto.UserId),
|
|
messCode,
|
|
message);
|
|
|
|
if ((byte)messCode.Value == 0)
|
|
{
|
|
throw new Exception(message.Value.ToString());
|
|
}
|
|
return new SupplierResponse();
|
|
}
|
|
public async Task<SupplierResponse> PostPutSupplier(CanvassDto CanvassDto)
|
|
{
|
|
var messCode = new SqlParameter("@MessCode", SqlDbType.TinyInt) { Direction = ParameterDirection.Output };
|
|
var message = new SqlParameter("@Message", SqlDbType.VarChar, 8000) { Direction = ParameterDirection.Output };
|
|
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync($"EXEC PostPutSupplier @SupplierId, @ItemNo, @IsActive, @UserId, @SupplierName, " +
|
|
"@EmailAddress, @Address, @ContactNo, @ContactPerson,@IsTagging, @MessCode OUTPUT, @Message OUTPUT",
|
|
new SqlParameter("@SupplierId", CanvassDto.SupplierId != null ? CanvassDto.SupplierId : 0L),
|
|
new SqlParameter("@ItemNo", CanvassDto.ItemNo),
|
|
new SqlParameter("@IsActive", CanvassDto.IsActive),
|
|
new SqlParameter("@UserId", CanvassDto.UserId),
|
|
new SqlParameter("@SupplierName", CanvassDto.SupplierName),
|
|
new SqlParameter("@EmailAddress", CanvassDto.EmailAddress),
|
|
new SqlParameter("@Address", CanvassDto.Address ?? "NONE"),
|
|
new SqlParameter("@ContactNo", CanvassDto.ContactNo ?? "NONE"),
|
|
new SqlParameter("@ContactPerson", CanvassDto.ContactPerson ?? "NONE"),
|
|
new SqlParameter("@IsTagging", CanvassDto.IsTagging),
|
|
messCode,
|
|
message);
|
|
|
|
if ((byte)messCode.Value == 0)
|
|
{
|
|
throw new Exception(message.Value.ToString());
|
|
}
|
|
|
|
return new SupplierResponse();
|
|
}
|
|
public async Task<SupplierResponse> PostPutMySupplier(CanvassDto canvassDto)
|
|
{
|
|
var messCode = new SqlParameter("@MessCode", SqlDbType.TinyInt) { Direction = ParameterDirection.Output };
|
|
var message = new SqlParameter("@Message", SqlDbType.VarChar, 8000) { Direction = ParameterDirection.Output };
|
|
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync($"EXEC PostPutMySupplier @SupplierId, @ItemNo, @IsActive, @UserId, @SupplierName," +
|
|
"@EmailAddress, @Address, @ContactNo, @ContactPerson,@TinNo,@LeadTime,@CurrencyId,@VatInc,@PaymentTermsId," +
|
|
"@MessCode OUTPUT, @Message OUTPUT",
|
|
new SqlParameter("@SupplierId", canvassDto.SupplierId != null ? canvassDto.SupplierId : 0L),
|
|
new SqlParameter("@ItemNo", canvassDto.ItemNo),
|
|
new SqlParameter("@IsActive", canvassDto.IsActive),
|
|
new SqlParameter("@UserId", canvassDto.UserId),
|
|
new SqlParameter("@SupplierName", canvassDto.SupplierName),
|
|
new SqlParameter("@EmailAddress", canvassDto.EmailAddress),
|
|
new SqlParameter("@Address", canvassDto.Address ?? "NONE"),
|
|
new SqlParameter("@ContactNo", canvassDto.ContactNo ?? "NONE"),
|
|
new SqlParameter("@ContactPerson", canvassDto.ContactPerson ?? "NONE"),
|
|
new SqlParameter("@TinNo", canvassDto.TinNo ?? "NONE"),
|
|
new SqlParameter("@LeadTime", canvassDto.LeadTime ?? "NONE"),
|
|
new SqlParameter("@CurrencyId", canvassDto.CurrencyId),
|
|
new SqlParameter("@VatInc", canvassDto.VatInc),
|
|
new SqlParameter("@PaymentTermsId", canvassDto.PaymentTermsId),
|
|
messCode,
|
|
message);
|
|
|
|
if ((byte)messCode.Value == 0)
|
|
{
|
|
throw new Exception(message.Value.ToString());
|
|
}
|
|
|
|
return new SupplierResponse();
|
|
}
|
|
public async Task<CanvassDetail> PutSuppUnitPrice(CanvassDto CanvassDto)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync($"EXEC PutSuppUnitPrice @UserId, @CanvassDetailId, @UnitPrice",
|
|
new SqlParameter("@CanvassDetailId", CanvassDto.CanvassDetailId != null ? CanvassDto.CanvassDetailId : 0L),
|
|
new SqlParameter("@UserId", CanvassDto.UserId),
|
|
new SqlParameter("@UnitPrice", CanvassDto.UnitPrice));
|
|
return new CanvassDetail();
|
|
}
|
|
public async Task<ForCanvassFollowUp> PutSupplierCanvass(long canvassSupplierId)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PutCanvassForFollowUp @CanvassSupplierId",
|
|
new SqlParameter("@CanvassSupplierId", canvassSupplierId));
|
|
return new ForCanvassFollowUp();
|
|
}
|
|
public async Task<CanvassDetail> PutSuppBidDetails(CanvassDto canvassDto)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync($"EXEC PutSuppBidDetails @UserId,@CanvassDetailId,@PRDetailsId,@UnitPrice,@Terms," +
|
|
$"@ItemName,@Specification,@Qty,@DeliveryDate,@CurrencyId,@Remarks,@Manufacturer,@UOMName,@IsReset",
|
|
new SqlParameter("@UserId", canvassDto.UserId),
|
|
new SqlParameter("@CanvassDetailId", canvassDto.CanvassDetailId),
|
|
new SqlParameter("@PRDetailsId", canvassDto.PRDetailsId),
|
|
new SqlParameter("@UnitPrice", canvassDto.UnitPrice),
|
|
new SqlParameter("@Terms", canvassDto.Terms),
|
|
new SqlParameter("@ItemName", canvassDto.ItemName),
|
|
new SqlParameter("@Specification", canvassDto.Specification),
|
|
new SqlParameter("@Qty", canvassDto.Qty),
|
|
new SqlParameter("@DeliveryDate", canvassDto.CommitmentDate),
|
|
new SqlParameter("@CurrencyId", canvassDto.CurrencyId),
|
|
new SqlParameter("@Remarks", canvassDto.Remarks),
|
|
new SqlParameter("@Manufacturer", canvassDto.Manufacturer),
|
|
new SqlParameter("@UOMName", canvassDto.UOMName),
|
|
new SqlParameter("@IsReset", canvassDto.IsReset));
|
|
return new CanvassDetail();
|
|
}
|
|
public async Task<CanvassSupplier> UnlockFormLink(CanvassDto canvassDto)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC UnlockFormLink @UserId,@CanvassSupplierId",
|
|
new SqlParameter("@UserId", canvassDto.UserId),
|
|
new SqlParameter("@CanvassSupplierId", canvassDto.CanvassSupplierId));
|
|
return new CanvassSupplier();
|
|
}
|
|
public async Task<bool> SendRFQ(SupplierEmailRequest supplierEmailRequest)
|
|
{
|
|
var baseTemplate = EMailTemplate("Content\\SMTPEmailContent", "SendToSupplier.cshtml");
|
|
|
|
var message = new StringBuilder(baseTemplate);
|
|
|
|
message.Replace("@ViewBag.FormLink", Convert.ToString(supplierEmailRequest.FormLink + supplierEmailRequest.Token));
|
|
message.Replace("@ViewBag.Supplier", supplierEmailRequest.SupplierName);
|
|
message.Replace("@ViewBag.Signature", supplierEmailRequest.Purchaser);
|
|
|
|
var messageDetails = new EmailMessageDetailsVM
|
|
{
|
|
Recipient = supplierEmailRequest.Recipient,
|
|
Message = message.ToString(),
|
|
Subject = supplierEmailRequest.Subject,
|
|
CC = supplierEmailRequest.CC,
|
|
IsSuccess = supplierEmailRequest.IsSuccess,
|
|
SenderEmail = supplierEmailRequest.UserName,
|
|
DisplayName = "llipurchasing.com",
|
|
NewPassword = supplierEmailRequest.Password,
|
|
OutGoingPort = supplierEmailRequest.OutGoingPort,
|
|
Server = supplierEmailRequest.Server,
|
|
UserName = supplierEmailRequest.UserName,
|
|
IsCanvass= supplierEmailRequest.IsCanvass,
|
|
AttachPath=supplierEmailRequest.AttachPath
|
|
};
|
|
|
|
if (await _smptHelper.SendEmailAsync(messageDetails))
|
|
return true;
|
|
return false;
|
|
}
|
|
public string EMailTemplate(string relativePath, string emailTemplate)
|
|
{
|
|
string basePath = AppContext.BaseDirectory;
|
|
string templateFolderPath = Path.Combine(basePath, relativePath);
|
|
string templateFilePath = Path.Combine(templateFolderPath, emailTemplate);
|
|
|
|
if (System.IO.File.Exists(templateFilePath))
|
|
{
|
|
return System.IO.File.ReadAllText(templateFilePath);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"File not found: {templateFilePath}");
|
|
return "Template file not found";
|
|
}
|
|
}
|
|
public async Task DeleteAsync(long pRDetailsId, CancellationToken ct)
|
|
{
|
|
await _dbContext.ForAISearchingTaggings
|
|
.Where(p => p.PRDetailsId == pRDetailsId)
|
|
.ExecuteDeleteAsync(ct);
|
|
}
|
|
|
|
public async Task<Result<StartCanvassResponse>> StartCanvass(CanvassVM request, CancellationToken ct)
|
|
{
|
|
var detailIds = request.ForSupplierSearchList?.PRDetailsId;
|
|
if (detailIds == null || !detailIds.Any())
|
|
return Result<StartCanvassResponse>.Failure("No items provided.");
|
|
|
|
// 1. Get IDs that already exist in one call to avoid the loop-check
|
|
var existingIds = await _dbContext.ForAISearchingTaggings
|
|
.Where(f => detailIds.Contains(f.PRDetailsId))
|
|
.Select(f => f.PRDetailsId)
|
|
.ToListAsync(ct);
|
|
|
|
var newTags = new List<ForAISearchingTagging>();
|
|
var idsToUpdate = new List<long>();
|
|
|
|
for (int i = 0; i < detailIds.Count; i++)
|
|
{
|
|
var currentId = detailIds[i];
|
|
if (existingIds.Contains(currentId)) continue;
|
|
|
|
newTags.Add(new ForAISearchingTagging
|
|
{
|
|
PRDetailsId = currentId,
|
|
PRNo = request.ForSupplierSearchList.PRNo[i],
|
|
ItemNo = request.ForSupplierSearchList.ItemNo[i],
|
|
ItemName = request.ForSupplierSearchList.ItemName[i],
|
|
ItemDescription = request.ForSupplierSearchList.ItemDescription[i],
|
|
IsInternational = request.IsInternational,
|
|
FullName = request.FullName,
|
|
UserId = request.UserId
|
|
});
|
|
|
|
idsToUpdate.Add(currentId);
|
|
}
|
|
|
|
if (newTags.Any())
|
|
{
|
|
// 2. Bulk Add
|
|
await _dbContext.ForAISearchingTaggings.AddRangeAsync(newTags, ct);
|
|
await _dbContext.SaveChangesAsync(ct);
|
|
|
|
// 3. Bulk Update the flags AFTER saving the tags
|
|
await _dbContext.PRDetails
|
|
.Where(p => idsToUpdate.Contains(p.PRDetailsId))
|
|
.ExecuteUpdateAsync(s => s.SetProperty(p => p.IsSearched, true), ct);
|
|
}
|
|
|
|
return Result<StartCanvassResponse>.Success(new StartCanvassResponse { });
|
|
}
|
|
#endregion
|
|
}
|
|
}
|