/
proc
/
self
/
root
/
home
/
old_henza
/
server
/
app
/
Http
/
Controllers
/
Adm
/
File Upload :
llllll
Current File: //proc/self/root/home/old_henza/server/app/Http/Controllers/Adm/PromotionController.php
<?php namespace App\Http\Controllers\Adm; use App\DB\Te\TableEngine; use App\Http\Controllers\Controller; use App\Models\Major\Cash; use App\Models\Major\Promotion; use App\Models\Major\PromotionItem; use App\Models\Product; use App\Models\ProductBrand; use App\Models\ProductVariant; use App\Models\PromotionProduct; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; class PromotionController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { $records = Promotion::orderBy('id', 'DESC') ->where('show',1); TableEngine::create($records,$request->all()) ->initSearch(['title','description']) ->select([ '*', Promotion::$SELECT_CAJ, Promotion::$SELECT_UAJ, Promotion::$SELECT_TYPE_STR, ])->export($records); return response()->json($records); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // } public function createTablePromotionProduct($promotion,$item_id,$product_id,$discount,$type){ PromotionProduct::create([ 'promotion_id'=>$promotion->id, 'promotion_item_id'=>$item_id, 'product_id'=>$product_id, 'discount'=>$discount, 'type'=>$type, 'active'=>$promotion->is_active, 'start_at'=>$promotion->start_at, 'end_at'=>$promotion->end_at, 'use_only'=>$promotion->use_only ]); } public function createPromotionProduct($promotion){ $items = PromotionItem::where( 'promotion_id',$promotion->id)->get(); if (!is_null($promotion->discount_percent) && $promotion->discount_percent>0){ $discount = $promotion->discount_percent; $type = 1; }else{ $discount = $promotion->discount_price; $type = 2; } if ($promotion->type == 1){ foreach ($items as $item){ $this->createTablePromotionProduct($promotion,$item->id,$item->item_id,$discount,$type); } } elseif ($promotion->type == 2){ foreach ($items as $item){ $products = Product::whereHas('collect',function($q)use($item){ return $q->where('collection_id_id',$item->item_id); })->get(); foreach ($products as $product){ $this->createTablePromotionProduct($promotion,$item->id,$product->id,$discount,$type); } } }elseif ($promotion->type == 4){ foreach ($items as $item){ $products = Product::where('brand_id',$item->item_id) ->whereIn('show_type', [1,3]) ->get(); foreach ($products as $product){ $this->createTablePromotionProduct($promotion,$item->id,$product->id,$discount,$type); } } } } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $input=$request->all(); $input['show']=1; $promotion=Promotion::create($input); foreach ($request->items as $item) { PromotionItem::create([ 'promotion_id'=>$promotion->id, 'item_id'=>$item, ]); } if ($promotion->type_customer == 1){ $this->createPromotionProduct($promotion); } return response()->json(true); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { return response()->json(Promotion::with(['items'])->findOrFail($id)); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $promotion=Promotion::findOrFail($id); $promotion->update($request->all()); if (!is_null($promotion->discount_percent) && $promotion->discount_percent>0){ $discount = $promotion->discount_percent; $type = 1; }else{ $discount = $promotion->discount_price; $type = 2; } $promotion_item_id=PromotionItem::where('promotion_id',$promotion->id)->pluck('item_id')->toArray(); $item_id = $request->items; $diff=array_diff($promotion_item_id,$request->items); $items=PromotionItem::where('promotion_id',$promotion->id)->whereIn('item_id',$diff)->pluck('id'); PromotionItem::destroy($items); PromotionProduct::where('promotion_id',$id) ->whereIn('promotion_item_id',$items) ->delete(); //old method /*$items=PromotionItem::where('promotion_id',$promotion->id)->pluck('id'); PromotionItem::destroy($items);*/ foreach ($request->items as $item) { $promotion_item = PromotionItem::where('promotion_id',$promotion->id)->where('item_id',$item)->first(); if ($promotion_item === null) { PromotionItem::create([ 'promotion_id'=>$promotion->id, 'item_id'=>$item, ]); if ($promotion->type_customer == 1){ $this->createPromotionProduct($promotion); } }else { $promotion_item->update([ 'promotion_id'=>$promotion->id, 'item_id'=>$item, ]); PromotionProduct::where('promotion_id',$id) ->where('promotion_item_id',$promotion_item->id) ->update([ 'active'=>$promotion->is_active, 'start_at'=>$promotion->start_at, 'end_at'=>$promotion->end_at, 'use_only'=>$promotion->use_only, 'type'=>$type, 'discount'=>$discount, ]); } //old method /* PromotionItem::create([ 'promotion_id'=>$promotion->id, 'item_id'=>$item, ]);*/ } // if ($promotion->type_customer == 1){ // PromotionProduct::where('promotion_id',) // $this->createPromotionProduct($promotion); // } return response()->json(true); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { return response()->json(Promotion::safeDelete($id)); } public function updateAutoPromotion(Request $request,$id=17){ $promotion=Promotion::findOrFail($id); $promotion->update($request->all()); return response()->json(true); } public function getAutoPromotion(){ return response()->json(Promotion::findOrFail(17)); } public function getBrand(Request $request){ $records = ProductBrand::query(); TableEngine::create($records, $request->all()) ->initSearch(['title']) ->select(['*' ]) ->export($records); return response()->json($records); } }
Copyright ©2k19 -
Hexid
|
Tex7ure