<?php

namespace App\Http\Controllers\Api\BackEnd;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use App\Http\Models\Api\Export;
use App\Http\Models\Api\StockSellStorageGoods;
use App\Http\Models\Api\Common;
use App\Http\Controllers\Api\YongYou\YongYouController;
use Illuminate\Support\Facades\Crypt;
use App\Http\Models\Api\OutinRecord;
use App\Http\Models\Api\YongYou;
use App\Http\Models\Api\YongYous;
use App\Http\Models\BackEnd\BackEnd;

class BackEndTestController extends Controller
{
    // 初始密码
    protected $password = 123456;
    protected $now_time = '2021-06-01 15:30:16';
    protected $date = '2020-09';
    protected $show_count = 10;

    public function test(Request $request)
    {
        // $this->enterssellssaves($request);//计算进销存 001(不需要运行)
        // $this->selectDataStatistical($request);//查询销售统计-销售明细界面的成本为0,跟进销存成本对比,不一致的查询出来 002
        // $this->adjustRecedeVoucher();//修改退货单1-3月份未同步U8的凭证
        // $this->adjustVirtualFranchises();//查询虚拟加盟店凭证金额跟工单金额不一致的数据
        // $this->adjustRecedeVoucher2();//查询退货单类型凭证金额跟订单类型退货单金额不一致的数据
        // $this->selectWarehouseGoods($request);//统计仓库库存数量和进销存库存数量不一致的数据 07
        // $this->StocktakeVoucher();// 修改盘库单凭证成
        // $this->getfalsedata($request);//计算期末金额 与 平均价*库存不相等
        // $this->syncOrder($request);//查询凭证列表已同步U8成本和销售统计成本不一致的 003
        // $this->dataStatistics($request);//统计凭证列表成本和销售统计成本不一致的 004
        // $this->editVoucher();//根据销售统计成本修改凭证的成本 005
        // $this->editentererrordata();//修改进销存数据
    }

