Post by snoopster » Sun Oct 18, 2015 7:10 am

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!

New member

Posts

Joined
Mon Nov 28, 2011 7:06 am

Post by straightlight » Sun Oct 18, 2015 10:14 pm

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.
Last edited by straightlight on Mon Oct 19, 2015 1:42 am, edited 1 time in total.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by snoopster » Mon Oct 19, 2015 12:41 am

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!

New member

Posts

Joined
Mon Nov 28, 2011 7:06 am

Post by straightlight » Mon Oct 19, 2015 1:44 am

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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by snoopster » Mon Oct 19, 2015 2:28 am

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')) {
?

New member

Posts

Joined
Mon Nov 28, 2011 7:06 am

Post by straightlight » Mon Oct 19, 2015 2:36 am

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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 98 guests