<?php 

/*echo var_dump($tex_master_gst);
die('s-');*/
 ?>

@extends('template.app')

@section('meta')
@endsection

@section('title')
    Invoice Create
@endsection

@section('styles')
@endsection

@section('content')
<div class="page-breadcrumb">
    <div class="row">
        <div class="col-7 align-self-center">
            <h4 class="page-title text-truncate text-dark font-weight-medium mb-1">Invoice Create</h4>
            <div class="d-flex align-items-center">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb m-0 p-0">
                        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}" class="text-muted">Dashboard</a></li>
                        <li class="breadcrumb-item"><a href="{{ route('car_varient') }}" class="text-muted">Invoice Create</a></li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>
</div>
<div class="container-fluid">
    <div class="row">
        @if($data->inventory_id == NULL)
        <div class="col-12">
            <div class="alert alert-danger" role="alert">
              First assign car for this order
            </div>
        </div> 
        @else
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <form action="{{ route('order.insert') }}" name="form" id="form" method="post" enctype="multipart/form-data">
                        @csrf
                        @method('POST')
                 
                        <input type="hidden" name="id" value="{{ $data->id }}">
                        <input type="hidden" name="order_id" value="{{ $order_id; }}">
                        <input type="hidden" name="status" value="invoice_create">
                        

                        <div class="row">
                            <div class="col-md-12 text-center">
                                <hr>
                                <h2>Vehicle Tax Invoice</h2>
                                <hr>
                            </div>

                            <div class="form-group col-sm-3">
                                <label for="name">Dealer GST</label>
                                <input type="text" class="form-control" disabled="disabled" value="24ABACS4515J1ZH">
                                <input type="hidden" name="dealer_gst" value="24ABACS4515J1ZH">

                            </div>      

                            <div class="form-group col-sm-3">
                                <label for="name">PAN No</label>
                                <input type="text" class="form-control" disabled="disabled" value="ABACS4515J">
                                <input type="hidden" name="dealer_pan_number" value="ABACS4515J">
                            </div>   

                            <input type="hidden" id="customer_name" value="{{ $data->customer_name ??'' }}">
                            <input type="hidden" id="company_name" value="{{ $data->company_name ??'' }}">
                            <div class="form-group col-md-6">
                                <label class="control-label">Bill To</label>
                                @php
                                    $bill_to_value = ''; 

                                    if(isset($data->customer_type) && $data->customer_type != NULL && ( $data->customer_type == 'individual' || $data->customer_type == 'govt') ){
                                        $bill_to_value = $data->customer_name   ; 
                                    }elseif(isset($data->customer_type) && $data->customer_type != NULL && $data->customer_type == 'corporate'){
                                        $bill_to_value = $data->company_name; 
                                    }
                                    if ($orders_data->invoice_date != NULL && $orders_data->invoice_no != NULL) {
                                        $bill_to_value = $data->customer_name;
                                    }
                                @endphp
                                <textarea name="bill_to" id="bill_to" class="form-control" placeholder="Enter Bill To">{{$bill_to_value}}</textarea>                                
                                <span class="kt-form__help error bill_to"></span>
                            </div>

                            <input type="hidden"  id="address_1" value="{{ $data->address ??'' }}">
                            <input type="hidden"  id="address_2" value="{{ $data->registration ??'' }}">
                            <div class="form-group col-md-6">
                                <label class="control-label">Address</label>
                                @php
                                    $address_set = ''; 

                                    if ($orders_data->invoice_date != NULL && $orders_data->invoice_no != NULL) {
                                        $address_set = $orders_data->address;
                                    }else{
                                        $address_set = $data->address;
                                    }
                                @endphp

                                <textarea name="address" id="address" class="form-control" placeholder="Enter Address">{{ $address_set}}</textarea>                                
                                <span class="kt-form__help error address"></span>
                            </div>

                            <div class="form-group col-md-3">
                                <label class="control-label">Ship To</label>
                                <!-- <br/>
                                <span class="btn btn-outline-secondary mb-2">&nbsp;&nbsp;&nbsp;<input id="address_ch_1_s" type="checkbox" class="form-check-input" value=""><b>Address Correspondence</b></span>

                                <span class="btn btn-outline-secondary mb-2">&nbsp;&nbsp;&nbsp<input id="address_ch_2_s" type="checkbox" class="form-check-input" value=""><b>Registration Correspondence</b></span> -->
                                @php
                                    $registration_set = ''; 

                                    if ($orders_data->invoice_date != NULL && $orders_data->invoice_no != NULL) {
                                        $registration_set = $orders_data->ship_to;
                                    }else{
                                        $registration_set = $data->registration;
                                    }
                                @endphp

                                <textarea name="ship_to" id="ship_to" class="form-control" placeholder="Enter Ship To">{{$registration_set}}</textarea>                                
                                <span class="kt-form__help error ship_to"></span>
                            </div>

                            <div class="form-group col-sm-3">
                                <label for="name">Financed by/Hypo. by</label>
                                <input type="text" class="form-control" disabled="disabled" value="{{ isset($finance_name) ? $finance_name : '' }}">
                                <input type="hidden" name="finance_name" value="{{ $finance_name ??'' }}">
                            </div>   

                            @if(isset($data->customer_type) && $data->customer_type != NULL && $data->customer_type == 'corporate')
                            <div class="form-group col-sm-3">
                                <label for="name">Customer GST No</label>
                                <input type="text" class="form-control" disabled="disabled" value="{{ $data->gst ??'' }}">
                                <input type="hidden" name="customer_gst" value="{{ $data->gst ??'' }}">
                            </div>      
                            @endif

                            <div class="form-group col-sm-3">
                                <label for="name">Customer ID</label>
                                <input type="text" class="form-control" disabled="disabled" value="{{ $data->id ??'' }}">
                                <input type="hidden" name="customer_id" value="{{ $data->id ??'' }}">
                            </div>  

                            <div class="form-group col-sm-3">
                                <label for="name">Invoice No</label>
                                <input type="text" class="form-control" name="invoice_no" value="" placeholder="{{ $invoice_prefix }}" required> 
                                <!--<input type="hidden" name="invoice_no" value="{{ $invoice_prefix }}">-->
                            </div>   

                            <div class="form-group col-sm-3">
                                <label for="name">Invoice Date</label>
                                @php
                                    $current_date_get = ''; 
                                    $current_date_get_set = ''; 

                                    if ($orders_data->invoice_date != NULL && $orders_data->invoice_no != NULL) {
                                        $current_date_get = $orders_data->invoice_date;
                                    }else{
                                        $current_date_get_set = $current_date;                                        
                                        $current_date_get = $current_date;                                        
                                    }
                                @endphp
                                <input type="text" name="invoice_date" class="form-control" value="{{$current_date_get_set}}" required>
                                <!--<input type="hidden" name="invoice_date" value="{{$current_date_get_set}}">-->
                            </div>   
                            
                            <div class="form-group col-sm-3">
                                <label for="name">Phone No</label>
                                <input type="text" class="form-control" disabled="disabled" value="{{ $data->contact_number ?? '' }}">
                                <input type="hidden" name="phone_no" value="{{ isset($data->contact_number) ? $data->contact_number : '' }}">
                            </div>   

                            <div class="form-group col-sm-3">
                                <label for="name">PAN No</label>
                                <input type="text" class="form-control" disabled="disabled" value="{{ $data->pan_number ??'' }}">
                                <input type="hidden" name="pan_number" value="{{ $data->pan_number ??'' }}">
                            </div>  

                            <div class="form-group col-sm-3">
                                <label for="name">Place of Supply</label>
                                <input type="text" class="form-control" disabled="disabled" value="GUJARAT">
                                <input type="hidden" name="place_of_supply" value="GUJARAT">
                            </div>
                            
                            <div class="form-group col-sm-3">
                                <label for="name">GST No</label>
                                <input type="text" class="form-control" disabled="disabled" value="{{ $data->gst ??'' }}">
                                <input type="hidden" name="customer_gst" value="{{ $data->gst ??'' }}">
                            </div>    
                            @php
                            
                            $cess = $deal_data->cess_tax_deal ?? '0';
                            $tcs = $deal_data->tcs_tax_deal ?? '0';

                            $on_road_price = isset($deal_data->deal_amt) ? $deal_data->deal_amt : 0;

                            $gst_amount = ($tex_master_gst / 100) * $on_road_price;
                            
                            $cgst = $sgst = $igst = 0;
                            if($data->state_id == 7 || $data->state_id == 'Gujarat'){
                                $cgst = $make_deal_gst_amt / 2;
                                $sgst = $make_deal_gst_amt / 2;
                                $igst = 0;
                            } else {
                                $cgst = 0;
                                $sgst = 0;
                                $igst = $make_deal_gst_amt;
                            }

                            $total = $on_road_price + $make_deal_gst_amt + $cess; 
                            
                            $grand_total = $total + $tcs; 

                            @endphp

                            <div class="col-sm-12">
                                <div class="table-responsive">
                                  <table class="table">
                                    <thead>
                                      <tr>
                                        <th><b>PARTICULARS</b></th>
                                        <th><b>AMOUNT(Rs)</b></th>                                       
                                      </tr>
                                    </thead>
                                    <tbody> 
                                      <tr>
                                        <th>{{ isset($product_name_get) ? $product_name_get : '' }}</th>
                                        <td>{{ isset($deal_data->deal_amt) ? $deal_data->deal_amt : '' }}</td>
                                      </tr>
                                      @if($data->state_id == '7' || $data->state_id == 'Gujarat')
                                      <tr>
                                        <th>CGST ({{$make_deal_gst_percentage/2}}%)</th>
                                        <td>{{ $make_deal_gst_amt / 2 }}</td>
                                      </tr>
                                      <tr>
                                        <th>SGST ({{$make_deal_gst_percentage/2}}%)</th>
                                        <td>{{ $make_deal_gst_amt / 2 }}</td>
                                      </tr>
                                      @else
                                      <tr>
                                        <th>IGST ({{$make_deal_gst_percentage}}%)</th>
                                        <td>{{ $make_deal_gst_amt }}</td>
                                      </tr>
                                      @endif
                                      <tr>
                                        <th>Compensation Cess ({{$tex_master_compensation_cess}}%)</th>
                                        <td>{{$cess}}</td>
                                      </tr>
                                      <tr>
                                        <th><b>TOTAL</b></th>
                                        <td>{{round($total)}}</td>
                                      </tr>
                                      <tr>
                                        <th><b>TCS @1%</b></th>
                                       <td>{{ $tcs}}</td>
                                      </tr>
                                      <tr>
                                        <th><b>GRAND TOTAL</b></th>
                                        <td>{{round($grand_total)}}</td>
                                      </tr>
                                    </tbody>
                                  </table>
                                </div>
                            </div>

                            <input type="hidden" name="product_name" value="{{ isset($product_name_get) ? $product_name_get : '' }}">
                            <input type="hidden" name="on_road_price" value="{{ isset($deal_data->deal_amt) ? $deal_data->deal_amt : '' }}">
                            <input type="hidden" name="cgst" value="{{$cgst}}">
                            <input type="hidden" name="sgst" value="{{$sgst}}">
                            <input type="hidden" name="igst" value="{{$igst}}">
                            <input type="hidden" name="compensation_cess" value="{{$cess}}">
                            <input type="hidden" name="total" value="{{$total}}">
                            <input type="hidden" name="tcs_tax" value="{{$tcs}}">
                            <input type="hidden" name="grand_total" value="{{$grand_total}}">
                            
                            <input type="hidden" name="vin_number" value="{{ isset($inventory_data->vin_number) ? $inventory_data->vin_number : '' }}">
                            <input type="hidden" name="chassis_number" value="{{ isset($inventory_data->chassis_number) ? $inventory_data->chassis_number : '' }}">
                            <input type="hidden" name="engine_number" value="{{ isset($inventory_data->engine_number) ? $inventory_data->engine_number : '' }}">
                            <input type="hidden" name="key_number" value="{{ isset($inventory_data->key_number) ? $inventory_data->key_number : '' }}"> 
                            <input type="hidden" name="hsn_code" value="{{ isset($hsn_code) ? $hsn_code : '' }}"> 

                            <div class="col-sm-12">
                                <h5>Vehicle Particulars</h5>
                                <div class="table-responsive">
                                  <table class="table">
                                    <tbody> 
                                      <tr>
                                        <th><b>Vin No</b></th>
                                        <th><b>Chassis No</b></th>
                                        <th><b>Engine No</b></th>
                                        <th><b>Key No</b></th>
                                        <th><b>HSN No</b></th>
                                      </tr>
                                      <tr>
                                        <td>{{ isset($inventory_data->vin_number) ? $inventory_data->vin_number : '' }}</td>
                                        <td>{{ isset($inventory_data->chassis_number) ? $inventory_data->chassis_number : '' }}</td>
                                        <td>{{ isset($inventory_data->engine_number) ? $inventory_data->engine_number : '' }}</td>
                                        <td>{{ isset($inventory_data->key_number) ? $inventory_data->key_number : '' }}</td>
                                        <td>{{ isset($hsn_code) ? $hsn_code : '' }}</td>
                                      </tr>
                                    </tbody>
                                  </table>
                                </div>
                            </div>

                            <!--<div class="form-group col-sm-3">-->
                            <!--    <label for="name"> Applied Promotion</label>-->
                            <!--    <input type="text" class="form-control" disabled="disabled" value="-">-->
                            <!--</div>  -->

                        </div>


                         
                       

                        <div class="form-group">
                            <button type="submit" class="btn waves-effect waves-light btn-rounded btn-outline-primary">Submit</button>
                            <a href="{{ route('order') }}" class="btn waves-effect waves-light btn-rounded btn-outline-secondary">Back</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        @endif
    </div>
