<?php require_once('header.php'); 
$uuid=$fn->gen_uuid();

if(isset($_POST['form1'])) {
    
    
    
    $memeber_id="VENUS-FTL-10000";
    $statement = $pdo->prepare("SHOW TABLE STATUS WHERE `Name` = 'tbl_ftl_details_new'");
    $statement->execute();
    $memeber_id_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
    foreach ($memeber_id_result as $result) {
        $memeber_id=$memeber_id+$result['Auto_increment'];
    }
    
    
    
    
    

	$statement = $pdo->prepare("INSERT INTO `tbl_ftl_details_new`(`ftl_number`, `ftl_date`, `transport_name`, `driver_mobile_no`, `vechicle_no`, 
	`vechicle_type`, `mode_of_dispatch`, `pick_up_form`, `delivery_location`, `delivery_date`, 
	`total_fright`, `loading_unloading`, `detention`, `advance`, `balance`, 
	`delivery_commitment_date`, `deliverd_date`, `pod_received_date`, `vehicle_owner_name`, `driver_name`, 
	`contact_no`, `alter_no`, `vender_advance_slip_no`, `vender_advance_date`, `consignee_id`,
	`docket_no`, `cus_fright_charge`, `cus_vendor_cost`, `cus_loading_unloading`, `cus_other_charges`, 
	`cus_pl`, `bank_name`, `account_no`, `ifsc_code`, `prepared_by`,
	`marketing_executive`, `approved_by`, `status`,`loading_chrg_init`,`loading_chrg_final`) VALUES (?,?,?,?,?,
	?,?,?,?,?,
	?,?,?,?,?,
	?,?,?,?,?,
	?,?,?,?,?,
	?,?,?,?,?,
	?,?,?,?,?,
	?,?,?,?,?)");
	$result = $statement->execute(array($memeber_id,$_POST['ftl_date'],$_POST['transport_name'],$_POST['driver_mobile_no'],$_POST['vechicle_no'],
	$_POST['vechicle_type'],$_POST['mode_of_dispatch'],$_POST['pick_up_form'],$_POST['delivery_location'],$_POST['delivery_date'],
	$_POST['total_fright'],$_POST['loading_unloading'],$_POST['detention'],$_POST['advance'],$_POST['balance'],
	$_POST['delivery_commitment_date'],$_POST['deliverd_date'],$_POST['pod_received_date'],$_POST['vehicle_owner_name'],$_POST['driver_name'],
	$_POST['contact_no'],$_POST['alter_no'],$_POST['vender_advance_slip_no'],$_POST['vender_advance_date'],$_POST['consignee_id'],
	$_POST['docket_no'],$_POST['cus_fright_charge'],$_POST['cus_vendor_cost'],$_POST['cus_loading_unloading'],$_POST['cus_other_charges'],
	$_POST['cus_pl'],$_POST['bank_name'],$_POST['account_no'],$_POST['ifsc_code'],$_POST['prepared_by'],
	$_POST['marketing_executive'],$_POST['approved_by'],'1',$_POST['loading_chrg_init'],$_POST['loading_chrg_final']));
	
	$ftl_id = $pdo->lastInsertId();
	if($result){
	    $success_message = 'New Courier Company is added successfully!';
	    
	    
        unset($_POST['ftl_number']);unset($_POST['ftl_date']);unset($_POST['transport_name']);unset($_POST['driver_mobile_no']);unset($_POST['vechicle_no']);
    	unset($_POST['vechicle_type']);unset($_POST['mode_of_dispatch']);unset($_POST['pick_up_form']);unset($_POST['delivery_location']);unset($_POST['delivery_date']);
    	unset($_POST['delivery_commitment_date']);unset($_POST['deliverd_date']);unset($_POST['pod_received_date']);unset($_POST['vehicle_owner_name']);unset($_POST['driver_name']);unset($_POST['ftl_date']);
    	unset($_POST['total_fright']);unset($_POST['loading_unloading']);unset($_POST['detention']);unset($_POST['advance']);unset($_POST['balance']);
    	unset($_POST['contact_no']);unset($_POST['alter_no']);unset($_POST['vender_advance_slip_no']);unset($_POST['vender_advance_date']);unset($_POST['consignee_id']);
    	unset($_POST['docket_no']);unset($_POST['cus_fright_charge']);unset($_POST['cus_vendor_cost']);unset($_POST['cus_loading_unloading']);unset($_POST['cus_other_charges']);
    	unset($_POST['cus_pl']);unset($_POST['bank_name']);unset($_POST['account_no']);unset($_POST['ifsc_code']);unset($_POST['prepared_by']);
    	unset($_POST['marketing_executive']);unset($_POST['approved_by']);unset($_POST['loading_chrg_final']);unset($_POST['loading_chrg_init']);

	}else{
	    
	    $error_message = 'Something Went Wrong Pls Check The FTL Booking Add Details';
	    
	}
	
}	
			
	
?>