    // 计算进销存  【21.07.02】
    public function enterssellssaves($request)
    {
        ignore_user_abort();
        set_time_limit(0);

        $oure_shop_idarray[] = '1';
        $oure_shop_idarray[] = '2';
        $oure_shop_idarray[] = '26';
        // 厦门固定仓库
        $ware_id = array('5', '7', '9', '10', '11', '13', '14', '42', '53', '83', '91', '96', '121', '122', '129', '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '140', '141', '143', '150', '151', '152', '153', '154', '155', '156', '181', '189', '190', '191', '200', '201', '202', '203', '204', '205', '206', '209',
            '210', '212', '213', '214', '215');
        $a = "0";
        $id1 = $request['id1']; //3.1  106100
        $id2 = $request['id2']; //3.1  106100
        $unit = '';
        $result = DB::table('outin_record')
            ->whereIn('oure_shop_id', $oure_shop_idarray)
            ->where(function ($query) use ($id1) {
                $query->where('oure_id', '>=', $id1);
            })
            ->where(function ($query) use ($id2) {
                $query->where('oure_id', '<=', $id2);
            })
            ->select('oure_id', 'oure_shop_id', 'oure_original_correlation_number', 'oure_original_number', 'oure_in_ware_id', 'oure_out_ware_id', 'oure_type', 'oure_out_in_type', 'oure_waon_class', 'oure_create_time')
            ->orderBy('oure_create_time', 'asc')
            ->get()->toArray();
        $result = json_decode(json_encode($result), true);
        foreach ($result as $k => $v) {
            $goo_result = DB::table('outin_details')
                ->where('oude_oure_id', $v['oure_id'])
                ->select('oude_oure_id', 'oude_goo_id', 'oude_goo_number', 'oude_type')
                ->get()->toArray();
            $goo_result = json_decode(json_encode($goo_result), true);
            $oure_create_time = $v['oure_create_time'];
            $oure_shop_id = $v['oure_shop_id'];
            $oure_original_correlation_number = $v['oure_original_correlation_number'];
            $oure_in_ware_id = $v['oure_in_ware_id'];
            $oure_out_ware_id = $v['oure_out_ware_id'];
            $oure_type = $v['oure_type'];
            $oure_out_in_type = $v['oure_out_in_type'];
            $oure_waon_class = $v['oure_waon_class'];
            if ($oure_waon_class == '2') {
                $oure_waon_class = '1';
            }
            // 入库id-》高献昌
            if ($oure_out_in_type == '2') {
                $oure_ware_id = $oure_in_ware_id;
            } else { // 出库
                $oure_ware_id = $oure_out_ware_id;
            }
            if ($v['oure_original_correlation_number'] == '' && $v['oure_shop_id'] == '26') {
                // $enters_sellsarray['ense_goo_id']= $value;//商品id
                $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                $enters_sellsarray['ense_order_number'] = $v['oure_original_correlation_number'];
                $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                $enters_sellsarray['ense_time'] = $oure_create_time;
                $enters_saves = DB::table('enters_sells_savesbak')->insertGetId($enters_sellsarray);
                continue;
            }

            foreach ($goo_result as $kk => $vv) {
                $value = $vv['oude_goo_id'];
                $goo_quantity = $vv['oude_goo_number'];
                $a = $a + 1;
                unset($enters_sellsarray);

                // 商品信息
                $goods = DB::table('goods')
                    ->where('goo_id', $value)
                    ->select('goo_name', 'goo_goods_encode', 'goo_ampere_hour', 'goo_attribute', 'goo_recycle_price')
                    ->first();

                // 当前成本价
                $datetime = date('Y-m-d H:i:s');
                $newprice = DB::table('enters_sells_savess')
                    ->where('ense_goo_id', $value)
                    ->where('ense_goo_warehouse', $oure_ware_id)
                    ->where('ense_goo_waretype', $oure_waon_class)
                    ->orderBy('ense_id', 'desc')
                    ->select('ense_end_price', 'ense_end_amount', 'ense_end_inventory')
                    ->first();
                if ($newprice) {
                    $new_nums = $newprice->ense_end_inventory;
                    if ($newprice->ense_end_price == 0) {
                        $ense_new_price = DB::table('enters_goods_price')
                            ->where('ense_goo_number', $goods->goo_goods_encode)
                            ->select('ense_new_price')
                            ->first();
                        if ($ense_new_price) {
                            if ($ense_new_price->ense_new_price == 0) {
                                $ense_new_price = DB::table('enters_goods_price')
                                    ->where('ense_goo_encode', $goods->goo_goods_encode)
                                    ->value('ense_new_price');
                                if ($ense_new_price) {
                                    $ense_end_price = $ense_new_price;
                                    // 当前总金额
                                    $ense_new_amount = $ense_new_price * $newprice->ense_end_inventory;
                                } else {
                                    $ense_new_price = DB::table('enters_goods_price')
                                        ->where('ense_goods_code', $goods->goo_goods_encode)
                                        ->value('ense_new_price');
                                    if ($ense_new_price) {
                                        $ense_end_price = $ense_new_price;
                                        // 当前总金额
                                        $ense_new_amount = $ense_new_price * $newprice->ense_end_inventory;
                                    } else {
                                        $ense_end_price = '0';
                                        $ense_new_amount = '0';
                                    }
                                }
                            } else {
                                $ense_end_price = $ense_new_price->ense_new_price;
                                // 当前总金额
                                $ense_new_amount = $ense_new_price->ense_new_price * $newprice->ense_end_inventory;
                            }
                        } else {
                            $ense_new_price = DB::table('enters_goods_price')
                                ->where('ense_goo_encode', $goods->goo_goods_encode)
                                ->value('ense_new_price');
                            if ($ense_new_price) {
                                $ense_end_price = $ense_new_price;
                                // 当前总金额
                                $ense_new_amount = $ense_new_price * $newprice->ense_end_inventory;
                            } else {
                                $ense_new_price = DB::table('enters_goods_price')
                                    ->where('ense_goods_code', $goods->goo_goods_encode)
                                    ->value('ense_new_price');
                                if ($ense_new_price) {
                                    $ense_end_price = $ense_new_price;
                                    // 当前总金额
                                    $ense_new_amount = $ense_new_price * $newprice->ense_end_inventory;
                                } else {
                                    $ense_end_price = '0';
                                    $ense_new_amount = '0';
                                }
                            }
                        }
                    } else {
                        $ense_end_price = $newprice->ense_end_price;
                        // 当前总金额
                        $ense_new_amount = $newprice->ense_end_amount;
                    }
                } else {
                    $new_nums = 0;
                    $ense_end_price = '0';
                    $ense_new_amount = '0';
                    if ($goods) {
                        $ense_new_price = DB::table('enters_goods_price')
                            ->where('ense_goo_number', $goods->goo_goods_encode)
                            ->select('ense_new_price')
                            ->first();
                        if ($ense_new_price) {
                            if ($ense_new_price->ense_new_price == 0) {
                                $ense_new_price = DB::table('enters_goods_price')
                                    ->where('ense_goo_encode', $goods->goo_goods_encode)
                                    ->value('ense_new_price');
                                if ($ense_new_price) {
                                    $ense_end_price = $ense_new_price;
                                    // 当前总金额
                                    $ense_new_amount = $ense_new_price * $new_nums;
                                } else {
                                    $ense_new_price = DB::table('enters_goods_price')
                                        ->where('ense_goods_code', $goods->goo_goods_encode)
                                        ->value('ense_new_price');
                                    if ($ense_new_price) {
                                        $ense_end_price = $ense_new_price;
                                        // 当前总金额
                                        $ense_new_amount = $ense_new_price * $new_nums;
                                    } else {
                                        $ense_end_price = '0';
                                        $ense_new_amount = '0';
                                    }
                                }
                            } else {
                                $ense_end_price = $ense_new_price->ense_new_price;
                                // 当前总金额
                                $ense_new_amount = $ense_new_price->ense_new_price * $new_nums;
                            }
                        } else {
                            $ense_new_price = DB::table('enters_goods_price')
                                ->where('ense_goo_encode', $goods->goo_goods_encode)
                                ->value('ense_new_price');
                            if ($ense_new_price) {
                                $ense_end_price = $ense_new_price;
                                // 当前总金额
                                $ense_new_amount = $ense_new_price * $new_nums;
                            } else {
                                $ense_new_price = DB::table('enters_goods_price')
                                    ->where('ense_goods_code', $goods->goo_goods_encode)
                                    ->value('ense_new_price');
                                if ($ense_new_price) {
                                    $ense_end_price = $ense_new_price;
                                    // 当前总金额
                                    $ense_new_amount = $ense_new_price * $new_nums;
                                } else {
                                    $ense_end_price = '0';
                                    $ense_new_amount = '0';
                                }
                            }
                        }
                    } else {
                        $new_nums = 0;
                        $ense_end_price = '0';
                        $ense_new_amount = '0';
                    }
                }
                if (!$goods) {
                    $enters_sellsarray['ense_goo_id'] = $value; //商品id
                    $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                    $enters_sellsarray['ense_order_number'] = $v['oure_original_correlation_number'];
                    $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                    $enters_sellsarray['ense_time'] = $oure_create_time;
                    $enters_sellsarray['ense_goo_unit'] = $oure_create_time;
                    $enters_saves = DB::table('enters_sells_savesbak')->insertGetId($enters_sellsarray);
                    continue;
                }
                if ($goods->goo_attribute == '') {
                    $unit = "";
                } else {
                    $units = json_decode($goods->goo_attribute, true);
                    foreach ($units as $ks => $vs) {
                        if ($vs['key'] == '单位') {
                            $unit = $vs['value'];
                        }
                    }
                }
                $dh = substr($oure_original_correlation_number, 0, 2);
                if ($oure_type == '1' || $oure_type == '7') {
                    // 获取入库价
                    if ($dh == 'CG') {
                        $procurement_goodsres = DB::table('storage_goods')
                            ->where('relation_number', $oure_original_correlation_number)
                            ->where('goods_id', $value)
                            ->select('univalence')
                            ->first();
                        // 入库价
                        if ($procurement_goodsres) {
                            $goods_price = $procurement_goodsres->univalence;
                        } else {
                            $goods_price = 0;
                        }
                    } elseif ($dh == 'DB') {
                        $transferring_orderres = DB::table('transferring_order')
                            ->where('tror_number', $oure_original_correlation_number)
                            ->select('tror_id', 'tror_out_ware_id')
                            ->first();
                        $transferring_order_detailsres = DB::table('transferring_order_details')
                            ->where('trod_tror_id', $transferring_orderres->tror_id)
                            ->select('trod_export_time')
                            ->first();
                        $enters_sells_savessres = DB::table('enters_sells_savess')
                            ->where('ense_goo_id', $value)
                            ->where('ense_goo_warehouse', $transferring_orderres->tror_out_ware_id)
                            ->orderBy('ense_id', 'desc')
                            ->select('ense_end_price')
                            ->first();
                        // 入库价
                        if ($enters_sells_savessres) {
                            $goods_price = $enters_sells_savessres->ense_end_price;
                        } else {
                            $goods_price = '0';
                        }
                    }
                    if ($new_nums < 0) {
                        // 期末平均价
                        $price = $goods_price;
                        // 期末总价
                        $ense_end_amount = ($goo_quantity * $goods_price) + ($price * $new_nums);
                    } else {
                        // 期末总价
                        $ense_end_amount = $ense_new_amount + $goods_price * $goo_quantity;
                        // 期末平均价
                        if ($new_nums + $goo_quantity == '0') {
                            $price = 0;
                        } else {
                            $price = sprintf("%.4f", $ense_end_amount / ($new_nums + $goo_quantity));
                        }
                    }

                    // 添加信息
                    $enters_sellsarray['ense_goo_id'] = $value; //商品id
                    $enters_sellsarray['ense_goo_name'] = $goods->goo_name; //商品名称
                    $enters_sellsarray['ense_goo_number'] = $goods->goo_goods_encode; //商品编码
                    $enters_sellsarray['ense_goo_unit'] = $unit; //商品单位
                    $enters_sellsarray['ense_goo_specifications'] = $goods->goo_ampere_hour . "AH"; //商品规格
                    $enters_sellsarray['ense_goo_waretype'] = $oure_waon_class; //仓库类别
                    $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                    $enters_sellsarray['ense_new_inventory'] = $new_nums; //当前库存
                    $enters_sellsarray['ense_new_price'] = $ense_end_price; //当前成本价
                    $enters_sellsarray['ense_new_amount'] = $ense_new_amount; //当前总金额
                    $enters_sellsarray['ense_end_inventory'] = $new_nums + $goo_quantity; //期末库存
                    $enters_sellsarray['ense_end_price'] = $price; //期末成本价
                    $enters_sellsarray['ense_end_amount'] = $ense_end_amount; //期末总金额
                    $enters_sellsarray['ense_in_num'] = $goo_quantity; //入库数量
                    $enters_sellsarray['ense_in_price'] = $goods_price; //入库单价
                    $enters_sellsarray['ense_in_amount'] = $goo_quantity * $goods_price; //入库总金额
                    $enters_sellsarray['ense_order_number'] = $oure_original_correlation_number; //
                    $enters_sellsarray['ense_out_in'] = $oure_out_in_type; //出入库类型：1出库，2入库
                    $enters_sellsarray['ense_type'] = $oure_type; //同出入库类型
                    $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                    $enters_sellsarray['ense_time'] = $oure_create_time;
                    $enters_saves = DB::table('enters_sells_savess')->insertGetId($enters_sellsarray);
                } elseif ($oure_type == '4') { //旧货回收
                    if ($oure_out_in_type == '2') {
                        // 获取回收金额
                        $recycleres = DB::table('junk_recycle')
                            ->where('jur_number', $v['oure_original_number'])
                            ->select('jur_id')
                            ->first();
                        if ($recycleres) {
                            $shop_setres = DB::table('junk_goods')
                                ->where('jugo_jur_id', $recycleres->jur_id)
                                ->select('jugo_goo_price')
                                ->first();
                            $goods_price = $shop_setres->jugo_goo_price / 100;
                        } else {
                            $goods_price = 0;
                        }
                        if ($new_nums < 0) {
                            // 期末平均价
                            $price = $goods_price;
                            // 期末总价
                            $ense_end_amount = ($goo_quantity * $goods_price) + ($price * $new_nums);
                        } else {
                            // 期末总价
                            $ense_end_amount = $ense_new_amount + $goods_price * $goo_quantity;
                            // 期末平均价
                            if ($new_nums + $goo_quantity == '0') {
                                $price = 0;
                            } else {
                                $price = sprintf("%.4f", $ense_end_amount / ($new_nums + $goo_quantity));
                            }
                        }
                        // 添加信息
                        $enters_sellsarray['ense_goo_id'] = $value; //商品id
                        $enters_sellsarray['ense_goo_name'] = $goods->goo_name; //商品名称
                        $enters_sellsarray['ense_goo_number'] = $goods->goo_goods_encode; //商品编码
                        $enters_sellsarray['ense_goo_unit'] = $unit; //商品单位
                        $enters_sellsarray['ense_goo_specifications'] = $goods->goo_ampere_hour . "AH"; //商品规格
                        $enters_sellsarray['ense_goo_waretype'] = $oure_waon_class; //仓库类别
                        $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                        $enters_sellsarray['ense_new_inventory'] = $new_nums; //当前库存
                        $enters_sellsarray['ense_new_price'] = $ense_end_price; //当前成本价
                        $enters_sellsarray['ense_new_amount'] = $ense_new_amount; //当前总金额
                        $enters_sellsarray['ense_end_inventory'] = $new_nums + $goo_quantity; //期末库存
                        $enters_sellsarray['ense_end_price'] = $price; //期末成本价
                        $enters_sellsarray['ense_end_amount'] = $ense_end_amount; //期末总金额
                        $enters_sellsarray['ense_in_num'] = $goo_quantity; //入库数量
                        $enters_sellsarray['ense_in_price'] = $goods_price; //入库单价
                        $enters_sellsarray['ense_in_amount'] = $goo_quantity * $goods_price; //入库总金额
                        $enters_sellsarray['ense_order_number'] = $oure_original_correlation_number; //原始单号
                        $enters_sellsarray['ense_out_in'] = $oure_out_in_type; //出入库类型：1出库，2入库
                        $enters_sellsarray['ense_type'] = $oure_type; //同出入库类型
                        $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                        $enters_sellsarray['ense_time'] = $oure_create_time;
                    } else { //出库
                        // 添加信息
                        $enters_sellsarray['ense_goo_id'] = $value; //商品id
                        $enters_sellsarray['ense_goo_name'] = $goods->goo_name; //商品名称
                        $enters_sellsarray['ense_goo_number'] = $goods->goo_goods_encode; //商品编码
                        $enters_sellsarray['ense_goo_unit'] = $unit; //商品单位
                        $enters_sellsarray['ense_goo_specifications'] = $goods->goo_ampere_hour . "AH"; //商品规格
                        $enters_sellsarray['ense_goo_waretype'] = $oure_waon_class; //仓库类别
                        $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                        $enters_sellsarray['ense_new_inventory'] = $new_nums; //当前库存
                        $enters_sellsarray['ense_new_price'] = $ense_end_price; //当前成本价
                        $enters_sellsarray['ense_new_amount'] = $ense_new_amount; //当前总金额
                        $enters_sellsarray['ense_end_inventory'] = $new_nums - $goo_quantity; //期末库存
                        $enters_sellsarray['ense_end_price'] = $ense_end_price; //期末成本价
                        $enters_sellsarray['ense_end_amount'] = $ense_new_amount - sprintf("%.4f", $goo_quantity * $ense_end_price); //期末总金额
                        $enters_sellsarray['ense_out_num'] = $goo_quantity; //出库数量
                        $enters_sellsarray['ense_out_price'] = $ense_end_price; //出库单价
                        $enters_sellsarray['ense_out_amount'] = sprintf("%.4f", $goo_quantity * $ense_end_price); //出库总金额
                        $enters_sellsarray['ense_order_number'] = $oure_original_correlation_number; //原始单号
                        $enters_sellsarray['ense_out_in'] = $oure_out_in_type; //出入库类型：1出库，2入库
                        $enters_sellsarray['ense_type'] = $oure_type; //同出入库类型
                        $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                        $enters_sellsarray['ense_time'] = $oure_create_time;
                    }
                    $enters_saves = DB::table('enters_sells_savess')->insertGetId($enters_sellsarray);
                } elseif ($oure_type == '5') {
                    $order_wareid = DB::table('order')
                        ->where('ord_order_number', $oure_original_correlation_number)
                        ->select('ord_ware_id')
                        ->first();
                    if ($order_wareid) {
                        if ($order_wareid->ord_ware_id == '0') {
                            $order_wareid = DB::table('recede_recycle')
                                ->where('rec_number', $v['oure_original_number'])
                                ->select('rec_ware_id')
                                ->first();
                            if (!$order_wareid) {
                                if (in_array($oure_ware_id, $ware_id)) {
                                    echo $v['oure_id'];
                                    exit;
                                } else {
                                    $enters_sellsarray['ense_goo_id'] = $value; //商品id
                                    $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                                    $enters_sellsarray['ense_order_number'] = $v['oure_original_correlation_number'];
                                    $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                                    $enters_sellsarray['ense_time'] = $oure_create_time;
                                    $enters_saves = DB::table('enters_sells_savesbak')->insertGetId($enters_sellsarray);
                                    continue;
                                }
                            }
                            $ord_ware_id = $order_wareid->rec_ware_id;
                        } else {
                            $ord_ware_id = $order_wareid->ord_ware_id;
                        }
                    } else {
                        $order_wareid = DB::table('recede_recycle')
                            ->where('rec_number', $v['oure_original_number'])
                            ->select('rec_ware_id')
                            ->first();
                        if (!$order_wareid) {
                            if (in_array($oure_ware_id, $ware_id)) {
                                echo $v['oure_id'];
                                exit;
                            } else {
                                $enters_sellsarray['ense_goo_id'] = $value; //商品id
                                $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                                $enters_sellsarray['ense_order_number'] = $v['oure_original_correlation_number'];
                                $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                                $enters_sellsarray['ense_time'] = $oure_create_time;
                                $enters_saves = DB::table('enters_sells_savesbak')->insertGetId($enters_sellsarray);
                                continue;
                            }
                        }
                        $ord_ware_id = $order_wareid->rec_ware_id;
                    }

                    $enters_sells_savessres = DB::table('enters_sells_savess')
                        ->where('ense_out_in', '1')
                        ->where('ense_type', '9')
                        ->where('ense_goo_id', $value)
                        ->where('ense_goo_warehouse', $ord_ware_id)
                        ->where('ense_order_number', $oure_original_correlation_number)
                        ->orderBy('ense_id', 'desc')
                        ->select('ense_end_price')
                        ->first();
                    // 入库金额
                    if ($enters_sells_savessres) {
                        $goods_price = $enters_sells_savessres->ense_end_price;
                    } else {
                        $goods_price = $ense_end_price;
                    }

                    // 期末总价
                    $ense_end_amount = $ense_new_amount + $goods_price * $goo_quantity;
                    $price = $ense_end_price;
                    // 添加信息
                    $enters_sellsarray['ense_goo_id'] = $value; //商品id
                    $enters_sellsarray['ense_goo_name'] = $goods->goo_name; //商品名称
                    $enters_sellsarray['ense_goo_number'] = $goods->goo_goods_encode; //商品编码
                    $enters_sellsarray['ense_goo_unit'] = $unit; //商品单位
                    $enters_sellsarray['ense_goo_specifications'] = $goods->goo_ampere_hour . "AH"; //商品规格
                    $enters_sellsarray['ense_goo_waretype'] = $oure_waon_class; //仓库类别
                    $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                    $enters_sellsarray['ense_new_inventory'] = $new_nums; //当前库存
                    $enters_sellsarray['ense_new_price'] = $ense_end_price; //当前成本价
                    $enters_sellsarray['ense_new_amount'] = $ense_new_amount; //当前总金额
                    $enters_sellsarray['ense_end_inventory'] = $new_nums + $goo_quantity; //期末库存
                    $enters_sellsarray['ense_end_price'] = $price; //期末成本价
                    $enters_sellsarray['ense_end_amount'] = $ense_end_amount; //期末总金额
                    $enters_sellsarray['ense_out_num'] = "-" . $goo_quantity; //入库数量
                    $enters_sellsarray['ense_out_price'] = $goods_price; //入库单价
                    $enters_sellsarray['ense_out_amount'] = "-" . abs($goo_quantity * $goods_price); //入库总金额
                    $enters_sellsarray['ense_order_number'] = $oure_original_correlation_number; //原始单号
                    $enters_sellsarray['ense_out_in'] = $oure_out_in_type; //出入库类型：1出库，2入库
                    $enters_sellsarray['ense_type'] = $oure_type; //同出入库类型
                    $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                    $enters_sellsarray['ense_time'] = $oure_create_time;
                    $enters_saves = DB::table('enters_sells_savess')->insertGetId($enters_sellsarray);
                } elseif ($oure_type == '12' || $oure_type == '13') { //退货单
                    $goods_price = $ense_end_price;
                    $price = $ense_end_price;
                    $ense_end_amount = $ense_new_amount + $goo_quantity * $goods_price;
                    // 添加信息
                    $enters_sellsarray['ense_goo_id'] = $value; //商品id
                    $enters_sellsarray['ense_goo_name'] = $goods->goo_name; //商品名称
                    $enters_sellsarray['ense_goo_number'] = $goods->goo_goods_encode; //商品编码
                    $enters_sellsarray['ense_goo_unit'] = $unit; //商品单位
                    $enters_sellsarray['ense_goo_specifications'] = $goods->goo_ampere_hour . "AH"; //商品规格
                    $enters_sellsarray['ense_goo_waretype'] = $oure_waon_class; //仓库类别
                    $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                    $enters_sellsarray['ense_new_inventory'] = $new_nums; //当前库存
                    $enters_sellsarray['ense_new_price'] = $ense_end_price; //当前成本价
                    $enters_sellsarray['ense_new_amount'] = $ense_new_amount; //当前总金额
                    $enters_sellsarray['ense_end_inventory'] = $new_nums + $goo_quantity; //期末库存
                    $enters_sellsarray['ense_end_price'] = $price; //期末成本价
                    $enters_sellsarray['ense_end_amount'] = $ense_end_amount; //期末总金额
                    $enters_sellsarray['ense_out_num'] = "-" . $goo_quantity; //入库数量
                    $enters_sellsarray['ense_out_price'] = $goods_price; //入库单价
                    $enters_sellsarray['ense_out_amount'] = "-" . abs($goo_quantity * $goods_price); //入库总金额
                    $enters_sellsarray['ense_order_number'] = $oure_original_correlation_number; //原始单号
                    $enters_sellsarray['ense_out_in'] = $oure_out_in_type; //出入库类型：1出库，2入库
                    $enters_sellsarray['ense_type'] = $oure_type; //同出入库类型
                    $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                    $enters_sellsarray['ense_time'] = $oure_create_time;
                    $enters_saves = DB::table('enters_sells_savess')->insertGetId($enters_sellsarray);
                } else {
                    // 采购退货单
                    if ($oure_type == '2' && $dh == 'TH') {
                        $procurement_goodsres = DB::table('storage_goods')
                            ->where('relation_number', $oure_original_correlation_number)
                            ->where('goods_id', $value)
                            ->select('univalence')
                            ->first();
                        // 入库价
                        if ($procurement_goodsres) {
                            $goods_price = $procurement_goodsres->univalence;
                        } else {
                            $goods_price = 0;
                        }

                        if ($new_nums < 0) {
                            // 期末平均价
                            $price = $goods_price;
                            // 期末总价
                            $ense_end_amount = ($price * $new_nums) - ($goo_quantity * $goods_price);
                        } else {
                            // 期末总价
                            $ense_end_amount = $ense_new_amount - $goods_price * $goo_quantity;
                            // 期末平均价
                            if ($new_nums + $goo_quantity == '0') {
                                $price = 0;
                            } else {
                                $price = sprintf("%.4f", $ense_end_amount / ($new_nums + $goo_quantity));
                            }
                        }
                        $enters_sellsarray['ense_goo_id'] = $value; //商品id
                        $enters_sellsarray['ense_goo_name'] = $goods->goo_name; //商品名称
                        $enters_sellsarray['ense_goo_number'] = $goods->goo_goods_encode; //商品编码
                        $enters_sellsarray['ense_goo_unit'] = $unit; //商品单位
                        $enters_sellsarray['ense_goo_specifications'] = $goods->goo_ampere_hour . "AH"; //商品规格
                        $enters_sellsarray['ense_goo_waretype'] = $oure_waon_class; //仓库类别
                        $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                        $enters_sellsarray['ense_new_inventory'] = $new_nums; //当前库存
                        $enters_sellsarray['ense_new_price'] = $ense_end_price; //当前成本价
                        $enters_sellsarray['ense_new_amount'] = $ense_new_amount; //当前总金额
                        $enters_sellsarray['ense_end_inventory'] = $new_nums - $goo_quantity; //期末库存
                        $enters_sellsarray['ense_end_price'] = $price; //期末成本价
                        $enters_sellsarray['ense_end_amount'] = $ense_end_amount; //期末总金额
                        $enters_sellsarray['ense_in_num'] = "-" . $goo_quantity; //入库数量
                        $enters_sellsarray['ense_in_price'] = $goods_price; //入库单价
                        $enters_sellsarray['ense_in_amount'] = "-" . abs($goo_quantity * $goods_price); //入库总金额
                        $enters_sellsarray['ense_order_number'] = $oure_original_correlation_number; //原始单号
                        $enters_sellsarray['ense_out_in'] = $oure_out_in_type; //出入库类型：1出库，2入库
                        $enters_sellsarray['ense_type'] = $oure_type; //同出入库类型
                        $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                        $enters_sellsarray['ense_time'] = $oure_create_time;
                        $enters_saves = DB::table('enters_sells_savess')->insertGetId($enters_sellsarray);
                    } else {
                        if ($oure_type == '9' && $oure_out_in_type == '2') {
                            $order_wareid = DB::table('order')
                                ->where('ord_order_number', $oure_original_correlation_number)
                                ->select('ord_ware_id')
                                ->first();
                            if (!$order_wareid) {
                                if (in_array($oure_ware_id, $ware_id)) {
                                    echo $v['oure_id'];
                                    exit;
                                } else {
                                    $enters_sellsarray['ense_goo_id'] = $value; //商品id
                                    $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                                    $enters_sellsarray['ense_order_number'] = $v['oure_original_correlation_number'];
                                    $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                                    $enters_sellsarray['ense_time'] = $oure_create_time;
                                    $enters_saves = DB::table('enters_sells_savesbak')->insertGetId($enters_sellsarray);
                                    continue;
                                }
                            }
                            $enters_sells_savessres = DB::table('enters_sells_savess')
                                ->where('ense_goo_id', $value)
                                ->where('ense_goo_warehouse', $order_wareid->ord_ware_id)
                                ->orderBy('ense_id', 'desc')
                                ->select('ense_end_price')
                                ->first();
                            if ($enters_sells_savessres) {
                                $goods_price = $enters_sells_savessres->ense_end_price;
                            } else {
                                $goods_price = '0';
                            }
                            if ($new_nums < 0) {
                                // 期末平均价
                                $price = $goods_price;
                                // 期末总价
                                $ense_end_amount = ($goo_quantity * $goods_price) + ($price * $new_nums);
                            } else {
                                // 期末总价
                                $ense_end_amount = $ense_new_amount + $goods_price * $goo_quantity;
                                // 期末平均价
                                if ($new_nums + $goo_quantity == '0') {
                                    $price = 0;
                                } else {
                                    $price = sprintf("%.4f", $ense_end_amount / ($new_nums + $goo_quantity));
                                }
                            }
                            // 添加信息
                            $enters_sellsarray['ense_goo_id'] = $value; //商品id
                            $enters_sellsarray['ense_goo_name'] = $goods->goo_name; //商品名称
                            $enters_sellsarray['ense_goo_number'] = $goods->goo_goods_encode; //商品编码
                            $enters_sellsarray['ense_goo_unit'] = $unit; //商品单位
                            $enters_sellsarray['ense_goo_specifications'] = $goods->goo_ampere_hour . "AH"; //商品规格
                            $enters_sellsarray['ense_goo_waretype'] = $oure_waon_class; //仓库类别
                            $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                            $enters_sellsarray['ense_new_inventory'] = $new_nums; //当前库存
                            $enters_sellsarray['ense_new_price'] = $ense_end_price; //当前成本价
                            $enters_sellsarray['ense_new_amount'] = $ense_new_amount; //当前总金额
                            $enters_sellsarray['ense_end_inventory'] = $new_nums + $goo_quantity; //期末库存
                            $enters_sellsarray['ense_end_price'] = $price; //期末成本价
                            $enters_sellsarray['ense_end_amount'] = $ense_end_amount; //期末总金额
                            $enters_sellsarray['ense_out_num'] = "-" . $goo_quantity; //入库数量
                            $enters_sellsarray['ense_out_price'] = $goods_price; //入库单价
                            $enters_sellsarray['ense_out_amount'] = "-" . abs($goo_quantity * $goods_price); //入库总金额
                            $enters_sellsarray['ense_order_number'] = $oure_original_correlation_number; //原始单号
                            $enters_sellsarray['ense_out_in'] = $oure_out_in_type; //出入库类型：1出库，2入库
                            $enters_sellsarray['ense_type'] = $oure_type; //同出入库类型
                            $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                            $enters_sellsarray['ense_time'] = $oure_create_time;
                            $enters_saves = DB::table('enters_sells_savess')->insertGetId($enters_sellsarray);
                        } elseif ($oure_type == '15' && $oure_out_in_type == '2') {
                            if ($vv['oude_type'] != '3') {
                                if ($v['oure_original_correlation_number'] == 'P0103130704100120213' || $v['oure_original_correlation_number'] == 'P0103130703290126554' || $v['oure_original_correlation_number'] == 'P0103130702240199157' || $v['oure_original_correlation_number'] == 'P0103130656360187499' || $v['oure_original_correlation_number'] == 'P0103130551150119910') {
                                    $enters_sells_savesres = DB::table('enters_goods_price')
                                        ->where('ense_goo_number', $goods->goo_goods_encode)
                                        ->select('ense_new_price')
                                        ->first();
                                    if ($enters_sells_savesres) {
                                        if ($enters_sells_savesres->ense_new_price == 0) {
                                            $ense_new_price = DB::table('enters_goods_price')
                                                ->where('ense_goo_encode', $goods->goo_goods_encode)
                                                ->value('ense_new_price');
                                            if ($ense_new_price) {
                                                $goods_price = $ense_new_price;
                                            } else {
                                                $ense_new_price = DB::table('enters_goods_price')
                                                    ->where('ense_goods_code', $goods->goo_goods_encode)
                                                    ->value('ense_new_price');
                                                if ($ense_new_price) {
                                                    $goods_price = $ense_new_price;
                                                } else {
                                                    $goods_price = '0';
                                                }
                                            }
                                        } else {
                                            $goods_price = $enters_sells_savesres->ense_new_price;
                                        }
                                    } else {
                                        $enters_sells_savesres = DB::table('enters_goods_price')
                                            ->where('ense_goo_number', $goods->goo_goods_encode)
                                            ->select('ense_new_price')
                                            ->first();
                                        if ($enters_sells_savesres) {
                                            if ($enters_sells_savesres->ense_new_price == 0) {
                                                $ense_new_price = DB::table('enters_goods_price')
                                                    ->where('ense_goo_encode', $goods->goo_goods_encode)
                                                    ->value('ense_new_price');
                                                if ($ense_new_price) {
                                                    $goods_price = $ense_new_price;
                                                } else {
                                                    $ense_new_price = DB::table('enters_goods_price')
                                                        ->where('ense_goods_code', $goods->goo_goods_encode)
                                                        ->value('ense_new_price');
                                                    if ($ense_new_price) {
                                                        $goods_price = $ense_new_price;
                                                    } else {
                                                        $goods_price = '0';
                                                    }
                                                };
                                            } else {
                                                $goods_price = $enters_sells_savesres->ense_new_price;
                                            }
                                        } else {
                                            $ense_new_price = DB::table('enters_goods_price')
                                                ->where('ense_goo_encode', $goods->goo_goods_encode)
                                                ->value('ense_new_price');
                                            if ($ense_new_price) {
                                                $goods_price = $ense_new_price;
                                            } else {
                                                $ense_new_price = DB::table('enters_goods_price')
                                                    ->where('ense_goods_code', $goods->goo_goods_encode)
                                                    ->value('ense_new_price');
                                                if ($ense_new_price) {
                                                    $goods_price = $ense_new_price;
                                                } else {
                                                    $goods_price = '0';
                                                }
                                            };
                                        }
                                    }
                                    // 期末总价
                                    $ense_end_amount = $goods_price * $vv['oude_goo_number'];
                                    // 添加信息
                                    $enters_sellsarray['ense_goo_id'] = $value; //商品id
                                    $enters_sellsarray['ense_goo_name'] = $goods->goo_name; //商品名称
                                    $enters_sellsarray['ense_goo_number'] = $goods->goo_goods_encode; //商品编码
                                    $enters_sellsarray['ense_goo_unit'] = $unit; //商品单位
                                    $enters_sellsarray['ense_goo_specifications'] = $goods->goo_ampere_hour . "AH"; //商品规格
                                    $enters_sellsarray['ense_goo_waretype'] = $oure_waon_class; //仓库类别
                                    $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                                    $enters_sellsarray['ense_new_inventory'] = $new_nums + $goo_quantity; //当前库存
                                    $enters_sellsarray['ense_new_price'] = $goods_price; //当前成本价
                                    $enters_sellsarray['ense_new_amount'] = $ense_end_amount; //当前总金额
                                    $enters_sellsarray['ense_end_inventory'] = $new_nums + $goo_quantity; //期末库存
                                    $enters_sellsarray['ense_end_price'] = $goods_price; //期末成本价
                                    $enters_sellsarray['ense_end_amount'] = $ense_end_amount; //期末总金额
                                    $enters_sellsarray['ense_in_num'] = $goo_quantity; //入库数量
                                    $enters_sellsarray['ense_in_price'] = $goods_price; //入库单价
                                    $enters_sellsarray['ense_in_amount'] = $goo_quantity * $goods_price; //入库总金额
                                    $enters_sellsarray['ense_order_number'] = $oure_original_correlation_number; //原始单号
                                    $enters_sellsarray['ense_out_in'] = $oure_out_in_type; //出入库类型：1出库，2入库
                                    $enters_sellsarray['ense_type'] = $oure_type; //同出入库类型
                                    $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                                    $enters_sellsarray['ense_time'] = $oure_create_time;
                                    $enters_saves = DB::table('enters_sells_savess')->insertGetId($enters_sellsarray);
                                } else {
                                    $enters_sells_savesres = DB::table('enters_sells_savess')
                                        ->where('ense_goo_id', $value)
                                        ->where('ense_goo_warehouse', $oure_ware_id)
                                        ->orderBy('ense_id', 'desc')
                                        ->select('ense_end_price')
                                        ->first();
                                    if ($enters_sells_savesres) {
                                        $goods_price = $enters_sells_savesres->ense_end_price;
                                    } else {
                                        $goods_price = '0';
                                    }
                                    // 期末总价
                                    $ense_end_amount = $ense_new_amount + $goods_price * $goo_quantity;
                                    if ($new_nums + $goo_quantity == '0') {
                                        $price = 0;
                                    } else {
                                        $price = sprintf("%.4f", $ense_end_amount / ($new_nums + $goo_quantity));
                                    }
                                    // 添加信息
                                    $enters_sellsarray['ense_goo_id'] = $value; //商品id
                                    $enters_sellsarray['ense_goo_name'] = $goods->goo_name; //商品名称
                                    $enters_sellsarray['ense_goo_number'] = $goods->goo_goods_encode; //商品编码
                                    $enters_sellsarray['ense_goo_unit'] = $unit; //商品单位
                                    $enters_sellsarray['ense_goo_specifications'] = $goods->goo_ampere_hour . "AH"; //商品规格
                                    $enters_sellsarray['ense_goo_waretype'] = $oure_waon_class; //仓库类别
                                    $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                                    $enters_sellsarray['ense_new_inventory'] = $new_nums; //当前库存
                                    $enters_sellsarray['ense_new_price'] = $ense_end_price; //当前成本价
                                    $enters_sellsarray['ense_new_amount'] = $ense_new_amount; //当前总金额
                                    $enters_sellsarray['ense_end_inventory'] = $new_nums + $goo_quantity; //期末库存
                                    $enters_sellsarray['ense_end_price'] = $price; //期末成本价
                                    $enters_sellsarray['ense_end_amount'] = $ense_end_amount; //期末总金额
                                    $enters_sellsarray['ense_in_num'] = $goo_quantity; //入库数量
                                    $enters_sellsarray['ense_in_price'] = $goods_price; //入库单价
                                    $enters_sellsarray['ense_in_amount'] = $goo_quantity * $goods_price; //入库总金额
                                    $enters_sellsarray['ense_order_number'] = $oure_original_correlation_number; //原始单号
                                    $enters_sellsarray['ense_out_in'] = $oure_out_in_type; //出入库类型：1出库，2入库
                                    $enters_sellsarray['ense_type'] = $oure_type; //同出入库类型
                                    $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                                    $enters_sellsarray['ense_time'] = $oure_create_time;
                                    $enters_saves = DB::table('enters_sells_savess')->insertGetId($enters_sellsarray);
                                }
                            }
                        } else {
                            // 添加信息
                            $enters_sellsarray['ense_goo_id'] = $value; //商品id
                            $enters_sellsarray['ense_goo_name'] = $goods->goo_name; //商品名称
                            $enters_sellsarray['ense_goo_number'] = $goods->goo_goods_encode; //商品编码
                            $enters_sellsarray['ense_goo_unit'] = $unit; //商品单位
                            $enters_sellsarray['ense_goo_specifications'] = $goods->goo_ampere_hour . "AH"; //商品规格
                            $enters_sellsarray['ense_goo_waretype'] = $oure_waon_class; //仓库类别
                            $enters_sellsarray['ense_goo_warehouse'] = $oure_ware_id; //所在仓库
                            $enters_sellsarray['ense_new_inventory'] = $new_nums; //当前库存
                            $enters_sellsarray['ense_new_price'] = $ense_end_price; //当前成本价
                            $enters_sellsarray['ense_new_amount'] = $ense_new_amount; //当前总金额
                            $enters_sellsarray['ense_end_inventory'] = $new_nums - $goo_quantity; //期末库存
                            $enters_sellsarray['ense_end_price'] = $ense_end_price; //期末成本价
                            $enters_sellsarray['ense_end_amount'] = $ense_new_amount - sprintf("%.4f", $goo_quantity * $ense_end_price); //期末总金额
                            $enters_sellsarray['ense_out_num'] = $goo_quantity; //出库数量
                            $enters_sellsarray['ense_out_price'] = $ense_end_price; //出库单价
                            $enters_sellsarray['ense_out_amount'] = sprintf("%.4f", $goo_quantity * $ense_end_price); //出库总金额
                            $enters_sellsarray['ense_order_number'] = $oure_original_correlation_number; //原始单号
                            $enters_sellsarray['ense_out_in'] = $oure_out_in_type; //出入库类型：1出库，2入库
                            $enters_sellsarray['ense_type'] = $oure_type; //同出入库类型
                            $enters_sellsarray['ense_shop_id'] = $oure_shop_id;
                            $enters_sellsarray['ense_time'] = $oure_create_time;
                            $enters_saves = DB::table('enters_sells_savess')->insertGetId($enters_sellsarray);
                        }
                    }
                }
            }
        }
        echo $a;
        exit;
    }

