From 3f4c00f970914319e98417a2f3d7d70ad415be34 Mon Sep 17 00:00:00 2001 From: Nurmuhammet Allanov Date: Sun, 10 Nov 2024 00:50:07 +0500 Subject: [PATCH] clean code --- .../Bank/Repositories/BankRepository.php | 7 +- .../Invoice/Actions/GenerateInvoiceExcell.php | 116 +++++++++--------- .../Invoice/Resources/Docs/invoice.xls | Bin 34816 -> 34816 bytes public/write.xls | Bin 15872 -> 15872 bytes 4 files changed, 66 insertions(+), 57 deletions(-) diff --git a/app/Modules/Bank/Repositories/BankRepository.php b/app/Modules/Bank/Repositories/BankRepository.php index 1f54e5d..a516c13 100644 --- a/app/Modules/Bank/Repositories/BankRepository.php +++ b/app/Modules/Bank/Repositories/BankRepository.php @@ -31,15 +31,18 @@ class BankRepository /** * Return available options * - * @return Collection|array + * @return Collection|array */ public function options(): Collection|array { - return CacheRepository::make( + /** @var Collection */ + $options = CacheRepository::make( name: static::$cacheName.'_options', value: fn () => $this->query->get(['id', 'name', 'bab'])->mapWithKeys(fn ($item) => [ $item->id => $item->name.' - '.$item->bab, ]), ); + + return $options; } } diff --git a/app/Modules/Invoice/Actions/GenerateInvoiceExcell.php b/app/Modules/Invoice/Actions/GenerateInvoiceExcell.php index d08fa13..54dc5b4 100644 --- a/app/Modules/Invoice/Actions/GenerateInvoiceExcell.php +++ b/app/Modules/Invoice/Actions/GenerateInvoiceExcell.php @@ -8,6 +8,7 @@ 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 @@ -93,40 +94,53 @@ class GenerateInvoiceExcell $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 { - // Hasap faktura nomeri... - $this->worksheet->getCell('C6')->setValue($this->hasapFakturaNomeri()); + $data = $this->getTemplateValues(); - // Sene... - $this->worksheet->getCell('C7')->setValue($this->sene()); + foreach ($data as $cell => $value) { + $this->worksheet->getCell($cell)->setValue($value); + } + } - // Satyjy... - $this->worksheet->getCell('A9')->setValue($this->satyjy()); - - // Satyjy şahsy salgyt belgi... - $this->worksheet->getCell('A11')->setValue($this->satyjySalgytBelgi()); - - // Satyjy banky... - $this->worksheet->getCell('A12')->setValue($this->satyjyBanky()); - - // Satyjy bank maglumatlary... - $this->worksheet->getCell('A13')->setValue(sprintf('şäherde (etrapda) %s BAB %s bankyň kor.hasap %s', 'Aşgabat', '390101601', '21101934110100700005000')); - - // Satyn alyjy... - $this->worksheet->getCell('A16')->setValue(sprintf('Satyn alyjy: %s', 'Türkmenistanyň „Türkmenbaşy“ paýdarlar täjirçilik banky')); - - // Satyn alyjy salgy... - $this->worksheet->getCell('A18')->setValue(sprintf('744000 Aşgabat ş., Çandebil şaýoly köç., 121,')); - - // Satyn alyjy banky... - $this->worksheet->getCell('A19')->setValue(sprintf('Alyjynyň bankynyň ady %s %s', 'Türkmenistanyň Merkezi Banky', 'Aşgabat ş.')); - - // Satyn alyjy banky maglumatlary... - $this->worksheet->getCell('A20')->setValue(sprintf('Satyn alyjynyň şahsy salgyt belgisi %s, MFO %s, Hasap № %s', '101301000408', '390101304', '21101934110100300007000')); + /** + * Get template cells and values + * + * @return array + */ + protected function getTemplateValues(): array + { + return [ + 'C6' => $this->hasapFakturaNomeri(), + 'C7' => $this->sene(), + 'A9' => $this->satyjy(), + 'A11' => $this->satyjySalgytBelgi(), + 'A12' => $this->satyjyBanky(), + 'A13' => sprintf('şäherde (etrapda) %s BAB %s bankyň kor.hasap %s', 'Aşgabat', '390101601', '21101934110100700005000'), + 'A16' => 'Satyn alyjy: Türkmenistanyň „Türkmenbaşy“ paýdarlar täjirçilik banky', + 'A18' => '744000 Aşgabat ş., Çandebil şaýoly köç., 121,', + 'A19' => sprintf('Alyjynyň bankynyň ady %s %s', 'Türkmenistanyň Merkezi Banky', 'Aşgabat ş.'), + 'A20' => sprintf('Satyn alyjynyň şahsy salgyt belgisi %s, MFO %s, Hasap № %s', '101301000408', '390101304', '21101934110100300007000'), + ]; } /** @@ -164,21 +178,17 @@ class GenerateInvoiceExcell */ public function satyjySalgytBelgi(): RichText { - $satyjy_salgyt_belgi = new RichText; - $satyjy_salgyt_belgi - ->createTextRun('Satyjynyň şahsy salgyt belgisi: ') - ->getFont() - ->setName('Times New Roman') - ->setSize(10); + $richText = new RichText; + $satyjy_salgyt_belgi = $this->addToRichText( + richText: $richText, + text: 'Satyjynyň şahsy salgyt belgisi: ' + ); - $satyjy_salgyt_belgi - ->createTextRun($this->data->seller_ssb) - ->getFont() - ->setBold(true) - ->setName('Times New Roman') - ->setSize(10); - - return $satyjy_salgyt_belgi; + return $this->addToRichText( + richText: $satyjy_salgyt_belgi, + text: $this->data->seller_ssb, + isBold: true + ); } /** @@ -186,20 +196,16 @@ class GenerateInvoiceExcell */ public function satyjyBanky(): RichText { - $satyjy_banky = new RichText; - $satyjy_banky - ->createTextRun(sprintf('Satyjynyň bankynyň ady %s, hasap № ', $this->data->seller_bank_name)) - ->getFont() - ->setName('Times New Roman') - ->setSize(10); + $richText = new RichText; + $satyjy_banky = $this->addToRichText( + richText: $richText, + text: sprintf('Satyjynyň bankynyň ady %s, hasap № ', $this->data->seller_bank_name) + ); - $satyjy_banky - ->createTextRun($this->data->seller_bank_number) - ->getFont() - ->setBold(true) - ->setName('Times New Roman') - ->setSize(10); - - return $satyjy_banky; + return $this->addToRichText( + richText: $satyjy_banky, + text: $this->data->seller_bank_number, + isBold: true + ); } } diff --git a/app/Modules/Invoice/Resources/Docs/invoice.xls b/app/Modules/Invoice/Resources/Docs/invoice.xls index e2b10eb9a7ea9d6e19d7bee287ac65628decbf8e..2130774fbc3350c29f482fe0810aa88d4d076c32 100755 GIT binary patch delta 20 ccmZpez|=5-X+usM>xBtVv7&BNh7%&)uu^EV*+$-M4=rUPGLT2)2Nx97t5;}~Gwv$^WGsMx2h-cJe zNMK-`yiZYXvWk?Ov@e4jgFl*)#y}%Y7<4vwOO?t`Hn0%cWL3e)$g{c9x}6CClCUQ} delta 163 zcmZpuX{gztEX;UnvWl=HWBlec;Q~fx69(hWH$|eD7(*wUh^xseFgP;AGo~{nG9&?U z34;O{IsrwU7!)Swi;GWg5$~08Wnc^jOII@F0Tm=N