ImportService.php 35 KB

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