ImportService.php 77 KB

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