ImportService.php 79 KB

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