diff --git a/app/Customer.php b/app/Customer.php index 09a8809..121fe9d 100644 --- a/app/Customer.php +++ b/app/Customer.php @@ -30,6 +30,11 @@ public function company() return $this->belongsTo('\App\Company'); } + public function order() + { + return $this->hasOne('\App\Order'); + } + protected $hidden = [ 'company_id' ]; diff --git a/app/Http/Controllers/Api/V1/CustomerController.php b/app/Http/Controllers/Api/V1/CustomerController.php index 7fe504a..7bb9d9e 100644 --- a/app/Http/Controllers/Api/V1/CustomerController.php +++ b/app/Http/Controllers/Api/V1/CustomerController.php @@ -6,6 +6,7 @@ use Illuminate\Http\Response; use App\Http\Controllers\Controller; use App\Customer; +use Carbon\Carbon; use Illuminate\Support\Facades\Auth; class CustomerController extends Controller @@ -15,9 +16,36 @@ public function __construct(){ } public function get(Request $request){ - $customer = Customer::where('company_id', Auth::user()->company->id)->get(); + $customer = Customer::where('company_id', Auth::user()->company->id); + + if($request->has('filter')){ + $params = (object) json_decode($request->filter, true); + + if(is_array($params->date_range)){ + $params->date_range = (object) $params->date_range; + } + if($params->name){ + $customer = $customer->where('full_name', 'like', '%' . $params->name . '%'); + } + if($params->company){ + $customer = $customer->where('company_name', 'like', '%' . $params->company . '%'); + } + if($params->city){ + $customer = $customer->where('city', 'like', '%' . $params->city . '%'); + } + if($params->country){ + $customer = $customer->where('country', 'like', '%' . $params->country . '%'); + } + + if(is_object($params->date_range) && ($params->date_range->from && $params->date_range->to)){ + $customer = $customer->whereBetween('created_at', [ + Carbon::createFromFormat('m/d/Y', $params->date_range->from)->format('Y-m-d')." 00:00:00", + Carbon::createFromFormat('m/d/Y', $params->date_range->to)->format('Y-m-d')." 23:59:59" + ]); + } + } - if($this->content['data'] = $customer){ + if($this->content['data'] = $customer->get()){ $this->content['status'] = 200; return response()->json($this->content, $this->content['status']); } diff --git a/app/Http/Controllers/Api/V1/OrderController.php b/app/Http/Controllers/Api/V1/OrderController.php index 3c84144..18bbf41 100644 --- a/app/Http/Controllers/Api/V1/OrderController.php +++ b/app/Http/Controllers/Api/V1/OrderController.php @@ -18,10 +18,39 @@ public function __construct(){ public function get(Request $request){ $order = Order::with(['order_detail.product', 'customer']) - ->where('company_id', Auth::user()->company->id) - ->get(); + ->where('company_id', Auth::user()->company->id); - if($this->content['data'] = $order){ + if($request->has('filter')){ + $params = (object) json_decode($request->filter, true); + + if(is_array($params->date_range)){ + $params->date_range = (object) $params->date_range; + } + if($params->id){ + $order = $order->where('id', 'like', '%' . $params->id . '%'); + } + if($params->product){ + $order = $order->whereHas('order_detail', function($q) use ($params) { + $q->whereHas('product', function($q) use ($params) { + $q->where('name', 'like', '%' . $params->product . '%'); + }); + }); + } + if($params->customer){ + $order = $order->whereHas('customer', function($q) use ($params) { + $q->where('full_name', 'like', '%' . $params->customer . '%'); + }); + } + + if(is_object($params->date_range) && ($params->date_range->from && $params->date_range->to)){ + $customer = $customer->whereBetween('created_at', [ + Carbon::createFromFormat('m/d/Y', $params->date_range->from)->format('Y-m-d')." 00:00:00", + Carbon::createFromFormat('m/d/Y', $params->date_range->to)->format('Y-m-d')." 23:59:59" + ]); + } + } + + if($this->content['data'] = $order->get()){ $this->content['status'] = 200; return response()->json($this->content, $this->content['status'], [], JSON_NUMERIC_CHECK); } diff --git a/app/Http/Controllers/Api/V1/ProductController.php b/app/Http/Controllers/Api/V1/ProductController.php index e74d74c..30af676 100644 --- a/app/Http/Controllers/Api/V1/ProductController.php +++ b/app/Http/Controllers/Api/V1/ProductController.php @@ -7,6 +7,8 @@ use App\Http\Controllers\Controller; use App\Product; use App\ProductType; +use App\Customer; +use Carbon\Carbon; use Illuminate\Support\Facades\Auth; use DB; @@ -18,10 +20,35 @@ public function __construct(){ public function get(Request $request){ $product = Product::with('type') - ->where('company_id', Auth::user()->company->id) - ->get(); + ->where('company_id', Auth::user()->company->id); + + if($request->has('filter')){ + $params = (object) json_decode($request->filter, true); + + if(is_array($params->type)){ + $params->type = (object) $params->type; + } + if(is_array($params->date_range)){ + $params->date_range = (object) $params->date_range; + } + if($params->name){ + $product = $product->where('name', 'like', '%' . $params->name . '%'); + } + if($params->sku){ + $product = $product->where('sku', 'like', '%' . $params->sku . '%'); + } + if(is_object($params->type) && $params->type->id){ + $product = $product->where('type_id', $params->type->id); + } + if(is_object($params->date_range) && ($params->date_range->from && $params->date_range->to)){ + $product = $product->whereBetween('created_at', [ + Carbon::createFromFormat('m/d/Y', $params->date_range->from)->format('Y-m-d')." 00:00:00", + Carbon::createFromFormat('m/d/Y', $params->date_range->to)->format('Y-m-d')." 23:59:59" + ]); + } + } - if($this->content['data'] = $product){ + if($this->content['data'] = $product->get()){ $this->content['status'] = 200; return response()->json($this->content, $this->content['status'], [], JSON_NUMERIC_CHECK); } @@ -48,6 +75,25 @@ public function find(Request $request){ return response()->json($this->content, $this->content['status'], [], JSON_NUMERIC_CHECK); } + public function customers(Request $request){ + $product_id = $request->id; + $customers = Customer::whereHas('order', function($query) use ($product_id) { + $query->whereHas('order_detail', function($query) use ($product_id) { + $query->where('product_id', $product_id); + }); + })->get(); + + if($this->content['data'] = $customers){ + $this->content['status'] = 200; + return response()->json($this->content, $this->content['status'], [], JSON_NUMERIC_CHECK); + } + + $this->content['error'] = "Server Error"; + $this->content['status'] = 500; + + return response()->json($this->content, $this->content['status'], [], JSON_NUMERIC_CHECK); + } + public function add(Request $request) { $product = Product::create([ diff --git a/readme.md b/readme.md index d4a318a..4412c04 100644 --- a/readme.md +++ b/readme.md @@ -10,15 +10,20 @@ Storaji is an Open Source, Responsive Inventory Management System, powered by El Go to [Release Section](https://github.com/IndomaximTechID/storaji/releases/) ### Status -Version: 1.0.0-alpha +Version: 1.0.0-beta.0 [TRY DEMO WEBSITE HERE](https://indomaximtechid.github.io/storaji/) ### Latest updates -- First released version 1.0.0-alpha +- Released version 1.0.0-beta.0 ### Consumer features - Products Management - Customers Management -- Add Order and Order Overview +- Orders Management and Order Overview - Statistics -- Top Selling Products \ No newline at end of file +- Top Selling Products +- Customers List on Product Overview +- Reports for Products, Orders, Customers +- Translations for Bahasa Indonesia, English +- Save Report as PDF +- Check for update app \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index 0f63c39..1b214fb 100644 --- a/routes/api.php +++ b/routes/api.php @@ -95,6 +95,11 @@ 'middleware' => 'auth:api', 'uses' => 'ProductController@find', ]); + Route::get('/{id}/customers', [ + 'as' => 'api.products.customers', + 'middleware' => 'auth:api', + 'uses' => 'ProductController@customers', + ]); Route::put('/{id}/update', [ 'as' => 'api.products.update', 'middleware' => 'auth:api',