<?php

function konek_db($db){
	$host = "localhost";
	$database = $db;
	$user = "berkahgl_root";
	$pass = "berkahgl_Sha1&Md5";
	$con = mysqli_connect($host, $user, $pass, $database);

	return $con;
}

function getResumeProduksi($tanggal){
	date_default_timezone_set('Asia/Jakarta');  
	$text = '';

	$con = konek_db('berkahgl_man');	

	$bln = substr($tanggal,5,2);
	$thn = substr($tanggal,0,4);
    $blnthn = substr($tanggal,0,7);

    $baris = 0;
	$status = '';
  	$alljmlpop=0;
    $unit = mysqli_query($con, "SELECT * FROM berkahgl_man.dataunit where sktunit != 'bas' and sktunit != 'bus' and sktunit != 'bsb'  order by nmunit ASC");
    while ($dunit = mysqli_fetch_array($unit)) {
        $baris++;
        $nmunit = $dunit['dbunit'];
        $con_unit = konek_db($nmunit);

        if ($stsfarm == "INF") {
            $qrhpp = mysqli_query($con_unit, "select $nmunit.rhpp.id_produksi, $nmunit.rhpp.avr_umur, sum($nmunit.rhpp.pakan_total) as pakan, sum($nmunit.rhpp.mati) as mati,sum($nmunit.rhpp.panen_ekor) as chick_out,sum($nmunit.rhpp.panen_tonase) as chick_out_ton,sum($nmunit.rhpp.thp_company) as lrcompany,sum($nmunit.rhpp.thp_ternak) as thp_ternak, $nmunit.rhpp.ip, $nmunit.farm.id_farm, $nmunit.farm.nama  from $nmunit.rhpp join $nmunit.farm on $nmunit.rhpp.id_farm=$nmunit.farm.id_farm where $nmunit.farm.nama like '%INF%' and panenbulan like '%$blnthn%'  order by $nmunit.rhpp.id_produksi");
        } else if ($stsfarm == "KMT") {
            $qrhpp = mysqli_query($con_unit, "select $nmunit.rhpp.id_produksi, $nmunit.rhpp.avr_umur, sum($nmunit.rhpp.pakan_total) as pakan, sum($nmunit.rhpp.mati) as mati,sum($nmunit.rhpp.panen_ekor) as chick_out,sum($nmunit.rhpp.panen_tonase) as chick_out_ton,sum($nmunit.rhpp.thp_company) as lrcompany,sum($nmunit.rhpp.thp_ternak) as thp_ternak, $nmunit.rhpp.ip, $nmunit.produksi.id_produksi, $nmunit.produksi.id_kontrak from $nmunit.rhpp JOIN $nmunit.produksi ON $nmunit.rhpp.id_produksi=$nmunit.produksi.id_produksi where panenbulan like '%$blnthn%' and $nmunit.produksi.id_kontrak like '%KMT%' order by $nmunit.rhpp.id_produksi");
        } else if ($stsfarm == "IJR") {
            $qrhpp = mysqli_query($con_unit, "select $nmunit.rhpp.id_produksi, $nmunit.rhpp.avr_umur, sum($nmunit.rhpp.pakan_total) as pakan, sum($nmunit.rhpp.mati) as mati,sum($nmunit.rhpp.panen_ekor) as chick_out,sum($nmunit.rhpp.panen_tonase) as chick_out_ton,sum($nmunit.rhpp.thp_company) as lrcompany,sum($nmunit.rhpp.thp_ternak) as thp_ternak, $nmunit.rhpp.ip, $nmunit.produksi.id_produksi, $nmunit.produksi.id_kontrak from $nmunit.rhpp JOIN $nmunit.produksi ON $nmunit.rhpp.id_produksi=$nmunit.produksi.id_produksi where panenbulan like '%$blnthn%' and $nmunit.produksi.id_kontrak not like '%KMT%' order by $nmunit.rhpp.id_produksi");
        } else {
            $qrhpp = mysqli_query($con_unit, "select avr_umur,sum(pakan_total) as pakan, sum(mati) as mati,sum(panen_ekor) as chick_out,sum(panen_tonase) as chick_out_ton,sum(thp_company) as lrcompany,sum(thp_ternak) as thp_ternak,ip from $nmunit.rhpp where panenbulan like '%$blnthn%' order by id_produksi");
        }

/*
        $qrhpp = mysqli_query("select avr_umur,sum(pakan_total) as pakan,sum(mati) as mati,sum(panen_ekor) as chick_out,sum(panen_tonase) as chick_out_ton,,sum(thp_company) as lrcompany,sum(thp_ternak) as thp_ternak,ip from $nmunit.rhpp where panenbulan like '%$blnthn%' order by id_produksi");
        $drhpp = mysqli_fetch_array($qrhpp);
*/

        $drhpp = mysqli_fetch_array($qrhpp);

        $totip = 0;

        if($stsfarm=="INF"){ 
        $qPro=mysqli_query($con_unit, "select $nmunit.rhpp.id_produksi, $nmunit.rhpp.ip, $nmunit.rhpp.id_farm, $nmunit.farm.id_farm, $nmunit.farm.nama  from $nmunit.rhpp join $nmunit.farm on $nmunit.rhpp.id_farm=$nmunit.farm.id_farm where $nmunit.farm.nama like '%INF%' and panenbulan like '%$blnthn%'  order by $nmunit.rhpp.id_produksi");
        }else if($stsfarm=="KMT"){
        $qPro=mysqli_query($con_unit, "select $nmunit.rhpp.id_produksi, $nmunit.rhpp.ip, $nmunit.produksi.id_produksi, $nmunit.produksi.id_kontrak  from $nmunit.rhpp join $nmunit.produksi on $nmunit.rhpp.id_produksi=$nmunit.produksi.id_produksi where panenbulan like '%$blnthn%' and id_kontrak like '%KMT%'  order by $nmunit.rhpp.id_produksi");
        }else if($stsfarm=="IJR"){
        $qPro=mysqli_query($con_unit, "select $nmunit.rhpp.id_produksi, $nmunit.rhpp.ip, $nmunit.produksi.id_produksi, $nmunit.produksi.id_kontrak  from $nmunit.rhpp join $nmunit.produksi on $nmunit.rhpp.id_produksi=$nmunit.produksi.id_produksi where panenbulan like '%$blnthn%' and id_kontrak not like '%KMT%'  order by $nmunit.rhpp.id_produksi");
        }else { 
        $qPro=mysqli_query($con_unit, "select id_produksi, ip from $nmunit.rhpp where panenbulan like '%$blnthn%' order by id_produksi");
        }

        $subTotalQtyPop=0;
        $subTotalQtyBeban=0;
        $jPro=0;
        while($dPro=mysqli_fetch_array($qPro)){
            $jPro=mysqli_num_rows($qPro);
        //populasi
        $qpop=mysqli_query($con_unit, "SELECT sum(qty) as qty from $nmunit.jual where id_produksi='$dPro[id_produksi]' and id_barang like '%D-%'");
        $dpop=mysqli_fetch_array($qpop);
        $subTotalQtyPop=$subTotalQtyPop+$dpop[qty];

        

        //beban
        $qbeban=mysqli_query($con_unit, "SELECT sum(qty) as qty from $nmunit.beban where id_produksi='$dPro[id_produksi]'");
        $dbeban=mysqli_fetch_array($qbeban);
        $subTotalQtyBeban=$subTotalQtyBeban+$dbeban[qty];


        $ip = ($dpop[qty] - $dbeban[qty]) *  $dPro[ip];
        $totip = $totip + $ip;
        }
        //$alljmlpop = $alljmlpop + $alljmlpop;
        $alljmlpop=$subTotalQtyPop-$subTotalQtyBeban;
        $alljmlpakan = $alljmlpakan + $drhpp['pakan'];
        $alljmlmati = $alljmlmati + $drhpp['mati'];
        $allchickout = $allchickout + $drhpp['chick_out'];
        $allchickoutton = $allchickoutton + $drhpp['chick_out_ton'];
        $kgcickout = $drhpp['chick_out_ton'];
        $ekorcickout = $drhpp['chick_out'];
//laba rugi company
        $lrcompany = $drhpp['lrcompany'];
//laba rugi farm
        $lrfarm = $drhpp['thp_ternak'];
//umur
        $umuravg = $drhpp['avr_umur'];
//    unit
        $jmlpopunit = $alljmlpop . "-" . $dunit[dbunit];
//laba farm
        if($stsfarm=="INF"){ 
        $qPro=mysqli_query($con_unit, "select $nmunit.rhpp.*, $nmunit.farm.id_farm, $nmunit.farm.nama  from $nmunit.rhpp join $nmunit.farm on $nmunit.rhpp.id_farm=$nmunit.farm.id_farm where $nmunit.farm.nama like '%INF%' and panenbulan like '%$blnthn%'  order by $nmunit.rhpp.id_produksi");
        }else if($stsfarm=="KMT"){
        $qPro=mysqli_query($con_unit, "select $nmunit.rhpp.*, $nmunit.produksi.id_produksi, $nmunit.produksi.id_kontrak  from $nmunit.rhpp join $nmunit.produksi on $nmunit.rhpp.id_produksi=$nmunit.produksi.id_produksi where panenbulan like '%$blnthn%' and id_kontrak like '%KMT%'  order by $nmunit.rhpp.id_produksi");
        }else if($stsfarm=="IJR"){
        $qPro=mysqli_query($con_unit, "select $nmunit.rhpp.*, $nmunit.produksi.id_produksi, $nmunit.produksi.id_kontrak  from $nmunit.rhpp join $nmunit.produksi on $nmunit.rhpp.id_produksi=$nmunit.produksi.id_produksi where panenbulan like '%$blnthn%' and id_kontrak not like '%KMT%'  order by $nmunit.rhpp.id_produksi");
        }else { 
        $qPro=mysqli_query($con_unit, "select $nmunit.rhpp.* from $nmunit.rhpp where panenbulan like '%$blnthn%' order by id_produksi");
        }
//        $produksi = mysqli_query("SELECT * FROM " . $dunit['dbunit'] . ".rhpp where panenbulan like '%$blnthn%'");
        $totallabar = 0;
        $alllainlain = 0;
        $total_hlain = 0;
        $totpengurang = 0;
        $all_total_ternak = 0;
        $operasional = 0;
        $totalhargapasar = 0;
        while ($dproduksi = mysqli_fetch_array($qPro)) {
//        while ($dproduksi = mysqli_fetch_array($produksi)) {
            $lfarm = mysqli_query($con_unit, "SELECT *,p_ayam-(`sapronak`-`retur_sapronak`) as labafarm FROM $nmunit.rhpp WHERE id_produksi='$dproduksi[id_produksi]'");
            //$dlfarm = mysqli_fetch_array($lfarm);
            //$lfarm = mysqli_query("SELECT thp_ternak+`bonus_ip`+`bonus_fc`+`bonus_shp`+`bonus_mati`+npenambah_lain1+npenambah_lain2+npenambah_lain3+npenambah_lain4+npenambah_lain5 as labafarm FROM " . $dunit['dbunit'] . ".rhpp WHERE id_produksi='$dproduksi[id_produksi]'");
            // $lfarm = mysqli_query("SELECT sum(thp_ternak+`bonus_ip`+`bonus_fc`+`bonus_shp`+`bonus_mati`+bop_awal) as labafarm FROM " . $dunit['dbunit'] . ".rhpp where panenbulan like '%$blnthn%'");
            $dlfarm = mysqli_fetch_array($lfarm);
            $labafarm = $dlfarm['labafarm'];
            $bonus = $dlfarm['bonus_ip'] + $dlfarm['bonus_fc'] + $dlfarm['bonus_shp'] + $dlfarm['bonus_mati'];
            $kompensasi = $dlfarm['bop_akhir'] + $dlfarm['npenambah_lain1'] + $dlfarm['npenambah_lain2'] + $dlfarm['npenambah_lain3'] + $dlfarm['npenambah_lain4'] + $dlfarm['npenambah_lain5'];
            /* if($labafarm < 0){
              $hasillabafarm=$kompensasi+$bonus;
              }else{
              $hasillabafarm=$labafarm+$kompensasi+$bonus;
              }
              $nakhirlabafarm = $nakhirlabafarm + $hasillabafarm;
              } */
            if ($dlfarm['thp_ternak'] > 0) {
                $laba_produksi = $dlfarm['p_ayam'] + $dlfarm['retur_sapronak'] - $dlfarm['sapronak']; //echo"$drhpp[p_ayam]+$drhpp[retur_sapronak]-$drhpp[sapronak]";
                if ($laba_produksi > 0) {
                    $total_ternak = ($laba_produksi + $dlfarm['bop_akhir'] + $dlfarm['bonus_ip'] + $dlfarm['bonus_fc'] + $dlfarm['bonus_mati'] + $dlfarm['bonus_shp'] + $dlfarm['npenambah_lain1'] + $dlfarm['npenambah_lain2'] + $dlfarm['npenambah_lain3'] + $dlfarm['npenambah_lain4'] + $dlfarm['npenambah_lain5']);
                } else {
                    $total_ternak = ($dlfarm['bop_akhir'] + $dlfarm['bonus_ip'] + $dlfarm['bonus_fc'] + $dlfarm['bonus_mati'] + $dlfarm['bonus_shp'] + $dlfarm['npenambah_lain1'] + $dlfarm['npenambah_lain2'] + $dlfarm['npenambah_lain3'] + $dlfarm['npenambah_lain4'] + $dlfarm['npenambah_lain5']);
                }
            } else {
                $laba_produksi = $dlfarm['p_ayam'] + $dlfarm['retur_sapronak'] - $dlfarm['sapronak']; //echo"$drhpp[p_ayam]+$drhpp[retur_sapronak]-$drhpp[sapronak]";
                $total_ternak = ($dlfarm['bop_akhir'] + $dlfarm['bonus_ip'] + $dlfarm['bonus_fc'] + $dlfarm['bonus_mati'] + $dlfarm['bonus_shp'] + $dlfarm['npenambah_lain1'] + $dlfarm['npenambah_lain2'] + $dlfarm['npenambah_lain3'] + $dlfarm['npenambah_lain4'] + $dlfarm['npenambah_lain5']);
            }
    //        $totallabar = $totallabar + $total_ternak;

            $subtot_lain=0;$sub_lain=0;
            $qhlain = mysqli_query($con_unit, "select * from $nmunit.jual where id_produksi='$dproduksi[id_produksi]' and (id_barang like '%O-%' or id_barang like '%E-%') order by tanggal");
            while($dhlain = mysqli_fetch_array($qhlain))
            {
                $hid_barang = $dhlain['id_barang'];
                $qhlain2=mysqli_query($con_unit, "select * from $nmunit.barang where id_barang='$hid_barang'");
                $dhlain2=mysqli_fetch_array($qhlain2);
                $sub_lain = $dhlain['qty']*$dhlain2['hrg_beli'];
                $subtot_lain = $subtot_lain+$sub_lain;
            }
                $subtot_retlain=0;$sub_retlain=0;
                $qhretlain = mysqli_query($con_unit, "select * from $nmunit.retur_jual where id_produksi='$dproduksi[id_produksi]' and (id_barang like '%O-%' or id_barang like '%E-%') order by tgl_rsap");
            while($dhretlain = mysqli_fetch_array($qhretlain))
            {
                $hretid_barang = $dhretlain['id_barang'];
                $qhretlain2=mysqli_query($con_unit, "select * from $nmunit.barang where id_barang='$hretid_barang'");
                $dhretlain2=mysqli_fetch_array($qhretlain2);
                $sub_retlain = $dhretlain['qty']*$dhretlain2['hrg_beli'];
                $subtot_retlain = $subtot_retlain+$sub_retlain;
            }
            $total_hlain=$subtot_lain-$subtot_retlain;
            $total_hrglain=$total_hrglain+$total_hlain;
            //lain per ekor
            $lain_per_ekor=substr(($total_hlain/$populasi_net),0,6); $lain_per_ekor2=number_format($lain_per_ekor, 0, '.', ',');

            $alllainlain = $alllainlain + $total_hlain;

            $populasix_notnet=0;
            $qp2 = mysqli_query($con_unit, "select * from $nmunit.jual where id_produksi='$dproduksi[id_produksi]' and id_barang like '%D-%' order by tanggal");
            while($dp2 = mysqli_fetch_array($qp2))
            {
            $tgl = $dp2['tanggal'];
        //    $tgl2=bln_indo($tgl);
            $populasi = $populasi+($dp2['qty']+$dp2['bonus']);
            $populasix_notnet = $populasix_notnet+($dp2['qty']);
            }


           $qprd = mysqli_query($con_unit, "select * from $nmunit.produksi where id_produksi='$dproduksi[id_produksi]'");
            $dprd = mysqli_fetch_array($qprd);
            $k = substr(($dprd['id_kontrak']),0,3);
            $k2 = substr(($dprd['id_kontrak']),3,3);

            if($dproduksi['thp_ternak'] > 0)
            {
                $thp_ternak = $dproduksi['thp_ternak'];
            }else
            {
                $thp_ternak = 0;
            }

            if($k == 'KMT')
            {
            $sistem='Kemitraan';
            if($dproduksi['thp_ternak'] > 0)
            {
            $laba_produksi = $dproduksi['p_ayam']+$dproduksi['retur_sapronak']-$dproduksi['sapronak'];//echo"$drhpp[p_ayam]+$drhpp[retur_sapronak]-$drhpp[sapronak]";
                if($laba_produksi > 0)
                {
                $totalternak = ($laba_produksi+$dproduksi['bop_akhir']+$dproduksi['bonus_ip']+$dproduksi['bonus_fc']+$dproduksi['bonus_mati']+$dproduksi['bonus_shp']+$dproduksi['npenambah_lain1']+$dproduksi['npenambah_lain2']+$dproduksi['npenambah_lain3']+$dproduksi['npenambah_lain4']+$dproduksi['npenambah_lain5']);
                }else
                {
                $totalternak = ($dproduksi['bop_akhir']+$dproduksi['bonus_ip']+$dproduksi['bonus_fc']+$dproduksi['bonus_mati']+$dproduksi['bonus_shp']+$dproduksi['npenambah_lain1']+$dproduksi['npenambah_lain2']+$dproduksi['npenambah_lain3']+$dproduksi['npenambah_lain4']+$dproduksi['npenambah_lain5']);
                }
            }else
            {
                $laba_produksi = $dproduksi['p_ayam']+$dproduksi['retur_sapronak']-$dproduksi['sapronak'];//echo"$drhpp[p_ayam]+$drhpp[retur_sapronak]-$drhpp[sapronak]";
                $totalternak = ($dproduksi['bop_akhir']+$dproduksi['bonus_ip']+$dproduksi['bonus_fc']+$dproduksi['bonus_mati']+$dproduksi['bonus_shp']+$dproduksi['npenambah_lain1']+$dproduksi['npenambah_lain2']+$dproduksi['npenambah_lain3']+$dproduksi['npenambah_lain4']+$dproduksi['npenambah_lain5']);
            }
            }else
                if((($k == 'MKL')and($k2 !='.KK')) or ($k == 'IJR'))
                {
                if(($k == 'MKL')and($k2 !='.KK')){
                $sistem='Makloon';
                $qp = mysqli_query($con_unit, "select * from $nmunit.produksi where id_produksi='$dproduksi[id_produksi]' ");
                $dp = mysqli_fetch_array($qp);
                $id_kontrak = $dp['id_kontrak'];

                $qkon = mysqli_query($con_unit, "select * from $nmunit.kontrak_mkl where id_kontrak='$id_kontrak' ");
                $dkon = mysqli_fetch_array($qkon);
                $biaya_mkl = $dkon['biaya_makloon'];
                $biaya_pemeliaharaan = $biaya_mkl*$populasix_notnet;
            }else{
                $sistem='Ijarah';
                $qp = mysqli_query($con_unit, "select * from $nmunit.produksi where id_produksi='$dproduksi[id_produksi]' ");
                $dp = mysqli_fetch_array($qp);
                $id_kontrak = $dp['id_kontrak'];

                $qkon = mysqli_query($con_unit, "select * from berkahgl_portal.kontrak_ijr where id_kontrak='$id_kontrak' ");
                $dkon = mysqli_fetch_array($qkon);
                $biaya_ijr = $dkon['biaya_ijarah'];
                $biaya_pemeliaharaan = $biaya_ijr*$populasix_notnet;
            }

            $selisih_bop_akhir_m = $dproduksi['bop_awal']-(($biaya_pemeliaharaan)+($dproduksi['bonus_ip']+$dproduksi['bonus_fc']+$dproduksi['bonus_mati']+$dproduksi['bonus_fc_bb']+$dproduksi['npenambah_lain1']+$dproduksi['npenambah_lain2']+$dproduksi['npenambah_lain3']+$dproduksi['npenambah_lain4']+$dproduksi['npenambah_lain5'])-($dproduksi['sangsi']));

            if($selisih_bop_akhir_m > 0)
            {
                $selisih_bop_m = $selisih_bop_akhir_m;
            }else
            {
                $selisih_bop_m = 0;
            }

            if($dproduksi['thp_ternak'] > 0)
            {
            $totalternak = ($biaya_pemeliaharaan)+($dproduksi['bonus_ip']+$dproduksi['bonus_fc']+$dproduksi['bonus_mati']+$dproduksi['bonus_fc_bb'])-($dproduksi['sangsi'])+($dproduksi['npenambah_lain1']+$dproduksi['npenambah_lain2']+$dproduksi['npenambah_lain3']+$dproduksi['npenambah_lain4']+$dproduksi['npenambah_lain5'])+$selisih_bop_m;
            }else
            {
            $totalternak = ($biaya_pemeliaharaan)+($dproduksi['bonus_ip']+$dproduksi['bonus_fc']+$dproduksi['bonus_mati']+$dproduksi['bonus_fc_bb'])-($dproduksi['sangsi'])+($dproduksi['npenambah_lain1']+$dproduksi['npenambah_lain2']+$dproduksi['npenambah_lain3']+$dproduksi['npenambah_lain4']+$dproduksi['npenambah_lain5'])+$selisih_bop_m;
            }

            }else
            if(($k == 'MKL')and($k2 =='.KK'))
            {
                $sistem='Company Farm';
                $totalternak = ($dproduksi['bop_awal']+$dproduksi['bop_tengah']+$dproduksi['npenambah_lain1']+$dproduksi['npenambah_lain2']+$dproduksi['npenambah_lain3']+$dproduksi['npenambah_lain4']+$dproduksi['npenambah_lain5']);
            }
            $all_total_ternak = $all_total_ternak + $totalternak;
            $totallabar = $totallabar + $totalternak;

        // cari pendapatan lain
            $pengurang = $dproduksi['npengurang_lain1']+$dproduksi['npengurang_lain2']+$dproduksi['npengurang_lain3']+$dproduksi['npengurang_lain4']+$dproduksi['jml_cicilan_prod'];
            $totpengurang = $totpengurang + $pengurang;
        //  cari harga pasar
            $Totpasar=0;
            $qpja = mysqli_query($con_unit, "select * from $nmunit.pja where id_produksi='$dproduksi[id_produksi]' order by tanggal") or die ("Ada kesalahan :".mysqli_error());
            while($dpja=mysqli_fetch_array($qpja))
            {
            $Tpasar = $dpja['hrg_real']*$dpja['tonase_real'];
            $Totpasar = $Totpasar+$Tpasar;
            }
            $totalhargapasar = $totalhargapasar + $Totpasar;

        }

            $operasional = $all_total_ternak - $totpengurang ;
            $totoperasional = $totoperasional + $operasional;

            $totlainlain = $totlainlain + $alllainlain;

            $text .= "🐔 *BGF ". strtoupper(substr($dunit['nmunit'], 21)) ."*"; 
            $text .= "\n";

            $text .= "FARM `". number_format($jPro) ."`"; 
            $text .= "\n";
            $tot_jPro += $jPro;

            $text .= "DOC `". number_format($alljmlpop, 0, "," , ".")."` Ekor";
            $text .= "\n";
            $totalAllPop=$totalAllPop+$alljmlpop;

            $hargadoc = cariharga($nmunit, $blnthn, "D-", "jual", $stsfarm);
        //    $text .= "". number_format($hargadoc);
            $allhargadoc = $allhargadoc + $hargadoc;

        // $text .= number_format($drhpp[pakan]); 

            $hargapakan = hargaPkanBulan($nmunit, $blnthn, $stsfarm);
        //    echo number_format($hargapakan);
           $allhargapakan = $allhargapakan + $hargapakan;

            $hargaovk = cariharga($dunit['dbunit'], $blnthn, "M-", "jual", $stsfarm);
            $hargaovkret = cariharga($dunit['dbunit'], $blnthn, "M-", "retur_jual", $stsfarm);
            $hargaovkfix = $hargaovk - $hargaovkret;
        //    echo number_format($hargaovkfix);
            $allhargaovk = $allhargaovk + $hargaovkfix;

           $hargaekorovk = $hargaovkfix / $alljmlpop;
            $text .= "OVK `". number_format($hargaekorovk, 2, "," , ".") ."` /Ekor";
            $text .= "\n";

        //    echo number_format($alllainlain);
            $text .= "MORTALITAS `". number_format($drhpp[mati], 0, "," , ".") ."` Ekor | `"; 
            $persenmati = $drhpp['mati'] / $alljmlpop * 100;
            $text .= number_format($persenmati, 2, "," , ".") ."`%";
            $text .= "\n";

            $text .= "CHICK OUT `". number_format($drhpp['chick_out'], 0, "," , ".") ."` Ekor | `"; 
            $text .= number_format($drhpp['chick_out_ton'], 0, "," , ".") ."` Kg | Rp. `";
            $text .= number_format($totalhargapasar, 0, "," , ".")."`";
            $text .= "\n";
            //total harag
            $ttljumlahhargapasar = $ttljumlahhargapasar + $totalhargapasar;
//    avr_bw
            $avrbw = $kgcickout / $ekorcickout;
            $text .= "AVR BW `". number_format($avrbw, 2, "," , ".")."`";
            $text .= "\n";
//    fi
            $fi = $drhpp['pakan'] / $ekorcickout;
            $text .= "FI `". number_format($fi, 2, "," , ".")."`";
            $text .= "\n";
//    ep
            $ep = ($kgcickout/$drhpp['pakan'])*100;
            $text .= "EP `". number_format($ep, 2, "," , ".") ."`%";
            $text .= "\n";
//    fc
            $fc = $drhpp['pakan'] / $kgcickout;
            $text .= "FC `". number_format($fc, 2, "," , ".")."`";
            $text .= "\n";
//    cari IP
        //    $ipcari = $drhpp['kaliIp']/$alljmlpop;
            $ipcari = $totip / $alljmlpop;
            $text .= "IP `". number_format($ipcari, 2, "," , ".")."`";
            $text .= "\n";
//    mecari rataan ip
            //$jumlahsemuaip = $jumalhsemuaip+$alljmlpop;
            $jumlahsemuaip = $jumlahsemuaip + ($ipcari * $alljmlpop);
//    input tabel ip populasi bulanan
            $cekdata = mysqli_query($con, "SELECT bln FROM berkahgl_man.res_unit_ip_pop where bln='".$bln."' and thn='".$thn."' and unitnm='$dunit[dbunit]'");
            $ncekdata = mysqli_fetch_array($cekdata);

//    cariumur
            $text .= "UMUR `". number_format($umuravg, 2, "," , ".")."`";
            $text .= "\n";
//jumlah umur
            $jumlahumur = $jumlahumur + ($umuravg * $alljmlpop);


            $bepdoc = $hargadoc / $drhpp['chick_out_ton'];
        //    echo number_format($bepdoc);

            $bepfeed = $hargapakan / $drhpp['chick_out_ton'];
        //    echo number_format($bepfeed);

            $bepovk = $hargaovkfix / $drhpp['chick_out_ton'];
        //    echo number_format($bepovk);

            $beplain = $alllainlain / $drhpp['chick_out_ton'];
        //    echo number_format($beplain);

            $ops = $totallabar / $drhpp['chick_out_ton'];
        //    echo number_format($ops);

            $beptotal = $bepdoc + $bepfeed + $bepovk + $beplain + $ops;
            $text .= "HPP `". number_format($beptotal, 0, "," , ".")."`";
            $text .= "\n";

        	$hargapasar = $totalhargapasar / $drhpp['chick_out_ton'];
            $text .= "HARGA PASAR `". number_format($hargapasar, 0, "," , ".")."`";
            $text .= "\n";

			$text .= "FARM `". number_format($operasional, 0, "," , ".") ."` | `";

            $sumakhirlaba = $sumakhirlaba + $totallabar;

            $ekoranfarm = ($operasional) / $alljmlpop;
            $text .= number_format($ekoranfarm, 0, "," , ".") ."`/Ekor";
            $text .= "\n";

            $tonasefarm = ($operasional) / $drhpp['chick_out_ton'];
        //    echo number_format($tonasefarm);

           	$text .= "COMPANY `". number_format($lrcompany, 0, "," , ".") ."`  | `";
            $alllrcompany = $alllrcompany + $lrcompany;

            $ekorancompany = $lrcompany / $alljmlpop;
            $text .= number_format($ekorancompany, 0, "," , "."). "`/Ekor";

            $tonasecompany = $lrcompany / $drhpp['chick_out_ton'];
        //    $text .= "COMPANY Perekor ". number_format($tonasecompany);
        	$text .= "\n\n";
    } 

    	$text .= "🐔 *TOTAL*";
    	$text .= "\n";
        $text .= "FARM `". number_format($tot_jPro) ."`";
        $text .= "\n";
        $text .= "DOC `". number_format($totalAllPop, 0, "," , ".") ."` Ekor"; 
        $text .= "\n";
        //echo number_format($allhargadoc); 
        //echo number_format($alljmlpakan); 
        //echo number_format($allhargapakan);
        //echo number_format($allhargaovk);

        $ovkekor = $allhargaovk / $totalAllPop;
        $text .= "OVK `". number_format($ovkekor, 2, "," , ".") ."` /Ekor";
        $text .= "\n";
        //echo number_format($totlainlain);
        $allpersenmati = $alljmlmati / $totalAllPop * 100;
        $text .= "MORTALITAS `". number_format($alljmlmati, 0, "," , ".") ."` Ekor | `". number_format($allpersenmati, 2) ."`%"; 
        $text .= "\n";
        //echo number_format($allpersenmati, 2);
        $text .= "CHICK OUT `". number_format($allchickout, 0, "," , ".") ."` Ekor | `". number_format($allchickoutton, 0, "," , ".") ."` Kg | Rp. `".number_format($ttljumlahhargapasar, 0, "," , ".") ."`"; 
        $text .= "\n";
//                cari rataan avr bw
        $text .= "AVR BW `". number_format($allchickoutton / $allchickout, 2, "," , ".") ."`";
        $text .= "\n";
//                cari rataan avr fi
        $text .= "FI `". number_format($alljmlpakan / $allchickout, 2, "," , ".") ."`";
        $text .= "\n";
//                cari rataan ep
        $text .= "EP `". number_format(($allchickoutton / $alljmlpakan) * 100, 2, "," , ".") ."`%";
        $text .= "\n";
//                cari rataan fc
        $text .= "FC `". number_format($alljmlpakan / $allchickoutton, 2, "," , ".") ."`";
        $text .= "\n";
//                cari rataan ip
        $text .= "IP `". number_format($jumlahsemuaip/$totalAllPop, 2, "," , ".") ."`";
        $text .= "\n";
//                cari rataan umur
        $text .= "UMUR `". number_format($jumlahumur / $totalAllPop, 2, "," , ".") ."`";
   		$text .= "\n";
//                cari rataan doc bep
        $avgbepdoc=$allhargadoc / $allchickoutton;
        //echo number_format($avgbepdoc);
        
//                cari rataan doc pakan
        $avgbeppakan=$allhargapakan / $allchickoutton;
        //echo number_format($avgbeppakan);
        
//                cari rataan ovk
        $avgbepovk=$allhargaovk / $allchickoutton;
        //echo number_format($avgbepovk);
        
            $avglainlain=$totlainlain/$allchickoutton;
        //    echo number_format($avglainlain);
        
          $avgops=$sumakhirlaba/$allchickoutton;
        //    $avgops=$totoperasional/$allchickoutton;
        //    echo number_format($avgops);
        
        $sumbep=$avgbepdoc+$avgbeppakan+$avgbepovk+$avglainlain+$avgops;
        $text .= "HPP `". number_format($sumbep, 0, "," , ".") ."`";
        $text .= "\n";
        $avghargapasar=$ttljumlahhargapasar/$allchickoutton;
        $text .= "HARGA PASAR `". number_format($avghargapasar, 0, "," , ".") ."`";
        $text .= "\n";
        $avgekoranfarm=$totoperasional/$totalAllPop;
		$text .= "FARM `". number_format($totoperasional, 0, "," , ".") ."` | `". number_format($avgekoranfarm, 0, "," , ".") ."` /Ekor ";
        $text .= "\n";
       // $avgtonasefarm=$sumakhirlaba/$allchickoutton;
        $avgtonasefarm=$totoperasional/$allchickoutton;
        //echo number_format($avgtonasefarm);
        
        //laba rugi kompany
        //ekoran laba rugi kompany
        $allekorancompany=$alllrcompany/$totalAllPop;
        $text .= "COMPANY `". number_format($alllrcompany, 0, "," , ".") ."` | `". number_format($allekorancompany, 0, "," , ".") ."` /Ekor";
        $text .= "\n";

        //kg laba rugi kompany
        $alltonasecompany=$alllrcompany/$allchickoutton;
    //    echo number_format($alltonasecompany);
    //    echo number_format($avr_jml_kontrol,2); 
    //    echo number_format($avr_lama_rhpp,2);

	return $text;
}



