ImportService.php 73 KB

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