ImportService.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. <?php
  2. namespace App\Service;
  3. use App\Exports\TableHeadExport;
  4. use App\Import\Import;
  5. use App\Import\ImportAll;
  6. use App\Model\BasicType;
  7. use App\Model\Customer;
  8. use App\Model\CustomerInfo;
  9. use App\Model\Employee;
  10. use App\Model\Product;
  11. use App\Model\ProductCategory;
  12. use App\Model\ProductPriceDetail;
  13. use App\Model\SalesOrder;
  14. use App\Model\SalesOrderInfo;
  15. use App\Model\SalesOrderProductInfo;
  16. use Illuminate\Support\Facades\DB;
  17. use Maatwebsite\Excel\Facades\Excel;
  18. use PhpOffice\PhpSpreadsheet\IOFactory;
  19. class ImportService extends Service
  20. {
  21. public static $type = [
  22. 'product', //产品
  23. 'customer', //客户
  24. 'salesOnline', //线上订单
  25. ];
  26. //写活的导入------------------- 暂时不好用
  27. //导入入口
  28. public function import($data,$user){
  29. if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
  30. if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
  31. if(empty($data['file'])) return [false,'导入文件不能为空'];
  32. //导入的数据并且校验写入
  33. list($status,$msg) = $this->importMain($data,$user);
  34. if(! $status) return [false, $msg];
  35. return [true, ''];
  36. }
  37. //主方法
  38. public function importMain($data,$user){
  39. //获取配置文件
  40. $config = "excel." . $data['type'];
  41. $config_array = config($config) ?? [];
  42. if(empty($config_array)) return [false, '配置文件不存在'];
  43. //(特殊 额外的表头数据)
  44. $config_array = $this->getTableTitle($config_array,$user,$data);
  45. //获取合并单元格范围
  46. $uploadedFile = $_FILES['file']['tmp_name']; // 获取上传文件的临时路径
  47. $spreadsheet = IOFactory::load($uploadedFile); // 加载上传的 Excel 文件
  48. $worksheet = $spreadsheet->getActiveSheet(); // 获取第一个工作表
  49. $mergedCells = $worksheet->getMergeCells(); // 获取单元格合并范围
  50. // 需要导入的公用数据
  51. $msg['user_id'] = $user['id'];
  52. $msg['depart_id'] = $this->getDepart($user);
  53. $msg['top_depart_id'] = $user['depart_map'][$msg['depart_id']] ?? 0;
  54. //导入
  55. $import = new Import();
  56. $import->setConfig($config_array, $mergedCells,$msg);
  57. \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
  58. //异常提示报错
  59. if($import->getMsg()) return [false, $import->getMsg()];
  60. return [true, ''];
  61. }
  62. //表头入口
  63. public function getTableTitle($config_array,$user,$data){
  64. if(! empty($config_array['dynamics_field'])){
  65. $func = $config_array['dynamics_field']['func'];
  66. return $this->$func($config_array,$user,$data);
  67. }
  68. return $config_array;
  69. }
  70. //产品导入的额外表头
  71. public function productTitle($config_array,$user,$data){
  72. $model = BasicType::TopClear($user,$data);
  73. $result = $model->whereRaw('type = 22 And del_time = 0')->get()->toArray();
  74. if(! empty($result)){
  75. foreach ($result as $value){
  76. $config_array['field'][$value['title']] = [
  77. "key" => $config_array['dynamics_field']['name'],
  78. "key_array" => [
  79. "basic_type_id" => $value['id'],
  80. "price" => 0,
  81. ],
  82. "rule" => "",
  83. "other_rule" => "is_numeric",
  84. "multiple" => true,
  85. "map" => [
  86. $value['title'] => "price",
  87. ],
  88. ];
  89. }
  90. }
  91. return $config_array;
  92. }
  93. //产品导入的额外数据
  94. public function fillInsertProductData($time){
  95. $last_insert_data = Product::where('crt_time',$time)
  96. ->where('del_time',0)
  97. ->select("id","product_category_id")
  98. ->get()->toArray();
  99. if(empty($last_insert_data)) return;
  100. $list = ProductCategory::where('del_time',0)
  101. ->select('id','parent_id')
  102. ->get()->toArray();
  103. foreach ($last_insert_data as $value){
  104. $parentsId = $this->findParentIds($value['product_category_id'], $list);
  105. array_unshift($parentsId, $value['product_category_id']);
  106. $result = array_reverse($parentsId);
  107. Product::where('id',$value['id'])->update([
  108. 'product_category' => json_encode($result)
  109. ]);
  110. }
  111. }
  112. //写活的导入------------------- 暂时不好用
  113. //写死的导入
  114. public function getTableTitleXls($data,$user){
  115. if(empty($data['type'])) return [false,'缺少类型'];
  116. if(! in_array($data['type'],self::$type)) return [false,'类型不存在'];
  117. //获取配置文件
  118. $fuc = $data['type'];
  119. list($status,$msg,$filename) = $this->$fuc($data,$user);
  120. if(!$status) return [false, $msg];
  121. $headers = array_column($msg,'value');
  122. Excel::store(new TableHeadExport([], $headers),"/public/export/{$filename}", null, 'Xlsx', []);
  123. return [true, ['file' => $filename]];
  124. }
  125. private function customer($data,$user){
  126. //生成下载文件
  127. $filename = "客户模板_" . time() . '.' . 'xlsx';
  128. //获取配置文件
  129. $config = "excel.customerTable";
  130. $config_array = config($config) ?? [];
  131. if(empty($config_array)) return [false, '配置文件不存在',''];
  132. return [true, $config_array,$filename];
  133. }
  134. private function product($data,$user){
  135. //获取配置文件
  136. $config = "excel.productTable";
  137. $config_array = config($config) ?? [];
  138. if(empty($config_array)) return [false, '配置文件不存在', ''];
  139. $result = (new BasicTypeService())->getMyBasicList($user, 22);
  140. if(! empty($result)){
  141. foreach ($result as $value){
  142. $config_array[] = [
  143. 'key' => 'table_id.' . $value['id'],
  144. 'value' => $value['title'],
  145. ];
  146. }
  147. }
  148. //生成下载文件
  149. $filename = "产品模板_" . time() . '.' . 'xlsx';
  150. return [true, $config_array,$filename];
  151. }
  152. private function salesOnline($data,$user){
  153. //生成下载文件
  154. $filename = "线上订单模板_" . time() . '.' . 'xlsx';
  155. //获取配置文件
  156. $config = "excel.salesOnlineTable";
  157. $config_array = config($config) ?? [];
  158. if(empty($config_array)) return [false, '配置文件不存在',''];
  159. return [true, $config_array,$filename];
  160. }
  161. //导入入口
  162. public function importAll($data,$user){
  163. if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
  164. if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
  165. if(empty($data['file'])) return [false,'导入文件不能为空'];
  166. $import = new ImportAll();
  167. //设置导入人id
  168. $import->setCrt($user['id']);
  169. $import->setUser($user);
  170. $import->setType($data['type']);
  171. //导入
  172. \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
  173. if($import->getMsg()) return [false, $import->getMsg()];
  174. return [true, ''];
  175. }
  176. public function customerImport($array, $user){
  177. $head = $user['head']['id'] ?? 0;
  178. // 去除表头
  179. unset($array[0]);
  180. if(empty($array)) return [false, '导入数据不能为空'];
  181. //第一次表格数据校验 非空 已经过滤数据
  182. $array_clean = $contact_info = [];
  183. foreach ($array as $key => $value){
  184. $rowData = array_filter($value);
  185. if (empty($rowData)) {
  186. unset($array[$key]);
  187. } elseif(empty($value[0]) || empty($value[1])) {
  188. return [false, '带*号的字段项必填'];
  189. }else{
  190. foreach ($value as $k => $v){
  191. $value[$k] = trim($v);
  192. }
  193. if(! isset(Customer::dk[$value[0]])) return [false, '客户模板填写错误'];
  194. $value[0] = Customer::dk[$value[0]];
  195. if(in_array($value[1],$array_clean)) return [false, '客户名称不能重复'];
  196. $array_clean[] = $value[1];
  197. $array[$key] = $value;
  198. if(! empty($value[13])){
  199. if(in_array($value[13],$contact_info)) return [false, '联系方式内容不能重复'];
  200. $contact_info[] = $value[13];
  201. }
  202. }
  203. }unset($array_clean);
  204. if(empty($array)) return [false, '导入数据不能为空'];
  205. //客户
  206. $model = Customer::Clear($user,[]);
  207. $customer = $model->where('del_time',0)
  208. ->whereIn('title',array_unique(array_column($array,'1')))
  209. ->pluck('id','title')
  210. ->toArray();
  211. $basic = (new BasicTypeService())->getMyBasicList($user, [1,2,3,4,5,9,10,30]);
  212. $basic_list = [];
  213. foreach ($basic as $value){
  214. if($value['type'] == 1){
  215. $basic_list[2][$value['title']] = $value['id'];
  216. }elseif ($value['type'] == 2){
  217. $basic_list[3][$value['title']] = $value['id'];
  218. }elseif ($value['type'] == 3){
  219. $basic_list[41][$value['title']] = $value['id'];
  220. }elseif ($value['type'] == 30){
  221. $basic_list[42][$value['title']] = $value['id'];
  222. }elseif ($value['type'] == 4){
  223. $basic_list[12][$value['title']] = $value['id'];
  224. }elseif ($value['type'] == 5){
  225. $basic_list[7][$value['title']] = $value['id'];
  226. }elseif ($value['type'] == 9){
  227. $basic_list[8][$value['title']] = $value['id'];
  228. }elseif ($value['type'] == 10){
  229. $basic_list[9][$value['title']] = $value['id'];
  230. }
  231. }
  232. $model = Product::ProductClear($user,[]);
  233. $product = $model->where('del_time',0)
  234. ->whereIn('title',array_unique(array_column($array,'6')))
  235. ->pluck('id','title')
  236. ->toArray();
  237. $emp = Employee::where('del_time',0)
  238. ->whereIn('number',array_unique(array_column($array,'14')))
  239. ->pluck('id','number')
  240. ->toArray();
  241. $top_depart_id = $user['depart_top'][0] ?? [];
  242. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  243. $contact_info_array = CustomerInfo::from('customer_info as a')
  244. ->join('customer as b','b.id','a.customer_id')
  245. ->where('a.del_time',0)
  246. ->where('b.del_time',0)
  247. ->where('b.top_depart_id',$top_depart_id)
  248. ->whereIn('a.contact_info', $contact_info)
  249. ->select('a.contact_info')->get()->toArray();
  250. $contact_info_array = array_column($contact_info_array,'contact_info');
  251. $time = time();
  252. $insert = [];
  253. $insert_detail = $insert_detail2 = [];
  254. foreach ($array as $value){
  255. $tmp = [
  256. 'model_type' => '',
  257. 'title' => '',
  258. 'customer_intention' => '',
  259. 'customer_from' => '',
  260. 'customer_type' => '',
  261. 'car_type' => '',
  262. 'consulting_product' => '',
  263. 'intention_product' => '',
  264. 'progress_stage' => '',
  265. 'state_type' => '',
  266. 'address2' => '',
  267. 'mark' => '',
  268. 'depart_id' => $head,
  269. 'top_depart_id' => $head,
  270. 'crt_id' => $user['id'],
  271. 'crt_time' => $time,
  272. 'upd_time' => $time,
  273. ];
  274. $tmp['model_type'] = $value['0'];
  275. $tmp['consulting_product'] = $value['5'];
  276. $tmp['mark'] = $value['10'];
  277. $tmp['address2'] = $value['11'];
  278. if(! empty($customer[$value['1']])) return [false, '客户:' . $value['1'] . '已存在'];
  279. $tmp['title'] = $value['1'];
  280. if($value['2']){
  281. if(empty($basic_list[2][$value['2']])) return [false, '客户意向度:' . $value['2'] . '不存在'];
  282. $tmp['customer_intention'] = $basic_list[2][$value['2']];
  283. }
  284. if($value['3']){
  285. if(empty($basic_list[3][$value['3']])) return [false, '客户来源:' . $value['3'] . '不存在'];
  286. $tmp['customer_from'] = $basic_list[3][$value['3']];
  287. }
  288. if($value['4']){
  289. $keys = 4 . $value[0];
  290. $model_title = Customer::dk2[$value[0]] ?? "";
  291. if(empty($basic_list[$keys][$value['4']])) return [false, '模板:' . $model_title . '下客户类别:' . $value['4'] . '不存在'];
  292. $tmp['customer_type'] = $basic_list[$keys][$value['4']];
  293. }
  294. if($value['6']){
  295. if(empty($product[$value['6']])) return [false, '意向产品:' . $value['6'] . '不存在'];
  296. $tmp['intention_product'] = $product[$value['6']];
  297. }
  298. if($value['7']){
  299. if(empty($basic_list[7][$value['7']])) return [false, '进展阶段:' . $value['7'] . '不存在'];
  300. $tmp['progress_stage'] = $basic_list[7][$value['7']];
  301. }
  302. if($value['8']){
  303. if(empty($basic_list[8][$value['8']])) return [false, '状态:' . $value['8'] . '不存在'];
  304. $tmp['state_type'] = $basic_list[8][$value['8']];
  305. }
  306. if($value['9']){
  307. if(empty($basic_list[9][$value['9']])) return [false, '车型:' . $value['9'] . '不存在'];
  308. $tmp['car_type'] = $basic_list[9][$value['9']];
  309. }
  310. $contact_id = 0;
  311. if($value['12']){
  312. if(empty($basic_list[12][$value['12']])) return [false, '联系方式类型:' . $value['12'] . '不存在'];
  313. $contact_id = $basic_list[12][$value['12']];
  314. }
  315. if($value['13'] && in_array($value['13'],$contact_info_array)) return [false, '联系方式内容:' . $value['13'] . '已存在'];
  316. $man = 0;
  317. if($value['14']){
  318. if(empty($emp[$value['14']])) return [false, '负责人:' . $value['14'] . '不存在'];
  319. $man = $emp[$value['14']];
  320. }
  321. // if($value['12']){
  322. // if(empty($emp[$value['12']])) return [false, '协同人:' . $value['12'] . '不存在'];
  323. // $tmp['emp_two'] = $emp[$value['12']];
  324. // }
  325. $insert[] = $tmp;
  326. $insert_detail[] = [
  327. 'customer_id' => 0,
  328. 'contact_type' => $contact_id,
  329. 'contact_info' => $value['13'],
  330. 'crt_time' => $time,
  331. 'type' => CustomerInfo::type_one
  332. ];
  333. $insert_detail2[] = [
  334. 'customer_id' => 0,
  335. 'data_id' => $man,
  336. 'crt_time' => $time,
  337. 'type' => CustomerInfo::type_two
  338. ];
  339. }
  340. try{
  341. DB::beginTransaction();
  342. if(! empty($insert)) Customer::insert($insert);
  343. //获取上一次所有id
  344. $last_insert_id = Customer::where('crt_time',$time)
  345. ->where('crt_time',$time)
  346. ->where('depart_id',$head)
  347. ->where('top_depart_id',$head)
  348. ->where('crt_id',$user['id'])
  349. ->select('id')->get()->toArray();
  350. $last_insert_id = array_column($last_insert_id,'id');
  351. //组织数据 写入与主表的关联id
  352. $insert_detail_1 = [];
  353. foreach ($insert_detail as $key => $value){
  354. if(empty($value['contact_type']) && empty($value['contact_info'])) continue;
  355. $value['customer_id'] = $last_insert_id[$key];
  356. $insert_detail_1[] = $value;
  357. }unset($insert_detail);
  358. $insert_detail_2 = [];
  359. foreach ($insert_detail2 as $key => $value){
  360. if(empty($value['data_id'])) continue;
  361. $value['customer_id'] = $last_insert_id[$key];
  362. $insert_detail_2[] = $value;
  363. }unset($insert_detail2);
  364. if(! empty($insert_detail_1)) CustomerInfo::insert($insert_detail_1);
  365. if(! empty($insert_detail_2)) CustomerInfo::insert($insert_detail_2);
  366. DB::commit();
  367. }catch (\Exception $e){
  368. DB::rollBack();
  369. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  370. }
  371. return [true, ''];
  372. }
  373. public function productImport($array, $user){
  374. $head = $user['head']['id'] ?? 0;
  375. // 去除表头
  376. $upload = $array[0];
  377. unset($array[0]);
  378. if(empty($array)) return [false, '导入数据不能为空'];
  379. //第一次表格数据校验 非空 已经过滤数据
  380. $array_clean = [];
  381. $search = "";
  382. foreach ($array as $key => $value){
  383. $rowData = array_filter($value);
  384. if (empty($rowData)) {
  385. unset($array[$key]);
  386. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2])) {
  387. return [false, '带*号的字段项必填'];
  388. }else{
  389. foreach ($value as $k => $v){
  390. $value[$k] = trim($v);
  391. }
  392. $t = $value[0] . $value[1];
  393. if(in_array($t,$array_clean)) return [false, '产品名称与编码不能重复'];
  394. $array_clean[] = $t;
  395. $array[$key] = $value;
  396. $search .= "(title = '".$value[0]."' and code ='".$value[1]."') or";
  397. }
  398. }unset($array_clean);
  399. if(empty($array)) return [false, '导入数据不能为空'];
  400. $search = rtrim($search,' or');
  401. $product = Product::whereRaw($search)
  402. ->where('del_time',0)
  403. ->select('code','title')
  404. ->get()->toArray();
  405. $product_array = [];
  406. foreach ($product as $value){
  407. $product_array[] = $value['title'] . $value['code'];
  408. }
  409. //默认进来 自身顶级公司
  410. $top_depart_id = $user['depart_top'][0] ?? [];
  411. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  412. $category_list = ProductCategory::where('del_time',0)
  413. ->where('top_depart_id',$top_depart_id)
  414. ->get()->toArray();
  415. $basic = (new BasicTypeService())->getMyBasicList($user, 20);
  416. $basic = array_column($basic,'id','title');
  417. $category = ProductCategory::whereIn('title',array_unique(array_column($array,'2')))
  418. ->where('del_time',0)
  419. ->where('top_depart_id',$top_depart_id)
  420. ->pluck('id','title')
  421. ->toArray();
  422. $category_parent = array_unique(array_column($category,'parent'));
  423. $time = time();
  424. $table_head = $this->product([],$user);
  425. $heads = $table_head[1];
  426. $tmp = array_column($heads,'key');
  427. $tmp = array_fill_keys($tmp, '');
  428. $tmp['product_category'] = '';
  429. $tmp['depart_id'] = $head;
  430. $tmp['top_depart_id'] = $head;
  431. $tmp['crt_id'] = $user['id'];
  432. $tmp['crt_time'] = $time;
  433. $tmp['upd_time'] = $time;
  434. $tmp['crt_id'] = $user['id'];
  435. $upload = array_flip($upload);
  436. $map = [];
  437. foreach ($heads as $value){
  438. if(strpos($value['key'], 'table_id.') !== false && isset($upload[$value['value']])){
  439. $map[$value['key']] = [
  440. 'col' => $upload[$value['value']],
  441. 'name' => $value['value']
  442. ];
  443. }
  444. }
  445. $array = array_values($array);
  446. $insert = $insert2 = [];
  447. foreach ($array as $key => $value){
  448. $pro_str = $value['0'] . $value['1'];
  449. if(in_array($pro_str, $product_array)) return [false, '产品名称、编码:' . $value['0']. '、' . $value['1'] . '已存在'];
  450. $tmp['title'] = $value['0'];
  451. $tmp['code'] = $value['1'];
  452. if(empty($category[$value['2']])) return [false,'产品分类:' . $value['2'] . '不存在'];
  453. $tmp['product_category_id'] = $category[$value['2']];
  454. if(in_array($tmp['product_category_id'],$category_parent)) return [false,'产品分类:' . $value['2'] . '下存在子分类,请将产品建与最底层分类下'];
  455. $parentsId = $this->findParentIds($tmp['product_category_id'], $category_list);
  456. array_unshift($parentsId, $tmp['product_category_id']);
  457. $result = array_reverse($parentsId);
  458. $tmp['product_category'] = json_encode($result);
  459. $tmp['size'] = $value['3'];
  460. if($value['4']){
  461. if(empty($basic[$value['4']])) return [false, '单位:' . $value['4'] . '不存在'];
  462. $tmp['unit'] = $basic[$value['4']];
  463. }
  464. $tmp['bar_code'] = $value['5'];
  465. $tmp['cost'] = $value['6'];
  466. $tmp['retail_price'] = $value['7'];
  467. foreach ($map as $m => $v){
  468. if($value[$v['col']]){
  469. if(! is_numeric($value[$v['col']])) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  470. $formattedNumber = number_format($value[$v['col']], 2, '.', '');
  471. if($formattedNumber != $value[$v['col']]) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  472. }
  473. $tmp[$m] = $value[$v['col']];
  474. }
  475. foreach ($tmp as $k => $v){
  476. if(strpos($k, 'table_id.') !== false){
  477. $tmp2 = [];
  478. $k_n = str_replace('table_id.', "", $k);
  479. $tmp2['basic_type_id'] = $k_n;
  480. $tmp2['price'] = $v;
  481. $tmp2['crt_time'] = $time;
  482. $tmp2['upd_time'] = $time;
  483. $insert2[$key][] = $tmp2;
  484. unset($tmp[$k]);
  485. }
  486. }
  487. $insert[] = $tmp;
  488. }
  489. try{
  490. DB::beginTransaction();
  491. if(! empty($insert)) Product::insert($insert);
  492. if(! empty($insert2)){
  493. $insert_detail = [];
  494. $last_insert_id = Product::where('crt_time',$time)
  495. ->select('id')
  496. ->get()->toArray();
  497. $last_insert_id = array_column($last_insert_id,'id');
  498. foreach ($last_insert_id as $key => $value){
  499. if(isset($insert2[$key])) {
  500. foreach ($insert2[$key] as $val){
  501. $val['product_id'] = $value;
  502. $insert_detail[] = $val;
  503. }
  504. }
  505. }
  506. ProductPriceDetail::insert($insert_detail);
  507. }
  508. DB::commit();
  509. }catch (\Exception $e){
  510. DB::rollBack();
  511. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  512. }
  513. return [true, ''];
  514. }
  515. public function salesOnlineImport($array, $user){
  516. $head = $user['head']['id'] ?? 0;
  517. // 去除表头
  518. unset($array[0]);
  519. if(empty($array)) return [false, '导入数据不能为空'];
  520. $basic = (new BasicTypeService())->getMyBasicList($user, [18,23,24,29]);
  521. $basic_list = [];
  522. foreach ($basic as $value){
  523. if($value['type'] == 18){
  524. $basic_list[9][$value['title']] = $value['id'];
  525. }elseif ($value['type'] == 23){
  526. $basic_list[10][$value['title']] = $value['id'];
  527. }elseif ($value['type'] == 24){
  528. $basic_list[3][$value['title']] = $value['id'];
  529. }elseif ($value['type'] == 29){
  530. $basic_list[2][$value['title']] = $value['id'];
  531. }
  532. }
  533. $search = "";
  534. $customer = [];
  535. foreach ($array as $key => $value){
  536. $rowData = array_filter($value);
  537. if (empty($rowData)) {
  538. unset($array[$key]);
  539. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || $value[6] === null) {
  540. return [false, '带*号的字段项必填'];
  541. }else{
  542. foreach ($value as $k => $v){
  543. $value[$k] = trim($v);
  544. }
  545. if(! isset($basic_list[2][$value[2]])) return [false, '客户简称:' . $value[2] .'不存在'];
  546. $value[2] = $basic_list[2][$value[2]];
  547. if(! isset($basic_list[3][$value[3]])) return [false, '店铺(平台类型):' . $value[3] .'不存在'];
  548. $value[3] = $basic_list[3][$value[3]];
  549. if(! is_numeric($value[5])) return [false, '货品数量请填写正确的数值'];
  550. if(! is_numeric($value[6])) return [false, '产品合同金额请填写正确的数值'];
  551. if(! empty($value[8]) && ! is_numeric($value[8])) return [false, '订单优惠金额请填写正确的数值'];
  552. if(! empty($value[9])){
  553. if(! isset($basic_list[9][$value[9]])) return [false, '安装方式:' . $value[9] .'不存在'];
  554. $value[9] = $basic_list[9][$value[9]];
  555. }
  556. if(! empty($value[10])){
  557. if(! isset($basic_list[10][$value[10]])) return [false, '安装地点:' . $value[10] .'不存在'];
  558. $value[10] = $basic_list[10][$value[10]];
  559. }
  560. if(! isset(SalesOrder::$order_type_name[$value[1]])) return [false, '产品类型填写错误'];
  561. $value[1] = SalesOrder::$order_type_name[$value[1]];
  562. if(! empty($value[11])) {
  563. list($status,$msg) = $this->changeAndReturnDate($value[11]);
  564. if(! $status) return [false,"施工日期请填写正确的日期格式,例如2023-05-01"];
  565. $value[11] = $msg;
  566. }
  567. if(! empty($value[12])) {
  568. list($status,$msg) = $this->changeAndReturnDate($value[12]);
  569. if(! $status) return [false,"交车日期请填写正确的日期格式,例如2023-05-01"];
  570. $value[12] = $msg;
  571. }
  572. $array[$key] = $value;
  573. $search .= "(code = '".$value[4]."') or";
  574. if(! empty($value[7]) && ! in_array($value[7], $customer)) $customer[] = $value[7];
  575. }
  576. }
  577. if(empty($array)) return [false, '导入数据不能为空'];
  578. $search = rtrim($search,' or');
  579. $model = Product::ProductClear($user,[]);
  580. $product = $model->whereRaw($search)
  581. ->where('del_time',0)
  582. ->select('title','id','code','cost','retail_price')
  583. ->get()->toArray();
  584. // $pro = (new ProductService())->productList(['product_id' => array_column($product,'id'), 'type' => 2],$user);
  585. $product_map = [];
  586. foreach ($product as $value){
  587. $product_map[$value['code']] = [
  588. 'id' => $value['id'],
  589. 'cost' => $value['cost'],
  590. 'retail_price' => $value['retail_price'],
  591. ];
  592. }
  593. $time = time();
  594. $model = Customer::Clear($user,[]);
  595. $customer_map = $model->where('del_time',0)
  596. ->whereIn('title',$customer)
  597. ->pluck('id','title')
  598. ->toArray();
  599. $customer_info = CustomerInfo::where('del_time',0)
  600. ->whereIn('customer_id',array_values($customer_map))
  601. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  602. ->select('customer_id','type','contact_info','data_id')
  603. ->orderBy('id','asc')
  604. ->get()->toArray();
  605. $customer_contact = $customer_man = [];
  606. foreach ($customer_info as $value){
  607. if($value['type'] == CustomerInfo::type_one && ! isset($customer_contact[$value['customer_id']])){
  608. $customer_contact[$value['customer_id']] = $value['contact_info'];
  609. }elseif ($value['type'] == CustomerInfo::type_two){
  610. $customer_man[$value['customer_id']][] = [
  611. 'type' => SalesOrderInfo::type_two,
  612. 'data_id' => $value['data_id'],
  613. 'crt_time' => $time,
  614. ];
  615. }
  616. }
  617. $tmp = [
  618. 'model_type' => SalesOrder::Model_type_four,
  619. 'sales_order_type' => 0,
  620. 'order_number' => '',
  621. 'customer_short_name' => '',
  622. 'plat_type' => '',
  623. 'plat_order' => '',
  624. 'sign_time' => $time,
  625. 'product_total' => 0,
  626. 'contract_fee' => 0,
  627. 'discount_fee' => 0,
  628. 'cdefine29' => '',//分社施工
  629. 'cdefine32' => '',//达人昵称
  630. 'cdefine30' => '',//业务员
  631. 'rate' => 100,
  632. 'depart_id' => $head,
  633. 'top_depart_id' => $head,
  634. 'crt_id' => $user['id'],
  635. 'crt_time' => $time,
  636. 'upd_time' => $time,
  637. ];
  638. $tmp_detail = [
  639. 'sales_order_id' => 0,
  640. 'product_id' => 0,
  641. 'cost' => 0,
  642. 'retail_price' => 0,
  643. 'basic_type_id' => 0,
  644. 'price' => 0,
  645. 'final_amount' => 0,
  646. 'number' => '',
  647. 'crt_time' => $time,
  648. ];
  649. $insert = $insert_detail = $insert_detail_man = [];
  650. $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
  651. foreach ($array as $value){
  652. $product_str = $value[4];
  653. if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[4]. ']' . '不存在'];
  654. $product_tmp = $product_map[$product_str] ?? [];
  655. $customer_tmp = 0;
  656. $customer_contact_tmp = "";
  657. $customer_man_tmp = [];
  658. if(! empty($value[7])){
  659. $customer_tmp = $customer_map[$value[7]] ?? 0;
  660. $customer_contact_tmp = $customer_contact[$customer_tmp] ?? "";
  661. $customer_man_tmp = $customer_man[$customer_tmp] ?? [];
  662. }
  663. $customer_man_tmp[] = [
  664. 'type' => SalesOrderInfo::type_one,
  665. 'data_id' => $user['id'],
  666. 'crt_time' => $time,
  667. ];
  668. $tmp['product_total'] = $tmp['contract_fee'] = 0;
  669. $keys = $value[0] . $value[1];
  670. $insert_detail_man[$keys] = $customer_man_tmp;
  671. if(! isset($insert[$keys])){
  672. $tmp['order_number'] = OrderNoService::createSalesOrderNumber($prefix);
  673. $tmp['sales_order_type'] = $value[1];
  674. $tmp['customer_short_name'] = $value[2];
  675. $tmp['customer_id'] = $customer_tmp;
  676. $tmp['customer_contact'] = $customer_contact_tmp;
  677. $tmp['plat_type'] = $value[3];
  678. $tmp['plat_order'] = $value[0];
  679. $tmp['discount_fee'] = $value[8] ?: 0;
  680. $tmp['cdefine29'] = $value[13] ?? "";//分社施工
  681. $tmp['cdefine32'] = $value[14] ?? "";//达人昵称
  682. $tmp['cdefine30'] = $value[15] ?? "";//业务员
  683. $tmp['product_total'] += $value[6];
  684. $tmp['contract_fee'] += $value[6] - $tmp['discount_fee'];
  685. $tmp['construction_time'] = $value[11] ?? 0;
  686. $tmp['handover_time'] = $value[12] ?? 0;
  687. $insert[$keys] = $tmp;
  688. }else{
  689. $insert[$keys]['product_total'] += $value[6];
  690. $insert[$keys]['contract_fee'] += $value[6];
  691. }
  692. $tmp_detail['product_id'] = $product_tmp['id'];
  693. $tmp_detail['cost'] = $product_tmp['cost'];
  694. $tmp_detail['retail_price'] = $product_tmp['retail_price'];
  695. $tmp_detail['price'] = $product_tmp['retail_price'];
  696. $tmp_detail['final_amount'] = $value[6];
  697. $tmp_detail['number'] = $value[5];
  698. $insert_detail[$keys][] = $tmp_detail;
  699. }
  700. $insert_detail = array_values($insert_detail);
  701. $insert_detail_man = array_values($insert_detail_man);
  702. try{
  703. DB::beginTransaction();
  704. if(! empty($insert)) SalesOrder::insert($insert);
  705. $last_insert_id = SalesOrder::where('crt_time',$time)
  706. ->select('id')
  707. ->get()->toArray();
  708. $last_insert_id = array_column($last_insert_id,'id');
  709. if(! empty($insert_detail)){
  710. $insert2 = [];
  711. foreach ($last_insert_id as $key => $value){
  712. if(isset($insert_detail[$key])) {
  713. foreach ($insert_detail[$key] as $val){
  714. $val['sales_order_id'] = $value;
  715. $insert2[] = $val;
  716. }
  717. }
  718. }
  719. SalesOrderProductInfo::insert($insert2);
  720. }
  721. if(! empty($insert_detail_man)){
  722. $insert3 = [];
  723. foreach ($last_insert_id as $key => $value){
  724. if(isset($insert_detail_man[$key])) {
  725. foreach ($insert_detail_man[$key] as $val){
  726. $val['sales_order_id'] = $value;
  727. $insert3[] = $val;
  728. }
  729. }
  730. }
  731. SalesOrderInfo::insert($insert3);
  732. }
  733. DB::commit();
  734. }catch (\Exception $e){
  735. DB::rollBack();
  736. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  737. }
  738. return [true, ''];
  739. }
  740. }