ImportService.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  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. $tmp = array_column($heads,'key');
  486. $tmp = array_fill_keys($tmp, '');
  487. $tmp['product_category'] = '';
  488. $tmp['upd_time'] = $time;
  489. $top_message = Depart::where('parent_id',0)
  490. ->pluck('title','id')
  491. ->toArray();
  492. $upload = array_flip($upload);
  493. $map = [];
  494. foreach ($heads as $value){
  495. if(strpos($value['key'], 'table_id.') !== false && isset($upload[$value['value']])){
  496. $map[$value['key']] = [
  497. 'col' => $upload[$value['value']],
  498. 'name' => $value['value']
  499. ];
  500. }
  501. }
  502. $array = array_values($array);
  503. $insert = $insert2 = $update = $update2 = [];
  504. foreach ($array as $value){
  505. if(isset($product_array[$value['1']])){
  506. $pro_tmp = $product_array[$value['1']] ?? [];
  507. if($pro_tmp['top_depart_id'] != $top_depart_id){
  508. $belong = $top_message[$pro_tmp['top_depart_id']] ?? "";
  509. $now = $top_message[$top_depart_id] ?? "";
  510. return [false, '产品编码:' . $value['1'] . '属于门店:' . $belong . ',当前门店:' . $now . ',不允许跨门店操作更新产品!'];
  511. }
  512. }
  513. $tmp['title'] = $value['0'];
  514. $tmp['code'] = $value['1'];
  515. if(empty($category[$value['2']])) return [false,'产品分类:' . $value['2'] . '不存在'];
  516. $tmp['product_category_id'] = $category[$value['2']];
  517. if(in_array($tmp['product_category_id'],$category_parent)) return [false,'产品分类:' . $value['2'] . '下存在子分类,请将产品建与最底层分类下'];
  518. $parentsId = $this->findParentIds($tmp['product_category_id'], $category_list);
  519. array_unshift($parentsId, $tmp['product_category_id']);
  520. $result = array_reverse($parentsId);
  521. $tmp['product_category'] = json_encode($result);
  522. $tmp['size'] = $value['3'];
  523. if($value['4']){
  524. if(empty($basic[$value['4']])) return [false, '单位:' . $value['4'] . '不存在'];
  525. $tmp['unit'] = $basic[$value['4']];
  526. }
  527. $tmp['bar_code'] = $value['5'];
  528. $tmp['cost'] = $value['6'] ?? 0;
  529. $tmp['retail_price'] = $value['7'] ?? 0;
  530. $tmp['product_attribute'] = $value['8'] ?? 0;
  531. $tmp['build_fee'] = $value['9'] ?? 0;
  532. foreach ($map as $m => $v){
  533. if($value[$v['col']]){
  534. if(! is_numeric($value[$v['col']])) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  535. $formattedNumber = number_format($value[$v['col']], 2, '.', '');
  536. if($formattedNumber != $value[$v['col']]) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  537. }
  538. $tmp[$m] = $value[$v['col']];
  539. }
  540. if(isset($product_array[$value['1']])){
  541. //更新
  542. $pro_tmp = $product_array[$value['1']] ?? [];
  543. $product_id = $pro_tmp['id'];
  544. //产品价格子表
  545. foreach ($tmp as $k => $v){
  546. if(strpos($k, 'table_id.') !== false){
  547. $tmp2 = [];
  548. $k_n = str_replace('table_id.', "", $k);
  549. $tmp2['product_id'] = $product_id;
  550. $tmp2['basic_type_id'] = $k_n;
  551. $tmp2['price'] = $v;
  552. $tmp2['crt_time'] = $time;
  553. $tmp2['upd_time'] = $time;
  554. $update2[] = $tmp2;
  555. unset($tmp[$k]);
  556. }
  557. }
  558. //产品主表
  559. $update[$product_id] = $tmp;
  560. }else{
  561. $tmp['depart_id'] = $depart_id;
  562. $tmp['top_depart_id'] = $top_depart_id;
  563. $tmp['crt_id'] = $user['id'];
  564. $tmp['crt_time'] = $time;
  565. //产品价格子表
  566. foreach ($tmp as $k => $v){
  567. if(strpos($k, 'table_id.') !== false){
  568. $tmp2 = [];
  569. $k_n = str_replace('table_id.', "", $k);
  570. $tmp2['basic_type_id'] = $k_n;
  571. $tmp2['price'] = $v;
  572. $tmp2['crt_time'] = $time;
  573. $tmp2['upd_time'] = $time;
  574. $insert2[$tmp['code']][] = $tmp2;
  575. unset($tmp[$k]);
  576. }
  577. }
  578. //产品主表
  579. $insert[$tmp['code']] = $tmp;
  580. }
  581. }
  582. try{
  583. DB::beginTransaction();
  584. //新增
  585. if(! empty($insert)){
  586. Product::insert($insert);
  587. if(! empty($insert2)){
  588. $insert_detail = [];
  589. $last_insert_id = Product::where('crt_time',$time)
  590. ->pluck('id','code')
  591. ->toArray();
  592. foreach ($insert2 as $code => $val){
  593. foreach ($val as $v2){
  594. $v2['product_id'] = $last_insert_id[$code] ?? 0;
  595. $insert_detail[] = $v2;
  596. }
  597. }
  598. ProductPriceDetail::insert($insert_detail);
  599. }
  600. }
  601. //编辑
  602. if(! empty($update)){
  603. foreach ($update as $p_id => $value){
  604. Product::where('id',$p_id)
  605. ->update($value);
  606. }
  607. if(! empty($update2)){
  608. $product_id_array = array_keys($update);
  609. ProductPriceDetail::whereIn('product_id',$product_id_array)
  610. ->update(['del_time' => $time]);
  611. ProductPriceDetail::insert($update2);
  612. }
  613. }
  614. DB::commit();
  615. }catch (\Exception $e){
  616. DB::rollBack();
  617. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  618. }
  619. return [true, ''];
  620. }
  621. public function salesOnlineImport($array, $user){
  622. $head = $user['head']['id'] ?? 0;
  623. // 去除表头
  624. unset($array[0]);
  625. if(empty($array)) return [false, '导入数据不能为空'];
  626. $basic = (new BasicTypeService())->getMyBasicList($user, [18,23,24,29]);
  627. $basic_list = [];
  628. foreach ($basic as $value){
  629. if($value['type'] == 18){
  630. $basic_list[9][$value['title']] = $value['id'];
  631. }elseif ($value['type'] == 23){
  632. $basic_list[10][$value['title']] = $value['id'];
  633. }elseif ($value['type'] == 24){
  634. $basic_list[3][$value['title']] = $value['id'];
  635. }elseif ($value['type'] == 29){
  636. $basic_list[2][$value['title']] = $value['id'];
  637. }
  638. }
  639. $search = "";
  640. $customer = [];
  641. foreach ($array as $key => $value){
  642. $rowData = array_filter($value);
  643. if (empty($rowData)) {
  644. unset($array[$key]);
  645. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || $value[6] === null) {
  646. return [false, '带*号的字段项必填'];
  647. }else{
  648. foreach ($value as $k => $v){
  649. $value[$k] = trim($v);
  650. }
  651. if(! isset($basic_list[2][$value[2]])) return [false, '客户简称:' . $value[2] .'不存在'];
  652. $value[2] = $basic_list[2][$value[2]];
  653. if(! isset($basic_list[3][$value[3]])) return [false, '店铺(平台类型):' . $value[3] .'不存在'];
  654. $value[3] = $basic_list[3][$value[3]];
  655. if(! is_numeric($value[5])) return [false, '货品数量请填写正确的数值'];
  656. if(! is_numeric($value[6])) return [false, '产品合同金额请填写正确的数值'];
  657. if(! empty($value[8]) && ! is_numeric($value[8])) return [false, '订单优惠金额请填写正确的数值'];
  658. if(! empty($value[9])){
  659. if(! isset($basic_list[9][$value[9]])) return [false, '安装方式:' . $value[9] .'不存在'];
  660. $value[9] = $basic_list[9][$value[9]];
  661. }
  662. if(! empty($value[10])){
  663. if(! isset($basic_list[10][$value[10]])) return [false, '安装地点:' . $value[10] .'不存在'];
  664. $value[10] = $basic_list[10][$value[10]];
  665. }
  666. if(! isset(SalesOrder::$order_type_name[$value[1]])) return [false, '产品类型填写错误'];
  667. $value[1] = SalesOrder::$order_type_name[$value[1]];
  668. if(! empty($value[11])) {
  669. list($status,$msg) = $this->changeAndReturnDate($value[11]);
  670. if(! $status) return [false,"施工日期请填写正确的日期格式,例如2023-05-01"];
  671. $value[11] = $msg;
  672. }
  673. if(! empty($value[12])) {
  674. list($status,$msg) = $this->changeAndReturnDate($value[12]);
  675. if(! $status) return [false,"交车日期请填写正确的日期格式,例如2023-05-01"];
  676. $value[12] = $msg;
  677. }
  678. $array[$key] = $value;
  679. $search .= "(code = '".$value[4]."') or";
  680. if(! empty($value[7]) && ! in_array($value[7], $customer)) $customer[] = $value[7];
  681. }
  682. }
  683. if(empty($array)) return [false, '导入数据不能为空'];
  684. $search = rtrim($search,' or');
  685. $search = "($search)";
  686. $model = Product::ProductClear($user,[]);
  687. $product = $model->whereRaw($search)
  688. ->where('del_time',0)
  689. ->select('title','id','code','cost','retail_price')
  690. ->get()->toArray();
  691. // $pro = (new ProductService())->productList(['product_id' => array_column($product,'id'), 'type' => 2],$user);
  692. $product_map = [];
  693. foreach ($product as $value){
  694. $product_map[$value['code']] = [
  695. 'id' => $value['id'],
  696. 'cost' => $value['cost'],
  697. 'retail_price' => $value['retail_price'],
  698. ];
  699. }
  700. $time = time();
  701. $model = Customer::Clear($user,[]);
  702. $customer_map = $model->where('del_time',0)
  703. ->whereIn('title',$customer)
  704. ->pluck('id','title')
  705. ->toArray();
  706. $customer_info = CustomerInfo::where('del_time',0)
  707. ->whereIn('customer_id',array_values($customer_map))
  708. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  709. ->select('customer_id','type','contact_info','data_id')
  710. ->orderBy('id','asc')
  711. ->get()->toArray();
  712. $customer_contact = $customer_man = [];
  713. foreach ($customer_info as $value){
  714. if($value['type'] == CustomerInfo::type_one && ! isset($customer_contact[$value['customer_id']])){
  715. $customer_contact[$value['customer_id']] = $value['contact_info'];
  716. }elseif ($value['type'] == CustomerInfo::type_two){
  717. $customer_man[$value['customer_id']][] = [
  718. 'type' => SalesOrderInfo::type_two,
  719. 'data_id' => $value['data_id'],
  720. 'crt_time' => $time,
  721. ];
  722. }
  723. }
  724. $tmp = [
  725. 'model_type' => SalesOrder::Model_type_four,
  726. 'sales_order_type' => 0,
  727. 'order_number' => '',
  728. 'customer_short_name' => '',
  729. 'plat_type' => '',
  730. 'plat_order' => '',
  731. 'sign_time' => $time,
  732. 'product_total' => 0,
  733. 'contract_fee' => 0,
  734. 'discount_fee' => 0,
  735. 'cdefine29' => '',//分社施工
  736. 'cdefine32' => '',//达人昵称
  737. 'cdefine30' => '',//业务员
  738. 'rate' => 100,
  739. 'depart_id' => $head,
  740. 'top_depart_id' => $head,
  741. 'crt_id' => $user['id'],
  742. 'crt_time' => $time,
  743. 'upd_time' => $time,
  744. ];
  745. $tmp_detail = [
  746. 'sales_order_id' => 0,
  747. 'product_id' => 0,
  748. 'cost' => 0,
  749. 'retail_price' => 0,
  750. 'basic_type_id' => 0,
  751. 'price' => 0,
  752. 'final_amount' => 0,
  753. 'number' => '',
  754. 'crt_time' => $time,
  755. ];
  756. $insert = $insert_detail = $insert_detail_man = [];
  757. $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
  758. foreach ($array as $value){
  759. $product_str = $value[4];
  760. if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[4]. ']' . '不存在'];
  761. $product_tmp = $product_map[$product_str] ?? [];
  762. $customer_tmp = 0;
  763. $customer_contact_tmp = "";
  764. $customer_man_tmp = [];
  765. if(! empty($value[7])){
  766. $customer_tmp = $customer_map[$value[7]] ?? 0;
  767. $customer_contact_tmp = $customer_contact[$customer_tmp] ?? "";
  768. $customer_man_tmp = $customer_man[$customer_tmp] ?? [];
  769. }
  770. $customer_man_tmp[] = [
  771. 'type' => SalesOrderInfo::type_one,
  772. 'data_id' => $user['id'],
  773. 'crt_time' => $time,
  774. ];
  775. $tmp['product_total'] = $tmp['contract_fee'] = 0;
  776. $keys = $value[0] . $value[1];
  777. $insert_detail_man[$keys] = $customer_man_tmp;
  778. if(! isset($insert[$keys])){
  779. $tmp['order_number'] = OrderNoService::createSalesOrderNumberImport($prefix);
  780. $tmp['sales_order_type'] = $value[1];
  781. $tmp['customer_short_name'] = $value[2];
  782. $tmp['customer_id'] = $customer_tmp;
  783. $tmp['customer_contact'] = $customer_contact_tmp;
  784. $tmp['plat_type'] = $value[3];
  785. $tmp['plat_order'] = $value[0];
  786. $tmp['discount_fee'] = $value[8] ?: 0;
  787. $tmp['cdefine29'] = $value[13] ?? "";//分社施工
  788. $tmp['cdefine32'] = $value[14] ?? "";//达人昵称
  789. $tmp['cdefine30'] = $value[15] ?? "";//业务员
  790. $tmp['product_total'] += $value[6];
  791. $tmp['contract_fee'] += $value[6] - $tmp['discount_fee'];
  792. $tmp['construction_time'] = $value[11] ?? 0;
  793. $tmp['handover_time'] = $value[12] ?? 0;
  794. $insert[$keys] = $tmp;
  795. }else{
  796. $insert[$keys]['product_total'] += $value[6];
  797. $insert[$keys]['contract_fee'] += $value[6];
  798. }
  799. $tmp_detail['product_id'] = $product_tmp['id'];
  800. $tmp_detail['cost'] = $product_tmp['cost'];
  801. $tmp_detail['retail_price'] = $product_tmp['retail_price'];
  802. $tmp_detail['price'] = $product_tmp['retail_price'];
  803. $tmp_detail['final_amount'] = $value[6];
  804. $tmp_detail['number'] = $value[5];
  805. $insert_detail[$keys][] = $tmp_detail;
  806. }
  807. $insert_detail = array_values($insert_detail);
  808. $insert_detail_man = array_values($insert_detail_man);
  809. try{
  810. DB::beginTransaction();
  811. if(! empty($insert)) SalesOrder::insert($insert);
  812. $last_insert_id = SalesOrder::where('crt_time',$time)
  813. ->select('id')
  814. ->get()->toArray();
  815. $last_insert_id = array_column($last_insert_id,'id');
  816. if(! empty($insert_detail)){
  817. $insert2 = [];
  818. foreach ($last_insert_id as $key => $value){
  819. if(isset($insert_detail[$key])) {
  820. foreach ($insert_detail[$key] as $val){
  821. $val['sales_order_id'] = $value;
  822. $insert2[] = $val;
  823. }
  824. }
  825. }
  826. SalesOrderProductInfo::insert($insert2);
  827. }
  828. if(! empty($insert_detail_man)){
  829. $insert3 = [];
  830. foreach ($last_insert_id as $key => $value){
  831. if(isset($insert_detail_man[$key])) {
  832. foreach ($insert_detail_man[$key] as $val){
  833. $val['sales_order_id'] = $value;
  834. $insert3[] = $val;
  835. }
  836. }
  837. }
  838. SalesOrderInfo::insert($insert3);
  839. }
  840. DB::commit();
  841. }catch (\Exception $e){
  842. DB::rollBack();
  843. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  844. }
  845. return [true, ''];
  846. }
  847. public function btOnlineImport($array, $user){
  848. $head = $user['head']['id'] ?? 0;
  849. // 去除表头
  850. unset($array[0]);
  851. if(empty($array)) return [false, '导入数据不能为空'];
  852. $basic = (new BasicTypeService())->getMyBasicList($user, [18,23,24,29]);
  853. $basic_list = [];
  854. foreach ($basic as $value){
  855. if($value['type'] == 18){
  856. $basic_list[9][$value['title']] = $value['id'];
  857. }elseif ($value['type'] == 23){
  858. $basic_list[10][$value['title']] = $value['id'];
  859. }elseif ($value['type'] == 24){
  860. $basic_list[3][$value['title']] = $value['id'];
  861. }elseif ($value['type'] == 29){
  862. $basic_list[2][$value['title']] = $value['id'];
  863. }
  864. }
  865. $search = "";
  866. $customer = [];
  867. foreach ($array as $key => $value){
  868. $rowData = array_filter($value);
  869. if (empty($rowData)) {
  870. unset($array[$key]);
  871. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || $value[6] === null) {
  872. return [false, '带*号的字段项必填'];
  873. }else{
  874. foreach ($value as $k => $v){
  875. $value[$k] = trim($v);
  876. }
  877. if(! isset($basic_list[2][$value[2]])) return [false, '客户简称:' . $value[2] .'不存在'];
  878. $value[2] = $basic_list[2][$value[2]];
  879. if(! isset($basic_list[3][$value[3]])) return [false, '店铺(平台类型):' . $value[3] .'不存在'];
  880. $value[3] = $basic_list[3][$value[3]];
  881. if(! is_numeric($value[5])) return [false, '货品数量请填写正确的数值'];
  882. if(! is_numeric($value[6])) return [false, '产品合同金额请填写正确的数值'];
  883. if(! empty($value[8]) && ! is_numeric($value[8])) return [false, '订单优惠金额请填写正确的数值'];
  884. if(! empty($value[9])){
  885. if(! isset($basic_list[9][$value[9]])) return [false, '安装方式:' . $value[9] .'不存在'];
  886. $value[9] = $basic_list[9][$value[9]];
  887. }
  888. if(! empty($value[10])){
  889. if(! isset($basic_list[10][$value[10]])) return [false, '安装地点:' . $value[10] .'不存在'];
  890. $value[10] = $basic_list[10][$value[10]];
  891. }
  892. if(! isset(SalesOrder::$order_type_name[$value[1]])) return [false, '产品类型填写错误'];
  893. $value[1] = SalesOrder::$order_type_name[$value[1]];
  894. if(! empty($value[11])) {
  895. list($status,$msg) = $this->changeAndReturnDate($value[11]);
  896. if(! $status) return [false,"施工日期请填写正确的日期格式,例如2023-05-01"];
  897. $value[11] = $msg;
  898. }
  899. if(! empty($value[12])) {
  900. list($status,$msg) = $this->changeAndReturnDate($value[12]);
  901. if(! $status) return [false,"交车日期请填写正确的日期格式,例如2023-05-01"];
  902. $value[12] = $msg;
  903. }
  904. $array[$key] = $value;
  905. $search .= "(code = '".$value[4]."') or";
  906. if(! empty($value[7]) && ! in_array($value[7], $customer)) $customer[] = $value[7];
  907. }
  908. }
  909. if(empty($array)) return [false, '导入数据不能为空'];
  910. $search = rtrim($search,' or');
  911. $search = "($search)";
  912. $model = Product::ProductClear($user,[]);
  913. $product = $model->whereRaw($search)
  914. ->where('del_time',0)
  915. ->select('title','id','code','cost','retail_price')
  916. ->get()->toArray();
  917. $product_map = [];
  918. foreach ($product as $value){
  919. $product_map[$value['code']] = [
  920. 'id' => $value['id'],
  921. 'cost' => $value['cost'],
  922. 'retail_price' => $value['retail_price'],
  923. ];
  924. }
  925. $time = time();
  926. $model = Customer::Clear($user,[]);
  927. $customer_map = $model->where('del_time',0)
  928. ->whereIn('title',$customer)
  929. ->pluck('id','title')
  930. ->toArray();
  931. $customer_info = CustomerInfo::where('del_time',0)
  932. ->whereIn('customer_id',array_values($customer_map))
  933. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  934. ->select('customer_id','type','contact_info','data_id')
  935. ->orderBy('id','asc')
  936. ->get()->toArray();
  937. $customer_contact = $customer_man = [];
  938. foreach ($customer_info as $value){
  939. if($value['type'] == CustomerInfo::type_one && ! isset($customer_contact[$value['customer_id']])){
  940. $customer_contact[$value['customer_id']] = $value['contact_info'];
  941. }elseif ($value['type'] == CustomerInfo::type_two){
  942. $customer_man[$value['customer_id']][] = [
  943. 'type' => SalesOrderInfo::type_two,
  944. 'data_id' => $value['data_id'],
  945. 'crt_time' => $time,
  946. ];
  947. }
  948. }
  949. $tmp = [
  950. 'model_type' => SalesOrder::Model_type_seven,
  951. 'sales_order_type' => 0,
  952. 'order_number' => '',
  953. 'customer_short_name' => '',
  954. 'plat_type' => '',
  955. 'plat_order' => '',
  956. 'sign_time' => $time,
  957. 'product_total' => 0,
  958. 'contract_fee' => 0,
  959. 'discount_fee' => 0,
  960. 'cdefine29' => '',//分社施工
  961. 'cdefine32' => '',//达人昵称
  962. 'cdefine30' => '',//业务员
  963. 'rate' => 100,
  964. 'depart_id' => $head,
  965. 'top_depart_id' => $head,
  966. 'crt_id' => $user['id'],
  967. 'crt_time' => $time,
  968. 'upd_time' => $time,
  969. ];
  970. $tmp_detail = [
  971. 'sales_order_id' => 0,
  972. 'product_id' => 0,
  973. 'cost' => 0,
  974. 'retail_price' => 0,
  975. 'basic_type_id' => 0,
  976. 'price' => 0,
  977. 'final_amount' => 0,
  978. 'number' => '',
  979. 'crt_time' => $time,
  980. ];
  981. $insert = $insert_detail = $insert_detail_man = [];
  982. $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
  983. foreach ($array as $value){
  984. $product_str = $value[4];
  985. if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[4]. ']' . '不存在'];
  986. $product_tmp = $product_map[$product_str] ?? [];
  987. $customer_tmp = 0;
  988. $customer_contact_tmp = "";
  989. $customer_man_tmp = [];
  990. if(! empty($value[7])){
  991. $customer_tmp = $customer_map[$value[7]] ?? 0;
  992. $customer_contact_tmp = $customer_contact[$customer_tmp] ?? "";
  993. $customer_man_tmp = $customer_man[$customer_tmp] ?? [];
  994. }
  995. $customer_man_tmp[] = [
  996. 'type' => SalesOrderInfo::type_one,
  997. 'data_id' => $user['id'],
  998. 'crt_time' => $time,
  999. ];
  1000. $tmp['product_total'] = $tmp['contract_fee'] = 0;
  1001. $keys = $value[0] . $value[1];
  1002. $insert_detail_man[$keys] = $customer_man_tmp;
  1003. if(! isset($insert[$keys])){
  1004. $tmp['order_number'] = OrderNoService::createSalesOrderNumberImport($prefix);
  1005. $tmp['sales_order_type'] = $value[1];
  1006. $tmp['customer_short_name'] = $value[2];
  1007. $tmp['customer_id'] = $customer_tmp;
  1008. $tmp['customer_contact'] = $customer_contact_tmp;
  1009. $tmp['plat_type'] = $value[3];
  1010. $tmp['plat_order'] = $value[0];
  1011. $tmp['discount_fee'] = $value[8] ?: 0;
  1012. $tmp['cdefine29'] = $value[13] ?? "";//分社施工
  1013. $tmp['cdefine32'] = $value[14] ?? "";//达人昵称
  1014. $tmp['cdefine30'] = $value[15] ?? "";//业务员
  1015. $tmp['product_total'] += $value[6];
  1016. $tmp['contract_fee'] += $value[6] - $tmp['discount_fee'];
  1017. $tmp['construction_time'] = $value[11] ?? 0;
  1018. $tmp['handover_time'] = $value[12] ?? 0;
  1019. $insert[$keys] = $tmp;
  1020. }else{
  1021. $insert[$keys]['product_total'] += $value[6];
  1022. $insert[$keys]['contract_fee'] += $value[6];
  1023. }
  1024. $tmp_detail['product_id'] = $product_tmp['id'];
  1025. $tmp_detail['cost'] = $product_tmp['cost'];
  1026. $tmp_detail['retail_price'] = $product_tmp['retail_price'];
  1027. $tmp_detail['price'] = $product_tmp['retail_price'];
  1028. $tmp_detail['final_amount'] = $value[6];
  1029. $tmp_detail['number'] = $value[5];
  1030. $insert_detail[$keys][] = $tmp_detail;
  1031. }
  1032. $insert_detail = array_values($insert_detail);
  1033. $insert_detail_man = array_values($insert_detail_man);
  1034. try{
  1035. DB::beginTransaction();
  1036. if(! empty($insert)) SalesOrder::insert($insert);
  1037. $last_insert_id = SalesOrder::where('crt_time',$time)
  1038. ->select('id')
  1039. ->get()->toArray();
  1040. $last_insert_id = array_column($last_insert_id,'id');
  1041. if(! empty($insert_detail)){
  1042. $insert2 = [];
  1043. foreach ($last_insert_id as $key => $value){
  1044. if(isset($insert_detail[$key])) {
  1045. foreach ($insert_detail[$key] as $val){
  1046. $val['sales_order_id'] = $value;
  1047. $insert2[] = $val;
  1048. }
  1049. }
  1050. }
  1051. SalesOrderProductInfo::insert($insert2);
  1052. }
  1053. if(! empty($insert_detail_man)){
  1054. $insert3 = [];
  1055. foreach ($last_insert_id as $key => $value){
  1056. if(isset($insert_detail_man[$key])) {
  1057. foreach ($insert_detail_man[$key] as $val){
  1058. $val['sales_order_id'] = $value;
  1059. $insert3[] = $val;
  1060. }
  1061. }
  1062. }
  1063. SalesOrderInfo::insert($insert3);
  1064. }
  1065. DB::commit();
  1066. }catch (\Exception $e){
  1067. DB::rollBack();
  1068. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  1069. }
  1070. return [true, ''];
  1071. }
  1072. public function lastJcImport($array, $user){
  1073. $head = $user['head']['id'] ?? 0;
  1074. // 去除表头
  1075. unset($array[0]);
  1076. if(empty($array)) return [false, '导入数据不能为空'];
  1077. $shop_name = $product_code = [];
  1078. foreach ($array as $key => $value){
  1079. $rowData = array_filter($value);
  1080. if (empty($rowData)) {
  1081. unset($array[$key]);
  1082. } elseif(empty($value[0]) || empty($value[1])) {
  1083. return [false, '带*号的字段项必填'];
  1084. }else{
  1085. foreach ($value as $k => $v){
  1086. $value[$k] = trim($v);
  1087. }
  1088. if(! is_numeric($value[2])) return [false, '* 上月结存数量请填写正确的数值'];
  1089. if(! is_numeric($value[3])) return [false, '* 上月结存单价请填写正确的数值'];
  1090. if(! is_numeric($value[4])) return [false, '* 上月结存金额请填写正确的数值'];
  1091. if(! in_array($value[0],$shop_name)) $shop_name[] = $value[0];
  1092. if(! in_array($value[1],$product_code)) $product_code[] = $value[1];
  1093. }
  1094. }
  1095. $model = Product::ProductClear($user,[]);
  1096. $product_map = $model->whereIn('code',$product_code)
  1097. ->where('del_time',0)
  1098. ->pluck('id','code')
  1099. ->toArray();
  1100. $depart_map = Depart::whereIn('title',$shop_name)
  1101. ->where('del_time',0)
  1102. ->pluck('id','title')
  1103. ->toArray();
  1104. $storehouse_map = Storehouse::whereIn('top_depart_id',array_values($depart_map))
  1105. ->where('del_time',0)
  1106. ->pluck('id','top_depart_id')
  1107. ->toArray();
  1108. $time = time();
  1109. $insert = [];
  1110. $last_month_stamp = strtotime(date('Y-m-t 23:59:59', strtotime('last month')));
  1111. foreach ($array as $value){
  1112. $depart_tmp = $depart_map[$value[0]] ?? 0;
  1113. if($depart_tmp <= 0) return [false, "门店:" . $value[0] . "不存在或已被删除"];
  1114. $product_tmp = $product_map[$value[1]] ?? 0;
  1115. if($product_tmp <= 0) return [false, "产品编码:" . $value[1] . "不存在或已被删除"];
  1116. $storehouse_tmp = $storehouse_map[$depart_tmp] ?? 0;
  1117. $insert[] = [
  1118. 'top_depart_id' => $depart_tmp,
  1119. 'product_id' => $product_tmp,
  1120. 'storehouse_id' => $storehouse_tmp,
  1121. 'number' => $value[2],
  1122. 'price' => $value[3],
  1123. 'total' => $value[4],
  1124. 'crt_time' => $time,
  1125. 'time' => $last_month_stamp
  1126. ];
  1127. }
  1128. if(empty($insert)) return [false, '暂无更新的数据'];
  1129. try{
  1130. DB::beginTransaction();
  1131. //更新数据为删除
  1132. foreach ($insert as $value){
  1133. LastJc::where('del_time',0)
  1134. ->where('time', $last_month_stamp)
  1135. ->where('top_depart_id',$value['top_depart_id'])
  1136. ->where('storehouse_id',$value['storehouse_id'])
  1137. ->where('product_id',$value['product_id'])
  1138. ->update(['del_time' => $time]);
  1139. }
  1140. //写入数据
  1141. LastJc::insert($insert);
  1142. DB::commit();
  1143. }catch (\Exception $e){
  1144. DB::rollBack();
  1145. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  1146. }
  1147. return [true, ''];
  1148. }
  1149. }