Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
liexin_supplier
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
cb471566
authored
Jul 27, 2022
by
杨树贤
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
修改上传格式
parent
50d2dfea
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
118 additions
and
126 deletions
app/Http/Controllers/Api/SupplierExaminationApiController.php
app/Http/Services/SupplierExaminationService.php
public/data/import_supplier_examination_template.xlsx
public/data/supplier_examination 副本.xlsx
resources/views/script/SupplierExaminationListScript.blade.php
app/Http/Controllers/Api/SupplierExaminationApiController.php
View file @
cb471566
...
...
@@ -129,7 +129,7 @@ class SupplierExaminationApiController extends Controller
if
(
$file
->
isValid
())
{
$ext
=
$file
->
getClientOriginalExtension
();
$realPath
=
$file
->
getRealPath
();
if
(
$ext
!=
'
csv
'
)
{
if
(
$ext
!=
'
xlsx
'
)
{
$this
->
response
(
-
1
,
'上传格式错误'
);
}
// 上传文件
...
...
app/Http/Services/SupplierExaminationService.php
View file @
cb471566
...
...
@@ -53,144 +53,136 @@ class SupplierExaminationService
ini_set
(
'memory_limit'
,
-
1
);
try
{
$content
=
file_get_contents
(
$filePath
);
$fileType
=
mb_detect_encoding
(
$content
,
array
(
'UTF-8'
,
'GBK'
,
'LATIN1'
,
'BIG5'
));
Excel
::
load
(
$filePath
,
function
(
$reader
)
{
$
result
=
$reader
->
getSheet
(
0
)
->
toArray
();
$header
=
$
result
[
0
];
$
data
=
$reader
->
getSheet
(
0
)
->
toArray
();
$header
=
$
data
[
0
];
if
(
!
$this
->
checkImportCsvHeader
(
$header
))
{
throw
new
\Exception
(
'文件不是标准上传模板,请核对后再上传'
);
}
},
$fileType
);
$valueBinder
=
new
CsvValueBinder
();
$data
=
Excel
::
setValueBinder
(
$valueBinder
)
->
load
(
$filePath
,
$fileType
)
->
get
();
if
(
empty
(
$data
))
{
throw
new
\Exception
(
'上传数据不能为空'
);
}
$data
=
$data
->
toArray
();
$dataMd5
=
md5
(
json_encode
(
$data
));
if
(
SupplierExaminationModel
::
where
(
'data_md5'
,
$dataMd5
)
->
exists
())
{
throw
new
\Exception
(
'该文件已经上传过,请选择其它文件'
);
}
//获取所有采购员用于校验
$channelUsers
=
(
new
IntracodeModel
())
->
getAllEncode
();
$channelUserNames
=
array_column
(
$channelUsers
,
'name'
);
$supplierNames
=
(
new
SupplierChannelModel
())
->
where
(
'is_type'
,
0
)
->
pluck
(
'supplier_name'
)
->
toArray
();
//拼装数据插入校验,校验完成插入数据库
$examineData
=
[];
foreach
(
$data
as
$index
=>
$item
)
{
$lineNo
=
$index
+
2
;
$item
=
array_map
(
function
(
$value
)
{
return
trim
(
$value
);
},
$item
);
$orderSn
=
$item
[
0
];
$purchaseSn
=
$item
[
1
];
$examineTime
=
$item
[
2
];
$salesName
=
$item
[
3
];
$channelUserName
=
$item
[
4
];
$ticketType
=
$item
[
5
];
$supplierName
=
$item
[
6
];
$skuName
=
$item
[
7
];
$brandName
=
$item
[
8
];
$amount
=
$item
[
9
];
$batch
=
$item
[
10
];
$producingArea
=
$item
[
11
];
$stockInDate
=
$item
[
12
];
$incomeSn
=
$item
[
13
];
$deliverySn
=
$item
[
14
];
$tallyRequest
=
$item
[
15
];
$examineRequest
=
$item
[
16
];
$unhealthyAmount
=
$item
[
17
];
$abnormalLevel
=
$item
[
18
];
$unhealthyContent
=
$item
[
19
];
$examineResult
=
$item
[
20
];
$remark
=
$item
[
21
];
if
(
empty
(
$supplierName
))
{
throw
new
\Exception
(
"供应商名称不能为空! (第${lineNo}行),请修改后再次提交"
);
}
if
(
empty
(
$examineTime
))
{
throw
new
\Exception
(
"检货时间不能为空! (第${lineNo}行),请修改为 2000-01-01 这种格式后再次提交"
);
}
if
(
!
isDateTime
(
$examineTime
))
{
throw
new
\Exception
(
"检货时间格式不规范! (第${lineNo}行),请修改为 2000-01-01 这种格式后再次提交"
);
$dataMd5
=
md5
(
json_encode
(
$data
));
if
(
SupplierExaminationModel
::
where
(
'data_md5'
,
$dataMd5
)
->
exists
())
{
throw
new
\Exception
(
'该文件已经上传过,请选择其它文件'
);
}
if
(
!
empty
(
$stockInDate
)
&&
!
isDateTime
(
$stockInDate
))
{
throw
new
\Exception
(
"入库日期时间格式不规范! (第${lineNo}行),请修改后再次提交"
);
//获取所有采购员用于校验
$channelUsers
=
(
new
IntracodeModel
())
->
getAllEncode
();
$channelUserNames
=
array_column
(
$channelUsers
,
'name'
);
$supplierNames
=
(
new
SupplierChannelModel
())
->
where
(
'is_type'
,
0
)
->
pluck
(
'supplier_name'
)
->
toArray
();
//拼装数据插入校验,校验完成插入数据库
$examineData
=
[];
foreach
(
$data
as
$index
=>
$item
)
{
if
(
$index
==
0
)
{
continue
;
}
$lineNo
=
$index
+
1
;
$item
=
array_map
(
function
(
$value
)
{
return
trim
(
$value
);
},
$item
);
$orderSn
=
$item
[
0
];
$purchaseSn
=
$item
[
1
];
$examineTime
=
$item
[
2
];
$salesName
=
$item
[
3
];
$channelUserName
=
$item
[
4
];
$ticketType
=
$item
[
5
];
$supplierName
=
$item
[
6
];
$skuName
=
$item
[
7
];
$brandName
=
$item
[
8
];
$amount
=
$item
[
9
];
$batch
=
$item
[
10
];
$producingArea
=
$item
[
11
];
$stockInDate
=
$item
[
12
];
$incomeSn
=
$item
[
13
];
$deliverySn
=
$item
[
14
];
$tallyRequest
=
$item
[
15
];
$examineRequest
=
$item
[
16
];
$unhealthyAmount
=
$item
[
17
];
$abnormalLevel
=
$item
[
18
];
$unhealthyContent
=
$item
[
19
];
$examineResult
=
$item
[
20
];
$remark
=
$item
[
21
];
if
(
empty
(
$supplierName
))
{
throw
new
\Exception
(
"供应商名称不能为空! (第${lineNo}行),请修改后再次提交"
);
}
if
(
empty
(
$examineTime
))
{
throw
new
\Exception
(
"检货时间不能为空! (第${lineNo}行),请修改为 2000-01-01 这种格式后再次提交"
);
}
if
(
!
isDateTime
(
$examineTime
))
{
throw
new
\Exception
(
"检货时间格式不规范! (第${lineNo}行),请修改为 2000-01-01 这种格式后再次提交"
);
}
if
(
!
empty
(
$stockInDate
)
&&
!
isDateTime
(
$stockInDate
))
{
throw
new
\Exception
(
"入库日期时间格式不规范! (第${lineNo}行),请修改后再次提交"
);
}
if
(
!
in_array
(
$supplierName
,
$supplierNames
))
{
throw
new
\Exception
(
"存在无效供应商(供应商系统不存在)的数据(第${lineNo}行),请修改后再次提交"
);
}
if
(
!
in_array
(
$channelUserName
,
$channelUserNames
))
{
throw
new
\Exception
(
"存在无效的采购员(第${lineNo}行),请修改后再次提交"
);
}
if
(
empty
(
$skuName
))
{
throw
new
\Exception
(
"型号不能为空! (第${lineNo}行),请修改后再次提交"
);
}
if
(
empty
(
$brandName
))
{
throw
new
\Exception
(
"品牌不能为空! (第${lineNo}行),请修改后再次提交"
);
}
if
(
!
is_numeric
(
$amount
)
||
empty
(
$amount
))
{
throw
new
\Exception
(
"数量只能为纯数字且不能为空! (第${lineNo}行),请修改后再次提交"
);
}
if
(
!
is_numeric
(
$unhealthyAmount
)
||
empty
(
$unhealthyAmount
))
{
throw
new
\Exception
(
"不良数量只能为纯数字且不能为空 (第${lineNo}行),请修改后再次提交"
);
}
if
(
empty
(
$examineResult
)
||
!
in_array
(
$examineResult
,
array_values
(
config
(
'field.SupplierExamineResult'
))))
{
throw
new
\Exception
(
"检验结果只能(退货|特批入库|正常入库)其中一个且不能为空 (第${lineNo}行),请修改后再次提交"
);
}
if
(
!
empty
(
$abnormalLevel
)
&&
!
in_array
(
$abnormalLevel
,
[
1
,
2
,
3
]))
{
throw
new
\Exception
(
"存在不合理的异常等级,等级必须为纯数字1,2,3或者不填 (第${lineNo}行),请修改后再次提交"
);
}
$examineData
[]
=
[
'order_sn'
=>
$orderSn
,
'purchase_sn'
=>
$purchaseSn
,
'examine_time'
=>
$examineTime
?
strtotime
(
$examineTime
)
:
0
,
'sales_name'
=>
$salesName
,
'purchase_name'
=>
$channelUserName
,
'ticket_type'
=>
$ticketType
,
'supplier_name'
=>
$supplierName
,
'sku_name'
=>
$skuName
,
'brand_name'
=>
$brandName
,
'amount'
=>
$amount
,
'batch'
=>
$batch
,
'producing_area'
=>
$producingArea
,
'stock_in_date'
=>
$stockInDate
?
strtotime
(
$stockInDate
)
:
0
,
'income_sn'
=>
$incomeSn
,
'delivery_sn'
=>
$deliverySn
,
'tally_request'
=>
$tallyRequest
,
'examine_request'
=>
$examineRequest
,
'unhealthy_amount'
=>
$unhealthyAmount
,
'abnormal_level'
=>
$abnormalLevel
,
'unhealthy_content'
=>
$unhealthyContent
,
'examine_result'
=>
$examineResult
,
'remark'
=>
$remark
,
'create_time'
=>
time
(),
'data_md5'
=>
$dataMd5
,
'create_uid'
=>
request
()
->
user
->
userId
,
'create_name'
=>
request
()
->
user
->
name
,
];
}
if
(
!
in_array
(
$supplierName
,
$supplierNames
))
{
throw
new
\Exception
(
"存在无效供应商(供应商系统不存在)的数据(第${lineNo}行),请修改后再次提交"
);
}
if
(
!
in_array
(
$channelUserName
,
$channelUserNames
))
{
throw
new
\Exception
(
"存在无效的采购员(第${lineNo}行),请修改后再次提交"
);
}
if
(
empty
(
$skuName
))
{
throw
new
\Exception
(
"型号不能为空! (第${lineNo}行),请修改后再次提交"
);
$examineData
=
collect
(
$examineData
);
foreach
(
$examineData
->
chunk
(
50
)
as
$chunk
)
{
SupplierExaminationModel
::
insert
(
$chunk
->
toArray
());
}
if
(
empty
(
$brandName
))
{
throw
new
\Exception
(
"品牌不能为空! (第${lineNo}行),请修改后再次提交"
);
}
if
(
!
is_numeric
(
$amount
)
||
empty
(
$amount
))
{
throw
new
\Exception
(
"数量只能为纯数字且不能为空! (第${lineNo}行),请修改后再次提交"
);
}
if
(
!
is_numeric
(
$unhealthyAmount
)
||
empty
(
$unhealthyAmount
))
{
throw
new
\Exception
(
"不良数量只能为纯数字且不能为空 (第${lineNo}行),请修改后再次提交"
);
}
if
(
empty
(
$examineResult
)
||
!
in_array
(
$examineResult
,
array_values
(
config
(
'field.SupplierExamineResult'
))))
{
throw
new
\Exception
(
"检验结果只能(退货|特批入库|正常入库)其中一个且不能为空 (第${lineNo}行),请修改后再次提交"
);
}
if
(
!
empty
(
$abnormalLevel
)
&&
!
in_array
(
$abnormalLevel
,
[
1
,
2
,
3
]))
{
throw
new
\Exception
(
"存在不合理的异常等级,等级必须为纯数字1,2,3或者不填 (第${lineNo}行),请修改后再次提交"
);
}
$examineData
[]
=
[
'order_sn'
=>
$orderSn
,
'purchase_sn'
=>
$purchaseSn
,
'examine_time'
=>
$examineTime
?
strtotime
(
$examineTime
)
:
0
,
'sales_name'
=>
$salesName
,
'purchase_name'
=>
$channelUserName
,
'ticket_type'
=>
$ticketType
,
'supplier_name'
=>
$supplierName
,
'sku_name'
=>
$skuName
,
'brand_name'
=>
$brandName
,
'amount'
=>
$amount
,
'batch'
=>
$batch
,
'producing_area'
=>
$producingArea
,
'stock_in_date'
=>
$stockInDate
?
strtotime
(
$stockInDate
)
:
0
,
'income_sn'
=>
$incomeSn
,
'delivery_sn'
=>
$deliverySn
,
'tally_request'
=>
$tallyRequest
,
'examine_request'
=>
$examineRequest
,
'unhealthy_amount'
=>
$unhealthyAmount
,
'abnormal_level'
=>
$abnormalLevel
,
'unhealthy_content'
=>
$unhealthyContent
,
'examine_result'
=>
$examineResult
,
'remark'
=>
$remark
,
'create_time'
=>
time
(),
'data_md5'
=>
$dataMd5
,
'create_uid'
=>
request
()
->
user
->
userId
,
'create_name'
=>
request
()
->
user
->
name
,
];
}
$examineData
=
collect
(
$examineData
);
foreach
(
$examineData
->
chunk
(
50
)
as
$chunk
)
{
SupplierExaminationModel
::
insert
(
$chunk
->
toArray
());
}
return
true
;
});
return
true
;
}
catch
(
\Exception
$exception
)
{
return
$exception
->
getMessage
();
}
finally
{
unlink
(
$filePath
);
}
}
private
function
checkImportCsvHeader
(
$header
)
...
...
public/data/import_supplier_examination_template.xlsx
0 → 100644
View file @
cb471566
No preview for this file type
public/data/supplier_examination 副本.xlsx
0 → 100644
View file @
cb471566
No preview for this file type
resources/views/script/SupplierExaminationListScript.blade.php
View file @
cb471566
...
...
@@ -207,7 +207,7 @@
elem
:
'#import_supplier_examination'
,
//绑定元素
url
:
'/api/supplier_examination/ImportSupplierExamination'
,
//上传接口
auto
:
true
,
exts
:
'
csv
'
,
exts
:
'
xlsx
'
,
before
:
function
(
obj
)
{
//obj参数包含的信息,跟 choose回调完全一致,可参见上文。
layer
.
msg
(
'上传中'
,
{
icon
:
16
});
//上传loading
},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment