ImportService.php 36 KB

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