Skip to content
Snippets Groups Projects
Commit c82c6858 authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Initial version, works already

parents
No related branches found
No related tags found
No related merge requests found
Makefile 0 → 100644
BASE=ordernumber
PLUGINTYPE=vmshopper
VERSION=1.0
PLUGINFILES=$(BASE).php $(BASE).xml
TRANSDIR=../../../administrator/language/
TRANSLATIONS=$(TRANSDIR)/en-GB/en-GB.plg_$(PLUGINTYPE)_$(BASE).sys.ini
ZIPFILE=plg_$(PLUGINTYPE)_$(BASE)_v$(VERSION).zip
zip: $(PLUGINFILES) $(TRANSLATIONS)
zip -r $(ZIPFILE) $(PLUGINFILES)
zip -r --junk-paths $(ZIPFILE) $(TRANSLATIONS)
<?php
defined('_JEXEC') or die( 'Direct Access to ' . basename( __FILE__ ) . ' is not allowed.' ) ;
/**
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
*
*
*/
if (!class_exists('vmShopperPlugin')) require(JPATH_VM_PLUGINS . DS . 'vmshopperplugin.php');
class plgVmShopperOrdernumber extends vmShopperPlugin {
function __construct(& $subject, $config) {
parent::__construct($subject, $config);
/* Create the database table */
$this->tableFields = array_keys ($this->getTableSQLFields ());
$this->onStoreInstallPluginTable($this->_psType);
}
public function getVmPluginCreateTableSQL () {
return $this->createTableSQL ('VM Shopper plugin: custom order and invoice numbers');
}
function getTableSQLFields () {
$SQLfields = array(
'id' => 'int(11) UNSIGNED NOT NULL AUTO_INCREMENT',
'number_type' => 'int(1) UNSIGNED',
'number_format' => 'varchar(255)',
'count' => 'int(1)',
);
return $SQLfields;
}
// We don't need this function, but the parent class declares it abstract, so we need to overload
function plgVmOnUpdateOrderBEShopper($_orderID) {}
/* Return a random "string" of the given length taken from the given alphabet */
static function randomString($alphabet, $len) {
$alen = strlen($alphabet);
$r = "";
for ($n=0; $n<$len; $n++) {
$r .= $alphabet[mt_rand(0, $alen-1)];
}
return $r;
}
/* Replace the format variables, match[1] is the variable name, match[2] and match[3] are only used for random fields */
function variable_replacement ($match) {
$varname = strtolower($match[1]);
switch ($varname) {
case "year": return date ("Y");
case "year2": return date ("y");
case "month": return date("m");
case "day": return date("d");
case "hour": return date("H");
case "hour12": return date("h");
case "ampm": return date("a");
case "minute": return date("i");
case "second": return date("s");
case "random":
/* the regexp matches (random)(Type)(Len) as match 1 to 3, Len is optional */
$len = ($match[3]?$match[3]:1);
// Fallback: If no Type is given, use Digit
$alphabet = "0123456789";
// Select the correct alphabet depending on Type
switch (strtolower($match[2])) {
case "digit": $alphabet = "0123456789"; break;
case "hex": $alphabet = "0123456789abcdef"; break;
case "letter": $alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; break;
case "uletter": $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; break;
case "lletter": $alphabet = "abcdefghijklmnopqrstuvwxyz"; break;
case "alphanum": $alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; break;
}
return self::randomString ($alphabet, $len);
}
// No variable type matched, so don't replace, return the original string
return $match[0];
}
function replace_fields ($fmt, $orderDetails) {
// Match variables for the form random[type][count] and everything else.
// This makes it possible to handle "random" just like any other type!
return preg_replace_callback (':\[(?|(random)(.*?)([0-9]*?)|([^\]]+))\]:', 'self::variable_replacement', $fmt);
}
/* Type 0 means order number, type 1 means invoice number */
function format_number ($fmt, $orderDetails, $nrtype = 0, $global = 1) {
// First, replace all variables:
$nr = $this->replace_fields ($fmt, $orderDetails);
// Look up the current counter
$db = JFactory::getDBO();
/* For global counting, simply read the empty number_format entries! */
$q = 'SELECT `count` FROM `'.$this->_tablename.'` WHERE `number_type`='.(int)$nrtype.' AND `number_format`="'.($global?"":$nr).'"';
$db->setQuery($q);
$existing = $db->loadResult();
$count = $existing?($existing+1):1;
// Insert new counter value into the db
if ($existing) {
$q = 'UPDATE `'.$this->_tablename.'` SET `count`= "'.$count.'" WHERE `number_type`='.(int)$nrtype.' AND `number_format`="'.($global?"":$nr).'"';
} else {
$q = 'INSERT INTO `'.$this->_tablename.'` (`count`, `number_type`, `number_format`) VALUES ('.(int)$count.','.(int)$nrtype.', "'.($global?"":$nr).'")';
}
$db->setQuery( $q );
$db->query();
// return the format with the counter inserted
return str_replace ("#", $count, $nr);
}
function plgVmOnUserOrder(&$orderDetails/*,&$data*/) {
// Is order number customization enabled?
if ($this->params->get('customize_order_number')) {
$nrtype=0; /*order-nr*/
$fmt = $this->params->get ('order_number_format', "#");
$global = $this->params->get ('order_number_global', 1);
$ordernr = $this->format_number ($fmt, $orderDetails, $nrtype, $global);
$orderDetails->order_number = $ordernr;
}
// Is order password customization enabled?
if ($this->params->get('customize_order_password')) {
$fmt = $this->params->get ('order_password_format', "#");
$passwd = $this->replace_fields ($fmt, $orderDetails);
$orderDetails->order_pass = $passwd;
}
}
function plgVmOnUserInvoice($orderDetails,&$data) {
// Is order number customization enabled?
if ($this->params->get('customize_invoice_number')) {
$nrtype=1; /*invoice-nr*/
$fmt = $this->params->get ('invoice_number_format', "#");
$global = $this->params->get ('invoice_number_global', 1);
$invoicenr = $this->format_number ($fmt, $orderDetails, $nrtype, $global);
$data['invoice_number'] = $invoicenr;
}
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<install version="1.5" type="plugin" group="vmshopper">
<name>VM - Custom Order and Invoice Numbers</name>
<creationDate>November 2012</creationDate>
<author>Reinhold Kainhofer</author>
<authorUrl>http://www.kainhofer.com/</authorUrl>
<copyright>Copyright (C) 2012 Reinhold Kainhofer. All rights reserved.</copyright>
<license>http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL</license>
<version>1.0.0</version>
<description>This plugin is used to create custom order and invoice numbers for Virtuemart 2
&lt;div style="font-weight: normal;"&gt;
&lt;p style="font-weight: normal;"&gt;
The format of the order and invoice numbers is a free-form text string, where &lt;tt&gt;#&lt;/tt&gt; indicates the running counter and &lt;tt&gt;[variable]&lt;/tt&gt; is understood as a variable and replaced by its current value. Currently, the following variables are available (case-insensitive):&lt;table border=1&gt;
&lt;tr&gt;&lt;td&gt;year&lt;/td&gt;&lt;td&gt;Current year (4 digits)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;year2&lt;/td&gt;&lt;td&gt;Current year (2 digits)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;month&lt;/td&gt;&lt;td&gt;Current month (2 digits); leading zeros if necessary&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;day&lt;/td&gt;&lt;td&gt;Current day (2 digits); leading zeros if necessary&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;hour&lt;/td&gt;&lt;td&gt;Current hour in 24-hour format; leading zeros if necessary&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;hour12&lt;/td&gt;&lt;td&gt;Current hour in 12-hour format; leading zeros if necessary&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;ampm&lt;/td&gt;&lt;td&gt;Current am-pm (for 12-hour format) in lower-case&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;minute&lt;/td&gt;&lt;td&gt;Current minute; leading zeros if necessary&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;second&lt;/td&gt;&lt;td&gt;Current second; leading zeros if necessary&lt;/td&gt;&lt;/tr&gt;
&lt;!-- &lt;tr&gt;&lt;td&gt;customer&lt;/td&gt;&lt;td&gt;The customer ID of the order&lt;/td&gt;&lt;/tr&gt; --&gt;
&lt;tr&gt;&lt;td&gt;randomDigit[n]&lt;/td&gt;&lt;td&gt;Random sequences of n decimal digits (n=1 if not given).&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;randomHex[n]&lt;/td&gt;&lt;td&gt;Random sequences of n hexadecimal digits (n=1 if not given).&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;randomLetter[n]&lt;/td&gt;&lt;td&gt;Random sequences of n (upper- and lowercase) letters (n=1 if not given).&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;randomULetter[n]&lt;/td&gt;&lt;td&gt;Random sequences of n uppercase letters (n=1 if not given).&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;randomLLetter[n]&lt;/td&gt;&lt;td&gt;Random sequences of n lowercase letters (n=1 if not given).&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;randomAlphanum[n]&lt;/td&gt;&lt;td&gt;Random sequences of n general alphanumeric characters (A-Z, a-z, 0-9) (n=1 if not given).&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
</description>
<languages>
<language tag="en-GB">en-GB.plg_vmshopper_ordernumber.sys.ini</language>
</languages>
<files>
<filename plugin="ordernumber">ordernumber.php</filename>
</files>
<config>
<fields name="params">
<fieldset name="basic">
<field name="order_options" type="spacer" label="&lt;b&gt;Order numbers&lt;/b&gt;" />
<field name="customize_order_number" type="radio" default="0" label="Customize order numbers" description="Check here if you want customized order number formats rather than the default.">
<option value="1">Yes</option>
<option value="0">No</option>
</field>
<field name="order_number_format" type="text" default="Order-[year][month]-#" label="Format of the order numbers" description="Here you can customize the order number"/>
<field name="order_number_global" type="radio" default="0" label="Counter" description="Choose whether you want one global counter or per-format value counters. E.g. with a format of '[year]-#' and the latter option checked, the order count will be within the year only.">
<option value="1">Global</option>
<option value="0">Separate counter per format value</option>
</field>
<field name="password_options" type="spacer" label="&lt;b&gt;Order password&lt;/b&gt;" />
<field name="customize_order_password" type="radio" default="0" label="Customize order password" description="Check here if you want customized order password formats rather than the default.">
<option value="1">Yes</option>
<option value="0">No</option>
</field>
<field name="order_password_format" type="text" default="p_[randomHex5]" label="Format of the order password" description="Here you can customize the order password (# is NOT replaced)"/>
<field name="invoice_options" type="spacer" label="&lt;b&gt;Invoice numbers&lt;/b&gt;" />
<field name="customize_invoice_number" type="radio" default="0" label="Customize invoice numbers" description="Check here if you want customized invoice number formats rather than the default.">
<option value="1">Yes</option>
<option value="0">No</option>
</field>
<field name="invoice_number_format" type="text" default="[year]/#" label="Format of the invoice numbers" description="Here you can customize the invoice number"/>
<field name="invoice_number_global" type="radio" default="0" label="Counter" description="Choose whether you want one global counter or per-format value counters. E.g. with a format of '[year]-#' and the latter option checked, the invoice count will be within the year only.">
<option value="1">Global</option>
<option value="0">Separate counter per format value</option>
</field>
</fieldset>
</fields>
</config>
</install>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment