direcciones...
This commit is contained in:
parent
f4cf440ff0
commit
877cb594b2
|
|
@ -141,7 +141,22 @@ public class DolibarrClient {
|
|||
}
|
||||
|
||||
if (matches != null && !matches.isEmpty()) {
|
||||
return matches.get(0);
|
||||
DolibarrThirdpartyDto existing = matches.get(0);
|
||||
if (address != null && (existing.address() == null || existing.address().isBlank())) {
|
||||
DolibarrThirdpartyDto updated = new DolibarrThirdpartyDto(
|
||||
existing.id(), existing.name(), existing.email(), existing.client(),
|
||||
existing.codeClient(), address, zip, city, existing.idCountry(), existing.phone());
|
||||
restClient.put()
|
||||
.uri("/thirdparties/{id}", existing.id())
|
||||
.body(updated)
|
||||
.retrieve()
|
||||
.toBodilessEntity();
|
||||
return restClient.get()
|
||||
.uri("/thirdparties/{id}", existing.id())
|
||||
.retrieve()
|
||||
.body(DolibarrThirdpartyDto.class);
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
|
||||
log.debug("Thirdparty not found, creating for email={}", email);
|
||||
|
|
@ -245,8 +260,8 @@ public class DolibarrClient {
|
|||
*
|
||||
* @param order the created order (must have {@code id}, {@code socid}, and {@code lines})
|
||||
*/
|
||||
public DolibarrInvoiceDto createInvoiceFromOrder(DolibarrOrderDto order) {
|
||||
log.debug("Creating Dolibarr invoice from orderId={}", order.id());
|
||||
public DolibarrInvoiceDto createInvoiceFromOrder(DolibarrOrderDto order, Integer fkAddress) {
|
||||
log.debug("Creating Dolibarr invoice from orderId={} fkAddress={}", order.id(), fkAddress);
|
||||
|
||||
List<DolibarrInvoiceDto.InvoiceLine> invoiceLines = order.lines() == null
|
||||
? List.of()
|
||||
|
|
@ -264,6 +279,7 @@ public class DolibarrClient {
|
|||
DolibarrInvoiceDto invoiceBody = DolibarrInvoiceDto.builder()
|
||||
.fkCommande(order.id())
|
||||
.socid(order.socid())
|
||||
.fkAddress(fkAddress)
|
||||
.lines(invoiceLines)
|
||||
.build();
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@ public record DolibarrInvoiceDto(
|
|||
Integer socid,
|
||||
/** Linked order internal ID */
|
||||
@JsonProperty("fk_commande") Integer fkCommande,
|
||||
/** Billing address (llx_socadresse ID); null = use thirdparty main address */
|
||||
@JsonProperty("fk_address") Integer fkAddress,
|
||||
/** 0 = draft, 1 = validated, 2 = paid, 3 = abandoned */
|
||||
Integer statut,
|
||||
List<InvoiceLine> lines
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ public record PrestashopOrderDto(
|
|||
@JsonProperty("total_shipping") String totalShipping,
|
||||
/** PrestaShop order state ID */
|
||||
@JsonProperty("current_state") String currentState,
|
||||
@JsonProperty("id_address_invoice") String idAddressInvoice,
|
||||
Associations associations
|
||||
) {
|
||||
|
||||
|
|
|
|||
|
|
@ -134,8 +134,9 @@ public class OrderSyncService {
|
|||
|
||||
DolibarrOrderDto createdOrder = dolibarrClient.createOrder(orderDto);
|
||||
|
||||
// 3. Create linked invoice with lines copied from the order
|
||||
DolibarrInvoiceDto invoice = dolibarrClient.createInvoiceFromOrder(createdOrder);
|
||||
// 3. Create linked invoice with lines copied from the order and billing address
|
||||
Integer fkAddress = resolveInvoiceAddress(psOrder, customer.id());
|
||||
DolibarrInvoiceDto invoice = dolibarrClient.createInvoiceFromOrder(createdOrder, fkAddress);
|
||||
|
||||
// 4. Persist mapping to prevent re-import
|
||||
orderMappingRepository.save(OrderMapping.builder()
|
||||
|
|
@ -279,6 +280,40 @@ public class OrderSyncService {
|
|||
}
|
||||
}
|
||||
|
||||
private Integer resolveInvoiceAddress(PrestashopOrderDto psOrder, Integer dolibarrThirdpartyId) {
|
||||
String addrId = psOrder.idAddressInvoice();
|
||||
if (addrId == null || addrId.isBlank() || "0".equals(addrId)) return null;
|
||||
|
||||
// If billing addr = customer default addr → already on thirdparty, no fk_address needed
|
||||
boolean isGuest = psOrder.idCustomer() == null
|
||||
|| psOrder.idCustomer().isBlank()
|
||||
|| "0".equals(psOrder.idCustomer());
|
||||
if (!isGuest) {
|
||||
try {
|
||||
PrestashopCustomerDto c = prestashopClient.getCustomer(Integer.parseInt(psOrder.idCustomer()));
|
||||
if (c != null && addrId.equals(c.idDefaultAddress())) return null;
|
||||
} catch (Exception e) {
|
||||
log.warn("Could not fetch PS customer for invoice address resolution: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Find matching Dolibarr socaddress by content
|
||||
try {
|
||||
PrestashopAddressDto billing = prestashopClient.getAddress(Integer.parseInt(addrId));
|
||||
if (billing == null) return null;
|
||||
String formatted = formatAddress(billing.address1(), billing.address2());
|
||||
return dolibarrClient.getThirdpartyAddresses(dolibarrThirdpartyId).stream()
|
||||
.filter(a -> a.address() != null && a.address().equals(formatted)
|
||||
&& a.city() != null && a.city().equals(billing.city()))
|
||||
.map(DolibarrAddressDto::id)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
} catch (Exception e) {
|
||||
log.warn("Could not resolve billing address for PS order {}: {}", psOrder.id(), e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String formatAddress(String addr1, String addr2) {
|
||||
if (addr1 == null || addr1.isBlank()) return null;
|
||||
if (addr2 == null || addr2.isBlank()) return addr1;
|
||||
|
|
|
|||
Loading…
Reference in New Issue