如何减少laravel的产品库存?

时间:2017-05-14 03:21:46

标签: laravel stock

我刚刚学习了laravel框架,我有一个项目来制作电子商务。但是在减少产品库存时存在一些限制因素,当客户在产品细节中添加数量并更新购物车然后减少产品库存时,有什么可以帮助我。

实际上,当客户在购物车中添加数量时,我不知道如何存储产品的问题在数据库中减少了。示例产品a在数据库上有库存12,当客户在结账阶段完成后在购物车中添加数量12时,存储在数据库中的产品库存耗尽。

应用/ HTTP /控制器/ CartController.php

<?php

namespace App\Http\Controllers;

use App\Product;
use Session;
use App\Support\CartService;
use Laracasts\Flash\Flash;
use Auth;
use App\Cart;
use Illuminate\Http\Request;

class CartController extends Controller
{

    public function addProduct(Request $request)
    {
        $this->validate($request, [
            'product_id' => 'required|exists:products,product_id',
            'quantity' => 'required|integer|min:1',
            'size' => 'required'
        ]);

        $product = Product::find($request->get('product_id'));
        $quantity = $request->get('quantity');
        $size = $request->get('size');
        Session::flash('flash_product_name', $product->name);

        if(Auth::check()) {
            $cart = Cart::firstOrCreate([
                'product_id' => $product->product_id,
                'user_id' => $request->user()->user_id
            ]);

            $cart->quantity += $quantity;
            $cart->size = $size;
            $cart->save();
            return redirect()->route('index');
        } else {
            $cart = $request->cookie('cart', []);
            if(array_key_exists($product->product_id, $cart)) {
                $quantity += $cart[$product->product_id];
            }
            $cart[$product->product_id] = $quantity;
            return redirect()->route('index')->withCookie(cookie()->forever('cart', $cart));
        }
    }

    public function changeQuantity(Request $request, $product_id)
    {
        $this->validate($request, [
            'quantity' => 'required|integer|min:1',
            'size' => 'required'
        ]);

        $quantity = $request->get('quantity');
        $size = $request->get('size');
        $cart = $this->cart->find($product_id);
        if(!$cart) return redirect('cart');

        Flash::success('Keranjang untuk produk ' . $cart['detail']['name'] . ' berhasil diubah');
        if(Auth::check()) {
            $cart = Cart::firstOrCreate([
                'user_id' => $request->user()->user_id,
                'product_id' => $product_id
            ]);

            $cart->quantity = $quantity;
            $cart->size = $size;
            $cart->save();
            return redirect('cart');
        } else {
            $cart = $request->cookie('cart', []);
            $cart[$product_id] = $quantity;
            return redirect('cart')->withCookie(cookie()->forever('cart', $cart));
        }
    }


}

应用/支持/ CartService.php

<?php

namespace App\Support;

use App\Product;
use Auth;
use App\Cart;
use Cookie;
use App\Address;
use Illuminate\Http\Request;

class CartService
{
  public function lists()
  {
      if(Auth::check()) {
          return Cart::where('user_id', Auth::user()->user_id)->lists('quantity', 'product_id');
      } else {
          return $this->request->cookie('cart');
      }
  }

  public function totalProduct()
  {
      return count($this->lists());
  }

  public function isEmpty()
  {
      return $this->totalProduct() < 1;
  }

  public function totalQuantity()
  {
      $total = 0;
      if ($this->totalProduct() > 0)
      {
        foreach ($this->lists() as $id => $quantity) {
          $product = Product::find($id);
          $total += $quantity;
        }
      }
      return $total;
  }

  public function details()
  {
      $result = [];
      if ($this->totalProduct() > 0) {
          foreach ($this->lists() as $id => $quantity) {
              $product = Product::find($id);
              array_push($result, [
                  'product_id' => $id,
                  'detail' => $product->toArray(),
                  'quantity' => $quantity,
                  'subtotal' => $product->price * $quantity
              ]);
          }
      }
      return $result;
  }

  public function totalPrice()
  {
      $result = 0;
      foreach ($this->details() as $order) {
      $result += $order['subtotal'];
      }
      return $result;
  }

  public function find($product_id)
  {
      foreach ($this->details() as $order) {
      if ($order['product_id'] == $product_id) return $order;
      }
      return null;
  }

  public function merge()
  {
      $cart_cookie = $this->request->cookie('cart', []);
      foreach($cart_cookie as $product_id => $quantity) {
          $cart = Cart::firstOrCreate([
              'user_id' => $this->request->user()->user_id,
              'product_id' => $product_id
          ]);
          $cart->quantity = $cart->quantity > 0 ? $cart->quantity : $quantity;
          $cart->save();
      }
      return Cookie::forget('cart');
  }

  protected function getDestinationId()
  {
      if (Auth::check() && session()->has('checkout.address.address_id')) {
          $address = Address::find(session('checkout.address.address_id'));
          return $address->regency_id;
      }
      return session('checkout.address.regency_id');
  }

  public function shippingFee()
  {
      $totalFee = 0;
      foreach ($this->lists() as $id => $quantity) {
          $fee = Product::find($id)->getCostTo($this->getDestinationId()) * $quantity;
          $totalFee += $fee;
      }
      return $totalFee;
  }

}

0 个答案:

没有答案