<?php
/**
 * Created by PhpStorm.
 * User: sunusi
 * Date: 2019-03-11
 * Time: 06:36
 */


//revenue formula
function calculateRevenue($month)
{
    $db = new db();

    //fetching all the active configuration record in the database
    $fixedConfiguration = $db->getAll("select * from udut_configurations where configStatus = 1 and configType = 'Fixed' ");
    $percent1Configuration = $db->getAll("select * from udut_configurations where configStatus = 1 and configType = '1 %' ");
    $percent2Configuration = $db->getAll("select * from udut_configurations where configStatus = 1 and configType = '2 %' ");
    $percent3Configuration = $db->getAll("select * from udut_configurations where configStatus = 1 and configType = '3 %' ");
    $percent4Configuration = $db->getAll("select * from udut_configurations where configStatus = 1 and configType = '4 %' ");
    $percent5Configuration = $db->getAll("select * from udut_configurations where configStatus = 1 and configType = '5 %' ");
    $fixedPercentConfiguration = $db->getAll("select * from udut_configurations where configStatus = 1 and configType = 'Fixed%' ");

    //getting the full month
    $m_y = explode("-", $month);
    $monthNum = $m_y[1];
    $dateObj = DateTime::createFromFormat('!m', $monthNum);
    $monthName = $dateObj->format('F'); // March

    //deduct all fixed amounts
    $totalMonthlyRevenue = getTotalMonthlyRevenue($month);
    $fixedRows = "";
    $percent1Rows = "";
    $percent2Rows = "";
    $percent3Rows = "";
    $percent4Rows = "";
    $percent5Rows = "";
    $fixedPercentRows = "";
    $totalPercent1 = 0;
    $totalPercent2 = 0;
    $totalPercent3 = 0;
    $totalPercent4 = 0;
    $totalPercent5 = 0;
    foreach ($fixedConfiguration as $fixedConfig) {
        $totalMonthlyRevenue = ($totalMonthlyRevenue - $fixedConfig['configValue']);
        $fixedRows =
            $fixedRows . "<tr>
              <td>" . ucwords($fixedConfig['configName']) . "</td>
              <td class='color-yellow'>" . $fixedConfig['configType'] . "</td>
              <td>&#x20A6; " . number_format($fixedConfig['configValue']) . " (Fixed)</td>
              <td>" . $monthName . ", " . $m_y[0] . "</td>
            </tr>";
//        print($config['configName'] . " = " . $config['configValue'] . "<br>");
    }
    foreach ($fixedPercentConfiguration as $fixedPercentConfig) {
        $amount = ($fixedPercentConfig['configValue'] * $totalMonthlyRevenue) / 100;
        $fixedPercentRows =
            $fixedPercentRows . "<tr>
              <td>" . ucwords($fixedPercentConfig['configName']) . "</td>
              <td class='color-yellow'>" . $fixedPercentConfig['configType'] . "</td>
              <td>&#x20A6; " . number_format($amount) . " (Fixed & % of &#x20A6;" . $totalMonthlyRevenue . "))</td>
              <td>" . $monthName . ", " . $m_y[0] . "</td>
            </tr>";
//        print($config['configName'] . " = " . $config['configValue'] . "<br>");
        $totalMonthlyRevenue = ($totalMonthlyRevenue - $amount);
    }
    $totalMonthlyRevenueAfterFixedValues1 = $totalMonthlyRevenue;
    foreach ($percent1Configuration as $percent1Config) {
        $amount = ($percent1Config['configValue'] * $totalMonthlyRevenueAfterFixedValues1) / 100;
        $percent1Rows =
            $percent1Rows . "<tr>
              <td>" . ucwords($percent1Config['configName']) . "</td>
              <td class='color-yellow'>" . $percent1Config['configType'] . "</td>
              <td>&#x20A6; " . number_format($amount) . " (" . $percent1Config['configValue'] . "% of &#x20A6;" . number_format($totalMonthlyRevenueAfterFixedValues1) . ")</td>
              <td>" . $monthName . ", " . $m_y[0] . "</td>
            </tr>";
        $totalMonthlyRevenue = ($totalMonthlyRevenue - $amount);
        $totalPercent1 = $totalPercent1 + $percent1Config['configValue'];
    }
    //    $totalPercent1 = 100 - $totalPercent1;
    $totalMonthlyRevenueAfterFixedValues2 = $totalMonthlyRevenue;
    foreach ($percent2Configuration as $percent2Config) {
        $amount = ($percent2Config['configValue'] * $totalMonthlyRevenueAfterFixedValues2) / 100;
        $percent2Rows =
            $percent2Rows . "<tr>
              <td>" . ucwords($percent2Config['configName']) . "</td>
              <td class='color-yellow'>" . $percent2Config['configType'] . "</td>
              <td>&#x20A6; " . number_format($amount) . " (" . $percent2Config['configValue'] . "% of " . $totalPercent1 . "% of &#x20A6;" . number_format($totalMonthlyRevenueAfterFixedValues1) . ")</td>
              <td>" . $monthName . ", " . $m_y[0] . "</td>
            </tr>";
        $totalMonthlyRevenue = ($totalMonthlyRevenue - $amount);
        $totalPercent2 = $totalPercent2 + $percent2Config['configValue'];
    }

    $totalMonthlyRevenueAfterFixedValues3 = $totalMonthlyRevenue;
    foreach ($percent3Configuration as $percent3Config) {
        $amount = ($percent3Config['configValue'] * $totalMonthlyRevenueAfterFixedValues3) / 100;
        $percent3Rows =
            $percent3Rows . "<tr>
              <td>" . ucwords($percent3Config['configName']) . "</td>
              <td class='color-yellow'>" . $percent3Config['configType'] . "</td>
              <td>&#x20A6; " . number_format($amount) . " (" . $percent3Config['configValue'] . "% of " . $totalPercent3 . "% of &#x20A6;" . number_format($totalMonthlyRevenueAfterFixedValues3) . ")</td>
              <td>" . $monthName . ", " . $m_y[0] . "</td>
            </tr>";
        $totalMonthlyRevenue = ($totalMonthlyRevenue - $amount);
        $totalPercent3 = $totalPercent3 + $percent3Config['configValue'];
    }

    $totalMonthlyRevenueAfterFixedValues4 = $totalMonthlyRevenue;
    foreach ($percent4Configuration as $percent4Config) {
        $amount = ($percent4Config['configValue'] * $totalMonthlyRevenueAfterFixedValues4) / 100;
        $percent4Rows =
            $percent4Rows . "<tr>
              <td>" . ucwords($percent4Config['configName']) . "</td>
              <td class='color-yellow'>" . $percent4Config['configType'] . "</td>
              <td>&#x20A6; " . number_format($amount) . " (" . $percent4Config['configValue'] . "% of " . $totalPercent4 . "% of &#x20A6;" . number_format($totalMonthlyRevenueAfterFixedValues4) . ")</td>
              <td>" . $monthName . ", " . $m_y[0] . "</td>
            </tr>";
        $totalMonthlyRevenue = ($totalMonthlyRevenue - $amount);
        $totalPercent4 = $totalPercent4 + $percent4Config['configValue'];
    }

    $totalMonthlyRevenueAfterFixedValues5 = $totalMonthlyRevenue;
    foreach ($percent5Configuration as $percent5Config) {
        $amount = ($percent5Config['configValue'] * $totalMonthlyRevenueAfterFixedValues5) / 100;
        $percent5Rows =
            $percent5Rows . "<tr>
              <td>" . ucwords($percent5Config['configName']) . "</td>
              <td class='color-yellow'>" . $percent5Config['configType'] . "</td>
              <td>&#x20A6; " . number_format($amount) . " (" . $percent5Config['configValue'] . "% of " . $totalPercent5 . "% of &#x20A6;" . number_format($totalMonthlyRevenueAfterFixedValues5) . ")</td>
              <td>" . $monthName . ", " . $m_y[0] . "</td>
            </tr>";
        $totalMonthlyRevenue = ($totalMonthlyRevenue - $amount);
        $totalPercent5 = $totalPercent5 + $percent5Config['configValue'];
    }

    $totalRevenue =
        "<tr>
              <td>" . ucwords("Total") . "</td>
              <td class='color-yellow'> - </td>
              <td class='color-red'><strong>&#x20A6; " . number_format(getTotalMonthlyRevenue($month)) . "</strong></td>
            </tr>";
//    echo $totalMonthlyRevenueAfterFixedValues;
    //getting the percentage value
    $revenueTablekhalifa = '
      <table class="table" width="100%" class="table table-condensed table-bordered">
          <thead>
            <tr>
              <th>Names</th>
              <th>Types</th>
              <th>Amount (Fixed / Percent)</th>
              <th>Month</th>
            </tr>
          </thead>
          <tbody>
            ' . $fixedRows . '
            ' . $fixedPercentRows . '
            ' . $percent1Rows . '
            ' . $percent2Rows . '
            ' . $percent3Rows . '
            ' . $percent4Rows . '
            ' . $percent5Rows . '
            ' . $totalRevenue . '
          </tbody>
      </table>
    ';

    $revenueTableForesight = '
      <table class="table" width="100%" class="table table-condensed table-bordered">
          <thead>
            <tr>
              <th>Names</th>
              <th>Types</th>
              <th>Amount (Fixed / Percent)</th>
              <th>Month</th>
            </tr>
          </thead>
          <tbody>
            ' . $fixedRows . '
            ' . $fixedPercentRows . '
            ' . $percent1Rows . '
            ' . $percent2Rows . '
            ' . $percent3Rows . '
            ' . $percent4Rows . '
            ' . $percent5Rows . '
            ' . $totalRevenue . '
          </tbody>
      </table>
    ';

    $revenueTableuduth = '
      <table class="table" width="100%" class="table table-condensed table-bordered">
          <thead>
            <tr>
              <th>Names</th>
              <th>Types</th>
              <th>Amount (Fixed / Percent)</th>
              <th>Month</th>
            </tr>
          </thead>
          <tbody>

            ' . $percent1Rows . '

          </tbody>
      </table>
    ';

    $sysUsers = $db->getOne("
                  SELECT udut_users.*,
                         udut_users_roles.*
                  FROM udut_users 
                  JOIN udut_users_roles
                  ON udut_users.userId = udut_users_roles.userId
                  WHERE udut_users.userId = '" . $_SESSION['YUZA_ID'] . "'
                ");
    if ($sysUsers["userUsername"] == "khalifa") {
        return $revenueTablekhalifa;
    }
    if ($sysUsers["userUsername"] == "uduth") {
        return $revenueTableuduth;
    }
    if ($sysUsers["userUsername"] == "foresight") {
        return $revenueTableForesight;
    }else{
        return "This user is not allowed to view the revenue page.";
    }

}