    // 修改退货单1-3月份未同步U8的凭证
    public function adjustRecedeVoucher()
    {
        ignore_user_abort();
        set_time_limit(0);

        $adm_cid = 1;
        $shop_id = 1;

        // 查询凭证未同步U8,类型为退货单的数据
        $find_vore_data = DB::table('voucher_receipts as vore')
            ->leftjoin('recede_recycle as rec', 'rec.rec_ord_id', '=', 'vore.vore_ord_id')
            ->where('vore_cid', $adm_cid)
            ->where('vore_shop_id', $shop_id)
            ->where('vore_service_type', 4)
            ->where('vore_state', 1)
            ->where('rec_source_type', 8)
            ->where('rec_state', 2)
            ->select('vore_id', 'vore_ord_id', 'vore_ord_number', 'rec_id')
            ->get()->toArray();
        foreach ($find_vore_data as $key => $value) {
            // 查询退货单商品
            $find_rego_data = DB::table('recede_goods')
                ->where('rego_rec_id', $value->rec_id)
                ->select('rego_id', 'rego_ware_id', 'rego_waon_class')
                ->get();
            if ($find_rego_data) {
                foreach ($find_rego_data as $kk => $vv) {
                    // 查询借贷表
                    $find_volo_data = DB::table('voucher_loan')
                        ->where('volo_vore_id', $value->vore_id)
                        ->get()->toArray();
                    $find_volo_data = json_decode(json_encode($find_volo_data), true);
                    if ($vv->rego_waon_class == 1) { //货物仓
                        if ($find_volo_data[0]['volo_borrow_costing'] > 0 || $find_volo_data[0]['volo_loan_costing'] > 0) {
                            $edit_data['volo_borrow_costing'] = '-' . $find_volo_data[0]['volo_borrow_costing'];
                            $edit_data['volo_loan_costing'] = '-' . $find_volo_data[0]['volo_loan_costing'];
                            $edit_data['volo_update_time'] = date('Y-m-d H:i:s');
                            $edit_res = DB::table('voucher_loan')
                                ->where('volo_id', $find_volo_data[0]['volo_id'])
                                ->update($edit_data);
                            dump($edit_res);
                            unset($edit_data);
                            unset($edit_res);
                        }
                    } elseif ($vv->rego_waon_class == 3) { //旧货仓
                        $edit_data['volo_borrow_subject'] = '140501';
                        $edit_data['volo_borrow_subject_name'] = '库存商品';
                        $edit_data['volo_borrow_price'] = abs($find_volo_data[0]['volo_borrow_price']);
                        $edit_data['volo_borrow_costing'] = 0;
                        $edit_data['volo_borrow_costing_subject'] = '';
                        $edit_data['volo_borrow_costing_name'] = '';
                        $edit_data['volo_loan_subject'] = '1122';
                        $edit_data['volo_loan_subject_name'] = '应收账款';
                        $edit_data['volo_loan_price'] = abs($find_volo_data[0]['volo_loan_price']);
                        $edit_data['volo_loan_costing'] = 0;
                        $edit_data['volo_loan_costing_subject'] = '';
                        $edit_data['volo_loan_costing_name'] = '';
                        $edit_data['volo_update_time'] = date('Y-m-d H:i:s');
                        $edit_res = DB::table('voucher_loan')
                            ->where('volo_id', $find_volo_data[0]['volo_id'])
                            ->update($edit_data);
                        dump($edit_res);
                        unset($edit_data);
                        unset($edit_res);
                    }
                }
            } else {
                echo 222;
            }
        }
        echo '成功';
    }

