| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- namespace App\Http\Controllers;
- use App\Commodity;
- use App\Imports\CommodityImport;
- use App\OracleBasSKU;
- use Exception;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Http\Request;
- use Illuminate\Http\Response;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Gate;
- use Illuminate\Support\Facades\Validator;
- use Maatwebsite\Excel\Facades\Excel;
- class CommodityController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return Response
- */
- public function index()
- {
- if(!Gate::allows('商品信息-查询')){ return redirect(url('/')); }
- $commodities=Commodity::query()->orderBy('id','desc')->paginate(50);
- return view('maintenance.commodity.index',['commodities'=>$commodities]);
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return Response
- */
- public function create()
- {
- if(!Gate::allows('商品信息-录入')){ return redirect(url('/')); }
- return view('maintenance.commodity.create');
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param Request $request
- * @return Response
- */
- public function store(Request $request)
- {
- if(!Gate::allows('商品信息-录入')){ return redirect(url('/')); }
- $this->validatorCreate($request->all())->validate();
- $commodity=new Commodity($request->all());
- $commodity->save();
- $commodity->newBarcode($request->input('barcode'));
- $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
- return redirect('maintenance/commodity/create')->with('successTip',"成功录入商品信息:“{$request->input('name')}”");
- }
- protected function validatorCreate(array $data)
- {
- return Validator::make($data, [
- 'name' => ['required', 'string', 'max:50'],
- // 'barcode' => ['required', 'string', 'max:50', 'unique:commodities'],
- ]);
- }
- protected function validatorUpdate(array $data)
- {
- return Validator::make($data, [
- 'name' => ['required', 'string', 'max:50'],
- 'barcode' => ['required', 'string', 'max:50'],
- ]);
- }
- /**
- * Display the specified resource.
- *
- * @param Commodity $commodity
- * @return Response
- */
- public function show(Commodity $commodity)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param Commodity $commodity
- * @return Response
- */
- public function edit(Commodity $commodity)
- {
- if(!Gate::allows('商品信息-编辑')){ return redirect(url('/')); }
- return view('maintenance.commodity.edit',['commodity'=>$commodity]);
- }
- /**
- * Update the specified resource in storage.
- *
- * @param Request $request
- * @param Commodity $commodity
- * @return Response
- */
- public function update(Request $request, Commodity $commodity)
- {
- if(!Gate::allows('商品信息-编辑')){ return redirect(url('/')); }
- $this->validatorUpdate($request->all())->validate();
- $commodity->fill($request->all());
- $commodity->update();
- $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
- return redirect('maintenance/commodity/')->with('successTip',"成功修改商品信息:“{$commodity['name']}”!");
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param Commodity $commodity
- * @return array|Response
- * @throws Exception
- */
- public function destroy(Commodity $commodity)
- {
- if(!Gate::allows('商品信息-删除')){ return redirect(url('/')); }
- $this->log(__METHOD__,__FUNCTION__,$commodity->toJson(),Auth::user()['id']);
- $re=$commodity->delete();
- return ['success'=>$re];
- }
- public function import()
- {
- return view('maintenance.commodity.import');
- }
- public function importExcel(Request $request)
- {
- $isOverride = $request->input('isOverride');
- try{
- ini_set('max_execution_time',2500);
- ini_set('memory_limit','1526M');
- $extension=$request->file()['file']->getClientOriginalExtension();
- $extension[0] = strtoupper($extension[0]);
- Excel::import(new CommodityImport($isOverride), $request->file()['file']->path(),null,$extension);
- return '<h1 class="text-success">导入成功</h1>';
- }catch (Exception $e){
- if(strstr($e->getMessage(),'No ReaderType')){return '<h1 class="text-danger">没有上传写权限,请修改php.ini 对应的upload_tmp_dir 目录或其权限</h1>'.$e->getMessage();}
- if(strstr($e->getMessage(),'SQLSTATE')){return '<h1 class="text-danger">数据库插入错误,数据不支持,可能有重复或异常字符</h1>'.$e->getMessage();}
- if(strstr(strtolower($e->getMessage()),'sku')){return '<h1 class="text-danger">请在第一行将 商品编码 字段名改成“SKU”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
- if(strstr(strtolower($e->getMessage()),'name')){return '<h1 class="text-danger">请在第一行将 商品名称 字段名改成“name”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
- if(strstr(strtolower($e->getMessage()),'barcode')){return '<h1 class="text-danger">请在第一行将 商品条码 字段名改成“barcode”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
- if(strstr(strtolower($e->getMessage()),'owner')){return '<h1 class="text-danger">请在第一行将 货主 字段名改成“owner”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
- return '<h1 class="text-danger">失败</h1>'.$e->getMessage();
- }
- }
- public function apiGetCommodityByBarcode(Request $request)
- {
- $barcode=$request->input('barcode');
- $name = '';
- if($barcode){
- $commodity=Commodity::whereHas('barcodes', function (Builder $query)use($barcode){
- $query->where('code',$barcode);
- })->first();
- if($commodity&&$commodity['name']) $name=$commodity['name'];
- }
- return ['success'=>'true','name'=>$name];
- }
- public function syncWMS(Request $request){
- $owner_code = $request->owner_code ?? false;
- $owner_id = $request->owner_id ?? false;
- if (!$owner_code || !$owner_id)return ['success'=>false, 'data'=>"未指定货主"];
- $map = [];
- $skus = [];
- $wmsCommodities = app('oracleBasSkuService')->getOwnerCommodities(['code' => $owner_code]);
- foreach ($wmsCommodities as $index => $wmsCommodity){
- $map[$wmsCommodity->sku] = $index;
- $skus[] = $wmsCommodity->sku;
- }
- $commodities = app('commodityService')->getOwnerCommodities(['id' => $owner_id, 'sku'=>$skus]);
- $updateCommodities = [];
- $updateCommodities[] = [
- 'id', 'name', 'length', 'width', 'height', 'volumn',
- ];
- foreach ($commodities as $commodity){
- $wms = $wmsCommodities[$map[$commodity->sku]];
- if (($commodity->length != $wms->skulength) || ($commodity->width != $wms->skuwidth)
- || ($commodity->height != $wms->skuhigh) || ($commodity->volumn != $wms->cube)
- || ($commodity->name != $wms->descr_c)){
- $updateCommodities[] = [
- 'id'=>$commodity->id,
- 'name' => $wms->descr_c,
- 'length' => $wms->skulength,
- 'width' => $wms->skuwidth,
- 'height' => $wms->skuhigh,
- 'volumn' => $wms->cube
- ];
- }
- unset($wmsCommodities[$map[$commodity->sku]]);
- unset($map[$commodity->sku]);
- }
- unset($commodities);
- unset($skus);
- if (count($updateCommodities) > 1)app('commodityService')->batchUpdate($updateCommodities);
- $createCommodities = [];
- $today = date('Y-m-d H:i:s');
- foreach ($map as $sku => $index){
- $wms = $wmsCommodities[$index];
- $createCommodities[] = [
- 'owner_id' => $owner_id,
- 'sku' => $wms->sku,
- 'name' => $wms->descr_c,
- 'length' => $wms->skulength,
- 'width' => $wms->skuwidth,
- 'height' => $wms->skuhigh,
- 'volumn' => $wms->cube,
- "created_at" => $today,
- ];
- unset($wmsCommodities[$index]);
- }
- if (count($createCommodities) > 0)app('commodityService')->insert($createCommodities);
- return ['success'=>true];
- }
- }
|