ImportService.php 63 KB

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