InvoiceController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\controllers\v1;
  8. use app\modules\client\controllers\BaseController;
  9. use app\models\SaasUserInvoiceConf;
  10. use app\models\OrderInvoice;
  11. use app\models\Order;
  12. use app\modules\admin\models\InvoiceForm;
  13. class InvoiceController extends BaseController
  14. {
  15. public function actionSearchInvoiceNo() {
  16. $keyword = input_params('keyword');
  17. $ApiSearchInvoiceNo = InvoiceForm::ApiSearchInvoiceNo(get_store_id(), $keyword);
  18. return $this->asJson($ApiSearchInvoiceNo);
  19. }
  20. public function actionInvoiceConf() {
  21. $invoiceConf = [];
  22. $invoice_is_open = InvoiceForm::is_open(get_store_id(), $invoiceConf);
  23. return $this->asJson([
  24. 'code' => 0,
  25. 'data' => [
  26. 'invoice_is_open' => $invoice_is_open,
  27. 'invoiceConf' => $invoiceConf,
  28. ],
  29. ]);
  30. }
  31. public function actionSaasUserConfList() {
  32. $list = SaasUserInvoiceConf::find()->where(['saas_id' => get_saas_user_id(), 'is_delete' => 0])->orderBy('updated_at DESC')->all();
  33. return $this->asJson([
  34. 'code' => 0,
  35. 'data' => $list,
  36. ]);
  37. }
  38. public function actionSaasUserConfDel() {
  39. $ids = input_params('ids');
  40. $del = SaasUserInvoiceConf::updateAll(['is_delete' => 1], ['saas_id' => get_saas_user_id(), 'is_delete' => 0, 'id' => explode(',', $ids)]);
  41. return $this->asJson([
  42. 'code' => $del ? 0 : 1,
  43. 'msg' => '删除数据:' . $del . '条',
  44. ]);
  45. }
  46. public function actionSaasUserConfSave() {
  47. try{
  48. $saas_id = get_saas_user_id();
  49. $params = all_params();
  50. if($params['id']){
  51. $conf = SaasUserInvoiceConf::findOne(['id' => $params['id'], 'saas_id' => $saas_id, 'is_delete' => 0]);
  52. }else{
  53. $conf = new SaasUserInvoiceConf();
  54. $conf->saas_id = $saas_id;
  55. }
  56. $conf->title = $params['title'];
  57. $conf->type = $params['type'];
  58. $conf->code = $params['code'];
  59. $conf->email = $params['email'];
  60. $conf->desc_type = $params['desc_type'];
  61. $conf->ext = is_array($params['ext']) ? json_encode($params['ext']) : $params['ext'];
  62. if(!$conf->save()){
  63. throw new \Exception(array_shift($conf->getFirstErrors()));
  64. }
  65. return $this->asJson([
  66. 'code' => 0,
  67. 'msg' => '操作成功!'
  68. ]);
  69. } catch (Exception $e) {
  70. \Yii::error($e);
  71. return $this->asJson([
  72. 'code' => 1,
  73. 'msg' => $e->getMessage()
  74. ]);
  75. }
  76. }
  77. public function actionUserInvoiceSave() {
  78. try{
  79. $order_id = input_params('order_id');
  80. $invoiceConf = input_params('invoiceConf');
  81. if(empty($order_id) || empty($invoiceConf)){
  82. throw new \Exception('缺少参数');
  83. }
  84. $save = InvoiceForm::orderInvoiceSave($order_id, $invoiceConf);
  85. if($save['code'] != 0){
  86. return $save;
  87. }
  88. return $this->asJson([
  89. 'code' => 0,
  90. 'msg' => '操作成功!'
  91. ]);
  92. } catch (Exception $e) {
  93. \Yii::error($e);
  94. return $this->asJson([
  95. 'code' => 1,
  96. 'msg' => $e->getMessage()
  97. ]);
  98. }
  99. }
  100. public function actionUserInvoiceApply() {
  101. try{
  102. $id = input_params('id') ? explode(',', input_params('id')) : null;
  103. $order_id = input_params('order_id') ? explode(',', input_params('order_id')) : null;
  104. if(empty($id) && empty($order_id)){
  105. throw new \Exception('缺少参数');
  106. }
  107. $query = OrderInvoice::find()
  108. ->where(['user_id' => get_user_id(), 'is_delete' => 0, 'has_apply' => 0]);
  109. if($id){
  110. $query->andWhere(['id' => $id]);
  111. }
  112. if($order_id){
  113. $query->andWhere(['order_id' => $order_id]);
  114. }
  115. $list = $query->all();
  116. if(!$list){
  117. throw new \Exception('未找到数据');
  118. }
  119. foreach($list as $invoice){
  120. InvoiceForm::orderInvoiceApply($invoice);
  121. if(!$invoice->save()){
  122. throw new \Exception(array_shift($invoice->getFirstErrors()));
  123. }
  124. }
  125. return $this->asJson([
  126. 'code' => 0,
  127. 'msg' => '操作成功!'
  128. ]);
  129. } catch (Exception $e) {
  130. \Yii::error($e);
  131. return $this->asJson([
  132. 'code' => 1,
  133. 'msg' => $e->getMessage()
  134. ]);
  135. }
  136. }
  137. public function actionUserInvoiceList() {
  138. $id = input_params('id');
  139. $order_id = input_params('order_id');
  140. $has_apply = input_params('has_apply', 1);
  141. $query = OrderInvoice::find()->alias('oi')
  142. ->leftJoin(['o' => Order::tableName()], 'o.id = oi.order_id')
  143. ->where(['oi.user_id' => get_user_id(), 'oi.is_delete' => 0, 'oi.has_apply' => $has_apply])
  144. ->andWhere(['o.is_pay' => 1])
  145. ->andWhere(['!=', 'o.trade_status', 1]);
  146. if(!$has_apply){
  147. $invoiceConf = [];
  148. $invoice_is_open = InvoiceForm::is_open(get_store_id(), $invoiceConf);
  149. if(!$invoice_is_open){
  150. $query->andWhere(['oi.id' => null]);
  151. }
  152. $query->andWhere(['>', 'o.pay_time', time() - 86400 * $invoiceConf['conf']['timeout']]);
  153. }
  154. if($id){
  155. $query->andWhere(['oi.id' => $id]);
  156. }
  157. if($order_id){
  158. $query->andWhere(['oi.order_id' => $order_id]);
  159. }
  160. $query->orderBy('oi.id DESC');
  161. $query->select(['oi.*', 'o.order_no', 'o.trade_status']);
  162. $list = pagination_make($query);
  163. return $this->asJson([
  164. 'q' => $query->createCommand()->getRawSql(),
  165. 'code' => 0,
  166. 'data' => $list,
  167. ]);
  168. }
  169. }