InsurancesExport.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Exports\Financial;
  3. use App\Repositories\Enums\Human\StaffInsuranceStatusEnum;
  4. use App\Repositories\Enums\ModelStatusEnum;
  5. use App\Repositories\Models\Base\Dict;
  6. use App\Repositories\Models\Financial\InsuranceRecord;
  7. use App\Repositories\Models\Recruit\Position;
  8. use Maatwebsite\Excel\Concerns\Exportable;
  9. use Maatwebsite\Excel\Concerns\FromQuery;
  10. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  11. use Maatwebsite\Excel\Concerns\WithColumnWidths;
  12. use Maatwebsite\Excel\Concerns\WithHeadings;
  13. use Maatwebsite\Excel\Concerns\WithMapping;
  14. use Maatwebsite\Excel\Concerns\WithStyles;
  15. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  16. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  17. class InsurancesExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
  18. {
  19. use Exportable;
  20. const BaseFields = [
  21. [
  22. 'name' => '序号',
  23. 'width' => 8,
  24. ],
  25. [
  26. 'name' => '类型',
  27. 'width' => 8,
  28. ],
  29. [
  30. 'name' => '姓名',
  31. 'width' => 15,
  32. ],
  33. [
  34. 'name' => '身份证号',
  35. 'width' => 30,
  36. ],
  37. [
  38. 'name' => '派遣单位',
  39. 'width' => 30,
  40. ],
  41. [
  42. 'name' => '派遣工种',
  43. 'width' => 15,
  44. ],
  45. [
  46. 'name' => '入职日期/离职日期',
  47. 'width' => 20,
  48. ],
  49. [
  50. 'name' => '商业保险金额',
  51. 'width' => 15,
  52. ],
  53. [
  54. 'name' => '状态',
  55. 'width' => 15,
  56. ]
  57. ];
  58. protected $rowNums = 0;
  59. protected $id = 0;
  60. protected $day = 0;
  61. protected $type = 0;
  62. protected $lastC = 'A';
  63. protected $fields = [];
  64. public function __construct($id = 0, $field_ids = [])
  65. {
  66. $this->id = $id;
  67. $fields = [];
  68. if (is_string($field_ids)) {
  69. $fields = self::BaseFields;
  70. } else {
  71. foreach ($field_ids as $field_id) {
  72. $fields[] = self::BaseFields[$field_id];
  73. }
  74. }
  75. $this->fields = $fields;
  76. }
  77. public function query()
  78. {
  79. $id = $this->id;
  80. return InsuranceRecord::query()
  81. ->with(['human', 'factory', 'staff'])
  82. ->where('status', ModelStatusEnum::OK)
  83. ->where(function ($query) use ($id) {
  84. return $query->orWhere('insurance_id', $id)->orWhere('out_insurance_id', $id);
  85. })
  86. ->orderBy('id');
  87. }
  88. public function headingRow(): int
  89. {
  90. return 2;
  91. }
  92. public function headings(): array
  93. {
  94. $room_name = '员工商业保险名单';
  95. $data[] = [$room_name];
  96. $data[] = array_column($this->fields, 'name');
  97. return $data;
  98. }
  99. public function columnWidths(): array
  100. {
  101. $i = 65;
  102. $data = [];
  103. foreach ($this->fields as $k => $field) {
  104. $s = intval($k / 26);
  105. if (!$s) $key = chr($i + $k);
  106. if ($s > 0) {
  107. $y = intval($k % 26);
  108. $key = chr($s + $i - 1);
  109. $key .= chr($y + $i);
  110. }
  111. $data["{$key}"] = $field['width'];
  112. $this->lastC = $key;
  113. }
  114. return $data;
  115. }
  116. public function styles(Worksheet $sheet)
  117. {
  118. $last = $this->lastC;
  119. $cellRange = "A1:{$last}1";
  120. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  121. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  122. // 文字居中
  123. $lastrow = $sheet->getHighestRow();
  124. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  125. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  126. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  127. $sheet->mergeCells("A1:{$last}1"); //合并
  128. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  129. }
  130. public function map($row): array
  131. {
  132. $row = $row->toArray();
  133. $this->rowNums++;
  134. $data = [];
  135. foreach (array_column($this->fields, 'name') as $k => $field) {
  136. if ($field == '序号') $data[$k] = $this->rowNums;
  137. if ($field == '类型') {
  138. $data[$k] = $row['out_insurance_id'] ? '离职' : '入职';
  139. }
  140. if ($field == '姓名') {
  141. $data[$k] = $row['human'] ? $row['human']['name'] : '';
  142. }
  143. if ($field == '身份证号') {
  144. $data[$k] = $row['human'] ? $row['human']['card_id'] . ' ' : '';
  145. }
  146. if ($field == '派遣单位') {
  147. $data[$k] = $row['factory'] ? $row['factory']['name'] : '';
  148. }
  149. if ($field == '派遣工种') {
  150. $data[$k] = Dict::byCodeAndIdGetDict(Position::JOB_CODE, $row['job_position']);
  151. }
  152. if ($field == '商业保险金额') $data[$k] = $row['money'];
  153. if ($row['out_insurance_id']) {
  154. //离职状态
  155. if ($field == '入职日期/离职日期') $data[$k] = $row['staff'] ? $row['staff']['quit_date'] : '';
  156. if ($field == '状态') {
  157. if ($row['insurance_status'] == StaffInsuranceStatusEnum::WAIT) $data[$k] = '待购买';
  158. if ($row['insurance_status'] == StaffInsuranceStatusEnum::BUY_OK) $data[$k] = '待退订';
  159. if ($row['insurance_status'] == StaffInsuranceStatusEnum::OUT_OK) $data[$k] = '已退订';
  160. }
  161. } else {
  162. if ($field == '入职日期/离职日期') $data[$k] = $row['staff'] ? $row['staff']['induction_date'] : '';
  163. if ($field == '状态') {
  164. if ($row['insurance_status'] == StaffInsuranceStatusEnum::WAIT) $data[$k] = '待购买';
  165. if ($row['insurance_status'] == StaffInsuranceStatusEnum::BUY_OK) $data[$k] = '已购买';
  166. if ($row['insurance_status'] == StaffInsuranceStatusEnum::OUT_OK) $data[$k] = '已退订';
  167. }
  168. }
  169. }
  170. return $data;
  171. }
  172. }