<section class="content-header" style="background-color: white;">
	    
	<div class="row">
	    <div class="col-md-12">
			<?php if($error_message): ?>
			<div class="callout callout-danger">
				<p>
					<?php echo $error_message; ?>
				</p>
			</div>
			<?php endif; ?>

			<?php if($success_message): ?>
			<div class="callout callout-success">
				<p><?php echo $success_message; ?></p>
				<p>Print The FTL DETAILS</p><a href="ftl-booking-print.php?id=<?=$ftl_id?>" target="_blank" class="btn btn-warning" style="text-decoration: none;">Print FTL</a>
			</div>
			<?php endif; ?>
		</div>
		<div class="col-md-4" style="text-align: left;">
			<h4><i class="fa fa-map-marker"></i> Add FTL Bookings Details</h4>
		</div>
		<div class="col-md-8" style="text-align: right;"> 
	         <a href="ftl-booking-list.php" class="btn btn-warning btn-sm"><i class="fa fa-arrow-left"></i> Back to FTL List</a>
		</div>
	</div> 
	<div class="row">
		<div class="col-md-12">
		    <form class="form-horizontal" action="" method="post" id="add_form" enctype="multipart/form-data">
		        <div class="box box-warning" style="box-shadow: rgba(0, 0, 0, 0.19) 0px 10px 20px, rgba(0, 0, 0, 0.23) 0px 6px 6px;">
					<div class="box-body">
					    <div class="row" style="margin: 20px 0px;">
						        <div class="col-md-3" style="margin-top: 10px;" hidden>
						            <label>FTL ORDER/ADVANCE SLIP NO<span style='color:red;'>*</span></label>
						            <input type="text" name="ftl_number" id="ftl_number" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>DATE<span style='color:red;'>*</span></label>
						            <input type="date" name="ftl_date" id="ftl_date" value="<?=date('Y-m-d')?>" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Transporter name<span style='color:red;'>*</span></label>
						            <select name="transport_id" id="transport_id" class="form-control select2">
						                 <option value="">Select Transporter name</option>
        							       <?php
    							        	$statement = $pdo->prepare("SELECT * FROM tbl_transporter where status=1");
                							$statement->execute();
                							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                    						 foreach($result as $rows)
                    						 {?>
                    						 <option value="<?=$rows['id']?>"><?=$rows['transport_name']?></option>
                    						 <?php } ?>
						            </select>
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Transporter Mobile No<span style='color:red;'>*</span></label>
						            <input type="text" name="driver_mobile_no" id="driver_mobile_no" class="form-control">
						            <input type="hidden" name="transport_name" id="transport_name" class="form-control">
						        </div>
						        
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Vehicle No<span style='color:red;'>*</span></label>
						            <input type="text" name="vechicle_no" id="vechicle_no" class="form-control">
						        </div>
						        
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Vehicle Type<span style='color:red;'>*</span></label>
						            <input type="text" name="vechicle_type" id="vechicle_type" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Mode Of Dispatch<span style='color:red;'>*</span></label>
						            <select name="mode_of_dispatch" id="mode_of_dispatch" class="form-control select2">
        							     <option value="FTL">FTL</option>
        							     <option value="PTL">PTL</option>
						            </select>
						         
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>PICK UP FROM<span style='color:red;'>*</span></label>
						            <input type="text" name="pick_up_form" id="pick_up_from" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Delivery Location<span style='color:red;'>*</span></label>
						            <input type="text" name="delivery_location" id="delivery_location" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Delivery State<span style='color:red;'>*</span></label>
						            <input type="text" name="delivery_date" id="delivery_date" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Total Frieght<span style='color:red;'>*</span></label>
						            <input type="text" name="total_fright" onInput=cal_frieght() id="total_fright" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Loading<span style='color:red;'>*</span></label>
						            <select name="loading_unloading" id="loading_unloading" class="form-control select2" required>
									   <option value="LOADING">LOADING</option>
									 
								</select>
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Loading Charges - Initial<span style='color:red;'>*</span></label>
						            <input type="number" name="loading_chrg_init"  onInput=totalamnt() id="loading_chrg_init" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Detention<span style='color:red;'>*</span></label>
						            <input type="number" name="detention" id="detention" onInput=totalamnt() class="form-control">
						        </div>
						        
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Advance<span style='color:red;'>*</span></label>
						            <input type="text" name="advance" id="advance" onInput=totalamnt()  class="form-control">
						        </div>
						        
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Balance<span style='color:red;'>*</span></label>
						            <input type="text" name="balance" id="balance" class="form-control">
						        </div>
						        
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Delivery Commitment date<span style='color:red;'>*</span></label>
						            <input type="date" name="delivery_commitment_date" id="delivery_commitment_date" value="<?=date('Y-m-d')?>" class="form-control">
						        </div>
						        
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Delivered date<span style='color:red;'>*</span></label>
						            <input type="date" name="deliverd_date" id="deliverd_date" value="<?=date('Y-m-d')?>" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>POD Received date<span style='color:red;'>*</span></label>
						            <input type="date" name="pod_received_date" id="pod_received_date" value="<?=date('Y-m-d')?>" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Vehicle Owner name<span style='color:red;'>*</span></label>
						            <input type="text" name="vehicle_owner_name" id="vehicle_owner_name" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Driver's Name<span style='color:red;'>*</span></label>
						            <input type="text" name="driver_name" id="driver_name" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Contact No<span style='color:red;'>*</span></label>
						            <input type="number" name="contact_no" id="contact_no" class="form-control">
						        </div>
						        
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Alter No<span style='color:red;'>*</span></label>
						            <input type="number" name="alter_no" id="alter_no" class="form-control">
						        </div>
						        
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Vender Advance slip no<span style='color:red;'>*</span></label>
						            <input type="number" name="vender_advance_slip_no" id="vender_advance_slip_no" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Vender Advance Date<span style='color:red;'>*</span></label>
						            <input type="date" name="vender_advance_date" id="vender_advance_date" value="<?=date('Y-m-d')?>" class="form-control">
						        </div>
						        
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Consignee<span style='color:red;'>*</span></label>
						            
						            <select name="consignee_id" id="consignee_id" class="form-control select2">
						                 <option value="">Select Consignee name</option>
        							       <?php
    							        	$statement = $pdo->prepare("SELECT * FROM tbl_client_new where status=1");
                							$statement->execute();
                							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                    						 foreach($result as $rows)
                    						 {?>
                    						 <option value="<?=$rows['id']?>"><?=$rows['name']?></option>
                    						 <?php } ?>
						            </select>
						            
						        </div>
								<div class="col-md-3" style="margin-top: 10px;">
						            <label>Venus Docket No<span style='color:red;'>*</span></label>
						            <input type="text" name="docket_no" id="docket_no" onInput=totalamnt() class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Our Frieght Charges<span style='color:red;'>*</span></label>
						            <input type="text" name="cus_fright_charge" onInput=totalamnt() id="cus_fright_charge" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Vender cost<span style='color:red;'>*</span></label>
						            <input type="text" name="cus_vendor_cost" onInput=totalamnt() id="cus_vendor_cost" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Unloading<span style='color:red;'>*</span></label>
						            <select name="cus_loading_unloading" id="cus_loading_unloading" class="form-control select2" required>
        								   <option value="UNLOADING">UNLOADING</option>
        							</select>
						        </div>
                                <div class="col-md-3" style="margin-top: 10px;">
						            <label>Unloading Charges - Final<span style='color:red;'>*</span></label>
						            <input type="number" name="loading_chrg_final" onInput=totalamnt() id="loading_chrg_final" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Other Charges<span style='color:red;'>*</span></label>
						            <input type="text" name="cus_other_charges" onInput=totalamnt() id="cus_other_charges" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>OUR P/L<span style='color:red;'>*</span></label>
						            <input type="text" name="cus_pl" id="cus_pl" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Bank Name<span style='color:red;'>*</span></label>
						            <input type="text" name="bank_name" id="bank_name" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Account no<span style='color:red;'>*</span></label>
						            <input type="text" name="account_no" id="account_no" class="form-control">
						        </div>
						        
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>IFSC<span style='color:red;'>*</span></label>
						            <input type="text" name="ifsc_code" id="ifsc_code" class="form-control">
						        </div>
						        
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Prepared by<span style='color:red;'>*</span></label>
						            <input type="text" name="prepared_by" id="prepared_by" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Marketing Executive<span style='color:red;'>*</span></label>
						            <input type="text" name="marketing_executive" id="marketing_executive" class="form-control">
						        </div>
						        <div class="col-md-3" style="margin-top: 10px;">
						            <label>Approved by<span style='color:red;'>*</span></label>
						            <input type="text" name="approved_by" id="approved_by" class="form-control">
						        </div>
						        <div class="form-group">
        							<div class="col-sm-12">
        								<center><input type="submit" id="submit" class="btn btn-success btn-sm" name="form1" value="Save FTL Details"></center>
        							</div>
        						</div>
						            <input   type="hidden" name="advanceper" id="advanceper"   class="form-control">
						        			        
						 </div>
					</div>
				</div>
		    </form>
		</div>
	</div>
