Api Komoditi Dashboard
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Komuditi;
|
||||
use App\KomuditiPurchase;
|
||||
use App\KomuditiTransaksi;
|
||||
use App\KomuditiProduk;
|
||||
use DB;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Carbon\Carbon;
|
||||
@@ -178,4 +179,111 @@ class KomuditiController extends Controller
|
||||
|
||||
return response()->json($komuditi_transaksi);
|
||||
}
|
||||
public function jumlahKomoditiDashboard()
|
||||
{
|
||||
$current = Carbon::now();
|
||||
$current = new Carbon();
|
||||
$komoditi = [];
|
||||
|
||||
$jumlahkomoditi = Komuditi::on('mysql7')->where([['date_purchase','=',$current->toDateString()], ['transaction_type', '=', 'Beli Komoditi']])->orderBy('created_at','desc')->first();
|
||||
|
||||
$bakikomodititransaksi = KomuditiPurchase::on('mysql7')->where('date_purchase','=',$current->toDateString())->orderBy('created_at','desc')->first();
|
||||
|
||||
array_push($komoditi,['jumlah_komoditi'=>number_format($jumlahkomoditi['unit_komuditi'],2),'baki_komoditi'=>number_format($bakikomodititransaksi['unit_komuditi'],2)]);
|
||||
|
||||
return response()->json($komoditi);
|
||||
|
||||
}
|
||||
|
||||
public function statistikKomoditiDashboard(Request $request)
|
||||
{
|
||||
$current = Carbon::now();
|
||||
$current = new Carbon();
|
||||
$weekly = Carbon::now();
|
||||
$weekly = new Carbon();
|
||||
$monthly = Carbon::now();
|
||||
$monthly = new Carbon();
|
||||
$yearly = Carbon::now();
|
||||
$yearly = new Carbon();
|
||||
$weekly_array = Carbon::now();
|
||||
$weekly_array = new Carbon();
|
||||
$monthly_array = Carbon::now();
|
||||
$monthly_array = new Carbon();
|
||||
$yearly_array = Carbon::now();
|
||||
$yearly_array = new Carbon();
|
||||
|
||||
$array_totalup = [];
|
||||
|
||||
if($request->method == 'daily'){
|
||||
array_push($array_totalup,['tarikh'=>$current->toDateString(),'totalkomoditi'=>0]);
|
||||
}
|
||||
if($request->method == 'weekly'){//weekly
|
||||
for($i=0;$i<7;$i++){
|
||||
if($i == 0){
|
||||
$weekly_array_date = $weekly->subDays(0);
|
||||
}else{
|
||||
$weekly_array_date = $weekly->subDays(1);
|
||||
}
|
||||
array_push($array_totalup,['tarikh'=>$weekly_array_date->toDateString(),'totalkomoditi'=>0]);
|
||||
}
|
||||
}
|
||||
if($request->method == 'monthly'){
|
||||
for($i=0;$i<12;$i++){
|
||||
if($i == 0){
|
||||
$monthly_array_date = $monthly_array->startOfMonth()->subMonths(0);
|
||||
}else{
|
||||
$monthly_array_date = $monthly_array->startOfMonth()->subMonths(1);
|
||||
}
|
||||
array_push($array_totalup,['tarikh'=>$monthly_array_date->year.'-'.sprintf("%02d", $monthly_array_date->month),'totalkomoditi'=>0]);
|
||||
}
|
||||
}
|
||||
if($request->method == 'yearly'){
|
||||
for($i=0;$i<5;$i++){
|
||||
if($i == 0){
|
||||
$yearly_array_date = $yearly_array->startOfYear()->subYears(0);
|
||||
}else{
|
||||
$yearly_array_date = $yearly_array->startOfYear()->subYears(1);
|
||||
}
|
||||
array_push($array_totalup,['tarikh'=>sprintf("%04d",$yearly_array_date->year),'totalkomoditi'=>0]);
|
||||
}
|
||||
}
|
||||
$weekly_date = $weekly->subDays(6)->toDateString();
|
||||
$monthly_date = $monthly->startOfMonth()->subMonths(11)->toDateString();
|
||||
$yearly_date = $yearly->startOfYear()->subYears(4)->toDateString();
|
||||
// foreach($cawangan_all as $index => $cawangan){
|
||||
if($request->method == 'daily'){
|
||||
$pembeliankomuditi = Komuditi::on('mysql7')->selectRaw('SUM(unit_komuditi) as totalkomoditi')->addSelect('date_purchase')->groupBy('date_purchase')
|
||||
->where('date_purchase','=',$current->toDateString())->orderBy('created_at','desc')->get();
|
||||
}elseif($request->method == 'weekly')
|
||||
{
|
||||
$pembeliankomuditi = Komuditi::on('mysql')->selectRaw('SUM(unit_komuditi) as totalkomoditi')->addSelect('date_purchase')->groupBy('date_purchase')
|
||||
->where([['date_purchase','<=',$current->toDateString()],['date_purchase','>=',$weekly_date]])->orderBy('created_at','desc')->get();
|
||||
|
||||
}elseif($request->method == 'monthly')
|
||||
{
|
||||
$pembeliankomuditi = Komuditi::on('mysql')->selectRaw('SUM(unit_komuditi) AS totalkomoditi')->addSelect(DB::connection('mysql')->raw('DATE_FORMAT(date_purchase, "%Y-%m") as date_purchase'))
|
||||
->groupBy(DB::connection('mysql')->raw("DATE_FORMAT(date_purchase,'%Y%m')"))->where([['date_purchase','<=',$current->toDateString()],['date_purchase','>=',$monthly_date]])
|
||||
->orderBy('created_at','desc')->get();
|
||||
|
||||
}elseif($request->method == 'yearly')
|
||||
{
|
||||
$pembeliankomuditi = Komuditi::on('mysql')->selectRaw('SUM(unit_komuditi) AS totalkomoditi')->addSelect(DB::connection('mysql')->raw('DATE_FORMAT(date_purchase, "%Y") as date_purchase'))
|
||||
->groupBy(DB::connection('mysql')->raw("DATE_FORMAT(date_purchase,'%Y')"))->where([['date_purchase','<=',$current->toDateString()],['date_purchase','>=',$yearly_date]])
|
||||
->orderBy('created_at','desc')->get();
|
||||
}
|
||||
|
||||
foreach($array_totalup as $array_index=>$totalup)
|
||||
{
|
||||
foreach($pembeliankomuditi as $indextebus=>$pembeliankomuditi_detail){
|
||||
if($totalup['tarikh'] == $pembeliankomuditi_detail['date_purchase']){
|
||||
$jumlahkomoditi = $totalup['totalkomoditi'];
|
||||
$jumlahkomoditi += $pembeliankomuditi_detail['totalkomoditi'];
|
||||
$array_totalup[$array_index]['totalkomoditi'] = $jumlahkomoditi;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return response()->json($array_totalup);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user