diff --git a/app/api/controller/User.php b/app/api/controller/User.php index a0d614a5..96d9b263 100644 --- a/app/api/controller/User.php +++ b/app/api/controller/User.php @@ -182,4 +182,32 @@ class User extends Controller return $this->renderSuccess(compact('list')); } + //开票记录 + public function invoicingDetail(): Json + { + $service = new InvoiceOrder(); + $data = $service->getDetail($this->request->param('id')); + return $this->renderSuccess(compact('data')); + } + + //订单开票 + public function invoicingAdd(): Json + { + $service = new InvoiceOrder(); + if (!$service->invoicingAdd($this->request->param())) { + return $this->renderSuccess($service->getError() ?: '操作失败'); + } + return $this->renderSuccess('提交成功,请耐心等待'); + } + + //订单开票 + public function invoicingEdit(): Json + { + $service = new InvoiceOrder(); + if (!$service->invoicingEdit($this->request->param())) { + return $this->renderSuccess($service->getError() ?: '操作失败'); + } + return $this->renderSuccess('提交成功,请耐心等待'); + } + } diff --git a/app/api/model/user/InvoiceOrder.php b/app/api/model/user/InvoiceOrder.php index bd264829..4918d35d 100644 --- a/app/api/model/user/InvoiceOrder.php +++ b/app/api/model/user/InvoiceOrder.php @@ -12,8 +12,10 @@ 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; /** * 用户余额变动明细模型 @@ -41,15 +43,40 @@ class InvoiceOrder extends InvoiceOrderModel return $list; } - public function add($data) + 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['header']) || empty($data['duty_no'])) { + 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, @@ -57,25 +84,26 @@ class InvoiceOrder extends InvoiceOrderModel ])); } - public function edit($data) + public function invoicingEdit($data) { - if (empty($data['header']) || empty($data['duty_no'])) { + if (empty($data['header']) || empty($data['id'])) { $this->error = '请补全信息'; return false; } $userId = UserService::getCurrentLoginUserId(); $detail = self::get(['user_id' => $userId, 'id' => $data['id']]); - empty($detail) && throwError('未找到该抬头'); - + 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'], - 'duty_no' => $data['duty_no'], - 'bank_name' => $data['bank_name'] ?? '', - 'bank_no' => $data['bank_no'] ?? '', - 'address' => $data['address'] ?? '', - 'phone' => $data['phone'] ?? '', ]) !== false; } diff --git a/app/common/model/user/InvoiceOrder.php b/app/common/model/user/InvoiceOrder.php index 6e90dfb1..038cd153 100644 --- a/app/common/model/user/InvoiceOrder.php +++ b/app/common/model/user/InvoiceOrder.php @@ -14,7 +14,7 @@ namespace app\common\model\user; use cores\BaseModel; use think\model\relation\BelongsTo; -use app\common\enum\user\balanceLog\Scene as SceneEnum; +use think\model\relation\HasOne; /** * 用户余额变动明细模型 @@ -41,4 +41,25 @@ class InvoiceOrder extends BaseModel return $this->belongsTo("app\\{$module}\\model\\User"); } + /** + * 关联抬头 + * @return HasOne + */ + public function invoice(): HasOne + { + + $module = self::getCalledModule(); + return $this->hasOne("app\\{$module}\\model\\user\\UserInvoice", 'id', 'invoice_id'); + } + + /** + * 关联订单 + * @return HasOne + */ + public function order(): HasOne + { + $module = self::getCalledModule(); + return $this->hasOne("app\\{$module}\\model\\Order", 'order_id', 'order_id'); + } + }