ImportService.php 73 KB

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