CommodityController.php 16 KB

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