Skip to content

Commit

Permalink
Add InvoicesAdmin:edit view
Browse files Browse the repository at this point in the history
- View is used to edit invoice details and items.

- Change `InvoiceButton` for admin if there is invoice
for payment to download. Change to dropdown with options
to download or edit invoice.

remp/crm#552
  • Loading branch information
Matefko committed Feb 26, 2021
1 parent 50ad049 commit 9f6a9e5
Show file tree
Hide file tree
Showing 10 changed files with 488 additions and 38 deletions.
13 changes: 12 additions & 1 deletion src/components/InvoiceButton/invoice_button.latte
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
{if $admin}
{if $payment->invoice}
<a href="{plink :Invoices:InvoicesAdmin:downloadInvoice $payment->id}" class="btn btn-success btn-sm" target="_bank"><i class="fa fa-download"></i> {_invoices.frontend.invoice_button.download}</a>
<div class="btn-group" style="margin-bottom: 2px">
<button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true"
aria-expanded="false">
<i class="fas fa-file-invoice"></i>
{_invoices.frontend.invoice_button.invoice} <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="{plink :Invoices:InvoicesAdmin:downloadInvoice $payment->id}"><i class="fa fa-download"></i> {_invoices.frontend.invoice_button.download}</a></li>
<li><a href="{plink :Invoices:InvoicesAdmin:edit $payment->invoice->id}"><i class="fa fa-edit"></i> {_invoices.frontend.invoice_button.edit}</a></li>
</ul>
</div>

{elseif $payment->paid_at && $payment->paid_at->diff(new \DateTime())->days > $canGenerateDaysLimit}
<span class="btn btn-sm btn-default disabled">{_invoices.frontend.invoice_button.not_available_anymore}</span>
{elseif $paymentInvoicable}
Expand Down
2 changes: 2 additions & 0 deletions src/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ services:
- Crm\InvoicesModule\Events\NewAddressHandler
- Crm\InvoicesModule\Events\PaymentStatusChangeHandler
- Crm\InvoicesModule\Forms\ChangeInvoiceDetailsFormFactory
- Crm\InvoicesModule\Forms\ChangeInvoiceFormFactory
- Crm\InvoicesModule\Forms\ChangeInvoiceItemsFormFactory
- Crm\InvoicesModule\Forms\UserInvoiceFormFactory
- Crm\InvoicesModule\Gateways\ProformaInvoice
- Crm\InvoicesModule\Hermes\ZipInvoicesHandler
Expand Down
101 changes: 101 additions & 0 deletions src/forms/ChangeInvoiceFormFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Crm\InvoicesModule\Forms;

use Crm\ApplicationModule\DataProvider\DataProviderManager;
use Crm\InvoicesModule\Repository\InvoicesRepository;
use Crm\UsersModule\DataProvider\AddressFormDataProviderInterface;
use Crm\UsersModule\Repository\CountriesRepository;
use Nette\Application\UI\Form;
use Nette\Localization\ITranslator;
use Tomaj\Form\Renderer\BootstrapRenderer;

class ChangeInvoiceFormFactory
{
public $onSuccess;

private $translator;

private $invoicesRepository;

private $dataProviderManager;

private $countriesRepository;

public function __construct(
ITranslator $translator,
InvoicesRepository $invoicesRepository,
DataProviderManager $dataProviderManager,
CountriesRepository $countriesRepository
) {
$this->translator = $translator;
$this->invoicesRepository = $invoicesRepository;
$this->dataProviderManager = $dataProviderManager;
$this->countriesRepository = $countriesRepository;
}

/**
* @param int $id
* @return Form
*/
public function create(int $id)
{
$form = new Form;
$form->setRenderer(new BootstrapRenderer());
$form->addProtection();

$form->addHidden('invoice_id', $id);

$form->addText('buyer_name', $this->translator->translate('invoices.form.invoice.label.company_name'))
->setAttribute('placeholder', $this->translator->translate('invoices.form.invoice.placeholder.company_name'));

$form->addText('buyer_address', $this->translator->translate('invoices.form.invoice.label.address'))
->setAttribute('placeholder', $this->translator->translate('invoices.form.invoice.placeholder.address'));

$form->addText('buyer_city', $this->translator->translate('invoices.form.invoice.label.city'))
->setAttribute('placeholder', $this->translator->translate('invoices.form.invoice.placeholder.city'));

$form->addText('buyer_zip', $this->translator->translate('invoices.form.invoice.label.zip'))
->setAttribute('placeholder', $this->translator->translate('invoices.form.invoice.placeholder.zip'));

$form->addSelect('country_id', $this->translator->translate('invoices.form.invoice.label.country_id'), $this->countriesRepository->getAllPairs());

$form->addText('company_id', $this->translator->translate('invoices.form.invoice.label.company_id'))
->setAttribute('placeholder', $this->translator->translate('invoices.form.invoice.placeholder.company_id'));

$form->addText('company_tax_id', $this->translator->translate('invoices.form.invoice.label.company_tax_id'))
->setAttribute('placeholder', $this->translator->translate('invoices.form.invoice.placeholder.company_tax_id'));

$form->addText('company_vat_id', $this->translator->translate('invoices.form.invoice.label.company_vat_id'))
->setAttribute('placeholder', $this->translator->translate('invoices.form.invoice.placeholder.company_vat_id'));

/** @var AddressFormDataProviderInterface $providers */
$providers = $this->dataProviderManager->getProviders('invoices.dataprovider.invoice_form', AddressFormDataProviderInterface::class);
foreach ($providers as $sorting => $provider) {
$form = $provider->provide(['form' => $form, 'addressType' => 'invoice']);
}

$form->addSubmit('send', $this->translator->translate('invoices.form.invoice.label.send'));

$form->onSuccess[] = [$this, 'formSucceeded'];
return $form;
}

public function formSucceeded($form, $values)
{
$invoice = $this->invoicesRepository->find($values->invoice_id);

$this->invoicesRepository->update($invoice, [
'buyer_name' => $values->buyer_name,
'buyer_address' => $values->buyer_address,
'buyer_city' => $values->buyer_city,
'buyer_zip' => $values->buyer_zip,
'buyer_country_id' => $values->country_id,
'buyer_id' => $values->company_id,
'buyer_tax_id' => $values->company_tax_id,
'buyer_vat_id' => $values->company_vat_id
]);

$this->onSuccess->__invoke($form);
}
}
93 changes: 93 additions & 0 deletions src/forms/ChangeInvoiceItemsFormFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Crm\InvoicesModule\Forms;