</div>
@endsection

@section('scripts')
<script>
    $("#percentage").keyup(function(e){
        $('.percentage').html('');
        if ($(this).val() > 99){
            $('.percentage').html('');
            $('.percentage').html('Enter Value Between 0 to 100!');
            $(this).val();
            $(this).val(100);
        }else if($(this).val() <= 99){
            $('.percentage').html('');
        }
    });

</script>

<script>
    $(document).ready(function() {
        var form = $('#form');
        $('.kt-form__help').html('');
        form.submit(function(e) {
            $('.help-block').html('');
            $('.m-form__help').html('');
            $.ajax({
                url: form.attr('action'),
                type: form.attr('method'),
                data: new FormData($(this)[0]),
                dataType: 'json',
                async: false,
                processData: false,
                contentType: false,
                success: function(json) {
                    return true;
                },
                error: function(json) {
                    if (json.status === 422) {
                        e.preventDefault();
                        var errors_ = json.responseJSON;
                        $('.kt-form__help').html('');
                        $.each(errors_.errors, function(key, value) {
                            $('.' + key).html(value);
                        });
                    }
                }
            });
        });
    });

    $('#bill_to_1').on('click', function(){
        $('#bill_to').val($('#customer_name').val());
        $('#bill_to_2').prop('checked',false);
    });

    $('#bill_to_2').on('click', function(){
        $('#bill_to').val($('#company_name').val());
        $('#bill_to_1').prop('checked',false);
    });

    $('#address_ch_1').on('click', function(){
        $('#address').val($('#address_1').val());
        $('#address_ch_2').prop('checked',false);
    });

    $('#address_ch_2').on('click', function(){
        $('#address').val($('#address_2').val());
        $('#address_ch_1').prop('checked',false);
    });

    $('#address_ch_1_s').on('click', function(){
        $('#ship_to').val($('#address_1').val());
        $('#address_ch_2_s').prop('checked',false);
    });

    $('#address_ch_2_s').on('click', function(){
        $('#ship_to').val($('#address_2').val());
        $('#address_ch_1_s').prop('checked',false);
    });
</script>
@endsection