
<?php $__env->startSection('title'); ?> Payments <?php $__env->stopSection(); ?>
<?php $__env->startSection('body'); ?>

<div class="row">
	<div class="form-group col-md-3">
		<label class="control-label">From</label>
		<input type="text" class="form-control fromDate" name="fromDate">
	</div>
	<div class="form-group col-md-3">
		<label class="control-label">To</label>
		<input type="text" class="form-control toDate" name="toDate">
	</div>
</div>
<div class="row">
	<div class="form-group col-md-3">
		<label>Transporter</label>
		<select class="form-control transporter chosen-select" name="transporter"></select>
	</div>
	<div class="form-group col-md-3">
		<label>Vehicle</label>
		<select class="form-control vehicle chosen-select" name="vehicle"></select>
	</div>
	<div class="form-group col-md-3">
		<button class="btn btn-primary generate" style="margin-top: 25px;">Generate</button>
		<button class="btn btn-primary export pull-right" style="margin-top: 25px;">Export</button>
	</div>
</div>

<div class="row">
	<div class="col-md-12">
		<h4 class="report_content title" style="margin: 0px 0px 25px 0px;"></h4>
	</div>
</div>
<div class="row normal_deliveries report_content"></div>
<div class="row up_country_deliveries report_content"></div>
<div class="row return_collections report_content"></div>
<div class="row direct_deliveris report_content"></div>
<div class="row fixed_payments report_content"></div>


<div class="row ap_direct_deliveries report_content"></div>
<div class="row ap_offloading_charges report_content"></div>
<div class="row ap_demurrages report_content"></div>
<div class="row ap_other report_content"></div>

<div class="row d_document_errors report_content"></div>
<div class="row d_other report_content"></div>

<div class="row totals report_content"></div>

<?php $__env->stopSection(); ?>
<?php $__env->startSection('script'); ?>
<script type="text/javascript">

