Compare commits
4 Commits
main
...
invoice-co
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4782bd3a89 | ||
|
|
81f6d83fd0 | ||
|
|
f31176cfc9 | ||
|
|
437d6d8f42 |
108
BimAI.API/Controllers/InvoiceController.cs
Normal file
108
BimAI.API/Controllers/InvoiceController.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using BimAI.Application.DTOModels;
|
||||
using BimAI.Application.DTOModels.Common;
|
||||
using BimAI.Infrastructure.Data;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace BimAI.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class InvoiceController(BimAIDbContext context) : ControllerBase
|
||||
{
|
||||
private readonly BimAIDbContext _context = context;
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<PagedResult<InvoiceDto>>> GetInvoices([FromQuery] InvoiceFilterRequest request)
|
||||
{
|
||||
var query = _context.Invoices.AsQueryable();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Search))
|
||||
{
|
||||
var searchTerm = request.Search.ToLower();
|
||||
query = query.Where(x =>
|
||||
x.DocumentNo.ToLower().Contains(searchTerm) ||
|
||||
x.ClientName.ToLower().Contains(searchTerm)
|
||||
);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.DocumentNo))
|
||||
{
|
||||
query = query.Where(x => x.DocumentNo.ToLower().Contains(request.DocumentNo.ToLower()));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.ClientName))
|
||||
{
|
||||
query = query.Where(x => x.ClientName.ToLower().Contains(request.ClientName.ToLower()));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Type))
|
||||
{
|
||||
query = query.Where(x => x.Type.ToLower().Contains(request.Type.ToLower()));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Source))
|
||||
{
|
||||
query = query.Where(x => x.Source.ToLower().Contains(request.Source.ToLower()));
|
||||
}
|
||||
|
||||
if (request.RegisterDateFrom.HasValue)
|
||||
{
|
||||
var from = request.RegisterDateFrom.Value.Date;
|
||||
query = query.Where(x => x.RegisterDate >= from);
|
||||
}
|
||||
|
||||
if (request.RegisterDateTo.HasValue)
|
||||
{
|
||||
var toExclusive = request.RegisterDateTo.Value.Date.AddDays(1);
|
||||
query = query.Where(x => x.RegisterDate < toExclusive);
|
||||
}
|
||||
|
||||
if (request.SellDateFrom.HasValue)
|
||||
{
|
||||
var from = request.SellDateFrom.Value.Date;
|
||||
query = query.Where(x => x.SellDate >= from);
|
||||
}
|
||||
|
||||
if (request.SellDateTo.HasValue)
|
||||
{
|
||||
var toExclusive = request.SellDateTo.Value.Date.AddDays(1);
|
||||
query = query.Where(x => x.SellDate < toExclusive);
|
||||
}
|
||||
|
||||
var totalCount = await query.CountAsync();
|
||||
|
||||
var items = await query
|
||||
.OrderByDescending(x => x.RegisterDate)
|
||||
.Skip((request.Page - 1) * request.PageSize)
|
||||
.Take(request.PageSize)
|
||||
.Select(x => new InvoiceDto
|
||||
{
|
||||
Id = x.Id,
|
||||
DocumentNo = x.DocumentNo,
|
||||
Type = x.Type,
|
||||
RegisterDate = x.RegisterDate,
|
||||
SellDate = x.SellDate,
|
||||
ClientName = x.ClientName,
|
||||
ClientId = x.ClientId,
|
||||
ClientNip = x.ClientNip,
|
||||
ClientAddress = x.ClientAddress,
|
||||
Currency = x.Currency,
|
||||
TotalNetto = x.TotalNetto,
|
||||
TotalBrutto = x.TotalBrutto,
|
||||
TotalVat = x.TotalVat,
|
||||
Source = x.Source,
|
||||
CreatedAt = x.CreatedAt,
|
||||
UpdatedAt = x.UpdatedAt
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
return Ok(new PagedResult<InvoiceDto>
|
||||
{
|
||||
Items = items,
|
||||
TotalCount = totalCount,
|
||||
Page = request.Page,
|
||||
PageSize = request.PageSize,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,9 @@ namespace BimAI.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class SyncController(ProductSyncService productSyncService) : ControllerBase
|
||||
public class SyncController(
|
||||
ProductSyncService productSyncService,
|
||||
InvoiceSyncService invoiceSyncService) : ControllerBase
|
||||
{
|
||||
[HttpPost("run-product-sync")]
|
||||
public async Task<IActionResult> RunProductSync()
|
||||
@@ -13,4 +15,11 @@ public class SyncController(ProductSyncService productSyncService) : ControllerB
|
||||
await productSyncService.RunAsync();
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("run-invoice-sync")]
|
||||
public async Task<IActionResult> RunInvoiceSync()
|
||||
{
|
||||
await invoiceSyncService.RunAsync();
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
||||
builder.Services.AddDbContext<BimAIDbContext>(options => options.UseSqlServer(connectionString));
|
||||
builder.Services.AddScoped<ProductSyncService>();
|
||||
builder.Services.AddScoped<InvoiceSyncService>();
|
||||
|
||||
builder.Services.AddHttpClient();
|
||||
builder.Services.AddControllers();
|
||||
@@ -135,4 +136,16 @@ RecurringJob.AddOrUpdate<ProductSyncJob>(
|
||||
: MisfireHandlingMode.Strict
|
||||
});
|
||||
|
||||
RecurringJob.AddOrUpdate<InvoiceSyncJob>(
|
||||
"invoice-sync",
|
||||
job => job.ExecuteAsync(),
|
||||
Cron.Daily(2, 30), // Every day at 2:30 AM
|
||||
new RecurringJobOptions
|
||||
{
|
||||
TimeZone = TimeZoneInfo.Local,
|
||||
MisfireHandling = app.Environment.IsDevelopment()
|
||||
? MisfireHandlingMode.Relaxed
|
||||
: MisfireHandlingMode.Strict
|
||||
});
|
||||
|
||||
app.Run();
|
||||
@@ -6,6 +6,9 @@
|
||||
"E5_CRM": {
|
||||
"ApiKey": "7e50a8a5-f01f-4fbc-8c1b-59f3fc474bb5"
|
||||
},
|
||||
"TWINPOL_CRM": {
|
||||
"ApiKey": "d68dac4c-f784-4e1b-8267-9ffcfa0eda4c"
|
||||
},
|
||||
"GoogleAuth": {
|
||||
"ClientId": "896226687615-rhmqk06t1hvll1hh2gtje2u6nbadtras.apps.googleusercontent.com"
|
||||
},
|
||||
|
||||
37
BimAI.Application/DTOModels/InvoiceDto.cs
Normal file
37
BimAI.Application/DTOModels/InvoiceDto.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
namespace BimAI.Application.DTOModels;
|
||||
|
||||
|
||||
public class InvoiceDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string DocumentNo { get; set; } = string.Empty;
|
||||
public string Type { get; set; } = string.Empty;
|
||||
public DateTime RegisterDate { get; set; }
|
||||
public DateTime SellDate { get; set; }
|
||||
public string ClientName { get; set; } = string.Empty;
|
||||
public string? ClientId { get; set; }
|
||||
public string? ClientNip { get; set; }
|
||||
public string? ClientAddress { get; set; }
|
||||
public string Currency { get; set; } = string.Empty;
|
||||
public decimal TotalNetto { get; set; }
|
||||
public decimal TotalBrutto { get; set; }
|
||||
public decimal TotalVat { get; set; }
|
||||
public string Source { get; set; } = string.Empty;
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
}
|
||||
|
||||
public class InvoiceFilterRequest
|
||||
{
|
||||
public string? Search { get; set; }
|
||||
public string? DocumentNo { get; set; }
|
||||
public string? ClientName { get; set; }
|
||||
public string? Type { get; set; }
|
||||
public string? Source { get; set; }
|
||||
public DateTime? RegisterDateFrom { get; set; }
|
||||
public DateTime? RegisterDateTo { get; set; }
|
||||
public DateTime? SellDateFrom { get; set; }
|
||||
public DateTime? SellDateTo { get; set; }
|
||||
public int Page { get; set; } = 1;
|
||||
public int PageSize { get; set; } = 20;
|
||||
}
|
||||
18
BimAI.Domain/Entities/Invoice.cs
Normal file
18
BimAI.Domain/Entities/Invoice.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
namespace BimAI.Domain.Entities;
|
||||
|
||||
public class Invoice: BaseEntity
|
||||
{
|
||||
public required string DocumentNo { get; set; }
|
||||
public required string Type { get; set; }
|
||||
public DateTime RegisterDate { get; set; }
|
||||
public DateTime SellDate { get; set; }
|
||||
public required string ClientName { get; set; }
|
||||
public string? ClientId { get; set; }
|
||||
public string? ClientNip { get; set; }
|
||||
public string? ClientAddress { get; set; }
|
||||
public required string Currency { get; set; }
|
||||
public decimal TotalNetto { get; set; }
|
||||
public decimal TotalBrutto { get; set; }
|
||||
public decimal TotalVat { get; set; }
|
||||
public required string Source { get; set; }
|
||||
}
|
||||
@@ -6,6 +6,7 @@ namespace BimAI.Infrastructure.Data;
|
||||
public class BimAIDbContext(DbContextOptions<BimAIDbContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<Product> Products { get; set; }
|
||||
public DbSet<Invoice> Invoices { get; set; }
|
||||
public DbSet<SyncState> SyncStates { get; set; }
|
||||
public DbSet<User> Users { get; set; }
|
||||
|
||||
@@ -20,6 +21,21 @@ public class BimAIDbContext(DbContextOptions<BimAIDbContext> options) : DbContex
|
||||
modelBuilder.Entity<Product>().Property(x => x.Ean).IsRequired().HasMaxLength(50);
|
||||
modelBuilder.Entity<Product>().Property(x => x.StockAddresses).IsRequired().HasMaxLength(512);
|
||||
|
||||
// Invoice properties
|
||||
modelBuilder.Entity<Invoice>().HasKey(x => x.Id);
|
||||
modelBuilder.Entity<Invoice>().Property(x => x.DocumentNo).IsRequired().HasMaxLength(100);
|
||||
modelBuilder.Entity<Invoice>().Property(x => x.Type).IsRequired().HasMaxLength(50);
|
||||
modelBuilder.Entity<Invoice>().Property(x => x.ClientName).IsRequired().HasMaxLength(255);
|
||||
modelBuilder.Entity<Invoice>().Property(x => x.ClientId).HasMaxLength(100);
|
||||
modelBuilder.Entity<Invoice>().Property(x => x.ClientNip).HasMaxLength(50);
|
||||
modelBuilder.Entity<Invoice>().Property(x => x.ClientAddress).HasMaxLength(500);
|
||||
modelBuilder.Entity<Invoice>().Property(x => x.Currency).IsRequired().HasMaxLength(10);
|
||||
modelBuilder.Entity<Invoice>().Property(x => x.Source).IsRequired().HasMaxLength(50);
|
||||
modelBuilder.Entity<Invoice>().Property(x => x.TotalNetto).HasPrecision(18, 2);
|
||||
modelBuilder.Entity<Invoice>().Property(x => x.TotalBrutto).HasPrecision(18, 2);
|
||||
modelBuilder.Entity<Invoice>().Property(x => x.TotalVat).HasPrecision(18, 2);
|
||||
modelBuilder.Entity<Invoice>().HasIndex(x => new { x.DocumentNo, x.Source }).IsUnique().HasDatabaseName("IX_Invoices_DocumentNo_Source");
|
||||
|
||||
// SyncState properties
|
||||
modelBuilder.Entity<SyncState>().HasKey((x => x.Entity));
|
||||
|
||||
|
||||
31
BimAI.Infrastructure/Jobs/InvoiceSyncJob.cs
Normal file
31
BimAI.Infrastructure/Jobs/InvoiceSyncJob.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using BimAI.Infrastructure.Sync;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace BimAI.Infrastructure.Jobs;
|
||||
|
||||
public class InvoiceSyncJob
|
||||
{
|
||||
private readonly InvoiceSyncService _invoiceSyncService;
|
||||
private readonly ILogger<InvoiceSyncJob> _logger;
|
||||
|
||||
public InvoiceSyncJob(InvoiceSyncService invoiceSyncService, ILogger<InvoiceSyncJob> logger)
|
||||
{
|
||||
_invoiceSyncService = invoiceSyncService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task ExecuteAsync()
|
||||
{
|
||||
_logger.LogInformation("Starting invoice sync...");
|
||||
|
||||
try
|
||||
{
|
||||
await _invoiceSyncService.RunAsync();
|
||||
_logger.LogInformation("Invoice sync finished.");
|
||||
} catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during invoice sync.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
151
BimAI.Infrastructure/Sync/InvoiceSyncService.cs
Normal file
151
BimAI.Infrastructure/Sync/InvoiceSyncService.cs
Normal file
@@ -0,0 +1,151 @@
|
||||
using System.Text.Json;
|
||||
using System.Web;
|
||||
using BimAI.Domain.Entities;
|
||||
using BimAI.Infrastructure.Data;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace BimAI.Infrastructure.Sync;
|
||||
|
||||
public class InvoiceSyncService(HttpClient httpClient, BimAIDbContext db, IConfiguration configuration)
|
||||
{
|
||||
private string DecodeHtmlEntities(string text)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text))
|
||||
return text;
|
||||
|
||||
return HttpUtility.HtmlDecode(text);
|
||||
}
|
||||
|
||||
public async Task RunAsync()
|
||||
{
|
||||
var apiKey = configuration["TWINPOL_CRM:ApiKey"];
|
||||
var syncState = db.SyncStates.FirstOrDefault(x => x.Entity == "Invoice") ?? new SyncState { Entity = "Invoice", LastSynced = 0 };
|
||||
|
||||
var url = $"https://crm.twinpol.com/REST/index.php?key={apiKey}&action=bimai.export.ecommerce&since=0";
|
||||
var response = await httpClient.GetStringAsync(url);
|
||||
|
||||
var jsonDoc = JsonSerializer.Deserialize<JsonElement>(response);
|
||||
if (!jsonDoc.TryGetProperty("data", out var dataElement))
|
||||
{
|
||||
Console.WriteLine("[SYNC] No 'data' property in response");
|
||||
return;
|
||||
}
|
||||
|
||||
var invoices = dataElement.EnumerateArray();
|
||||
var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||
var processedCount = 0;
|
||||
var batchSize = 20;
|
||||
|
||||
foreach (var invoiceJson in invoices)
|
||||
{
|
||||
try
|
||||
{
|
||||
var e5Id = invoiceJson.GetProperty("e5Id").GetString() ?? "";
|
||||
var documentNo = DecodeHtmlEntities(invoiceJson.GetProperty("documentNo").GetString() ?? "");
|
||||
var type = DecodeHtmlEntities(invoiceJson.GetProperty("type").GetString() ?? "");
|
||||
var registerDateStr = invoiceJson.GetProperty("registerDate").GetString() ?? "";
|
||||
var sellDateStr = invoiceJson.GetProperty("sellDate").GetString() ?? "";
|
||||
var clientName = DecodeHtmlEntities(invoiceJson.GetProperty("clientName").GetString() ?? "");
|
||||
var clientId = DecodeHtmlEntities(invoiceJson.GetProperty("clientId").GetString() ?? "");
|
||||
var clientNip = DecodeHtmlEntities(invoiceJson.GetProperty("clientNip").GetString() ?? "");
|
||||
var clientAddress = DecodeHtmlEntities(invoiceJson.GetProperty("clientAddress").GetString() ?? "");
|
||||
var currency = invoiceJson.GetProperty("currency").GetString() ?? "PLN";
|
||||
var totalNettoStr = invoiceJson.GetProperty("totalNetto").GetString() ?? "0";
|
||||
var totalBruttoStr = invoiceJson.GetProperty("totalBrutto").GetString() ?? "0";
|
||||
var totalVatStr = invoiceJson.GetProperty("totalVat").GetString() ?? "0";
|
||||
var source = invoiceJson.GetProperty("source").GetString() ?? "";
|
||||
|
||||
if (!DateTime.TryParse(registerDateStr, out var registerDate))
|
||||
{
|
||||
Console.WriteLine($"[SYNC] Invalid registerDate for invoice {documentNo}: {registerDateStr}");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!DateTime.TryParse(sellDateStr, out var sellDate))
|
||||
{
|
||||
Console.WriteLine($"[SYNC] Invalid sellDate for invoice {documentNo}: {sellDateStr}");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!decimal.TryParse(totalNettoStr, out var totalNetto))
|
||||
totalNetto = 0;
|
||||
|
||||
if (!decimal.TryParse(totalBruttoStr, out var totalBrutto))
|
||||
totalBrutto = 0;
|
||||
|
||||
if (!decimal.TryParse(totalVatStr, out var totalVat))
|
||||
totalVat = 0;
|
||||
|
||||
var existing = db.Invoices.FirstOrDefault(x => x.DocumentNo == documentNo && x.Source == source);
|
||||
|
||||
if (existing == null)
|
||||
{
|
||||
var invoice = new Invoice
|
||||
{
|
||||
DocumentNo = documentNo,
|
||||
Type = type,
|
||||
RegisterDate = registerDate,
|
||||
SellDate = sellDate,
|
||||
ClientName = clientName,
|
||||
ClientId = string.IsNullOrWhiteSpace(clientId) ? null : clientId,
|
||||
ClientNip = string.IsNullOrWhiteSpace(clientNip) ? null : clientNip,
|
||||
ClientAddress = string.IsNullOrWhiteSpace(clientAddress) ? null : clientAddress,
|
||||
Currency = currency,
|
||||
TotalNetto = totalNetto,
|
||||
TotalBrutto = totalBrutto,
|
||||
TotalVat = totalVat,
|
||||
Source = source,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
};
|
||||
db.Invoices.Add(invoice);
|
||||
Console.WriteLine($"[SYNC] Added invoice: {documentNo} from {source}");
|
||||
}
|
||||
else
|
||||
{
|
||||
existing.Type = type;
|
||||
existing.RegisterDate = registerDate;
|
||||
existing.SellDate = sellDate;
|
||||
existing.ClientName = clientName;
|
||||
existing.ClientId = string.IsNullOrWhiteSpace(clientId) ? null : clientId;
|
||||
existing.ClientNip = string.IsNullOrWhiteSpace(clientNip) ? null : clientNip;
|
||||
existing.ClientAddress = string.IsNullOrWhiteSpace(clientAddress) ? null : clientAddress;
|
||||
existing.Currency = currency;
|
||||
existing.TotalNetto = totalNetto;
|
||||
existing.TotalBrutto = totalBrutto;
|
||||
existing.TotalVat = totalVat;
|
||||
existing.UpdatedAt = DateTime.UtcNow;
|
||||
Console.WriteLine($"[SYNC] Updated invoice: {documentNo} from {source}");
|
||||
}
|
||||
|
||||
processedCount++;
|
||||
|
||||
if (processedCount % batchSize == 0)
|
||||
{
|
||||
await db.SaveChangesAsync();
|
||||
Console.WriteLine($"[SYNC] Saved batch of {batchSize} invoices. Total processed: {processedCount}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[SYNC] Error processing invoice: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
Console.WriteLine($"[SYNC] Saved remaining invoices. Total processed: {processedCount}");
|
||||
|
||||
syncState.LastSynced = now;
|
||||
if (db.SyncStates.FirstOrDefault(x => x.Entity == "Invoice") == null)
|
||||
{
|
||||
db.SyncStates.Add(syncState);
|
||||
}
|
||||
else
|
||||
{
|
||||
db.SyncStates.Update(syncState);
|
||||
}
|
||||
Console.WriteLine("Before final SaveChangesAsync");
|
||||
await db.SaveChangesAsync();
|
||||
Console.WriteLine("[SYNC] Invoice sync completed");
|
||||
}
|
||||
}
|
||||
160
BimAI.UI.Shared/Components/InvoiceListComponent.razor
Normal file
160
BimAI.UI.Shared/Components/InvoiceListComponent.razor
Normal file
@@ -0,0 +1,160 @@
|
||||
@using MudBlazor.Internal
|
||||
@using System.Data
|
||||
<MudText Typo="Typo.h4" Class="mb-4">Lista Faktur</MudText>
|
||||
|
||||
<MudExpansionPanels Class="mb-4">
|
||||
<MudExpansionPanel Icon="@Icons.Material.Filled.FilterList"
|
||||
Text="Filtry"
|
||||
Expanded="true">
|
||||
<MudGrid>
|
||||
<MudItem xs="12" sm="6" md="4">
|
||||
<MudTextField @bind-Value="filterRequest.Search"
|
||||
Label="Szukaj"
|
||||
Placeholder="Numer dokumentu, klient..."
|
||||
Immediate="true"
|
||||
DebounceInterval="500"
|
||||
OnDebounceIntervalElapsed="SearchInvoices"
|
||||
Clearable="true" />
|
||||
</MudItem>
|
||||
|
||||
<MudItem xs="12" sm="6" md="4">
|
||||
<MudTextField @bind-Value="filterRequest.DocumentNo"
|
||||
Label="Numer dokumentu"
|
||||
Immediate="true"
|
||||
DebounceInterval="500"
|
||||
OnDebounceIntervalElapsed="SearchInvoices"
|
||||
Clearable="true" />
|
||||
</MudItem>
|
||||
|
||||
<MudItem xs="12" sm="6" md="4">
|
||||
<MudTextField @bind-Value="filterRequest.Type"
|
||||
Label="Typ dokumentu"
|
||||
Immediate="true"
|
||||
DebounceInterval="500"
|
||||
OnDebounceIntervalElapsed="SearchInvoices"
|
||||
Clearable="true" />
|
||||
</MudItem>
|
||||
|
||||
<MudItem xs="12" sm="6" md="4">
|
||||
<MudTextField @bind-Value="filterRequest.ClientName"
|
||||
Label="Nazwa klienta"
|
||||
Immediate="true"
|
||||
DebounceInterval="500"
|
||||
OnDebounceIntervalElapsed="SearchInvoices"
|
||||
Clearable="true" />
|
||||
</MudItem>
|
||||
|
||||
<MudItem xs="12" sm="6" md="4">
|
||||
<MudDatePicker @bind-Date="filterRequest.RegisterDateFrom"
|
||||
Label="Data rejestracji od"
|
||||
DateFormat="dd.MM.yyyy"
|
||||
Editable="true"
|
||||
Clearable="true"
|
||||
Immediate="true"
|
||||
@bind-Date:after="OnFilterChanged" />
|
||||
</MudItem>
|
||||
|
||||
<MudItem xs="12" sm="6" md="4">
|
||||
<MudDatePicker @bind-Date="filterRequest.RegisterDateTo"
|
||||
Label="Data rejestracji do"
|
||||
DateFormat="d.MM.yyyy"
|
||||
Editable="true"
|
||||
Clearable="true"
|
||||
Immediate="true"
|
||||
@bind-Date:after="OnFilterChanged" />
|
||||
</MudItem>
|
||||
|
||||
<MudItem xs="12" sm="6" md="4">
|
||||
<MudDatePicker @bind-Date="filterRequest.SellDateFrom"
|
||||
Label="Data sprzedaży od"
|
||||
DateFormat="d.MM.yyyy"
|
||||
Editable="true"
|
||||
Clearable="true"
|
||||
Immediate="true"
|
||||
@bind-Date:after="OnFilterChanged" />
|
||||
</MudItem>
|
||||
|
||||
<MudItem xs="12" sm="6" md="4">
|
||||
<MudDatePicker @bind-Date="filterRequest.SellDateTo"
|
||||
Label="Data sprzedaży do"
|
||||
DateFormat="d.MM.yyyy"
|
||||
Editable="true"
|
||||
Clearable="true"
|
||||
Immediate="true"
|
||||
@bind-Date:after="OnFilterChanged" />
|
||||
</MudItem>
|
||||
|
||||
<MudItem xs="12" sm="6" md="4">
|
||||
<MudButton Variant="Variant.Outlined"
|
||||
OnClick="ClearFilters"
|
||||
StartIcon="Icons.Material.Filled.Clear">
|
||||
Wyczyść filtry
|
||||
</MudButton>
|
||||
</MudItem>
|
||||
</MudGrid>
|
||||
</MudExpansionPanel>
|
||||
</MudExpansionPanels>
|
||||
|
||||
|
||||
<MudDivider Class="my-4"></MudDivider>
|
||||
|
||||
<MudTable Items="invoices.Items"
|
||||
Dense="true"
|
||||
Hover="true"
|
||||
Loading="isLoading"
|
||||
LoadingProgressColor="Color.Info">
|
||||
<HeaderContent>
|
||||
<MudTh>Numer dokumentu</MudTh>
|
||||
<MudTh>Typ</MudTh>
|
||||
<MudTh>Klient</MudTh>
|
||||
<MudTh>Data rejestracji</MudTh>
|
||||
<MudTh>Data sprzedaży</MudTh>
|
||||
<MudTh>Wartość brutto</MudTh>
|
||||
<MudTh>Akcje</MudTh>
|
||||
</HeaderContent>
|
||||
<RowTemplate>
|
||||
<MudTd DataLabel="Numer dokumentu">@context.DocumentNo</MudTd>
|
||||
<MudTd DataLabel="Typ">@context.Type</MudTd>
|
||||
<MudTd DataLabel="Klient">@context.ClientName</MudTd>
|
||||
<MudTd DataLabel="Data rejestracji">@context.RegisterDate.ToShortDateString()</MudTd>
|
||||
<MudTd DataLabel="Data sprzedaży">@context.SellDate.ToShortDateString()</MudTd>
|
||||
<MudTd DataLabel="Wartość brutto">@context.TotalBrutto.ToString("C2")</MudTd>
|
||||
<MudTd DataLabel="Akcje">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Visibility"
|
||||
Size="Size.Small"
|
||||
OnClick="() => ViewInvoice(context.Id)"/>
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Edit"
|
||||
Size="Size.Small"
|
||||
OnClick="() => EditInvoice(context.Id)"/>
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Delete"
|
||||
Size="Size.Small"
|
||||
Color="Color.Error"
|
||||
OnClick="() => DeleteInvoice(context.Id)"/>
|
||||
</MudTd>
|
||||
</RowTemplate>
|
||||
<NoRecordsContent>
|
||||
<MudText>Brak faktur do wyświetlenia</MudText>
|
||||
</NoRecordsContent>
|
||||
<LoadingContent>
|
||||
Ładowanie...
|
||||
</LoadingContent>
|
||||
</MudTable>
|
||||
|
||||
@if (invoices.TotalCount > 0)
|
||||
{
|
||||
<MudGrid Class="mt-4" AlignItems="Center">
|
||||
<MudItem xs="12" sm="6">
|
||||
<MudText Typo="Typo.body2">
|
||||
Wyniki @((invoices.Page - 1) * invoices.PageSize + 1) - @Math.Min(invoices.Page * invoices.PageSize, invoices.TotalCount)
|
||||
z @invoices.TotalCount
|
||||
</MudText>
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="6" Class="d-flex justify-end">
|
||||
<MudPagination Count="invoices.TotalPages"
|
||||
Selected="invoices.Page"
|
||||
SelectedChanged="OnPageChanged"
|
||||
ShowFirstButton="true"
|
||||
ShowLastButton="true"/>
|
||||
</MudItem>
|
||||
</MudGrid>
|
||||
}
|
||||
85
BimAI.UI.Shared/Components/InvoiceListComponent.razor.cs
Normal file
85
BimAI.UI.Shared/Components/InvoiceListComponent.razor.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using BimAI.UI.Shared.Services;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using BimAI.Application.DTOModels;
|
||||
using BimAI.Application.DTOModels.Common;
|
||||
using MudBlazor;
|
||||
|
||||
namespace BimAI.UI.Shared.Components;
|
||||
|
||||
public partial class InvoiceListComponent : ComponentBase
|
||||
{
|
||||
[Inject] private InvoiceService InvoiceService { get; set; } = default!;
|
||||
[Inject] private ISnackbar Snackbar { get; set; } = default!;
|
||||
|
||||
private PagedResult<InvoiceDto> invoices = new();
|
||||
private InvoiceFilterRequest filterRequest = new();
|
||||
private bool isLoading = false;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await LoadInvoices();
|
||||
}
|
||||
|
||||
private async Task LoadInvoices()
|
||||
{
|
||||
isLoading = true;
|
||||
|
||||
try
|
||||
{
|
||||
var result = await InvoiceService.GetInvoiceAsync(filterRequest);
|
||||
invoices = result ?? new PagedResult<InvoiceDto>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Loading invoices failed: {ex.Message}");
|
||||
Console.WriteLine($"Stack trace: {ex.StackTrace}");
|
||||
Snackbar.Add($"Błąd podczas ładowania faktur: {ex.Message}", Severity.Error);
|
||||
invoices = new PagedResult<InvoiceDto>();
|
||||
}
|
||||
finally
|
||||
{
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SearchInvoices()
|
||||
{
|
||||
filterRequest.Page = 1;
|
||||
await LoadInvoices();
|
||||
}
|
||||
|
||||
private async Task OnPageChanged(int page)
|
||||
{
|
||||
filterRequest.Page = page;
|
||||
await LoadInvoices();
|
||||
}
|
||||
private async Task OnFilterChanged()
|
||||
{
|
||||
filterRequest.Page = 1;
|
||||
await SearchInvoices();
|
||||
}
|
||||
|
||||
private async Task ClearFilters()
|
||||
{
|
||||
filterRequest = new InvoiceFilterRequest();
|
||||
await LoadInvoices();
|
||||
}
|
||||
|
||||
private async Task ViewInvoice(Guid invoiceId)
|
||||
{
|
||||
// TODO
|
||||
Console.WriteLine($"Zobacz fakturę: {invoiceId}");
|
||||
}
|
||||
|
||||
private async Task EditInvoice(Guid invoiceId)
|
||||
{
|
||||
// TODO
|
||||
Console.WriteLine($"Edytuj fakturę: {invoiceId}");
|
||||
}
|
||||
|
||||
private async Task DeleteInvoice(Guid invoiceId)
|
||||
{
|
||||
// TODO
|
||||
Console.WriteLine($"Usuń fakturę: {invoiceId}");
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,12 @@ public static class ServiceCollectionExtensions
|
||||
{
|
||||
client.BaseAddress = new Uri(apiBaseUrl);
|
||||
});
|
||||
|
||||
services.AddHttpClient<InvoiceService>(client =>
|
||||
{
|
||||
client.BaseAddress = new Uri(apiBaseUrl);
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
<MudThemeProvider/>
|
||||
<MudDialogProvider/>
|
||||
<MudSnackbarProvider/>
|
||||
<MudPopoverProvider />
|
||||
|
||||
<MudLayout>
|
||||
<MudBreakpointProvider OnBreakpointChanged="OnBreakpointChanged"></MudBreakpointProvider>
|
||||
@@ -28,6 +29,7 @@
|
||||
<MudNavMenu>
|
||||
<MudNavLink Href="/dashboard" Icon="@Icons.Material.Filled.Dashboard">Dashboard</MudNavLink>
|
||||
<MudNavLink Href="/products" Icon="@Icons.Material.Filled.Inventory">Products</MudNavLink>
|
||||
<MudNavLink Href="/invoices" Icon="@Icons.Material.Filled.FilePresent">Invoices</MudNavLink>
|
||||
</MudNavMenu>
|
||||
</MudDrawer>
|
||||
|
||||
|
||||
8
BimAI.UI.Shared/Pages/InvoiceListPage.razor
Normal file
8
BimAI.UI.Shared/Pages/InvoiceListPage.razor
Normal file
@@ -0,0 +1,8 @@
|
||||
@page "/invoices"
|
||||
@using BimAI.UI.Shared.Components
|
||||
|
||||
<PageTitle>Faktury</PageTitle>
|
||||
|
||||
<MudContainer MaxWidth="MaxWidth.ExtraExtraLarge">
|
||||
<InvoiceListComponent />
|
||||
</MudContainer>
|
||||
73
BimAI.UI.Shared/Services/InvoiceService.cs
Normal file
73
BimAI.UI.Shared/Services/InvoiceService.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System.Text.Json;
|
||||
using BimAI.Application.DTOModels;
|
||||
using BimAI.Application.DTOModels.Common;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
|
||||
namespace BimAI.UI.Shared.Services;
|
||||
|
||||
public class InvoiceService(HttpClient httpClient)
|
||||
{
|
||||
private readonly HttpClient _httpClient = httpClient;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
public async Task<PagedResult<InvoiceDto>> GetInvoiceAsync(InvoiceFilterRequest request)
|
||||
{
|
||||
var queryParams = new Dictionary<string, string?>
|
||||
{
|
||||
["page"] = request.Page.ToString(),
|
||||
["pageSize"] = request.PageSize.ToString(),
|
||||
};
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.Search))
|
||||
{
|
||||
queryParams["search"] = request.Search;
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(request.ClientName))
|
||||
{
|
||||
queryParams["clientName"] = request.ClientName;
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(request.Type))
|
||||
{
|
||||
queryParams["type"] = request.Type;
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(request.DocumentNo))
|
||||
{
|
||||
queryParams["documentNo"] = request.DocumentNo;
|
||||
}
|
||||
if (request.RegisterDateFrom.HasValue)
|
||||
{
|
||||
queryParams["registerDateFrom"] = request.RegisterDateFrom.Value.ToString("yyyy-MM-dd");;
|
||||
}
|
||||
if (request.RegisterDateTo.HasValue)
|
||||
{
|
||||
queryParams["registerDateTo"] = request.RegisterDateTo.Value.ToString("yyyy-MM-dd");
|
||||
}
|
||||
if (request.SellDateFrom.HasValue)
|
||||
{
|
||||
queryParams["sellDateFrom"] = request.SellDateFrom.Value.ToString("yyyy-MM-dd");
|
||||
}
|
||||
if (request.SellDateTo.HasValue)
|
||||
{
|
||||
queryParams["sellDateTo"] = request.SellDateTo.Value.ToString("yyyy-MM-dd");
|
||||
}
|
||||
var uri = QueryHelpers.AddQueryString("api/invoice", queryParams);
|
||||
|
||||
var response = await _httpClient.GetAsync(uri);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorContent = await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
|
||||
var result = JsonSerializer.Deserialize<PagedResult<InvoiceDto>>(json, _jsonOptions);
|
||||
|
||||
return result ?? new PagedResult<InvoiceDto>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user