<?php 
include "includes/header.php"; 
include "includes/Mdme-functions.php";
$mdme = getMdmeProduct_flowEntry($db,'');
include_once "includes/pagepermission.php";


//check permission - START
if(!($res_modm_prm)){
	header("Location:".admin_public_url."error.php");
}
//check permission - END

$id=$_REQUEST['id'];
if($id!="")
{
	
//check edit permission - START	
if(trim($res_modm_prm['EditPrm'])=="0") {
?>
<script>
  window.location="error.php";
</script>
<?php	
}
//check edit permission - END	

$operation="Edit";
$act="update";
$btn='Update';

$str_ed = "select * from t_product_flow_details where IsActive != '2' and p_id = '".base64_decode($id)."' ";
$res_ed = $db->get_a_line($str_ed);


$edit_id = $res_ed['p_id'];
$disp_inv_no = $res_ed['actu_inv_no'];


}
else
{
	
//check edit permission - START	
if(trim($res_modm_prm['AddPrm'])=="0") {
?>
<script>
  window.location="error.php";
</script>
<?php	
}
//check edit permission - END
	$operation="Add";
	$act="insert";
	$btn='Save';
	

$disp_pdt_cnt=1;

//$disp_over_all_tot=0;
//$edit_id=0;
}
?>  

<div class="wrapper">
    <div class="container">        
		<!-- Page-Title - START -->
        <div class="row">
            <div class="col-sm-12">										
                <h4 class="page-title"><?php echo $operation; ?> Weaver's Entry</h4>						
            </div>
        </div>
		<!-- Page-Title - END -->	
										<?php
											$display = ($operation === "Edit") ? 'none' : ''; 
										?>
					
		<div class="row">
			<div class="col-sm-12">
				<form id="jvalidate" name="frmPurchase" role="form" class="form-horizontal" action="#" method="post" enctype="multipart/form-data" >
				<input type="hidden" name="action" value="<?php echo $act; ?>" />
				<input type="hidden" name="edit_id" id="edit_id" value="<?php echo $edit_id; ?>"  />
				<input type="hidden" name="hdn_pdt_sno" id="hdn_pdt_sno" value="<?php echo $disp_pdt_cnt; ?>" />
				
					<div class="card-box">				
						
						<div class="row">

							<div class="col-sm-3">
							
								<div class="form-group">
									<label>Financial Year<span class="text-danger">*</span></label>							
									<select onchange="get_inv_no_by_cmpny_id($('#sel_company').val());" name="financial_id" id="financial_id" class="form-control select2 jsrequired">
										<option value="">Select</option>
										<?php 
										$rslt_fy_list = $db->get_rsltset("select financial_id,f_start_date,f_end_date from m_financial_year order by f_start_date asc");
										$current_fy_id = '';
										if($act=='insert') {
											$current_fy_details = end($rslt_fy_list);
											$current_fy_id = $current_fy_details[0]['financial_id'];
										}
										foreach($rslt_fy_list as $r){
											$financial_year_label = date('Y',strtotime($r['f_start_date'])).'-'.date('Y',strtotime($r['f_end_date']));
											if($act=='update')
												$sel= ($res_ed['financial_id']==$r['financial_id'])?' selected="selected" ':'';
											else
												$sel= ($current_fy_id==$r['financial_id'])?' selected="selected" ':'';
										?>
										<option value="<?php echo $r['financial_id']; ?>" <?php echo $sel; ?>><?php echo $financial_year_label; ?></option>
										<?php	
										}
										?>
									</select>
								</div>	
								
							</div>	

							

                           <div class="col-sm-3">
                            <div class="form-group">
									<label> Date</label>							
									<input type="text" class="form-control jsrequired" name="txt_inv_date" id="txt_inv_date" value="<?php echo  date('d-m-Y', strtotime($res_ed['p_date'])); ?>"  />
								</div>	
                            </div>
							
                            <div class="col-sm-3">	</div>
						</div>