</section>

<script>
$(document).ready(function() {
    $("#transport_id").change(function() {
	    get_transporter_details();
	    
    });

    function get_transporter_details(){
      
    var transport_id=$('#transport_id').val();
    
    $.ajax({
        url: 'apis/get-transport-details.php',
        type: 'post',
        dataType: 'json',
        data: {type:'get_transport',transport_id:transport_id},
         success:function(response){
             console.log(response);
             if(response['error']==false){
                 
                 $('#transport_name').val(response['data'][0]['transport_name']);
                 $('#driver_mobile_no').val(response['data'][0]['driver_mobile_no']);
                 $('#vechicle_no').val(response['data'][0]['vechicle_no']);
                 $('#vechicle_type').val(response['data'][0]['vechicle_type']);
                 $('#advanceper').val(response['data'][0]['advance_percentage']);
                 $('#bank_name').val(response['data'][0]['bank']);
                 $('#ifsc_code').val(response['data'][0]['ifsc']);
                 $('#account_no').val(response['data'][0]['ac_no']);
                /* $('#advanceper').val(response['data'][0]['pan_no']);*/
                 /*calculation part*/
                //  var percent =  response['data'][0]['advance_percentage'];
                //  var frieght = $('#total_fright').val();
                //  var advamount =  percent*frieght/100;
             }else{
                 
                 $('#transport_name').val();
                 $('#driver_mobile_no').val();
                 $('#vechicle_no').val();
                 $('#vechicle_type').val();
                 $('#advance').val();
                 $('#bank_name').val();
                 $('#ifsc_code').val();
                 $('#account_no').val();
             }
        }
    });
}
});
          function cal_frieght(){
                 var percent =  $('#advanceper').val();
                 var frieght = $('#total_fright').val();
                 var advamount =  percent*frieght/100;
                 $('#advance').val(advamount);
                 
                 
                  totalamnt();
             }
             
        function totalamnt(){
          console.log('Function starts..');

            var addAmnt = parseFloat($('#advance').val()) || 0;
            var detention = parseFloat($('#detention').val()) || 0;
            var total_fright = parseFloat($('#total_fright').val()) || 0;
            var loadingChrgInit = parseFloat($('#loading_chrg_init').val()) || 0;
            var loadingChrgFinal = parseFloat($('#loading_chrg_final').val()) || 0;
            var cusOtherCharges = parseFloat($('#cus_other_charges').val()) || 0;
            var cusVendorCost = parseFloat($('#cus_vendor_cost').val()) || 0;
            var cusFreightCharge = parseFloat($('#cus_fright_charge').val()) || 0;
            
            var ftl_balance_amt = (total_fright + detention)-(addAmnt);
            
            console.log(ftl_balance_amt);
            var final = (loadingChrgInit + detention + detention) - (cusOtherCharges + loadingChrgFinal + cusVendorCost + cusFreightCharge);
            $('#cus_pl').val(final);
            $('#balance').val(ftl_balance_amt);
            $('#cus_fright_charge').val(total_fright);
            
        }
</script>

<?php require_once('footer.php'); ?>