148 lines
5.6 KiB
PHP
148 lines
5.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Exports\Sheets;
|
||
|
|
|
||
|
|
use App\Models\Server\MerchantAccount;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
||
|
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||
|
|
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
|
||
|
|
use Maatwebsite\Excel\Concerns\WithEvents;
|
||
|
|
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
|
||
|
|
use Maatwebsite\Excel\Concerns\WithTitle;
|
||
|
|
use Maatwebsite\Excel\Events\AfterSheet;
|
||
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||
|
|
|
||
|
|
class SaasQuarterIncome implements FromCollection, WithTitle, WithEvents, WithStrictNullComparison, ShouldAutoSize,WithColumnFormatting
|
||
|
|
{
|
||
|
|
|
||
|
|
protected $date_arr, $key;
|
||
|
|
public function __construct($key, $date_arr)
|
||
|
|
{
|
||
|
|
$this->key = $key;
|
||
|
|
$this->date_arr = $date_arr;
|
||
|
|
}
|
||
|
|
|
||
|
|
//格式化数据
|
||
|
|
public function columnFormats(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'D' => NumberFormat::FORMAT_NUMBER_00, //金额保留两位小数
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* sheet数据
|
||
|
|
* @return \Illuminate\Support\Collection
|
||
|
|
*/
|
||
|
|
public function collection()
|
||
|
|
{
|
||
|
|
$rows = [
|
||
|
|
["深圳福恋智能信息科技有限公司
|
||
|
|
|
||
|
|
SAAS系统 直属福恋收款明细表", '', '', '', '','','',''],
|
||
|
|
["序号", '日期', '收款名称', '金额', '收款手续费', '收款手续费','收入到账银行','备注']
|
||
|
|
];
|
||
|
|
|
||
|
|
foreach ($this->date_arr as $date) {
|
||
|
|
$start_date = $date['start_date'];
|
||
|
|
$end_date = $date['end_date'];
|
||
|
|
$date = date('Y-m', strtotime($start_date));
|
||
|
|
|
||
|
|
$merchant1 = MerchantAccount::with(['earnings'=> function($sql) use($start_date, $end_date){
|
||
|
|
$sql->where('m_user_id', 0)->whereBetween('created_at', [$start_date, $end_date]);
|
||
|
|
}, 'anchorV2'])->where('id', 71)->first();
|
||
|
|
if ($merchant1) {
|
||
|
|
$amount1 = number_format($merchant1->earnings->sum('value'), 2, '.', '') ;
|
||
|
|
}else {
|
||
|
|
$amount1 = 0;
|
||
|
|
}
|
||
|
|
$rows[] = [1, $date, '悦睦', $amount1, '', '', '微信', ''];
|
||
|
|
$merchant2 = MerchantAccount::with(['earnings'=> function($sql) use($start_date, $end_date){
|
||
|
|
$sql->where('m_user_id', 0)->whereBetween('created_at', [$start_date, $end_date]);
|
||
|
|
}, 'anchorV2'])->where('id', 491)->first();
|
||
|
|
if ($merchant2) {
|
||
|
|
$amount2 = number_format($merchant2->earnings->sum('value'), 2, '.', '') ;
|
||
|
|
}else {
|
||
|
|
$amount2 = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
$rows[] = [2, $date, "福恋", $amount2, '', '', '微信', ''];
|
||
|
|
$rows[] = ['','', '合计', number_format($amount1 + $amount2, 2, '.', ''), '', '', ''];
|
||
|
|
}
|
||
|
|
return collect($rows);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function registerEvents(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
AfterSheet::class => function(AfterSheet $event) {
|
||
|
|
//设置列宽
|
||
|
|
$arr = ['A','B','C','D','E','F',"G","H"];
|
||
|
|
foreach ($arr as $value) {
|
||
|
|
$event->sheet->getDelegate()->getColumnDimension($value)->setWidth(20);
|
||
|
|
}
|
||
|
|
//自动换行
|
||
|
|
$event->sheet->getStyle("A1:F1")->getAlignment()->setWrapText(TRUE);
|
||
|
|
$event->sheet->getDelegate()->getRowDimension(1)->setRowHeight(80);
|
||
|
|
//设置行高,$i为数据行数
|
||
|
|
for ($i = 2; $i<=1265; $i++) {
|
||
|
|
$event->sheet->getDelegate()->getRowDimension($i)->setRowHeight(30);
|
||
|
|
}
|
||
|
|
//设置区域单元格垂直居中
|
||
|
|
$event->sheet->getDelegate()->getStyle('A1:K1265')->getAlignment()->setVertical('center');
|
||
|
|
//设置区域单元格水平居中
|
||
|
|
$event->sheet->getDelegate()->getStyle('A1:K1265')->getAlignment()->setHorizontal('center');
|
||
|
|
//设置区域单元格字体、颜色、背景等,其他设置请查看 applyFromArray 方法,提供了注释
|
||
|
|
$event->sheet->getDelegate()->getStyle('A2:H11')->applyFromArray([
|
||
|
|
'borders'=> [
|
||
|
|
'allBorders' => [
|
||
|
|
'borderStyle' =>'thin',
|
||
|
|
]
|
||
|
|
]
|
||
|
|
]);
|
||
|
|
$event->sheet->getDelegate()->getStyle('A1:H1')->applyFromArray([
|
||
|
|
'font' => [
|
||
|
|
'name' => '宋体',
|
||
|
|
'bold' => false,
|
||
|
|
'italic' => false,
|
||
|
|
'strikethrough' => false,
|
||
|
|
'size' => 14,
|
||
|
|
'color' => [
|
||
|
|
'rgb' => '000000'
|
||
|
|
],
|
||
|
|
]
|
||
|
|
|
||
|
|
]);
|
||
|
|
$event->sheet->getDelegate()->getStyle('A2:H1080')->applyFromArray([
|
||
|
|
'font' => [
|
||
|
|
'name' => '宋体',
|
||
|
|
'bold' => false,
|
||
|
|
'italic' => false,
|
||
|
|
'strikethrough' => false,
|
||
|
|
'size' => 11,
|
||
|
|
'color' => [
|
||
|
|
'rgb' => '000000'
|
||
|
|
],
|
||
|
|
]
|
||
|
|
|
||
|
|
]);
|
||
|
|
//合并单元格
|
||
|
|
$event->sheet->getDelegate()->mergeCells('A1:H1');
|
||
|
|
$event->sheet->getDelegate()->mergeCells('A5:B5');
|
||
|
|
$event->sheet->getDelegate()->mergeCells('A8:B8');
|
||
|
|
$event->sheet->getDelegate()->mergeCells('A11:B11');
|
||
|
|
}
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* sheet标题
|
||
|
|
* @return string
|
||
|
|
*/
|
||
|
|
public function title(): string
|
||
|
|
{
|
||
|
|
return "第".($this->key + 1)."季度福恋收款明细";
|
||
|
|
}
|
||
|
|
}
|