function cariharga($db, $blncari, $tipebarang, $tabel, $stsfarm) {
	$con_unit = konek_db($db);
    if($stsfarm=="INF"){ 
        $qPro=mysqli_query($con_unit, "select $db.rhpp.id_produksi, $db.rhpp.id_farm, $db.rhpp.ip, $db.farm.id_farm, $db.farm.nama  from $db.rhpp join $db.farm on $db.rhpp.id_farm=$db.farm.id_farm where $db.farm.nama like '%INF%' and panenbulan like '%$blncari%'  order by $db.rhpp.id_produksi");
    }else if($stsfarm=="KMT"){
        $qPro=mysqli_query($con_unit, "select $db.rhpp.id_produksi, $db.rhpp.ip, $db.produksi.id_produksi, $db.produksi.id_kontrak  from $db.rhpp join $db.produksi on $db.rhpp.id_produksi=$db.produksi.id_produksi where panenbulan like '%$blncari%' and id_kontrak like '%KMT%'  order by $db.rhpp.id_produksi");
    }else if($stsfarm=="IJR"){
        $qPro=mysqli_query($con_unit, "select $db.rhpp.id_produksi, $db.rhpp.ip, $db.produksi.id_produksi, $db.produksi.id_kontrak  from $db.rhpp join $db.produksi on $db.rhpp.id_produksi=$db.produksi.id_produksi where panenbulan like '%$blncari%' and id_kontrak not like '%KMT%'  order by $db.rhpp.id_produksi");
    }else { 
        $qPro=mysqli_query($con_unit, "select $db.rhpp.id_produksi from $db.rhpp where $db.rhpp.panenbulan like '%$blncari%' order by $db.rhpp.id_produksi");
    }

    while($dPro=mysqli_fetch_array($qPro)){ 
//    $qjmlharga = mysqli_query("select $db.$tabel.id_barang, $db.$tabel.id_produksi, $db.$tabel.qty, $db.barang.id_barang, $db.barang.hrg_beli from $db.$tabel join $db.barang on $db.$tabel.id_barang=$db.barang.id_barang where $db.$tabel.id_produksi ='$dPro[id_produksi]' and $db.$tabel.id_barang like '%$tipebarang%'");
    $harga = carihargaprod($db, $dPro['id_produksi'], $tipebarang, $tabel);


//    $djmlharga = mysqli_fetch_array($qjmlharga);
//    $harga = $djmlharga['hrg_beli'] * $djmlharga['qty'];
//    $harga = $djmlharga['qty'];
    $totharga = $totharga + $harga;
    }
    return $totharga;
}

