审查视图

application/api/controller/Invoice.php 20.9 KB
wangzhi authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php
/**
 * Created by PhpStorm.
 * User: 86132
 * Date: 2020/7/17
 * Time: 14:36
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;

/**
 * 发票接口
 */
class Invoice extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }


    /**
王智 authored
30 31
     * @ApiTitle    (发票接口-填写发票资料)
     * @ApiSummary  (填写发票资料)
wangzhi authored
32
     * @ApiMethod   (POST)
王智 authored
33
     * @ApiRoute    (/api/Invoice/ApplyForProfessionalInvoice)
wangzhi authored
34
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
王智 authored
35
     * @ApiParams   (name="invoice_head", type="string", required=false, description="发票抬头")
wangzhi authored
36 37 38 39 40 41 42 43 44 45 46 47
     * @ApiParams   (name="invoice_people", type="string", required=true, description="收票人")
     * @ApiParams   (name="mobile", type="int", required=true, description="手机号码")
     * @ApiParams   (name="address", type="string", required=true, description="收票地址")
     * @ApiParams   (name="address_con", type="string", required=true, description="详细地址")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": "1"
    })
     */
王智 authored
48
    public function ApplyForProfessionalInvoice()
wangzhi authored
49 50 51
    {
        $user_id = $this->is_token($this->request->header());
        $param = $this->request->param();
王智 authored
52 53
        $selectedOptionszero = input('selectedOptionszero');
//        $arr = json_decode(htmlspecialchars_decode($selectedOptionszero), true);
王智 authored
54
//        $str=implode(',', $arr);
王智 authored
55 56 57 58 59 60 61 62
//        if ($param['type'] == 1) {
//            $is_company = Db::name('company')->where(['company_holder' => $user_id])->find();
//            if (empty($is_company)) {
//                $is_team = Db::name('team')->where(['user_id' => $user_id])->value('company_id');
//                if (empty($is_team)) {
//                    $this->error('您还没有加入公司不能开企业单位发票', 0);
//                }
//            }
王智 authored
63 64 65 66 67 68 69
        $data = [
            'user_id' => $user_id,
            'shou' => $param['invoice_people'],
            'mobile' => $param['mobile'],
            'address' => $param['address'],
            'address_con' => $param['address_con'],
            'invoice_head' => $param['invoice_head'],
王智 authored
70
            'selectedOptionszero' => $selectedOptionszero
王智 authored
71
        ];
王智 authored
72 73 74 75 76 77 78 79 80 81 82
//        } else {
//            $data = [
//                'user_id' => $user_id,
//                'shou' => $param['invoice_people'],
//                'mobile' => $param['mobile'],
//                'address' => $param['address'],
//                'address_con' => $param['address_con'],
//                'type' => $param['type'],
//                'invoice_head' => $param['invoice_head'],
//            ];
//        }
王智 authored
83
        $invoice_id = Db::name('invoice_ziliao')->where(['user_id' => $user_id])->value('id');
王智 authored
84 85 86 87 88
        if (empty($invoice_id)) {
            $res = Db::name('invoice_ziliao')->insert($data);
        } else {
            $res = Db::name('invoice_ziliao')->where(['id' => $invoice_id])->update($data);
        }
wangzhi authored
89 90 91 92 93 94 95 96 97
        if ($res) {
            $this->success('成功', 1);
        } else {
            $this->error('失败', 0);
        }
    }


    /**
王智 authored
98 99 100 101 102 103 104 105 106 107
     * @ApiTitle    (发票接口-发票资料渲染)
     * @ApiSummary  (发票资料渲染)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Invoice/InvoiceDataRendering)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
王智 authored
108 109 110 111 112 113 114 115
    "data": {
    "company_list": {
    "company_name": "",
    "credit": "",
    "bank_name": "",
    "bank_num": "",
    "company_tel": "",
    "invoice_address": ""
王智 authored
116 117 118 119 120 121
    },
    "user_list": {
    "shou": "",
    "mobile": "",
    "address": "",
    "address_con": ""
王智 authored
122
    "invoice_head":""
王智 authored
123 124
    }
    }
王智 authored
125 126 127 128 129
    })
     */
    public function InvoiceDataRendering()
    {
        $user_id = $this->is_token($this->request->header());
王智 authored
130
        $is_company = Db::name('company')->where(['company_holder' => $user_id])->where(['status' => 1])->value('id');
王智 authored
131
        if (empty($is_company)) {
王智 authored
132
            $is_team = Db::name('team')->where(['user_id' => $user_id])->where(['status' => 1])->value('company_id');
王智 authored
133
            if (empty($is_team)) {
王智 authored
134
                $company_list = '';
王智 authored
135
            } else {
王智 authored
136
                $team_company_arr = Db::name('company')->where(['id' => $is_team])->find();
王智 authored
137
                $company_list = [
王智 authored
138 139 140 141 142 143
                    'company_name' => $team_company_arr['company_name'],
                    'credit' => $team_company_arr['credit'],
                    'bank_name' => $team_company_arr['bank_name'],
                    'bank_num' => $team_company_arr['bank_num'],
                    'company_tel' => $team_company_arr['company_tel'],
                    'invoice_address' => $team_company_arr['invoice_address'],
王智 authored
144 145
                ];
            }
王智 authored
146
        } else {
王智 authored
147
            $company_arr = Db::name('company')->where(['id' => $is_company])->find();
王智 authored
148
            $company_list = [
王智 authored
149 150 151 152 153 154
                'company_name' => $company_arr['company_name'],
                'credit' => $company_arr['credit'],
                'bank_name' => $company_arr['bank_name'],
                'bank_num' => $company_arr['bank_num'],
                'company_tel' => $company_arr['company_tel'],
                'invoice_address' => $company_arr['invoice_address'],
王智 authored
155
            ];
王智 authored
156
        }
王智 authored
157 158
        $ziliao = Db::name('invoice_ziliao')->where(['user_id' => $user_id])->find();
        if (empty($ziliao)) {
王智 authored
159
            $user_list = '';
王智 authored
160 161 162 163 164
        } else {
            $user_list = [
                'shou' => $ziliao['shou'],
                'mobile' => $ziliao['mobile'],
                'address' => $ziliao['address'],
王智 authored
165
                'address_con' => $ziliao['address_con'],
王智 authored
166
                'invoice_head' => $ziliao['invoice_head'],
王智 authored
167
                'selectedOptionszero' => $ziliao['selectedOptionszero']
王智 authored
168 169
            ];
        }
王智 authored
170
        $return['company_list'] = $company_list;
王智 authored
171
        $return['user_list'] = $user_list;
王智 authored
172 173 174 175 176
        $this->success('成功', $return);
    }


    /**
王智 authored
177 178
     * @ApiTitle    (发票接口-申请发票)
     * @ApiSummary  (申请发票)
wangzhi authored
179
     * @ApiMethod   (POST)
王智 authored
180
     * @ApiRoute    (/api/Invoice/ApplicationForOrdinaryInvoice)
wangzhi authored
181
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
王智 authored
182 183 184
     * @ApiParams   (name="invoice_type", type="int", required=true, description="发票类型:1=专用发票,2=普通发票")
     * @ApiParams   (name="type", type="int", required=true, description="抬头类型:1=企业单位,2=个人")
     * @ApiParams   (name="order_sn", type="string", required=true, description="订单号")
wangzhi authored
185 186 187 188 189 190 191 192
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": "1"
    })
     */
