Jump to content

[Solucionado] Modificar Fecha de Facturación


Recommended Posts

Buenas,

Estoy buscando modificar cómo Prestashop 1.7 asigna la fecha de la factura. La lógica deseada es:

- Si el pedido está pagado completamente, la fecha de la factura debe ser la fecha del último pago registrado.
- Si no, debe seguir utilizando la fecha actual autoasignada al generarse la factura.

He intentado modificar OrderInvoice.php con el siguiente código sin éxito. La factura sigue generándose con la fecha actual.

Este es el código que había originalmente:

public function add($autodate = true, $null_values = false)
{
    $order = new Order($this->id_order);

    $this->shop_address = OrderInvoice::getCurrentFormattedShopAddress($order->id_shop);

    return parent::add();
}

 

Esta es la modificación que he intentado:

    public function add($autodate = true, $null_values = false)
    {
        $order = new Order($this->id_order);
        $this->shop_address = OrderInvoice::getCurrentFormattedShopAddress($order->id_shop);

        // Inicializar fecha de adición
        $invoice_date = date('Y-m-d H:i:s');

        // Verificar si el pedido está pagado completamente
        if ($order->getCurrentState() == Configuration::get('PS_OS_PAYMENT')) {
            // Obtener la fecha del último pago registrado
            $payments = OrderPayment::getByOrderReference($order->reference);
            if (!empty($payments)) {
                $last_payment = end($payments);
                $invoice_date = $last_payment->date_add;
            }
        }

        // Asignar la fecha de la factura
        $this->date_add = $invoice_date;

        // Llamar al método padre para agregar la factura
        return parent::add($autodate, $null_values);
    }

¿Alguien puede indicar cuál es el método correcto o el enfoque adecuado para lograr esto? Cualquier ayuda sería muy apreciada.

Gracias de antemano!

Link to comment
Share on other sites

public function add($autodate = true, $null_values = false)
{
    $order = new Order($this->id_order);

    // Obtain the payments for this order
    $payments = OrderPayment::getByOrderReference($order->reference);

    if (!empty($payments)) {
        // Get the date of the last payment
        $last_payment = end($payments);
        if ($last_payment && property_exists($last_payment, 'date_add')) {
            $this->date_add = $last_payment->date_add;
        }
    }

    // Call the parent method to add the invoice
    $result = parent::add($autodate, $null_values);

    // If the date was overwritten, set it again
    if (!empty($payments) && $this->date_add !== $last_payment->date_add) {
        $this->date_add = $last_payment->date_add;
        Db::getInstance()->update('order_invoice', ['date_add' => $this->date_add], 'id_order_invoice = ' . (int)$this->id);
    }

    // Set the shop address
    $this->shop_address = OrderInvoice::getCurrentFormattedShopAddress($order->id_shop);

    return $result;
}

He resuelto el problema con la personalización de la fecha de la factura en PrestaShop 1.7. Aquí está la solución final que me funcionó

Link to comment
Share on other sites

  • Eutanasio changed the title to [Solucionado] Modificar Fecha de Facturación
  • 4 months later...
Posted (edited)
  On 1/2/2025 at 12:41 PM, Marta_web04 said:

Como lo editas en el panel ? 

Expand  

Debes modificar la clase /classes/Order/Order.Invoice.php, de preferencia crea un override para ello y así no perderás la customización si alguna actualización u otro módulo te cambia nuevamente dicha clase en el futuro.

Lo que hace mi customización es que la fecha de la factura que generes sea la del último pago registrado en el pedido (si o hay) o bien asignar a la factura la fecha de hoy y no la del día en que se generó el pedido (lo cual es el comportamiento por defecto de Prestashop).

Un saludo

Edited by Eutanasio (see edit history)
Link to comment
Share on other sites

Disculpa me he explicado mal, he reemplazado el código por tu solución, esto edita la fecha de la factura ? o permite personalizarla desde el backend ? 

 

un saludo  

 

public function add($autodate = true, $null_values = false)
{
    $order = new Order($this->id_order);

    // Obtain the payments for this order
    $payments = OrderPayment::getByOrderReference($order->reference);

    if (!empty($payments)) {
        // Get the date of the last payment
        $last_payment = end($payments);
        if ($last_payment && property_exists($last_payment, 'date_add')) {
            $this->date_add = $last_payment->date_add;
        }
    }

    // Call the parent method to add the invoice
    $result = parent::add($autodate, $null_values);

    // If the date was overwritten, set it again
    if (!empty($payments) && $this->date_add !== $last_payment->date_add) {
        $this->date_add = $last_payment->date_add;
        Db::getInstance()->update('order_invoice', ['date_add' => $this->date_add], 'id_order_invoice = ' . (int)$this->id);
    }

    // Set the shop address
    $this->shop_address = OrderInvoice::getCurrentFormattedShopAddress($order->id_shop);

    return $result;
}

 

Link to comment
Share on other sites

  On 1/2/2025 at 4:12 PM, Marta_web04 said:

Disculpa me he explicado mal, he reemplazado el código por tu solución, esto edita la fecha de la factura ? o permite personalizarla desde el backend ? 

 

un saludo  

 

public function add($autodate = true, $null_values = false)
{
    $order = new Order($this->id_order);

    // Obtain the payments for this order
    $payments = OrderPayment::getByOrderReference($order->reference);

    if (!empty($payments)) {
        // Get the date of the last payment
        $last_payment = end($payments);
        if ($last_payment && property_exists($last_payment, 'date_add')) {
            $this->date_add = $last_payment->date_add;
        }
    }

    // Call the parent method to add the invoice
    $result = parent::add($autodate, $null_values);

    // If the date was overwritten, set it again
    if (!empty($payments) && $this->date_add !== $last_payment->date_add) {
        $this->date_add = $last_payment->date_add;
        Db::getInstance()->update('order_invoice', ['date_add' => $this->date_add], 'id_order_invoice = ' . (int)$this->id);
    }

    // Set the shop address
    $this->shop_address = OrderInvoice::getCurrentFormattedShopAddress($order->id_shop);

    return $result;
}

 

Expand  

He editado mi mensaje anterior, mis disculpas, me he liado, pensé que estabas comentando otro post que recién publiqué 🙏

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...