<div class="wrapper">
    <div class="container-full">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <div class="pull-right m-t-15">
                    <form class="form-horizontal m-r-10" method="POST" action="<?php echo base_url();?>accounting_controller/ho_pnl">                        
                        <div class="row">
                            <div class="col-md-3">
                                <select class="form-control" name="business_id">
                                <?php if($this->session->userdata('ho')=='Yes'){ ?> 
                                    <option value="0">All</option>
                                <?php } ?>
                                <?php foreach($business_list as $b){?>
                                    <option <?php if($b['id_business'] == $business_id){ echo 'selected = "selected"'; } ?> value="<?php echo $b['id_business'];?>"><?php echo $b['business_name'];?></option>
                                <?php } ?>
                                </select>
                            </div>
                            <div class="col-md-3">
                                <input class="form-control" type="date" name="startdate" value="<?php if($startdate!=null){ echo $startdate; } ?>">
                            </div>
                            <div class="col-md-3">
                                <input class="form-control" type="date" name="enddate"  value="<?php if($enddate!=null){ echo $enddate; } ?>">
                            </div>
                            <div class="col-md-3 m-t-5">
                                <button class="btn btn-primary">Go</button>
                            </div>
                        </div>

                    </form>
                </div>
                <h4 class="page-title">Profit and Loss:</h4>
            </div>
        </div>
        <div class="row" >
            <div class="col-sm-12">
                <div class="card-box ">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="pull-right">
                                <button class="btn btn-inverse btn-sm" onclick="printDiv('printableArea')"><i class="fa fa-icon fa-print"></i></button>
                               
                                
                            </div>
                        </div>
                        <div class="pull-left">
                            <h4 class="header-title m-t-0 m-b-30"></h4>
                        </div>
                    </div>
                    

                    <?php $col = sizeof($businesses); $x=0;?>
                    <div class="row" id="printableArea">
                        <div class="col-sm-12">                        
                        <?php foreach($businesses as $business){ $x++;?>
                        <?php if ($x > 4){ $x=0;?>
                        <div class="row" style="margin-top:20px;">
                        <?php } ?>
                            <div class="<?php if($col==1){echo "col-md-8";}else{echo "col-md-3";} ?>" style="min-height:600px; <?php if($col>1){ ?>overflow-y: scroll; overflow-x: hidden<?php } ?>">
                                <h3><?php echo $business['business_name'];?></h3>
                                <button class="btn btn-primary btn-sm" onclick="excelExport('<?php echo str_replace(' ', '', $business['business_name']);?>', <?php echo $business['id_business'];?>)">Excel</button>
                                
                                <div style="text-align:right;" id="pnl_<?php echo $business['id_business'];?>">
                                    <h4 style="text-align: left;" class="section_dates"><?php echo date_format(date_create($startdate), 'd-m-Y').' || '. date_format(date_create($enddate), 'd-m-Y');?></h4>
                                    <?php 
                                    $filterBy = 'Assets'; // or Finance etc.
                                    $assets = array_filter($business['accounts'], function ($account_type){
                                            return $account_type['account_main'] == 'Assets';
                                        });                 
                                    $expenses = array_filter($business['accounts'], function ($account_type){
                                        return $account_type['account_main'] == 'Expenses';
                                    });
                                    $revenue = array_filter($business['accounts'], function ($account_type){
                                        return $account_type['account_main'] == 'Revenue';
                                    });
                                    $equity = array_filter($business['accounts'], function ($account_type){
                                        return $account_type['account_main'] == 'Equity';
                                    });
                                    $liability = array_filter($business['accounts'], function ($account_type){
                                        return $account_type['account_main'] == 'Liability';
                                    });

                                    $cog = array_filter($business['accounts'], function ($account_type){
                                        return $account_type['expense_type'] == 'COST OF GOODS';
                                    });

                                    $adminexpense = array_filter($business['accounts'], function ($account_type){
                                        return $account_type['expense_type'] == 'ADMIN EXPENSE';
                                    });

                                    $salaryexpense = array_filter($business['accounts'], function ($account_type){
                                        return $account_type['expense_type'] == 'SALARY EXPENSE';
                                    });

                                    $fixedexpense = array_filter($business['accounts'], function ($account_type){
                                        return $account_type['expense_type'] == 'FIXED EXPENSE';
                                    });
                                    
                                    $otherexpense = array_filter($business['accounts'], function ($account_type){
                                        return $account_type['expense_type'] == 'GENERAL EXPENSE';
                                    });


                                    ?>
                                    <!--Revenue Section-->
                                    <div class="row" >
                                        <div class="col-md-12 section" style="text-align:left;">
                                            <h5 style="font-size: 16px;" class = "section_name">INCOME:</h5>
                                            <?php $sumincome=0; foreach($revenue as $revenue){?>
                                            <div class="row accounts">
                                                
                                                <div class="col-md-3">
                                                    <p style="text-align:left;" class="account_head"><strong><?php echo $revenue['account_head']; ?></strong></p>
                                                </div>                    
                                                <div class="col-md-3">
                                                    <p style="text-align:right;" class="account_value"><strong><?php echo number_format($revenue['current_balance'],2); $sumincome += $revenue['current_balance'];  ?></strong></p>
                                                </div>  
                                                <div class="col-md-3"></div>  
                                                <div class="col-md-3"></div>                 
                                            </div>    
                                            <?php } ?>
                                            
                                            <div class="row section_total">
                                                <div class="col-md-3 " style="border-top:1px solid #808080; font-size:12px; font-weight:700;">
                                                    <p class="total_name">NET INCOME: </p>
                                                </div>
                                                <div class="col-md-3 " style="border-top:1px solid #808080; font-size:12px; font-weight:700;"></div>
                                                <div class="col-md-3 " style="border-top:1px solid #808080; font-size:12px; font-weight:700;"></div>       
                                                <div class="col-md-3 " style="border-top:1px solid #808080; font-size:12px; font-weight:700;">
                                                    <p class="total_value"><?php echo number_format($sumincome,2);?></p>
                                                </div>
                                                          
                                            </div>
                                        </div>
                                    </div>
                                    <!--Revenue Section-->
                                    <!--COG Section-->
                                    <div class="row">
                                    <div class="col-md-12 section" style="text-align:left !important;">
                                            <h5 style="font-size: 16px;" class="section_name">COST OF GOODS:</h5>
                                    
                                            <?php $sumcog=0; foreach($cog as $expense){?>
                                            <div class="row accounts">
                                               
                                                <div class="col-md-3">
                                                    <p style="text-align:left;" class="account_head"><strong><?php echo $expense['account_head']; ?></strong></p>
                                                </div>                    
                                                <div class="col-md-3">
                                                    <p style="text-align:right;" class="account_value"><strong><?php echo number_format($expense['current_balance'],2); $sumcog += $expense['current_balance'];  ?></strong></p>
                                                </div>   
                                                 <div class="col-md-3"></div>
                                                <div class="col-md-3"></div>                 
                                            </div>                
                                            <?php } ?>
                                            <div class="row section_total">
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;">
                                                    <p class="total_name">NET COST OF GOODS: </p>
                                                </div>
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;"></div>
                                                <div class="col-md-3" style="text-align:right; border-top:1px solid #808080; font-size:12px; font-weight:700;"></div>
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;">
                                                    <p class="total_value"><?php echo number_format($sumcog,2);?></p>
                                                </div>
                                               
                                            </div>
                                            <div class="row">
                                                <div class="col-md-12" style="text-align:right; border-top:1px solid #808080; font-size:12px; font-weight:700;"><?php echo number_format($sumincome - $sumcog,2); ?></div>                 
                                            </div>
                                        </div>
                                    </div>
                                    <!--COG Section-->

                                    <!--ADMIN Section-->
                                    <div class="row">
                                        <div class="col-md-12 section" style="text-align:left !important;">
                                            <h5 class="section_name" style="font-size: 16px;">ADIM EXPENSES:</h5>
                                        
                                            <?php $sumadminexpense=0; foreach($adminexpense as $expense){?>
                                            <div class="row accounts">
                                               
                                                <div class="col-md-3">
                                                    <p style="text-align:left;" class="account_head"><strong><?php echo $expense['account_head']; ?></strong></p>
                                                </div>                    
                                                <div class="col-md-3">
                                                    <p style="text-align:right;" class="account_value"><strong><?php echo number_format($expense['current_balance'],2); $sumadminexpense += $expense['current_balance'];  ?></strong></p>
                                                </div>   
                                                 <div class="col-md-3"></div>
                                                <div class="col-md-3"></div>                 
                                            </div>                
                                            <?php } ?>
                                            <div class="row section_total">
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;">
                                                    <p class="total_name">NET ADMIN EXPENSES: </p>
                                                </div>
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;"></div>
                                                
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;">
                                                    <p class="total_value"><?php echo number_format($sumadminexpense,2);?></p>
                                                </div>              
                                                <div class="col-md-3" style="text-align:right; border-top:1px solid #808080; font-size:12px; font-weight:700;"></div>                               
                                            </div>
                                            <div class="row">
                                                <div class="col-md-12" style="text-align:right; border-top:1px solid #808080; font-size:12px; font-weight:700;"><?php echo number_format($sumincome - $sumcog - $sumadminexpense,2); ?></div>
                                            </div>
                                        </div>
                                    </div>
                                    <!--ADMIN Section-->
                                    <!--SALARY Section-->
                                    <div class="row">
                                        <div class="col-md-12 section" style="text-align:left !important;">
                                            <h5 class="section_name" style="font-size: 16px;">OTHER EXPENSES:</h5>
                                        
                                            <?php $sumotherexpense=0; foreach($otherexpense as $expense){?>
                                            <div class="row accounts">
                                                <div class="col-md-3">
                                                    <p class="account_head" style="text-align:left;"><strong><?php echo $expense['account_head']; ?></strong></p>
                                                </div>                    
                                                <div class="col-md-3">
                                                    <p class="account_value" style="text-align:right;"><strong><?php echo number_format($expense['current_balance'],2); $sumotherexpense += $expense['current_balance'];  ?></strong></p>
                                                </div>   
                                                <div class="col-md-3"></div>
                                            </div>                
                                            <?php } ?>
                                            <div class="row section_total">
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;">
                                                    <p class="total_name">NET OTHER EXPENSES: </p>
                                                </div>
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;"></div>
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;">
                                                    <p class="total_value"><?php echo number_format($sumotherexpense,2);?></p>
                                                </div>
                                                <div class="col-md-3" style="text-align:right; border-top:1px solid #808080; font-size:12px; font-weight:700;"></div>                 
                                            </div>
                                            <div class="row">
                                                <div class="col-md-12" style="text-align:right; border-top:1px solid #808080; font-size:12px; font-weight:700;"><?php echo number_format($sumincome - $sumcog - $sumadminexpense  - $sumotherexpense,2); ?></div>                 
                                            </div>
                                        </div>
                                    </div>
                                    <!--SALARY Section-->

                                    <!--SALARY Section-->
                                    <div class="row">
                                        <div class="col-md-12 section" style="text-align:left !important;">
                                            <h5 class="section_name" style="font-size: 16px;">SALARY EXPENSES:</h5>
                                        
                                            <?php $sumsalaryexpense=0; foreach($salaryexpense as $expense){?>
                                            <div class="row accounts">
                                                <div class="col-md-3">
                                                    <p class="account_head" style="text-align:left;"><strong><?php echo $expense['account_head']; ?></strong></p>
                                                </div>                    
                                                <div class="col-md-3">
                                                    <p class="account_value" style="text-align:right;"><strong><?php echo number_format($expense['current_balance'],2); $sumsalaryexpense += $expense['current_balance'];  ?></strong></p>
                                                </div>   
                                                <div class="col-md-3"></div>
                                            </div>                
                                            <?php } ?>
                                            <div class="row section_total">
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;">
                                                    <p class="total_name">NET SALARY EXPENSES: </p>
                                                </div>
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;"></div>
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;">
                                                    <p class="total_value"> <?php echo number_format($sumsalaryexpense,2);?></p>
                                                </div>
                                                <div class="col-md-3" style="text-align:right; border-top:1px solid #808080; font-size:12px; font-weight:700;"></div>                 
                                            </div>
                                            <div class="row">
                                                <div class="col-md-12" style="text-align:right; border-top:1px solid #808080; font-size:12px; font-weight:700;"><?php echo number_format($sumincome - $sumcog - $sumadminexpense - $sumotherexpense - $sumsalaryexpense  ,2); ?></div>                 
                                            </div>
                                        </div>
                                    </div>
                                    <!--SALARY Section-->

                                    <!--FIXED Section-->
                                    <div class="row">                    
                                        <div class="col-md-12 section" style="text-align:left !important;">
                                            <h5 class="section_name" style="font-size: 16px;">FIXED EXPENSES:</h5>
                                                                
                                            <?php $sumfixedexpense=0; foreach($fixedexpense as $expense){?>
                                            <div class="row accounts">
                                                
                                                <div class="col-md-3">
                                                    <p class="account_head" style="text-align:left;"><strong><?php echo $expense['account_head']; ?></strong></p>
                                                </div>                    
                                                <div class="col-md-3">
                                                    <p class="account_value" style="text-align:right;"><strong><?php echo number_format($expense['current_balance'],2); $sumfixedexpense += $expense['current_balance'];  ?></strong></p>
                                                </div>   
                                                <div class="col-md-3"></div>
                                                <div class="col-md-3"></div>
                                            </div>                
                                            <?php } ?>
                                            <div class="row section_total">
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;">
                                                    <p class="total_name">NET FIXED EXPENSES: </p>
                                                </div>
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;"></div>
                                                <div class="col-md-3" style="border-top:1px solid #808080; font-size:12px; font-weight:700;">
                                                    <p class="total_value"><?php echo number_format($sumfixedexpense,2);?></p>
                                                </div>
                                                <div class="col-md-3" style="text-align:right; border-top:1px solid #808080; font-size:12px; font-weight:700;"></div>   
                                            </div>
                                            <div class="row">
                                                <div class="col-md-12 pnl_value" style="text-align:right; border-top:1px solid #808080; font-size:12px; font-weight:700;"><?php echo number_format($sumincome - $sumcog - $sumadminexpense - $sumotherexpense - $sumsalaryexpense - $sumfixedexpense, 2); ?></div>                 
                                            </div>
                                        </div>
                                    </div>
                                    <!--FIXED EXPENSES Section-->
                                </div>
                            </div>
                        <?php if ($x == 4){ ?>
                        </div>
                        <?php } ?>    
                        <?php } ?>                                
                        </div>
                    </div>
            </div>
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
<script>
    $('document').ready(function(){
        //$("#startdate").datepicker();
        //$("#enddate").datepicker();
    });


    function printDiv(divName) {
        var printContents = document.getElementById(divName).innerHTML;
        var originalContents = document.body.innerHTML;
        document.body.innerHTML = printContents;
        window.print();
        document.body.innerHTML = originalContents;
    }

    function excelExport(business, business_id){
        
        var elem = "#pnl_"+business_id+ ' .section';
        const data = [['Branch', business, '', '']];

        var dates = $("#pnl_"+business_id).find(".section_dates").text();  
        data.push([dates,'', '', '']);


        $(elem).each(function() {
           
            const field = $(this).find('.section_name').text();
            const value = '';
            data.push([field, value, '', '']);


            $(this).find('.accounts').each(function() {
                const field = $(this).find('.account_head').text();
                const value = $(this).find('.account_value').text();
                data.push(['', field, value, '']);
            });

            $(this).find('.section_total').each(function() {
                const field = $(this).find('.total_name').text();
                const value = $(this).find('.total_value').text();
                data.push([field, '', '', value]);
            });

        });


        data.push(['','', 'PnL', $(".pnl_value").text()]);

        
        const worksheet = XLSX.utils.aoa_to_sheet(data);

            // Apply some basic formatting
            const range = XLSX.utils.decode_range(worksheet['!ref']);
            for (let C = range.s.c; C <= range.e.c; C++) {
                const cellAddress = XLSX.utils.encode_cell({ r: 0, c: C });
                if (!worksheet[cellAddress]) continue;
                worksheet[cellAddress].s = {
                    font: { bold: true },
                    fill: { fgColor: { rgb: "FFFFCC00" } },
                    alignment: { horizontal: "center" }
                };
            }

            const workbook = XLSX.utils.book_new();
            XLSX.utils.book_append_sheet(workbook, worksheet, "Data");

            
            const bookname = "pnl_"+business+".xlsx";
            XLSX.writeFile(workbook, bookname);
    }
</script>