ImportService.php 81 KB

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