ImportService.php 48 KB

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