    // 查询虚拟加盟店凭证金额跟工单金额不一致的数据
    public function adjustVirtualFranchises()
    {
        ignore_user_abort();
        set_time_limit(0);

        $adm_cid = 1;
        $shop_id = 1;

        // 查询凭证未同步U8,单号类型为工单的数据
        $find_vore_data = DB::table('voucher_receipts as vore')
            ->where('vore_cid', $adm_cid)
            ->where('vore_shop_id', $shop_id)
            ->where('vore_order_type', 2)
            ->where('vore_state', 1)
            ->select('vore_id', 'vore_ord_id', 'vore_ord_number', 'vore_wor_id', 'vore_wor_number')
            ->get()->toArray();
        foreach ($find_vore_data as $key => $value) {
            // 查询工单类型为外派工单,门店类型为虚拟加盟店,完成已通过的数据
            $find_wor_data = DB::table('work_order as wor')
                ->leftjoin('work_client as wocl', 'wocl.wocl_wor_id', '=', 'wor.wor_id')
                ->where('wor_id', $value->vore_wor_id)
                ->where('wor_number', $value->vore_wor_number)
                ->where('wor_delete', 1)
                ->where('wor_collection_bill', 1)
                ->where('wor_state', 10)
                ->select('wor_id', 'wor_number', 'wor_actual_price')
                ->first();
            if ($find_wor_data) {
                // 查询借贷表
                $find_volo_data = DB::table('voucher_loan')
                    ->where('volo_vore_id', $value->vore_id)
                    ->get()->toArray();
                $find_volo_data = json_decode(json_encode($find_volo_data), true);
                $wor_actual_price = $find_wor_data->wor_actual_price / 100;
                if ($wor_actual_price != $find_volo_data[0]['volo_borrow_price']) {
                    dump($value->vore_ord_number);
                }
            } else {
                echo 222;
            }
        }
        echo '成功';
    }