王智 authored
193
    public function ApplicationForOrdinaryInvoice()
wangzhi authored
194 195 196
    {
        $user_id = $this->is_token($this->request->header());
        $param = $this->request->param();
王智 authored
197
        $ziliao = Db::name('invoice_ziliao')->where(['user_id' => $user_id])->find();
王智 authored
198 199 200 201 202 203 204 205 206 207 208 209
        if ($param['type'] == 1) {
            $is_company = Db::name('company')->where(['company_holder' => $user_id])->find();
            if (empty($is_company)) {
                $is_team = Db::name('team')->where(['user_id' => $user_id])->value('company_id');
                $team = Db::name('company')->where(['id' => $is_team])->find();
                $data = [
                    'invoice_head_name' => $team['company_name'],
                    'invoice_head' => 1,
                    'invoice_type' => $param['invoice_type'],
                    'company_code' => $team['credit'],
                    'bank_name' => $team['bank_name'],
                    'bank_num' => $team['bank_num'],
王智 authored
210
                    'paragraph' => $team['paragraph'],
王智 authored
211
                    'company_tel' => $team['company_tel'],
王智 authored
212
                    'register_tel' => $team['register_tel'],
王智 authored
213 214 215
                    'invoice_address' => $team['invoice_address'],
                    'order_sn' => $param['order_sn'],
                    'user_id' => $user_id,
王智 authored
216
                    'invoice_people' => $ziliao['shou'],
王智 authored
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                    'mobile' => $ziliao['mobile'],
                    'address' => $ziliao['address'],
                    'address_con' => $ziliao['address_con'],
                    'invoice_status' => 1,
                    'createtime' => time(),
                    'updatetime' => time()
                ];
            } else {
                $data = [
                    'invoice_head_name' => $is_company['company_name'],
                    'invoice_head' => 1,
                    'invoice_type' => $param['invoice_type'],
                    'company_code' => $is_company['credit'],
                    'bank_name' => $is_company['bank_name'],
                    'bank_num' => $is_company['bank_num'],
王智 authored
232
                    'paragraph' => $is_company['paragraph'],
王智 authored
233
                    'company_tel' => $is_company['company_tel'],
王智 authored
234
                    'register_tel' => $is_company['register_tel'],
王智 authored
235 236 237
                    'invoice_address' => $is_company['invoice_address'],
                    'order_sn' => $param['order_sn'],
                    'user_id' => $user_id,
王智 authored
238
                    'invoice_people' => $ziliao['shou'],
王智 authored
239 240 241 242 243 244 245 246 247 248 249 250 251 252
                    'mobile' => $ziliao['mobile'],
                    'address' => $ziliao['address'],
                    'address_con' => $ziliao['address_con'],
                    'invoice_status' => 1,
                    'createtime' => time(),
                    'updatetime' => time()
                ];
            }
        } else {
            $data = [
                'invoice_head' => 2,
                'invoice_type' => $param['invoice_type'],
                'order_sn' => $param['order_sn'],
                'user_id' => $user_id,
王智 authored
253
                'invoice_people' => $ziliao['shou'],
王智 authored
254
                'invoice_head_people' => $ziliao['invoice_head'],
王智 authored
255 256 257 258 259 260 261 262
                'mobile' => $ziliao['mobile'],
                'address' => $ziliao['address'],
                'address_con' => $ziliao['address_con'],
                'invoice_status' => 1,
                'createtime' => time(),
                'updatetime' => time()
            ];
        }
wangzhi authored
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
        $res = Db::name('order_invoice')->insert($data);
        Db::name('order')->where(['order_sn' => $param['order_sn']])->update(['invoice_status' => 2]);
        if ($res) {
            $this->success('成功', 1);
        } else {
            $this->error('失败', 0);
        }
    }


    /**
     * @ApiTitle    (发票接口-发票查询)
     * @ApiSummary  (发票查询)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Invoice/InvoiceQuery)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="string", required=true, description="1:待开票,2=开票中,3=已开票")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": {
    "ToBeReviewed": 待开票,
    "GoodsToBeReceived": 开票中,
    "Completed": 已开票,
    "itemList": [
    {
    "total": "总价",
    "order_sn": "202007161753342661571130",
    "username": "下单人",
    "shopping": 快递,
    "shop_order": 快递单号,
    "invoice_code": 发票代码,
    "invoice_num": 发票号,
    "buy_num": 数量,
    "list": [
    {
    "avatar": "http://qco519e0n.bkt.clouddn.com/uploads/20200703/Fpi3RGA38eqT3eDk_sh99hOZ7wAA.png",
    "name": "12kf(123)+10.50v",
    "class_con": "详细分类3",
    "logo": "品牌1",
    "price": 60,
    "gradient": [
    {
    "tidu": "100",
    "price": "0.10"
    }
    ]
    }
    ]
    }
    })
     */
    public function InvoiceQuery()
    {
        $user_id = $this->is_token($this->request->header());
        $type = input('type');
        $ToBeReviewed_arr = Db::name('order')->where(['user_id' => $user_id])->where(['invoice_status' => 1])->select();
        $incoice = [];
        $incoice['invoice_status'] = array(array('eq', 2), array('eq', 3), 'or');
        $GoodsToBeReceived_arr = Db::name('order_invoice')->where(['user_id' => $user_id])
            ->where($incoice)
            ->select();
        $Completed_arr = Db::name('order_invoice')->where(['user_id' => $user_id])->where(['invoice_status' => 4])->select();
        $return['ToBeReviewed'] = count($ToBeReviewed_arr);
        $return['GoodsToBeReceived'] = count($GoodsToBeReceived_arr);
        $return['Completed'] = count($Completed_arr);
        $map = [];
        $map2 = [];
        if ($type == 1) {
            $map['o.invoice_status'] = array('eq', $type);
            $map2['invoice_status'] = array('eq', $type);
            $list = Db::name('order')
                ->alias('o')
                ->where($map)
                ->where(['o.user_id' => $user_id])
                ->join('user u', 'u.id=o.user_id')
王智 authored
341
                ->field('o.id,o.total,o.order_sn,u.username')
wangzhi authored
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
                ->select();
            $shopping = Db::name('order')->where(['user_id' => $user_id])->where($map2)->select();
            foreach ($list as $k1 => $v1) {
                $list[$k1]['shopping'] = null;
                $list[$k1]['shop_order'] = null;
                $list[$k1]['invoice_code'] = null;
                $list[$k1]['invoice_num'] = null;
            }
            foreach ($shopping as $k => $v) {
                $order_con_arr[0] = Db::name('order_con')->where(['order_sn' => $v['order_sn']])->select();
                $order_con = $this->three_arr($order_con_arr);
                foreach ($order_con as $k1 => $v1) {
                    $product_arr[$k1] = $this->GenerateOrderPriceCalculation($v1['product_id'], $v1['buy_num']);
                    $buy_num[$k1] = $v1['buy_num'];
                }
                $product_list = $this->three_arr($product_arr);
                $list[$k]['buy_num'] = array_sum($buy_num);
                $list[$k]['list'] = $product_list;
            }
            $return['itemList'] = $list;
            $this->success('成功', $return);
        }
        if ($type == 2) {
            $map['i.invoice_status'] = array(array('eq', 2), array('eq', 3), 'or');
            $map2['invoice_status'] = array(array('eq', 2), array('eq', 3), 'or');
            $list = Db::name('order_invoice')
                ->alias('i')
                ->where(['i.user_id' => $user_id])
                ->where($map)
                ->join('user u', 'u.id=i.user_id')
                ->join('order o', 'o.order_sn=i.order_sn')
王智 authored
373
                ->field('o.id,o.total,o.order_sn,u.username,o.order_status')
wangzhi authored
374 375 376
                ->select();
            $shopping = Db::name('order_invoice')->where(['user_id' => $user_id])->where($map2)->select();
        }
王智 authored
377
        if ($type == 3) {
王智 authored
378 379
            $map['i.invoice_status'] = array('eq', 4);
            $map2['invoice_status'] = array('eq', 4);
wangzhi authored
380 381 382 383 384 385
            $list = Db::name('order_invoice')
                ->alias('i')
                ->where(['i.user_id' => $user_id])
                ->where($map)
                ->join('user u', 'u.id=i.user_id')
                ->join('order o', 'o.order_sn=i.order_sn')
王智 authored
386
                ->field('o.id,o.total,o.order_sn,u.username,o.order_status')
wangzhi authored
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
                ->select();
            $shopping = Db::name('order_invoice')->where(['user_id' => $user_id])->where($map2)->select();
        }
        foreach ($shopping as $k => $v) {
            if ($v['shopping'] == NULL) {
                foreach ($list as $k1 => $v1) {
                    $list[$k]['shopping'] = null;
                    $list[$k]['shop_order'] = null;
                }
            } else {
                foreach ($list as $k1 => $v1) {
                    $list[$k]['shopping'] = $v['shopping'];
                    $list[$k]['shop_order'] = $v['shop_order'];
                }
            }
            if ($v['invoice_code'] == NULL) {
                foreach ($list as $k1 => $v1) {
                    $list[$k]['invoice_code'] = null;
王智 authored
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
                    $list[$k]['invoice_num'] = null;
                }
            } else {
                foreach ($list as $k1 => $v1) {
                    $list[$k]['invoice_code'] = $v['invoice_code'];
                    $list[$k]['invoice_num'] = $v['invoice_num'];
                }
            }
        }
        foreach ($shopping as $k => $v) {
            $order_con_arr[0] = Db::name('order_con')->where(['order_sn' => $v['order_sn']])->select();
            $order_con = $this->three_arr($order_con_arr);
            foreach ($order_con as $k1 => $v1) {
                $product_arr[$k1] = $this->GenerateOrderPriceCalculation($v1['product_id'], $v1['buy_num']);
                $buy_num[$k1] = $v1['buy_num'];
            }
            $product_list = $this->three_arr($product_arr);
            $list[$k]['buy_num'] = array_sum($buy_num);
            $list[$k]['list'] = $product_list;
        }
        $return['itemList'] = $list;
        $this->success('成功', $return);
    }


    /**
     * @ApiTitle    (发票接口-发票搜索)
     * @ApiSummary  (发票搜索)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Invoice/InvoiceSearch)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="keyword", type="string", required=true, description="搜索")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": {
    "ToBeReviewed": 待开票,
    "GoodsToBeReceived": 开票中,
    "Completed": 已开票,
    "itemList": [
    {
    "total": "总价",
    "order_sn": "202007161753342661571130",
    "username": "下单人",
    "shopping": 快递,
    "shop_order": 快递单号,
    "invoice_code": 发票代码,
    "invoice_num": 发票号,
    "buy_num": 数量,
    "list": [
    {
    "avatar": "http://qco519e0n.bkt.clouddn.com/uploads/20200703/Fpi3RGA38eqT3eDk_sh99hOZ7wAA.png",
    "name": "12kf(123)+10.50v",
    "class_con": "详细分类3",
    "logo": "品牌1",
    "price": 60,
    "gradient": [
    {
    "tidu": "100",
    "price": "0.10"
    }
    ]
    }
    ]
    }
    })
     */
    public function InvoiceSearch()
    {
        $user_id = $this->is_token($this->request->header());
        $key = input('key');
        $map = [];
        $map2 = [];
王智 authored
480 481
        $map['i.order_sn'] = ['LIKE', '%' . $key . '%'];
        $map2['order_sn'] = ['LIKE', '%' . $key . '%'];
王智 authored
482 483 484 485 486 487
        $list = Db::name('order_invoice')
            ->alias('i')
            ->where(['i.user_id' => $user_id])
            ->where($map)
            ->join('user u', 'u.id=i.user_id')
            ->join('order o', 'o.order_sn=i.order_sn')
王智 authored
488
            ->field('o.id,o.invoice_status,o.total,o.order_sn,u.username,o.order_status')
王智 authored
489
            ->select();
王智 authored
490
        $shopping = Db::name('order_invoice')->where(['user_id' => $user_id])->where($map2)->select();
王智 authored
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
        foreach ($shopping as $k => $v) {
            if ($v['shopping'] == NULL) {
                foreach ($list as $k1 => $v1) {
                    $list[$k]['shopping'] = null;
                    $list[$k]['shop_order'] = null;
                }
            } else {
                foreach ($list as $k1 => $v1) {
                    $list[$k]['shopping'] = $v['shopping'];
                    $list[$k]['shop_order'] = $v['shop_order'];
                }
            }
            if ($v['invoice_code'] == NULL) {
                foreach ($list as $k1 => $v1) {
                    $list[$k]['invoice_code'] = null;
wangzhi authored
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
                    $list[$k]['invoice_num'] = null;
                }
            } else {
                foreach ($list as $k1 => $v1) {
                    $list[$k]['invoice_code'] = $v['invoice_code'];
                    $list[$k]['invoice_num'] = $v['invoice_num'];
                }
            }
        }
        foreach ($shopping as $k => $v) {
            $order_con_arr[0] = Db::name('order_con')->where(['order_sn' => $v['order_sn']])->select();
            $order_con = $this->three_arr($order_con_arr);
            foreach ($order_con as $k1 => $v1) {
                $product_arr[$k1] = $this->GenerateOrderPriceCalculation($v1['product_id'], $v1['buy_num']);
                $buy_num[$k1] = $v1['buy_num'];
            }
            $product_list = $this->three_arr($product_arr);
            $list[$k]['buy_num'] = array_sum($buy_num);
            $list[$k]['list'] = $product_list;
        }
        $return['itemList'] = $list;
        $this->success('成功', $return);
    }
}