|
|
@@ -4,7 +4,10 @@ namespace App\Service;
|
|
|
|
|
|
use App\Exports\TableHeadExport;
|
|
|
use App\Import\ImportAll;
|
|
|
+use App\Model\CustomerSupply;
|
|
|
use App\Model\Employee;
|
|
|
+use App\Model\Order;
|
|
|
+use App\Model\OrderDetails;
|
|
|
use App\Model\Organization;
|
|
|
use App\Model\GiveOut;
|
|
|
use App\Model\Product;
|
|
|
@@ -17,9 +20,7 @@ use PhpOffice\PhpSpreadsheet\Shared\Date;
|
|
|
class ImportService extends Service
|
|
|
{
|
|
|
public static $type = [
|
|
|
- 'product', //存货
|
|
|
- 'freight', //运费设置
|
|
|
- 'giveOut', //发放统计
|
|
|
+ 'order', //订单
|
|
|
];
|
|
|
|
|
|
public function getTableTitleXls($data,$user){
|
|
|
@@ -45,36 +46,16 @@ class ImportService extends Service
|
|
|
if(empty($type)) return [];
|
|
|
|
|
|
//获取配置文件
|
|
|
- $config = "excel." . $type . "Table";
|
|
|
+ $config = "excel." . $type;
|
|
|
return config($config) ?? [];
|
|
|
}
|
|
|
|
|
|
- private function product($data,$user){
|
|
|
+ private function order($data,$user){
|
|
|
$config_array = $this->getTableConfig($data['type']);
|
|
|
if(empty($config_array)) return [false, ['导入配置表头文件不存在','']];
|
|
|
|
|
|
//生成下载文件
|
|
|
- $filename = "存货导入模板_" . time() . '.' . 'xlsx';
|
|
|
-
|
|
|
- return [true, [$config_array, $filename]];
|
|
|
- }
|
|
|
-
|
|
|
- private function freight($data,$user){
|
|
|
- $config_array = $this->getTableConfig($data['type']);
|
|
|
- if(empty($config_array)) return [false, ['导入配置表头文件不存在','']];
|
|
|
-
|
|
|
- //生成下载文件
|
|
|
- $filename = "运费设置导入模板_" . time() . '.' . 'xlsx';
|
|
|
-
|
|
|
- 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';
|
|
|
+ $filename = "订单导入模板_" . time() . '.' . 'xlsx';
|
|
|
|
|
|
return [true, [$config_array, $filename]];
|
|
|
}
|
|
|
@@ -131,7 +112,7 @@ class ImportService extends Service
|
|
|
return [true, ''];
|
|
|
}
|
|
|
|
|
|
- public function productImport($array, $user, $other_param){
|
|
|
+ public function orderImport($array, $user, $other_param){
|
|
|
$upload = $array[0];
|
|
|
list($status, $msg) = $this->compareTableAndReturn($upload, $other_param);
|
|
|
if(! $status) return [false, $msg];
|
|
|
@@ -145,48 +126,77 @@ class ImportService extends Service
|
|
|
if(! empty($error)) return [0, $error];
|
|
|
if(empty($array)) return [false, '导入数据不能为空'];
|
|
|
|
|
|
- //查找产品
|
|
|
- $product_map = Product::whereIn("code", array_column($array,0))
|
|
|
- ->where('del_time',0)
|
|
|
- ->pluck('id','code')
|
|
|
- ->toArray();
|
|
|
+ list($error, $map, $total_map) = $this->orderCheck($array, $user);
|
|
|
+ if(! empty($error)) return [0, $error];
|
|
|
+ $order_map = $this->getOrder($array, $user);
|
|
|
|
|
|
$time = time();
|
|
|
- $insert = $update = [];
|
|
|
- foreach ($array as $value){
|
|
|
- $tmp = [];
|
|
|
+ $insert = $update = $detail = $order_number = [];
|
|
|
+ foreach ($array as $key => $value){
|
|
|
+ $main_tmp = $detail_tmp = [];
|
|
|
foreach ($value as $k => $val){
|
|
|
- $field = $table_config[$k]['key'];
|
|
|
- $tmp[$field] = $val;
|
|
|
+ $t = $table_config[$k];
|
|
|
+ $field = $t['key'];
|
|
|
+ if(! empty($t['is_main'])){
|
|
|
+ $main_tmp[$field] = $val;
|
|
|
+ }
|
|
|
+ if(! empty($t['is_detail'])){
|
|
|
+ $detail_tmp[$field] = $val;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- if(isset($product_map[$tmp['code']])){
|
|
|
- $product_id = $product_map[$tmp['code']];
|
|
|
- //产品主表
|
|
|
- $update[$product_id] = $tmp;
|
|
|
+ $main_tmp['customer_id'] = $map[$key]['customer_id'] ?? 0;
|
|
|
+ $main_tmp['supply_id'] = $map[$key]['supply_id'] ?? 0;
|
|
|
+ $main_tmp['quantity'] = $total_map[$main_tmp['order_number']] ?? 0;
|
|
|
+ if(isset($order_map[$main_tmp['order_number']])){
|
|
|
+ $order_id = $order_map[$main_tmp['order_number']];
|
|
|
+ //更新
|
|
|
+ $update[$order_id] = $main_tmp;
|
|
|
}else{
|
|
|
- $tmp['crt_id'] = $user['id'];
|
|
|
- $tmp['crt_time'] = $time;
|
|
|
+ $main_tmp['crt_id'] = $user['id'];
|
|
|
+ $main_tmp['crt_time'] = $time;
|
|
|
|
|
|
- //产品主表
|
|
|
- $insert[$tmp['code']] = $tmp;
|
|
|
+ //新增
|
|
|
+ $insert[$main_tmp['order_number']] = $main_tmp;
|
|
|
}
|
|
|
+
|
|
|
+ $detail_tmp['order_number'] = $main_tmp['order_number'];
|
|
|
+ $detail_tmp['crt_time'] = $time;
|
|
|
+ $detail[] = $detail_tmp;
|
|
|
+
|
|
|
+ $order_number[] = $main_tmp['order_number'];
|
|
|
}
|
|
|
|
|
|
try{
|
|
|
//新增
|
|
|
if(! empty($insert)){
|
|
|
- Product::insert($insert);
|
|
|
+ Order::insert($insert);
|
|
|
}
|
|
|
|
|
|
//编辑
|
|
|
if(! empty($update)){
|
|
|
foreach ($update as $p_id => $value){
|
|
|
- Product::where('id',$p_id)
|
|
|
+ Order::where('id',$p_id)
|
|
|
->update($value);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ if(! empty($detail)){
|
|
|
+ $maps = Order::where('del_time',0)
|
|
|
+ ->whereIn('order_number',$order_number)
|
|
|
+ ->pluck('id','order_number')
|
|
|
+ ->toArray();
|
|
|
+ foreach ($detail as $key => $value){
|
|
|
+ $detail[$key]['order_id'] = $maps[$value['order_number']] ?? 0;
|
|
|
+ unset($detail[$key]['order_number']);
|
|
|
+ }
|
|
|
+ OrderDetails::where('del_time',0)
|
|
|
+ ->where('order_id',array_values($maps))
|
|
|
+ ->update(['del_time' => $time]);
|
|
|
+
|
|
|
+ OrderDetails::insert($detail);
|
|
|
+ }
|
|
|
+
|
|
|
DB::commit();
|
|
|
}catch (\Exception $e){
|
|
|
DB::rollBack();
|
|
|
@@ -196,137 +206,181 @@ class ImportService extends Service
|
|
|
return [true, ''];
|
|
|
}
|
|
|
|
|
|
- public function freightImport($array, $user, $other_param){
|
|
|
- $upload = $array[0];
|
|
|
- list($status, $msg) = $this->compareTableAndReturn($upload, $other_param);
|
|
|
- if(! $status) return [false, $msg];
|
|
|
- $table_config = $msg;
|
|
|
+ private function getCustomerSupply($array, $user){
|
|
|
+ //查找客户 供应商
|
|
|
+ $customer_codes = array_unique(array_filter(array_column($array,1)));
|
|
|
+ $customer_titles = array_unique(array_filter(array_column($array,2)));
|
|
|
+ $supply_codes = array_unique(array_filter(array_column($array,3)));
|
|
|
+ $supply_titles = array_unique(array_filter(array_column($array,4)));
|
|
|
+ $records = CustomerSupply::where('del_time', 0)
|
|
|
+ ->where('crt_id', $user['id'])
|
|
|
+ ->where(function($query) use ($customer_codes, $customer_titles, $supply_codes, $supply_titles) {
|
|
|
+ $query->where(function($q) use ($customer_codes, $customer_titles) {
|
|
|
+ $q->where('type', CustomerSupply::type_one)
|
|
|
+ ->where(function($sub) use ($customer_codes, $customer_titles) {
|
|
|
+ $sub->whereIn('code', $customer_codes)
|
|
|
+ ->orWhereIn('title', $customer_titles);
|
|
|
+ });
|
|
|
+ })->orWhere(function($q) use ($supply_codes, $supply_titles) {
|
|
|
+ $q->where('type', CustomerSupply::type_two)
|
|
|
+ ->where(function($sub) use ($supply_codes, $supply_titles) {
|
|
|
+ $sub->whereIn('code', $supply_codes)
|
|
|
+ ->orWhereIn('title', $supply_titles);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ })
|
|
|
+ ->select('id','title','code','type')
|
|
|
+ ->get()
|
|
|
+ ->toArray();
|
|
|
|
|
|
- // 去除表头
|
|
|
- unset($array[0]);
|
|
|
- if(empty($array)) return [false, '导入数据不能为空'];
|
|
|
+ return $records;
|
|
|
+ }
|
|
|
|
|
|
- list($array, $error) = $this->checkCommon($array, $table_config);
|
|
|
- if(! empty($error)) return [0, $error];
|
|
|
- if(empty($array)) return [false, '导入数据不能为空'];
|
|
|
+ private function getOrder($array, $user){
|
|
|
+ $order = array_unique(array_filter(array_column($array,0)));
|
|
|
+ $records = Order::where('del_time', 0)
|
|
|
+ ->where('crt_id', $user['id'])
|
|
|
+ ->whereIn('order_number', $order)
|
|
|
+ ->pluck('id','order_number')
|
|
|
+ ->toArray();
|
|
|
|
|
|
- $time = time();
|
|
|
- $insert = [];
|
|
|
- foreach ($array as $value){
|
|
|
- $tmp = [];
|
|
|
- foreach ($value as $k => $val){
|
|
|
- $field = $table_config[$k]['key'];
|
|
|
- $tmp[$field] = $val;
|
|
|
- }
|
|
|
+ return $records;
|
|
|
+ }
|
|
|
|
|
|
- $tmp['crt_id'] = $user['id'];
|
|
|
- $tmp['crt_time'] = $time;
|
|
|
+ private function orderCheck(&$array, $user)
|
|
|
+ {
|
|
|
+ // 查询所有客户 / 供应商档案
|
|
|
+ $customer_supply = $this->getCustomerSupply($array, $user);
|
|
|
|
|
|
- $insert[] = $tmp;
|
|
|
+ // 建立映射表(code、title 都可以匹配到记录)
|
|
|
+ $map_by_code = array_column($customer_supply, null, 'code');
|
|
|
+ $map_by_title = [];
|
|
|
+ foreach ($customer_supply as $value){
|
|
|
+ $map_by_title[$value['title']][] = $value;
|
|
|
}
|
|
|
|
|
|
- try{
|
|
|
- Organization::where('del_time',0)
|
|
|
- ->update(['del_time' => $time]);
|
|
|
- //新增
|
|
|
- if(! empty($insert)) Organization::insert($insert);
|
|
|
+ $errors = []; // 存放错误信息
|
|
|
+ $success = []; // 存放校验通过的行
|
|
|
|
|
|
- DB::commit();
|
|
|
- }catch (\Exception $e){
|
|
|
- DB::rollBack();
|
|
|
- return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
|
|
|
- }
|
|
|
+ $order_material_map = [];
|
|
|
+ $order_total_map = []; // 统计每个订单号的总数量
|
|
|
+ foreach ($array as $rowIndex => $value) {
|
|
|
+ $rowNum = $rowIndex; // 行号
|
|
|
|
|
|
- return [true, ''];
|
|
|
- }
|
|
|
+ if(! empty($value[5])){
|
|
|
+ list($status, $msg) = $this->convertExcelCellToDate($value[5]);
|
|
|
+ if(! $status){
|
|
|
+ $error_2[] = "第" . $rowNum ."行" . "的订单日期错误";
|
|
|
+ }else{
|
|
|
+ $array[$rowIndex][5] = $msg;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- 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;
|
|
|
+ // -------- 客户校验 --------
|
|
|
+ $customer_id = 0;
|
|
|
+ if (!empty($value[1]) || !empty($value[2])) {
|
|
|
+ $customer_id = $this->checkData($value, $rowNum,'客户',CustomerSupply::type_one, $errors,$map_by_code,$map_by_title);
|
|
|
+ }
|
|
|
|
|
|
- // 去除表头
|
|
|
- unset($array[0]);
|
|
|
- if(empty($array)) return [false, '导入数据不能为空'];
|
|
|
+ // -------- 供应商校验 --------
|
|
|
+ $supply_id = 0;
|
|
|
+ if (!empty($value[3]) || !empty($value[4])) {
|
|
|
+ $supply_id = $this->checkData($value, $rowNum,'供应商',CustomerSupply::type_two, $errors,$map_by_code,$map_by_title);
|
|
|
+ }
|
|
|
|
|
|
- list($array, $error) = $this->checkCommon($array, $table_config);
|
|
|
- if(! empty($error)) return [0, $error];
|
|
|
- if(empty($array)) return [false, '导入数据不能为空'];
|
|
|
+ $success[$rowNum] = [
|
|
|
+ 'customer_id' => $customer_id,
|
|
|
+ 'supply_id' => $supply_id,
|
|
|
+ ];
|
|
|
+
|
|
|
+ // -------- 新增:校验订单号 + 物料编码 不重复 --------
|
|
|
+ $order_no = trim($value[0] ?? '');
|
|
|
+ $material_code = trim($value[6] ?? '');
|
|
|
+
|
|
|
+ if ($order_no !== '' && $material_code !== '') {
|
|
|
+ // 组合唯一 key
|
|
|
+ $unique_key = $order_no . '|' . $material_code;
|
|
|
+
|
|
|
+ if (isset($order_material_map[$unique_key])) {
|
|
|
+ // 已经存在重复
|
|
|
+ $firstRow = $order_material_map[$unique_key];
|
|
|
+ $errors[] = "第{$rowNum}行:订单号 {$order_no} 下的物料编码 {$material_code} 重复(首次出现在第 {$firstRow} 行)";
|
|
|
+ } else {
|
|
|
+ // 记录首次出现的行号
|
|
|
+ $order_material_map[$unique_key] = $rowNum;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $qty = $value[10];
|
|
|
+ if ($order_no !== '') {
|
|
|
+ if (isset($order_total_map[$order_no])) {
|
|
|
+ $tmp_num = bcadd($qty, $order_total_map[$order_no],2);
|
|
|
+ $order_total_map[$order_no] = $tmp_num;
|
|
|
+ }else{
|
|
|
+ $order_total_map[$order_no] = $qty;
|
|
|
+ }
|
|
|
|
|
|
- $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;
|
|
|
+ $error_string = "";
|
|
|
+ if(! empty($errors)) $error_string = implode('|', $errors);
|
|
|
+
|
|
|
+ return [
|
|
|
+ $error_string ,
|
|
|
+ $success,
|
|
|
+ $order_total_map
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function checkData($value, $rowNum, $main_title, $type,&$errors,$map_by_code,$map_by_title){
|
|
|
+ $customer_id_1 = $customer_id_2 = 0;
|
|
|
+ if($type == CustomerSupply::type_one){
|
|
|
+ $code = $value[1] ?? '';
|
|
|
+ $title = $value[2] ?? '';
|
|
|
+ }else{
|
|
|
+ $code = $value[3] ?? '';
|
|
|
+ $title = $value[4] ?? '';
|
|
|
}
|
|
|
- $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;
|
|
|
+ $match_code = $map_by_code[$code] ?? null;
|
|
|
+ $match_title = $map_by_title[$title] ?? null;
|
|
|
+ if($code){
|
|
|
+ if(empty($match_code) || $match_code['type'] != $type){
|
|
|
+ $errors[] = "第{$rowNum}行:{$main_title}编码({$code}) 未在档案中找到";
|
|
|
+ }else {
|
|
|
+ $customer_id_1 = $match_code['id'];
|
|
|
}
|
|
|
- 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 . "归属日期错误";
|
|
|
+ }
|
|
|
+ if($title){
|
|
|
+ if(empty($match_title)){
|
|
|
+ $errors[] = "第{$rowNum}行:{$main_title}名称({$title}) 未在档案中找到";
|
|
|
+ }elseif (count($match_title) > 1){
|
|
|
+ if(empty($code)){
|
|
|
+ $errors[] = "第{$rowNum}行:{$main_title}名称({$title}) 在档案中找到多条数据,请填写客户编码";
|
|
|
}else{
|
|
|
- $new_array[$key]['start_time'] = $msg;
|
|
|
- $new_array[$key]['end_time'] = $msg;
|
|
|
- unset($new_array[$key]['belong_time']);
|
|
|
+ $tmp = array_column($match_title,null,'code');
|
|
|
+ if(isset($tmp[$code])){
|
|
|
+ $t = $tmp[$code];
|
|
|
+ if($t['type'] != $type) {
|
|
|
+ $errors[] = "第{$rowNum}行:{$main_title}名称({$title}) 未在档案中找到";
|
|
|
+ }else{
|
|
|
+ $customer_id_2 = $t['id'];
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ $errors[] = "第{$rowNum}行:{$main_title}编码和名称对应数据未在档案中找到";
|
|
|
+ }
|
|
|
}
|
|
|
+ }else {
|
|
|
+ $tt = $match_title[0] ?? [];
|
|
|
+ $customer_id_2 = $tt['id'] ?? 0;
|
|
|
}
|
|
|
- $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];
|
|
|
}
|
|
|
+ if($customer_id_1 && $customer_id_2 && $customer_id_1 != $customer_id_2) $errors[] = "第{$rowNum}行:{$main_title}编码和名称对应数据未在档案中找到";
|
|
|
|
|
|
- try{
|
|
|
- //新增
|
|
|
- if(! empty($new_array)) GiveOut::insert($new_array);
|
|
|
-
|
|
|
- DB::commit();
|
|
|
- }catch (\Exception $e){
|
|
|
- DB::rollBack();
|
|
|
- return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
|
|
|
- }
|
|
|
+ $customer_id = $customer_id_1 ? $customer_id_1 : $customer_id_2;
|
|
|
|
|
|
- return [true, ''];
|
|
|
+ return $customer_id;
|
|
|
}
|
|
|
|
|
|
//公共校验
|