use Crm\InvoicesModule\Repository\InvoiceItemsRepository;
use Crm\InvoicesModule\Repository\InvoicesRepository;
use Nette\Application\UI\Form;
use Nette\Localization\ITranslator;
use Tomaj\Form\Renderer\BootstrapRenderer;

class ChangeInvoiceItemsFormFactory
{
public $onSuccess;

private $translator;

private $invoicesRepository;


private $invoiceItemsRepository;

public function __construct(
ITranslator $translator,
InvoicesRepository $invoicesRepository,
InvoiceItemsRepository $invoiceItemsRepository
) {
$this->translator = $translator;
$this->invoicesRepository = $invoicesRepository;
$this->invoiceItemsRepository = $invoiceItemsRepository;
}

/**
* @param int $id
* @return Form
*/
public function create(int $id)
{
$invoiceItems = null;
$invoice = $this->invoicesRepository->find($id);
if ($invoice) {
$invoiceItems = $invoice->related('invoice_items');
}

$form = new Form;
$form->setRenderer(new BootstrapRenderer());
$form->addProtection();
$defaults = [];

$items = $form->addContainer('items');
$i = 1;
foreach ($invoiceItems as $item) {
$itemName = "item_" . $item->id;

$items->addTextArea($itemName, $this->translator->translate('invoices.form.change_invoice_items.item', ['i' => $i++]))
->setAttribute('placeholder', $this->translator->translate('invoices.form.change_invoice_items.placeholder'))
->setAttribute('class', 'simple-editor')
->setAttribute('rows', 2)
->setAttribute(
'data-html-editor',
['btns' => [
['viewHTML'],
['undo', 'redo'],
['formatting'],
['strong', 'em', 'del']
]]
);

$defaults['items'][$itemName] = $item->text;
}

$form->addSubmit('send', $this->translator->translate('invoices.frontend.change_invoice_details.submit'));

$form->setDefaults($defaults);

$form->onSuccess[] = [$this, 'formSucceeded'];
return $form;
}

public function formSucceeded($form, $values)
{
$items = $values['items'];
foreach ($items as $key => $value) {
$itemId = explode('_', $key)[1];
$invoiceItem = $this->invoiceItemsRepository->find($itemId);

$this->invoiceItemsRepository->update($invoiceItem, [
'text' => $value
]);
}

$this->onSuccess->__invoke($form);
}
}
14 changes: 7 additions & 7 deletions src/forms/UserInvoiceFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ public function create(ActiveRow $payment): Form
$form->addText('zip', 'invoices.form.invoice.label.zip')
->setAttribute('placeholder', 'invoices.form.invoice.placeholder.zip')
->setRequired('invoices.form.invoice.required.zip');
$form->addText('company_id', 'invoices.form.invoice.label.ico')
->setAttribute('placeholder', 'invoices.form.invoice.placeholder.ico');
$form->addText('company_tax_id', 'invoices.form.invoice.label.dic')
->setAttribute('placeholder', 'invoices.form.invoice.placeholder.dic');
$form->addText('company_vat_id', 'invoices.form.invoice.label.icdph')
->setAttribute('placeholder', 'invoices.form.invoice.placeholder.icdph');
$form->addText('company_id', 'invoices.form.invoice.label.company_id')
->setAttribute('placeholder', 'invoices.form.invoice.placeholder.company_id');
$form->addText('company_tax_id', 'invoices.form.invoice.label.company_tax_id')
->setAttribute('placeholder', 'invoices.form.invoice.placeholder.company_tax_id');
$form->addText('company_vat_id', 'invoices.form.invoice.label.company_vat_id')
->setAttribute('placeholder', 'invoices.form.invoice.placeholder.company_vat_id');

