ObraNominaController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace app\modules\excel\controllers;
  3. use DateTime;
  4. use DateTimeZone;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  7. use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
  8. use v1\models\Nomina;
  9. use v1\models\Obra;
  10. use v1\models\Pago;
  11. use Yii;
  12. use yii\web\Controller;
  13. class ObraNominaController extends Controller {
  14. public function actionExcelObraNomina($idObra) {
  15. $obra = Obra::findOne($idObra);
  16. $request = Yii::$app->request;
  17. $query= Nomina::find()
  18. ->joinWith('empleado')
  19. ->andWhere(['{{Nomina}}.[[eliminado]]'=>null])->andWhere(['{{Nomina}}.[[idObra]]'=>$idObra]);
  20. $query->orderBy(['creado' => SORT_DESC]);
  21. $spreadsheet = new Spreadsheet();
  22. $sheet = $spreadsheet->getActiveSheet();
  23. $sheet->setTitle('Nomina de la obra');
  24. $BASEPATH = \Yii::getAlias('@app') . "/web";
  25. $logo = $BASEPATH . '/img/logos/edesarrollos-unicolor-azul.png';
  26. $fechaInicio = (new DateTime($obra->fechaInicio))->format('d-m-Y');
  27. $fechaFin = (new DateTime($obra->fechaFinal))->format('d-m-Y');
  28. $drawing = new Drawing();
  29. $drawing->setName('Logotipo');
  30. $drawing->setDescription('Logotipo de la empresa');
  31. $drawing->setPath($logo);
  32. $drawing->setHeight(70);
  33. $drawing->setCoordinates('A1');
  34. $drawing->setOffsetX(10);
  35. $drawing->setOffsetY(10);
  36. $drawing->setWorksheet($sheet);
  37. $sheet->mergeCells('A5:J5');
  38. $sheet->setCellValue('A5','Nomina relacionado a la obra: ' . $obra->nombre);
  39. $sheet->mergeCells('A6:E6');
  40. $sheet->setCellValue('A6','Fecha Inicial: ' .$fechaInicio);
  41. $sheet->mergeCells('F6:J6');
  42. $sheet->setCellValue('F6','Fecha Final: ' .$fechaFin);
  43. $sheet->getStyle('A5')->getFont()->setBold(true);
  44. $sheet->getStyle('A5')->getFont()->setSize(16);
  45. $headerStyle = [
  46. 'font' => [
  47. 'bold' => true,
  48. 'color' => ['argb' => 'FFFFFFFF'] // Letras en blanco
  49. ],
  50. 'alignment' => [
  51. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  52. 'vertical' => Alignment::VERTICAL_CENTER,
  53. 'wrapText' => true
  54. ],
  55. 'fill' => [
  56. 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
  57. 'startColor' => ['argb' => 'FF625FF5'] // Fondo azul (con opacidad FF)
  58. ],
  59. 'borders' => [
  60. 'allBorders' => [
  61. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  62. 'color' => ['argb' => 'FF020073'], // Bordes azul oscuro (con opacidad FF)
  63. ],
  64. ],
  65. ];
  66. // Aplicar el borde a las celdas combinadas
  67. $sheet->getStyle('A5:J5')->applyFromArray( $headerStyle);
  68. $sheet->getStyle('A6:E6')->applyFromArray( $headerStyle);
  69. $sheet->getStyle('F6:J6')->applyFromArray( $headerStyle);
  70. $sheet->mergeCells('A8:C8');
  71. $sheet->setCellValue('A8', 'Concepto Obra');
  72. $sheet->getStyle('A8:C8')->applyFromArray( $headerStyle);
  73. $sheet->mergeCells('D8:F8');
  74. $sheet->setCellValue('D8', 'Cantidad');
  75. $sheet->getStyle('D8:F8')->applyFromArray( $headerStyle);
  76. $sheet->mergeCells('G8:J8');
  77. $sheet->setCellValue('G8', 'Fecha Inicio y Fecha Fin');
  78. $sheet->getStyle('G8:J8')->applyFromArray( $headerStyle);
  79. $cellBorderStyle = [
  80. 'borders' => [
  81. 'allBorders' => [
  82. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  83. 'color' => ['argb' => 'FF000000'], // Color negro
  84. ],
  85. ],
  86. ];
  87. $row = 9;
  88. foreach ($query->each() as $obraNomina) {
  89. $fechaInicio = (new DateTime($obraNomina->fechaInicio))->format('d-m-Y');
  90. $fechaFin = (new DateTime($obraNomina->fechaFin))->format('d-m-Y');
  91. $empleado = $obraNomina->empleado;
  92. $sheet->mergeCells('A' . $row . ':C' . $row);
  93. $sheet->setCellValue('A' . $row, $empleado->nombre);
  94. $sheet->getStyle('A' . $row . ':C' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  95. $sheet->getStyle('A' . $row . ':C' . $row)->applyFromArray($cellBorderStyle);
  96. $sheet->mergeCells('D' . $row . ':F' . $row);
  97. $sheet->setCellValue('D' . $row, $obraNomina->montoPagado);
  98. $sheet->getStyle('D' . $row . ':F' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  99. $sheet->getStyle('D' . $row . ':F' . $row)->applyFromArray($cellBorderStyle);
  100. $sheet->getStyle('D' . $row)
  101. ->getNumberFormat()
  102. ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
  103. $sheet->mergeCells('G' . $row . ':J' . $row);
  104. $sheet->setCellValue('G' . $row, $fechaInicio . ' al ' .$fechaFin);
  105. $sheet->getStyle('G' . $row . ':J' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  106. $sheet->getStyle('G' . $row . ':J' . $row)->applyFromArray($cellBorderStyle);
  107. $row++;
  108. }
  109. $sheet->getStyle('A5')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  110. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
  111. try {
  112. ob_start();
  113. $writer->save("php://output");
  114. $documento = ob_get_contents();
  115. ob_clean();
  116. Yii::$app->getResponse()->sendContentAsFile($documento, "Nomina.xlsx");
  117. } catch (\Exception $exception) {
  118. return null;
  119. }
  120. }
  121. public function actionExcelNomina($idObra) {
  122. $obra = Obra::findOne($idObra);
  123. $request = Yii::$app->request;
  124. $query= Nomina::find()
  125. ->joinWith('empleado')
  126. ->andWhere(['{{Nomina}}.[[eliminado]]'=>null])->andWhere(['{{Nomina}}.[[idObra]]'=>$idObra]);
  127. $query->orderBy(['creado' => SORT_DESC]);
  128. $spreadsheet = new Spreadsheet();
  129. $sheet = $spreadsheet->getActiveSheet();
  130. $sheet->setTitle('Nomina de la obra');
  131. $BASEPATH = \Yii::getAlias('@app') . "/web";
  132. $logo = $BASEPATH . '/img/logos/edesarrollos-unicolor-azul.png';
  133. $fechaInicio = (new DateTime($obra->fechaInicio))->format('d-m-Y');
  134. $fechaFin = (new DateTime($obra->fechaFinal))->format('d-m-Y');
  135. $drawing = new Drawing();
  136. $drawing->setName('Logotipo');
  137. $drawing->setDescription('Logotipo de la empresa');
  138. $drawing->setPath($logo);
  139. $drawing->setHeight(70);
  140. $drawing->setCoordinates('A1');
  141. $drawing->setOffsetX(10);
  142. $drawing->setOffsetY(10);
  143. $drawing->setWorksheet($sheet);
  144. $sheet->mergeCells('A5:J5');
  145. $sheet->setCellValue('A5','Nomina relacionado a la obra: ' . $obra->nombre);
  146. $sheet->mergeCells('A6:E6');
  147. $sheet->setCellValue('A6','Fecha Inicial: ' .$fechaInicio);
  148. $sheet->mergeCells('F6:J6');
  149. $sheet->setCellValue('F6','Fecha Final: ' .$fechaFin);
  150. $sheet->getStyle('A5')->getFont()->setBold(true);
  151. $sheet->getStyle('A5')->getFont()->setSize(16);
  152. $headerStyle = [
  153. 'font' => [
  154. 'bold' => true,
  155. 'color' => ['argb' => 'FFFFFFFF'] // Letras en blanco
  156. ],
  157. 'alignment' => [
  158. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  159. 'vertical' => Alignment::VERTICAL_CENTER,
  160. 'wrapText' => true
  161. ],
  162. 'fill' => [
  163. 'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
  164. 'startColor' => ['argb' => 'FF625FF5'] // Fondo azul (con opacidad FF)
  165. ],
  166. 'borders' => [
  167. 'allBorders' => [
  168. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  169. 'color' => ['argb' => 'FF020073'], // Bordes azul oscuro (con opacidad FF)
  170. ],
  171. ],
  172. ];
  173. // Aplicar el borde a las celdas combinadas
  174. $sheet->getStyle('A5:J5')->applyFromArray( $headerStyle);
  175. $sheet->getStyle('A6:E6')->applyFromArray( $headerStyle);
  176. $sheet->getStyle('F6:J6')->applyFromArray( $headerStyle);
  177. $sheet->mergeCells('A8:C8');
  178. $sheet->setCellValue('A8', 'Concepto Obra');
  179. $sheet->getStyle('A8:C8')->applyFromArray( $headerStyle);
  180. $sheet->mergeCells('D8:F8');
  181. $sheet->setCellValue('D8', 'Cantidad');
  182. $sheet->getStyle('D8:F8')->applyFromArray( $headerStyle);
  183. $sheet->mergeCells('G8:J8');
  184. $sheet->setCellValue('G8', 'Fecha Inicio y Fecha Fin');
  185. $sheet->getStyle('G8:J8')->applyFromArray( $headerStyle);
  186. $cellBorderStyle = [
  187. 'borders' => [
  188. 'allBorders' => [
  189. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  190. 'color' => ['argb' => 'FF000000'], // Color negro
  191. ],
  192. ],
  193. ];
  194. $row = 9;
  195. foreach ($query->each() as $obraNomina) {
  196. $fechaInicio = (new DateTime($obraNomina->fechaInicio))->format('d-m-Y');
  197. $fechaFin = (new DateTime($obraNomina->fechaFin))->format('d-m-Y');
  198. $empleado = $obraNomina->empleado;
  199. $sheet->mergeCells('A' . $row . ':C' . $row);
  200. $sheet->setCellValue('A' . $row, $empleado->nombre);
  201. $sheet->getStyle('A' . $row . ':C' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  202. $sheet->getStyle('A' . $row . ':C' . $row)->applyFromArray($cellBorderStyle);
  203. $sheet->mergeCells('D' . $row . ':F' . $row);
  204. $sheet->setCellValue('D' . $row, $obraNomina->montoPagado);
  205. $sheet->getStyle('D' . $row . ':F' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  206. $sheet->getStyle('D' . $row . ':F' . $row)->applyFromArray($cellBorderStyle);
  207. $sheet->getStyle('D' . $row)
  208. ->getNumberFormat()
  209. ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
  210. $sheet->mergeCells('G' . $row . ':J' . $row);
  211. $sheet->setCellValue('G' . $row, $fechaInicio . ' al ' .$fechaFin);
  212. $sheet->getStyle('G' . $row . ':J' . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY);
  213. $sheet->getStyle('G' . $row . ':J' . $row)->applyFromArray($cellBorderStyle);
  214. $row++;
  215. }
  216. $sheet->getStyle('A5')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  217. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
  218. try {
  219. return $sheet;
  220. } catch (\Exception $exception) {
  221. return null;
  222. }
  223. }
  224. }