821 lines
36 KiB
C#
821 lines
36 KiB
C#
using CPRNIMS.Domain.Contracts.PO;
|
|
using CPRNIMS.Infrastructure.Database;
|
|
using CPRNIMS.Infrastructure.Dto.PO;
|
|
using CPRNIMS.Infrastructure.Entities.Canvass;
|
|
using CPRNIMS.Infrastructure.Entities.Common;
|
|
using CPRNIMS.Infrastructure.Entities.LocalDb.NonInvent;
|
|
using CPRNIMS.Infrastructure.Entities.PO;
|
|
using CPRNIMS.Infrastructure.Helper;
|
|
using CPRNIMS.Infrastructure.ViewModel.Common;
|
|
using CPRNIMS.Infrastructure.ViewModel.PO;
|
|
using Dapper;
|
|
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Data;
|
|
using System.Text;
|
|
using static CPRNIMS.Domain.Services.OutputParamMessage;
|
|
|
|
namespace CPRNIMS.Domain.Services.PO
|
|
{
|
|
public class PurchaseOrder : IPurchaseOrder
|
|
{
|
|
private readonly NonInventoryDbContext _dbContext;
|
|
private readonly PurchLocalDbContext _dbLocalContext;
|
|
private readonly SMTPHelper _smptHelper;
|
|
public PurchaseOrder(NonInventoryDbContext dbContext, PurchLocalDbContext purchLocalDb, SMTPHelper smptHelper)
|
|
{
|
|
_dbContext = dbContext;
|
|
_dbLocalContext = purchLocalDb;
|
|
_smptHelper = smptHelper;
|
|
}
|
|
#region Get
|
|
public async Task<POFormData> GetPOFormDataAsync(long? poId)
|
|
{
|
|
// Reuse the connection from your existing DbContext
|
|
var conn = _dbContext.Database.GetDbConnection();
|
|
|
|
var param = new DynamicParameters();
|
|
param.Add("@POId", poId, DbType.Int64);
|
|
|
|
// Open only if not already open (EF Core may have it open already)
|
|
if (conn.State != ConnectionState.Open)
|
|
await conn.OpenAsync();
|
|
|
|
using var multi = await conn.QueryMultipleAsync(
|
|
"GetExistingPOFormData",
|
|
param,
|
|
commandType: CommandType.StoredProcedure
|
|
);
|
|
|
|
return new POFormData
|
|
{
|
|
Header = await multi.ReadFirstOrDefaultAsync<POHeader>(),
|
|
LineItems = (await multi.ReadAsync<POLineItem>()).ToList(),
|
|
Charges = (await multi.ReadAsync<POCharges>()).ToList(),
|
|
DocsRequired = (await multi.ReadAsync<DocRequired>()).ToList()
|
|
};
|
|
}
|
|
public async Task<List<Incoterm>> GetIncoterms(PODto itemDto)
|
|
{
|
|
return await _dbContext.Incoterms.ToListAsync();
|
|
}
|
|
public async Task<List<OtherCharges>> GetOtherCharges(PODto itemDto)
|
|
{
|
|
var charges = await _dbContext.OtherCharges.ToListAsync();
|
|
return charges;
|
|
}
|
|
public async Task<List<Suppliers>> GetSuppliers(PODto itemDto)
|
|
{
|
|
var allItems = await _dbContext.Suppliers
|
|
.FromSqlRaw($"EXEC GetSuppliers @UserId,@SupplierName",
|
|
new SqlParameter("@UserId", itemDto.UserId),
|
|
new SqlParameter("@SupplierName", itemDto.SupplierName))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<Suppliers>();
|
|
}
|
|
public async Task<List<PRWOCanvass>> GetPRWOCanvass(PODto itemDto)
|
|
{
|
|
var allItems = await _dbContext.PRWOCanvasses
|
|
.FromSqlRaw($"EXEC GetPRWOCanvass @UserId,@PRDetailsId",
|
|
new SqlParameter("@UserId", itemDto.UserId),
|
|
new SqlParameter("@PRDetailsId", itemDto.PRDetailsId))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<PRWOCanvass>();
|
|
}
|
|
public async Task<List<CreatedPO>> GetCreatedPO(PODto pODto)
|
|
{
|
|
var createdPOs = await _dbContext.CreatedPOs
|
|
.FromSqlRaw($"EXEC GetCreatedPO @UserId",
|
|
new SqlParameter("@UserId", pODto.UserId))
|
|
.ToListAsync();
|
|
|
|
return createdPOs ?? new List<CreatedPO>();
|
|
}
|
|
public async Task<List<CreatedPO>> GetMyCreatedPO(PODto pODto)
|
|
{
|
|
var createdPOs = await _dbContext.CreatedPOs
|
|
.FromSqlRaw($"EXEC GetMyCreatedPO @UserId",
|
|
new SqlParameter("@UserId", pODto.UserId))
|
|
.ToListAsync();
|
|
|
|
return createdPOs ?? new List<CreatedPO>();
|
|
}
|
|
public async Task<List<ApprovedPO>> GetApprovedPO(PODto PODto)
|
|
{
|
|
var allItems = await _dbContext.ApprovedPOs
|
|
.FromSqlRaw($"EXEC GetApprovedPO @UserId ,@IsArchived",
|
|
new SqlParameter("@UserId", PODto.UserId),
|
|
new SqlParameter("@IsArchived", PODto.IsArchived))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<ApprovedPO>();
|
|
}
|
|
public async Task<List<ItemListForPO>> GetApprovedPOPerEmail(PODto PODto)
|
|
{
|
|
var allItems = await _dbContext.ItemListForPOs
|
|
.FromSqlRaw($"EXEC GetApprovedPOPerEmail @UserId,@EmailAddress,@POTypeId,@PONo,@IsArchived",
|
|
new SqlParameter("@UserId", PODto.UserId),
|
|
new SqlParameter("@EmailAddress", PODto.EmailAddress),
|
|
new SqlParameter("@POTypeId", PODto.POTypeId),
|
|
new SqlParameter("@PONo", PODto.PONo),
|
|
new SqlParameter("@IsArchived", PODto.IsArchived))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<ItemListForPO>();
|
|
}
|
|
public async Task<List<CreatedPOPerSupId>> GetCreatedPOPerSupId(PODto pODto)
|
|
{
|
|
var allItems = await _dbContext.CreatedPOPerSupIds
|
|
.FromSqlRaw("EXEC GetCreatedPOPerSupId @UserId,@SupplierId,@POTypeId,@PONo,@IsManual",
|
|
new SqlParameter("@UserId", pODto.UserId),
|
|
new SqlParameter("@SupplierId", pODto.SupplierId),
|
|
new SqlParameter("@POTypeId", pODto.POTypeId),
|
|
new SqlParameter("@PONo", pODto.PONo),
|
|
new SqlParameter("@IsManual", pODto.IsManual))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<CreatedPOPerSupId>();
|
|
}
|
|
public async Task<List<DocRequired>> GetDocRequired(PODto pODto)
|
|
{
|
|
var requiredDocs = await _dbContext.DocRequireds.ToListAsync();
|
|
return requiredDocs ?? new List<DocRequired>();
|
|
}
|
|
public async Task<List<BiddingApproval>> GetForBiddingApproval(PODto PODto)
|
|
{
|
|
var allItems = await _dbContext.BiddingApprovals
|
|
.FromSqlRaw($"EXEC GetForBiddingApproval @UserId",
|
|
new SqlParameter("@UserId", PODto.UserId))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<BiddingApproval>();
|
|
}
|
|
public async Task<List<ForPO>> GetForPO(PODto PODto)
|
|
{
|
|
var allItems = await _dbContext.ForPOs
|
|
.FromSqlRaw($"EXEC GetForPO @UserId",
|
|
new SqlParameter("@UserId", PODto.UserId))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<ForPO>();
|
|
}
|
|
public async Task<List<ForPOApproval>> GetForPOApproval(PODto PODto)
|
|
{
|
|
var allItems = await _dbContext.ForPOApprovals
|
|
.FromSqlRaw($"EXEC GetForPOApproval @UserId = '{PODto.UserId}'")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<ForPOApproval>();
|
|
}
|
|
public async Task<List<Infrastructure.Entities.PO.PurchaseOrder>>
|
|
GetForPOApprovalByPRNo(PODto PODto)
|
|
{
|
|
var allItems = await _dbContext.PurchaseOrders
|
|
.FromSqlRaw($"EXEC GetForPOApprovalByPRNo @UserId = '{PODto.UserId}',@PRNo = '{PODto.PRNo}'")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<Infrastructure.Entities.PO.PurchaseOrder>();
|
|
}
|
|
public async Task<List<ItemListForPO>> GetForPOPerSuppEmail(PODto PODto)
|
|
{
|
|
var allItems = await _dbContext.ItemListForPOs
|
|
.FromSqlRaw($"EXEC GetForPOPerSuppEmail @UserId = '{PODto.UserId}',@EmailAddress = '{PODto.EmailAddress}',@POTypeId = '{PODto.POTypeId}',@PONo = '{PODto.PONo}',@IsArchived = '{PODto.IsArchived}'")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<ItemListForPO>();
|
|
}
|
|
public async Task<List<CentralPONo>> GetLatestPO(PODto pODto)
|
|
{
|
|
var allItems = await _dbContext.CentralPONos
|
|
.FromSqlRaw($"EXEC GetLatestPO")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<CentralPONo>();
|
|
}
|
|
public async Task<List<SystemSettings>> GetLatestPO2(PODto pODto)
|
|
{
|
|
var latestPO = await _dbLocalContext.SystemSettings.ToListAsync();
|
|
return latestPO ?? new List<SystemSettings>();
|
|
}
|
|
|
|
private bool isUpdated;
|
|
private string formattedPONumber;
|
|
public async Task<string> GetLatestPOById(PODto pODto)
|
|
{
|
|
if (pODto == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(pODto));
|
|
}
|
|
|
|
// Retrieve the latest PONo
|
|
var po = await _dbLocalContext.SystemSettings.SingleOrDefaultAsync();
|
|
|
|
if (po == null)
|
|
{
|
|
throw new InvalidOperationException("System settings not found.");
|
|
}
|
|
|
|
// Initialize variables
|
|
int latestPONumber;
|
|
|
|
// Determine which PO number to use based on POTypeId
|
|
string currentPONo = pODto.POTypeId switch
|
|
{
|
|
1 => po.PONoVatInc, // SI
|
|
2 => po.PONoVatEx, // DR
|
|
3 => po.IPONoVatInc, // IMPORT
|
|
_ => throw new ArgumentException("Invalid POTypeId.")
|
|
};
|
|
|
|
// Attempt to convert PONo to integer, default to 0 if conversion fails
|
|
if (!int.TryParse(currentPONo, out latestPONumber))
|
|
{
|
|
latestPONumber = 0;
|
|
}
|
|
|
|
// Increment the PO number
|
|
latestPONumber += 1;
|
|
|
|
// Format the new PO number based on POTypeId
|
|
formattedPONumber = pODto.POTypeId switch
|
|
{
|
|
1 => $"00{latestPONumber}", // Prefix "00" for SI
|
|
2 => latestPONumber.ToString(), // No prefix for DR
|
|
3 => $"10-{latestPONumber}", // Prefix "10-" for IMPORT
|
|
_ => throw new ArgumentException("Invalid POTypeId.")
|
|
};
|
|
|
|
// Assign the new PONo back to DTO
|
|
pODto.PONo = latestPONumber.ToString();
|
|
|
|
// Call the update function
|
|
PutLocalCentralPo(pODto);
|
|
isUpdated = true;
|
|
return formattedPONumber;
|
|
}
|
|
public async Task<List<PaymentTerm>> GetPaymentTerms(PODto pODto)
|
|
{
|
|
var allItems = await _dbContext.PaymentTerms
|
|
.Where(t => t.PaymentTerms.Contains(pODto.PaymentTerms))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<PaymentTerm>();
|
|
}
|
|
public async Task<List<PortOfDischarges>> GetPortOfDischarge(PODto pODto)
|
|
{
|
|
var allItems = await _dbContext.PortOfDischarges
|
|
.Where(t => t.PortOfDischarge.Contains(pODto.PortOfDischarge))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<PortOfDischarges>();
|
|
}
|
|
public async Task<List<RFQReference>> GetRFQ(PODto PODto)
|
|
{
|
|
var allItems = await _dbContext.RFQReferences
|
|
.FromSqlRaw($"EXEC GetRFQPerSupplier @SupplierId = '{PODto.SupplierId}',@UserId = '{PODto.UserId}'")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<RFQReference>();
|
|
}
|
|
public async Task<List<RFQPerSupplier>> GetSupplierBidById(PODto itemDto)
|
|
{
|
|
var allItems = await _dbContext.RFQPerSuppliers
|
|
.FromSqlRaw($"EXEC GetSupplierBidById @CanvassDetailId = '{itemDto.CanvassDetailId}'")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<RFQPerSupplier>();
|
|
}
|
|
public async Task<List<RFQPerSupplier>> GetSupplierBidByItem(PODto itemDto)
|
|
{
|
|
var allItems = await _dbContext.RFQPerSuppliers
|
|
.FromSqlRaw($"EXEC GetSupplierBidByItem @UserId,@Status,@ItemNo,@CanvassId,@IsHistory,@PRDetailsId",
|
|
new SqlParameter("@UserId", itemDto.UserId),
|
|
new SqlParameter("@Status", itemDto.Status),
|
|
new SqlParameter("@ItemNo", itemDto.ItemNo),
|
|
new SqlParameter("@CanvassId", itemDto.CanvassId),
|
|
new SqlParameter("@IsHistory", itemDto.IsHistory),
|
|
new SqlParameter("@PRDetailsId", itemDto.PRDetailsId))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<RFQPerSupplier>();
|
|
}
|
|
public async Task<List<PRPOSummaryCount>> GetPRPOSummaryReport(PODto itemDto)
|
|
{
|
|
var allItems = await _dbContext.PRPOSummaryCounts
|
|
.FromSqlRaw("EXEC GetPRPOSummaryCount @UserId,@From,@To",
|
|
new SqlParameter("@UserId", itemDto.UserId),
|
|
new SqlParameter("@From", itemDto.From),
|
|
new SqlParameter("@To", itemDto.To))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<PRPOSummaryCount>();
|
|
}
|
|
public async Task<List<PRPOSummaryItem>> GetPRPOSummaryItem(PODto itemDto)
|
|
{
|
|
var allItems = await _dbContext.PRPOSummaryItems
|
|
.FromSqlRaw("EXEC GetPRPOSummaryItem @UserId,@From,@To",
|
|
new SqlParameter("@UserId", itemDto.UserId),
|
|
new SqlParameter("@From", itemDto.From),
|
|
new SqlParameter("@To", itemDto.To))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<PRPOSummaryItem>();
|
|
}
|
|
public async Task<List<IndexCard>> GetIndexCard(PODto poDto)
|
|
{
|
|
var allItems = await _dbContext.IndexCards
|
|
.FromSqlRaw("EXEC GetIndexCard @ItemNo",
|
|
new SqlParameter("@ItemNo", poDto.ItemNo))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<IndexCard>();
|
|
}
|
|
public async Task<List<IncomingShipmentDto>> GetIncomingShipment(PODto itemDto)
|
|
{
|
|
var allItems = await _dbContext.IncomingShipmentDtos
|
|
.FromSqlRaw("EXEC GetIncomingShipment")
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<IncomingShipmentDto>();
|
|
}
|
|
public async Task<List<Infrastructure.Entities.PO.PO>> GetPOListByTerm(PODto itemDto)
|
|
{
|
|
return await _dbContext.POs
|
|
.Where(p => !p.IsCancel && !p.IsPOClosed &&
|
|
p.IsActive && p.PONo.StartsWith(itemDto.PONo ?? "N/A"))
|
|
.Take(50)
|
|
.AsNoTracking()
|
|
.ToListAsync();
|
|
}
|
|
#endregion
|
|
#region PostPut
|
|
public async Task<Infrastructure.Entities.PO.PurchaseOrder> PostApprovedPO(PODto PODto)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PostApprovedPO @UserId, @PONo",
|
|
new SqlParameter("@PONo", PODto.PONo != null ? PODto.PONo : 0L),
|
|
new SqlParameter("@UserId", PODto.UserId));
|
|
return new Infrastructure.Entities.PO.PurchaseOrder();
|
|
}
|
|
public async Task<Infrastructure.Entities.PO.PurchaseOrder> PostApprovedSuggested(PODto PODto)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PostSuggestedSupp @UserId, @CanvassDetailId, @ItemNo, @SupplierId,@CanvassId",
|
|
new SqlParameter("@CanvassDetailId", PODto.CanvassDetailId != null ? PODto.CanvassDetailId : 0L),
|
|
new SqlParameter("@UserId", PODto.UserId),
|
|
new SqlParameter("@ItemNo", PODto.ItemNo != null ? PODto.ItemNo : 0L),
|
|
new SqlParameter("@SupplierId", PODto.SupplierId),
|
|
new SqlParameter("@CanvassId", PODto.CanvassId));
|
|
return new Infrastructure.Entities.PO.PurchaseOrder();
|
|
}
|
|
public async Task<Suppliers> PostApprovedSupplier(PODto PODto)
|
|
{ await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PostApprovedSupplier @UserId, @CanvassDetailId, @ItemNo",
|
|
new SqlParameter("@CanvassDetailId", PODto.CanvassDetailId != null ? PODto.CanvassDetailId : 0L),
|
|
new SqlParameter("@UserId", PODto.UserId),
|
|
new SqlParameter("@ItemNo", PODto.ItemNo));
|
|
return new Suppliers();
|
|
}
|
|
public async Task<Infrastructure.Entities.PO.PurchaseOrder> PostPOToSupplier(PODto PODto)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PostPOToSupplier @UserId, @POTypeId, @EmailAddress,@PONumber",
|
|
new SqlParameter("@POTypeId", PODto.POTypeId),
|
|
new SqlParameter("@UserId", PODto.UserId),
|
|
new SqlParameter("@EmailAddress", PODto.EmailAddress),
|
|
new SqlParameter("@PONumber", PODto.PONo));
|
|
return new Infrastructure.Entities.PO.PurchaseOrder();
|
|
}
|
|
public async Task<Infrastructure.Entities.PO.PurchaseOrder> PostPutPO(PODto PODto)
|
|
{
|
|
if (PODto.UserId == "89da2977-c70f-4df9-94d4-9a610aa999ea" ||
|
|
PODto.UserId == "ac95500c-4b73-4df8-bdcb-965f9fafec30")
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync($"EXEC PostPutPO @UserId,@POTypeId,@SupplierId,@PONumber,@PORemarks,@IncoTermsId,@PODId,@ProfInvoiceNo,@ProfInvoiceDate,@PaymentTermsId,@ShippingInstructionId,@DeliverTo",
|
|
new SqlParameter("@UserId", PODto.UserId),
|
|
new SqlParameter("@POTypeId", PODto.POTypeId),
|
|
new SqlParameter("@SupplierId", PODto.SupplierId),
|
|
new SqlParameter("@PONumber", PODto.PONo),
|
|
new SqlParameter("@PORemarks", PODto.PORemarks ?? "N/A"),
|
|
new SqlParameter("@IncoTermsId", 1),
|
|
new SqlParameter("@PODId", 1),
|
|
new SqlParameter("@ProfInvoiceNo", PODto.ProfInvoiceNo ?? "N/A"),
|
|
new SqlParameter("@ProfInvoiceDate", DateTime.Now),
|
|
new SqlParameter("@PaymentTermsId", 1),
|
|
new SqlParameter("@ShippingInstructionId", 1),
|
|
new SqlParameter("@DeliverTo", PODto.DeliverTo));
|
|
}
|
|
else
|
|
{
|
|
if (!isUpdated)
|
|
{
|
|
PODto.PONo = await GetLatestPOById(PODto);//Retrieve and Update SystemSettings
|
|
}
|
|
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync($"EXEC PostPutPO @UserId,@POTypeId,@SupplierId,@PONumber,@PORemarks,@IncoTermsId,@PODId,@ProfInvoiceNo,@ProfInvoiceDate,@PaymentTermsId,@ShippingInstructionId,@DeliverTo",
|
|
new SqlParameter("@UserId", PODto.UserId),
|
|
new SqlParameter("@POTypeId", PODto.POTypeId),
|
|
new SqlParameter("@SupplierId", PODto.SupplierId),
|
|
new SqlParameter("@PONumber", formattedPONumber),
|
|
new SqlParameter("@PORemarks", PODto.PORemarks ?? "N/A"),
|
|
new SqlParameter("@IncoTermsId", 1),
|
|
new SqlParameter("@PODId", 1),
|
|
new SqlParameter("@ProfInvoiceNo", PODto.ProfInvoiceNo ?? "N/A"),
|
|
new SqlParameter("@ProfInvoiceDate", DateTime.Now),
|
|
new SqlParameter("@PaymentTermsId", 1),
|
|
new SqlParameter("@ShippingInstructionId", 1),
|
|
new SqlParameter("@DeliverTo", PODto.DeliverTo));
|
|
}
|
|
return new Infrastructure.Entities.PO.PurchaseOrder();
|
|
}
|
|
private void PutLocalCentralPo(PODto PODto)
|
|
{
|
|
string columnName;
|
|
string updatedPONo = "";
|
|
// Determine the column to update based on POTypeId
|
|
switch (PODto.POTypeId)
|
|
{
|
|
case 1: // SI
|
|
// Remove the first two characters from PONo
|
|
updatedPONo = PODto.PONo;
|
|
columnName = "PONoVatInc";
|
|
break;
|
|
case 2: // DR
|
|
updatedPONo = PODto.PONo;
|
|
columnName = "PONoVatEx";
|
|
break;
|
|
case 3: // IMPORT
|
|
updatedPONo = PODto.PONo;
|
|
columnName = "IPONoVatInc";
|
|
break;
|
|
default:
|
|
throw new ArgumentException("Invalid POTypeId.");
|
|
}
|
|
|
|
// Build the SQL query
|
|
string query = $@"
|
|
UPDATE [dbo].[SystemSettings]
|
|
SET {columnName} = @UpdatedPONo
|
|
WHERE {columnName} IS NOT NULL";
|
|
|
|
// Execute the raw SQL query
|
|
_dbLocalContext.Database.ExecuteSqlRaw(query, new SqlParameter("@UpdatedPONo", updatedPONo));
|
|
}
|
|
public async Task<CustomPO> PostPutCustomPO(PODto pODto)
|
|
{
|
|
if (pODto.POTypeId != 3)
|
|
{
|
|
pODto.PODId = 0;
|
|
pODto.ProfInvoiceDate = DateTime.Now;
|
|
pODto.ShippingInstructionId = 0;
|
|
pODto.IncoTermsId = 0;
|
|
}
|
|
if (!isUpdated)
|
|
{
|
|
pODto.PONo = await GetLatestPOById(pODto);
|
|
}
|
|
|
|
var (messCode, message) = CreateOutputParams();
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync($"EXEC PostPutCustomPO @UserId,@POTypeId,@PONumber,@PRDetailsId,@Specification,@PRNo,@PORemarks,@IncoTermsId," +
|
|
$"@PODId,@ProfInvoiceNo,@ProfInvoiceDate,@PaymentTermsId,@ShippingInstructionId,@SupplierId,@DeliveryDate,@Discount,@Amount," +
|
|
$"@UnitPrice,@Quantity,@DeliverTo, @MessCode OUTPUT, @Message OUTPUT",
|
|
new SqlParameter("@UserId", pODto.UserId),
|
|
new SqlParameter("@POTypeId", pODto.POTypeId),
|
|
new SqlParameter("@PONumber", formattedPONumber),
|
|
new SqlParameter("@PRDetailsId", pODto.PRDetailsId),
|
|
new SqlParameter("@Specification", pODto.Specification ?? string.Empty),
|
|
new SqlParameter("@PRNo", pODto.PRNo),
|
|
new SqlParameter("@PORemarks", pODto.PORemarks ?? "N/A"),
|
|
new SqlParameter("@IncoTermsId", pODto.IncoTermsId),
|
|
new SqlParameter("@PODId", pODto.PODId),
|
|
new SqlParameter("@ProfInvoiceNo", pODto.ProfInvoiceNo ?? "N/A"),
|
|
new SqlParameter("@ProfInvoiceDate", pODto.ProfInvoiceDate == null || pODto.ProfInvoiceDate == DateTime.MinValue
|
|
? DateTime.Now : pODto.ProfInvoiceDate),
|
|
new SqlParameter("@PaymentTermsId", pODto.PaymentTermsId),
|
|
new SqlParameter("@ShippingInstructionId", pODto.ShippingInstructionId),
|
|
new SqlParameter("@SupplierId", pODto.SupplierId),
|
|
new SqlParameter("@DeliveryDate", pODto.DeliveryDate),
|
|
new SqlParameter("@Discount", pODto.Discount),
|
|
new SqlParameter("@Amount", pODto.Amount),
|
|
new SqlParameter("@UnitPrice", pODto.UnitPrice),
|
|
new SqlParameter("@Quantity", pODto.Quantity),
|
|
new SqlParameter("@DeliverTo", pODto.DeliverTo),
|
|
messCode,
|
|
message);
|
|
|
|
if ((byte)messCode.Value == 0)
|
|
{
|
|
throw new Exception(message.Value.ToString());
|
|
}
|
|
return new CustomPO
|
|
{
|
|
PONo = formattedPONumber
|
|
};
|
|
}
|
|
public async Task Prerequisite(POVM poVM)
|
|
{
|
|
//CHARGES LIST
|
|
if (poVM.OtherChargesList != null)
|
|
{
|
|
foreach (var otherChargesId in poVM.OtherChargesList.OtherChargesId)
|
|
{
|
|
var index = poVM.OtherChargesList.OtherChargesId.IndexOf(otherChargesId);
|
|
var poDto = CreatePoDto(poVM, 0, otherChargesId, 0, 0,
|
|
poVM.OtherChargesList.Amount[index], 0, 0, "n/a");
|
|
await PostSuppCharges(poDto);
|
|
}
|
|
}
|
|
|
|
// Handle the creation of poVM for doc requirements
|
|
if (poVM.DocRequiredList.DocRequirementId != null)
|
|
{
|
|
// Loop through each document requirement id and call PostSuppDocRequirements for each
|
|
foreach (var docRequirementId in poVM.DocRequiredList.DocRequirementId)
|
|
{
|
|
var poDto = CreatePoDto(poVM, docRequirementId, 0, 0, 0, 0, 0, 0, "n/a");
|
|
await PostSuppDocRequirements(poDto);
|
|
}
|
|
}
|
|
}
|
|
public PODto CreatePoDto(POVM PODto, byte docRequirementId, int otherChargesId, long prDetailsId
|
|
, decimal unitPrice, decimal amount, long prNo, decimal quantity, string specification)
|
|
{
|
|
return new PODto
|
|
{
|
|
DocRequirementId = docRequirementId,
|
|
OtherChargesId = otherChargesId,
|
|
PRDetailsId = prDetailsId,
|
|
Amount = amount,
|
|
UnitPrice = unitPrice,
|
|
Quantity = quantity,
|
|
PRNo = prNo,
|
|
PONo = PODto.PONo,
|
|
UserId = PODto.UserId,
|
|
POTypeId = PODto.POTypeId,
|
|
PORemarks = PODto.PORemarks,
|
|
IncoTermsId = PODto.IncotermsId,
|
|
PODId = PODto.PodId,
|
|
ProfInvoiceNo = PODto.ProfInvoiceNo,
|
|
ProfInvoiceDate = PODto.ProfInvoiceDate,
|
|
PaymentTermsId = PODto.PaymentTermsId,
|
|
ShippingInstructionId = PODto.ShippingInstructionId,
|
|
SupplierId = PODto.SupplierId,
|
|
Discount = PODto.Discount,
|
|
DeliveryDate = PODto.DeliveryDate,
|
|
DeliverTo = PODto.DeliverTo ?? "N/A",
|
|
Specification = specification
|
|
};
|
|
}
|
|
public async Task<DocRequired> PostSuppDocRequirements(PODto poDto)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PostSuppDocRequirements @UserId,@DocRequirementId,@PONumber",
|
|
new SqlParameter("@DocRequirementId", poDto.DocRequirementId),
|
|
new SqlParameter("@UserId", poDto.UserId),
|
|
new SqlParameter("@PONumber", formattedPONumber));
|
|
return new DocRequired();
|
|
}
|
|
public async Task<OtherCharges> PostSuppCharges(PODto poDto)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PostSuppCharges @UserId,@PONo,@OtherChargesId,@POTypeId,@Amount",
|
|
new SqlParameter("@UserId", poDto.UserId),
|
|
new SqlParameter("@PONo", formattedPONumber),
|
|
new SqlParameter("@OtherChargesId", poDto.OtherChargesId),
|
|
new SqlParameter("@POTypeId", poDto.POTypeId),
|
|
new SqlParameter("@Amount", poDto.Amount));
|
|
return new OtherCharges();
|
|
}
|
|
public async Task<Infrastructure.Entities.PO.PurchaseOrder> PutPRItemDetails(PODto pODto)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync($"EXEC PutPRItemDetails @UserId,@ItemName,@Specification,@Qty,@UOMId,@PRDetailsId",
|
|
new SqlParameter("@UserId", pODto.UserId),
|
|
new SqlParameter("@ItemName", pODto.ItemName),
|
|
new SqlParameter("@Specification", pODto.Specification),
|
|
new SqlParameter("@Qty", pODto.Qty),
|
|
new SqlParameter("@UOMId", pODto.UOMId),
|
|
new SqlParameter("@PRDetailsId", pODto.PRDetailsId));
|
|
return new Infrastructure.Entities.PO.PurchaseOrder();
|
|
}
|
|
public async Task<Infrastructure.Entities.PO.PurchaseOrder> PutPOItemDetail(PODto pODto)
|
|
{
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync($"EXEC PutPOItemDetail @UserId,@UOMId,@Qty,@PRDetailsId,@Remarks,@ItemDescription,@Specification,@PONo,@UnitPrice,@PaymentTermsId,@PODId",
|
|
new SqlParameter("@UserId", pODto.UserId),
|
|
new SqlParameter("@UOMId", pODto.UOMId),
|
|
new SqlParameter("@Qty", pODto.Qty),
|
|
new SqlParameter("@PRDetailsId", pODto.PRDetailsId),
|
|
new SqlParameter("@Remarks", pODto.PORemarks),
|
|
new SqlParameter("@ItemDescription", pODto.ItemDescription),
|
|
new SqlParameter("@Specification", pODto.Specification),
|
|
new SqlParameter("@PONo", pODto.PONo),
|
|
new SqlParameter("@UnitPrice", pODto.UnitPrice),
|
|
new SqlParameter("@PaymentTermsId", pODto.PaymentTermsId),
|
|
new SqlParameter("@PODId", pODto.PODId));
|
|
return new Infrastructure.Entities.PO.PurchaseOrder();
|
|
}
|
|
public async Task<Infrastructure.Entities.PO.PurchaseOrder> PutMyPONo(PODto pODto)
|
|
{
|
|
string oldPONo = pODto.PONo;
|
|
if (!isUpdated)
|
|
{
|
|
await GetLatestPOById(pODto);
|
|
}
|
|
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync($"EXEC PutMyPONo @UserId,@OldPONo,@NewPONo,@PORemarks",
|
|
new SqlParameter("@UserId", pODto.UserId),
|
|
new SqlParameter("@OldPONo", oldPONo),
|
|
new SqlParameter("@NewPONo", formattedPONumber),
|
|
new SqlParameter("@PORemarks", pODto.PORemarks));
|
|
return new Infrastructure.Entities.PO.PurchaseOrder();
|
|
}
|
|
public async Task<List<POItemDetail>> GetPOItemDetail(PODto pODto)
|
|
{
|
|
var allItems = await _dbContext.POItemDetails
|
|
.FromSqlRaw($"EXEC GetPOItemDetail @UserId,@PRDetailsId,@PONo",
|
|
new SqlParameter("@UserId", pODto.UserId),
|
|
new SqlParameter("@PRDetailsId", pODto.PRDetailsId),
|
|
new SqlParameter("@PONo", pODto.PONo))
|
|
.ToListAsync();
|
|
|
|
return allItems ?? new List<POItemDetail>();
|
|
}
|
|
public async Task<CustomPO> ApprovedSelectedPO(PODto poDto)
|
|
{
|
|
var pono = await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC ApprovedSelectedPO @UserId,@PONo",
|
|
new SqlParameter("@UserId", poDto.UserId),
|
|
new SqlParameter("@PONo", poDto.PONo));
|
|
return new CustomPO(); ;
|
|
}
|
|
public async Task<DocRequired> PostPutDocRequired(PODto dto)
|
|
{
|
|
var doc = await _dbContext.DocRequireds
|
|
.FirstOrDefaultAsync(d => d.DocRequirementId == dto.DocRequirementId);
|
|
|
|
if (doc == null)
|
|
{
|
|
var document = new DocRequired
|
|
{
|
|
DocName = dto.DocName,
|
|
};
|
|
|
|
await _dbContext.DocRequireds.AddAsync(document);
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
return document;
|
|
}
|
|
else
|
|
{
|
|
doc.DocName = dto.DocName;
|
|
await _dbContext.SaveChangesAsync();
|
|
return doc;
|
|
}
|
|
}
|
|
public async Task<OtherCharges> PostPutOtherCharges(PODto dto)
|
|
{
|
|
var charges = await _dbContext.OtherCharges
|
|
.FirstOrDefaultAsync(d => d.OtherChargesId == dto.OtherChargesId);
|
|
|
|
if (charges == null)
|
|
{
|
|
var otherCharges = new OtherCharges
|
|
{
|
|
OtherChargesName = dto.OtherChargesName,
|
|
};
|
|
|
|
await _dbContext.OtherCharges.AddAsync(otherCharges);
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
return otherCharges;
|
|
}
|
|
else
|
|
{
|
|
charges.OtherChargesName = dto.OtherChargesName;
|
|
await _dbContext.SaveChangesAsync();
|
|
return charges;
|
|
}
|
|
}
|
|
public async Task<CustomPO> PutPOCancel(PODto poDto)
|
|
{
|
|
var (messCode, message) = CreateOutputParams();
|
|
await _dbContext.Database
|
|
.ExecuteSqlRawAsync("EXEC PutPOCancel @UserId,@POTypeId,@PONumber,@Remarks,@MessCode OUTPUT, @Message OUTPUT",
|
|
new SqlParameter("@UserId", poDto.UserId),
|
|
new SqlParameter("@POTypeId", poDto.POTypeId),
|
|
new SqlParameter("@PONumber", poDto.PONo),
|
|
new SqlParameter("@Remarks", poDto.Remarks),
|
|
messCode,
|
|
message);
|
|
|
|
if ((byte)messCode.Value == 0)
|
|
{
|
|
throw new Exception(message.Value.ToString());
|
|
}
|
|
return new CustomPO
|
|
{
|
|
PONo = formattedPONumber,
|
|
Message = message.Value.ToString()
|
|
};
|
|
}
|
|
public async Task<Incoterm> PostPutIncoterms(PODto dto)
|
|
{
|
|
var incot = await _dbContext.Incoterms
|
|
.FirstOrDefaultAsync(d => d.IncotermsId == dto.IncoTermsId);
|
|
|
|
if (incot == null)
|
|
{
|
|
var incoterms = new Incoterm
|
|
{
|
|
IncotermsName = dto.IncotermsName,
|
|
};
|
|
|
|
await _dbContext.Incoterms.AddAsync(incoterms);
|
|
await _dbContext.SaveChangesAsync();
|
|
return incoterms;
|
|
}
|
|
else
|
|
{
|
|
incot.IncotermsName = dto.IncotermsName;
|
|
await _dbContext.SaveChangesAsync();
|
|
return incot;
|
|
}
|
|
}
|
|
public async Task<bool> DeleteIncShip(PODto poDto)
|
|
{
|
|
var inc = await _dbContext.IncomingShipments
|
|
.FirstOrDefaultAsync(i => i.POId == poDto.POId);
|
|
|
|
if (inc != null)
|
|
{
|
|
inc.IsActive = false;
|
|
_dbContext.SaveChanges();
|
|
}
|
|
return true;
|
|
}
|
|
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 PostIncShipFollowUp(PODto itemDto, List<IncomingShipmentDto> shipFollowUp)
|
|
{
|
|
var baseTemplate = EMailTemplate("Content\\SMTPEmailContent", "IncShipFollowUp.cshtml");
|
|
|
|
// Use the passed data instead of re-querying
|
|
foreach (var incShip in shipFollowUp)
|
|
{
|
|
var message = new StringBuilder(baseTemplate);
|
|
message.Replace("@ViewBag.PONo", Convert.ToString(incShip.PONo));
|
|
message.Replace("@ViewBag.ProfInvoiceNo", Convert.ToString(incShip.ProfInvoiceNo));
|
|
message.Replace("@ViewBag.DeliveryDate", Convert.ToString(incShip.DeliveryDate));
|
|
message.Replace("@ViewBag.Supplier", Convert.ToString(incShip.SupplierName));
|
|
message.Replace("@ViewBag.Signature", Convert.ToString(incShip.Signature));
|
|
|
|
var messageDetails = new EmailMessageDetailsVM
|
|
{
|
|
Recipient = incShip.EmailAddress,
|
|
Message = message.ToString(),
|
|
Subject = incShip.Subject,
|
|
CC = incShip.CC,
|
|
IsSuccess = false,
|
|
SenderEmail = incShip.UserName,
|
|
DisplayName = "llipurchasing.com",
|
|
NewPassword = incShip.Password,
|
|
OutGoingPort = 587,
|
|
Server = incShip.Server,
|
|
UserName = incShip.UserName
|
|
};
|
|
|
|
await _smptHelper.SendEmailAsync(messageDetails);
|
|
}
|
|
}
|
|
public async Task<bool> PostIncShipFollowUp(PODto itemDto)
|
|
{
|
|
var shipFollowUp = await GetIncomingShipment(itemDto);
|
|
await PostIncShipFollowUp(itemDto, shipFollowUp);
|
|
return true;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|