1@inject StateService _stateService
2@inject CustomerViewModel _customerViewModel
3@inject ModuleViewModel _moduleViewModel
4@inject ProductViewModel _productViewModel
5@inject AgreementViewModel _agreementViewModel
6@inject CurrencyViewModel _currencyViewModel
7@inject PriceListViewModel _priceListViewModel
8@inject PriceElementViewModel _priceElementViewModel
9@inject AgreementElementViewModel _agreementElementViewModel
10@using Microsoft.AspNetCore.Components
11@using Escali.LicenseControl.Client.ClientModels.InputClientModels
13@implements IDisposable
14@inject IMapper _mapper
16@if (_stateService.AddAgreementCustomer.Value != null)
18 <EditForm Model="NewAgreement" OnValidSubmit="Submit" OnInvalidSubmit='() => Console.WriteLine("Offor funkerukke")' >
20 <StandardModal Header="Ny avtale"
21 HeaderSecond="@_customerViewModel.Customers?.Single(c => c.CustomerSeq == _stateService.AddAgreementCustomer.Value).CustomerName"
22 SubmitButtonName="Opprett avtale"
23 @ref="Modal" ShowModal="true"
24 OnModalClosed="ResetAgreement">
26 <div class="create-modal-container">
27 <ObjectGraphDataAnnotationsValidator />
29 @if (_productViewModel.Products != null)
32 <FloatingInput Name="Produkt">
33 <InputSelect @bind-Value="NewAgreement.SelectedProduct" class="form-select form-select-modal" id="floatingSelect" >
34 <option selected>Velg produkt</option>
35 @foreach (var p in _productViewModel.Products)
37 <option value="@p.ProductSeq">@p.ProductName</option>
44 @if (_moduleViewModel.Modules != null && NewAgreement.SelectedProduct != null)
46 <FloatingInput Name="Modul">
47 <InputSelect @bind-Value="NewAgreement.SelectedModule" class="form-select form-select-modal" id="floatingSelect" >
48 <option selected>Velg modul</option>
49 @foreach (var m in _moduleViewModel.Modules.Where(m => m.ProductSeq == NewAgreement.SelectedProduct))
51 <option value="@m.ModuleSeq">@m.ModuleName</option>
57 @if (NewAgreement.SelectedModule != null)
59 <FloatingInput Name="Modulnivå">
60 <InputSelect @bind-Value="NewAgreement.ModuleLevelSeq" class="form-select form-select-modal" id="floatingSelect" >
61 <option selected>Velg modulnivå</option>
62 @if (NewAgreement.SelectedModule != null)
64 foreach (var m in _moduleViewModel.Modules.Single(m => m.ModuleSeq == NewAgreement.SelectedModule.Value).ModuleLevels)
66 <option value="@m.ModuleLevelSeq">@m.ModuleLevelName</option>
73 <div class="input-split">
74 <FloatingInput Name="Startdato">
75 <InputDate @bind-Value="NewAgreement.DateFrom" class="form-control input-text input-split-left" id="floatingInput" autocomplete="off"/>
78 <FloatingInput Name="Sluttdato">
79 <InputDate @bind-Value="NewAgreement.DateTo" class="form-control input-text input-split-right" id="floatingInput" autocomplete="off"/>
83 @if (_agreementViewModel.Agreements != null && NewAgreementNecessary && NewAgreement.SelectedProduct != null)
86 <h6 class="modal-header-second">
87 Ekstra informasjon er nødvendig.<br />
88 Kunden har ingen tidligere @_productViewModel.GetProductByIdLocal(NewAgreement.SelectedProduct.Value)?.ProductName-avtaler
91 <FloatingInput Name="Avtalenavn*">
92 <InputText @bind-Value="NewAgreement.AgreementNameInput" type="text" class="form-control input-text" id="floatingInput" placeholder=" " autocomplete="off"/>
95 @if (_currencyViewModel.Currencies != null)
97 <FloatingInput Name="Valuta*">
98 <InputText @bind-Value="NewAgreement.CurrencyNameInput" type="text" class="form-control input-text hide-picker" id="floatingInput" placeholder=" " list="valutaList" autocomplete="off"/>
100 <datalist id="valutaList">
101 @foreach (var c in _currencyViewModel.Currencies.DistinctBy(c => c.CurrencyName).ToList())
103 <option value="@c.CurrencyName">@c.CurrencyName</option>
111 @if (NewAgreement.ModuleLevelSeq != 0 && NewAgreement.CurrencyNameInput != null)
113 <FloatingInput Name=@("Prisliste (" + NewAgreement.CurrencyNameInput + ")")>
114 <InputSelect @bind-Value="NewAgreement.Price" class="form-select form-select-modal" id="floatingSelect" >
115 <option selected disabled>Velg prisliste (@NewAgreement.CurrencyNameInput)</option>
116 @foreach (var pl in _priceListViewModel.PriceLists)
118 if (@pl.PriceElements.Any(ml => ml.ModuleLevelSeq == NewAgreement.ModuleLevelSeq))
120 <option value="@pl.PriceElements.Single(ml => ml.ModuleLevelSeq == NewAgreement.ModuleLevelSeq).Price">
121 @pl.PriceListName (@pl.PriceElements.Single(ml => ml.ModuleLevelSeq == NewAgreement.ModuleLevelSeq).Price)
129 @if (NewAgreement.CurrencyNameInput != null && NewAgreement.Price != null)
131 <FloatingInput Name=@("Rabatt (" + NewAgreement.CurrencyNameInput + ")")>
132 <InputNumber @bind-Value="NewAgreement.Discount" class="form-control input-text" id="floatingInput" placeholder=" " autocomplete="off"/>
147 private int? _selectedProduct
149 get => NewAgreement.SelectedProduct;
152 NewAgreement.SelectedProduct = value;
153 NewAgreement.SelectedModule = null; // TODO Sett til første modul
154 NewAgreement.ModuleLevelSeq = 0;
155 CheckIfCustomerHasAgreementOnProduct();
159 private bool NewAgreementNecessary { get; set; }
161 private AgreementElementClientModelInput NewAgreement { get; set; } = new();
163 public StandardModal Modal;
165 private async Task Submit()
167 AgreementClientModel? agreement = new();
168 if (NewAgreementNecessary)
170 AgreementClientModel agreementClientModel = new();
171 agreementClientModel.AgreementName = NewAgreement.AgreementNameInput;
172 agreementClientModel.ProductSeq = NewAgreement.SelectedProduct.Value;
173 agreementClientModel.CustomerSeq = (int) _stateService.AddAgreementCustomer.Value;
175 var currency = _currencyViewModel.Currencies?.SingleOrDefault(c => c.CurrencyName == NewAgreement.CurrencyNameInput);
176 if (currency != null)
178 agreementClientModel.CurrencySeq = currency.CurrencySeq.Value;
182 agreementClientModel.Currency = new() { CurrencyName = NewAgreement.CurrencyNameInput };
185 agreement = await _agreementViewModel.CreateAgreement(agreementClientModel);
187 if (agreement != null)
188 NewAgreement.AgreementSeq = agreement.AgreementSeq.Value;
191 if (!NewAgreementNecessary || agreement != null)
193 await _agreementElementViewModel.CreateAgreementElement(_mapper.Map<AgreementElementClientModel>(NewAgreement));
194 await _customerViewModel.GetCustomers();
195 await _currencyViewModel.GetCurrencies();
201 private void ResetAgreement()
203 _stateService.AddAgreementCustomer.Value = null;
204 NewAgreement = new();
207 private void CheckIfCustomerHasAgreementOnProduct()
209 if (_agreementViewModel.Agreements != null)
211 var agreement = _agreementViewModel.Agreements.SingleOrDefault(a =>
212 a.CustomerSeq == _stateService.AddAgreementCustomer.Value
213 && a.ProductSeq == NewAgreement.SelectedProduct);
215 if (agreement != null)
217 if (agreement.AgreementSeq != null)
219 NewAgreement.AgreementSeq = agreement.AgreementSeq.Value;
220 NewAgreement.AgreementNameInput = agreement.AgreementName;
221 NewAgreement.CurrencyNameInput = agreement.Currency?.CurrencyName;
222 NewAgreementNecessary = false;
227 NewAgreement.AgreementSeq = null;
228 NewAgreement.AgreementNameInput = null;
229 NewAgreement.CurrencyNameInput = null;
230 NewAgreementNecessary = true;
233 NewAgreement.SelectedModule = null; // TODO Sett til første modul
234 NewAgreement.ModuleLevelSeq = 0;
237 protected override void OnInitialized()
239 _currencyViewModel.DataChanged += StateHasChanged;
240 _agreementViewModel.DataChanged += StateHasChanged;
241 _customerViewModel.DataChanged += StateHasChanged;
243 _stateService.AddAgreementCustomer.ValueChanged += StateHasChanged;
245 if (NewAgreement != null)
246 NewAgreement.ProductChange += CheckIfCustomerHasAgreementOnProduct;
249 void IDisposable.Dispose()
251 _currencyViewModel.DataChanged -= StateHasChanged;
252 _agreementViewModel.DataChanged -= StateHasChanged;
253 _customerViewModel.DataChanged -= StateHasChanged;
255 _stateService.AddAgreementCustomer.ValueChanged -= StateHasChanged;
257 if (NewAgreement != null)
258 NewAgreement.ProductChange -= CheckIfCustomerHasAgreementOnProduct;