    // 查询退货单类型凭证金额跟订单类型退货单金额不一致的数据
    public function adjustRecedeVoucher2()
    {
        ignore_user_abort();
        set_time_limit(0);

        $adm_cid = 1;
        $shop_id = 1;

        // 查询凭证未同步U8,单号类型为工单的数据
        $find_vore_data = DB::table('voucher_receipts')
            ->where('vore_cid', $adm_cid)
            ->where('vore_shop_id', $shop_id)
            ->where('vore_service_type', 4)
            ->where('vore_state', 1)
            ->select('vore_id', 'vore_ord_id', 'vore_ord_number')
            ->get()->toArray();
        foreach ($find_vore_data as $key => $value) {
            // 查询订单状态已完成的数据
            $find_wor_data = DB::table('order')
                ->where('ord_id', $value->vore_ord_id)
                ->where('ord_order_number', $value->vore_ord_number)
                ->where('ord_delete', 1)
                ->where('ord_collection_bill', 1)
                ->where('ord_service_type', 4)
                ->where('ord_wor_state', 4)
                ->select('ord_id', 'ord_order_number', 'ord_actual_price')
                ->first();
            if ($find_wor_data) {
                // 查询借贷表
                $find_volo_data = DB::table('voucher_loan')
                    ->where('volo_vore_id', $value->vore_id)
                    ->get()->toArray();
                $find_volo_data = json_decode(json_encode($find_volo_data), true);
                $ord_actual_price = $find_wor_data->ord_actual_price / 100;
                if (abs($ord_actual_price) != abs($find_volo_data[0]['volo_borrow_price'])) {
                    dump($value->vore_ord_number);
                }
            } else {
                echo 222;
            }
        }
        echo '成功';
    }

    // 修改盘库单凭证成本
    public function StocktakeVoucher()
    {   
        ignore_user_abort();
        set_time_limit(0);

        $adm_cid = 1;
        $shop_id = 2;

        $stoc_storage_number = ['P0207141843454913992'];

        // 查询盘库单状态已通过的数据
        $find_wor_data = DB::table('stocktake')
            ->where('stoc_cid', $adm_cid)
            ->where('stoc_shop_id', $shop_id)
            ->where('stoc_state', 3)
            // ->where('stoc_type', '!=', 3)
            ->whereIn('stoc_storage_number', $stoc_storage_number)
            ->select('stoc_id', 'stoc_cid', 'stoc_superior_shop_id', 'stoc_shop_id', 'stoc_adm_id', 'stoc_storage_number', 'stoc_type', 'stoc_differ_number', 'stoc_ware_id', 'stoc_waon_class', 'stoc_create_time')
            ->get()->toArray();
        $cost_price = 0;
        foreach ($find_wor_data as $key => $value) {
            // 根据盘库单id,查询商品数据
            $find_stgo_data = DB::table('stocktake_goods')
                ->where('stgo_stoc_id', $value->stoc_id)
                ->select('stgo_goo_id', 'stgo_stock_number', 'stgo_stocktake_number', 'stgo_priority_stock_number', 'stgo_priority_stocktake_number', 'stgo_unchecked_stock_number', 'stgo_unchecked_stocktake_number', 'stgo_goo_id')
                ->get()->toArray();
            if ($find_stgo_data) {
                foreach ($find_stgo_data as $kkkk => $vvvv) {
                    // 获取当前月份的成本价
                    $find_ense_data = DB::table('enters_sells_saves')
                        ->where('ense_goo_id', $vvvv->stgo_goo_id)
                        ->where('ense_goo_warehouse', $value->stoc_ware_id)
                        ->where('ense_goo_waretype', $value->stoc_waon_class)
                        ->where('ense_order_number', $value->stoc_storage_number)
                        ->orderBy('ense_id', 'desc')
                        ->select('ense_new_price', 'ense_out_in', 'ense_in_num', 'ense_out_num')
                        ->first();
                    if ($find_ense_data->ense_new_price) {
                        $find_ense_new_price = sprintf('%.2f', $find_ense_data->ense_new_price);
                    } else {
                        $find_ense_new_price = 0;
                    }
                    if ($find_ense_data->ense_out_in == 1) {//出库
                        $cost_price += $find_ense_new_price*$find_ense_data->ense_out_num*(-1);
                    } elseif ($find_ense_data->ense_out_in == 2) {//入库
                        $cost_price += $find_ense_new_price*$find_ense_data->ense_in_num;
                    }
                }
                // 凭证单据报表
                // 判断凭证单据是否存在
                $vore_id = DB::table('voucher_receipts')
                    ->where('vore_cid', $adm_cid)
                    ->where('vore_shop_id', $shop_id)
                    ->where('vore_ord_id', $value->stoc_id)
                    ->where('vore_ord_number', $value->stoc_storage_number)
                    ->where('vore_order_type', 6)
                    ->where('vore_state', 1)
                    ->select('vore_id', 'vore_wor_id', 'vore_wor_number')
                    ->first();
                if (empty($vore_id)) {
                    if ($cost_price != 0) {
                        $vore_data['vore_cid'] = $value->stoc_cid; //公司id
                        $vore_data['vore_superior_shop_id'] = $value->stoc_superior_shop_id; //总店id
                        $vore_data['vore_shop_id'] = $value->stoc_shop_id; //门店id
                        $vore_data['vore_create_id'] = $value->stoc_adm_id; //创建人
                        $vore_data['vore_order_type'] = 6; //单号类型1.订单2.工单3.采购单4.销售单5.采购退货单6.盘库单
                        $vore_data['vore_wor_id'] = ''; //工单id
                        $vore_data['vore_wor_number'] = ''; //工单单号
                        $vore_data['vore_ord_id'] = $value->stoc_id; //订单id
                        $vore_data['vore_ord_number'] = $value->stoc_storage_number; //订单单号
                        $vore_data['vore_plus_minus'] = 1; //金额正负1正值2负值
                        $vore_data['vore_actual_price'] = abs($cost_price); //结算金额
                        $vore_data['vore_collection_price'] = 0; //代收金额
                        $vore_data['vore_recycle_price'] = 0; //回收金额
                        $vore_data['vore_early_costing'] = 0; //成本
                        $vore_data['vore_type'] = ''; //单位类型1会员零售2客户单位3速电派单
                        $vore_data['vore_client_id'] = ''; //客户单位/客户id
                        $vore_data['vore_channel_unit'] = '';
                        $vore_data['vore_create_time'] = $value->stoc_create_time;
                        $vore_id = DB::table('voucher_receipts')->insertGetId($vore_data);
                    }
                    // // 凭证借贷表(结算金额)
                    // if ($value->stoc_type == '1') { //盘盈
                    //     $volo_data[0]['volo_shop_id'] = $value->stoc_shop_id;
                    //     $volo_data[0]['volo_vore_id'] = $vore_id;
                    //     $volo_data[0]['volo_type'] = 1; //类型1借方2贷方
                    //     $volo_data[0]['volo_borrow_subject'] = '140501'; //借方科目
                    //     $volo_data[0]['volo_borrow_subject_name'] = '库存商品'; //借方科目名称
                    //     $volo_data[0]['volo_borrow_price'] = abs($cost_price); //借方金额
                    //     $volo_data[0]['volo_loan_subject'] = '190101'; //贷方科目
                    //     $volo_data[0]['volo_loan_subject_name'] = '待处理财产损溢'; //贷方科目名称
                    //     $volo_data[0]['volo_loan_price'] = abs($cost_price); //贷方金额
                    // } elseif ($value->stoc_type == '2') { //盘亏
                    //     $volo_data[0]['volo_shop_id'] = $value->stoc_shop_id;
                    //     $volo_data[0]['volo_vore_id'] = $vore_id;
                    //     $volo_data[0]['volo_type'] = 1; //类型1借方2贷方
                    //     $volo_data[0]['volo_borrow_subject'] = '190101'; //借方科目
                    //     $volo_data[0]['volo_borrow_subject_name'] = '待处理财产损溢'; //借方科目名称
                    //     $volo_data[0]['volo_borrow_price'] = abs($cost_price); //借方金额
                    //     $volo_data[0]['volo_loan_subject'] = '140501'; //贷方科目
                    //     $volo_data[0]['volo_loan_subject_name'] = '库存商品'; //贷方科目名称
                    //     $volo_data[0]['volo_loan_price'] = abs($cost_price); //贷方金额
                    // }

                    if ($cost_price > 0) {//金额盘盈
                        $volo_data[0]['volo_shop_id'] = $value->stoc_shop_id;
                        $volo_data[0]['volo_vore_id'] = $vore_id;
                        $volo_data[0]['volo_type'] = 1; //类型1借方2贷方
                        $volo_data[0]['volo_borrow_subject'] = '140501'; //借方科目
                        $volo_data[0]['volo_borrow_subject_name'] = '库存商品'; //借方科目名称
                        $volo_data[0]['volo_borrow_price'] = abs($cost_price); //借方金额
                        $volo_data[0]['volo_loan_subject'] = '190101'; //贷方科目
                        $volo_data[0]['volo_loan_subject_name'] = '待处理财产损溢'; //贷方科目名称
                        $volo_data[0]['volo_loan_price'] = abs($cost_price); //贷方金额
                        $volo_data[0]['volo_update_time'] = $value->stoc_create_time; //更新时间
                        $volo_id = DB::table('voucher_loan')->insert($volo_data);
                    } elseif($cost_price < 0) {// 金额盘亏
                        $volo_data[0]['volo_shop_id'] = $value->stoc_shop_id;
                        $volo_data[0]['volo_vore_id'] = $vore_id;
                        $volo_data[0]['volo_type'] = 1; //类型1借方2贷方
                        $volo_data[0]['volo_borrow_subject'] = '190101'; //借方科目
                        $volo_data[0]['volo_borrow_subject_name'] = '待处理财产损溢'; //借方科目名称
                        $volo_data[0]['volo_borrow_price'] = abs($cost_price); //借方金额
                        $volo_data[0]['volo_loan_subject'] = '140501'; //贷方科目
                        $volo_data[0]['volo_loan_subject_name'] = '库存商品'; //贷方科目名称
                        $volo_data[0]['volo_loan_price'] = abs($cost_price); //贷方金额
                        $volo_data[0]['volo_update_time'] = $value->stoc_create_time; //更新时间
                        $volo_id = DB::table('voucher_loan')->insert($volo_data);
                    }
                } else {
                    $vore_id = true;
                    $volo_id = true;
                }
            } else {
                $vore_id = true;
                $volo_id = true;
            }
        }
        dump($cost_price);
        echo '成功';
    }