$contactEmail = $this->applicationConfig->get('contact_email');
$form->addSelect('country_id', 'invoices.form.invoice.label.invoice_country_id', $this->countriesRepository->getDefaultCountryPair())
$form->addSelect('country_id', 'invoices.form.invoice.label.country_id', $this->countriesRepository->getDefaultCountryPair())
->setOption('id', 'invoice-country')
->setOption(
'description',
Expand Down
30 changes: 20 additions & 10 deletions src/lang/invoices.cs_CZ.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ frontend:
not_available_anymore: Faktura již nedostupná
generate: Vygeneruj
system_error: Systémová chyba! Prosím kontaktujte technickou podporu!
invoice: Faktura
download: Stáhni fakturu
edit: Uprav fakturu
invoice_details: Fakturační údaje

invoice_details:
Expand Down Expand Up @@ -78,6 +80,13 @@ admin:
invoices: Faktury
generate: Vygeneruj
scheduled: "Generování faktur bylo naplánováno, chvíli počkejte."
edit:
title: Úprava faktury
edit_invoice: Upravit adresu odběratele
current_invoice_address: Použít aktuální fakturační adresu
edit_items: Upravit položky
back_to_user: Zpět na uživatele
success: Faktura byla úspěšně změněna.

component:
payment_success_invoice_widget:
Expand All @@ -97,10 +106,10 @@ form:
number: Číslo
city: Město
zip: PSČ
ico: IČO
dic: DIČ
icdph: IČDPH
invoice_country_id: Země
country_id: Země
company_id: IČO
company_tax_id: DIČ
company_vat_id: IČDPH
send: Uložit
save: Uložit údaje
placeholder:
Expand All @@ -109,18 +118,19 @@ form:
number: napište číslo domu
city: napište město
zip: napište PSČ
ico: například 23590235
dic: například 2359023536
icdph: například CZ2359023536
company_id: například 23590235
company_tax_id: například 2359023536
company_vat_id: například CZ2359023536
required:
company_name: Název firmy musí být vyplněn
address: Adresa firmy musí být vyplněna
number: Číslo domu musí být vyplněno
city: Město musí být vyplněno
zip: PSČ musí být vyplněno
ico: IČO musí být vyplněno
dic: DIČ musí být vyplněno
icdph: IČDPH musí být vyplněno
country_id: Země musí být vyplněna
company_id: IČO musí být vyplněno
company_tax_id: DIČ musí být vyplněno
company_vat_id: IČDPH musí být vyplněno
options:
foreign_country: "Pro informace o doručení do zahraničí nás prosím kontaktujte na %contactEmail%"

Expand Down
31 changes: 21 additions & 10 deletions src/lang/invoices.en_US.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ frontend:
not_available_anymore: Invoice not available anymore
generate: Generate
system_error: System failure! Please contact tech support!
invoice: invoice
download: Download invoice
edit: Edit invoice
invoice_details: Invoice details

invoice_details:
Expand Down Expand Up @@ -78,6 +80,14 @@ admin:
invoices: Invoices
generate: Generate
scheduled: "Batch generation was scheduled, please wait."
edit:
title: Edit invoice
edit_invoice: Edit buyer address
current_invoice_address: Use current invoice address
edit_items: Edit items
back_to_user: Back to user detail
success: Invoice was successfuly changed.

component:
payment_success_invoice_widget:
require_invoice: I need an invoice
Expand All @@ -96,10 +106,10 @@ form:
number: Number
city: City
zip: ZIP
ico: Businness ID
dic: Tax ID
icdph: VAT number
invoice_country_id: Country
country_id: Country
company_id: Businness ID
company_tax_id: Tax ID
company_vat_id: VAT number
send: Save
save: Save
placeholder:
Expand All @@ -108,18 +118,19 @@ form:
number: house number
city: fill your city
zip: fill your zip
ico: e.g. 23590235
dic: e.g. 2359023574
icdph: e.g. SK2359023574
company_id: e.g. 23590235
company_tax_id: e.g. 2359023574
company_vat_id: e.g. SK2359023574
required:
company_name: Company name is required
address: Company address is required
number: House number is required
city: City is required
zip: ZIP is required
ico: Business ID is required
dic: Tax ID is required
icdph: VAT number is required
country_id: Country is required
company_id: Business ID is required
company_tax_id: Tax ID is required
company_vat_id: VAT number is required
options:
foreign_country: "For delivery to foreign countries please contact us through %contactEmail%"

Expand Down
Loading

0 comments on commit 9f6a9e5

Please sign in to comment.