model = new \app\admin\model\InvoiceTitle; $this->view->assign('titleTypeList', $this->model->getTitleTypeList()); $this->view->assign('invoiceTypeList', $this->model->getDefaultInvoiceTypeList()); } /** * 抬头列表(关联所属用户/签约商户及累计开票统计) */ public function index() { $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $list = $this->model ->with(['user', 'merchant']) ->where($where) ->order($sort, $order) ->paginate($limit); $rows = $list->items(); $ids = []; foreach ($rows as $r) { $ids[] = (int)$r['id']; } $invStats = []; if ($ids) { $agg = Db::name('invoice') ->where('title_id', 'in', $ids) ->where('status', 'issued') ->field('title_id, COUNT(*) AS cnt, SUM(amount) AS sum_amt') ->group('title_id') ->select(); foreach ($agg as $g) { $invStats[(int)$g['title_id']] = [ 'cnt' => (int)$g['cnt'], 'amt' => round((float)$g['sum_amt'], 2), ]; } } foreach ($rows as &$item) { $tid = (int)$item['id']; $item['issued_count'] = $invStats[$tid]['cnt'] ?? 0; $item['issued_amount'] = $invStats[$tid]['amt'] ?? 0.00; } unset($item); return json(['total' => $list->total(), 'rows' => $rows]); } /** * 发票抬头档案详情(关联历史开票记录) */ public function detail($ids = null) { $row = $this->model->with(['user', 'merchant'])->where('invoice_title.id', (int)$ids)->find(); if (!$row) { $this->error(__('No Results were found')); } $invoices = Db::name('invoice') ->where('title_id', (int)$row['id']) ->order('id desc') ->limit(20) ->select(); $this->view->assign([ 'row' => $row, 'invoices' => $invoices, ]); return $this->view->fetch(); } /** * 财务协助订正抬头信息(如税号、开户行、邮箱笔误) */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } if (false === $this->request->isPost()) { $this->view->assign('row', $row); return $this->view->fetch(); } $params = $this->request->post('row/a', []); $titleType = in_array($params['title_type'] ?? '', ['enterprise', 'personal'], true) ? $params['title_type'] : $row['title_type']; $title = trim((string)($params['title'] ?? '')); $taxNo = strtoupper(trim((string)($params['tax_no'] ?? ''))); if ($title === '') { $this->error('发票抬头名称不能为空'); } if ($titleType === 'enterprise' && $taxNo === '') { $this->error('企业单位抬头必须填写纳税人识别号'); } $row->save([ 'title_type' => $titleType, 'default_invoice_type' => in_array($params['default_invoice_type'] ?? '', ['normal', 'special'], true) ? $params['default_invoice_type'] : $row['default_invoice_type'], 'title' => mb_substr($title, 0, 150), 'tax_no' => mb_substr($taxNo, 0, 50), 'bank_name' => mb_substr(trim((string)($params['bank_name'] ?? '')), 0, 100), 'bank_account' => mb_substr(trim((string)($params['bank_account'] ?? '')), 0, 50), 'reg_address' => mb_substr(trim((string)($params['reg_address'] ?? '')), 0, 255), 'reg_mobile' => mb_substr(trim((string)($params['reg_mobile'] ?? '')), 0, 30), 'email' => mb_substr(trim((string)($params['email'] ?? '')), 0, 100), 'receive_mobile' => mb_substr(trim((string)($params['receive_mobile'] ?? '')), 0, 20), ]); $this->success(); } /** * 抬头由用户或企业入驻维护,后台不开放手工盲增 */ public function add() { $this->error('发票抬头由业主或签约企业在小程序端维护,后台无需手工新增'); } /** * 已关联开票申请的抬头禁止删除 */ public function del($ids = null) { if (false === $this->request->isPost()) { $this->error(__('Invalid parameters')); } $ids = $ids ?: $this->request->post('ids'); $idArr = array_values(array_filter(array_map('intval', explode(',', (string)$ids)))); if (!$idArr) { $this->error(__('Parameter %s can not be empty', 'ids')); } $used = (int)Db::name('invoice')->where('title_id', 'in', $idArr)->count(); if ($used > 0) { $this->error('所选发票抬头已关联 ' . $used . ' 笔开票记录,禁止删除'); } return parent::del($ids); } }