<?php require_once('auth.php'); ?>
<section class="content" style="padding-bottom:28px;">
  <ul class="nav nav-pills" style="background-color: honeydew;padding: 2px 0;border-bottom:1px solid #dee2e6;margin-left:2px;position:fixed;z-index:1030;width:100%;">
    <?php if (per('daybook') == 1) { ?>
      <li class="nav-item"><a style="padding: 0px 8px;" class="nav-link" href="#" data-toggle="tab" onclick="pagecall('#','#dashview');"><i class="fa fa-fw fa-calculator"></i><?php echo language(27); ?></a></li>|
    <?php } ?>
    <?php if (per('d_history') == 1) { ?>
      <li class="nav-item"><a style="padding: 0px 8px;" class="nav-link" href="#" data-toggle="tab" onclick="pagecall('../auth/d_summary.php','#dashview');"><i class="fa  fa-fw fa-calculator"></i><?php echo language(28); ?></a></li>|
    <?php } ?>
    <?php if (per('recyle_bin') == 1) { ?>
      <li class="nav-item"><a style="padding: 0px 8px;" class="nav-link" href="#" data-toggle="tab" onclick="pagecall('#','#dashview');"><i class="fa fa-fw fa-calculator"></i><?php echo language(29); ?></a></li>|

    <?php } ?>
    <?php if (per('bluk_sms') == 1) { ?>
      <li class="nav-item"><a style="padding: 0px 8px;" class="nav-link" href="#" data-toggle="tab" onclick="pagecall('../sms/','#dashview');"><i class="fa fa-fw fa-calculator"></i><?php echo language(30); ?></a></li>|
    <?php } ?>
    <?php if (per('visitor') == 1) { ?>
      <li class="nav-item"><a style="padding: 0px 8px;" class="nav-link" data-toggle="tab" href="#"><i class="fa fa-fw fa-book"></i> <?php echo language(169); ?> </a></li>|
    <?php } ?>


    <li class="nav-item"><a style="padding: 0px 8px;" class="nav-link" data-toggle="tab" onclick="pagecall('../auth/default_ac.php','#dashview');" href="#"><i class="fa fa-fw fa-compass"></i> <?php echo language(308); ?> </a></li>|

    <li class="nav-item"><a style="padding: 0px 8px;" class="nav-link" target="_blank" href="https://download.anydesk.com/AnyDesk.exe" onclick="return confirm('are you sure you want to download Anydesk for Windows?');"><i class="fa fa-fw fa-download"></i> <?php echo language(31); ?> </a></li>|
    <li class="nav-item"><a style="padding: 0px 8px;" class="nav-link" target="_blank" href="https://download.teamviewer.com/download/TeamViewer_Setup_x64.exe" onclick="return confirm('are you sure you want to download TeamViewer for Windows?');"><i class="fa fa-fw fa-download"></i> <?php echo language(378); ?> </a></li>|

    <li class="nav-item"><a style="padding: 0px 8px;" class="nav-link" target="_blank" data-toggle="tab" href="#"><i class="fa fa-fw fa-video"></i> <?php echo language(12); ?> </a></li>|

  </ul>
