<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;
use App\Models\OrderModel;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use App\Models\InvoiceSettingModel;
class InvoiceController extends Controller
{
    public function Invoice($id)

    {
      
    $order = DB::table("orders")
    ->select('orders.*',
    'product.id as product_id',
    'product.qty_text',
     'product.title',
     'user_address.name',
     'user_address.s_phone',
     'user_address.flat_no',
     'user_address.landmark',
     'user_address.apartment_name',
     'user_address.area',
     'user_address.city',
     'user_address.pincode'
     )
     ->Join('product','orders.product_id', '=','product.id')
     ->Join('user_address','user_address.id', '=','orders.address_id')
     ->where("orders.id","=",$id)
      ->first();
      
          $dataInvoiceSet1 = DB::table("invoice_setting")
      ->select('invoice_setting.*')
      ->where("invoice_setting.id","=",1)
        ->first();
             $dataInvoiceSet2 = DB::table("invoice_setting")
      ->select('invoice_setting.*')
      ->where("invoice_setting.id","=",2)
        ->first();
             $dataInvoiceSet3 = DB::table("invoice_setting")
      ->select('invoice_setting.*')
      ->where("invoice_setting.id","=",3)
        ->first();
             $dataInvoiceSet4 = DB::table("invoice_setting")
      ->select('invoice_setting.*')
      ->where("invoice_setting.id","=",4)
        ->first();
                     $dataInvoiceSet5 = DB::table("invoice_setting")
      ->select('invoice_setting.*')
      ->where("invoice_setting.id","=",5)
        ->first();
                     $dataInvoiceSet6 = DB::table("invoice_setting")
      ->select('invoice_setting.*')
      ->where("invoice_setting.id","=",6)
        ->first();
      $paymeny_status=$order->trasation_id!=null?"Paid":"Pending";

        $invoice_date = date('jS F Y', strtotime($order->created_at)); 
        $pdf = PDF::loadView('invoice_pdf',array(
            'order'=>$order,
        'paymeny_status'=>$paymeny_status,
        'logo_url'=>$dataInvoiceSet1->value??"",
        'lp_1'=>$dataInvoiceSet2->value??"--",
        'lp_2'=>$dataInvoiceSet3->value??"--",
        'lp_3'=>$dataInvoiceSet4->value??"--",
        'lp_4'=>$dataInvoiceSet5->value??"--",
         'bp'=>$dataInvoiceSet6->value??"--"
        )); ;

       return $pdf->download('Invoice_Order_No # '.$id.' Date_'.$invoice_date.'.pdf');
 

    }
    public function SubInvoice($month,$year, $id)

    {
          $dataInvoiceSet1 = DB::table("invoice_setting")
      ->select('invoice_setting.*')
      ->where("invoice_setting.id","=",1)
        ->first();
             $dataInvoiceSet2 = DB::table("invoice_setting")
      ->select('invoice_setting.*')
      ->where("invoice_setting.id","=",2)
        ->first();
             $dataInvoiceSet3 = DB::table("invoice_setting")
      ->select('invoice_setting.*')
      ->where("invoice_setting.id","=",3)
        ->first();
             $dataInvoiceSet4 = DB::table("invoice_setting")
      ->select('invoice_setting.*')
      ->where("invoice_setting.id","=",4)
        ->first();
                     $dataInvoiceSet5 = DB::table("invoice_setting")
      ->select('invoice_setting.*')
      ->where("invoice_setting.id","=",5)
        ->first();
                     $dataInvoiceSet6 = DB::table("invoice_setting")
      ->select('invoice_setting.*')
      ->where("invoice_setting.id","=",6)
        ->first();
        
    $order = DB::table("orders")
    ->select('orders.*',
    'product.id as product_id',
    'product.qty_text',
     'product.title',
     'user_address.name',
     'user_address.s_phone',
     'user_address.flat_no',
     'user_address.landmark',
     'user_address.apartment_name',
     'user_address.area',
     'user_address.city',
     'user_address.pincode'
     )
     ->Join('product','orders.product_id', '=','product.id')
     ->Join('user_address','user_address.id', '=','orders.address_id')
     ->where("orders.id","=",$id)
      ->first();
 $subName=  $order->subscription_type==1?"One Time Order":
 $order->subscription_type==2?"Weekly  Subscription":
 $order->subscription_type==3?"Monthly Subscription":
 $order->subscription_type==4?"Alternative Subscription":"N/A";
 $myDate = $month.'/01/2020';
 $date = Carbon::createFromFormat('m/d/Y', $myDate);

    $monthName = $date->format('F');
    $deliveryCount= DB::table("subscribed_order_delivery")
    ->select('subscribed_order_delivery.*')
     ->where('order_id','=', $order->id)
     ->whereMonth('date', '=', $month)
     ->whereYear('date', '=', $year)
      ->count();
      $newOrderAmount=$deliveryCount*$order->order_amount;
        $invoice_date = date('jS F Y', strtotime($order->created_at)); 
        $pdf = PDF::loadView('sub_invoice_pdf',array('order'=>$order,'newOrderAmount'=>$newOrderAmount,'subName'=>$subName,'year'=>$year,'monthName'=>$monthName,'deliveryCount'=>$deliveryCount,
         'logo_url'=>$dataInvoiceSet1->value??"",
        'lp_1'=>$dataInvoiceSet2->value??"--",
        'lp_2'=>$dataInvoiceSet3->value??"--",
        'lp_3'=>$dataInvoiceSet4->value??"--",
        'lp_4'=>$dataInvoiceSet5->value??"--",
         'bp'=>$dataInvoiceSet6->value??"--"
        
        )); ;

       return $pdf->download('Invoice_Order_No # '.$id.' Date_'.$invoice_date.'.pdf');
      
    }
}