72 lines
3.2 KiB
PHP
72 lines
3.2 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | 后台 · 企油通发票管理(7.14)
|
||
// +----------------------------------------------------------------------
|
||
// | list() 发票记录(按 企业 + 通道 + 月份 + 司机手机号 维度)
|
||
// | sync() 同步:指定企业则同步该企业(可带司机手机号);不指定则批量同步所有启用企业的当月全量
|
||
// +----------------------------------------------------------------------
|
||
declare (strict_types=1);
|
||
|
||
namespace app\store\controller\qyt;
|
||
|
||
use app\common\model\qyt\Enterprise as EnterpriseModel;
|
||
use app\common\service\QytInvoice;
|
||
use app\store\controller\Controller;
|
||
use think\facade\Db;
|
||
use think\response\Json;
|
||
|
||
class Invoice extends Controller
|
||
{
|
||
public function list(): Json
|
||
{
|
||
$param = $this->request->param();
|
||
$query = Db::name('qyt_invoice');
|
||
!empty($param['qyt_enterprise_id']) && $query->where('qyt_enterprise_id', (int) $param['qyt_enterprise_id']);
|
||
!empty($param['month']) && $query->where('month', (string) $param['month']);
|
||
!empty($param['mobile']) && $query->where('mobile', trim((string) $param['mobile']));
|
||
if (isset($param['has_invoice']) && $param['has_invoice'] !== '' && (int) $param['has_invoice'] >= 0) {
|
||
(int) $param['has_invoice'] === 1 ? $query->where('invoice_count', '>', 0) : $query->where('invoice_count', 0);
|
||
}
|
||
|
||
$list = $query->order(['month' => 'desc', 'invoice_id' => 'desc'])->paginate(15)->toArray();
|
||
|
||
$names = EnterpriseModel::column('company_name', 'enterprise_id');
|
||
foreach ($list['data'] as &$row) {
|
||
$row = QytInvoice::format($row);
|
||
$row['company_name'] = $names[$row['qyt_enterprise_id']] ?? '(企业已删除)';
|
||
}
|
||
return $this->renderSuccess(compact('list'));
|
||
}
|
||
|
||
public function sync(): Json
|
||
{
|
||
$month = trim((string) $this->request->param('month'));
|
||
$mobile = trim((string) $this->request->param('mobile', ''));
|
||
$enterpriseId = (int) $this->request->param('enterpriseId', 0);
|
||
|
||
if ($enterpriseId > 0) {
|
||
$enterprise = EnterpriseModel::detail($enterpriseId);
|
||
if (!$enterprise) {
|
||
return $this->renderError('企业不存在');
|
||
}
|
||
$res = QytInvoice::sync($enterprise, $month, $mobile);
|
||
return $res['ok'] ? $this->renderSuccess($res['row'], $res['message']) : $this->renderError($res['message']);
|
||
}
|
||
|
||
// 批量:所有启用企业的该月全量
|
||
$ok = $fail = 0;
|
||
$errors = [];
|
||
foreach (EnterpriseModel::where('is_enable', 1)->select() as $enterprise) {
|
||
$res = QytInvoice::sync($enterprise, $month, '');
|
||
if ($res['ok']) {
|
||
$ok++;
|
||
} else {
|
||
$fail++;
|
||
$errors[] = $enterprise['company_name'] . ':' . $res['message'];
|
||
}
|
||
}
|
||
$message = "批量同步完成:成功 {$ok} 家,失败 {$fail} 家" . ($errors ? '(' . implode(';', array_slice($errors, 0, 3)) . ')' : '');
|
||
return $fail && !$ok ? $this->renderError($message) : $this->renderSuccess([], $message);
|
||
}
|
||
}
|