ImportService.php 67 KB

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