<br>

						<div class="row">
							<div class="col-md-12">
								<div class="table-responsive challan-table">
									<table class="table table-bordered table-hover" id="tbl_item_det">
										<thead>
											<tr>
												<!--<th width="1%">#</th>-->
												<th width="30%">Supplier </th>
												<th width="20%">Product Type</th>
												<th width="20%">Available in Grams</th>
												<th width="15%">Product Flow</th>
												<th width="17%">Grams</th>
												<th width="15%"></th>
												
											</tr>
										</thead>
										<tbody id="tbl_body_category_det">
											<?php 
											if($pdt_sv_cnt >0){
												$ai=0;
												foreach($rslt_pdt_list as $r){
													$ai = $ai+1;
													$pdt_prev_img='NoImage.png';
													if (!empty($r['upload_img']) && ($act == 'update')) { 
														if(file_exists(CAAROOT_DIR."/uploads/product_image/".$r['upload_img']))
														{
															$pdt_prev_img = $r['upload_img'];	
														}											
													}	
												?>	
												
												<tr id="dy_rw_<?php echo $ai; ?>">
													<input type="hidden" name="hdn_pdt_cnt[]" value="<?php echo $ai; ?>" />
													<input type="hidden" name="hdn_prev_sv_id[]" value="<?php echo $r['p_invoice_det_id']; ?>" />

													<td>
                                        <select onchange="get_supplier_details(this.value);" name="sel_supplier[]" id="sel_supplier_<?php echo $ai; ?>" class="form-control select2 jsrequired">
										<option value="">Select</option>
										<?php 
										$rslt_list = $db->get_rsltset("select sup_id AS Id,sup_name AS Name from m_supplier where IsActive = '1' and sup_product = '2' order by sup_name asc");
										foreach($rslt_list as $r){
											$sel='';
											if($res_ed['sup_id']==$r['Id'])
												$sel=' selected="selected" ';
										?>
										<option value="<?php echo $r['Id']; ?>" <?php echo $sel; ?>><?php echo $r['Name']; ?></option>
										<?php	
										}
										?>
									</select>
													</td>

													<td>
                                <select onchange="sendAjaxRequest(this.value, <?php echo $ai; ?>)" name="txt_pdt_type[]" id="txt_pdt_type_<?php echo $ai; ?>"  class="form-control select2 jsrequired">
										<option value="">Select</option>
										<?php 
										$rslt_list = $db->get_rsltset("select product_id AS Id,product_name AS Name from m_product where IsActive = '1' order by product_name asc");
										foreach($rslt_list as $r){
											$sel='';
											if($res_ed['pdt_type']==$r['Id'])
												$sel=' selected="selected" ';
										?>
										<option value="<?php echo $r['Id']; ?>" <?php echo $sel; ?>><?php echo $r['Name']; ?></option>
										<?php	
										}
										?>
									</select>
										<div class="row" style="padding-left: 20px ;">
											<span id="pdt_total_<?php echo $ai; ?>" style="color:green; font-size:12px;"></span>
											
										</div>
													</td>
													<td id="pdt_total_cell_<?php echo $ai; ?>" class="pdt-total-cell" style="color:green; font-size:12px;" value=""></td>
   

													<td>
                                                    <select onchange="get_flow(this.value, <?php echo $ai; ?>);" name="product_flow[]" id="product_flow_<?php echo $ai; ?>" class="form-control select2 jsrequired">
                                                        <option value="">Select Flow</option>
                                                        <option value="1">Inward</option>
                                                        <option value="2">Outward</option>
                                                    </select>

													</td>
													
													<td>
														<input type="text" class="form-control cls_pdt_rate" name="txt_pdt_grm[]" id="txt_pdt_grm_<?php echo $ai; ?>"  value="<?php echo $r['qty_grm']; ?>" onKeyPress="return isNumberKey(event,this)"  />
														
														<?php 
														if($ai >1){
														?>
														<a onclick="delete_pdt_row(<?php echo $ai; ?>,<?php echo $r['p_invoice_det_id']; ?>)" href="javascript:;"><span class="icon-close" style="display:inline"></span></a> 
														<?php	
														}
														?>
												</td>
												
												
													<td>
														<input type ="" class="btn " name ="btn_type[]" id="btn_type_<?php echo $ai; ?>" value=""/>
													</td>	

												</tr>
												
												<?php		
												}												
											}else{
											?>
											
											<tr id="dy_rw_1">
												<input type="hidden" name="hdn_pdt_cnt[]" value="1" />
												<input type="hidden" name="hdn_prev_sv_id[]" value="0" />
                                                <td>
                                        <select onchange="get_supplier_details(this.value);" name="sel_supplier[]" id="sel_supplier_1" class="form-control select2 jsrequired">
										<option value="">Select</option>
										<?php 
										$rslt_list = $db->get_rsltset("select sup_id AS Id,sup_name AS Name from m_supplier where IsActive = '1' and sup_product = '2' order by sup_name asc");
										foreach($rslt_list as $r){
											$sel='';
											if($res_ed['sup_id']==$r['Id'])
												$sel=' selected="selected" ';
										?>
										<option value="<?php echo $r['Id']; ?>" <?php echo $sel; ?>><?php echo $r['Name']; ?></option>
										<?php	
										}
										?>
									</select>
													</td>

												<td>
                                        <select onchange="sendAjaxRequest(this.value, 1)"  name="txt_pdt_type[]" id="txt_pdt_type_1" class="form-control select2 jsrequired">
										<option value="">Select</option>
										<?php 
										$rslt_list = $db->get_rsltset("select product_id AS Id,product_name AS Name from m_product where IsActive = '1' order by product_name asc");
										foreach($rslt_list as $r){
											$sel='';
											if($res_ed['pdt_type']==$r['Id'])
												$sel=' selected="selected" ';
										?>
										<option value="<?php echo $r['Id']; ?>" <?php echo $sel; ?>><?php echo $r['Name']; ?></option>
										<?php	
										}
										?>
									</select>
									<div class="row" style="padding-left: 20px ;">
										<span id="pdt_total_1" style="color:green; font-size:12px;"></span>
										
									</div>
								</td>

									<td id="pdt_total_cell_1" style="color:green; "></td>
   
												<td>
                                                <select onchange="get_flow(this.value, 1);" name="product_flow[]" id="product_flow_1" class="form-control select2 jsrequired">
                                                    <option value="">Select Flow</option>
        <option value="1" <?php if ($res_ed['p_flow'] == '1') echo 'selected="selected"'; ?>>Inward</option>
        <option value="2" <?php if ($res_ed['p_flow'] == '2') echo 'selected="selected"'; ?>>Outward</option>
    </select>
												</td>
												
												<td>
                                                    <input type="text" class="form-control cls_pdt_rate" name="txt_pdt_grm[]" id="txt_pdt_grm_1" onKeyPress="return isNumberKey(event,this)"  value="<?php echo $res_ed['qty_grm']; ?>"/>
												</td>	
												<td>
                                                    <?php
                                                    if ($res_ed['p_flow'] == '1') {
                                                        // Code to execute if p_flow is 1
                                                        echo '<input type="" class="btn btn-primary" name="btn_type[]" id="btn_type_1" value="Debit">';
                                                    } else if ($res_ed['p_flow'] == '2') {
                                                        // Code to execute if p_flow is 2
                                                        echo '<input type="" class="btn btn-success " name="btn_type[]" id="btn_type_1" value="Credit">';
                                                    }
                                                    else{
                                                        echo '<input type="" readonly class="btn  " name="btn_type[]" id="btn_type_1" value="">';
                                                    }
                                                    ?>
                                                </td>
	

													

											</tr>

											<?php	
											}
											?>	
																						
										</tbody>
										<tfoot>
											
											<tr style="display:<?php echo $display; ?> ;">
												<td colspan="5">&nbsp;</td>
												<td>
													<a onclick="add_dynamic_pdt_rw()" class="btn btn-warning waves-effect waves-light" href="javascript:;">Add <span class="m-l-5"> <i class="fa fa-plus"></i></span></a>
												</td>
											</tr>
											
											
											
										</tfoot>
									</table>
								</div>
							</div>
						</div>
						
						<div class="row">
							<div class="col-sm-4">&nbsp;</div>
					
							<div class="col-sm-8">						
								<!--<div class="form-group">
									<div class="btn-group pull-right m-t-15">
										<label>Over All Total</label>
										<input readonly type="text" class="form-control" name="txt_over_all_total" id="txt_over_all_total" value="<?php echo $disp_over_all_tot; ?>" />
									</div>
								</div>	-->								
							</div>							
						</div>
						
						<div class="row">
							<div class="col-sm-8">&nbsp;</div>
					
							<div class="col-sm-4">						
								<div class="form-group">
                                <div class="btn-group pull-right m-t-15">
										
										<button class="btn btn-primary waves-effect waves-light" type="button" onClick="javascript:funSave_purchase('frmPurchase','product_flow_actions.php','jvalidate','purchase','product_flow_mng.php');" ><span id="spSubmit"><?php echo $btn; ?> Details</span></button>
										
										<button class="btn btn-secondary waves-effect m-l-5" type="button" onClick="javascript:funCancel('frmPurchase','jvalidate','purchase','product_flow_mng.php');" >Cancel</button>													 
										
									</div>
								</div>						
							</div>
							
						</div>
										
						
					</div>
					
				</form>
            </div>
        </div> <!-- end row -->	
			

