ImportService.php 63 KB

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