Jump to content

[Solved] Customize how Prestashop assigns invoicing dates


Eutanasio

Recommended Posts

Hi,

I am looking to modify how Prestashop 1.7 assigns the invoice date. The desired logic is:

- If the order is fully paid, the invoice date should be the date of the last recorded payment.
- If not, it should continue using the current auto-assigned date when the invoice is generated.

I tried modifying `OrderInvoice.php` with the following code without success. The invoice is still generated with the current date.

Here is the original code:

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();
}

Here is the modification I attempted:

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

    // Initialize addition date
    $invoice_date = date('Y-m-d H:i:s');

    // Check if the order is fully paid
    if ($order->getCurrentState() == Configuration::get('PS_OS_PAYMENT')) {
        // Get the date of the last recorded payment
        $payments = OrderPayment::getByOrderReference($order->reference);
        if (!empty($payments)) {
            $last_payment = end($payments);
            $invoice_date = $last_payment->date_add;
        }
    }

    // Assign the invoice date
    $this->date_add = $invoice_date;

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

Can anyone indicate the correct method or the appropriate approach to achieve this? Any help would be greatly appreciated.

Thanks in advance!

Link to comment
Share on other sites

I've solved the issue with customizing the invoice date in PrestaShop 1.7. Here is the final solution that worked for me:

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

  • Eutanasio changed the title to [Solved] Customize how Prestashop assigns invoicing dates

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...