diff --git a/Includes/processing-functions.php b/Includes/processing-functions.php
index b8d7e0323eb828b6af83f81fbb83e3312d1ca0d3..15731d80da23b950dc88989c977ceb7dbba7dd6a 100644
--- a/Includes/processing-functions.php
+++ b/Includes/processing-functions.php
@@ -180,53 +180,70 @@ function mi_bsale_create_document_from_order($order_id) {
$subtotal_bruto = $item->get_subtotal() + $item->get_subtotal_tax();
$cantidad = $item->get_quantity();
$precio_unitario_bruto = ($cantidad > 0) ? $subtotal_bruto / $cantidad : 0;
$precio_unitario_neto = round($precio_unitario_bruto / 1.19, 2);
$details[] = [
'variantId' => $variant_id,
'quantity' => $cantidad,
'netUnitValue' => $precio_unitario_neto
];
}
$payment_mappings = get_option('bsale_payment_mappings', []);
$payment_gateway_id = $order->get_payment_method();
$bsale_payment_type_id = $payment_mappings[$payment_gateway_id] ?? get_option('bsale_default_payment_type_id');
$payments_array = [];
if (!empty($bsale_payment_type_id)) {
$payments_array[] = [
'paymentTypeId' => $bsale_payment_type_id,
'amount' => $order->get_total(),
'recordDate' => time()
];
}
- // LÓGICA DE ENVÍO FINAL: se añade el costo de despacho
- $shipping_total = (float) $order->get_shipping_total() + (float) $order->get_shipping_tax();
+ // LÓGICA DE ENVÍO FINAL: se añade el costo de despacho
+ // WooCommerce entrega el costo de envío sin impuestos en
+ // get_shipping_total(), por lo que enviar también el impuesto genera
+ // un monto mayor al esperado en Bsale/SII. De acuerdo con la
+ // documentación de Bsale, el campo "shippingValue" espera el valor
+ // neto. Se redondea a dos decimales para mantener consistencia con
+ // la API.
+ $shipping_total = round((float) $order->get_shipping_total(), 2);
+
+ if ($shipping_total > 0) {
+ $payload_shipping = ['shippingValue' => $shipping_total];
+ } else {
+ $payload_shipping = [];
+ }
- $payload = [
- 'declareSii' => 1,
- 'emissionDate' => time(),
- 'shippingValue' => $shipping_total,
- 'documentTypeId' => $doc_type_id,
- 'officeId' => $office_id,
- 'clientId' => $client_id,
- 'details' => $details,
- 'payments' => $payments_array
- ];
+ $payload = [
+ 'declareSii' => 1,
+ 'emissionDate' => time(),
+ 'documentTypeId' => $doc_type_id,
+ 'officeId' => $office_id,
+ 'clientId' => $client_id,
+ 'details' => $details,
+ 'payments' => $payments_array
+ ];
+
+ // Solo se agrega el valor de despacho cuando corresponde para evitar
+ // que Bsale cree una línea extra sin costo.
+ if (!empty($payload_shipping)) {
+ $payload = array_merge($payload, $payload_shipping);
+ }
$document = mi_bsale_api_post('/v1/documents.json', $payload);
if (is_wp_error($document)) {
throw new Exception("Error al crear documento: " . $document->get_error_message());
}
$order->update_meta_data('_bsale_document_id', $document['id']);
$order->update_meta_data('_bsale_document_url', $document['href']);
$order->save();
$order->add_order_note(sprintf('Proceso Bsale completado. Documento: %s.', $document['href'], $document['number']));
} catch (Exception $e) {
$order->add_order_note('FALLO Bsale: ' . $e->getMessage());
}
}
Tienda – Premium Store
“XVR PARA CAMARAS MODELO: DH-XVR5108HS-4KL-I3” se ha añadido a tu carrito.
Ver carrito
Tienda
Ordenar Por:
Precio mas Alto