审查视图

application/api/controller/Invoice.php 20.8 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 54 55 56 57 58 59
//        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
60 61 62 63 64 65 66
        $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
67
            'selectedOptionszero' => $param['selectedOptionszero']
王智 authored
68
        ];
王智 authored
69 70 71 72 73 74 75 76 77 78 79
//        } 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
80
        $invoice_id = Db::name('invoice_ziliao')->where(['user_id' => $user_id])->value('id');
王智 authored
81 82 83 84 85
        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
86 87 88 89 90 91 92 93 94
        if ($res) {
            $this->success('成功', 1);
        } else {
            $this->error('失败', 0);
        }
    }


    /**
王智 authored
95 96 97 98 99 100 101 102 103 104
     * @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
105 106 107 108 109 110 111 112
    "data": {
    "company_list": {
    "company_name": "",
    "credit": "",
    "bank_name": "",
    "bank_num": "",
    "company_tel": "",
    "invoice_address": ""
王智 authored
113 114 115 116 117 118
    },
    "user_list": {
    "shou": "",
    "mobile": "",
    "address": "",
    "address_con": ""
王智 authored
119
    "invoice_head":""
王智 authored
120 121
    }
    }
王智 authored
122 123 124 125 126
    })
     */
    public function InvoiceDataRendering()
    {
        $user_id = $this->is_token($this->request->header());
王智 authored
127
        $is_company = Db::name('company')->where(['company_holder' => $user_id])->where(['status' => 1])->value('id');
王智 authored
128
        if (empty($is_company)) {
王智 authored
129
            $is_team = Db::name('team')->where(['user_id' => $user_id])->where(['status' => 1])->value('company_id');
王智 authored
130
            if (empty($is_team)) {
王智 authored
131
                $company_list = '';
王智 authored
132
            } else {
王智 authored
133
                $team_company_arr = Db::name('company')->where(['id' => $is_team])->find();
王智 authored
134
                $company_list = [
王智 authored
135 136 137 138 139 140
                    '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
141 142
                ];
            }
王智 authored
143
        } else {
王智 authored
144
            $company_arr = Db::name('company')->where(['id' => $is_company])->find();
王智 authored
145
            $company_list = [
王智 authored
146 147 148 149 150 151
                '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
152
            ];
王智 authored
153
        }
王智 authored
154 155
        $ziliao = Db::name('invoice_ziliao')->where(['user_id' => $user_id])->find();
        if (empty($ziliao)) {
王智 authored
156
            $user_list = '';
王智 authored
157 158 159 160 161
        } else {
            $user_list = [
                'shou' => $ziliao['shou'],
                'mobile' => $ziliao['mobile'],
                'address' => $ziliao['address'],
王智 authored
162
                'address_con' => $ziliao['address_con'],
王智 authored
163 164
                'invoice_head' => $ziliao['invoice_head'],
                'selectedOptionszero' => $ziliao['selectedOptionszero']
王智 authored
165 166
            ];
        }
王智 authored
167
        $return['company_list'] = $company_list;
王智 authored
168
        $return['user_list'] = $user_list;
王智 authored
169 170 171 172 173
        $this->success('成功', $return);
    }


    /**
王智 authored
174 175
     * @ApiTitle    (发票接口-申请发票)
     * @ApiSummary  (申请发票)
wangzhi authored
176
     * @ApiMethod   (POST)
王智 authored
177
     * @ApiRoute    (/api/Invoice/ApplicationForOrdinaryInvoice)
wangzhi authored
178
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
王智 authored
179 180 181
     * @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
182 183 184 185 186 187 188 189
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": "1"
    })
     */
王智 authored
190
    public function ApplicationForOrdinaryInvoice()
wangzhi authored
191 192 193
    {
        $user_id = $this->is_token($this->request->header());
        $param = $this->request->param();
王智 authored
194
        $ziliao = Db::name('invoice_ziliao')->where(['user_id' => $user_id])->find();
王智 authored
195 196 197 198 199 200 201 202 203 204 205 206
        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
207
                    'paragraph' => $team['paragraph'],
王智 authored
208
                    'company_tel' => $team['company_tel'],
王智 authored
209
                    'register_tel' => $team['register_tel'],
王智 authored
210 211 212
                    'invoice_address' => $team['invoice_address'],
                    'order_sn' => $param['order_sn'],
                    'user_id' => $user_id,
王智 authored
213
                    'invoice_people' => $ziliao['shou'],
王智 authored
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
                    '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
229
                    'paragraph' => $is_company['paragraph'],
王智 authored
230
                    'company_tel' => $is_company['company_tel'],
王智 authored
231
                    'register_tel' => $is_company['register_tel'],
王智 authored
232 233 234
                    'invoice_address' => $is_company['invoice_address'],
                    'order_sn' => $param['order_sn'],
                    'user_id' => $user_id,
王智 authored
235
                    'invoice_people' => $ziliao['shou'],
王智 authored
236 237 238 239 240 241 242 243 244 245 246 247 248 249
                    '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
250
                'invoice_people' => $ziliao['shou'],
王智 authored
251
                'invoice_head_people' => $ziliao['invoice_head'],
王智 authored
252 253 254 255 256 257 258 259
                'mobile' => $ziliao['mobile'],
                'address' => $ziliao['address'],
                'address_con' => $ziliao['address_con'],
                'invoice_status' => 1,
                'createtime' => time(),
                'updatetime' => time()
            ];
        }
wangzhi authored
260 261 262 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
        $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
338
                ->field('o.id,o.total,o.order_sn,u.username')
wangzhi authored
339 340 341 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
                ->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
370
                ->field('o.id,o.total,o.order_sn,u.username,o.order_status')
wangzhi authored
371 372 373
                ->select();
            $shopping = Db::name('order_invoice')->where(['user_id' => $user_id])->where($map2)->select();
        }
王智 authored
374
        if ($type == 3) {
王智 authored
375 376
            $map['i.invoice_status'] = array('eq', 4);
            $map2['invoice_status'] = array('eq', 4);
wangzhi authored
377 378 379 380 381 382
            $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
383
                ->field('o.id,o.total,o.order_sn,u.username,o.order_status')
wangzhi authored
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
                ->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
402 403 404 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
                    $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
477 478
        $map['i.order_sn'] = ['LIKE', '%' . $key . '%'];
        $map2['order_sn'] = ['LIKE', '%' . $key . '%'];
王智 authored
479 480 481 482 483 484
        $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
485
            ->field('o.id,o.invoice_status,o.total,o.order_sn,u.username,o.order_status')
王智 authored
486
            ->select();
王智 authored
487
        $shopping = Db::name('order_invoice')->where(['user_id' => $user_id])->where($map2)->select();
王智 authored
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
        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
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
                    $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);
    }
}