<?php include "includes/footer.php";  ?> 
	 </div> <!-- container -->
</div> <!-- End wrapper -->

<?php include "includes/footer_script.php";  ?> 

<script type="text/javascript" src="assets/plugins/autocomplete/jquery.autocomplete.min.js"></script>

<script type="text/javascript"> 

function setReadOnlyAttributes(flow_value, rw_cnt) {
    var credit_debit_btn = $("#btn_type_" + rw_cnt);
    
    if (flow_value == "1") { // Inward flow
        credit_debit_btn.removeClass().addClass("btn btn-primary waves-effect waves-light block").val("Debit").prop("disabled", false);
    } else if (flow_value == "2") { // Outward flow
        credit_debit_btn.removeClass().addClass("btn btn-success waves-effect waves-light").val("Credit").prop("disabled", false);
    } else { // Default
        credit_debit_btn.prop("disabled", true); // Disable the button
    }
}


function get_flow(flow_value, rw_cnt) {


    // Call the common function to set readonly attributes
    setReadOnlyAttributes(flow_value, rw_cnt);
}


function sendAjaxRequest(product_type, rw_cnt) {
    var pdtType = product_type;
    var financial_id = $('#financial_id').val();

    // Construct the dynamic ID for the cell element
    var cellId = 'pdt_total_cell_' + rw_cnt;

    // Check if a product type is selected
    if (pdtType !== "") {
        // Send AJAX request
        $.ajax({
            url: 'ajax_actions.php',
            type: 'POST',
            dataType: 'json',
            data: 'action=get_pdt_type_qty&product_type=' + pdtType + '&financial_id=' + financial_id,
            beforeSend: loading(),
            success: function(response) {
				console.log('Cell ID: ' + cellId);
    			console.log('Response Quantity: ' + response.pdt_qty);
                // Handle the response from the server
                // console.log(response.pdt_qty);

                // Show the value received from AJAX in the dynamically generated cell element
                // $('#' + cellId).text(response.pdt_qty).show();

				var cell = document.getElementById(cellId);
				
				console.log(cell);

// Check if the cell exists
if (cell) {
	// Update the content of the cell with the response quantity using innerHTML
	cell.innerHTML = response.pdt_qty;
} else {
	console.error('Cell with ID ' + cellId + ' not found.');
}
            },
            error: function(xhr, status, error) {
                // Handle errors
                console.error(xhr, status, error);
            }
        });
    } else {
        // If no product type is selected, hide the dynamically generated cell element
        $('#' + cellId).hide();
    }
    unloading();
}




