<?php $this->load->view("common/header"); ?>
<!-- end: HEAD -->
<link rel="stylesheet" href="<?php echo asset_url();?>css/buttons.dataTables.min.css" type="text/css" />
<!-- end: CSS REQUIRED FOR THIS PAGE ONLY -->
<!-- end: HEAD -->
<!-- start: BODY -->
<style type="text/css">
.dt-button-collection {
	font-size: 1.4em;
}
.dt-button-collection .active span:after {
	font-family: FontAwesome;
	content: " \f00c";
	text-align: right
}
a.dt-button, button.dt-button, div.dt-button, .dt-buttons {
	font-size: .83em;
	float: right!important;
	/*  background-color:#e9e9e9;
    background-image:linear-gradient(to bottom,#fff 0,#e9e9e9 100%);*/
		
	margin-left: .3em;
}
</style>
<body>

<!-- start: HEADER -->
<div class="navbar navbar-inverse navbar-fixed-top"> 
  <!-- start: TOP NAVIGATION CONTAINER -->
  <div class="container">
    <div class="navbar-header"> 
      <!-- start: RESPONSIVE MENU TOGGLER -->
      <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button"> <span class="clip-list-2"></span> </button>
      <!-- end: RESPONSIVE MENU TOGGLER --> 
      <!-- start: LOGO -->
      <?php $this->load->view("common/logo"); ?>
      <!-- end: LOGO --> 
    </div>
    <div class="navbar-tools"> 
      <!-- start: TOP NAVIGATION MENU -->
      <?php $this->load->view("common/notifications.php"); ?>
      <!-- end: TOP NAVIGATION MENU --> 
    </div>
  </div>
  <!-- end: TOP NAVIGATION CONTAINER --> 
</div>
<!-- end: HEADER --> 
<!-- start: MAIN CONTAINER -->
<div class="main-container">
<div class="navbar-content"> 
  <!-- start: SIDEBAR -->
  <?php $this->load->view("common/navigation"); ?>
  <!-- end: SIDEBAR --> 
</div>
<!-- start: PAGE -->
<div class="main-content">
<!-- end: SPANEL CONFIGURATION MODAL FORM -->
<div class="container">
<!-- start: PAGE HEADER -->
<div class="row">
  <div class="col-sm-12"> 
    <!-- start: PAGE TITLE & BREADCRUMB -->
    <ol class="breadcrumb">
      <li> <a href="#"> Dashboard </a> </li>
      <li> <a href="<?php echo base_url('sales2?s='.rand()); ?>"> Loading </a> </li>
      <li class="active"> Delivery Details </li>
      <li class="search-box">
        <form class="sidebar-search">
          <div class="form-group">
            <input type="text" placeholder="Start Searching...">
            <button class="submit"> <i class="clip-search-3"></i> </button>
          </div>
        </form>
      </li>
    </ol>
    <div class="page-header">
      <h4>ONGOING DELIVERY - <?php echo $delivary_details['loading_ref_no']?></h4>
    </div>
  </div>
</div>
<!-- end: PAGE HEADER --> 
<!-- start: PAGE CONTENT 
                    <!-- start grid -->

<div class="row">
  <div class="col-md-12"> 
    <!-- start: DYNAMIC TABLE PANEL -->
    <div class="panel panel-default">
      <div class="panel-heading"> <i class="fa fa-plus"></i>Date Time - <?php echo $delivary_details['loading_date']?> , Area - <?php echo $delivary_details['area_name']?> , Vehicle - <?php echo $delivary_details['vehicle_code']?> , Sales Rep - <?php echo $delivary_details['user_first_name']?> </div>
      <div class="panel-body">
        <div class="alert alert-danger" style="display:none;">
          <button class="close" data-dismiss="alert"> × </button>
          <i class="fa fa-times-circle"></i> <strong></strong> <span class="errortxt"></span> </div>
        <div class="alert alert-success" style="display:none;">
          <button class="close" data-dismiss="alert"> × </button>
          <i class="fa fa-check-circle"></i> <strong></strong><span class="succetxt"></span> </div>
        <form role="form" class="form-horizontal" id="create_sales_form" action="#" method="post">
          <div class="col-md-12"></div>
          <!--col-md-12-->
          <div class="col-md-12">
            <div class="col-md-12">
              <div class="panel panel-default">
                <div class="panel-heading" > Delivery Items</div>
                <div class="panel-body">
                  <input type="hidden" id="delivary_id" name="delivary_id" value="<?php echo $delivary_details['id']?>">
                  <input type="hidden" name="area_id" value="<?php echo $delivary_details['area_id']?>">
                  <div class="col-md-12">
                    <div class="form-group">
                      <table class="table items table-striped table-bordered table-condensed table-hover"  width="100%" style="text-align:left">
                        <thead>
                          <tr>
                            <th style="" class="col-md-1">Customer</th>
                            <th style="" class="col-md-1">Order NO</th>
                            <th style="" class="col-md-1">Order Date</th>
                            <th style="" class="col-md-1">Product / Qty</th>
                            <th style="" class="col-md-1">Bill Total</th>
                            <th style="" class="col-md-1">Route</th>
                            <th style="" class="col-md-1">Action</th>
                          </tr>
                        </thead>
                        <tbody>
                          <?php
