CommodityController.php 15 KB

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