    // 统计仓库库存数量和进销存库存数量不一致的数据
    public function selectWarehouseGoods(Request $request)
    {
        ignore_user_abort();
        set_time_limit(0);

        $adm_cid = 1;
        $shop_id = 1;

        $search_data = $request->all();
        $list_data = DB::table('enters_sells_saves')
            ->where('ense_shop_id', $shop_id)
            ->where('ense_goo_warehouse', $request['sssg_ware_id'])
            ->where('ense_goo_waretype', $request['sssg_waon_class'])
            ->where(function ($query) use ($search_data) {
                if (!empty($search_data['goo_name'])) {
                    $query->where('ense_goo_name', 'like', '%' . $search_data['goo_name'] . '%');
                }
            }) //商品名称
            ->where(function ($query) use ($search_data) {
                if (!empty($search_data['goo_goods_encode'])) {
                    $query->where('ense_goo_number', 'like', '%' . $search_data['goo_goods_encode'] . '%');
                }
            }) //商品编码
            ->where(function ($query) use ($search_data) {
                if (!empty($search_data['ense_time_start'])) {
                    $query->where('ense_time', '>=', $search_data['ense_time_start'] . " 00:00:00");
                }
            }) //起始时间
            ->where(function ($query) use ($search_data) {
                if (!empty($search_data['ense_time_end'])) {
                    $query->where('ense_time', '<=', $search_data['ense_time_end'] . " 23:59:59");
                }
            }) //结束时间
            ->select('ense_goo_id')
            ->orderBy('ense_id', 'asc')
            ->groupBy('ense_goo_id')
            ->get()->toArray();
        $list_items = $list_data;
        $ids = array();

        foreach ($list_items as $k => $v) {
            foreach ($v as $kk => $vv) {
                $ids[] = $vv;
            }
        }
        $list = DB::table('enters_sells_saves')
            ->where('ense_shop_id', $shop_id)
            ->where('ense_goo_warehouse', $request['sssg_ware_id'])
            ->where('ense_goo_waretype', $request['sssg_waon_class'])
            ->whereIn('ense_goo_id', $ids)
            ->where(function ($query) use ($search_data) {
                if (!empty($search_data['goo_name'])) {
                    $query->where('ense_goo_name', 'like', '%' . $search_data['goo_name'] . '%');
                }
            }) //商品名称
            ->where(function ($query) use ($search_data) {
                if (!empty($search_data['goo_goods_encode'])) {
                    $query->where('ense_goo_number', 'like', '%' . $search_data['goo_goods_encode'] . '%');
                }
            }) //商品编码
            ->where(function ($query) use ($search_data) {
                if (!empty($search_data['ense_time_start'])) {
                    $query->where('ense_time', '>=', $search_data['ense_time_start'] . " 00:00:00");
                }
            }) //起始时间
            ->where(function ($query) use ($search_data) {
                if (!empty($search_data['ense_time_end'])) {
                    $query->where('ense_time', '<=', $search_data['ense_time_end'] . " 23:59:59");
                }
            }) //结束时间
            ->select('ense_id', 'ense_goo_id', 'ense_goo_name', 'ense_goo_number', 'ense_goo_waretype', 'ense_goo_warehouse', 'ense_new_inventory', 'ense_end_inventory')
            ->orderBy('ense_id', 'asc')
            ->get()
            ->toArray();
        $dataayyar = array();
        if ($list) {
            // 查询仓库数据
            $warehouse_data = DB::table('warehouse')
                ->where('ware_id', $request['sssg_ware_id'])
                ->select('ware_id', 'ware_name')
                ->first();
            // 查询仓库类别数据
            $warehouse_class = DB::table('warehouse_class')
                ->where('wacl_cid', $adm_cid)
                ->where('wacl_superior_shop_id', $shop_id)
                ->where('wacl_class_numerical', $request['sssg_waon_class'])
                ->select('wacl_class_numerical', 'wacl_class_name')
                ->first();
            $warehouse_data = $warehouse_data->ware_id;
            $warehouse_class = $warehouse_class->wacl_class_numerical;
            $listarray = array();
            $list = json_decode(json_encode($list), true);
            foreach ($list as $k => $v) {
                if (isset($listarray[$v['ense_goo_id']])) {
                    $listarray[$v['ense_goo_id']]['ense_goo_waretype'] = $warehouse_class;
                    $listarray[$v['ense_goo_id']]['ense_goo_warehouse'] = $warehouse_data;
                    $listarray[$v['ense_goo_id']]['ense_end_inventory'] = $v['ense_end_inventory'];
                } else {
                    $v['ense_goo_waretype'] = $warehouse_class;
                    $v['ense_goo_warehouse'] = $warehouse_data;
                    $listarray[$v['ense_goo_id']] = $v;
                }
            }
            $i = "0";
            foreach ($listarray as $k => $v) {
                $dataayyar[$i] = $v;
                $i = $i + 1;
            }
        }

        // dump($dataayyar);
        // exit;
        $stocks_numbers = [];
        foreach ($dataayyar as $k1 => $v1) {
            // dump($v1);
            // dump($v1['ense_goo_warehouse']);
            // 根据仓库id,仓库类别,查询仓库商品表的数量
            $find_wago_data = DB::table('warehouse_goods')
                ->where('wago_ware_id', $v1['ense_goo_warehouse'])
                ->where('wago_class', $v1['ense_goo_waretype'])
                ->where('wago_goo_id', $v1['ense_goo_id'])
                ->select('wago_id', 'wago_stock_number', 'wago_lock_quantity', 'wago_priority_stock_number', 'wago_priority_lock_quantity', 'wago_unchecked_stock_number')
                ->first();
            if ($find_wago_data) {
                // 货物仓
                // $stock_amount = $find_wago_data->wago_stock_number + $find_wago_data->wago_lock_quantity;
                // 旧货仓
                $stock_amount = $find_wago_data->wago_stock_number + $find_wago_data->wago_lock_quantity + $find_wago_data->wago_priority_stock_number + $find_wago_data->wago_priority_lock_quantity + $find_wago_data->wago_unchecked_stock_number;
                // dump($v1['ense_end_inventory']);
                // dump($stock_amount);
                // if ($v1['ense_end_inventory'] != $stock_amount && $v1['ense_end_inventory'] >= 0) {
                if ($v1['ense_end_inventory'] != $stock_amount) {
                    // dump($find_wago_data->wago_id);
                    $stocks_numbers[$k1]['wago_id'] = $find_wago_data->wago_id;
                    $stocks_numbers[$k1]['ense_goo_number'] = $v1['ense_goo_number'];
                    $stocks_numbers[$k1]['ense_end_inventory'] = $v1['ense_end_inventory'];
                    $stocks_numbers[$k1]['stock_amount'] = $stock_amount;
                    // if ($find_wago_data->wago_lock_quantity == 0) {
                    if ($find_wago_data->wago_lock_quantity == 0 && $find_wago_data->wago_priority_lock_quantity == 0) {
                        // 货物仓
                        $edit_wago_data['wago_stock_number'] = $v1['ense_end_inventory']; //良品库存数量
                        // 旧货仓
                        // $edit_wago_data['wago_unchecked_stock_number'] = $v1['ense_end_inventory'] - $find_wago_data->wago_stock_number - $find_wago_data->wago_priority_stock_number; //待检测库存数量
                    } else {
                        // 货物仓
                        $edit_wago_data['wago_stock_number'] = $v1['ense_end_inventory'] - $find_wago_data->wago_lock_quantity; //良品库存数量
                        // 旧货仓
                        // $edit_wago_data['wago_unchecked_stock_number'] = $v1['ense_end_inventory'] - $find_wago_data->wago_stock_number - $find_wago_data->wago_lock_quantity - $find_wago_data->wago_priority_stock_number - $find_wago_data->wago_priority_lock_quantity; //待检测库存数量
                    }
                    // $edit_wago_res = DB::table('warehouse_goods')
                    //     ->where('wago_id', $find_wago_data->wago_id)
                    //     ->update($edit_wago_data);
                    // unset($edit_wago_data);
                    // unset($edit_wago_res);
                }
            }
        }
        dump($stocks_numbers);
        echo 'string';
    }

    // 计算期末金额 与 平均价*库存不相等
    public function getfalsedata($search_data)
    {
        ignore_user_abort();
        set_time_limit(0);
        // $delete_data = DB::table('enters_sells_savess')
        //     ->where('ense_id', '>=', 146126)
        //     ->delete();
        // echo 666;die;
        $enters_data = DB::table('enters_sells_saves')
            ->where(function ($query) use ($search_data) {
                // 如果选择开始时间并且选择结束时间
                if (!empty($search_data['create_time_start']) && !empty($search_data['create_time_end'])) {
                    $query->whereBetween('ense_time', [$search_data['create_time_start'], $search_data['create_time_end'] . ' 23:59:59']);
                } else {
                    // 如果选择开始时间
                    if (!empty($search_data['create_time_start'])) {
                        $query->where('ense_time', '>=', $search_data['create_time_start']);
                    } else {
                        // 如果选择结束时间
                        if (!empty($search_data['create_time_end'])) {
                            $query->where('ense_time', '<=', $search_data['create_time_end'] . ' 23:59:59');
                        }
                    }
                }
            }) //创建时间
            ->get()->toArray();
        $enters_data = json_decode(json_encode($enters_data, true), true);
        $i = 0;
        $j = 0;
        foreach ($enters_data as $key => $value) {
            $difference = $value['ense_end_amount'] - ($value['ense_end_price']*$value['ense_end_inventory']);
            if ($difference != 0 && $difference > 1) {
                $add_data[$i] = $value;
                $add_data[$i]['ense_difference'] = $difference;
                $add_data_res = DB::table('enters_sells_saves_back')->insert($add_data[$i]);
                $i++;
            }
            if ($value['ense_out_num'] < 0 && $value['ense_out_price'] == 0) {
                $add_datas[$j] = $value;
                $add_data_res1 = DB::table('enters_sells_saves_backs')->insert($add_datas[$j]);
                $j++;
            }
        }
        echo $i;
        echo '--------';
        echo $j;
        die;
    }

