|
@@ -10,6 +10,7 @@ use app\common\constant\LeaveConstant;
|
|
|
use app\common\constant\MaintainConstant;
|
|
|
use app\common\constant\OfferConstant;
|
|
|
use app\common\model\Goods;
|
|
|
+use app\common\model\GoodsCategory;
|
|
|
use hg\apidoc\annotation as Apidoc;
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
|
@@ -65,6 +66,19 @@ class Common extends Base
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 导入模板
|
|
|
+ *
|
|
|
+ * @Apidoc\Method("POST")
|
|
|
+ **/
|
|
|
+ public function get_import_template(){
|
|
|
+ $this->request->domain().'导入模板-采购物品.xlsx';
|
|
|
+ $this->request->domain().'导入模板-入库物品.xlsx';
|
|
|
+ $this->request->domain().'导入模板-领用物品.xlsx';
|
|
|
+
|
|
|
+ exit;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 导入
|
|
|
*
|
|
|
* @Apidoc\Method("POST")
|
|
@@ -123,9 +137,12 @@ class Common extends Base
|
|
|
//加载文件
|
|
|
$insert = [];
|
|
|
$data = [];
|
|
|
- $update_stock_data = [];
|
|
|
+ $category_first_data = [];
|
|
|
+ $category_data = [];
|
|
|
+ $use_data = [];
|
|
|
$apply_goods_data = [];
|
|
|
$error_data = [];
|
|
|
+ $flag = 2;
|
|
|
try {
|
|
|
if (!$PHPExcel = $reader->load($filePath)) {
|
|
|
$this->error('未知的数据格式!');
|
|
@@ -145,15 +162,17 @@ class Common extends Base
|
|
|
$values = [];
|
|
|
for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
|
$val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
|
- $values[] = is_null($val) ? '' : $val;
|
|
|
+ $values[] = is_null($val) ? '' : trim($val); // 消除空格
|
|
|
}
|
|
|
$values = array_filter($values); // 过滤空值
|
|
|
- $temp = array_combine($fields, $values);
|
|
|
+ $temp = array_combine($fields, $values); // 合并两个数组来创建一个新数组,其中的一个数组元素为键名,另一个数组元素为键值
|
|
|
if ($temp) {
|
|
|
$insert[] = $temp;
|
|
|
|
|
|
if ($module == CommonConstant::MODULE_4) {
|
|
|
$data[$values[1] . ',' . $values[2] . ',' . $values[3]][] = $temp;
|
|
|
+ $category_first_data[$values[1]] = $values[1];
|
|
|
+ $category_data[$values[2]] = $values[2];
|
|
|
} else {
|
|
|
$data[$values[1] . ',' . $values[2] . ',' . $values[4]][] = $temp;
|
|
|
}
|
|
@@ -166,53 +185,84 @@ class Common extends Base
|
|
|
if (!$insert) {
|
|
|
$this->error('记录未找到');
|
|
|
}
|
|
|
+
|
|
|
if ($module == CommonConstant::MODULE_4) {
|
|
|
- $msg = '导入的物品,';
|
|
|
+ $category_first_list = GoodsCategory::where('name','in',$category_first_data)->where('pid',0)->where('is_deleted',CommonConstant::IS_DELETED_0)->column('id,name');
|
|
|
+ $category_list = GoodsCategory::where('name','in',$category_data)->where('pid','gt',0)->where('is_deleted',CommonConstant::IS_DELETED_0)->column('id,name');
|
|
|
+ $category_first_list_flip = array_flip($category_first_list); // 交换数组中的键和值
|
|
|
+ $category_list_flip = array_flip($category_list);
|
|
|
+
|
|
|
+ $msg = '导入的物品:';
|
|
|
foreach ($data as $key => $value) {
|
|
|
$arr = explode(',', $key);
|
|
|
$goods_name = $arr[2];
|
|
|
- $goods_info = Goods::field('status,is_deleted,create_at', true)
|
|
|
- ->where('goods_category_first', $arr[0])
|
|
|
- ->where('goods_category_id', $arr[1])
|
|
|
- ->where('goods_name', $goods_name)
|
|
|
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
|
|
|
- ->with([
|
|
|
- 'goodsStock',
|
|
|
- ])
|
|
|
- ->find();
|
|
|
- if ($goods_info) {
|
|
|
- // 商品库里有该商品
|
|
|
- $goods_info = $goods_info->toArray();
|
|
|
- $goods_stock_info = array_column($goods_info['goods_stock'], null, 'name');
|
|
|
- foreach ($value as $val) {
|
|
|
- if (array_key_exists($val['name'], $goods_stock_info)) {
|
|
|
- // 商品库里有该商品规格
|
|
|
- $stock = isset($val['stock']) ? $val['stock'] : 0;
|
|
|
- $total_stock = $goods_stock_info[$val['name']]['stock'];
|
|
|
- if($total_stock > 0){
|
|
|
- $update_stock_data[$key] = $val;
|
|
|
- }
|
|
|
- if ($stock > $total_stock) {
|
|
|
- $lack_stock = bcsub($stock, $total_stock, 1);
|
|
|
- $error_data[] = [
|
|
|
- 'msg' => $msg . $goods_name . '-' . $val['name'] . '库存不足,缺少' . $lack_stock . '件',
|
|
|
- ];
|
|
|
+ if(array_key_exists($arr[0],$category_first_list_flip)){
|
|
|
+ // 一级里有该商品分类
|
|
|
+ if(array_key_exists($arr[1],$category_list_flip)){
|
|
|
+ // 二级里有该商品分类
|
|
|
+ $goods_category_first = $category_first_list_flip[$arr[0]];
|
|
|
+ $goods_category_id = $category_list_flip[$arr[1]];
|
|
|
+ $goods_info = Goods::field('status,is_deleted,create_at', true)
|
|
|
+ ->where('goods_category_first', $goods_category_first)
|
|
|
+ ->where('goods_category_id', $goods_category_id)
|
|
|
+ ->where('goods_name', $goods_name)
|
|
|
+ ->where('is_deleted', CommonConstant::IS_DELETED_0)
|
|
|
+ ->with([
|
|
|
+ 'goodsStock',
|
|
|
+ ])
|
|
|
+ ->find();
|
|
|
+ if ($goods_info) {
|
|
|
+ // 商品库里有该商品
|
|
|
+ $goods_info = $goods_info->toArray();
|
|
|
+ $goods_stock_info = array_column($goods_info['goods_stock'], null, 'name');
|
|
|
+ foreach ($value as $kk=>$val) {
|
|
|
+ if (array_key_exists($val['goods_sku_value'], $goods_stock_info)) {
|
|
|
+ // 商品库里有该商品规格
|
|
|
+ $stock = isset($val['stock']) ? $val['stock'] : 0;
|
|
|
+ $total_stock = $goods_stock_info[$val['goods_sku_value']]['stock'];
|
|
|
+ if($total_stock > 0){
|
|
|
+ // 商品规格有库存
|
|
|
+ $use_data[$key][] = $val;
|
|
|
+ $use_data[$key][$kk]['goods_category_first'] = $goods_category_first;
|
|
|
+ $use_data[$key][$kk]['goods_category_id'] = $goods_category_id;
|
|
|
+ }
|
|
|
+ if ($stock > $total_stock) {
|
|
|
+ // 商品规格库存不足
|
|
|
+ $lack_stock = bcsub($stock, $total_stock);
|
|
|
+ $use_data[$key][$kk]['stock'] = $total_stock; // 替换库存
|
|
|
+
|
|
|
+ $error_data[] = [
|
|
|
+ 'msg' => $msg . $goods_name . '-' . $val['goods_sku_value'] . '库存不足,缺少' . $lack_stock . '件',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 商品库里没有该商品规格
|
|
|
+ $error_data[] = [
|
|
|
+ 'msg' => $msg . $goods_name . '-' . $val['goods_sku_value'] . '商品库不存在',
|
|
|
+ ];
|
|
|
+ }
|
|
|
}
|
|
|
} else {
|
|
|
- // 商品库里没有该商品规格
|
|
|
+ // 商品库里没有该商品
|
|
|
$error_data[] = [
|
|
|
- 'msg' => $msg . $goods_name . '-' . $val['name'] . '商品库不存在',
|
|
|
+ 'msg' => $msg . $goods_name . '商品库不存在',
|
|
|
];
|
|
|
}
|
|
|
+ } else{
|
|
|
+ // 二级里没有该商品分类
|
|
|
+ $error_data[] = [
|
|
|
+ 'msg' => $msg . $goods_name . '商品库不存在',
|
|
|
+ ];
|
|
|
}
|
|
|
- } else {
|
|
|
- // 商品库里没有该商品
|
|
|
+ } else{
|
|
|
+ // 一级里没有该商品分类
|
|
|
$error_data[] = [
|
|
|
'msg' => $msg . $goods_name . '商品库不存在',
|
|
|
];
|
|
|
}
|
|
|
}
|
|
|
- $data = $update_stock_data;
|
|
|
+ $data = $use_data;
|
|
|
+ $flag = 3;
|
|
|
}
|
|
|
foreach ($data as $key => $value) {
|
|
|
// 商品
|
|
@@ -237,7 +287,7 @@ class Common extends Base
|
|
|
'goods_brand' => isset($val['goods_brand']) ? $val['goods_brand'] : '',
|
|
|
];
|
|
|
}
|
|
|
- $goods_data['flag'] = 2;
|
|
|
+ $goods_data['flag'] = $flag;
|
|
|
$goods_data['total_price'] = $total_price;
|
|
|
$goods_data['goods_stock'] = $goods_stock_data;
|
|
|
$apply_goods_data[] = $goods_data;
|