Files
telekeci/app/Modules/Invoice/Actions/GenerateInvoiceExcell.php
2024-11-10 14:18:24 +05:00

261 lines
6.1 KiB
PHP

<?php
namespace App\Modules\Invoice\Actions;
use App\Modules\Invoice\Data\InvoiceExcellData;
use App\Modules\Makeable;
use Illuminate\Support\Carbon;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class GenerateInvoiceExcell
{
use Makeable;
/** Template file */
protected string $templateFile;
/** Excell Spreadsheet */
protected Spreadsheet $spreadsheet;
/** Active worksheet */
protected Worksheet $worksheet;
/** Invoice data */
protected InvoiceExcellData $data;
/**
* Let's generate an invoice excell file...
*/
public function __construct()
{
// For turkmen date format...
Carbon::setLocale('tk');
}
/**
* Run conversion process
*/
public function handle(): self
{
$this->setSpreadsheetFile();
$this->setWorksheetFile();
$this->fillTheCells();
return $this;
}
/**
* Save the invoice
*/
public function save(): void
{
$writer = IOFactory::createWriter($this->spreadsheet, 'Xls');
$writer->save('write.xls');
}
/**
* Set template file path
*/
public function setTemplateFile(string $file): self
{
$this->templateFile = $file;
return $this;
}
/**
* Set data
*/
public function setData(InvoiceExcellData $data): self
{
$this->data = $data;
return $this;
}
/**
* Load template to a new spreadsheet
*/
protected function setSpreadsheetFile(): void
{
$this->spreadsheet = IOFactory::load($this->templateFile);
}
/**
* Set working spreadsheet
*/
protected function setWorksheetFile(): void
{
$this->worksheet = $this->spreadsheet->getActiveSheet();
}
/**
* Create a RichText object with specified text and font settings.
*/
protected function addToRichText(RichText $richText, string $text, bool $isBold = false): RichText
{
$textRun = $richText->createTextRun($text);
$font = $textRun->getFont() ?: new Font;
$font->setName('Times New Roman')->setSize(10);
if ($isBold) {
$font->setBold(true);
}
return $richText;
}
/**
* Fill the cells
*/
protected function fillTheCells(): void
{
$data = $this->getTemplateValues();
foreach ($data as $cell => $value) {
$this->worksheet->getCell($cell)->setValue($value);
}
}
/**
* Get template cells and values
*
* @return array<string, mixed>
*/
protected function getTemplateValues(): array
{
return [
'C6' => $this->hasapFakturaNomeri(),
'C7' => $this->sene(),
'A9' => $this->satyjy(),
'A11' => $this->satyjySalgytBelgi(),
'A12' => $this->satyjyBanky(),
'A13' => $this->satyjyBankyMaglumatlary(),
'A16' => $this->satynAlyjy(),
'A18' => $this->satynAlyjySalgy(),
'A19' => $this->satynAlyjyBanky(),
'A20' => $this->satynAlyjyBankyMaglumatlary(),
];
}
/**
* Hasap faktura nomeri
*/
public function hasapFakturaNomeri(): string
{
return 'Hasap-faktura № '.$this->data->number;
}
/**
* Sene
*/
public function sene(): string
{
$date = $this->data->date;
$year = $date->year;
$day = $date->format('d');
$month_as_string = $date->translatedFormat('F');
return sprintf('%s ýylyň «%s» %s', $year, $day, $month_as_string);
}
/**
* Satyjy
*/
public function satyjy(): string
{
return sprintf('Satyjy: %s %s', $this->data->seller_firm_type, $this->data->seller_firm_name);
}
/**
* Satyjy salgyt belgisi
*/
public function satyjySalgytBelgi(): RichText
{
$richText = new RichText;
$satyjy_salgyt_belgi = $this->addToRichText(
richText: $richText,
text: 'Satyjynyň şahsy salgyt belgisi: '
);
return $this->addToRichText(
richText: $satyjy_salgyt_belgi,
text: $this->data->seller_ssb,
isBold: true
);
}
/**
* Satyjy bank maglumatlary
*/
public function satyjyBanky(): RichText
{
$richText = new RichText;
$satyjy_banky = $this->addToRichText(
richText: $richText,
text: sprintf('Satyjynyň bankynyň ady %s, hasap № ', $this->data->seller_bank_name)
);
return $this->addToRichText(
richText: $satyjy_banky,
text: $this->data->seller_bank_hb_1,
isBold: true
);
}
/**
* Satyjy bank maglumatlary
*/
public function satyjyBankyMaglumatlary(): RichText
{
$richText = new RichText;
$city_label = $this->addToRichText($richText, 'şäherde (etrapda) ');
$city = $this->addToRichText($city_label, $this->data->seller_bank_city, isBold: true);
$bab_label = $this->addToRichText($city, ' BAB ');
$bab = $this->addToRichText($bab_label, $this->data->seller_bank_bab, isBold: true);
$hb_2_label = $this->addToRichText($bab, ' bankyň kor.hasap ');
return $this->addToRichText($hb_2_label, $this->data->seller_bank_hb_2, isBold: true);
}
/**
* Satyn alyjy
*/
public function satynAlyjy(): string
{
return 'Satyn alyjy: '.$this->data->buyer;
}
/**
* Satyn alyjy salgy
*/
public function satynAlyjySalgy(): string
{
return $this->data->buyer_address;
}
/**
* Satyn alyjy banky we salgysy
*/
public function satynAlyjyBanky(): string
{
return sprintf('Alyjynyň bankynyň ady %s %s', $this->data->buyer_bank, $this->data->buyer_bank_address);
}
/**
* Satyn alyjynyň bank maglumatlary
*/
public function satynAlyjyBankyMaglumatlary(): string
{
return 'Satyn alyjynyň '.$this->data->buyer_bank_data;
}
}