From 582c5dada5d21d0e0d4bf222ec966d408c2ebef2 Mon Sep 17 00:00:00 2001 From: Nurmuhammet Allanov Date: Tue, 12 Nov 2024 23:00:03 +0500 Subject: [PATCH] beautify code --- app/Modules/Excell/Excell.php | 16 ++++ .../ExcellModule.php} | 4 +- app/Modules/Excell/Fonts/Fonts.php | 8 ++ .../Types/iRichText/RichTextWrapper.php | 67 ++++++++++++++++ .../Traits/HasExcellRichTextWrappers.php | 5 ++ .../Invoice/Actions/GenerateInvoiceExcell.php | 74 ++++++------------ app/Modules/Invoice/Resources/.DS_Store | Bin 6148 -> 6148 bytes .../Resources/Docs/invoice-original.xls | Bin 34816 -> 34816 bytes app/Modules/PHPExcell/.DS_Store | Bin 6148 -> 0 bytes public/write.xls | Bin 14336 -> 14336 bytes 10 files changed, 123 insertions(+), 51 deletions(-) create mode 100644 app/Modules/Excell/Excell.php rename app/Modules/{PHPExcell/PHPExcellModule.php => Excell/ExcellModule.php} (89%) create mode 100644 app/Modules/Excell/Fonts/Fonts.php create mode 100644 app/Modules/Excell/Types/iRichText/RichTextWrapper.php create mode 100644 app/Modules/Excell/Types/iRichText/Traits/HasExcellRichTextWrappers.php delete mode 100644 app/Modules/PHPExcell/.DS_Store diff --git a/app/Modules/Excell/Excell.php b/app/Modules/Excell/Excell.php new file mode 100644 index 0000000..9c7bae3 --- /dev/null +++ b/app/Modules/Excell/Excell.php @@ -0,0 +1,16 @@ +defaultFontName = $fontname; + + return $this; + } + + /** + * Default font size + */ + public function setDefaultSize(int $size): self + { + $this->defaultSize = $size; + + return $this; + } + + /** + * Add or attach text + * + * @param array $styles + */ + public function add(?string $text, array $styles = [], bool $bold = false): self + { + if (! $text) { + return $this; + } + + $textRun = $this->richText->createTextRun($text); + + $font = $textRun->getFont() ?: new Font; + $font->setName($this->defaultFontName) + ->setSize($this->defaultSize); + + if ($bold) { + $font->setBold(true); + } + + return $this; + } + + /** + * Return RichText + */ + public function toRichText(): RichText + { + return $this->richText; + } +} diff --git a/app/Modules/Excell/Types/iRichText/Traits/HasExcellRichTextWrappers.php b/app/Modules/Excell/Types/iRichText/Traits/HasExcellRichTextWrappers.php new file mode 100644 index 0000000..891572e --- /dev/null +++ b/app/Modules/Excell/Types/iRichText/Traits/HasExcellRichTextWrappers.php @@ -0,0 +1,5 @@ +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 */ @@ -244,7 +230,7 @@ class GenerateInvoiceExcell ); $this->worksheet->getStyle('A'.$itemCellNumber)->applyFromArray([ 'font' => ['bold' => false, 'size' => 8, 'name' => 'Times New Roman'], - 'alignment' => ['vertical' => Alignment::VERTICAL_TOP] + 'alignment' => ['vertical' => Alignment::VERTICAL_TOP], ]); } @@ -304,17 +290,12 @@ class GenerateInvoiceExcell */ 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 - ); + return Excell::richText() + ->setDefaultFont(FONTS::TIMES_NEW_ROMAN) + ->setDefaultSize(10) + ->add('Satyjynyň şahsy salgyt belgisi: ') + ->add($this->data->seller_ssb, bold: true) + ->toRichText(); } /** @@ -322,17 +303,12 @@ class GenerateInvoiceExcell */ 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 - ); + return Excell::richText() + ->setDefaultFont(FONTS::TIMES_NEW_ROMAN) + ->setDefaultSize(10) + ->add(sprintf('Satyjynyň bankynyň ady %s, hasap № ', $this->data->seller_bank_name)) + ->add($this->data->seller_bank_hb_1, bold: true) + ->toRichText(); } /** @@ -340,16 +316,16 @@ class GenerateInvoiceExcell */ 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); + return Excell::richText() + ->setDefaultFont(FONTS::TIMES_NEW_ROMAN) + ->setDefaultSize(10) + ->add('şäherde (etrapda) ') + ->add($this->data->seller_bank_city, bold: true) + ->add(' BAB ') + ->add($this->data->seller_bank_bab, bold: true) + ->add(' bankyň kor.hasap ') + ->add($this->data->seller_bank_hb_2, bold: true) + ->toRichText(); } /** diff --git a/app/Modules/Invoice/Resources/.DS_Store b/app/Modules/Invoice/Resources/.DS_Store index 3043b7be762567feeed7110f75569ebfa009d493..88b7b975e31fbfe53cf61d6e95e7694e9be5e517 100644 GIT binary patch delta 21 ccmZoMXffC@gNeh$&`3wY*ur4*0;UvE07jz*2LJ#7 delta 21 ccmZoMXffC@gNeh)+(Jje$kJl-0;UvE07pUw82|tP diff --git a/app/Modules/Invoice/Resources/Docs/invoice-original.xls b/app/Modules/Invoice/Resources/Docs/invoice-original.xls index fd920b6f6dabec158e8240174c2688d1939d090c..e5b4bff2d8aa56bb5c580f2fa455b758c6994425 100755 GIT binary patch delta 23 ecmZpez|=5-X+usMhk~e=t&Zt!#?2*dJ&XWh3H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0=$K}IzIIo}2I delta 16 XcmZoDXeik5N08Z)!D#b;K}IzIIr9bf