multi-store/app/common/lists/StoreBillLists.php
2024-06-08 15:07:46 +08:00

113 lines
4.8 KiB
PHP

<?php
namespace app\common\lists;
use app\admin\lists\BaseAdminDataLists;
use app\common\enum\OrderEnum;
use app\common\enum\PayEnum;
use app\common\model\store_finance_flow\StoreFinanceFlow;
use think\db\Query;
class StoreBillLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExcelInterface
{
public function setSearch(): array
{
return [
'between_time' => 'create_time'
];
}
public function lists(): array
{
$this->params['type'] = !empty($this->params['type']) ? $this->params['type'] : 1;
return StoreFinanceFlow::with(['user', 'staff'])
->where($this->searchWhere)
->when(!empty($this->request->adminInfo['store_id']), function ($query) {
$query->where('store_id', $this->request->adminInfo['store_id']);
})
->when(!empty($this->params['type']), function (Query $query) {
$income = 1;
$expense = 0;
if (!empty($this->request->adminInfo['store_id'])) {
$income = 0;
$expense = 1;
}
switch ($this->params['type']) {
case 2:
$query->fieldRaw("*,sum(IF(financial_pm=$income,number,0)) as income,sum(IF(financial_pm=$expense,number,0)) as expense,YEAR(FROM_UNIXTIME(create_time)) as year,WEEK(FROM_UNIXTIME(create_time), 1) as week")->group("year,week");
break;
case 3:
$query->fieldRaw("*,sum(IF(financial_pm=$income,number,0)) as income,sum(IF(financial_pm=$expense,number,0)) as expense,FROM_UNIXTIME(create_time, '%Y-%m') as time")->group("time");
break;
default:
$query->fieldRaw("*,sum(IF(financial_pm=$income,number,0)) as income,sum(IF(financial_pm=$expense,number,0)) as expense,FROM_UNIXTIME(create_time, '%Y-%m-%d') as time")->group("time");
break;
}
})
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()->each(function ($item) {
$item->month = date('m', strtotime($item->create_time));
if (!empty($this->request->adminInfo['store_id'])) {
$item->pm_type = $item->financial_pm == 0 ? '收入' : '支出';
} else {
$item->pm_type = $item->financial_pm == 0 ? '支出' : '收入';
}
$item['pay_type_name'] = PayEnum::getPaySceneDesc($item['pay_type']);
$item['financial_type_name'] = OrderEnum::getFinancialType($item['financial_type']);
})
->toArray();
}
public function count(): int
{
return StoreFinanceFlow::where('1=1')
->where($this->searchWhere)
->when(!empty($this->request->adminInfo['store_id']), function ($query) {
$query->where('store_id', $this->request->adminInfo['store_id']);
})
->when(!empty($this->params['type']), function (Query $query) {
$income = 1;
$expense = 0;
if (!empty($this->request->adminInfo['store_id'])) {
$income = 0;
$expense = 1;
}
switch ($this->params['type']) {
case 2:
$query->fieldRaw("*,sum(IF(financial_pm=$income,number,0)) as income,sum(IF(financial_pm=$expense,number,0)) as expense,YEAR(FROM_UNIXTIME(create_time)) as year,WEEK(FROM_UNIXTIME(create_time), 1) as week")->group("year,week");
break;
case 3:
$query->fieldRaw("*,sum(IF(financial_pm=$income,number,0)) as income,sum(IF(financial_pm=$expense,number,0)) as expense,FROM_UNIXTIME(create_time, '%Y-%m') as time")->group("time");
break;
default:
$query->fieldRaw("*,sum(IF(financial_pm=$income,number,0)) as income,sum(IF(financial_pm=$expense,number,0)) as expense,FROM_UNIXTIME(create_time, '%Y-%m-%d') as time")->group("time");
break;
}
})
->count();
}
public function setExcelFields(): array
{
return [
'financial_record_sn' => '交易单号',
'order_sn' => '关联订单',
'create_time' => '交易时间',
'number' => '交易金额',
'pm_type' => '支出收入',
'nickname' => '交易人',
'staff_name' => '关联店员',
'financial_type_name' => '交易类型',
'pay_type_name' => '支付方式',
];
}
public function setFileName(): string
{
return '账单导出';
}
}