$(document).ready(function(){

    $(".select2").select2({
        width: '100%',
    });
});

$(function () {	
	var date = new Date();
	var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());		
	$('#txt_inv_date').datepicker({	
		format: 'dd-mm-yyyy',
		startDate: '-100y',
		endDate: '+0d',
		autoclose: true,	
		assumeNearbyYear:20,	
		todayHighlight: true
		
	});	
	
	<?php if($act=="insert"){ ?> 
	$('#txt_inv_date').datepicker('setDate', today);
	<?php } ?>

});


function add_dynamic_pdt_rw() {			
   

    var rw_cnt = parseInt($("#hdn_pdt_sno").val()) + 1;

    <?php 
        // Fetch product options from the database
        $rslt_list = $db->get_rsltset("SELECT m_product.*, product_id AS Id, product_name AS Name FROM m_product WHERE IsActive = '1' ORDER BY product_name ASC");

        // Construct options HTML
        $optionsHtml = '<option value="">Select</option>';
        foreach($rslt_list as $r) {
            $sel = ($res_ed['pdt_type'] == $r['Id']) ? 'selected="selected"' : '';
            $optionsHtml .= '<option value="' . $r['Id'] . '" ' . $sel . '>' . $r['Name'] . '</option>';
        }
        
        $rslt_list_sup = $db->get_rsltset("select sup_id AS Id,sup_name AS Name from m_supplier where IsActive = '1' and sup_product = '2' order by sup_name asc");
        $supplierHtml =  '<option value="">Select</option>';
        foreach($rslt_list_sup as $r){
            $sel='';
            if($res_ed['sup_id']==$r['Id'])
                $sel=' selected="selected" ';
                $supplierHtml.=   '<option value="' . $r['Id'] . '" ' . $sel . '>' . $r['Name'] . '</option>';
        }
    ?>

var dy_html = '<tr id="dy_rw_' + rw_cnt + '">' +
                    '<input type="hidden" name="hdn_pdt_cnt[]" value="' + rw_cnt + '" />' +
                    '<input type="hidden" name="hdn_prev_sv_id[]" value="0" />' +
                    '<td>' +
                        '<select class="form-control select2 jsrequired " name="sel_supplier[]" id="sel_supplier_' + rw_cnt + '" >' +
                            '<?php echo $supplierHtml; ?>' +
                        '</select>' +
                    '</td>' +
                    '<td>' +
                        '<select onchange="sendAjaxRequest(this.value, ' + rw_cnt + ')"  class="form-control select2 jsrequired pdtskucls" name="txt_pdt_type[]" id="txt_pdt_type_' + rw_cnt + '" >' +
                            '<?php echo $optionsHtml; ?>' +
                        '</select>' +
						'<div class="row" style="padding-left: 20px ;">'+
										'<span id="pdt_total_' + rw_cnt + '" style="color:green; font-size:12px;"></span>'+
										
									'</div>'+
                    '</td>' +
					
                    '<td id="pdt_total_cell_' + rw_cnt + '" style="color:green; "></td>' +

                    '<td>' +
                        '<select onchange="get_flow(this.value, ' + rw_cnt + ');" name="product_flow[]" id="product_flow_' + rw_cnt + '" class="form-control select2 jsrequired">' +
                            '<option value="">Select Flow</option>' +
                            '<option value="1">Inward</option>' +
                            '<option value="2">Outward</option>' +
                        '</select>' +
                    '</td>' +
                    
                    '<td>' +
                        '<input type="text" class="form-control cls_pdt_rate" name="txt_pdt_grm[]" id="txt_pdt_grm_' + rw_cnt + '" onKeyPress="return isNumberKey(event,this)"  />' +
                    '</td>' +
                    '<td>' +
                        '<input type ="" readonly class="btn " name ="btn_type[]" id="btn_type_' + rw_cnt + '" value=""/>' +
                       '<a onclick="remove_pdt_row('+rw_cnt+')" href="javascript:;"><span class="icon-close" style="display:inline"></span></a>'+
                    '</td>' +
                    
                    
                '</tr>';



    $("#tbl_body_category_det").append(dy_html);
    $("#dy_rw_" + rw_cnt + " .select2").select2();
    $("#hdn_pdt_sno").val(rw_cnt);	

    auto_complete_intialize(rw_cnt);
	$(".select2").select2({
        width: '100%',
    });
}

