ImportService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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\Employee;
  9. use App\Model\Product;
  10. use App\Model\ProductCategory;
  11. use App\Model\ProductPriceDetail;
  12. use App\Model\SalesOrder;
  13. use App\Model\SalesOrderProductInfo;
  14. use Illuminate\Support\Facades\DB;
  15. use Maatwebsite\Excel\Facades\Excel;
  16. use PhpOffice\PhpSpreadsheet\IOFactory;
  17. class ImportService extends Service
  18. {
  19. public static $type = [
  20. 'product', //产品
  21. 'customer', //客户
  22. 'salesOnline', //线上订单
  23. ];
  24. //写活的导入------------------- 暂时不好用
  25. //导入入口
  26. public function import($data,$user){
  27. if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
  28. if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
  29. if(empty($data['file'])) return [false,'导入文件不能为空'];
  30. //导入的数据并且校验写入
  31. list($status,$msg) = $this->importMain($data,$user);
  32. if(! $status) return [false, $msg];
  33. return [true, ''];
  34. }
  35. //主方法
  36. public function importMain($data,$user){
  37. //获取配置文件
  38. $config = "excel." . $data['type'];
  39. $config_array = config($config) ?? [];
  40. if(empty($config_array)) return [false, '配置文件不存在'];
  41. //(特殊 额外的表头数据)
  42. $config_array = $this->getTableTitle($config_array,$user,$data);
  43. //获取合并单元格范围
  44. $uploadedFile = $_FILES['file']['tmp_name']; // 获取上传文件的临时路径
  45. $spreadsheet = IOFactory::load($uploadedFile); // 加载上传的 Excel 文件
  46. $worksheet = $spreadsheet->getActiveSheet(); // 获取第一个工作表
  47. $mergedCells = $worksheet->getMergeCells(); // 获取单元格合并范围
  48. // 需要导入的公用数据
  49. $msg['user_id'] = $user['id'];
  50. $msg['depart_id'] = $this->getDepart($user);
  51. $msg['top_depart_id'] = $user['depart_map'][$msg['depart_id']] ?? 0;
  52. //导入
  53. $import = new Import();
  54. $import->setConfig($config_array, $mergedCells,$msg);
  55. \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
  56. //异常提示报错
  57. if($import->getMsg()) return [false, $import->getMsg()];
  58. return [true, ''];
  59. }
  60. //表头入口
  61. public function getTableTitle($config_array,$user,$data){
  62. if(! empty($config_array['dynamics_field'])){
  63. $func = $config_array['dynamics_field']['func'];
  64. return $this->$func($config_array,$user,$data);
  65. }
  66. return $config_array;
  67. }
  68. //产品导入的额外表头
  69. public function productTitle($config_array,$user,$data){
  70. $model = BasicType::TopClear($user,$data);
  71. $result = $model->whereRaw('type = 22 And del_time = 0')->get()->toArray();
  72. if(! empty($result)){
  73. foreach ($result as $value){
  74. $config_array['field'][$value['title']] = [
  75. "key" => $config_array['dynamics_field']['name'],
  76. "key_array" => [
  77. "basic_type_id" => $value['id'],
  78. "price" => 0,
  79. ],
  80. "rule" => "",
  81. "other_rule" => "is_numeric",
  82. "multiple" => true,
  83. "map" => [
  84. $value['title'] => "price",
  85. ],
  86. ];
  87. }
  88. }
  89. return $config_array;
  90. }
  91. //产品导入的额外数据
  92. public function fillInsertProductData($time){
  93. $last_insert_data = Product::where('crt_time',$time)
  94. ->where('del_time',0)
  95. ->select("id","product_category_id")
  96. ->get()->toArray();
  97. if(empty($last_insert_data)) return;
  98. $list = ProductCategory::where('del_time',0)
  99. ->select('id','parent_id')
  100. ->get()->toArray();
  101. foreach ($last_insert_data as $value){
  102. $parentsId = $this->findParentIds($value['product_category_id'], $list);
  103. array_unshift($parentsId, $value['product_category_id']);
  104. $result = array_reverse($parentsId);
  105. Product::where('id',$value['id'])->update([
  106. 'product_category' => json_encode($result)
  107. ]);
  108. }
  109. }
  110. //写活的导入------------------- 暂时不好用
  111. //写死的导入
  112. public function getTableTitleXls($data,$user){
  113. if(empty($data['type'])) return [false,'缺少类型'];
  114. if(! in_array($data['type'],self::$type)) return [false,'类型不存在'];
  115. //获取配置文件
  116. $fuc = $data['type'];
  117. list($status,$msg,$filename) = $this->$fuc($data,$user);
  118. if(!$status) return [false, $msg];
  119. $headers = array_column($msg,'value');
  120. Excel::store(new TableHeadExport([], $headers),"/public/export/{$filename}", null, 'Xlsx', []);
  121. return [true, ['file' => $filename]];
  122. }
  123. private function customer($data,$user){
  124. //生成下载文件
  125. $filename = "客户模板_" . time() . '.' . 'xlsx';
  126. //获取配置文件
  127. $config = "excel.customerTable";
  128. $config_array = config($config) ?? [];
  129. if(empty($config_array)) return [false, '配置文件不存在',''];
  130. return [true, $config_array,$filename];
  131. }
  132. private function product($data,$user){
  133. //获取配置文件
  134. $config = "excel.productTable";
  135. $config_array = config($config) ?? [];
  136. if(empty($config_array)) return [false, '配置文件不存在', ''];
  137. $model = BasicType::TopClear($user,$data);
  138. $result = $model->whereRaw('type = 22 And del_time = 0')->get()->toArray();
  139. if(! empty($result)){
  140. foreach ($result as $value){
  141. $config_array[] = [
  142. 'key' => 'table_id.' . $value['id'],
  143. 'value' => $value['title'],
  144. ];
  145. }
  146. }
  147. //生成下载文件
  148. $filename = "产品模板_" . time() . '.' . 'xlsx';
  149. return [true, $config_array,$filename];
  150. }
  151. private function salesOnline($data,$user){
  152. //生成下载文件
  153. $filename = "线上订单模板_" . time() . '.' . 'xlsx';
  154. //获取配置文件
  155. $config = "excel.salesOnlineTable";
  156. $config_array = config($config) ?? [];
  157. if(empty($config_array)) return [false, '配置文件不存在',''];
  158. return [true, $config_array,$filename];
  159. }
  160. //导入入口
  161. public function importAll($data,$user){
  162. if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
  163. if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
  164. if(empty($data['file'])) return [false,'导入文件不能为空'];
  165. $import = new ImportAll();
  166. //设置导入人id
  167. $import->setCrt($user['id']);
  168. $import->setUser($user);
  169. $import->setType($data['type']);
  170. //导入
  171. \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
  172. if($import->getMsg()) return [false, $import->getMsg()];
  173. return [true, ''];
  174. }
  175. public function customerImport($array, $user){
  176. $head = $user['head']['id'] ?? 0;
  177. // 去除表头
  178. unset($array[0]);
  179. if(empty($array)) return [false, '导入数据不能为空'];
  180. //第一次表格数据校验 非空 已经过滤数据
  181. $array_clean = [];
  182. foreach ($array as $key => $value){
  183. $rowData = array_filter($value);
  184. if (empty($rowData)) {
  185. unset($array[$key]);
  186. } elseif(empty($value[0]) || empty($value[1])) {
  187. return [false, '带*号的字段项必填'];
  188. }else{
  189. foreach ($value as $k => $v){
  190. $value[$k] = trim($v);
  191. }
  192. if(! isset(Customer::dk[$value[0]])) return [false, '客户模板填写错误'];
  193. $value[0] = Customer::dk[$value[0]];
  194. if(in_array($value[1],$array_clean)) return [false, '客户名称不能重复'];
  195. $array_clean[] = $value[1];
  196. $array[$key] = $value;
  197. }
  198. }unset($array_clean);
  199. if(empty($array)) return [false, '导入数据不能为空'];
  200. //客户
  201. $model = Customer::Clear($user,[]);
  202. $customer = $model->where('del_time',0)
  203. ->whereIn('title',array_unique(array_column($array,'1')))
  204. ->pluck('id','title')
  205. ->toArray();
  206. $model = BasicType::TopClear($user,[]);
  207. $basic = $model->where('del_time',0)
  208. ->whereIn('type',[1,2,3,5,9,10])
  209. ->select('id','title','type')
  210. ->get()
  211. ->toArray();
  212. $basic_list = [];
  213. foreach ($basic as $value){
  214. if($value['type'] == 1){
  215. $basic_list[2][$value['title']] = $value['id'];
  216. }elseif ($value['type'] == 2){
  217. $basic_list[3][$value['title']] = $value['id'];
  218. }elseif ($value['type'] == 3){
  219. $basic_list[4][$value['title']] = $value['id'];
  220. }elseif ($value['type'] == 5){
  221. $basic_list[7][$value['title']] = $value['id'];
  222. }elseif ($value['type'] == 9){
  223. $basic_list[8][$value['title']] = $value['id'];
  224. }elseif ($value['type'] == 10){
  225. $basic_list[9][$value['title']] = $value['id'];
  226. }
  227. }
  228. $model = Product::ProductClear($user,[]);
  229. $product = $model->where('del_time',0)
  230. ->whereIn('title',array_unique(array_column($array,'6')))
  231. ->pluck('id','title')
  232. ->toArray();
  233. $emp = Employee::where('del_time',0)
  234. ->whereIn('emp_name',array_unique(array_merge_recursive(array_column($array,'11'),array_column($array,'12'))))
  235. ->pluck('id','emp_name')
  236. ->toArray();
  237. $time = time();
  238. $insert = [];
  239. foreach ($array as $value){
  240. $tmp = [
  241. 'model_type' => '',
  242. 'title' => '',
  243. 'customer_intention' => '',
  244. 'customer_from' => '',
  245. 'customer_type' => '',
  246. 'car_type' => '',
  247. 'consulting_product' => '',
  248. 'intention_product' => '',
  249. 'progress_stage' => '',
  250. 'state_type' => '',
  251. 'mark' => '',
  252. 'depart_id' => $head,
  253. 'top_depart_id' => $head,
  254. 'crt_id' => $user['id'],
  255. 'crt_time' => $time,
  256. 'upd_time' => $time,
  257. ];
  258. $tmp['model_type'] = $value['0'];
  259. $tmp['consulting_product'] = $value['5'];
  260. $tmp['mark'] = $value['10'];
  261. if(! empty($customer[$value['1']])) return [false, '客户:' . $value['1'] . '已存在'];
  262. $tmp['title'] = $value['1'];
  263. if($value['2']){
  264. if(empty($basic_list[2][$value['2']])) return [false, '客户意向度:' . $value['2'] . '不存在'];
  265. $tmp['customer_intention'] = $basic_list[2][$value['2']];
  266. }
  267. if($value['3']){
  268. if(empty($basic_list[3][$value['3']])) return [false, '客户来源:' . $value['3'] . '不存在'];
  269. $tmp['customer_from'] = $basic_list[3][$value['3']];
  270. }
  271. if($value['4']){
  272. if(empty($basic_list[4][$value['4']])) return [false, '客户类别:' . $value['4'] . '不存在'];
  273. $tmp['customer_type'] = $basic_list[4][$value['4']];
  274. }
  275. if($value['6']){
  276. if(empty($product[$value['6']])) return [false, '意向产品:' . $value['6'] . '不存在'];
  277. $tmp['intention_product'] = $product[$value['6']];
  278. }
  279. if($value['7']){
  280. if(empty($basic_list[7][$value['7']])) return [false, '进展阶段:' . $value['7'] . '不存在'];
  281. $tmp['progress_stage'] = $basic_list[7][$value['7']];
  282. }
  283. if($value['8']){
  284. if(empty($basic_list[8][$value['8']])) return [false, '状态:' . $value['8'] . '不存在'];
  285. $tmp['state_type'] = $basic_list[8][$value['8']];
  286. }
  287. if($value['9']){
  288. if(empty($basic_list[9][$value['9']])) return [false, '车型:' . $value['9'] . '不存在'];
  289. $tmp['car_type'] = $basic_list[9][$value['9']];
  290. }
  291. // if($value['11']){
  292. // if(empty($emp[$value['11']])) return [false, '负责人:' . $value['11'] . '不存在'];
  293. // $tmp['emp_one'] = $emp[$value['11']];
  294. // }
  295. // if($value['12']){
  296. // if(empty($emp[$value['12']])) return [false, '协同人:' . $value['12'] . '不存在'];
  297. // $tmp['emp_two'] = $emp[$value['12']];
  298. // }
  299. $insert[] = $tmp;
  300. }
  301. try{
  302. DB::beginTransaction();
  303. if(! empty($insert)) Customer::insert($insert);
  304. DB::commit();
  305. }catch (\Exception $e){
  306. DB::rollBack();
  307. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  308. }
  309. return [true, ''];
  310. }
  311. public function productImport($array, $user){
  312. $head = $user['head']['id'] ?? 0;
  313. // 去除表头
  314. $upload = $array[0];
  315. unset($array[0]);
  316. if(empty($array)) return [false, '导入数据不能为空'];
  317. //第一次表格数据校验 非空 已经过滤数据
  318. $array_clean = [];
  319. $search = "";
  320. foreach ($array as $key => $value){
  321. $rowData = array_filter($value);
  322. if (empty($rowData)) {
  323. unset($array[$key]);
  324. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2])) {
  325. return [false, '带*号的字段项必填'];
  326. }else{
  327. foreach ($value as $k => $v){
  328. $value[$k] = trim($v);
  329. }
  330. $t = $value[0] . $value[1];
  331. if(in_array($t,$array_clean)) return [false, '产品名称与编码不能重复'];
  332. $array_clean[] = $t;
  333. $array[$key] = $value;
  334. $search .= "(title = '".$value[0]."' and code ='".$value[1]."') or";
  335. }
  336. }unset($array_clean);
  337. if(empty($array)) return [false, '导入数据不能为空'];
  338. $search = rtrim($search,' or');
  339. $product = Product::whereRaw($search)
  340. ->where('del_time',0)
  341. ->select('code','title')
  342. ->get()->toArray();
  343. $product_array = [];
  344. foreach ($product as $value){
  345. $product_array[] = $value['title'] . $value['code'];
  346. }
  347. //默认进来 自身顶级公司
  348. $top_depart_id = $user['depart_top'][0] ?? [];
  349. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  350. $category_list = ProductCategory::where('del_time',0)
  351. ->where('top_depart_id',$top_depart_id)
  352. ->get()->toArray();
  353. $model = BasicType::TopClear($user,[]);
  354. $basic = $model->where('del_time',0)
  355. ->where('type',20)
  356. ->pluck('id','title')
  357. ->toArray();
  358. $category = ProductCategory::whereIn('title',array_unique(array_column($array,'2')))
  359. ->where('del_time',0)
  360. ->where('top_depart_id',$top_depart_id)
  361. ->pluck('id','title')
  362. ->toArray();
  363. $category_parent = array_unique(array_column($category,'parent'));
  364. $time = time();
  365. $table_head = $this->product([],$user);
  366. $heads = $table_head[1];
  367. $tmp = array_column($heads,'key');
  368. $tmp = array_fill_keys($tmp, '');
  369. $tmp['product_category'] = '';
  370. $tmp['depart_id'] = $head;
  371. $tmp['top_depart_id'] = $head;
  372. $tmp['crt_id'] = $user['id'];
  373. $tmp['crt_time'] = $time;
  374. $tmp['upd_time'] = $time;
  375. $tmp['crt_id'] = $user['id'];
  376. $upload = array_flip($upload);
  377. $map = [];
  378. foreach ($heads as $value){
  379. if(strpos($value['key'], 'table_id.') !== false && isset($upload[$value['value']])){
  380. $map[$value['key']] = [
  381. 'col' => $upload[$value['value']],
  382. 'name' => $value['value']
  383. ];
  384. }
  385. }
  386. $array = array_values($array);
  387. $insert = $insert2 = [];
  388. foreach ($array as $key => $value){
  389. $pro_str = $value['0'] . $value['1'];
  390. if(in_array($pro_str, $product_array)) return [false, '产品名称、编码:' . $value['0']. '、' . $value['1'] . '已存在'];
  391. $tmp['title'] = $value['0'];
  392. $tmp['code'] = $value['1'];
  393. if(empty($category[$value['2']])) return [false,'产品分类:' . $value['2'] . '不存在'];
  394. $tmp['product_category_id'] = $category[$value['2']];
  395. if(in_array($tmp['product_category_id'],$category_parent)) return [false,'产品分类:' . $value['2'] . '下存在子分类,请将产品建与最底层分类下'];
  396. $parentsId = $this->findParentIds($tmp['product_category_id'], $category_list);
  397. array_unshift($parentsId, $tmp['product_category_id']);
  398. $result = array_reverse($parentsId);
  399. $tmp['product_category'] = json_encode($result);
  400. $tmp['size'] = $value['3'];
  401. if($value['4']){
  402. if(empty($basic[$value['4']])) return [false, '单位:' . $value['4'] . '不存在'];
  403. $tmp['unit'] = $basic[$value['4']];
  404. }
  405. $tmp['bar_code'] = $value['5'];
  406. $tmp['cost'] = $value['6'];
  407. $tmp['retail_price'] = $value['7'];
  408. foreach ($map as $m => $v){
  409. if($value[$v['col']]){
  410. if(! is_numeric($value[$v['col']])) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  411. $formattedNumber = number_format($value[$v['col']], 2, '.', '');
  412. if($formattedNumber != $value[$v['col']]) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  413. }
  414. $tmp[$m] = $value[$v['col']];
  415. }
  416. foreach ($tmp as $k => $v){
  417. if(strpos($k, 'table_id.') !== false){
  418. $tmp2 = [];
  419. $k_n = str_replace('table_id.', "", $k);
  420. $tmp2['basic_type_id'] = $k_n;
  421. $tmp2['price'] = $v;
  422. $tmp2['crt_time'] = $time;
  423. $tmp2['upd_time'] = $time;
  424. $insert2[$key][] = $tmp2;
  425. unset($tmp[$k]);
  426. }
  427. }
  428. $insert[] = $tmp;
  429. }
  430. try{
  431. DB::beginTransaction();
  432. if(! empty($insert)) Product::insert($insert);
  433. if(! empty($insert2)){
  434. $insert_detail = [];
  435. $last_insert_id = Product::where('crt_time',$time)
  436. ->select('id')
  437. ->get()->toArray();
  438. $last_insert_id = array_column($last_insert_id,'id');
  439. foreach ($last_insert_id as $key => $value){
  440. if(isset($insert2[$key])) {
  441. foreach ($insert2[$key] as $val){
  442. $val['product_id'] = $value;
  443. $insert_detail[] = $val;
  444. }
  445. }
  446. }
  447. ProductPriceDetail::insert($insert_detail);
  448. }
  449. DB::commit();
  450. }catch (\Exception $e){
  451. DB::rollBack();
  452. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  453. }
  454. return [true, ''];
  455. }
  456. public function salesOnlineImport($array, $user){
  457. $head = $user['head']['id'] ?? 0;
  458. // 去除表头
  459. unset($array[0]);
  460. if(empty($array)) return [false, '导入数据不能为空'];
  461. $model = BasicType::TopClear($user,[]);
  462. $basic_type = $model->where('del_time',0)
  463. ->where('type',24)
  464. ->pluck('id','title')
  465. ->toArray();
  466. $search = "";
  467. foreach ($array as $key => $value){
  468. $rowData = array_filter($value);
  469. if (empty($rowData)) {
  470. unset($array[$key]);
  471. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || empty($value[6])) {
  472. return [false, '带*号的字段项必填'];
  473. }else{
  474. foreach ($value as $k => $v){
  475. $value[$k] = trim($v);
  476. }
  477. if(! isset($basic_type[$value[1]])) return [false, '店铺(平台类型):' . $value[1] .'不存在'];
  478. $value[1] = $basic_type[$value[1]];
  479. if(! isset(SalesOrder::$order_type[$value[6]])) return [false, '产品类型填写错误'];
  480. $array[$key] = $value;
  481. $search .= "(code = '".$value[2]."' and title ='".$value[3]."') or";
  482. }
  483. }
  484. if(empty($array)) return [false, '导入数据不能为空'];
  485. $search = rtrim($search,' or');
  486. $model = Product::ProductClear($user,[]);
  487. $product = $model->whereRaw($search)
  488. ->where('del_time',0)
  489. ->select('title','id','code','cost','retail_price')
  490. ->get()->toArray();
  491. $product_map = [];
  492. foreach ($product as $value){
  493. $product_map[$value['code'] . $value['title']] = [
  494. 'id' => $value['id'],
  495. 'cost' => $value['cost'],
  496. 'retail_price' => $value['retail_price'],
  497. ];
  498. }
  499. $time = time();
  500. $tmp = [
  501. 'model_type' => SalesOrder::Model_type_four,
  502. 'sales_order_type' => 0,
  503. 'order_number' => '',
  504. 'plat_type' => '',
  505. 'plat_order' => '',
  506. 'sign_time' => $time,
  507. 'product_total' => 0,
  508. 'contract_fee' => 0,
  509. 'rate' => 100,
  510. 'depart_id' => $head,
  511. 'top_depart_id' => $head,
  512. 'crt_id' => $user['id'],
  513. 'crt_time' => $time,
  514. 'upd_time' => $time,
  515. ];
  516. $tmp_detail = [
  517. 'sales_order_id' => 0,
  518. 'product_id' => 0,
  519. 'cost' => 0,
  520. 'retail_price' => 0,
  521. 'price' => 0,
  522. 'final_amount' => 0,
  523. 'number' => '',
  524. 'crt_time' => $time,
  525. ];
  526. $insert = $insert_detail = [];
  527. $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
  528. foreach ($array as $value){
  529. $product_str = $value[2] . $value[3];
  530. if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[2]. ']' . '[' . $value[3]. ']' . '不存在'];
  531. $product_tmp = $product_map[$product_str] ?? [];
  532. $tmp['product_total'] = $tmp['contract_fee'] = 0;
  533. $keys = $value[0] . $value[6];
  534. if(! isset($insert[$keys])){
  535. $tmp['order_number'] = OrderNoService::createSalesOrderNumber($prefix);
  536. $tmp['sales_order_type'] = $value[6];
  537. $tmp['plat_type'] = $value[1];
  538. $tmp['plat_order'] = $value[0];
  539. $tmp['product_total'] += $value[4] * $value[5];
  540. $tmp['contract_fee'] += $value[4] * $value[5];
  541. $insert[$keys] = $tmp;
  542. }else{
  543. $insert[$keys]['product_total'] += $value[4] * $value[5];
  544. $insert[$keys]['contract_fee'] += $value[4] * $value[5];
  545. }
  546. $tmp_detail['product_id'] = $product_tmp['id'];
  547. $tmp_detail['cost'] = $product_tmp['cost'];
  548. $tmp_detail['retail_price'] = $product_tmp['retail_price'];
  549. $tmp_detail['price'] = $value[5];
  550. $tmp_detail['final_amount'] = $value[4] * $value[5];
  551. $tmp_detail['number'] = $value[4];
  552. $insert_detail[$keys][] = $tmp_detail;
  553. }
  554. $insert_detail = array_values($insert_detail);
  555. try{
  556. DB::beginTransaction();
  557. if(! empty($insert)) SalesOrder::insert($insert);
  558. if(! empty($insert_detail)){
  559. $insert2 = [];
  560. $last_insert_id = SalesOrder::where('crt_time',$time)
  561. ->select('id')
  562. ->get()->toArray();
  563. $last_insert_id = array_column($last_insert_id,'id');
  564. foreach ($last_insert_id as $key => $value){
  565. if(isset($insert_detail[$key])) {
  566. foreach ($insert_detail[$key] as $val){
  567. $val['sales_order_id'] = $value;
  568. $insert2[] = $val;
  569. }
  570. }
  571. }
  572. SalesOrderProductInfo::insert($insert2);
  573. }
  574. DB::commit();
  575. }catch (\Exception $e){
  576. DB::rollBack();
  577. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  578. }
  579. return [true, ''];
  580. }
  581. }