using Microsoft.AspNetCore.Mvc; using Nop.Plugin.Misc.FruitBankPlugin.Models; using Nop.Plugin.Misc.FruitBankPlugin.Services; using Nop.Web.Areas.Admin.Models.Customers; using Nop.Web.Framework.Components; namespace Nop.Plugin.Misc.FruitBankPlugin.Components; [ViewComponent(Name = "CustomerCreditWidget")] public class CustomerCreditWidgetViewComponent : NopViewComponent { private readonly ICustomerCreditService _customerCreditService; public CustomerCreditWidgetViewComponent(ICustomerCreditService customerCreditService) { _customerCreditService = customerCreditService; } public async Task InvokeAsync(string widgetZone, object additionalData) { if (additionalData is not CustomerModel customerModel) return Content(""); var customerId = customerModel.Id; var credit = await _customerCreditService.GetByCustomerIdAsync(customerId); var outstanding = await _customerCreditService.GetOutstandingBalanceAsync(customerId); var model = new CustomerCreditWidgetModel { CustomerId = customerId, HasCreditLimit = credit != null, CreditLimit = credit?.CreditLimit ?? 0m, OutstandingBalance = outstanding, RemainingCredit = credit != null ? credit.CreditLimit - outstanding : (decimal?)null, Comment = credit?.Comment }; return View("~/Plugins/Misc.FruitBankPlugin/Views/CustomerCreditWidget.cshtml", model); } }