ImportService.php 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  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\BasicTypeAllUse;
  8. use App\Model\Customer;
  9. use App\Model\CustomerAccessFlat;
  10. use App\Model\CustomerInfo;
  11. use App\Model\CustomerPerFormance;
  12. use App\Model\CustomerRemain;
  13. use App\Model\CustomerRepeat;
  14. use App\Model\Depart;
  15. use App\Model\Employee;
  16. use App\Model\Inventory;
  17. use App\Model\InventorySub;
  18. use App\Model\LastJc;
  19. use App\Model\Product;
  20. use App\Model\ProductAdjustment;
  21. use App\Model\ProductAdjustmentSub;
  22. use App\Model\ProductCategory;
  23. use App\Model\ProductInventorySet;
  24. use App\Model\ProductItemCodeMessage;
  25. use App\Model\ProductPriceDetail;
  26. use App\Model\SalesOrder;
  27. use App\Model\SalesOrderInfo;
  28. use App\Model\SalesOrderProductInfo;
  29. use App\Model\Storehouse;
  30. use Illuminate\Support\Facades\DB;
  31. use Illuminate\Support\Facades\Log;
  32. use Maatwebsite\Excel\Facades\Excel;
  33. use PhpOffice\PhpSpreadsheet\IOFactory;
  34. use PhpOffice\PhpSpreadsheet\Shared\Date;
  35. class ImportService extends Service
  36. {
  37. public static $type = [
  38. 'product', //产品
  39. 'customer', //客户
  40. 'salesOnline', //线上订单
  41. 'btOnline', //补贴订单
  42. 'lastJc', //上月结存
  43. 'inventory', // 盘点
  44. 'productAdjustment', // 产品金额调整单
  45. 'customerPerformance', // 客资金额
  46. ];
  47. //写活的导入------------------- 暂时不好用
  48. //导入入口
  49. public function import($data,$user){
  50. if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
  51. if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
  52. if(empty($data['file'])) return [false,'导入文件不能为空'];
  53. //导入的数据并且校验写入
  54. list($status,$msg) = $this->importMain($data,$user);
  55. if(! $status) return [false, $msg];
  56. return [true, ''];
  57. }
  58. //主方法
  59. public function importMain($data,$user){
  60. //获取配置文件
  61. $config = "excel." . $data['type'];
  62. $config_array = config($config) ?? [];
  63. if(empty($config_array)) return [false, '配置文件不存在'];
  64. //(特殊 额外的表头数据)
  65. $config_array = $this->getTableTitle($config_array,$user,$data);
  66. //获取合并单元格范围
  67. $uploadedFile = $_FILES['file']['tmp_name']; // 获取上传文件的临时路径
  68. $spreadsheet = IOFactory::load($uploadedFile); // 加载上传的 Excel 文件
  69. $worksheet = $spreadsheet->getActiveSheet(); // 获取第一个工作表
  70. $mergedCells = $worksheet->getMergeCells(); // 获取单元格合并范围
  71. // 需要导入的公用数据
  72. $msg['user_id'] = $user['id'];
  73. $msg['depart_id'] = $this->getDepart($user);
  74. $msg['top_depart_id'] = $user['depart_map'][$msg['depart_id']] ?? 0;
  75. //导入
  76. $import = new Import();
  77. $import->setConfig($config_array, $mergedCells,$msg);
  78. \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
  79. //异常提示报错
  80. if($import->getMsg()) return [false, $import->getMsg()];
  81. return [true, ''];
  82. }
  83. //表头入口
  84. public function getTableTitle($config_array,$user,$data){
  85. if(! empty($config_array['dynamics_field'])){
  86. $func = $config_array['dynamics_field']['func'];
  87. return $this->$func($config_array,$user,$data);
  88. }
  89. return $config_array;
  90. }
  91. //产品导入的额外表头
  92. public function productTitle($config_array,$user,$data){
  93. $model = BasicType::TopClear($user,$data);
  94. $result = $model->whereRaw('type = 22 And del_time = 0')->get()->toArray();
  95. if(! empty($result)){
  96. foreach ($result as $value){
  97. $config_array['field'][$value['title']] = [
  98. "key" => $config_array['dynamics_field']['name'],
  99. "key_array" => [
  100. "basic_type_id" => $value['id'],
  101. "price" => 0,
  102. ],
  103. "rule" => "",
  104. "other_rule" => "is_numeric",
  105. "multiple" => true,
  106. "map" => [
  107. $value['title'] => "price",
  108. ],
  109. ];
  110. }
  111. }
  112. return $config_array;
  113. }
  114. //产品导入的额外数据
  115. public function fillInsertProductData($time){
  116. $last_insert_data = Product::where('crt_time',$time)
  117. ->where('del_time',0)
  118. ->select("id","product_category_id")
  119. ->get()->toArray();
  120. if(empty($last_insert_data)) return;
  121. $list = ProductCategory::where('del_time',0)
  122. ->select('id','parent_id')
  123. ->get()->toArray();
  124. foreach ($last_insert_data as $value){
  125. $parentsId = $this->findParentIds($value['product_category_id'], $list);
  126. array_unshift($parentsId, $value['product_category_id']);
  127. $result = array_reverse($parentsId);
  128. Product::where('id',$value['id'])->update([
  129. 'product_category' => json_encode($result)
  130. ]);
  131. }
  132. }
  133. //写活的导入------------------- 暂时不好用
  134. //写死的导入
  135. public function getTableTitleXls($data,$user){
  136. if(empty($data['type'])) return [false,'缺少类型'];
  137. if(! in_array($data['type'],self::$type)) return [false,'类型不存在'];
  138. //获取配置文件
  139. $fuc = $data['type'];
  140. list($status,$msg,$filename) = $this->$fuc($data,$user);
  141. if(!$status) return [false, $msg];
  142. $headers = array_column($msg,'value');
  143. Excel::store(new TableHeadExport([], $headers),"/public/export/{$filename}", null, 'Xlsx', []);
  144. return [true, ['file' => $filename]];
  145. }
  146. private function customer($data,$user){
  147. //生成下载文件
  148. $filename = "客户模板_" . time() . '.' . 'xlsx';
  149. //获取配置文件
  150. $config = "excel.customerTable";
  151. $config_array = config($config) ?? [];
  152. if(empty($config_array)) return [false, '配置文件不存在',''];
  153. return [true, $config_array,$filename];
  154. }
  155. private function product($data,$user){
  156. //获取配置文件
  157. $config = "excel.productTable";
  158. $config_array = config($config) ?? [];
  159. if(empty($config_array)) return [false, '配置文件不存在', ''];
  160. $result = (new BasicTypeService())->getMyBasicList($user, 22);
  161. if(! empty($result)){
  162. foreach ($result as $value){
  163. $config_array[] = [
  164. 'key' => 'table_id.' . $value['id'],
  165. 'value' => $value['title'],
  166. ];
  167. }
  168. }
  169. //生成下载文件
  170. $filename = "产品模板_" . time() . '.' . 'xlsx';
  171. return [true, $config_array,$filename];
  172. }
  173. private function salesOnline($data,$user){
  174. //生成下载文件
  175. $filename = "线上订单模板_" . time() . '.' . 'xlsx';
  176. //获取配置文件
  177. $config = "excel.salesOnlineTable";
  178. $config_array = config($config) ?? [];
  179. if(empty($config_array)) return [false, '配置文件不存在',''];
  180. return [true, $config_array,$filename];
  181. }
  182. private function btOnline($data,$user){
  183. //生成下载文件
  184. $filename = "补贴订单模板_" . time() . '.' . 'xlsx';
  185. //获取配置文件
  186. $config = "excel.salesOnlineTable";
  187. $config_array = config($config) ?? [];
  188. if(empty($config_array)) return [false, '配置文件不存在',''];
  189. return [true, $config_array,$filename];
  190. }
  191. private function lastJc($data,$user){
  192. //生成下载文件
  193. $filename = "上月结存数据更新模板_" . time() . '.' . 'xlsx';
  194. //获取配置文件
  195. $config = "excel.lastJc";
  196. $config_array = config($config) ?? [];
  197. if(empty($config_array)) return [false, '配置文件不存在',''];
  198. return [true, $config_array,$filename];
  199. }
  200. private function inventory($data,$user){
  201. //生成下载文件
  202. $filename = "盘点单导入模板_" . time() . '.' . 'xlsx';
  203. //获取配置文件
  204. $config = "excel.inventory";
  205. $config_array = config($config) ?? [];
  206. if(empty($config_array)) return [false, '配置文件不存在',''];
  207. return [true, $config_array,$filename];
  208. }
  209. private function productAdjustment($data,$user){
  210. //生成下载文件
  211. $filename = "产品金额调整单导入模板_" . time() . '.' . 'xlsx';
  212. //获取配置文件
  213. $config = "excel.productAdjustment";
  214. $config_array = config($config) ?? [];
  215. if(empty($config_array)) return [false, '配置文件不存在',''];
  216. return [true, $config_array,$filename];
  217. }
  218. private function customerPerformance($data,$user){
  219. //生成下载文件
  220. $filename = "客资业绩导入模板_" . time() . '.' . 'xlsx';
  221. //获取配置文件
  222. $config = "excel.customerPerformanceTable";
  223. $config_array = config($config) ?? [];
  224. if(empty($config_array)) return [false, '配置文件不存在',''];
  225. return [true, $config_array,$filename];
  226. }
  227. //导入入口
  228. public function importAll($data,$user){
  229. // //不超时
  230. // ini_set('max_execution_time', 0);
  231. // //内存设置
  232. // ini_set('memory_limit', -1);
  233. // $reader = IOFactory::createReader('Xlsx');
  234. // $reader->setReadDataOnly(true); // 只读取有数据的单元格
  235. // $spreadsheet = $reader->load($data['file']);
  236. // dd($spreadsheet);
  237. // // 创建一个Reader对象
  238. // $reader = IOFactory::createReader('Xlsx'); // 根据你的文件格式选择合适的reader
  239. //
  240. //// 加载Excel文件
  241. // $spreadsheet = $reader->load($data['file']);
  242. //
  243. //// 获取第一个工作表
  244. // $worksheet = $spreadsheet->getActiveSheet();
  245. //
  246. //// 获取总行数
  247. // $totalRows = $worksheet->getHighestRow();dd($totalRows);
  248. if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
  249. if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
  250. if(empty($data['file'])) return [false,'导入文件不能为空'];
  251. try {
  252. $import = new ImportAll();
  253. //设置导入人id
  254. $import->setCrt($user['id']);
  255. $import->setUser($user);
  256. $import->setType($data['type']);
  257. $other = $data;
  258. unset($other['file']);
  259. $import->setOtherParam($other);
  260. //导入
  261. \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
  262. if($import->getMsg()) {
  263. $bool = $import->getIsLongText();
  264. if($bool) {
  265. return [0, $import->getMsg()];
  266. }else{
  267. return [false, $import->getMsg()];
  268. }
  269. }
  270. }catch (\Throwable $exception) {
  271. return [false, $exception->getMessage() . ' (Code: ' . $exception->getCode() . ', Line: ' . $exception->getLine() . ')'];
  272. }
  273. return [true, ''];
  274. }
  275. public function customerImport($array, $user){
  276. $time = time();
  277. $head = $user['depart_top'][0] ?? [];
  278. $head = $head['depart_id'] ?? 0;
  279. if(empty($head)) return [false, '导入异常错误,门店信息丢失'];
  280. // 去除表头
  281. unset($array[0]);
  282. if(empty($array)) return [false, '导入数据不能为空'];
  283. //第一次表格数据校验 非空 已经过滤数据
  284. list($status, $msg) = $this->customerFilterFirst($array, $time);
  285. if(! $status) return [0, $msg];
  286. list($contact_info, $fz, $fp) = $msg;
  287. if(empty($array)) return [false, '导入数据不能为空'];
  288. //客户
  289. $model = Customer::Clear($user,[]);
  290. $customer = $model->where('del_time',0)
  291. ->whereIn('title',array_unique(array_column($array,'1')))
  292. ->pluck('id','title')
  293. ->toArray();
  294. $basic = (new BasicTypeService())->getMyBasicList($user, [1,2,3,4,5,9,10,30]);
  295. $basic_list = [];
  296. foreach ($basic as $value){
  297. if($value['type'] == 1){
  298. $basic_list[2][$value['title']] = $value['id'];
  299. }elseif ($value['type'] == 2){
  300. $basic_list[3][$value['title']] = $value['id'];
  301. }elseif ($value['type'] == 3){
  302. $basic_list[41][$value['title']] = $value['id'];
  303. }elseif ($value['type'] == 30){
  304. $basic_list[42][$value['title']] = $value['id'];
  305. }elseif ($value['type'] == 4){
  306. $basic_list[12][$value['title']] = $value['id'];
  307. }elseif ($value['type'] == 5){
  308. $basic_list[7][$value['title']] = $value['id'];
  309. }elseif ($value['type'] == 9){
  310. $basic_list[8][$value['title']] = $value['id'];
  311. }elseif ($value['type'] == 10){
  312. $basic_list[9][$value['title']] = $value['id'];
  313. }
  314. }
  315. // $model = Product::ProductClear($user,[]);
  316. // $product = $model->where('del_time',0)
  317. // ->whereIn('title',array_unique(array_column($array,'6')))
  318. // ->pluck('id','title')
  319. // ->toArray();
  320. $basic_use = BasicTypeAllUse::where('del_time',0)
  321. ->where('type', BasicTypeAllUse::type_one)
  322. ->whereIn('title',array_unique(array_column($array,'5')))
  323. ->pluck('id','title')
  324. ->toArray();
  325. $empList = Employee::where('del_time',0)
  326. ->when(! empty($fz), function($query) use($fz) {
  327. return $query->where(function ($q) use ($fz) {
  328. $q->whereIn('emp_name', $fz)
  329. ->orWhereIn("number", $fz);
  330. });
  331. })
  332. ->select('id','number','emp_name as name')
  333. ->get()->toArray();
  334. $emp_map = $emp_map1 = [];
  335. foreach ($empList as $value){
  336. $emp_map[$value['name']][] = $value['id'];
  337. $emp_map1[$value['number']] = $value['id'];
  338. }
  339. $departList = Depart::where('del_time',0)
  340. ->where('parent_id', 0)
  341. ->whereIn('title', $fp)
  342. ->select('title','id','notify_id')
  343. ->get()->toArray();
  344. $depart = array_column($departList,null,'title');
  345. $top_depart_id = $user['depart_top'][0] ?? [];
  346. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  347. $contact_info_array = CustomerInfo::from('customer_info as a')
  348. ->join('customer as b','b.id','a.customer_id')
  349. ->where('a.del_time',0)
  350. ->where('b.del_time',0)
  351. ->where('b.top_depart_id',$top_depart_id)
  352. ->whereIn('a.contact_info', $contact_info)
  353. ->select('a.contact_info')->get()->toArray();
  354. $contact_info_array = array_column($contact_info_array,'contact_info');
  355. list($status, $msg) = $this->customerFilterSecond($array,$time,$head,$user,$contact_info_array,$customer,$basic_list,$emp_map,$emp_map1,$basic_use,$depart);
  356. if(! $status) return [0, $msg];
  357. list($insert, $insert_detail, $insert_detail2, $insert_detail3) = $msg;
  358. try{
  359. DB::beginTransaction();
  360. if(! empty($insert)) Customer::insert($insert);
  361. //获取上一次所有id
  362. $last_insert_id = Customer::where('crt_time',$time)
  363. ->where('crt_time',$time)
  364. ->where('depart_id',$head)
  365. ->where('top_depart_id',$head)
  366. ->where('crt_id',$user['id'])
  367. ->select('id')->get()->toArray();
  368. $last_insert_id = array_column($last_insert_id,'id');
  369. //组织数据 写入与主表的关联id
  370. $insert_detail_1 = [];
  371. foreach ($insert_detail as $key => $value){
  372. if(empty($value['contact_type']) && empty($value['contact_info'])) continue;
  373. $value['customer_id'] = $last_insert_id[$key];
  374. $insert_detail_1[] = $value;
  375. }unset($insert_detail);
  376. $insert_detail_2 = [];
  377. foreach ($insert_detail2 as $key => $value){
  378. if(empty($value['data_id'])) continue;
  379. $value['customer_id'] = $last_insert_id[$key];
  380. $insert_detail_2[] = $value;
  381. }unset($insert_detail2);
  382. $insert_detail_3 = [];
  383. foreach ($insert_detail3 as $key => $value){
  384. $value['customer_id'] = $last_insert_id[$key];
  385. $insert_detail_3[] = $value;
  386. }unset($insert_detail3);
  387. if(! empty($insert_detail_1)) CustomerInfo::insert($insert_detail_1);
  388. if(! empty($insert_detail_2)) {
  389. CustomerInfo::insert($insert_detail_2);
  390. // 新增
  391. $insertArray = CustomerInfo::whereIn('customer_id', $last_insert_id)
  392. ->where('crt_time', $time)
  393. ->whereIn('type', [CustomerInfo::type_two])
  394. ->select('id as source_id',DB::raw(CustomerAccessFlat::source_type_one .' as source_type'), 'customer_id','data_id as target_id',DB::raw(CustomerAccessFlat::type_one .' as type'))
  395. ->get()
  396. ->toArray();
  397. foreach ($insertArray as $key => $value){
  398. $insertArray[$key]['top_depart_id'] = $head;
  399. }
  400. if(! empty($insertArray)) CustomerAccessFlat::insert($insertArray);
  401. }
  402. if(! empty($insert_detail_3)) CustomerRemain::insert($insert_detail_3);
  403. DB::commit();
  404. }catch (\Exception $e){
  405. DB::rollBack();
  406. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  407. }
  408. return [true, ''];
  409. }
  410. function customerFilterFirst(&$array, $time){
  411. $array_clean = $contact_info = $fz = $fp =[];
  412. $error = [];
  413. foreach ($array as $key => $value){
  414. $line = '第' . $key . '行';
  415. $rowData = array_filter($value);
  416. if (empty($rowData)) {
  417. unset($array[$key]);
  418. } else{
  419. foreach ($value as $k => $v){
  420. $value[$k] = trim($v);
  421. }
  422. if(empty($value[0])){
  423. $error[] = $line . '客户模板字段项必填';
  424. }else{
  425. if(! isset(Customer::dk[$value[0]])) {
  426. $error[] = $line . '客户模板填写错误';
  427. }else{
  428. $value[0] = Customer::dk[$value[0]];
  429. }
  430. }
  431. if(empty($value[1])){
  432. $error[] = $line . '客户名称字段项必填';
  433. }else{
  434. if(in_array($value[1],$array_clean)) {
  435. $error[] = $line . '客户名称' . $value[1] .'在导入文件中重复出现';
  436. }else{
  437. $array_clean[] = $value[1];
  438. }
  439. }
  440. if(! empty($value[12])){
  441. if(in_array($value[12],$contact_info)) {
  442. $error[] = $line . '联系方式内容'. $value[12] .'在导入文件内重复出现';
  443. }else{
  444. $contact_info[] = $value[12];
  445. }
  446. }
  447. if(! empty($value[14])){
  448. list($status, $msg) = $this->convertExcelCellToDate($value[14]);
  449. if(! $status) {
  450. $error[] = $line . $msg;
  451. }else{
  452. $value[14] = $msg;
  453. }
  454. } else{
  455. $value[14] = $time;
  456. }
  457. if(empty($value[15])) {
  458. if(empty($value[13])) {
  459. $error[] = $line . '(分配门店为空时)负责人名或负责人工号必须填写';
  460. }else{
  461. if(! in_array($value[13], $fz)) $fz[] = $value[13];
  462. }
  463. }else{
  464. if(! in_array($value[15], $fp)) $fp[] = $value[15];
  465. }
  466. $array[$key] = $value;
  467. }
  468. }unset($array_clean);
  469. if(! empty($error)){
  470. $error = implode(',', $error);
  471. return [false, $error];
  472. }
  473. return [true, [$contact_info, $fz, $fp]];
  474. }
  475. function customerFilterSecond($array, $time, $head, $user, $contact_info_array,$customer,$basic_list,$emp_name,$emp_number,$basic_use,$depart){
  476. $error = $insert = $insert_detail = $insert_detail2 = $insert_detail3 = $repeat_array = [];
  477. foreach ($array as $key => $value){
  478. $line = '第' . $key . '行';
  479. $tmp = [
  480. 'model_type' => '',
  481. 'title' => '',
  482. 'customer_intention' => '',
  483. 'customer_from' => '',
  484. 'customer_type' => '',
  485. 'car_type' => '',
  486. 'consulting_product_new' => '',
  487. // 'intention_product' => '',
  488. 'progress_stage' => '',
  489. 'state_type' => '',
  490. 'address2' => '',
  491. 'mark' => '',
  492. 'depart_id' => $head,
  493. 'top_depart_id' => $head,
  494. 'crt_id' => $user['id'],
  495. 'crt_time' => $time,
  496. 'upd_time' => $time,
  497. 'enter_time' => $value['14'],
  498. ];
  499. $tmp['model_type'] = $value['0'];
  500. $tmp['mark'] = $value['9'];
  501. $tmp['address2'] = $value['10'];
  502. $repeat = 0;//是否重复
  503. if(! empty($customer[$value['1']])) {
  504. // $error[] = $line . '客户名称:' . $value['1'] . '已存在';
  505. // $repeat = 1;
  506. }else{
  507. $tmp['title'] = $value['1'];
  508. }
  509. if($value['2']){
  510. if(empty($basic_list[2][$value['2']])) {
  511. $error[] = $line . '客户意向度:' . $value['2'] . '不存在';
  512. }else{
  513. $tmp['customer_intention'] = $basic_list[2][$value['2']];
  514. }
  515. }
  516. if($value['3']){
  517. if(empty($basic_list[3][$value['3']])) {
  518. $error[] = $line . '客户来源:' . $value['3'] . '不存在';
  519. }else{
  520. $tmp['customer_from'] = $basic_list[3][$value['3']];
  521. }
  522. }
  523. if($value['4']){
  524. $keys = 4 . $value[0];
  525. $model_title = Customer::dk2[$value[0]] ?? "";
  526. if(empty($basic_list[$keys][$value['4']])) {
  527. $error[] = $line . '模板:' . $model_title . '下客户类别:' . $value['4'] . '不存在';
  528. }else{
  529. $tmp['customer_type'] = $basic_list[$keys][$value['4']];
  530. }
  531. }
  532. if($value['5']){
  533. if(empty($basic_use[$value['5']])) {
  534. $error[] = $line . '咨询产品:' . $value['5'] . '不存在';
  535. }else{
  536. $tmp['consulting_product_new'] = $basic_use[$value['5']];
  537. }
  538. }
  539. // if($value['6']){
  540. // if(empty($product[$value['6']])) {
  541. // $error[] = $line . '意向产品:' . $value['6'] . '不存在';
  542. // }else{
  543. // $tmp['intention_product'] = $product[$value['6']];
  544. // }
  545. // }
  546. if($value['6']){
  547. if(empty($basic_list[7][$value['6']])) {
  548. $error[] = $line . '进展阶段:' . $value['6'] . '不存在';
  549. }else{
  550. $tmp['progress_stage'] = $basic_list[7][$value['6']];
  551. }
  552. }
  553. if($value['7']){
  554. if(empty($basic_list[8][$value['7']])) {
  555. $error[] = $line . '状态:' . $value['7'] . '不存在';
  556. }else{
  557. $tmp['state_type'] = $basic_list[8][$value['7']];
  558. }
  559. }
  560. if($value['8']){
  561. if(empty($basic_list[9][$value['8']])) {
  562. $error[] = $line . '车型:' . $value['8'] . '不存在';
  563. }else{
  564. $tmp['car_type'] = $basic_list[9][$value['8']];
  565. }
  566. }
  567. $contact_id = 0;
  568. if($value['11']){
  569. if(empty($basic_list[12][$value['11']])) {
  570. $error[] = $line . '联系方式类型:' . $value['11'] . '不存在';
  571. }else{
  572. $contact_id = $basic_list[12][$value['11']];
  573. }
  574. }
  575. if($value['12'] && in_array($value['12'],$contact_info_array)){
  576. $repeat = 1;
  577. $error[] = $line . '联系方式内容:' . $value['12'] . '已存在';
  578. }
  579. $fp = $man = 0;
  580. if(empty($value['15'])){
  581. //分配门店为空 负责人必填
  582. list($return_man, $return_error) = $this->returnMan($emp_name, $emp_number, $value['13'], $line);
  583. if(! empty($return_error)) $error[] = $return_error;
  584. if(! empty($return_man)) $man = $return_man;
  585. }else{
  586. if(empty($depart[$value['15']])) {
  587. $error[] = $line . '分配门店:' . $value['15'] . '不存在';
  588. }else{
  589. $d_t = $depart[$value['15']];
  590. $fp = $d_t['id']; //门店id
  591. $fp_man = $d_t['notify_id']; //门店负责人
  592. if(empty($fp_man)) {
  593. $error[] = $line . '分配门店:' . $value['15'] . '门店负责人暂未设置';
  594. }else{
  595. if(! empty($value['13'])){
  596. list($return_man, $return_error) = $this->returnMan($emp_name, $emp_number, $value['13'], $line);
  597. if(! empty($return_error)) $error[] = $return_error;
  598. if(! empty($return_man)) $man = $return_man;
  599. if($fp_man != $man) $error[] = $line . '分配门店:' . $value['15'] . '门店负责人与用户填写的负责人不一致';
  600. }
  601. }
  602. $man = $fp_man;
  603. }
  604. }
  605. $insert[] = $tmp;
  606. $insert_detail[] = [
  607. 'customer_id' => 0,
  608. 'contact_type' => $contact_id,
  609. 'contact_info' => $value['12'],
  610. 'crt_time' => $time,
  611. 'type' => CustomerInfo::type_one
  612. ];
  613. $insert_detail2[] = [
  614. 'customer_id' => 0,
  615. 'data_id' => $man,
  616. 'crt_time' => $time,
  617. 'type' => CustomerInfo::type_two
  618. ];
  619. $insert_detail3[] = [
  620. 'customer_id' => 0,
  621. 'fp' => $fp,
  622. 'fz' => $man,
  623. 'crt_time' => $time,
  624. ];
  625. if($repeat){
  626. $tmp['contact_type'] = $contact_id;
  627. $tmp['contact'] = $value['12'];
  628. $tmp['fz_man'] = $man;
  629. $repeat_array[] = $tmp;
  630. }
  631. }
  632. //重复录入数据记录
  633. if($repeat_array) CustomerRepeat::insert($repeat_array);
  634. if(! empty($error)){
  635. $error = implode(',', $error);
  636. return [false, $error];
  637. }
  638. return [true, [$insert, $insert_detail, $insert_detail2, $insert_detail3]];
  639. }
  640. function returnMan($emp_name, $emp_number, $value, $line){
  641. $return = [];
  642. $type = 0;
  643. if(! empty($emp_name[$value])){
  644. $return = $emp_name[$value];
  645. $type = 1;
  646. }else{
  647. if(! empty($emp_number[$value])) $return = $emp_number[$value];
  648. }
  649. $man = 0;$error = "";
  650. if($type){
  651. if(count($return) > 1) {
  652. $error = $line . '负责人名:' . $value . '在系统中多次出现,请填写工号精准定位负责人';
  653. }else{
  654. $man = $return[0];
  655. }
  656. }else{
  657. if(! empty($return)){
  658. $man = $return;
  659. }else{
  660. $error = $line . '负责人工号:' . $value . '不存在';
  661. }
  662. }
  663. return [$man, $error];
  664. }
  665. function convertExcelCellToDate($cellValue) {
  666. // 尝试将单元格值转换为浮点数(Excel 日期序列号)
  667. $excelTimestamp = filter_var($cellValue, FILTER_VALIDATE_FLOAT);
  668. if ($excelTimestamp !== false && $excelTimestamp > 0) {
  669. // 如果成功转换并且值大于0,则认为是Excel日期序列号
  670. try {
  671. $dateTimeObject = Date::excelToDateTimeObject($cellValue);
  672. if ($dateTimeObject->format('H:i:s') === '00:00:00') {
  673. // 如果是,则将时间设置为 '23:59:00'
  674. $dateTimeObject->setTime(23, 59);
  675. }
  676. // 现在你可以格式化这个日期了
  677. $formattedDate = $dateTimeObject->format('Y-m-d H:i:s');
  678. if(! strtotime($formattedDate)) return [false, '录入日期请填写正确的日期'];
  679. return [true, strtotime($formattedDate)];
  680. } catch (\Exception $e) {
  681. // 处理转换失败的情况
  682. return [false, '单元格日期格式转换时间戳失败'];
  683. }
  684. }
  685. // 如果不是有效的浮点数,则尝试按照多种日期格式解析
  686. if(! strtotime($cellValue)) return [false, '单元格文本格式转换时间戳失败'];
  687. return [true, strtotime($cellValue)];
  688. }
  689. public function productImport($array, $user){
  690. //当前门店
  691. $depart_id = $this->getDepart($user);
  692. $top_depart_id = $user['depart_map'][$depart_id] ?? 0;
  693. // 去除表头
  694. $upload = $array[0];
  695. unset($array[0]);
  696. if(empty($array)) return [false, '导入数据不能为空'];
  697. //第一次表格数据校验 非空 已经过滤数据
  698. $array_clean = [];
  699. $search = "";
  700. $map_attr = array_flip(Product::$product_attribute);
  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])) {
  706. return [false, '带*号的字段项必填'];
  707. }else{
  708. foreach ($value as $k => $v){
  709. $value[$k] = trim($v);
  710. }
  711. $t = $value[1];
  712. if(in_array($t,$array_clean)) return [false, '产品编码:'. $value[1] .'在文件中重复出现'];
  713. $array_clean[] = $t;
  714. if(! empty($value[8])){
  715. if(! isset($map_attr[$value[8]])) return [false, '产品属性不存在' . $value[8]];
  716. $value[8] = $map_attr[$value[8]];
  717. }else{
  718. $value[8] = Product::Product_attribute_zero;
  719. }
  720. $array[$key] = $value;
  721. $search .= "(binary code ='".$value[1]."') or";
  722. }
  723. }unset($array_clean);
  724. if(empty($array)) return [false, '导入数据不能为空'];
  725. $search = rtrim($search,' or');
  726. $search = "($search)";
  727. $product = Product::whereRaw($search)
  728. ->where('del_time',0)
  729. ->select('code','top_depart_id','id')
  730. ->get()->toArray();
  731. $product_array = [];
  732. foreach ($product as $value){
  733. $product_array[$value['code']] = [
  734. 'id' => $value['id'],
  735. 'top_depart_id' => $value['top_depart_id']
  736. ];
  737. }
  738. //产品分类
  739. $category_list = ProductCategory::where('del_time',0)
  740. ->select('id','title','parent_id')
  741. ->where('top_depart_id',$top_depart_id)
  742. ->get()->toArray();
  743. // 创建一个以 id 为键的数据映射
  744. $idMap = [];
  745. foreach ($category_list as $item) {
  746. $idMap[$item['id']] = $item;
  747. }
  748. // 创建最终的结果数组
  749. $category_parent = [];
  750. foreach ($idMap as $id => $item) {
  751. // 如果没有子项,则处理
  752. if (! $this->hasChildren($id, $idMap)) {
  753. $category_parent[$id] = $this->getAncestors($id, $idMap);
  754. }
  755. }
  756. $category_map = array_column($category_list,'id','title');
  757. //基础类型
  758. $basic = (new BasicTypeService())->getMyBasicList($user, 20);
  759. $basic = array_column($basic,'id','title');
  760. $time = time();
  761. //表头
  762. $table_head = $this->product([],$user);
  763. $heads = $table_head[1];
  764. $head_value = array_column($heads,'value');
  765. if($head_value !== $upload) return [false, '产品模板错误,请重新下载模板导入'];
  766. //写入数据库的字段
  767. $tmp = array_column($heads,'key');
  768. $tmp = array_fill_keys($tmp, '');
  769. $tmp['product_category'] = '';
  770. $tmp['upd_time'] = $time;
  771. $top_message = Depart::where('parent_id',0)
  772. ->pluck('title','id')
  773. ->toArray();
  774. $upload = array_flip($upload);
  775. $map = [];
  776. foreach ($heads as $value){
  777. if(strpos($value['key'], 'table_id.') !== false && isset($upload[$value['value']])){
  778. $map[$value['key']] = [
  779. 'col' => $upload[$value['value']],
  780. 'name' => $value['value']
  781. ];
  782. }
  783. }
  784. $array = array_values($array);
  785. $insert = $insert2 = $insert_detail = $update = $update2 = $update_detail = [];
  786. foreach ($array as $value){
  787. if(isset($product_array[$value['1']])){
  788. $pro_tmp = $product_array[$value['1']] ?? [];
  789. if($pro_tmp['top_depart_id'] != $top_depart_id){
  790. $belong = $top_message[$pro_tmp['top_depart_id']] ?? "";
  791. $now = $top_message[$top_depart_id] ?? "";
  792. return [false, '产品编码:' . $value['1'] . '属于门店:' . $belong . ',当前门店:' . $now . ',不允许跨门店操作更新产品!'];
  793. }
  794. }
  795. $tmp['title'] = $value['0'];
  796. $tmp['code'] = $value['1'];
  797. if(empty($category_map[$value['2']])) return [false,'产品分类:' . $value['2'] . '不存在'];
  798. $tmp['product_category_id'] = $category_map[$value['2']];
  799. if(! isset($category_parent[$tmp['product_category_id']])) return [false,'产品分类:' . $value['2'] . '下存在子分类,请将产品建与最底层分类下'];
  800. $parentsId = $category_parent[$tmp['product_category_id']];
  801. $tmp['product_category'] = json_encode($parentsId);
  802. $tmp['size'] = $value['3'];
  803. if($value['4']){
  804. if(empty($basic[$value['4']])) return [false, '单位:' . $value['4'] . '不存在'];
  805. $tmp['unit'] = $basic[$value['4']];
  806. }
  807. $tmp['bar_code'] = $value['5'];
  808. $tmp['cost'] = $value['6'] ?? 0;
  809. $tmp['retail_price'] = $value['7'] ?? 0;
  810. $tmp['product_attribute'] = $value['8'] ?? 0;
  811. $tmp['build_fee'] = $value['9'] ?? 0;
  812. $tmp['item_code'] = $value['10'] ?? "";
  813. $tmp['warranty_time'] = $value['11'] ?? 0;
  814. $tmp['product_item_code_title'] = $value['12'] ?? "";
  815. foreach ($map as $m => $v){
  816. if($value[$v['col']]){
  817. if(! is_numeric($value[$v['col']])) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  818. $formattedNumber = number_format($value[$v['col']], 2, '.', '');
  819. if($formattedNumber != $value[$v['col']]) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  820. }
  821. $tmp[$m] = $value[$v['col']];
  822. }
  823. if(isset($product_array[$value['1']])){
  824. //更新
  825. $pro_tmp = $product_array[$value['1']] ?? [];
  826. $product_id = $pro_tmp['id'];
  827. //产品价格子表
  828. foreach ($tmp as $k => $v){
  829. if(strpos($k, 'table_id.') !== false){
  830. $tmp2 = [];
  831. $k_n = str_replace('table_id.', "", $k);
  832. $tmp2['product_id'] = $product_id;
  833. $tmp2['basic_type_id'] = $k_n;
  834. $tmp2['price'] = $v;
  835. // $tmp2['crt_time'] = $time;
  836. // $tmp2['upd_time'] = $time;
  837. $update2[] = $tmp2;
  838. unset($tmp[$k]);
  839. }
  840. }
  841. //规格
  842. if($tmp['size'] == "") unset($tmp['size']);
  843. //单位
  844. if($tmp['unit'] == "") unset($tmp['unit']);
  845. //条码
  846. if($tmp['bar_code'] == "") unset($tmp['bar_code']);
  847. //成本
  848. if($tmp['cost'] == "") unset($tmp['cost']);
  849. //零售
  850. if($tmp['retail_price'] == "") unset($tmp['retail_price']);
  851. //产品属性
  852. if($tmp['product_attribute'] == "") unset($tmp['product_attribute']);
  853. //安装费
  854. if($tmp['build_fee'] == "") unset($tmp['build_fee']);
  855. //商品编码
  856. if($tmp['item_code'] == "") unset($tmp['item_code']);
  857. if(! empty($tmp['item_code']) && $tmp['product_item_code_title'] != "" && ! isset($update_detail[$tmp['item_code']])) $update_detail[$tmp['item_code']] = [
  858. 'item_code' => $tmp['item_code'],
  859. 'title' => $tmp['product_item_code_title']
  860. ];
  861. unset($tmp['product_item_code_title']);
  862. //质保年月
  863. if($tmp['warranty_time'] == "") unset($tmp['warranty_time']);
  864. //产品主表
  865. $update[$product_id] = $tmp;
  866. }else{
  867. $tmp['depart_id'] = $depart_id;
  868. $tmp['top_depart_id'] = $top_depart_id;
  869. $tmp['crt_id'] = $user['id'];
  870. $tmp['crt_time'] = $time;
  871. //产品价格子表
  872. foreach ($tmp as $k => $v){
  873. if(strpos($k, 'table_id.') !== false){
  874. $tmp2 = [];
  875. $k_n = str_replace('table_id.', "", $k);
  876. $tmp2['basic_type_id'] = $k_n;
  877. $tmp2['price'] = $v;
  878. // $tmp2['crt_time'] = $time;
  879. // $tmp2['upd_time'] = $time;
  880. $insert2[$tmp['code']][] = $tmp2;
  881. unset($tmp[$k]);
  882. }
  883. }
  884. if(! empty($tmp['item_code']) && $tmp['product_item_code_title'] != "" && ! isset($insert_detail[$tmp['item_code']])) $insert_detail[$tmp['item_code']] = [
  885. 'item_code' => $tmp['item_code'],
  886. 'title' => $tmp['product_item_code_title']
  887. ];
  888. unset($tmp['product_item_code_title']);
  889. //产品主表
  890. $insert[$tmp['code']] = $tmp;
  891. }
  892. }
  893. try{
  894. DB::beginTransaction();
  895. //新增
  896. if(! empty($insert)){
  897. Product::insert($insert);
  898. if(! empty($insert2)){
  899. $insert_detail_d = [];
  900. $last_insert_id = Product::where('crt_time',$time)
  901. ->pluck('id','code')
  902. ->toArray();
  903. foreach ($insert2 as $code => $val){
  904. foreach ($val as $v2){
  905. $v2['product_id'] = $last_insert_id[$code] ?? 0;
  906. $insert_detail_d[] = $v2;
  907. }
  908. }
  909. ProductPriceDetail::insert($insert_detail_d);
  910. }
  911. if(! empty($insert_detail)) ProductItemCodeMessage::insert(array_values($insert_detail));
  912. }
  913. //编辑
  914. if(! empty($update)){
  915. foreach ($update as $p_id => $value){
  916. Product::where('id',$p_id)
  917. ->update($value);
  918. }
  919. if(! empty($update2)){
  920. $product_id_array = array_keys($update);
  921. ProductPriceDetail::whereIn('product_id',$product_id_array)
  922. ->where('del_time',0)
  923. ->update(['del_time' => $time]);
  924. ProductPriceDetail::insert($update2);
  925. }
  926. if(! empty($update_detail)) {
  927. $item_code = array_keys($update_detail);
  928. ProductItemCodeMessage::where('del_time',0)
  929. ->whereIn('item_code',$item_code)
  930. ->update(['del_time' => $time]);
  931. ProductItemCodeMessage::insert(array_values($update_detail));
  932. }
  933. }
  934. DB::commit();
  935. }catch (\Exception $e){
  936. DB::rollBack();
  937. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  938. }
  939. return [true, ''];
  940. }
  941. function convertHyphens($string) {
  942. // 定义要被替换的字符
  943. $search = array('-', '-', '—'); // 全角连字符、半角连字符 和 破折号
  944. $replace = '-'; // 替换成半角连字符
  945. // 执行替换
  946. $res = str_replace($search, $replace, $string);
  947. return $res;
  948. }
  949. function generateTitleToIdMap($tree, $prefix = '') {
  950. $result = [];
  951. foreach ($tree as $node) {
  952. $currentTitle = $prefix . $node['title'];
  953. $result[$currentTitle] = $node['id'];
  954. if (!empty($node['children'])) {
  955. $result = array_merge(
  956. $result,
  957. $this->generateTitleToIdMap($node['children'], $currentTitle . '-')
  958. );
  959. }
  960. }
  961. return $result;
  962. }
  963. function getAncestors($id, &$idMap) {
  964. if (!isset($idMap[$id])) return [];
  965. $ancestors = [$id];
  966. $parentId = $idMap[$id]['parent_id'];
  967. if ($parentId !== null) {
  968. // 递归调用自己来添加父级 ID
  969. $ancestors = array_merge($this->getAncestors($parentId, $idMap), $ancestors);
  970. }
  971. return $ancestors;
  972. }
  973. // 检查是否拥有子项
  974. function hasChildren($id, &$idMap) {
  975. foreach ($idMap as $item) {
  976. if ($item['parent_id'] == $id) {
  977. return true;
  978. }
  979. }
  980. return false;
  981. }
  982. public function salesOnlineImport($array, $user){
  983. $head = $user['head']['id'] ?? 0;
  984. // 去除表头
  985. unset($array[0]);
  986. if(empty($array)) return [false, '导入数据不能为空'];
  987. $basic = (new BasicTypeService())->getMyBasicList($user, [18,23,24,29]);
  988. $basic_list = [];
  989. foreach ($basic as $value){
  990. if($value['type'] == 18){
  991. $basic_list[9][$value['title']] = $value['id'];
  992. }elseif ($value['type'] == 23){
  993. $basic_list[10][$value['title']] = $value['id'];
  994. }elseif ($value['type'] == 24){
  995. $basic_list[3][$value['title']] = $value['id'];
  996. }elseif ($value['type'] == 29){
  997. $basic_list[2][$value['title']] = $value['id'];
  998. }
  999. }
  1000. $search = "";
  1001. $customer = [];
  1002. foreach ($array as $key => $value){
  1003. $rowData = array_filter($value);
  1004. if (empty($rowData)) {
  1005. unset($array[$key]);
  1006. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || $value[6] === null) {
  1007. return [false, '带*号的字段项必填'];
  1008. }else{
  1009. foreach ($value as $k => $v){
  1010. $value[$k] = trim($v);
  1011. }
  1012. if(! isset($basic_list[2][$value[2]])) return [false, '客户简称:' . $value[2] .'不存在'];
  1013. $value[2] = $basic_list[2][$value[2]];
  1014. if(! isset($basic_list[3][$value[3]])) return [false, '店铺(平台类型):' . $value[3] .'不存在'];
  1015. $value[3] = $basic_list[3][$value[3]];
  1016. if(! is_numeric($value[5])) return [false, '货品数量请填写正确的数值'];
  1017. if(! is_numeric($value[6])) return [false, '产品合同金额请填写正确的数值'];
  1018. if(! empty($value[8]) && ! is_numeric($value[8])) return [false, '订单优惠金额请填写正确的数值'];
  1019. if(! empty($value[9])){
  1020. if(! isset($basic_list[9][$value[9]])) return [false, '安装方式:' . $value[9] .'不存在'];
  1021. $value[9] = $basic_list[9][$value[9]];
  1022. }
  1023. if(! empty($value[10])){
  1024. if(! isset($basic_list[10][$value[10]])) return [false, '安装地点:' . $value[10] .'不存在'];
  1025. $value[10] = $basic_list[10][$value[10]];
  1026. }
  1027. if(! isset(SalesOrder::$order_type_name[$value[1]])) return [false, '产品类型填写错误'];
  1028. $value[1] = SalesOrder::$order_type_name[$value[1]];
  1029. if(! empty($value[11])) {
  1030. list($status,$msg) = $this->changeAndReturnDate($value[11]);
  1031. if(! $status) return [false,"施工日期请填写正确的日期格式,例如2023-05-01"];
  1032. $value[11] = $msg;
  1033. }
  1034. if(! empty($value[12])) {
  1035. list($status,$msg) = $this->changeAndReturnDate($value[12]);
  1036. if(! $status) return [false,"交车日期请填写正确的日期格式,例如2023-05-01"];
  1037. $value[12] = $msg;
  1038. }
  1039. $array[$key] = $value;
  1040. $search .= "(code = '".$value[4]."') or";
  1041. if(! empty($value[7]) && ! in_array($value[7], $customer)) $customer[] = $value[7];
  1042. }
  1043. }
  1044. if(empty($array)) return [false, '导入数据不能为空'];
  1045. $search = rtrim($search,' or');
  1046. $search = "($search)";
  1047. $model = Product::ProductClear($user,[]);
  1048. $product = $model->whereRaw($search)
  1049. ->where('del_time',0)
  1050. ->select('title','id','code','cost','retail_price')
  1051. ->get()->toArray();
  1052. // $pro = (new ProductService())->productList(['product_id' => array_column($product,'id'), 'type' => 2],$user);
  1053. $product_map = [];
  1054. foreach ($product as $value){
  1055. $product_map[$value['code']] = [
  1056. 'id' => $value['id'],
  1057. 'cost' => $value['cost'],
  1058. 'retail_price' => $value['retail_price'],
  1059. ];
  1060. }
  1061. $time = time();
  1062. $model = Customer::Clear($user,[]);
  1063. $customer_map = $model->where('del_time',0)
  1064. ->whereIn('title',$customer)
  1065. ->pluck('id','title')
  1066. ->toArray();
  1067. $customer_info = CustomerInfo::where('del_time',0)
  1068. ->whereIn('customer_id',array_values($customer_map))
  1069. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  1070. ->select('customer_id','type','contact_info','data_id')
  1071. ->orderBy('id','asc')
  1072. ->get()->toArray();
  1073. $customer_contact = $customer_man = [];
  1074. foreach ($customer_info as $value){
  1075. if($value['type'] == CustomerInfo::type_one && ! isset($customer_contact[$value['customer_id']])){
  1076. $customer_contact[$value['customer_id']] = $value['contact_info'];
  1077. }elseif ($value['type'] == CustomerInfo::type_two){
  1078. $customer_man[$value['customer_id']][] = [
  1079. 'type' => SalesOrderInfo::type_two,
  1080. 'data_id' => $value['data_id'],
  1081. 'crt_time' => $time,
  1082. ];
  1083. }
  1084. }
  1085. $tmp = [
  1086. 'model_type' => SalesOrder::Model_type_four,
  1087. 'sales_order_type' => 0,
  1088. 'order_number' => '',
  1089. 'customer_short_name' => '',
  1090. 'plat_type' => '',
  1091. 'plat_order' => '',
  1092. 'sign_time' => $time,
  1093. 'product_total' => 0,
  1094. 'contract_fee' => 0,
  1095. 'discount_fee' => 0,
  1096. 'cdefine29' => '',//分社施工
  1097. 'cdefine32' => '',//达人昵称
  1098. 'cdefine30' => '',//业务员
  1099. 'rate' => 100,
  1100. 'depart_id' => $head,
  1101. 'top_depart_id' => $head,
  1102. 'crt_id' => $user['id'],
  1103. 'crt_time' => $time,
  1104. 'upd_time' => $time,
  1105. ];
  1106. $tmp_detail = [
  1107. 'sales_order_id' => 0,
  1108. 'product_id' => 0,
  1109. 'cost' => 0,
  1110. 'retail_price' => 0,
  1111. 'basic_type_id' => 0,
  1112. 'price' => 0,
  1113. 'final_amount' => 0,
  1114. 'number' => '',
  1115. 'crt_time' => $time,
  1116. ];
  1117. $insert = $insert_detail = $insert_detail_man = [];
  1118. $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
  1119. foreach ($array as $value){
  1120. $product_str = $value[4];
  1121. if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[4]. ']' . '不存在'];
  1122. $product_tmp = $product_map[$product_str] ?? [];
  1123. $customer_tmp = 0;
  1124. $customer_contact_tmp = "";
  1125. $customer_man_tmp = [];
  1126. if(! empty($value[7])){
  1127. $customer_tmp = $customer_map[$value[7]] ?? 0;
  1128. $customer_contact_tmp = $customer_contact[$customer_tmp] ?? "";
  1129. $customer_man_tmp = $customer_man[$customer_tmp] ?? [];
  1130. }
  1131. $customer_man_tmp[] = [
  1132. 'type' => SalesOrderInfo::type_one,
  1133. 'data_id' => $user['id'],
  1134. 'crt_time' => $time,
  1135. ];
  1136. $tmp['product_total'] = $tmp['contract_fee'] = 0;
  1137. $keys = $value[0] . $value[1];
  1138. $insert_detail_man[$keys] = $customer_man_tmp;
  1139. if(! isset($insert[$keys])){
  1140. $tmp['order_number'] = OrderNoService::createSalesOrderNumberImport($prefix);
  1141. $tmp['sales_order_type'] = $value[1];
  1142. $tmp['customer_short_name'] = $value[2];
  1143. $tmp['customer_id'] = $customer_tmp;
  1144. $tmp['customer_contact'] = $customer_contact_tmp;
  1145. $tmp['plat_type'] = $value[3];
  1146. $tmp['plat_order'] = $value[0];
  1147. $tmp['discount_fee'] = $value[8] ?: 0;
  1148. $tmp['cdefine29'] = $value[13] ?? "";//分社施工
  1149. $tmp['cdefine32'] = $value[14] ?? "";//达人昵称
  1150. $tmp['cdefine30'] = $value[15] ?? "";//业务员
  1151. $tmp['product_total'] += $value[6];
  1152. $tmp['contract_fee'] += $value[6] - $tmp['discount_fee'];
  1153. $tmp['construction_time'] = $value[11] ?? 0;
  1154. $tmp['handover_time'] = $value[12] ?? 0;
  1155. $insert[$keys] = $tmp;
  1156. }else{
  1157. $insert[$keys]['product_total'] += $value[6];
  1158. $insert[$keys]['contract_fee'] += $value[6];
  1159. }
  1160. $tmp_detail['product_id'] = $product_tmp['id'];
  1161. $tmp_detail['cost'] = $product_tmp['cost'];
  1162. $tmp_detail['retail_price'] = $product_tmp['retail_price'];
  1163. $tmp_detail['price'] = $product_tmp['retail_price'];
  1164. $tmp_detail['final_amount'] = $value[6];
  1165. $tmp_detail['number'] = $value[5];
  1166. $insert_detail[$keys][] = $tmp_detail;
  1167. }
  1168. $insert_detail = array_values($insert_detail);
  1169. $insert_detail_man = array_values($insert_detail_man);
  1170. try{
  1171. DB::beginTransaction();
  1172. if(! empty($insert)) SalesOrder::insert($insert);
  1173. $last_insert_id = SalesOrder::where('crt_time',$time)
  1174. ->select('id')
  1175. ->get()->toArray();
  1176. $last_insert_id = array_column($last_insert_id,'id');
  1177. if(! empty($insert_detail)){
  1178. $insert2 = [];
  1179. foreach ($last_insert_id as $key => $value){
  1180. if(isset($insert_detail[$key])) {
  1181. foreach ($insert_detail[$key] as $val){
  1182. $val['sales_order_id'] = $value;
  1183. $insert2[] = $val;
  1184. }
  1185. }
  1186. }
  1187. SalesOrderProductInfo::insert($insert2);
  1188. }
  1189. if(! empty($insert_detail_man)){
  1190. $insert3 = [];
  1191. foreach ($last_insert_id as $key => $value){
  1192. if(isset($insert_detail_man[$key])) {
  1193. foreach ($insert_detail_man[$key] as $val){
  1194. $val['sales_order_id'] = $value;
  1195. $insert3[] = $val;
  1196. }
  1197. }
  1198. }
  1199. SalesOrderInfo::insert($insert3);
  1200. }
  1201. DB::commit();
  1202. }catch (\Exception $e){
  1203. DB::rollBack();
  1204. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  1205. }
  1206. return [true, ''];
  1207. }
  1208. public function btOnlineImport($array, $user){
  1209. $head = $user['head']['id'] ?? 0;
  1210. // 去除表头
  1211. unset($array[0]);
  1212. if(empty($array)) return [false, '导入数据不能为空'];
  1213. $basic = (new BasicTypeService())->getMyBasicList($user, [18,23,24,29]);
  1214. $basic_list = [];
  1215. foreach ($basic as $value){
  1216. if($value['type'] == 18){
  1217. $basic_list[9][$value['title']] = $value['id'];
  1218. }elseif ($value['type'] == 23){
  1219. $basic_list[10][$value['title']] = $value['id'];
  1220. }elseif ($value['type'] == 24){
  1221. $basic_list[3][$value['title']] = $value['id'];
  1222. }elseif ($value['type'] == 29){
  1223. $basic_list[2][$value['title']] = $value['id'];
  1224. }
  1225. }
  1226. $search = "";
  1227. $customer = [];
  1228. foreach ($array as $key => $value){
  1229. $rowData = array_filter($value);
  1230. if (empty($rowData)) {
  1231. unset($array[$key]);
  1232. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || $value[6] === null) {
  1233. return [false, '带*号的字段项必填'];
  1234. }else{
  1235. foreach ($value as $k => $v){
  1236. $value[$k] = trim($v);
  1237. }
  1238. if(! isset($basic_list[2][$value[2]])) return [false, '客户简称:' . $value[2] .'不存在'];
  1239. $value[2] = $basic_list[2][$value[2]];
  1240. if(! isset($basic_list[3][$value[3]])) return [false, '店铺(平台类型):' . $value[3] .'不存在'];
  1241. $value[3] = $basic_list[3][$value[3]];
  1242. if(! is_numeric($value[5])) return [false, '货品数量请填写正确的数值'];
  1243. if(! is_numeric($value[6])) return [false, '产品合同金额请填写正确的数值'];
  1244. if(! empty($value[8]) && ! is_numeric($value[8])) return [false, '订单优惠金额请填写正确的数值'];
  1245. if(! empty($value[9])){
  1246. if(! isset($basic_list[9][$value[9]])) return [false, '安装方式:' . $value[9] .'不存在'];
  1247. $value[9] = $basic_list[9][$value[9]];
  1248. }
  1249. if(! empty($value[10])){
  1250. if(! isset($basic_list[10][$value[10]])) return [false, '安装地点:' . $value[10] .'不存在'];
  1251. $value[10] = $basic_list[10][$value[10]];
  1252. }
  1253. if(! isset(SalesOrder::$order_type_name[$value[1]])) return [false, '产品类型填写错误'];
  1254. $value[1] = SalesOrder::$order_type_name[$value[1]];
  1255. if(! empty($value[11])) {
  1256. list($status,$msg) = $this->changeAndReturnDate($value[11]);
  1257. if(! $status) return [false,"施工日期请填写正确的日期格式,例如2023-05-01"];
  1258. $value[11] = $msg;
  1259. }
  1260. if(! empty($value[12])) {
  1261. list($status,$msg) = $this->changeAndReturnDate($value[12]);
  1262. if(! $status) return [false,"交车日期请填写正确的日期格式,例如2023-05-01"];
  1263. $value[12] = $msg;
  1264. }
  1265. $array[$key] = $value;
  1266. $search .= "(code = '".$value[4]."') or";
  1267. if(! empty($value[7]) && ! in_array($value[7], $customer)) $customer[] = $value[7];
  1268. }
  1269. }
  1270. if(empty($array)) return [false, '导入数据不能为空'];
  1271. $search = rtrim($search,' or');
  1272. $search = "($search)";
  1273. $model = Product::ProductClear($user,[]);
  1274. $product = $model->whereRaw($search)
  1275. ->where('del_time',0)
  1276. ->select('title','id','code','cost','retail_price')
  1277. ->get()->toArray();
  1278. $product_map = [];
  1279. foreach ($product as $value){
  1280. $product_map[$value['code']] = [
  1281. 'id' => $value['id'],
  1282. 'cost' => $value['cost'],
  1283. 'retail_price' => $value['retail_price'],
  1284. ];
  1285. }
  1286. $time = time();
  1287. $model = Customer::Clear($user,[]);
  1288. $customer_map = $model->where('del_time',0)
  1289. ->whereIn('title',$customer)
  1290. ->pluck('id','title')
  1291. ->toArray();
  1292. $customer_info = CustomerInfo::where('del_time',0)
  1293. ->whereIn('customer_id',array_values($customer_map))
  1294. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  1295. ->select('customer_id','type','contact_info','data_id')
  1296. ->orderBy('id','asc')
  1297. ->get()->toArray();
  1298. $customer_contact = $customer_man = [];
  1299. foreach ($customer_info as $value){
  1300. if($value['type'] == CustomerInfo::type_one && ! isset($customer_contact[$value['customer_id']])){
  1301. $customer_contact[$value['customer_id']] = $value['contact_info'];
  1302. }elseif ($value['type'] == CustomerInfo::type_two){
  1303. $customer_man[$value['customer_id']][] = [
  1304. 'type' => SalesOrderInfo::type_two,
  1305. 'data_id' => $value['data_id'],
  1306. 'crt_time' => $time,
  1307. ];
  1308. }
  1309. }
  1310. $tmp = [
  1311. 'model_type' => SalesOrder::Model_type_seven,
  1312. 'sales_order_type' => 0,
  1313. 'order_number' => '',
  1314. 'customer_short_name' => '',
  1315. 'plat_type' => '',
  1316. 'plat_order' => '',
  1317. 'sign_time' => $time,
  1318. 'product_total' => 0,
  1319. 'contract_fee' => 0,
  1320. 'discount_fee' => 0,
  1321. 'cdefine29' => '',//分社施工
  1322. 'cdefine32' => '',//达人昵称
  1323. 'cdefine30' => '',//业务员
  1324. 'rate' => 100,
  1325. 'depart_id' => $head,
  1326. 'top_depart_id' => $head,
  1327. 'crt_id' => $user['id'],
  1328. 'crt_time' => $time,
  1329. 'upd_time' => $time,
  1330. ];
  1331. $tmp_detail = [
  1332. 'sales_order_id' => 0,
  1333. 'product_id' => 0,
  1334. 'cost' => 0,
  1335. 'retail_price' => 0,
  1336. 'basic_type_id' => 0,
  1337. 'price' => 0,
  1338. 'final_amount' => 0,
  1339. 'number' => '',
  1340. 'crt_time' => $time,
  1341. ];
  1342. $insert = $insert_detail = $insert_detail_man = [];
  1343. $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
  1344. foreach ($array as $value){
  1345. $product_str = $value[4];
  1346. if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[4]. ']' . '不存在'];
  1347. $product_tmp = $product_map[$product_str] ?? [];
  1348. $customer_tmp = 0;
  1349. $customer_contact_tmp = "";
  1350. $customer_man_tmp = [];
  1351. if(! empty($value[7])){
  1352. $customer_tmp = $customer_map[$value[7]] ?? 0;
  1353. $customer_contact_tmp = $customer_contact[$customer_tmp] ?? "";
  1354. $customer_man_tmp = $customer_man[$customer_tmp] ?? [];
  1355. }
  1356. $customer_man_tmp[] = [
  1357. 'type' => SalesOrderInfo::type_one,
  1358. 'data_id' => $user['id'],
  1359. 'crt_time' => $time,
  1360. ];
  1361. $tmp['product_total'] = $tmp['contract_fee'] = 0;
  1362. $keys = $value[0] . $value[1];
  1363. $insert_detail_man[$keys] = $customer_man_tmp;
  1364. if(! isset($insert[$keys])){
  1365. $tmp['order_number'] = OrderNoService::createSalesOrderNumberImport($prefix);
  1366. $tmp['sales_order_type'] = $value[1];
  1367. $tmp['customer_short_name'] = $value[2];
  1368. $tmp['customer_id'] = $customer_tmp;
  1369. $tmp['customer_contact'] = $customer_contact_tmp;
  1370. $tmp['plat_type'] = $value[3];
  1371. $tmp['plat_order'] = $value[0];
  1372. $tmp['discount_fee'] = $value[8] ?: 0;
  1373. $tmp['cdefine29'] = $value[13] ?? "";//分社施工
  1374. $tmp['cdefine32'] = $value[14] ?? "";//达人昵称
  1375. $tmp['cdefine30'] = $value[15] ?? "";//业务员
  1376. $tmp['product_total'] += $value[6];
  1377. $tmp['contract_fee'] += $value[6] - $tmp['discount_fee'];
  1378. $tmp['construction_time'] = $value[11] ?? 0;
  1379. $tmp['handover_time'] = $value[12] ?? 0;
  1380. $insert[$keys] = $tmp;
  1381. }else{
  1382. $insert[$keys]['product_total'] += $value[6];
  1383. $insert[$keys]['contract_fee'] += $value[6];
  1384. }
  1385. $tmp_detail['product_id'] = $product_tmp['id'];
  1386. $tmp_detail['cost'] = $product_tmp['cost'];
  1387. $tmp_detail['retail_price'] = $product_tmp['retail_price'];
  1388. $tmp_detail['price'] = $product_tmp['retail_price'];
  1389. $tmp_detail['final_amount'] = $value[6];
  1390. $tmp_detail['number'] = $value[5];
  1391. $insert_detail[$keys][] = $tmp_detail;
  1392. }
  1393. $insert_detail = array_values($insert_detail);
  1394. $insert_detail_man = array_values($insert_detail_man);
  1395. try{
  1396. DB::beginTransaction();
  1397. if(! empty($insert)) SalesOrder::insert($insert);
  1398. $last_insert_id = SalesOrder::where('crt_time',$time)
  1399. ->select('id')
  1400. ->get()->toArray();
  1401. $last_insert_id = array_column($last_insert_id,'id');
  1402. if(! empty($insert_detail)){
  1403. $insert2 = [];
  1404. foreach ($last_insert_id as $key => $value){
  1405. if(isset($insert_detail[$key])) {
  1406. foreach ($insert_detail[$key] as $val){
  1407. $val['sales_order_id'] = $value;
  1408. $insert2[] = $val;
  1409. }
  1410. }
  1411. }
  1412. SalesOrderProductInfo::insert($insert2);
  1413. }
  1414. if(! empty($insert_detail_man)){
  1415. $insert3 = [];
  1416. foreach ($last_insert_id as $key => $value){
  1417. if(isset($insert_detail_man[$key])) {
  1418. foreach ($insert_detail_man[$key] as $val){
  1419. $val['sales_order_id'] = $value;
  1420. $insert3[] = $val;
  1421. }
  1422. }
  1423. }
  1424. SalesOrderInfo::insert($insert3);
  1425. }
  1426. DB::commit();
  1427. }catch (\Exception $e){
  1428. DB::rollBack();
  1429. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  1430. }
  1431. return [true, ''];
  1432. }
  1433. public function lastJcImport($array, $user){
  1434. $head = $user['head']['id'] ?? 0;
  1435. // 去除表头
  1436. unset($array[0]);
  1437. if(empty($array)) return [false, '导入数据不能为空'];
  1438. $shop_name = $product_code = [];
  1439. foreach ($array as $key => $value){
  1440. $rowData = array_filter($value);
  1441. if (empty($rowData)) {
  1442. unset($array[$key]);
  1443. } elseif(empty($value[0]) || empty($value[1])) {
  1444. return [false, '带*号的字段项必填'];
  1445. }else{
  1446. foreach ($value as $k => $v){
  1447. $value[$k] = trim($v);
  1448. }
  1449. if(! is_numeric($value[2])) return [false, '* 上月结存数量请填写正确的数值'];
  1450. if(! is_numeric($value[3])) return [false, '* 上月结存单价请填写正确的数值'];
  1451. if(! is_numeric($value[4])) return [false, '* 上月结存金额请填写正确的数值'];
  1452. if(! in_array($value[0],$shop_name)) $shop_name[] = $value[0];
  1453. if(! in_array($value[1],$product_code)) $product_code[] = $value[1];
  1454. }
  1455. }
  1456. $model = Product::ProductClear($user,[]);
  1457. $product_map = $model->whereIn('code',$product_code)
  1458. ->where('del_time',0)
  1459. ->pluck('id','code')
  1460. ->toArray();
  1461. $depart_map = Depart::whereIn('title',$shop_name)
  1462. ->where('del_time',0)
  1463. ->pluck('id','title')
  1464. ->toArray();
  1465. $storehouse_map = Storehouse::whereIn('top_depart_id',array_values($depart_map))
  1466. ->where('del_time',0)
  1467. ->pluck('id','top_depart_id')
  1468. ->toArray();
  1469. $time = time();
  1470. $insert = [];
  1471. $last_month_stamp = strtotime(date('Y-m-t 23:59:59', strtotime('last month')));
  1472. foreach ($array as $value){
  1473. $depart_tmp = $depart_map[$value[0]] ?? 0;
  1474. if($depart_tmp <= 0) return [false, "门店:" . $value[0] . "不存在或已被删除"];
  1475. $product_tmp = $product_map[$value[1]] ?? 0;
  1476. if($product_tmp <= 0) return [false, "产品编码:" . $value[1] . "不存在或已被删除"];
  1477. $storehouse_tmp = $storehouse_map[$depart_tmp] ?? 0;
  1478. $insert[] = [
  1479. 'top_depart_id' => $depart_tmp,
  1480. 'product_id' => $product_tmp,
  1481. 'storehouse_id' => $storehouse_tmp,
  1482. 'number' => $value[2],
  1483. 'price' => $value[3],
  1484. 'total' => $value[4],
  1485. 'crt_time' => $time,
  1486. 'time' => $last_month_stamp
  1487. ];
  1488. }
  1489. if(empty($insert)) return [false, '暂无更新的数据'];
  1490. try{
  1491. DB::beginTransaction();
  1492. //更新数据为删除
  1493. foreach ($insert as $value){
  1494. LastJc::where('del_time',0)
  1495. ->where('time', $last_month_stamp)
  1496. ->where('top_depart_id',$value['top_depart_id'])
  1497. ->where('storehouse_id',$value['storehouse_id'])
  1498. ->where('product_id',$value['product_id'])
  1499. ->update(['del_time' => $time]);
  1500. }
  1501. //写入数据
  1502. LastJc::insert($insert);
  1503. DB::commit();
  1504. }catch (\Exception $e){
  1505. DB::rollBack();
  1506. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  1507. }
  1508. return [true, ''];
  1509. }
  1510. public function inventoryImport($array, $user){
  1511. $head = $user['head']['id'] ?? 0;
  1512. // 去除表头
  1513. unset($array[0]);
  1514. if(empty($array)) return [false, '导入数据不能为空'];
  1515. $shop_name = $product_code = [];
  1516. foreach ($array as $key => $value){
  1517. $rowData = array_filter($value);
  1518. if (empty($rowData)) {
  1519. unset($array[$key]);
  1520. } elseif(empty($value[0]) || empty($value[1])) {
  1521. if($key != 1){
  1522. if(empty($value[1])) return [false, '带*号的字段项必填'];
  1523. } else{
  1524. return [false, '带*号的字段项必填'];
  1525. }
  1526. }else{
  1527. foreach ($value as $k => $v){
  1528. $value[$k] = trim($v);
  1529. }
  1530. if($value[0] && ! in_array($value[0],$shop_name)) $shop_name[] = $value[0];
  1531. if(! in_array($value[1],$product_code)) {
  1532. $product_code[] = $value[1];
  1533. }else{
  1534. return [false, '产品编号:' . $value[1] .'已存在'];
  1535. }
  1536. if(! is_numeric($value[2])) return [false, '* 盘点数量请填写正确的数值'];
  1537. if(! empty($value[3])){
  1538. $bool = $this->checkNumber($value[3]);
  1539. if(! $bool) return [false, '盘点单价请输入大于等于0的两位小数的数字'];
  1540. }
  1541. }
  1542. }
  1543. if(count($shop_name) != 1) return [false, '只允许单门店盘点'];
  1544. //门店信息
  1545. $depart = Depart::whereIn('title',$shop_name)
  1546. ->where('del_time',0)
  1547. ->where('parent_id',0)
  1548. ->select('id','title','basic_type_id')
  1549. ->first();
  1550. if(empty($depart)) return [false, "门店:" .$shop_name . "不存在或已被删除"];
  1551. $depart = $depart->toArray();
  1552. $top_depart_id = $depart['id'];
  1553. $basic_type_id = $depart['basic_type_id'];
  1554. //门店的仓库
  1555. $storehouse = Storehouse::where('top_depart_id', $top_depart_id)
  1556. ->where('del_time',0)
  1557. ->select('id')
  1558. ->first();
  1559. if(empty($storehouse)) return [false, "门店:" .$shop_name . "的仓库不存在或已被删除"];
  1560. $storehouse = $storehouse->toArray();
  1561. $storehouse_id = $storehouse['id'];
  1562. if(empty($storehouse_id)) return [false, "门店:" .$shop_name . "的仓库不存在或已被删除"];
  1563. //产品
  1564. $model = Product::ProductClear($user,[]);
  1565. $product = $model->whereIn('code',$product_code)
  1566. ->where('del_time',0)
  1567. ->select('id','code','retail_price')
  1568. ->get()->toArray();
  1569. $product_map = array_column($product,'id','code');
  1570. $product_map_2 = array_column($product,'retail_price','id');
  1571. //产品使用价格
  1572. $product_use = [];
  1573. if(! empty($basic_type_id)){
  1574. $product_id = array_column($product,'id');
  1575. $service = new ProductService();
  1576. $detail_map = $service->getProductPrice($product_id, 1, $basic_type_id);
  1577. foreach ($detail_map as $key => $value){
  1578. if(isset($value[$basic_type_id]) && ! empty($value[$basic_type_id]['price'])) {
  1579. $product_use[$key] = $value[$basic_type_id]['price'];
  1580. }else{
  1581. $product_use[$key] = $product_map_2[$key] ?? 0;
  1582. }
  1583. }
  1584. }else{
  1585. $product_use = $product_map_2;
  1586. }
  1587. $time = time();
  1588. $product_submit = $product_id = $sub = [];
  1589. foreach ($array as $value){
  1590. $product_tmp = $product_map[$value[1]] ?? 0;
  1591. if($product_tmp <= 0) return [false, "产品编码:" . $value[1] . "不存在或已被删除"];
  1592. if(isset($value[3])){
  1593. $bool = $this->checkNumber($value[3]);
  1594. if(! $bool) return [false, '盘点单价不合法,需是大于等于0的两位数值'];
  1595. $price = $value[3];
  1596. }else{
  1597. if(! isset($product_use[$product_tmp]) || floatval($product_use[$product_tmp]) < 0) return [false, "产品编码:" . $value[1] . "获取单价所得小于0,请检查门店所设置价格以及产品零售价"];
  1598. $price = $product_use[$product_tmp];
  1599. }
  1600. $key = $product_tmp . ',' . $storehouse_id;
  1601. if(! isset($product_submit[$key])) $product_submit[$key] = 0;
  1602. //总的产品id
  1603. $product_id[] = $product_tmp;
  1604. $sub[] = [
  1605. 'product_id' => $product_tmp,
  1606. 'storehouse_id' => $storehouse_id,
  1607. 'order_number' => "",
  1608. 'counted_num' => $value[2],
  1609. 'book_num' => 0,
  1610. 'final_num' => 0,
  1611. 'inventory_id' => 0,
  1612. 'price' => $price,
  1613. ];
  1614. }
  1615. //库存校验
  1616. $set = ProductInventorySet::where('top_depart_id',$top_depart_id)->where('del_time',0)->first();
  1617. if(empty($set)) return [false, '门店是否校验库存设置缺失'];
  1618. $set = $set->toArray();
  1619. $is_check_stock = $set['param_one'] <= 0 ? ProductInventorySet::type_one : $set['param_one'];
  1620. //获取库存
  1621. $inventory_map = [];
  1622. $inventory = ProductInventoryService::getRealStock($product_id, $storehouse_id);
  1623. foreach ($inventory as $value){
  1624. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  1625. $inventory_map[$key] = $value['real_number'];
  1626. }
  1627. $order_number = (new OrderNoService())->createOrderNumber(Inventory::prefix);
  1628. $product_submit_reduce = $product_reduce_id = [];
  1629. //盘点详情
  1630. foreach ($sub as $key => $value){
  1631. $sub[$key]['order_number'] = $order_number;
  1632. $keys = $value['product_id'] . ',' . $value['storehouse_id'];
  1633. if(isset($inventory_map[$keys])){
  1634. $sub[$key]['book_num'] = $inventory_map[$keys];
  1635. $num = bcsub($value['counted_num'], $inventory_map[$keys],4);
  1636. $sub[$key]['final_num'] = $num;
  1637. }else{
  1638. $sub[$key]['final_num'] = $value['counted_num'];
  1639. }
  1640. $final_amount = bcmul($sub[$key]['final_num'], $value['price'],2);
  1641. $sub[$key]['final_amount'] = $final_amount;
  1642. if($sub[$key]['final_num'] < 0) {
  1643. $product_reduce_id[] = $value['product_id'];
  1644. $product_submit_reduce[$keys] = $sub[$key]['final_num'];
  1645. }
  1646. }
  1647. //校验是否存在产品盘点
  1648. list($status, $msg) = (new InventoryService())->issetProduct(['top_depart_id' => $top_depart_id], $product_submit);
  1649. if(! $status) return [false, $msg];
  1650. if(! empty($product_submit_reduce)){
  1651. //校验库存
  1652. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_reduce_id, $product_submit_reduce);
  1653. if(! $status) return [false, $msg];
  1654. }
  1655. if(empty($sub)) return [false, '暂无需要写入的盘点数据'];
  1656. try{
  1657. DB::beginTransaction();
  1658. $inventory_model = new Inventory();
  1659. $inventory_model->order_number = $order_number;
  1660. $inventory_model->storehouse_id = $storehouse_id;
  1661. $inventory_model->counted_id = $user['id'];
  1662. $inventory_model->counted_time = $time;
  1663. $inventory_model->depart_id = $top_depart_id;
  1664. $inventory_model->top_depart_id = $top_depart_id;
  1665. $inventory_model->crt_id = $user['id'];
  1666. $inventory_model->save();
  1667. foreach ($sub as $key => $value){
  1668. $sub[$key]['inventory_id'] = $inventory_model->id;
  1669. }
  1670. //写入数据
  1671. InventorySub::insert($sub);
  1672. //单据创建时是否校验库存
  1673. (new CheckService())->orderInventoryInsert(['order_number' => $order_number, 'is_check_stock' => $is_check_stock]);
  1674. DB::commit();
  1675. }catch (\Exception $exception){
  1676. DB::rollBack();
  1677. if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
  1678. return [false, '网络波动,请重新操作!'];
  1679. }
  1680. return [false, $exception->getMessage() . $exception->getLine() . $exception->getCode()];
  1681. }
  1682. return [true, ''];
  1683. }
  1684. public function productAdjustmentImport($array, $user){
  1685. $head = $user['head']['id'] ?? 0;
  1686. // 去除表头
  1687. unset($array[0]);
  1688. if(empty($array)) return [false, '导入数据不能为空'];
  1689. $shop_name = $product_code = [];
  1690. foreach ($array as $key => $value){
  1691. $rowData = array_filter($value);
  1692. if (empty($rowData)) {
  1693. unset($array[$key]);
  1694. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2])) {
  1695. if($key != 1){
  1696. if(empty($value[1])) return [false, '带*号的字段项必填'];
  1697. } else{
  1698. return [false, '带*号的字段项必填'];
  1699. }
  1700. }else{
  1701. foreach ($value as $k => $v){
  1702. $value[$k] = trim($v);
  1703. }
  1704. if($value[0] && ! in_array($value[0],$shop_name)) $shop_name[] = $value[0];
  1705. if(! in_array($value[1],$product_code)) {
  1706. $product_code[] = $value[1];
  1707. }else{
  1708. return [false, '产品编号:' . $value[1] .'已存在'];
  1709. }
  1710. if(! is_numeric($value[2])) return [false, '调整金额请填写正确的数值'];
  1711. }
  1712. }
  1713. if(count($shop_name) != 1) return [false, '只允许单门店创建调整金额'];
  1714. //门店信息
  1715. $depart = Depart::whereIn('title',$shop_name)
  1716. ->where('del_time',0)
  1717. ->where('parent_id',0)
  1718. ->select('id','title')
  1719. ->first();
  1720. if(empty($depart)) return [false, "门店:" .$shop_name . "不存在或已被删除"];
  1721. $depart = $depart->toArray();
  1722. $top_depart_id = $depart['id'];
  1723. //门店的仓库
  1724. $storehouse = Storehouse::where('top_depart_id', $top_depart_id)
  1725. ->where('del_time',0)
  1726. ->select('id')
  1727. ->first();
  1728. if(empty($storehouse)) return [false, "门店:" .$shop_name . "的仓库不存在或已被删除"];
  1729. $storehouse = $storehouse->toArray();
  1730. $storehouse_id = $storehouse['id'];
  1731. if(empty($storehouse_id)) return [false, "门店:" .$shop_name . "的仓库不存在或已被删除"];
  1732. //产品
  1733. $model = Product::ProductClear($user,[]);
  1734. $product = $model->whereIn('code',$product_code)
  1735. ->where('del_time',0)
  1736. ->select('id','code')
  1737. ->get()->toArray();
  1738. $product_map = array_column($product,'id','code');
  1739. $product_submit = [];
  1740. foreach ($product as $value){
  1741. $key = $value['id'] . ',' . $storehouse_id;
  1742. if(! isset($product_submit[$key])) $product_submit[$key] = 0;
  1743. }
  1744. //校验是否存在产品调整
  1745. list($status, $msg) = (new ProductAdjustService())->issetProduct(['top_depart_id' => $top_depart_id], $product_submit);
  1746. if(! $status) return [false, $msg];
  1747. $time = time();
  1748. $sub = [];
  1749. foreach ($array as $value){
  1750. $product_tmp = $product_map[$value[1]] ?? 0;
  1751. if($product_tmp <= 0) return [false, "产品编码:" . $value[1] . "不存在或已被删除"];
  1752. $sub[] = [
  1753. 'product_id' => $product_tmp,
  1754. 'storehouse_id' => $storehouse_id,
  1755. 'order_number' => "",
  1756. 'product_adjustment_id' => 0,
  1757. 'final_amount' => $value[2],
  1758. ];
  1759. }
  1760. if(empty($sub)) return [false, '暂无需要写入的产品调整单数据'];
  1761. try{
  1762. $order_number = (new OrderNoService())->createOrderNumber(ProductAdjustment::prefix);
  1763. DB::beginTransaction();
  1764. $inventory_model = new ProductAdjustment();
  1765. $inventory_model->order_number = $order_number;
  1766. $inventory_model->storehouse_id = $storehouse_id;
  1767. $inventory_model->counted_id = $user['id'];
  1768. $inventory_model->counted_time = $time;
  1769. $inventory_model->depart_id = $top_depart_id;
  1770. $inventory_model->top_depart_id = $top_depart_id;
  1771. $inventory_model->crt_id = $user['id'];
  1772. $inventory_model->save();
  1773. foreach ($sub as $key => $value){
  1774. $sub[$key]['product_adjustment_id'] = $inventory_model->id;
  1775. $sub[$key]['order_number'] = $order_number;
  1776. }
  1777. //写入数据
  1778. ProductAdjustmentSub::insert($sub);
  1779. DB::commit();
  1780. }catch (\Exception $exception){
  1781. DB::rollBack();
  1782. if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
  1783. return [false, '网络波动,请重新操作!'];
  1784. }
  1785. return [false, $exception->getMessage() . $exception->getLine() . $exception->getCode()];
  1786. }
  1787. return [true, ''];
  1788. }
  1789. public function customerPerformanceImport($array, $user, $other_param){
  1790. if(empty($other_param["ym"])) return [false,'请选择导入客资业绩年月'];
  1791. // 去除表头
  1792. unset($array[0]);
  1793. if(empty($array)) return [false, '导入数据不能为空'];
  1794. $insert = [];
  1795. foreach ($array as $key => $value){
  1796. $rowData = array_filter($value);
  1797. if (empty($rowData)) {
  1798. unset($array[$key]);
  1799. } elseif(empty($value[0]) || empty($value[1])) {
  1800. return [false, '带*号的字段项必填'];
  1801. }else{
  1802. foreach ($value as $k => $v){
  1803. $value[$k] = trim($v);
  1804. }
  1805. if(! empty($value[2]) && ! is_numeric($value[2])) return [false, '当月打款金额请填写正确的数值'];
  1806. }
  1807. $insert[] = [
  1808. 'area' => $value['0'],
  1809. 'title' => $value['1'],
  1810. 'current_month_payment' => $value['2'] ?? 0,
  1811. 'crt_time' => $other_param['ym']
  1812. ];
  1813. }
  1814. $start_time = strtotime(date("Y-m-01 00:00:00", $other_param['ym']));
  1815. $end_time = strtotime(date("Y-m-t 23:59:59", $other_param['ym']));
  1816. CustomerPerFormance::where('del_time',0)
  1817. ->where('crt_time','>=', $start_time)
  1818. ->where('crt_time','<=', $end_time)
  1819. ->update(['del_time' => time()]);
  1820. if(! empty($insert)) CustomerPerFormance::insert($insert);
  1821. return [true, ''];
  1822. }
  1823. }