ImportService.php 61 KB

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