function remove_pdt_row(rw_id){
	$("#dy_rw_"+rw_id).remove();
	
	final_amount_calculations();
}

function readURL1(input,rw_id) { 
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $("[id*=preview_img_"+rw_id+"]").attr("src", e.target.result);
        };
        reader.readAsDataURL(input.files[0]);       
    }
}





function funSave_purchase($frm,$urll,$acts,$stats,$lodlnk)
{
	if ($('#'+$acts).valid()) {
		if($('#txt_over_all_total').val() == '0'){
			swal("Warning!", "Your total amount should not be zero", "warning");	
			return false;
		}
		//check validate
		var chk_validate = check_validate();
		if(chk_validate == true){
			
			$("button").attr('disabled',true);
			
			var m_data = new FormData();
			var formdatas = $("#"+$acts).serializeArray();
			$.each( formdatas, function( key, value ) {
				 m_data.append( value.name, value.value);							 
			});
			
			var c=0;
			var file_data;
			$('input[type="file"]').each(function(){
				file_data = $('input[type="file"]')[c].files;
				for(var i = 0;i<file_data.length;i++){
                   m_data.append("uploadimg_"+c, file_data[i]); // we can put more than 1 image file
				}				
				c++;
			});
						

			$.ajax({
				url        : $urll,
				type	   : 'POST',
				dataType   : 'json',
				processData: false,
				contentType: false,
				data       : m_data,
				beforeSend: function() { loading(); },
				success: function(response){
					if(response.rslt == "1"){
						swal("Success!", "Invoice details save successfully", "success");										
						$("#"+$acts)[0].reset();
						$(location).attr('href', $lodlnk);
						unloading();
					}
					else if(response.rslt == "2"){
						swal("Success!", "Invoice details update successfully", "success");										
						$("#"+$acts)[0].reset();
						$(location).attr('href', $lodlnk);
						unloading();
					}
					else if(response.rslt == "3"){
						swal("Error!", response.err_msg, "warning");										
						//$("#"+$acts)[0].reset();
						//$(location).attr('href', $lodlnk);
						unloading();
					}					
					else{
						swal("Failure!", "somehing missing try some time", "warning");
						unloading();
					}
					unloading();
					
					$("button").attr('disabled',false);
				}
			});	
			
		}
	}
}

