<?php defined('BASEPATH') OR exit('No direct script access allowed'); 
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>
<style>
  page {
        background: white;
    padding: 10px;
    display: block;
    margin: 0 auto;
    margin-bottom: 0.5cm;
   box-shadow: rgba(0, 0, 0, 0.22) 0px 2px 44px 0px;
  }



  .header_sec h3{
    font-weight: 600;
	font-size: 22px;
  }

  .company_name{
    font-size: 15px;
    font-weight: 600;
  }

  .date_1{
    font-size: 14px;
    margin-bottom: 25px;
  }

  .table th, .table td {
    padding: 5px !important;
  }

  .right_align{
    text-align: right !important;
  }

  .right_float{
    float: right;
  }
  
  .btn {
    padding: 1px 12px;
}
/* div.tab {
    overflow: hidden;
  } */
  div.tab button {
    background-color: inherit;
    float: left;
    outline: none;
    cursor: pointer;
    padding:5px 10px;
    transition: 0.3s;
    font-size: 17px;
    border: 2px solid #428bca;
    margin-right: 5px;
    margin: 5px;
  }
  div.tab button:hover {
    background-color: #428bca;
    color: #fff;
  }
  div.tab button.active {
    background-color: #428bca;
    color: #fff;
  }
  .tabcontent {
    display: none;
    padding: 6px 12px;
    border-top: none;
  }
  .button {
    position: relative;
    background-color: #000000;
    border: none;
    font-size: 28px;
    color: #FFFFFF;
    padding: 20px;
    width: 200px;
    text-align: center;
    -webkit-transition-duration: 0.4s; 
    transition-duration: 0.4s;
    text-decoration: none;
    overflow: hidden;
    cursor: pointer;
  }
  .button:after {
    content: "";
    background: #90EE90;
    display: block;
    position: absolute;
    padding-top: 300%;
    padding-left: 350%;
    margin-left: -20px!important;
    margin-top: -120%;
    opacity: 0;
    transition: all 0.8s
  }
  .button:active:after {
    padding: 0;
    margin: 0;
    opacity: 1;
    transition: 0s
  }
@media print {
 page[size="A4"] {  
    width: 21cm;
    height: 29.7cm; 
  }

}
</style>
<?php

$v = "";
/* if($this->input->post('name')){
  $v .= "&product=".$this->input->post('product');
  } */

if ($this->input->post('start_date')) {
    $v .= "&start_date=" . $this->input->post('start_date');
}
if ($this->input->post('end_date')) {
    $v .= "&end_date=" . $this->input->post('end_date');
}

?>

<script type="text/javascript">
    $(document).ready(function () {
        $('#form').hide();
        
        $('.toggle_down').click(function () {
            $("#form").slideDown();
            return false;
        });
        $('.toggle_up').click(function () {
            $("#form").slideUp();
            return false;
        });
    });