function cekpopall($db,$blnthn){
	$con_unit = konek_db($db);
  $qPro=mysqli_query($con_unit, "select id_produksi from $db.rhpp where panenbulan like '%$blnthn%' order by id_produksi");
  while($dPro=mysqli_fetch_array($qPro)){
    //populasi
    $qpop=mysqli_query($con_unit, "SELECT qty from $db.jual where id_produksi='$dpro[id_produksi]' and id_barang like '%D-%'");
    while($dpop=mysqli_fetch_array($qpop)){
      $subTotalQtyPop=$subTotalQtyPop+$dpop[qty];
    }
    //beban
    $qbeban=mysqli_query($con_unit, "SELECT qty from $db.beban where id_produksi='$dpro[id_produksi]'");
    while($dbeban=mysqli_fetch_array($qbeban)){
      $subTotalQtyBeban=$subTotalQtyBeban+$dbeban[qty];
    }
    $subTotal=$subTotalQtyPop-$subTotalQtyBeban;
  }
  return $subTotal;
}
function carihargaprod($db, $blncari, $tipebarang, $tabel) {
	$con_unit = konek_db($db);
    $qjmlharga = mysqli_query($con_unit, "select sum(qty * hrg_beli) as hrg from $db.$tabel j inner join $db.barang b on j.id_barang=b.id_barang where id_produksi in (SELECT id_produksi FROM $db.rhpp where id_produksi like '%$blncari%') and j.id_barang like '%$tipebarang%'");
    $djmlharga = mysqli_fetch_array($qjmlharga);
    $harga = $djmlharga['hrg'];
    return $harga;

}
function carihargafarm($db, $blncari, $tipebarang, $tabel,$idproduksi) {
	$con_unit = konek_db($db);
    $qjmlharga = mysqli_query($con_unit, "select sum(qty * hrg_beli) as hrg from $db.$tabel j inner join $db.barang b on j.id_barang=b.id_barang where id_produksi='$idproduksi' and j.id_barang like '%$tipebarang%'");
    $djmlharga = mysqli_fetch_array($qjmlharga);
    $harga = $djmlharga['hrg'];
    return $harga;
}

