海南旅游SAAS
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.

57 lines
1.1 KiB

  1. <?php
  2. namespace App\Exports;
  3. use App\Models\ProductSpec;
  4. use Illuminate\Support\Collection;
  5. use Maatwebsite\Excel\Concerns\FromQuery;
  6. use Maatwebsite\Excel\Concerns\WithHeadings;
  7. class ProductSpecExport implements FromQuery, WithHeadings
  8. {
  9. private int $product_id;
  10. public function __construct(int $product_id)
  11. {
  12. $this->product_id = $product_id;
  13. }
  14. /**
  15. * @return Collection
  16. */
  17. public function query()
  18. {
  19. return ProductSpec::where([
  20. ['product_id', '=', $this->product_id],
  21. ['date', '>=', date('Y-m-d')],
  22. ])->orderBy('name')->orderBy('date');
  23. }
  24. public function prepareRows($rows)
  25. {
  26. return $rows->transform(function ($row) {
  27. return [
  28. '规格名称' => $row['name'],
  29. '日期' => $row['date'],
  30. '库存' => $row['stock'],
  31. '市场价' => $row['original_price'],
  32. '销售价' => $row['price'],
  33. '成本价' => $row['cost_price'],
  34. ];
  35. });
  36. }
  37. public function headings(): array
  38. {
  39. return [
  40. '规格名称', '日期', '库存', '市场价', '销售价', '成本价',
  41. ];
  42. }
  43. public function columnFormats(): array
  44. {
  45. return [
  46. 'B' => 'yyyy/m/d',
  47. ];
  48. }
  49. }