ImportService.php 37 KB

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