
<?php

include('../../config.php');

session_start();
if(empty($_SESSION['uid'])){

    header("location:../admin_panel/login.php");
}

//don't display errors
ini_set('display_errors', 0);
//write errors to log
ini_set('log_errors', 1);
//error log file name
ini_set('log_errors', '/error.log');

error_reporting(E_ALL);



?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- Custom fonts -->
      <link href="../vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" />
      <link href="https://fonts.googleapis.com/css2?family=Kufam:wght@400;500;600;700;800;900&display=swap"
            rel="stylesheet">

      <!-- Custom styles for this template -->
      <link href="../public/css/main.css" rel="stylesheet">

    <!-- <link href="style2.css" rel="stylesheet"> -->
  </head>
  <body>

  <?php
   $id = $_GET['id'];
 $query = "SELECT list_of_orders.*,list_of_orders.id as order_id,list_of_orders.transaction_token as token ,list_of_orders.status as stus,list_of_orders.enable as enble,list_of_orders.date_created as date_order,list_of_orders.currency_symbol as cur_symbol,
            user.*,user.first_name as names ,  user_address.*,user_address.address as user_adress, user_address.apartment as user_apart, user_address.city as user_city,user_address.postal_code as user_postal_code
            ,user_address.phone as user_contact,user_address.country as user_country
          FROM ListOfOrders as list_of_orders 
         INNER JOIN User as user ON user.id = list_of_orders.user_id
         INNER JOIN UserMeta as user_address ON user_address.id = list_of_orders.address_id
         where list_of_orders.id = '$id'";
  $result = queryRunner($query);
  $row = rowRetriever($result);

                                $token = $row['token'];
                                  $card_name="";
                                  if ($token !== null) {
                                      $card_name = "Credit / Debit Card";
                                  }
                                  else {
                                      $card_name ="COD";
                                  }
                                   $discount=ceil($row['discounted_amount']);
                                  $total_payment='';

                                  if($discount == "0"){
                                      $total_payment = $row['total_amount'] + $row['shipping_fee'];

                                  }
                                  else {
                                      $total_payment = $discount;
                                  }

                                    $disc="";

                                  if($discount !== null){
                                      $disc = $discount;
                                  }
                                  else {
                                      $disc = "0";
                                  }



  ?>
    <div class="container">
      <div class="invoice-wrap">
        <div class="printnow">
          <div class="comp-name">
            <h1>AliXpress</h1>
          </div>
          <div class="print-btn">
            <button class="btn printbtn">
              <span><i class="fa fa-print" aria-hidden="true"></i></span>Print
            </button>
          </div>
        </div>
        <h1 class="inovice-head">Invoice #<?php echo $row['order_no']; ?></h1>
        <div class="row">
          <div class="col-lg-6 col-md-6">
            <div class="date-heading">
              <h5>Issue Date</h5>
            </div>
            <div class="date-detail">
              <p id="issue-date-current">06/06/2019</p>
            </div>
          </div>
          <div class="col-lg-6 col-md-6">
            <div class="date-heading">
              <h5>Order Date</h5>
            </div>
            <div class="date-detail">
              <p><?= $row['date_order'];?></p>
            </div>
          </div>
          <div class="col-lg-6 col-md-6">
            <div class="date-heading">
              <h5>For</h5>
            </div>
            <div class="date-heading2">
              <h5>Comapny inc.</h5>
            </div>
            <div class="date-detail">
              <p>
                <?php echo $row['names']; ?> <br />
                  <?php echo $row['user_adress']; ?> <br />
                  <?php echo $row['user_city']; ?> <br />
                  <?php echo $row['user_country']; ?>

              </p>
            </div>
          </div>
          <div class="col-lg-6 col-md-6">
            <div class="date-heading">
              <h5>Form</h5>
            </div>
            <div class="date-heading2">
              <h5>UXDN inc.</h5>
            </div>
            <div class="date-detail">
              <p>
                5171 W Campbell Ave <br />
                Utah 53127 <br />
                United States
              </p>
            </div>
          </div>
          <div class="col-lg-12">
            <div class="table-wrap" style="padding: 0;">
              <!--Table-->
              <div class="table-responsive table-invoice">
                <table class="table table1">
                  <thead>
                  <?php
                  $id2 = $_GET['id'];

                  $query = " SELECT order_detail.*,order_detail.discounted_amount as dicuont_amt ,order_detail.id as order_id,order_detail.quantity as qunty,order_detail.price as prices ,brand.*,brand.id as brand_id,brand.name as brand_name,brand.image as brand_image ,brand.address as brand_adrs,  product.*, product.id 
        as pro_id,product.image as pro_image,product.name as pro_name,product.processing_time as pro_time,product.price as pro_price , shipping.* ,shipping.estimated_delivery_days as ship_days,
  shipping.name as shipping_name,shipping.id as shipping_id ,shipping.logo as ship_image, packages.name as packages_name , packages.* , 
        coupon.name as cou_name , coupon_detail.* 
          FROM OrderMeta as order_detail 
         INNER JOIN Brand as brand ON brand.id = order_detail.brand_id
         INNER JOIN Product as product ON product.id = order_detail.product_id
          INNER JOIN CustomizedShipping as shipping ON shipping.id = order_detail.shipping_id
           INNER JOIN ShippingPackages as packages ON packages.id = order_detail.package_id AND packages.customized_shipping_id = shipping.id
         LEFT JOIN Coupon as coupon ON coupon.id = order_detail.coupon_id 
         LEFT JOIN CouponMeta as coupon_detail ON coupon_detail.id = coupon.type
        
         where order_detail.term_id = '$id2'";
                  $result_order = queryRunner($query);



                  ?>
                    <tr>
                      <th scope="col">Name</th>
                      <!--<th scope="col">Attribute</th>-->
                      <th scope="col">Price</th>
                      <th scope="col">Qty</th>
                        <th scope="col">Disc</th>
                        <th scope="col">Total</th>


                    </tr>
                  </thead>
                  <tbody>
                  <?php
                   while ($row2 = rowRetriever($result_order)) {

                       $attribute = getProductAttribute($row2['attribute_id']);

                       $std="";

                       for($a=0;$a<count($attribute); $a++){
                           $att = $attribute[$a];
                           $std .= $att['name'];
                           if ($a < (count($attribute)-1)){
                               $std .=",";
                           }
                       }

                       $dis_amt = ceil($row2['dicuont_amt']);
                       $total_detail_payment = '';
                       if ($dis_amt != "" ) {

                           $total_detail_payment = $dis_amt;
                       } else {
                           $total_detail_payment = $row2['qunty'] * $row2['prices'];

                       }
                       $coupon_name=$row2['cou_name'];
                       $cou_name= '';
                       if($coupon_name !== null){
                           $cou_name = $coupon_name;
                       }else {
                           $cou_name = "0";
                       }

                       ?>
                    <tr>
                      <td scope="row">
                        <?php echo $row2['pro_name']."  (".$std.")"; ?>

                      </td>
                      <!--<td><?php /*echo $std;*/?></td>-->
                      <td><?php
                           echo $row['cur_symbol']." ".$row2['prices']; ?></td>
                      <td><?php
                          echo  $row2['qunty']; ?></td>
                        <td><?=$cou_name;  ?></td>
                        <td><?= $row['cur_symbol']." ".$total_detail_payment;  ?></td>

                    </tr>
                    <?php }?>
                  </tbody>
                </table>
              </div>
              <!--Table-->
            </div>
          </div>
          <div class="col-lg-6 col-md-6 up">
            <div class="any-question-section">
              <div>
                <div>
                  <span
                    ><i class="fa fa-question-circle" aria-hidden="true"></i
                  ></span>
                </div>
                <p>
                  Any questions ? Please set us know <br />
                  <a href="#">inovice@uxdn.co</a>
                </p>
              </div>
            </div>
          </div>
          <div class="col-lg-6 col-md-6">
            <div class="invoice-summary">
              <h1>INOVICE SUMMARY</h1>
              <div class="invoice-summary-detail">
                <div class="invoice-summary-detail-heading">
                  <h5>Total Amount</h5>
                  <h5>Shipping Amount</h5>
                  <h5>Payment Method</h5>
                  <h5>Discount %</h5>
                    <h5>Discounted amount</h5>

                </div>
                <div class="invoice-summary-detail-heading">
                  <h5><?=$row['cur_symbol']." ". $total_payment; ?></h5>
                  <h5><?= $row['cur_symbol']." ".$row['shipping_fee']; ?></h5>
                  <h5><?= $card_name;?></h5>
                  <h5><?=$cou_name;  ?></h5>
                    <h5><?=$row['cur_symbol']." ".$disc;  ?></h5>

                </div>
              </div>
            </div>
          </div>
          <div class="col-lg-6 col-md-6 below">
            <div class="any-question-section">
              <div>
                <div>
                  <span
                    ><i class="fa fa-question-circle" aria-hidden="true"></i
                  ></span>
                </div>
                <p>
                  Any questions ? Please set us know <br />
                  <a href="#">inovice@alixpress.com</a>
                </p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
    <script src="../public/js/customjs.js"></script>
  <script>
      var d = new Date();

      var month = d.getMonth()+1;
      var day = d.getDate();

      var output = d.getFullYear() + '/' +
          ((''+month).length<2 ? '0' : '') + month + '/' +
          ((''+day).length<2 ? '0' : '') + day;

     $("#issue-date-current").text(output);

      $(".printbtn").click(function () {
          window.print();
      });
  </script>

  </body>
</html>
<?php

function getProductAttribute($id){

    ///Utility::Logger("Meta Id = ".$id);
    $listOfData = [];

    $query = " SELECT * FROM AttributeItem 
         where id IN('$id') ";
    $result = queryRunner($query);
    while ($row = rowRetriever($result)) {

        array_push($listOfData, [
            'id' => $row['id'],
            'name' => $row['name'],



        ]);
    }

    return $listOfData;


}

?>
