| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\QcodeLarge;
- use app\admin\model\QcodeSmall;
- use app\common\controller\Backend;
- use app\admin\model\QcodeProduct;
- use app\admin\model\QcodeCompany;
- use app\admin\model\QcodeBach;
- use app\admin\model\QcodeExport;
- use fast\Arr;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- use PhpOffice\PhpSpreadsheet\Exception;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- use app\admin\model\QcodeGoods;
- use think\Config;
- use think\Session;
- class Deliver extends Backend
- {
- /**
- * 首页
- */
- public function index()
- {
- return $this->view->fetch();
- }
- /**
- * 大件发货
- */
- // public function lager()
- // {
- //
- // $company = new QcodeCompany();
- // $product = new QcodeProduct();
- // $large = new QcodeLarge();
- // $bach = new QcodeBach();
- // $small = new QcodeSmall();
- //
- // $this->request->filter(['strip_tags', 'trim']);
- //
- // if (false === $this->request->isAjax()) {
- // return $this->view->fetch();
- // }
- //
- // if ($this->request->request('keyField')) {
- // return $this->selectpage();
- // }
- //
- // $userInfo = Session::get('admin');
- // $where = ['delete_time'=> ''];
- // $filter = input('filter');
- // $filter = json_decode($filter, true);
- //
- // $whereList = ['delete_time'=>''];
- //
- // if (isset($filter['matter_name'])){
- // $whereList['matter_no'] = $filter['matter_name'];
- // }
- // if (isset($filter['bach'])){
- // $whereList['bach_num'] = $filter['bach'];
- // }
- // if (isset($filter['manufacture_date'])){
- // $begin = substr($filter['manufacture_date'], 0, 19);
- // $end = substr($filter['manufacture_date'], 22);
- // $begin = (int)date('ymd', strtotime($begin));
- // $end = (int)date('ymd', strtotime($end));
- // $whereList['manufacture_date'] = ['between', [$begin, $end]];
- // }
- //
- // // 查出相关 bach_id
- // $bach_list = $bach->name($userInfo['company'].'_qcode_bach')->where($whereList)->column('_id');
- // $bach_id = [];
- // foreach ($bach_list as $v){
- // $id = substr(json_encode($v), 9, -2);
- // $bach_id[] = $id;
- // }
- //
- //// // 获取分页参数
- // $offset = input('offset', 0); // 当前页数
- // $limit = input('limit', 10); // 每页多少条
- //
- // // 总数量
- // $total = $large->name($userInfo['company'].'_qcode_large')
- // ->where('l_status', 0)
- // ->where($where)
- // ->whereIn('bach_id', $bach_id)
- // ->count();
- //
- // // 当前页数据
- // $large_list = $large->name($userInfo['company'].'_qcode_large')
- // ->where($where)
- // ->where('l_status', 0)
- // ->whereIn('bach_id', $bach_id)
- // ->limit($limit)
- // ->skip($offset)
- // ->select();
- //
- // // 数据处理
- // $list = [];
- // foreach ($large_list as $k => $v) {
- // $bach_detail = $bach->name($userInfo['company'].'_qcode_bach')->where('_id', $v['bach_id'])->find();
- //
- // $larger_num = (int)$bach_detail['larger_num']; // 总箱数
- // $boxes_per_pallet = (int)$bach_detail['total_boxes']; // 每托盘箱数
- // $small_num = (int)$bach_detail['small_num']; // 每箱数量
- //
- // // 计算总托盘数和最后一托的箱数
- // $full_pallets = floor($larger_num / $boxes_per_pallet);
- // $last_pallet_boxes = $larger_num % $boxes_per_pallet;
- // $total_pallets = $full_pallets + ($last_pallet_boxes > 0 ? 1 : 0);
- //
- // // 当前大件是第几托
- // $current_flow = (int) ltrim(substr($v['code'], 53, 6), '0');
- //
- // // 默认每托盘箱数
- // $total_boxes = $boxes_per_pallet;
- //
- // // 如果是最后一托且有余数,修正箱数
- // if ($current_flow === $total_pallets && $last_pallet_boxes > 0) {
- // $total_boxes = $last_pallet_boxes;
- // }
- //
- //
- // $list[$k]['id'] = substr(json_encode($v['_id']), 9, -2);
- // $list[$k]['l_flow'] = $bach_detail['l_flow'];
- // $list[$k]['bach'] = $bach_detail['bach_num'];
- // $list[$k]['num'] = $bach_detail['num'];
- // $list[$k]['small_num'] = $bach_detail['small_num'];
- // $list[$k]['order_ddbh'] = $bach_detail['order_ddbh'];
- //
- // $list[$k]['danwei'] = $bach_detail['danwei'];
- //
- // $list[$k]['cpbm'] = $bach_detail['cpbm'];
- // $list[$k]['matter_name'] = $bach_detail['matter_name'];
- //// $list[$k]['total_boxes'] = $bach_detail['total_boxes'];
- // $list[$k]['total_boxes'] = $total_boxes;
- // $list[$k]['tray_num'] = $bach_detail['tray_num'];
- // $list[$k]['box_num'] = $bach_detail['box_num'];
- // $list[$k]['pallet_height'] = $bach_detail['pallet_height'];
- // $list[$k]['pallet_length'] = $bach_detail['pallet_length'];
- // $list[$k]['pallet_width'] = $bach_detail['pallet_width'];
- // $list[$k]['larger_num'] = $bach_detail['larger_num'];
- // $list[$k]['manufacture_date'] = date('Y-m-d', strtotime('20'.$bach_detail['manufacture_date']));
- // $list[$k]['code'] = $v['code'];
- // $list[$k]['l_flow'] = ltrim(substr($v['code'], 53, 6), '0');
- //
- //// $small_num = $small->name($userInfo['company'].'_qcode_small')
- //// ->where('large_id', $list[$k]['id'])
- //// ->count();
- //// $list[$k]['small_num'] = $small_num;
- // }
- // // 总数与分页
- //// $list = array_slice($list, $offset, $limit);
- // return json(['total' => $total, 'rows' => $list]);
- // }
- public function lager()
- {
- $company = new QcodeCompany();
- $product = new QcodeProduct();
- $large = new QcodeLarge();
- $bach = new QcodeBach();
- $small = new QcodeSmall();
- $this->request->filter(['strip_tags', 'trim']);
- if (false === $this->request->isAjax()) {
- return $this->view->fetch();
- }
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- $userInfo = Session::get('admin');
- $where = ['delete_time' => ''];
- $filter = input('filter');
- $filter = json_decode($filter, true);
- $whereList = ['delete_time' => ''];
- if (isset($filter['matter_name'])) {
- $whereList['matter_no'] = $filter['matter_name'];
- }
- if (isset($filter['bach'])) {
- $whereList['bach_num'] = $filter['bach'];
- }
- if (isset($filter['manufacture_date'])) {
- $begin = substr($filter['manufacture_date'], 0, 19);
- $end = substr($filter['manufacture_date'], 22);
- $begin = (int)date('ymd', strtotime($begin));
- $end = (int)date('ymd', strtotime($end));
- $whereList['manufacture_date'] = ['between', [$begin, $end]];
- }
- // 查出相关 bach_id
- $bach_list = $bach->name($userInfo['company'].'_qcode_bach')->where($whereList)->column('_id');
- $bach_id = [];
- foreach ($bach_list as $v) {
- $id = substr(json_encode($v), 9, -2);
- $bach_id[] = $id;
- }
- // 获取分页参数
- $offset = input('offset', 0);
- $limit = input('limit', 10);
- // 获取所有大件记录用于总量判断
- $all_large_list = $large->name($userInfo['company'].'_qcode_large')
- ->where($where)
- ->where('l_status', 0)
- ->whereIn('bach_id', $bach_id)
- ->select();
- // 统计总数量
- $total = count($all_large_list);
- // 分页
- $large_list = array_slice($all_large_list, $offset, $limit);
- $list = [];
- foreach ($large_list as $k => $v) {
- $bach_detail = $bach->name($userInfo['company'].'_qcode_bach')->where('_id', $v['bach_id'])->find();
- $num = (int)$bach_detail['num']; // 总张数
- $small_num = (int)$bach_detail['small_num']; // 每箱张数
- $boxes_per_pallet = (int)$bach_detail['total_boxes']; // 每托箱数
- // 重新计算总箱数
- $larger_num = (int)ceil($num / $small_num);
- // 当前批次对应的总托数
- $full_pallets = floor($larger_num / $boxes_per_pallet);
- $last_pallet_boxes = $larger_num % $boxes_per_pallet;
- $total_pallets = $full_pallets + ($last_pallet_boxes > 0 ? 1 : 0);
- // 当前是第几托(code中解析)
- $current_flow_str = substr($v['code'], 53, 6);
- $current_flow = (int) ltrim($current_flow_str, '0');
- $total_pallets = (int) $total_pallets;
- // 是否是最后一托
- $is_last = ($current_flow === $total_pallets) ? 1 : 0;
- // 修正箱数
- $total_boxes = $boxes_per_pallet;
- if ($is_last && $last_pallet_boxes > 0) {
- $total_boxes = $last_pallet_boxes;
- }
- $list[$k] = [
- 'id' => substr(json_encode($v['_id']), 9, -2),
- 'l_flow' => $current_flow,
- 'bach' => $bach_detail['bach_num'],
- 'num' => $num,
- 'small_num' => $small_num,
- 'order_ddbh' => $bach_detail['order_ddbh'],
- 'danwei' => $bach_detail['danwei'],
- 'cpbm' => $bach_detail['cpbm'],
- 'matter_name' => $bach_detail['matter_name'],
- 'total_boxes' => $total_boxes,
- 'tray_num' => $bach_detail['tray_num'],
- 'box_num' => $bach_detail['box_num'],
- 'pallet_height' => $bach_detail['pallet_height'],
- 'pallet_length' => $bach_detail['pallet_length'],
- 'pallet_width' => $bach_detail['pallet_width'],
- 'larger_num' => $larger_num,
- 'manufacture_date' => date('Y-m-d', strtotime('20' . $bach_detail['manufacture_date'])),
- 'code' => $v['code'],
- 'is_last' => $is_last,
- ];
- }
- return json(['total' => $total, 'rows' => $list]);
- }
- /**
- * 小件列表
- * @return \think\response\Json
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function smallList()
- {
- $userinfo = Session::get('admin');
- $lager = new QcodeLarge();
- $small = new QcodeSmall();
- if ($this->request->isAjax() === false){
- $this->error('请求错误');
- }
- $filter = input('filter');
- $filter = json_decode($filter,true);
- $order = input('order');
- $offset = input('offset');
- $limit = input('limit');
- $where = [
- 'delete_time' => '',
- ];
- if (isset($filter['large_id'])){
- $where['large_id'] =$filter['large_id'];
- }
- $smallList = $small->name($userinfo['company'].'_'.'qcode_small')
- ->where($where)
- ->skip($offset)
- ->limit($limit)
- ->select();
- $total = $small->name($userinfo['company'].'_'.'qcode_small')->where($where)->count();
- $list = [];
- foreach ($smallList as $k=>$v){
- $list[$k] = [
- 'code' => $v['code'],
- 'l_flow' => $v['l_flow'],
- 'print_num' => $v['p_nums'],
- 'status' => $v['status'],
- ];
- }
- return json(['total'=>$total,'rows'=>$list]);
- }
- /**
- * 导出发货
- * @return void
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function print()
- {
- $userinfo = Session::get('admin');
- $lager = new QcodeLarge();
- $bach = new QcodeBach();
- $small = new QcodeSmall();
- $export = new QcodeExport();
- if ($this->request->isAjax() === false){
- $this->error('请求错误');
- }
- $lager_id = input('lager_id');
- if ($lager_id === ''){
- $this->error('请至少选择一个大件');
- }
- $lagerId = [];
- if (strpos($lager_id,',') === false){
- $lagerId[0] = $lager_id;
- }else{
- $lagerId = explode(',',$lager_id);
- }
- $lager_num = count($lagerId);
- $bach_id = [];
- foreach ($lagerId as $k=>$v){
- $lagerList = $lager->name($userinfo['company'].'_'.'qcode_large')->where('_id',$v)->find();
- $bachList = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$lagerList['bach_id'])->find();
- $bach_id[$k] = substr(json_encode($bachList['_id']),9,-2);
- }
- $bach_id = array_unique($bach_id);
- $data = [];
- foreach ($bach_id as $k=>$v){
- $data[$k] = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$v)->field('matter_no')->find()['matter_no'];
- }
- $data = array_unique($data);
- if (count($data)>1){
- $this->error('请选择同一产品');
- }
- $n =0;
- foreach (explode(',',$lager_id) as $kk=>$vv){
- $n = $n + $small->name($userinfo['company'].'_'.'qcode_small')->where('large_id',$vv)->count();
- }
- // $bachDetail = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$bach_id[0])->find();
- $bachArray = $bach->name($userinfo['company'].'_'.'qcode_bach')
- ->where('_id', $bach_id[0])
- ->find();
- $bachDetail = $bachArray ? $bachArray->toArray() : [];
- if ($bachDetail['box_num'] === 1){
- $mater_type = 2;
- }else{
- $mater_type = 1;
- }
- $exportData = $this->exportdata($bach_id,$lager_id,$userinfo['company'],$lager_num,$n);
- $file_dir = $this->exportExcel($exportData);
- $row = [
- 'username' => $bachDetail['supplier_name'],//公司
- 'matter_no' => $bachDetail['matter_no'],
- 'order_ddbh' => $bachDetail['order_ddbh'],
- 'large_str' => $lager_id,
- 'status' => 0,
- 'bach_num' => $bachDetail['bach_num'],
- 'note' => '',
- 'user_id' => $userinfo['id'],
- 'create_time' => time(),
- 'file_dir' => $file_dir,
- 'company_id' => $userinfo['company'],
- 'large_num' => $lager_num,//你已选中 N 个大件【托盘数】
- 'small_num' => $n,//共包含了 N 个小件
- 'matter_name' => $bachDetail['matter_name'],//产品名称
- 'total_boxes' => $bachDetail['total_boxes'],//每托箱数
- 'tray_num' => $bachDetail['tray_num'],//每层箱数
- 'box_num' => $bachDetail['box_num'],//每托层数
- 'pallet_height' => $bachDetail['pallet_height'],//每托高度
- 'pallet_length' => $bachDetail['pallet_length'],//托盘规格
- 'pallet_width' => $bachDetail['pallet_width'],//托盘规格
- 'larger_num' => $bachDetail['larger_num'],//总箱数
- ];
- if ($bachDetail['box_num'] != 1){
- $row['num'] = $bachDetail['box_num'];
- $row['mater_type'] = 1;
- }else{
- $row['num'] = $bachDetail['s_weight'];
- $row['mater_type'] = 2;
- }
- $res = $export->save($row);
- if ($res === false){
- $this->error('发货失败');
- }
- $lager = new QcodeLarge();
- $lager->name($userinfo['company'].'_'.'qcode_large')->whereIn('_id',$lagerId)->update(['l_status'=>1]);
- $this->success('成功','',$file_dir);
- }
- /**
- * 表格数据处理
- * @param $bach_id
- * @param $large_str
- * @param $company
- * @param $large_num
- * @param $small_num
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function exportdata($bach_id,$large_str,$company,$large_num,$small_num)
- {
- $bach = new QcodeBach();
- $large = new QcodeLarge();
- $small = new QcodeSmall();
- $bachList = $bach->name($company . '_' . 'qcode_bach')->where('_id', $bach_id[0])->find();
- $data = [
- 'supplier_name' => $bachList['supplier_name'],
- 'supplier_no' => $bachList['supplier_code'],
- 'matter_name' => $bachList['matter_name'],
- 'matter_no' => $bachList['matter_no'],
- 'manufacture_date' => date('Y/m/d',strtotime('20'.$bachList['manufacture_date'])),
- 'print_date' => date('Y/m/d',strtotime('20'.$bachList['print_date'])),
- 'box_num' => $large_num,
- 'tray_num' => $bachList['tray_num'],
- 'small_num' => $small_num,
- 'l_reservation' => $bachList['l_reservation'],
- 's_reservation' => $bachList['s_reservation'],
- 'l_flow' => $bachList['l_flow'],
- 's_flow' => $bachList['s_flow'],
- 'l_weight' => 0,
- 's_weight' => $bachList['s_weight']/1000,
- 'code' => [],
- ];
- $large_id = explode(',', $large_str);
- foreach ($large_id as $k => $v) {
- $largeList = $large->name($company . '_' . 'qcode_large')->where('_id', $v)->find();
- $data['l_weight'] = $data['l_weight'] + (int)$largeList['l_weight']/100;
- $small_list = $small->name($company . '_' . 'qcode_small')->where('large_id', $v)->select();
- foreach ($small_list as $value) {
- $code = [
- 'large_code' => $largeList['code'],
- 'small_code' => $value['code'],
- ];
- array_push($data['code'],$code);
- }
- }
- return $data;
- }
- /**
- * 生成excel
- * @param $data
- * @return string
- * @throws Exception
- * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
- */
- public function exportExcel($data)
- {
- $objexcel = new Spreadsheet();
- $sheet = $objexcel->getActiveSheet();
- //设置全局单元格垂直居中
- $objexcel->getDefaultStyle()->getAlignment()->setVertical(Alignment::HORIZONTAL_CENTER);
- //设置全局单元格水平居中
- $objexcel->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
- //设置全局单元格自动换行
- $objexcel->getDefaultStyle()->getAlignment()->setWrapText(true);
- //设置单元格宽度
- $sheet->getDefaultColumnDimension()->setWidth(50);
- //设置表头
- $sheet->setCellValue('A1', '文件流水号');
- $sheet->setCellValue('A2', '订单号');
- $sheet->setCellValue('A3', '供应商');
- $sheet->setCellValue('A4', '供应商代码');
- $sheet->setCellValue('A5', '物料名称');
- $sheet->setCellValue('A6', '物料代码');
- $sheet->setCellValue('A7', '生产日期');
- $sheet->setCellValue('A8', '打码日期');
- $sheet->setCellValue('A9', '大件数量');
- $sheet->setCellValue('A10', '托盘小件数量');
- $sheet->setCellValue('A11', '小件总数量');
- $sheet->setCellValue('C3', '大件预留码');
- $sheet->setCellValue('C5', '小件预留码');
- $sheet->setCellValue('C7', '大件开始流水号');
- $sheet->setCellValue('C9', '小件开始流水号');
- $sheet->setCellValue('C11', '大件重量(Kg)');
- $sheet->setCellValue('C13', '小件重量(Kg)');
- $sheet->setCellValue('A15', '大件二维码');
- $sheet->setCellValue('B15', '大件条码(预留)');
- $sheet->setCellValue('C15', '小件二维码');
- $sheet->setCellValue('D15', '小件条码(预留)');
- //插入表格数据
- $sheet->getCell('B3')->setValue(isset($data['supplier_name'])?$data['supplier_name']:'');
- $sheet->getCell('B4')->setValue(isset($data['supplier_no'])?$data['supplier_no']:'');
- $sheet->getCell('B5')->setValue(isset($data['matter_name'])?$data['matter_name']:'');
- $sheet->getCell('B6')->setValue(isset($data['matter_no'])?$data['matter_no']:'');
- $sheet->getCell('B7')->setValue(isset($data['manufacture_date'])?$data['manufacture_date']:'');
- $sheet->getCell('B8')->setValue(isset($data['print_date'])?$data['print_date']:'');
- $sheet->getCell('B9')->setValue(isset($data['box_num'])?$data['box_num']:'');
- $sheet->getCell('B10')->setValue(isset($data['tray_num'])?$data['tray_num']:'');
- $sheet->getCell('B11')->setValue(isset($data['small_num'])?$data['small_num']:'');
- $sheet->getCell('C4')->setValue(isset($data['l_reservation'])?$data['l_reservation']:'');
- $sheet->getCell('C6')->setValue(isset($data['s_reservation'])?$data['s_reservation']:'');
- $sheet->getCell('C8')->setValue(isset($data['l_flow'])?$data['l_flow']:'');
- $sheet->getCell('C10')->setValue(isset($data['s_flow'])?$data['s_flow']:'');
- $sheet->getCell('C12')->setValue(isset($data['l_weight'])?$data['l_weight']:'');
- $sheet->getCell('C14')->setValue(isset($data['s_weight'])?$data['s_weight']:'');
- foreach ($data['code'] as $k=>$v){
- $m = $k+16;
- $sheet->getCell('A'.$m)->setValue(isset($v['large_code'])?$v['large_code']:'');
- $sheet->getCell('B'.$m)->setValue('');
- $sheet->getCell('C'.$m)->setValue(isset($v['small_code'])?$v['small_code']:'');
- $sheet->getCell('D'.$m)->setValue('');
- }
- //文件另存
- $filename = date('Ymd',time()).rand(100,999).'_'.$data['supplier_name'].'_'.str_replace('/','_',$data['matter_name']).'_'.$data['small_num'].'.xlsx';
- // $filename = iconv("utf-8","gb2312",$filename);
- header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
- header('Content-Disposition:attachment;filename='.$filename);
- header('Cache-Control: max-age=0');
- header("Content-Type:text/html;charset=utf-8");
- if(is_dir(ROOT_PATH.'public/uploads/export/') === false)
- {
- mkdir(ROOT_PATH.'public/uploads/export/',0777,true);
- }
- $writer = IOFactory::createWriter($objexcel, 'Xlsx');
- $writer->save(ROOT_PATH.'public/uploads/export/'.$filename);
- return 'uploads/export/'.$filename;
- }
- /**
- * 批次发货申请
- * @return string|\think\response\Json
- * @throws \think\Exception
- */
- public function apply()
- {
- $export = new QcodeExport();
- $this->request->filter(['strip_tags', 'trim']);
- if (false === $this->request->isAjax()) {
- return $this->view->fetch();
- }
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- // 接收参数
- $userInfo = Session::get('admin');
- $where = [
- 'company_id' => $userInfo['company'],
- 'delete_time'=> ''
- ];
- $filter = input('filter');
- $filter = json_decode($filter,true);
- //样品编号查询
- if (isset($filter['matter_name'])){
- $where['matter_no'] =$filter['matter_name'];
- }
- if (isset($filter['username'])){
- $where['username'] = $filter['username'];
- }
- if (isset($filter['status'])){
- $where['status'] = (int)$filter['status'];
- }
- if (isset($filter['create_time'])){
- $begin = substr($filter['create_time'],0,19);
- $end = substr($filter['create_time'],22);
- $begin = strtotime($begin);
- $end = strtotime($end);
- $where['create_time'] = ['between',[$begin,$end]];
- }
- $order = input('order');
- $offset = input('offset');
- $limit = input('limit');
- $total = $export->where($where)->count();
- $export_list = $export
- ->order('_id',$order)
- ->where($where)
- ->whereIn('status',[0,1])
- ->skip($offset)
- ->limit($limit)
- ->select();
- $list=[];
- foreach ($export_list as $k=>$v) {
- $list[$k]['large_num'] = $v['large_num'];//托盘数量
- $list[$k]['total_boxes'] = $v['total_boxes'];//每箱托数
- $list[$k]['tray_num'] = $v['tray_num'];//每层箱数
- $list[$k]['box_num'] = $v['box_num'];//每托层数
- $list[$k]['pallet_height'] = $v['pallet_height'];//托盘高度
- $list[$k]['pallet_length'] = $v['pallet_length'];//托盘规格
- $list[$k]['pallet_width'] = $v['pallet_width'];//托盘规格
- $list[$k]['small_num'] = $v['small_num'];//总箱数
- $list[$k]['larger_num'] = $v['larger_num'];//总箱数
- $list[$k]['id'] = substr(json_encode($v['_id']),9,-2);
- $list[$k]['matter_name'] = $v['matter_name'];
- $list[$k]['matter_no'] = $v['matter_no'];
- $list[$k]['username'] = $v['username'];
- $list[$k]['large_num'] = $v['large_num'];
- $list[$k]['small_num'] = $v['small_num'];
- $list[$k]['create_time'] = $v['create_time'];
- $list[$k]['file_dir'] = $v['file_dir'];
- $list[$k]['status'] = $v['status'];
- }
- $result = ['total'=>$total,'rows'=>$list];
- return json($result);
- }
- /**
- * 批次发货删除
- * @param $ids
- * @return void
- */
- public function apply_del($ids)
- {
- $export = new QcodeExport();
- $userinfo = Session::get('admin');
- $large = new QcodeLarge();
- if (empty($ids)){
- $this->error('请求错误');
- }
- $res = $export->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
- if ($res === false){
- $this->error('删除失败');
- }
- $large_list = $export->where('_id',$ids)->find();
- $large_id = explode(',',$large_list['large_str']);
- foreach ($large_id as $v){
- $result = $large->name($userinfo['company'].'_'.'qcode_large')->where('_id',$v)->update(['l_status'=>0]);
- if ($result === 0){
- $this->error('删除失败');
- }
- }
- $this->success('成功');
- }
- /**
- * 司机弹窗
- * @param $ids
- * @return string
- * @throws \think\Exception
- */
- public function goods($ids)
- {
- $this->view->assign('ids',$ids);
- return $this->view->fetch();
- }
- /**
- * 批次发货提交
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function apply_add()
- {
- $goods = new QcodeGoods();
- $export = new QcodeExport();
- $userinfo = Session::get('admin');
- if ($this->request->isAjax() === false){
- $this->error('请求错误');
- }
- $ids = input('ids');
- $order_number = input('order_number');
- $deliveryman = input('deliveryman');
- $shr_phone = input('shr_phone');
- $plate_number = input('plate_number');
- $note = input('note');
- $id_list = [];
- if (strpos($ids,',') === false){
- $id_list[0] = $ids;
- }else{
- $id_list = explode(',',$ids);
- }
- $update_status = $export->where('company_id',$userinfo['company'])->whereIn('_id',$id_list)->update(['status'=>2]);
- if ($update_status){
- $export_list = $export->whereIn('_id',$id_list)->select();
- $daterq = date('ymd',time());
- $data = [
- 'shdh' => substr($daterq,2,2).' '.substr($daterq,2,6).mt_rand(1000,9999),
- 'order_number' => $order_number,
- 'deliveryman' => $deliveryman,
- 'shr_phone' => $shr_phone,
- 'plate_number' => $plate_number,
- 'shrq_date' => '',
- 'supplier_name' => $export_list[0]['username'],
- 'supplier_id' => $userinfo['id'],
- 'user_id' => $userinfo['id'],
- 'status' => 0,
- 'export_id' => $ids,
- 'create_time' => time(),
- 'note' => $note,
- 'company_id' => $userinfo['company'],
- ];
- $data['qrcode_add'] = $this->Qrcode($data['shdh']);
- $res = $goods->save($data);
- if ($res === false){
- $this->error('失败');
- }
- $this->success('成功');
- }else{
- $this->error('失败');
- }
- }
- /**
- * 生成二维码
- * @param $url
- * @return string
- */
- public function Qrcode($url)
- {
- $level=2;
- $size=5;
- Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
- $errorCorrectionLevel =intval($level) ;//容错级别
- $matrixPointSize = intval($size);//生成图片大小
- //生成二维码图片
- $object = new \QRcode();
- //打开缓冲区
- ob_start();
- $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
- //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
- $imageString = base64_encode(ob_get_contents());
- //关闭缓冲区
- ob_end_clean();
- //把生成的base64字符串返回给前端
- return 'data:image/png;base64,'.$imageString;
- }
- /**
- * 发货单
- * @return string|\think\response\Json
- * @throws \think\Exception
- */
- public function dispatch()
- {
- $export = new QcodeGoods();
- $this->request->filter(['strip_tags', 'trim']);
- if (false === $this->request->isAjax()) {
- return $this->view->fetch();
- }
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- // 接收参数
- $userInfo = Session::get('admin');
- $where = [
- 'company_id' => $userInfo['company'],
- 'delete_time'=> ''
- ];
- $search = input('search');
- if (isset($search)){
- $where['order_number|supplier_name|status|plate_number|shdh|deliveryman'] = new \MongoDB\BSON\Regex($search);
- }
- $filter = input('filter');
- $filter = json_decode($filter,true);
- //订单编号查询
- if (isset($filter['order_number'])){
- $where['order_number'] =$filter['order_number'];
- }
- //所属公司查询
- if (isset($filter['supplier_name'])){
- $where['supplier_name'] = $filter['supplier_name'];
- }
- if (isset($filter['status'])){
- $where['status'] = (int)$filter['status'];
- }
- if (isset($filter['create_time'])){
- $begin = substr($filter['create_time'],0,19);
- $end = substr($filter['create_time'],22);
- $begin = strtotime($begin);
- $end = strtotime($end);
- $where['create_time'] = ['between',[$begin,$end]];
- }
- $order = input('order');
- $offset = input('offset');
- $limit = input('limit');
- $total = $export->where($where)->count();
- $export_list = $export
- ->order('_id',$order)
- ->where($where)
- ->whereIn('status',[0,1])
- ->skip($offset)
- ->limit($limit)
- ->select();
- $list=[];
- foreach ($export_list as $k=>$v) {
- $list[$k]['id'] = substr(json_encode($v['_id']),9,-2);
- $list[$k]['shdh'] = $v['shdh'];
- $list[$k]['order_number'] = $v['order_number'];
- $list[$k]['supplier_name'] = $v['supplier_name'];
- $list[$k]['deliveryman'] = $v['deliveryman'];
- $list[$k]['plate_number'] = $v['plate_number'];
- $list[$k]['create_time'] = $v['create_time'];
- $list[$k]['status'] = $v['status'];
- }
- $result = ['total'=>$total,'rows'=>$list];
- return json($result);
- }
- /**
- * 发货单打印
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function printqrcode()
- {
- $goods = new QcodeGoods();
- $export = new QcodeExport();
- $large = new QcodeLarge();
- $userinfo = Session::get('admin');
- if ($this->request->isAjax() === false) {
- $this->error('请求错误');
- }
- $id = input('id');
- $good_list = $goods->where('_id', $id)->find();
- if ($good_list) {
- if (strpos($good_list['export_id'], ',') === false) {
- $export_id[0] = $good_list['export_id'];
- } else {
- $export_id = explode(',', $good_list['export_id']);
- }
- $data = [];
- $motter_nos = []; // 用于存储 motter_no 字段
- foreach ($export_id as $k => $v) {
- $data[$k] = $export->where('_id', $v)->find();
- // 确保 $data[$k] 是一个有效的对象
- if ($data[$k] instanceof QcodeExport) {
- if (!empty($data[$k]['order_ddbh'])) {
- $motter_nos[] = $data[$k]['order_ddbh'];
- }
- }
- $large_str = explode(',', $data[$k]['large_str']);
- $large_list = $large->name($userinfo['company'] . '_' . 'qcode_large')->whereIn('_id', $large_str)->select();
- $large_num = $large_weight = 0;
- foreach ($large_list as $value) {
- $large_num += $value['l_num']; // 累加数量
- $large_weight += $value['l_weight']; // 累加重量
- }
- // 更新数据
- $data[$k]['l_num'] = $large_num / 10000; // 转换数量
- $data[$k]['l_weight'] = $large_weight; // 更新重量
- // 添加 motter_no 到数组
- if (!empty($data[$k]['order_ddbh'])) {
- $order_ddbh[] = $data[$k]['order_ddbh'];
- }
- }
- // 拼接 motter_no 字段
- $good_list['order_ddbh'] = implode(',', $order_ddbh);
- // 继续处理其他数据
- $good_list['address'] = $userinfo['company_address'];
- $good_list['data'] = $data;
- $good_list['count'] = count($data);
- $good_list['shrq_date'] = substr($good_list['create_time'], 0, 10);
- if (empty($good_list['qrcode_add'])) {
- $good_list['qrcode_add'] = $this->Qrcode($good_list['shdh']);
- $goods->where('_id', $id)->update(['qrcode_add' => $good_list['qrcode_add']]);
- }
- if ($good_list['note'] === 'NULL') {
- $good_list['note'] = '';
- }
- // 新增代码
- $good_array = $good_list ? $good_list->toArray() : [];
- $dataList = $good_array['data'] ?? [];
- // 步骤1:按照 create_time 升序排序
- usort($dataList, function ($a, $b) {
- return strtotime($a['create_time']) - strtotime($b['create_time']);
- });
- // 步骤2:添加 large_ber 字段
- $start = 1;
- foreach ($dataList as &$item) {
- $count = intval($item['large_num']);
- $end = $start + $count - 1;
- $item['large_ber'] = ($start === $end) ? (string)$start : "$start-$end";
- $start = $end + 1;
- }
- unset($item);
- $good_list['data'] = $dataList;
- return json(['code' => 1, 'data' => $good_list]);
- }
- }
- // public function printqrcode()
- // {
- // $goods = new QcodeGoods();
- // $export = new QcodeExport();
- // $large = new QcodeLarge();
- // $userinfo = Session::get('admin');
- // if ($this->request->isAjax() === false) {
- // $this->error('请求错误');
- // }
- // $id = input('id');
- // $good_list = $goods->where('_id',$id)->find();
- //
- // if ($good_list){
- // if (strpos($good_list['export_id'],',') === false){
- // $export_id[0] = $good_list['export_id'];
- // }else{
- // $export_id = explode(',',$good_list['export_id']);
- // }
- // $data = [];
- // foreach ($export_id as $k=>$v){
- // $data[$k] = $export->where('_id',$v)->find();
- // $large_str = explode(',',$data[$k]['large_str']);
- // $large_list = $large->name($userinfo['company'].'_'.'qcode_large')->whereIn('_id',$large_str)->select();
- // $large_num = $large_weight = 0;
- // foreach ($large_list as $value){
- // $large_num = $large_num + $value['l_num'];
- // $large_weight = $large_weight + $value['l_weight'];
- // }
- // $data[$k]['l_num'] = $large_num/10000;
- // $data[$k]['l_weight'] = $large_weight;
- // }
- // $good_list['address'] = $userinfo['company_address'];
- // $good_list['data'] = $data;
- // $good_list['count'] = count($data);
- // $good_list['shrq_date'] = substr($good_list['create_time'],0,10) ;
- // if (empty($good_list['qrcode_add'])){
- // $good_list['qrcode_add'] = $this->Qrcode($good_list['shdh']);
- // $goods->where('_id',$id)->update(['qrcode_add'=>$good_list['qrcode_add']]);
- // }
- // if ($good_list['note'] == 'NULL'){
- // $good_list['note'] = '';
- // }
- //
- // //新增代码
- // $good_array = $good_list ? $good_list->toArray() : [];
- // // 假设 $goodArray 是已经转为普通数组的结构
- // $dataList = $good_array['data'] ?? [];
- // // 步骤1:按照 create_time 升序排序
- // usort($dataList, function ($a, $b) {
- // return strtotime($a['create_time']) - strtotime($b['create_time']);
- // });
- // // 步骤2:添加 large_ber 字段
- // $start = 1;
- // foreach ($dataList as &$item) {
- // $count = intval($item['large_num']);
- // $end = $start + $count - 1;
- // $item['large_ber'] = ($start === $end) ? (string)$start : "$start-$end";
- // $start = $end + 1;
- // }
- // unset($item);
- // $good_list['data'] = $dataList;
- //
- // return json(['code'=>1,'data'=>$good_list]);
- // }
- // }
- /**
- * 获取辅料名称
- * @return \think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function matterName()
- {
- $userinfo = Session::get('admin');
- $lager = new QcodeLarge();
- $bach = new QcodeBach();
- $bach_id = $lager->name($userinfo['company'].'_'.'qcode_large')->where('delete_time','')->column('bach_id');
- $matter_name = [];
- if (!empty($bach_id)){
- foreach ($bach_id as $v){
- $bach_list = $bach->name($userinfo['company'].'_'.'qcode_bach')->where('_id',$v)->find();
- $matter_name[$bach_list['matter_no']] = $bach_list['matter_name'];
- }
- }
- $matter_name = array_unique($matter_name);
- return json($matter_name);
- }
- /**
- * 发货单删除
- * @param $ids
- * @return void
- */
- public function dispatch_del($ids)
- {
- $goods = new QcodeGoods();
- $export = new QcodeExport();
- if (empty($ids)){
- $this->error('参数错误');
- }
- $res = $goods->where('_id',$ids)->update(['delete_time'=>date('Y-m-d H:i:s',time())]);
- if ($res === false){
- $this->error('删除失败');
- }
- $export_list = $goods->where('_id',$ids)->find();
- $export_id = explode(',',$export_list['export_id']);
- foreach ($export_id as $v){
- $result = $export->where('_id',$v)->update(['status'=>0]);
- if ($result === false){
- $this->error('删除失败');
- }
- }
- $this->success('成功');
- }
- /**
- * 收货列表
- */
- public function receive()
- {
- //当前是否为关联查询
- $this->relationSearch = false;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- $where = [
- 'delete_time'=> ''
- ];
- $req = input();
- $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
- $order = $req['order'];
- $offset = $req['offset'];
- $limit = $req['limit'];
- // 构造模糊查询条件
- // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
- // $filter = ['field' => $regex];
- $filter = json_decode($req['filter'], true);
- if (isset($filter['status'])){
- $where['status'] = intval($filter['status']);
- unset($filter['status']);
- }
- if (isset($filter['create_time'])){
- $begin = substr($filter['create_time'],0,19);
- $end = substr($filter['create_time'],22);
- $begin = strtotime($begin);
- $end = strtotime($end);
- $where['create_time'] = ['between',[$begin,$end]];
- unset($filter['create_time']);
- }
- foreach ($filter as $k => $v){
- $where[$k] = new \MongoDB\BSON\Regex($v);
- }
- $goods = new QcodeGoods();
- $total = $goods->where($where)->count();
- $list = $goods->where($where)
- ->order($sort,$order)
- ->limit($limit)
- ->skip($offset)
- ->select();
- foreach ($list as $k=>$v) {
- $oid = $v['_id']->jsonSerialize();
- $list[$k]['id'] = $oid['$oid'];
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 扫码/输入收货
- */
- public function receive_add()
- {
- $req = $this->request->param();
- if(!isset($req['shdh']) || empty($req['shdh'])){
- $this->error('请输入送货单号');
- }
- $data = [
- 'shdh'=>$req['shdh'],
- 'delete_time'=>'',
- ];
- $goods = new QcodeGoods();
- $row = $goods->where($data)->find();
- if (!$row) $this->error('未查询到该单号');
- if ($row['status']==2) $this->error('该单号已收货');
- //收货---修改status为2
- $bool = $goods->where($data)->update(['status'=>2,'sync_flag'=>0]);
- if ($bool){
- $this->success('成功');
- }else{
- $this->error('失败');
- }
- }
- /**
- * 取消收货
- */
- public function receive_del()
- {
- $req = $this->request->param();
- if(!isset($req['ids']) || empty($req['ids'])){
- $this->error('id不能为空');
- }
- $data = [
- '_id'=>$req['ids'],
- 'delete_time'=>'',
- ];
- $goods = new QcodeGoods();
- //收货---修改status为2
- $bool = $goods->where($data)->update(['status'=>0,'sync_flag'=>0]);
- if ($bool){
- $this->success('成功');
- }else{
- $this->error('失败');
- }
- }
- }
|