CommodityController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. app('LogService')->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. app('LogService')->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. app('LogService')->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',"pack_spec"
  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. || ($commodity->pack_spec === null)){
  216. $updateCommodities[] = [
  217. 'id'=>$commodity->id,
  218. 'sku'=>$trimSku,
  219. 'name' => $wms->descr_c,
  220. 'length' => $wms->skulength,
  221. 'width' => $wms->skuwidth,
  222. 'height' => $wms->skuhigh,
  223. 'volumn' => $wms->cube,
  224. "pack_spec" => $wms->packid == 'STANDARD' ? 0 : explode("/",$wms->packid)[1],
  225. ];
  226. }
  227. if ($wms->alternate_sku1){
  228. $wmsCode = rtrim($wms->alternate_sku1,"*");
  229. $barcodeMap[$wmsCode] = $commodity->id;
  230. $barcodes[] = $wmsCode;
  231. }
  232. if ($wms->alternate_sku2){
  233. $wmsCode = rtrim($wms->alternate_sku2,"*");
  234. $barcodeMap[$wmsCode] = $commodity->id;
  235. $barcodes[] = $wmsCode;
  236. }
  237. $commoditiesId[] = $commodity->id;
  238. unset($wmsCommodities[$map[$commodity->sku]]);
  239. if (isset($map[$wms->sku]))unset($map[$commodity->sku]);
  240. if (isset($map[$trimSku]))unset($map[$trimSku]);
  241. }
  242. unset($commodities,$skus);
  243. if (count($updateCommodities) > 1){
  244. $commodityService->batchUpdate($updateCommodities);
  245. app('LogService')->log(__METHOD__,"同步商品-批量更新",json_encode($updateCommodities));
  246. $commodityBarcodes = $commodityBarcodeService->get(['commodity_id'=>$commoditiesId, 'code'=>$barcodes]);
  247. unset($commoditiesId,$barcodes);
  248. foreach ($commodityBarcodes as $barcode){
  249. if (($barcodeMap[$barcode->code] ?? false) && $barcodeMap[$barcode->code] == $barcode->commodity_id){
  250. unset($barcodeMap[$barcode->code]);
  251. }
  252. }
  253. if (count($barcodeMap) > 0){
  254. $barcodeInsert = [];
  255. foreach ($barcodeMap as $key => $value){
  256. $barcodeInsert[] = [
  257. 'commodity_id'=>$value,
  258. 'code' => $key,
  259. 'created_at' => $today
  260. ];
  261. }
  262. $commodityBarcodeService->insert($barcodeInsert);
  263. app('LogService')->log(__METHOD__,"同步商品-录入条码",json_encode($barcodeInsert));
  264. }
  265. }
  266. $createCommodities = [];
  267. $barcodeMap = [];
  268. $skus = [];
  269. $barcodes = [];
  270. $barcodeIndex = [];
  271. foreach ($map as $sku => $index){
  272. if (substr($sku,-1) == "*")continue;
  273. $wms = $wmsCommodities[$index];
  274. $createCommodities[] = [
  275. 'owner_id' => $owner_id,
  276. 'sku' => $wms->sku,
  277. 'name' => $wms->descr_c,
  278. 'length' => $wms->skulength,
  279. 'width' => $wms->skuwidth,
  280. 'height' => $wms->skuhigh,
  281. 'volumn' => $wms->cube,
  282. "pack_spec" => $wms->packid == 'STANDARD' ? 0 : explode("/",$wms->packid)[1],
  283. "created_at" => $today,
  284. ];
  285. $barcodeMap[$wms->sku] = [];
  286. if ($wms->alternate_sku1){
  287. $code = rtrim($wms->alternate_sku1,"*");
  288. $barcodeMap[$wms->sku][] = $code;
  289. $barcodes[] = $code;
  290. $barcodeIndex[$code] = count($createCommodities) - 1;
  291. }
  292. if ($wms->alternate_sku2){
  293. $code = rtrim($wms->alternate_sku2,"*");
  294. $barcodeMap[$wms->sku][] = $code;
  295. $barcodes[] = $code;
  296. $barcodeIndex[$code] = count($createCommodities) - 1;
  297. }
  298. $skus[] = $wms->sku;
  299. unset($wmsCommodities[$index]);
  300. }
  301. if (count($barcodes) > 0){
  302. // TODO ownerBarcodeSeekCommodityGet可指定第三个参数为true 默认无差别覆盖 如若指定该布尔值true代表仅覆盖SKU空值项
  303. $commodities = $commodityService->ownerBarcodeSeekCommodityGet(['id'=>$owner_id], $barcodes);
  304. $updateCommodities = [];
  305. $updateCommodities[] = [
  306. 'id', 'sku', 'name', 'length', 'width', 'height', 'volumn',
  307. ];
  308. foreach ($commodities as $commodity){
  309. foreach ($commodity->barcodes as $code){
  310. if (isset($barcodeIndex[$code->code])){
  311. $goods = $createCommodities[$barcodeIndex[$code->code]] ?? false;
  312. if (!$goods)continue;
  313. $updateCommodities[] = [
  314. "id" => $commodity->id,
  315. 'sku'=>$goods['sku'],
  316. 'name' => $goods['name'],
  317. 'length' => $goods['length'],
  318. 'width' => $goods['width'],
  319. 'height' => $goods['height'],
  320. 'volumn' => $goods['volumn'],
  321. "pack_spec" => $goods["pack_spec"],
  322. ];
  323. unset($createCommodities[$barcodeIndex[$code->code]]);
  324. break;
  325. }
  326. }
  327. }
  328. if (count($updateCommodities) > 0){
  329. $commodityService->batchUpdate($updateCommodities);
  330. app('LogService')->log(__METHOD__,"同步商品-批量更新",json_encode($updateCommodities));
  331. }
  332. }
  333. if (count($createCommodities) > 0){
  334. $commodityService->insert($createCommodities);
  335. app('LogService')->log(__METHOD__,"同步商品-录入商品",json_encode($createCommodities));
  336. $commodities = $commodityService->get(['owner_id'=>$owner_id , 'sku'=>$skus]);
  337. $barcodeInsert = [];
  338. foreach ($commodities as $commodity){
  339. foreach ($barcodeMap[$commodity->sku] as $code){
  340. $barcodeInsert[] = [
  341. 'commodity_id'=>$commodity->id,
  342. 'code' => $code,
  343. 'created_at' => $today
  344. ];
  345. }
  346. }
  347. $commodityBarcodeService->insert($barcodeInsert);
  348. app('LogService')->log(__METHOD__,"同步商品-录入条码",json_encode($barcodeInsert));
  349. }
  350. }
  351. }