410 lines
16 KiB
C#
410 lines
16 KiB
C#
using CPRNIMS.Domain.Contracts.Account;
|
|
using CPRNIMS.Domain.Contracts.Items;
|
|
using CPRNIMS.Infrastructure.Database;
|
|
using CPRNIMS.Infrastructure.Dto.Canvass;
|
|
using CPRNIMS.Infrastructure.Dto.Items;
|
|
using CPRNIMS.Infrastructure.Entities.Account;
|
|
using CPRNIMS.Infrastructure.Entities.Canvass;
|
|
using CPRNIMS.Infrastructure.Entities.Items;
|
|
using CPRNIMS.Infrastructure.Entities.Purchasing;
|
|
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CPRNIMS.Domain.Services.Items
|
|
{
|
|
public class Item : IItem
|
|
{
|
|
private readonly NonInventoryDbContext _dbContext;
|
|
public Item(NonInventoryDbContext dbContext) {
|
|
_dbContext = dbContext;
|
|
}
|
|
public async Task<ItemCodeDto> PostPutItem(ItemCodeDto itemDto)
|
|
{
|
|
try
|
|
{
|
|
var messCodeParam = new SqlParameter("@MessCode", SqlDbType.TinyInt)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
var messageParam = new SqlParameter("@Message", SqlDbType.VarChar, 500)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
var itemCodeParam = new SqlParameter("@ItemCode", SqlDbType.BigInt)
|
|
{
|
|
Direction = ParameterDirection.Output
|
|
};
|
|
|
|
await _dbContext.Database.ExecuteSqlRawAsync(
|
|
"EXEC PostPutItem @ItemCodeId, @ItemName, @ItemDescription, @ItemCategoryId, @Status, @UserId, " +
|
|
"@ItemCode OUTPUT, @MessCode OUTPUT, @Message OUTPUT",
|
|
new SqlParameter("@ItemCodeId", itemDto.ItemCodeId != null ? itemDto.ItemCodeId : 0L),
|
|
new SqlParameter("@ItemName", itemDto.ItemName ?? (object)DBNull.Value),
|
|
new SqlParameter("@ItemDescription", itemDto.ItemDescription ?? (object)DBNull.Value),
|
|
new SqlParameter("@ItemCategoryId", itemDto.ItemCategoryId),
|
|
new SqlParameter("@Status", itemDto.Status != null ? itemDto.Status : 4),
|
|
new SqlParameter("@UserId", itemDto.UserId ?? (object)DBNull.Value),
|
|
itemCodeParam,
|
|
messCodeParam,
|
|
messageParam
|
|
);
|
|
|
|
var messCode = (byte)messCodeParam.Value;
|
|
var message = messageParam.Value?.ToString();
|
|
var itemCode = itemCodeParam.Value != DBNull.Value ? (long?)itemCodeParam.Value : null;
|
|
|
|
return new ItemCodeDto
|
|
{
|
|
ItemCodeId = Convert.ToInt64(itemCode),
|
|
ItemName = itemDto.ItemName,
|
|
ItemDescription = itemDto.ItemDescription,
|
|
ItemCategoryId = itemDto.ItemCategoryId,
|
|
Status = itemDto.Status,
|
|
UserId = itemDto.UserId
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// You could log ex.Message or rethrow with a custom message
|
|
throw new Exception("An error occurred while executing PostPutItem.", ex);
|
|
}
|
|
}
|
|
|
|
public async Task<Infrastructure.Entities.Items.Item> PutItemDetail(ItemDto itemDto)
|
|
{
|
|
try
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync($"EXEC PutItemDetail @ItemCodeId,@ItemLocalId,@ItemTypeId,@UOMId,@ItemColorId,@IsActive," +
|
|
$"@UserId,@ItemNo,@PRTypeId,@PackagingTypeId,@Qty,@ItemAttachId,@ItemAttachPath,@IsCommon,@ItemCategoryId," +
|
|
$"@RequestTypeId,@ItemName,@ItemDescription,@IsMDLD",
|
|
new SqlParameter("@ItemCodeId", itemDto.ItemCodeId != null ? itemDto.ItemCodeId : 0L),
|
|
new SqlParameter("@ItemLocalId", itemDto.ItemLocalId),
|
|
new SqlParameter("@ItemTypeId", itemDto.ItemTypeId),
|
|
new SqlParameter("@UOMId", itemDto.UOMId),
|
|
new SqlParameter("@ItemColorId", itemDto.ItemColorId),
|
|
new SqlParameter("@IsActive", itemDto.IsActive),
|
|
new SqlParameter("@UserId", itemDto.UserId),
|
|
new SqlParameter("@ItemNo", itemDto.ItemNo),
|
|
new SqlParameter("@PRTypeId", itemDto.PRTypeId),
|
|
new SqlParameter("@PackagingTypeId", itemDto.PackagingTypeId),
|
|
new SqlParameter("@Qty", itemDto.Qty),
|
|
new SqlParameter("@ItemAttachId", itemDto.ItemAttachId != null ? itemDto.ItemAttachId : 0L),
|
|
new SqlParameter("@ItemAttachPath", itemDto.ItemAttachPath ?? "N/A"),
|
|
new SqlParameter("@IsCommon", itemDto.IsCommon),
|
|
new SqlParameter("@ItemCategoryId", itemDto.ItemCategoryId),
|
|
new SqlParameter("@RequestTypeId", itemDto.RequestTypeId),
|
|
new SqlParameter("@ItemName", itemDto.ItemName),
|
|
new SqlParameter("@ItemDescription", itemDto.ItemDescription),
|
|
new SqlParameter("@IsMDLD", itemDto.IsMDLD));
|
|
|
|
return new Infrastructure.Entities.Items.Item();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
public async Task<List<ItemLocalization>> GetItemLocalization(ItemDto itemDto)
|
|
{
|
|
try
|
|
{
|
|
var localizations = await _dbContext.ItemLocalizations
|
|
.Where(ic => ic.IsActive == true &&
|
|
EF.Functions.Like(ic.ItemLocalName, $"%{itemDto.ItemLocalName}%"))
|
|
.Take(15)
|
|
.ToListAsync();
|
|
|
|
return localizations ?? new List<ItemLocalization>();
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
public async Task<List<ItemCategory>> GetItemCateg(ItemDto itemDto)
|
|
{
|
|
try
|
|
{
|
|
if(itemDto.ItemCategoryId ==0 || itemDto.ItemCategoryId == null)
|
|
{
|
|
var categories = await _dbContext.ItemCategories
|
|
.Where(ic => ic.IsActive == true)
|
|
.ToListAsync();
|
|
return categories ?? new List<ItemCategory>();
|
|
}
|
|
else
|
|
{
|
|
var categories = await _dbContext.ItemCategories
|
|
.Where(ic => ic.IsActive == true && ic.ItemCategoryId == itemDto.ItemCategoryId)
|
|
.ToListAsync();
|
|
return categories ?? new List<ItemCategory>();
|
|
}
|
|
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<Infrastructure.Entities.Items.Item>> GetItemDetail(ItemDto itemDto)
|
|
{
|
|
try
|
|
{
|
|
var allItems = await _dbContext.Items
|
|
.FromSqlRaw($"EXEC GetItemDetail @ItemCodeId,@UserId",
|
|
new SqlParameter("@ItemCodeId", itemDto.ItemCodeId),
|
|
new SqlParameter("@UserId", itemDto.UserId))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<Infrastructure.Entities.Items.Item>();
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<ItemList>> GetItemList(ItemCodeDto itemCode)
|
|
{
|
|
try
|
|
{
|
|
var allItems = await _dbContext.ItemList
|
|
.FromSqlRaw($"EXEC GetItemList @UserId = '{itemCode.UserId}'")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<ItemList>();
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<ItemColor>> GetItemColor(ItemDto itemDto)
|
|
{
|
|
try
|
|
{
|
|
var colors = await _dbContext.ItemColors
|
|
.Where(ic => EF.Functions.Like(ic.ItemColorName, $"%{itemDto.ItemColorName}%"))
|
|
.Take(5)
|
|
.ToListAsync();
|
|
|
|
return colors ?? new List<ItemColor>();
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<UnitOfMessure>> GetItemUOM(ItemDto itemDto)
|
|
{
|
|
try
|
|
{
|
|
var uoms = await _dbContext.UnitOfMessures
|
|
.Where(ic => ic.IsActive == true &&
|
|
EF.Functions.Like(ic.UOMName, $"%{itemDto.UOMName}%"))
|
|
.Take(150)
|
|
.ToListAsync();
|
|
|
|
return uoms ?? new List<UnitOfMessure>();
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<ItemAttachement> PostPutItemPath(ItemDto itemDto)
|
|
{
|
|
try
|
|
{
|
|
var isExist = await _dbContext.ItemAttachements
|
|
.FirstOrDefaultAsync(ia => ia.IsActive == true
|
|
&& ia.ItemNo == itemDto.ItemNo);
|
|
if(isExist != null)
|
|
{
|
|
isExist.ItemAttachPath = itemDto.ItemAttachPath;
|
|
await _dbContext.SaveChangesAsync();
|
|
return new ItemAttachement();
|
|
}
|
|
else
|
|
{
|
|
return new ItemAttachement();
|
|
}
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
public async Task<ItemCart> PostPutItemCart(ItemDto itemDto)
|
|
{
|
|
try
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PostPutItemCart @ItemCartId,@ItemNo,@UserId,@UOMId,@ItemColorId,@ItemCategoryId,@PackagingTypeId,@ItemAttachId,@ItemLocalId",
|
|
new SqlParameter("@ItemCartId", itemDto.ItemCartId != null ? itemDto.ItemCartId : 0L),
|
|
new SqlParameter("@ItemNo", itemDto.ItemNo != null ? itemDto.ItemNo : 0L),
|
|
new SqlParameter("@UOMId", itemDto.UOMId),
|
|
new SqlParameter("@ItemColorId", itemDto.ItemColorId),
|
|
new SqlParameter("@ItemCategoryId", itemDto.ItemCategoryId),
|
|
new SqlParameter("@PackagingTypeId", itemDto.PackagingTypeId),
|
|
new SqlParameter("@ItemAttachId", itemDto.ItemAttachId),
|
|
new SqlParameter("@UserId", itemDto.UserId),
|
|
new SqlParameter("@ItemLocalId", itemDto.ItemLocalId));
|
|
return new ItemCart();
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<List<ItemCart>> GetItemCart(ItemDto itemDto)
|
|
{
|
|
try
|
|
{
|
|
var allItems = await _dbContext.ItemCarts
|
|
.FromSqlRaw($"EXEC GetItemCart @UserId,@RequestTypeId,@IsCount",
|
|
new SqlParameter("@UserId", itemDto.UserId),
|
|
new SqlParameter("@RequestTypeId", itemDto.RequestTypeId),
|
|
new SqlParameter("@IsCount", itemDto.IsCount))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<ItemCart>();
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<ItemCart> PostPurchRequest(ItemDto itemDto)
|
|
{
|
|
try
|
|
{
|
|
if(itemDto.RequestTypeId == 1)//Internal
|
|
{
|
|
itemDto.Status = 2;
|
|
itemDto.IsApproved = 2;
|
|
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PostPutReqItems @UserId, @RequestItemId, @ItemNo, @QtyRequest,@Status,@IsApproved,@QtyReceived,@LotId",
|
|
new SqlParameter("@UserId", itemDto.UserId),
|
|
new SqlParameter("@ItemNo", itemDto.ItemNo),
|
|
new SqlParameter("@RequestItemId", itemDto.RequestItemId != null ? itemDto.RequestItemId : 0L),
|
|
new SqlParameter("@QtyRequest", itemDto.Qty),
|
|
new SqlParameter("@Status", itemDto.Status),
|
|
new SqlParameter("@IsApproved", itemDto.IsApproved),
|
|
new SqlParameter("@QtyReceived", itemDto.QtyReceived),
|
|
new SqlParameter("@LotId", itemDto.LotId));
|
|
}
|
|
else
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PostPurchRequest @ItemCartId, @IsActive, @UserId,@ItemCount,@PRNo,@DateNeeded,@Qty,@ChargeTo,@Remarks",
|
|
new SqlParameter("@ItemCartId", itemDto.ItemCartId != null ? itemDto.ItemCartId : 0L),
|
|
new SqlParameter("@IsActive", 1),
|
|
new SqlParameter("@UserId", itemDto.UserId),
|
|
new SqlParameter("@ItemCount", itemDto.ItemCount),
|
|
new SqlParameter("@PRNo", itemDto.PRNo),
|
|
new SqlParameter("@DateNeeded", itemDto.DateNeeded),
|
|
new SqlParameter("@Qty", itemDto.Qty),
|
|
new SqlParameter("@ChargeTo", itemDto.ChargeTo),
|
|
new SqlParameter("@Remarks", itemDto.Remarks));
|
|
}
|
|
return new ItemCart();
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
public async Task<long> GetPRNo()
|
|
{
|
|
try
|
|
{
|
|
// Query the PRs table
|
|
var latestPR = await _dbContext.PRs
|
|
.OrderByDescending(ic => ic.PRNo) // Sort by PRNo in descending order
|
|
.FirstOrDefaultAsync(); // Retrieve the first record
|
|
|
|
if (latestPR != null)
|
|
{
|
|
return latestPR.PRNo;
|
|
}
|
|
else
|
|
{
|
|
return 0; // Example: Return 0 if no records found
|
|
}
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
// Handle the exception (log, rethrow, etc.)
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
Task<ItemCart> IItem.PostPutItemPath(ItemDto itemDto)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public async Task <List<Departments>> GetDepartment(ItemCodeDto itemCode)
|
|
{
|
|
try
|
|
{
|
|
var departments = await _dbContext.Departments
|
|
.Where(d => d.IsActive == true)
|
|
.ToListAsync();
|
|
return departments;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Log the exception or handle it as needed
|
|
throw;
|
|
}
|
|
}
|
|
public async Task<List<NotifUserKey>> GetNotifUserKey(ItemDto itemDto)
|
|
{
|
|
try
|
|
{
|
|
var allItems = await _dbContext.NotifUserKeys
|
|
.FromSqlRaw($"EXEC GetNotifUserKey @UserId,@Status,@PRDetailsId,@PRNo",
|
|
new SqlParameter("@UserId", itemDto.UserId),
|
|
new SqlParameter("@Status", itemDto.Status),
|
|
new SqlParameter("@PRDetailsId", itemDto.PRDetailsId),
|
|
new SqlParameter("@PRNo", itemDto.PRNo))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<NotifUserKey>();
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
ex.ToString();
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|