CommodityController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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('/')); }
  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('/')); }
  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('/')); }
  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('/')); }
  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. public function syncOwnerCommodities($owner_id,$owner_code,$sku = null){
  166. ini_set('max_execution_time', '0');
  167. $params = ['customerid' => $owner_code];
  168. if ($sku) $params['sku'] = $sku;
  169. /** @var OracleBasSkuService $oracleBasSkuService */
  170. $oracleBasSkuService = app('oracleBasSkuService');
  171. $amount = 1000;
  172. $sum = $oracleBasSkuService->count($params);
  173. $number = ceil($sum/$amount);
  174. for ($i = 0;$i<$number; $i++){
  175. $wmsCommodities = $oracleBasSkuService->getPiece($params,($i*$amount),$amount);
  176. if (!$wmsCommodities)continue;
  177. $this->execute($owner_id,$wmsCommodities);
  178. }
  179. }
  180. private function execute($owner_id,array $wmsCommodities){
  181. if (count($wmsCommodities) < 1)return;
  182. $today = date('Y-m-d H:i:s');
  183. $map = [];
  184. $skus = [];
  185. foreach ($wmsCommodities as $index => $wmsCommodity){
  186. $map[$wmsCommodity->sku] = $index;
  187. $skus[] = $wmsCommodity->sku;
  188. $trimSku = rtrim($wmsCommodity->sku,"*");
  189. if ($trimSku != $wmsCommodity->sku){
  190. $skus[] = $trimSku;
  191. $map[$trimSku] = $index;
  192. }
  193. }
  194. /** @var CommodityService $commodityService */
  195. $commodityService = app('commodityService');
  196. /** @var CommodityBarcodeService $commodityBarcodeService */
  197. $commodityBarcodeService = app('commodityBarcodeService');
  198. $commodities = $commodityService->getOwnerCommodities(['owner_id' => $owner_id, 'sku'=>$skus]);
  199. $updateCommodities = [];
  200. $updateCommodities[] = [
  201. 'id', 'sku', 'name', 'length', 'width', 'height', 'volumn',
  202. ];
  203. $barcodeMap = [];
  204. $commoditiesId = [];
  205. $barcodes = [];
  206. foreach ($commodities as $commodity){
  207. if (!isset($map[$commodity->sku]))continue;
  208. $wms = $wmsCommodities[$map[$commodity->sku]];
  209. $trimSku = rtrim($wms->sku,"*");
  210. if (($commodity->sku != $trimSku) || ($commodity->length != $wms->skulength)
  211. || ($commodity->width != $wms->skuwidth) || ($commodity->name != $wms->descr_c)
  212. || ($commodity->height != $wms->skuhigh) || ($commodity->volumn != $wms->cube)){
  213. $updateCommodities[] = [
  214. 'id'=>$commodity->id,
  215. 'sku'=>$trimSku,
  216. 'name' => $wms->descr_c,
  217. 'length' => $wms->skulength,
  218. 'width' => $wms->skuwidth,
  219. 'height' => $wms->skuhigh,
  220. 'volumn' => $wms->cube
  221. ];
  222. }
  223. if ($wms->alternate_sku1){
  224. $wmsCode = rtrim($wms->alternate_sku1,"*");
  225. $barcodeMap[$wmsCode] = $commodity->id;
  226. $barcodes[] = $wmsCode;
  227. }
  228. if ($wms->alternate_sku2){
  229. $wmsCode = rtrim($wms->alternate_sku2,"*");
  230. $barcodeMap[$wmsCode] = $commodity->id;
  231. $barcodes[] = $wmsCode;
  232. }
  233. $commoditiesId[] = $commodity->id;
  234. unset($wmsCommodities[$map[$commodity->sku]]);
  235. if (isset($map[$wms->sku]))unset($map[$commodity->sku]);
  236. if (isset($map[$trimSku]))unset($map[$trimSku]);
  237. }
  238. unset($commodities,$skus);
  239. if (count($updateCommodities) > 1){
  240. $commodityService->batchUpdate($updateCommodities);
  241. LogService::log(__METHOD__,"同步商品-批量更新",json_encode($updateCommodities));
  242. $commodityBarcodes = $commodityBarcodeService->get(['commodity_id'=>$commoditiesId, 'code'=>$barcodes]);
  243. unset($commoditiesId,$barcodes);
  244. foreach ($commodityBarcodes as $barcode){
  245. if (($barcodeMap[$barcode->code] ?? false) && $barcodeMap[$barcode->code] == $barcode->commodity_id){
  246. unset($barcodeMap[$barcode->code]);
  247. }
  248. }
  249. if (count($barcodeMap) > 0){
  250. $barcodeInsert = [];
  251. foreach ($barcodeMap as $key => $value){
  252. $barcodeInsert[] = [
  253. 'commodity_id'=>$value,
  254. 'code' => $key,
  255. 'created_at' => $today
  256. ];
  257. }
  258. $commodityBarcodeService->insert($barcodeInsert);
  259. LogService::log(__METHOD__,"同步商品-录入条码",json_encode($barcodeInsert));
  260. }
  261. }
  262. $createCommodities = [];
  263. $barcodeMap = [];
  264. $skus = [];
  265. $barcodes = [];
  266. $barcodeIndex = [];
  267. foreach ($map as $sku => $index){
  268. if (substr($sku,-1) == "*")continue;
  269. $wms = $wmsCommodities[$index];
  270. $createCommodities[] = [
  271. 'owner_id' => $owner_id,
  272. 'sku' => $wms->sku,
  273. 'name' => $wms->descr_c,
  274. 'length' => $wms->skulength,
  275. 'width' => $wms->skuwidth,
  276. 'height' => $wms->skuhigh,
  277. 'volumn' => $wms->cube,
  278. "created_at" => $today,
  279. ];
  280. $barcodeMap[$wms->sku] = [];
  281. if ($wms->alternate_sku1){
  282. $code = rtrim($wms->alternate_sku1,"*");
  283. $barcodeMap[$wms->sku][] = $code;
  284. $barcodes[] = $code;
  285. $barcodeIndex[$code] = count($createCommodities) - 1;
  286. }
  287. if ($wms->alternate_sku2){
  288. $code = rtrim($wms->alternate_sku2,"*");
  289. $barcodeMap[$wms->sku][] = $code;
  290. $barcodes[] = $code;
  291. $barcodeIndex[$code] = count($createCommodities) - 1;
  292. }
  293. $skus[] = $wms->sku;
  294. unset($wmsCommodities[$index]);
  295. }
  296. if (count($barcodes) > 0){
  297. $commodities = $commodityService->ownerBarcodeSeekCommodityGet(['id'=>$owner_id], $barcodes, true);
  298. $updateCommodities = [];
  299. $updateCommodities[] = [
  300. 'id', 'sku', 'name', 'length', 'width', 'height', 'volumn',
  301. ];
  302. foreach ($commodities as $commodity){
  303. foreach ($commodity->barcodes as $code){
  304. if (isset($barcodeIndex[$code->code])){
  305. $goods = $createCommodities[$barcodeIndex[$code->code]] ?? false;
  306. if (!$goods)continue;
  307. $updateCommodities[] = [
  308. "id" => $commodity->id,
  309. 'sku'=>$goods['sku'],
  310. 'name' => $goods['name'],
  311. 'length' => $goods['length'],
  312. 'width' => $goods['width'],
  313. 'height' => $goods['height'],
  314. 'volumn' => $goods['volumn'],
  315. ];
  316. unset($createCommodities[$barcodeIndex[$code->code]]);
  317. break;
  318. }
  319. }
  320. }
  321. if (count($updateCommodities) > 0){
  322. $commodityService->batchUpdate($updateCommodities);
  323. LogService::log(__METHOD__,"同步商品-批量更新",json_encode($updateCommodities));
  324. }
  325. }
  326. if (count($createCommodities) > 0){
  327. $commodityService->insert($createCommodities);
  328. LogService::log(__METHOD__,"同步商品-录入商品",json_encode($createCommodities));
  329. $commodities = $commodityService->get(['owner_id'=>$owner_id , 'sku'=>$skus]);
  330. $barcodeInsert = [];
  331. foreach ($commodities as $commodity){
  332. foreach ($barcodeMap[$commodity->sku] as $code){
  333. $barcodeInsert[] = [
  334. 'commodity_id'=>$commodity->id,
  335. 'code' => $code,
  336. 'created_at' => $today
  337. ];
  338. }
  339. }
  340. $commodityBarcodeService->insert($barcodeInsert);
  341. LogService::log(__METHOD__,"同步商品-录入条码",json_encode($barcodeInsert));
  342. }
  343. }
  344. }