Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FaultyStatisticSheet.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App\Exports;
  3. use Maatwebsite\Excel\Concerns\FromCollection;
  4. use Maatwebsite\Excel\Concerns\WithHeadings;
  5. use Maatwebsite\Excel\Concerns\WithTitle;
  6. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  7. use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
  8. use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
  9. use Maatwebsite\Excel\Concerns\WithEvents;
  10. use Maatwebsite\Excel\Events\BeforeExport;
  11. use Maatwebsite\Excel\Events\AfterSheet;
  12. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  13. class FaultyStatisticSheet implements FromCollection, WithHeadings, WithTitle, WithEvents, WithStrictNullComparison, WithCalculatedFormulas
  14. {
  15. protected $data, $year, $title, $subtitle, $start_date, $end_date;
  16. public function __construct($data, $month, $start_date, $end_date)
  17. {
  18. $this->data = $data;
  19. $this->$start_date = $start_date;
  20. $this->end_date = $end_date;
  21. $this->year = date('Y', strtotime($start_date));
  22. if($month == '01'){
  23. $this->title = 'JAN';
  24. $this->subtitle = 'LAPORAN PENGELUARAN KOMPAUN MENGIKUT KESALAHAN BERDASARKAN TARIKH KOMPAUN 01/'.$month.'/'.$this->year.' HINGGA 31/'.$month.'/'.$this->year;
  25. }else if($month == '02'){
  26. $this->title = 'FEB';
  27. $this->subtitle = 'LAPORAN PENGELUARAN KOMPAUN MENGIKUT KESALAHAN BERDASARKAN TARIKH KOMPAUN 01/'.$month.'/'.$this->year.' HINGGA 28/'.$month.'/'.$this->year;
  28. }else if($month == '03'){
  29. $this->title = 'MAC';
  30. $this->subtitle = 'LAPORAN PENGELUARAN KOMPAUN MENGIKUT KESALAHAN BERDASARKAN TARIKH KOMPAUN 01/'.$month.'/'.$this->year.' HINGGA 31/'.$month.'/'.$this->year;
  31. }else if($month == '04'){
  32. $this->title = 'APRIL';
  33. $this->subtitle = 'LAPORAN PENGELUARAN KOMPAUN MENGIKUT KESALAHAN BERDASARKAN TARIKH KOMPAUN 01/'.$month.'/'.$this->year.' HINGGA 30/'.$month.'/'.$this->year;
  34. }else if($month == '05'){
  35. $this->title = 'MEI';
  36. $this->subtitle = 'LAPORAN PENGELUARAN KOMPAUN MENGIKUT KESALAHAN BERDASARKAN TARIKH KOMPAUN 01/'.$month.'/'.$this->year.' HINGGA 31/'.$month.'/'.$this->year;
  37. }else if($month == '06'){
  38. $this->title = 'JUN';
  39. $this->subtitle = 'LAPORAN PENGELUARAN KOMPAUN MENGIKUT KESALAHAN BERDASARKAN TARIKH KOMPAUN 01/'.$month.'/'.$this->year.' HINGGA 30/'.$month.'/'.$this->year;
  40. }else if($month == '07'){
  41. $this->title = 'JULY';
  42. $this->subtitle = 'LAPORAN PENGELUARAN KOMPAUN MENGIKUT KESALAHAN BERDASARKAN TARIKH KOMPAUN 01/'.$month.'/'.$this->year.' HINGGA 31/'.$month.'/'.$this->year;
  43. }else if($month == '08'){
  44. $this->title = 'OGOS';
  45. $this->subtitle = 'LAPORAN PENGELUARAN KOMPAUN MENGIKUT KESALAHAN BERDASARKAN TARIKH KOMPAUN 01/'.$month.'/'.$this->year.' HINGGA 31/'.$month.'/'.$this->year;
  46. }else if($month == '09'){
  47. $this->title = 'SEPT';
  48. $this->subtitle = 'LAPORAN PENGELUARAN KOMPAUN MENGIKUT KESALAHAN BERDASARKAN TARIKH KOMPAUN 01/'.$month.'/'.$this->year.' HINGGA 30/'.$month.'/'.$this->year;
  49. }else if($month == '10'){
  50. $this->title = 'OKT';
  51. $this->subtitle = 'LAPORAN PENGELUARAN KOMPAUN MENGIKUT KESALAHAN BERDASARKAN TARIKH KOMPAUN 01/'.$month.'/'.$this->year.' HINGGA 30/'.$month.'/'.$this->year;
  52. }else if($month == '11'){
  53. $this->title = 'NOV';
  54. $this->subtitle = 'LAPORAN PENGELUARAN KOMPAUN MENGIKUT KESALAHAN BERDASARKAN TARIKH KOMPAUN 01/'.$month.'/'.$this->year.' HINGGA 30/'.$month.'/'.$this->year;
  55. }else if($month == '12'){
  56. $this->title = 'DIS';
  57. $this->subtitle = 'LAPORAN PENGELUARAN KOMPAUN MENGIKUT KESALAHAN BERDASARKAN TARIKH KOMPAUN 01/'.$month.'/'.$this->year.' HINGGA 31/'.$month.'/'.$this->year;
  58. }else if($month == 'All'){
  59. $this->title = 'ALL';
  60. $this->subtitle = 'LAPORAN PENGELUARAN KOMPAUN MENGIKUT KESALAHAN BERDASARKAN TARIKH KOMPAUN '.date('d/m/Y', strtotime($start_date)).' HINGGA '.date('d/m/Y', strtotime($end_date));
  61. }
  62. }
  63. public function collection()
  64. {
  65. return collect($this->data);
  66. }
  67. public function headings(): array
  68. {
  69. return [
  70. 'BIL',
  71. 'KESALAHAN',
  72. 'JUMLAH KOMPAUN KELUAR',
  73. 'AMAUN KOMPAUN (RM)',
  74. 'JUMLAH BAYAR',
  75. 'AMAUN BAYAR (RM)',
  76. 'JUMLAH KOMPAUN BELUM BAYAR',
  77. 'AMAUN TUNGGAKAN (RM)',
  78. 'KOMPAUN BATAL',
  79. 'AMAUN BATAL (RM)',
  80. 'KOMPAUN KURANG',
  81. 'AMAUN KURANG (RM)',
  82. ];
  83. }
  84. public function title(): string
  85. {
  86. return $this->title;
  87. }
  88. public function registerEvents(): array
  89. {
  90. return [
  91. AfterSheet::class => function(AfterSheet $event) {
  92. $column = ['C','D','E','F','G','H','I','J','K','L','M'];
  93. foreach ($column as $key => $c) {
  94. $event->sheet->getDelegate()->getColumnDimension($c)->setWidth(8.43);
  95. }
  96. $event->sheet->getDelegate()->getColumnDimension('B')->setWidth(52);
  97. // set up a style array for cell formatting
  98. $style_bold_text_center = [
  99. 'alignment' => [
  100. 'horizontal' => 'center',
  101. 'vertical' => 'center',
  102. ],
  103. 'font' => [
  104. 'bold' => true,
  105. 'size' => 8,
  106. 'name' => 'Arial'
  107. ],
  108. ];
  109. $style_bold_text_left = [
  110. 'alignment' => [
  111. 'horizontal' => 'left',
  112. 'vertical' => 'center',
  113. ],
  114. 'font' => [
  115. 'bold' => true,
  116. 'size' => 8,
  117. 'name' => 'Arial'
  118. ]
  119. ];
  120. $style_normal_text_left = [
  121. 'alignment' => [
  122. 'horizontal' => 'left',
  123. 'vertical' => 'center',
  124. ],
  125. 'font' => [
  126. 'bold' => false,
  127. 'size' => 8,
  128. 'name' => 'Arial'
  129. ]
  130. ];
  131. $style_normal_text_center = [
  132. 'alignment' => [
  133. 'horizontal' => 'center',
  134. 'vertical' => 'center',
  135. ],
  136. 'font' => [
  137. 'bold' => false,
  138. 'size' => 8,
  139. 'name' => 'Arial'
  140. ]
  141. ];
  142. $style_border = [
  143. 'borders' => [
  144. 'allBorders' => [
  145. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
  146. 'color' => ['argb' => '000000'],
  147. ],
  148. ],
  149. ];
  150. // at row 1, insert 2 rows
  151. $event->sheet->insertNewRowBefore(1, 2);
  152. // merge cells for full-width
  153. $event->sheet->mergeCells('A1:L1');
  154. $event->sheet->mergeCells('A2:L2');
  155. // assign cell values
  156. $event->sheet->setCellValue('A1', $this->subtitle);
  157. $event->sheet->setCellValue('A2','');
  158. // last column as letter value (e.g., D)
  159. if($this->data != null){
  160. $last_column = Coordinate::stringFromColumnIndex(count($this->data[0]));
  161. $second_last_column = Coordinate::stringFromColumnIndex(count($this->data[0])-1);
  162. // calculate last row + 1 (total results + header rows + column headings row + new row)
  163. $last_row = count($this->data) + 2 + 1 + 1 + 2;
  164. $event->sheet->setCellValue(sprintf('B%d',$last_row-2), 'JUMLAH');
  165. $event->sheet->setCellValue(sprintf('C%d',$last_row-2), '=SUM(C4:C'.($last_row-3).')');
  166. $event->sheet->setCellValue(sprintf('D%d',$last_row-2), '=SUM(D4:D'.($last_row-3).')');
  167. $event->sheet->setCellValue(sprintf('E%d',$last_row-2), '=SUM(E4:E'.($last_row-3).')');
  168. $event->sheet->setCellValue(sprintf('F%d',$last_row-2), '=SUM(F4:F'.($last_row-3).')');
  169. $event->sheet->setCellValue(sprintf('G%d',$last_row-2), '=SUM(G4:G'.($last_row-3).')');
  170. $event->sheet->setCellValue(sprintf('H%d',$last_row-2), '=SUM(H4:H'.($last_row-3).')');
  171. $event->sheet->setCellValue(sprintf('I%d',$last_row-2), '=SUM(I4:I'.($last_row-3).')');
  172. $event->sheet->setCellValue(sprintf('J%d',$last_row-2), '=SUM(J4:J'.($last_row-3).')');
  173. $event->sheet->setCellValue(sprintf('K%d',$last_row-2), '=SUM(K4:K'.($last_row-3).')');
  174. $event->sheet->setCellValue(sprintf('L%d',$last_row-2), '=SUM(L4:L'.($last_row-3).')');
  175. for($i = 0; $i < count($this->data); $i++){
  176. $event->sheet->setCellValue(sprintf('A%d',$i+4), $i+1);
  177. $event->sheet->setCellValue(sprintf('H%d',$i+4), '=D'.($i+4).'-F'.($i+4).'-L'.($i+4));
  178. }
  179. // assign cell styles
  180. $event->sheet->getStyle('A1:A2')->applyFromArray($style_bold_text_center);
  181. $event->sheet->getStyle(sprintf('A3:L3',$last_column))->applyFromArray($style_bold_text_center);
  182. $event->sheet->getStyle(sprintf('A3:L3',$last_column))->getAlignment()->setWrapText(true);
  183. $event->sheet->getStyle(sprintf('B4:B%d',($last_row-2)))->getAlignment()->setWrapText(true);
  184. $event->sheet->getStyle(sprintf('A4:A%d',($last_row-2)))->applyFromArray($style_normal_text_center);
  185. $event->sheet->getStyle(sprintf('B4:B%d',($last_row-2)))->applyFromArray($style_normal_text_left);
  186. $event->sheet->getStyle(sprintf('C4:C%d',($last_row-2)))->applyFromArray($style_normal_text_center);
  187. $event->sheet->getStyle(sprintf('D4:D%d',($last_row-2)))->applyFromArray($style_normal_text_center);
  188. $event->sheet->getStyle(sprintf('E4:E%d',($last_row-2)))->applyFromArray($style_normal_text_center);
  189. $event->sheet->getStyle(sprintf('F4:F%d',($last_row-2)))->applyFromArray($style_normal_text_center);
  190. $event->sheet->getStyle(sprintf('G4:G%d',($last_row-2)))->applyFromArray($style_normal_text_center);
  191. $event->sheet->getStyle(sprintf('H4:H%d',($last_row-2)))->applyFromArray($style_normal_text_center);
  192. $event->sheet->getStyle(sprintf('I4:I%d',($last_row-2)))->applyFromArray($style_normal_text_center);
  193. $event->sheet->getStyle(sprintf('J4:J%d',($last_row-2)))->applyFromArray($style_normal_text_center);
  194. $event->sheet->getStyle(sprintf('K4:K%d',($last_row-2)))->applyFromArray($style_normal_text_center);
  195. $event->sheet->getStyle(sprintf('L4:L%d',($last_row-2)))->applyFromArray($style_normal_text_center);
  196. $event->sheet->getStyle(sprintf('B%d:L%d',($last_row-2),($last_row-2)))->applyFromArray($style_bold_text_center);
  197. //assign border styles
  198. $event->sheet->getStyle(sprintf('A3:A%d',($last_row-2)))->applyFromArray($style_border);
  199. $event->sheet->getStyle(sprintf('B3:B%d',($last_row-2)))->applyFromArray($style_border);
  200. $event->sheet->getStyle(sprintf('C3:C%d',($last_row-2)))->applyFromArray($style_border);
  201. $event->sheet->getStyle(sprintf('D3:D%d',($last_row-2)))->applyFromArray($style_border);
  202. $event->sheet->getStyle(sprintf('E3:E%d',($last_row-2)))->applyFromArray($style_border);
  203. $event->sheet->getStyle(sprintf('F3:F%d',($last_row-2)))->applyFromArray($style_border);
  204. $event->sheet->getStyle(sprintf('G3:G%d',($last_row-2)))->applyFromArray($style_border);
  205. $event->sheet->getStyle(sprintf('H3:H%d',($last_row-2)))->applyFromArray($style_border);
  206. $event->sheet->getStyle(sprintf('I3:I%d',($last_row-2)))->applyFromArray($style_border);
  207. $event->sheet->getStyle(sprintf('J3:J%d',($last_row-2)))->applyFromArray($style_border);
  208. $event->sheet->getStyle(sprintf('K3:K%d',($last_row-2)))->applyFromArray($style_border);
  209. $event->sheet->getStyle(sprintf('L3:L%d',($last_row-2)))->applyFromArray($style_border);
  210. }
  211. },
  212. ];
  213. }
  214. }