ImportService.php 37 KB

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