| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use yii\base\Model;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\Order;
- use app\models\OrderInvoice;
- class InvoiceForm extends Model
- {
- public $store_id;
- public $id;
- public $ids;
- public $name;
- public $invoice_type;
- public $order_no;
- public $invoice_pics;
- public $is_cover;
- public $is_delete;
- public $start_time;
- public $end_time;
- public $time_finish_start_time;
- public $time_finish_end_time;
- public $status;
- public $status_ext;
- public $reason;
- public $has_apply;
- /**
- * @return array the validation rules.
- */
- public function rules()
- {
- return [
- [['reason', 'name'], 'trim'],
- [['order_no', 'name'], 'string'],
- [['store_id', 'id', 'invoice_type', 'is_cover', 'is_delete', 'status', 'status_ext', 'has_apply'], 'integer'],
- [['ids', 'invoice_pics', 'start_time', 'end_time', 'time_finish_start_time', 'time_finish_end_time', 'reason'], 'safe'],
- ];
- }
- public function init() {
- parent::init();
- if(empty($this->store_id)){
- $this->store_id = get_store_id();
- }
- }
-
- public function conf(){
- $conf = Option::get(OptionSetting::INVOICE_CONF, $this->store_id, 'store')['value'];
- if($conf){
- $conf = json_decode($conf, true);
- }else{
- $conf = ['conf' => [
- 'rule' => '发票须知',
- 'is_open' => 0,
- 'types' => [],
- 'timeout' => '0',
- 'tax' => 0,
- 'tax_rate' => 0,
- 'appcode' => ''
- ]];
- }
- $conf['conf']['appcode_desc'] = '购买接口地址:https://market.aliyun.com/products/56928005/cmapi00060851.html;从已购买服务里查询appcode:https://market.console.aliyun.com/#/bizlist';
- return [
- 'code'=>0,
- 'msg'=>'ok',
- 'data' => $conf,
- ];
- }
- public function confSave($conf){
- if(!is_array($conf)){
- $conf = json_decode($conf, true);
- }
- if(empty($conf['types'])){
- return [
- 'code'=>1,
- 'msg'=>'请选择发票类型'
- ];
- }
- $data = ['conf' => $conf];
- Option::set(OptionSetting::INVOICE_CONF, json_encode($data), $this->store_id, 'store');
- return [
- 'code'=>0,
- 'msg'=>'保存成功'
- ];
- }
- public static function is_open($store_id, &$invoiceConf = []) {
- $form = new self(['store_id' => $store_id]);
- $conf = $form->conf();
- $invoiceConf = $conf['data'];
- return (int)$invoiceConf['conf']['is_open'];
- }
- public static function ApiSearchInvoiceNo($store_id, $keyword) {
- $invoiceConf = [];
- $is_open = self::is_open($store_id, $invoiceConf);
- if(!$invoiceConf['conf']['appcode']){
- return [
- 'code' => 1,
- 'msg' => '店铺后台查询参数未配置',
- ];
- }
- $appcode = $invoiceConf['conf']['appcode'];
- $url = 'https://ein.market.alicloudapi.com/enterpriseIdNumberQuery';
- $querys = 'com=' . urlencode($keyword);
- $res = market_alicloudapi_com($appcode, $url, $querys, 'POST');
- // $res = '{"error_code":50002,"reason":"查询无结果","result":[],"ordersign":"20240130592970689200402432"}';
- // $res = '{"error_code": 0,"reason": "查询成功","result": {"creditCode": "91XXXXXXXXXXXXXXXXX","address": "洛阳市XXXXXXXXXXXXXXXXXXXXXXXXX园区FS309","tel": "","regNumber": "410XXXXXXXXXX","taxNumber": "91XXXXXXXXXXXXXXXXX","code": "91XXXXXXXXXXXXXXXXX"},"ordersign": "20240130592972684552441856"}';
- $res = json_decode($res, true);
- $res['code'] = $res['error_code'];
- $res['msg'] = $res['reason'];
- $res['result'] = $res['result'] ? : null;
- if($res['code'] != 0 || empty($res['result'])){
- return $res;
- }
- $item = &$res['result'];
- $item['title'] = $item['companyName'];
- $item['code'] = $item['taxNumber'];
- $item['address'] = $item['address'];
- $item['mobile'] = $item['tel'];
- return $res;
- }
- public static function tax_price($store_id, $price) {
- $form = new self(['store_id' => $store_id]);
- $conf = $form->conf();
- if($conf['data']['conf']['is_open'] && $conf['data']['conf']['tax']){
- return sprintf('%.2f', $price * $conf['data']['conf']['tax_rate'] / 100);
- }
- return 0;
- }
- public function orderInvoiceList ()
- {
- try {
- $query = OrderInvoice::find()->alias('oi')
- ->leftJoin(['o' => Order::tableName()], 'o.id = oi.order_id')
- ->where(['oi.has_apply' => 1, 'oi.is_delete' => 0, 'oi.store_id' => $this->store_id])
- ->andWhere(['o.is_pay' => 1])
- ->andWhere(['!=', 'o.trade_status', 1]);
- if (isset($this->status_ext) && $this->status_ext > -1) {
- $query->andWhere(['oi.status_ext' => $this->status_ext]);
- }
- if (isset($this->invoice_type) && $this->invoice_type > -1) {
- $query->andWhere(['oi.invoice_type' => $this->invoice_type]);
- }
- if (isset($this->is_cover) && $this->is_cover > -1) {
- $query->andWhere(['is_cover' => $this->is_cover]);
- }
- if (!empty($this->order_no)) {
- $query->andWhere(['oi.order_id' => Order::find()->select('id')->where(['order_no' => $this->order_no])]);
- }
- if (!empty($this->start_time)) {
- $query->andWhere(['>' , 'oi.created_at', strtotime($this->start_time)]);
- }
- if (!empty($this->end_time)) {
- $query->andWhere(['<' , 'oi.created_at', strtotime($this->end_time)]);
- }
- if (!empty($this->time_finish_start_time)) {
- $query->andWhere(['>' , 'oi.time_finish', strtotime($this->time_finish_start_time)]);
- }
- if (!empty($this->time_finish_end_time)) {
- $query->andWhere(['<' , 'oi.time_finish', strtotime($this->time_finish_end_time)]);
- }
- $query->orderBy('oi.id DESC');
- $query->select(['oi.*', 'o.order_no', 'o.order_type', 'o.is_pay', 'o.trade_status', 'o.name', 'o.mobile', 'o.address']);
- $pagination = pagination_make($query);
- // foreach($pagination['list'] as &$item){
- // $item['invoice_conf_arr'] = json_decode($item['invoice_conf'], true);
- // $item['invoice_conf_arr']['ext_arr'] = json_decode($item['invoice_conf_arr']['ext'], true);
- // }
- return [
- 'q' => $query->createCommand()->getRawSql(),
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination,
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public static function orderInvoiceApply (&$invoice)
- {
- $invoice->has_apply = 1;
- $conf = json_decode($invoice->invoice_conf, true);
- $conf['has_apply'] = 1;
- $invoice->invoice_conf = json_encode($conf);
- }
- public function orderInvoiceStatus ()
- {
- try {
- if ($this->ids) {
- $ids = explode(',', $this->ids);
- //拒绝
- if (in_array($this->status_ext, [OrderInvoice::STATUS_REJECT])) {
- OrderInvoice::updateAll(['status_ext' => $this->status_ext, 'reason' => (string)$this->reason], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]);
- }
- //删除
- if ((int)$this->is_delete === 1) {
- OrderInvoice::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]);
- }
- $id = $ids[0];
- $orderInvoice = OrderInvoice::findOne($id);
- if(empty($orderInvoice)){
- throw new \Exception('未找到数据');
- }
- if ($this->invoice_pics) {
- $orderInvoice->invoice_pics = is_array($this->invoice_pics) ? json_encode($this->invoice_pics) : $this->invoice_pics;
- $orderInvoice->time_finish = time();
- }
- //完成开票
- if ($this->status_ext == OrderInvoice::STATUS_FINISH) {
- $orderInvoice->status_ext = $this->status_ext;
- }
- //红冲
- if ($this->is_cover) {
- $orderInvoice->is_cover = 1;
- }
- if(!$orderInvoice->save()){
- throw new \Exception(array_shift($orderInvoice->getFirstErrors()));
- }
-
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public static function orderInvoiceSave ($order_id, $invoiceConf = [])
- {
- try {
- $order = Order::findOne($order_id);
- if(!$order){
- throw new \Exception('订单不存在');
- }
- if(($order->pay_price == 0)){
- throw new \Exception('订单未支付或支付金额为0');
- }
- if(in_array($order->trade_status, [Order::ORDER_FLOW_CANCEL])){
- throw new \Exception('订单未支付或已取消');
- }
- $orderInvoice = OrderInvoice::findOne(['order_id' => $order_id]) ?: new OrderInvoice();
- $orderInvoice->store_id = $order->store_id;
- $orderInvoice->order_id = $order->id;
- $orderInvoice->has_apply = $invoiceConf['has_apply'];
- $orderInvoice->user_id = $order->user_id;
- $orderInvoice->price = $order->pay_price;
- $orderInvoice->invoice_type = $invoiceConf['type'];
- $orderInvoice->invoice_title = $invoiceConf['title'];
- $orderInvoice->invoice_conf = json_encode($invoiceConf);
- $orderInvoice->status_ext = OrderInvoice::STATUS_DEFAULT;
- if(!$orderInvoice->save()){
- throw new \Exception(array_shift($orderInvoice->getFirstErrors()));
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => '开发票出错,' . $e->getMessage()
- ];
- }
- }
- }
|