ImportService.php 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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. if ($dateTimeObject->format('H:i:s') === '00:00:00') {
  453. // 如果是,则将时间设置为 '23:59:00'
  454. $dateTimeObject->setTime(23, 59);
  455. }
  456. // 现在你可以格式化这个日期了
  457. $formattedDate = $dateTimeObject->format('Y-m-d H:i:s');
  458. if(! strtotime($formattedDate)) return [false, '录入日期请填写正确的日期'];
  459. return [true, strtotime($formattedDate)];
  460. } catch (\Exception $e) {
  461. // 处理转换失败的情况
  462. return [false, '单元格日期格式转换时间戳失败'];
  463. }
  464. }
  465. // 如果不是有效的浮点数,则尝试按照多种日期格式解析
  466. if(! strtotime($cellValue)) return [false, '单元格文本格式转换时间戳失败'];
  467. return [true, strtotime($cellValue)];
  468. }
  469. public function productImport($array, $user){
  470. //当前门店
  471. $depart_id = $this->getDepart($user);
  472. $top_depart_id = $user['depart_map'][$depart_id] ?? 0;
  473. // 去除表头
  474. $upload = $array[0];
  475. unset($array[0]);
  476. if(empty($array)) return [false, '导入数据不能为空'];
  477. //第一次表格数据校验 非空 已经过滤数据
  478. $array_clean = [];
  479. $search = "";
  480. $map_attr = array_flip(Product::$product_attribute);
  481. foreach ($array as $key => $value){
  482. $rowData = array_filter($value);
  483. if (empty($rowData)) {
  484. unset($array[$key]);
  485. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2])) {
  486. return [false, '带*号的字段项必填'];
  487. }else{
  488. foreach ($value as $k => $v){
  489. $value[$k] = trim($v);
  490. }
  491. $t = $value[1];
  492. if(in_array($t,$array_clean)) return [false, '产品编码:'. $value[1] .'在文件中重复出现'];
  493. $array_clean[] = $t;
  494. if(! empty($value[8])){
  495. if(! isset($map_attr[$value[8]])) return [false, '产品属性不存在' . $value[8]];
  496. $value[8] = $map_attr[$value[8]];
  497. }else{
  498. $value[8] = Product::Product_attribute_zero;
  499. }
  500. $array[$key] = $value;
  501. $search .= "(binary code ='".$value[1]."') or";
  502. }
  503. }unset($array_clean);
  504. if(empty($array)) return [false, '导入数据不能为空'];
  505. $search = rtrim($search,' or');
  506. $search = "($search)";
  507. $product = Product::whereRaw($search)
  508. ->where('del_time',0)
  509. ->select('code','top_depart_id','id')
  510. ->get()->toArray();
  511. $product_array = [];
  512. foreach ($product as $value){
  513. $product_array[$value['code']] = [
  514. 'id' => $value['id'],
  515. 'top_depart_id' => $value['top_depart_id']
  516. ];
  517. }
  518. //产品分类
  519. $category_list = ProductCategory::where('del_time',0)
  520. ->select('id','title','parent_id')
  521. ->where('top_depart_id',$top_depart_id)
  522. ->get()->toArray();
  523. // 创建一个以 id 为键的数据映射
  524. $idMap = [];
  525. foreach ($category_list as $item) {
  526. $idMap[$item['id']] = $item;
  527. }
  528. // 创建最终的结果数组
  529. $category_parent = [];
  530. foreach ($idMap as $id => $item) {
  531. // 如果没有子项,则处理
  532. if (! $this->hasChildren($id, $idMap)) {
  533. $category_parent[$id] = $this->getAncestors($id, $idMap);
  534. }
  535. }
  536. $category_map = array_column($category_list,'id','title');
  537. //基础类型
  538. $basic = (new BasicTypeService())->getMyBasicList($user, 20);
  539. $basic = array_column($basic,'id','title');
  540. $time = time();
  541. //表头
  542. $table_head = $this->product([],$user);
  543. $heads = $table_head[1];
  544. $head_value = array_column($heads,'value');
  545. if($head_value !== $upload) return [false, '产品模板错误,请重新下载模板导入'];
  546. //写入数据库的字段
  547. $tmp = array_column($heads,'key');
  548. $tmp = array_fill_keys($tmp, '');
  549. $tmp['product_category'] = '';
  550. $tmp['upd_time'] = $time;
  551. $top_message = Depart::where('parent_id',0)
  552. ->pluck('title','id')
  553. ->toArray();
  554. $upload = array_flip($upload);
  555. $map = [];
  556. foreach ($heads as $value){
  557. if(strpos($value['key'], 'table_id.') !== false && isset($upload[$value['value']])){
  558. $map[$value['key']] = [
  559. 'col' => $upload[$value['value']],
  560. 'name' => $value['value']
  561. ];
  562. }
  563. }
  564. $array = array_values($array);
  565. $insert = $insert2 = $update = $update2 = [];
  566. foreach ($array as $value){
  567. if(isset($product_array[$value['1']])){
  568. $pro_tmp = $product_array[$value['1']] ?? [];
  569. if($pro_tmp['top_depart_id'] != $top_depart_id){
  570. $belong = $top_message[$pro_tmp['top_depart_id']] ?? "";
  571. $now = $top_message[$top_depart_id] ?? "";
  572. return [false, '产品编码:' . $value['1'] . '属于门店:' . $belong . ',当前门店:' . $now . ',不允许跨门店操作更新产品!'];
  573. }
  574. }
  575. $tmp['title'] = $value['0'];
  576. $tmp['code'] = $value['1'];
  577. if(empty($category_map[$value['2']])) return [false,'产品分类:' . $value['2'] . '不存在'];
  578. $tmp['product_category_id'] = $category_map[$value['2']];
  579. if(! isset($category_parent[$tmp['product_category_id']])) return [false,'产品分类:' . $value['2'] . '下存在子分类,请将产品建与最底层分类下'];
  580. $parentsId = $category_parent[$tmp['product_category_id']];
  581. $tmp['product_category'] = json_encode($parentsId);
  582. $tmp['size'] = $value['3'];
  583. if($value['4']){
  584. if(empty($basic[$value['4']])) return [false, '单位:' . $value['4'] . '不存在'];
  585. $tmp['unit'] = $basic[$value['4']];
  586. }
  587. $tmp['bar_code'] = $value['5'];
  588. $tmp['cost'] = $value['6'] ?? 0;
  589. $tmp['retail_price'] = $value['7'] ?? 0;
  590. $tmp['product_attribute'] = $value['8'] ?? 0;
  591. $tmp['build_fee'] = $value['9'] ?? 0;
  592. $tmp['item_code'] = $value['10'] ?? "";
  593. foreach ($map as $m => $v){
  594. if($value[$v['col']]){
  595. if(! is_numeric($value[$v['col']])) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  596. $formattedNumber = number_format($value[$v['col']], 2, '.', '');
  597. if($formattedNumber != $value[$v['col']]) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  598. }
  599. $tmp[$m] = $value[$v['col']];
  600. }
  601. if(isset($product_array[$value['1']])){
  602. //更新
  603. $pro_tmp = $product_array[$value['1']] ?? [];
  604. $product_id = $pro_tmp['id'];
  605. //产品价格子表
  606. foreach ($tmp as $k => $v){
  607. if(strpos($k, 'table_id.') !== false){
  608. $tmp2 = [];
  609. $k_n = str_replace('table_id.', "", $k);
  610. $tmp2['product_id'] = $product_id;
  611. $tmp2['basic_type_id'] = $k_n;
  612. $tmp2['price'] = $v;
  613. // $tmp2['crt_time'] = $time;
  614. // $tmp2['upd_time'] = $time;
  615. $update2[] = $tmp2;
  616. unset($tmp[$k]);
  617. }
  618. }
  619. //安装费
  620. if($tmp['build_fee'] == "") unset($tmp['build_fee']);
  621. //商品编码
  622. if($tmp['item_code'] == "") unset($tmp['item_code']);
  623. //产品主表
  624. $update[$product_id] = $tmp;
  625. }else{
  626. $tmp['depart_id'] = $depart_id;
  627. $tmp['top_depart_id'] = $top_depart_id;
  628. $tmp['crt_id'] = $user['id'];
  629. $tmp['crt_time'] = $time;
  630. //产品价格子表
  631. foreach ($tmp as $k => $v){
  632. if(strpos($k, 'table_id.') !== false){
  633. $tmp2 = [];
  634. $k_n = str_replace('table_id.', "", $k);
  635. $tmp2['basic_type_id'] = $k_n;
  636. $tmp2['price'] = $v;
  637. // $tmp2['crt_time'] = $time;
  638. // $tmp2['upd_time'] = $time;
  639. $insert2[$tmp['code']][] = $tmp2;
  640. unset($tmp[$k]);
  641. }
  642. }
  643. //产品主表
  644. $insert[$tmp['code']] = $tmp;
  645. }
  646. }
  647. try{
  648. DB::beginTransaction();
  649. //新增
  650. if(! empty($insert)){
  651. Product::insert($insert);
  652. if(! empty($insert2)){
  653. $insert_detail = [];
  654. $last_insert_id = Product::where('crt_time',$time)
  655. ->pluck('id','code')
  656. ->toArray();
  657. foreach ($insert2 as $code => $val){
  658. foreach ($val as $v2){
  659. $v2['product_id'] = $last_insert_id[$code] ?? 0;
  660. $insert_detail[] = $v2;
  661. }
  662. }
  663. ProductPriceDetail::insert($insert_detail);
  664. }
  665. }
  666. //编辑
  667. if(! empty($update)){
  668. foreach ($update as $p_id => $value){
  669. Product::where('id',$p_id)
  670. ->update($value);
  671. }
  672. if(! empty($update2)){
  673. $product_id_array = array_keys($update);
  674. ProductPriceDetail::whereIn('product_id',$product_id_array)
  675. ->update(['del_time' => $time]);
  676. ProductPriceDetail::insert($update2);
  677. }
  678. }
  679. DB::commit();
  680. }catch (\Exception $e){
  681. DB::rollBack();
  682. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  683. }
  684. return [true, ''];
  685. }
  686. function convertHyphens($string) {
  687. // 定义要被替换的字符
  688. $search = array('-', '-', '—'); // 全角连字符、半角连字符 和 破折号
  689. $replace = '-'; // 替换成半角连字符
  690. // 执行替换
  691. $res = str_replace($search, $replace, $string);
  692. return $res;
  693. }
  694. function generateTitleToIdMap($tree, $prefix = '') {
  695. $result = [];
  696. foreach ($tree as $node) {
  697. $currentTitle = $prefix . $node['title'];
  698. $result[$currentTitle] = $node['id'];
  699. if (!empty($node['children'])) {
  700. $result = array_merge(
  701. $result,
  702. $this->generateTitleToIdMap($node['children'], $currentTitle . '-')
  703. );
  704. }
  705. }
  706. return $result;
  707. }
  708. function getAncestors($id, &$idMap) {
  709. if (!isset($idMap[$id])) return [];
  710. $ancestors = [$id];
  711. $parentId = $idMap[$id]['parent_id'];
  712. if ($parentId !== null) {
  713. // 递归调用自己来添加父级 ID
  714. $ancestors = array_merge($this->getAncestors($parentId, $idMap), $ancestors);
  715. }
  716. return $ancestors;
  717. }
  718. // 检查是否拥有子项
  719. function hasChildren($id, &$idMap) {
  720. foreach ($idMap as $item) {
  721. if ($item['parent_id'] == $id) {
  722. return true;
  723. }
  724. }
  725. return false;
  726. }
  727. public function salesOnlineImport($array, $user){
  728. $head = $user['head']['id'] ?? 0;
  729. // 去除表头
  730. unset($array[0]);
  731. if(empty($array)) return [false, '导入数据不能为空'];
  732. $basic = (new BasicTypeService())->getMyBasicList($user, [18,23,24,29]);
  733. $basic_list = [];
  734. foreach ($basic as $value){
  735. if($value['type'] == 18){
  736. $basic_list[9][$value['title']] = $value['id'];
  737. }elseif ($value['type'] == 23){
  738. $basic_list[10][$value['title']] = $value['id'];
  739. }elseif ($value['type'] == 24){
  740. $basic_list[3][$value['title']] = $value['id'];
  741. }elseif ($value['type'] == 29){
  742. $basic_list[2][$value['title']] = $value['id'];
  743. }
  744. }
  745. $search = "";
  746. $customer = [];
  747. foreach ($array as $key => $value){
  748. $rowData = array_filter($value);
  749. if (empty($rowData)) {
  750. unset($array[$key]);
  751. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || $value[6] === null) {
  752. return [false, '带*号的字段项必填'];
  753. }else{
  754. foreach ($value as $k => $v){
  755. $value[$k] = trim($v);
  756. }
  757. if(! isset($basic_list[2][$value[2]])) return [false, '客户简称:' . $value[2] .'不存在'];
  758. $value[2] = $basic_list[2][$value[2]];
  759. if(! isset($basic_list[3][$value[3]])) return [false, '店铺(平台类型):' . $value[3] .'不存在'];
  760. $value[3] = $basic_list[3][$value[3]];
  761. if(! is_numeric($value[5])) return [false, '货品数量请填写正确的数值'];
  762. if(! is_numeric($value[6])) return [false, '产品合同金额请填写正确的数值'];
  763. if(! empty($value[8]) && ! is_numeric($value[8])) return [false, '订单优惠金额请填写正确的数值'];
  764. if(! empty($value[9])){
  765. if(! isset($basic_list[9][$value[9]])) return [false, '安装方式:' . $value[9] .'不存在'];
  766. $value[9] = $basic_list[9][$value[9]];
  767. }
  768. if(! empty($value[10])){
  769. if(! isset($basic_list[10][$value[10]])) return [false, '安装地点:' . $value[10] .'不存在'];
  770. $value[10] = $basic_list[10][$value[10]];
  771. }
  772. if(! isset(SalesOrder::$order_type_name[$value[1]])) return [false, '产品类型填写错误'];
  773. $value[1] = SalesOrder::$order_type_name[$value[1]];
  774. if(! empty($value[11])) {
  775. list($status,$msg) = $this->changeAndReturnDate($value[11]);
  776. if(! $status) return [false,"施工日期请填写正确的日期格式,例如2023-05-01"];
  777. $value[11] = $msg;
  778. }
  779. if(! empty($value[12])) {
  780. list($status,$msg) = $this->changeAndReturnDate($value[12]);
  781. if(! $status) return [false,"交车日期请填写正确的日期格式,例如2023-05-01"];
  782. $value[12] = $msg;
  783. }
  784. $array[$key] = $value;
  785. $search .= "(code = '".$value[4]."') or";
  786. if(! empty($value[7]) && ! in_array($value[7], $customer)) $customer[] = $value[7];
  787. }
  788. }
  789. if(empty($array)) return [false, '导入数据不能为空'];
  790. $search = rtrim($search,' or');
  791. $search = "($search)";
  792. $model = Product::ProductClear($user,[]);
  793. $product = $model->whereRaw($search)
  794. ->where('del_time',0)
  795. ->select('title','id','code','cost','retail_price')
  796. ->get()->toArray();
  797. // $pro = (new ProductService())->productList(['product_id' => array_column($product,'id'), 'type' => 2],$user);
  798. $product_map = [];
  799. foreach ($product as $value){
  800. $product_map[$value['code']] = [
  801. 'id' => $value['id'],
  802. 'cost' => $value['cost'],
  803. 'retail_price' => $value['retail_price'],
  804. ];
  805. }
  806. $time = time();
  807. $model = Customer::Clear($user,[]);
  808. $customer_map = $model->where('del_time',0)
  809. ->whereIn('title',$customer)
  810. ->pluck('id','title')
  811. ->toArray();
  812. $customer_info = CustomerInfo::where('del_time',0)
  813. ->whereIn('customer_id',array_values($customer_map))
  814. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  815. ->select('customer_id','type','contact_info','data_id')
  816. ->orderBy('id','asc')
  817. ->get()->toArray();
  818. $customer_contact = $customer_man = [];
  819. foreach ($customer_info as $value){
  820. if($value['type'] == CustomerInfo::type_one && ! isset($customer_contact[$value['customer_id']])){
  821. $customer_contact[$value['customer_id']] = $value['contact_info'];
  822. }elseif ($value['type'] == CustomerInfo::type_two){
  823. $customer_man[$value['customer_id']][] = [
  824. 'type' => SalesOrderInfo::type_two,
  825. 'data_id' => $value['data_id'],
  826. 'crt_time' => $time,
  827. ];
  828. }
  829. }
  830. $tmp = [
  831. 'model_type' => SalesOrder::Model_type_four,
  832. 'sales_order_type' => 0,
  833. 'order_number' => '',
  834. 'customer_short_name' => '',
  835. 'plat_type' => '',
  836. 'plat_order' => '',
  837. 'sign_time' => $time,
  838. 'product_total' => 0,
  839. 'contract_fee' => 0,
  840. 'discount_fee' => 0,
  841. 'cdefine29' => '',//分社施工
  842. 'cdefine32' => '',//达人昵称
  843. 'cdefine30' => '',//业务员
  844. 'rate' => 100,
  845. 'depart_id' => $head,
  846. 'top_depart_id' => $head,
  847. 'crt_id' => $user['id'],
  848. 'crt_time' => $time,
  849. 'upd_time' => $time,
  850. ];
  851. $tmp_detail = [
  852. 'sales_order_id' => 0,
  853. 'product_id' => 0,
  854. 'cost' => 0,
  855. 'retail_price' => 0,
  856. 'basic_type_id' => 0,
  857. 'price' => 0,
  858. 'final_amount' => 0,
  859. 'number' => '',
  860. 'crt_time' => $time,
  861. ];
  862. $insert = $insert_detail = $insert_detail_man = [];
  863. $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
  864. foreach ($array as $value){
  865. $product_str = $value[4];
  866. if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[4]. ']' . '不存在'];
  867. $product_tmp = $product_map[$product_str] ?? [];
  868. $customer_tmp = 0;
  869. $customer_contact_tmp = "";
  870. $customer_man_tmp = [];
  871. if(! empty($value[7])){
  872. $customer_tmp = $customer_map[$value[7]] ?? 0;
  873. $customer_contact_tmp = $customer_contact[$customer_tmp] ?? "";
  874. $customer_man_tmp = $customer_man[$customer_tmp] ?? [];
  875. }
  876. $customer_man_tmp[] = [
  877. 'type' => SalesOrderInfo::type_one,
  878. 'data_id' => $user['id'],
  879. 'crt_time' => $time,
  880. ];
  881. $tmp['product_total'] = $tmp['contract_fee'] = 0;
  882. $keys = $value[0] . $value[1];
  883. $insert_detail_man[$keys] = $customer_man_tmp;
  884. if(! isset($insert[$keys])){
  885. $tmp['order_number'] = OrderNoService::createSalesOrderNumberImport($prefix);
  886. $tmp['sales_order_type'] = $value[1];
  887. $tmp['customer_short_name'] = $value[2];
  888. $tmp['customer_id'] = $customer_tmp;
  889. $tmp['customer_contact'] = $customer_contact_tmp;
  890. $tmp['plat_type'] = $value[3];
  891. $tmp['plat_order'] = $value[0];
  892. $tmp['discount_fee'] = $value[8] ?: 0;
  893. $tmp['cdefine29'] = $value[13] ?? "";//分社施工
  894. $tmp['cdefine32'] = $value[14] ?? "";//达人昵称
  895. $tmp['cdefine30'] = $value[15] ?? "";//业务员
  896. $tmp['product_total'] += $value[6];
  897. $tmp['contract_fee'] += $value[6] - $tmp['discount_fee'];
  898. $tmp['construction_time'] = $value[11] ?? 0;
  899. $tmp['handover_time'] = $value[12] ?? 0;
  900. $insert[$keys] = $tmp;
  901. }else{
  902. $insert[$keys]['product_total'] += $value[6];
  903. $insert[$keys]['contract_fee'] += $value[6];
  904. }
  905. $tmp_detail['product_id'] = $product_tmp['id'];
  906. $tmp_detail['cost'] = $product_tmp['cost'];
  907. $tmp_detail['retail_price'] = $product_tmp['retail_price'];
  908. $tmp_detail['price'] = $product_tmp['retail_price'];
  909. $tmp_detail['final_amount'] = $value[6];
  910. $tmp_detail['number'] = $value[5];
  911. $insert_detail[$keys][] = $tmp_detail;
  912. }
  913. $insert_detail = array_values($insert_detail);
  914. $insert_detail_man = array_values($insert_detail_man);
  915. try{
  916. DB::beginTransaction();
  917. if(! empty($insert)) SalesOrder::insert($insert);
  918. $last_insert_id = SalesOrder::where('crt_time',$time)
  919. ->select('id')
  920. ->get()->toArray();
  921. $last_insert_id = array_column($last_insert_id,'id');
  922. if(! empty($insert_detail)){
  923. $insert2 = [];
  924. foreach ($last_insert_id as $key => $value){
  925. if(isset($insert_detail[$key])) {
  926. foreach ($insert_detail[$key] as $val){
  927. $val['sales_order_id'] = $value;
  928. $insert2[] = $val;
  929. }
  930. }
  931. }
  932. SalesOrderProductInfo::insert($insert2);
  933. }
  934. if(! empty($insert_detail_man)){
  935. $insert3 = [];
  936. foreach ($last_insert_id as $key => $value){
  937. if(isset($insert_detail_man[$key])) {
  938. foreach ($insert_detail_man[$key] as $val){
  939. $val['sales_order_id'] = $value;
  940. $insert3[] = $val;
  941. }
  942. }
  943. }
  944. SalesOrderInfo::insert($insert3);
  945. }
  946. DB::commit();
  947. }catch (\Exception $e){
  948. DB::rollBack();
  949. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  950. }
  951. return [true, ''];
  952. }
  953. public function btOnlineImport($array, $user){
  954. $head = $user['head']['id'] ?? 0;
  955. // 去除表头
  956. unset($array[0]);
  957. if(empty($array)) return [false, '导入数据不能为空'];
  958. $basic = (new BasicTypeService())->getMyBasicList($user, [18,23,24,29]);
  959. $basic_list = [];
  960. foreach ($basic as $value){
  961. if($value['type'] == 18){
  962. $basic_list[9][$value['title']] = $value['id'];
  963. }elseif ($value['type'] == 23){
  964. $basic_list[10][$value['title']] = $value['id'];
  965. }elseif ($value['type'] == 24){
  966. $basic_list[3][$value['title']] = $value['id'];
  967. }elseif ($value['type'] == 29){
  968. $basic_list[2][$value['title']] = $value['id'];
  969. }
  970. }
  971. $search = "";
  972. $customer = [];
  973. foreach ($array as $key => $value){
  974. $rowData = array_filter($value);
  975. if (empty($rowData)) {
  976. unset($array[$key]);
  977. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || $value[6] === null) {
  978. return [false, '带*号的字段项必填'];
  979. }else{
  980. foreach ($value as $k => $v){
  981. $value[$k] = trim($v);
  982. }
  983. if(! isset($basic_list[2][$value[2]])) return [false, '客户简称:' . $value[2] .'不存在'];
  984. $value[2] = $basic_list[2][$value[2]];
  985. if(! isset($basic_list[3][$value[3]])) return [false, '店铺(平台类型):' . $value[3] .'不存在'];
  986. $value[3] = $basic_list[3][$value[3]];
  987. if(! is_numeric($value[5])) return [false, '货品数量请填写正确的数值'];
  988. if(! is_numeric($value[6])) return [false, '产品合同金额请填写正确的数值'];
  989. if(! empty($value[8]) && ! is_numeric($value[8])) return [false, '订单优惠金额请填写正确的数值'];
  990. if(! empty($value[9])){
  991. if(! isset($basic_list[9][$value[9]])) return [false, '安装方式:' . $value[9] .'不存在'];
  992. $value[9] = $basic_list[9][$value[9]];
  993. }
  994. if(! empty($value[10])){
  995. if(! isset($basic_list[10][$value[10]])) return [false, '安装地点:' . $value[10] .'不存在'];
  996. $value[10] = $basic_list[10][$value[10]];
  997. }
  998. if(! isset(SalesOrder::$order_type_name[$value[1]])) return [false, '产品类型填写错误'];
  999. $value[1] = SalesOrder::$order_type_name[$value[1]];
  1000. if(! empty($value[11])) {
  1001. list($status,$msg) = $this->changeAndReturnDate($value[11]);
  1002. if(! $status) return [false,"施工日期请填写正确的日期格式,例如2023-05-01"];
  1003. $value[11] = $msg;
  1004. }
  1005. if(! empty($value[12])) {
  1006. list($status,$msg) = $this->changeAndReturnDate($value[12]);
  1007. if(! $status) return [false,"交车日期请填写正确的日期格式,例如2023-05-01"];
  1008. $value[12] = $msg;
  1009. }
  1010. $array[$key] = $value;
  1011. $search .= "(code = '".$value[4]."') or";
  1012. if(! empty($value[7]) && ! in_array($value[7], $customer)) $customer[] = $value[7];
  1013. }
  1014. }
  1015. if(empty($array)) return [false, '导入数据不能为空'];
  1016. $search = rtrim($search,' or');
  1017. $search = "($search)";
  1018. $model = Product::ProductClear($user,[]);
  1019. $product = $model->whereRaw($search)
  1020. ->where('del_time',0)
  1021. ->select('title','id','code','cost','retail_price')
  1022. ->get()->toArray();
  1023. $product_map = [];
  1024. foreach ($product as $value){
  1025. $product_map[$value['code']] = [
  1026. 'id' => $value['id'],
  1027. 'cost' => $value['cost'],
  1028. 'retail_price' => $value['retail_price'],
  1029. ];
  1030. }
  1031. $time = time();
  1032. $model = Customer::Clear($user,[]);
  1033. $customer_map = $model->where('del_time',0)
  1034. ->whereIn('title',$customer)
  1035. ->pluck('id','title')
  1036. ->toArray();
  1037. $customer_info = CustomerInfo::where('del_time',0)
  1038. ->whereIn('customer_id',array_values($customer_map))
  1039. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  1040. ->select('customer_id','type','contact_info','data_id')
  1041. ->orderBy('id','asc')
  1042. ->get()->toArray();
  1043. $customer_contact = $customer_man = [];
  1044. foreach ($customer_info as $value){
  1045. if($value['type'] == CustomerInfo::type_one && ! isset($customer_contact[$value['customer_id']])){
  1046. $customer_contact[$value['customer_id']] = $value['contact_info'];
  1047. }elseif ($value['type'] == CustomerInfo::type_two){
  1048. $customer_man[$value['customer_id']][] = [
  1049. 'type' => SalesOrderInfo::type_two,
  1050. 'data_id' => $value['data_id'],
  1051. 'crt_time' => $time,
  1052. ];
  1053. }
  1054. }
  1055. $tmp = [
  1056. 'model_type' => SalesOrder::Model_type_seven,
  1057. 'sales_order_type' => 0,
  1058. 'order_number' => '',
  1059. 'customer_short_name' => '',
  1060. 'plat_type' => '',
  1061. 'plat_order' => '',
  1062. 'sign_time' => $time,
  1063. 'product_total' => 0,
  1064. 'contract_fee' => 0,
  1065. 'discount_fee' => 0,
  1066. 'cdefine29' => '',//分社施工
  1067. 'cdefine32' => '',//达人昵称
  1068. 'cdefine30' => '',//业务员
  1069. 'rate' => 100,
  1070. 'depart_id' => $head,
  1071. 'top_depart_id' => $head,
  1072. 'crt_id' => $user['id'],
  1073. 'crt_time' => $time,
  1074. 'upd_time' => $time,
  1075. ];
  1076. $tmp_detail = [
  1077. 'sales_order_id' => 0,
  1078. 'product_id' => 0,
  1079. 'cost' => 0,
  1080. 'retail_price' => 0,
  1081. 'basic_type_id' => 0,
  1082. 'price' => 0,
  1083. 'final_amount' => 0,
  1084. 'number' => '',
  1085. 'crt_time' => $time,
  1086. ];
  1087. $insert = $insert_detail = $insert_detail_man = [];
  1088. $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
  1089. foreach ($array as $value){
  1090. $product_str = $value[4];
  1091. if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[4]. ']' . '不存在'];
  1092. $product_tmp = $product_map[$product_str] ?? [];
  1093. $customer_tmp = 0;
  1094. $customer_contact_tmp = "";
  1095. $customer_man_tmp = [];
  1096. if(! empty($value[7])){
  1097. $customer_tmp = $customer_map[$value[7]] ?? 0;
  1098. $customer_contact_tmp = $customer_contact[$customer_tmp] ?? "";
  1099. $customer_man_tmp = $customer_man[$customer_tmp] ?? [];
  1100. }
  1101. $customer_man_tmp[] = [
  1102. 'type' => SalesOrderInfo::type_one,
  1103. 'data_id' => $user['id'],
  1104. 'crt_time' => $time,
  1105. ];
  1106. $tmp['product_total'] = $tmp['contract_fee'] = 0;
  1107. $keys = $value[0] . $value[1];
  1108. $insert_detail_man[$keys] = $customer_man_tmp;
  1109. if(! isset($insert[$keys])){
  1110. $tmp['order_number'] = OrderNoService::createSalesOrderNumberImport($prefix);
  1111. $tmp['sales_order_type'] = $value[1];
  1112. $tmp['customer_short_name'] = $value[2];
  1113. $tmp['customer_id'] = $customer_tmp;
  1114. $tmp['customer_contact'] = $customer_contact_tmp;
  1115. $tmp['plat_type'] = $value[3];
  1116. $tmp['plat_order'] = $value[0];
  1117. $tmp['discount_fee'] = $value[8] ?: 0;
  1118. $tmp['cdefine29'] = $value[13] ?? "";//分社施工
  1119. $tmp['cdefine32'] = $value[14] ?? "";//达人昵称
  1120. $tmp['cdefine30'] = $value[15] ?? "";//业务员
  1121. $tmp['product_total'] += $value[6];
  1122. $tmp['contract_fee'] += $value[6] - $tmp['discount_fee'];
  1123. $tmp['construction_time'] = $value[11] ?? 0;
  1124. $tmp['handover_time'] = $value[12] ?? 0;
  1125. $insert[$keys] = $tmp;
  1126. }else{
  1127. $insert[$keys]['product_total'] += $value[6];
  1128. $insert[$keys]['contract_fee'] += $value[6];
  1129. }
  1130. $tmp_detail['product_id'] = $product_tmp['id'];
  1131. $tmp_detail['cost'] = $product_tmp['cost'];
  1132. $tmp_detail['retail_price'] = $product_tmp['retail_price'];
  1133. $tmp_detail['price'] = $product_tmp['retail_price'];
  1134. $tmp_detail['final_amount'] = $value[6];
  1135. $tmp_detail['number'] = $value[5];
  1136. $insert_detail[$keys][] = $tmp_detail;
  1137. }
  1138. $insert_detail = array_values($insert_detail);
  1139. $insert_detail_man = array_values($insert_detail_man);
  1140. try{
  1141. DB::beginTransaction();
  1142. if(! empty($insert)) SalesOrder::insert($insert);
  1143. $last_insert_id = SalesOrder::where('crt_time',$time)
  1144. ->select('id')
  1145. ->get()->toArray();
  1146. $last_insert_id = array_column($last_insert_id,'id');
  1147. if(! empty($insert_detail)){
  1148. $insert2 = [];
  1149. foreach ($last_insert_id as $key => $value){
  1150. if(isset($insert_detail[$key])) {
  1151. foreach ($insert_detail[$key] as $val){
  1152. $val['sales_order_id'] = $value;
  1153. $insert2[] = $val;
  1154. }
  1155. }
  1156. }
  1157. SalesOrderProductInfo::insert($insert2);
  1158. }
  1159. if(! empty($insert_detail_man)){
  1160. $insert3 = [];
  1161. foreach ($last_insert_id as $key => $value){
  1162. if(isset($insert_detail_man[$key])) {
  1163. foreach ($insert_detail_man[$key] as $val){
  1164. $val['sales_order_id'] = $value;
  1165. $insert3[] = $val;
  1166. }
  1167. }
  1168. }
  1169. SalesOrderInfo::insert($insert3);
  1170. }
  1171. DB::commit();
  1172. }catch (\Exception $e){
  1173. DB::rollBack();
  1174. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  1175. }
  1176. return [true, ''];
  1177. }
  1178. public function lastJcImport($array, $user){
  1179. $head = $user['head']['id'] ?? 0;
  1180. // 去除表头
  1181. unset($array[0]);
  1182. if(empty($array)) return [false, '导入数据不能为空'];
  1183. $shop_name = $product_code = [];
  1184. foreach ($array as $key => $value){
  1185. $rowData = array_filter($value);
  1186. if (empty($rowData)) {
  1187. unset($array[$key]);
  1188. } elseif(empty($value[0]) || empty($value[1])) {
  1189. return [false, '带*号的字段项必填'];
  1190. }else{
  1191. foreach ($value as $k => $v){
  1192. $value[$k] = trim($v);
  1193. }
  1194. if(! is_numeric($value[2])) return [false, '* 上月结存数量请填写正确的数值'];
  1195. if(! is_numeric($value[3])) return [false, '* 上月结存单价请填写正确的数值'];
  1196. if(! is_numeric($value[4])) return [false, '* 上月结存金额请填写正确的数值'];
  1197. if(! in_array($value[0],$shop_name)) $shop_name[] = $value[0];
  1198. if(! in_array($value[1],$product_code)) $product_code[] = $value[1];
  1199. }
  1200. }
  1201. $model = Product::ProductClear($user,[]);
  1202. $product_map = $model->whereIn('code',$product_code)
  1203. ->where('del_time',0)
  1204. ->pluck('id','code')
  1205. ->toArray();
  1206. $depart_map = Depart::whereIn('title',$shop_name)
  1207. ->where('del_time',0)
  1208. ->pluck('id','title')
  1209. ->toArray();
  1210. $storehouse_map = Storehouse::whereIn('top_depart_id',array_values($depart_map))
  1211. ->where('del_time',0)
  1212. ->pluck('id','top_depart_id')
  1213. ->toArray();
  1214. $time = time();
  1215. $insert = [];
  1216. $last_month_stamp = strtotime(date('Y-m-t 23:59:59', strtotime('last month')));
  1217. foreach ($array as $value){
  1218. $depart_tmp = $depart_map[$value[0]] ?? 0;
  1219. if($depart_tmp <= 0) return [false, "门店:" . $value[0] . "不存在或已被删除"];
  1220. $product_tmp = $product_map[$value[1]] ?? 0;
  1221. if($product_tmp <= 0) return [false, "产品编码:" . $value[1] . "不存在或已被删除"];
  1222. $storehouse_tmp = $storehouse_map[$depart_tmp] ?? 0;
  1223. $insert[] = [
  1224. 'top_depart_id' => $depart_tmp,
  1225. 'product_id' => $product_tmp,
  1226. 'storehouse_id' => $storehouse_tmp,
  1227. 'number' => $value[2],
  1228. 'price' => $value[3],
  1229. 'total' => $value[4],
  1230. 'crt_time' => $time,
  1231. 'time' => $last_month_stamp
  1232. ];
  1233. }
  1234. if(empty($insert)) return [false, '暂无更新的数据'];
  1235. try{
  1236. DB::beginTransaction();
  1237. //更新数据为删除
  1238. foreach ($insert as $value){
  1239. LastJc::where('del_time',0)
  1240. ->where('time', $last_month_stamp)
  1241. ->where('top_depart_id',$value['top_depart_id'])
  1242. ->where('storehouse_id',$value['storehouse_id'])
  1243. ->where('product_id',$value['product_id'])
  1244. ->update(['del_time' => $time]);
  1245. }
  1246. //写入数据
  1247. LastJc::insert($insert);
  1248. DB::commit();
  1249. }catch (\Exception $e){
  1250. DB::rollBack();
  1251. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  1252. }
  1253. return [true, ''];
  1254. }
  1255. public function inventoryImport($array, $user){
  1256. $head = $user['head']['id'] ?? 0;
  1257. // 去除表头
  1258. unset($array[0]);
  1259. if(empty($array)) return [false, '导入数据不能为空'];
  1260. $shop_name = $product_code = $counted_number = [];
  1261. foreach ($array as $key => $value){
  1262. $rowData = array_filter($value);
  1263. if (empty($rowData)) {
  1264. unset($array[$key]);
  1265. } elseif(empty($value[0]) || empty($value[1])) {
  1266. if($key != 1){
  1267. if(empty($value[1])) return [false, '带*号的字段项必填'];
  1268. } else{
  1269. return [false, '带*号的字段项必填'];
  1270. }
  1271. }else{
  1272. foreach ($value as $k => $v){
  1273. $value[$k] = trim($v);
  1274. }
  1275. if($value[0] && ! in_array($value[0],$shop_name)) $shop_name[] = $value[0];
  1276. if(! in_array($value[1],$product_code)) {
  1277. $product_code[] = $value[1];
  1278. }else{
  1279. return [false, '产品编号:' . $value[1] .'已存在'];
  1280. }
  1281. if(! is_numeric($value[2])) return [false, '* 盘点数量请填写正确的数值'];
  1282. }
  1283. }
  1284. if(count($shop_name) != 1) return [false, '只允许单门店盘点'];
  1285. $model = Product::ProductClear($user,[]);
  1286. $product_map = $model->whereIn('code',$product_code)
  1287. ->where('del_time',0)
  1288. ->pluck('id','code')
  1289. ->toArray();
  1290. $depart_map = Depart::whereIn('title',$shop_name)
  1291. ->where('del_time',0)
  1292. ->pluck('id','title')
  1293. ->toArray();
  1294. $storehouse_map = Storehouse::whereIn('top_depart_id',array_values($depart_map))
  1295. ->where('del_time',0)
  1296. ->pluck('id','top_depart_id')
  1297. ->toArray();
  1298. $time = time();
  1299. $top_depart_id = $storehouse_id = 0;
  1300. $product_submit = $product_id = $sub = [];
  1301. foreach ($array as $value){
  1302. if($value[0]){
  1303. $depart_tmp = $depart_map[$value[0]] ?? 0;
  1304. if($depart_tmp <= 0) return [false, "门店:" . $value[0] . "不存在或已被删除"];
  1305. if(! $top_depart_id) $top_depart_id = $depart_tmp;
  1306. $storehouse_tmp = $storehouse_map[$depart_tmp] ?? 0;
  1307. if(! $storehouse_id) $storehouse_id = $storehouse_tmp;
  1308. }
  1309. $product_tmp = $product_map[$value[1]] ?? 0;
  1310. if($product_tmp <= 0) return [false, "产品编码:" . $value[1] . "不存在或已被删除"];
  1311. if(! $storehouse_id) return [false, '门店下仓库信息未找到'];
  1312. $key = $product_tmp . ',' . $storehouse_id;
  1313. if(! isset($product_submit[$key])) $product_submit[$key] = 0;
  1314. //总的产品id
  1315. $product_id[] = $product_tmp;
  1316. $sub[] = [
  1317. 'product_id' => $product_tmp,
  1318. 'storehouse_id' => $storehouse_id,
  1319. 'order_number' => "",
  1320. 'counted_num' => $value[2],
  1321. 'book_num' => 0,
  1322. 'final_num' => 0,
  1323. 'inventory_id' => 0
  1324. ];
  1325. }
  1326. //库存校验
  1327. $set = ProductInventorySet::where('top_depart_id',$top_depart_id)->where('del_time',0)->first();
  1328. if(empty($set)) return [false, '门店是否校验库存设置缺失'];
  1329. $set = $set->toArray();
  1330. $is_check_stock = $set['param_one'] <= 0 ? ProductInventorySet::type_one : $set['param_one'];
  1331. //获取库存
  1332. $inventory_map = [];
  1333. $inventory = ProductInventoryService::getRealStock($product_id, $storehouse_id);
  1334. foreach ($inventory as $value){
  1335. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  1336. $inventory_map[$key] = $value['real_number'];
  1337. }
  1338. $order_number = (new OrderNoService())->createOrderNumber(Inventory::prefix);
  1339. $product_submit_reduce = $product_reduce_id = [];
  1340. //盘点详情
  1341. foreach ($sub as $key => $value){
  1342. $sub[$key]['order_number'] = $order_number;
  1343. $keys = $value['product_id'] . ',' . $value['storehouse_id'];
  1344. if(isset($inventory_map[$keys])){
  1345. $sub[$key]['book_num'] = $inventory_map[$keys];
  1346. $num = bcsub($value['counted_num'], $inventory_map[$keys],2);
  1347. $sub[$key]['final_num'] = $num;
  1348. }else{
  1349. $sub[$key]['final_num'] = $value['counted_num'];
  1350. }
  1351. if($sub[$key]['final_num'] < 0) {
  1352. $product_reduce_id[] = $value['product_id'];
  1353. $product_submit_reduce[$keys] = $sub[$key]['final_num'];
  1354. }
  1355. }
  1356. //校验是否存在产品盘点
  1357. list($status, $msg) = (new InventoryService())->issetProduct(['top_depart_id' => $top_depart_id], $product_submit);
  1358. if(! $status) return [false, $msg];
  1359. if(! empty($product_submit_reduce)){
  1360. //校验库存
  1361. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_reduce_id, $product_submit_reduce);
  1362. if(! $status) return [false, $msg];
  1363. }
  1364. if(empty($sub)) return [false, '暂无需要写入的盘点数据'];
  1365. try{
  1366. DB::beginTransaction();
  1367. $inventory_model = new Inventory();
  1368. $inventory_model->order_number = $order_number;
  1369. $inventory_model->storehouse_id = $storehouse_id;
  1370. $inventory_model->counted_id = $user['id'];
  1371. $inventory_model->counted_time = $time;
  1372. $inventory_model->depart_id = $top_depart_id;
  1373. $inventory_model->top_depart_id = $top_depart_id;
  1374. $inventory_model->crt_id = $user['id'];
  1375. $inventory_model->save();
  1376. foreach ($sub as $key => $value){
  1377. $sub[$key]['inventory_id'] = $inventory_model->id;
  1378. }
  1379. //写入数据
  1380. InventorySub::insert($sub);
  1381. //单据创建时是否校验库存
  1382. (new CheckService())->orderInventoryInsert(['order_number' => $order_number, 'is_check_stock' => $is_check_stock]);
  1383. DB::commit();
  1384. }catch (\Exception $exception){
  1385. DB::rollBack();
  1386. if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
  1387. return [false, '网络波动,请重新操作!'];
  1388. }
  1389. return [false, $exception->getMessage() . $exception->getLine() . $exception->getCode()];
  1390. }
  1391. return [true, ''];
  1392. }
  1393. }