ImportService.php 36 KB

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