CommodityController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Commodity;
  4. use App\Imports\CommodityImport;
  5. use App\Services\CommodityBarcodeService;
  6. use App\Services\CommodityService;
  7. use App\Services\OracleBasSkuService;
  8. use App\Services\OwnerService;
  9. use Exception;
  10. use Illuminate\Database\Eloquent\Builder;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Http\Response;
  13. use Illuminate\Support\Facades\Auth;
  14. use Illuminate\Support\Facades\Gate;
  15. use Illuminate\Support\Facades\Validator;
  16. use Maatwebsite\Excel\Facades\Excel;
  17. class CommodityController extends Controller
  18. {
  19. /**
  20. * Display a listing of the resource.
  21. *
  22. * @return Response
  23. */
  24. public function index()
  25. {
  26. if(!Gate::allows('商品信息-查询')){ return redirect(url('denied')); }
  27. $commodities=Commodity::query()->orderBy('id','desc')->paginate(50);
  28. return view('maintenance.commodity.index',['commodities'=>$commodities]);
  29. }
  30. /**
  31. * Show the form for creating a new resource.
  32. *
  33. * @return Response
  34. */
  35. public function create()
  36. {
  37. if(!Gate::allows('商品信息-录入')){ return redirect(url('denied')); }
  38. $ownerService=app(OwnerService::class);
  39. $owners = $ownerService->getIntersectPermitting();
  40. return view('maintenance.commodity.create',compact('owners'));
  41. }
  42. /**
  43. * Store a newly created resource in storage.
  44. *
  45. * @param Request $request
  46. * @return Response
  47. */
  48. public function store(Request $request)
  49. {
  50. if(!Gate::allows('商品信息-录入')){ return redirect(url('denied')); }
  51. $this->validatorCreate($request->all())->validate();
  52. $commodity=new Commodity($request->all());
  53. $commodity->save();
  54. $commodity->newBarcode($request->input('barcode'));
  55. app('LogService')->log(__METHOD__,'录入商品'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  56. return redirect('maintenance/commodity/create')->with('successTip',"成功录入商品信息:“{$request->input('name')}”");
  57. }
  58. protected function validatorCreate(array $data)
  59. {
  60. return Validator::make($data, [
  61. 'sku'=>['required', 'string', 'max:50'],
  62. 'name' => ['required', 'string', 'max:50'],
  63. 'barcode' => ['required', 'string', 'max:50'],
  64. 'owner_id' => ['required', 'string', 'max:50'],
  65. ]);
  66. }
  67. protected function validatorUpdate(array $data)
  68. {
  69. return Validator::make($data, [
  70. 'sku'=>['required', 'string', 'max:50'],
  71. 'name' => ['required', 'string', 'max:50'],
  72. 'barcode' => ['required', 'string', 'max:50'],
  73. 'owner_id' => ['required', 'string', 'max:50'],
  74. ]);
  75. }
  76. /**
  77. * Display the specified resource.
  78. *
  79. * @param Commodity $commodity
  80. * @return Response
  81. */
  82. public function show(Commodity $commodity)
  83. {
  84. //
  85. }
  86. /**
  87. * Show the form for editing the specified resource.
  88. *
  89. * @param Commodity $commodity
  90. * @return Response
  91. */
  92. public function edit(Commodity $commodity)
  93. {
  94. if(!Gate::allows('商品信息-编辑')){ return redirect(url('denied')); }
  95. $ownerService=app(OwnerService::class);
  96. return view('maintenance.commodity.edit',['commodity'=>$commodity,'owners'=>$owners]);
  97. }
  98. /**
  99. * Update the specified resource in storage.
  100. *
  101. * @param Request $request
  102. * @param Commodity $commodity
  103. * @return Response
  104. */
  105. public function update(Request $request, Commodity $commodity)
  106. {
  107. if(!Gate::allows('商品信息-编辑')){ return redirect(url('/')); }
  108. $this->validatorUpdate($request->all())->validate();
  109. $commodity->fill($request->all());
  110. $commodity->update();
  111. app('LogService')->log(__METHOD__,'修改商品信息'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  112. return redirect('maintenance/commodity/')->with('successTip',"成功修改商品信息:“{$commodity['name']}”!");
  113. }
  114. /**
  115. * Remove the specified resource from storage.
  116. *
  117. * @param Commodity $commodity
  118. * @return array|Response
  119. * @throws Exception
  120. */
  121. public function destroy(Commodity $commodity)
  122. {
  123. if(!Gate::allows('商品信息-删除')){ return redirect(url('/')); }
  124. app('LogService')->log(__METHOD__,__FUNCTION__,$commodity->toJson(),Auth::user()['id']);
  125. $re=$commodity->delete();
  126. return ['success'=>$re];
  127. }
  128. public function import()
  129. {
  130. return view('maintenance.commodity.import');
  131. }
  132. public function isExist(Request $request)
  133. {
  134. if (app('CommodityService')->isExist($request->input()))
  135. return ["success"=>true];
  136. else return ["success"=>false];
  137. }
  138. public function importExcel(Request $request)
  139. {
  140. $isOverride = $request->input('isOverride');
  141. try{
  142. ini_set('max_execution_time',2500);
  143. ini_set('memory_limit','1526M');
  144. $extension=$request->file()['file']->getClientOriginalExtension();
  145. $extension[0] = strtoupper($extension[0]);
  146. Excel::import(new CommodityImport($isOverride), $request->file()['file']->path(),null,$extension);
  147. return '<h1 class="text-success">导入成功</h1>';
  148. }catch (Exception $e){
  149. if(strstr($e->getMessage(),'No ReaderType')){return '<h1 class="text-danger">没有上传写权限,请修改php.ini 对应的upload_tmp_dir 目录或其权限</h1>'.$e->getMessage();}
  150. if(strstr($e->getMessage(),'SQLSTATE')){return '<h1 class="text-danger">数据库插入错误,数据不支持,可能有重复或异常字符</h1>'.$e->getMessage();}
  151. if(strstr(strtolower($e->getMessage()),'sku')){return '<h1 class="text-danger">请在第一行将 商品编码 字段名改成“SKU”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  152. if(strstr(strtolower($e->getMessage()),'name')){return '<h1 class="text-danger">请在第一行将 商品名称 字段名改成“name”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  153. if(strstr(strtolower($e->getMessage()),'barcode')){return '<h1 class="text-danger">请在第一行将 商品条码 字段名改成“barcode”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  154. if(strstr(strtolower($e->getMessage()),'owner')){return '<h1 class="text-danger">请在第一行将 货主 字段名改成“owner”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  155. return '<h1 class="text-danger">失败</h1>'.$e->getMessage();
  156. }
  157. }
  158. public function apiGetCommodityByBarcode(Request $request)
  159. {
  160. $barcode=$request->input('barcode');
  161. $owner_id=$request->input('owner_id');
  162. // $name = '';
  163. $commodity=[];
  164. if($barcode){
  165. $commodity=Commodity::query()->where('owner_id',$owner_id)->whereHas('barcodes', function (Builder $query)use($barcode){
  166. $query->where('code',$barcode);
  167. })->first();
  168. if (!$commodity)$commodity=Commodity::query()->whereNotNull('owner_id')->whereHas('barcodes', function (Builder $query)use($barcode){
  169. $query->where('code',$barcode);
  170. })->first();
  171. // if($commodity&&$commodity['name']) $name=$commodity['name'];
  172. }
  173. return ['success'=>'true','commodity'=>$commodity];
  174. }
  175. public function syncWMS(Request $request){
  176. if(!Gate::allows('商品信息-编辑')){ return redirect(url('/')); }
  177. $owner_code = $request->owner_code ?? false;
  178. $owner_id = $request->owner_id ?? false;
  179. if (!$owner_code || !$owner_id)return ['success'=>false, 'data'=>"未指定货主"];
  180. $this->syncOwnerCommodities($owner_id,$owner_code);
  181. return ['success'=>true];
  182. }
  183. //通过货主与SKU同步 WMS商品至本地 有则比对修正,无则录入
  184. public function syncOwnerCommodities($owner_id,$owner_code,$sku = null, $barcode = null){
  185. ini_set('max_execution_time', '0');
  186. $params = ['customerid' => $owner_code];
  187. if ($sku) $params['sku'] = $sku;
  188. if ($barcode) $params['alternate_sku1'] = $barcode;
  189. /** @var OracleBasSkuService $oracleBasSkuService */
  190. $oracleBasSkuService = app('OracleBasSkuService');
  191. $amount = 1000;
  192. $sum = $oracleBasSkuService->count($params);
  193. $number = ceil($sum/$amount);
  194. for ($i = 0;$i<$number; $i++){
  195. $wmsCommodities = $oracleBasSkuService->getPiece($params,($i*$amount),$amount);
  196. if (!$wmsCommodities)continue;
  197. $this->execute($owner_id,$wmsCommodities);
  198. }
  199. }
  200. private function execute($owner_id,array $wmsCommodities){
  201. if (count($wmsCommodities) < 1)return;
  202. $today = date('Y-m-d H:i:s');
  203. $map = [];
  204. $skus = [];
  205. foreach ($wmsCommodities as $index => $wmsCommodity){
  206. $map[$wmsCommodity->sku] = $index;
  207. $skus[] = $wmsCommodity->sku;
  208. $trimSku = rtrim($wmsCommodity->sku,"*");
  209. if ($trimSku != $wmsCommodity->sku){
  210. $skus[] = $trimSku;
  211. $map[$trimSku] = $index;
  212. }
  213. }
  214. /** @var CommodityService $commodityService */
  215. $commodityService = app('CommodityService');
  216. /** @var CommodityBarcodeService $commodityBarcodeService */
  217. $commodityBarcodeService = app('CommodityBarcodeService');
  218. $commodities = $commodityService->getOwnerCommodities(['owner_id' => $owner_id, 'sku'=>$skus]);
  219. $updateCommodities = [];
  220. $updateCommodities[] = [
  221. 'id', 'sku', 'name', 'length', 'width', 'height', 'volumn',"pack_spec","remark"
  222. ];
  223. $barcodeMap = [];
  224. $commoditiesId = [];
  225. $barcodes = [];
  226. foreach ($commodities as $commodity){
  227. if (!isset($map[$commodity->sku]))continue;
  228. $wms = $wmsCommodities[$map[$commodity->sku]];
  229. $trimSku = rtrim($wms->sku,"*");
  230. if (($commodity->sku != $trimSku) || ($commodity->length != $wms->skulength)
  231. || ($commodity->width != $wms->skuwidth) || ($commodity->name != $wms->descr_c)
  232. || ($commodity->height != $wms->skuhigh) || ($commodity->volumn != $wms->cube)
  233. || ($commodity->pack_spec === null)){
  234. $updateCommodities[] = [
  235. 'id'=>$commodity->id,
  236. 'sku'=>$trimSku,
  237. 'name' => $wms->descr_c,
  238. 'length' => $wms->skulength,
  239. 'width' => $wms->skuwidth,
  240. 'height' => $wms->skuhigh,
  241. 'volumn' => $wms->cube,
  242. "pack_spec" => $wms->packid == 'STANDARD' ? 0 : explode("/",$wms->packid)[1],
  243. 'remark' => $wms->notes,
  244. ];
  245. }
  246. if ($wms->alternate_sku1){
  247. $wmsCode = rtrim($wms->alternate_sku1,"*");
  248. $barcodeMap[$wmsCode] = $commodity->id;
  249. $barcodes[] = $wmsCode;
  250. }
  251. if ($wms->alternate_sku2){
  252. $wmsCode = rtrim($wms->alternate_sku2,"*");
  253. $barcodeMap[$wmsCode] = $commodity->id;
  254. $barcodes[] = $wmsCode;
  255. }
  256. $commoditiesId[] = $commodity->id;
  257. unset($wmsCommodities[$map[$commodity->sku]]);
  258. if (isset($map[$wms->sku]))unset($map[$commodity->sku]);
  259. if (isset($map[$trimSku]))unset($map[$trimSku]);
  260. }
  261. unset($commodities,$skus);
  262. if (count($updateCommodities) > 1){
  263. $commodityService->batchUpdate($updateCommodities);
  264. app('LogService')->log(__METHOD__,"同步商品-批量更新",json_encode($updateCommodities));
  265. $commodityBarcodes = $commodityBarcodeService->get(['commodity_id'=>$commoditiesId, 'code'=>$barcodes]);
  266. unset($commoditiesId,$barcodes);
  267. foreach ($commodityBarcodes as $barcode){
  268. if (($barcodeMap[$barcode->code] ?? false) && $barcodeMap[$barcode->code] == $barcode->commodity_id){
  269. unset($barcodeMap[$barcode->code]);
  270. }
  271. }
  272. if (count($barcodeMap) > 0){
  273. $barcodeInsert = [];
  274. foreach ($barcodeMap as $key => $value){
  275. $barcodeInsert[] = [
  276. 'commodity_id'=>$value,
  277. 'code' => $key,
  278. 'created_at' => $today
  279. ];
  280. }
  281. $commodityBarcodeService->insert($barcodeInsert);
  282. app('LogService')->log(__METHOD__,"同步商品-录入条码",json_encode($barcodeInsert));
  283. }
  284. }
  285. $createCommodities = [];
  286. $barcodeMap = [];
  287. $skus = [];
  288. $barcodes = [];
  289. $barcodeIndex = [];
  290. foreach ($map as $sku => $index){
  291. if (substr($sku,-1) == "*")continue;
  292. $wms = $wmsCommodities[$index];
  293. $createCommodities[] = [
  294. 'owner_id' => $owner_id,
  295. 'sku' => $wms->sku,
  296. 'name' => $wms->descr_c,
  297. 'length' => $wms->skulength,
  298. 'width' => $wms->skuwidth,
  299. 'height' => $wms->skuhigh,
  300. 'volumn' => $wms->cube,
  301. "pack_spec" => $wms->packid == 'STANDARD' ? 0 : explode("/",$wms->packid)[1],
  302. 'remark' => $wms->notes,
  303. "created_at" => $today,
  304. ];
  305. $barcodeMap[$wms->sku] = [];
  306. if ($wms->alternate_sku1){
  307. $code = rtrim($wms->alternate_sku1,"*");
  308. $barcodeMap[$wms->sku][] = $code;
  309. $barcodes[] = $code;
  310. $barcodeIndex[$code] = count($createCommodities) - 1;
  311. }
  312. if ($wms->alternate_sku2){
  313. $code = rtrim($wms->alternate_sku2,"*");
  314. $barcodeMap[$wms->sku][] = $code;
  315. $barcodes[] = $code;
  316. $barcodeIndex[$code] = count($createCommodities) - 1;
  317. }
  318. $skus[] = $wms->sku;
  319. unset($wmsCommodities[$index]);
  320. }
  321. if (count($barcodes) > 0){
  322. // ownerBarcodeSeekCommodityGet可指定第三个参数为true 默认无差别覆盖 如若指定该布尔值true代表仅覆盖SKU空值项
  323. $commodities = $commodityService->ownerBarcodeSeekCommodityGet(['id'=>$owner_id], $barcodes);
  324. $updateCommodities = [];
  325. $updateCommodities[] = [
  326. 'id', 'sku', 'name', 'length', 'width', 'height', 'volumn','pack_spec','remark'
  327. ];
  328. foreach ($commodities as $commodity){
  329. foreach ($commodity->barcodes as $code){
  330. if (isset($barcodeIndex[$code->code])){
  331. $goods = $createCommodities[$barcodeIndex[$code->code]] ?? false;
  332. if (!$goods)continue;
  333. $updateCommodities[] = [
  334. "id" => $commodity->id,
  335. 'sku'=>$goods['sku'],
  336. 'name' => $goods['name'],
  337. 'length' => $goods['length'],
  338. 'width' => $goods['width'],
  339. 'height' => $goods['height'],
  340. 'volumn' => $goods['volumn'],
  341. "pack_spec" => $goods["pack_spec"],
  342. 'remark' => $goods["notes"],
  343. ];
  344. unset($createCommodities[$barcodeIndex[$code->code]]);
  345. break;
  346. }
  347. }
  348. }
  349. if (count($updateCommodities) > 0){
  350. $commodityService->batchUpdate($updateCommodities);
  351. app('LogService')->log(__METHOD__,"同步商品-批量更新",json_encode($updateCommodities));
  352. }
  353. }
  354. if (count($createCommodities) > 0){
  355. $commodityService->insert($createCommodities);
  356. app('LogService')->log(__METHOD__,"同步商品-录入商品",json_encode($createCommodities));
  357. $commodities = $commodityService->get(['owner_id'=>$owner_id , 'sku'=>$skus]);
  358. $barcodeInsert = [];
  359. foreach ($commodities as $commodity){
  360. foreach ($barcodeMap[$commodity->sku] as $code){
  361. $barcodeInsert[] = [
  362. 'commodity_id'=>$commodity->id,
  363. 'code' => $code,
  364. 'created_at' => $today
  365. ];
  366. }
  367. }
  368. $commodityBarcodeService->insert($barcodeInsert);
  369. app('LogService')->log(__METHOD__,"同步商品-录入条码",json_encode($barcodeInsert));
  370. }
  371. }
  372. }