CommodityController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Commodity;
  4. use App\Imports\CommodityImport;
  5. use App\OracleBasSKU;
  6. use Exception;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Http\Response;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\Gate;
  12. use Illuminate\Support\Facades\Validator;
  13. use Maatwebsite\Excel\Facades\Excel;
  14. class CommodityController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return Response
  20. */
  21. public function index()
  22. {
  23. if(!Gate::allows('商品信息-查询')){ return redirect(url('/')); }
  24. $commodities=Commodity::query()->orderBy('id','desc')->paginate(50);
  25. return view('maintenance.commodity.index',['commodities'=>$commodities]);
  26. }
  27. /**
  28. * Show the form for creating a new resource.
  29. *
  30. * @return Response
  31. */
  32. public function create()
  33. {
  34. if(!Gate::allows('商品信息-录入')){ return redirect(url('/')); }
  35. return view('maintenance.commodity.create');
  36. }
  37. /**
  38. * Store a newly created resource in storage.
  39. *
  40. * @param Request $request
  41. * @return Response
  42. */
  43. public function store(Request $request)
  44. {
  45. if(!Gate::allows('商品信息-录入')){ return redirect(url('/')); }
  46. $this->validatorCreate($request->all())->validate();
  47. $commodity=new Commodity($request->all());
  48. $commodity->save();
  49. $commodity->newBarcode($request->input('barcode'));
  50. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  51. return redirect('maintenance/commodity/create')->with('successTip',"成功录入商品信息:“{$request->input('name')}”");
  52. }
  53. protected function validatorCreate(array $data)
  54. {
  55. return Validator::make($data, [
  56. 'name' => ['required', 'string', 'max:50'],
  57. // 'barcode' => ['required', 'string', 'max:50', 'unique:commodities'],
  58. ]);
  59. }
  60. protected function validatorUpdate(array $data)
  61. {
  62. return Validator::make($data, [
  63. 'name' => ['required', 'string', 'max:50'],
  64. 'barcode' => ['required', 'string', 'max:50'],
  65. ]);
  66. }
  67. /**
  68. * Display the specified resource.
  69. *
  70. * @param Commodity $commodity
  71. * @return Response
  72. */
  73. public function show(Commodity $commodity)
  74. {
  75. //
  76. }
  77. /**
  78. * Show the form for editing the specified resource.
  79. *
  80. * @param Commodity $commodity
  81. * @return Response
  82. */
  83. public function edit(Commodity $commodity)
  84. {
  85. if(!Gate::allows('商品信息-编辑')){ return redirect(url('/')); }
  86. return view('maintenance.commodity.edit',['commodity'=>$commodity]);
  87. }
  88. /**
  89. * Update the specified resource in storage.
  90. *
  91. * @param Request $request
  92. * @param Commodity $commodity
  93. * @return Response
  94. */
  95. public function update(Request $request, Commodity $commodity)
  96. {
  97. if(!Gate::allows('商品信息-编辑')){ return redirect(url('/')); }
  98. $this->validatorUpdate($request->all())->validate();
  99. $commodity->fill($request->all());
  100. $commodity->update();
  101. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  102. return redirect('maintenance/commodity/')->with('successTip',"成功修改商品信息:“{$commodity['name']}”!");
  103. }
  104. /**
  105. * Remove the specified resource from storage.
  106. *
  107. * @param Commodity $commodity
  108. * @return array|Response
  109. * @throws Exception
  110. */
  111. public function destroy(Commodity $commodity)
  112. {
  113. if(!Gate::allows('商品信息-删除')){ return redirect(url('/')); }
  114. $this->log(__METHOD__,__FUNCTION__,$commodity->toJson(),Auth::user()['id']);
  115. $re=$commodity->delete();
  116. return ['success'=>$re];
  117. }
  118. public function import()
  119. {
  120. return view('maintenance.commodity.import');
  121. }
  122. public function importExcel(Request $request)
  123. {
  124. $isOverride = $request->input('isOverride');
  125. try{
  126. ini_set('max_execution_time',2500);
  127. ini_set('memory_limit','1526M');
  128. $extension=$request->file()['file']->getClientOriginalExtension();
  129. $extension[0] = strtoupper($extension[0]);
  130. Excel::import(new CommodityImport($isOverride), $request->file()['file']->path(),null,$extension);
  131. return '<h1 class="text-success">导入成功</h1>';
  132. }catch (Exception $e){
  133. if(strstr($e->getMessage(),'No ReaderType')){return '<h1 class="text-danger">没有上传写权限,请修改php.ini 对应的upload_tmp_dir 目录或其权限</h1>'.$e->getMessage();}
  134. if(strstr($e->getMessage(),'SQLSTATE')){return '<h1 class="text-danger">数据库插入错误,数据不支持,可能有重复或异常字符</h1>'.$e->getMessage();}
  135. if(strstr(strtolower($e->getMessage()),'sku')){return '<h1 class="text-danger">请在第一行将 商品编码 字段名改成“SKU”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  136. if(strstr(strtolower($e->getMessage()),'name')){return '<h1 class="text-danger">请在第一行将 商品名称 字段名改成“name”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  137. if(strstr(strtolower($e->getMessage()),'barcode')){return '<h1 class="text-danger">请在第一行将 商品条码 字段名改成“barcode”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  138. if(strstr(strtolower($e->getMessage()),'owner')){return '<h1 class="text-danger">请在第一行将 货主 字段名改成“owner”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  139. return '<h1 class="text-danger">失败</h1>'.$e->getMessage();
  140. }
  141. }
  142. public function apiGetCommodityByBarcode(Request $request)
  143. {
  144. $barcode=$request->input('barcode');
  145. $name = '';
  146. if($barcode){
  147. $commodity=Commodity::whereHas('barcodes', function (Builder $query)use($barcode){
  148. $query->where('code',$barcode);
  149. })->first();
  150. if($commodity&&$commodity['name']) $name=$commodity['name'];
  151. }
  152. return ['success'=>'true','name'=>$name];
  153. }
  154. public function syncWMS(Request $request){
  155. $owner_code = $request->owner_code ?? false;
  156. $owner_id = $request->owner_id ?? false;
  157. if (!$owner_code || !$owner_id)return ['success'=>false, 'data'=>"未指定货主"];
  158. $map = [];
  159. $skus = [];
  160. $wmsCommodities = app('oracleBasSkuService')->getOwnerCommodities(['code' => $owner_code]);
  161. foreach ($wmsCommodities as $index => $wmsCommodity){
  162. $map[$wmsCommodity->sku] = $index;
  163. $skus[] = $wmsCommodity->sku;
  164. }
  165. $commodities = app('commodityService')->getOwnerCommodities(['id' => $owner_id, 'sku'=>$skus]);
  166. $updateCommodities = [];
  167. $updateCommodities[] = [
  168. 'id', 'name', 'length', 'width', 'height', 'volumn',
  169. ];
  170. foreach ($commodities as $commodity){
  171. $wms = $wmsCommodities[$map[$commodity->sku]];
  172. if (($commodity->length != $wms->skulength) || ($commodity->width != $wms->skuwidth)
  173. || ($commodity->height != $wms->skuhigh) || ($commodity->volumn != $wms->cube)
  174. || ($commodity->name != $wms->descr_c)){
  175. $updateCommodities[] = [
  176. 'id'=>$commodity->id,
  177. 'name' => $wms->descr_c,
  178. 'length' => $wms->skulength,
  179. 'width' => $wms->skuwidth,
  180. 'height' => $wms->skuhigh,
  181. 'volumn' => $wms->cube
  182. ];
  183. }
  184. unset($wmsCommodities[$map[$commodity->sku]]);
  185. unset($map[$commodity->sku]);
  186. }
  187. unset($commodities);
  188. unset($skus);
  189. if (count($updateCommodities) > 1)app('commodityService')->batchUpdate($updateCommodities);
  190. $createCommodities = [];
  191. $today = date('Y-m-d H:i:s');
  192. foreach ($map as $sku => $index){
  193. $wms = $wmsCommodities[$index];
  194. $createCommodities[] = [
  195. 'owner_id' => $owner_id,
  196. 'sku' => $wms->sku,
  197. 'name' => $wms->descr_c,
  198. 'length' => $wms->skulength,
  199. 'width' => $wms->skuwidth,
  200. 'height' => $wms->skuhigh,
  201. 'volumn' => $wms->cube,
  202. "created_at" => $today,
  203. ];
  204. unset($wmsCommodities[$index]);
  205. }
  206. if (count($createCommodities) > 0)app('commodityService')->insert($createCommodities);
  207. return ['success'=>true];
  208. }
  209. }