Escali License control 1
AddPriceListModal.razor
Gå til dokumentasjonen til denne filen.
1@inject CurrencyViewModel _currencyViewService
2@inject PriceListViewModel _priceListViewService
3@inject StateService _stateService
4@using System.ComponentModel.DataAnnotations
5@using AutoMapper
6@using Microsoft.AspNetCore.Components
7@inject IMapper _mapper
8@implements IDisposable
9
10<EditForm Model="NewPriceList" OnValidSubmit="Submit" OnInvalidSubmit="EventCallback.Empty" >
11
12 <StandardModal Header="Ny prisliste" SubmitButtonName="Opprett prisliste" @ref="Modal" OnModalClosed="ResetPriceList">
13
14 <div class="create-modal-container">
15 <ObjectGraphDataAnnotationsValidator />
16
17 <FloatingInput Name="Navn*">
18 <InputText @bind-Value="NewPriceList.PriceListName" type="text" class="form-control input-text" id="floatingInput" placeholder=" " autocomplete="off"/>
19 </FloatingInput>
20
21 @if (_currencyViewService.Currencies != null && NewPriceList.Currency != null)
22 {
23 <FloatingInput Name="Valuta*">
24 <InputText @bind-Value="NewPriceList.Currency.CurrencyName" type="text" class="form-control input-text hide-picker" id="floatingInput" placeholder=" " list="valutaList" autocomplete="off"/>
25 </FloatingInput>
26 <datalist id="valutaList">
27 @foreach (var c in _currencyViewService.Currencies.DistinctBy(c => c.CurrencyName).ToList())
28 {
29 <option value="@c.CurrencyName">@c.CurrencyName</option>
30 }
31 </datalist>
32 }
33
34 <div class="input-split">
35 <div class="input-container form-floating mb-3">
36 <InputDate @bind-Value="NewPriceList.DateFrom" class="form-control input-text input-split-left" id="floatingInput" autocomplete="off" max="@NewPriceList.DateTo"/>
37 <label for="floatingInput">Startdato</label>
38 </div>
39
40 <div class="input-container form-floating mb-3">
41 <InputDate @bind-Value="NewPriceList.DateTo" class="form-control input-text input-split-right" id="floatingInput" autocomplete="off"/>
42 <label for="floatingInput">Sluttdato</label>
43 </div>
44 </div>
45
46 <div class="input-container">
47 <InputSelect class="form-select form-select-modal" @bind-Value="NewPriceList.PriceListToCopy" >
48 <option disabled>Velg prisliste for utgangspunkt*</option>
49 @if (_priceListViewService.PriceLists != null)
50 {
51 if (!_priceListViewService.PriceLists.Any(p => p.ProductSeq == _stateService.SelectedProductSeq.Value && p.Currency.CurrencyName == NewPriceList.Currency?.CurrencyName))
52 {
53 <option selected value="@(() => NewPriceList.PriceListToCopy = null)">Finnes ingen prislister med valgt valuta</option>
54 }
55 @foreach (var p in _priceListViewService.PriceLists.Where(p => p.ProductSeq == _stateService.SelectedProductSeq.Value && p.Currency.CurrencyName == NewPriceList.Currency?.CurrencyName).ToList())
56 {
57 <option value="@p.PriceListSeq">@p.PriceListName</option>
58 }
59 }
60 </InputSelect>
61 </div>
62
63 <div class="input-container form-floating mb-3">
64 <InputNumber @bind-Value="NewPriceList.PriceAdjustmentFactor" class="form-control input-text" id="floatingInput" placeholder="Faktor for prisøkning" autocomplete="off"/>
65 <label for="floatingInput">Faktor for prisøkning (min. 1)*</label>
66 </div>
67
68 </div>
69
70 </StandardModal>
71
72</EditForm>
73
74<button class="btn btn-primary button-with-icon" @onclick="() => Modal.OpenModal()">
75 Ny prisliste
76 <img class="button-icon" src="Icons/add-icon.svg" alt=""/>
77</button>
78
79@code {
80
81 private PriceListClientModelInput NewPriceList { get; set; } = new()
82 {
83 Currency = new(),
84 DateFrom = DateTime.Now,
85 DateTo = DateTime.Now
86 };
87 private StandardModal Modal { get; set; }
88
89 private async void Submit()
90 {
91 NewPriceList.ProductSeq = _stateService.SelectedProductSeq.Value;
92
93 int currencySeq = 0;
94 if (_currencyViewService.Currencies != null)
95 {
96 var seq = _currencyViewService.Currencies.SingleOrDefault(c => c.CurrencyName == NewPriceList.Currency.CurrencyName)?.CurrencySeq;
97 if (seq != null) currencySeq = seq.Value;
98 }
99
100 if (currencySeq != 0)
101 {
102 NewPriceList.Currency = null;
103 NewPriceList.CurrencySeq = currencySeq;
104 }
105
106 var mappedPriceList = _mapper.Map<PriceListClientModel>(NewPriceList);
107
108 var addedPricelist = await _priceListViewService.CreatePricelist(mappedPriceList, NewPriceList.PriceListToCopy!.Value, NewPriceList.PriceAdjustmentFactor!.Value);
109 if (addedPricelist != null)
110 {
111 await InvokeAsync(_currencyViewService.GetCurrencies);
112
113 ResetPriceList();
114 Modal.HideModal();
115 }
116 }
117
118 private void ResetPriceList()
119 {
120 NewPriceList = new()
121 {
122 Currency = new(),
123 DateFrom = DateTime.Now,
124 DateTo = DateTime.Now
125 };
126 }
127
128 protected override void OnInitialized()
129 {
130 _currencyViewService.DataChanged += StateHasChanged;
131 }
132
133 void IDisposable.Dispose()
134 {
135 _currencyViewService.DataChanged -= StateHasChanged;
136 }
137
138}