<?php

require_once "../../config.php";
require_once "../../validation.php";
require_once "../../Helper.php";
require_once "../../ajax/send_email.php";
require_once "check_edit_page_ajax.php";

error_reporting(E_ALL);
ini_set('display_errors', 1);

include_once "../../ichat/admin/route.php";
if ($session->is_logged_in() != true ) {
        redirect_to(WEB_LINK.'/login');
} else {
    $current_user = User::get_specific_id($session->admin_id);
    $current_user->set_online();
    $group = $current_user->prvlg_group;
}


require_once("../../ichat/admin/assets/includes/Library/SessManager.php");
require_once("../../ichat/admin/assets/includes/Library/User.php");
$session = new  SessManager();

require_once '../database1.php';
class convertToInvoice extends database
{
    public function getCheckout($checkoutID)
    {
        return $this->fetchRow('tb_checkout', ['cl_id' => $checkoutID]);
    }

    public function current_count()
    {
        $where['cl_type'] = 'Invoice';
         return $this->fetchRow('tb_auto_incre', $where);
    }

    public function getAllCheckoutItems($checkoutID)
    {
        $sql = " SELECT ci.*, p.cl_name FROM tb_checkout_item ci INNER JOIN tb_product p ON ci.cl_product_id=p.cl_id WHERE ci.cl_deleted_at IS NULL AND ci.cl_checkout_id=? ";
        $where[] = $checkoutID;
        return $this->callQuery($sql, $where);
    }
}

if(!isset($_GET['id']) || !is_numeric($_GET['id']))
{
    http_response_code(400);
    echo json_encode(array('error_text' => 'Order Not Found'));
    exit;
}

$checkoutID = $_GET['id'];

$obj = new convertToInvoice();
$checkout = $obj->getCheckout($checkoutID);
if(!$checkout)
{
    http_response_code(400);
    echo json_encode(array('error_text' => 'Invoice Not Found'));
    exit;
}

$checkoutType = $checkout['cl_checkout_type'];
$invoiceNo = $checkout['cl_id'];

$checkoutItems = $obj->getAllCheckoutItems($checkoutID);



