ImportService.php 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  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()) {
  232. $bool = $import->getIsLongText();
  233. if($bool) {
  234. return [0, $import->getMsg()];
  235. }else{
  236. return [false, $import->getMsg()];
  237. }
  238. }
  239. }catch (\Throwable $exception) {
  240. return [false, $exception->getMessage() . ' (Code: ' . $exception->getCode() . ', Line: ' . $exception->getLine() . ')'];
  241. }
  242. return [true, ''];
  243. }
  244. public function customerImport($array, $user){
  245. $time = time();
  246. $head = $user['depart_top'][0] ?? [];
  247. $head = $head['depart_id'] ?? 0;
  248. if(empty($head)) return [false, '导入异常错误,门店信息丢失'];
  249. // 去除表头
  250. unset($array[0]);
  251. if(empty($array)) return [false, '导入数据不能为空'];
  252. //第一次表格数据校验 非空 已经过滤数据
  253. list($status, $msg) = $this->customerFilterFirst($array, $time);
  254. if(! $status) return [0, $msg];
  255. $contact_info = $msg;
  256. if(empty($array)) return [false, '导入数据不能为空'];
  257. //客户
  258. $model = Customer::Clear($user,[]);
  259. $customer = $model->where('del_time',0)
  260. ->whereIn('title',array_unique(array_column($array,'1')))
  261. ->pluck('id','title')
  262. ->toArray();
  263. $basic = (new BasicTypeService())->getMyBasicList($user, [1,2,3,4,5,9,10,30]);
  264. $basic_list = [];
  265. foreach ($basic as $value){
  266. if($value['type'] == 1){
  267. $basic_list[2][$value['title']] = $value['id'];
  268. }elseif ($value['type'] == 2){
  269. $basic_list[3][$value['title']] = $value['id'];
  270. }elseif ($value['type'] == 3){
  271. $basic_list[41][$value['title']] = $value['id'];
  272. }elseif ($value['type'] == 30){
  273. $basic_list[42][$value['title']] = $value['id'];
  274. }elseif ($value['type'] == 4){
  275. $basic_list[12][$value['title']] = $value['id'];
  276. }elseif ($value['type'] == 5){
  277. $basic_list[7][$value['title']] = $value['id'];
  278. }elseif ($value['type'] == 9){
  279. $basic_list[8][$value['title']] = $value['id'];
  280. }elseif ($value['type'] == 10){
  281. $basic_list[9][$value['title']] = $value['id'];
  282. }
  283. }
  284. $model = Product::ProductClear($user,[]);
  285. $product = $model->where('del_time',0)
  286. ->whereIn('title',array_unique(array_column($array,'6')))
  287. ->pluck('id','title')
  288. ->toArray();
  289. $emp = Employee::where('del_time',0)
  290. ->whereIn('number',array_unique(array_column($array,'14')))
  291. ->pluck('id','number')
  292. ->toArray();
  293. $top_depart_id = $user['depart_top'][0] ?? [];
  294. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  295. $contact_info_array = CustomerInfo::from('customer_info as a')
  296. ->join('customer as b','b.id','a.customer_id')
  297. ->where('a.del_time',0)
  298. ->where('b.del_time',0)
  299. ->where('b.top_depart_id',$top_depart_id)
  300. ->whereIn('a.contact_info', $contact_info)
  301. ->select('a.contact_info')->get()->toArray();
  302. $contact_info_array = array_column($contact_info_array,'contact_info');
  303. list($status, $msg) = $this->customerFilterSecond($array,$time,$head,$user,$contact_info_array,$customer,$basic_list,$product,$emp);
  304. if(! $status) return [0, $msg];
  305. list($insert, $insert_detail, $insert_detail2) = $msg;
  306. try{
  307. DB::beginTransaction();
  308. if(! empty($insert)) Customer::insert($insert);
  309. //获取上一次所有id
  310. $last_insert_id = Customer::where('crt_time',$time)
  311. ->where('crt_time',$time)
  312. ->where('depart_id',$head)
  313. ->where('top_depart_id',$head)
  314. ->where('crt_id',$user['id'])
  315. ->select('id')->get()->toArray();
  316. $last_insert_id = array_column($last_insert_id,'id');
  317. //组织数据 写入与主表的关联id
  318. $insert_detail_1 = [];
  319. foreach ($insert_detail as $key => $value){
  320. if(empty($value['contact_type']) && empty($value['contact_info'])) continue;
  321. $value['customer_id'] = $last_insert_id[$key];
  322. $insert_detail_1[] = $value;
  323. }unset($insert_detail);
  324. $insert_detail_2 = [];
  325. foreach ($insert_detail2 as $key => $value){
  326. if(empty($value['data_id'])) continue;
  327. $value['customer_id'] = $last_insert_id[$key];
  328. $insert_detail_2[] = $value;
  329. }unset($insert_detail2);
  330. if(! empty($insert_detail_1)) CustomerInfo::insert($insert_detail_1);
  331. if(! empty($insert_detail_2)) CustomerInfo::insert($insert_detail_2);
  332. DB::commit();
  333. }catch (\Exception $e){
  334. DB::rollBack();
  335. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  336. }
  337. return [true, ''];
  338. }
  339. function customerFilterFirst(&$array, $time){
  340. $array_clean = $contact_info = [];
  341. $error = [];
  342. foreach ($array as $key => $value){
  343. $line = '第' . $key . '行';
  344. $rowData = array_filter($value);
  345. if (empty($rowData)) {
  346. unset($array[$key]);
  347. } else{
  348. foreach ($value as $k => $v){
  349. $value[$k] = trim($v);
  350. }
  351. if(empty($value[0])){
  352. $error[] = $line . '客户模板字段项必填';
  353. }else{
  354. if(! isset(Customer::dk[$value[0]])) {
  355. $error[] = $line . '客户模板填写错误';
  356. }else{
  357. $value[0] = Customer::dk[$value[0]];
  358. }
  359. }
  360. if(empty($value[1])){
  361. $error[] = $line . '客户名称字段项必填';
  362. }else{
  363. if(in_array($value[1],$array_clean)) {
  364. $error[] = $line . '客户名称' . $value[1] .'在导入文件中重复出现';
  365. }else{
  366. $array_clean[] = $value[1];
  367. }
  368. }
  369. if(! empty($value[13])){
  370. if(in_array($value[13],$contact_info)) {
  371. $error[] = $line . '联系方式内容'. $value[13] .'在导入文件内重复出现';
  372. }else{
  373. $contact_info[] = $value[13];
  374. }
  375. }
  376. if(! empty($value[15])){
  377. list($status, $msg) = $this->convertExcelCellToDate($value[15]);
  378. if(! $status) {
  379. $error[] = $line . $msg;
  380. }else{
  381. $value[15] = $msg;
  382. }
  383. } else{
  384. $value[15] = $time;
  385. }
  386. $array[$key] = $value;
  387. }
  388. }unset($array_clean);
  389. if(! empty($error)){
  390. $error = implode(',', $error);
  391. return [false, $error];
  392. }
  393. return [true, $contact_info];
  394. }
  395. function customerFilterSecond($array, $time, $head, $user, $contact_info_array,$customer,$basic_list,$product,$emp){
  396. $error = $insert = $insert_detail = $insert_detail2 = [];
  397. foreach ($array as $key => $value){
  398. $line = '第' . $key . '行';
  399. $tmp = [
  400. 'model_type' => '',
  401. 'title' => '',
  402. 'customer_intention' => '',
  403. 'customer_from' => '',
  404. 'customer_type' => '',
  405. 'car_type' => '',
  406. 'consulting_product' => '',
  407. 'intention_product' => '',
  408. 'progress_stage' => '',
  409. 'state_type' => '',
  410. 'address2' => '',
  411. 'mark' => '',
  412. 'depart_id' => $head,
  413. 'top_depart_id' => $head,
  414. 'crt_id' => $user['id'],
  415. 'crt_time' => $time,
  416. 'upd_time' => $time,
  417. 'enter_time' => $value['15'],
  418. ];
  419. $tmp['model_type'] = $value['0'];
  420. $tmp['consulting_product'] = $value['5'];
  421. $tmp['mark'] = $value['10'];
  422. $tmp['address2'] = $value['11'];
  423. if(! empty($customer[$value['1']])) {
  424. $error[] = $line . '客户名称:' . $value['1'] . '已存在';
  425. }else{
  426. $tmp['title'] = $value['1'];
  427. }
  428. if($value['2']){
  429. if(empty($basic_list[2][$value['2']])) {
  430. $error[] = $line . '客户意向度:' . $value['2'] . '不存在';
  431. }else{
  432. $tmp['customer_intention'] = $basic_list[2][$value['2']];
  433. }
  434. }
  435. if($value['3']){
  436. if(empty($basic_list[3][$value['3']])) {
  437. $error[] = $line . '客户来源:' . $value['3'] . '不存在';
  438. }else{
  439. $tmp['customer_from'] = $basic_list[3][$value['3']];
  440. }
  441. }
  442. if($value['4']){
  443. $keys = 4 . $value[0];
  444. $model_title = Customer::dk2[$value[0]] ?? "";
  445. if(empty($basic_list[$keys][$value['4']])) {
  446. $error[] = $line . '模板:' . $model_title . '下客户类别:' . $value['4'] . '不存在';
  447. }else{
  448. $tmp['customer_type'] = $basic_list[$keys][$value['4']];
  449. }
  450. }
  451. if($value['6']){
  452. if(empty($product[$value['6']])) {
  453. $error[] = $line . '意向产品:' . $value['6'] . '不存在';
  454. }else{
  455. $tmp['intention_product'] = $product[$value['6']];
  456. }
  457. }
  458. if($value['7']){
  459. if(empty($basic_list[7][$value['7']])) {
  460. $error[] = $line . '进展阶段:' . $value['7'] . '不存在';
  461. }else{
  462. $tmp['progress_stage'] = $basic_list[7][$value['7']];
  463. }
  464. }
  465. if($value['8']){
  466. if(empty($basic_list[8][$value['8']])) {
  467. $error[] = $line . '状态:' . $value['8'] . '不存在';
  468. }else{
  469. $tmp['state_type'] = $basic_list[8][$value['8']];
  470. }
  471. }
  472. if($value['9']){
  473. if(empty($basic_list[9][$value['9']])) {
  474. $error[] = $line . '车型:' . $value['9'] . '不存在';
  475. }else{
  476. $tmp['car_type'] = $basic_list[9][$value['9']];
  477. }
  478. }
  479. $contact_id = 0;
  480. if($value['12']){
  481. if(empty($basic_list[12][$value['12']])) {
  482. $error[] = $line . '联系方式类型:' . $value['12'] . '不存在';
  483. }else{
  484. $contact_id = $basic_list[12][$value['12']];
  485. }
  486. }
  487. if($value['13'] && in_array($value['13'],$contact_info_array)){
  488. $error[] = $line . '联系方式内容:' . $value['13'] . '已存在';
  489. }
  490. $man = 0;
  491. if($value['14']){
  492. if(empty($emp[$value['14']])) {
  493. $error[] = $line . '负责人:' . $value['14'] . '不存在';
  494. }else{
  495. $man = $emp[$value['14']];
  496. }
  497. }
  498. // if($value['12']){
  499. // if(empty($emp[$value['12']])) return [false, '协同人:' . $value['12'] . '不存在'];
  500. // $tmp['emp_two'] = $emp[$value['12']];
  501. // }
  502. $insert[] = $tmp;
  503. $insert_detail[] = [
  504. 'customer_id' => 0,
  505. 'contact_type' => $contact_id,
  506. 'contact_info' => $value['13'],
  507. 'crt_time' => $time,
  508. 'type' => CustomerInfo::type_one
  509. ];
  510. $insert_detail2[] = [
  511. 'customer_id' => 0,
  512. 'data_id' => $man,
  513. 'crt_time' => $time,
  514. 'type' => CustomerInfo::type_two
  515. ];
  516. }
  517. if(! empty($error)){
  518. $error = implode(',', $error);
  519. return [false, $error];
  520. }
  521. return [true, [$insert, $insert_detail, $insert_detail2]];
  522. }
  523. function convertExcelCellToDate($cellValue) {
  524. // 尝试将单元格值转换为浮点数(Excel 日期序列号)
  525. $excelTimestamp = filter_var($cellValue, FILTER_VALIDATE_FLOAT);
  526. if ($excelTimestamp !== false && $excelTimestamp > 0) {
  527. // 如果成功转换并且值大于0,则认为是Excel日期序列号
  528. try {
  529. $dateTimeObject = Date::excelToDateTimeObject($cellValue);
  530. if ($dateTimeObject->format('H:i:s') === '00:00:00') {
  531. // 如果是,则将时间设置为 '23:59:00'
  532. $dateTimeObject->setTime(23, 59);
  533. }
  534. // 现在你可以格式化这个日期了
  535. $formattedDate = $dateTimeObject->format('Y-m-d H:i:s');
  536. if(! strtotime($formattedDate)) return [false, '录入日期请填写正确的日期'];
  537. return [true, strtotime($formattedDate)];
  538. } catch (\Exception $e) {
  539. // 处理转换失败的情况
  540. return [false, '单元格日期格式转换时间戳失败'];
  541. }
  542. }
  543. // 如果不是有效的浮点数,则尝试按照多种日期格式解析
  544. if(! strtotime($cellValue)) return [false, '单元格文本格式转换时间戳失败'];
  545. return [true, strtotime($cellValue)];
  546. }
  547. public function productImport($array, $user){
  548. //当前门店
  549. $depart_id = $this->getDepart($user);
  550. $top_depart_id = $user['depart_map'][$depart_id] ?? 0;
  551. // 去除表头
  552. $upload = $array[0];
  553. unset($array[0]);
  554. if(empty($array)) return [false, '导入数据不能为空'];
  555. //第一次表格数据校验 非空 已经过滤数据
  556. $array_clean = [];
  557. $search = "";
  558. $map_attr = array_flip(Product::$product_attribute);
  559. foreach ($array as $key => $value){
  560. $rowData = array_filter($value);
  561. if (empty($rowData)) {
  562. unset($array[$key]);
  563. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2])) {
  564. return [false, '带*号的字段项必填'];
  565. }else{
  566. foreach ($value as $k => $v){
  567. $value[$k] = trim($v);
  568. }
  569. $t = $value[1];
  570. if(in_array($t,$array_clean)) return [false, '产品编码:'. $value[1] .'在文件中重复出现'];
  571. $array_clean[] = $t;
  572. if(! empty($value[8])){
  573. if(! isset($map_attr[$value[8]])) return [false, '产品属性不存在' . $value[8]];
  574. $value[8] = $map_attr[$value[8]];
  575. }else{
  576. $value[8] = Product::Product_attribute_zero;
  577. }
  578. $array[$key] = $value;
  579. $search .= "(binary code ='".$value[1]."') or";
  580. }
  581. }unset($array_clean);
  582. if(empty($array)) return [false, '导入数据不能为空'];
  583. $search = rtrim($search,' or');
  584. $search = "($search)";
  585. $product = Product::whereRaw($search)
  586. ->where('del_time',0)
  587. ->select('code','top_depart_id','id')
  588. ->get()->toArray();
  589. $product_array = [];
  590. foreach ($product as $value){
  591. $product_array[$value['code']] = [
  592. 'id' => $value['id'],
  593. 'top_depart_id' => $value['top_depart_id']
  594. ];
  595. }
  596. //产品分类
  597. $category_list = ProductCategory::where('del_time',0)
  598. ->select('id','title','parent_id')
  599. ->where('top_depart_id',$top_depart_id)
  600. ->get()->toArray();
  601. // 创建一个以 id 为键的数据映射
  602. $idMap = [];
  603. foreach ($category_list as $item) {
  604. $idMap[$item['id']] = $item;
  605. }
  606. // 创建最终的结果数组
  607. $category_parent = [];
  608. foreach ($idMap as $id => $item) {
  609. // 如果没有子项,则处理
  610. if (! $this->hasChildren($id, $idMap)) {
  611. $category_parent[$id] = $this->getAncestors($id, $idMap);
  612. }
  613. }
  614. $category_map = array_column($category_list,'id','title');
  615. //基础类型
  616. $basic = (new BasicTypeService())->getMyBasicList($user, 20);
  617. $basic = array_column($basic,'id','title');
  618. $time = time();
  619. //表头
  620. $table_head = $this->product([],$user);
  621. $heads = $table_head[1];
  622. $head_value = array_column($heads,'value');
  623. if($head_value !== $upload) return [false, '产品模板错误,请重新下载模板导入'];
  624. //写入数据库的字段
  625. $tmp = array_column($heads,'key');
  626. $tmp = array_fill_keys($tmp, '');
  627. $tmp['product_category'] = '';
  628. $tmp['upd_time'] = $time;
  629. $top_message = Depart::where('parent_id',0)
  630. ->pluck('title','id')
  631. ->toArray();
  632. $upload = array_flip($upload);
  633. $map = [];
  634. foreach ($heads as $value){
  635. if(strpos($value['key'], 'table_id.') !== false && isset($upload[$value['value']])){
  636. $map[$value['key']] = [
  637. 'col' => $upload[$value['value']],
  638. 'name' => $value['value']
  639. ];
  640. }
  641. }
  642. $array = array_values($array);
  643. $insert = $insert2 = $update = $update2 = [];
  644. foreach ($array as $value){
  645. if(isset($product_array[$value['1']])){
  646. $pro_tmp = $product_array[$value['1']] ?? [];
  647. if($pro_tmp['top_depart_id'] != $top_depart_id){
  648. $belong = $top_message[$pro_tmp['top_depart_id']] ?? "";
  649. $now = $top_message[$top_depart_id] ?? "";
  650. return [false, '产品编码:' . $value['1'] . '属于门店:' . $belong . ',当前门店:' . $now . ',不允许跨门店操作更新产品!'];
  651. }
  652. }
  653. $tmp['title'] = $value['0'];
  654. $tmp['code'] = $value['1'];
  655. if(empty($category_map[$value['2']])) return [false,'产品分类:' . $value['2'] . '不存在'];
  656. $tmp['product_category_id'] = $category_map[$value['2']];
  657. if(! isset($category_parent[$tmp['product_category_id']])) return [false,'产品分类:' . $value['2'] . '下存在子分类,请将产品建与最底层分类下'];
  658. $parentsId = $category_parent[$tmp['product_category_id']];
  659. $tmp['product_category'] = json_encode($parentsId);
  660. $tmp['size'] = $value['3'];
  661. if($value['4']){
  662. if(empty($basic[$value['4']])) return [false, '单位:' . $value['4'] . '不存在'];
  663. $tmp['unit'] = $basic[$value['4']];
  664. }
  665. $tmp['bar_code'] = $value['5'];
  666. $tmp['cost'] = $value['6'] ?? 0;
  667. $tmp['retail_price'] = $value['7'] ?? 0;
  668. $tmp['product_attribute'] = $value['8'] ?? 0;
  669. $tmp['build_fee'] = $value['9'] ?? 0;
  670. $tmp['item_code'] = $value['10'] ?? "";
  671. foreach ($map as $m => $v){
  672. if($value[$v['col']]){
  673. if(! is_numeric($value[$v['col']])) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  674. $formattedNumber = number_format($value[$v['col']], 2, '.', '');
  675. if($formattedNumber != $value[$v['col']]) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  676. }
  677. $tmp[$m] = $value[$v['col']];
  678. }
  679. if(isset($product_array[$value['1']])){
  680. //更新
  681. $pro_tmp = $product_array[$value['1']] ?? [];
  682. $product_id = $pro_tmp['id'];
  683. //产品价格子表
  684. foreach ($tmp as $k => $v){
  685. if(strpos($k, 'table_id.') !== false){
  686. $tmp2 = [];
  687. $k_n = str_replace('table_id.', "", $k);
  688. $tmp2['product_id'] = $product_id;
  689. $tmp2['basic_type_id'] = $k_n;
  690. $tmp2['price'] = $v;
  691. // $tmp2['crt_time'] = $time;
  692. // $tmp2['upd_time'] = $time;
  693. $update2[] = $tmp2;
  694. unset($tmp[$k]);
  695. }
  696. }
  697. //安装费
  698. if($tmp['build_fee'] == "") unset($tmp['build_fee']);
  699. //商品编码
  700. if($tmp['item_code'] == "") unset($tmp['item_code']);
  701. //产品主表
  702. $update[$product_id] = $tmp;
  703. }else{
  704. $tmp['depart_id'] = $depart_id;
  705. $tmp['top_depart_id'] = $top_depart_id;
  706. $tmp['crt_id'] = $user['id'];
  707. $tmp['crt_time'] = $time;
  708. //产品价格子表
  709. foreach ($tmp as $k => $v){
  710. if(strpos($k, 'table_id.') !== false){
  711. $tmp2 = [];
  712. $k_n = str_replace('table_id.', "", $k);
  713. $tmp2['basic_type_id'] = $k_n;
  714. $tmp2['price'] = $v;
  715. // $tmp2['crt_time'] = $time;
  716. // $tmp2['upd_time'] = $time;
  717. $insert2[$tmp['code']][] = $tmp2;
  718. unset($tmp[$k]);
  719. }
  720. }
  721. //产品主表
  722. $insert[$tmp['code']] = $tmp;
  723. }
  724. }
  725. try{
  726. DB::beginTransaction();
  727. //新增
  728. if(! empty($insert)){
  729. Product::insert($insert);
  730. if(! empty($insert2)){
  731. $insert_detail = [];
  732. $last_insert_id = Product::where('crt_time',$time)
  733. ->pluck('id','code')
  734. ->toArray();
  735. foreach ($insert2 as $code => $val){
  736. foreach ($val as $v2){
  737. $v2['product_id'] = $last_insert_id[$code] ?? 0;
  738. $insert_detail[] = $v2;
  739. }
  740. }
  741. ProductPriceDetail::insert($insert_detail);
  742. }
  743. }
  744. //编辑
  745. if(! empty($update)){
  746. foreach ($update as $p_id => $value){
  747. Product::where('id',$p_id)
  748. ->update($value);
  749. }
  750. if(! empty($update2)){
  751. $product_id_array = array_keys($update);
  752. ProductPriceDetail::whereIn('product_id',$product_id_array)
  753. ->update(['del_time' => $time]);
  754. ProductPriceDetail::insert($update2);
  755. }
  756. }
  757. DB::commit();
  758. }catch (\Exception $e){
  759. DB::rollBack();
  760. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  761. }
  762. return [true, ''];
  763. }
  764. function convertHyphens($string) {
  765. // 定义要被替换的字符
  766. $search = array('-', '-', '—'); // 全角连字符、半角连字符 和 破折号
  767. $replace = '-'; // 替换成半角连字符
  768. // 执行替换
  769. $res = str_replace($search, $replace, $string);
  770. return $res;
  771. }
  772. function generateTitleToIdMap($tree, $prefix = '') {
  773. $result = [];
  774. foreach ($tree as $node) {
  775. $currentTitle = $prefix . $node['title'];
  776. $result[$currentTitle] = $node['id'];
  777. if (!empty($node['children'])) {
  778. $result = array_merge(
  779. $result,
  780. $this->generateTitleToIdMap($node['children'], $currentTitle . '-')
  781. );
  782. }
  783. }
  784. return $result;
  785. }
  786. function getAncestors($id, &$idMap) {
  787. if (!isset($idMap[$id])) return [];
  788. $ancestors = [$id];
  789. $parentId = $idMap[$id]['parent_id'];
  790. if ($parentId !== null) {
  791. // 递归调用自己来添加父级 ID
  792. $ancestors = array_merge($this->getAncestors($parentId, $idMap), $ancestors);
  793. }
  794. return $ancestors;
  795. }
  796. // 检查是否拥有子项
  797. function hasChildren($id, &$idMap) {
  798. foreach ($idMap as $item) {
  799. if ($item['parent_id'] == $id) {
  800. return true;
  801. }
  802. }
  803. return false;
  804. }
  805. public function salesOnlineImport($array, $user){
  806. $head = $user['head']['id'] ?? 0;
  807. // 去除表头
  808. unset($array[0]);
  809. if(empty($array)) return [false, '导入数据不能为空'];
  810. $basic = (new BasicTypeService())->getMyBasicList($user, [18,23,24,29]);
  811. $basic_list = [];
  812. foreach ($basic as $value){
  813. if($value['type'] == 18){
  814. $basic_list[9][$value['title']] = $value['id'];
  815. }elseif ($value['type'] == 23){
  816. $basic_list[10][$value['title']] = $value['id'];
  817. }elseif ($value['type'] == 24){
  818. $basic_list[3][$value['title']] = $value['id'];
  819. }elseif ($value['type'] == 29){
  820. $basic_list[2][$value['title']] = $value['id'];
  821. }
  822. }
  823. $search = "";
  824. $customer = [];
  825. foreach ($array as $key => $value){
  826. $rowData = array_filter($value);
  827. if (empty($rowData)) {
  828. unset($array[$key]);
  829. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || $value[6] === null) {
  830. return [false, '带*号的字段项必填'];
  831. }else{
  832. foreach ($value as $k => $v){
  833. $value[$k] = trim($v);
  834. }
  835. if(! isset($basic_list[2][$value[2]])) return [false, '客户简称:' . $value[2] .'不存在'];
  836. $value[2] = $basic_list[2][$value[2]];
  837. if(! isset($basic_list[3][$value[3]])) return [false, '店铺(平台类型):' . $value[3] .'不存在'];
  838. $value[3] = $basic_list[3][$value[3]];
  839. if(! is_numeric($value[5])) return [false, '货品数量请填写正确的数值'];
  840. if(! is_numeric($value[6])) return [false, '产品合同金额请填写正确的数值'];
  841. if(! empty($value[8]) && ! is_numeric($value[8])) return [false, '订单优惠金额请填写正确的数值'];
  842. if(! empty($value[9])){
  843. if(! isset($basic_list[9][$value[9]])) return [false, '安装方式:' . $value[9] .'不存在'];
  844. $value[9] = $basic_list[9][$value[9]];
  845. }
  846. if(! empty($value[10])){
  847. if(! isset($basic_list[10][$value[10]])) return [false, '安装地点:' . $value[10] .'不存在'];
  848. $value[10] = $basic_list[10][$value[10]];
  849. }
  850. if(! isset(SalesOrder::$order_type_name[$value[1]])) return [false, '产品类型填写错误'];
  851. $value[1] = SalesOrder::$order_type_name[$value[1]];
  852. if(! empty($value[11])) {
  853. list($status,$msg) = $this->changeAndReturnDate($value[11]);
  854. if(! $status) return [false,"施工日期请填写正确的日期格式,例如2023-05-01"];
  855. $value[11] = $msg;
  856. }
  857. if(! empty($value[12])) {
  858. list($status,$msg) = $this->changeAndReturnDate($value[12]);
  859. if(! $status) return [false,"交车日期请填写正确的日期格式,例如2023-05-01"];
  860. $value[12] = $msg;
  861. }
  862. $array[$key] = $value;
  863. $search .= "(code = '".$value[4]."') or";
  864. if(! empty($value[7]) && ! in_array($value[7], $customer)) $customer[] = $value[7];
  865. }
  866. }
  867. if(empty($array)) return [false, '导入数据不能为空'];
  868. $search = rtrim($search,' or');
  869. $search = "($search)";
  870. $model = Product::ProductClear($user,[]);
  871. $product = $model->whereRaw($search)
  872. ->where('del_time',0)
  873. ->select('title','id','code','cost','retail_price')
  874. ->get()->toArray();
  875. // $pro = (new ProductService())->productList(['product_id' => array_column($product,'id'), 'type' => 2],$user);
  876. $product_map = [];
  877. foreach ($product as $value){
  878. $product_map[$value['code']] = [
  879. 'id' => $value['id'],
  880. 'cost' => $value['cost'],
  881. 'retail_price' => $value['retail_price'],
  882. ];
  883. }
  884. $time = time();
  885. $model = Customer::Clear($user,[]);
  886. $customer_map = $model->where('del_time',0)
  887. ->whereIn('title',$customer)
  888. ->pluck('id','title')
  889. ->toArray();
  890. $customer_info = CustomerInfo::where('del_time',0)
  891. ->whereIn('customer_id',array_values($customer_map))
  892. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  893. ->select('customer_id','type','contact_info','data_id')
  894. ->orderBy('id','asc')
  895. ->get()->toArray();
  896. $customer_contact = $customer_man = [];
  897. foreach ($customer_info as $value){
  898. if($value['type'] == CustomerInfo::type_one && ! isset($customer_contact[$value['customer_id']])){
  899. $customer_contact[$value['customer_id']] = $value['contact_info'];
  900. }elseif ($value['type'] == CustomerInfo::type_two){
  901. $customer_man[$value['customer_id']][] = [
  902. 'type' => SalesOrderInfo::type_two,
  903. 'data_id' => $value['data_id'],
  904. 'crt_time' => $time,
  905. ];
  906. }
  907. }
  908. $tmp = [
  909. 'model_type' => SalesOrder::Model_type_four,
  910. 'sales_order_type' => 0,
  911. 'order_number' => '',
  912. 'customer_short_name' => '',
  913. 'plat_type' => '',
  914. 'plat_order' => '',
  915. 'sign_time' => $time,
  916. 'product_total' => 0,
  917. 'contract_fee' => 0,
  918. 'discount_fee' => 0,
  919. 'cdefine29' => '',//分社施工
  920. 'cdefine32' => '',//达人昵称
  921. 'cdefine30' => '',//业务员
  922. 'rate' => 100,
  923. 'depart_id' => $head,
  924. 'top_depart_id' => $head,
  925. 'crt_id' => $user['id'],
  926. 'crt_time' => $time,
  927. 'upd_time' => $time,
  928. ];
  929. $tmp_detail = [
  930. 'sales_order_id' => 0,
  931. 'product_id' => 0,
  932. 'cost' => 0,
  933. 'retail_price' => 0,
  934. 'basic_type_id' => 0,
  935. 'price' => 0,
  936. 'final_amount' => 0,
  937. 'number' => '',
  938. 'crt_time' => $time,
  939. ];
  940. $insert = $insert_detail = $insert_detail_man = [];
  941. $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
  942. foreach ($array as $value){
  943. $product_str = $value[4];
  944. if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[4]. ']' . '不存在'];
  945. $product_tmp = $product_map[$product_str] ?? [];
  946. $customer_tmp = 0;
  947. $customer_contact_tmp = "";
  948. $customer_man_tmp = [];
  949. if(! empty($value[7])){
  950. $customer_tmp = $customer_map[$value[7]] ?? 0;
  951. $customer_contact_tmp = $customer_contact[$customer_tmp] ?? "";
  952. $customer_man_tmp = $customer_man[$customer_tmp] ?? [];
  953. }
  954. $customer_man_tmp[] = [
  955. 'type' => SalesOrderInfo::type_one,
  956. 'data_id' => $user['id'],
  957. 'crt_time' => $time,
  958. ];
  959. $tmp['product_total'] = $tmp['contract_fee'] = 0;
  960. $keys = $value[0] . $value[1];
  961. $insert_detail_man[$keys] = $customer_man_tmp;
  962. if(! isset($insert[$keys])){
  963. $tmp['order_number'] = OrderNoService::createSalesOrderNumberImport($prefix);
  964. $tmp['sales_order_type'] = $value[1];
  965. $tmp['customer_short_name'] = $value[2];
  966. $tmp['customer_id'] = $customer_tmp;
  967. $tmp['customer_contact'] = $customer_contact_tmp;
  968. $tmp['plat_type'] = $value[3];
  969. $tmp['plat_order'] = $value[0];
  970. $tmp['discount_fee'] = $value[8] ?: 0;
  971. $tmp['cdefine29'] = $value[13] ?? "";//分社施工
  972. $tmp['cdefine32'] = $value[14] ?? "";//达人昵称
  973. $tmp['cdefine30'] = $value[15] ?? "";//业务员
  974. $tmp['product_total'] += $value[6];
  975. $tmp['contract_fee'] += $value[6] - $tmp['discount_fee'];
  976. $tmp['construction_time'] = $value[11] ?? 0;
  977. $tmp['handover_time'] = $value[12] ?? 0;
  978. $insert[$keys] = $tmp;
  979. }else{
  980. $insert[$keys]['product_total'] += $value[6];
  981. $insert[$keys]['contract_fee'] += $value[6];
  982. }
  983. $tmp_detail['product_id'] = $product_tmp['id'];
  984. $tmp_detail['cost'] = $product_tmp['cost'];
  985. $tmp_detail['retail_price'] = $product_tmp['retail_price'];
  986. $tmp_detail['price'] = $product_tmp['retail_price'];
  987. $tmp_detail['final_amount'] = $value[6];
  988. $tmp_detail['number'] = $value[5];
  989. $insert_detail[$keys][] = $tmp_detail;
  990. }
  991. $insert_detail = array_values($insert_detail);
  992. $insert_detail_man = array_values($insert_detail_man);
  993. try{
  994. DB::beginTransaction();
  995. if(! empty($insert)) SalesOrder::insert($insert);
  996. $last_insert_id = SalesOrder::where('crt_time',$time)
  997. ->select('id')
  998. ->get()->toArray();
  999. $last_insert_id = array_column($last_insert_id,'id');
  1000. if(! empty($insert_detail)){
  1001. $insert2 = [];
  1002. foreach ($last_insert_id as $key => $value){
  1003. if(isset($insert_detail[$key])) {
  1004. foreach ($insert_detail[$key] as $val){
  1005. $val['sales_order_id'] = $value;
  1006. $insert2[] = $val;
  1007. }
  1008. }
  1009. }
  1010. SalesOrderProductInfo::insert($insert2);
  1011. }
  1012. if(! empty($insert_detail_man)){
  1013. $insert3 = [];
  1014. foreach ($last_insert_id as $key => $value){
  1015. if(isset($insert_detail_man[$key])) {
  1016. foreach ($insert_detail_man[$key] as $val){
  1017. $val['sales_order_id'] = $value;
  1018. $insert3[] = $val;
  1019. }
  1020. }
  1021. }
  1022. SalesOrderInfo::insert($insert3);
  1023. }
  1024. DB::commit();
  1025. }catch (\Exception $e){
  1026. DB::rollBack();
  1027. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  1028. }
  1029. return [true, ''];
  1030. }
  1031. public function btOnlineImport($array, $user){
  1032. $head = $user['head']['id'] ?? 0;
  1033. // 去除表头
  1034. unset($array[0]);
  1035. if(empty($array)) return [false, '导入数据不能为空'];
  1036. $basic = (new BasicTypeService())->getMyBasicList($user, [18,23,24,29]);
  1037. $basic_list = [];
  1038. foreach ($basic as $value){
  1039. if($value['type'] == 18){
  1040. $basic_list[9][$value['title']] = $value['id'];
  1041. }elseif ($value['type'] == 23){
  1042. $basic_list[10][$value['title']] = $value['id'];
  1043. }elseif ($value['type'] == 24){
  1044. $basic_list[3][$value['title']] = $value['id'];
  1045. }elseif ($value['type'] == 29){
  1046. $basic_list[2][$value['title']] = $value['id'];
  1047. }
  1048. }
  1049. $search = "";
  1050. $customer = [];
  1051. foreach ($array as $key => $value){
  1052. $rowData = array_filter($value);
  1053. if (empty($rowData)) {
  1054. unset($array[$key]);
  1055. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || $value[6] === null) {
  1056. return [false, '带*号的字段项必填'];
  1057. }else{
  1058. foreach ($value as $k => $v){
  1059. $value[$k] = trim($v);
  1060. }
  1061. if(! isset($basic_list[2][$value[2]])) return [false, '客户简称:' . $value[2] .'不存在'];
  1062. $value[2] = $basic_list[2][$value[2]];
  1063. if(! isset($basic_list[3][$value[3]])) return [false, '店铺(平台类型):' . $value[3] .'不存在'];
  1064. $value[3] = $basic_list[3][$value[3]];
  1065. if(! is_numeric($value[5])) return [false, '货品数量请填写正确的数值'];
  1066. if(! is_numeric($value[6])) return [false, '产品合同金额请填写正确的数值'];
  1067. if(! empty($value[8]) && ! is_numeric($value[8])) return [false, '订单优惠金额请填写正确的数值'];
  1068. if(! empty($value[9])){
  1069. if(! isset($basic_list[9][$value[9]])) return [false, '安装方式:' . $value[9] .'不存在'];
  1070. $value[9] = $basic_list[9][$value[9]];
  1071. }
  1072. if(! empty($value[10])){
  1073. if(! isset($basic_list[10][$value[10]])) return [false, '安装地点:' . $value[10] .'不存在'];
  1074. $value[10] = $basic_list[10][$value[10]];
  1075. }
  1076. if(! isset(SalesOrder::$order_type_name[$value[1]])) return [false, '产品类型填写错误'];
  1077. $value[1] = SalesOrder::$order_type_name[$value[1]];
  1078. if(! empty($value[11])) {
  1079. list($status,$msg) = $this->changeAndReturnDate($value[11]);
  1080. if(! $status) return [false,"施工日期请填写正确的日期格式,例如2023-05-01"];
  1081. $value[11] = $msg;
  1082. }
  1083. if(! empty($value[12])) {
  1084. list($status,$msg) = $this->changeAndReturnDate($value[12]);
  1085. if(! $status) return [false,"交车日期请填写正确的日期格式,例如2023-05-01"];
  1086. $value[12] = $msg;
  1087. }
  1088. $array[$key] = $value;
  1089. $search .= "(code = '".$value[4]."') or";
  1090. if(! empty($value[7]) && ! in_array($value[7], $customer)) $customer[] = $value[7];
  1091. }
  1092. }
  1093. if(empty($array)) return [false, '导入数据不能为空'];
  1094. $search = rtrim($search,' or');
  1095. $search = "($search)";
  1096. $model = Product::ProductClear($user,[]);
  1097. $product = $model->whereRaw($search)
  1098. ->where('del_time',0)
  1099. ->select('title','id','code','cost','retail_price')
  1100. ->get()->toArray();
  1101. $product_map = [];
  1102. foreach ($product as $value){
  1103. $product_map[$value['code']] = [
  1104. 'id' => $value['id'],
  1105. 'cost' => $value['cost'],
  1106. 'retail_price' => $value['retail_price'],
  1107. ];
  1108. }
  1109. $time = time();
  1110. $model = Customer::Clear($user,[]);
  1111. $customer_map = $model->where('del_time',0)
  1112. ->whereIn('title',$customer)
  1113. ->pluck('id','title')
  1114. ->toArray();
  1115. $customer_info = CustomerInfo::where('del_time',0)
  1116. ->whereIn('customer_id',array_values($customer_map))
  1117. ->whereIn('type',[CustomerInfo::type_one,CustomerInfo::type_two])
  1118. ->select('customer_id','type','contact_info','data_id')
  1119. ->orderBy('id','asc')
  1120. ->get()->toArray();
  1121. $customer_contact = $customer_man = [];
  1122. foreach ($customer_info as $value){
  1123. if($value['type'] == CustomerInfo::type_one && ! isset($customer_contact[$value['customer_id']])){
  1124. $customer_contact[$value['customer_id']] = $value['contact_info'];
  1125. }elseif ($value['type'] == CustomerInfo::type_two){
  1126. $customer_man[$value['customer_id']][] = [
  1127. 'type' => SalesOrderInfo::type_two,
  1128. 'data_id' => $value['data_id'],
  1129. 'crt_time' => $time,
  1130. ];
  1131. }
  1132. }
  1133. $tmp = [
  1134. 'model_type' => SalesOrder::Model_type_seven,
  1135. 'sales_order_type' => 0,
  1136. 'order_number' => '',
  1137. 'customer_short_name' => '',
  1138. 'plat_type' => '',
  1139. 'plat_order' => '',
  1140. 'sign_time' => $time,
  1141. 'product_total' => 0,
  1142. 'contract_fee' => 0,
  1143. 'discount_fee' => 0,
  1144. 'cdefine29' => '',//分社施工
  1145. 'cdefine32' => '',//达人昵称
  1146. 'cdefine30' => '',//业务员
  1147. 'rate' => 100,
  1148. 'depart_id' => $head,
  1149. 'top_depart_id' => $head,
  1150. 'crt_id' => $user['id'],
  1151. 'crt_time' => $time,
  1152. 'upd_time' => $time,
  1153. ];
  1154. $tmp_detail = [
  1155. 'sales_order_id' => 0,
  1156. 'product_id' => 0,
  1157. 'cost' => 0,
  1158. 'retail_price' => 0,
  1159. 'basic_type_id' => 0,
  1160. 'price' => 0,
  1161. 'final_amount' => 0,
  1162. 'number' => '',
  1163. 'crt_time' => $time,
  1164. ];
  1165. $insert = $insert_detail = $insert_detail_man = [];
  1166. $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
  1167. foreach ($array as $value){
  1168. $product_str = $value[4];
  1169. if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[4]. ']' . '不存在'];
  1170. $product_tmp = $product_map[$product_str] ?? [];
  1171. $customer_tmp = 0;
  1172. $customer_contact_tmp = "";
  1173. $customer_man_tmp = [];
  1174. if(! empty($value[7])){
  1175. $customer_tmp = $customer_map[$value[7]] ?? 0;
  1176. $customer_contact_tmp = $customer_contact[$customer_tmp] ?? "";
  1177. $customer_man_tmp = $customer_man[$customer_tmp] ?? [];
  1178. }
  1179. $customer_man_tmp[] = [
  1180. 'type' => SalesOrderInfo::type_one,
  1181. 'data_id' => $user['id'],
  1182. 'crt_time' => $time,
  1183. ];
  1184. $tmp['product_total'] = $tmp['contract_fee'] = 0;
  1185. $keys = $value[0] . $value[1];
  1186. $insert_detail_man[$keys] = $customer_man_tmp;
  1187. if(! isset($insert[$keys])){
  1188. $tmp['order_number'] = OrderNoService::createSalesOrderNumberImport($prefix);
  1189. $tmp['sales_order_type'] = $value[1];
  1190. $tmp['customer_short_name'] = $value[2];
  1191. $tmp['customer_id'] = $customer_tmp;
  1192. $tmp['customer_contact'] = $customer_contact_tmp;
  1193. $tmp['plat_type'] = $value[3];
  1194. $tmp['plat_order'] = $value[0];
  1195. $tmp['discount_fee'] = $value[8] ?: 0;
  1196. $tmp['cdefine29'] = $value[13] ?? "";//分社施工
  1197. $tmp['cdefine32'] = $value[14] ?? "";//达人昵称
  1198. $tmp['cdefine30'] = $value[15] ?? "";//业务员
  1199. $tmp['product_total'] += $value[6];
  1200. $tmp['contract_fee'] += $value[6] - $tmp['discount_fee'];
  1201. $tmp['construction_time'] = $value[11] ?? 0;
  1202. $tmp['handover_time'] = $value[12] ?? 0;
  1203. $insert[$keys] = $tmp;
  1204. }else{
  1205. $insert[$keys]['product_total'] += $value[6];
  1206. $insert[$keys]['contract_fee'] += $value[6];
  1207. }
  1208. $tmp_detail['product_id'] = $product_tmp['id'];
  1209. $tmp_detail['cost'] = $product_tmp['cost'];
  1210. $tmp_detail['retail_price'] = $product_tmp['retail_price'];
  1211. $tmp_detail['price'] = $product_tmp['retail_price'];
  1212. $tmp_detail['final_amount'] = $value[6];
  1213. $tmp_detail['number'] = $value[5];
  1214. $insert_detail[$keys][] = $tmp_detail;
  1215. }
  1216. $insert_detail = array_values($insert_detail);
  1217. $insert_detail_man = array_values($insert_detail_man);
  1218. try{
  1219. DB::beginTransaction();
  1220. if(! empty($insert)) SalesOrder::insert($insert);
  1221. $last_insert_id = SalesOrder::where('crt_time',$time)
  1222. ->select('id')
  1223. ->get()->toArray();
  1224. $last_insert_id = array_column($last_insert_id,'id');
  1225. if(! empty($insert_detail)){
  1226. $insert2 = [];
  1227. foreach ($last_insert_id as $key => $value){
  1228. if(isset($insert_detail[$key])) {
  1229. foreach ($insert_detail[$key] as $val){
  1230. $val['sales_order_id'] = $value;
  1231. $insert2[] = $val;
  1232. }
  1233. }
  1234. }
  1235. SalesOrderProductInfo::insert($insert2);
  1236. }
  1237. if(! empty($insert_detail_man)){
  1238. $insert3 = [];
  1239. foreach ($last_insert_id as $key => $value){
  1240. if(isset($insert_detail_man[$key])) {
  1241. foreach ($insert_detail_man[$key] as $val){
  1242. $val['sales_order_id'] = $value;
  1243. $insert3[] = $val;
  1244. }
  1245. }
  1246. }
  1247. SalesOrderInfo::insert($insert3);
  1248. }
  1249. DB::commit();
  1250. }catch (\Exception $e){
  1251. DB::rollBack();
  1252. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  1253. }
  1254. return [true, ''];
  1255. }
  1256. public function lastJcImport($array, $user){
  1257. $head = $user['head']['id'] ?? 0;
  1258. // 去除表头
  1259. unset($array[0]);
  1260. if(empty($array)) return [false, '导入数据不能为空'];
  1261. $shop_name = $product_code = [];
  1262. foreach ($array as $key => $value){
  1263. $rowData = array_filter($value);
  1264. if (empty($rowData)) {
  1265. unset($array[$key]);
  1266. } elseif(empty($value[0]) || empty($value[1])) {
  1267. return [false, '带*号的字段项必填'];
  1268. }else{
  1269. foreach ($value as $k => $v){
  1270. $value[$k] = trim($v);
  1271. }
  1272. if(! is_numeric($value[2])) return [false, '* 上月结存数量请填写正确的数值'];
  1273. if(! is_numeric($value[3])) return [false, '* 上月结存单价请填写正确的数值'];
  1274. if(! is_numeric($value[4])) return [false, '* 上月结存金额请填写正确的数值'];
  1275. if(! in_array($value[0],$shop_name)) $shop_name[] = $value[0];
  1276. if(! in_array($value[1],$product_code)) $product_code[] = $value[1];
  1277. }
  1278. }
  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. $time = time();
  1293. $insert = [];
  1294. $last_month_stamp = strtotime(date('Y-m-t 23:59:59', strtotime('last month')));
  1295. foreach ($array as $value){
  1296. $depart_tmp = $depart_map[$value[0]] ?? 0;
  1297. if($depart_tmp <= 0) return [false, "门店:" . $value[0] . "不存在或已被删除"];
  1298. $product_tmp = $product_map[$value[1]] ?? 0;
  1299. if($product_tmp <= 0) return [false, "产品编码:" . $value[1] . "不存在或已被删除"];
  1300. $storehouse_tmp = $storehouse_map[$depart_tmp] ?? 0;
  1301. $insert[] = [
  1302. 'top_depart_id' => $depart_tmp,
  1303. 'product_id' => $product_tmp,
  1304. 'storehouse_id' => $storehouse_tmp,
  1305. 'number' => $value[2],
  1306. 'price' => $value[3],
  1307. 'total' => $value[4],
  1308. 'crt_time' => $time,
  1309. 'time' => $last_month_stamp
  1310. ];
  1311. }
  1312. if(empty($insert)) return [false, '暂无更新的数据'];
  1313. try{
  1314. DB::beginTransaction();
  1315. //更新数据为删除
  1316. foreach ($insert as $value){
  1317. LastJc::where('del_time',0)
  1318. ->where('time', $last_month_stamp)
  1319. ->where('top_depart_id',$value['top_depart_id'])
  1320. ->where('storehouse_id',$value['storehouse_id'])
  1321. ->where('product_id',$value['product_id'])
  1322. ->update(['del_time' => $time]);
  1323. }
  1324. //写入数据
  1325. LastJc::insert($insert);
  1326. DB::commit();
  1327. }catch (\Exception $e){
  1328. DB::rollBack();
  1329. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  1330. }
  1331. return [true, ''];
  1332. }
  1333. public function inventoryImport($array, $user){
  1334. $head = $user['head']['id'] ?? 0;
  1335. // 去除表头
  1336. unset($array[0]);
  1337. if(empty($array)) return [false, '导入数据不能为空'];
  1338. $shop_name = $product_code = [];
  1339. foreach ($array as $key => $value){
  1340. $rowData = array_filter($value);
  1341. if (empty($rowData)) {
  1342. unset($array[$key]);
  1343. } elseif(empty($value[0]) || empty($value[1])) {
  1344. if($key != 1){
  1345. if(empty($value[1])) return [false, '带*号的字段项必填'];
  1346. } else{
  1347. return [false, '带*号的字段项必填'];
  1348. }
  1349. }else{
  1350. foreach ($value as $k => $v){
  1351. $value[$k] = trim($v);
  1352. }
  1353. if($value[0] && ! in_array($value[0],$shop_name)) $shop_name[] = $value[0];
  1354. if(! in_array($value[1],$product_code)) {
  1355. $product_code[] = $value[1];
  1356. }else{
  1357. return [false, '产品编号:' . $value[1] .'已存在'];
  1358. }
  1359. if(! is_numeric($value[2])) return [false, '* 盘点数量请填写正确的数值'];
  1360. if(! empty($value[3])){
  1361. $bool = $this->checkNumber($value[3]);
  1362. if(! $bool) return [false, '盘点单价请输入大于等于0的两位小数的数字'];
  1363. }
  1364. }
  1365. }
  1366. if(count($shop_name) != 1) return [false, '只允许单门店盘点'];
  1367. //门店信息
  1368. $depart = Depart::whereIn('title',$shop_name)
  1369. ->where('del_time',0)
  1370. ->where('parent_id',0)
  1371. ->select('id','title','basic_type_id')
  1372. ->first();
  1373. if(empty($depart)) return [false, "门店:" .$shop_name . "不存在或已被删除"];
  1374. $depart = $depart->toArray();
  1375. $top_depart_id = $depart['id'];
  1376. $basic_type_id = $depart['basic_type_id'];
  1377. //门店的仓库
  1378. $storehouse = Storehouse::where('top_depart_id', $top_depart_id)
  1379. ->where('del_time',0)
  1380. ->select('id')
  1381. ->first();
  1382. if(empty($storehouse)) return [false, "门店:" .$shop_name . "的仓库不存在或已被删除"];
  1383. $storehouse = $storehouse->toArray();
  1384. $storehouse_id = $storehouse['id'];
  1385. if(empty($storehouse_id)) return [false, "门店:" .$shop_name . "的仓库不存在或已被删除"];
  1386. //产品
  1387. $model = Product::ProductClear($user,[]);
  1388. $product = $model->whereIn('code',$product_code)
  1389. ->where('del_time',0)
  1390. ->select('id','code','retail_price')
  1391. ->get()->toArray();
  1392. $product_map = array_column($product,'id','code');
  1393. $product_map_2 = array_column($product,'retail_price','id');
  1394. //产品使用价格
  1395. $product_use = [];
  1396. if(! empty($basic_type_id)){
  1397. $product_id = array_column($product,'id');
  1398. $service = new ProductService();
  1399. $detail_map = $service->getProductPrice($product_id, 1, $basic_type_id);
  1400. foreach ($detail_map as $key => $value){
  1401. if(isset($value[$basic_type_id]) && ! empty($value[$basic_type_id]['price'])) {
  1402. $product_use[$key] = $value[$basic_type_id]['price'];
  1403. }else{
  1404. $product_use[$key] = $product_map_2[$key] ?? 0;
  1405. }
  1406. }
  1407. }
  1408. $time = time();
  1409. $product_submit = $product_id = $sub = [];
  1410. foreach ($array as $value){
  1411. $product_tmp = $product_map[$value[1]] ?? 0;
  1412. if($product_tmp <= 0) return [false, "产品编码:" . $value[1] . "不存在或已被删除"];
  1413. if(isset($value[3])){
  1414. $bool = $this->checkNumber($value[3]);
  1415. if(! $bool) return [false, '盘点单价不合法,需是大于等于0的两位数值'];
  1416. $price = $value[3];
  1417. }else{
  1418. if(! isset($product_use[$product_tmp]) || floatval($product_use[$product_tmp]) < 0) return [false, "产品编码:" . $value[1] . "获取单价所得小于0,请检查门店所设置价格以及产品零售价"];
  1419. $price = $product_use[$product_tmp];
  1420. }
  1421. $key = $product_tmp . ',' . $storehouse_id;
  1422. if(! isset($product_submit[$key])) $product_submit[$key] = 0;
  1423. //总的产品id
  1424. $product_id[] = $product_tmp;
  1425. $sub[] = [
  1426. 'product_id' => $product_tmp,
  1427. 'storehouse_id' => $storehouse_id,
  1428. 'order_number' => "",
  1429. 'counted_num' => $value[2],
  1430. 'book_num' => 0,
  1431. 'final_num' => 0,
  1432. 'inventory_id' => 0,
  1433. 'price' => $price,
  1434. ];
  1435. }
  1436. //库存校验
  1437. $set = ProductInventorySet::where('top_depart_id',$top_depart_id)->where('del_time',0)->first();
  1438. if(empty($set)) return [false, '门店是否校验库存设置缺失'];
  1439. $set = $set->toArray();
  1440. $is_check_stock = $set['param_one'] <= 0 ? ProductInventorySet::type_one : $set['param_one'];
  1441. //获取库存
  1442. $inventory_map = [];
  1443. $inventory = ProductInventoryService::getRealStock($product_id, $storehouse_id);
  1444. foreach ($inventory as $value){
  1445. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  1446. $inventory_map[$key] = $value['real_number'];
  1447. }
  1448. $order_number = (new OrderNoService())->createOrderNumber(Inventory::prefix);
  1449. $product_submit_reduce = $product_reduce_id = [];
  1450. //盘点详情
  1451. foreach ($sub as $key => $value){
  1452. $sub[$key]['order_number'] = $order_number;
  1453. $keys = $value['product_id'] . ',' . $value['storehouse_id'];
  1454. if(isset($inventory_map[$keys])){
  1455. $sub[$key]['book_num'] = $inventory_map[$keys];
  1456. $num = bcsub($value['counted_num'], $inventory_map[$keys],2);
  1457. $sub[$key]['final_num'] = $num;
  1458. }else{
  1459. $sub[$key]['final_num'] = $value['counted_num'];
  1460. }
  1461. $final_amount = bcmul($sub[$key]['final_num'], $value['price'],2);
  1462. $sub[$key]['final_amount'] = $final_amount;
  1463. if($sub[$key]['final_num'] < 0) {
  1464. $product_reduce_id[] = $value['product_id'];
  1465. $product_submit_reduce[$keys] = $sub[$key]['final_num'];
  1466. }
  1467. }
  1468. //校验是否存在产品盘点
  1469. list($status, $msg) = (new InventoryService())->issetProduct(['top_depart_id' => $top_depart_id], $product_submit);
  1470. if(! $status) return [false, $msg];
  1471. if(! empty($product_submit_reduce)){
  1472. //校验库存
  1473. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_reduce_id, $product_submit_reduce);
  1474. if(! $status) return [false, $msg];
  1475. }
  1476. if(empty($sub)) return [false, '暂无需要写入的盘点数据'];
  1477. try{
  1478. DB::beginTransaction();
  1479. $inventory_model = new Inventory();
  1480. $inventory_model->order_number = $order_number;
  1481. $inventory_model->storehouse_id = $storehouse_id;
  1482. $inventory_model->counted_id = $user['id'];
  1483. $inventory_model->counted_time = $time;
  1484. $inventory_model->depart_id = $top_depart_id;
  1485. $inventory_model->top_depart_id = $top_depart_id;
  1486. $inventory_model->crt_id = $user['id'];
  1487. $inventory_model->save();
  1488. foreach ($sub as $key => $value){
  1489. $sub[$key]['inventory_id'] = $inventory_model->id;
  1490. }
  1491. //写入数据
  1492. InventorySub::insert($sub);
  1493. //单据创建时是否校验库存
  1494. (new CheckService())->orderInventoryInsert(['order_number' => $order_number, 'is_check_stock' => $is_check_stock]);
  1495. DB::commit();
  1496. }catch (\Exception $exception){
  1497. DB::rollBack();
  1498. if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
  1499. return [false, '网络波动,请重新操作!'];
  1500. }
  1501. return [false, $exception->getMessage() . $exception->getLine() . $exception->getCode()];
  1502. }
  1503. return [true, ''];
  1504. }
  1505. }