ImportService.php 62 KB

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