Try your search with a different keyword or use * as a wildcard.
using Microsoft.AspNetCore.Http;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Orders;
using Nop.Core.Http.Extensions;
using Nop.Data;
using Nop.Plugin.Misc.RFQ.Domains;
using Nop.Services.Catalog;
using Nop.Services.Customers;
using Nop.Services.Localization;
using Nop.Services.Orders;
using Nop.Services.Security;
using Nop.Services.Tax;
namespace Nop.Plugin.Misc.RFQ.Services;
///
/// Represent the RFQ service
///
public class RfqService
{
#region Fields
private readonly ICustomerService _customerService;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILocalizationService _localizationService;
private readonly IPermissionService _permissionService;
private readonly IProductService _productService;
private readonly IRepository _customerRepository;
private readonly IRepository _quoteRepository;
private readonly IRepository _quoteItemRepository;
private readonly IRepository _requestQuoteRepository;
private readonly IRepository _requestQuoteItemRepository;
private readonly IRepository _sciRepository;
private readonly IShoppingCartService _shoppingCartService;
private readonly IStoreContext _storeContext;
private readonly ITaxService _taxService;
private readonly IWorkContext _workContext;
private readonly RfqMessageService _rfqMessageService;
#endregion
#region Ctor
public RfqService(ICustomerService customerService,
IHttpContextAccessor httpContextAccessor,
ILocalizationService localizationService,
IPermissionService permissionService,
IProductService productService,
IRepository customerRepository,
IRepository quoteRepository,
IRepository quoteItemRepository,
IRepository requestQuoteRepository,
IRepository requestQuoteItemRepository,
IRepository sciRepository,
IShoppingCartService shoppingCartService,
IStoreContext storeContext,
ITaxService taxService,
IWorkContext workContext,
RfqMessageService rfqMessageService)
{
_customerService = customerService;
_httpContextAccessor = httpContextAccessor;
_localizationService = localizationService;
_permissionService = permissionService;
_productService = productService;
_customerRepository = customerRepository;
_quoteRepository = quoteRepository;
_quoteItemRepository = quoteItemRepository;
_requestQuoteRepository = requestQuoteRepository;
_requestQuoteItemRepository = requestQuoteItemRepository;
_sciRepository = sciRepository;
_shoppingCartService = shoppingCartService;
_storeContext = storeContext;
_taxService = taxService;
_workContext = workContext;
_rfqMessageService = rfqMessageService;
}
#endregion
#region Utilities
private void LogNote(IAdminNote noteItem, string note)
{
if (string.IsNullOrEmpty(note))
return;
note = DateTime.UtcNow.ToString(RfqDefaults.DateTimeStringFormat) + $": {note}";
if (!string.IsNullOrEmpty(noteItem.AdminNotes))
note += "\r\n";
else
noteItem.AdminNotes = string.Empty;
noteItem.AdminNotes = noteItem.AdminNotes.Insert(0, note);
}
private async Task UpdateQuantityWithLogAsync(RequestQuoteItem requestQuoteItem, int newQuantity)
{
var note = string.Format(
await _localizationService.GetResourceAsync("Plugins.Misc.RFQ.QuantityChanged"), requestQuoteItem.RequestedQty,
newQuantity, await GetCurrentCustomerEmailAsync());
LogNote(requestQuoteItem, note);
requestQuoteItem.RequestedQty = newQuantity;
}
private async Task UpdateUnitPriceWithLogAsync(RequestQuoteItem requestQuoteItem, decimal newUnitPrice)
{
var note = string.Format(
await _localizationService.GetResourceAsync("Plugins.Misc.RFQ.UnitPriceChanged"), requestQuoteItem.RequestedUnitPrice,
newUnitPrice, await GetCurrentCustomerEmailAsync());
LogNote(requestQuoteItem, note);
requestQuoteItem.RequestedUnitPrice = newUnitPrice;
}
private async Task UpdateCustomerNotesWithLogAsync(RequestQuote requestQuote, string newCustomerNotes)
{
var note = await _localizationService.GetResourceAsync("Plugins.Misc.RFQ.CustomerNoteChanged");
LogNote(requestQuote, note);
requestQuote.CustomerNotes = newCustomerNotes;
}
private async Task SetIfExistsAsync(RequestQuoteItem requestQuoteItem, IFormCollection form, string formKey)
{
var key = $"{formKey}{requestQuoteItem.Id}";
if (!form.ContainsKey(key))
return;
var formValue = form[key];
switch (formKey)
{
case RfqDefaults.QUANTITY_FORM_KEY:
if (int.TryParse(formValue, out var quantity) && requestQuoteItem.RequestedQty != quantity)
await UpdateQuantityWithLogAsync(requestQuoteItem, quantity);
break;
case RfqDefaults.UNIT_PRICE_FORM_KEY:
if (decimal.TryParse(formValue, out var price) && requestQuoteItem.RequestedUnitPrice != price)
await UpdateUnitPriceWithLogAsync(requestQuoteItem, price);
break;
}
}
private async Task GetCurrentCustomerEmailAsync()
{
var customer = await _workContext.GetCurrentCustomerAsync();
return customer.Email;
}
private async Task InsertQuoteAsync(Quote quote)
{
LogNote(quote, string.Format(await _localizationService.GetResourceAsync(quote.RequestQuoteId.HasValue ? "Plugins.Misc.RFQ.AdminQuote.QuoteCreatedByRequest" : "Plugins.Misc.RFQ.AdminQuote.QuoteCreatedManuallyByStoreOwner"), await GetCurrentCustomerEmailAsync()));
await _quoteRepository.InsertAsync(quote);
return quote;
}
private async Task UpdateRequestQuoteItemAsync(RequestQuoteItem requestQuoteItem)
{
await _requestQuoteItemRepository.UpdateAsync(requestQuoteItem);
}
private async Task UpdateQuoteItemAsync(QuoteItem quoteItem)
{
await _quoteItemRepository.UpdateAsync(quoteItem);
}
private async Task InsertQuoteItemsAsync(IList quoteItems)
{
if (!quoteItems.Any())
return;
await _quoteItemRepository.InsertAsync(quoteItems);
}
private async Task CheckIsQuoteExpiredAsync(Quote quote)
{
if (quote == null)
return null;
if (!quote.ExpirationDateUtc.HasValue)
return quote;
if (quote.Status is QuoteStatus.Expired or QuoteStatus.OrderCreated)
return quote;
if (quote.ExpirationDateUtc.Value > DateTime.UtcNow)
return quote;
quote.Status = QuoteStatus.Expired;
var note = await _localizationService.GetResourceAsync("Plugins.Misc.RFQ.QuoteExpired");
LogNote(quote, note);
await UpdateQuoteAsync(quote);
return quote;
}
#endregion
#region Methods
#region Request a quote
///
/// Changes the request quote status and add the note to the admin note field
///
/// Request quote to change status
/// New status to change
/// A task that represents the asynchronous operation
public async Task ChangeAndLogRequestQuoteStatusAsync(RequestQuote requestQuote, RequestQuoteStatus newStatus)
{
if (requestQuote.Status == newStatus)
return;
var note = string.Format(
await _localizationService.GetResourceAsync("Plugins.Misc.RFQ.RequestQuoteStatusChanged"),
await _localizationService.GetLocalizedEnumAsync(requestQuote.Status), await _localizationService.GetLocalizedEnumAsync(newStatus),
await GetCurrentCustomerEmailAsync());
LogNote(requestQuote, note);
requestQuote.Status = newStatus;
}
///
/// Creates request a quote by shopping cart items
///
///
/// A task that represents the asynchronous operation
/// The task result contains the request a quote and its items
///
public async Task<(RequestQuote requestQuote, List requestQuoteItem)> CreateRequestQuoteByShoppingCartAsync()
{
var customer = await _workContext.GetCurrentCustomerAsync();
var store = await _storeContext.GetCurrentStoreAsync();
var cart = await _shoppingCartService.GetShoppingCartAsync(customer, ShoppingCartType.ShoppingCart, store.Id);
if (!cart.Any())
return (null, null);
var requestQuote = new RequestQuote
{
CreatedOnUtc = DateTime.UtcNow,
CustomerId = customer.Id
};
var items = await cart.SelectAwait(async item =>
{
var (shoppingCartUnitPriceWithDiscountBase, _) = await _taxService.GetProductPriceAsync(await _productService.GetProductByIdAsync(item.ProductId),
(await _shoppingCartService.GetUnitPriceAsync(item, true)).unitPrice);
var requestQuoteItem = new RequestQuoteItem
{
Id = item.Id,
ProductAttributesXml = item.AttributesXml,
OriginalProductPrice = shoppingCartUnitPriceWithDiscountBase,
ProductId = item.ProductId,
RequestedQty = item.Quantity,
RequestedUnitPrice = shoppingCartUnitPriceWithDiscountBase,
RequestQuoteId = requestQuote.Id
};
return requestQuoteItem;
}).ToListAsync();
return (requestQuote, items);
}
///
/// Gets the request quote item by identifier
///
/// Request quote identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the request quote item
///
public async Task GetRequestQuoteByIdAsync(int requestId)
{
return await _requestQuoteRepository.GetByIdAsync(requestId);
}
///
/// Update request quote
///
/// Request a quote
/// A task that represents the asynchronous operation
public async Task UpdateRequestQuoteAsync(RequestQuote requestQuote)
{
await _requestQuoteRepository.UpdateAsync(requestQuote);
}
///
/// Send new request a quote
///
/// Customer notes
///
/// A task that represents the asynchronous operation
/// The task result contains the request a quote identifier
///
public async Task SendNewRequestAsync(string customerNotes)
{
var (request, items) = await CreateRequestQuoteByShoppingCartAsync();
LogNote(request, string.Format(await _localizationService.GetResourceAsync("Plugins.Misc.RFQ.RequestCreated"), await GetCurrentCustomerEmailAsync()));
request.Status = RequestQuoteStatus.Submitted;
await _requestQuoteRepository.InsertAsync(request);
var httpRequest = _httpContextAccessor.HttpContext?.Request;
if (httpRequest == null)
return 0;
var needSave = false;
if (!string.IsNullOrEmpty(customerNotes) && (!request.CustomerNotes?.Equals(customerNotes) ?? true))
{
needSave = true;
await UpdateCustomerNotesWithLogAsync(request, customerNotes);
}
if (needSave)
await UpdateRequestQuoteAsync(request);
items ??= await GetRequestQuoteItemsAsync(request.Id);
if (items.Any() && httpRequest.IsPostRequest() && httpRequest.HasFormContentType)
{
var form = await httpRequest.ReadFormAsync();
foreach (var requestQuoteItem in items)
{
await SetIfExistsAsync(requestQuoteItem, form, RfqDefaults.QUANTITY_FORM_KEY);
await SetIfExistsAsync(requestQuoteItem, form, RfqDefaults.UNIT_PRICE_FORM_KEY);
requestQuoteItem.Id = 0;
requestQuoteItem.RequestQuoteId = request.Id;
await InsertRequestQuoteItemAsync(requestQuoteItem);
}
}
await _rfqMessageService.CustomerSentNewRequestQuoteAsync(request);
return request.Id;
}
///
/// Gets customer requests a quote
///
/// Customer identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the request a quote item
///
public async Task> GetCustomerRequestsAsync(int customerId)
{
return await _requestQuoteRepository.Table.Where(p => p.CustomerId == customerId).OrderByDescending(p => p.Id)
.ToListAsync();
}
///
/// Gets customer quotes
///
/// Customer identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the request a quote item
///
public async Task> GetCustomerQuotesAsync(int customerId)
{
var statuses = new[] { (int)QuoteStatus.Submitted, (int)QuoteStatus.OrderCreated };
return await _quoteRepository.Table.Where(p => p.CustomerId == customerId && statuses.Contains(p.StatusId))
.OrderByDescending(p => p.Id).ToListAsync();
}
///
/// Search requests a quote
///
/// Request a quote status identifier
/// Created on from
/// Created on to
/// The customer email
/// Page index
/// Page size
///
/// A task that represents the asynchronous operation
/// The task result contains the request a quote item
///
public async Task> SearchRequestsQuoteAsync(int requestQuoteStatusId, DateTime? createdOnFrom, DateTime? createdOnTo, string customerEmail, int pageIndex = 0, int pageSize = int.MaxValue)
{
var requests = await _requestQuoteRepository.GetAllPagedAsync(query =>
{
if (createdOnFrom.HasValue)
query = query.Where(l => createdOnFrom.Value <= l.CreatedOnUtc);
if (createdOnTo.HasValue)
query = query.Where(l => createdOnTo.Value >= l.CreatedOnUtc);
if (requestQuoteStatusId > 0)
query = query.Where(l => requestQuoteStatusId == l.StatusId);
if (!string.IsNullOrEmpty(customerEmail))
{
query = from rq in query
join customer in _customerRepository.Table on rq.CustomerId equals customer.Id
where customer.Email.Contains(customerEmail)
select rq;
}
query = query.OrderByDescending(l => l.CreatedOnUtc);
return query;
}, pageIndex, pageSize);
return requests;
}
///
/// Deletes the request quote
///
/// Request quote identifier
/// A task that represents the asynchronous operation
public async Task DeleteRequestQuoteAsync(int requestQuoteId)
{
var requestQuote = await GetRequestQuoteByIdAsync(requestQuoteId);
await DeleteRequestQuoteAsync(requestQuote);
}
///
/// Deletes the request quote
///
/// Request a quote
/// A task that represents the asynchronous operation
public async Task DeleteRequestQuoteAsync(RequestQuote requestQuote)
{
if (requestQuote == null)
return;
if (requestQuote.QuoteId.HasValue)
{
var quote = await GetQuoteByIdAsync(requestQuote.QuoteId.Value);
quote.RequestQuoteId = null;
await UpdateQuoteAsync(quote);
}
await _requestQuoteRepository.DeleteAsync(requestQuote);
}
///
/// Deletes the requests a quote by identifiers
///
/// Identifiers of request a quote to delete
/// A task that represents the asynchronous operation
public async Task DeleteRequestsQuoteByIdsAsync(ICollection ids)
{
await _requestQuoteRepository.DeleteAsync(await _requestQuoteRepository.GetByIdsAsync(ids.ToArray()));
}
#endregion
#region Request quote item
///
/// Get request quote items
///
/// Request quote identifier to load items
///
/// A task that represents the asynchronous operation
/// The task result contains the request a quote items
///
public async Task> GetRequestQuoteItemsAsync(int requestId)
{
return await _requestQuoteItemRepository.Table
.Where(p => p.RequestQuoteId == requestId)
.ToListAsync();
}
///
/// Gets request a quote item
///
/// Request an quote item identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the request a quote item
///
public async Task GetRequestQuoteItemByIdAsync(int requestQuoteItemId)
{
return await _requestQuoteItemRepository.GetByIdAsync(requestQuoteItemId, _ => default);
}
///
/// Deletes request a quote item
///
/// Request a quote item identifier
/// A task that represents the asynchronous operation
public async Task DeleteRequestQuoteItemAsync(int requestQuoteItemId)
{
var item = await GetRequestQuoteItemByIdAsync(requestQuoteItemId);
if (item == null)
return;
await _requestQuoteItemRepository.DeleteAsync(item);
var requestQuote = await GetRequestQuoteByIdAsync(item.RequestQuoteId);
var note = string.Format(await _localizationService.GetResourceAsync("Plugins.Misc.RFQ.RequestQuoteItemDeleted"), requestQuoteItemId, await GetCurrentCustomerEmailAsync());
LogNote(requestQuote, note);
await UpdateRequestQuoteAsync(requestQuote);
}
///
/// Updates request a quote item
///
/// Request a quote item identifier
/// Quantity
/// Unit price
/// A task that represents the asynchronous operation
public async Task UpdateRequestQuoteItemAsync(int requestQuoteItemId, int quantity, decimal unitPrice)
{
var item = await GetRequestQuoteItemByIdAsync(requestQuoteItemId);
if (item == null)
return;
if (item.RequestedQty != quantity)
await UpdateQuantityWithLogAsync(item, quantity);
if (item.RequestedUnitPrice != unitPrice)
await UpdateUnitPriceWithLogAsync(item, unitPrice);
await UpdateRequestQuoteItemAsync(item);
}
///
/// Inserts request a quote item
///
/// Request a quote item
/// A task that represents the asynchronous operation
/// The task result contains the request a quote item
public async Task InsertRequestQuoteItemAsync(RequestQuoteItem requestQuoteItem)
{
LogNote(requestQuoteItem, string.Format(await _localizationService.GetResourceAsync("Plugins.Misc.RFQ.RequestItemCreated"), await GetCurrentCustomerEmailAsync()));
await _requestQuoteItemRepository.InsertAsync(requestQuoteItem);
return requestQuoteItem;
}
#endregion
#region Quote
///
/// Creates the empty quote for specified customer
///
/// The customer identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the new quote
///
public async Task CreateQuoteAsync(int customerId)
{
if (customerId <= 0)
return null;
var requestQuote = new Quote
{
Status = QuoteStatus.CreatedManuallyByStoreOwner,
CreatedOnUtc = DateTime.UtcNow,
CustomerId = customerId
};
return await InsertQuoteAsync(requestQuote);
}
///
/// Deletes the quotes by identifiers
///
/// Identifiers of the quote to delete
/// A task that represents the asynchronous operation
public async Task DeleteQuotesByIdsAsync(ICollection ids)
{
await _quoteRepository.DeleteAsync(await _quoteRepository.GetByIdsAsync(ids.ToArray()));
}
///
/// Search the quotes
///
/// Request a quote status identifier
/// Created on from
/// Created on to
/// The customer email
/// Page index
/// Page size
///
/// A task that represents the asynchronous operation
/// The task result contains the request a quote item
///
public async Task> SearchQuotesAsync(int quoteStatusId, DateTime? createdOnFrom, DateTime? createdOnTo, string customerEmail, int pageIndex = 0, int pageSize = int.MaxValue)
{
var quotes = await _quoteRepository.GetAllPagedAsync(query =>
{
if (createdOnFrom.HasValue)
query = query.Where(l => createdOnFrom.Value <= l.CreatedOnUtc);
if (createdOnTo.HasValue)
query = query.Where(l => createdOnTo.Value >= l.CreatedOnUtc);
if (quoteStatusId > 0)
query = query.Where(l => quoteStatusId == l.StatusId);
if (!string.IsNullOrEmpty(customerEmail))
{
query = from rq in query
join customer in _customerRepository.Table on rq.CustomerId equals customer.Id
where customer.Email.Contains(customerEmail)
select rq;
}
query = query.OrderByDescending(l => l.CreatedOnUtc);
return query;
}, pageIndex, pageSize);
foreach (var quote in quotes)
await CheckIsQuoteExpiredAsync(quote);
return quotes;
}
///
/// Creates the quote by request a quote
///
/// Request a quote identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the new quote identifier
///
public async Task CreateQuoteByRequestAsync(int requestQuoteId)
{
var requestQuote = await GetRequestQuoteByIdAsync(requestQuoteId);
if (requestQuote == null)
return 0;
var quote = new Quote
{
CreatedOnUtc = DateTime.UtcNow,
AdminNotes = string.Empty,
RequestQuoteId = requestQuoteId,
Status = QuoteStatus.CreatedFromRequestQuote,
CustomerId = requestQuote.CustomerId,
};
await InsertQuoteAsync(quote);
var items = await GetRequestQuoteItemsAsync(requestQuoteId);
await InsertQuoteItemsAsync(items.Select(item => new QuoteItem
{
AttributesXml = item.ProductAttributesXml,
ProductId = item.ProductId,
OfferedQty = item.RequestedQty,
QuoteId = quote.Id,
RequestedQty = item.RequestedQty,
RequestedUnitPrice = item.RequestedUnitPrice,
OfferedUnitPrice = item.RequestedUnitPrice,
RequestQuoteId = item.Id
}).ToList());
await ChangeAndLogRequestQuoteStatusAsync(requestQuote, RequestQuoteStatus.QuoteIsCreated);
requestQuote.QuoteId = quote.Id;
await UpdateRequestQuoteAsync(requestQuote);
return quote.Id;
}
///
/// Gets the quote item by identifier
///
/// The quote identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the quote item
///
public async Task GetQuoteByIdAsync(int quoteId)
{
return await CheckIsQuoteExpiredAsync(await _quoteRepository.GetByIdAsync(quoteId));
}
///
/// Changes the quote status and add the note to the admin note field
///
/// Quote to change status
/// New status to change
/// A task that represents the asynchronous operation
public async Task ChangeAndLogQuoteStatusAsync(Quote quote, QuoteStatus newStatus)
{
if (quote.Status == newStatus)
return;
var note = string.Format(await _localizationService.GetResourceAsync("Plugins.Misc.RFQ.QuoteStatusChanged"),
await _localizationService.GetLocalizedEnumAsync(quote.Status), await _localizationService.GetLocalizedEnumAsync(newStatus),
await GetCurrentCustomerEmailAsync());
LogNote(quote, note);
quote.Status = newStatus;
}
///
/// Update quote
///
/// The quote
/// A task that represents the asynchronous operation
public async Task UpdateQuoteAsync(Quote quote)
{
await _quoteRepository.UpdateAsync(quote);
}
///
/// Deletes the quote
///
/// Quote identifier
/// A task that represents the asynchronous operation
public async Task DeleteQuoteAsync(int quoteId)
{
var quote = await GetQuoteByIdAsync(quoteId);
if (quote == null)
return;
await _quoteRepository.DeleteAsync(quote);
if (quote.RequestQuoteId is null or 0)
return;
var requestQuote = await _requestQuoteRepository.GetByIdAsync(quote.RequestQuoteId);
if (requestQuote == null)
return;
requestQuote.QuoteId = null;
requestQuote.Status = RequestQuoteStatus.Submitted;
await UpdateRequestQuoteAsync(requestQuote);
}
///
/// Creates shopping cart items by quote
///
/// Quote identifier
/// A task that represents the asynchronous operation
public async Task CreateShoppingCartAsync(int quoteId)
{
var customer = await _workContext.GetCurrentCustomerAsync();
if (!await _permissionService.AuthorizeAsync(StandardPermission.PublicStore.ENABLE_SHOPPING_CART, customer))
throw new NopException("Shopping cart is disabled");
var store = await _storeContext.GetCurrentStoreAsync();
var quoteItems = await GetQuoteItemsAsync(quoteId);
await _shoppingCartService.ClearShoppingCartAsync(customer, store.Id);
//reset checkout info
await _customerService.ResetCheckoutDataAsync(customer, store.Id);
foreach (var quoteItem in quoteItems)
{
var now = DateTime.UtcNow;
var shoppingCartItem = new ShoppingCartItem
{
ShoppingCartType = ShoppingCartType.ShoppingCart,
StoreId = store.Id,
ProductId = quoteItem.ProductId,
AttributesXml = quoteItem.AttributesXml,
CustomerEnteredPrice = decimal.Zero,
Quantity = quoteItem.OfferedQty,
RentalStartDateUtc = null,
RentalEndDateUtc = null,
CreatedOnUtc = now,
UpdatedOnUtc = now,
CustomerId = customer.Id
};
await _sciRepository.InsertAsync(shoppingCartItem, false);
quoteItem.ShoppingCartItemId = shoppingCartItem.Id;
}
await UpdateQuoteItemsAsync(quoteItems);
//updated "HasShoppingCartItems" property used for performance optimization
if (!customer.HasShoppingCartItems)
{
customer.HasShoppingCartItems = true;
await _customerService.UpdateCustomerAsync(customer);
}
}
#endregion
#region Quote item
///
/// Inserts the quote item
///
/// Quote item
/// A task that represents the asynchronous operation
/// The task result contains the quote item
public async Task InsertQuoteItemAsync(QuoteItem quoteItem)
{
await _quoteItemRepository.InsertAsync(quoteItem);
var quote = await GetQuoteByIdAsync(quoteItem.QuoteId);
var note = string.Format(await _localizationService.GetResourceAsync("Plugins.Misc.RFQ.QuoteItemAdded"),
quoteItem.Id, await GetCurrentCustomerEmailAsync());
LogNote(quote, note);
await UpdateQuoteAsync(quote);
return quoteItem;
}
///
/// Get quote items
///
/// Quote identifier to load items
///
/// A task that represents the asynchronous operation
/// The task result contains the quote items
///
public async Task> GetQuoteItemsAsync(int quoteId)
{
return await _quoteItemRepository.Table
.Where(p => p.QuoteId == quoteId)
.ToListAsync();
}
///
/// Gets the quote item
///
/// Quote item identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the request a quote item
///
public async Task GetQuoteItemByIdAsync(int quoteItemId)
{
return await _quoteItemRepository.GetByIdAsync(quoteItemId, _ => default);
}
///
/// Deletes the quote item
///
/// Quote item identifier
/// A task that represents the asynchronous operation
public async Task DeleteQuoteItemAsync(int quoteItemId)
{
var item = await GetQuoteItemByIdAsync(quoteItemId);
if (item == null)
return;
await _quoteItemRepository.DeleteAsync(item);
var quote = await GetQuoteByIdAsync(item.QuoteId);
var note = string.Format(await _localizationService.GetResourceAsync("Plugins.Misc.RFQ.QuoteItemDeleted"), quoteItemId, await GetCurrentCustomerEmailAsync());
LogNote(quote, note);
await UpdateQuoteAsync(quote);
}
///
/// Updates a quote item
///
/// A quote item identifier
/// Quantity
/// Unit price
/// A task that represents the asynchronous operation
public async Task UpdateQuoteItemAsync(int quoteItemId, int quantity, decimal unitPrice)
{
var item = await GetQuoteItemByIdAsync(quoteItemId);
if (item == null)
return;
if (item.OfferedQty == quantity && item.OfferedUnitPrice == unitPrice)
return;
item.OfferedQty = quantity;
item.OfferedUnitPrice = unitPrice;
await UpdateQuoteItemAsync(item);
}
///
/// Gets the quote item by shopping cart item identifier
///
/// Shopping cart item identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the quote item
///
public async Task GetQuoteItemByShoppingCartItemIdAsync(int shoppingCartItemId)
{
return await _quoteItemRepository.Table.FirstOrDefaultAsync(qi => qi.ShoppingCartItemId == shoppingCartItemId);
}
///
/// Gets the quote items by shopping cart item identifiers
///
/// Shopping cart item identifiers
///
/// A task that represents the asynchronous operation
/// The task result contains the list of quote item
///
public async Task> GetQuoteItemsByShoppingCartItemIdsAsync(IList shoppingCartItemIds)
{
return await _quoteItemRepository.Table.Where(qi => qi.ShoppingCartItemId.HasValue && shoppingCartItemIds.Contains(qi.ShoppingCartItemId.Value)).ToListAsync();
}
///
/// Updates request a quote items
///
/// Request a quote items for update
/// A task that represents the asynchronous operation
public async Task UpdateQuoteItemsAsync(List quoteItems)
{
await _quoteItemRepository.UpdateAsync(quoteItems);
}
#endregion
#endregion
}