$body = '<table width="100%" role="presentation" style="width:720px;padding-left: 30px;padding-right: 30px;font-family: arial;color: #1F2535;border:1px solid lightgray;margin : 0 auto">
  <tr>
    <th colspan="2" style="text-align: center;padding-top: 25px">
      <img src="https://www.eretail.lv/images/logo.png" width="100px" />
    </th>
  </tr>
  <tr>
    <th colspan="2" style="text-align: center;padding-top: 50px;font-size: 16px">
      <span style="color:#1F2535">Pavadzīme nr.</span> <span style="color:#FCA311"># OR-' . $checkout['cl_id'] . '</span>
    </th>
  </tr>
  <tr>
    
    <td style="width: 50%;padding-top: 80px;vertical-align: top">
      <b style="width:100%;font-size: 16px;color:#1F2535">Sūtītājs</b>
      
      <div style="width:100%;font-size: 14px;padding-top: 15px;padding-bottom: 10px"><b>Nosaukums: </b><span>SIA eretail</span></div>
      <div style="width:100%;font-size: 14px;padding-bottom: 10px"><b>Reģistrācijas nr: </b><span>51203062881</span></div>
      <div style="width:100%;font-size: 14px;padding-bottom: 10px"><b>Adrese: </b><span>Ezera iela 28, Ventspils, LV-3602</span></div>
      <div style="width:100%;font-size: 14px;padding-bottom: 10px"><b>PVN maksātāja nr: </b><span>LV51203062881</span></div>';
       if($checkoutType == 'ind')
      {
        $body.= '<div style="width:100%;font-size: 14px;padding-bottom: 10px"><b>Banka: </b><span>AS "SEB banka"</span></div>
          <div style="width:100%;font-size: 14px;padding-bottom: 10px"><b>Konts: </b><span>LV78UNLA0055001619953</span></div>';
      }
      else
      {
        $body.= '<div style="width:100%;font-size: 14px;padding-bottom: 10px"><b>Banka: </b><span>A/S "Citadele banka"</span></div>
          <div style="width:100%;font-size: 14px;padding-bottom: 10px"><b>Konts: </b><span>LV88PARX0020875540002</span></div>';
      }
      
      $body.= '<div style="width:100%;font-size: 14px;padding-bottom: 10px;padding-top: 25px"><b style="font-size: 16px;">Apmaksas metode</b></div>';
      if($checkout['cl_payment_type'] == 'Bank Transfer')
      {
        $body.= '<div style="width:100%;padding-top: 15px"><span>Bankas pārskaitījums</span></div>';
      }
      else
      {
        $body.= '<div style="width:100%;padding-top: 15px"><span>Karšu maksājums</span></div>';
      }
    $body.= '</td>
    <td style="width: 50%;padding-top: 80px;vertical-align: top">
      <b style="width:100%;font-size: 16px">Pasūtītājs</b>';
      

      if($checkoutType == 'ind')
      {
        $body.= '<div style="width:100%;font-size: 14px;padding-top: 15px;padding-bottom: 10px"><b>Vārds: </b><span>' . $checkout['cl_fname'] . '</span></div>
        <div style="width:100%;font-size: 14px;padding-bottom: 10px"><b>Uzvārds: </b><span>' . $checkout['cl_surname'] . '</span></div>
        <div style="width:100%;font-size: 14px;padding-bottom: 10px"><b>Piegādes adrese: </b><span>' . $checkout['cl_address'] . ', ' . $checkout['cl_city'] . ',' . $checkout['cl_country'] . ',' . $checkout['cl_pasta_indeks'] . '</span></div>
        <div style="width:100%;font-size: 14px;padding-bottom: 10px;padding-top: 25px"><b>Pasūtījuma datums </b></div>
        <div style="width:100%;font-size: 14px;padding-bottom: 10px;padding-top: 15px"><span>' . date("d. M, Y.", strtotime($checkout['cl_created_at'])) . 'gads , ' . date("H:i", strtotime($checkout['cl_created_at'])) . '</span></div>';
      }
      else
      {
          $body.= '<div style="width:100%;font-size: 14px;padding-top: 15px;padding-bottom: 10px"><b>Nosaukums: </b><span>' . $checkout['cl_company_name'] . '</span></div>
        <div style="width:100%;font-size: 14px;padding-bottom: 10px"><b>Reģistrācijas nr: </b><span>' . $checkout['cl_registration_number'] . '</span></div>
        <div style="width:100%;font-size: 14px;padding-bottom: 10px"><b>PVN maksātāja nr: </b><span>' . $checkout['cl_vat_number'] . '</span></div>
        <div style="width:100%;font-size: 14px;padding-bottom: 10px"><b>Piegādes adrese: </b><span>' . $checkout['cl_address'] . ', ' . $checkout['cl_city'] . ',' . $checkout['cl_country'] . ',' . $checkout['cl_pasta_indeks'] . '</span></div>

        <div style="width:100%;font-size: 14px;padding-bottom: 10px;padding-top: 25px"><b>Pasūtījuma datums </b></div>
        <div style="width:100%;font-size: 14px;padding-bottom: 10px;padding-top: 15px"><span>' . date("d. M, Y.", strtotime($checkout['cl_created_at'])) . 'gads , ' . date("H:i", strtotime($checkout['cl_created_at'])) . '</span></div>';
      }
    $body.= '</td>
  </tr>
  <tr>
    <td colspan="2" style="height: 50px">

    </td>
  </tr>';

  if($checkout['cl_payment_type'] != 'Card')
  { 
  $body.= '<tr>
    <td colspan="2" style="text-align: center;background-color: rgba(252,163,17,0.1);padding-top: 32px;padding-bottom: 32px;">
      <b style="width:100%;font-size: 16px;color: #1F2535">
        Rekvizīti apmaksas veikšanai
      </b>
      
      <div style="width:100%;padding-top: 20px;font-size: 14px"><b>Nosaukums: </b><span>SIA eretail</span></div>
      <div style="width:100%;padding-top: 10px;font-size: 14px"><b>Reģ. numurs: </b><span>51203062881</span></div>';

      if($checkoutType == 'ind')
      {
     
      $body.= '<div style="width:100%;padding-top: 10px;font-size: 14px"><b>Konta numurs: </b><span>LV78UNLA0055001619953</span></div>
      <div style="width:100%;padding-top: 10px;font-size: 14px"><b>Banka: </b><span>AS "SEB banka"</span></div>';

      }
      else
      {
        $body.= '<div style="width:100%;padding-top: 10px;font-size: 14px"><b>Konta numurs: </b><span>LV88PARX0020875540002</span></div>
      <div style="width:100%;padding-top: 10px;font-size: 14px"><b>Banka: </b><span>A/S "Citadele banka"</span></div>';
      }
     
      $body.= '<div style="width:100%;padding-top: 10px;font-size: 14px"><b>Maksājuma mērķis: </b><span>OR-' . $res['checkout_id'] . '</span></div>
    </td>
  </tr>';
 } 
  $body.= '<tr>
    <th colspan="2" style="text-align: left;padding-top: 47px">
      Pasūtījuma kopsavilkums
    </th>
  </tr>

  <tr>
    <td colspan="2" style="padding-top: 20px">
      <table style="width:100%">
        <thead>
          <tr>
            <th style="background-color: #F4F6F7;text-align: left;padding-left: 30px;padding-right:30px;padding-top: 12px;padding-bottom: 12px;font-size: 12px">
              Prece
            </th>
            <th style="background-color: #F4F6F7;text-align: left;padding-left: 30px;padding-right:30px;padding-top: 12px;padding-bottom: 12px;font-size: 12px">
              Cena
            </th>
            <th style="background-color: #F4F6F7;text-align: left;padding-left: 30px;padding-right:30px;padding-top: 12px;padding-bottom: 12px;font-size: 12px">
              Daudzums
            </th>
            <th style="background-color: #F4F6F7;text-align: left;padding-left: 30px;padding-right:30px;padding-top: 12px;padding-bottom: 12px;font-size: 12px">
              Kopā
            </th>
          </tr>
        </thead>
        <tbody>';

        if(!empty($checkoutItems))
        {
            

            foreach($checkoutItems AS $item)
            {   
                if($item['cl_type'] == 'General')
                {
                  $supplierPrice = $item['cl_supplier_price'];
                }
                else if($item['cl_type'] == 'Variable')
                {
                  $variableProduct = $obj->getVariableProduct($item['cl_variable_product_id']);
                  $supplierPrice = $variableProduct['cl_supplier_price'];
                }
                  
                if($supplierPrice < 20)
                {
                    $profitPercentage = $profitPercentage0_20;
                }
                else if($supplierPrice >= 20 && $supplierPrice <= 50)
                {
                    $profitPercentage = $profitPercentage20_50;
                }
                else if($supplierPrice >= 50 && $supplierPrice <= 100)
                {
                    $profitPercentage = $profitPercentage50_100;
                }
                else if($supplierPrice >= 100 && $supplierPrice <= 200)
                {
                    $profitPercentage = $profitPercentage100_200;
                }
                else if($supplierPrice >= 200 && $supplierPrice <= 800)
                {
                  $profitPercentage = $profitPercentage200_800;
                }
                else
                {
                  $profitPercentage = $profitPercentageMoreThan800;
                }

                $salePrice = $supplierPrice + ($supplierPrice * $profitPercentage / 100);

                 $body.= '<tr>
                    <td style="padding-left: 30px;padding-top: 15px;padding-bottom: 15px;;font-size: 12px">' . $item['cl_name'] . '</td><td style="padding-left: 30px;padding-top: 15px;padding-bottom: 15px;;font-size: 12px">€ ' . number_format($salePrice,2) . '</td><td style="padding-left: 30px;padding-top: 15px;padding-bottom: 15px;;font-size: 12px">' . $item['cl_quantity'] . '</td><td style="padding-left: 30px;padding-top: 15px;padding-bottom: 15px;;font-size: 12px">€ ' . number_format($item['cl_quantity'] * $salePrice, 2) . '</td>
                   
                  </tr>';
                  $total+= $item['cl_quantity'] * $salePrice;
            }
        }


 $body.= '</tbody>
        <tfoot>';
            $totalWithPiegade = $total + $pvn;
                                     $body.='<tr>
            <td colspan="4" style="text-align: right;padding-top: 15px;padding-bottom: 10px;font-size: 12px">Kopā: € ' . number_format($total,2) . '
                                          </td>
                                       </tr>
                                       <tr>

                                        <td colspan="4" style="text-align: right;padding-bottom: 10px;font-size: 12px">
              Piegāde: € ' . number_format($pvn,2) . '
                                          </td>
                                       </tr>
                                       
                                       <tr>
                                          
                                       <tr>
            <td colspan="4" style="text-align: right;padding-bottom: 10px;font-size: 12px">Kopā bez PVN: € ' .  number_format($totalWithPiegade,2) . '
                                          </td>
                                        </tr>';
                                        $body1111 = '<tr>
            <td colspan="4" style="text-align: right;padding-bottom: 10px;padding-top:10px;;font-size: 12px">
              Atlaide: <b style="color:#499F54">- € 0</b>
            </td>
          </tr>
          <tr>
            <td colspan="4" style="text-align: right;padding-bottom: 10px;font-size: 12px">
              Kopā bez PVN: <b>€ ' .  number_format($totalWithPiegade,2) . '</b>
            </td>
          </tr>';
                                     $body.= '<tr>';
                                          
                                          $vatAmount = ($totalWithPiegade * $taxPercentage / 100);
                                          $body.= '<td colspan="4" style="text-align: right;padding-bottom: 10px;padding-top:10px;;font-size: 12px">
              PVN: € ' .  number_format($vatAmount, 2) . '
                                          </td>
                                        </tr>
                                        <tr>
                                          
                                         <td colspan="4" style="text-align: right;padding-bottom: 10px;font-size: 12px">
              Kopā apmaksai: <b style="color:#FCA311"> € ' .  number_format($totalWithPiegade + $vatAmount,2) . '
                                          </td>
                                        </tr>
        </tfoot>
      </table>
    </td>
  </tr>
  <tr>
    <td colspan="2" style="padding-top: 25px;font-size: 14px">
      Rēķins ir sagatavots elektroniski un ir derīgs bez paraksta.
    </td>
  </tr>
  <tr>
    <th colspan="2" style="text-align: center;padding-top: 81px;font-size: 18px">
      Paldies, ka izvēlējāties eretail.lv!
    </th>
  </tr>
  <tr>
    <td colspan="2" style="text-align: center;padding-top: 40px;">
      <img src="https://www.eretail.lv/images/logo.png" width="100px" />
    </td>
  </tr>
  
  <tr>
    <td colspan="2" style="text-align: center;padding-top: 30px;padding-bottom: 25px;color: #676F82">
       Part of the eretail Group © SIA eretail 2017 - 2020
    </td>
  </tr>