$totqty=0;
 foreach($customer_list as $row2){ 	
	$products = $this->Customer_Order_Process_Model->get_cus_order_item_details($row2['cus_order_master_id']);
?>
                          <tr>
                            <td style=""><?php echo $row2['cus_name']."<br>".$row2['cus_address']?></td>
                            <td style=""><?php echo $row2['cus_order_ref_no']?></td>
                            <td style=""><?php echo $row2['customer_order_date']?></td>
                            <td style=""><table  class="table table-bordered" style="width: 100%">
                                <?php foreach($products as $pds) echo "<tr><td class=\"col-xs-10\" style=\"border-top: solid 1px;border: solid 1px;\">".$pds['product_name']."</td><td class=\"col-xs-2\" style=\"border-top: solid 1px;border: solid 1px; text-align:center \">".$pds['issue_qty']."</td></tr>" ?>
                              </table></td>
                           
                              <td style=""><?php echo $row2['total_amount']?></td>
                             
                               <td style=""><?php echo $row2['route_name']?></td>
                            <th style="" class="col-md-1">  
                                <?php if($row2['invoiced_status'] != 1){ ?>
                                <div class="btn-group text-left">
                                <button data-toggle="dropdown" class="btn btn-default btn-xs btn-primary dropdown-toggle" type="button"> Actions <span class="caret"></span></button>
                                <ul role="menu" class="dropdown-menu pull-right">
                                 	<li><a href="<?php echo base_url().'sales/order_invoice/'.$row2['cus_order_master_id'] ?>"><i class="fa fa-check"></i> Invoice Order</a></li>
                                	<li><a onClick="sales_rep_details_print_3(<?php echo $row2['cus_order_master_id'] ?>)"><i class="fa fa-print"></i> Print Order</a></li>
                                    <?php if($row2['invoice_type']==1001) {?>                                    
                                    <li><a onClick="turn_invoice_type_as_cash(<?php echo $row2['cus_order_master_id'] ?>)"><i class="fa fa-gears"></i> Turn as Cash Bill</a></li>
                                   <?php } ?>
                                   <?php if($this->session->userdata('ss_group_id')==100 || $this->session->userdata('ss_group_id')==100 ){?>
                                      <li><a onClick="roll_back_order(<?php echo $row2['cus_order_master_id'] ?>)"><i class="fa fa-backward"></i> Roll Back Ordrer</a></li>
                                    <?php }?>
                                </ul>
                              </div>
								<?php }else{ ?>								  
                                  Invoiced                                  
								  <?php } ?>                                
                            </th>
                          </tr>
                          <?php }?>
                        </tbody>
                        <tfoot>
                          <tr class="tfoot active" id="tfoot">
                            <th style="" class="col-md-1">Customer</th>
                              <th style="" class="col-md-1">Order NO</th>
                            <th style="" class="col-md-1">Order Date</th>
                            <th style="" class="col-md-1">Product / Qty</th>
                            <th style="" class="col-md-1">Bill Total</th>
                            <th style="" class="col-md-2">Route</th>
                            <th style="" class="col-md-1">Action</th>
                          </tr>
                        </tfoot>
                      </table>
                    </div>
                  </div>
                </div>
                <div class="modal-footer" style="margin-bottom:10px;">
                  <?php /*?> <input type="button" class="btn btn-primary" value="Print Pick-up Sheet" name="pp" id="pp" onClick="pickup_sheet_print(<?php echo $delivary_details['id'] ?> )"><?php */?>
                  <?php if($delivary_details['approval_status']==1){ ?>
                  <a style="display:none" class="btn btn-success pull-left"  href="<?php echo base_url('marketing_delivery_process/add_new_lot/'.$delivary_details['id']); ?>"> Add new lot (Products Items) </a>
                  <a onClick="sales_rep_details_print_1(<?php echo $delivary_details['id'] ?>)" class="btn btn-info pull-right"> Print Customer Delivery Sheet </a>
				  <?php } ?>
                </div>
              </div>
              <div class="panel panel-default">
                <div class="panel-heading" > Delivery Items</div>
                <div class="panel-body">
                  <input type="hidden" id="delivary_id" name="delivary_id" value="<?php echo $delivary_details['id']?>">
                  <input type="hidden" name="area_id" value="<?php echo $delivary_details['area_id']?>">
                  <div class="col-md-12">
                    <div class="form-group">
                      <table class="table items table-striped table-bordered table-condensed table-hover" id="sample_table">
                        <thead>
                          <tr>
                            <th class="col-md-1">Product Code</th>
                            <th class="col-md-4">Product</th>
                            <th class="col-md-1">Issue Qty</th>
                            <th class="col-md-1">Invoice Qty</th>
                            <th class="col-md-1">Return Qty</th>
                            <th class="col-md-1">On hand Qty</th>
                            <th class="col-md-1">Unload Qty</th>
                            <th class="col-md-1">Action</th>
                          </tr>
                        </thead>
                        <tfoot>
                        </tfoot>
                      </table>
                    </div>
                  </div>
                </div>
                <div class="modal-footer" style="margin-bottom:10px;">
                  <?php /*?> <input type="button" class="btn btn-primary" value="Print Pick-up Sheet" name="pp" id="pp" onClick="pickup_sheet_print(<?php echo $delivary_details['id'] ?> )"><?php */?>
                  <?php if($delivary_details['approval_status']==0){ ?>
                  <a class="btn btn-success pull-left"  href="<?php echo base_url('marketing_delivery_process/add_new_customer_order/'.$delivary_details['id']); ?>"> Add new Customer Orders </a>
                   <?php } ?>
                   <?php if($delivary_details['approval_status']==1){ ?>
                  <a onClick="sales_rep_details_print_2(<?php echo $delivary_details['id'] ?>)" class="btn btn-info pull-right"> Print Loading Sheet </a>
                  
				  <?php } ?>
                </div>
              </div>
            </div>
          </div>
          
          <!--col-md-8-->
          
          <div class="col-md-12"> </div>
        </form>
        
        <!-- end: DYNAMIC TABLE PANEL --> 
        
      </div>
    </div>
    <span style="font-family: 'Times New Roman'; font-size:14px; " > </span> 
    <!-- end grid --> 
    
  </div>
  <!-- end: PAGE --> 
