Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Unique InvoiceIDs with prefix #87

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
150 changes: 93 additions & 57 deletions system/modules/li_crm/classes/Invoice.php
Expand Up @@ -21,6 +21,18 @@ class Invoice extends \BackendModule
* Template
*/
protected $strTemplate = 'be_invoice';

/**
* This current invoice's unique ID with eventual prefix
* @param string
*/
protected $strInvoiceId = '';

/**
* Name of the current table
* @var string
*/
protected $strTable = 'tl_li_invoice';

/**
* Generate the module
Expand All @@ -30,42 +42,42 @@ public function generate()
{
parent::generate();

$key = \Input::get('key');
$id = \Input::get('id');
$this->key = \Input::get('key');
$this->id = \Input::get('id');

if($key == 'paid')
if($this->key == 'paid')
{
$this->Template->success = $this->invoicePaid($id);
$this->Template->success = $this->invoicePaid();
}
elseif($key == 'print')
elseif($this->key == 'print')
{
$this->Template->filePath = $this->printInvoiceAsPDF($id);
$this->Template->filePath = $this->printInvoiceAsPDF();
}
elseif($key == 'html')
elseif($this->key == 'html')
{
$this->generateHtmlInvoice($id);
$this->generateHtmlInvoice();
}
elseif ($key == 'reports')
elseif ($this->key == 'reports')
{
$this->Template->graphData = $this->generateReports();
}
elseif ($key == 'send')
elseif ($this->key == 'send')
{
$this->Template->dispatchSuccessful = $this->sendInvoice($id);
$this->Template->dispatchSuccessful = $this->sendInvoice();
}
elseif($key == 'generation')
elseif($this->key == 'generation')
{
$generationId = $this->buildGeneration($id);
$generationId = $this->buildGeneration();
$this->redirect('contao/main.php?do=li_invoices&table=tl_li_invoice_generation&act=edit&id='.$generationId.'&rt='.REQUEST_TOKEN);
}
elseif ($key == 'pdf')
elseif ($this->key == 'pdf')
{
// Return the file and do not render the template
$this->returnFile($id);
$this->returnFile($this->id);
}

$this->Template->id = $id;
$this->Template->key = $key;
$this->Template->id = $this->id;
$this->Template->key = $this->key;

return $this->Template->parse();
}
Expand Down Expand Up @@ -106,7 +118,7 @@ public function generateAlias($varValue, \DataContainer $dc)
return $varValue;
}

public function generateAliasWithoutDC($title, $id)
public function generateAliasWithoutDC($title)
{
// Generate alias
$alias = standardize($title);
Expand All @@ -126,7 +138,7 @@ public function generateAliasWithoutDC($title, $id)
// Add ID to alias
if ($objAlias->numRows)
{
$alias .= '-'.$id;
$alias .= '-'.$this->id;
}
return $alias;
}
Expand Down Expand Up @@ -223,32 +235,56 @@ public function dispatchIcon($row, $href, $label, $title, $icon, $attributes)
}
}

public function getInvoiceCount($insertConfig)
private function generateInvoiceID()
{
$arrSplit = explode('::', $insertConfig);

if ($arrSplit[0] == 'countInvoices')
if ($this->strInvoiceId != '')
{
if (isset($arrSplit[1]))
return $this->strInvoiceId;
}

// !HOOK: generate a custom invoice ID
if (isset($GLOBALS['LICRM_HOOKS']['generateInvoiceId']) && is_array($GLOBALS['LICRM_HOOKS']['generateInvoiceId']))
{
foreach ($GLOBALS['LICRM_HOOKS']['generateInvoiceId'] as $callback)
{
$objInvoice = $this->Database->prepare("
SELECT COUNT(id) AS countInvoices
FROM tl_li_invoice
WHERE isOut = '1'")
->limit(1)
->executeUncached();

$count = $objInvoice->countInvoices;
$this->import($callback[0]);
$strInvoiceId = $this->$callback[0]->$callback[1]($this);

if (!empty($GLOBALS['TL_CONFIG']['li_crm_invoice_number_generation_start']))
if ($strInvoiceId !== false)
{
$count += $GLOBALS['TL_CONFIG']['li_crm_invoice_number_generation_start'];
$this->strInvoiceId = $strOrderId;
break;
}
return str_pad($count, $arrSplit[1], '0', STR_PAD_LEFT);
}
return false;
}
return false;

if ($this->strInvoiceId == '')
{
$objDatabase = \Database::getInstance();

$strPrefix = preg_replace('/\{\{countInvoices::(\d+)\}\}/i', "", $GLOBALS['TL_CONFIG']['li_crm_invoice_number_generation']);
$strPrefix = $this->replaceInsertTags($strPrefix, false);
$intPrefix = utf8_strlen($strPrefix);

preg_match_all('/\{\{countInvoices::(\d+)\}\}/i', $GLOBALS['TL_CONFIG']['li_crm_invoice_number_generation'], $matches);
$orderDigits = $matches[1][0];

// Lock tables so no other order can get the same ID
$objDatabase->lockTables(array($this->strTable => 'WRITE'));

// Retrieve the highest available order ID
$objMax = $objDatabase->prepare("SELECT invoiceNumber FROM {$this->strTable}". ($strPrefix != '' ? " WHERE invoiceNumber LIKE '$strPrefix%' " : '') . " ORDER BY CAST(" . ($strPrefix != '' ? ("SUBSTRING(invoiceNumber, " . ($intPrefix+1) . ")") : 'invoiceNumber') . " AS UNSIGNED) DESC")->limit(1)->executeUncached();
$intMax = (int) substr($objMax->invoiceNumber, $intPrefix);

$this->strInvoiceId = $strPrefix . str_pad($intMax+1, $orderDigits, '0', STR_PAD_LEFT);

$objDatabase->prepare("UPDATE {$this->strTable} SET invoiceNumber=? WHERE id={$this->id}")->executeUncached($this->strInvoiceId);

// Unlock table
$objDatabase->unlockTables();
}

return $this->strInvoiceId;
}

public function getAddressOptions(\DataContainer $dc)
Expand Down Expand Up @@ -358,12 +394,12 @@ public function getHourOptions(\MultiColumnWizard $mcw)
return $options;
}

public function printInvoiceAsPDF($id)
public function printInvoiceAsPDF()
{
// Log process
$this->log('Generate new pdf invoice', 'Generate invoice with id '.$id, TL_FILES);
$this->log('Generate new pdf invoice', 'Generate invoice with id '.$this->id, TL_FILES);

$data = $this->getInvoiceData($id,"pdf");
$data = $this->getInvoiceData($this->id,"pdf");
$strHtml = $data['html'];

$invoiceNumber = $data['invoiceNumber'];
Expand All @@ -375,7 +411,7 @@ public function printInvoiceAsPDF($id)
INNER JOIN tl_li_invoice_template AS t
ON i.toTemplate = t.id
WHERE i.id = ?
")->limit(1)->execute($id);
")->limit(1)->execute($this->id);

$dompdf = new \ContaoDOMPDF();
$dompdf->set_paper('a4');
Expand Down Expand Up @@ -432,13 +468,13 @@ public function printInvoiceAsPDF($id)
UPDATE tl_li_invoice
SET file = ?, invoiceNumber = ?, price = ?
WHERE id = ?
")->execute($filePath, $invoiceNumber, $fullNetto, $id);
")->execute($filePath, $invoiceNumber, $fullNetto, $this->id);

// Return link to template
return $templateLink;
}

private function getInvoiceData($id,$type)
private function getInvoiceData($type)
{
$this->import('Encryption');

Expand Down Expand Up @@ -478,7 +514,7 @@ private function getInvoiceData($id,$type)
LEFT JOIN tl_member AS m
ON i.toCustomer = m.id
WHERE i.id = ?
")->limit(1)->execute($id);
")->limit(1)->execute($this->id);

$objCustomerAddress = $this->Database->prepare("
SELECT company, firstname, lastname, street, postal, city, gender, country
Expand All @@ -499,7 +535,7 @@ private function getInvoiceData($id,$type)
$countries = $this->getCountries();
$country = $objCustomerAddress->country != $GLOBALS['TL_CONFIG']['li_crm_company_country'] ? $countries[$objCustomerAddress->country] : '';

$invoiceNumber = $objInvoice->invoiceNumber != '' ? $objInvoice->invoiceNumber : $this->replaceInsertTags($GLOBALS['TL_CONFIG']['li_crm_invoice_number_generation']);
$invoiceNumber = $objInvoice->invoiceNumber != '' ? $objInvoice->invoiceNumber : $this->generateInvoiceID();

$objLogo = \FilesModel::findByPk($objInvoice->logo);

Expand Down Expand Up @@ -1011,23 +1047,23 @@ private function generateReports()
return $graphData;
}

private function generateHtmlInvoice($id)
private function generateHtmlInvoice()
{
$data = $this->getInvoiceData($id,"html");
$data = $this->getInvoiceData($this->id,"html");
echo $data['html'];

exit;
}

public function sendInvoice($id)
public function sendInvoice()
{
$objInvoice = $this->Database->prepare("
SELECT i.invoiceDate, i.invoiceNumber, i.file, a.lastname, a.gender, a.email
FROM tl_li_invoice AS i
INNER JOIN tl_address AS a
ON a.id = i.toAddress
WHERE i.id = ?
")->limit(1)->execute($id);
")->limit(1)->execute($this->id);
try
{
$objEmail = new \Email();
Expand Down Expand Up @@ -1055,13 +1091,13 @@ private function getOddEven($row)
return $row % 2 == 0 ? 'odd' : 'even';
}

private function returnFile($id)
private function returnFile()
{
$objInvoice = $this->Database->prepare("
SELECT file AS pdfFile
FROM tl_li_invoice
WHERE id = ?
")->limit(1)->execute($id);
")->limit(1)->execute($this->id);

$path = '../'.$objInvoice->pdfFile;

Expand All @@ -1071,14 +1107,14 @@ private function returnFile($id)
readfile($path);
}

public function returnFileForFrontend($id)
public function returnFileForFrontend()
{
$this->import('FrontendUser', 'User');
$objInvoice = $this->Database->prepare("
SELECT toCustomer, file AS pdfFile
FROM tl_li_invoice
WHERE id = ?
")->limit(1)->execute($id);
")->limit(1)->execute($this->id);
if ($this->User->id != '')
{
if ($objInvoice->toCustomer == $this->User->id)
Expand Down Expand Up @@ -1178,20 +1214,20 @@ public function togglePaidIcon($row, $href, $label, $title, $icon, $attributes)
}
}

public function invoicePaid($id)
public function invoicePaid()
{
$objInvoice = $this->Database->prepare("
SELECT paid
FROM tl_li_invoice
WHERE id = ?
")->limit(1)->execute($id);
")->limit(1)->execute($this->id);
$this->Database->prepare("
UPDATE tl_li_invoice
SET paid = ?
WHERE id = ?
")->execute(
$objInvoice->paid == 1 ? 0 : 1,
$id
$this->id
);
return true;
}
Expand Down
1 change: 0 additions & 1 deletion system/modules/li_crm/config/config.php
Expand Up @@ -125,7 +125,6 @@
// - Replace insert tags
$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = array('LiCRM\Customer', 'getCustomerCount');
$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = array('LiCRM\Project', 'getProjectCount');
$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = array('LiCRM\Invoice', 'getInvoiceCount');

// - Registration
$GLOBALS['TL_HOOKS']['createNewUser'][] = array('LiCRM\CustomerRegistration', 'createNewUser');
Expand Down