Advertisement
sufehmi

Automatic People-Area-Time Allocator (APATA)

Nov 14th, 2018
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.43 KB | None | 0 0
  1. <?php
  2.  
  3. $sdm    = ['Budi','Dora','Dewi','Indro','Dono'];
  4.  
  5. $day        = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
  6. $hari       = ['Senin','Selasa','Rabu','Kamis','Jumat','Sabtu','Ahad'];
  7. $month_list     = ['0','January','February','March','April','May','June','July','August','September','October','November','December'];
  8.  
  9. $slot_jadwal    = ['Pagi','Siang','Sore'];
  10.  
  11. $no_go      = ['Rabu Sore','Kamis Sore'];
  12. $no_schedule    = [
  13.         'Budi'  => ['Senin','Rabu Siang','Jumat Siang'],
  14.         'Dora'  => ['Rabu Sore', 'Kamis Sore'],
  15.         'Dewi'  => ['Rabu Sore','Kamis Pagi','Ahad'],
  16.         'Indro' => ['Selasa Sore','Rabu Sore','Kamis Sore','Sabtu'],
  17.         'Dono'  => ['Senin','Selasa','Rabu Sore','Kamis Sore','Jumat'],
  18.         ];
  19. $no_area    = [
  20.         'Dewi'      => ['Jakarta Barat','noll']
  21.         ];
  22.  
  23. $area       = ['Jakarta Barat','Pondok Aren','Ciputat','Pondok Labu','Kebayoran Lama'];
  24. $area_detals    = ['Jakarta Barat'  => ['Grogol','Tanah Abang','Thamrin City'],
  25.         'Pondok Aren'       => ['Ceger','Japos','Pondok Kacang'],
  26.         'Ciputat'       => ['UIN','Kasino 1','Kasino 2','ZXC'],
  27.         'Pondok Labu'       => ['ATK 1','ATK 2','Tex 1','Tex 2','MMM','Gandul 1','Gandul 2'],
  28.         'Kebayoran Lama'    => ['ATK 1','ATK 2','ATK 3','Tex 1','Tex 2','Tex 3','Sepatu'],
  29.         ];
  30.  
  31. $tahun = 2018;
  32. $bulan = 1;
  33.  
  34. //===================
  35.  
  36. ApplyHeader($tahun);
  37.  
  38. $booked = [];
  39. $twice  = [];
  40. $kemarin= [];
  41. $ctrKemarin = 0;
  42.  
  43. while ($bulan < 13) {
  44.  
  45.   echo "<h1>Bulan : ".$month_list[$bulan]."</h1>\n\n";
  46.   $tanggal = 1;
  47.  
  48.  
  49.   // bikin header table bulan
  50.   echo "
  51.  <table class='greenTable'>
  52.  <thead>
  53.     <tr>
  54.         <th>Senin</th>
  55.         <th>Selasa</th>
  56.         <th>Rabu</th>
  57.         <th>Kamis</th>
  58.         <th>Jumat</th>
  59.         <th>Sabtu</th>
  60.         <th>Ahad</th>
  61.     </tr>
  62.  </thead>
  63.  <tbody>
  64.     <tr>";
  65.  
  66.   $hari_ini = getDay($tanggal,$bulan,$tahun);
  67.  
  68.   // jika tanggal 1, bikin kolom-kolom kosong s/d ke kolom hari yang benar
  69.   // hitung jumlah kolom yang perlu dikosongkan
  70.   $skip = array_search($hari_ini, $hari);
  71.   // kosongkan kolom
  72.   for ($i = 1; $i <= $skip; $i++) {
  73.     echo "<td></td>";
  74.   };
  75.  
  76.   // pilih salah satu hari Sabtu
  77.   $xSaturday    = getRandomSaturday($bulan, $tahun);
  78.   // pilih salah satu hari Ahad
  79.   $xSunday  = getRandomSunday($bulan, $tahun);
  80.  
  81.     // mulai buat daftar keliling bulan ini
  82.     while (checkdate ($bulan, $tanggal, $tahun)) {
  83.  
  84.         $hari_ini = getDay($tanggal,$bulan,$tahun);
  85.  
  86.         // jika hari Senin, bikin row baru
  87.         if ($hari_ini == 'Senin') {
  88.             echo "</tr><tr>";
  89.         };
  90.          
  91.         // kalau sabtu / minggu = skip
  92.         if ( (($hari_ini == 'Sabtu') || ($hari_ini == 'Ahad')) && ($tanggal <> $xSunday) && ($tanggal <> $xSaturday) ) {
  93.  
  94.             // do nothing  
  95.             echo "<td>$tanggal</td>";
  96.  
  97.         } else {
  98.  
  99.         // bikin kolom hari
  100.         echo "<td><h3>".$hari_ini." : ".$tanggal."</h3><br />\n\n";
  101.  
  102.         foreach ($area as &$daerah) {
  103.    
  104.  
  105.             // dapatkan waktu
  106.             do {
  107.                 $x  = getRandomNumber(0, sizeof($slot_jadwal)-1);
  108.                 $slot   = $slot_jadwal[$x];
  109.  
  110.                 $slot_hari  = $hari_ini." ".$slot;
  111.             } while ( in_array($slot_hari, $no_go) );
  112.  
  113.             // dapatkan personil
  114.             $ctr        = 1;
  115.             $dapat_sdm  = false;
  116.             do {
  117.                 $nomor  = getRandomNumber(0, (sizeof($sdm)-1));
  118.                 $nama   = $sdm[$nomor];
  119.  
  120.                
  121.                 $ctr++;
  122.                 // jika $ctr > 10000, berarti ada infinite loop
  123.                 // pecahkan dengan cara sbb :
  124.                 if ($ctr > 1000) {
  125.  
  126.                     $nama = '';
  127.                     // temukan orang yang belum ada di $booked[]
  128.                     foreach ($sdm as &$n) { //echo "nama1 ".$n."/".$slot_hari;
  129.                         if (!(in_array($n, $booked)) && !(AdaKonflikJadwal($n, $slot_hari, $no_schedule)) && !(in_array($daerah, $no_area["$n"])) && !(SudahKemarin($n,$daerah,$kemarin)) ) {
  130.                             $nama = $n;
  131.                         };
  132.                     };
  133.  
  134.                     if ($nama <> '') {
  135.  
  136.                         $booked[] = $nama; if ($nama == 'Devy') { $twice[] = $nama;};
  137.  
  138.                     // jika tidak ada, maka ambil dari $booked[]
  139.                     } elseif ($nama == '') {
  140.                         do {
  141.                             $nomor      = getRandomNumber(0, (sizeof($booked)-1));
  142.                             $nama       = $booked[$nomor];
  143.                         } while ( (in_array($nama, $twice)) && SudahKemarin($nama,$daerah,$kemarin) );
  144.                         $twice[]    = $nama;
  145.                     }; // if ($nama == '')
  146.  
  147.                     $dapat_sdm = true;
  148.  
  149.                 }; // if ($ctr > 10000)
  150.  
  151.                 if ($dapat_sdm) { break;};
  152.  
  153.             } while ( (AdaKonflikJadwal($nama, $slot_hari, $no_schedule)) || (in_array($nama, $booked)) || (in_array($daerah, $no_area["$nama"])) || (in_array($nama, $twice)) || SudahKemarin($nama,$daerah,$kemarin) );
  154.        
  155.             $booked[] = $nama; if ($nama == 'Devy') { $twice[] = $nama;};
  156.             $kemarin[$ctrKemarin] = [$nama,$daerah];
  157.             $ctrKemarin++;
  158.  
  159.             // cetak nama & jadwal
  160.             //echo $daerah." : ".$nama." - ".$slot."\n\n";
  161.             echo $daerah." : ".$nama."<br />\n\n";
  162.  
  163.         }; // foreach ($area as &$daerah)
  164.  
  165.             // tutup kolom hari
  166.             echo "</td>";
  167.         }; // if (($hari == 'Sabtu') || $hari == 'Ahad')
  168.  
  169.  
  170.         $booked = [];
  171.         $twice  = [];
  172.         $tanggal++;
  173.        
  174.         if ( $ctrKemarin > (sizeof($sdm)+1) ) { $ctrKemarin = 0;};
  175.  
  176.     }; // while (checkdate ($bulan, $tanggal, $tahun)) {
  177.    
  178.     $bulan++;
  179.    
  180.     // bikin footer table bulan
  181.     echo "</tr></tbody></table>";
  182.  
  183. }; // while (bulan < 13)
  184.  
  185.  
  186.  
  187.  
  188. exit(); // end
  189. //===================
  190.  
  191. function getRandomSunday($month, $year) {
  192.  
  193. $month_list     = ['0','January','February','March','April','May','June','July','August','September','October','November','December'];
  194. $sunday_list    = [];
  195. $today      = 1;
  196.  
  197.     // get a list of Sundays
  198.     while (checkdate($month, $today, $year)) {
  199.  
  200.         $curdate = strtotime($today.' '.$month_list[$month].' '.$year);
  201.         if (date('l', $curdate) == 'Sunday') {
  202.             $sunday_list[] = $today.'-'.$month_list[$month].'-'.$year;
  203.         };
  204.  
  205.         $today++;
  206.     };
  207.  
  208.     // pick a random Sunday
  209.     $max    = sizeof($sunday_list) - 1;
  210.     $result = getRandomNumber(0, $max);
  211.  
  212.     return $sunday_list[$result];  
  213. };
  214.  
  215.  
  216. function getRandomSaturday($month, $year) {
  217.  
  218. $month_list     = ['0','January','February','March','April','May','June','July','August','September','October','November','December'];
  219. $sunday_list    = [];
  220. $today      = 1;
  221.  
  222.     // get a list of Saturday
  223.     while (checkdate($month, $today, $year)) {
  224.  
  225.         $curdate = strtotime($today.' '.$month_list[$month].' '.$year);
  226.         if (date('l', $curdate) == 'Saturday') {
  227.             $sunday_list[] = $today.'-'.$month_list[$month].'-'.$year;
  228.         };
  229.  
  230.         $today++;
  231.     };
  232.  
  233.     // pick a random Sunday
  234.     $max    = sizeof($sunday_list) - 1;
  235.     $result = getRandomNumber(0, $max);
  236.  
  237.     return $sunday_list[$result];  
  238. };
  239.  
  240.  
  241. function getRandomNumber ($min, $max) {
  242.  
  243.     // better random seed
  244.     $x  = openssl_random_pseudo_bytes(100);
  245.     $x  = hash('crc32', $x);
  246.     mt_srand(intval(hexdec($x)));
  247.    
  248.     $result = mt_rand($min,$max);
  249.     //$result = random_int($min,$max);
  250.  
  251.     //$src = "https://www.random.org/integers/?num=1&min=".$min."&max=".$max."&col=1&base=10&format=plain&rnd=new";     $dh = fopen("$src",'r'); $result = fread($dh,1);
  252.  
  253.     return $result;
  254.  
  255. }
  256.    
  257.  
  258. function getDay($tanggal, $bulan, $tahun) {
  259.  
  260.     $month_list     = ['0','January','February','March','April','May','June','July','August','September','October','November','December'];
  261.     $day        = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
  262.     $hari       = ['Senin','Selasa','Rabu','Kamis','Jumat','Sabtu','Ahad'];
  263.  
  264.  
  265.     // dapatkan hari
  266.     $hari_ini = date('l', strtotime($tanggal.' '.$month_list[$bulan].' '.$tahun));
  267.  
  268.     // ubah jadi bahasa Indonesia
  269.     $pos        = array_search($hari_ini, $day);
  270.     $hari_ini   = $hari[$pos];
  271.  
  272.     return $hari_ini;
  273. }
  274.  
  275.  
  276. function AdaKonflikJadwal ($nama, $slot_hari, $no_schedule) {
  277.  
  278.     $oke = false;
  279.     foreach ($no_schedule[$nama] as &$jadwal) {
  280.         if (strpos($slot_hari, $jadwal) !== false) {
  281.             $oke = true;
  282.             //echo "NIH : ".$slot_hari."/".$jadwal;
  283.         };
  284.     };
  285.  
  286.     return $oke;
  287. }
  288.  
  289.  
  290. function SudahKemarin($nama,$daerah,$kemarin) {
  291.  
  292.     $sudah = false;
  293.     foreach ($kemarin as &$v) {
  294.         if ( ($v[0] == $nama) && ($v[1] == $daerah) ) {
  295.             $sudah = true;
  296.         };
  297.     };
  298.     return $sudah;
  299. }
  300.  
  301.  
  302.  
  303. function ApplyHeader($tahun) {
  304.  
  305. ?>
  306. <html>
  307. <body>
  308. <style>
  309. table.greenTable {
  310.   font-family: Arial, Helvetica, sans-serif;
  311.   border: 6px solid #24943A;
  312.   background-color: #D4EED1;
  313.   width: 100%;
  314.   text-align: center;
  315. }
  316. table.greenTable td, table.greenTable th {
  317.   border: 1px solid #24943A;
  318.   padding: 3px 2px;
  319. }
  320. table.greenTable tbody td {
  321.   font-size: 13px;
  322. }
  323. table.greenTable thead {
  324.   background: #24943A;
  325.   background: -moz-linear-gradient(top, #5baf6b 0%, #3a9e4d 66%, #24943A 100%);
  326.   background: -webkit-linear-gradient(top, #5baf6b 0%, #3a9e4d 66%, #24943A 100%);
  327.   background: linear-gradient(to bottom, #5baf6b 0%, #3a9e4d 66%, #24943A 100%);
  328.   border-bottom: 0px solid #444444;
  329. }
  330. table.greenTable thead th {
  331.   font-size: 19px;
  332.   font-weight: bold;
  333.   color: #F0F0F0;
  334.   text-align: center;
  335.   border-left: 2px solid #24943A;
  336. }
  337. table.greenTable thead th:first-child {
  338.   border-left: none;
  339. }
  340.  
  341. table.greenTable tfoot {
  342.   font-size: 13px;
  343.   font-weight: bold;
  344.   color: #F0F0F0;
  345.   background: #24943A;
  346.   background: -moz-linear-gradient(top, #5baf6b 0%, #3a9e4d 66%, #24943A 100%);
  347.   background: -webkit-linear-gradient(top, #5baf6b 0%, #3a9e4d 66%, #24943A 100%);
  348.   background: linear-gradient(to bottom, #5baf6b 0%, #3a9e4d 66%, #24943A 100%);
  349.   border-top: 1px solid #24943A;
  350. }
  351. table.greenTable tfoot td {
  352.   font-size: 13px;
  353. }
  354. table.greenTable tfoot .links {
  355.   text-align: right;
  356. }
  357. table.greenTable tfoot .links a{
  358.   display: inline-block;
  359.   background: #FFFFFF;
  360.   color: #24943A;
  361.   padding: 2px 8px;
  362.   border-radius: 5px;
  363. }
  364. </style>
  365.  
  366. <h1><center>Jadwal Keliling tahun <?php echo $tahun; ?></center></h1>
  367.  
  368. <?php
  369.  
  370. }
  371.  
  372.  
  373. function ApplyFooter() {
  374.  
  375. ?>
  376.  
  377. </table>
  378. </body>
  379. </html>
  380.  
  381. <?php
  382.  
  383.  
  384. }
  385.  
  386.  
  387.  
  388. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement