294 lines
14 KiB
PHP
294 lines
14 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
namespace App\Exports;
|
|||
|
|
|
|||
|
|
use App\Models\Consultation;
|
|||
|
|
use App\Models\Course\Course;
|
|||
|
|
use App\Models\Live\Anchor;
|
|||
|
|
use App\Models\MerchantShop;
|
|||
|
|
use App\Models\Order;
|
|||
|
|
use App\Models\Server\CommunityActivity;
|
|||
|
|
use App\Models\Server\MEarning;
|
|||
|
|
use App\Models\Server\MerchantAccount;
|
|||
|
|
use App\Models\Server\MerchantEvaluate;
|
|||
|
|
use App\Models\Server\MerchantTransferLog;
|
|||
|
|
use App\Models\Server\MerchantUser;
|
|||
|
|
use App\Models\Server\TouristOrder;
|
|||
|
|
use App\Models\User;
|
|||
|
|
use Illuminate\Support\Facades\Log;
|
|||
|
|
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\Events\AfterSheet;
|
|||
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
|||
|
|
|
|||
|
|
class StatementExport implements FromCollection,WithColumnFormatting, WithEvents, WithStrictNullComparison, ShouldAutoSize
|
|||
|
|
{
|
|||
|
|
protected $start_date, $end_date, $type;
|
|||
|
|
public function __construct($start_date, $type="")
|
|||
|
|
{
|
|||
|
|
$this->start_date = date('Y-m-d', strtotime($start_date));
|
|||
|
|
$this->end_date = date("Y-m-d", strtotime('next month', strtotime($start_date)));
|
|||
|
|
$this->type = $type;
|
|||
|
|
}
|
|||
|
|
public function saasContentV2($start_date, $end_date)
|
|||
|
|
{
|
|||
|
|
$rows = [
|
|||
|
|
["深圳福恋智能信息科技有限公司
|
|||
|
|
|
|||
|
|
SAAS收入明细表", '', '', '', '','','','','','',''],
|
|||
|
|
['ID',"订单号", "商户", "性质", "类型",'商品名称', '金额', '订单退款金额', '手续费率', '手续费', '订单时间', '月份']
|
|||
|
|
];
|
|||
|
|
$orders = TouristOrder::with('mEarning', 'refunds')->where('price', ">", 0)->whereBetween('created_at', [$start_date, $end_date])->get();
|
|||
|
|
foreach ($orders as $order)
|
|||
|
|
{
|
|||
|
|
$goods_name = $order->goods;
|
|||
|
|
if (empty($order->goods)) {
|
|||
|
|
switch ($order->type) {
|
|||
|
|
case "community":
|
|||
|
|
$goods_name = CommunityActivity::where('id', $order->type_id)->value('title');
|
|||
|
|
break;
|
|||
|
|
case "course":
|
|||
|
|
$goods_name = Course::where('id', $order->type_id)->value('title');
|
|||
|
|
break;
|
|||
|
|
case "consult":
|
|||
|
|
$goods_name = Consultation::where('id', $order->type_id)->value("title");
|
|||
|
|
break;
|
|||
|
|
case "evaluate":
|
|||
|
|
$goods_name = MerchantEvaluate::where("id", $order->type_id)->value("title");
|
|||
|
|
break;
|
|||
|
|
case "shop":
|
|||
|
|
$goods_name = MerchantShop::where("id", $order->type_id)->value("title");
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if ($order) {
|
|||
|
|
$earnings = $order->mEarning;
|
|||
|
|
$refund_price = $order->refunds->sum('refund_fee')?:0;
|
|||
|
|
$poundage = 0.006;
|
|||
|
|
$rows[] = [$order->id."-1", $order->trade_no, "福恋平台", "代收", $goods_name, "-".$order->price, $refund_price, $poundage, number_format($order->price * $poundage, 2, '.',''), $order->created_at, date('m', strtotime($start_date))];
|
|||
|
|
$index = 2;
|
|||
|
|
foreach ($earnings as $earning) {
|
|||
|
|
if ($earning->m_user_id) {
|
|||
|
|
$name = MerchantUser::where('id', $earning->m_user_id)->value('nickname')?:"";
|
|||
|
|
$name = "用户( $name )";
|
|||
|
|
$poundage = 0.02;
|
|||
|
|
}else{
|
|||
|
|
$name = Anchor::where('m_id', $earning->m_id)->value('name')?:"";
|
|||
|
|
$name = "商家( $name )";
|
|||
|
|
$poundage = $order->merchant?($order->merchant->poundage / 100) :0.02;
|
|||
|
|
}
|
|||
|
|
$type = "代收";
|
|||
|
|
if ($order->type == 'transfer') {
|
|||
|
|
$log = MerchantTransferLog::where('m_earning_id', $earning->id)->first();
|
|||
|
|
if (empty($log)) dd($earning->id);
|
|||
|
|
$merchant = MerchantAccount::where('id', $log->m_id)->first();
|
|||
|
|
$merchant_name = $merchant&&$merchant->anchorV2?$merchant->anchorV2->name:"未知";
|
|||
|
|
$type = "<$merchant_name>转账";
|
|||
|
|
}
|
|||
|
|
$rows[] = [$order->id."-$index", $order->trade_no, $name, $type, $goods_name, $earning->price, 0, $poundage, number_format($earning->value * $poundage, 2, '.',''), $order->created_at, date('m', strtotime($start_date))];
|
|||
|
|
|
|||
|
|
$index++;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return $rows;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function saasContent($start_date, $end_date)
|
|||
|
|
{
|
|||
|
|
$rows = [
|
|||
|
|
["深圳福恋智能信息科技有限公司
|
|||
|
|
|
|||
|
|
SAAS收入明细表", '', '', '', '','','','','','',''],
|
|||
|
|
['ID',"订单号", "商户", "性质", "类型",'商品名称', '金额', '订单退款金额', '手续费率', '手续费', '订单时间', '结算时间', '下单月份']
|
|||
|
|
];
|
|||
|
|
$earnings = MEarning::with('order', 'order.refunds')->whereHas('order')->where('value', ">", 0)->whereBetween('created_at', [$start_date, $end_date])->get();
|
|||
|
|
$pre_id = null;
|
|||
|
|
foreach ($earnings as $earning)
|
|||
|
|
{
|
|||
|
|
$order = $earning->order;
|
|||
|
|
// if(empty($order)) dd($earning->m_order_id);
|
|||
|
|
$goods_name = $order->goods;
|
|||
|
|
if (empty($order->goods)) {
|
|||
|
|
switch ($order->type) {
|
|||
|
|
case "community":
|
|||
|
|
$goods_name = CommunityActivity::where('id', $order->type_id)->value('title');
|
|||
|
|
break;
|
|||
|
|
case "course":
|
|||
|
|
$goods_name = Course::where('id', $order->type_id)->value('title');
|
|||
|
|
break;
|
|||
|
|
case "consult":
|
|||
|
|
$goods_name = Consultation::where('id', $order->type_id)->value("title");
|
|||
|
|
break;
|
|||
|
|
case "evaluate":
|
|||
|
|
$goods_name = MerchantEvaluate::where("id", $order->type_id)->value("title");
|
|||
|
|
break;
|
|||
|
|
case "shop":
|
|||
|
|
$goods_name = MerchantShop::where("id", $order->type_id)->value("title");
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if ($order) {
|
|||
|
|
if ($earning->m_user_id) {
|
|||
|
|
$name = MerchantUser::where('id', $earning->m_user_id)->value('nickname')?:"";
|
|||
|
|
$name = "用户( $name )";
|
|||
|
|
$poundage = 0.02;
|
|||
|
|
}else{
|
|||
|
|
$name = Anchor::where('m_id', $earning->m_id)->value('name')?:"";
|
|||
|
|
$name = "商家( $name )";
|
|||
|
|
$poundage = $order->merchant?($order->merchant->poundage / 100) :0.02;
|
|||
|
|
}
|
|||
|
|
$type = "代付";
|
|||
|
|
$nature = "代收代付";
|
|||
|
|
if ($order->merchant_id == 491) {
|
|||
|
|
$nature = "福恋";
|
|||
|
|
}elseif ($order->merchant_id == 44) {
|
|||
|
|
$nature = "福恋(友福)";
|
|||
|
|
}
|
|||
|
|
$value = "-".$earning->value;
|
|||
|
|
if ($order->type == 'transfer') {
|
|||
|
|
$log = MerchantTransferLog::where('m_earning_id', $earning->id)->first();
|
|||
|
|
if (empty($log)) dd($earning->id);
|
|||
|
|
$merchant = MerchantAccount::where('id', $log->m_id)->first();
|
|||
|
|
$merchant_name = $merchant&&$merchant->anchorV2?$merchant->anchorV2->name:"未知";
|
|||
|
|
$type = "<$merchant_name>转账";
|
|||
|
|
$value = $earning->value;
|
|||
|
|
}else{
|
|||
|
|
if ($pre_id != $earning->m_order_id) {//新订单
|
|||
|
|
$index = 0;
|
|||
|
|
//提现费率
|
|||
|
|
//订单退款
|
|||
|
|
$refund_price = $order->refunds->sum('refund_fee')?:0;
|
|||
|
|
//订单代收
|
|||
|
|
$rows[] = [$order->id."-$index", $order->trade_no, "福恋平台", $nature, "代收", $goods_name, $order->price, $refund_price, 0.006, "-".number_format(($order->price - $refund_price) * 0.006, 2, '.',''), $order->created_at, $earning->created_at, date('m', strtotime($start_date))];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$index++;
|
|||
|
|
if ($order->merchant_id == 491) {
|
|||
|
|
$poundage_fee = 0;
|
|||
|
|
}else {
|
|||
|
|
$poundage_fee = number_format($earning->value * $poundage, 2, '.','');
|
|||
|
|
}
|
|||
|
|
$rows[] = [$order->id."-$index", $order->trade_no, $name, $nature, $type, $goods_name, $value, 0, $poundage, $poundage_fee, $order->created_at, $earning->created_at, date('m', strtotime($start_date))];
|
|||
|
|
|
|||
|
|
$pre_id = $earning->m_order_id;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return $rows;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function loveContent($start_date, $end_date)
|
|||
|
|
{
|
|||
|
|
$rows = [
|
|||
|
|
["深圳福恋智能信息科技有限公司
|
|||
|
|
|
|||
|
|
福恋产品收入明细表", '', '', '', '', '', '', '', '', '', ''],
|
|||
|
|
['ID', "订单号", "商户", "类型", '商品名称', '金额', '订单退款金额', '手续费率', '手续费', '订单时间', '月份',"支付方式"]
|
|||
|
|
];
|
|||
|
|
$orders = Order::whereBetween('created_at', [$this->start_date, $this->end_date])->where('pay_status', '<>', "UNPAID")->get();
|
|||
|
|
foreach ($orders as $order)
|
|||
|
|
{
|
|||
|
|
$pay_type = "微信支付";
|
|||
|
|
if ($order->pay_type == 'ios') {
|
|||
|
|
$pay_type = "苹果支付";
|
|||
|
|
}
|
|||
|
|
$merchant_name = "商家(福恋平台)";
|
|||
|
|
$refund_fee = $order->refundOrders()->sum("refund_fee");
|
|||
|
|
$service_fee = number_format(($order->price - $refund_fee) * 0.006, 2, '.', '');
|
|||
|
|
$earnings = $order->earning()->where('value', '>', 0)->get();
|
|||
|
|
$index = 1;
|
|||
|
|
if (count($earnings->toArray()) && $earnings->where('user_id', '<>', 0)->count()) {
|
|||
|
|
foreach ($earnings as $earning) {
|
|||
|
|
$charge = 0.006;
|
|||
|
|
$type = "营收";
|
|||
|
|
if ($earning->user_id) {
|
|||
|
|
$name = User::where('id', $earning->user_id)->value('nickname');
|
|||
|
|
$merchant_name = "用户($name)";
|
|||
|
|
$charge = 0.02;
|
|||
|
|
$service_fee = number_format($earning->value * $charge,2, '.','');
|
|||
|
|
$type = "服务费";
|
|||
|
|
}
|
|||
|
|
$rows[] = [$order->id."-$index", $order->trade_no, $merchant_name, $type, $order->goods, $earning->value, 0, $charge, $service_fee, $order->created_at, date('m', strtotime($this->start_date)), $pay_type];
|
|||
|
|
$index++;
|
|||
|
|
}
|
|||
|
|
}else {
|
|||
|
|
$rows[] = [$order->id.'-1', $order->trade_no, $merchant_name, "营收", $order->goods, $order->price, $refund_fee, '0.006', $service_fee, $order->created_at, date('m', strtotime($this->start_date)), $pay_type];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
return $rows;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @return \Illuminate\Support\Collection
|
|||
|
|
*/
|
|||
|
|
public function collection()
|
|||
|
|
{
|
|||
|
|
$start_date = $this->start_date;
|
|||
|
|
$end_date = $this->end_date;
|
|||
|
|
Log::info($start_date.'---'.$end_date);
|
|||
|
|
// $orders = Order::with('refundOrders')->where('pay_status','PAID')->whereBetween('created_at', [$start_date, $end_date])->get();
|
|||
|
|
if ($this->type) {
|
|||
|
|
$rows = $this->loveContent($start_date, $end_date);
|
|||
|
|
}else {
|
|||
|
|
$rows = $this->saasContent($start_date, $end_date);
|
|||
|
|
}
|
|||
|
|
return collect($rows);
|
|||
|
|
}
|
|||
|
|
public function columnFormats(): array
|
|||
|
|
{
|
|||
|
|
return [
|
|||
|
|
'G' => NumberFormat::FORMAT_NUMBER_00, //金额保留两位小数
|
|||
|
|
'H' => NumberFormat::FORMAT_NUMBER_00, //金额保留两位小数
|
|||
|
|
'I' => NumberFormat::FORMAT_PERCENTAGE_00, //金额保留两位小数
|
|||
|
|
'J' => NumberFormat::FORMAT_NUMBER_00, //金额保留两位小数
|
|||
|
|
'B' => NumberFormat::FORMAT_NUMBER, //订单号
|
|||
|
|
|
|||
|
|
];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function registerEvents(): array
|
|||
|
|
{
|
|||
|
|
return [
|
|||
|
|
AfterSheet::class => function(AfterSheet $event) {
|
|||
|
|
//设置列宽
|
|||
|
|
$arr = ['B','C','D','E','F',"G","H",'I','J','K', 'L'];
|
|||
|
|
foreach ($arr as $value) {
|
|||
|
|
$event->sheet->getDelegate()->getColumnDimension($value)->setWidth(20);
|
|||
|
|
}
|
|||
|
|
$event->sheet->getDelegate()->getColumnDimension("C")->setWidth(50);
|
|||
|
|
|
|||
|
|
//自动换行
|
|||
|
|
$event->sheet->getStyle("A1:L1")->getAlignment()->setWrapText(TRUE);
|
|||
|
|
$event->sheet->getDelegate()->getRowDimension(1)->setRowHeight(50);
|
|||
|
|
// //设置行高,$i为数据行数
|
|||
|
|
for ($i = 2; $i<=1265; $i++) {
|
|||
|
|
$event->sheet->getDelegate()->getRowDimension($i)->setRowHeight(30);
|
|||
|
|
}
|
|||
|
|
// //设置区域单元格垂直居中
|
|||
|
|
$event->sheet->getDelegate()->getStyle('A1:L1265')->getAlignment()->setVertical('center');
|
|||
|
|
// //设置区域单元格水平居中
|
|||
|
|
$event->sheet->getDelegate()->getStyle('A1:L1265')->getAlignment()->setHorizontal('center');
|
|||
|
|
//合并单元格
|
|||
|
|
$event->sheet->getDelegate()->mergeCells('A1:L1');
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @return string
|
|||
|
|
*/
|
|||
|
|
public function title(): string
|
|||
|
|
{
|
|||
|
|
return 'Saas收入明细';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|