</div>
<!-- end: MAIN CONTAINER --> 
<!-- start: FOOTER -->
<div class="footer clearfix">
  <div class="footer-inner"> 2014 &copy; clip-one by cliptheme. </div>
  <div class="footer-items"> <span class="go-top"><i class="clip-chevron-up"></i></span> </div>
</div>
<!-- end: FOOTER --> 
<!-- start: RIGHT SIDEBAR --> 
<!-- end: RIGHT SIDEBAR -->
<div id="event-management" class="modal fade" tabindex="-1" data-width="760" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times; </button>
        <h4 class="modal-title">Event Management</h4>
      </div>
      <div class="modal-body"></div>
      <div class="modal-footer">
        <button type="button" data-dismiss="modal" class="btn btn-light-grey"> Close </button>
        <button type="button" class="btn btn-danger remove-event no-display"> <i class='fa fa-trash-o'></i> Delete Event </button>
        <button type='submit' class='btn btn-success save-event'> <i class='fa fa-check'></i> Save </button>
      </div>
    </div>
  </div>
</div>
<input name="tmpVal" type="hidden" id="tmpVal" value="0" >
<input name="" id="type_sale" type="hidden" value="22">
<!-- start ajax model -->
<div id="ajax-modal" class="modal fade" data-backdrop="static" data-keyboard="false" tabindex="-1" style="display: none;"></div>
<!-- end ajax model --> 