function carihargapassar($db, $blncari) {
	$con_unit = konek_db($db);
    $hargapasar = mysqli_query($con_unit, "SELECT (sum(hrg_real*tonase_real)/sum(tonase_real)) as rataan FROM $db.pja WHERE `id_produksi` in (SELECT id_produksi FROM $db.rhpp where panenbulan like '%$blncari%')");
    $dhargapasar = mysqli_fetch_array($hargapasar);
    $rataan = $dhargapasar['rataan'];
    return $rataan;
}

function labafarm($db, $blncari) {
	$con_unit = konek_db($db);
    //$lfarm = mysqli_query("SELECT sum((p_ayam-(`sapronak`-`retur_sapronak`))+`bonus_ip`+`bonus_fc`+`bonus_shp`+`bonus_mati`) as labafarm FROM $db.rhpp WHERE panenbulan like '%$blncari%'");
    $lfarm = mysqli_query($con_unit, "SELECT sum(thp_ternak+`bonus_ip`+`bonus_fc`+`bonus_shp`+`bonus_mati`) as labafarm FROM $db.rhpp WHERE panenbulan like '%$blncari%'");
    $dlfarm = mysqli_fetch_array($lfarm);
    $labafarm = $dlfarm['labafarm'];
    return $labafarm;
}
function cekfarm($db,$id){
	$con_unit = konek_db($db);
    $lfarm = mysqli_query($con_unit, "SELECT * FROM $db.farm WHERE id_farm='$id'");
    $dlfarm = mysqli_fetch_array($lfarm);
    $nmfarm = $dlfarm['nama'];
    return $nmfarm;
}
function hargaPakan($dbu, $idproduksi){
	$con_unit = konek_db($dbu);
  $subtot_pkn=0;$sub_pkn=0;
  $qhpkn = mysqli_query($con_unit, "select * from $dbu.jual where id_produksi='$idproduksi' and id_barang like '%F-%' order by tanggal");
  while($dhpkn = mysqli_fetch_array($qhpkn))
  {
    $hid_barang = $dhpkn['id_barang'];
    $qhpkn2=mysqli_query($con_unit, "select * from $dbu.barang where id_barang='$hid_barang'");
    $dhpkn2=mysqli_fetch_array($qhpkn2);
    $sub_pkn = $dhpkn['qty']*$dhpkn2['hrg_beli'];
    $subtot_pkn = $subtot_pkn+$sub_pkn;
  }
  $subtot_retpkn=0;$sub_retpkn=0;
  $qhretpkn = mysqli_query($con_unit, "select * from $dbu.retur_jual where id_produksi='$idproduksi' and id_barang like '%F-%' order by tgl_rsap");
  while($dhretpkn = mysqli_fetch_array($qhretpkn))
  {
    $hretid_barang = $dhretpkn['id_barang'];
    $qhretpkn2=mysqli_query($con_unit, "select * from $dbu.barang where id_barang='$hretid_barang'");
    $dhretpkn2=mysqli_fetch_array($qhretpkn2);
    $sub_retpkn = $dhretpkn['qty']*$dhretpkn2['hrg_beli'];
    $subtot_retpkn = $subtot_retpkn+$sub_retpkn;
  }
  $total_hpkn=$subtot_pkn-$subtot_retpkn;
  return   $total_hpkn;
}

