diff --git a/IES-Bandung/app/Http/Controllers/ObjectSellController.php b/IES-Bandung/app/Http/Controllers/ObjectSellController.php index bccba197b10eef74ba438d618b53b58920f13adf..5e3fcc739606d871237d26d0a07e590b91c9e31d 100644 --- a/IES-Bandung/app/Http/Controllers/ObjectSellController.php +++ b/IES-Bandung/app/Http/Controllers/ObjectSellController.php @@ -10,6 +10,7 @@ use App\Model\ObjectType; use App\Model\VendorUser; use Validator; use Auth; +use DB; class ObjectSellController extends Controller { @@ -79,4 +80,20 @@ class ObjectSellController extends Controller return redirect('/profile'); } + + public function showAllObjectSellToJSON() { + $objects = ObjectSell::join('object_types', 'object_sells.object_type_id', '=', 'object_types.id') + ->select(DB::raw('object_sells.name as nama, object_types.name as jenis, min(price) as termurah, max(price) as termahal, avg(price) as rerata, unit as satuan')) + ->groupBy('object_sells.name')->groupBy('object_types.name')->groupBy('unit') + ->get(); + + $result = array(); + foreach ($objects as $value) { + $value->termurah = number_format($value->termurah, 0, '', '.'); + $value->termahal = number_format($value->termahal, 0, '', '.'); + $value->rerata = number_format($value->rerata, 0, '', '.'); + } + $result['aaData'] = $objects; + return response()->json($result); + } } diff --git a/IES-Bandung/app/Http/routes.php b/IES-Bandung/app/Http/routes.php index 71730420f33e1194d6d2b2a43173383ca9f8451d..9ae35916eaa1708484db75868c1d7d8a39491ab9 100755 --- a/IES-Bandung/app/Http/routes.php +++ b/IES-Bandung/app/Http/routes.php @@ -32,6 +32,7 @@ Route::post('/item/update', 'ObjectSellController@updateObjectSell'); Route::post('/item/delete/{id}', ['uses' => 'ObjectSellController@deleteObjectSell']); Route::get('/api/object_type', 'ObjectTypeController@showAllNamesToJSON'); +Route::get('/api/pasar', 'ObjectSellController@showAllObjectSellToJSON'); Route::get('/market', 'MarketController@index'); diff --git a/IES-Bandung/app/Model/ObjectSell.php b/IES-Bandung/app/Model/ObjectSell.php index 9f820f53ba93fea229e7179935b6133a9e2e14bb..06b603790f5a23ec3c1a45bb2f6d6f60638c4963 100644 --- a/IES-Bandung/app/Model/ObjectSell.php +++ b/IES-Bandung/app/Model/ObjectSell.php @@ -19,19 +19,25 @@ class ObjectSell extends Model } public function getAllObjectSell($vendorUserID) { - return $this->join('object_types', 'ObjectSell.object_type_id', '=', 'ObjectType.id')->where('vendor_user_id', '=', $vendorUserID)->get(); + return $this->join('object_types', 'object_sells.object_type_id', '=', 'object_types.id')->where('vendor_user_id', '=', $vendorUserID)->get(); } public static function getMostUpdated($vendorID) { $updatedObject = ObjectSell::where('vendor_user_id', '=', $vendorID)->orderBy('updated_at', 'desc')->limit(5)->get(); foreach ($updatedObject as $key => $value) { - $lastlog = DB::table('object_sell_logs')->where('object_sell_id', '=', $value['id'])->where('old_price', '<>', 'new_price')->orderBy('updated_at', 'desc')->select('old_price', 'new_price')->first(); + $lastlog = ObjectSell::getOldPrice($value['id']); if(is_null($lastlog)) $value['old_price'] = $value['price']; else - $value['old_price'] = $lastlog->old_price; + $value['old_price'] = $lastlog; $value['presentase'] = 100.0*($value['price']-$value['old_price'])/$value['old_price']; } return $updatedObject; } + public static function getOldPrice($object_sell_id) { + $result = DB::table('object_sell_logs')->where('object_sell_id', '=', $object_sell_id)->where('old_price', '<>', 'new_price')->orderBy('updated_at', 'desc')->select('old_price', 'new_price')->first(); + if(is_null($result)) + return null; + return $result->old_price; + } } diff --git a/IES-Bandung/resources/views/home.blade.php b/IES-Bandung/resources/views/home.blade.php index 8a35bedde979a6fd73e07a79bb120be6cfdaae32..109105657e0db6e7d4301eeec7b816a2400aef26 100755 --- a/IES-Bandung/resources/views/home.blade.php +++ b/IES-Bandung/resources/views/home.blade.php @@ -35,22 +35,24 @@ <div class="panel-body"> <div class="table-responsive"> <table ui-jq="dataTable" ui-options="{ - sAjaxSource: 'api/pasar.json', + sAjaxSource: 'api/pasar', aoColumns: [ { mData: 'nama' }, { mData: 'jenis' }, - { mData: 'termurah' }, { mData: 'termahal' }, - { mData: 'kenaikan' } + { mData: 'termurah' }, + { mData: 'rerata' }, + { mData: 'satuan' } ] }" class="table table-striped b-t b-b"> <thead> <tr> - <th style="width:30%">Nama</th> + <th style="width:20%">Nama</th> <th style="width:20%">Jenis</th> - <th style="width:20%">Harga Termahal/Kg</th> - <th style="width:20%">Harga Termurah/Kg</th> - <th style="width:10%">Persentase Kenaikan</th> + <th style="width:15%">Harga Termahal</th> + <th style="width:15%">Harga Termurah</th> + <th style="width:15%">Harga Rata-rata</th> + <th style="width:15%">Satuan</th> </tr> </thead> <tbody>