Jump to content

[solved] Generate invoices


Recommended Posts

I need to generate invoices for some orders that I have imported into the prestashop database.

I use the following function:

    public static function generateInvoice()
    {
        $result = Db::getInstance()->executeS("SELECT `id_order` FROM `" . _DB_PREFIX_ . "orders` WHERE `id_order` BETWEEN 10 AND 100");
        foreach ($result as $order) {
            $order = new Order((int)$order['id_order']);
            if (Validate::isModuleName($order->module)) {
                $order->setInvoice(true);
            }
        }
    }


Would this be a good solution, or is there a better and smoother way to generate invoices?

Thanks,
Fredrik

Link to comment
Share on other sites

Thanks SmartDataSoft!
Your help and input is very much appreciated.

I noticed that orders with already existing invoice could be generated once again, so I end up with multiply rows with the same id_order in the table ps_order_invoice.
So I made a small addition to the function. It might not be the best optimized way, but I guess it will work.

 

    public static function generateInvoice()
    {
        $result = Db::getInstance()->executeS("SELECT `id_order` FROM `" . _DB_PREFIX_ . "orders` WHERE `id_order` BETWEEN 1 AND 90");
        foreach ($result as $order) {

            $oid = $order['id_order'];

            $check_invoces = Db::getInstance()->executeS("SELECT `id_order` FROM `" . _DB_PREFIX_ . "order_invoice` WHERE `id_order` = " . $oid);
            $match  = mysqli_num_rows($check_invoces);

                    if ($match > 0) {
                        continue;
                    } else {

                        $order = new Order((int)$order['id_order']);
                        if (Validate::isModuleName($order->module)) {
                            $order->setInvoice(true);
                        }

                    }                
        }
    }

 

Link to comment
Share on other sites

Hello, you can count the result like bellow 


if ($results = Db::getInstance()->executeS("SELECT `id_order` FROM `" . _DB_PREFIX_ . "order_invoice` WHERE `id_order` = " . $oid)){

$order = new Order((int)$order['id_order']);
                        if (Validate::isModuleName($order->module)) {
                            $order->setInvoice(true);
                        }
}

ref: https://doc.prestashop.com/pages/viewpage.action?pageId=51184692

it is not good to use mysqli_num_rows. as you used Db class. Db class call internal db function depend on database it may be mysql or other database.

 

thank you

  • Thanks 1
Link to comment
Share on other sites

  • redrum changed the title to [solved] Generate invoices

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