    // 查询销售统计-销售明细界面的成本为0,跟进销存成本对比,不一致的查询出来 【21.07.02】
    public function selectDataStatistical(Request $request)
    {
        ignore_user_abort();
        set_time_limit(0);

        $search_data = $request->all();

        // 查询销售统计表
        $find_dast_data = DB::table('data_statistical as dast')
            ->leftJoin('goods as b', 'b.goo_id', '=', 'dast.dast_goo_id')
            ->where('dast_cid', 1)
            ->where('dast_shop_id', 1)
            ->where('dast_superior_shop_id', 1)
            ->whereIn('dast_order_type', [1, 2])
            ->where('dast_goo_id', '!=', 0)
            ->where('dast_ord_delete', 1)
            ->where('dast_ord_form_type', 1)
            // ->where('dast_id', '>', 80000)
            // ->where('dast_id', '<=', 100000)
            // ->where('dast_goo_cost', 0)
            ->where('dast_service_type', '!=', 4)
            // ->where('dast_waon_class', '=', 1)
            ->whereIn('dast_good_state', [1, 5, 8])
            // ->where('dast_order_number', 'D2021030915335900082')
            ->where(function ($query) use ($search_data) {
                //如果选择开始时间并且选择结束时间
                if (!empty($search_data['create_time_start']) && !empty($search_data['create_time_end'])) {
                    $query->whereBetween('dast_create_time', [$search_data['create_time_start'], $search_data['create_time_end'] . ' 23:59:59']);
                } else {
                    // 如果选择开始时间
                    if (!empty($search_data['create_time_start'])) {
                        $query->where('dast_create_time', '>=', $search_data['create_time_start']);
                    } else {
                        // 如果选择结束时间
                        if (!empty($search_data['create_time_end'])) {
                            $query->where('dast_create_time', '<=', $search_data['create_time_end'] . ' 23:59:59');
                        }
                    }
                }
            }) //创建时间
            ->select('dast_id', 'dast_ord_id', 'dast_order_number', 'dast_goo_quantity', 'dast_goo_cost', 'dast_goo_id', 'dast_orgo_id', 'dast_ware_id', 'dast_waon_class', 'goo_goods_encode', 'goo_id', 'goo_name', 'dast_create_time', 'dast_goo_price')
            ->get()->toArray();
        foreach ($find_dast_data as $key => $value) {
            // 查询进销存列表的数据
            $ense_end_price = DB::table('enters_sells_saves')
                ->where('ense_shop_id', 1)
                ->where('ense_goo_warehouse', $value->dast_ware_id)
                ->where('ense_goo_waretype', $value->dast_waon_class)
                ->where('ense_goo_id', $value->dast_goo_id)
                ->where('ense_order_number', $value->dast_order_number)
                // ->where('ense_end_price', '!=', 0)
                ->value('ense_end_price');
            if ($ense_end_price != $value->dast_goo_cost) {
                // 根据订单id,查询对应的商品id
                /*$add_data['dast_cid'] = 1;//所属企业id
                $add_data['dast_superior_shop_id'] = 1;//所属总店id
                $add_data['dast_shop_id'] = 1;//所属门店id
                $add_data['dast_ord_id'] = $value->dast_ord_id;//订单id
                $add_data['dast_order_number'] = $value->dast_order_number;//订单单号
                $add_data['dast_service_type'] = 1;
                $add_data['dast_goo_id'] = $value->goo_id;//商品id
                $add_data['dast_goo_name'] = $value->goo_name;//商品名称
                $add_data['dast_goo_encode'] = $value->goo_goods_encode;//检索编码
                $add_data['dast_ense_end_price'] = $ense_end_price;//期末平均价
                $add_data['dast_goo_cost'] = $value->dast_goo_cost / 100;//销售统计单个商品成本
                $add_data['dast_goo_costs'] = $value->dast_goo_cost / 100;//销售统计总成本
                $add_data['dast_vour_costs'] = sprintf("%.2f", $ense_end_price);//凭证列表的成本
                $add_data['dast_diff_cost'] = sprintf("%.2f", $ense_end_price) - $value->dast_goo_cost/100;//凭证列表的成本
                $add_data['dast_add_time'] = $value->dast_create_time;//开单时间
                $dast_id = DB::table('data_statistical_test')->insert($add_data);
                unset($add_data);
                unset($dast_id);*/

                $ense_end_price = sprintf("%.2f", $ense_end_price);
                // 修改订单商品表成本价
                $edit_orgo_data['orgo_goo_cost'] = $ense_end_price * 100;
                // $edit_orgo_data['orgo_goo_cost'] = 0;
                $edit_orgo_res = DB::table('order_goods')
                    ->where('orgo_id', $value->dast_orgo_id)
                    ->update($edit_orgo_data);
                // 修改销售统计表成本价
                $edit_dast_data['dast_goo_cost'] = $ense_end_price * 100;
                // $edit_dast_data['dast_goo_cost'] = 0;
                $edit_dast_res = DB::table('data_statistical')
                    ->where('dast_id', $value->dast_id)
                    ->update($edit_dast_data);
            }
            // if ($value->dast_goo_cost > abs($value->dast_goo_price)) {//成本价大于销售价
            //     // 根据订单id,查询对应的商品id
            //     $add_data_dif['dast_cid'] = 1;//所属企业id
            //     $add_data_dif['dast_superior_shop_id'] = 1;//所属总店id
            //     $add_data_dif['dast_shop_id'] = 1;//所属门店id
            //     $add_data_dif['dast_ord_id'] = $value->dast_ord_id;//订单id
            //     $add_data_dif['dast_order_number'] = $value->dast_order_number;//订单单号
            //     $add_data_dif['dast_service_type'] = 1;
            //     $add_data_dif['dast_goo_id'] = $value->goo_id;//商品id
            //     $add_data_dif['dast_goo_name'] = $value->goo_name;//商品名称
            //     $add_data_dif['dast_goo_encode'] = $value->goo_goods_encode;//检索编码
            //     $add_data_dif['dast_goo_price'] = $value->dast_goo_price / 100;//销售统计单个商品销售价
            //     $add_data_dif['dast_goo_cost'] = $value->dast_goo_cost / 100;//销售统计单个商品成本
            //     $add_data_dif['dast_goo_costs'] = $value->dast_goo_cost / 100;//销售统计总成本
            //     $add_data_dif['dast_vour_costs'] = sprintf("%.2f", $ense_end_price);//凭证列表的成本
            //     $add_data_dif['dast_diff_cost'] = sprintf("%.2f", $ense_end_price) - $value->dast_goo_cost/100;//凭证列表的成本
            //     $add_data_dif['dast_add_time'] = $value->dast_create_time;//开单时间
            //     $dast_ids = DB::table('data_statistical_test_dif')->insert($add_data_dif);
            //     unset($add_data_dif);
            //     unset($dast_ids);
            // }
            unset($ense_end_price);
        }
        echo "string";
        exit;
    }

    // 查询凭证列表已同步U8成本和销售统计成本不一致的 【21.07.02】
    public function syncOrder(Request $request)
    {
        ignore_user_abort();
        set_time_limit(0);

        $search_data = $request->all();
        // 查询销售统计表
        $find_dast_data = DB::table('data_statistical as dast')
            ->leftjoin('order as ord', 'ord.ord_id', '=', 'dast_ord_id')
            ->where('dast_cid', 1)
            ->where('dast_shop_id', 1)
            ->where('dast_superior_shop_id', 1)
            ->whereIn('dast_order_type', [1, 2])
            ->where('dast_goo_id', '!=', 0)
            ->where('dast_ord_delete', 1)
            ->where('dast_ord_form_type', 1)
            // ->where('dast_id', '>', 60000)
            // ->where('dast_id', '<=', 90000)
            ->where('dast_service_type', '!=', 4)
            // ->where('dast_waon_class', '=', 1)
            ->where(function ($query) use ($search_data) {
                // 如果选择开始时间并且选择结束时间
                if (!empty($search_data['create_time_start']) && !empty($search_data['create_time_end'])) {
                    $query->whereBetween('dast_create_time', [$search_data['create_time_start'], $search_data['create_time_end'] . ' 23:59:59']);
                } else {
                    // 如果选择开始时间
                    if (!empty($search_data['create_time_start'])) {
                        $query->where('dast_create_time', '>=', $search_data['create_time_start']);
                    } else {
                        // 如果选择结束时间
                        if (!empty($search_data['create_time_end'])) {
                            $query->where('dast_create_time', '<=', $search_data['create_time_end'] . ' 23:59:59');
                        }
                    }
                }
            }) //创建时间
            ->where(function ($query) use ($search_data) {
                if (!empty($search_data['dast_order_number'])) {
                    $query->where('dast_order_number', 'like', '%' . $search_data['dast_order_number'] . '%');
                }
            }) //单号
            ->select('dast_id', 'dast_ord_id', 'dast_order_number', 'dast_goo_quantity', 'dast_goo_cost', 'dast_goo_id', 'dast_orgo_id')
            ->get()->toArray();
        $dast_subtract = [];

        foreach ($find_dast_data as $kk => $vv) {
            if (!isset($dast_subtract[$vv->dast_ord_id])) {
                $dast_subtract[$vv->dast_ord_id] = 0;
            }
            $dast_subtract[$vv->dast_ord_id] += ($vv->dast_goo_quantity * $vv->dast_goo_cost); //商品数量
        }
        unset($find_dast_data);
        foreach ($dast_subtract as $key => $value) {
            // 查询凭证列表的数据
            $find_vore_data = DB::table('voucher_receipts')
                ->where('vore_cid', 1)
                ->where('vore_shop_id', 1)
                ->where('vore_superior_shop_id', 1)
                ->where('vore_ord_id', $key)
                ->where('vore_state', 2)
                ->where('vore_service_type', '!=', 3)
                ->select('vore_id', 'vore_ord_id', 'vore_ord_number', 'vore_early_costing', 'vore_create_time')
                ->first();
            if ($find_vore_data) {
                // 销售统计总成本
                $dast_goo_cost = abs($value) / 100;
                // 凭证列表总成本
                $find_volo_data = DB::table('voucher_loan')
                    ->where('volo_vore_id', $find_vore_data->vore_id)
                    ->get()->toArray();
                $find_volo_data = json_decode(json_encode($find_volo_data), true);
                $total_cost = $find_vore_data->vore_early_costing;
                // 凭证成本和销售统计成本差值
                $edits_cost = $dast_goo_cost - abs($find_volo_data[0]['volo_borrow_costing']);
                // 成本价差异小于10
                if ($dast_goo_cost != abs($find_volo_data[0]['volo_borrow_costing'])) {
                    // if ($dast_goo_cost != $find_volo_data[0]['volo_borrow_costing'] && abs($edits_cost) < 10 && abs($edits_cost) != 0) {
                    // 成本价差异大于10
                    // if ($dast_goo_cost != $find_volo_data[0]['volo_borrow_costing'] && abs($edits_cost) >= 10 && abs($edits_cost) != 0) {
                    // dump($edits_cost);
                    // continue;
                    // 根据商品id,查询对应的检索编码
                    $find_dats = DB::table("data_statistical as a")
                        ->leftJoin('goods as b', 'b.goo_id', '=', 'a.dast_goo_id')
                        ->leftjoin('order as ord', 'ord.ord_id', '=', 'a.dast_ord_id')
                        ->where('dast_ord_id', $key)
                        ->where('dast_cid', 1)
                        ->where('dast_shop_id', 1)
                        ->where('dast_superior_shop_id', 1)
                        ->whereIn('dast_order_type', [1, 2])
                        ->where('dast_goo_id', '!=', 0)
                        ->where('dast_ord_delete', 1)
                        ->where('dast_ord_form_type', 1)
                        // ->where('dast_id', '>', 60000)
                        // ->where('dast_id', '<=', 90000)
                        ->where('dast_service_type', '!=', 4)
                        // ->where('dast_waon_class', '=', 1)
                        ->select('goo_goods_encode', 'goo_id', 'dast_goo_cost', 'goo_name', 'dast_create_time', 'dast_id', 'dast_orgo_id')
                        ->get()->toArray();
                    // dump($find_dats);
                    // dump(count($find_dats));
                    $count_array = count($find_dats);

                    foreach ($find_dats as $kkk => $vvv) {
                        // 根据订单id,查询对应的商品id
                        $add_data['dast_cid'] = 1; //所属企业id
                        $add_data['dast_superior_shop_id'] = 1; //所属总店id
                        $add_data['dast_shop_id'] = 1; //所属门店id
                        $add_data['dast_ord_id'] = $find_vore_data->vore_ord_id; //订单id
                        $add_data['dast_order_number'] = $find_vore_data->vore_ord_number; //订单单号
                        $add_data['dast_service_type'] = 1;
                        $add_data['dast_goo_id'] = $vvv->goo_id; //商品id
                        $add_data['dast_goo_name'] = $vvv->goo_name; //商品名称
                        $add_data['dast_goo_encode'] = $vvv->goo_goods_encode; //检索编码
                        $add_data['dast_goo_cost'] = $vvv->dast_goo_cost / 100; //销售统计单个商品成本
                        $add_data['dast_goo_costs'] = $dast_goo_cost; //销售统计总成本
                        $add_data['dast_vour_costs'] = $find_volo_data[0]['volo_borrow_costing']; //凭证列表的成本
                        $add_data['dast_diff_cost'] = sprintf("%.2f", $edits_cost); //凭证列表的成本
                        $add_data['dast_add_time'] = $vvv->dast_create_time; //开单时间
                        $dast_id = DB::table('data_statistical_test')->insert($add_data);
                        unset($add_data);
                        unset($dast_id);
                        continue;
                        // 成本价小于10
                        if (($count_array - 1) == $kkk) {
                            // dump($kkk);
                            // dump($vvv);
                            // 修改销售统计列表的成本
                            $edits_cost =  sprintf("%.2f", $edits_cost);
                            $edit_dast_data['dast_goo_cost'] = ($vvv->dast_goo_cost / 100 + $edits_cost) * 100;
                            dump($edits_cost . '差异');
                            dump($vvv->dast_goo_cost / 100);
                            dump($edit_dast_data['dast_goo_cost'] . '修正后的');
                            // $edit_dast_res = DB::table("data_statistical")
                            //     ->where('dast_id', $vvv->dast_id)
                            //     ->update($edit_dast_data);
                            // 修改订单商品列表的成本
                            $edit_orgo_data['orgo_goo_cost'] = ($vvv->dast_goo_cost / 100 + $edits_cost) * 100;
                            // $edit_orgo_res = DB::table("order_goods")
                            //     ->where('orgo_id', $vvv->dast_orgo_id)
                            //     ->update($edit_orgo_data);
                        }
                        continue;
                        // 成本价大于10
                        // 修改销售统计列表的成本
                        // 多商品时,差值/总商品个数
                        $diff_cost = $edits_cost / $count_array;
                        $asss_cost = sprintf("%.2f", ($vvv->dast_goo_cost / 100 + $diff_cost));
                        $edit_dast_data['dast_goo_cost'] = $asss_cost * 100;
                        // dump($count_array . '**********');
                        // dump($edits_cost . '+_+_+_+_');
                        // dump($diff_cost . '!!!!!!');
                        // dump($asss_cost . '&&&&&&');
                        // dump($vvv->dast_goo_cost / 100 . '====');
                        // dump($edit_dast_data['dast_goo_cost'] . '````====````');
                        $edit_dast_res = DB::table("data_statistical")
                            ->where('dast_id', $vvv->dast_id)
                            ->update($edit_dast_data);
                        // 修改订单商品列表的成本
                        $edit_orgo_data['orgo_goo_cost'] = $asss_cost * 100;
                        $edit_orgo_res = DB::table("order_goods")
                            ->where('orgo_id', $vvv->dast_orgo_id)
                            ->update($edit_orgo_data);
                    }
                    unset($find_dats);
                }
            }
            unset($find_vore_data);
        }
        echo "string";
        exit;
    }

