SaasUserInvoiceConf.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. use yii\db\ActiveRecord;
  10. use yii\behaviors\TimestampBehavior;
  11. /**
  12. * This is the model class for table "{{%saas_user_invoice_conf}}".
  13. *
  14. */
  15. class SaasUserInvoiceConf extends \yii\db\ActiveRecord
  16. {
  17. const TYPE_DEFAULT = 1;//普通发票
  18. const TYPE_SPECIAL = 2;//专用发票
  19. const DESC_TYPE_GOODS = 1;//商品明细
  20. const DESC_TYPE_CAT = 2;//商品类别
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%saas_user_invoice_conf}}';
  27. }
  28. public function behaviors()
  29. {
  30. return [
  31. [
  32. 'class' => TimestampBehavior::class,
  33. ]
  34. ];
  35. }
  36. public static function saveOrderInvoiceConf($orderInvoice)
  37. {
  38. try{
  39. $user = User::findOne($orderInvoice->user_id);
  40. $saasUser = SaasUser::findOne(['mobile' => $user['binding'], 'is_delete' => 0]);
  41. if(!$saasUser){
  42. \Yii::error([__METHOD__, $orderInvoice->user_id, $user['binding']]);
  43. return;
  44. }
  45. $invoice_conf = json_decode($orderInvoice->invoice_conf, true);
  46. $conf = SaasUserInvoiceConf::findOne(['title' => $invoice_conf['title'], 'saas_id' => $saasUser['id'], 'is_delete' => 0]);
  47. if(!$conf){
  48. $conf = new SaasUserInvoiceConf();
  49. $conf->saas_id = $saasUser['id'];
  50. $conf->title = $invoice_conf['title'];
  51. }
  52. $conf->type = $invoice_conf['type'];
  53. $conf->code = $invoice_conf['code'];
  54. $conf->email = $invoice_conf['email'];
  55. $conf->desc_type = $invoice_conf['desc_type'];
  56. $conf->ext = is_array($invoice_conf['ext']) ? json_encode($invoice_conf['ext']) : $invoice_conf['ext'];
  57. if(!$conf->save()){
  58. throw new \Exception(array_shift($conf->getFirstErrors()));
  59. }
  60. return [
  61. 'code' => 0,
  62. 'msg' => '操作成功!'
  63. ];
  64. } catch (Exception $e) {
  65. \Yii::error($e);
  66. return [
  67. 'code' => 1,
  68. 'msg' => '开发票出错,' . $e->getMessage()
  69. ];
  70. }
  71. }
  72. }