// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\model\user; use app\api\model\Order; use app\api\service\User as UserService; use app\common\model\user\InvoiceOrder as InvoiceOrderModel; use app\common\model\user\UserInvoice; /** * 用户余额变动明细模型 * Class BalanceLog * @package app\api\model\user */ class InvoiceOrder extends InvoiceOrderModel { /** * 隐藏字段 * @var array */ protected $hidden = [ 'store_id', ]; public function getList($param = []) { // 当前用户ID $userId = UserService::getCurrentLoginUserId(); // 获取列表数据 $query = $this->where('user_id', '=', $userId); $list = $query->order(['created_at' => 'desc']) ->select(); return $list; } public function getDetail($id) { $info = $this->with(['order.goods'])->where(['id' => $id])->find()->toArray(); $info['invoice'] = UserInvoice::where(['id' => $info['invoice_id']])->find()->toArray(); return $info; } public function invoicingAdd($data) { // 当前用户ID $userId = UserService::getCurrentLoginUserId(); $store_id = app()->request->storeId(); if (empty($data['invoice_id']) || empty($data['order_id'])) { $this->error = '请补全信息'; return false; } //校验订单信息 $order = Order::where(['order_id' => $data['order_id'], 'user_id' => $userId])->find(); if ($order->order_status != 30 || $order->pay_price <= 0) { $this->error = '订单状态异常,无法开票'; return false; } //校验是否已经打印过该订单 $has = $this->where(['order_id' => $data['order_id']])->find(); if ($has) { $this->error = '请勿重复申请'; return false; } $invoice = UserInvoice::where(['id' => $data['invoice_id']])->find()->toArray(); $data['type'] = $data['type'] ?? $invoice['type']; $data['source'] = $data['source'] ?? $invoice['source']; $data['header'] = $data['header'] ?? $invoice['header']; $data['order_no'] = $order->order_no; return $this->save(array_merge($data, [ 'user_id' => $userId, 'store_id' => $store_id, 'created_at' => time(), ])); } public function invoicingEdit($data) { if (empty($data['header']) || empty($data['id'])) { $this->error = '请补全信息'; return false; } $userId = UserService::getCurrentLoginUserId(); $detail = self::get(['user_id' => $userId, 'id' => $data['id']]); if (empty($detail)) { $this->error = '未找到该抬头'; return false; } if ($detail->status == 1) { $this->error = '已开票,无法修改'; return false; } return $detail->save([ 'type' => $data['type'], 'source' => $data['source'], 'header' => $data['header'], ]) !== false; } }