</table>';





/*$body = '<table style="width:100%;border:1px solid lightgray"><tr><td style="text-align:left;border-bottom:1px solid lightgray;;padding:15px" colspan="2">Sveicināti, ' . $checkout['cl_fname'] . ' ' . $checkout['cl_surname'] . '</td></tr><tr><td style="text-align:left;border-bottom:1px solid lightgray;;padding:15px" colspan="2">Atsaucoties uz Jūsu pasūtījumu interneta veikalā www.eretail.lv, pielikumā nosūtām rēķinu par pasūtījumu.</td></tr><tr><td colspan="2" style="text-align:left;border-bottom:1px solid lightgray;;padding:15px">Lūdzam apmaksāt rēķinu, norādot tā numuru: ' . $invoiceNo . '<br/><br/>
Rekvizīti apmaksas veikšanai:<br/>
SIA eretail<br/>
Konta nr: LV88PARX0020875540002<br/>
Banka: AS Swedbank<br/>
Reģ.nr:  51203062881<br/><br/>
Nenorādot rēķina numuru, maksājuma apstrāde var aizkavēties. Lai garantētu laicīgi preces piegādi, lūdzam, veikt pasūtījuma apmaksu pēc iespējas ātrāk*.</td></tr>';
$body.= '<tr><th style="text-align:left;border-bottom:1px solid lightgray;;padding:15px;width: 170px">Vārds Uzvārds</th><td style="text-align:left;border-bottom:1px solid lightgray;;padding:15px">' . $checkout['cl_fname'] . ' ' . $checkout['cl_surname'] . '</td></tr>';
$body.= '<tr><th style="text-align:left;border-bottom:1px solid lightgray;;padding:15px;width: 170px">Pilsēta</th><td style="text-align:left;border-bottom:1px solid lightgray;;padding:15px">' . $checkout['cl_city'] . '</td></tr>';
$body.= '<tr><th style="text-align:left;border-bottom:1px solid lightgray;;padding:15px;width: 170px">Adrese</th><td style="text-align:left;border-bottom:1px solid lightgray;;padding:15px">' . $checkout['cl_address'] . '</td></tr>';
$body.= '<tr><th style="text-align:left;border-bottom:1px solid lightgray;;padding:15px;width: 170px">Pasta indekss</th><td style="text-align:left;border-bottom:1px solid lightgray;;padding:15px">' . $checkout['cl_pasta_indeks'] . '</td></tr>';

$body.= '<tr><th style="text-align:left;border-bottom:1px solid lightgray;;padding:15px;width: 170px">Apmaksas veids</th><td style="text-align:left;border-bottom:1px solid lightgray;;padding:15px">' . $checkout['cl_payment_type'] . '</td></tr>';

$body.= '</table>';

$total = 0;

$body.= '<table style="width:100%;border:1px solid lightgray;margin-top:15px">';
if(!empty($checkoutItems))
{
    $body.= '<thead><tr><th style="text-align:left;border-bottom:1px solid lightgray;;padding:15px">Pasūtītās preces</th><th style="text-align:left;border-bottom:1px solid lightgray;;padding:15px">Kopējais preču daudzums</th><th style="text-align:left;border-bottom:1px solid lightgray;border-right:1px solid lightgray;;padding:15px">Cena</th></tr></thead><tbody>';
    foreach($checkoutItems AS $item)
    {
        $body.= '<tr><td style="text-align:left;border-bottom:1px solid lightgray;border-right:1px solid lightgray;;padding:15px">' . $item['cl_name'] . '</td><td style="text-align:left;border-bottom:1px solid lightgray;;padding:15px">' . $item['cl_quantity'] . '</td><td style="text-align:left;border-bottom:1px solid lightgray;;padding:15px">' . $item['cl_amount'] . '</td></tr>';
        $total+= $item['cl_amount'];
    }
}
$body.= '</tbody><tfoot>';
$body.= '<tr><td colspan="3" style="text-align:right">Cena: ' . $total . ' €</td></tr>';
$body.= '<tr><td colspan="3" style="text-align:right">Piegādes maksa: ' . $checkout['cl_pvn'] . ' €</td></tr>';
$body.= '<tr><td colspan="3" style="text-align:right">Pavisam kopā: ' . $checkout['cl_total'] . ' €</td></tr>';
$body.= '</tfoot></table>'; */

$emailRes = send_email($checkout['cl_email'], $checkout['cl_fname'], 'eretail.LV - Invoice # ' . $invoiceNo, $body, 1 ,1);


http_response_code(200);
echo json_encode(array('message' => 'Invoice Sent Successfully'));
exit;