</script>
<div class="box">
    <div class="box-header">
		<h2 class="blue"><i class="fa fa-money"></i><?= lang('profit_loss'); ?> 
		</h2>
		<div class="box-icon">
            <ul class="btn-tasks">
                <li class="dropdown">
                    <a href="#" class="toggle_up tip" title="<?= lang('hide_form') ?>">
                        <i class="icon fa fa-toggle-up"></i>
                    </a>
                </li>
                <li class="dropdown">
                    <a href="#" class="toggle_down tip" title="<?= lang('show_form') ?>">
                        <i class="icon fa fa-toggle-down"></i>
                    </a>
                </li>
            </ul>
        </div>
    </div>
    <div class="box-content">
        <div class="row">
            <div class="col-lg-12">
				<div id="form">
					<?php echo admin_form_open("reports/profit_losss"); ?>
						<div class="row">
							<div class="col-sm-4">
								<div class="form-group">
									<?= lang("start_date", "start_date"); ?>
									<?php echo form_input('start_date', (isset($_POST['start_date']) ? $_POST['start_date'] : ""), 'class="form-control datetime" id="start_date"'); ?>
								</div>
							</div>
							<div class="col-sm-4">
								<div class="form-group">
									<?= lang("end_date", "end_date"); ?>
									<?php echo form_input('end_date', (isset($_POST['end_date']) ? $_POST['end_date'] : ""), 'class="form-control datetime" id="end_date"'); ?>
								</div>
							</div>
						</div>
						<div class="form-group">
							<div
								class="controls"> <?php echo form_submit('submit_report', $this->lang->line("submit"), 'class="btn btn-primary"'); ?> 
							</div>
						</div>
                    <?php echo form_close(); ?>
				</div>
                <div class="clearfix"></div>
				<page size="A4">
				<div class="container">
					<div class="row">
						<div class="header_sec col-md-11">
							<h3><?= lang('profit_loss'); ?></h3>
							<span class="company_name"></span><br>
							<div class="date_1">
								<span> <?= lang("date"); ?></span>
								<span><?php
										if ($this->input->post('start_date')) 
										{
											echo "From " . $this->input->post('start_date') . " to " . $this->input->post('end_date');
										}
										else
										{
											echo  "From " . $this->sma->hrld(date('Y-m-d 00:00:00')). " to " .$this->sma->hrld(date('Y-m-d H:i:s')) ;
										}
										?>
								</span>
							</div>
						</div>
						<div class="col-md-1">
							<button type="button" class="no-print btn btn-primary" onclick="window.print();">Print</button>
						</div>
						<div class="col-md-12">
							<span><b></b></span>
							<span class="right_float"> <?= lang("date"); ?>: <?= $this->sma->hrld(date('Y-m-d H:i:s')); ?></span>
						</div>
						<div class="tab">
							<button class="tablinks no-print" onclick="Tools(event, 'Summary')">Summary</button>
							<button class="tablinks no-print" onclick="Tools(event, 'Detail')">Detail Report</button>
						</div>
						<div id="Summary" class="tabcontent" >
							<table class="table" >
								<thead class="thead-light">
									<tr>
										<th colspan="2" style="text-align:  left;"><b>INCOME</b></th>
									</tr>
								</thead>
								<tbody>
									<?php
								$p_type = $p_typs = $purchase_sold = $operating = $p_inc = $p_incos = $p_incose = $p_expect_inco =$p_incoss = $p_incosee = $p_expects_incos = $p_incoos = $p_incoose = $p_expecto_incoo = $p_inco = $gross_profit = $percentage = $percent = $cross_profit = $cros_percentage = $cros_percent = $profit_loss = $incose = $incos = 0;
										foreach ($profit as $pro)
										{ 
											if($pro->type =='income')
											{
												$p_type+= $pro->amount;
											}
											if($pro->type =='expense')
											{
												$p_typs+= $pro->amount;
												//print_r($p_typs);
											}
											if($pro->acc_type =='31')
											{
												$purchase_sold+= $pro->amount;
												//print_r( $invent);
											}
											if(($pro->acc_type =='30')||($pro->acc_type =='32')|| ($pro->acc_type =='33')||($pro->acc_type =='34') || ($pro->acc_type =='35'))
											{
												$operating+= $pro->amount;
												//print_r( $invent);
											}
											if(($pro->acc_type =='18')|| ($pro->acc_type =='26')||($pro->acc_type =='27') || ($pro->acc_type =='28')||($pro->acc_type =='32'))
											{
												$p_inc+= $pro->amount;
													//print_r($p_inc);
											}
											if(($pro->acc_type =='7')&&($pro->type =='income'))
											{
											   $p_incos+= $pro->amount;
											}
											if(($pro->acc_type =='7')&&($pro->type =='expense'))
											{
												$p_incose+= $pro->amount;
											}
											$p_expect_inco = $incos -  $incose ;
											
											if(($pro->acc_type =='25')&&($pro->type =='income'))
											{
												$p_incoss+= $pro->amount;
											}
											if(($pro->acc_type =='25')&&($pro->type =='expense'))
											{
											   $p_incosee+= $pro->amount;
											}
											   $p_expects_incos = $p_incoss -  $p_incosee ;
											   
											if(($pro->acc_type =='22')&&($pro->type =='income'))
											{
											   $p_incoos+= $pro->amount;
											}
											if(($pro->acc_type =='22')&&($pro->type =='expense'))
											{
												$p_incoose+= $pro->amount;
											}
											$p_expecto_incoo = $p_incoos -  $p_incoose ;
											$p_inco = $p_inc +   $p_expect_inco +  $p_expects_incos + $p_incoose; 
			  
										} 
									?>
									<tr>
										<td><?= lang('sales') ?></td>
										<td class="right_align"><?php echo $this->sma->formatQuantity($p_inco); ?></td>
									</tr>
           
								</tbody>
							</table>
							<table class="table">
								<thead class="thead-light">
									<tr>
										<th colspan="2" style="text-align:  left;">Cost of Goods Sold</th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td><?= lang('purchases') ?></td>
										<td class="right_align"><?= $purchase_sold;?></td>
									</tr>
								</tbody>
							</table>
							<table class="table">
								<thead class="thead-light">
									<tr>
										<th style="text-align:left; background-color: #165082;"><b>Gross Profit</b></th>
										<?php 
											$gross_profit =  $p_inco - $purchase_sold;
											//print_r($gross_profit);
											if($p_inco)
											{ 
												if($gross_profit > 0)
												{
													$percentage = $gross_profit / $p_inco ;
												}
												else
												{
													$percentage = 0;
												}
											}
											else
											{
											$percentage = 0;
											}
											$percent=  $percentage * 100;
										?>
										<th class="right_align" style=" background-color: #165082;"><?php echo $this->sma->formatQuantity($gross_profit);?></th>
									</tr>
									<tr>
										<th style="text-align:left; background-color: #165082;color: #d2cece;"><b>As a percentage of Total Income</b></th>
										<th class="right_align" style=" background-color: #165082;color: #d2cece;"><?php echo $this->sma->formatQuantity($percent);?>%</th>
									</tr>
								</thead>
							</table>
							<table class="table">
								<thead class="thead-light">
								</thead>
								<tbody>
									<tr>
										<td>Operating Expenses</td>
										<td class="right_align"><?=$operating;?></td>
									</tr>
								</tbody>
							</table>
							<table class="table">
								<thead class="thead-light">
									<tr>
										<th style="text-align:left; background-color: #165082;"><b>Net Profit</b></th>
										<?php 
											$cross_profit = $gross_profit - $operating;
											if($p_inco)
											{
												if($cross_profit > 0)
												{
													$cros_percentage = $cross_profit /  $p_inco;
												}
												else 
												{
													$cros_percentage = 0; 
												}
											}
											else
											{
												$cros_percentage = 0;
											}
											$cros_percent = $cros_percentage * 100;
										?>
										<th class="right_align" style=" background-color: #165082;"><?php echo $this->sma->formatQuantity($cross_profit);?></th>
									</tr>
									<tr>
										<th style="text-align:left; background-color: #165082;color: #d2cece;"><b>As a percentage of Total Income</b></th>
										<th class="right_align" style=" background-color: #165082;color: #d2cece;"><?php echo $this->sma->formatQuantity($cros_percent);?>%</th>
									</tr>
								</thead>
							</table>
						</div>
						<div id="Detail" class="tabcontent" >
							<table class="table" >
								<thead class="thead-light">
									<tr>
										<th colspan="2" style="text-align:  left;"><b>INCOME</b></th>
									</tr>
								</thead>
								<tbody>
									<?php 
									$cash_flow = $cash_flow_a = $cash_flow_b = $cash_flow_c = $cash_flow_d = 0;
									foreach($profit as $rose)
									{ 	
										if(!empty($rose->acc_type))
										{
											if(($rose->acc_type =='18')&&($rose->type =='income'))
											{
												$cash_flow+= $rose->amount;  //print_r( $cash_flow);
											}
											if(($rose->acc_type =='25')&&($rose->type =='income'))
											{
												$cash_flow_a+= $rose->amount;  
											}
											if(($rose->acc_type =='26')&&($rose->type =='income'))
											{
												$cash_flow_b+= $rose->amount;  //print_r( $amountedf);
											}
											if(($rose->acc_type == '27')&&($rose->type == 'income'))
											{
												$cash_flow_c+= $rose->amount;  
											}
											if(($rose->acc_type == '28')&&($rose->type == 'income'))
											{
												$cash_flow_d+= $rose->amount;  //print_r( $amountedf);
											}
										}
										
									}

									$sale_total = $cash_flow + $cash_flow_a + $cash_flow_b + $cash_flow_c + $cash_flow_d ;
								?>
								
									<tr>
										<td>
											<?php echo $this->db->get_where('accounts',array('type' =>'18'))->row()->Name;?>
										</td>
										<?php if($cash_flow)
										{?>
											<td class="text-right"><?=$cash_flow;?></td>
										<?php 
										} 
										else
										{?>
											<td class="text-right">0.00</td>
										<?php 
										} ?>
									</tr>
									<?php 
									$abc = $this->db->get('accounts')->result_array();
									foreach ($abc as $cba){
										//print_r($cba['type']);
										$type = $cba['type'];
									}
									if($type == 26){
									?>
									<tr>
										<td>
											<?php echo $this->db->get_where('accounts',array('type' =>'26'))->row()->Name;?>
										</td>
										<?php if($cash_flow_a)
										{?>
											<td class="text-right"><?=$cash_flow_a;?></td>
										<?php 
										} 
										else
										{?>
											<td class="text-right">0.00</td>
										<?php 
										} ?>
									</tr>
									<?php } 
									if($type == 25){
									?>
									
									<tr>
										<td>
											<?php echo $this->db->get_where('accounts',array('type' =>'25'))->row()->Name;?>
										</td>
										<?php if($cash_flow_b)
										{?>
											<td class="text-right"><?=$cash_flow_b;?></td>
										<?php 
										} 
										else
										{?>
											<td class="text-right">0.00</td>
										<?php 
										} ?>
									</tr>
									<?php } ?>
									<tr>
										<td>
											<?php echo $this->db->get_where('accounts',array('type' =>'27'))->row()->Name;?>
										</td>
										<?php if($cash_flow_c)
										{?>
											<td class="text-right"><?=$cash_flow_c;?></td>
										<?php 
										} 
										else
										{?>
											<td class="text-right">0.00</td>
										<?php 
										} ?>
									</tr>
									<tr>
										<td>
											<?php echo $this->db->get_where('accounts',array('type' =>'28'))->row()->Name;?>
										</td>
										<?php if($cash_flow_d)
										{?>
											<td class="text-right"><?=$cash_flow_d;?></td>
										<?php 
										} 
										else
										{?>
											<td class="text-right">0.00</td>
										<?php 
										} ?>
									</tr>
									<tr class="net_value">
										<td><b>Total Income</b></td>
										<?php if($sale_total)
										{?>
											<td class="text-right"><?=$sale_total;?></td>
										<?php 
										} 
										else
										{?>
											<td class="text-right">0.00</td>
										<?php 
										} ?>
									</tr>
           
								</tbody>
							</table>
							<table class="table">
								<thead class="thead-light">
									<tr>
										<th colspan="2" style="text-align:  left;">Cost of Goods Sold</th>
									</tr>
								</thead>
								<tbody>
									<?php
									$purchase_a = $purchase_b=$purchase_c=$purchase_d=$purchase_e= $purchase_f=$purchase_g=$purchase_h=$purchase_i=$purchase_j=0;
										foreach ($profit as $ros)
										{
											if(!empty($ros->acc_type))
											{
												if(($ros->acc_type =='31')&&($ros->type =='expense'))
												{
													$purchase_a+= $ros->amount;
												}
												if(($ros->acc_type =='30')&&($ros->type =='expense'))
												{
													$purchase_b+= $ros->amount;
												}
												if((($ros->acc_type) == '32')&&(($ros->type) == 'income'))
												{
													$purchase_e+= $ros->amount;  //print_r( $amountedf);
												}
												if(($ros->acc_type == '32')&&($ros->type == 'expense'))
												{
													$purchase_f+= $ros->amount;  //print_r( $amountede);
												}
												$purchase_g = $purchase_e - $purchase_f;
												if(($ros->acc_type =='33')&&($ros->type =='income'))
												{
													$purchase_h+= $ros->amount;
												}
												if(($ros->acc_type =='33')&&($ros->type =='expense'))
												{
													$purchase_i+= $ros->amount;
												}
												$purchase_j = $purchase_h - $purchase_i  ;
												if(($ros->acc_type =='34')&&($ros->type =='expense'))
												{
													$purchase_c+= $ros->amount;
												}
												if(($ros->acc_type =='35')&&($ros->type =='expense'))
												{
													$purchase_d+= $ros->amount;
												}
												
											}
										}
										$total_operate = $purchase_d + $purchase_c + $purchase_j + $purchase_g + $purchase_b + $purchase_a;
										
									?>
									<tr>
										<td>
											<?php echo $this->db->get_where('accounts',array('type' =>'31'))->row()->Name;?>
										</td>
										<?php if($purchase_a)
										{?>
											<td class="text-right"><?=$purchase_a;?></td>
										<?php 
										} 
										else
										{?>
											<td class="text-right">0.00</td>
										<?php 
										} ?>
									</tr>
								</tbody>
							</table>
							<table class="table">
								<thead class="thead-light">
									<tr>
										<th style="text-align:left; background-color: #165082;"><b>Gross Profit</b></th>
										<?php 
											$gross_profit =  $p_inco - $purchase_sold;
											//print_r($gross_profit);
											if($p_inco)
											{ 
												if($gross_profit > 0)
												{
													$percentage = $gross_profit / $p_inco ;
												}
												else
												{
													$percentage = 0;
												}
											}
											else
											{
											$percentage = 0;
											}
											$percent=  $percentage * 100;
										?>
										<th class="right_align" style=" background-color: #165082;"><?php echo $this->sma->formatQuantity($gross_profit);?></th>
									</tr>
									<tr>
										<th style="text-align:left; background-color: #165082;color: #d2cece;"><b>As a percentage of Total Income</b></th>
										<th class="right_align" style=" background-color: #165082;color: #d2cece;"><?php echo $this->sma->formatQuantity($percent);?>%</th>
									</tr>
								</thead>
							</table>
							<table class="table">
								<thead class="thead-light">
								</thead>
								<tbody>
									<tr>
										<td>
											<?php echo $this->db->get_where('accounts',array('type' =>'30'))->row()->Name;?>
										</td>
										<?php if($purchase_b)
										{?>
											<td class="text-right"><?=$purchase_b;?></td>
										<?php 
										} 
										else
										{?>
											<td class="text-right">0.00</td>
										<?php 
										} ?>
									</tr>
									<tr>
										<td>
											<?php echo $this->db->get_where('accounts',array('type' =>'32'))->row()->Name;?>
										</td>
										<?php if($purchase_g)
										{?>
											<td class="text-right"><?=$purchase_g;?></td>
										<?php 
										} 
										else
										{?>
											<td class="text-right">0.00</td>
										<?php 
										} ?>
									</tr>
									<tr>
										<td>
											<?php echo $this->db->get_where('accounts',array('type' =>'33'))->row()->Name;?>
										</td>
										<?php if($purchase_j)
										{?>
											<td class="text-right"><?=$purchase_j;?></td>
										<?php 
										} 
										else
										{?>
											<td class="text-right">0.00</td>
										<?php 
										} ?>
									</tr>
									<tr>
										<td>
											<?php echo $this->db->get_where('accounts',array('type' =>'34'))->row()->Name;?>
										</td>
										<?php if($purchase_c)
										{?>
											<td class="text-right"><?=$purchase_c;?></td>
										<?php 
										} 
										else
										{?>
											<td class="text-right">0.00</td>
										<?php 
										} ?>
									</tr>
									<tr>
										<td>
											<?php echo $this->db->get_where('accounts',array('type' =>'35'))->row()->Name;?>
										</td>
										<?php if($purchase_d)
										{?>
											<td class="text-right"><?=$purchase_d;?></td>
										<?php 
										} 
										else
										{?>
											<td class="text-right">0.00</td>
										<?php 
										} ?>
									</tr>
									<tr class="net_value">
										<td><b>Total Operating Expenses</b></td>
										<?php if($total_operate)
										{?>
											<td class="text-right"><?=$total_operate;?></td>
										<?php 
										} 
										else
										{?>
											<td class="text-right">0.00</td>
										<?php 
										} ?>
										
									</tr>
								</tbody>
							</table>
							<table class="table">
								<thead class="thead-light">
									<tr>
										<th style="text-align:left; background-color: #165082;"><b>Net Profit</b></th>
										<?php 
											$cross_profit = $gross_profit - $operating;
											if($p_inco)
											{
												if($cross_profit > 0)
												{
													$cros_percentage = $cross_profit /  $p_inco;
												}
												else 
												{
													$cros_percentage = 0; 
												}
											}
											else
											{
												$cros_percentage = 0;
											}
											$cros_percent = $cros_percentage * 100;
										?>
										<th class="right_align" style=" background-color: #165082;"><?php echo $this->sma->formatQuantity($cross_profit);?></th>
									</tr>
									<tr>
										<th style="text-align:left; background-color: #165082;color: #d2cece;"><b>As a percentage of Total Income</b></th>
										<th class="right_align" style=" background-color: #165082;color: #d2cece;"><?php echo $this->sma->formatQuantity($cros_percent);?>%</th>
									</tr>
								</thead>
							</table>
						</div>
					</div>
				</div> 
				</page>
			</div>
        </div>
    </div>
</div>
<script>
  function Tools(evt, cityName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
      tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
      tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
    document.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
  }
</script>