From a5603b7724ea153906e2b04f4176d24d30fcbff2 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Fri, 31 Jan 2020 22:18:20 +0300 Subject: [PATCH] Almost finished first dirty version. --- composer.json | 1 + composer.lock | 188 ++++++++++++++++++++++++++++- config/parameters.yaml.dist | 54 ++++++++- src/Generator/InvoiceGenerator.php | 21 +++- src/Kernel.php | 2 +- templates/invoice.html.twig | 2 +- templates/invoice_side.html.twig | 7 +- 7 files changed, 261 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 71eead7..ad91a22 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "mpdf/mpdf": "^8.0", "symfony/yaml": "^5.0", "twig/html-extra": "^3.0", + "twig/intl-extra": "^3.0", "twig/twig": "^3.0" }, "config": { diff --git a/composer.lock b/composer.lock index 62f72c4..0a98c69 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c214995f6d2d08b81315006f8b75ff8f", + "content-hash": "08be0128a2de0c32973a187511fbb665", "packages": [ { "name": "asika/simple-console", @@ -358,6 +358,81 @@ ], "time": "2019-01-30T14:11:19+00:00" }, + { + "name": "symfony/intl", + "version": "v5.0.4", + "source": { + "type": "git", + "url": "https://github.com/symfony/intl.git", + "reference": "519bcb27ea53835c1e8e7f7c8a799c867d570156" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/intl/zipball/519bcb27ea53835c1e8e7f7c8a799c867d570156", + "reference": "519bcb27ea53835c1e8e7f7c8a799c867d570156", + "shasum": "" + }, + "require": { + "php": "^7.2.5", + "symfony/polyfill-intl-icu": "~1.0" + }, + "require-dev": { + "symfony/filesystem": "^4.4|^5.0" + }, + "suggest": { + "ext-intl": "to use the component with locales other than \"en\"" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.0-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Intl\\": "" + }, + "classmap": [ + "Resources/stubs" + ], + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + }, + { + "name": "Eriksen Costa", + "email": "eriksen.costa@infranology.com.br" + }, + { + "name": "Igor Wiedler", + "email": "igor@wiedler.ch" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "A PHP replacement layer for the C intl extension that includes additional data from the ICU library.", + "homepage": "https://symfony.com", + "keywords": [ + "i18n", + "icu", + "internationalization", + "intl", + "l10n", + "localization" + ], + "time": "2020-01-31T09:13:47+00:00" + }, { "name": "symfony/mime", "version": "v5.0.3", @@ -478,6 +553,64 @@ ], "time": "2019-11-27T13:56:44+00:00" }, + { + "name": "symfony/polyfill-intl-icu", + "version": "v1.13.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-intl-icu.git", + "reference": "b3dffd68afa61ca70f2327f2dd9bbeb6aa53d70b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-intl-icu/zipball/b3dffd68afa61ca70f2327f2dd9bbeb6aa53d70b", + "reference": "b3dffd68afa61ca70f2327f2dd9bbeb6aa53d70b", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "symfony/intl": "~2.3|~3.0|~4.0|~5.0" + }, + "suggest": { + "ext-intl": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.13-dev" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for intl's ICU-related data and classes", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "icu", + "intl", + "polyfill", + "portable", + "shim" + ], + "time": "2019-11-27T13:56:44+00:00" + }, { "name": "symfony/polyfill-intl-idn", "version": "v1.13.1", @@ -766,6 +899,59 @@ ], "time": "2019-12-28T07:09:27+00:00" }, + { + "name": "twig/intl-extra", + "version": "v3.0.1", + "source": { + "type": "git", + "url": "https://github.com/twigphp/intl-extra.git", + "reference": "291d79ef98891da3efe14f0771fbe03a25fe6bec" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twigphp/intl-extra/zipball/291d79ef98891da3efe14f0771fbe03a25fe6bec", + "reference": "291d79ef98891da3efe14f0771fbe03a25fe6bec", + "shasum": "" + }, + "require": { + "php": "^7.1.3", + "symfony/intl": "^4.3|^5.0", + "twig/twig": "^2.4|^3.0" + }, + "require-dev": { + "symfony/phpunit-bridge": "^4.4|^5.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "psr-4": { + "Twig\\Extra\\Intl\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + } + ], + "description": "A Twig extension for Intl", + "homepage": "https://twig.symfony.com", + "keywords": [ + "intl", + "twig" + ], + "time": "2019-12-28T07:09:27+00:00" + }, { "name": "twig/twig", "version": "v3.0.1", diff --git a/config/parameters.yaml.dist b/config/parameters.yaml.dist index eeca91d..2ba4e7d 100644 --- a/config/parameters.yaml.dist +++ b/config/parameters.yaml.dist @@ -8,6 +8,10 @@ configuration: images: signature: '' stamp: '' + global_substitutions: + # TODO extract to parameters + '%invoice_number%': '999' + '%invoice_date%': '01.01.1970' services: - @@ -24,7 +28,7 @@ translations: source: variables: title: 'Счёт на оплату №%invoice_number% от %invoice_date%' - currency: '%currency%' + currency: 'EUR' rows: supplier: 'Поставщик' buyer: 'Покупатель' @@ -66,5 +70,49 @@ translations: - 'IBAN: EU123131231231232112' static_substitutions: '%contract_date%': '01.01.1970' - '%currency%': 'EUR' - #target: \ No newline at end of file + + target: + variables: + title: 'Счёт на оплату №%invoice_number% от %invoice_date%' + currency: 'EUR' + rows: + supplier: 'Поставщик' + buyer: 'Покупатель' + # service table + th_number: '№' + th_name: 'Наименование работ, услуг' + th_amount: 'Кол-во' + th_units: 'Ед.' + th_price: 'Цена' + th_sum: 'Сумма' + tf_total: 'Итого' + # bank data + account_data: 'Банковские реквизиты поставщика' + account_number: 'Номер счёта' + bank: 'Банк' + bank_address: 'Адрес' + swift: 'SWIFT' + corr_bank: 'Банк-посредник' + total_to_pay: 'Всего к оплате' + signature: 'Подпись' + supplier: + title: '123456' + short_title: '123' + address: 'some address' + extra: [] + bank: + account: '9999999999999999999' + name: 'Some Bank of some Country' + address: 'Some address' + swift: 'SOMESWIFT' + corr_bank: + name: 'SOME CORR BANK' + swift: 'SOMESWIFT2' + buyer: + title: '"Some Client" LLC' + address: 'Some Address' + extra: + - 'VAT: EU12313123' + - 'IBAN: EU123131231231232112' + static_substitutions: + '%contract_date%': '01.01.1970' \ No newline at end of file diff --git a/src/Generator/InvoiceGenerator.php b/src/Generator/InvoiceGenerator.php index ee6cc78..9f65bfb 100644 --- a/src/Generator/InvoiceGenerator.php +++ b/src/Generator/InvoiceGenerator.php @@ -6,7 +6,7 @@ use App\Kernel; use App\Twig\NumberToWordsExtension; use App\Util\StringReplacer; use Twig\Environment as Twig; -use Twig\Extra\Html\HtmlExtension; +use Twig\Extra\{Html\HtmlExtension, Intl\IntlExtension}; use Twig\Loader\FilesystemLoader; class InvoiceGenerator @@ -15,13 +15,23 @@ class InvoiceGenerator { $twig = static::createTwig(); - $sourceStaticSubstitutions = $config['translations']['source']['static_substitutions']; - //$sourceStaticSubstitutions = $config['translations']['target']['static_substitutions']; + $sourceStaticSubstitutions = array_merge( + $config['translations']['source']['static_substitutions'], + $config['configuration']['global_substitutions'] + ); + $targetStaticSubstitutions = array_merge( + $config['translations']['target']['static_substitutions'], + $config['configuration']['global_substitutions'] + ); + $source = StringReplacer::recursiveReplace( $config['translations']['source']['variables'], $sourceStaticSubstitutions ); - //$target = StringReplacer::recursiveReplace($config['translations']['target']['variables']); + $target = StringReplacer::recursiveReplace( + $config['translations']['target']['variables'], + $targetStaticSubstitutions + ); // @TODO fix multilingual substitution depending on the context $services = StringReplacer::recursiveReplace($config['services'], $sourceStaticSubstitutions); @@ -31,7 +41,7 @@ class InvoiceGenerator 'configuration' => $config['configuration'], 'trans_data' => [ 'source' => $source, - //'target' => $target, + 'target' => $target, ], 'services' => $services, 'images' => $images, @@ -55,6 +65,7 @@ class InvoiceGenerator $twig = new Twig($loader); $twig->addExtension(new NumberToWordsExtension()); + $twig->addExtension(new IntlExtension()); $twig->addExtension(new HtmlExtension()); return $twig; diff --git a/src/Kernel.php b/src/Kernel.php index 2ad67d3..f1a195c 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -8,4 +8,4 @@ class Kernel { return dirname(__DIR__); } -} \ No newline at end of file +} diff --git a/templates/invoice.html.twig b/templates/invoice.html.twig index 065a85e..6ea5347 100644 --- a/templates/invoice.html.twig +++ b/templates/invoice.html.twig @@ -26,7 +26,7 @@ {{ include ('invoice_side.html.twig', { - t: trans_data.source, + t: trans_data.target, services: services, images: images, context: 'target', diff --git a/templates/invoice_side.html.twig b/templates/invoice_side.html.twig index 62b06fb..6759604 100644 --- a/templates/invoice_side.html.twig +++ b/templates/invoice_side.html.twig @@ -6,14 +6,14 @@   - {{ t.rows.supplier }} + {{ t.rows.supplier }}: {{ t.supplier.title }} {{ t.supplier.address }} - {{ t.rows.buyer }} + {{ t.rows.buyer }}: {{ t.buyer.title }} @@ -61,10 +61,11 @@ +
- {{ t.rows.total_to_pay }}: {{ total|ntw(locale) }} + {{ t.rows.total_to_pay }}: {{ total|ntw(locale) }} {{ t.currency|currency_name(locale) }}.