0) { $y = array_keys($s[0]); $x = 'UPDATE ' . $table . ' aa , ( '; $x .= 'SELECT ' . implode(', ', array_map(function ($g) { return 'a.' . $g; }, $y)) . " FROM (\n"; $x .= implode("\n UNION ALL \n", array_map(function ($w) use ($y) { $f = " SELECT "; $f .= implode(', ', array_map(function ($q) use ($w) { return '"' . str_replace('"', '\\"', $w[$q]) . "\" `$q`"; }, $y)); return $f; }, $s)); $x .= "\n) a ) bb SET "; $x .= implode(', ', array_map(function ($c) { return " aa.$c = bb.$c"; }, $y)); $x .= ' WHERE '; if (is_array($wht)) { $x .= implode(' AND ', array_map(function ($whtx) { return " aa.$whtx = bb.$whtx"; }, $wht)); } else { $x .= " aa.$wht = bb.$wht"; } return $x; } else { return []; } } function array_to_insert($array, $table = 'test', $wht = '') { $s = $array; // Check if the array has elements if (count($s) > 0) { $y = array_keys($s[0]); $x = 'INSERT INTO ' . $table . '(' . implode(', ', array_map(function ($u) { return "`$u`"; }, $y)) . ")\n"; $x .= 'SELECT ' . implode(', ', array_map(function ($g) { return 'a.' . $g; }, $y)) . " FROM (\n"; $x .= implode("\n UNION ALL \n", array_map(function ($w) use ($y) { $f = " SELECT "; $f .= implode(', ', array_map(function ($q) use ($w) { if ($w[$q] !== null) { return '"' . str_replace('"', '\\"', $w[$q]) . "\" `$q`"; } else { return '"-" `$q`'; } }, $y)); return $f; }, $s)); $x .= "\n) a"; // Check if wht is an array if (is_array($wht)) { $x .= ' LEFT JOIN ' . $table . ' ON '; $x .= implode(' AND ', array_map(function ($whtx) use ($table) { return "$table.$whtx = a.$whtx"; }, $wht)); $x .= ' WHERE '; $x .= implode(' AND ', array_map(function ($whtx) use ($table) { return "$table.$whtx IS NULL"; }, $wht)); } return $x; } else { return []; } } class Help { public static function cond($array, $search = '', $name = '') { if (!empty($search)) { $search = is_numeric($search) ? strtolower(strval($search)) : strtolower($search); $result = array_filter($array, function ($dat) use ($search, $name) { if (is_array($dat) && isset($dat[$name])) { $g = $dat[$name]; $numcek = 0; if ($g !== null) { $g = is_numeric($g) ? strtolower(strval($g)) : strtolower($g); if ($numcek == 0) { if ($g == $search) { $numcek = 1; } } } return $numcek == 1 ? true : false; } elseif ($dat !== null) { if (is_numeric($dat)) { $dats = strtolower(strval($dat)); return $dats == $search ? true : false; } elseif (strtolower($dat) == $search) { return true; } } return false; }); if(count($result) > 0){ $jx = []; foreach ($result as $key => $ax) { $jx[] = $ax; } return $jx; }else{ return []; } } else { return []; } } } function buat($newDate, $a) { $year = $newDate->format('Y'); $month = $newDate->format('m'); $day = $newDate->format('d'); $format = '00'; $ansMonth = str_pad($month, strlen($format), '0', STR_PAD_LEFT); $ansDay = str_pad($day, strlen($format), '0', STR_PAD_LEFT); $dayKnow = $ansDay . '-' . $ansMonth . '-' . $year; return ($a === null) ? "" : $dayKnow; } function buatN($newDate, $a) { $year = $newDate->format('Y'); $month = $newDate->format('n') - 1; $day = $newDate->format('d'); $format = '00'; $ansMonth = $GLOBALS['namaBulan'][$month]; $ansDay = str_pad($day, strlen($format), '0', STR_PAD_LEFT); $dayKnow = $ansDay . ' ' . $ansMonth . ' ' . $year; return ($a === null) ? "" : $dayKnow; } function buatO($newDate, $a) { $year = $newDate->format('Y'); $month = $newDate->format('m'); $day = $newDate->format('d'); $format = '00'; $ansMonth = str_pad($month, strlen($format), '0', STR_PAD_LEFT); $ansDay = str_pad($day, strlen($format), '0', STR_PAD_LEFT); $dayKnow = $year . '-' . $ansMonth . '-' . $ansDay; return [ 'full' => $dayKnow, 'day' => $newDate->format('w'), ]; } function buatNum($newDate, $a) { $year = $newDate->format('Y'); $month = $newDate->format('m'); $day = $newDate->format('d'); $format = '00'; $ansMonth = str_pad($month, strlen($format), '0', STR_PAD_LEFT); $ansDay = str_pad($day, strlen($format), '0', STR_PAD_LEFT); $dayKnow = $year . $ansMonth . $ansDay; return intval($dayKnow); } function buatC($newDate, $a) { $year = $newDate->format('Y'); $month = $newDate->format('m'); $day = $newDate->format('d'); $dateK = new DateTime($year . '-' . $month . '-' . $day); return $dateK; } function tanggal($a = null) { $newDate = new DateTime(); if ($a !== null) { if ($a === "gugus") { $newDate = new DateTime(helper_sesiGet('tahun') . '-' . helper_sesiGet('bulan')); } else { $newDate = new DateTime($a); } } $date = new DateTime(); $y = $date->format('Y'); $m = $date->format('m'); $firstDay = new DateTime($newDate->format('Y-m') . '-01'); $lastDay = new DateTime($newDate->format('Y-m-t')); $returnData = [ 'oneDayMilisecond' => 86400000, 'milisecond' => $newDate->getTimestamp(), 'normal' => buatO($newDate,$a)['full'], 'cek1' => buatC($newDate,$a), 'sekarang' => buat($newDate,$a), 'sekarang2' => buatN($newDate,$a), 'cek2' => buatC($firstDay,$a), 'normal2' => buatO($firstDay,$a)['full'], 'awal' => buat($firstDay,$a), 'awal2' => buatN($firstDay,$a), 'akhir' => buat($lastDay,$a), 'akhir2' => buatN($lastDay,$a), 'cek3' => buatC($lastDay,$a), 'normal3' => buatO($lastDay,$a)['full'], 'angka' => buatNum($newDate,$a), 'dayn' => $GLOBALS['namaHari'][buatO($newDate,$a)['day']], 'day' => buatO($newDate,$a)['day'], 'day2n' => $GLOBALS['namaHari'][buatO($firstDay,$a)['day']], 'day2' => buatO($firstDay,$a)['day'], 'day3n' => $GLOBALS['namaHari'][buatO($lastDay,$a)['day']], 'day3' => buatO($lastDay,$a)['day'], ]; return $returnData; } function _slice7($w = []) { $n = count($w) / 7; $newArray = []; for ($x = 0; $x < $n; $x++) { $ar = []; for ($j = 0; $j < 7; $j++) { $h = $j + ($x * 7); $ar[] = $w[$h]; } $newArray[] = $ar; } return $newArray; } function accnil($year = null, $app = null) { if (!$year) { $year = explode('-', tanggal()['normal'])[0]; } if (!$app) { $app = "acc"; } $condition = ""; if ($app == 'usp') { $condition = " AND acc.app = 'usp' "; } elseif ($app == 'acc') { $condition = " AND acc.app = 'acc' "; } $year = is_array($year) ? $year : [$year]; $queries = []; foreach ($year as $yr) { $query = " SELECT main, rek, nama_main, nama, sum(if(bln = 'a0', total, 0)) ba0, sum(if(bln = '00', total, 0)) b00, sum(if(bln = '01', total, 0)) b01, sum(if(bln = '02', total, 0)) b02, sum(if(bln = '03', total, 0)) b03, sum(if(bln = '04', total, 0)) b04, sum(if(bln = '05', total, 0)) b05, sum(if(bln = '06', total, 0)) b06, sum(if(bln = '07', total, 0)) b07, sum(if(bln = '08', total, 0)) b08, sum(if(bln = '09', total, 0)) b09, sum(if(bln = '10', total, 0)) b10, sum(if(bln = '11', total, 0)) b11, sum(if(bln = '12', total, 0)) b12, sum(if(bln = '13', total, 0)) b13 FROM ( ( SELECT 'a0' bln, main, nama_main, dk, kode rek, nama, total FROM bbtahunan acc WHERE tahun = " . (intval($yr) - 2) . $condition . " ) UNION ALL ( SELECT '00' bln, main, nama_main, dk, kode rek, nama, total FROM bbtahunan acc WHERE tahun = " . (intval($yr) - 1) . $condition . " ) UNION ALL ( SELECT bln, main, nama_main, dk, rek, nama, sum(total) total FROM ( SELECT lpad(month(tgl), 2, 0) bln, main, nama_main, dk, rekkredit rek, nama, sum(if(dk = 'D', kredit * -1, kredit)) total FROM lap_bb LEFT JOIN acc ON acc.kode = lap_bb.rekkredit WHERE tgl LIKE '{$yr}%' $condition GROUP BY rekkredit, bln UNION ALL SELECT lpad(month(tgl), 2, 0) bln, main, nama_main, dk, rekdebit rek, nama, sum(if(dk <> 'D', debit * -1, debit)) total FROM lap_bb LEFT JOIN acc ON acc.kode = lap_bb.rekdebit WHERE tgl LIKE '{$yr}%' $condition GROUP BY rekdebit, bln ) a GROUP BY rek, bln HAVING main IS NOT NULL ORDER BY bln ASC, main ASC, rek ASC ) ) a GROUP BY rek[;] SELECT * FROM setbeban[;] SELECT * FROM akn[;] SELECT * FROM tahun_awal[;] SELECT * FROM v_beban_acc[;] SELECT * FROM v_beban_usp[;] SELECT * FROM ( SELECT 'usp' app, 2022 tahun, ifnull((SELECT data FROM `profile` WHERE kode = 'shu usp'), 0) total UNION ALL SELECT 'acc' app, 2022 tahun, ifnull((SELECT data FROM `profile` WHERE kode = 'shu induk'), 0) total ) a "; $queries[] = explode("[;]",$query); } $getData = []; foreach ($queries as $key => $q) { foreach ($q as $key => $y) { $getData[] = DB::query_result_assoc($y); } } return $getData; } class OpenData { // Properties private $data = []; private $acc = []; // Constructor public function __construct($data=[], $acc=[]) { $this->data = $data; $this->acc = $acc; } // Method public function lastBBUpdate() { $dat = $this->data; // Assuming $this->acc is an array or an object with a "cond" method $data = $dat['data']; // Assuming $this->acc is an array or an object with a "cond" method $acc = $this->acc; // Assuming $this->acc is an array or an object with a "cond" method $year = $dat['thn']; // Assuming $this->acc is an array or an object with a "cond" method $newData = array_map(function($w) use ($acc, $year) { list($dataR) = Help::cond($acc, $w['rek'], 'kode'); return [ 'kode' => $w['rek'], 'main' => $w['main'], 'nama' => $w['nama'], 'nama_main' => $w['nama_main'], 'tahun' => $year, 'total' => (float) $w['b13'], 'dk' => $dataR ? $dataR['dk'] : null, 'nr' => $dataR ? $dataR['nr'] : null, 'app' => $dataR ? $dataR['app'] : null ]; }, $data); return $newData; } } function accNillRes($res, $year) { list($data, $beban, $acc, $thn, $bacc, $busp, $shuawal) = $res; $dat = [ 'tahun' => $thn[0]['tahun'], 'thn' => intval($year), 'bacc' => $bacc, 'busp' => $busp, 'shuawal' => $shuawal, 'dates' => tanggal($thn[0]['dates'] . '-01')['milisecond'], 'data' => array_map(function ($d) use ($acc, $year, $thn) { $b13 = 0; [$nr] = Help::cond($acc, $d['rek'], 'kode'); $d['nr'] = $nr ? $nr['nr'] : '-'; $d['dk'] = $nr ? $nr['dk'] : '-'; $d['amain'] = $nr ? $nr['amain'] : '-'; $d['asub'] = $nr ? $nr['asub'] : '-'; $d['ba0'] = intval($d['ba0']); for ($x = 0; $x <= 12; $x++) { $pad = 'b' . str_pad($x, 2, '0', STR_PAD_LEFT); if ($x == 0) { if (intval($year) > intval($thn[0]['tahun'])) { $b13 += round($d[$pad]); } } else { $b13 += round($d[$pad]); } $d[$pad] = round($d[$pad]); } $d['b13'] = $b13; return $d; }, $data) ]; $funcOpen = new OpenData($dat, $GLOBALS["acc"]); return $funcOpen; } ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); function array_sum_custom($array, $key = null) { // Create a copy of the array to avoid modifying the original $copy = $array; // If a key is provided, map the array to get the values of that key if ($key !== null) { $copy = array_map(function ($item) use ($key) { return isset($item[$key]) ? $item[$key] : 0; }, $copy); } // Use the array_sum function to calculate the sum return array_sum($copy); } function loadDataR($a = null, $s = [], $appType = "", $year = "") { if($a){ $update = $a->lastBBUpdate(); $labarugi = array_sum_custom( array_map(function ($w) { $h = $w; if ($w['dk'] == 'D') { $h['total'] = $w['total'] * -1; } return $h; }, Help::cond($update,'L', 'nr')) , 'total'); [$hs] = Help::cond($s, $appType, 'app'); $shubl = [ "kode" => $hs['kode'], "main" => $hs['main'], "nama" => $hs['nama'], "nama_main" => $hs['nama_main'], "tahun" => $year, "total" => $labarugi, "dk" => $hs['dk'], "nr" => $hs['nr'], "app" => $hs['app'] ]; $update = array_filter($update, function ($h) use ($hs) { return $h['kode'] != $hs['kode']; }); $update[] = $shubl; echo(json_encode([ "hs" => $hs, "data" => $update ])); // DB::query( array_to_insert($update, 'bbtahunan',['kode', 'tahun']) ); // DB::query( array_to_update($update, 'bbtahunan',['kode', 'tahun']) ); } } $GLOBALS['s'] = DB::query_result_assoc("SELECT a.kode, b.main, b.nama, b.nama_main, b.dk, b.nr, b.app FROM ( SELECT if(substring_index(kode, \"bl\", -1) = 'usp', 'usp', 'acc') app , data kode FROM `profile` WHERE kode LIKE 'shubl%' ) a LEFT JOIN acc b ON a.kode = b.kode"); function abc($apptype = 'usp'){ $yr = [date('Y')]; if(isset($_GET['thn'])){ $yr = [$_GET['thn']]; } $get = _slice7(accnil($yr, $apptype)); foreach ($get as $key => $getRes) { $w = accNillRes($getRes, $yr[$key]); loadDataR($w, $GLOBALS['s'], $apptype, $yr[$key]); } } abc('acc'); // abc('usp');