<!-- start: MAIN JAVASCRIPTS -->
<?php $this->load->view("common/footer"); ?>
<!-- end: MAIN JAVASCRIPTS --> 
<script type="text/javascript" src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script> 
<script type="text/javascript" src="https://cdn.datatables.net/1.10.10/js/dataTables.bootstrap.min.js"></script> 
<script type="text/javascript" src="<?php echo asset_url(); ?>js/accounting.js"></script> 
<script type="text/javascript" src="<?php echo asset_url(); ?>js/accounting.min.js"></script> 
<script type="text/javascript" src="<?php echo asset_url(); ?>js/jquery.dataTables.min.js"></script> 
<script type="text/javascript" src="<?php echo asset_url(); ?>js/dataTables.bootstrap.min.js"></script> 
<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/dataTables.buttons.min.js">
        </script> 
<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/buttons.print.min.js">
        </script> 
<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/buttons.flash.min.js">
        </script> 
<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/jszip.min.js">
        </script> 
<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/pdfmake.min.js">
        </script> 
<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/vfs_fonts.js">
        </script> 
<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/buttons.html5.min.js">
        </script> 
<script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/buttons.print.min.js"></script> 
<script type="text/javascript" src="<?php echo base_url('thems/js/bootbox.min.js'); ?>"></script>
<script>


