$(document).ready(function(){
  $('#quotation_create').click(function(){

    generateQUOTATION_NO();
    setcompanydataintable();
    $("#add_new_quotation").modal('show');
  });

  $('#quotation_product').click(function(){
    $("#invoice_product").modal('show');
  });
  
  $('#quotation_creatation').css("width","1170px");

 
 
});


function generateQUOTATION_NO()
{
  $.ajaxSetup({

    headers: {
      
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      
    }
      
    });  
      $.ajax({
      
        url:url_generatenumber,
      
        type:"POST",
      
        data:{
            "quotation_number":"quotation_number"
        },
         
        success:function(data)
        {
          if(data.quotation_no==null)
          {
            var currentyear=new Date().getFullYear();
            var final=('QRO'+currentyear+'0001');
            $(document).find('#generated_quotationcode').val(final);
 
          }
          else
          {
            var lastno=data.quotation_no; 
            var currentmonth=new Date().getMonth() ;
						var currentyear=new Date().getFullYear() ;

						if(currentmonth in ([0,1,2]))
						{
							--currentyear;
						}
						yearcheck =lastno.substr(3, 4);
					
						if(currentmonth == 3 && yearcheck != currentyear)
						{							
								tmpNum = '0001';
						}else{						
							var tmpNum = lastno.slice(7);
							tmpNum++;
						}
						
					var getfinalnumber=pad(tmpNum,"4");
					var final=('QRO'+currentyear+getfinalnumber);
						console.log(final);
            
            $(document).find('#generated_quotationcode').val(final);
          }
         

        }
      });
}

function pad (str, max) {
  str = str.toString();
  return str.length < max ? pad("0" + str, max) : str;
}

function setcompanydataintable()
{
  $.ajaxSetup({
    headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    } 
  });

  $.ajax({  
    url:url_getcompanyfortable,

    method:"GET",

    data:{},

    success:function(result)

    {     
      console.log(result);
    }

 });
} 