ImportService.php 60 KB

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