redrum Posted August 4, 2021 Share Posted August 4, 2021 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 More sharing options...
SmartDataSoft Posted August 4, 2021 Share Posted August 4, 2021 Hello, which version of PrestaShop you are using. Thank you Link to comment Share on other sites More sharing options...
redrum Posted August 4, 2021 Author Share Posted August 4, 2021 1 hour ago, SmartDataSoft said: Hello, which version of PrestaShop you are using. Sorry, I missed that. It's 1.7.7.5 Link to comment Share on other sites More sharing options...
SmartDataSoft Posted August 4, 2021 Share Posted August 4, 2021 Hello, After view other forum post and your code. Its looks ok to me to set invoice as you set limit of the oder. Thank you 1 Link to comment Share on other sites More sharing options...
SmartDataSoft Posted August 4, 2021 Share Posted August 4, 2021 I found a blog post it's code may help you https://www.prestasoo.com/blog/how-to-generate-prestashop-invoice-by-customer-name.html Thank you 1 Link to comment Share on other sites More sharing options...
redrum Posted August 4, 2021 Author Share Posted August 4, 2021 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 More sharing options...
SmartDataSoft Posted August 4, 2021 Share Posted August 4, 2021 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 1 Link to comment Share on other sites More sharing options...
redrum Posted August 4, 2021 Author Share Posted August 4, 2021 Ah, ok, awesome! Thank you so much! Link to comment Share on other sites More sharing options...
SmartDataSoft Posted August 4, 2021 Share Posted August 4, 2021 @redrum Change the title as [solve] if your issue is solved and do not forget to click the love icon Thank you Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now