<?php require_once('../auth/auth.php');require_once('../auth/function.php');?>
<?php
 $gbid   = $_GET['bid'];
 $gbrid  = $_GET['brid'];
 $fdate  = $_GET['fdate'];
 $edate  = $_GET['edate'];


$inacc= mysqli_query($conn,"SELECT co_name,owner_name,co_phone,co_email,co_address,co_logo FROM company WHERE coid='$coid' LIMIT 1") or die(mysqli_error($conn));
$inac= mysqli_fetch_assoc($inacc);

$co_name        = $inac['co_name'];
$owner_name     = $inac['owner_name'];
$co_address     = $inac['co_address'];
$co_phone       = $inac['co_phone'];
$co_email       = $inac['co_email'];
$co_logo        = $inac['co_logo']; 
$invpur         = 0;

                   // For Stock Query Start //

$stock = mysqli_fetch_assoc(mysqli_query($conn,"SELECT g.ac_g_catid,
  SUM(COALESCE(CASE WHEN t.dr_gcatid = g.ac_g_catid AND date(date)<'$fdate' THEN amount END,0))-SUM(COALESCE(CASE WHEN t.cr_gcatid = g.ac_g_catid AND date(date)<'$fdate' THEN amount END,0)) opstock,
  SUM(COALESCE(CASE WHEN t.dr_gcatid = g.ac_g_catid THEN amount END,0))-SUM(COALESCE(CASE WHEN t.cr_gcatid = g.ac_g_catid THEN amount END,0)) clstock
 FROM transaction t LEFT JOIN ac_sub_g_category g ON (t.dr_gcatid=g.ac_g_catid ||t.cr_gcatid=g.ac_g_catid) WHERE t.coid='$coid' AND t.bid='$gbid' AND t.brid='$gbrid' AND t.active=1 AND date(date)<='$edate' AND g.g_type='invtr' GROUP BY g.ac_g_catid"));
$opstock = ($stock)?$stock['opstock']:0;
$clstock = ($stock)?$stock['clstock']:0;

                   // For Stock Query End //


                   // For All Others Query Start //
function abcdf($conn,$coid,$gbid,$gbrid,$fdate,$edate,$gtype) {

  $result = mysqli_fetch_assoc(mysqli_query($conn,"SELECT g.ac_g_catid,
    SUM(COALESCE(CASE WHEN t.dr_gcatid = g.ac_g_catid THEN amount END,0))-SUM(COALESCE(CASE WHEN t.cr_gcatid = g.ac_g_catid THEN amount END,0)) amount
   FROM transaction t LEFT JOIN ac_sub_g_category g ON (t.dr_gcatid=g.ac_g_catid ||t.cr_gcatid=g.ac_g_catid) WHERE t.coid='$coid' AND t.bid='$gbid' AND t.brid='$gbrid' AND t.active=1 AND date(date) BETWEEN '$fdate' AND '$edate' AND g.g_type='$gtype' GROUP BY g.ac_g_catid"));
  $gcatid = ($result)?$result['ac_g_catid']:0;
  $amount = ($result)?$result['amount']:0;

  return compact('gcatid', 'amount');
}
                   // For All Others Query End //

             // For Sales Query Start //
$slrow = abcdf($conn,$coid,$gbid,$gbrid,$fdate,$edate,'sl-inc');
$sl_gcatid = $slrow['gcatid'];
$slamount  = (-$slrow['amount']);
             // For Sales Query End //

             // For COGS Query Start //
$cgrow = abcdf($conn,$coid,$gbid,$gbrid,$fdate,$edate,'cs-gds');
$cg_gcatid  = $cgrow['gcatid'];
$tcostgods  = $cgrow['amount'];
             // For COGS Query End //

             // For Direct Income Start //
$drinrow = abcdf($conn,$coid,$gbid,$gbrid,$fdate,$edate,'dr-inc');
$drinc_gcatid = $drinrow['gcatid'];
$drincome     = (-$drinrow['amount']);
             // For Direct Income End //


             // For Direct Expense Query Start //
$drxnrow = abcdf($conn,$coid,$gbid,$gbrid,$fdate,$edate,'dr-exp');
$drexp_gcatid = $drxnrow['gcatid'];
$drexpense    = $drxnrow['amount'];
             // For Direct Expense Query End //

             // For Direct Expense Query Start //
$indrxrow = abcdf($conn,$coid,$gbid,$gbrid,$fdate,$edate,'in-exp');
$indrexp_gcatid = $indrxrow['gcatid'];
$indrexpense    = $indrxrow['amount'];
             // For Direct Expense Query End //

             // For Salary Expense Query Start //
$slxrow = abcdf($conn,$coid,$gbid,$gbrid,$fdate,$edate,'sl-exp');
$slexp_gcatid = $slxrow['gcatid'];
$slexpense    = $slxrow['amount'];
             // For Salary Expense Query End //

             // For Direct Expense Query Start //
$indrinrow = abcdf($conn,$coid,$gbid,$gbrid,$fdate,$edate,'in-inc');
$indrinc_gcatid = $indrinrow['gcatid'];
$indrincome     = (-$indrinrow['amount']);
             // For Direct Expense Query End //

             // For Gross Profit Query Start //
$exptotal        = ($tcostgods+$drexpense+$indrexpense+$slexpense);
$inctotal        = ($slamount+$drincome+$indrincome);
             // For Gross Profit Query End //

             // For Net Profit Query Start //
$profitloss     = ($inctotal-$exptotal);
$netprofit      = ($profitloss>0)?($profitloss):0;
$netloss        = ($profitloss<0)?(-$profitloss):0;
             // For Net Profit Query End //
?>
<section class="content">
   <div class="row">
      <div class="col-sm-12">
         <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
            <div class="card-header" style="padding: 3px 8px;">
               <h3 class="card-title">Profit & Loss View</h3>
               <div class="card-tools">
                  <span id="sdf"></span>
                  <button type="button" onclick="pdfMe('#prlss');" class="btn btn-tool" title="Download Pdf"><i class="fas fa-download"></i></button>
                  <button type="button" class="btn btn-tool" onclick="printMe('#prlss');" title="Print Now"><i class="fas fa-print"></i></button>
                  <button type="button" class="btn btn-tool" data-card-widget="remove">
                  <i class="fas fa-times"></i>
                  </button>
               </div>
            </div>
            <div class="card-body p-1 table-responsive cscroll" id="prlss" style="height: 500px;">
             <input type="hidden" id="vounameset" value="Profit & Loss Report <?php echo date('d/M/y', strtotime($fdate));?> To <?php echo date('d/M/y', strtotime($edate));?> ">

               <div class="row d-flex">
                  <div class="col-sm-12">
                     <table class="ptable" cellpadding="0" cellspacing="0">
                        <thead>
                           <tr>
                              <th rowspan="3" class="thborder" style="width:10%;">
                                   <?php if($co_logo != ""): ?>
                        <img id="img_data" class="profile-user-img img-fluid img-circle" style="width:80px;height:77px;" src="data:image/gif;base64,<?php echo $co_logo;?>" alt="User profile picture">
                        <?php else : ?>
                        <img id="img_data" class="profile-user-img img-fluid img-circle" style="width:80px;height:77px;" src="../../dist/img/no_img.jpg" alt="company profile">
                        <?php endif; ?>
                              </th>
                              <th rowspan="3" class="thborder" style="width:70%;">
                                    <dt style="font-size:18px;font-weight: 600;"><?php echo $co_name;?></dt>
                                    <dt><?php echo $owner_name;?></dt>
                                    <dt><?php echo $co_address;?></dt>
                                    <dt><?php echo $co_phone;?>#<?php echo $co_email;?></dt>
                              </th>
                              <th class="thborder text-center" style="width:20%;"><?php echo language(307)?></th>
                           </tr>
                           <tr>
                              <th class="thborder text-center"><?php echo language(181)?></th>
                           </tr>
                           <tr>
                              <th class="thborder text-center"><?php echo date('d/m/y', strtotime($fdate));?> <?php echo language(291)?> <?php echo date('d/m/y', strtotime($edate));?>
                              </th>
                           </tr>
                        </thead>
                     </table>
                  </div>
                  <div class="col-sm-6 pr-0">
                     <table class="ptable" cellpadding="0" cellspacing="0">
                        <thead>
                           <tr>
                              <th style="width:90%;border-bottom:1px solid #969c9d;"><?php echo language(295)?></th>
                              <th class="text-right" style="width:10%;border-bottom:1px solid #969c9d;"><?php echo language(163)?></th>
                           </tr>
                           <tr>
                              <th colspan="2" style="width:90%;border-bottom:1px solid #969c9d;"><?php echo language(230)?></th>
                           </tr>
                        </thead>
                        <tbody>
                           <tr>
                             <td <?php if($cg_gcatid>0){ echo'onclick="sbtbl(this)"';}?> gcatid="<?php echo $cg_gcatid;?>">
                                 <div data-toggle="collapse" href="#sbtbl<?php echo $cg_gcatid;?>">
                                  <dt><?php echo language(385)?> <?php if($cg_gcatid>0){ echo'<i class="fas fa-caret-left float-right"></i>';}?></dt>
                                  <div class="collapse" id="sbtbl<?php echo $cg_gcatid;?>"></div>
                                </div> 
                              </td>
                              <td class="text-right">
                                 <dt><?php echo money($tcostgods,true);?></dt>
                              </td>
                           </tr>
                           <tr>
                              <td <?php if($drexp_gcatid>0){ echo'onclick="sbtbl(this)"';}?> gcatid="<?php echo $drexp_gcatid;?>">
                                 <div data-toggle="collapse" href="#sbtbl<?php echo $drexp_gcatid;?>">
                                  <dt><?php echo language(284)?> <?php if($drexp_gcatid>0){ echo'<i class="fas fa-caret-left float-right"></i>';}?></dt>
                                  <div class="collapse" id="sbtbl<?php echo $drexp_gcatid;?>"></div>
                                </div> 
                              </td>
                              <td class="text-right">
                                 <dt><?php echo money($drexpense,true);?></dt>
                              </td>
                           </tr>
                           <tr>
                            <td <?php if($indrexp_gcatid>0){ echo'onclick="sbtbl(this)"';}?> gcatid="<?php echo $indrexp_gcatid;?>">
                                <div data-toggle="collapse" href="#sbtbl<?php echo $indrexp_gcatid;?>">
                                  <dt><?php echo language(286)?> <?php if($indrexp_gcatid>0){ echo'<i class="fas fa-caret-left float-right"></i>';}?></dt>
                                  <div class="collapse" id="sbtbl<?php echo $indrexp_gcatid;?>"></div>
                                </div> 
                              </td>
                              <td class="text-right">
                                 <dt><?php echo money($indrexpense,true);?></dt>
                              </td>
                           </tr>
                           <tr>
                            <td <?php if($slexp_gcatid>0){ echo'onclick="sbtbl(this)"';}?> gcatid="<?php echo $slexp_gcatid;?>">
                                <div data-toggle="collapse" href="#sbtbl<?php echo $slexp_gcatid;?>">
                                  <dt><?php echo language(435)?> <?php if($slexp_gcatid>0){ echo'<i class="fas fa-caret-left float-right"></i>';}?></dt>
                                  <div class="collapse" id="sbtbl<?php echo $slexp_gcatid;?>"></div>
                                </div> 
                              </td>
                              <td class="text-right">
                                 <dt><?php echo money($slexpense,true);?></dt>
                              </td>
                           </tr>
                           <tr>
                              <td colspan="2">&nbsp;</td>
                           </tr>
                           <tr>
                              <td>
                                 <dt><?php echo language(140)?></dt>
                              </td>
                              <td class="text-right" style="border-top:1px solid #969c9d;border-bottom:1px solid #969c9d;">
                                 <dt><?php echo money($exptotal,true);?></dt>
                              </td>
                           </tr>
                           <tr>
                              <td colspan="2">&nbsp;</td>
                           </tr>
                        </tbody>
                        <tfoot>
                           <tr>
                              <th style="border-top:1px solid #969c9d;"><?php echo language(305)?></th>
                              <th class="text-right" style="border-top:1px solid #969c9d;"><?php echo money($netloss,true);?></th>
                           </tr>
                        </tfoot>
                     </table>
                  </div>
                  <div class="col-sm-6 pl-0">
                     <table class="ptable" cellpadding="0" cellspacing="0">
                        <thead>
                           <tr>
                              <th style="width:90%;border-bottom:1px solid #969c9d;"><?php echo language(295)?></th>
                              <th class="text-right" style="width:10%;border-bottom:1px solid #969c9d;"><?php echo language(163)?></th>
                           </tr>
                           <tr>
                              <th colspan="2" style="width:90%;border-bottom:1px solid #969c9d;"><?php echo language(229)?></th>
                           </tr>
                        </thead>
                        <tbody>
                           <tr>
                              <td <?php if($sl_gcatid>0){ echo'onclick="sbtbl(this)"';}?> gcatid="<?php echo $sl_gcatid;?>">
                                <div data-toggle="collapse" href="#sbtbl<?php echo $sl_gcatid;?>">
                                  <dt><?php echo language(302)?> <?php if($sl_gcatid>0){ echo'<i class="fas fa-caret-left float-right"></i>';}?></dt>
                                  <div class="collapse" id="sbtbl<?php echo $sl_gcatid;?>"></div>
                                </div> 
                              </td>
                              <td class="text-right">
                                 <dt><?php echo money($slamount,true);?></dt>
                              </td>
                           </tr>
                           <tr>
                              <td <?php if($drinc_gcatid>0){ echo'onclick="sbtbl(this)"';}?> gcatid="<?php echo $drinc_gcatid;?>">
                                <div data-toggle="collapse" href="#sbtbl<?php echo $drinc_gcatid;?>">
                                  <dt><?php echo language(303)?> <?php if($drinc_gcatid>0){ echo'<i class="fas fa-caret-left float-right"></i>';}?></dt>
                                  <div class="collapse" id="sbtbl<?php echo $drinc_gcatid;?>"></div>
                                </div>                                 
                              </td>
                              <td class="text-right">
                                 <dt><?php echo money($drincome,true);?></dt>
                              </td>
                           </tr>
                           <tr>
                            <td <?php if($indrinc_gcatid>0){ echo'onclick="sbtbl(this)"';}?> gcatid="<?php echo $indrinc_gcatid;?>">
                                <div data-toggle="collapse" href="#sbtbl<?php echo $indrinc_gcatid;?>">
                                  <dt><?php echo language(306)?> <?php if($indrinc_gcatid>0){ echo'<i class="fas fa-caret-left float-right"></i>';}?></dt>
                                  <div class="collapse" id="sbtbl<?php echo $indrinc_gcatid;?>"></div>
                                </div>                                
                              </td>
                              <td class="text-right">
                                 <dt><?php echo money($indrincome,true);?></dt>
                              </td>
                           </tr>
                           <tr>
                              <td colspan="2">&nbsp;</td>
                           </tr>
                           <tr>
                              <td colspan="2">&nbsp;</td>
                           </tr>                           
                           <tr>
                              <td>
                                 <dt><?php echo language(140)?></dt>
                              </td>
                              <td class="text-right" style="border-top:1px solid #969c9d;border-bottom:1px solid #969c9d;">
                                 <dt><?php echo money($inctotal,true);?></dt>
                              </td>
                           </tr>
                           <tr>
                              <td colspan="2">&nbsp;</td>
                           </tr>
                        </tbody>
                        <tfoot>
                           <tr>
                              <th style="border-top:1px solid #969c9d;"><?php echo language(285)?></th>
                              <th class="text-right" style="border-top:1px solid #969c9d;"><?php echo money($netprofit,true);?></th>
                           </tr>
                        </tfoot>
                     </table>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>
</section>

<script>
 function sbtbl(sid) {
  var gcatid = $(sid).attr('gcatid');
  var viewid = $(sid).find("#sbtbl"+gcatid);
     $(".preloader").css('display','flex');
     $(viewid).load("../display_report/sub_table.php",
     ({
        gcatid: gcatid,
        gbid:'<?php echo $gbid;?>',
        gbrid:'<?php echo $gbrid;?>',
        fdate:'<?php echo $fdate;?>',
        edate:'<?php echo $edate;?>'
    }),
    function() {
     $('.preloader').hide(); 
     $(sid).removeAttr("onclick");
   });
 }
</script>