Allocation.php 63.5 KB
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 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 338 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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 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 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
<?php

namespace app\admin\controller\kuerp;

use app\common\controller\Backend;
use think\Db;
use Exception;
use think\Session;
/**
 * 调拨单
 *
 * @icon fa fa-circle-o
 */
class Allocation extends Backend
{
    protected $noNeedRight = ['getUnit','getSerial'];
    /**
     * Allocation模型对象
     * @var \app\admin\model\kuerp\Allocation
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $setData = (new Goods())->getSetData();
        $this->assign('set', $setData['setList']);
        $this->assignconfig('set',$setData['setList']);
        $this->assign('nowmenu', $setData['nowmenu']);

        $this->model = new \app\admin\model\kuerp\Allocation;
        $this->view->assign("examineStatusList", $this->model->getExamineStatusList());

        $this->view->assign("goodsList", $this->model->getGoodsList());
        $this->assignconfig("goodsList", $this->model->getGoodsList());

        $this->view->assign("storesList", $this->model->getStoresList());
        $this->assignconfig("storesList", $this->model->getStoresList());
    }

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

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $wheres = [];
            $searchdata = json_decode(input('filter'),true);
            if($searchdata){
                foreach ($searchdata as $k => &$v) {
                    if(!empty($v)){
                        if(strpos($v,' - ') !== false && strpos($k,'time') !== false){
                            $str = explode(' - ',$v);
                            $wheres['allocation.'.$k] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                        }else{
                            $wheres['allocation.'.$k] = ["LIKE", "%{$v}%"];
                        }
                    }
                }
            }

            $list = $this->model
                ->with(['admin'])
                ->where($where)->where($wheres)
                ->where((new Goods())->setWhere([],'allocation'))
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $row) {
                $row->getRelation('admin')->visible(['username']);
            }

            $store = Db::name('kuerp_stores')->where((new Goods())->setWhere())->column('name','id');
            $lists = [];
            foreach ($list as $list_k=>$list_v){
                if(isset($list_v['print_num']) && empty($list_v['print_num'])){
                    $list_v['print_num'] = 0;
                }
                $info = Db::name('kuerp_allocation_info')->alias('ai')
                    ->join('kuerp_goods g','g.id=ai.goods_id')
                    ->join('kuerp_goods_type gt','gt.id=g.goods_type_id')
                    ->join('kuerp_unit u','u.id=ai.goods_unit')
                    ->where((new Goods())->setWhere(['ai.allocation_id'=>$list_v['id']],'ai'))
                    ->field('gt.supplier_type_name as type_name,g.goods_name,ai.goods_note,ai.number,u.name as unit_name,ai.remarks as remark,ai.stores_out,ai.stores_enter')
                    ->select();
                $list_v['preparation_id'] = Db::name('admin')->where(['id'=>$list_v['preparation_id']])->value('username');
                $list_v['operation_id']   = Db::name('admin')->where(['id'=>$list_v['operation_id']])->value('username');
                $list_v['examine_id']   = Db::name('admin')->where(['id'=>$list_v['examine_id']])->value('username');
                foreach ($info as $k=>$v){
                    $v['stores_out'] = isset($store[$v['stores_out']])?$store[$v['stores_out']]:'';
                    $v['stores_enter'] = isset($store[$v['stores_enter']])?$store[$v['stores_enter']]:'';
                    $v['id'] = $list_v['id'];
                    $v['purchase_code'] = $list_v['purchase_code'];
                    $v['purchase_time'] = $list_v['purchase_time'];
                    $v['preparation_id'] = $list_v['preparation_id'];
                    $v['operation_id'] = $list_v['operation_id'];
                    $v['examine_id'] = $list_v['examine_id'];
                    $v['print_num'] = $list_v['print_num'];
                    $v['remarks'] = $list_v['remarks'];
                    $lists[] = $v;
                }
            }
            $result = array("total" => $list->total(), "rows" => $lists);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add($ids = null)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $examine= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'examine']))->value('is_checked');
            if ($params) {
                $params = $this->preExcludeFields($params);
                (new \app\admin\controller\kuerp\Checkout())->checkOutTime($params['purchase_time']);
                if(empty($params['purchase_code'])){
                    $this->error('请填写单据编号');
                }
                $admin = Session::get('admin');

                $set= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'serial']))->value('is_checked');
                $batch= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'batch']))->value('is_checked');
                $auxiliary_status= Db::name('kuerp_sets')->where(['name'=>'auxiliary'])->value('is_checked');
                $data = [];
                $goods_serial_all = [];
                foreach ($params['goods_code'] as $k=>$v){
                    if(!empty($v)){//有产品编号
                        if(!isset($params['stores_out'][$k]) || empty($params['stores_out'][$k])){
                            $this->error('请选择调出仓库');
                        }else if(!isset($params['stores_enter'][$k]) || empty($params['stores_enter'][$k])){
                            $this->error('请选择调入仓库');
                        }else if($params['stores_out'][$k]==$params['stores_enter'][$k]){
                            $this->error('调出仓库不能和调入仓库相同');
                        }

                        $goods = Db::name('kuerp_goods')->where((new Goods())->setWhere(['goods_code'=>$v]))->field('id,serial,batch,auxiliary')->find();
                        if ($goods['auxiliary']==1 && empty($params['goods_attr'][$k]) && $auxiliary_status==1){
                            return ['status'=>500,'msg'=>'请选择商品'.$v.'属性'];
                        }
                        if($goods['batch']==1 && $batch==1){
                            if(empty($params['batch'][$k])){
                                $this->error('请选择生产批次');
                            }
                            $batch_data = Db::name('kuerp_goods_batch')->where((new Goods())->setWhere(['id'=>$params['batch_id'][$k]]))->find();
                            if(empty($batch_data)){
                                $this->error('生产批次不存在');
                            }
                        }

                        $serial_code = [];
                        if(isset($params['period_period'][$k]) && $goods['serial']==1 && $set==1){
                            if(empty($params['period_period'][$k])){
                                $this->error('请输入商品序列号');
                            }
                            $period_period = unserialize($params['period_period'][$k]);
                            foreach ($period_period as $period_period_k=>$period_period_v){
                                if(in_array($period_period_v['serial'],$goods_serial_all)){
                                    $this->error('商品序列号'.$period_period_v['serial'].'重复');
                                }else{
                                    $goods_serial_all[] = $period_period_v['serial'];
                                }
                                $serial_code[] = $period_period_v;
                            }
                        }
                        $data[] = [
                            'goods_id'          =>  $goods['id'],//商品信息
                            'goods_attr'        =>  (float)$params['goods_attr'][$k],//商品属性
                            'goods_unit'        =>  (float)$params['goods_unit'][$k],//商品单位
                            'stores_out'        =>  (float)$params['stores_out'][$k],//调出仓库
                            'stores_enter'      =>  (float)$params['stores_enter'][$k],//调入仓库
                            'number'            =>  (float)$params['number'][$k],//数量
                            'allow_stock'       =>  (float)$params['allow_stock'][$k],
                            'goods_batch_id'    =>  (float)$params['batch_id'][$k],
                            'goods_batch_name'  =>  $params['batch'][$k],
                            'goods_serial_id'   =>  !empty($serial_code)?serialize($serial_code):null,
                            'remarks'           =>  $params['remark'][$k],
                            'goods_note'        =>  $params['goods_note'][$k],
                        ];
                    }
                }
                if(!empty($data)){
                    $row = [
                        'purchase_code'     =>  $params['purchase_code'],
                        'purchase_time'     =>  strtotime($params['purchase_time']),
                        'remarks'           =>  $params['note'],//备注
                        'preparation_id'    =>  $admin['id'],
                        'operation_id'      =>  $admin['id'],
                        'createtime'        => time(),
                        'updatetime'        => time(),
                    ];
                }

                if($this->model->where((new Goods())->setWhere(['purchase_code'=>trim($params['purchase_code'])]))->find()){
                    $this->error('单据编号已占用');
                }
                $result = false;
                Db::startTrans();
                try {
                    if(!empty($row)){
                        $result = $this->model->insertGetId((new Goods())->setDataInfo($row));
                    }
                    if($result){
                        foreach ($data as $data_k=>$data_v){
                            $data_v['allocation_id'] = $result;
                            $data_v = (new Goods())->setDataInfo($data_v);
                            $data[$data_k] = $data_v;
                        }
                        Db::name('kuerp_allocation_info')->insertAll($data);
                    }
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if (isset($result) && $result !== false) {
                    if($examine!=1){
                        try {
                            $exa = $this->examine($result,1);
                        } catch (Exception $e) {
                        }
                        if(!isset($exa) || $exa['status']!=1){
                            $exa = $this->examine($result,1);
                            try {
                                $this->del($result);
                            } catch (Exception $e) {
                            }
                            $this->error($exa['msg']);
                        }
                    }
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $code = (new Goods())->getCodes('',13);
        $this->view->assign("code", $code);
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        $row = $this->model->where((new Goods())->setWhere(['id'=>$ids]))->find();
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            (new \app\admin\controller\kuerp\Checkout())->checkOutTime($row['purchase_time']);
            $examine= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'examine']))->value('is_checked');
            if($row['examine_status']==1 && $examine==1){
                $this->error('不能编辑已审核的数据');
            }

            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                if(empty($params['purchase_code'])){
                    $this->error('请填写单据编号');
                }
                $admin = Session::get('admin');

                $set= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'serial']))->value('is_checked');
                $batch= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'batch']))->value('is_checked');
                $auxiliary_status= Db::name('kuerp_sets')->where(['name'=>'auxiliary'])->value('is_checked');
                $data = [];
                $goods_serial_all = [];
                foreach ($params['goods_code'] as $k=>$v){
                    if(!empty($v)){//有产品编号
                        if(!isset($params['stores_out'][$k]) || empty($params['stores_out'][$k])){
                            $this->error('请选择调出仓库');
                        }else if(!isset($params['stores_enter'][$k]) || empty($params['stores_enter'][$k])){
                            $this->error('请选择调入仓库');
                        }else if($params['stores_out'][$k]==$params['stores_enter'][$k]){
                            $this->error('调出仓库不能和调入仓库相同');
                        }

                        $goods = Db::name('kuerp_goods')->where((new Goods())->setWhere(['goods_code'=>$v]))->field('id,serial,batch,auxiliary')->find();
                        if ($goods['auxiliary']==1 && empty($params['goods_attr'][$k]) && $auxiliary_status==1){
                            return ['status'=>500,'msg'=>'请选择商品'.$v.'属性'];
                        }
                        if($goods['batch']==1 && $batch==1){
                            if(empty($params['batch'][$k])){
                                $this->error('请选择生产批次');
                            }
                            $batch_data = Db::name('kuerp_goods_batch')->where((new Goods())->setWhere(['id'=>$params['batch_id'][$k]]))->find();
                            if(empty($batch_data)){
                                $this->error('生产批次不存在');
                            }
                        }

                        $serial_code = [];
                        if(isset($params['period_period'][$k]) && $goods['serial']==1 && $set==1){
                            if(empty($params['period_period'][$k])){
                                $this->error('请输入商品序列号');
                            }
                            $period_period = unserialize($params['period_period'][$k]);
                            foreach ($period_period as $period_period_k=>$period_period_v){
                                if(in_array($period_period_v['serial'],$goods_serial_all)){
                                    $this->error('商品序列号'.$period_period_v['serial'].'重复');
                                }else{
                                    $goods_serial_all[] = $period_period_v['serial'];
                                }
                                $serial_code[] = $period_period_v;
                            }
                        }
                        $data[] = [
                            'goods_id'          =>  $goods['id'],//商品信息
                            'goods_attr'        =>  (float)$params['goods_attr'][$k],//商品属性
                            'goods_unit'        =>  (float)$params['goods_unit'][$k],//商品单位
                            'stores_out'        =>  (float)$params['stores_out'][$k],//调出仓库
                            'stores_enter'      =>  (float)$params['stores_enter'][$k],//调入仓库
                            'number'            =>  (float)$params['number'][$k],//数量
                            'allow_stock'       =>  (float)$params['allow_stock'][$k],
                            'goods_batch_id'    =>  (float)$params['batch_id'][$k],
                            'goods_batch_name'  =>  $params['batch'][$k],
                            'goods_serial_id'   =>  !empty($serial_code)?serialize($serial_code):null,
                            'remarks'           =>  $params['remark'][$k],
                            'goods_note'        =>  $params['goods_note'][$k],
                        ];
                    }
                }
                if(!empty($data)){
                    $rowdata = [
                        'purchase_code'     =>  $params['purchase_code'],
                        'purchase_time'     =>  strtotime($params['purchase_time']),
                        'remarks'           =>  $params['note'],//备注
                        'preparation_id'    =>  $admin['id'],
                        'operation_id'      =>  $admin['id'],
                        'updatetime'        => time(),
                    ];
                }

                if($this->model->where((new Goods())->setWhere(['purchase_code'=>trim($params['purchase_code']),'id'=>['<>',$ids]]))->find()){
                    $this->error('单据编号已占用');
                }
                $result = false;
                if($examine!=1 && $row['examine_status']==1){
                    $this->examine($ids,2);
                }
                Db::startTrans();
                try {
                    $result = $this->model->where((new Goods())->setWhere(['id'=>$ids]))->update($rowdata);

                    if($result){
                        Db::name('kuerp_allocation_info')->where((new Goods())->setWhere(['allocation_id'=>$ids]))->delete();
                        foreach ($data as $data_k=>$data_v){
                            $data_v['allocation_id'] = $ids;
                            $data_v = (new Goods())->setDataInfo($data_v);
                            $data[$data_k] = $data_v;
                        }
                        Db::name('kuerp_allocation_info')->insertAll($data);
                    }
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    if($examine!=1){
                        try {
                            $exa = $this->examine($ids,1);
                        } catch (Exception $e) {
                        }
                        if(!isset($exa) || $exa['status']!=1){
                            $this->error(__('No rows were inserted'));
                        }
                    }
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);

        $info = Db::name('kuerp_allocation_info')->where((new Goods())->setWhere(['allocation_id'=>$ids]))->select();
        foreach ($info as $info_k=>$info_v){
            $info[$info_k]['unit'] = json_decode($this->getUnit('','','',$info_v['goods_id']),true);
            if(!empty($info_v['goods_serial_id'])){
                $info[$info_k]['serial'] = serialize($info_v['goods_serial_id']);
            }
            if(!empty($info_v['goods_batch_id'])){
                $goods_batch = Db::name('kuerp_goods_batch')->where((new Goods())->setWhere(['id'=>$info_v['goods_batch_id']]))->field('manufacture,guarantee,validity')->find();
                if($goods_batch){
                    $info[$info_k]['manufacture'] = $goods_batch['manufacture'];
                    $info[$info_k]['guarantee'] = $goods_batch['guarantee'];
                    $info[$info_k]['validity'] = $goods_batch['validity'];
                }
            }
        }
        $this->view->assign("info_list", $info);

        //审核禁止输入
        $examine_set= Db::name('kuerp_sets')->where(['name'=>'examine'])->value('is_checked');
        if($examine_set==1 && $row['examine_status']==1){
            $this->assign('exclass','hiddens');
        }
        $this->assignconfig("examine_status", $row['examine_status']);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->where((new Goods())->setWhere())->select();
            $examine= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'examine']))->value('is_checked');

            foreach ($list as $list_k=>$list_v){
                if($list_v['examine_status']==1){
                    (new \app\admin\controller\kuerp\Checkout())->checkOutTime($list_v['purchase_time']);
                }
                if($list_v['examine_status']==1 && $examine==1){
                    $this->error('不能删除已审核的数据');
                }
            }

            $count = 0;
            $error = [];
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    if ($v['examine_status']==1 && $examine!=1){
                        $status = $this->examine($list_v['id'],2);
                        if($status['status']==1){
                            $count += $v->delete();
                            Db::name('kuerp_allocation_info')->where((new Goods())->setWhere(['allocation_id'=>$v['id']]))->delete();
                        }else{
                            $error[] = $v['purchase_code'];
                        }
                    }else{
                        $count += $v->delete();
                        Db::name('kuerp_allocation_info')->where((new Goods())->setWhere(['allocation_id'=>$v['id']]))->delete();
                    }
                }
                Db::commit();
            }catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $msg = $count.'条数据删除成功';
                if(!empty($error)){
                    $error = implode(',',$error);
                    $msg .= $error.'删除失败';
                }
                $this->success($msg);
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    //审核/反审核
    public function examine($ids = null,$examine=1){
        $examine_set= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'examine']))->value('is_checked');
        if(empty($ids)){
            if($examine_set!=1){
                return ['status'=>0,'msg'=>'系统错误'];
            }
            $this->error('请选择要操作的数据');
        }

        //获取设置
        $set= \think\Db::name('kuerp_sets')->where((new Goods())->setWhere())->field('type,name,value,is_checked,set')->select();
        $setList = [];
        foreach ($set as $set_k=>$set_v){
            if(!empty($set_v['set'])){
                $set_v['set'] = unserialize($set_v['set']);
            }
            $setList[$set_v['name']] = $set_v;
        }
        //数量价格格式化
        $number_decimal = $setList['number_decimal']['value'];
        $price_decimal  = $setList['price_decimal']['value'];
        $batch   = $setList['batch']['is_checked'];
        $serial  = $setList['serial']['is_checked'];

        if($examine==1){
            $list = $this->model->where((new Goods())->setWhere(['id'=>['in',$ids],'examine_status'=>'0']))->select();
            foreach ($list as $list_k=>$list_v){
                (new \app\admin\controller\kuerp\Checkout())->checkOutTime($list_v['purchase_time']);
            }
            if(empty($list)){
                if($examine_set!=1){
                    return ['status'=>0,'msg'=>'请选择需要审核的数据'];
                }
                $this->error('请选择需要审核的数据');
            }

            $info_list = Db::name('kuerp_allocation_info')->where((new Goods())->setWhere(['allocation_id'=>['in',$ids]]))->select();

            $goods_stock_add = [];$goods_stock_edit = [];
            $goods_batch_add = [];$goods_batch_edit = [];
            $serial_edit = [];$goods_batch_log = [];
            $goods_stock_log = [];
            foreach ($info_list as $list_k=>$list_v){

                $goods = Db::name('kuerp_goods')->where((new Goods())->setWhere(['id'=>$list_v['goods_id']]))->field('id,goods_code,goods_name,attribute_id,auxiliary,serial,inventory,unit_id,batch')->find();
                //先通过单位计算需修改多单位商品的库存数量
                if($goods['unit_id']==$list_v['goods_unit']){
                    $stock_number = $list_v['number'];
                }else{
                    $unit_conversion = Db::name('kuerp_unit_conversion')->where((new Goods())->setWhere(['main_unit'=>$goods['unit_id'],'vice_unit'=>$list_v['goods_unit']]))->value('advance_rate');
                    if(empty($unit_conversion)){
                        if($examine_set!=1){
                            return ['status'=>0,'msg'=>'商品单位信息查询失败'];
                        }
                        $this->error('商品单位信息查询失败');
                    }else{
                        $stock_number = round(($list_v['number']*$unit_conversion),$number_decimal);
                    }
                }
                if($goods['auxiliary']==1 && empty($list_v['goods_attr'])){
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>'请选择商品属性'];
                    }
                    $this->error('请选择商品属性');
                }
                if(!empty($list_v['goods_attr'])){
                    $attr_id = $list_v['goods_attr'];
                }
                //查询调出仓库库存
                $where = [];
                $where['goods_id'] = $goods['id'];
                if(isset($attr_id)){
                    $where['attribute_id'] = $attr_id;
                }
                $where['stores_id'] = $list_v['stores_out'];
                $where = (new Goods())->setWhere($where);
                $goods_stock = Db::name('kuerp_goods_stock')->where($where)->find();
                //减调出仓库库存
                if(empty($goods_stock)){
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'库存不足'];
                    }
                    $this->error('商品'.$goods['goods_name'].'库存不足');
                }else if($goods_stock['stock']<$stock_number){
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'库存不足'];
                    }
                    $this->error('商品'.$goods['goods_name'].'库存不足');
                }else{
                    if(isset($goods_stock_edit[$goods_stock['id']])){
                        $goods_stock_edit[$goods_stock['id']] -= $stock_number;
                    }else{
                        $goods_stock_edit[$goods_stock['id']] = $goods_stock['stock']-$stock_number;
                    }
                }
                //收发详情
                $allocation = Db::name('kuerp_allocation')->where((new Goods())->setWhere(['id'=>$list_v['allocation_id']]))->field('purchase_code,purchase_time,remarks')->find();
                //获取成本单价
                $balance_unit=0;
                $goodsInfo = json_decode((new GoodsPurchase())->getUnit($goods['goods_code'],$list_v['stores_out'],$list_v['goods_attr']),true);
                $unitData = isset($goodsInfo['unit'])?$goodsInfo['unit']:[];
                if(empty($unitData)){
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>'商品信息获取错误'];
                    }
                    $this->error('商品信息获取错误');
                }else{
                    foreach ($unitData as $unitData_k=>$unitData_v){
                        if($unitData_v['id']==$goods['unit_id']){
                            $balance_unit = $unitData_v['purchase'];
                        }
                    }
                }
                //获取成本单价结束
                if($goods){
                    //调出
                    $goods_stock_log[] = (new Goods())->setDataInfo([
                        'purchase_code' => $allocation['purchase_code'],
                        'purchase_id'   => $list_v['id'],
                        'purchase_time' => $allocation['purchase_time'],
                        'business_type' => '6',
                        'goods_id'      => $goods['id'],
                        'goods_code'    => $goods['goods_code'],
                        'attribute_id'  => (float)$list_v['goods_attr'],
                        'goods_unit'    => (float)$list_v['goods_unit'],
                        'stores_id'     => (float)$list_v['stores_out'],
                        'remark'        => $list_v['remarks'],
                        'enter_number'  => null,
                        'enter_basics'  => null,
                        'enter_unit'    => null,
                        'enter_cost'    => null,
                        'out_number'    => (float)$list_v['number'],
                        'out_basics'    => (float)$stock_number,
                        'out_unit'      => $balance_unit,
                        'out_cost'      => round($balance_unit*$stock_number,4),
                        'createtime'    => time(),
                        'updatetime'    => time(),
                    ]);
                    //调入
                    $goods_stock_log[] = (new Goods())->setDataInfo([
                        'purchase_code' => $allocation['purchase_code'],
                        'purchase_id'   => $list_v['id'],
                        'purchase_time' => $allocation['purchase_time'],
                        'business_type' => '6',
                        'goods_id'      => $goods['id'],
                        'goods_code'    => $goods['goods_code'],
                        'attribute_id'  => (float)$list_v['goods_attr'],
                        'goods_unit'    => (float)$list_v['goods_unit'],
                        'stores_id'     => (float)$list_v['stores_enter'],
                        'remark'        => $list_v['remarks'],
                        'enter_number'  => (float)$list_v['number'],
                        'enter_basics'  => (float)$stock_number,
                        'enter_unit'    => $balance_unit,
                        'enter_cost'    => round($balance_unit*$stock_number,4),
                        'out_number'    => null,
                        'out_basics'    => null,
                        'out_unit'      => null,
                        'out_cost'      => null,
                        'createtime'    => time(),
                        'updatetime'    => time(),
                    ]);
                }
                //收发详情结束
                //调入仓库
                $where = [];
                $where['goods_id'] = $goods['id'];
                if(isset($attr_id)){
                    $where['attribute_id'] = $attr_id;
                }
                $where['stores_id'] = $list_v['stores_enter'];
                $where = (new Goods())->setWhere($where);
                $goods_stock = Db::name('kuerp_goods_stock')->where($where)->find();
                if(empty($goods_stock)){//调入仓库无数据,新增仓储数据
                    $where['stores_id'] = $list_v['stores_out'];
                    $goods_stocks = Db::name('kuerp_goods_stock')->where($where)->find();
                    if(isset($goods_stock_add[$goods_stocks['id']])){
                        $goods_stock_add[$goods_stocks['id']]['stock'] += $stock_number;
                    }else{
                        $goods_stocks_id = $goods_stocks['id'];
                        unset($goods_stocks['id']);
                        $goods_stocks['stock'] = $stock_number;
                        $goods_stocks['stores_id'] = $list_v['stores_enter'];
                        $goods_stocks['createtime'] = time();
                        $goods_stocks['updatetime'] = time();
                        $goods_stock_add[$goods_stocks_id] = $goods_stocks;
                    }
                }else{
                    if(isset($goods_stock_edit[$goods_stock['id']])){
                        $goods_stock_edit[$goods_stock['id']] += $stock_number;
                    }else{
                        $goods_stock_edit[$goods_stock['id']] = $goods_stock['stock']+$stock_number;
                    }
                }
                foreach ($goods_stock_edit as $k=>$v){
                    if($v<0){
                        if($examine_set!=1){
                            return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'库存不足'];
                        }
                        $this->error('商品'.$goods['goods_name'].'库存不足');
                    }
                }

                if($goods['batch']==1 && $batch==1){//启用批次管理

                    //调出商品批次跟踪
                    $allocation = Db::name('kuerp_allocation')->where((new Goods())->setWhere(['id'=>$list_v['allocation_id']]))->field('purchase_code,purchase_time,remarks')->find();
                    $goods_batch_log[] = (new Goods())->setDataInfo([
                        'goods_id'      => $goods['id'],
                        'goods_unit'    => $list_v['goods_unit'],
                        'stores_id'     => $list_v['stores_out'],
                        'batch_id'      => (float)$list_v['goods_batch_id'],
                        'number'        => (float)$list_v['number'],
                        'purchase_code' => $allocation['purchase_code'],
                        'purchase_time' => $allocation['purchase_time'],
                        'business_type' => '6',
                        'remarks'       => $allocation['remarks'],
                        'order_id'      => $list_v['id'],
                        'createtime'    => time(),
                        'updatetime'    => time(),
                    ]);
                    //调出商品批次跟踪结束
                    //调入商品批次跟踪
                    $goods_batch_log[] = (new Goods())->setDataInfo([
                        'goods_id'      => $goods['id'],
                        'goods_unit'    => $list_v['goods_unit'],
                        'stores_id'     => $list_v['stores_enter'],
                        'batch_id'      => (float)$list_v['goods_batch_id'],
                        'number'        => (float)$list_v['number'],
                        'purchase_code' => $allocation['purchase_code'],
                        'purchase_time' => $allocation['purchase_time'],
                        'business_type' => '6',
                        'remarks'       => $allocation['remarks'],
                        'order_id'      => $list_v['id'],
                        'createtime'    => time(),
                        'updatetime'    => time(),
                    ]);
                    //调入商品批次跟踪结束

                    $where = [];
                    $where['goods_id'] = $goods['id'];
                    if(!empty($list_v['goods_attr'])){
                        $where['attribute_id'] = $list_v['goods_attr'];
                    }
                    $where['stores_id'] = $list_v['stores_out'];
                    $where['batch_id'] = $list_v['goods_batch_id'];
                    $where = (new Goods())->setWhere($where);
                    $goods_batch_stock = Db::name('kuerp_goods_batch_stock')->where($where)->find();
                    if(empty($goods_batch_stock)){
                        if($examine_set!=1){
                            return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'批次信息查询失败'];
                        }
                        $this->error('商品'.$goods['goods_name'].'批次信息查询失败');
                    }else if($goods_batch_stock['stock']<$stock_number){
                        if($examine_set!=1){
                            return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'批次'.$list_v['goods_batch_name'].'库存不足'];
                        }
                        $this->error('商品'.$goods['goods_name'].'批次'.$list_v['goods_batch_name'].'库存不足');
                    }else{
                        if(isset($goods_batch_edit[$goods_batch_stock['id']])){
                            $goods_batch_edit[$goods_batch_stock['id']] -= $stock_number;
                        }else{
                            $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']-$stock_number;
                        }
                    }
                    foreach ($goods_batch_edit as $k=>$v){
                        if($v<0){
                            if($examine_set!=1){
                                return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'批次'.$list_v['goods_batch_name'].'库存不足'];
                            }
                            $this->error('商品'.$goods['goods_name'].'批次'.$list_v['goods_batch_name'].'库存不足');
                        }
                    }

                    $where['stores_id'] = $list_v['stores_enter'];
                    $goods_batch_stock = Db::name('kuerp_goods_batch_stock')->where($where)->find();
                    if(empty($goods_batch_stock)){//调入批次仓库无数据,新增仓储数据
                        $where['stores_id'] = $list_v['stores_out'];
                        $goods_stocks = Db::name('kuerp_goods_batch_stock')->where($where)->find();
                        if(isset($goods_batch_add[$goods_stocks['id']])){
                            $goods_batch_add[$goods_stocks['id']]['stock'] += $stock_number;
                        }else{
                            $goods_stocks_id = $goods_stocks['id'];
                            unset($goods_stocks['id']);
                            $goods_stocks['stock'] = $stock_number;
                            $goods_stocks['stores_id'] = $list_v['stores_enter'];
                            $goods_stocks['createtime'] = time();
                            $goods_stocks['updatetime'] = time();
                            $goods_batch_add[$goods_stocks_id] = $goods_stocks;
                        }
                    }else{
                        if(isset($goods_batch_edit[$goods_batch_stock['id']])){
                            $goods_batch_edit[$goods_batch_stock['id']] += $stock_number;
                        }else{
                            $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']+$stock_number;
                        }
                    }
                }
                if($goods['serial']==1 && $serial==1){//启用序列号管理
                    if(empty($list_v['goods_serial_id'])){
                        if($examine_set!=1){
                            return ['status'=>0,'msg'=>'请选择序列号'];
                        }
                        $this->error('请选择序列号');
                    }
                    $goods_serial = unserialize($list_v['goods_serial_id']);
                    $serial_number = [];
                    foreach ($goods_serial as $goods_serial_k=>$goods_serial_v){
                        $serial_number[] = $goods_serial_v['serial'];
                    }
                    $where = [];
                    $where['goods_id'] = $goods['id'];
                    $where['attribute_id'] = $list_v['goods_attr'];
                    $where['stores_id'] = $list_v['stores_out'];
                    $where['serial'] = ['in',$serial_number];
                    $where['store_status'] = '1';
                    $where = (new Goods())->setWhere($where);
                    $goods_number_stock = Db::name('kuerp_goods_serial')->where($where)->column('id');//需修改仓库的序列号ID
                    if(sizeof($goods_number_stock)!=sizeof($serial_number)){
                        if($examine_set!=1){
                            return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'序列号存在未在库,请重新选择'];
                        }
                        $this->error('商品'.$goods['goods_name'].'序列号存在未在库,请重新选择');
                    }
                    foreach ($goods_number_stock as $k=>$v){
                        if(!isset($serial_edit[$v])){
                            $serial_edit[$v] = ['id'=>$v,'stores_id'=>$list_v['stores_enter']];
                        }
                    }
                }
            }

            $admin = Session::get('admin');
            Db::startTrans();
            try {
                if(!empty($goods_stock_log)){
                    Db::name('kuerp_goods_log')->insertAll($goods_stock_log);
                }
                if(!empty($goods_batch_log)){
                    Db::name('kuerp_goods_batch_log')->insertAll($goods_batch_log);
                }

                $result = $this->model->where((new Goods())->setWhere(['id'=>['in',$ids],'examine_status'=>'0']))
                    ->update(['examine_status'=>1,'examine_id'=>$admin['id'],'examine_time'=>time(),'operation_id'=>$admin['id']]);
                if(!empty($goods_stock_add)){
                    Db::name('kuerp_goods_stock')->insertAll($goods_stock_add);
                }
                if(!empty($goods_stock_edit)){
                    foreach ($goods_stock_edit as $k=>$v){
                        Db::name('kuerp_goods_stock')->where((new Goods())->setWhere(['id'=>$k]))->update(['stock'=>$v]);
                    }
                }
                if(!empty($goods_batch_add)){
                    Db::name('kuerp_goods_batch_stock')->insertAll($goods_batch_add);
                }
                if(!empty($goods_batch_edit)){
                    foreach ($goods_batch_edit as $k=>$v){
                        Db::name('kuerp_goods_batch_stock')->where((new Goods())->setWhere(['id'=>$k]))->update(['stock'=>$v]);
                    }
                }
                if(!empty($serial_edit)){
                    (new \app\admin\model\kuerp\GoodsSerial())->saveAll($serial_edit);
                }
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if(isset($result) && $result !== false){
                if($examine_set!=1){
                    return ['status'=>1,'msg'=>'操作成功'];
                }
                $this->success(sizeof($list).'条数据审核成功');
            }else{
                if($examine_set!=1){
                    return ['status'=>0,'msg'=>'操作失败'];
                }
                $this->error('审核失败');
            }
        }else{
            $list = $this->model->where((new Goods())->setWhere(['id'=>['in',$ids],'examine_status'=>'1']))->select();
            foreach ($list as $list_k=>$list_v){
                (new \app\admin\controller\kuerp\Checkout())->checkOutTime($list_v['purchase_time']);
            }
            if(empty($list)){
                if($examine_set!=1){
                    return ['status'=>0,'msg'=>'操作失败'];
                }
                $this->error('请选择需要反审核的数据');
            }
            $info_list = Db::name('kuerp_allocation_info')->where((new Goods())->setWhere(['allocation_id'=>['in',$ids]]))->select();

            $goods_stock_add = [];$goods_stock_edit = [];
            $goods_batch_add = [];$goods_batch_edit = [];
            $serial_edit = [];$info_ids = [];
            foreach ($info_list as $list_k=>$list_v){
                $info_ids[] = $list_v['id'];
                $goods = Db::name('kuerp_goods')->where((new Goods())->setWhere(['id'=>$list_v['goods_id']]))->field('auxiliary,serial,id,goods_name,attribute_id,inventory,unit_id,batch')->find();
                //先通过单位计算需修改多单位商品的库存数量
                if($goods['unit_id']==$list_v['goods_unit']){
                    $stock_number = $list_v['number'];
                }else{
                    $unit_conversion = Db::name('kuerp_unit_conversion')->where((new Goods())->setWhere(['main_unit'=>$goods['unit_id'],'vice_unit'=>$list_v['goods_unit']]))->value('advance_rate');
                    if(empty($unit_conversion)){
                        if($examine_set!=1){
                            return ['status'=>0,'msg'=>'商品单位信息查询失败'];
                        }
                        $this->error('商品单位信息查询失败');
                    }else{
                        $stock_number = round(($list_v['number']*$unit_conversion),$number_decimal);
                    }
                }
                if($goods['auxiliary']==1 && empty($list_v['goods_attr'])){
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>'请选择商品属性'];
                    }
                    $this->error('请选择商品属性');
                }
                if(!empty($list_v['goods_attr'])){
                    $attr_id = $list_v['goods_attr'];
                }
                //查询调入仓库库存
                $where = [];
                $where['goods_id'] = $goods['id'];
                if(isset($attr_id)){
                    $where['attribute_id'] = $attr_id;
                }
                $where['stores_id'] = $list_v['stores_enter'];
                $where = (new Goods())->setWhere($where);
                $goods_stock = Db::name('kuerp_goods_stock')->where($where)->find();
                //减调入仓库库存
                if(empty($goods_stock)){
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'库存不足'];
                    }
                    $this->error('商品'.$goods['goods_name'].'库存不足');
                }else if($goods_stock['stock']<$stock_number){
                    if($examine_set!=1){
                        return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'库存不足'];
                    }
                    $this->error('商品'.$goods['goods_name'].'库存不足');
                }else{
                    if(isset($goods_stock_edit[$goods_stock['id']])){
                        $goods_stock_edit[$goods_stock['id']] -= $stock_number;
                    }else{
                        $goods_stock_edit[$goods_stock['id']] = $goods_stock['stock']-$stock_number;
                    }
                }
                //调出仓库
                $where = [];
                $where['goods_id'] = $goods['id'];
                if(isset($attr_id)){
                    $where['attribute_id'] = $attr_id;
                }
                $where['stores_id'] = $list_v['stores_out'];
                $where = (new Goods())->setWhere($where);
                $goods_stock = Db::name('kuerp_goods_stock')->where($where)->find();
                if(empty($goods_stock)){//调出仓库无数据,新增仓储数据
                    $where['stores_id'] = $list_v['stores_enter'];
                    $goods_stocks = Db::name('kuerp_goods_stock')->where($where)->find();
                    if(isset($goods_stock_add[$goods_stocks['id']])){
                        $goods_stock_add[$goods_stocks['id']]['stock'] += $stock_number;
                    }else{
                        $goods_stocks_id = $goods_stocks['id'];
                        unset($goods_stocks['id']);
                        $goods_stocks['stock'] = $stock_number;
                        $goods_stocks['stores_id'] = $list_v['stores_out'];
                        $goods_stocks['createtime'] = time();
                        $goods_stocks['updatetime'] = time();
                        $goods_stock_add[$goods_stocks_id] = $goods_stocks;
                    }
                }else{
                    if(isset($goods_stock_edit[$goods_stock['id']])){
                        $goods_stock_edit[$goods_stock['id']] += $stock_number;
                    }else{
                        $goods_stock_edit[$goods_stock['id']] = $goods_stock['stock']+$stock_number;
                    }
                }
                foreach ($goods_stock_edit as $k=>$v){
                    if($v<0){
                        if($examine_set!=1){
                            return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'库存不足'];
                        }
                        $this->error('商品'.$goods['goods_name'].'库存不足');
                    }
                }

                if($goods['batch']==1 && $batch==1){//启用批次管理
                    $where = [];
                    $where['goods_id'] = $goods['id'];
                    if(!empty($list_v['goods_attr'])){
                        $where['attribute_id'] = $list_v['goods_attr'];
                    }
                    $where['stores_id'] = $list_v['stores_enter'];
                    $where['batch_id'] = $list_v['goods_batch_id'];
                    $where = (new Goods())->setWhere($where);
                    $goods_batch_stock = Db::name('kuerp_goods_batch_stock')->where($where)->find();
                    if(empty($goods_batch_stock)){
                        if($examine_set!=1){
                            return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'批次信息查询失败'];
                        }
                        $this->error('商品'.$goods['goods_name'].'批次信息查询失败');
                    }else if($goods_batch_stock['stock']<$stock_number){
                        if($examine_set!=1){
                            return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'批次'.$list_v['goods_batch_name'].'库存不足'];
                        }
                        $this->error('商品'.$goods['goods_name'].'批次'.$list_v['goods_batch_name'].'库存不足');
                    }else{
                        if(isset($goods_batch_edit[$goods_batch_stock['id']])){
                            $goods_batch_edit[$goods_batch_stock['id']] -= $stock_number;
                        }else{
                            $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']-$stock_number;
                        }
                    }
                    foreach ($goods_batch_edit as $k=>$v){
                        if($v<0){
                            if($examine_set!=1){
                                return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'批次'.$list_v['goods_batch_name'].'库存不足'];
                            }
                            $this->error('商品'.$goods['goods_name'].'批次'.$list_v['goods_batch_name'].'库存不足');
                        }
                    }

                    $where['stores_id'] = $list_v['stores_out'];
                    $goods_batch_stock = Db::name('kuerp_goods_batch_stock')->where($where)->find();
                    if(empty($goods_batch_stock)){//调出批次仓库无数据,新增仓储数据
                        $where['stores_id'] = $list_v['stores_enter'];
                        $goods_stocks = Db::name('kuerp_goods_batch_stock')->where($where)->find();
                        if(isset($goods_batch_add[$goods_stocks['id']])){
                            $goods_batch_add[$goods_stocks['id']]['stock'] += $stock_number;
                        }else{
                            $goods_stocks_id = $goods_stocks['id'];
                            unset($goods_stocks['id']);
                            $goods_stocks['stock'] = $stock_number;
                            $goods_stocks['stores_id'] = $list_v['stores_out'];
                            $goods_stocks['createtime'] = time();
                            $goods_stocks['updatetime'] = time();
                            $goods_batch_add[$goods_stocks_id] = $goods_stocks;
                        }
                    }else{
                        if(isset($goods_batch_edit[$goods_batch_stock['id']])){
                            $goods_batch_edit[$goods_batch_stock['id']] += $stock_number;
                        }else{
                            $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']+$stock_number;
                        }
                    }
                }
                if($goods['serial']==1 && $serial==1){//启用序列号管理
                    if(empty($list_v['goods_serial_id'])){
                        if($examine_set!=1){
                            return ['status'=>0,'msg'=>'请选择序列号'];
                        }
                        $this->error('请选择序列号');
                    }
                    $goods_serial = unserialize($list_v['goods_serial_id']);
                    $serial_number = [];
                    foreach ($goods_serial as $goods_serial_k=>$goods_serial_v){
                        $serial_number[] = $goods_serial_v['serial'];
                    }
                    $where = [];
                    $where['goods_id'] = $goods['id'];
                    $where['attribute_id'] = $list_v['goods_attr'];
                    $where['stores_id'] = $list_v['stores_enter'];
                    $where['serial'] = ['in',$serial_number];
                    $where['store_status'] = '1';
                    $where = (new Goods())->setWhere($where);
                    $goods_number_stock = Db::name('kuerp_goods_serial')->where($where)->column('id');//需修改仓库的序列号ID
                    if(sizeof($goods_number_stock)!=sizeof($serial_number)){
                        if($examine_set!=1){
                            return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'序列号存在未在库,请重新选择'];
                        }
                        $this->error('商品'.$goods['goods_name'].'序列号存在未在库,请重新选择');
                    }
                    foreach ($goods_number_stock as $k=>$v){
                        if(!isset($serial_edit[$v])){
                            $serial_edit[$v] = ['id'=>$v,'stores_id'=>$list_v['stores_out']];
                        }
                    }
                }
            }

            $admin = Session::get('admin');
            Db::startTrans();
            try {
                //收发详情
                if(!empty($info_ids)){
                    Db::name('kuerp_goods_log')->where((new Goods())->setWhere(['business_type'=>'6','purchase_id'=>['in',$info_ids]]))->delete();
                }
                //删除批次跟踪
                if(!empty($info_ids)){
                    Db::name('kuerp_goods_batch_log')->where((new Goods())->setWhere(['business_type'=>'6','order_id'=>['in',$info_ids]]))->delete();
                }

                $result = $this->model->where((new Goods())->setWhere(['id'=>['in',$ids],'examine_status'=>'1']))
                    ->update(['examine_status'=>'0','examine_id'=>'','examine_time'=>'','operation_id'=>$admin['id']]);
                if(!empty($goods_stock_add)){
                    Db::name('kuerp_goods_stock')->insertAll($goods_stock_add);
                }
                if(!empty($goods_stock_edit)){
                    foreach ($goods_stock_edit as $k=>$v){
                        Db::name('kuerp_goods_stock')->where((new Goods())->setWhere(['id'=>$k]))->update(['stock'=>$v]);
                    }
                }
                if(!empty($goods_batch_add)){
                    Db::name('kuerp_goods_batch_stock')->insertAll($goods_batch_add);
                }
                if(!empty($goods_batch_edit)){
                    foreach ($goods_batch_edit as $k=>$v){
                        Db::name('kuerp_goods_batch_stock')->where((new Goods())->setWhere(['id'=>$k]))->update(['stock'=>$v]);
                    }
                }
                if(!empty($serial_edit)){
                    (new \app\admin\model\kuerp\GoodsSerial())->saveAll($serial_edit);
                }
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if(isset($result) && $result !== false){
                if($examine_set!=1){
                    return ['status'=>1,'msg'=>'操作成功'];
                }
                $this->success(sizeof($list).'条数据反审核成功');
            }else{
                if($examine_set!=1){
                    return ['status'=>0,'msg'=>'操作失败'];
                }
                $this->error('反审核失败');
            }
        }
    }
    //获取单位价格信息
    public function getUnit($goods_code='',$store='',$attr='',$goods_id=''){
        if($goods_code){
            $row = Db::name('kuerp_goods')->where((new Goods())->setWhere(['goods_code'=>$goods_code]))
                ->field('id,note,market_sell_price,wholesale_price,vip_price,discount,discount2,batch,serial,id,auxiliary,unit_id,more_unit,more_unit_id,purchase,goods_code,manager_warehouse_id,batch,shelf_life')
                ->find();
        }else{
            $row = Db::name('kuerp_goods')->where((new Goods())->setWhere(['id'=>$goods_id]))
                ->field('id,note,market_sell_price,wholesale_price,vip_price,discount,discount2,batch,serial,auxiliary,unit_id,more_unit,more_unit_id,purchase,goods_code,manager_warehouse_id,batch,shelf_life')
                ->find();
        }
        $goods_id = isset($row['id'])?$row['id']:0;
        if(empty($row)){
            return [];
        }

        $auxiliary = Db::name('kuerp_auxiliary')->where((new Goods())->setWhere(['status'=>1]))->column('name','id');
        $goods_attr = Db::name('kuerp_goods_attr')->where((new Goods())->setWhere(['goods_id'=>$goods_id]))->select();
        foreach ($goods_attr as $goods_attr_k=>$goods_attr_v){
            $aux_combination = Db::name('kuerp_aux_combination')->where((new Goods())->setWhere(['id'=>$goods_attr_v['attribute_id']]))->value('attr_ids');
            $goods_attr[$goods_attr_k]['attr_ids'] = [];
            $goods_attr[$goods_attr_k]['attr_name'] = '';
            if($aux_combination){
                $goods_attr[$goods_attr_k]['attr_id'] = $aux_combination;
                $aux_combination = explode('&',$aux_combination);
                $goods_attr[$goods_attr_k]['attr_ids'] = $aux_combination;
                foreach ($aux_combination as $aux_combination_k=>$aux_combination_v){
                    $aux_combination[$aux_combination_k] = $auxiliary[$aux_combination_v];
                }
                $aux_combination = implode('/',$aux_combination);
                $goods_attr[$goods_attr_k]['attr_name'] = $aux_combination;
            }
        }


        $where = [];
        $where['goods_id'] = $goods_id;
        if($store){
            $where['stores_id'] = $store;
        }else{
            $where['stores_id'] = $row['manager_warehouse_id'];
        }
        if($attr){
            $attribute_id = Db::name('kuerp_goods_attr')->where((new Goods())->setWhere(['goods_id'=>$goods_id,'id'=>$attr]))->value('attribute_id');
            $where['attribute_id'] = $attribute_id;
        }
        $where = (new Goods())->setWhere($where);
        $stock = Db::name('kuerp_goods_stock')->where($where)->value('stock');
        $row['stock'] = $stock;
        if($row['more_unit']==1){
            $unit = Db::name('kuerp_unit_conversion')->alias('uc')
                ->join('kuerp_unit u','u.id=uc.vice_unit')
                ->where((new Goods())->setWhere(['uc.main_unit'=>$row['unit_id']],'uc'))
                ->order('uc.advance_rate','ASC')
                ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')
                ->select();
            $unit_ids = [$row['unit_id']];
            foreach ($unit as $unit_k=>$unit_v){
                $unit_ids[] = $unit_v['id'];
            }
        }else{
            $unit_ids = [$row['unit_id']];
        }

        $unit = Db::name('kuerp_unit')->where((new Goods())->setWhere(['id'=>['in',$unit_ids]]))->field('id,name')->select();
        return json_encode(['unit'=>$unit,'attr'=>$goods_attr,'goods'=>$row]);
    }
    /**
     * 获取可用序列号
     * @param string goods_code 商品编号
     * @param string stores_id 仓库ID
     * @param string attribute_id 属性id
     * @return array
     */
    public function getSerial(){
        $data = input();
        $goods_code    = isset($data['goods_code'])?$data['goods_code']:'';
        $stores_id     = isset($data['stores_id'])?$data['stores_id']:'';
        $attribute_id  = isset($data['attribute_id'])?$data['attribute_id']:'';
        $where = [];
        $where['store_status'] = '1';
        $where['goods_code'] = $goods_code;
        $where['stores_id'] = $stores_id;
        if($attribute_id){
            $where['attribute_id'] = $attribute_id;
        }
        $where = (new Goods())->setWhere($where);
        $serial = Db::name('kuerp_goods_serial')->where($where)->field('serial,remark,status')->select();
        return json_encode(['list'=>$serial]);
    }
}