Escali License control 1
AddAgreementModal.razor
Gå til dokumentasjonen til denne filen.
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
12@using AutoMapper
13@implements IDisposable
14@inject IMapper _mapper
15
16@if (_stateService.AddAgreementCustomer.Value != null)
17{
18 <EditForm Model="NewAgreement" OnValidSubmit="Submit" OnInvalidSubmit='() => Console.WriteLine("Offor funkerukke")' >
19
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">
25
26 <div class="create-modal-container">
27 <ObjectGraphDataAnnotationsValidator />
28
29 @if (_productViewModel.Products != null)
30 {
31
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)
36 {
37 <option value="@p.ProductSeq">@p.ProductName</option>
38 }
39 </InputSelect>
40 </FloatingInput>
41
42 }
43
44 @if (_moduleViewModel.Modules != null && NewAgreement.SelectedProduct != null)
45 {
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))
50 {
51 <option value="@m.ModuleSeq">@m.ModuleName</option>
52 }
53 </InputSelect>
54 </FloatingInput>
55 }
56
57 @if (NewAgreement.SelectedModule != null)
58 {
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)
63 {
64 foreach (var m in _moduleViewModel.Modules.Single(m => m.ModuleSeq == NewAgreement.SelectedModule.Value).ModuleLevels)
65 {
66 <option value="@m.ModuleLevelSeq">@m.ModuleLevelName</option>
67 }
68 }
69 </InputSelect>
70 </FloatingInput>
71 }
72
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"/>
76 </FloatingInput>
77
78 <FloatingInput Name="Sluttdato">
79 <InputDate @bind-Value="NewAgreement.DateTo" class="form-control input-text input-split-right" id="floatingInput" autocomplete="off"/>
80 </FloatingInput>
81 </div>
82
83 @if (_agreementViewModel.Agreements != null && NewAgreementNecessary && NewAgreement.SelectedProduct != null)
84 {
85
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
89 </h6>
90
91 <FloatingInput Name="Avtalenavn*">
92 <InputText @bind-Value="NewAgreement.AgreementNameInput" type="text" class="form-control input-text" id="floatingInput" placeholder=" " autocomplete="off"/>
93 </FloatingInput>
94
95 @if (_currencyViewModel.Currencies != null)
96 {
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"/>
99 </FloatingInput>
100 <datalist id="valutaList">
101 @foreach (var c in _currencyViewModel.Currencies.DistinctBy(c => c.CurrencyName).ToList())
102 {
103 <option value="@c.CurrencyName">@c.CurrencyName</option>
104 }
105 </datalist>
106 }
107
108 }
109
110
111 @if (NewAgreement.ModuleLevelSeq != 0 && NewAgreement.CurrencyNameInput != null)
112 {
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)
117 {
118 if (@pl.PriceElements.Any(ml => ml.ModuleLevelSeq == NewAgreement.ModuleLevelSeq))
119 {
120 <option value="@pl.PriceElements.Single(ml => ml.ModuleLevelSeq == NewAgreement.ModuleLevelSeq).Price">
121 @pl.PriceListName (@pl.PriceElements.Single(ml => ml.ModuleLevelSeq == NewAgreement.ModuleLevelSeq).Price)
122 </option>
123 }
124 }
125 </InputSelect>
126 </FloatingInput>
127 }
128
129 @if (NewAgreement.CurrencyNameInput != null && NewAgreement.Price != null)
130 {
131 <FloatingInput Name=@("Rabatt (" + NewAgreement.CurrencyNameInput + ")")>
132 <InputNumber @bind-Value="NewAgreement.Discount" class="form-control input-text" id="floatingInput" placeholder=" " autocomplete="off"/>
133 </FloatingInput>
134 }
135
136
137
138 </div>
139
140 </StandardModal>
141
142 </EditForm>
143}
144
145@code {
146
147 private int? _selectedProduct
148 {
149 get => NewAgreement.SelectedProduct;
150 set
151 {
152 NewAgreement.SelectedProduct = value;
153 NewAgreement.SelectedModule = null; // TODO Sett til første modul
154 NewAgreement.ModuleLevelSeq = 0;
155 CheckIfCustomerHasAgreementOnProduct();
156 }
157 }
158
159 private bool NewAgreementNecessary { get; set; }
160
161 private AgreementElementClientModelInput NewAgreement { get; set; } = new();
162
163 public StandardModal Modal;
164
165 private async Task Submit()
166 {
167 AgreementClientModel? agreement = new();
168 if (NewAgreementNecessary)
169 {
170 AgreementClientModel agreementClientModel = new();
171 agreementClientModel.AgreementName = NewAgreement.AgreementNameInput;
172 agreementClientModel.ProductSeq = NewAgreement.SelectedProduct.Value;
173 agreementClientModel.CustomerSeq = (int) _stateService.AddAgreementCustomer.Value;
174
175 var currency = _currencyViewModel.Currencies?.SingleOrDefault(c => c.CurrencyName == NewAgreement.CurrencyNameInput);
176 if (currency != null)
177 {
178 agreementClientModel.CurrencySeq = currency.CurrencySeq.Value;
179 }
180 else
181 {
182 agreementClientModel.Currency = new() { CurrencyName = NewAgreement.CurrencyNameInput };
183 }
184
185 agreement = await _agreementViewModel.CreateAgreement(agreementClientModel);
186
187 if (agreement != null)
188 NewAgreement.AgreementSeq = agreement.AgreementSeq.Value;
189 }
190
191 if (!NewAgreementNecessary || agreement != null)
192 {
193 await _agreementElementViewModel.CreateAgreementElement(_mapper.Map<AgreementElementClientModel>(NewAgreement));
194 await _customerViewModel.GetCustomers();
195 await _currencyViewModel.GetCurrencies();
196 Modal.HideModal();
197 }
198
199 }
200
201 private void ResetAgreement()
202 {
203 _stateService.AddAgreementCustomer.Value = null;
204 NewAgreement = new();
205 }
206
207 private void CheckIfCustomerHasAgreementOnProduct()
208 {
209 if (_agreementViewModel.Agreements != null)
210 {
211 var agreement = _agreementViewModel.Agreements.SingleOrDefault(a =>
212 a.CustomerSeq == _stateService.AddAgreementCustomer.Value
213 && a.ProductSeq == NewAgreement.SelectedProduct);
214
215 if (agreement != null)
216 {
217 if (agreement.AgreementSeq != null)
218 {
219 NewAgreement.AgreementSeq = agreement.AgreementSeq.Value;
220 NewAgreement.AgreementNameInput = agreement.AgreementName;
221 NewAgreement.CurrencyNameInput = agreement.Currency?.CurrencyName;
222 NewAgreementNecessary = false;
223 }
224 }
225 else
226 {
227 NewAgreement.AgreementSeq = null;
228 NewAgreement.AgreementNameInput = null;
229 NewAgreement.CurrencyNameInput = null;
230 NewAgreementNecessary = true;
231 }
232 }
233 NewAgreement.SelectedModule = null; // TODO Sett til første modul
234 NewAgreement.ModuleLevelSeq = 0;
235 }
236
237 protected override void OnInitialized()
238 {
239 _currencyViewModel.DataChanged += StateHasChanged;
240 _agreementViewModel.DataChanged += StateHasChanged;
241 _customerViewModel.DataChanged += StateHasChanged;
242
243 _stateService.AddAgreementCustomer.ValueChanged += StateHasChanged;
244
245 if (NewAgreement != null)
246 NewAgreement.ProductChange += CheckIfCustomerHasAgreementOnProduct;
247 }
248
249 void IDisposable.Dispose()
250 {
251 _currencyViewModel.DataChanged -= StateHasChanged;
252 _agreementViewModel.DataChanged -= StateHasChanged;
253 _customerViewModel.DataChanged -= StateHasChanged;
254
255 _stateService.AddAgreementCustomer.ValueChanged -= StateHasChanged;
256
257 if (NewAgreement != null)
258 NewAgreement.ProductChange -= CheckIfCustomerHasAgreementOnProduct;
259 }
260
261}