J2Store4 / j2store4

J2Store 4 - Open Source eCommerce extension for Joomla
https://www.j2store.org
GNU General Public License v3.0
15 stars 7 forks source link

Generate invoice number in hexadecimal format #82

Closed mehdininja closed 3 months ago

mehdininja commented 4 months ago

hi The invoice number is AUTO_INCREMENT, I want the invoice number to be generated as a random hexadecimal string, Which files should I edit?

mgopin3 commented 4 months ago

Hello @mehdininja

If you wish to generate a unique hexadecimal string as a prefix for your invoice numbers, you can easily achieve this through the J2Store Invoice Number Generator app. Here's a step-by-step guide:

  1. Navigate to J2Store -> Apps.
  2. Search for the "Invoice Number Generator" app and enter its settings.
  3. In the "Basic Settings" tab, locate the "Allow custom invoice prefix" option and set it to "Yes."
  4. Scroll down to find the "Invoice Prefix" field. Here, you can enter the desired value for the invoice number prefix that will be generated on your site.

Once you've completed these steps, the invoice numbers will be generated with the specified prefix after each purchase. For a visual guide, you can refer to this instructional video: [Video link]

mehdininja commented 4 months ago

Hello, I like this method : Edit => administrator/components/com_j2store/tables/order.php

public function generateInvoiceNumber ()
    {
        if ( empty( $this->order_id ) ) return;

        $db = JFactory::getDbo ();
        $unique = false;
        while (!$unique) {

            $invoice_number = 
                strtoupper(
                    str_pad(dechex(rand(0, 255)), 2, '0', STR_PAD_LEFT) . 
                    str_pad(dechex(rand(0, 255)), 2, '0', STR_PAD_LEFT) . 
                    str_pad(dechex(rand(0, 255)), 2, '0', STR_PAD_LEFT)
                );

            $query = $db->getQuery(true)
                        ->select('COUNT(invoice_number)')
                        ->from($db->quoteName('#__j2store_orders'))
                        ->where($db->quoteName('invoice_number') . ' = ' . $db->quote($invoice_number));
            $db->setQuery($query);

            $count = $db->loadResult();
            if ($count == 0) {
                $unique = true;
            $this->invoice_number = $invoice_number;
            }
        }
            $order = $this;
            J2Store::plugin ()->event ( 'AfterGenerateInvoiceNumber', array(&$order) );
    }

Sample invoice number: A48C92 512AC7 9F3C30

mgopin3 commented 4 months ago

@mehdininja I understand your concern. Typically, if a user wishes to generate an invoice number according to their preferences, we would recommend using an invoice generator app. If you prefer generating an invoice number as a hexadecimal value using custom code, we do not recommend making changes directly to the core files. Instead, you can utilize our custom code app. You can find this app at the file path /plugins/j2store/app_customcode/app_customcode.php. Within this file, you can write your custom function. After implementing this, if your custom code works as intended, you can proceed with it. It will not affect your core code files.

Here is the download link for custom-code App [link]

mehdininja commented 4 months ago

@mgopin3 how to add generateInvoiceNumber () to app_customcode.php ?

mgopin3 commented 3 months ago

@mehdininja Currently, we are offering J2Store 4.x.x free of charge, which restricts us from engaging in custom development. Additionally, we have not tested the custom generateInvoiceNumber code modifications in the local environment provided by you. Should you wish to implement this, we suggest engaging a third-party developer. You can locate the application at the file path within your backend code: /plugins/j2store/app_customcode/app_customcode.php. Within this file, you can integrate your custom function. Once successful implementation and validation of your custom code have been completed, you may proceed with confidence, assured that it will not impact your core code files.

mgopin3 commented 3 months ago

@mehdininja We believe the reported issue has been addressed. Could you please confirm that the problem is resolved on your end? If everything looks good, we'll go ahead and close this issue.

If you have any further questions or if there's anything else you'd like assistance with, please don't hesitate to let us know. We appreciate your feedback!