|
@@ -4,7 +4,9 @@ namespace App\Service;
|
|
|
|
|
|
use App\Exports\TableHeadExport;
|
|
use App\Exports\TableHeadExport;
|
|
use App\Import\ImportAll;
|
|
use App\Import\ImportAll;
|
|
|
|
+use App\Model\Employee;
|
|
use App\Model\Freight;
|
|
use App\Model\Freight;
|
|
|
|
+use App\Model\GiveOut;
|
|
use App\Model\Product;
|
|
use App\Model\Product;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Log;
|
|
@@ -17,6 +19,7 @@ class ImportService extends Service
|
|
public static $type = [
|
|
public static $type = [
|
|
'product', //存货
|
|
'product', //存货
|
|
'freight', //运费设置
|
|
'freight', //运费设置
|
|
|
|
+ 'giveOut', //发放统计
|
|
];
|
|
];
|
|
|
|
|
|
public function getTableTitleXls($data,$user){
|
|
public function getTableTitleXls($data,$user){
|
|
@@ -30,7 +33,11 @@ class ImportService extends Service
|
|
if(! $status) return [false, $msg];
|
|
if(! $status) return [false, $msg];
|
|
|
|
|
|
$headers = array_column($msg,'value');
|
|
$headers = array_column($msg,'value');
|
|
- Excel::store(new TableHeadExport([], $headers),"/public/export/{$filename}", null, 'Xlsx', []);
|
|
|
|
|
|
+ $comments = [];
|
|
|
|
+ foreach ($msg as $value){
|
|
|
|
+ if(! empty($value['comments'])) $comments[$value['value']] = $value['comments'];
|
|
|
|
+ }
|
|
|
|
+ Excel::store(new TableHeadExport([], $headers, $comments),"/public/export/{$filename}", null, 'Xlsx', []);
|
|
return [true, ['file' => $filename]];
|
|
return [true, ['file' => $filename]];
|
|
}
|
|
}
|
|
|
|
|
|
@@ -62,6 +69,16 @@ class ImportService extends Service
|
|
return [true, [$config_array, $filename]];
|
|
return [true, [$config_array, $filename]];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private function giveOut($data,$user){
|
|
|
|
+ $config_array = $this->getTableConfig($data['type']);
|
|
|
|
+ if(empty($config_array)) return [false, ['导入配置表头文件不存在','']];
|
|
|
|
+
|
|
|
|
+ //生成下载文件
|
|
|
|
+ $filename = "发放统计导入模板_" . time() . '.' . 'xlsx';
|
|
|
|
+
|
|
|
|
+ return [true, [$config_array, $filename]];
|
|
|
|
+ }
|
|
|
|
+
|
|
//导入入口
|
|
//导入入口
|
|
public function importAll($data,$user){
|
|
public function importAll($data,$user){
|
|
// //不超时
|
|
// //不超时
|
|
@@ -205,7 +222,6 @@ class ImportService extends Service
|
|
$tmp['crt_id'] = $user['id'];
|
|
$tmp['crt_id'] = $user['id'];
|
|
$tmp['crt_time'] = $time;
|
|
$tmp['crt_time'] = $time;
|
|
|
|
|
|
- //产品主表
|
|
|
|
$insert[] = $tmp;
|
|
$insert[] = $tmp;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -224,6 +240,95 @@ class ImportService extends Service
|
|
return [true, ''];
|
|
return [true, ''];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public function giveOutImport($array, $user, $other_param){
|
|
|
|
+ $upload = $array[0];
|
|
|
|
+ list($status, $msg) = $this->compareTableAndReturn($upload, $other_param);
|
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
|
+ $table_config = $msg;
|
|
|
|
+
|
|
|
|
+ // 去除表头
|
|
|
|
+ unset($array[0]);
|
|
|
|
+ if(empty($array)) return [false, '导入数据不能为空'];
|
|
|
|
+
|
|
|
|
+ list($array, $error) = $this->checkCommon($array, $table_config);
|
|
|
|
+ if(! empty($error)) return [0, $error];
|
|
|
|
+ if(empty($array)) return [false, '导入数据不能为空'];
|
|
|
|
+
|
|
|
|
+ $time = time();
|
|
|
|
+ $new_array = [];
|
|
|
|
+ foreach ($array as $value){
|
|
|
|
+ $tmp = [];
|
|
|
|
+ foreach ($value as $k => $val){
|
|
|
|
+ $field = $table_config[$k]['key'];
|
|
|
|
+ $tmp[$field] = $val;
|
|
|
|
+ }
|
|
|
|
+ $tmp['crt_id'] = $user['id'];
|
|
|
|
+ $tmp['crt_time'] = $time;
|
|
|
|
+
|
|
|
|
+ $new_array[] = $tmp;
|
|
|
|
+ }
|
|
|
|
+ $emp_map = Employee::where('del_time',0)
|
|
|
|
+ ->whereIn('emp_name', array_column($new_array,'employee_id_1_title'))
|
|
|
|
+ ->pluck('id','emp_name')
|
|
|
|
+ ->toArray();
|
|
|
|
+
|
|
|
|
+ $error_2 = [];
|
|
|
|
+ foreach ($new_array as $key => $value){
|
|
|
|
+ $line_number = $key + 2;
|
|
|
|
+ $line = '第' . $line_number . '行';
|
|
|
|
+ if(! isset($emp_map[$value['employee_id_1_title']])) {
|
|
|
|
+ $error_2[] = $line . "业务员不存在或已被删除";
|
|
|
|
+ }else{
|
|
|
|
+ $new_array[$key]['employee_id_1'] = $emp_map[$value['employee_id_1_title']];
|
|
|
|
+ }
|
|
|
|
+ list($status, $msg) = $this->convertExcelCellToDate($value['send_time']);
|
|
|
|
+ if(! $status){
|
|
|
|
+ $error_2[] = $line . "发放日期错误";
|
|
|
|
+ }else{
|
|
|
|
+ $new_array[$key]['send_time'] = $msg;
|
|
|
|
+ }
|
|
|
|
+ if (strpos($value['belong_time'], '|') !== false) {
|
|
|
|
+ list($start,$end) = explode('|', $value['belong_time']);
|
|
|
|
+ $startStamp = strtotime($start);
|
|
|
|
+ $endStamp = strtotime($end);
|
|
|
|
+ if(! $startStamp || ! $endStamp || $startStamp > $endStamp) {
|
|
|
|
+ $error_2[] = $line . "归属日期错误";
|
|
|
|
+ }else{
|
|
|
|
+ $new_array[$key]['start_time'] = $startStamp;
|
|
|
|
+ $new_array[$key]['end_time'] = $endStamp;
|
|
|
|
+ unset($new_array[$key]['belong_time']);
|
|
|
|
+ }
|
|
|
|
+ }else{
|
|
|
|
+ list($status, $msg) = $this->convertExcelCellToDate($value['belong_time']);
|
|
|
|
+ if(! $status){
|
|
|
|
+ $error_2[] = $line . "归属日期错误";
|
|
|
|
+ }else{
|
|
|
|
+ $new_array[$key]['start_time'] = $msg;
|
|
|
|
+ $new_array[$key]['end_time'] = $msg;
|
|
|
|
+ unset($new_array[$key]['belong_time']);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ $res = $this->checkNumber($value['give_out_amount'],2,'positive');
|
|
|
|
+ if(! $res['valid']) $error_2[] = $line . "分红已发放金额:" . $res['error'];
|
|
|
|
+ }
|
|
|
|
+ if(! empty($error_2)) {
|
|
|
|
+ $error_string = implode('|', $error_2);
|
|
|
|
+ return [0, $error_string];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try{
|
|
|
|
+ //新增
|
|
|
|
+ if(! empty($new_array)) GiveOut::insert($new_array);
|
|
|
|
+
|
|
|
|
+ DB::commit();
|
|
|
|
+ }catch (\Exception $e){
|
|
|
|
+ DB::rollBack();
|
|
|
|
+ return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return [true, ''];
|
|
|
|
+ }
|
|
|
|
+
|
|
//公共校验
|
|
//公共校验
|
|
private function checkCommon($array, $table_config){
|
|
private function checkCommon($array, $table_config){
|
|
$error = [];
|
|
$error = [];
|
|
@@ -304,5 +409,37 @@ class ImportService extends Service
|
|
|
|
|
|
return [true, $config_array];
|
|
return [true, $config_array];
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ //转换日期
|
|
|
|
+ function convertExcelCellToDate($cellValue) {
|
|
|
|
+ // 尝试将单元格值转换为浮点数(Excel 日期序列号)
|
|
|
|
+ $excelTimestamp = filter_var($cellValue, FILTER_VALIDATE_FLOAT);
|
|
|
|
+
|
|
|
|
+ if ($excelTimestamp !== false && $excelTimestamp > 0) {
|
|
|
|
+ // 如果成功转换并且值大于0,则认为是Excel日期序列号
|
|
|
|
+ try {
|
|
|
|
+ $dateTimeObject = Date::excelToDateTimeObject($cellValue);
|
|
|
|
+
|
|
|
|
+// if ($dateTimeObject->format('H:i:s') === '00:00:00') {
|
|
|
|
+// // 如果是,则将时间设置为 '23:59:00'
|
|
|
|
+// $dateTimeObject->setTime(23, 59);
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ // 现在你可以格式化这个日期了
|
|
|
|
+ $formattedDate = $dateTimeObject->format('Y-m-d');
|
|
|
|
+ if(! strtotime($formattedDate)) return [false, ''];
|
|
|
|
+
|
|
|
|
+ return [true, strtotime($formattedDate)];
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
+ // 处理转换失败的情况
|
|
|
|
+ return [false, '单元格日期格式转换时间戳失败'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 如果不是有效的浮点数,则尝试按照多种日期格式解析
|
|
|
|
+ if(! strtotime($cellValue)) return [false, '单元格文本格式转换时间戳失败'];
|
|
|
|
+
|
|
|
|
+ return [true, strtotime($cellValue)];
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|