ImportService.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. $product = Product::whereRaw($search)
  437. ->where('del_time',0)
  438. ->select('code','top_depart_id','id')
  439. ->get()->toArray();
  440. $product_array = [];
  441. foreach ($product as $value){
  442. $product_array[$value['code']] = [
  443. 'id' => $value['id'],
  444. 'top_depart_id' => $value['top_depart_id']
  445. ];
  446. }
  447. $category_list = ProductCategory::where('del_time',0)
  448. ->where('top_depart_id',$top_depart_id)
  449. ->get()->toArray();
  450. $basic = (new BasicTypeService())->getMyBasicList($user, 20);
  451. $basic = array_column($basic,'id','title');
  452. $category = ProductCategory::whereIn('title',array_unique(array_column($array,'2')))
  453. ->where('del_time',0)
  454. ->where('top_depart_id',$top_depart_id)
  455. ->pluck('id','title')
  456. ->toArray();
  457. $category_parent = array_unique(array_column($category,'parent'));
  458. $time = time();
  459. $table_head = $this->product([],$user);
  460. $heads = $table_head[1];
  461. $tmp = array_column($heads,'key');
  462. $tmp = array_fill_keys($tmp, '');
  463. $tmp['product_category'] = '';
  464. $tmp['upd_time'] = $time;
  465. $top_message = Depart::where('parent_id',0)
  466. ->pluck('title','id')
  467. ->toArray();
  468. $upload = array_flip($upload);
  469. $map = [];
  470. foreach ($heads as $value){
  471. if(strpos($value['key'], 'table_id.') !== false && isset($upload[$value['value']])){
  472. $map[$value['key']] = [
  473. 'col' => $upload[$value['value']],
  474. 'name' => $value['value']
  475. ];
  476. }
  477. }
  478. $array = array_values($array);
  479. $insert = $insert2 = $update = $update2 = [];
  480. foreach ($array as $value){
  481. if(isset($product_array[$value['1']])){
  482. $pro_tmp = $product_array[$value['1']] ?? [];
  483. if($pro_tmp['top_depart_id'] != $top_depart_id){
  484. $belong = $top_message[$pro_tmp['top_depart_id']] ?? "";
  485. $now = $top_message[$top_depart_id] ?? "";
  486. return [false, '产品编码:' . $value['1'] . '属于门店:' . $belong . ',当前门店:' . $now . ',不允许跨门店操作更新产品!'];
  487. }
  488. }
  489. $tmp['title'] = $value['0'];
  490. $tmp['code'] = $value['1'];
  491. if(empty($category[$value['2']])) return [false,'产品分类:' . $value['2'] . '不存在'];
  492. $tmp['product_category_id'] = $category[$value['2']];
  493. if(in_array($tmp['product_category_id'],$category_parent)) return [false,'产品分类:' . $value['2'] . '下存在子分类,请将产品建与最底层分类下'];
  494. $parentsId = $this->findParentIds($tmp['product_category_id'], $category_list);
  495. array_unshift($parentsId, $tmp['product_category_id']);
  496. $result = array_reverse($parentsId);
  497. $tmp['product_category'] = json_encode($result);
  498. $tmp['size'] = $value['3'];
  499. if($value['4']){
  500. if(empty($basic[$value['4']])) return [false, '单位:' . $value['4'] . '不存在'];
  501. $tmp['unit'] = $basic[$value['4']];
  502. }
  503. $tmp['bar_code'] = $value['5'];
  504. $tmp['cost'] = $value['6'];
  505. $tmp['retail_price'] = $value['7'];
  506. $tmp['product_attribute'] = $value['8'] ?? 0;
  507. foreach ($map as $m => $v){
  508. if($value[$v['col']]){
  509. if(! is_numeric($value[$v['col']])) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  510. $formattedNumber = number_format($value[$v['col']], 2, '.', '');
  511. if($formattedNumber != $value[$v['col']]) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  512. }
  513. $tmp[$m] = $value[$v['col']];
  514. }
  515. if(isset($product_array[$value['1']])){
  516. //更新
  517. $pro_tmp = $product_array[$value['1']] ?? [];
  518. $product_id = $pro_tmp['id'];
  519. //产品价格子表
  520. foreach ($tmp as $k => $v){
  521. if(strpos($k, 'table_id.') !== false){
  522. $tmp2 = [];
  523. $k_n = str_replace('table_id.', "", $k);
  524. $tmp2['product_id'] = $product_id;
  525. $tmp2['basic_type_id'] = $k_n;
  526. $tmp2['price'] = $v;
  527. $tmp2['crt_time'] = $time;
  528. $tmp2['upd_time'] = $time;
  529. $update2[] = $tmp2;
  530. unset($tmp[$k]);
  531. }
  532. }
  533. //产品主表
  534. $update[$product_id] = $tmp;
  535. }else{
  536. $tmp['depart_id'] = $depart_id;
  537. $tmp['top_depart_id'] = $top_depart_id;
  538. $tmp['crt_id'] = $user['id'];
  539. $tmp['crt_time'] = $time;
  540. //产品价格子表
  541. foreach ($tmp as $k => $v){
  542. if(strpos($k, 'table_id.') !== false){
  543. $tmp2 = [];
  544. $k_n = str_replace('table_id.', "", $k);
  545. $tmp2['basic_type_id'] = $k_n;
  546. $tmp2['price'] = $v;
  547. $tmp2['crt_time'] = $time;
  548. $tmp2['upd_time'] = $time;
  549. $insert2[$tmp['code']][] = $tmp2;
  550. unset($tmp[$k]);
  551. }
  552. }
  553. //产品主表
  554. $insert[$tmp['code']] = $tmp;
  555. }
  556. }
  557. try{
  558. DB::beginTransaction();
  559. //新增
  560. if(! empty($insert)){
  561. Product::insert($insert);
  562. if(! empty($insert2)){
  563. $insert_detail = [];
  564. $last_insert_id = Product::where('crt_time',$time)
  565. ->pluck('id','code')
  566. ->toArray();
  567. foreach ($insert2 as $code => $val){
  568. foreach ($val as $v2){
  569. $v2['product_id'] = $last_insert_id[$code] ?? 0;
  570. $insert_detail[] = $v2;
  571. }
  572. }
  573. ProductPriceDetail::insert($insert_detail);
  574. }
  575. }
  576. //编辑
  577. if(! empty($update)){
  578. foreach ($update as $p_id => $value){
  579. Product::where('id',$p_id)
  580. ->update($value);
  581. }
  582. if(! empty($update2)){
  583. $product_id_array = array_keys($update);
  584. ProductPriceDetail::whereIn('product_id',$product_id_array)
  585. ->update(['del_time' => $time]);
  586. ProductPriceDetail::insert($update2);
  587. }
  588. }
  589. DB::commit();
  590. }catch (\Exception $e){
  591. DB::rollBack();
  592. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  593. }
  594. return [true, ''];
  595. }
  596. public function salesOnlineImport($array, $user){
  597. $head = $user['head']['id'] ?? 0;
  598. // 去除表头
  599. unset($array[0]);
  600. if(empty($array)) return [false, '导入数据不能为空'];
  601. $basic = (new BasicTypeService())->getMyBasicList($user, [18,23,24,29]);
  602. $basic_list = [];
  603. foreach ($basic as $value){
  604. if($value['type'] == 18){
  605. $basic_list[9][$value['title']] = $value['id'];
  606. }elseif ($value['type'] == 23){
  607. $basic_list[10][$value['title']] = $value['id'];
  608. }elseif ($value['type'] == 24){
  609. $basic_list[3][$value['title']] = $value['id'];
  610. }elseif ($value['type'] == 29){
  611. $basic_list[2][$value['title']] = $value['id'];
  612. }
  613. }
  614. $search = "";
  615. $customer = [];
  616. foreach ($array as $key => $value){
  617. $rowData = array_filter($value);
  618. if (empty($rowData)) {
  619. unset($array[$key]);
  620. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || $value[6] === null) {
  621. return [false, '带*号的字段项必填'];
  622. }else{
  623. foreach ($value as $k => $v){
  624. $value[$k] = trim($v);
  625. }
  626. if(! isset($basic_list[2][$value[2]])) return [false, '客户简称:' . $value[2] .'不存在'];
  627. $value[2] = $basic_list[2][$value[2]];
  628. if(! isset($basic_list[3][$value[3]])) return [false, '店铺(平台类型):' . $value[3] .'不存在'];
  629. $value[3] = $basic_list[3][$value[3]];
  630. if(! is_numeric($value[5])) return [false, '货品数量请填写正确的数值'];
  631. if(! is_numeric($value[6])) return [false, '产品合同金额请填写正确的数值'];
  632. if(! empty($value[8]) && ! is_numeric($value[8])) return [false, '订单优惠金额请填写正确的数值'];
  633. if(! empty($value[9])){
  634. if(! isset($basic_list[9][$value[9]])) return [false, '安装方式:' . $value[9] .'不存在'];
  635. $value[9] = $basic_list[9][$value[9]];
  636. }
  637. if(! empty($value[10])){
  638. if(! isset($basic_list[10][$value[10]])) return [false, '安装地点:' . $value[10] .'不存在'];
  639. $value[10] = $basic_list[10][$value[10]];
  640. }
  641. if(! isset(SalesOrder::$order_type_name[$value[1]])) return [false, '产品类型填写错误'];
  642. $value[1] = SalesOrder::$order_type_name[$value[1]];
  643. if(! empty($value[11])) {
  644. list($status,$msg) = $this->changeAndReturnDate($value[11]);
  645. if(! $status) return [false,"施工日期请填写正确的日期格式,例如2023-05-01"];
  646. $value[11] = $msg;
  647. }
  648. if(! empty($value[12])) {
  649. list($status,$msg) = $this->changeAndReturnDate($value[12]);
  650. if(! $status) return [false,"交车日期请填写正确的日期格式,例如2023-05-01"];
  651. $value[12] = $msg;
  652. }
  653. $array[$key] = $value;
  654. $search .= "(code = '".$value[4]."') or";
  655. if(! empty($value[7]) && ! in_array($value[7], $customer)) $customer[] = $value[7];
  656. }
  657. }
  658. if(empty($array)) return [false, '导入数据不能为空'];
  659. $search = rtrim($search,' or');
  660. $model = Product::ProductClear($user,[]);
  661. $product = $model->whereRaw($search)
  662. ->where('del_time',0)
  663. ->select('title','id','code','cost','retail_price')
  664. ->get()->toArray();
  665. // $pro = (new ProductService())->productList(['product_id' => array_column($product,'id'), 'type' => 2],$user);
  666. $product_map = [];
  667. foreach ($product as $value){
  668. $product_map[$value['code']] = [
  669. 'id' => $value['id'],
  670. 'cost' => $value['cost'],
  671. 'retail_price' => $value['retail_price'],
  672. ];
  673. }
  674. $time = time();
  675. $model = Customer::Clear($user,[]);
  676. $customer_map = $model->where('del_time',0)
  677. ->whereIn('title',$customer)
  678. ->pluck('id','title')
  679. ->toArray();
  680. $customer_info = CustomerInfo::where('del_time',0)
  681. ->whereIn('customer_id',array_values($customer_map))
  682. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  683. ->select('customer_id','type','contact_info','data_id')
  684. ->orderBy('id','asc')
  685. ->get()->toArray();
  686. $customer_contact = $customer_man = [];
  687. foreach ($customer_info as $value){
  688. if($value['type'] == CustomerInfo::type_one && ! isset($customer_contact[$value['customer_id']])){
  689. $customer_contact[$value['customer_id']] = $value['contact_info'];
  690. }elseif ($value['type'] == CustomerInfo::type_two){
  691. $customer_man[$value['customer_id']][] = [
  692. 'type' => SalesOrderInfo::type_two,
  693. 'data_id' => $value['data_id'],
  694. 'crt_time' => $time,
  695. ];
  696. }
  697. }
  698. $tmp = [
  699. 'model_type' => SalesOrder::Model_type_four,
  700. 'sales_order_type' => 0,
  701. 'order_number' => '',
  702. 'customer_short_name' => '',
  703. 'plat_type' => '',
  704. 'plat_order' => '',
  705. 'sign_time' => $time,
  706. 'product_total' => 0,
  707. 'contract_fee' => 0,
  708. 'discount_fee' => 0,
  709. 'cdefine29' => '',//分社施工
  710. 'cdefine32' => '',//达人昵称
  711. 'cdefine30' => '',//业务员
  712. 'rate' => 100,
  713. 'depart_id' => $head,
  714. 'top_depart_id' => $head,
  715. 'crt_id' => $user['id'],
  716. 'crt_time' => $time,
  717. 'upd_time' => $time,
  718. ];
  719. $tmp_detail = [
  720. 'sales_order_id' => 0,
  721. 'product_id' => 0,
  722. 'cost' => 0,
  723. 'retail_price' => 0,
  724. 'basic_type_id' => 0,
  725. 'price' => 0,
  726. 'final_amount' => 0,
  727. 'number' => '',
  728. 'crt_time' => $time,
  729. ];
  730. $insert = $insert_detail = $insert_detail_man = [];
  731. $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
  732. foreach ($array as $value){
  733. $product_str = $value[4];
  734. if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[4]. ']' . '不存在'];
  735. $product_tmp = $product_map[$product_str] ?? [];
  736. $customer_tmp = 0;
  737. $customer_contact_tmp = "";
  738. $customer_man_tmp = [];
  739. if(! empty($value[7])){
  740. $customer_tmp = $customer_map[$value[7]] ?? 0;
  741. $customer_contact_tmp = $customer_contact[$customer_tmp] ?? "";
  742. $customer_man_tmp = $customer_man[$customer_tmp] ?? [];
  743. }
  744. $customer_man_tmp[] = [
  745. 'type' => SalesOrderInfo::type_one,
  746. 'data_id' => $user['id'],
  747. 'crt_time' => $time,
  748. ];
  749. $tmp['product_total'] = $tmp['contract_fee'] = 0;
  750. $keys = $value[0] . $value[1];
  751. $insert_detail_man[$keys] = $customer_man_tmp;
  752. if(! isset($insert[$keys])){
  753. $tmp['order_number'] = OrderNoService::createSalesOrderNumber($prefix);
  754. $tmp['sales_order_type'] = $value[1];
  755. $tmp['customer_short_name'] = $value[2];
  756. $tmp['customer_id'] = $customer_tmp;
  757. $tmp['customer_contact'] = $customer_contact_tmp;
  758. $tmp['plat_type'] = $value[3];
  759. $tmp['plat_order'] = $value[0];
  760. $tmp['discount_fee'] = $value[8] ?: 0;
  761. $tmp['cdefine29'] = $value[13] ?? "";//分社施工
  762. $tmp['cdefine32'] = $value[14] ?? "";//达人昵称
  763. $tmp['cdefine30'] = $value[15] ?? "";//业务员
  764. $tmp['product_total'] += $value[6];
  765. $tmp['contract_fee'] += $value[6] - $tmp['discount_fee'];
  766. $tmp['construction_time'] = $value[11] ?? 0;
  767. $tmp['handover_time'] = $value[12] ?? 0;
  768. $insert[$keys] = $tmp;
  769. }else{
  770. $insert[$keys]['product_total'] += $value[6];
  771. $insert[$keys]['contract_fee'] += $value[6];
  772. }
  773. $tmp_detail['product_id'] = $product_tmp['id'];
  774. $tmp_detail['cost'] = $product_tmp['cost'];
  775. $tmp_detail['retail_price'] = $product_tmp['retail_price'];
  776. $tmp_detail['price'] = $product_tmp['retail_price'];
  777. $tmp_detail['final_amount'] = $value[6];
  778. $tmp_detail['number'] = $value[5];
  779. $insert_detail[$keys][] = $tmp_detail;
  780. }
  781. $insert_detail = array_values($insert_detail);
  782. $insert_detail_man = array_values($insert_detail_man);
  783. try{
  784. DB::beginTransaction();
  785. if(! empty($insert)) SalesOrder::insert($insert);
  786. $last_insert_id = SalesOrder::where('crt_time',$time)
  787. ->select('id')
  788. ->get()->toArray();
  789. $last_insert_id = array_column($last_insert_id,'id');
  790. if(! empty($insert_detail)){
  791. $insert2 = [];
  792. foreach ($last_insert_id as $key => $value){
  793. if(isset($insert_detail[$key])) {
  794. foreach ($insert_detail[$key] as $val){
  795. $val['sales_order_id'] = $value;
  796. $insert2[] = $val;
  797. }
  798. }
  799. }
  800. SalesOrderProductInfo::insert($insert2);
  801. }
  802. if(! empty($insert_detail_man)){
  803. $insert3 = [];
  804. foreach ($last_insert_id as $key => $value){
  805. if(isset($insert_detail_man[$key])) {
  806. foreach ($insert_detail_man[$key] as $val){
  807. $val['sales_order_id'] = $value;
  808. $insert3[] = $val;
  809. }
  810. }
  811. }
  812. SalesOrderInfo::insert($insert3);
  813. }
  814. DB::commit();
  815. }catch (\Exception $e){
  816. DB::rollBack();
  817. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  818. }
  819. return [true, ''];
  820. }
  821. }