Skip to content

Instantly share code, notes, and snippets.

@inogo
Created December 9, 2015 18:59
Show Gist options
  • Save inogo/a0f563513d21234b4e79 to your computer and use it in GitHub Desktop.
Save inogo/a0f563513d21234b4e79 to your computer and use it in GitHub Desktop.
<?php
class InvoiceGenerator
{
/**
* @var modX
*/
public $modx;
public $file_name_prefix = 'invoice_';
public function __construct(modX &$modx) {
$this->modx = &$modx;
}
protected function num2str($num) {
$nul='ноль';
$ten=array(
array('','один','два','три','четыре','пять','шесть','семь', 'восемь','девять'),
array('','одна','две','три','четыре','пять','шесть','семь', 'восемь','девять'),
);
$a20=array('десять','одиннадцать','двенадцать','тринадцать','четырнадцать' ,'пятнадцать','шестнадцать','семнадцать','восемнадцать','девятнадцать');
$tens=array(2=>'двадцать','тридцать','сорок','пятьдесят','шестьдесят','семьдесят' ,'восемьдесят','девяносто');
$hundred=array('','сто','двести','триста','четыреста','пятьсот','шестьсот', 'семьсот','восемьсот','девятьсот');
$unit=array( // Units
array('копейка' ,'копейки' ,'копеек', 1),
array('рубль' ,'рубля' ,'рублей' ,0),
array('тысяча' ,'тысячи' ,'тысяч' ,1),
array('миллион' ,'миллиона','миллионов' ,0),
array('миллиард','милиарда','миллиардов',0),
);
//
list($rub,$kop) = explode('.',sprintf("%015.2f", floatval($num)));
$out = array();
if (intval($rub)>0) {
foreach(str_split($rub,3) as $uk=>$v) { // by 3 symbols
if (!intval($v)) continue;
$uk = sizeof($unit)-$uk-1; // unit key
$gender = $unit[$uk][3];
list($i1,$i2,$i3) = array_map('intval',str_split($v,1));
// mega-logic
$out[] = $hundred[$i1]; # 1xx-9xx
if ($i2>1) $out[]= $tens[$i2].' '.$ten[$gender][$i3]; # 20-99
else $out[]= $i2>0 ? $a20[$i3] : $ten[$gender][$i3]; # 10-19 | 1-9
// units without rub & kop
if ($uk>1) $out[]= $this->morph($v,$unit[$uk][0],$unit[$uk][1],$unit[$uk][2]);
} //foreach
}
else $out[] = $nul;
$out[] = $this->morph(intval($rub), $unit[1][0],$unit[1][1],$unit[1][2]); // rub
$out[] = $kop.' '.$this->morph($kop,$unit[0][0],$unit[0][1],$unit[0][2]); // kop
return trim(preg_replace('/ {2,}/', ' ', join(' ',$out)));
}
protected function morph($n, $f1, $f2, $f5) {
$n = abs(intval($n)) % 100;
if ($n>10 && $n<20) return $f5;
$n = $n % 10;
if ($n>1 && $n<5) return $f2;
if ($n==1) return $f1;
return $f5;
}
function ucfirst($str) {
return mb_strtoupper(mb_substr($str, 0, 1)) . mb_substr($str, 1);
}
public function generate($order, $template_path) {
if (!is_object($order) || !file_exists($template_path))
return;
$orderData = $order->toArray();
$contacts = unserialize($orderData['contacts']);
$items = array();
$i = 1;
foreach (unserialize($orderData['content']) as $item) {
$items[] = array(
'no' => $i++,
'name' => $item['name'],
'qty' => $item['count'],
'price' => $item['price'],
'sum' => $item['price'] * $item['count'],
);
}
if ($contacts['delivery_cost'] > 0) {
$items[] = array(
'no' => $i++,
'name' => 'Услуга доставки',
'qty' => 1,
'price' => $contacts['delivery_cost'],
'sum' => $contacts['delivery_cost'],
);
}
$orderTime = strtotime($orderData['date']);
setlocale(LC_TIME, 'ru_RU.UTF-8');
$data['date'] = strftime('%e %B %Y', $orderTime);
$data['orderno'] = $orderData['id'];
$data['org_name'] = $contacts['org_name'];
$data['org_inn'] = $contacts['org_inn'];
$data['total'] = $contacts['payment_price'];
$data['total_text'] = $this->ucfirst($this->num2str($contacts['payment_price']));
require_once($this->modx->getOption('core_path') . 'components/invoice/tbs/tbs_class.php');
require_once($this->modx->getOption('core_path') . 'components/invoice/tbs/tbs_plugin_opentbs.php');
$TBS = new clsTinyButStrong();
$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);
$TBS->LoadTemplate($template_path, OPENTBS_ALREADY_UTF8);
$TBS->MergeField('data', $data);
$TBS->MergeBlock('item', $items);
$output_file_name = $this->file_name_prefix . 'order-' . $orderData['id'] . '_' . date('Y-m-d', $orderTime) . '.docx';
$TBS->Show(OPENTBS_DOWNLOAD, $output_file_name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment