ImportService.php 73 KB

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