function hargaPkanBulan($dbu, $blnthn, $stsfarm){
	$con_unit = konek_db($dbu);
    if($stsfarm=="INF"){ 
        $qPro=mysqli_query($con_unit, "select $dbu.rhpp.id_produksi, $dbu.rhpp.id_farm, $dbu.farm.id_farm, $dbu.farm.nama  from $dbu.rhpp join $dbu.farm on $dbu.rhpp.id_farm=$dbu.farm.id_farm where $dbu.farm.nama like '%INF%' and panenbulan like '%$blnthn%'  order by $dbu.rhpp.id_produksi");
    }else if($stsfarm=="KMT"){
        $qPro=mysqli_query($con_unit, "select $dbu.rhpp.id_produksi, $dbu.rhpp.ip, $dbu.produksi.id_produksi, $dbu.produksi.id_kontrak  from $dbu.rhpp join $dbu.produksi on $dbu.rhpp.id_produksi=$dbu.produksi.id_produksi where panenbulan like '%$blnthn%' and id_kontrak like '%KMT%'  order by $dbu.rhpp.id_produksi");
    }else if($stsfarm=="IJR"){
        $qPro=mysqli_query($con_unit, "select $dbu.rhpp.id_produksi, $dbu.rhpp.ip, $dbu.produksi.id_produksi, $dbu.produksi.id_kontrak  from $dbu.rhpp join $dbu.produksi on $dbu.rhpp.id_produksi=$dbu.produksi.id_produksi where panenbulan like '%$blnthn%' and id_kontrak not like '%KMT%'  order by $dbu.rhpp.id_produksi");
    }else { 
        $qPro=mysqli_query($con_unit, "select id_produksi from $dbu.rhpp where panenbulan like '%$blnthn%' order by id_produksi");
    }

  while($dPro=mysqli_fetch_array($qPro)){
  $subtot_pkn=0;$sub_pkn=0;
  $qhpkn = mysqli_query($con_unit, "select * from $dbu.jual where id_produksi='$dPro[id_produksi]' and id_barang like '%F-%' order by tanggal");
  while($dhpkn = mysqli_fetch_array($qhpkn))
  {
    $hid_barang = $dhpkn['id_barang'];
    $qhpkn2=mysqli_query($con_unit, "select * from $dbu.barang where id_barang='$hid_barang'");
    $dhpkn2=mysqli_fetch_array($qhpkn2);
    $sub_pkn = $dhpkn['qty']*$dhpkn2['hrg_beli'];
    $subtot_pkn = $subtot_pkn+$sub_pkn;
  }
  $subtot_retpkn=0;$sub_retpkn=0;
  $qhretpkn = mysqli_query($con_unit, "select * from $dbu.retur_jual where id_produksi='$dPro[id_produksi]' and id_barang like '%F-%' order by tgl_rsap");
  while($dhretpkn = mysqli_fetch_array($qhretpkn))
  {
    $hretid_barang = $dhretpkn['id_barang'];
    $qhretpkn2=mysqli_query($con_unit, "select * from $dbu.barang where id_barang='$hretid_barang'");
    $dhretpkn2=mysqli_fetch_array($qhretpkn2);
    $sub_retpkn = $dhretpkn['qty']*$dhretpkn2['hrg_beli'];
    $subtot_retpkn = $subtot_retpkn+$sub_retpkn;
  }
  $allpakan = $allpakan +$subtot_pkn;
  $allretpakan = $allretpakan + $subtot_retpkn;
}
  $total_hpkn=$allpakan-$allretpakan;
  return  $total_hpkn;
}

function jenispakan($dbu, $id_produksi) {
	$con_unit = konek_db($dbu);
    $qpakan = mysqli_query($con_unit, "SELECT $dbu.jual.*, $dbu.barang.nama FROM $dbu.jual join $dbu.barang on $dbu.jual.id_barang = $dbu.barang.id_barang WHERE (id_produksi='$id_produksi') and (barang.id_barang like 'F-%') group by barang.nama") OR die(mysqli_error(0));
    $JenisPakan ='';

    while ( $data  = mysqli_fetch_array($qpakan) ) {
        $JenisPakan = $JenisPakan ." " .$data['nama'];
    };
    return $JenisPakan;
}

?>

