ImportService.php 52 KB

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