Page 1 of 1

Generate invoice number when order status is 'complete'

Posted: Sun Oct 18, 2015 7:10 am
by snoopster
Greetings!

I have the following code:

Code: Select all

public function createInvoiceNo($order_id) {
		$this->load->model('checkout/order');
		$order_info = $this->model_checkout_order->getOrder($order_id);

		if ($order_info && !$order_info['invoice_no']) {
			$query = $this->db->query("SELECT MAX(invoice_no) AS invoice_no FROM `" . DB_PREFIX . "order` WHERE invoice_prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "'");

			if ($query->row['invoice_no']) {
				$invoice_no = $query->row['invoice_no'] + 1;
			} else {
				$invoice_no = 1;
			}

			$this->db->query("UPDATE `" . DB_PREFIX . "order` SET invoice_no = '" . (int)$invoice_no . "', invoice_prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "' WHERE order_id = '" . (int)$order_id . "'");

			return $order_info['invoice_prefix'] . $invoice_no;
		}
	}
I am trying to auto-generate the invoice number only when the order status is 'complete'.

Anybody has any idea what must be changed?

Opencart v. 2.0.3.1

Kind regards!

Re: Generate invoice number when order status is 'complete'

Posted: Sun Oct 18, 2015 10:14 pm
by straightlight
That would be the wrong location. The model initiation would be where the action would initially dictate where the invoice number needs to be created at that point.

In admin/controller/sale/order.php file,

find:

Code: Select all

public function createInvoiceNo() {
replace:

Code: Select all

$this->load->model('sale/order');

$invoice_no = $this->model_sale_order->createInvoiceNo($order_id);

if ($invoice_no) {
				$json['invoice_no'] = $invoice_no;
			} else {
				$json['error'] = $this->language->get('error_action');
			}
with:

Code: Select all

$this->load->model('sale/order');
			
			$order_info = $this->model_sale_order->getOrder($order_id);
			
			if (!empty($order_info['order_status_id']) && (int)$order_info['order_status_id'] == (int)$this->config->get('config_complete_status_id')) {
                                $invoice_no = $this->model_sale_order->createInvoiceNo($order_id);
			}
			
			if (!empty($invoice_no)) {
				$json['invoice_no'] = $invoice_no;
			} else {
				$json['error'] = $this->language->get('error_action');
			}
Tested on OC v2.1.0.1 release.

Re: Generate invoice number when order status is 'complete'

Posted: Mon Oct 19, 2015 12:41 am
by snoopster
Thank you for your reply!

Did you mean admin/controller/sale/order.php ?

I replaced the code you provided, but it did not work for me...

Is it possible that because I modified the Complete status to 'Paid' status, this code alteration would not work?(I only modified it in Localisation -> Order Statuses)

I am using this extension: http://www.opencart.com/index.php?route ... n_id=20734 to auto-generate the invoice number, but it generates the invoice number regardless of the order status while the order is placed by the customer.

This is the code in catalog/controller/module/autoinvoicenumber.php

Code: Select all

<?php
class ControllerModuleAutoInvoiceNumber extends Controller {
	private $error = array();

    public function on_order_add($order_id) {

		$mod_status = $this->config->get('autoinvoicenumber_status');
		if($mod_status)
		{
			$invoice_no = $this->createInvoiceNo($order_id);

			if ($invoice_no) {
				// successful generation of invoice number
			} else {
				$log->write('AutoInvoiceNumber - Failed to generate invoice number for order Id : ' .$order_id );
			}
		}
    }

	// can not load admin model classes in catalog.
	// therefore duplicating the code here from admin/model/sale/order.php
	public function createInvoiceNo($order_id) {
		$this->load->model('checkout/order');
		$order_info = $this->model_checkout_order->getOrder($order_id);

		if ($order_info && !$order_info['invoice_no']) {
			$query = $this->db->query("SELECT MAX(invoice_no) AS invoice_no FROM `" . DB_PREFIX . "order` WHERE invoice_prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "'");

			if ($query->row['invoice_no']) {
				$invoice_no = $query->row['invoice_no'] + 1;
			} else {
				$invoice_no = 1;
			}

			$this->db->query("UPDATE `" . DB_PREFIX . "order` SET invoice_no = '" . (int)$invoice_no . "', invoice_prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "' WHERE order_id = '" . (int)$order_id . "'");

			return $order_info['invoice_prefix'] . $invoice_no;
		}
	}
}
Can this code be edited to generate the invoice number only when the status of the order is 'Paid'(Complete)?

Kind regards!

Re: Generate invoice number when order status is 'complete'

Posted: Mon Oct 19, 2015 1:44 am
by straightlight
Did you mean admin/controller/sale/order.php ?
Correct. Location edited above.
$mod_status = $this->config->get('autoinvoicenumber_status');
if($mod_status)
{
This is causing the issue, actually. You'd need to remove the additional:

Code: Select all

}
along with it, in the mean time. Then, to apply my modification. This should work as expected, afterwards.

Re: Generate invoice number when order status is 'complete'

Posted: Mon Oct 19, 2015 2:28 am
by snoopster
The edited code for autoinvoicenumber.php:

Code: Select all

<?php
class ControllerModuleAutoInvoiceNumber extends Controller {
	private $error = array();

    public function on_order_add($order_id) {

		//$mod_status = $this->config->get('autoinvoicenumber_status');
		//if($mod_status)
		//{
			$invoice_no = $this->createInvoiceNo($order_id);

			if ($invoice_no) {
				// successful generation of invoice number
			} else {
				$log->write('AutoInvoiceNumber - Failed to generate invoice number for order Id : ' .$order_id );
			}
		//}
    }

	// can not load admin model classes in catalog.
	// therefore duplicating the code here from admin/model/sale/order.php
	public function createInvoiceNo($order_id) {
		$this->load->model('checkout/order');
		$order_info = $this->model_checkout_order->getOrder($order_id);

		if ($order_info && !$order_info['invoice_no']) {
			$query = $this->db->query("SELECT MAX(invoice_no) AS invoice_no FROM `" . DB_PREFIX . "order` WHERE invoice_prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "'");

			if ($query->row['invoice_no']) {
				$invoice_no = $query->row['invoice_no'] + 1;
			} else {
				$invoice_no = 1;
			}

			$this->db->query("UPDATE `" . DB_PREFIX . "order` SET invoice_no = '" . (int)$invoice_no . "', invoice_prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "' WHERE order_id = '" . (int)$order_id . "'");

			return $order_info['invoice_prefix'] . $invoice_no;
		}
	}
}

It still generates the invoice number regardless of the order status.

I have applied your code to order.php, but it did not work(with the auto-invoice-number extension enabled or disabled) - it did not generate the invoice number at all.

Could the above extension be further edited, with something like:

Code: Select all

if ($order_info && !$order_info['invoice_no']) {
to

Code: Select all

if ($order_info && !$order_info['invoice_no'] && (int)$order_info['order_status_id'] == (int)$this->config->get('config_complete_status_id')) {
?

Re: Generate invoice number when order status is 'complete'

Posted: Mon Oct 19, 2015 2:36 am
by straightlight
with the auto-invoice-number extension enabled or disabled
If you're using an extension inclusively, which would affect the created invoice number, then it would be best to contact the author of the contribution and to ask for this request to be implemented.