InvoiceForm.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use yii\base\Model;
  9. use app\models\Option;
  10. use app\constants\OptionSetting;
  11. use app\models\Order;
  12. use app\models\OrderInvoice;
  13. class InvoiceForm extends Model
  14. {
  15. public $store_id;
  16. public $id;
  17. public $ids;
  18. public $name;
  19. public $invoice_type;
  20. public $order_no;
  21. public $invoice_pics;
  22. public $is_cover;
  23. public $is_delete;
  24. public $start_time;
  25. public $end_time;
  26. public $time_finish_start_time;
  27. public $time_finish_end_time;
  28. public $status;
  29. public $status_ext;
  30. public $reason;
  31. public $has_apply;
  32. /**
  33. * @return array the validation rules.
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['reason', 'name'], 'trim'],
  39. [['order_no', 'name'], 'string'],
  40. [['store_id', 'id', 'invoice_type', 'is_cover', 'is_delete', 'status', 'status_ext', 'has_apply'], 'integer'],
  41. [['ids', 'invoice_pics', 'start_time', 'end_time', 'time_finish_start_time', 'time_finish_end_time', 'reason'], 'safe'],
  42. ];
  43. }
  44. public function init() {
  45. parent::init();
  46. if(empty($this->store_id)){
  47. $this->store_id = get_store_id();
  48. }
  49. }
  50. public function conf(){
  51. $conf = Option::get(OptionSetting::INVOICE_CONF, $this->store_id, 'store')['value'];
  52. if($conf){
  53. $conf = json_decode($conf, true);
  54. }else{
  55. $conf = ['conf' => [
  56. 'rule' => '发票须知',
  57. 'is_open' => 0,
  58. 'types' => [],
  59. 'timeout' => '0',
  60. 'tax' => 0,
  61. 'tax_rate' => 0,
  62. 'appcode' => ''
  63. ]];
  64. }
  65. $conf['conf']['appcode_desc'] = '购买接口地址:https://market.aliyun.com/products/56928005/cmapi00060851.html;从已购买服务里查询appcode:https://market.console.aliyun.com/#/bizlist';
  66. return [
  67. 'code'=>0,
  68. 'msg'=>'ok',
  69. 'data' => $conf,
  70. ];
  71. }
  72. public function confSave($conf){
  73. if(!is_array($conf)){
  74. $conf = json_decode($conf, true);
  75. }
  76. if(empty($conf['types'])){
  77. return [
  78. 'code'=>1,
  79. 'msg'=>'请选择发票类型'
  80. ];
  81. }
  82. $data = ['conf' => $conf];
  83. Option::set(OptionSetting::INVOICE_CONF, json_encode($data), $this->store_id, 'store');
  84. return [
  85. 'code'=>0,
  86. 'msg'=>'保存成功'
  87. ];
  88. }
  89. public static function is_open($store_id, &$invoiceConf = []) {
  90. $form = new self(['store_id' => $store_id]);
  91. $conf = $form->conf();
  92. $invoiceConf = $conf['data'];
  93. return (int)$invoiceConf['conf']['is_open'];
  94. }
  95. public static function ApiSearchInvoiceNo($store_id, $keyword) {
  96. $invoiceConf = [];
  97. $is_open = self::is_open($store_id, $invoiceConf);
  98. if(!$invoiceConf['conf']['appcode']){
  99. return [
  100. 'code' => 1,
  101. 'msg' => '店铺后台查询参数未配置',
  102. ];
  103. }
  104. $appcode = $invoiceConf['conf']['appcode'];
  105. $url = 'https://ein.market.alicloudapi.com/enterpriseIdNumberQuery';
  106. $querys = 'com=' . urlencode($keyword);
  107. $res = market_alicloudapi_com($appcode, $url, $querys, 'POST');
  108. // $res = '{"error_code":50002,"reason":"查询无结果","result":[],"ordersign":"20240130592970689200402432"}';
  109. // $res = '{"error_code": 0,"reason": "查询成功","result": {"creditCode": "91XXXXXXXXXXXXXXXXX","address": "洛阳市XXXXXXXXXXXXXXXXXXXXXXXXX园区FS309","tel": "","regNumber": "410XXXXXXXXXX","taxNumber": "91XXXXXXXXXXXXXXXXX","code": "91XXXXXXXXXXXXXXXXX"},"ordersign": "20240130592972684552441856"}';
  110. $res = json_decode($res, true);
  111. $res['code'] = $res['error_code'];
  112. $res['msg'] = $res['reason'];
  113. $res['result'] = $res['result'] ? : null;
  114. if($res['code'] != 0 || empty($res['result'])){
  115. return $res;
  116. }
  117. $item = &$res['result'];
  118. $item['title'] = $item['companyName'];
  119. $item['code'] = $item['taxNumber'];
  120. $item['address'] = $item['address'];
  121. $item['mobile'] = $item['tel'];
  122. return $res;
  123. }
  124. public static function tax_price($store_id, $price) {
  125. $form = new self(['store_id' => $store_id]);
  126. $conf = $form->conf();
  127. if($conf['data']['conf']['is_open'] && $conf['data']['conf']['tax']){
  128. return sprintf('%.2f', $price * $conf['data']['conf']['tax_rate'] / 100);
  129. }
  130. return 0;
  131. }
  132. public function orderInvoiceList ()
  133. {
  134. try {
  135. $query = OrderInvoice::find()->alias('oi')
  136. ->leftJoin(['o' => Order::tableName()], 'o.id = oi.order_id')
  137. ->where(['oi.has_apply' => 1, 'oi.is_delete' => 0, 'oi.store_id' => $this->store_id])
  138. ->andWhere(['o.is_pay' => 1])
  139. ->andWhere(['!=', 'o.trade_status', 1]);
  140. if (isset($this->status_ext) && $this->status_ext > -1) {
  141. $query->andWhere(['oi.status_ext' => $this->status_ext]);
  142. }
  143. if (isset($this->invoice_type) && $this->invoice_type > -1) {
  144. $query->andWhere(['oi.invoice_type' => $this->invoice_type]);
  145. }
  146. if (isset($this->is_cover) && $this->is_cover > -1) {
  147. $query->andWhere(['is_cover' => $this->is_cover]);
  148. }
  149. if (!empty($this->order_no)) {
  150. $query->andWhere(['oi.order_id' => Order::find()->select('id')->where(['order_no' => $this->order_no])]);
  151. }
  152. if (!empty($this->start_time)) {
  153. $query->andWhere(['>' , 'oi.created_at', strtotime($this->start_time)]);
  154. }
  155. if (!empty($this->end_time)) {
  156. $query->andWhere(['<' , 'oi.created_at', strtotime($this->end_time)]);
  157. }
  158. if (!empty($this->time_finish_start_time)) {
  159. $query->andWhere(['>' , 'oi.time_finish', strtotime($this->time_finish_start_time)]);
  160. }
  161. if (!empty($this->time_finish_end_time)) {
  162. $query->andWhere(['<' , 'oi.time_finish', strtotime($this->time_finish_end_time)]);
  163. }
  164. $query->orderBy('oi.id DESC');
  165. $query->select(['oi.*', 'o.order_no', 'o.order_type', 'o.is_pay', 'o.trade_status', 'o.name', 'o.mobile', 'o.address']);
  166. $pagination = pagination_make($query);
  167. // foreach($pagination['list'] as &$item){
  168. // $item['invoice_conf_arr'] = json_decode($item['invoice_conf'], true);
  169. // $item['invoice_conf_arr']['ext_arr'] = json_decode($item['invoice_conf_arr']['ext'], true);
  170. // }
  171. return [
  172. 'q' => $query->createCommand()->getRawSql(),
  173. 'code' => 0,
  174. 'msg' => 'success',
  175. 'data' => $pagination,
  176. ];
  177. } catch (\Exception $e) {
  178. \Yii::error($e);
  179. return [
  180. 'code' => 1,
  181. 'msg' => $e->getMessage()
  182. ];
  183. }
  184. }
  185. public static function orderInvoiceApply (&$invoice)
  186. {
  187. $invoice->has_apply = 1;
  188. $conf = json_decode($invoice->invoice_conf, true);
  189. $conf['has_apply'] = 1;
  190. $invoice->invoice_conf = json_encode($conf);
  191. }
  192. public function orderInvoiceStatus ()
  193. {
  194. try {
  195. if ($this->ids) {
  196. $ids = explode(',', $this->ids);
  197. //拒绝
  198. if (in_array($this->status_ext, [OrderInvoice::STATUS_REJECT])) {
  199. OrderInvoice::updateAll(['status_ext' => $this->status_ext, 'reason' => (string)$this->reason], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]);
  200. }
  201. //删除
  202. if ((int)$this->is_delete === 1) {
  203. OrderInvoice::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]);
  204. }
  205. $id = $ids[0];
  206. $orderInvoice = OrderInvoice::findOne($id);
  207. if(empty($orderInvoice)){
  208. throw new \Exception('未找到数据');
  209. }
  210. if ($this->invoice_pics) {
  211. $orderInvoice->invoice_pics = is_array($this->invoice_pics) ? json_encode($this->invoice_pics) : $this->invoice_pics;
  212. $orderInvoice->time_finish = time();
  213. }
  214. //完成开票
  215. if ($this->status_ext == OrderInvoice::STATUS_FINISH) {
  216. $orderInvoice->status_ext = $this->status_ext;
  217. }
  218. //红冲
  219. if ($this->is_cover) {
  220. $orderInvoice->is_cover = 1;
  221. }
  222. if(!$orderInvoice->save()){
  223. throw new \Exception(array_shift($orderInvoice->getFirstErrors()));
  224. }
  225. }
  226. return [
  227. 'code' => 0,
  228. 'msg' => '操作成功!'
  229. ];
  230. } catch (\Exception $e) {
  231. \Yii::error($e);
  232. return [
  233. 'code' => 1,
  234. 'msg' => $e->getMessage()
  235. ];
  236. }
  237. }
  238. public static function orderInvoiceSave ($order_id, $invoiceConf = [])
  239. {
  240. try {
  241. $order = Order::findOne($order_id);
  242. if(!$order){
  243. throw new \Exception('订单不存在');
  244. }
  245. if(($order->pay_price == 0)){
  246. throw new \Exception('订单未支付或支付金额为0');
  247. }
  248. if(in_array($order->trade_status, [Order::ORDER_FLOW_CANCEL])){
  249. throw new \Exception('订单未支付或已取消');
  250. }
  251. $orderInvoice = OrderInvoice::findOne(['order_id' => $order_id]) ?: new OrderInvoice();
  252. $orderInvoice->store_id = $order->store_id;
  253. $orderInvoice->order_id = $order->id;
  254. $orderInvoice->has_apply = $invoiceConf['has_apply'];
  255. $orderInvoice->user_id = $order->user_id;
  256. $orderInvoice->price = $order->pay_price;
  257. $orderInvoice->invoice_type = $invoiceConf['type'];
  258. $orderInvoice->invoice_title = $invoiceConf['title'];
  259. $orderInvoice->invoice_conf = json_encode($invoiceConf);
  260. $orderInvoice->status_ext = OrderInvoice::STATUS_DEFAULT;
  261. if(!$orderInvoice->save()){
  262. throw new \Exception(array_shift($orderInvoice->getFirstErrors()));
  263. }
  264. return [
  265. 'code' => 0,
  266. 'msg' => '操作成功!'
  267. ];
  268. } catch (\Exception $e) {
  269. \Yii::error($e);
  270. return [
  271. 'code' => 1,
  272. 'msg' => '开发票出错,' . $e->getMessage()
  273. ];
  274. }
  275. }
  276. }