vd/app/Domains/Stats/Exports/OrderExport.php
2019-01-08 19:01:15 +08:00

94 lines
2.2 KiB
PHP

<?php
namespace App\Domains\Stats\Exports;
use App\Core\AbstractExport;
use Dipper\Excel\Concerns\Exportable;
use Dipper\Excel\Concerns\WithHeadings;
use Dipper\Excel\Concerns\FromCollection;
use Dipper\Excel\Concerns\WithHeadingRow;
use App\Domains\Stats\Services\OrderService;
class OrderExport extends AbstractExport implements FromCollection, WithHeadings, WithHeadingRow
{
public $conditions;
public static $types = ['销售', '续费', '续费包', '加油包'];
public function __construct(array $conditions = [])
{
$this->conditions = $conditions;
parent::__construct();
}
public function collection()
{
set_time_limit(-1);
$orders = app(OrderService::class)->index($this->conditions)->map(function ($item) {
return collect([
'company_name' => $item->company_name,
'package_name' => $item->package_name,
'pay_channel_name' => $item->pay_channel_name,
'unit_price' => $item->unit_price,
'counts' => $item->counts,
'custom_price' => $item->custom_price,
]);
});
$orders->push([
'总计',
'',
'',
'',
array_sum($orders->pluck('counts')->toArray()) ?: 0,
array_sum($orders->pluck('custom_price')->toArray()) ?: 0,
]);
return $orders;
}
public function headings(): array
{
return [
'企业名称',
'套餐名称',
'支付方式',
'单价',
'数量',
'总金额',
];
}
/**
* 表格标题
*
* @return string
*/
public function title(): string
{
$title = parent::title();
$title = self::$types[$this->conditions['type']] . $title;
return $title;
}
/**
* 文件名称
*
* @return string
*/
private function filename(): string
{
$title = self::$classes[get_class($this)];
$filename = $title . date('YmdHis');
$filename = self::$types[$this->conditions['type']] . $filename;
return "export/{$filename}.xlsx";
}
}