$(function(){
	var _token = $('meta[name="_token"]').attr('content');
	var errors = JSON.parse('<?php echo json_encode($errors->toArray()) ?>');
	$('body').highlight({errors: errors.errors});

	$('.chosen-select').chosen({search_contains:true});
	
	$(document).ajaxStart(function () {
        $('button').data('loading-text', '...');
        $('button').button('loading');
    }).ajaxStop(function () {
        $('button').button('reset');
    });

    $(".export").click(function(){
		exportReport();
	});
	
	$('.fromDate').datepicker({
		dateFormat: "yy-mm-dd",
		onSelect: function(){
			$(".toDate").datepicker( "option", "minDate", new Date($('.fromDate').val()) );
			transporters(function(){
				vehicles();
			});
		}
	});
	
	$(".toDate").datepicker({
		dateFormat: "yy-mm-dd",
		onSelect: function(){
			$(".fromDate").datepicker( "option", "maxDate", new Date($('.toDate').val()) );
			transporters(function(){
				vehicles();
			});
		}
	});

	$(".transporter").change(function(){
		vehicles();
	});

	transporters(function(){
		vehicles();
	});

	function transporters(callback){
		var $select = $(".transporter");

		var data = {
            fromDate : $(".fromDate").val(),
            toDate: $(".toDate").val()
        };
		$.post("<?php echo e(url('/report7/transporters')); ?>", {_token: _token, data: data}, function(e){
			var dom = '';
			if(typeof(e) == 'object' && Object.keys(e).length > 0){
				$.each(e, function(i, k){
					dom += '<option value="'+ k.id +'">'+ k.name +'</option>';
				});
			}else{
				dom = '<option value="">No transporters found.</option>';
			}
			$select.html(dom);
			$('.chosen-select').trigger('chosen:updated');

			if(typeof(callback) == "function"){
				callback();
			}
		}, 'json');
	}

	function vehicles(){
		var $select = $(".vehicle");

		var data = {
            fromDate : $(".fromDate").val(),
            toDate: $(".toDate").val(),
            transporter_id: $(".transporter").val()
        };

		$.post("<?php echo e(url('/report7/vehicles')); ?>", {_token: _token, data: data}, function(e){
			var dom = '';
			if(typeof(e) == 'object' && Object.keys(e).length > 0){
				$.each(e, function(i, k){
					dom += '<option value="'+ k.id +'">'+ k.number +'</option>';
				});
			}else{
				dom = '<option value="">No vehicles found.</option>';
			}
			$select.html(dom);
			$('.chosen-select').trigger('chosen:updated');
		}, 'json');
	}

	$(".generate").click(function(){
		generate();
	});

	function getTitle(){
		var transporter = $(".transporter option:selected").text();

		var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', '-'];

		var fd = $(".fromDate").datepicker("getDate");
		var td = $(".toDate").datepicker("getDate");
		
		var y1 = fd.getFullYear();
		var y2 = td.getFullYear();

		var m1 =  fd.getMonth();
		var m2 =  td.getMonth();

		var title = "<b>"+ transporter +" </b> Payment details for ";
		if(y1 != y2){
			var month1 = (m1 < 12 && m1 >= 0) ? m1 : 12;
			var month2 = (m2 < 12 && m2 >= 0) ? m2 : 12;
			var year1 = y1 > 0 ? y1 : "-";
			var year2 = y2 > 0 ? y2 : "-";
			
			title += "<b>" + months[month1] + " " + year1 + " - " + months[month2] + " " + year2;
		}else{
			if(m1 != m2){
				//different month// same year
				var month1 = (m1 < 12 && m1 >= 0) ? m1 : 12;
				var month2 = (m2 < 12 && m2 >= 0) ? m2 : 12;
				var year = (y1 > 0 && y2 > 0) ? y1 : "-";
				title += "<b>" + months[month1] + " - " + months[month2] + ", " + year + "</b>";
			}else{
				//same month //same year
				var month = (m1 < 12 && m1 >= 0) && (m2 < 12 && m2 >= 0) ? m1 : 12;
				var year = (y1 > 0 && y2 > 0) ? y1 : "-";
				title += "the month of <b>" + months[month] + " " + year + "</b>";
			}
		}

		return title;
	}

	function highlightOrThrow(e, callback){
		$.clearHighlights();
		if(e != undefined && typeof(e) == 'object'){
			if(e.hasOwnProperty('errors')){
				$.each(e.errors, function(i, k){
					$('.'+i).highlightElement({msg: k});
				});
			}else if(e.hasOwnProperty('ok') && e.hasOwnProperty('msg') && e.ok == 0){
				$.notify.error({msg: e.msg});
			}else if(e.hasOwnProperty('ok') && e.hasOwnProperty('msg')  && e.ok == 1){
				$.notify.success({msg: e.msg});
				if(callback != undefinded && typeof(callback) == 'function'){
					callback();
				}
			}else{
				callback();
			}
		}else{
			console.log('object expected.');
		}
	}

	function generate(){

		var data = {
			vehicle_id: $(".vehicle").val(),
			fromDate : $(".fromDate").val(),
            toDate: $(".toDate").val(),
            transporter_id: $(".transporter").val(),
		};
		$(".report_content").html("");
		$(".title").html(getTitle());
		$.post("<?php echo e(url('/report7/generate')); ?>", {_token: _token, data: data}, function(e){
			highlightOrThrow(e, function(){
				report(e);
			});
		}, 'json');
	}

	function exportReport(){
		var data = {
			vehicle_id: $(".vehicle").val(),
			fromDate : $(".fromDate").val(),
            toDate: $(".toDate").val(),
            transporter_id: $(".transporter").val(),
            title: getTitle()
		};

		var dom = '<input type="hidden" name="_token" value="'+ _token +'"/>';
		$.each(data, function(k, v){
			if(v == null){
				v = '';
			}
			if(typeof(v) == "object"){
				$.each(v, function(i, j){
					dom += '<input type="hidden" name="'+ k +'['+ i +']" value="'+ j +'"/>';
				});
			}else{
				dom += '<input type="hidden" name="'+ k +'" value="'+ v +'"/>';
			}
		});
		$('<form action="<?php echo e(url("/report7/export")); ?>" method="post">'+ dom +'</form>').appendTo('body').submit();

		$.clearHighlights();
	}

	function report(e){
		if(Object.keys(e.normal_deliveries.data).length > 0){
			var tbody = '';
			$.each(e.normal_deliveries.data, function(i, k){
				tbody += '<tr>';
				tbody += '	<td>'+ k.date +'</td>';
				tbody += '	<td>'+ k.vehicle +'</td>';
				tbody += '	<td>'+ k.locations +'</td>';
				tbody += '	<td>'+ k.distance +' km</td>';
				tbody += '	<td>'+ k.distributors +'</td>';
				tbody += '</tr>';
			});
			var tfoot = '';
			tfoot += '<tr>';
			tfoot += '	<th colspan="3" class="text-right">Total distance</th>';
			tfoot += '	<th>'+ e.normal_deliveries.distance +' km</th>';
			tfoot += '	<th>Rate : '+ e.normal_deliveries.rate +'</th>';
			tfoot += '</tr>';
			var table = create_table({
				title: "NORMAL DELIVERIES",
				tbody: tbody,
				tfoot: tfoot
			});
			$(".normal_deliveries").html(table);
		}else{
			$(".normal_deliveries").html('');
		}


		if(Object.keys(e.up_country_deliveries.data).length > 0){
			var tbody = '';
			$.each(e.up_country_deliveries.data, function(i, k){
				tbody += '<tr>';
				tbody += '	<td>'+ k.date +'</td>';
				tbody += '	<td>'+ k.vehicle +'</td>';
				tbody += '	<td>'+ k.locations +'</td>';
				tbody += '	<td>'+ k.distance +' km</td>';
				tbody += '	<td>'+ k.distributors +'</td>';
				tbody += '</tr>';
			});
			var tfoot = '';
			tfoot += '<tr>';
			tfoot += '	<th colspan="3" class="text-right">Total distance</th>';
			tfoot += '	<th>'+ e.up_country_deliveries.distance +' km</th>';
			tfoot += '	<th>Rate : '+ e.up_country_deliveries.rate +'</th>';
			tfoot += '</tr>';
			var table = create_table({
				title: "UP COUNTRY DELIVERIES",
				tbody: tbody,
				tfoot: tfoot
			});
			$(".up_country_deliveries").html(table);
		}else{
			$(".up_country_deliveries").html('');
		}


		if(Object.keys(e.return_collections.data).length > 0){
			var tbody = '';
			$.each(e.return_collections.data, function(i, k){
				tbody += '<tr>';
				tbody += '	<td>'+ k.date +'</td>';
				tbody += '	<td>'+ k.vehicle +'</td>';
				tbody += '	<td>'+ k.locations +'</td>';
				tbody += '	<td>'+ k.distance +' km</td>';
				tbody += '	<td>'+ k.distributors +'</td>';
				tbody += '</tr>';
			});
			var tfoot = '';
			tfoot += '<tr>';
			tfoot += '	<th colspan="3" class="text-right">Total distance</th>';
			tfoot += '	<th>'+ e.return_collections.distance +' km</th>';
			tfoot += '	<th>Rate : '+ e.return_collections.rate +'</th>';
			tfoot += '</tr>';
			var table = create_table({
				title: "RETURN COLLECTIONs",
				tbody: tbody,
				tfoot: tfoot
			});
			$(".return_collections").html(table);
		}else{
			$(".return_collections").html('');
		}


		if(Object.keys(e.direct_deliveris.data).length > 0){
			var tbody = '';
			$.each(e.direct_deliveris.data, function(i, k){
				tbody += '<tr>';
				tbody += '	<td>'+ k.date +'</td>';
				tbody += '	<td>'+ k.vehicle +'</td>';
				tbody += '	<td>'+ k.locations +'</td>';
				tbody += '	<td>'+ k.distance +' km</td>';
				tbody += '	<td>'+ k.distributors +'</td>';
				tbody += '</tr>';
			});
			var tfoot = '';
			tfoot += '<tr>';
			tfoot += '	<th colspan="3" class="text-right">Total distance</th>';
			tfoot += '	<th>'+ e.direct_deliveris.distance +' km</th>';
			tfoot += '	<th>Rate : '+ e.direct_deliveris.rate +'</th>';
			tfoot += '</tr>';
			var table = create_table({
				title: "DIRECT DELIVERIES",
				tbody: tbody,
				tfoot: tfoot
			});
			$(".direct_deliveris").html(table);
		}else{
			$(".direct_deliveris").html('');
		}

		if(Object.keys(e.fixed_payments.data).length > 0){
			var tbody = '';
			$.each(e.fixed_payments.data, function(i, k){
				tbody += '<tr>';
				tbody += '	<td>'+ k.date +'</td>';
				tbody += '	<td>'+ k.vehicle +'</td>';
				tbody += '	<td>'+ k.locations +'</td>';
				tbody += '	<td>Rs '+ k.cost +'</td>';
				tbody += '	<td>'+ k.distributors +'</td>';
				tbody += '</tr>';
			});
			var tfoot = '';
			tfoot += '<tr>';
			tfoot += '	<th colspan="3" class="text-right">Total payments</th>';
			tfoot += '	<th colspan="2" class="text-left">Rs '+ e.fixed_payments.cost +'</th>';
			tfoot += '</tr>';
			var table = create_table({
				title: "FIXED PAYMENTS",
				tbody: tbody,
				tfoot: tfoot
			});
			$(".fixed_payments").html(table);
		}else{
			$(".fixed_payments").html('');
		}

		if(Object.keys(e.ap_direct_deliveries.data).length > 0){
			var tbody = '';
			$.each(e.ap_direct_deliveries.data, function(i, k){
				tbody += '<tr>';
				tbody += '	<td>'+ k.date +'</td>';
				tbody += '	<td>'+ k.vehicle +'</td>';
				tbody += '	<td>'+ k.locations +'</td>';
				tbody += '	<td>Rs '+ k.cost +'</td>';
				tbody += '	<td>'+ k.distributors +'</td>';
				tbody += '</tr>';
			});
			var tfoot = '';
			tfoot += '<tr>';
			tfoot += '	<th colspan="3" class="text-right">Total payments</th>';
			tfoot += '	<th colspan="2" class="text-left">Rs '+ e.ap_direct_deliveries.cost +'</th>';
			tfoot += '</tr>';
			var table = create_table({
				heading: "ADDITIONAL PAYMENTS",
				title: "DIRECT DELIVERIES",
				tbody: tbody,
				tfoot: tfoot
			});
			$(".ap_direct_deliveries").html(table);
		}else{
			$(".ap_direct_deliveries").html('');
		}

		if(Object.keys(e.ap_offloading_charges.data).length > 0){
			var tbody = '';
			$.each(e.ap_offloading_charges.data, function(i, k){
				tbody += '<tr>';
				tbody += '	<td>'+ k.date +'</td>';
				tbody += '	<td>'+ k.vehicle +'</td>';
				tbody += '	<td>'+ k.locations +'</td>';
				tbody += '	<td>Rs '+ k.cost +'</td>';
				tbody += '	<td>'+ k.distributors +'</td>';
				tbody += '</tr>';
			});
			var tfoot = '';
			tfoot += '<tr>';
			tfoot += '	<th colspan="3" class="text-right">Total payments</th>';
			tfoot += '	<th colspan="2" class="text-left">Rs '+ e.ap_offloading_charges.cost +'</th>';
			tfoot += '</tr>';
			var table = create_table({
				title: "OFFLOADING CHARGES",
				tbody: tbody,
				tfoot: tfoot
			});
			$(".ap_offloading_charges").html(table);
		}else{
			$(".ap_offloading_charges").html('');	
		}

		if(Object.keys(e.ap_demurrages.data).length > 0){
			var tbody = '';
			$.each(e.ap_demurrages.data, function(i, k){
				tbody += '<tr>';
				tbody += '	<td>'+ k.date +'</td>';
				tbody += '	<td>'+ k.vehicle +'</td>';
				tbody += '	<td>'+ k.locations +'</td>';
				tbody += '	<td>Rs '+ k.cost +'</td>';
				tbody += '	<td>'+ k.distributors +'</td>';
				tbody += '</tr>';
			});
			var tfoot = '';
			tfoot += '<tr>';
			tfoot += '	<th colspan="3" class="text-right">Total payments</th>';
			tfoot += '	<th colspan="2" class="text-left">Rs '+ e.ap_demurrages.cost +'</th>';
			tfoot += '</tr>';
			var table = create_table({
				title: "DEMURRAGE",
				tbody: tbody,
				tfoot: tfoot
			});
			$(".ap_demurrages").html(table);
		}else{
			$(".ap_demurrages").html('');
		}

		if(Object.keys(e.ap_other.data).length > 0){
			var tbody = '';
			$.each(e.ap_other.data, function(i, k){
				tbody += '<tr>';
				tbody += '	<td>'+ k.date +'</td>';
				tbody += '	<td>'+ k.vehicle +'</td>';
				tbody += '	<td>'+ k.locations +'</td>';
				tbody += '	<td>Rs '+ k.cost +'</td>';
				tbody += '	<td>'+ k.distributors +'</td>';
				tbody += '</tr>';
			});
			var tfoot = '';
			tfoot += '<tr>';
			tfoot += '	<th colspan="3" class="text-right">Total payments</th>';
			tfoot += '	<th colspan="2" class="text-left">Rs '+ e.ap_other.cost +'</th>';
			tfoot += '</tr>';
			var table = create_table({
				title: "OTHER",
				tbody: tbody,
				tfoot: tfoot
			});
			$(".ap_other").html(table);
		}else{
			$(".ap_other").html('');
		}

		if(Object.keys(e.d_document_errors.data).length > 0){
			var tbody = '';
			$.each(e.d_document_errors.data, function(i, k){
				tbody += '<tr>';
				tbody += '	<td>'+ k.date +'</td>';
				tbody += '	<td>'+ k.vehicle +'</td>';
				tbody += '	<td>'+ k.locations +'</td>';
				tbody += '	<td>Rs '+ k.cost +'</td>';
				tbody += '	<td>'+ k.distributors +'</td>';
				tbody += '</tr>';
			});
			var tfoot = '';
			tfoot += '<tr>';
			tfoot += '	<th colspan="3" class="text-right">Total deductions</th>';
			tfoot += '	<th colspan="2" class="text-left">Rs '+ e.d_document_errors.cost +'</th>';
			tfoot += '</tr>';
			var table = create_table({
				heading: "DEDUCTIONS",
				title: "DOCUMENT ERRORS",
				tbody: tbody,
				tfoot: tfoot
			});
			$(".d_document_errors").html(table);
		}else{
			$(".d_document_errors").html('');
		}

		if(Object.keys(e.d_other.data).length > 0){
			var tbody = '';
			$.each(e.d_other.data, function(i, k){
				tbody += '<tr>';
				tbody += '	<td>'+ k.date +'</td>';
				tbody += '	<td>'+ k.vehicle +'</td>';
				tbody += '	<td>'+ k.locations +'</td>';
				tbody += '	<td>Rs '+ k.cost +'</td>';
				tbody += '	<td>'+ k.distributors +'</td>';
				tbody += '</tr>';
			});
			var tfoot = '';
			tfoot += '<tr>';
			tfoot += '	<th colspan="3" class="text-right">Total deductions</th>';
			tfoot += '	<th colspan="2" class="text-left">Rs '+ e.d_other.cost +'</th>';
			tfoot += '</tr>';
			var table = create_table({
				title: "OTHER",
				tbody: tbody,
				tfoot: tfoot
			});
			$(".d_other").html(table);
		}else{
			$(".d_other").html('');
		}

		var totals = '';
		totals += '<div class="col-md-6">';
		totals += '	<div class="table-responsive">';
		totals += '		<table class="table table-bordered table-condensed">';
		totals += '			<tr>';
		totals += '				<th>Total distance</th><th>'+ e.total_distance +' km</th>';
		totals += '			</tr>';
		totals += '			<tr>';
		totals += '				<th>Total payment</th><th>Rs '+ e.total_cost +'</th>';
		totals += '			</tr>';
		totals += '			<tr>';
		totals += '				<th>Number of trips</th><th>'+ Object.keys(e.trips).length +'</th>';
		totals += '			</tr>';
		totals += '		</table>';
		totals += '	</div>';
		totals += '</div>';
		$(".totals").html(totals);
	}

	function create_table(options){
		
		var settings = $.extend({
			heading: "",
			title: "title",
			tbody: "",
			tfoot: ""
		}, options);

		var dom = '';

		dom += '<div class="col-md-12">';
		if(settings.heading.length > 0){
			dom += '	<h3>'+ settings.heading +'</h3>';
			dom += '</br>';
		}
		dom += '	<div class="table-responsive">';
		dom += '		<label>'+ settings.title +'</label>';
		dom += '		<table class="table table-bordered table-condensed">';
		dom += '			<thead>';
		dom += '				<tr>';
		dom += '					<th>Date</th>';
		dom += '					<th>Vehicle</th>';
		dom += '					<th>Locations</th>';
		dom += '					<th>Standard</th>';
		dom += '					<th>Distributors</th>';
		dom += '				</tr>';
		dom += '			</thead>';
		dom += '			<tbody>';
		dom += 					settings.tbody;
		dom += '			</tbody>';
		if(settings.tfoot.length > 0){
			dom += '		<tfoot>';
			dom += 				settings.tfoot;
			dom += '		</tfoot>';
		}
		dom += '		</table>';
		dom += '	</div>';
		dom += '</div>';

		return dom;
	}
});
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.master', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>