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. $model = BasicType::TopClear($user,$data);
  140. $result = $model->whereRaw('type = 22 And del_time = 0')->get()->toArray();
  141. if(! empty($result)){
  142. foreach ($result as $value){
  143. $config_array[] = [
  144. 'key' => 'table_id.' . $value['id'],
  145. 'value' => $value['title'],
  146. ];
  147. }
  148. }
  149. //生成下载文件
  150. $filename = "产品模板_" . time() . '.' . 'xlsx';
  151. return [true, $config_array,$filename];
  152. }
  153. private function salesOnline($data,$user){
  154. //生成下载文件
  155. $filename = "线上订单模板_" . time() . '.' . 'xlsx';
  156. //获取配置文件
  157. $config = "excel.salesOnlineTable";
  158. $config_array = config($config) ?? [];
  159. if(empty($config_array)) return [false, '配置文件不存在',''];
  160. return [true, $config_array,$filename];
  161. }
  162. //导入入口
  163. public function importAll($data,$user){
  164. if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
  165. if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
  166. if(empty($data['file'])) return [false,'导入文件不能为空'];
  167. $import = new ImportAll();
  168. //设置导入人id
  169. $import->setCrt($user['id']);
  170. $import->setUser($user);
  171. $import->setType($data['type']);
  172. //导入
  173. \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
  174. if($import->getMsg()) return [false, $import->getMsg()];
  175. return [true, ''];
  176. }
  177. public function customerImport($array, $user){
  178. $head = $user['head']['id'] ?? 0;
  179. // 去除表头
  180. unset($array[0]);
  181. if(empty($array)) return [false, '导入数据不能为空'];
  182. //第一次表格数据校验 非空 已经过滤数据
  183. $array_clean = $contact_info = [];
  184. foreach ($array as $key => $value){
  185. $rowData = array_filter($value);
  186. if (empty($rowData)) {
  187. unset($array[$key]);
  188. } elseif(empty($value[0]) || empty($value[1])) {
  189. return [false, '带*号的字段项必填'];
  190. }else{
  191. foreach ($value as $k => $v){
  192. $value[$k] = trim($v);
  193. }
  194. if(! isset(Customer::dk[$value[0]])) return [false, '客户模板填写错误'];
  195. $value[0] = Customer::dk[$value[0]];
  196. if(in_array($value[1],$array_clean)) return [false, '客户名称不能重复'];
  197. $array_clean[] = $value[1];
  198. $array[$key] = $value;
  199. if(! empty($value[13])){
  200. if(in_array($value[13],$contact_info)) return [false, '联系方式内容不能重复'];
  201. $contact_info[] = $value[13];
  202. }
  203. }
  204. }unset($array_clean);
  205. if(empty($array)) return [false, '导入数据不能为空'];
  206. //客户
  207. $model = Customer::Clear($user,[]);
  208. $customer = $model->where('del_time',0)
  209. ->whereIn('title',array_unique(array_column($array,'1')))
  210. ->pluck('id','title')
  211. ->toArray();
  212. $model = BasicType::TopClear($user,[]);
  213. $basic = $model->where('del_time',0)
  214. ->whereIn('type',[1,2,3,4,5,9,10])
  215. ->select('id','title','type')
  216. ->get()
  217. ->toArray();
  218. $basic_list = [];
  219. foreach ($basic as $value){
  220. if($value['type'] == 1){
  221. $basic_list[2][$value['title']] = $value['id'];
  222. }elseif ($value['type'] == 2){
  223. $basic_list[3][$value['title']] = $value['id'];
  224. }elseif ($value['type'] == 3){
  225. $basic_list[4][$value['title']] = $value['id'];
  226. }elseif ($value['type'] == 4){
  227. $basic_list[12][$value['title']] = $value['id'];
  228. }elseif ($value['type'] == 5){
  229. $basic_list[7][$value['title']] = $value['id'];
  230. }elseif ($value['type'] == 9){
  231. $basic_list[8][$value['title']] = $value['id'];
  232. }elseif ($value['type'] == 10){
  233. $basic_list[9][$value['title']] = $value['id'];
  234. }
  235. }
  236. $model = Product::ProductClear($user,[]);
  237. $product = $model->where('del_time',0)
  238. ->whereIn('title',array_unique(array_column($array,'6')))
  239. ->pluck('id','title')
  240. ->toArray();
  241. $emp = Employee::where('del_time',0)
  242. ->whereIn('number',array_unique(array_column($array,'14')))
  243. ->pluck('id','number')
  244. ->toArray();
  245. $top_depart_id = $user['depart_top'][0] ?? [];
  246. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  247. $contact_info_array = CustomerInfo::from('customer_info as a')
  248. ->join('customer as b','b.id','a.customer_id')
  249. ->where('a.del_time',0)
  250. ->where('b.del_time',0)
  251. ->where('b.top_depart_id',$top_depart_id)
  252. ->whereIn('a.contact_info', $contact_info)
  253. ->select('a.contact_info')->get()->toArray();
  254. $contact_info_array = array_column($contact_info_array,'contact_info');
  255. $time = time();
  256. $insert = [];
  257. $insert_detail = $insert_detail2 = [];
  258. foreach ($array as $value){
  259. $tmp = [
  260. 'model_type' => '',
  261. 'title' => '',
  262. 'customer_intention' => '',
  263. 'customer_from' => '',
  264. 'customer_type' => '',
  265. 'car_type' => '',
  266. 'consulting_product' => '',
  267. 'intention_product' => '',
  268. 'progress_stage' => '',
  269. 'state_type' => '',
  270. 'address2' => '',
  271. 'mark' => '',
  272. 'depart_id' => $head,
  273. 'top_depart_id' => $head,
  274. 'crt_id' => $user['id'],
  275. 'crt_time' => $time,
  276. 'upd_time' => $time,
  277. ];
  278. $tmp['model_type'] = $value['0'];
  279. $tmp['consulting_product'] = $value['5'];
  280. $tmp['mark'] = $value['10'];
  281. $tmp['address2'] = $value['11'];
  282. if(! empty($customer[$value['1']])) return [false, '客户:' . $value['1'] . '已存在'];
  283. $tmp['title'] = $value['1'];
  284. if($value['2']){
  285. if(empty($basic_list[2][$value['2']])) return [false, '客户意向度:' . $value['2'] . '不存在'];
  286. $tmp['customer_intention'] = $basic_list[2][$value['2']];
  287. }
  288. if($value['3']){
  289. if(empty($basic_list[3][$value['3']])) return [false, '客户来源:' . $value['3'] . '不存在'];
  290. $tmp['customer_from'] = $basic_list[3][$value['3']];
  291. }
  292. if($value['4']){
  293. if(empty($basic_list[4][$value['4']])) return [false, '客户类别:' . $value['4'] . '不存在'];
  294. $tmp['customer_type'] = $basic_list[4][$value['4']];
  295. }
  296. if($value['6']){
  297. if(empty($product[$value['6']])) return [false, '意向产品:' . $value['6'] . '不存在'];
  298. $tmp['intention_product'] = $product[$value['6']];
  299. }
  300. if($value['7']){
  301. if(empty($basic_list[7][$value['7']])) return [false, '进展阶段:' . $value['7'] . '不存在'];
  302. $tmp['progress_stage'] = $basic_list[7][$value['7']];
  303. }
  304. if($value['8']){
  305. if(empty($basic_list[8][$value['8']])) return [false, '状态:' . $value['8'] . '不存在'];
  306. $tmp['state_type'] = $basic_list[8][$value['8']];
  307. }
  308. if($value['9']){
  309. if(empty($basic_list[9][$value['9']])) return [false, '车型:' . $value['9'] . '不存在'];
  310. $tmp['car_type'] = $basic_list[9][$value['9']];
  311. }
  312. $contact_id = 0;
  313. if($value['12']){
  314. if(empty($basic_list[12][$value['12']])) return [false, '联系方式类型:' . $value['12'] . '不存在'];
  315. $contact_id = $basic_list[12][$value['12']];
  316. }
  317. if($value['13'] && in_array($value['13'],$contact_info_array)) return [false, '联系方式内容:' . $value['13'] . '已存在'];
  318. $man = 0;
  319. if($value['14']){
  320. if(empty($emp[$value['14']])) return [false, '负责人:' . $value['14'] . '不存在'];
  321. $man = $emp[$value['14']];
  322. }
  323. // if($value['12']){
  324. // if(empty($emp[$value['12']])) return [false, '协同人:' . $value['12'] . '不存在'];
  325. // $tmp['emp_two'] = $emp[$value['12']];
  326. // }
  327. $insert[] = $tmp;
  328. $insert_detail[] = [
  329. 'customer_id' => 0,
  330. 'contact_type' => $contact_id,
  331. 'contact_info' => $value['13'],
  332. 'crt_time' => $time,
  333. 'type' => CustomerInfo::type_one
  334. ];
  335. $insert_detail2[] = [
  336. 'customer_id' => 0,
  337. 'data_id' => $man,
  338. 'crt_time' => $time,
  339. 'type' => CustomerInfo::type_two
  340. ];
  341. }
  342. try{
  343. DB::beginTransaction();
  344. if(! empty($insert)) Customer::insert($insert);
  345. //获取上一次所有id
  346. $last_insert_id = Customer::where('crt_time',$time)
  347. ->where('crt_time',$time)
  348. ->where('depart_id',$head)
  349. ->where('top_depart_id',$head)
  350. ->where('crt_id',$user['id'])
  351. ->select('id')->get()->toArray();
  352. $last_insert_id = array_column($last_insert_id,'id');
  353. //组织数据 写入与主表的关联id
  354. $insert_detail_1 = [];
  355. foreach ($insert_detail as $key => $value){
  356. if(empty($value['contact_type']) && empty($value['contact_info'])) continue;
  357. $value['customer_id'] = $last_insert_id[$key];
  358. $insert_detail_1[] = $value;
  359. }unset($insert_detail);
  360. $insert_detail_2 = [];
  361. foreach ($insert_detail2 as $key => $value){
  362. if(empty($value['data_id'])) continue;
  363. $value['customer_id'] = $last_insert_id[$key];
  364. $insert_detail_2[] = $value;
  365. }unset($insert_detail2);
  366. if(! empty($insert_detail_1)) CustomerInfo::insert($insert_detail_1);
  367. if(! empty($insert_detail_2)) CustomerInfo::insert($insert_detail_2);
  368. DB::commit();
  369. }catch (\Exception $e){
  370. DB::rollBack();
  371. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  372. }
  373. return [true, ''];
  374. }
  375. public function productImport($array, $user){
  376. $head = $user['head']['id'] ?? 0;
  377. // 去除表头
  378. $upload = $array[0];
  379. unset($array[0]);
  380. if(empty($array)) return [false, '导入数据不能为空'];
  381. //第一次表格数据校验 非空 已经过滤数据
  382. $array_clean = [];
  383. $search = "";
  384. foreach ($array as $key => $value){
  385. $rowData = array_filter($value);
  386. if (empty($rowData)) {
  387. unset($array[$key]);
  388. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2])) {
  389. return [false, '带*号的字段项必填'];
  390. }else{
  391. foreach ($value as $k => $v){
  392. $value[$k] = trim($v);
  393. }
  394. $t = $value[0] . $value[1];
  395. if(in_array($t,$array_clean)) return [false, '产品名称与编码不能重复'];
  396. $array_clean[] = $t;
  397. $array[$key] = $value;
  398. $search .= "(title = '".$value[0]."' and code ='".$value[1]."') or";
  399. }
  400. }unset($array_clean);
  401. if(empty($array)) return [false, '导入数据不能为空'];
  402. $search = rtrim($search,' or');
  403. $product = Product::whereRaw($search)
  404. ->where('del_time',0)
  405. ->select('code','title')
  406. ->get()->toArray();
  407. $product_array = [];
  408. foreach ($product as $value){
  409. $product_array[] = $value['title'] . $value['code'];
  410. }
  411. //默认进来 自身顶级公司
  412. $top_depart_id = $user['depart_top'][0] ?? [];
  413. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  414. $category_list = ProductCategory::where('del_time',0)
  415. ->where('top_depart_id',$top_depart_id)
  416. ->get()->toArray();
  417. $model = BasicType::TopClear($user,[]);
  418. $basic = $model->where('del_time',0)
  419. ->where('type',20)
  420. ->pluck('id','title')
  421. ->toArray();
  422. $category = ProductCategory::whereIn('title',array_unique(array_column($array,'2')))
  423. ->where('del_time',0)
  424. ->where('top_depart_id',$top_depart_id)
  425. ->pluck('id','title')
  426. ->toArray();
  427. $category_parent = array_unique(array_column($category,'parent'));
  428. $time = time();
  429. $table_head = $this->product([],$user);
  430. $heads = $table_head[1];
  431. $tmp = array_column($heads,'key');
  432. $tmp = array_fill_keys($tmp, '');
  433. $tmp['product_category'] = '';
  434. $tmp['depart_id'] = $head;
  435. $tmp['top_depart_id'] = $head;
  436. $tmp['crt_id'] = $user['id'];
  437. $tmp['crt_time'] = $time;
  438. $tmp['upd_time'] = $time;
  439. $tmp['crt_id'] = $user['id'];
  440. $upload = array_flip($upload);
  441. $map = [];
  442. foreach ($heads as $value){
  443. if(strpos($value['key'], 'table_id.') !== false && isset($upload[$value['value']])){
  444. $map[$value['key']] = [
  445. 'col' => $upload[$value['value']],
  446. 'name' => $value['value']
  447. ];
  448. }
  449. }
  450. $array = array_values($array);
  451. $insert = $insert2 = [];
  452. foreach ($array as $key => $value){
  453. $pro_str = $value['0'] . $value['1'];
  454. if(in_array($pro_str, $product_array)) return [false, '产品名称、编码:' . $value['0']. '、' . $value['1'] . '已存在'];
  455. $tmp['title'] = $value['0'];
  456. $tmp['code'] = $value['1'];
  457. if(empty($category[$value['2']])) return [false,'产品分类:' . $value['2'] . '不存在'];
  458. $tmp['product_category_id'] = $category[$value['2']];
  459. if(in_array($tmp['product_category_id'],$category_parent)) return [false,'产品分类:' . $value['2'] . '下存在子分类,请将产品建与最底层分类下'];
  460. $parentsId = $this->findParentIds($tmp['product_category_id'], $category_list);
  461. array_unshift($parentsId, $tmp['product_category_id']);
  462. $result = array_reverse($parentsId);
  463. $tmp['product_category'] = json_encode($result);
  464. $tmp['size'] = $value['3'];
  465. if($value['4']){
  466. if(empty($basic[$value['4']])) return [false, '单位:' . $value['4'] . '不存在'];
  467. $tmp['unit'] = $basic[$value['4']];
  468. }
  469. $tmp['bar_code'] = $value['5'];
  470. $tmp['cost'] = $value['6'];
  471. $tmp['retail_price'] = $value['7'];
  472. foreach ($map as $m => $v){
  473. if($value[$v['col']]){
  474. if(! is_numeric($value[$v['col']])) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  475. $formattedNumber = number_format($value[$v['col']], 2, '.', '');
  476. if($formattedNumber != $value[$v['col']]) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  477. }
  478. $tmp[$m] = $value[$v['col']];
  479. }
  480. foreach ($tmp as $k => $v){
  481. if(strpos($k, 'table_id.') !== false){
  482. $tmp2 = [];
  483. $k_n = str_replace('table_id.', "", $k);
  484. $tmp2['basic_type_id'] = $k_n;
  485. $tmp2['price'] = $v;
  486. $tmp2['crt_time'] = $time;
  487. $tmp2['upd_time'] = $time;
  488. $insert2[$key][] = $tmp2;
  489. unset($tmp[$k]);
  490. }
  491. }
  492. $insert[] = $tmp;
  493. }
  494. try{
  495. DB::beginTransaction();
  496. if(! empty($insert)) Product::insert($insert);
  497. if(! empty($insert2)){
  498. $insert_detail = [];
  499. $last_insert_id = Product::where('crt_time',$time)
  500. ->select('id')
  501. ->get()->toArray();
  502. $last_insert_id = array_column($last_insert_id,'id');
  503. foreach ($last_insert_id as $key => $value){
  504. if(isset($insert2[$key])) {
  505. foreach ($insert2[$key] as $val){
  506. $val['product_id'] = $value;
  507. $insert_detail[] = $val;
  508. }
  509. }
  510. }
  511. ProductPriceDetail::insert($insert_detail);
  512. }
  513. DB::commit();
  514. }catch (\Exception $e){
  515. DB::rollBack();
  516. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  517. }
  518. return [true, ''];
  519. }
  520. public function salesOnlineImport($array, $user){
  521. $head = $user['head']['id'] ?? 0;
  522. // 去除表头
  523. unset($array[0]);
  524. if(empty($array)) return [false, '导入数据不能为空'];
  525. $model = BasicType::TopClear($user,[]);
  526. $basic = $model->where('del_time',0)
  527. ->whereIn('type',[18,23,24,29])
  528. ->select('id','title','type')
  529. ->get()
  530. ->toArray();
  531. $basic_list = [];
  532. foreach ($basic as $value){
  533. if($value['type'] == 18){
  534. $basic_list[10][$value['title']] = $value['id'];
  535. }elseif ($value['type'] == 23){
  536. $basic_list[11][$value['title']] = $value['id'];
  537. }elseif ($value['type'] == 24){
  538. $basic_list[3][$value['title']] = $value['id'];
  539. }elseif ($value['type'] == 29){
  540. $basic_list[2][$value['title']] = $value['id'];
  541. }
  542. }
  543. $search = "";
  544. $customer = [];
  545. foreach ($array as $key => $value){
  546. $rowData = array_filter($value);
  547. if (empty($rowData)) {
  548. unset($array[$key]);
  549. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || empty($value[6]) || empty($value[7])) {
  550. return [false, '带*号的字段项必填'];
  551. }else{
  552. foreach ($value as $k => $v){
  553. $value[$k] = trim($v);
  554. }
  555. if(! isset($basic_list[2][$value[2]])) return [false, '客户简称:' . $value[2] .'不存在'];
  556. if(! isset($basic_list[3][$value[3]])) return [false, '店铺(平台类型):' . $value[3] .'不存在'];
  557. $value[3] = $basic_list[3][$value[3]];
  558. if(! empty($value[10])){
  559. if(! isset($basic_list[10][$value[10]])) return [false, '安装方式:' . $value[3] .'不存在'];
  560. $value[10] = $basic_list[10][$value[10]];
  561. }
  562. if(! empty($value[11])){
  563. if(! isset($basic_list[11][$value[11]])) return [false, '安装地点:' . $value[3] .'不存在'];
  564. $value[11] = $basic_list[11][$value[11]];
  565. }
  566. if(! isset(SalesOrder::$order_type_name[$value[1]])) return [false, '产品类型填写错误'];
  567. $value[1] = SalesOrder::$order_type_name[$value[1]];
  568. if(! empty($value[12])) {
  569. list($status,$msg) = $this->changeAndReturnDate($value[12]);
  570. if(! $status) return [false,"施工日期请填写正确的日期格式,例如2023-05-01"];
  571. $value[12] = $msg;
  572. }
  573. if(! empty($value[13])) {
  574. list($status,$msg) = $this->changeAndReturnDate($value[13]);
  575. if(! $status) return [false,"交车日期请填写正确的日期格式,例如2023-05-01"];
  576. $value[13] = $msg;
  577. }
  578. $array[$key] = $value;
  579. $search .= "(code = '".$value[4]."' and title ='".$value[5]."') or";
  580. if(! empty($value[8]) && ! in_array($value[8], $customer)) $customer[] = $value[8];
  581. }
  582. }
  583. if(empty($array)) return [false, '导入数据不能为空'];
  584. $search = rtrim($search,' or');
  585. $model = Product::ProductClear($user,[]);
  586. $product = $model->whereRaw($search)
  587. ->where('del_time',0)
  588. ->select('title','id','code','cost','retail_price')
  589. ->get()->toArray();
  590. // $pro = (new ProductService())->productList(['product_id' => array_column($product,'id'), 'type' => 2],$user);
  591. $product_map = [];
  592. foreach ($product as $value){
  593. $product_map[$value['code'] . $value['title']] = [
  594. 'id' => $value['id'],
  595. 'cost' => $value['cost'],
  596. 'retail_price' => $value['retail_price'],
  597. ];
  598. }
  599. $time = time();
  600. $model = Customer::Clear($user,[]);
  601. $customer_map = $model->where('del_time',0)
  602. ->whereIn('title',$customer)
  603. ->pluck('id','title')
  604. ->toArray();
  605. $customer_info = CustomerInfo::where('del_time',0)
  606. ->whereIn('customer_id',array_values($customer_map))
  607. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  608. ->select('customer_id','type','contact_info','data_id')
  609. ->orderBy('id','asc')
  610. ->get()->toArray();
  611. $customer_contact = $customer_man = [];
  612. foreach ($customer_info as $value){
  613. if($value['type'] == CustomerInfo::type_one && ! isset($customer_contact[$value['customer_id']])){
  614. $customer_contact[$value['customer_id']] = $value['contact_info'];
  615. }elseif ($value['type'] == CustomerInfo::type_two){
  616. $customer_man[$value['customer_id']][] = [
  617. 'type' => SalesOrderInfo::type_two,
  618. 'data_id' => $value['data_id'],
  619. 'crt_time' => $time,
  620. ];
  621. }
  622. }
  623. $tmp = [
  624. 'model_type' => SalesOrder::Model_type_four,
  625. 'sales_order_type' => 0,
  626. 'order_number' => '',
  627. 'customer_short_name' => '',
  628. 'plat_type' => '',
  629. 'plat_order' => '',
  630. 'sign_time' => $time,
  631. 'product_total' => 0,
  632. 'contract_fee' => 0,
  633. 'discount_fee' => 0,
  634. 'rate' => 100,
  635. 'depart_id' => $head,
  636. 'top_depart_id' => $head,
  637. 'crt_id' => $user['id'],
  638. 'crt_time' => $time,
  639. 'upd_time' => $time,
  640. ];
  641. $tmp_detail = [
  642. 'sales_order_id' => 0,
  643. 'product_id' => 0,
  644. 'cost' => 0,
  645. 'retail_price' => 0,
  646. 'basic_type_id' => 0,
  647. 'price' => 0,
  648. 'final_amount' => 0,
  649. 'number' => '',
  650. 'crt_time' => $time,
  651. ];
  652. $insert = $insert_detail = $insert_detail_man = [];
  653. $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
  654. foreach ($array as $value){
  655. $product_str = $value[4] . $value[5];
  656. if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[4]. ']' . '[' . $value[5]. ']' . '不存在'];
  657. $product_tmp = $product_map[$product_str] ?? [];
  658. $customer_tmp = 0;
  659. $customer_contact_tmp = "";
  660. $customer_man_tmp = [];
  661. if(! empty($value[8])){
  662. $customer_tmp = $customer_map[$value[8]] ?? 0;
  663. $customer_contact_tmp = $customer_contact[$customer_tmp] ?? "";
  664. $customer_man_tmp = $customer_man[$customer_tmp] ?? [];
  665. }
  666. $customer_man_tmp[] = [
  667. 'type' => SalesOrderInfo::type_one,
  668. 'data_id' => $user['id'],
  669. 'crt_time' => $time,
  670. ];
  671. $tmp['product_total'] = $tmp['contract_fee'] = 0;
  672. $keys = $value[0] . $value[1];
  673. $insert_detail_man[$keys] = $customer_man_tmp;
  674. if(! isset($insert[$keys])){
  675. $tmp['order_number'] = OrderNoService::createSalesOrderNumber($prefix);
  676. $tmp['sales_order_type'] = $value[1];
  677. $tmp['customer_short_name'] = $value[2];
  678. $tmp['customer_id'] = $customer_tmp;
  679. $tmp['customer_contact'] = $customer_contact_tmp;
  680. $tmp['plat_type'] = $value[3];
  681. $tmp['plat_order'] = $value[0];
  682. $tmp['discount_fee'] = $value[9];
  683. $tmp['product_total'] += $value[7];
  684. $tmp['contract_fee'] += $value[7] - ($value[9]);
  685. $tmp['construction_time'] = $value[12] ?? 0;
  686. $tmp['handover_time'] = $value[13] ?? 0;
  687. $insert[$keys] = $tmp;
  688. }else{
  689. $insert[$keys]['product_total'] += $value[7];
  690. $insert[$keys]['contract_fee'] += $value[7];
  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[7];
  697. $tmp_detail['number'] = $value[6];
  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. }