| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\QcodeCompany;
- use app\admin\model\QcodeGsmc;
- use app\admin\model\QcodeProduct;
- use app\common\controller\Backend;
- use app\admin\model\ResetFlow;
- use app\admin\model\QcodeLarge;
- use app\admin\model\QcodeBach;
- use app\admin\model\QcodeSmall;
- use app\admin\model\QcodeLiushui;
- use fast\Arr;
- use MongoDB\BSON\ObjectId;
- use think\Db;
- use think\Session;
- class QcodeAdd extends Backend{
- /**
- * 首页展示
- */
- public function index(){
- //获取登录账号信息【厂商信息】
- $userinfo = Session::get('admin');
- $data = [
- 'nickname' => $userinfo['company_name'],
- 'postcode' => $userinfo['postcode'],
- 'mobile' => $userinfo['mobile'],
- 'printer_code' => $userinfo['printer_code'],
- 'company_address' => $userinfo['company_address']
- ];
- $this->view->assign('row',$data);
- //将对此账号创建公司唯一id,进行创建公司对应公司表名company id
- $user_company_value = Db::name('admin')->where('id', $userinfo['id'])->value('company');
- if (empty($user_company_value)) {
- $max_company = Db::name('admin')->max('company');
- $new_company_value = $max_company ? ($max_company + 1) : 1;
- Db::name('admin')->where('id', $userinfo['id'])->update(['company' => $new_company_value]);
- } else {
- $new_company_value = $user_company_value;
- }
- $tableNames = [
- $new_company_value . '_qcode_bach',
- $new_company_value . '_qcode_company',
- $new_company_value . '_qcode_large',
- $new_company_value . '_qcode_liushui',
- $new_company_value . '_qcode_small',
- $new_company_value . '_reset_flow',
- ];
- foreach ($tableNames as $tableName) {
- try {
- Db::connect('mongodb')->name($tableName)->insert(['init' => 1]);
- // 删除测试数据(可选,如果不想保留这个测试数据)
- Db::connect('mongodb')->name($tableName)->where('init', 1)->delete();
- } catch (\Exception $e) {
- echo "创建集合 {$tableName} 失败:" . $e->getMessage();
- }
- }
- return $this->view->fetch();
- }
- /**
- * 获取产品信息
- */
- public function product(){
- $params = input('');
- $page = max(1, (int)$params['page']);
- $limit = max(1, (int)$params['limit']);
- $offset = ($page - 1) * $limit;
- // 获取工单编号列表
- $db3 = Db::connect(config('database.db3'));
- $gdRows = $db3->query("SELECT DISTINCT `Gd_gdbh`, `Gd_客户代号`, `Gd_cpdh` FROM `工单_基本资料` WHERE `Gd_客户代号` = ?", ['J0031']);
- $Gd_gdbhList = array_column($gdRows, 'Gd_gdbh');
- $gdbhList = array_column($gdRows, 'Gd_cpdh');
- if (empty($gdbhList)) {
- $this->assign([
- 'data' => '',
- 'total' => '',
- ]);
- }
- // Mongo 查询
- $mongo = \think\Db::connect('mongodb');
- $where = ['成品编码' => ['in', $gdbhList],'jjcp_gdbh' => ['in', $Gd_gdbhList], 'jjcp_smb' => '末 板'];
- // $where = ['成品编码' => ['in', $gdbhList], 'jjcp_smb' => '末 板'];
- if (!empty($params['search'])) {
- $where['成品编码|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($params['search'], 'i');
- }
- // 查询符合条件的成品数据
- $al_products = $mongo->name('finished_products')
- ->where($where)
- ->order('Sys_rq', 'desc')
- ->select();
- // 查询 texts = 新增产品 的数据
- $xz_products = $mongo->name('finished_products')
- ->where('texts', '新增产品')
- ->order('Sys_rq', 'desc')
- ->select();
- // 合并数据
- $all_products = array_merge($al_products, $xz_products);
- // 分组聚合处理
- $grouped_products = [];
- foreach ($all_products as $item) {
- // 转换日期格式(如果是 d/m/Y H:i:s)
- $date = \DateTime::createFromFormat('d/m/Y H:i:s', $item['Sys_rq']);
- $item['Sys_rq'] = $date ? $date->format('Y-m-d H:i:s') : $item['Sys_rq'];
- $group_key = $item['订单编号'] . '_' . $item['jjcp_gdbh'];
- if (!isset($grouped_products[$group_key])) {
- $grouped_products[$group_key] = [
- 'Sys_rq' => $item['Sys_rq'],
- 'order_ddbh' => $item['订单编号'],
- 'gdbh' => $item['jjcp_gdbh'],
- 'cpbm' => $item['成品编码'],
- 'cpmc' => $item['成品名称'],
- 'sl' => 0,
- 'cpdh_list' => [],
- 'cpbm_list' => []
- ];
- }
- $grouped_products[$group_key]['sl'] += (int)$item['jjcp_sl'];
- if (!in_array($item['jjcp_cpdh'], $grouped_products[$group_key]['cpdh_list'])) {
- $grouped_products[$group_key]['cpdh_list'][] = $item['jjcp_cpdh'];
- }
- if (!in_array($item['成品编码'], $grouped_products[$group_key]['cpbm_list'])) {
- $grouped_products[$group_key]['cpbm_list'][] = $item['成品编码'];
- }
- }
- // 格式化列表字符串
- foreach ($grouped_products as &$group) {
- $group['cpdh'] = implode(',', $group['cpdh_list']);
- $group['cpbm'] = implode(',', $group['cpbm_list']);
- unset($group['cpdh_list'], $group['cpbm_list']);
- }
- // 聚合结果排序(按时间降序)
- $all_grouped = array_values($grouped_products);
- usort($all_grouped, function ($a, $b) {
- $timeA = strtotime($a['Sys_rq']) ?: 0;
- $timeB = strtotime($b['Sys_rq']) ?: 0;
- return $timeB <=> $timeA;
- });
- // 再分页
- $total = count($all_grouped);
- $paged_grouped = array_slice($all_grouped, $offset, $limit);
- // 查询库存并计算剩余数
- foreach ($paged_grouped as &$prod) {
- $inventory = $mongo->name('inventory_summary')->where([
- 'order_ddbh' => $prod['order_ddbh'],
- 'gdbh' => $prod['gdbh'],
- 'cpmc' => $prod['cpmc'],
- ])->find();
- $total_chu_quantity = isset($inventory['total_chu_quantity']) ? (int)$inventory['total_chu_quantity'] : 0;
- $prod['total_chu_quantity'] = $total_chu_quantity;
- $prod['remaining_quantity'] = $prod['sl'] - $total_chu_quantity;
- }
- unset($prod);
- return json([
- 'code' => 1,
- 'data' => $paged_grouped,
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit
- ]);
- }
- public function add_bach()
- {
- if (!$this->request->isAjax()) {
- $this->error('请求错误');
- }
- $data = input('row');
- if (empty($data)) {
- $this->error('参数错误');
- }
- $data = json_decode($data, true);
- if (json_last_error() !== JSON_ERROR_NONE) {
- $this->error('JSON解析错误');
- }
- if (empty($data['products'])) {
- $this->error('产品数据不能为空');
- }
- $userinfo = Session::get('admin');
- if (empty($userinfo) || !isset($userinfo['company'])) {
- $this->error('用户信息获取失败');
- }
- $mongo = \think\Db::connect('mongodb');
- $collection = $userinfo['company'] . '_qcode_bach';
- $bachsummary = $userinfo['company'] . '_qcode_bachsummary';
- $insertData = [];
- $currentday = date('Y-m-d');
- $currentTime = date('Y-m-d H:i:s');
- $timestamp = time();
- $bachNum = 'BACH' . date('YmdHis') . mt_rand(1000, 9999);
- $totalQuantity = 0;
- $totalBoxes = 0;
- $totalPallets = 0;
- $palletSequence = 1; // 全局托盘序号计数器
- foreach ($data['products'] as $product) {
- $small_num = 20;
- $tray_num = 6;
- $box_num = 3;
- $total_boxes = $tray_num * $box_num;
- $remaining_quantity = (int)$product['remaining_quantity'];
- $larger_num = ceil($remaining_quantity / $small_num);
- $large_num = ceil($larger_num / $total_boxes);
- $totalQuantity += $remaining_quantity;
- $totalBoxes += $larger_num;
- $totalPallets += $large_num;
- // 计算完整托盘数和剩余箱数
- $full_pallets = floor($larger_num / $total_boxes);
- $remaining_boxes = $larger_num % $total_boxes;
- // 为每个托盘创建记录
- for ($i = 1; $i <= $large_num; $i++) {
- $is_full_pallet = ($i <= $full_pallets) || ($i == $full_pallets + 1 && $remaining_boxes == $total_boxes);
- $boxes_in_pallet = $is_full_pallet ? $total_boxes : ($i == $full_pallets + 1 ? $remaining_boxes : 0);
- $insertData[] = [
- // 用户信息相关字段
- 'userid' => $userinfo['id'], // 用户ID
- 'supplier_id' => $userinfo['id'], // 供应商ID (同用户ID)
- 'supplier_code' => $userinfo['printer_code'], // 供应商编码/打印机编码
- 'supplier_name' => $data['company_name'], // 供应商名称/公司名称
- // 托盘信息相关字段
- 'pallet' => $palletSequence, // 托盘编号 (全局唯一序号)
- 'pallet_sequence' => $palletSequence, // 托盘序号 (同pallet字段,冗余设计)
- 'pallet_type' => $is_full_pallet ? 'full' : 'partial', // 托盘类型: full-整托/partial-部分托
- // 产品基本信息
- 'matter_no' => $product['gdbh'], // 产品编号/工单编号
- 'matter_id' => $product['gdbh'], // 产品ID (同工单编号)
- 'matter_type' => '01', // 产品类型 (固定值01)
- 'order_ddbh' => $product['order_ddbh'], // 订单编号
- 'cpbm' => $product['cpbm'], // 产品编码
- 'matter_name' => $product['cpmc'], // 产品名称
- 'remark' => $product['remark'], // 备注信息
- // 数量单位信息
- 'num_danwei' => '2', // 数量单位类型 (固定值2)
- 'danwei' => '套', // 单位名称 (套)
- // 数量计算相关字段
- 'num' => $i <= $full_pallets ? $small_num * $total_boxes : ($i == $full_pallets + 1 ? $small_num * $remaining_boxes : 0), // 本托盘实际产品数量
- 'small_num' => $small_num, // 每箱产品数量 (固定20)
- 'tray_num' => $tray_num, // 每层箱数 (固定6)
- 'box_num' => $box_num, // 每托盘层数 (固定3)
- 'total_boxes' => $total_boxes, // 每托盘总箱数 (tray_num * box_num)
- 'boxes_in_pallet' => $boxes_in_pallet, // 本托盘实际箱数 (full时为18,partial时为余数)
- // 托盘汇总信息
- 'large_num' => $large_num, // 本产品总托盘数
- 'larger_num' => $larger_num, // 本产品总箱数
- // 托盘尺寸规格
- 'pallet_height' => '1.05', // 托盘高度(米)
- 'pallet_length' => '0.9', // 托盘长度(米)
- 'pallet_width' => '1.2', // 托盘宽度(米)
- // 物流相关字段 (当前为空)
- 'l_reservation' => '', // 物流预留字段
- 'l_flow' => '1', // 物流流向 (固定值1)
- 'l_weight' => '', // 物流重量
- 's_flow' => '', // 备用流向字段
- 's_weight' => '', // 备用重量字段
- 's_reservation' => '', // 备用预留字段
- // 状态信息
- 'bach_status' => 0, // 批次状态 (0-初始状态)
- 'bach_ids' => $bachNum, // 批次编号 (唯一标识)
- 'large_endnum' => $large_num, // 本产品结束托盘号 (同large_num)
- // 日期相关字段
- 'manufacture_date' => '', // 生产日期 (空)
- 'print_date' => '', // 打印日期 (空)
- 'sys_rq' => $currentday, // 系统日期 (YYYY-MM-DD)
- 'created_at' => $currentTime, // 创建时间 (YYYY-MM-DD HH:MM:SS)
- 'create_time' => $timestamp, // 创建时间戳 (Unix timestamp)
- 'updated_at' => '', // 更新时间 (空)
- // 系统标志字段
- 'sync_flag' => '0', // 同步标志 (0-未同步)
- // 删除相关字段
- 'delete_time' => '', // 删除时间 (空)
- 'delect_time' => '' // 删除时间 (拼写错误,同delete_time)
- ];
- $palletSequence++; // 递增全局托盘序号
- }
- }
- $insertData_bachsummary = [
- 'bach_ids' => $bachNum,
- 'userid' => $userinfo['id'],
- 'supplier_id' => $userinfo['id'],
- 'supplier_code' => $userinfo['printer_code'],
- 'supplier_name' => $data['company_name'],
- 'total_quantity' => $totalQuantity,
- 'total_boxes' => $totalBoxes,
- 'total_pallets' => $totalPallets,
- 'full_pallets' => $totalPallets - count(array_filter($insertData, function($item) { return $item['pallet_type'] === 'partial'; })),
- 'partial_pallets' => count(array_filter($insertData, function($item) { return $item['pallet_type'] === 'partial'; })),
- 'product_count' => count($data['products']),
- 'bach_status' => 0,
- 'sys_rq' => $currentday,
- 'created_at' => $currentTime,
- 'create_time' => $timestamp,
- 'updated_at' => '',
- 'delete_time' => ''
- ];
- //echo "<pre>";
- //print_r($insertData);
- //echo "<pre>";die;
- // 插入数据
- $mongo->name($collection)->insertAll($insertData);
- $mongo->name($bachsummary)->insert($insertData_bachsummary);
- //die;
- // 更新库存信息
- foreach ($data['products'] as $product) {
- $where = [
- 'gdbh' => $product['gdbh'],
- 'order_ddbh' => $product['order_ddbh'],
- 'cpmc' => $product['cpmc'],
- ];
- $actual_quantity = isset($product['actual_quantity']) ? (int)$product['actual_quantity'] : 0;
- $remaining_quantity = isset($product['remaining_quantity']) ? (int)$product['remaining_quantity'] : 0;
- $insertData = [
- 'gdbh' => $product['gdbh'],
- 'order_ddbh' => $product['order_ddbh'],
- 'cpbm' => $product['cpbm'],
- 'cpmc' => $product['cpmc'],
- 'total_ru_quantity' => $remaining_quantity,
- 'total_chu_quantity' => $actual_quantity,
- 'remaining_quantity' => $remaining_quantity - $actual_quantity,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => '',
- 'mod_rq' => '',
- 'company' => $data['company_name'] ?? '',
- ];
- $existing = $mongo->name('inventory_summary')->where($where)->find();
- if ($existing) {
- $new_chu = (int)$existing['total_chu_quantity'] + $actual_quantity;
- $new_remaining = (int)$existing['remaining_quantity'] - $actual_quantity;
- $mongo->name('inventory_summary')
- ->where($where)
- ->update([
- 'total_ru_quantity' => (int)$existing['total_ru_quantity'],
- 'total_chu_quantity' => $new_chu,
- 'remaining_quantity' => $new_remaining,
- 'updated_at' => date('Y-m-d H:i:s'),
- ]);
- } else {
- $mongo->name('inventory_summary')->insert($insertData);
- }
- $mongo->name('inventory_records')->insert($insertData);
- }
- $this->success('添加成功');
- }
- // public function add_bach()
- // {
- // if (!$this->request->isAjax()) {
- // $this->error('请求错误');
- // }
- //
- // $data = input('row');
- // if (empty($data)) {
- // $this->error('参数错误');
- // }
- //
- // $data = json_decode($data, true);
- //
- // if (json_last_error() !== JSON_ERROR_NONE) {
- // $this->error('JSON解析错误');
- // }
- //
- // if (empty($data['products'])) {
- // $this->error('产品数据不能为空');
- // }
- //
- // $userinfo = Session::get('admin');
- // if (empty($userinfo) || !isset($userinfo['company'])) {
- // $this->error('用户信息获取失败');
- // }
- //
- // $mongo = \think\Db::connect('mongodb');
- // $collection = $userinfo['company'] . '_qcode_bach';
- // $bachsummary = $userinfo['company'] . '_qcode_bachsummary';
- //
- // $insertData = [];
- // $currentday = date('Y-m-d');
- // $currentTime = date('Y-m-d H:i:s');
- // $timestamp = time();
- // $bachNum = 'BACH' . date('YmdHis') . mt_rand(1000, 9999);
- //
- // $totalQuantity = 0;
- // $totalBoxes = 0;
- // $totalPallets = 0;
- //
- // foreach ($data['products'] as $product) {
- // $small_num = 20;
- // $tray_num = 6;
- // $box_num = 3;
- // $total_boxes = $tray_num * $box_num;
- //
- // $remaining_quantity = (int)$product['remaining_quantity'];
- // $larger_num = ceil($remaining_quantity / $small_num);
- // $large_num = ceil($larger_num / $total_boxes);
- //
- // $totalQuantity += $remaining_quantity;
- // $totalBoxes += $larger_num;
- // $totalPallets += $large_num;
- //
- // $palletRange = '1-' . $large_num;
- //
- // $insertData[] = [
- // 'userid' => $userinfo['id'],
- // 'supplier_id' => $userinfo['id'],
- // 'supplier_code' => $userinfo['printer_code'],
- // 'supplier_name' => $data['company_name'],
- // 'pallet' => $palletRange,
- // 'matter_no' => $product['gdbh'],
- // 'matter_id' => $product['gdbh'],
- // 'matter_type' => '01',
- // 'order_ddbh' => $product['order_ddbh'],
- // 'cpbm' => $product['cpbm'],
- // 'matter_name' => $product['cpmc'],
- // 'remark' => $product['remark'],
- // 'num_danwei' => '2',
- // 'danwei' => '套',
- // 'num' => $remaining_quantity,
- // 'small_num' => $small_num,
- // 'tray_num' => $tray_num,
- // 'box_num' => $box_num,
- // 'total_boxes' => $total_boxes,
- // 'large_num' => $large_num,
- // 'larger_num' => $larger_num,
- // 'pallet_height' => '1.05',
- // 'pallet_length' => '0.9',
- // 'pallet_width' => '1.2',
- // 'l_reservation' => '',
- // 'l_flow' => '1',
- // 'l_weight' => '',
- // 's_flow' => '',
- // 's_weight' => '',
- // 's_reservation' => '',
- // 'bach_status' => 0,
- // 'bach_ids' => $bachNum,
- // 'large_endnum' => $large_num,
- // 'manufacture_date' => '',
- // 'print_date' => '',
- // 'sys_rq' => $currentday,
- // 'created_at' => $currentTime,
- // 'create_time' => $timestamp,
- // 'updated_at' => '',
- // 'sync_flag' => '0',
- // 'delete_time' => '',
- // 'delect_time' => ''
- // ];
- // }
- //
- // $insertData_bachsummary = [
- // 'bach_ids' => $bachNum,
- // 'userid' => $userinfo['id'],
- // 'supplier_id' => $userinfo['id'],
- // 'supplier_code' => $userinfo['printer_code'],
- // 'supplier_name' => $data['company_name'],
- // 'total_quantity' => $totalQuantity,
- // 'total_boxes' => $totalBoxes,
- // 'total_pallets' => $totalPallets,
- // 'product_count' => count($data['products']),
- // 'bach_status' => 0,
- // 'sys_rq' => $currentday,
- // 'created_at' => $currentTime,
- // 'create_time' => $timestamp,
- // 'updated_at' => '',
- // 'delete_time' => ''
- // ];
- //
- //echo "<pre>";
- //print_r($insertData);
- //echo "<pre>";die;
- // $mongo->name($collection)->insertAll($insertData);
- // $mongo->name($bachsummary)->insert($insertData_bachsummary);
- //
- //
- // foreach ($data['products'] as $product) {
- // $where = [
- // 'gdbh' => $product['gdbh'],
- // 'order_ddbh' => $product['order_ddbh'],
- // 'cpmc' => $product['cpmc'],
- // ];
- //
- // // 实际出库数量
- // $actual_quantity = isset($product['actual_quantity']) ? (int)$product['actual_quantity'] : 0;
- // $remaining_quantity = isset($product['remaining_quantity']) ? (int)$product['remaining_quantity'] : 0;
- //
- // // 插入/更新字段
- // $insertData = [
- // 'gdbh' => $product['gdbh'],
- // 'order_ddbh' => $product['order_ddbh'],
- // 'cpbm' => $product['cpbm'],
- // 'cpmc' => $product['cpmc'],
- // 'total_ru_quantity' => $remaining_quantity, // 入库数来自当前剩余
- // 'total_chu_quantity' => $actual_quantity, // 本次出库数
- // 'remaining_quantity' => $remaining_quantity - $actual_quantity, // 计算结存
- // 'created_at' => date('Y-m-d H:i:s'),
- // 'updated_at' => '',
- // 'mod_rq' => '',
- // 'company' => $data['company_name'] ?? '',
- // ];
- //
- // // 查询是否已存在
- // $existing = $mongo->name('inventory_summary')->where($where)->find();
- //
- // if ($existing) {
- // // 累加更新
- // $new_chu = (int)$existing['total_chu_quantity'] + $actual_quantity;
- // $new_remaining = (int)$existing['remaining_quantity'] - $actual_quantity;
- //
- // $mongo->name('inventory_summary')
- // ->where($where)
- // ->update([
- // 'total_ru_quantity' => (int)$existing['total_ru_quantity'], // 保持不变或你可定义逻辑
- // 'total_chu_quantity' => $new_chu,
- // 'remaining_quantity' => $new_remaining,
- // 'updated_at' => date('Y-m-d H:i:s'),
- // ]);
- // } else {
- // // 插入新记录
- // $mongo->name('inventory_summary')->insert($insertData);
- // }
- //
- // // 插入库存明细记录
- // $mongo->name('inventory_records')->insert($insertData);
- // }
- // $this->success('添加成功');
- //
- // }
- // public function add_bach()
- // {
- // if ($this->request->isAjax() === false) {
- // $this->error('请求错误');
- // }
- //
- // $data = input('row');
- // if (empty($data)) {
- // $this->error('参数错误');
- // }
- //
- // $data = json_decode($data, true); // 解码为关联数组
- //
- // if (json_last_error() !== JSON_ERROR_NONE) {
- // $this->error('JSON解析错误');
- // }
- //
- // if (!isset($data['products']) || empty($data['products'])) {
- // $this->error('产品数据不能为空');
- // }
- //
- // $userinfo = Session::get('admin'); // 获取用户信息
- // if (empty($userinfo) || !isset($userinfo['company'])) {
- // $this->error('用户信息获取失败');
- // }
- //
- // $mongo = \think\Db::connect('mongodb');
- // $collection = $userinfo['company'] . '_qcode_bach';
- // $bachsummary = $userinfo['company'] . '_qcode_bachsummary';
- //
- // $insertData = [];
- // $currentday = date('Y-m-d');
- // $currentTime = date('Y-m-d H:i:s');
- //
- // foreach ($data['products'] as $product) {
- //
- // // 计算相关数量
- // $small_num = 20; // 每一箱数量
- // $tray_num = 6; // 每层箱数【箱/层】
- // $box_num = 3; // 每托层数【层/托】
- // $total_boxes = $tray_num * $box_num; // 每托盘箱数【箱/托】= 6*3=18
- //
- // // 计算总箱数和托盘数
- // $remaining_quantity = (int)$product['remaining_quantity'];
- // $larger_num = ceil($remaining_quantity / $small_num); // 总箱数 = 剩余数量/每箱数量(向上取整)
- // $large_num = ceil($larger_num / $total_boxes); // 本次打包托盘数 = 总箱数/每托盘箱数(向上取整)
- //
- // $insertData[] = [
- // //登录用户字段信息
- // 'userid' => $userinfo['id'],//用户id
- // 'supplier_id' => $userinfo['id'],//用户id
- // 'supplier_code' => $userinfo['printer_code'],//供应商编码
- // 'supplier_name' => $data['company_name'],//公司名称
- //
- // // 产品字段信息
- // 'pallet' => '1-'.$large_num,//托盘序号
- // 'matter_no' => $product['gdbh'],//生产批次号-工单编号
- // 'matter_id' => $product['gdbh'],
- // 'matter_type' => '01',
- // 'order_ddbh' => $product['order_ddbh'],//销售订单号
- // 'cpbm' => $product['cpbm'],//成品编码
- // 'matter_name' => $product['cpmc'],//成品名称
- // 'remark' => $product['remark'],
- //
- // 'num_danwei' => '2',//单位
- // 'danwei' => '套',//单位
- // 'num' => $remaining_quantity,//总数量(张/个)
- // 'small_num' => $small_num,//每一箱数量
- // 'tray_num' => $tray_num,//每层箱数【箱/层】
- // 'box_num' => $box_num,//每托层数【 层/托】
- // 'total_boxes' => $total_boxes,//每托盘箱数【箱/托】
- // 'large_num' => $large_num,//本次打包托盘数
- // 'larger_num' => $larger_num,//总箱数
- // 'pallet_height' => '1.05',//每托高度
- // 'pallet_length' => '0.9',//托盘规格
- // 'pallet_width' => '1.2',//托盘规格
- //
- // 'l_reservation' => '',
- // 'l_flow' => '1',//大件流水
- // 'l_weight' => '',//大件重量
- // 's_flow' => '',//小件流水
- // 's_weight' => '',//小件重量
- // 's_reservation' => '',
- // 'bach_status' => 0,
- // 'bach_num' => $product['gdbh'],//生产批次号-工单编号
- // 'large_endnum' => $large_num,
- //
- // // 其他字段信息
- // 'manufacture_date' => '',//生产日期
- // 'print_date' => '',//打码日期
- // 'sys_rq' => $currentday,//新增日期
- // 'created_at' => $currentTime,//新增时间
- // 'create_time' => time(),//新增时间
- // 'updated_at' => '',
- // 'sync_flag' => '0',
- // 'delete_time' => '',
- // 'delect_time' => ''//删除时间
- // ];
- // }
- //echo "<pre>";
- //print_r($insertData_bachsummary);
- //echo "<pre>";die;
- //
- //
- // if (!empty($insertData)) {
- // //汇总数据
- // $mongo->name($bachsummary)->insertAll($insertData_bachsummary);
- // //明细数据
- // $mongo->name($collection)->insertAll($insertData);
- // $this->success('添加成功');
- // } else {
- // $this->error('没有有效数据可添加');
- // }
- //
- // }
- /**
- * 增加新批次
- */
- public function add()
- {
- $product = new QcodeProduct();
- $bach = new QcodeBach();
- $large = new QcodeLarge();
- $small = new QcodeSmall();
- $liushui = new QcodeLiushui();
- $resetFlow = new ResetFlow();
- $QcodeCompany = new QcodeCompany();
- if ($this->request->isAjax() === false){
- $this->error('请求错误');
- }
- $rows = input('row');
- if (empty($rows)){
- $this->error('参数错误');
- }
- $rows = json_decode($rows);
- $data = [];
- foreach ($rows as $value){
- foreach ($value as $k=>$v){
- $data[$k] = $v;
- }
- }
- //打印信息
- // echo "接口获取";echo "<pre>";print_r($data);echo "<pre>";die;
- //记录库存操作
- $mongo = \think\Db::connect('mongodb');
- $where = [
- 'gdbh' => $data['gdbh'],
- 'order_ddbh' => $data['order_ddbh'],
- 'cpmc' => $data['cpmc'],
- ];
- //插入字段[inventory_summaryk库存明细]
- $insertData = [
- 'gdbh' => $data['gdbh'],
- 'order_ddbh' => $data['order_ddbh'],
- 'cpbm' => $data['cpbm'],
- 'cpmc' => $data['cpmc'],
- 'total_ru_quantity' => $data['sl'],//入库数
- 'total_chu_quantity' => $data['number'],//出库数
- 'remaining_quantity' => $data['remaining_quantity'] - $data['number'],//剩余数
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => '',
- 'mod_rq' => '',
- 'company' => '',
- ];
- // 查询库存汇总表数据是否存在记录
- $list = $mongo->name('inventory_summary')->where($where)->find();
- if ($list) {
- // 如果数据存在,更新库存汇总记录
- $chu = $list['total_chu_quantity'] + $data['number'];//累计出库数量
- $jiecun = $list['remaining_quantity'] - $data['number'];//剩余结存数量
- $updateResult = $mongo->name('inventory_summary')
- ->where($where)
- ->update([
- 'total_ru_quantity' => $data['sl'],
- 'total_chu_quantity' => $chu,
- 'remaining_quantity' => $jiecun,
- 'updated_at' => date('Y-m-d H:i:s'),
- ]);
- } else {
- // 数据不存在则插入新记录
- $mongo->name('inventory_summary')->insert($insertData);
- }
- //记录库存明细
- $mongo->name('inventory_records')->insert($insertData);
- // // 调试输出
- // echo "<pre>"; print_r($list); echo "</pre>";die;
- // if ($data['danwei'] == 1){
- // $num = $data['number'];
- // $tray_num = $data['tray_num'];
- // $box_number = $data['box_number'];
- // //$small_num = (int)ceil((int)总数量(张/个)/(int)张数);
- // $small_num = (int)ceil((int)$data['number']/(int)$data['box_number']);
- // $large_num = (int)ceil($small_num/$tray_num);
- // }else{
- // $num = 0;
- // $tray_num = $data['volume_num'];
- // $small_num = $data['small_num'];
- // $large_num = (int)ceil($small_num/$tray_num);
- // $box_number = 1;
- // $tray_num1 = 1;
- // }
- switch ($data['danwei']) {
- case 1:
- $danwei = '个';
- break;
- case 2:
- $danwei = '套';
- break;
- case 3:
- $danwei = '张';
- break;
- default:
- $danwei = '';
- break;
- }
- $userinfo = Session::get('admin');//获取用户信息
- $arr = [
- 'batch' => $data['gdbh'],
- 'create_time' => time(),
- 'delect_time' => '',
- 'sync_flag' => 0,
- ];
- $productres = $QcodeCompany->save($arr);
- if ($productres === 0){
- $this->error('添加失败');
- }
- $batchList = [
- 'userid' => $userinfo['id'],//登录用户id
- 'supplier_id' => $userinfo['id'],//登录用户id
- 'supplier_code' => $userinfo['printer_code'],
- 'supplier_name' => $data['company_name'],//公司名称
- 'cpbm' => $data['cpbm'],//成品编码
- 'matter_name' => $data['cpmc'],//成品名称
- 'matter_no' => $data['gdbh'],//生产批次号-工单编号
- 'order_ddbh' => $data['order_ddbh'],//销售订单号
- 'matter_id' => $data['gdbh'],
- 'matter_type' => '01',
- 'manufacture_date' => (int)date('ymd',strtotime($data['manufacture_date'])),//生产日期
- 'print_date' => (int)date('ymd',strtotime($data['print_date'])),//打码日期
- 'num_danwei' => $data['danwei'],//单位
- 'danwei' => $danwei,//单位
- 'num' => $data['number'],//总数量(张/个)
- 'small_num' => $data['box_number'],//每一箱数量
- 'tray_num' => $data['tray_num'],//每层箱数【箱/层】
- 'box_num' => $data['tray_number'],//每托层数【 层/托】
- 'total_boxes' => $data['total_boxes'],//每托盘箱数【箱/托】
- 'large_num' => $data['box_num'],//本次打包托盘数
- 'larger_num' => $data['small_num'],//总箱数
- 'pallet_height' => $data['pallet_height'],//每托高度
- 'pallet_length' => $data['pallet_length'],//托盘规格
- 'pallet_width' => $data['pallet_width'],//托盘规格
- 'l_reservation' => '',
- // 处理可能为空的流水/重量字段
- 'l_flow' => $data['big_liushui'],//大件流水
- 'l_weight' => $data['big_weight'],//大件重量
- 's_flow' => $data['small_start_liushui'],//小件流水
- 's_weight' => $data['small_weight'],//小件重量
- 's_reservation' => '',
- 'bach_status' => 0,
- 'bach_num' => $data['gdbh'],//生产批次号-工单编号
- 'large_endnum' => $data['big_liushui'] + $data['box_num'] -1,
- 'create_time' => time(),//新增时间
- 'delect_time' => '',//删除时间
- ];
- $res = $bach->save($batchList);
- if ($res === 0){
- $this->error('添加失败');
- }
- $flow = [
- 'l_flow' => (int)$batchList['large_num'],
- 'bach_num' => $batchList['matter_no'],
- ];
- if ($resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_no'])->find()){
- $resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_no'])->update($flow);
- }else{
- $flow['product_id'] = $batchList['matter_no'];
- $resetFlow->save($flow);
- }
- $last_id = $bach->getLastInsID();
- if ($last_id){
- //插入大小二维码数据 二维码数据不变区域
- // echo "<pre>";print_r($batchList);echo "<pre>";die;
- $fixed_code = '';
- $fixed_code.=$this->intTochar($batchList['matter_type'],2);//2位 辅料种类编码
- $fixed_code .= $this->intTochar($batchList['supplier_code'], 21);//21位 供应商编码
- $fixed_code .= '9'; // 补0一位
- $fixed_code.=$this->intTochar($batchList['cpbm'],10);//10位 辅料编码
- $fixed_code.=$batchList['manufacture_date'];//6位 生产日期
- $print_code=$batchList['print_date'];//6位 打码日期
- $small_liushui = [
- 'onlycode' => 'AB92'.$fixed_code.$print_code,
- 'last_num' => 0,
- 'user_id' => $userinfo['company'],
- 'stype' => 2,
- 'dateTime' => time(),
- ];
- $whereSmall = [
- 'onlycode' => $small_liushui['onlycode'],
- 'user_id' => $userinfo['company'],
- ];
- if ($liushui->name($userinfo['company'].'_'.'qcode_liushui')->where($whereSmall)->find()){
- //小件二维码存在,更新小件二维码最后流水号
- $lastNum = $liushui->name($userinfo['company'].'_'.'qcode_liushui')->where($whereSmall)->find();
- }else{
- //小件二维码不存在,新增记录
- $liushui->save($small_liushui);
- $lastNum['last_num'] = 0;
- }
- //循环插入大件二维码数据
- for ($i=0;$i<$data['box_num'];$i++){
- $large = new QcodeLarge();
- $l_flow = $this->intTochar($batchList['l_flow']+$i,6);
- $l_weight = $this->intTochar($batchList['l_weight']*100,6);
- $l_reservation = $this->intTochar($batchList['matter_no'],10);
- $l_reservation = $l_reservation.'0000000000';
- $remainder = $batchList['num'] - $batchList['total_boxes'] * $i;//最后一托盘小件数量
- if ($remainder < $batchList['tray_num']){
- $small_n = $this->intTochar($remainder,3);//3位小件数量,不足补0
- }else{
- $small_n = $this->intTochar($batchList['tray_num'],3);
- }
- // $l_num = $small_n * $batchList['box_num'];
- $l_num = 0;
- //大件二维码数据
- $code_data = $this->CodeData('AB92',$fixed_code,$small_n,$print_code,$l_flow,$l_weight,'2',$l_reservation);
- //大码数据信息
- $l_data = [
- 'bach_id' => $last_id,
- 'code' => $code_data['code'],
- 'code_cp1' => $code_data['code_cp1'],
- 'code_cp2' => $code_data['code_cp2'],
- 'print_date' => $print_code,
- 'create_time' => time(),
- 'p_nums' => 0,
- 'userid' =>$userinfo['id'],
- 'l_weight' =>$batchList['l_weight']*100,
- 'l_num' =>$l_num,
- 'l_status' => 0,
- 'l_print' => 0
- ];
- $l_res = $large->save($l_data);
- if ($l_res === 0){
- $this->error('大件码插入失败');
- }
- $large_id = $large->getLastInsID();
- if ($large_id){
- // //小件码循环插入
- // for ($j=0;$j<$data['tray_num'] and ($j+$i*$data['tray_num'])<$batchList['small_num'];$j++){
- for ($j=0;$j<$data['total_boxes'] and ($j+$i*$data['total_boxes'])<$batchList['total_boxes'];$j++){
- $small = new QcodeSmall();
- $s_flow = $this->intTochar($batchList['s_flow']+$j+$i*$data['tray_num']+$lastNum['last_num'],6);//小件码序号从1开始
- $s_weight = $this->intTochar($batchList['s_weight'],6);
- $small_sign = '000';
- $s_reservation = $this->intTochar($batchList['bach_num'],10);
- $s_reservation = $s_reservation . '0000000000';
- //生成小件码
- $small_code_data = $this->CodeData('AB92',$fixed_code,$small_sign,$print_code,$s_flow,$s_weight,'1',$s_reservation);
- //小码数据
- $s_data = [
- 'large_id'=>$large_id,
- 'bach_id'=>$last_id,
- 'code'=>$small_code_data['code'],
- 'code_cp1'=>$small_code_data['code_cp1'],
- 'code_cp2'=>$small_code_data['code_cp2'],
- 'l_flow'=>$j+1,
- 'print_date'=>$print_code,
- 'create_time'=>time(),
- 'p_nums'=>0,
- 'userid'=>$userinfo['id'],//小码绑定用户id
- 's_weight'=>$batchList['s_weight'],//单个小件重量
- 'status' => 0,
- ];
- $s_res = $small->save($s_data);
- if ($s_res === 0){
- $this->error('小件码插入失败');
- }
- }
- }else{
- $this->error('添加失败');
- }
- }
- }
- $liushui_res = $liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->update(['last_num'=>$batchList['small_num']]);
- if ($liushui_res === false){
- $this->error('添加失败');
- }
- return json(['code' => 1, 'msg' => '成功', 'data' => '']);
- // $this->success('成功');
- }
- /**
- * 编码补位
- * @param $num
- * @param $len
- * @return string
- */
- function intTochar($num=0,$len){
- //规定的不足的时候自动补足零
- $code=(string)$num;
- $buwei='';
- if(strlen($code)<$len){
- for($i=strlen($code);$i<$len;$i++){
- $buwei.='0';
- }
- }
- return $buwei.$code;
- }
- /**
- * 二维码编码生成
- * @param $sign
- * @param $fixed_code
- * @param $small_num
- * @param $print_date
- * @param $flow
- * @param $weight
- * @param $large_sign
- * @param $reservation
- * @return array
- */
- function CodeData($sign,$fixed_code,$small_num,$print_date,$flow,$weight,$large_sign,$reservation){
- $code=$sign;//4 位固定标志位
- $code.=$fixed_code; // 固定字符串
- $code.=$small_num;//3位 小件数量
- $code.=$print_date;//6 位 日期
- $code.=$flow;//6位打印流水号
- $code.=$weight;//6位辅料重量
- $code.=$large_sign;//大小件标示位
- $code.=$reservation;//20 位 预留号
- //大码数据信息
- $data=[
- 'code'=>str_replace(" ","",$code),
- 'code_cp1'=>$print_date.$flow,
- 'code_cp2'=>$weight.$reservation,//20位补充
- 'print_date'=>time(),
- 'p_nums'=>0,
- ];
- return $data;
- }
- }
|