function check_validate(){
	var rslt = true;
		
	return rslt;
}

function delete_pdt_row(row_id, pdt_det_id){
	if(row_id !='' && pdt_det_id !=''){
		swal({
			title: "Are you sure?",
			text: "You will not be able to recover this details!",
			type: "warning",
			showCancelButton: true,
			confirmButtonColor: "#DD6B55",
			confirmButtonText: "Yes, delete it!",
			closeOnConfirm: true,
			closeOnCancel: true
		},
		function () {			
			 $.ajax({
				url: 'purchase_actions.php',
				type: 'POST',
				dataType : 'json',
				data: 'action=del_pdt_det&pdt_det_id='+pdt_det_id,
				beforeSend: loading(),
				success: function(response) {				
					if(response.rslt == "1"){
						swal("Success!", "Product Delete Successfully", "success");
						$("#dy_rw_"+row_id).remove();
	
						final_amount_calculations();
					}					
					else if(response.rslt == "-9"){
						swal("Failure!", response.err_msg, "warning");
					}
					else{
						swal("Failure!", "somehing missing try some time", "warning");
					}					
					unloading();
				}
			});
		}); 	
	}	
}


function check_pdt_sku(txt_bx_id,pdt_sku){
	if(pdt_sku){
		
		if($("#sel_company").val()){
		
			$.ajax({
				url: 'purchase_actions.php',
				type: 'POST',
				dataType : 'json',
				data: 'action=check_pdt_sku&pdt_sku='+pdt_sku+'&company_name='+$("#sel_company").val()+'&invoice_id='+$("#edit_id").val(),
				beforeSend: loading(),
				success: function(response) {				
					if(response.rslt == "-9"){
						swal("Failure!", response.err_msg, "warning");
						$("#"+txt_bx_id).val("");
					}				
					unloading();
				}
			});

			  var obj = {};
  			  var i = 0;

			 $(".pdtskucls").each(function(){			 	 
			     if(obj.hasOwnProperty(this.value)) {
			      alert("there is a duplicate value " + this.value);
			      $('#'+txt_bx_id).val("")
			    } else {
			      obj[this.value] = this.value;
			    }
			  });

		}
		else{
			swal("Failure!", "Please Select Company", "warning");
		}
	}
}


