invoiceModel = new \app\wap\model\wap\Invoice(); $this->invoiceRecordsModel = new InvoiceRecords(); } /** * 白名单 * */ public static function WhiteList() { return [ ]; } /** * 开票记录 * @param Request $request * @return null */ public function records(Request $request) { $page = $request->get('page', 1); // 当前页码 $limit = $request->get('limit', 20); // 每页显示的记录数 $invoiceList = $this->invoiceRecordsModel->order('id', 'desc')->paginate($limit, false, ['page' => $page]); $totalCount = $invoiceList->total(); $data = [ 'page' => $page, 'total' => $totalCount, 'list' => $invoiceList->items(), ]; return JsonService::successful($data); } /** * 开发票 * @param Request $request * @return null */ public function make(Request $request) { $postData = $request->post(); $rule = [ 'order_number' => 'require', 'invoice_amount' => 'require|float', 'header_type' => 'require|in:1,2', 'invoice_header' => 'require', 'tax_number' => 'require', 'address' => 'require', 'bank' => 'require' ]; $validate = new Validate($rule); $validate->rule([ 'tax_number' => 'requireIf:header_type,2', 'address' => 'requireIf:header_type,2', 'bank' => 'requireIf:header_type,2' ]); if (!$validate->check($postData)) { JsonService::fail($validate->getError()); } $postData['uid'] = $this->uid; $invoice = $this->invoiceRecordsModel->create($postData); return JsonService::successful($invoice); } /** * 抬头列表 * @param Request $request * @return null */ public function list(Request $request) { $page = $request->get('page', 1); // 当前页码 $limit = $request->get('limit', 20); // 每页显示的记录数 $invoiceList = $this->invoiceModel->order('id', 'desc')->paginate($limit, false, ['page' => $page]); $totalCount = $invoiceList->total(); $data = [ 'page' => $page, 'total' => $totalCount, 'list' => $invoiceList->items(), ]; return JsonService::successful($data); } /** * 添加抬头 * @param Request $request * @return null */ public function add(Request $request) { $postData = $request->post(); if (isset($postData['id'])) { unset($postData['id']); } $this->_validate($postData); $postData['uid'] = $this->uid; $invoice = $this->invoiceModel->create($postData); return JsonService::successful($invoice); } /** * 修改抬头 * @param Request $request * @return null */ public function edit(Request $request) { $postData = $request->post(); $this->_validate($postData, true); $invoice = $this->invoiceModel->find($postData['id']); if (!$invoice) { return JsonService::fail('没有发票信息'); } $postData['uid'] = $this->uid; $invoice->save($postData); return JsonService::successful($invoice); } /** * 删除抬头 * @param Request $request * @return null */ public function delete(Request $request) { $postData = $request->post(); $invoice = $this->invoiceModel->find($postData['id']); if (!$invoice) { return JsonService::fail('没有发票信息'); } $invoice->delete(); return JsonService::successful(); } private function _validate($postData, $edit = false) { $rule = [ 'header_type' => 'require|in:1,2', 'invoice_header' => 'require', 'tax_number' => 'require', 'address' => 'require', 'bank' => 'require' ]; if ($edit) { $rule['id'] = 'require'; } $validate = new Validate($rule); $validate->rule([ 'tax_number' => 'requireIf:header_type,2', 'address' => 'requireIf:header_type,2', 'bank' => 'requireIf:header_type,2' ]); if (!$validate->check($postData)) { JsonService::fail($validate->getError()); } } }