ImportService.php 52 KB

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