function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	
   return true;
}


function get_supplier_details(sup_id){	
	
	if(sup_id !=''){	
		var psno = $('#hdn_pdt_sno').val();
		$('#txt_pdt_desc_'+psno).val('');
		$.ajax({
			url: 'ajax_actions.php',
			type: 'POST',
			dataType : 'json',
			data: 'action=get_supplier_det_by_sup_id&supplier_id='+sup_id,
			beforeSend: loading(),
			success: function(response) {				
				if(response.rslt == "1"){					
					//cst tax value bind on 
					var supplier_gst_no = response.supplier_gst;			
					var sup_address = response.sup_address;
					var sup_city = response.sup_city;
					var sup_state = response.sup_state;
					var sup_postcode = response.sup_postcode;

					$("#supplier_gst_no").text(supplier_gst_no);	
					$("#supplier_address").text(sup_address);	
					$("#supplier_city").text(sup_city);	
					$("#supplier_state").text(sup_state);	
					$("#supplier_postcode").text(sup_postcode);	
					$(".row").show();

					var sup_state_name = response.sup_state.toLowerCase().replace(/\s/g,'');

					var sup_type = response.sup_type;					
										
					if(sup_state_name !='tamilnadu'){
						if(sup_state_name == ''){
							$("#txt_cgst").val(2.5);
							$("#txt_sgst").val(2.5);
							
							$("#txt_igst").val(0);
						}
						else{
							$("#txt_igst").val(5);
							
							$("#txt_cgst").val(0);
							$("#txt_sgst").val(0);
						}
					}else{
						$("#txt_cgst").val(2.5);
						$("#txt_sgst").val(2.5);
							
						$("#txt_igst").val(0);
					}

					if(sup_type == 1){
						$("#txt_cgst").val(0);
						$("#txt_sgst").val(0);							
						$("#txt_igst").val(0);
					}
					
				}			
				unloading();
			}
		});   	
	}
	auto_complete_intialize(1);
}


function get_inv_no_by_cmpny_id(cmpny_id){
	var financial_id =  $('#financial_id').val();
	if(cmpny_id !=''){					
		$.ajax({		
			url        : 'ajax_actions.php',
			method     : 'POST',
			dataType   : 'json',
			data       : 'action=get_inv_no_by_company_id&company_id='+cmpny_id+'&financial_id='+financial_id,
			beforeSend: function() { loading(); },
			success: function(response){
				$("#txt_inv_no").val(response.rslt);
				$("#sku_last_id").html("Last Sku: "+response.pdt_sku);

				unloading();
			}		
		});
	}
}

function auto_complete_intialize(rw_cnt){		
	$('.autocmplte_sareedesc').autocomplete({ 
		width:400,
		lookup: function (query, done) {		
			$.ajax({
				url  : "autocomplete_actions.php?pageaction=autocomplete_purchase_entry&srch_type=saree_desc&supplier="+$("#sel_supplier").val()+"&srch_keyword="+query,
				dataType: "json",						
				success: function (data)
				{					
				 done(data);				 
				}				
			});			
		},
		onSelect:function(dt){
			var current_element = $(this).prop("id");
			rw_cnt = current_element.replace("txt_pdt_sku_","");                         
			//bind_product_details(rw_cnt, dt.value, dt.data);
		}
	});
}

</script>



</body>
</html>
	  