CommodityController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 Exception;
  9. use Illuminate\Database\Eloquent\Builder;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Http\Response;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\Gate;
  14. use Illuminate\Support\Facades\Validator;
  15. use Maatwebsite\Excel\Facades\Excel;
  16. class CommodityController extends Controller
  17. {
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. * @return Response
  22. */
  23. public function index()
  24. {
  25. if(!Gate::allows('商品信息-查询')){ return redirect(url('/')); }
  26. $commodities=Commodity::query()->orderBy('id','desc')->paginate(50);
  27. return view('maintenance.commodity.index',['commodities'=>$commodities]);
  28. }
  29. /**
  30. * Show the form for creating a new resource.
  31. *
  32. * @return Response
  33. */
  34. public function create()
  35. {
  36. if(!Gate::allows('商品信息-录入')){ return redirect(url('/')); }
  37. return view('maintenance.commodity.create');
  38. }
  39. /**
  40. * Store a newly created resource in storage.
  41. *
  42. * @param Request $request
  43. * @return Response
  44. */
  45. public function store(Request $request)
  46. {
  47. if(!Gate::allows('商品信息-录入')){ return redirect(url('/')); }
  48. $this->validatorCreate($request->all())->validate();
  49. $commodity=new Commodity($request->all());
  50. $commodity->save();
  51. $commodity->newBarcode($request->input('barcode'));
  52. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  53. return redirect('maintenance/commodity/create')->with('successTip',"成功录入商品信息:“{$request->input('name')}”");
  54. }
  55. protected function validatorCreate(array $data)
  56. {
  57. return Validator::make($data, [
  58. 'name' => ['required', 'string', 'max:50'],
  59. // 'barcode' => ['required', 'string', 'max:50', 'unique:commodities'],
  60. ]);
  61. }
  62. protected function validatorUpdate(array $data)
  63. {
  64. return Validator::make($data, [
  65. 'name' => ['required', 'string', 'max:50'],
  66. 'barcode' => ['required', 'string', 'max:50'],
  67. ]);
  68. }
  69. /**
  70. * Display the specified resource.
  71. *
  72. * @param Commodity $commodity
  73. * @return Response
  74. */
  75. public function show(Commodity $commodity)
  76. {
  77. //
  78. }
  79. /**
  80. * Show the form for editing the specified resource.
  81. *
  82. * @param Commodity $commodity
  83. * @return Response
  84. */
  85. public function edit(Commodity $commodity)
  86. {
  87. if(!Gate::allows('商品信息-编辑')){ return redirect(url('/')); }
  88. return view('maintenance.commodity.edit',['commodity'=>$commodity]);
  89. }
  90. /**
  91. * Update the specified resource in storage.
  92. *
  93. * @param Request $request
  94. * @param Commodity $commodity
  95. * @return Response
  96. */
  97. public function update(Request $request, Commodity $commodity)
  98. {
  99. if(!Gate::allows('商品信息-编辑')){ return redirect(url('/')); }
  100. $this->validatorUpdate($request->all())->validate();
  101. $commodity->fill($request->all());
  102. $commodity->update();
  103. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  104. return redirect('maintenance/commodity/')->with('successTip',"成功修改商品信息:“{$commodity['name']}”!");
  105. }
  106. /**
  107. * Remove the specified resource from storage.
  108. *
  109. * @param Commodity $commodity
  110. * @return array|Response
  111. * @throws Exception
  112. */
  113. public function destroy(Commodity $commodity)
  114. {
  115. if(!Gate::allows('商品信息-删除')){ return redirect(url('/')); }
  116. $this->log(__METHOD__,__FUNCTION__,$commodity->toJson(),Auth::user()['id']);
  117. $re=$commodity->delete();
  118. return ['success'=>$re];
  119. }
  120. public function import()
  121. {
  122. return view('maintenance.commodity.import');
  123. }
  124. public function importExcel(Request $request)
  125. {
  126. $isOverride = $request->input('isOverride');
  127. try{
  128. ini_set('max_execution_time',2500);
  129. ini_set('memory_limit','1526M');
  130. $extension=$request->file()['file']->getClientOriginalExtension();
  131. $extension[0] = strtoupper($extension[0]);
  132. Excel::import(new CommodityImport($isOverride), $request->file()['file']->path(),null,$extension);
  133. return '<h1 class="text-success">导入成功</h1>';
  134. }catch (Exception $e){
  135. if(strstr($e->getMessage(),'No ReaderType')){return '<h1 class="text-danger">没有上传写权限,请修改php.ini 对应的upload_tmp_dir 目录或其权限</h1>'.$e->getMessage();}
  136. if(strstr($e->getMessage(),'SQLSTATE')){return '<h1 class="text-danger">数据库插入错误,数据不支持,可能有重复或异常字符</h1>'.$e->getMessage();}
  137. if(strstr(strtolower($e->getMessage()),'sku')){return '<h1 class="text-danger">请在第一行将 商品编码 字段名改成“SKU”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  138. if(strstr(strtolower($e->getMessage()),'name')){return '<h1 class="text-danger">请在第一行将 商品名称 字段名改成“name”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  139. if(strstr(strtolower($e->getMessage()),'barcode')){return '<h1 class="text-danger">请在第一行将 商品条码 字段名改成“barcode”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  140. if(strstr(strtolower($e->getMessage()),'owner')){return '<h1 class="text-danger">请在第一行将 货主 字段名改成“owner”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  141. return '<h1 class="text-danger">失败</h1>'.$e->getMessage();
  142. }
  143. }
  144. public function apiGetCommodityByBarcode(Request $request)
  145. {
  146. $barcode=$request->input('barcode');
  147. $name = '';
  148. if($barcode){
  149. $commodity=Commodity::query()->whereHas('barcodes', function (Builder $query)use($barcode){
  150. $query->where('code',$barcode);
  151. })->first();
  152. if($commodity&&$commodity['name']) $name=$commodity['name'];
  153. }
  154. return ['success'=>'true','name'=>$name];
  155. }
  156. public function syncWMS(Request $request){
  157. if(!Gate::allows('商品信息-编辑')){ return redirect(url('/')); }
  158. $owner_code = $request->owner_code ?? false;
  159. $owner_id = $request->owner_id ?? false;
  160. if (!$owner_code || !$owner_id)return ['success'=>false, 'data'=>"未指定货主"];
  161. $this->syncOwnerCommodities($owner_id,$owner_code);
  162. return ['success'=>true];
  163. }
  164. public function syncOwnerCommodities($owner_id,$owner_code){
  165. $today = date('Y-m-d H:i:s');
  166. $codeColumn = ['alternate_sku1','alternate_sku2'];
  167. $map = [];
  168. $skus = [];
  169. $wmsCommodities = app('oracleBasSkuService')->getOwnerCommodities(['code' => $owner_code]);
  170. foreach ($wmsCommodities as $index => $wmsCommodity){
  171. $map[$wmsCommodity->sku] = $index;
  172. $skus[] = $wmsCommodity->sku;
  173. $trimSku = rtrim($wmsCommodity->sku,"*");
  174. if ($trimSku != $wmsCommodity->sku){
  175. $skus[] = $trimSku;
  176. $map[$trimSku] = $index;
  177. }
  178. }
  179. /** @var CommodityService $commodityService */
  180. $commodityService = app('commodityService');
  181. /** @var CommodityBarcodeService $commodityBarcodeService */
  182. $commodityBarcodeService = app('commodityBarcodeService');
  183. $commodities = $commodityService->getOwnerCommodities(['owner_id' => $owner_id, 'sku'=>$skus]);
  184. $updateCommodities = [];
  185. $updateCommodities[] = [
  186. 'id', 'sku', 'name', 'length', 'width', 'height', 'volumn',
  187. ];
  188. $barcodeMap = [];
  189. $commoditiesId = [];
  190. $barcodes = [];
  191. foreach ($commodities as $commodity){
  192. if (!isset($map[$commodity->sku]))continue;
  193. $wms = $wmsCommodities[$map[$commodity->sku]];
  194. $trimSku = rtrim($wms->sku,"*");
  195. if (($commodity->sku != $trimSku) || ($commodity->length != $wms->skulength)
  196. || ($commodity->width != $wms->skuwidth) || ($commodity->name != $wms->descr_c)
  197. || ($commodity->height != $wms->skuhigh) || ($commodity->volumn != $wms->cube)){
  198. $updateCommodities[] = [
  199. 'id'=>$commodity->id,
  200. 'sku'=>$trimSku,
  201. 'name' => $wms->descr_c,
  202. 'length' => $wms->skulength,
  203. 'width' => $wms->skuwidth,
  204. 'height' => $wms->skuhigh,
  205. 'volumn' => $wms->cube
  206. ];
  207. }
  208. foreach ($codeColumn as $column){
  209. if ($wms[$column]){
  210. $wmsCode = rtrim($wms[$column],"*");
  211. $barcodeMap[$wmsCode] = $commodity->id;
  212. $barcodes[] = $wmsCode;
  213. }
  214. }
  215. $commoditiesId[] = $commodity->id;
  216. unset($wmsCommodities[$map[$commodity->sku]]);
  217. if (isset($map[$wms->sku]))unset($map[$commodity->sku]);
  218. if (isset($map[$trimSku]))unset($map[$trimSku]);
  219. }
  220. unset($commodities,$skus);
  221. if (count($updateCommodities) > 1){
  222. $commodityService->batchUpdate($updateCommodities);
  223. LogService::log(__METHOD__,"同步商品-批量更新",json_encode($updateCommodities));
  224. $commodityBarcodes = $commodityBarcodeService->get(['commodity_id'=>$commoditiesId, 'code'=>$barcodes]);
  225. unset($commoditiesId,$barcodes);
  226. foreach ($commodityBarcodes as $barcode){
  227. if (($barcodeMap[$barcode->code] ?? false) && $barcodeMap[$barcode->code] == $barcode->commodity_id){
  228. unset($barcodeMap[$barcode->code]);
  229. }
  230. }
  231. if (count($barcodeMap) > 0){
  232. $barcodeInsert = [];
  233. foreach ($barcodeMap as $key => $value){
  234. $barcodeInsert[] = [
  235. 'commodity_id'=>$value,
  236. 'code' => $key,
  237. 'created_at' => $today
  238. ];
  239. }
  240. $commodityBarcodeService->insert($barcodeInsert);
  241. LogService::log(__METHOD__,"同步商品-录入条码",json_encode($barcodeInsert));
  242. }
  243. }
  244. $createCommodities = [];
  245. $barcodeMap = [];
  246. $skus = [];
  247. foreach ($map as $sku => $index){
  248. if (substr($sku,-1) == "*")continue;
  249. $wms = $wmsCommodities[$index];
  250. $createCommodities[] = [
  251. 'owner_id' => $owner_id,
  252. 'sku' => $wms->sku,
  253. 'name' => $wms->descr_c,
  254. 'length' => $wms->skulength,
  255. 'width' => $wms->skuwidth,
  256. 'height' => $wms->skuhigh,
  257. 'volumn' => $wms->cube,
  258. "created_at" => $today,
  259. ];
  260. $barcodeMap[$wms->sku] = [];
  261. foreach ($codeColumn as $column){
  262. if ($wms[$column]){
  263. $barcodeMap[$wms->sku][] = rtrim($wms[$column],"*");
  264. }
  265. }
  266. $skus[] = $wms->sku;
  267. unset($wmsCommodities[$index]);
  268. }
  269. if (count($createCommodities) > 0){
  270. $commodityService->insert($createCommodities);
  271. LogService::log(__METHOD__,"同步商品-录入商品",json_encode($createCommodities));
  272. $commodities = $commodityService->get(['owner_id'=>$owner_id , 'sku'=>$skus]);
  273. $barcodeInsert = [];
  274. foreach ($commodities as $commodity){
  275. foreach ($barcodeMap[$commodity->sku] as $code){
  276. $barcodeInsert[] = [
  277. 'commodity_id'=>$commodity->id,
  278. 'code' => $code,
  279. 'created_at' => $today
  280. ];
  281. }
  282. }
  283. $commodityBarcodeService->insert($barcodeInsert);
  284. LogService::log(__METHOD__,"同步商品-录入条码",json_encode($barcodeInsert));
  285. }
  286. }
  287. }