@using Front.Services
@using Shared
@using Shared.DTOs
@using Shared.DTOs.Warehouse
@using Shared.Enums
@inject HttpClientController hc;
@if (NewItem)
{
}
else
{
@if (!_UsedFromInvoice)
{
}
}
@code {
[Parameter] public CirculationDto model { get; set; }
[Parameter] public EventCallback OnMultipleOfThree { get; set; }
[Parameter] public bool NewItem { get; set; }
[Parameter] public List> CODrequest { get; set; }
private ConfirmDialog dialog = default!;
AlertColor alertColor = AlertColor.Primary;
IconName alertIconName = IconName.CheckCircleFill;
bool Hidealert = true;
string alertMessage = "";
bool _UsedFromInvoice = false;
public bool SpinnerVisible { get; set; } = false;
private bool forsale { get; set; } = true;
}
@functions {
protected override async Task OnParametersSetAsync()
{
if (NewItem)
forsale = true;
else
forsale = model.ForSale.HasValue ? model.ForSale.Value : false;
_UsedFromInvoice = model.invoiceID.HasValue;
if (NewItem)
model.Date = DateTime.Now.ConvertMiladiToShamsiinFront().ShamciToFormatShamciinFront();
await base.OnParametersSetAsync();
}
private void ShowSuccessAlert(string msg)
{
Hidealert = false;
alertColor = AlertColor.Success;
alertIconName = IconName.CheckCircleFill;
alertMessage = msg;
}
private void ShowDangerAlert(string msg)
{
Hidealert = false;
alertColor = AlertColor.Danger;
alertIconName = IconName.ExclamationTriangleFill;
alertMessage = msg;
}
public async Task OnClickDelete()
{
if (NewItem)
return;
SpinnerVisible = true;
await Delete();
SpinnerVisible = false;
}
private async Task Delete()
{
string route = model.Type == TypeCirculation.Receipt ? "Receipt" : "Remittance";
var rsp = await hc.Delete($"{route}/{model.ID}");
if (rsp.IsSuccessStatusCode)
{
ActionInResultComponent result = new ActionInResultComponent();
result.Status = ComponentStatus.success;
result.Action = ComponentAction.delete;
await OnMultipleOfThree.InvokeAsync(result);
}
else if (rsp.StatusCode == System.Net.HttpStatusCode.NotFound)
{
ShowDangerAlert("سندی یافت نشد");
}
else ShowDangerAlert("خطایی در اجرای عملیات رخ داده");
}
private async Task Addorupdate()
{
if ((model.Type == TypeCirculation.Receipt && !await ValidateReceipt())
|| (model.Type == TypeCirculation.Remittance && !await ValidateRemittance()))
return;
string controller = model.Type == TypeCirculation.Receipt ? "Receipt" : "Remittance";
string route = NewItem ? "ADD" : "Update";
ActionInResultComponent result = new ActionInResultComponent();
HttpResponseMessage rsp = new HttpResponseMessage();
if (NewItem)
{
if (model.Type == TypeCirculation.Receipt)
rsp = await hc.Post($"{controller}/{route}", new ReceiptDto()
{
CODID = model.CODID,
Count = model.Count,
Date = model.Date,
ForSale = model.ForSale.Value,
info = model.info,
Type = (TypeReceipt)model.ModelTypeID,
});
if (model.Type == TypeCirculation.Remittance)
rsp = await hc.Post($"{controller}/{route}", new RemittanceDto()
{
CODID = model.CODID,
Count = model.Count,
Date = model.Date,
info = model.info,
Type = (TypeRemittance)model.ModelTypeID,
});
result.Action = ComponentAction.add;
}
else
{
if (model.Type == TypeCirculation.Receipt)
rsp = await hc.Put($"{controller}/{route}", new ReceiptDto()
{
ID = model.ID,
CODID = model.CODID,
Count = model.Count,
Date = model.Date,
ForSale = model.ForSale.Value,
info = model.info,
Type = (TypeReceipt)model.ModelTypeID,
});
if (model.Type == TypeCirculation.Remittance)
rsp = await hc.Put($"{controller}/{route}", new RemittanceDto()
{
ID = model.ID,
CODID = model.CODID,
Count = model.Count,
Date = model.Date,
info = model.info,
Type = (TypeRemittance)model.ModelTypeID,
});
result.Action = ComponentAction.update;
}
if (rsp.IsSuccessStatusCode)
{
result.Status = ComponentStatus.success;
await OnMultipleOfThree.InvokeAsync(result);
}
else
{
try
{
var request = await rsp.Content.ReadFromJsonAsync>();
ShowDangerAlert(request[0]);
}
catch (Exception)
{
ShowDangerAlert("حظای سیستمی");
}
}
}
public async Task OnClickAddOrUpdate()
{
SpinnerVisible = true;
await Addorupdate();
SpinnerVisible = false;
}
private async Task ShowConfirmationDeleteAsync()
{
if (!NewItem)
{
var confirmation = await dialog.ShowAsync(
title: "عملیات حذف سند انبار",
message1: $"{model.info}",
message2: "اطمینان دارید?");
if (confirmation)
await OnClickDelete();
}
}
private async Task ValidateRemittance()
{
if (model.Type == TypeCirculation.Remittance)
{
if (model.ModelTypeID != 3 && model.ModelTypeID != 4)
ShowDangerAlert("نوع سند صحیح نمی باشد");
else return await Validate();
}
return false;
}
private async Task ValidateReceipt()
{
if (model.Type == TypeCirculation.Receipt)
{
model.ForSale = forsale;
if (model.ModelTypeID != 1 && model.ModelTypeID != 2)
ShowDangerAlert("نوع سند صحیح نمی باشد");
else return await Validate();
}
return false;
}
private async Task Validate()
{
if (model.CODID == null || model.CODID == 0)
ShowDangerAlert("کالایی انتخاب کنید");
else if (model.Count <= 0)
ShowDangerAlert("تعدادی وارد کنید");
else if (string.IsNullOrEmpty(model.Date))
ShowDangerAlert("تاریخ را مشخص کنید");
else if (model.Date.Replace("/", "").Length != 8)
ShowDangerAlert("تاریخ صحیح نمی باشد");
else if (string.IsNullOrEmpty(model.info))
ShowDangerAlert("توضیحی مشخص کنید");
else return true;
return false;
}
private async Task>> CODDataProvider(AutoCompleteDataProviderRequest> request)
{
return await Task.FromResult(request.ApplyTo(CODrequest.OrderBy(cod => cod.ID)));
}
private void OnAutoCompleteChanged(CODIdName cOD)
{
model.CODID = cOD.ID;
}
}