ImportService.php 34 KB

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