ImportService.php 62 KB

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