OrderInvoiceController.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace addons\TinyShop\api\modules\v1\controllers\member;
  3. use Yii;
  4. use common\helpers\ResultHelper;
  5. use common\enums\StatusEnum;
  6. use yii\data\ActiveDataProvider;
  7. use api\controllers\UserAuthController;
  8. use addons\TinyShop\common\models\order\Invoice;
  9. use addons\TinyShop\api\modules\v1\forms\InvoiceForm;
  10. use addons\TinyShop\common\models\order\Order;
  11. /**
  12. * 订单发票
  13. *
  14. * Class OrderInvoiceController
  15. * @package addons\TinyShop\api\modules\v1\controllers\member
  16. * @author jianyan74 <751393839@qq.com>
  17. */
  18. class OrderInvoiceController extends UserAuthController
  19. {
  20. /**
  21. * @var Invoice
  22. */
  23. public $modelClass = Invoice::class;
  24. /**
  25. * @return ActiveDataProvider
  26. */
  27. public function actionIndex()
  28. {
  29. return new ActiveDataProvider([
  30. 'query' => $this->modelClass::find()
  31. ->where(['status' => StatusEnum::ENABLED, 'member_id' => Yii::$app->user->identity->member_id])
  32. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  33. ->orderBy('id desc')
  34. ->with(['order'])
  35. ->asArray(),
  36. 'pagination' => [
  37. 'pageSize' => $this->pageSize,
  38. 'validatePage' => false,// 超出分页不返回data
  39. ],
  40. ]);
  41. }
  42. /**
  43. * @return Invoice|array|mixed|\yii\db\ActiveRecord
  44. */
  45. public function actionCreate()
  46. {
  47. $config = Yii::$app->tinyShopService->config->setting();
  48. if (empty($config['order_invoice_status'])) {
  49. return ResultHelper::json(422, '发票申请已关闭,请联系管理员');
  50. }
  51. /* @var $model InvoiceForm */
  52. $model = new InvoiceForm();
  53. $model->attributes = Yii::$app->request->post();
  54. $model->member_id = Yii::$app->user->identity->member_id;
  55. if (!$model->validate()) {
  56. return ResultHelper::json(422, $this->getError($model));
  57. }
  58. if (Yii::$app->tinyShopService->orderInvoice->findByOrderId($model->order_id)) {
  59. return ResultHelper::json(422, '已申请发票,不可重新补发票');
  60. }
  61. /** @var Order $order */
  62. $order = Yii::$app->tinyShopService->order->findById($model->order_id);
  63. // 创建发票记录
  64. $orderInvoice = Yii::$app->tinyShopService->orderInvoice->create($order, $model->invoice, $model->invoice_content);
  65. // 关联发票编号
  66. Order::updateAll(['invoice_id' => $model->invoice_id], ['id' => $model->order_id]);
  67. return $orderInvoice;
  68. }
  69. /**
  70. * 权限验证
  71. *
  72. * @param string $action 当前的方法
  73. * @param null $model 当前的模型类
  74. * @param array $params $_GET变量
  75. * @throws \yii\web\BadRequestHttpException
  76. */
  77. public function checkAccess($action, $model = null, $params = [])
  78. {
  79. // 方法名称
  80. if (in_array($action, ['delete', 'update'])) {
  81. throw new \yii\web\BadRequestHttpException('权限不足');
  82. }
  83. }
  84. }
粤ICP备19079148号