    // 统计凭证列表成本和销售统计成本不一致的 【21.07.02】   未同步u8
    public function dataStatistics(Request $request)
    {
        ignore_user_abort();
        set_time_limit(0);

        $search_data = $request->all();

        // 查询销售统计表
        $find_dast_data = DB::table('data_statistical as dast')
            ->leftjoin('voucher_receipts as vore', 'vore.vore_ord_id', '=', 'dast_ord_id')
            ->where('dast_cid', 1)
            ->where('dast_shop_id', 1)
            ->whereIn('dast_order_type', [1, 2])
            ->where('dast_goo_id', '!=', 0)
            ->where('dast_ord_delete', 1)
            ->where('dast_ord_form_type', 1)
            ->where('vore_state', 1)
            ->where('vore_service_type', '!=', 3)
            ->where('dast_service_type', '=', 4)
            ->where('dast_waon_class', '=', 3)
            ->where(function ($query) use ($search_data) {
                //如果选择开始时间并且选择结束时间
                if (!empty($search_data['create_time_start']) && !empty($search_data['create_time_end'])) {
                    $query->whereBetween('dast_create_time', [$search_data['create_time_start'], $search_data['create_time_end'] . ' 23:59:59']);
                } else {
                    // 如果选择开始时间
                    if (!empty($search_data['create_time_start'])) {
                        $query->where('dast_create_time', '>=', $search_data['create_time_start']);
                    } else {
                        // 如果选择结束时间
                        if (!empty($search_data['create_time_end'])) {
                            $query->where('dast_create_time', '<=', $search_data['create_time_end'] . ' 23:59:59');
                        }
                    }
                }
            }) //创建时间
            ->where(function ($query) use ($search_data) {
                if (!empty($search_data['dast_order_number'])) {
                    $query->where('dast_order_number', 'like', '%' . $search_data['dast_order_number'] . '%');
                }
            }) //单号
            // ->where('dast_order_number', 'D2021031011133700040')
            ->select('dast_id', 'dast_ord_id', 'dast_order_number', 'dast_goo_quantity', 'dast_goo_cost', 'dast_goo_id', 'dast_orgo_id')
            ->get()->toArray();
        $dast_subtract = [];
        foreach ($find_dast_data as $kk => $vv) {
            if (!isset($dast_subtract[$vv->dast_ord_id])) {
                $dast_subtract[$vv->dast_ord_id] = 0;
            }
            $dast_subtract[$vv->dast_ord_id] += ($vv->dast_goo_quantity * $vv->dast_goo_cost); //商品数量
        }
        unset($find_dast_data);
        foreach ($dast_subtract as $key => $value) {
            // 查询凭证列表的数据
            $find_vore_data = DB::table('voucher_receipts')
                ->where('vore_cid', 1)
                ->where('vore_shop_id', 1)
                ->where('vore_superior_shop_id', 1)
                ->where('vore_ord_id', $key)
                ->where('vore_state', 1)
                ->where('vore_service_type', '!=', 3)
                ->select('vore_id', 'vore_ord_id', 'vore_ord_number', 'vore_early_costing', 'vore_create_time')
                ->first();
            if ($find_vore_data) {
                // 销售统计总成本
                $dast_goo_cost = abs($value) / 100;
                // 凭证列表总成本
                $find_volo_data = DB::table('voucher_loan')
                    ->where('volo_vore_id', $find_vore_data->vore_id)
                    ->get()->toArray();
                $find_volo_data = json_decode(json_encode($find_volo_data), true);
                $total_cost = abs($find_vore_data->vore_early_costing);
                if (abs($dast_goo_cost) != abs($find_volo_data[0]['volo_borrow_costing'])) {
                    // 根据商品id,查询对应的检索编码
                    $find_dats = DB::table("data_statistical as a")
                        ->leftJoin('goods as b', 'b.goo_id', '=', 'a.dast_actual_goo_id')
                        ->where('dast_ord_id', $key)
                        ->whereIn('dast_order_type', [1, 2])
                        ->where('dast_service_type', '=', 4)
                        ->where('dast_waon_class', '=', 3)
                        ->select('goo_goods_encode', 'goo_id', 'dast_goo_cost', 'goo_name', 'dast_create_time')
                        ->get()->toArray();
                    foreach ($find_dats as $kkk => $vvv) {
                        // 根据订单id,查询对应的商品id
                        $add_data['dast_cid'] = 1; //所属企业id
                        $add_data['dast_superior_shop_id'] = 1; //所属总店id
                        $add_data['dast_shop_id'] = 1; //所属门店id
                        $add_data['dast_ord_id'] = $find_vore_data->vore_ord_id; //订单id
                        $add_data['dast_order_number'] = $find_vore_data->vore_ord_number; //订单单号
                        $add_data['dast_service_type'] = 1;
                        $add_data['dast_goo_id'] = $vvv->goo_id; //商品id
                        $add_data['dast_goo_name'] = $vvv->goo_name; //商品名称
                        $add_data['dast_goo_encode'] = $vvv->goo_goods_encode; //检索编码
                        $add_data['dast_goo_cost'] = $vvv->dast_goo_cost / 100; //销售统计单个商品成本
                        $add_data['dast_goo_costs'] = $dast_goo_cost; //销售统计总成本
                        $add_data['dast_vour_costs'] = $total_cost; //凭证列表的成本
                        $add_data['dast_diff_cost'] = $dast_goo_cost - $total_cost; //凭证列表的成本
                        $add_data['dast_add_time'] = $vvv->dast_create_time; //开单时间
                        $dast_id = DB::table('data_statistical_test')->insert($add_data);
                        unset($add_data);
                        unset($dast_id);
                    }
                    unset($find_dats);
                }
            }
            unset($find_vore_data);
        }
        echo "string";
        exit;
    }

    // 根据销售统计成本修改凭证的成本 【21.07.02】 未同步u8的
    public function editVoucher()
    {
        ignore_user_abort();
        set_time_limit(0);

        // 查询销售统计表
        $find_dast_data = DB::table('data_statistical_test')
            // ->where('dast_vour_costs', 0)
            ->get()->toArray();
        foreach ($find_dast_data as $key => $value) {
            // 查询凭证列表的数据
            $find_vore_data = DB::table('voucher_receipts')
                ->where('vore_cid', 1)
                ->where('vore_shop_id', 1)
                ->where('vore_superior_shop_id', 1)
                ->where('vore_ord_id', $value->dast_ord_id)
                ->where('vore_ord_number', $value->dast_order_number)
                ->where('vore_state', 1)
                ->where('vore_service_type', '!=', 3)
                ->select('vore_id', 'vore_ord_id', 'vore_ord_number', 'vore_early_costing', 'vore_create_time')
                ->first();
            if ($find_vore_data) {
                // 凭证列表总成本
                $find_volo_data = DB::table('voucher_loan')
                    ->where('volo_vore_id', $find_vore_data->vore_id)
                    ->get()->toArray();
                $find_volo_data = json_decode(json_encode($find_volo_data), true);
                if ($find_volo_data) {
                    foreach ($find_volo_data as $kkk => $vvv) {
                        // 修改凭证列表的成本
                        $volo_edit_data['volo_borrow_costing'] = $value->dast_goo_costs;
                        $volo_edit_data['volo_loan_costing'] = $value->dast_goo_costs;
                        $volo_edit_res = DB::table('voucher_loan')
                            ->where('volo_id', $vvv['volo_id'])
                            ->where('volo_vore_id', $find_vore_data->vore_id)
                            ->where('volo_loan_subject', '!=', 0)
                            ->update($volo_edit_data);
                        unset($volo_edit_data);
                        unset($volo_edit_res);
                    }
                }
                echo '2222';
            }
            unset($find_vore_data);
        }
        echo "修改凭证成功";
        exit;
    }

    public function editentererrordata ()
    {
        $find_enter_data = DB::table('enters_sells_saves')
            ->where('ense_id', '>=', '195536')
            ->where('ense_goo_number', '=', 'X1010005')
            ->where('ense_shop_id', 1)
            ->where('ense_goo_warehouse', 151)
            ->where('ense_goo_waretype', 1)
            ->get()->toArray();
        $ense_end_amount = 42431.76980000001;//前一条的期末总额
        $ense_end_price = 286.7011;//前一条的期末平均价
        foreach ($find_enter_data as $key => $value) {
            if ($value->ense_out_num != 0) {
                $update_data['ense_new_price'] = $ense_end_price;
                $update_data['ense_new_amount'] = $ense_end_amount;
                $update_data['ense_out_amount'] = $ense_end_price*$value->ense_out_num;
                $ense_end_amount-=$update_data['ense_out_amount'];
                $ense_end_price = sprintf("%.4f",$ense_end_amount/$value->ense_end_inventory);
                $update_data['ense_end_price'] = $ense_end_price;
                $update_data['ense_end_amount'] = $ense_end_amount;
                $update_data['ense_out_price'] = $ense_end_price;
            } elseif ($value->ense_in_num != 0) {
                $update_data['ense_new_price'] = $ense_end_price;
                $update_data['ense_new_amount'] = $ense_end_amount;
                // $update_data['ense_in_amount'] = $ense_end_price*$value->ense_in_num;
                $ense_end_amount+=$value->ense_in_amount;
                $ense_end_price = sprintf("%.4f",$ense_end_amount/$value->ense_end_inventory);
                $update_data['ense_end_price'] = $ense_end_price;
                $update_data['ense_end_amount'] = $ense_end_amount;
                $update_data['ense_in_price'] = $value->ense_in_price;
            }
            $update_data_res = DB::table('enters_sells_saves')
                ->where('ense_id', $value->ense_id)
                ->update($update_data);
            unset($update_data);
        }
        echo 999;
        die;
    }
}