Преглед изворни кода

提交导入异常的 问题

lvhao пре 12 часа
родитељ
комит
f35d9fdf44
2 измењених фајлова са 225 додато и 1 уклоњено
  1. 145 0
      core/CoreApp/controllers/Whlabel.php
  2. 80 1
      template/erp/whlabel_type.html

+ 145 - 0
core/CoreApp/controllers/Whlabel.php

@@ -137,6 +137,9 @@ class Whlabel extends Start_Controller
 		} else if ($arg == 'presetedit') //预设库存导入
 		{
 			$this->_presetedit($arg_array);
+		}else if ($arg == 'presetedittwo') //预设库存导入
+		{
+			$this->_presetedittwo($arg_array);
 		} else if ($arg == 'summary') //汇总导出
 		{
 			$this->_summary($arg_array);
@@ -4606,6 +4609,148 @@ class Whlabel extends Start_Controller
 			}
 		}
 	}
+
+
+	public function _presetedittwo(){
+		$data = $this->input->post(NULL, TRUE);
+		if(empty($list)){
+			$raw_data = $this->input->raw_input_stream;
+			// 解码 JSON
+			$data  = json_decode($raw_data, true);
+		}
+		if(empty($data)){
+			echo json_encode(array('msg' => '传输数据异常!', 'error' => 1, 'success' => false));
+		}
+		$list = [];
+		if(isset($data['list'])){
+			$list = $data['list'];
+		}
+		
+		$i = 0;
+		$j = 0;
+		$ed = array();
+		foreach ($list as $key => $value) {
+			$time = time();
+			if ($value['0'] == "") {
+				continue;
+			}
+		
+			$warehouse = $this->warehouse->get_title($value['0']);
+			$purchase = $this->purchase->get_bm($value['1']);
+			$sku =  $value['2'];
+			$num =  $value['3']; //调整数量
+			$text =  $value['4']; //扣减说明
+			$type =  $value['5']; //调整方式0扣减1删除
+			$rktype =  $value['6'] != '' ? $value['6'] : 0; //增加库存
+			$cktype =  $value['7'] != '' ? $value['7'] : 0; //减少库存
+			
+			$d = $this->whlabel->get_warehouseSkuByDesc($warehouse['id'], $sku);
+			if (!$purchase) //如果没有这个SKU
+			{
+				$ed[] = array($value['1'] . '-不存在此供应商编码');
+				$j++;
+				continue;
+			}
+			if (!$d) //如果没有这个SKU
+			{
+				$ed[] = array($sku . '-库存中不存在此SKU');
+				$j++;
+				continue;
+			}
+			if (!isset($warehouse['id'])) {
+				$ed[] = array($sku . '-仓库名错误!');
+				$j++;
+				continue;
+			}
+			if ($type != '0' && $type != '1') {
+				$ed[] = array($sku . '-扣减方式填写错误!');
+				$j++;
+				continue;
+			}
+			if ($num < 0) //扣减库存
+			{
+				if ($warehouse['id'] == 5 && ($cktype == '' || ($cktype > 1 && $text == ''))) {
+					$ed[] = array($sku . '-扣减必须填写扣减类型,非类型1必须填写备注');
+					$j++;
+					continue;
+				}
+				$data = $this->whlabel->find_all('warehouse = "' . $warehouse['id'] . '" and sku = "' . $sku . '" and zd = "" and state = 0');
+				if (count($data) < abs($num)) {
+					$ed[] = array($sku . '-扣减数量大于库存剩余数量!');
+					$j++;
+					continue;
+				}
+				if ($type == 0) //如果扣减方式为 扣减库存
+				{
+					for ($i = 0; $i < abs($num); $i++) {
+						$this->whlabel->save(array('state' => 8, 'textout' => $text, 'deltime' => $time, 'cktype' => $cktype, 'time' => $time, 'outk' => $time), $data[$i]['id']);
+					}
+				} else if ($type == 1) {
+					$zt = $this->whlabeltransport->find_all('sku = "' . $sku . '" and warehouse = "' . $warehouse['id'] . '" and features = "' . $d['features'] . '" and cz = "0"');
+					if (count($zt) > 0) {
+						$ed[] = array($sku . '-有在途库存无法删除!');
+						$j++;
+						continue;
+					}
+					for ($i = 0; $i < abs($num); $i++) {
+						$this->whlabel->remove($data[$i]['id']);
+					}
+				}
+			} else if ($num > 0) //增加库存
+			{
+				if ($warehouse['id'] == 5 && ($rktype == '' || ($rktype > 1 && $text == ''))) {
+					$ed[] = array($sku . '-增加必须填写增加类型,非类型1必须填写备注');
+					$j++;
+					continue;
+				}
+				$post['sku'] = $d['sku'];
+				$post['shop'] = $d['shop'];
+				$post['warehouse'] = $warehouse['id'];
+				$post['purchase'] = $purchase['id'];
+				$post['retreatwarehouse'] = $d['retreatwarehouse'];
+				$post['state'] = 0;
+				$post['title'] = $d['title'];
+				$post['number'] = $d['number'];
+				$post['features'] = $d['features'];
+				$post['enter'] = $time;
+				$post['time'] = $time; //操作时间
+				$post['rktype'] = $rktype;
+				$post['sku_master_id'] = $d['sku_master_id'];
+				$post['sku_shop_id'] = $d['sku_shop_id'];
+				for ($i = 0; $i < $num; $i++) {
+					$post['label'] = time() . rand(100, 999) . $i;
+					$this->whlabel->insert($post);
+				}
+			}
+			sleep(1); //防止入库时间一样,需要延迟1秒
+		}
+		if ($j > 0) {
+			$tt = date('Ymd', time());
+			$title = '库存导入错误信息-' . $tt;
+			$titlename = "<table border=1><tr><td>错误详情</td></tr></table>";
+			$tail = "\n";
+			$filename = $title . ".xls";
+			$ecl = $this->excel->get_fz3($ed, $titlename, $filename, $tail);
+			$dir = '/data/excel/' . $time . '/';
+			$file_name = 'error_' . $time . rand(1000, 9999);
+			if (!is_dir('.' . $dir)) mkdir('.' . $dir, 0777);
+			$myfile = fopen("." . $dir . $file_name . ".xls", "w") or die();
+			fwrite($myfile, $ecl);
+			fclose($myfile);
+			$error = $dir . $file_name . '.xls';
+			echo json_encode(array('msg' => '导入成功,' . $j . '条异常,', 'error' => $error, 'success' => true));
+			exit;
+		} else {
+			echo json_encode(array('msg' => '导入成功!', 'error' => 1, 'success' => true));
+			exit;
+		}
+		
+	}
+
+
+
+
+	
 	//笛卡儿积
 	function cp($ps, $sets, $list)
 	{

+ 80 - 1
template/erp/whlabel_type.html

@@ -206,7 +206,7 @@
 <a href="javascript:void(0);" class="summary">出库汇总</a>
 
 <a href="javascript:void(0);" data-url="presetout" data-id="3" class="whlabelexcel">导出库存模板</a>
-<a href="javascript:void(0);" class="whlabeldr">导入库存</a>
+<a href="javascript:void(0);" class="whlabeldr_two">导入库存</a>
 {if $vip > 0} {/if}
 <a href="javascript:void(0);" class="whlabelxq">导出数据详情</a>
 <a href="javascript:void(0);" class="whlabelexcelzh">导出库存数据及在途中</a>
@@ -376,6 +376,85 @@ $(".dccrtype").click(function() {
     })
     
 </script>
+
+
+<script>
+
+$(".whlabeldr_two").click(function() {
+    var dr = $(this);
+    $("#upload-file").click();
+    $("#upload-file").unbind("change");
+    $("#upload-file").bind("change",function(){
+    whlabeldr_two(dr);
+        layx.load('loadId','导入中,请稍后',{shadable:0.6});
+    });
+});
+
+function whlabeldr_two(dr){
+    var file = document.getElementById("upload-file").files[0];
+    if (!file) return;
+
+    // 显示加载提示
+    //layx.load('loadId', '解析 Excel 中,请稍后', { shadable: 0.6 });
+
+    // 使用 FileReader 读取文件
+    var reader = new FileReader();
+    reader.onload = function(e) {
+        var data = e.target.result;
+        try {
+            // 使用 SheetJS 解析 Excel 为 JSON
+            var workbook = XLSX.read(data, { type: 'array' });
+            var sheetName = workbook.SheetNames[0]; // 取第一个工作表
+            var worksheet = workbook.Sheets[sheetName];
+            var jsonData = XLSX.utils.sheet_to_json(worksheet, { header:1,defval: "" });
+            jsonData.shift(); // 去掉第一行表头
+           
+            // 构建要上传的数据(可以包含文件名、sheet名等)
+            // var uploadData = {
+            //     fileName: file.name,
+            //     sheetName: sheetName,
+            //     data: jsonData   // 解析后的 JSON 数组
+            // };
+            // 通过 AJAX 上传 JSON 数据
+            document.getElementById("upload-file").files[0] = '';
+            $.ajax({
+                url: excel + "presetedittwo/",  // 注意:后端接口可能需要改为接收 JSON
+                type: "POST",
+                contentType: "application/json", // 发送 JSON 格式
+                data:JSON.stringify({
+                    list:jsonData
+                }),
+                dataType: "json",
+                success: function(a) {
+                    layx.destroy('loadId');
+                    if (a && a.success) {
+                        if (a.error == 1) errora(a);
+                        else errorb(a);
+                    } else {
+                        errorc(a);
+                    }
+                },
+                error: function(xhr, status, error) {
+                    layx.destroy('loadId');
+                    console.error("上传失败:", error);
+                    errorc({ success: false, message: "上传失败" });
+                }
+            });
+        } catch (err) {
+            layx.destroy('loadId');
+            console.error("解析 Excel 失败:", err);
+            errorc({ success: false, message: "解析 Excel 文件失败" });
+        }
+    };
+    reader.onerror = function() {
+        layx.destroy('loadId');
+        errorc({ success: false, message: "文件读取失败" });
+    };
+    reader.readAsArrayBuffer(file); // 以 ArrayBuffer 读取
+}
+
+</script>
+<script type="text/javascript" src="{$theme}js/excel/xlxs.js" ></script>
 <script type="text/javascript" src="{$theme}js/laydate.js"></script>
 <div style="display:none;">
 <input id="upload-file" name="files" accept="image/xls,image/xlsx" type="file">