ImportService.php 61 KB

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