refactor: usar strings para status de facturas en lugar de enum
- Cambiar InvoiceDto.Status de enum a string - Eliminar InvoiceStatus enum y mapper - Usar directamente los status de Dolibarr (draft, unpaid, paid, cancelled)
This commit is contained in:
parent
9d871dccf9
commit
b937c64e39
|
|
@ -15,9 +15,12 @@ public class InvoicesController : ControllerBase
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<IActionResult> GetInvoices()
|
public async Task<IActionResult> GetInvoices(
|
||||||
|
[FromQuery] int limit = 50,
|
||||||
|
[FromQuery] int page = 0,
|
||||||
|
[FromQuery] string? status = null)
|
||||||
{
|
{
|
||||||
var invoices = await _dolibarrClient.GetInvoicesAsync();
|
var invoices = await _dolibarrClient.GetInvoicesAsync(limit, page, status);
|
||||||
return Ok(invoices);
|
return Ok(invoices);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
namespace DoliMiddlewareApi.Dtos.Enums;
|
|
||||||
|
|
||||||
public enum InvoiceStatus
|
|
||||||
{
|
|
||||||
Draft = 0,
|
|
||||||
Validated = 1,
|
|
||||||
Paid = 2,
|
|
||||||
Unknown = 3
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class InvoiceStatusMapper
|
|
||||||
{
|
|
||||||
public static InvoiceStatus FromDolibarrStatus(string status)
|
|
||||||
{
|
|
||||||
return status switch
|
|
||||||
{
|
|
||||||
"0" => InvoiceStatus.Draft,
|
|
||||||
"1" => InvoiceStatus.Validated,
|
|
||||||
"2" => InvoiceStatus.Paid,
|
|
||||||
_ => InvoiceStatus.Unknown
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
using DoliMiddlewareApi.Dtos.Enums;
|
|
||||||
|
|
||||||
namespace DoliMiddlewareApi.Dtos;
|
namespace DoliMiddlewareApi.Dtos;
|
||||||
|
|
||||||
public class InvoiceDto
|
public class InvoiceDto
|
||||||
|
|
@ -11,7 +9,7 @@ public class InvoiceDto
|
||||||
public int ClientId { get; set; }
|
public int ClientId { get; set; }
|
||||||
public decimal? Total { get; set; }
|
public decimal? Total { get; set; }
|
||||||
public decimal? RemainToPay { get; set; }
|
public decimal? RemainToPay { get; set; }
|
||||||
public InvoiceStatus Status { get; set; }
|
public string Status { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ using System.Globalization;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
using DoliMiddlewareApi.Dtos;
|
using DoliMiddlewareApi.Dtos;
|
||||||
using DoliMiddlewareApi.Dtos.Enums;
|
|
||||||
using DoliMiddlewareApi.Exceptions;
|
using DoliMiddlewareApi.Exceptions;
|
||||||
|
|
||||||
namespace DoliMiddlewareApi.Services;
|
namespace DoliMiddlewareApi.Services;
|
||||||
|
|
@ -23,9 +22,17 @@ public class DolibarrApiClient
|
||||||
return MapToInvoiceDto(data);
|
return MapToInvoiceDto(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<InvoiceDto>> GetInvoicesAsync()
|
public async Task<List<InvoiceDto>> GetInvoicesAsync(
|
||||||
|
int limit = 50,
|
||||||
|
int page = 1,
|
||||||
|
string? status = null)
|
||||||
{
|
{
|
||||||
var dataList = await GetListAsync<InvoiceResponse>("invoices");
|
var endpoint = $"invoices?limit={limit}&page={page-1}";
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(status))
|
||||||
|
endpoint += $"&status={status}";
|
||||||
|
|
||||||
|
var dataList = await GetListAsync<InvoiceResponse>(endpoint);
|
||||||
return dataList.Select(MapToInvoiceDto).ToList();
|
return dataList.Select(MapToInvoiceDto).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,14 +54,16 @@ public class DolibarrApiClient
|
||||||
|
|
||||||
ClientId = int.TryParse(invoiceResponse.socid, out int clientId) ? clientId : 0,
|
ClientId = int.TryParse(invoiceResponse.socid, out int clientId) ? clientId : 0,
|
||||||
|
|
||||||
Total = decimal.TryParse(invoiceResponse.total_ttc, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal total)
|
Total = decimal.TryParse(invoiceResponse.total_ttc, NumberStyles.Any, CultureInfo.InvariantCulture,
|
||||||
|
out decimal total)
|
||||||
? Math.Round(total, 2)
|
? Math.Round(total, 2)
|
||||||
: null,
|
: null,
|
||||||
RemainToPay = decimal.TryParse(invoiceResponse.remaintopay, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal remain)
|
RemainToPay = decimal.TryParse(invoiceResponse.remaintopay, NumberStyles.Any, CultureInfo.InvariantCulture,
|
||||||
|
out decimal remain)
|
||||||
? Math.Round(remain, 2)
|
? Math.Round(remain, 2)
|
||||||
: null,
|
: null,
|
||||||
|
|
||||||
Status = InvoiceStatusMapper.FromDolibarrStatus(invoiceResponse.statut ?? "0")
|
Status = invoiceResponse.statut ?? "unknown"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue