Skip to content

Instantly share code, notes, and snippets.

@mneuhaus
Created October 14, 2015 20:26
Show Gist options
  • Select an option

  • Save mneuhaus/74aa94d50b0f60bbb064 to your computer and use it in GitHub Desktop.

Select an option

Save mneuhaus/74aa94d50b0f60bbb064 to your computer and use it in GitHub Desktop.
Eel NumberGenerator
<?php
namespace CE\Devis\Invoice;
/* *
* This script belongs to the TYPO3 Flow framework. *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use CE\Devis\Domain\Model\InvoiceModule;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Http\Response;
use TYPO3\Flow\Mvc\ActionRequest;
use TYPO3\Flow\Mvc\RequestMatcher;
use TYPO3\Flow\Security\Account;
/**
*
*/
class NumberGenerator {
/**
* @var InvoiceModule
*/
protected $module;
/**
* @var array
*/
protected $parts = array();
/**
* @param InvoiceModule $request
*/
public function __construct($module = NULL) {
$this->module = $module;
}
public function reset() {
$this->parts = array();
}
public function getNumber() {
return implode('', $this->parts);
}
public function cycleCounter($start, $step, $resetCycle) {
$lastNumber = $this->module->getLastInvoiceNumber();
$lastDate = new \DateTime($this->module->getLastInvoiceDate());
if ($lastDate->format($resetCycle) !== date($resetCycle)) {
$this->module->setLastInvoiceDate(date('d.m.Y'));
$this->module->setLastInvoiceNumber($start);
$nextNumber = $start;
} else {
$nextNumber = $lastNumber + $step;
$this->module->setLastInvoiceNumber($nextNumber);
}
$this->parts[] = $nextNumber;
return $this;
}
public function counter($start, $step) {
$lastNumber = $this->module->getLastInvoiceNumber();
if ($lastNumber < $start) {
$this->module->setLastInvoiceDate(date('d.m.Y'));
$this->module->setLastInvoiceNumber($start);
$nextNumber = $start;
} else {
$nextNumber = $lastNumber + $step;
$this->module->setLastInvoiceNumber($nextNumber);
}
$this->parts[] = $nextNumber;
return $this;
}
public function numberFormat($length, $decimals) {
$lastPart = array_pop($this->parts);
$number = number_format($lastPart, $decimals);
$this->parts[] = str_pad($number, $length, '0', STR_PAD_LEFT);
return $this;
}
public function date($format) {
$this->parts[] = date($format);
return $this;
}
public function text($text) {
$this->parts[] = $text;
return $this;
}
}
?>
<?php
class foo {
public function generateInvoiceNumber() {
$generator = new NumberGenerator($this);
$context = new Context($generator);
$eelEvaluator = new CompilingEvaluator();
$eelEvaluator->evaluate($this->invoiceNumberFormat, $context);
return $generator->getNumber();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment