2026-01-02 18:01:04 +00:00
|
|
|
using System.Globalization;
|
2025-12-31 14:15:00 +00:00
|
|
|
using DoliMiddlewareApi.Dtos;
|
2026-01-02 18:01:04 +00:00
|
|
|
using DoliMiddlewareApi.Dtos.command;
|
2025-12-31 14:15:00 +00:00
|
|
|
using DoliMiddlewareApi.Dtos.Dolibarr;
|
|
|
|
|
using DoliMiddlewareApi.Mappers;
|
|
|
|
|
using DoliMiddlewareApi.Services.Clients;
|
|
|
|
|
|
|
|
|
|
namespace DoliMiddlewareApi.Services;
|
|
|
|
|
|
|
|
|
|
public class InvoiceService
|
|
|
|
|
{
|
|
|
|
|
private readonly DolibarrApiClient _apiClient;
|
|
|
|
|
|
|
|
|
|
public InvoiceService(DolibarrApiClient apiClient)
|
|
|
|
|
{
|
|
|
|
|
_apiClient = apiClient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<InvoiceDetailDto> GetInvoiceAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
var data = await _apiClient.GetResourceAsync<InvoiceDetailResponse>($"invoices/{id}");
|
|
|
|
|
return InvoiceMapper.MapToInvoiceDetailDto(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<InvoiceDto>> GetInvoicesAsync(
|
|
|
|
|
int limit = 50,
|
|
|
|
|
int page = 1,
|
|
|
|
|
string? status = null)
|
|
|
|
|
{
|
|
|
|
|
// empieza por 1 para el frontend
|
|
|
|
|
var endpoint = $"invoices?limit={limit}&page={page - 1}";
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(status))
|
|
|
|
|
{
|
|
|
|
|
endpoint += $"&status={status}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dataList = await _apiClient.GetCollectionAsync<InvoiceResponse>(endpoint);
|
|
|
|
|
return dataList.Select(InvoiceMapper.MapToInvoiceDto).ToList();
|
|
|
|
|
}
|
2026-01-02 18:01:04 +00:00
|
|
|
|
|
|
|
|
public async Task<int> CreateInvoiceAsync(CreateInvoiceDto dto)
|
|
|
|
|
{
|
|
|
|
|
var requestBody = new
|
|
|
|
|
{
|
|
|
|
|
socid = dto.ClientId.ToString(),
|
|
|
|
|
type = "0",
|
|
|
|
|
statut = dto.Status == "unpaid" ? "1" : "0",
|
|
|
|
|
date = ((DateTimeOffset)dto.Date).ToUnixTimeSeconds().ToString(),
|
|
|
|
|
date_lim_reglement = dto.ExpireDate.HasValue
|
|
|
|
|
? ((DateTimeOffset)dto.ExpireDate.Value).ToUnixTimeSeconds().ToString()
|
|
|
|
|
: null,
|
|
|
|
|
@ref = dto.Reference,
|
|
|
|
|
note_public = dto.NotePublic,
|
|
|
|
|
note_private = dto.NotePrivate,
|
|
|
|
|
lines = dto.Lines.Select(line => new
|
|
|
|
|
{
|
|
|
|
|
desc = line.Description,
|
|
|
|
|
qty = line.Quantity.ToString(CultureInfo.InvariantCulture),
|
|
|
|
|
subprice = line.UnitPrice.ToString(CultureInfo.InvariantCulture),
|
|
|
|
|
tva_tx = line.TaxRate.ToString(CultureInfo.InvariantCulture)
|
|
|
|
|
}).ToArray()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var responseBody = await _apiClient.PostAsync("invoices", requestBody);
|
|
|
|
|
|
|
|
|
|
return int.Parse(responseBody);
|
|
|
|
|
}
|
2025-12-31 14:15:00 +00:00
|
|
|
}
|