CommodityController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. $owners = $ownerService->getIntersectPermitting();
  97. return view('maintenance.commodity.edit',['commodity'=>$commodity,'owners'=>$owners]);
  98. }
  99. /**
  100. * Update the specified resource in storage.
  101. *
  102. * @param Request $request
  103. * @param Commodity $commodity
  104. * @return Response
  105. */
  106. public function update(Request $request, Commodity $commodity)
  107. {
  108. if(!Gate::allows('商品信息-编辑')){ return redirect(url('/')); }
  109. $this->validatorUpdate($request->all())->validate();
  110. $commodity->fill($request->all());
  111. $commodity->update();
  112. app('LogService')->log(__METHOD__,'修改商品信息'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  113. return redirect('maintenance/commodity/')->with('successTip',"成功修改商品信息:“{$commodity['name']}”!");
  114. }
  115. /**
  116. * Remove the specified resource from storage.
  117. *
  118. * @param Commodity $commodity
  119. * @return array|Response
  120. * @throws Exception
  121. */
  122. public function destroy(Commodity $commodity)
  123. {
  124. if(!Gate::allows('商品信息-删除')){ return redirect(url('/')); }
  125. app('LogService')->log(__METHOD__,__FUNCTION__,$commodity->toJson(),Auth::user()['id']);
  126. $re=$commodity->delete();
  127. return ['success'=>$re];
  128. }
  129. public function import()
  130. {
  131. return view('maintenance.commodity.import');
  132. }
  133. public function isExist(Request $request)
  134. {
  135. if (app('CommodityService')->isExist($request->input()))
  136. return ["success"=>true];
  137. else return ["success"=>false];
  138. }
  139. public function importExcel(Request $request)
  140. {
  141. $isOverride = $request->input('isOverride');
  142. try{
  143. ini_set('max_execution_time',2500);
  144. ini_set('memory_limit','1526M');
  145. $extension=$request->file()['file']->getClientOriginalExtension();
  146. $extension[0] = strtoupper($extension[0]);
  147. Excel::import(new CommodityImport($isOverride), $request->file()['file']->path(),null,$extension);
  148. return '<h1 class="text-success">导入成功</h1>';
  149. }catch (Exception $e){
  150. if(strstr($e->getMessage(),'No ReaderType')){return '<h1 class="text-danger">没有上传写权限,请修改php.ini 对应的upload_tmp_dir 目录或其权限</h1>'.$e->getMessage();}
  151. if(strstr($e->getMessage(),'SQLSTATE')){return '<h1 class="text-danger">数据库插入错误,数据不支持,可能有重复或异常字符</h1>'.$e->getMessage();}
  152. if(strstr(strtolower($e->getMessage()),'sku')){return '<h1 class="text-danger">请在第一行将 商品编码 字段名改成“SKU”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  153. if(strstr(strtolower($e->getMessage()),'name')){return '<h1 class="text-danger">请在第一行将 商品名称 字段名改成“name”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  154. if(strstr(strtolower($e->getMessage()),'barcode')){return '<h1 class="text-danger">请在第一行将 商品条码 字段名改成“barcode”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  155. if(strstr(strtolower($e->getMessage()),'owner')){return '<h1 class="text-danger">请在第一行将 货主 字段名改成“owner”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  156. return '<h1 class="text-danger">失败</h1>'.$e->getMessage();
  157. }
  158. }
  159. public function apiGetCommodityByBarcode(Request $request)
  160. {
  161. $barcode=$request->input('barcode');
  162. $name = '';
  163. if($barcode){
  164. $commodity=Commodity::query()->whereHas('barcodes', function (Builder $query)use($barcode){
  165. $query->where('code',$barcode);
  166. })->first();
  167. if($commodity&&$commodity['name']) $name=$commodity['name'];
  168. }
  169. return ['success'=>'true','name'=>$name];
  170. }
  171. public function syncWMS(Request $request){
  172. if(!Gate::allows('商品信息-编辑')){ return redirect(url('/')); }
  173. $owner_code = $request->owner_code ?? false;
  174. $owner_id = $request->owner_id ?? false;
  175. if (!$owner_code || !$owner_id)return ['success'=>false, 'data'=>"未指定货主"];
  176. $this->syncOwnerCommodities($owner_id,$owner_code);
  177. return ['success'=>true];
  178. }
  179. //通过货主与SKU同步 WMS商品至本地 有则比对修正,无则录入
  180. public function syncOwnerCommodities($owner_id,$owner_code,$sku = null, $barcode = null){
  181. ini_set('max_execution_time', '0');
  182. $params = ['customerid' => $owner_code];
  183. if ($sku) $params['sku'] = $sku;
  184. if ($barcode) $params['alternate_sku1'] = $barcode;
  185. /** @var OracleBasSkuService $oracleBasSkuService */
  186. $oracleBasSkuService = app('OracleBasSkuService');
  187. $amount = 1000;
  188. $sum = $oracleBasSkuService->count($params);
  189. $number = ceil($sum/$amount);
  190. for ($i = 0;$i<$number; $i++){
  191. $wmsCommodities = $oracleBasSkuService->getPiece($params,($i*$amount),$amount);
  192. if (!$wmsCommodities)continue;
  193. $this->execute($owner_id,$wmsCommodities);
  194. }
  195. }
  196. private function execute($owner_id,array $wmsCommodities){
  197. if (count($wmsCommodities) < 1)return;
  198. $today = date('Y-m-d H:i:s');
  199. $map = [];
  200. $skus = [];
  201. foreach ($wmsCommodities as $index => $wmsCommodity){
  202. $map[$wmsCommodity->sku] = $index;
  203. $skus[] = $wmsCommodity->sku;
  204. $trimSku = rtrim($wmsCommodity->sku,"*");
  205. if ($trimSku != $wmsCommodity->sku){
  206. $skus[] = $trimSku;
  207. $map[$trimSku] = $index;
  208. }
  209. }
  210. /** @var CommodityService $commodityService */
  211. $commodityService = app('CommodityService');
  212. /** @var CommodityBarcodeService $commodityBarcodeService */
  213. $commodityBarcodeService = app('CommodityBarcodeService');
  214. $commodities = $commodityService->getOwnerCommodities(['owner_id' => $owner_id, 'sku'=>$skus]);
  215. $updateCommodities = [];
  216. $updateCommodities[] = [
  217. 'id', 'sku', 'name', 'length', 'width', 'height', 'volumn',"pack_spec","remark"
  218. ];
  219. $barcodeMap = [];
  220. $commoditiesId = [];
  221. $barcodes = [];
  222. foreach ($commodities as $commodity){
  223. if (!isset($map[$commodity->sku]))continue;
  224. $wms = $wmsCommodities[$map[$commodity->sku]];
  225. $trimSku = rtrim($wms->sku,"*");
  226. if (($commodity->sku != $trimSku) || ($commodity->length != $wms->skulength)
  227. || ($commodity->width != $wms->skuwidth) || ($commodity->name != $wms->descr_c)
  228. || ($commodity->height != $wms->skuhigh) || ($commodity->volumn != $wms->cube)
  229. || ($commodity->pack_spec === null)){
  230. $updateCommodities[] = [
  231. 'id'=>$commodity->id,
  232. 'sku'=>$trimSku,
  233. 'name' => $wms->descr_c,
  234. 'length' => $wms->skulength,
  235. 'width' => $wms->skuwidth,
  236. 'height' => $wms->skuhigh,
  237. 'volumn' => $wms->cube,
  238. "pack_spec" => $wms->packid == 'STANDARD' ? 0 : explode("/",$wms->packid)[1],
  239. 'remark' => $wms->notes,
  240. ];
  241. }
  242. if ($wms->alternate_sku1){
  243. $wmsCode = rtrim($wms->alternate_sku1,"*");
  244. $barcodeMap[$wmsCode] = $commodity->id;
  245. $barcodes[] = $wmsCode;
  246. }
  247. if ($wms->alternate_sku2){
  248. $wmsCode = rtrim($wms->alternate_sku2,"*");
  249. $barcodeMap[$wmsCode] = $commodity->id;
  250. $barcodes[] = $wmsCode;
  251. }
  252. $commoditiesId[] = $commodity->id;
  253. unset($wmsCommodities[$map[$commodity->sku]]);
  254. if (isset($map[$wms->sku]))unset($map[$commodity->sku]);
  255. if (isset($map[$trimSku]))unset($map[$trimSku]);
  256. }
  257. unset($commodities,$skus);
  258. if (count($updateCommodities) > 1){
  259. $commodityService->batchUpdate($updateCommodities);
  260. app('LogService')->log(__METHOD__,"同步商品-批量更新",json_encode($updateCommodities));
  261. $commodityBarcodes = $commodityBarcodeService->get(['commodity_id'=>$commoditiesId, 'code'=>$barcodes]);
  262. unset($commoditiesId,$barcodes);
  263. foreach ($commodityBarcodes as $barcode){
  264. if (($barcodeMap[$barcode->code] ?? false) && $barcodeMap[$barcode->code] == $barcode->commodity_id){
  265. unset($barcodeMap[$barcode->code]);
  266. }
  267. }
  268. if (count($barcodeMap) > 0){
  269. $barcodeInsert = [];
  270. foreach ($barcodeMap as $key => $value){
  271. $barcodeInsert[] = [
  272. 'commodity_id'=>$value,
  273. 'code' => $key,
  274. 'created_at' => $today
  275. ];
  276. }
  277. $commodityBarcodeService->insert($barcodeInsert);
  278. app('LogService')->log(__METHOD__,"同步商品-录入条码",json_encode($barcodeInsert));
  279. }
  280. }
  281. $createCommodities = [];
  282. $barcodeMap = [];
  283. $skus = [];
  284. $barcodes = [];
  285. $barcodeIndex = [];
  286. foreach ($map as $sku => $index){
  287. if (substr($sku,-1) == "*")continue;
  288. $wms = $wmsCommodities[$index];
  289. $createCommodities[] = [
  290. 'owner_id' => $owner_id,
  291. 'sku' => $wms->sku,
  292. 'name' => $wms->descr_c,
  293. 'length' => $wms->skulength,
  294. 'width' => $wms->skuwidth,
  295. 'height' => $wms->skuhigh,
  296. 'volumn' => $wms->cube,
  297. "pack_spec" => $wms->packid == 'STANDARD' ? 0 : explode("/",$wms->packid)[1],
  298. 'remark' => $wms->notes,
  299. "created_at" => $today,
  300. ];
  301. $barcodeMap[$wms->sku] = [];
  302. if ($wms->alternate_sku1){
  303. $code = rtrim($wms->alternate_sku1,"*");
  304. $barcodeMap[$wms->sku][] = $code;
  305. $barcodes[] = $code;
  306. $barcodeIndex[$code] = count($createCommodities) - 1;
  307. }
  308. if ($wms->alternate_sku2){
  309. $code = rtrim($wms->alternate_sku2,"*");
  310. $barcodeMap[$wms->sku][] = $code;
  311. $barcodes[] = $code;
  312. $barcodeIndex[$code] = count($createCommodities) - 1;
  313. }
  314. $skus[] = $wms->sku;
  315. unset($wmsCommodities[$index]);
  316. }
  317. if (count($barcodes) > 0){
  318. // ownerBarcodeSeekCommodityGet可指定第三个参数为true 默认无差别覆盖 如若指定该布尔值true代表仅覆盖SKU空值项
  319. $commodities = $commodityService->ownerBarcodeSeekCommodityGet(['id'=>$owner_id], $barcodes);
  320. $updateCommodities = [];
  321. $updateCommodities[] = [
  322. 'id', 'sku', 'name', 'length', 'width', 'height', 'volumn','pack_spec','remark'
  323. ];
  324. foreach ($commodities as $commodity){
  325. foreach ($commodity->barcodes as $code){
  326. if (isset($barcodeIndex[$code->code])){
  327. $goods = $createCommodities[$barcodeIndex[$code->code]] ?? false;
  328. if (!$goods)continue;
  329. $updateCommodities[] = [
  330. "id" => $commodity->id,
  331. 'sku'=>$goods['sku'],
  332. 'name' => $goods['name'],
  333. 'length' => $goods['length'],
  334. 'width' => $goods['width'],
  335. 'height' => $goods['height'],
  336. 'volumn' => $goods['volumn'],
  337. "pack_spec" => $goods["pack_spec"],
  338. 'remark' => $goods["notes"],
  339. ];
  340. unset($createCommodities[$barcodeIndex[$code->code]]);
  341. break;
  342. }
  343. }
  344. }
  345. if (count($updateCommodities) > 0){
  346. $commodityService->batchUpdate($updateCommodities);
  347. app('LogService')->log(__METHOD__,"同步商品-批量更新",json_encode($updateCommodities));
  348. }
  349. }
  350. if (count($createCommodities) > 0){
  351. $commodityService->insert($createCommodities);
  352. app('LogService')->log(__METHOD__,"同步商品-录入商品",json_encode($createCommodities));
  353. $commodities = $commodityService->get(['owner_id'=>$owner_id , 'sku'=>$skus]);
  354. $barcodeInsert = [];
  355. foreach ($commodities as $commodity){
  356. foreach ($barcodeMap[$commodity->sku] as $code){
  357. $barcodeInsert[] = [
  358. 'commodity_id'=>$commodity->id,
  359. 'code' => $code,
  360. 'created_at' => $today
  361. ];
  362. }
  363. }
  364. $commodityBarcodeService->insert($barcodeInsert);
  365. app('LogService')->log(__METHOD__,"同步商品-录入条码",json_encode($barcodeInsert));
  366. }
  367. }
  368. }