2016-01-30 10:56:27 +01:00
|
|
|
<?php namespace App\Http\Controllers;
|
|
|
|
|
2016-02-01 03:32:12 +01:00
|
|
|
use App\Ninja\Repositories\ProductRepository;
|
2016-01-30 10:57:47 +01:00
|
|
|
use App\Ninja\Transformers\ProductTransformer;
|
2016-01-30 10:56:27 +01:00
|
|
|
use Auth;
|
|
|
|
use Str;
|
|
|
|
use DB;
|
|
|
|
use Datatable;
|
|
|
|
use Utils;
|
|
|
|
use URL;
|
|
|
|
use View;
|
|
|
|
use Input;
|
|
|
|
use Session;
|
|
|
|
use Redirect;
|
|
|
|
|
|
|
|
use App\Models\Product;
|
|
|
|
use App\Models\TaxRate;
|
|
|
|
use App\Services\ProductService;
|
|
|
|
|
|
|
|
class ProductApiController extends BaseAPIController
|
|
|
|
{
|
|
|
|
protected $productService;
|
2016-05-01 22:55:13 +02:00
|
|
|
protected $productRepo;
|
|
|
|
|
|
|
|
protected $entityType = ENTITY_PRODUCT;
|
2016-02-01 03:32:12 +01:00
|
|
|
|
|
|
|
public function __construct(ProductService $productService, ProductRepository $productRepo)
|
2016-01-30 10:56:27 +01:00
|
|
|
{
|
2016-03-02 15:36:46 +01:00
|
|
|
parent::__construct();
|
2016-01-30 10:56:27 +01:00
|
|
|
|
|
|
|
$this->productService = $productService;
|
2016-02-01 03:32:12 +01:00
|
|
|
$this->productRepo = $productRepo;
|
2016-01-30 10:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2016-05-01 22:55:13 +02:00
|
|
|
$products = Product::scope()
|
|
|
|
->withTrashed()
|
|
|
|
->orderBy('created_at', 'desc');
|
2016-01-30 11:51:52 +01:00
|
|
|
|
2016-05-01 22:55:13 +02:00
|
|
|
return $this->returnList($products);
|
2016-01-30 10:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDatatable()
|
|
|
|
{
|
|
|
|
return $this->productService->getDatatable(Auth::user()->account_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
2016-02-01 03:32:12 +01:00
|
|
|
public function update(\Illuminate\Http\Request $request, $publicId)
|
2016-01-30 10:56:27 +01:00
|
|
|
{
|
2016-02-01 03:32:12 +01:00
|
|
|
|
|
|
|
if ($request->action == ACTION_ARCHIVE) {
|
|
|
|
$product = Product::scope($publicId)->withTrashed()->firstOrFail();
|
|
|
|
$this->productRepo->archive($product);
|
|
|
|
|
|
|
|
$transformer = new ProductTransformer(\Auth::user()->account, Input::get('serializer'));
|
|
|
|
$data = $this->createItem($product, $transformer, 'products');
|
|
|
|
|
|
|
|
return $this->response($data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return $this->save($publicId);
|
2016-01-30 10:56:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-30 12:29:22 +01:00
|
|
|
public function destroy($publicId)
|
2016-01-30 10:56:27 +01:00
|
|
|
{
|
2016-02-01 03:32:12 +01:00
|
|
|
//stub
|
2016-01-30 10:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function save($productPublicId = false)
|
|
|
|
{
|
|
|
|
if ($productPublicId) {
|
|
|
|
$product = Product::scope($productPublicId)->firstOrFail();
|
|
|
|
} else {
|
|
|
|
$product = Product::createNew();
|
|
|
|
}
|
|
|
|
|
|
|
|
$product->product_key = trim(Input::get('product_key'));
|
|
|
|
$product->notes = trim(Input::get('notes'));
|
|
|
|
$product->cost = trim(Input::get('cost'));
|
|
|
|
//$product->default_tax_rate_id = Input::get('default_tax_rate_id');
|
|
|
|
|
|
|
|
$product->save();
|
|
|
|
|
2016-01-30 10:57:47 +01:00
|
|
|
$transformer = new ProductTransformer(\Auth::user()->account, Input::get('serializer'));
|
2016-01-30 10:56:27 +01:00
|
|
|
$data = $this->createItem($product, $transformer, 'products');
|
|
|
|
|
|
|
|
return $this->response($data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|