//----------------------------------------------------------------------------	
jQuery(document).ready(function() {
    loadGrid();
	
});
//----------------------------------------------------------------------------	
function loadGrid() {
    $('#sample_table').DataTable({
		dom: 'Blfrtip',
				lengthMenu: [[10, 25, 50, -1 ], [10, 25, 50, "All"]],
				buttons: [
						{ extend: 'copy', text: '<i class="fa fa-copy fa-2x"></i>',footer: true, exportOptions:
				 [ 0,1,2]},
						   { extend: 'print', text: '<i class="fa fa-print fa-2x"></i>',footer: true,exportOptions:
	 { columns: [0,1,2] },title:"<span style='font-size:22px;' > <center>Pending Delivery Details<br><small>Date: <?php date_default_timezone_set("Asia/Colombo"); ;echo date("Y-m-d h:i:sa")?> , REF: <?php echo $delivary_details['loading_ref_no']?> , Area: <?php echo $delivary_details['area_name']?> , Vehicle: <?php echo $delivary_details['vehicle_code']?> , Sales Rep: <?php echo $delivary_details['user_first_name']?></small></center></span>"},
							{ extend: 'excel', text: '<i class="far fa-file-excel fa-2x"></i>Excel',footer: true,exportOptions:
	 { columns: [0,1,2] },title:"Sales Invoice List"},
							 { extend: 'pdf', text: '<i class="far fa-file-pdf fa-2x"></i>PDF',footer: true,exportOptions:
	 { columns: [0,1,2] },title:"Sales Invoice List"},							 
						],
        "processing": true,
        //"serverSide": true,
        "ajax": "<?php echo base_url('marketing_delivery_process/get_delivery_items?id='.$delivary_details['id']) ?>",
        "bDestroy": true,
        "iDisplayLength": 10,
        "order": [
            [0, "desc"]
        ],
        "fnFooterCallback": function(nRow, aaData, iStart, iEnd, aiDisplay) {}
    });
}
//----------------------------------------------------------------------------	
function expand_loading_sheet_print(id) 
{
	u=location.href;
	t=document.title;
	window.open('<?php echo base_url() ?>marketing_delivery_process/expand_loading_sheet_print?id='+id,'sharer','toolbar=0,status=0,width=700,height=700, left=10, top=10,scrollbars=yes');return false;
}
//----------------------------------------------------------------------------		
function update_return_product(id)
	{
		var return_qty = $('#return_qty_'+id).val();
		var delivery_id = $('#delivary_id').val();
	bootbox.confirm({
    message: "Are you sure return this item Qty "+return_qty+" ? Do you proceed it?",
    buttons: {
        confirm: {
            label: 'Yes',
            className: 'btn-success'
        },
        cancel: {
            label: 'No',
            className: 'btn-danger'
        }
    },
    callback: function (result) {
        console.log('This was logged in the callback: ' + result);
		if(result==true){
			var form_data = {
            product_id: id,
			return_qty: return_qty,
			delivery_id: delivery_id
        };
	jQuery.ajax({  type: "POST", 
                    url: "<?php echo base_url().'marketing_delivery_process/return_product'?>",
                    data: form_data,
                    cache: false,
                    success: function(response) {
                        var obj = JSON.parse(response);
                        if (obj.status == 1) {loadGrid();} else {
                            bootbox.alert({
                                message: obj.message,
                                size: 'small'
                            });
							loadGrid();
                        }
                    }
                });			
		}	
    }
});		
}
//----------------------------------------------------------------------------	
function show_hide(id)
{
	$(".autohide").hide();
	$("#del"+id).show();		
}
//----------------------------------------------------------------------------	
function sales_rep_details_print(id)
{
	u=location.href;
	t=document.title;
	window.open(''+id,'sharer','toolbar=0,status=0,width=700,height=700, left=10, top=10,scrollbars=yes');return false;
}
//----------------------------------------------------------------------------	
function sales_rep_details_print_1(id)
{
	u=location.href;
	t=document.title;
	window.open('<?php echo base_url().'marketing_delivery_process/print_delivary_sheet/'?>'+id,'sharer','toolbar=0,status=0,width=700,height=700, left=10, top=10,scrollbars=yes');return false;
}
//----------------------------------------------------------------------------	
function sales_rep_details_print_2(id)
{
	u=location.href;
	t=document.title;
	window.open('<?php echo base_url().'marketing_delivery_process/print_loading_sheet/'?>'+id,'sharer','toolbar=0,status=0,width=700,height=700, left=10, top=10,scrollbars=yes');return false;
}
//----------------------------------------------------------------------------	
function sales_rep_details_print_3(id)
{
	u=location.href;
	t=document.title;
	window.open('<?php echo base_url().'customer_order_process/bill/'?>'+id,'sharer','toolbar=0,status=0,width=700,height=700, left=10, top=10,scrollbars=yes');return false;
}
//----------------------------------------------------------------------------		
function turn_invoice_type_as_cash(id) {
    bootbox.confirm({
        title: "Are You Sure ?",
        message: "Do you want this credit bill turn as a cash bill?.",
        buttons: {
            cancel: {
                label: '<i class="fa fa-times"></i> No Cancel'
            },
            confirm: {
                label: '<i class="fa fa-check"></i> Yes Confirm'
            }
        },
        callback: function(result) {
            if (result == true) {
                jQuery.ajax({
                    type: "POST", 
                    url: "<?php echo base_url().'customer_order_process/turn_credit_order_as_cash_order?id='?>"+id,
                    //       data: 'id='+id,
                    cache: false,
                    success: function(response) {
                        var obj = JSON.parse(response);
                        if (obj.status == 1) {} else {
                            bootbox.alert({
                                message: obj.disMsg,
                                size: 'small'
                            });
                        }
                        loadGrid();
                    }
                });
            }
        }
    });
}
//----------------------------------------------------------------------------		
function roll_back_order(id) {
    bootbox.confirm({
        title: "Are You Sure ?",
        message: "Do you want rollback this order?.",
        buttons: {
            cancel: {
                label: '<i class="fa fa-times"></i> No Cancel'
            },
            confirm: {
                label: '<i class="fa fa-check"></i> Yes Confirm'
            }
        },
        callback: function(result) {
            if (result == true) {
                jQuery.ajax({
                    type: "POST", 
                    url: "<?php echo base_url().'customer_order_process/rollback_order?id='?>"+id,
                    //       data: 'id='+id,
                    cache: false,
                    success: function(response) {
                        var obj = JSON.parse(response);
                        if (obj.status == 1) {} else {
                            bootbox.alert({
                                message: obj.disMsg,
                                size: 'small'
                            });
                        }
						location.reload();
                        //loadGrid();
                    }
                });
            }
        }
    });
}
//----------------------------------------------------------------------------		
	


</script>
</body>
<!-- end: BODY -->
</html>