ImportService.php 75 KB

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