</section>
<section class="content" id="dashview">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-12 mb-2 d-flex">
        <span class="text-white bg-navyblue p-1">Notice:</span>
        <marquee behavior="scroll" bgcolor="003d54" class="p-1 text-white lanbn">
          <?php
          $notice_query = mysqli_query($conn, "SELECT * FROM notice_board  WHERE status=1 AND coid='$coid' AND brid='$brid' AND '$date' < end_date  ORDER BY end_date DESC ");
          while ($row_not = mysqli_fetch_array($notice_query)) {
            $getbrid = $row_not['brid'];
          ?>
            <i class="fa fa-spinner" aria-hidden="true"></i>
            <?php if ($getbrid == $brid) { ?>
              <span> <?php echo $row_not['notice'];  ?> <i class="fa fa-question-circle"></i> </span>
            <?php } else {   ?>
              <span> <?php echo $row_not['notice']; ?></span>
          <?php }
          } ?> ** আমাদের সাথে থাকার জন্য ধন্যবাদ **
        </marquee>
      </div>
    </div>



    <?php if (per('dash2_layout_show') !== 1) { ?>


      <div class="row">
        <!-- Cash In Hand START -->

        <div class="col-12 col-sm-6 col-md-3">
          <div class="info-box">
            <span class="info-box-icon bg-green elevation-1"><i class="fa-solid fa-money-bill-1"></i></span>
            <div class="info-box-content">
              <span class="info-box-text text-bold text-md pl-1"><?php echo language(297); ?></span>
              <span class="info-box-number pl-1">
                <?php
                if ($administrator == 'primary') {
                  $cash_query = mysqli_query($conn, "SELECT balance FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ac_type='cash' LIMIT 1 ") or die(mysqli_error($conn));
                } else {
                  $cze = mysqli_fetch_assoc(mysqli_query($conn, "SELECT SUM(cash_amount),SUM(bank_amount) FROM cash_zero WHERE coid='$coid' AND uid='$uid'"));
                  $cash_query = mysqli_query($conn, "SELECT SUM(COALESCE(CASE WHEN t.dr_acid=a.acid THEN amount END,0))-SUM(COALESCE(CASE WHEN t.cr_acid=a.acid THEN amount END,0)) balance FROM transaction t INNER JOIN accounts a ON (t.dr_acid=a.acid || t.cr_acid=a.acid) WHERE t.coid='$coid' AND t.bid='$bid' AND t.brid='$brid' AND t.active=1 AND (t.dr_acid=a.acid || t.cr_acid=a.acid) AND a.ac_type='cash' AND t.userid='$uid'") or die(mysqli_error($conn));
                }
                $rowcash = mysqli_fetch_assoc($cash_query);
                echo @number_format((@$rowcash['balance'] - @$cze['SUM(cash_amount)']), $decimel); ?>
                <small class="text-666"><?php echo $currency; ?></small>
              </span>
            </div>
          </div>
        </div>
        <!-- Cash In Hand END -->


        <!-- BANK Balance START -->
        <div class="col-12 col-sm-6 col-md-3">
          <div class="info-box mb-3">
            <span class="info-box-icon bg-danger elevation-1"><i class="fa fa-university"></i></span>
            <div class="info-box-content">
              <span class="info-box-text text-bold text-md pl-1">Total Expences</span>
              <span class="info-box-number pl-1">0
                <small class="text-666"><?php echo $currency; ?></small>
              </span>
            </div>
          </div>
        </div>
        <!-- BANK Balance END -->
        <div class="clearfix hidden-md-up"></div>

        <!-- Received Balance END -->
        <div class="col-12 col-sm-6 col-md-3">
          <div class="info-box mb-3">
            <span class="info-box-icon bg-navyblue text-white elevation-1"><i class="fa-solid fa-hand-holding-dollar"></i></span>
            <div class="info-box-content">
              <span class="info-box-text text-bold text-md pl-1"><?php echo language(299); ?></span>
              <span class="info-box-number pl-1">
                <?php
                if ($administrator == 'primary') {
                  $received_query = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype in('R','R_I','R_R','W_R') AND date(date) LIKE '$date' AND active=1") or die(mysqli_error($conn));
                } else {
                  $received_query = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype in('R','R_I','R_R','W_R') AND date(date) LIKE '$date' AND active=1 AND userid='$uid'") or die(mysqli_error($conn));
                }
                $rres = mysqli_fetch_assoc($received_query);
                echo @number_format(@$rres['sum(amount)'], $decimel);
                ?>
                <small class="text-666"><?php echo $currency; ?></small>
              </span>
            </div>
          </div>
        </div>
        <!-- Received Balance END -->
        <div class="col-12 col-sm-6 col-md-3">
          <div class="info-box mb-3">
            <span class="info-box-icon bg-warning elevation-1"><i class="fa-solid fa-money-bill-trend-up"></i>
            </span>
            <div class="info-box-content">
              <span class="info-box-text text-bold text-md pl-1"><?php echo language(300); ?></span>
              <span class="info-box-number pl-1">
                <?php
                if ($administrator == 'primary') {
                  $payment_query = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype='P' AND date(date) LIKE '$date' AND active=1") or die(mysqli_error($conn));
                } else {
                  $payment_query = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype='P' AND date(date) LIKE '$date' AND active=1 AND userid='$uid'") or die(mysqli_error($conn));
                }
                $rpay = mysqli_fetch_assoc($payment_query);
                echo @number_format(@$rpay['sum(amount)'], $decimel);
                ?>
                <small class="text-666"><?php echo $currency; ?></small>
              </span>
            </div>
          </div>
        </div>


      </div>

    <?php } else { ?>

      <div class="row">

        <div class="col-md-6">
          <div class="row">
            <div class="col-md-6">
              <div class="info-box">
                <span class="info-box-icon bg-green elevation-1"><i class="fa-solid fa-money-bill-1"></i></span>
                <div class="info-box-content">
                  <span class="info-box-text text-bold text-md pl-1"><?php echo language(297); ?></span>
                  <span class="info-box-number pl-1">
                    <?php
                    if ($administrator == 'primary') {
                      $cash_query = mysqli_query($conn, "SELECT balance FROM accounts WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ac_type='cash' LIMIT 1 ") or die(mysqli_error($conn));
                    } else {
                      $cze = mysqli_fetch_assoc(mysqli_query($conn, "SELECT SUM(cash_amount),SUM(bank_amount) FROM cash_zero WHERE coid='$coid' AND uid='$uid'"));
                      $cash_query = mysqli_query($conn, "SELECT SUM(COALESCE(CASE WHEN t.dr_acid=a.acid THEN amount END,0))-SUM(COALESCE(CASE WHEN t.cr_acid=a.acid THEN amount END,0)) balance FROM transaction t INNER JOIN accounts a ON (t.dr_acid=a.acid || t.cr_acid=a.acid) WHERE t.coid='$coid' AND t.bid='$bid' AND t.brid='$brid' AND t.active=1 AND (t.dr_acid=a.acid || t.cr_acid=a.acid) AND a.ac_type='cash' AND t.userid='$uid'") or die(mysqli_error($conn));
                    }
                    $rowcash = mysqli_fetch_assoc($cash_query);
                    echo @number_format((@$rowcash['balance'] - @$cze['SUM(cash_amount)']), $decimel); ?>
                    <small class="text-666"><?php echo $currency; ?></small>
                  </span>
                </div>
              </div>
            </div>
            <div class="col-md-6">
              <div class="info-box mb-3">
                <span class="info-box-icon bg-danger elevation-1"><i class="fa fa-university"></i></span>
                <div class="info-box-content">
                  <span class="info-box-text text-bold text-md pl-1">Total Expences</span>
                  <span class="info-box-number pl-1">0
                    <small class="text-666"><?php echo $currency; ?></small>
                  </span>
                </div>
              </div>
            </div>
          </div>

          <div class="row">
            <div class="col-md-6">
              <div class="info-box mb-3">
                <span class="info-box-icon bg-navyblue text-white elevation-1"><i class="fa-solid fa-hand-holding-dollar"></i></span>
                <div class="info-box-content">
                  <span class="info-box-text text-bold text-md pl-1"><?php echo language(299); ?></span>
                  <span class="info-box-number pl-1">
                    <?php
                    if ($administrator == 'primary') {
                      $received_query = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype in('R','R_I','R_R','W_R') AND date(date) LIKE '$date' AND active=1") or die(mysqli_error($conn));
                    } else {
                      $received_query = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype in('R','R_I','R_R','W_R') AND date(date) LIKE '$date' AND active=1 AND userid='$uid'") or die(mysqli_error($conn));
                    }
                    $rres = mysqli_fetch_assoc($received_query);
                    echo @number_format(@$rres['sum(amount)'], $decimel);
                    ?>
                    <small class="text-666"><?php echo $currency; ?></small>
                  </span>
                </div>
              </div>
            </div>
            <div class="col-md-6">
              <div class="info-box mb-3">
                <span class="info-box-icon bg-warning elevation-1"><i class="fa-solid fa-money-bill-trend-up"></i>
                </span>
                <div class="info-box-content">
                  <span class="info-box-text text-bold text-md pl-1"><?php echo language(300); ?></span>
                  <span class="info-box-number pl-1">
                    <?php
                    if ($administrator == 'primary') {
                      $payment_query = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype='P' AND date(date) LIKE '$date' AND active=1") or die(mysqli_error($conn));
                    } else {
                      $payment_query = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype='P' AND date(date) LIKE '$date' AND active=1 AND userid='$uid'") or die(mysqli_error($conn));
                    }
                    $rpay = mysqli_fetch_assoc($payment_query);
                    echo @number_format(@$rpay['sum(amount)'], $decimel);
                    ?>
                    <small class="text-666"><?php echo $currency; ?></small>
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="col-md-6">



          <section class="col-md-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"><?php echo 'Today Summary'; ?> </h3>
                <div class="card-tools">
                  <button type="button" class="btn btn-tool" onclick="ExcelMe('#d_view')" title="Download Excel">
                    <i class="far fa-file-excel"></i>
                  </button>
                  <button type="button" onclick="BfpdfMe('#d_view');" class="btn btn-tool" title="Download Pdf"><i class="fas fa-download"></i></button>
                  <button type="button" class="btn btn-tool" onclick="printMe('#d_view','header')" title="Print Now"><i class="fas fa-print"></i></button>
                  <button type="button" class="btn btn-tool" data-card-widget="remove" data-dismiss="modal"><i class="fas fa-times"></i></button>
                </div>
              </div>
              <div class="card-body p-1" style="height: 240px;">
                <section class="col-md-12" style="border:1px solid #969c9d;background: hsla(0,0%,50.2%,.11);border-radius: 5px;">
                  <div class="row mt-1 mb-n2">
                    <?php if ($bid_check > 1 || $multi_bid > 1) { ?>
                      <div class="col-md-4">
                        <div class="form-group">
                          <label> <?php echo language(231); ?> </label>
                          <select id="bbid" class="form-control select2" style="width: 100%" onchange="lives();">
                            <option selected value="<?php echo $bid; ?>">
                              <?php echo $bid_name = mysqli_fetch_assoc(mysqli_query($conn, "SELECT bid_name FROM business WHERE bid='$bid' LIMIT 1 "))['bid_name'];
                              ?>
                            </option>
                            <option value="0"> <?php echo language(222); ?> </option>
                            <?php
                            $prr = mysqli_query($conn, "SELECT bid,bid_name FROM business WHERE coid='$coid'") or die(mysqli_error($conn));
                            while ($prw = mysqli_fetch_assoc($prr)) {
                              echo '<option value=' . $prw["bid"] . '>' . $prw["bid_name"] . '</option>';
                            } ?>
                          </select>
                        </div>
                      </div>
                    <?php } else { ?>
                      <input type="hidden" id="bbid" value="<?php echo $bid; ?>">
                    <?php }
                    if ($brid_check > 1 ||  $multi_brid > 1) { ?>
                      <div class="col-md-4">
                        <div class="form-group">
                          <label> <?php echo language(232); ?> </label>
                          <select id="bbrid" class="form-control select2" style="width: 100%" required="true" onchange="lives();">
                            <option selected value="<?php echo $brid; ?>">
                              <?php echo $branch_name = mysqli_fetch_assoc(mysqli_query($conn, "SELECT branch_name FROM branch WHERE bid='$bid'  LIMIT 1 "))['branch_name'];
                              ?>
                            </option>
                            <option value="0"> <?php echo language(222); ?> </option>
                            <?php
                            $prr = mysqli_query($conn, "SELECT brid,branch_name FROM branch WHERE coid='$coid' AND bid='$bid'") or die(mysqli_error($conn));
                            while ($prw = mysqli_fetch_assoc($prr)) {
                              echo '<option value=' . $prw["brid"] . '>' . $prw["branch_name"] . '</option>';
                            } ?>
                          </select>
                        </div>
                      </div>
                    <?php } else { ?>
                      <input type="hidden" id="bbrid" value="<?php echo $brid; ?>">
                    <?php } ?>

                    <div class="col-md-4">
                      <div class="form-group">
                        <label><?php echo language(257); ?></label>
                        <input type="text" id="fdate" class="form-control form-control-sm text-center" onmouseover="lcdate(this)" readonly="true" required="true" value="<?php echo $date; ?>">
                      </div>
                    </div>
                    <div class="col-md-4">
                      <div class="form-group">
                        <label><?php echo language(258); ?></label>
                        <input type="text" id="edate" class="form-control form-control-sm text-center" onmouseover="lcdate(this)" readonly="true" required="true" value="<?php echo $date; ?>">
                      </div>
                    </div>
                  </div>

                  <div class="mt-1" id="d_view"></div>


                </section>
              </div>
          </section>
        </div>

      </div>
      <script type="text/javascript">
        $(document).ready(function() {
          $('select').select2();
          pagecall('../auth/d_summary_view.php?bbid=<?php echo $bid; ?>' + '&bbrid=<?php echo $brid; ?>' + '&fdate=<?php echo $date; ?>' + '&edate=<?php echo $date; ?>', '#d_view', 0);
        });

        function lives() {
          let bbid = $('#bbid').val();
          let bbrid = $('#bbrid').val();
          let fdate = $('#fdate').val();
          let edate = $('#edate').val();
          $.get("../auth/d_summary_view.php", {
              bbid: bbid,
              bbrid: bbrid,
              fdate: fdate,
              edate: edate
            },
            function(html) {
              $("#d_view").html(html);
            });
        }
        // FOR DATE PICKER //
        var timer = null;
        $('#fdate,#edate').change(function() {
          clearTimeout(timer);
          timer = setTimeout(lives, 100);
        });
      </script>
    <?php }  ?>

    <div class="row">
      <section class="col-md-4 connectedSortable">
        <div class="card direct-chat direct-chat-primary">
          <div class="card-header">
            <h3 class="card-title">Income & Expense</h3>
            <div class="card-tools">
              <button type="button" class="btn btn-tool" data-card-widget="collapse">
                <i class="fas fa-minus"></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" style="height: 250px;">
            <div class="col-md-12">
              <div class="row">
                <div class="col-md-8">
                  <div class="chart-responsive">
                    <canvas id="incomeChart" height="200"></canvas>
                  </div>
                </div>
                <div class="col-md-4">
                  <ul class="chart-legend clearfix mt-5">
                    <li><i class="far fa-circle" style="color:green;"></i> Income</li>
                    <li><i class="far fa-circle" style="color:darkorange;"></i> Expense</li>
                    <li><i class="far fa-circle" style="color:blue;"></i> Profit</li>
                    <li><i class="far fa-circle" style="color:Red;"></i> Loss</li>
                  </ul>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>
      <section class="col-md-4 connectedSortable p-0">
        <div class="card direct-chat direct-chat-primary">
          <div class="card-header">
            <h3 class="card-title">Transaction</h3>
            <div class="card-tools">
              <button type="button" class="btn btn-tool" data-card-widget="collapse">
                <i class="fas fa-minus"></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" style="height: 250px;">
            <div class="col-md-12">
              <canvas id="transactionChart" height="240" style="width:100%;"></canvas>
            </div>
          </div>
        </div>
      </section>
      <section class="col-md-4 connectedSortable">
        <div class="card direct-chat direct-chat-primary">
          <div class="card-header">
            <h3 class="card-title">Purchase & Sales</h3>
            <div class="card-tools">
              <button type="button" class="btn btn-tool" data-card-widget="collapse">
                <i class="fas fa-minus"></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" style="height: 250px;">
            <div class="col-md-12">
              <canvas id="prslChart" height="240" style="width:100%;"></canvas>
            </div>
          </div>
        </div>
      </section>
    </div>

    <?php if (per('dash_y_show') == 1) { ?>
      <div class="row mt-3">
        <section class="col-md-4 connectedSortable">
          <div class="card direct-chat direct-chat-primary">
            <div class="card-header">
              <h3 class="card-title"> <?php echo 'Inventory Stock'; ?></h3>
              <div class="card-tools">
                <button type="button" class="btn btn-tool" data-card-widget="collapse">
                  <i class="fas fa-minus"></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" style="height: 250px;">
              <div class="col-md-12">
                <table class="table table-sm">
                  <tbody>
                    <?php
                    $purchase_query = mysqli_query($conn, "SELECT MONTHNAME(`date`) as mname, YEAR(`date`) AS year,   SUM(`amount`) as total
                          FROM  `transaction` where coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype in ('COGS') AND active=1 GROUP BY  MONTH(`date`),YEAR(`date`) ORDER BY year DESC limit 12; ");
                    $i = 0;
                    while ($row_pur = mysqli_fetch_array($purchase_query)) {
                    ?>
                      <tr>
                        <td> <?php echo  ++$i; ?> </td>
                        <td> <?php echo  $row_pur['mname'] . ' - ' . $row_pur['year']; ?> </td>
                        <td class="text-right"> <?php echo  $row_pur['total']; ?> </td>
                      </tr>
                    <?php } ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </section>
        <section class="col-md-4 connectedSortable p-0">
          <div class="card direct-chat direct-chat-primary">
            <div class="card-header">
              <h3 class="card-title"><?php echo 'Purchase History'; ?></h3>
              <div class="card-tools">
                <button type="button" class="btn btn-tool" data-card-widget="collapse">
                  <i class="fas fa-minus"></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" style="height: 250px;">
              <div class="col-md-12">
                <table class="table table-sm">
                  <tbody>
                    <?php
                    $purchase_query = mysqli_query($conn, "SELECT MONTHNAME(`date`) as mname, YEAR(`date`) AS year,   SUM(`amount`) as total
                          FROM  `transaction` where coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype in ('EPUR','GPUR') AND active=1 GROUP BY  MONTH(`date`),YEAR(`date`) ORDER BY year DESC limit 12; ");
                    $i = 0;
                    while ($row_pur = mysqli_fetch_array($purchase_query)) {
                    ?>
                      <tr>
                        <td> <?php echo  ++$i; ?> </td>
                        <td> <?php echo  $row_pur['mname'] . ' - ' . $row_pur['year']; ?> </td>
                        <td class="text-right"> <?php echo  $row_pur['total']; ?> </td>
                      </tr>
                    <?php } ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </section>


        <section class="col-md-4 connectedSortable">
          <div class="card direct-chat direct-chat-primary">
            <div class="card-header">
              <h3 class="card-title"><?php echo 'Sales History'; ?> </h3>
              <div class="card-tools">
                <button type="button" class="btn btn-tool" data-card-widget="collapse">
                  <i class="fas fa-minus"></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" style="height: 250px;">
              <div class="col-md-12">
                <table class="table table-sm">
                  <tbody>
                    <?php
                    $sales_query = mysqli_query($conn, "SELECT MONTHNAME(`date`) as mname, YEAR(`date`) AS year,   SUM(`amount`) as total
                          FROM  `transaction` where coid='$coid' AND bid='$bid' AND brid='$brid' AND 
                          ttype in ('ECSL','ERSL','INSL') AND active=1 GROUP BY  MONTH(`date`),YEAR(`date`) ORDER BY year DESC limit 12; ");
                    $i = 0;
                    while ($row_sal = mysqli_fetch_array($sales_query)) {
                    ?>
                      <tr>
                        <td> <?php echo  ++$i; ?> </td>
                        <td> <?php echo  $row_sal['mname'] . ' - ' . $row_sal['year']; ?> </td>
                        <td class="text-right"> <?php echo  $row_sal['total']; ?> </td>
                      </tr>
                    <?php } ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </section>

      </div>
    <?php } else {
      echo "";
    } ?>

















  </div>
</section>





<?php


if ($administrator == 'primary') {
  $received_query2 = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype in('R','R_I','R_R','W_R') AND date(date) LIKE '$date' AND active=1") or die(mysqli_error($conn));
} else {
  $received_query2 = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype in('R','R_I','R_R','W_R')  AND date(date) LIKE '$date' AND active=1 AND userid='$uid'") or die(mysqli_error($conn));
}
$rres = mysqli_fetch_assoc($received_query2);
$ReceIved_chart = @$rres['sum(amount)'];




if ($administrator == 'primary') {
  $payment_query2 = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype='P' AND date(date) LIKE '$date' AND active=1") or die(mysqli_error($conn));
} else {
  $payment_query2 = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype='P' AND date(date) LIKE '$date' AND active=1 AND userid='$uid'") or die(mysqli_error($conn));
}
$rpay = mysqli_fetch_assoc($payment_query2);
$PayMent_chart = @$rpay['sum(amount)'];




if ($administrator == 'primary') {
  $contra_query2 = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype='C' AND date(date) LIKE '$date' AND active=1") or die(mysqli_error($conn));
} else {
  $contra_query2 = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype='C' AND date(date) LIKE '$date' AND active=1 AND userid='$uid'") or die(mysqli_error($conn));
}
$rcon = mysqli_fetch_assoc($contra_query2);
$ConTra_chart = @$rcon['sum(amount)'];


if ($administrator == 'primary') {
  $jurnal_query2 = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype in('JUR') AND date(date) LIKE '$date' AND active=1") or die(mysqli_error($conn));
} else {
  $jurnal_query2 = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype in('JUR') AND date(date) LIKE '$date' AND active=1 AND userid='$uid'") or die(mysqli_error($conn));
}
$rjur = mysqli_fetch_assoc($jurnal_query2);
$jurnal_chart = @$rjur['sum(amount)'];




if ($administrator == 'primary') {
  $ins_query2 = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype in('INSR') AND date(date) LIKE '$date' AND active=1") or die(mysqli_error($conn));
} else {
  $ins_query2 = mysqli_query($conn, "SELECT sum(amount) FROM transaction WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND ttype in('INSR') AND date(date) LIKE '$date' AND active=1 AND userid='$uid'") or die(mysqli_error($conn));
}
$rins = mysqli_fetch_assoc($ins_query2);
$ins_chart = @$rins['sum(amount)'];





?>



<script>
  $(function() {
    'use strict'
    var labels = ["Income", "Expense", "Profit", "Loss", ];
    var datasets = [300, 300, 300, 300];
    var backgroundColor = ["green", "darkorange", "blue", "red"];

    new Chart("incomeChart", {
      type: "pie",
      data: {
        labels: labels,
        datasets: [{
          backgroundColor: backgroundColor,
          data: datasets
        }]
      },
      options: {
        maintainAspectRatio: false,
        legend: {
          display: false
        },
        title: {
          display: true,
          text: "Income VS Expense - " + '<?php echo $date ?>'
        },
        responsive: true
      }
    });


    var xValues = ["Received", "Payment", "Contra", "Journal", "Ins Received"];
    var yValues = [<?php echo $ReceIved_chart ?>, <?php echo $PayMent_chart ?>, <?php echo $ConTra_chart ?>, <?php echo $jurnal_chart ?>, <?php echo $ins_chart ?>];
    var barColors = ["green", "red", "darkorange", "blue", "brown"];

    new Chart("transactionChart", {
      type: "bar",
      data: {
        labels: xValues,
        datasets: [{
          backgroundColor: barColors,
          data: yValues
        }]
      },
      options: {
        maintainAspectRatio: false,
        legend: {
          display: false
        },
        title: {
          display: true,
          text: "All Transaction - " + '<?php echo date('d-M-Y', strtotime($date)); ?>'
        },
        responsive: true
      }
    });


    new Chart("prslChart", {
      type: 'line',
      data: {
        labels: ["01-08-22", "02-08-22", "03-08-22", "04-08-22", "05-08-22", "06-08-22", "07-08-22", "08-08-22", "09-08-22", "10-08-22"],
        datasets: [{
          data: [86, 114, 106, 106, 107, 111, 133, 221, 783, 2478],
          label: "Purchase",
          borderColor: "darkorange",
          fill: false
        }, {
          data: [282, 350, 411, 502, 635, 809, 947, 1402, 3700, 5267],
          label: "Sales",
          borderColor: "green",
          fill: false
        }]
      },
      options: {
        maintainAspectRatio: false,
        legend: {
          display: false
        },
        title: {
          display: true,
          text: "Purchase VS Sales - 2022"
        },
        responsive: true
      }
    });


    $('.connectedSortable').sortable({
      placeholder: 'sort-highlight',
      connectWith: '.connectedSortable',
      handle: '.card-header, .nav-tabs',
      forcePlaceholderSize: true,
      zIndex: 999999
    })
    $('.connectedSortable .card-header').css('cursor', 'move')
    //$('.todo-list').sortable({placeholder:'sort-highlight',handle:'.handle',forcePlaceholderSize:true,zIndex:999999})
  })
</script>