BillExport.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace App\Exports\Car;
  3. use App\Repositories\Enums\Car\BillTypeEnum;
  4. use App\Repositories\Enums\Car\OrderDrivingStatusEnum;
  5. use App\Repositories\Enums\Check\StatusEnum;
  6. use App\Repositories\Models\Base\Dict;
  7. use App\Repositories\Models\Car\Bill;
  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 BillExport 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' => 15,
  28. // ],
  29. [
  30. 'name' => '学员姓名',
  31. 'width' => 15,
  32. ],
  33. // [
  34. // 'name' => '手机号',
  35. // 'width' => 15,
  36. // ],
  37. // [
  38. // 'name' => '身份证号',
  39. // 'width' => 30,
  40. // ],
  41. [
  42. 'name' => '班型',
  43. 'width' => 15,
  44. ],
  45. [
  46. 'name' => '费用类型',
  47. 'width' => 15,
  48. 'type' => 0,
  49. ],
  50. [
  51. 'name' => '总学费',
  52. 'width' => 10,
  53. ],
  54. // [
  55. // 'name' => '优惠金额',
  56. // 'width' => 10,
  57. // ],
  58. // [
  59. // 'name' => '已交学费',
  60. // 'width' => 10,
  61. // ],
  62. // [
  63. // 'name' => '学车进度',
  64. // 'width' => 15,
  65. // ],
  66. [
  67. 'name' => '门店名称',
  68. 'width' => 20,
  69. ],
  70. [
  71. 'name' => '准驾类型',
  72. 'width' => 10,
  73. ],
  74. [
  75. 'name' => '招生老师',
  76. 'width' => 15,
  77. ],
  78. [
  79. 'name' => '缴费金额',
  80. 'width' => 10,
  81. 'type' => 1
  82. ],
  83. [
  84. 'name' => '奖励金额',
  85. 'width' => 10,
  86. 'type' => 0
  87. ],
  88. [
  89. 'name' => '录入人员',
  90. 'width' => 15,
  91. 'type' => 1,
  92. ],
  93. [
  94. 'name' => '审核状态',
  95. 'width' => 15,
  96. 'type' => 0
  97. ],
  98. [
  99. 'name' => '确收状态',
  100. 'width' => 15,
  101. 'type' => 1
  102. ],
  103. [
  104. 'name' => '审核人',
  105. 'width' => 15,
  106. 'type' => 0
  107. ],
  108. [
  109. 'name' => '确收人',
  110. 'width' => 15,
  111. 'type' => 1
  112. ],
  113. [
  114. 'name' => '收款人',
  115. 'width' => 15,
  116. 'type' => 0,
  117. ],
  118. [
  119. 'name' => '订单号',
  120. 'width' => 25,
  121. ],
  122. [
  123. 'name' => '是否发放',
  124. 'width' => 10,
  125. 'type' => 0
  126. ],
  127. [
  128. 'name' => '备注',
  129. 'width' => 20
  130. ],
  131. [
  132. 'name' => '时间',
  133. 'width' => 20
  134. ],
  135. ];
  136. protected $rowNums = 0;
  137. protected $where = [];
  138. protected $lastC = 'A';
  139. protected $fields = [];
  140. public function __construct($where, $field_ids = [])
  141. {
  142. $this->where = $where;
  143. $fields = [];
  144. if (!count($field_ids)) {
  145. $fields = self::BaseFields;
  146. } else {
  147. foreach ($field_ids as $field_id) {
  148. $fields[] = self::BaseFields[$field_id];
  149. }
  150. }
  151. $this->fields = $fields;
  152. }
  153. public function map($row): array
  154. {
  155. $row = $row->toArray();
  156. $this->rowNums++;
  157. $data = [];
  158. foreach (array_column($this->fields, 'name') as $k => $field) {
  159. if ($field == '序号') $data[$k] = $this->rowNums;
  160. // if ($field == '报名日期') $data[$k] = $row['sign_up_day'];
  161. if ($field == '学员姓名') $data[$k] = $row['user'] ? $row['user']['name'] : '';
  162. // if ($field == '手机号') $data[$k] = $row['user_mobile'];
  163. // if ($field == '身份证号') $data[$k] = $row['user_id_card'];
  164. if ($field == '总学费') $data[$k] = $row['order']['total_money'];
  165. if ($field == '缴费金额') $data[$k] = $row['money'];
  166. if ($field == '奖励金额') $data[$k] = $row['money'];
  167. if ($field == '备注') $data[$k] = $row['remark'];
  168. if ($field == '是否发放') $data[$k] = $row['is_give'];
  169. if ($field == '时间') $data[$k] = $row['created_at'];
  170. if ($field == '班型') $data[$k] = $row['order'] ? $row['order']['grade']['name'] : '';
  171. if ($field == '门店名称') $data[$k] = $row['shop'] ? $row['shop']['name'] : '';
  172. if ($field == '招生老师') $data[$k] = $row['order']['salesman_admin'] ? $row['order']['salesman_admin']['name'] : '';
  173. if ($field == '录入人员') $data[$k] = $row['admin'] ? $row['admin']['name'] : '';
  174. if ($field == '录入人员') $data[$k] = $row['admin'] ? $row['admin']['name'] : '';
  175. if ($field == '收款人') $data[$k] = $row['admin'] ? $row['admin']['name'] : '';
  176. if ($field == '审核人') $data[$k] = $row['check_admin'] ? $row['check_admin']['name'] : '';
  177. if ($field == '确收人') $data[$k] = $row['check_admin'] ? $row['check_admin']['name'] : '';
  178. if ($field == '订单号') $data[$k] = $row['order'] ? $row['order']['no'] : '';
  179. if ($field == '费用类型') $data[$k] = BillTypeEnum::getDescription($row['type_id']);
  180. if ($field == '审核状态') $data[$k] = StatusEnum::getDescription($row['check_status']);
  181. if ($field == '确收状态') $data[$k] = StatusEnum::getDescription($row['check_status']);
  182. if ($field == '学车进度') $data[$k] = OrderDrivingStatusEnum::getDescription($row['order']['driving_status']);
  183. if ($field == '准驾类型') $data[$k] = Dict::byCodeAndIdGetDict('CAR_TYPE', $row['order']['drive_type']);
  184. }
  185. return $data;
  186. }
  187. public function query()
  188. {
  189. $where = $this->where;
  190. return Bill::query()
  191. ->with(['user', 'shop', 'order', 'order.grade', 'admin', 'check_admin'])
  192. ->when(array_key_exists('ids', $where) && count($where['ids']), function ($query) use ($where) {
  193. return $query->whereIn('id', $where['ids']);
  194. })
  195. ->when(array_key_exists('disburse', $where), function ($query) use ($where) {
  196. return $query->where('disburse', $where['disburse']);
  197. })
  198. ->when(array_key_exists('check_status', $where), function ($query) use ($where) {
  199. return $query->where('check_status', $where['check_status']);
  200. })
  201. ->when(array_key_exists('type_id', $where), function ($query) use ($where) {
  202. return $query->where('type_id', $where['type_id']);
  203. })
  204. ->when(array_key_exists('shop_id', $where), function ($query) use ($where) {
  205. return $query->where('shop_id', $where['shop_id']);
  206. })
  207. ->when(array_key_exists('is_give', $where), function ($query) use ($where) {
  208. return $query->where('is_give', $where['is_give']);
  209. })
  210. ->when(array_key_exists('start_check_day', $where), function ($query) use ($where) {
  211. return $query->whereDate('check_time', '>=', $where['start_check_day']);
  212. })
  213. ->when(array_key_exists('end_check_time', $where), function ($query) use ($where) {
  214. return $query->whereDate('check_time', '<=', $where['start_check_day']);
  215. })
  216. ->when(array_key_exists('admin_id', $where), function ($query) use ($where) {
  217. return $query->where('admin_id', $where['admin_id']);
  218. })
  219. ->where('is_lock', 0)
  220. ->orderByDesc('id');
  221. }
  222. public function headings(): array
  223. {
  224. $room_name = '财务流水列表';
  225. $data[] = [$room_name];
  226. $data[] = array_column($this->fields, 'name');
  227. return $data;
  228. }
  229. public function headingRow(): int
  230. {
  231. return 2;
  232. }
  233. public function columnWidths(): array
  234. {
  235. $i = 65;
  236. $data = [];
  237. foreach ($this->fields as $k => $field) {
  238. $s = intval($k / 26);
  239. if (!$s) $key = chr($i + $k);
  240. if ($s > 0) {
  241. $y = intval($k % 26);
  242. $key = chr($s + $i - 1);
  243. $key .= chr($y + $i);
  244. }
  245. $data["{$key}"] = $field['width'];
  246. $this->lastC = $key;
  247. }
  248. return $data;
  249. }
  250. public function styles(Worksheet $sheet)
  251. {
  252. $last = $this->lastC;
  253. $cellRange = "A1:{$last}1";
  254. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  255. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  256. // 文字居中
  257. $lastrow = $sheet->getHighestRow();
  258. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  259. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  260. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  261. $sheet->mergeCells("A1:{$last}1"); //合并
  262. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  263. }
  264. }