ImportService.php 34 KB

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