在Laravel 5.5中将多个数据表临时数据映射到数据库中

时间:2019-03-20 08:52:00

标签: javascript php laravel datatables laravel-blade

您好,我想知道如何从临时数据表数据中保存数据, 我使用模态形式调用数据,并将其填充到texte字段中,然后使用按钮将其发送到临时数据表,

我想在我按下保存按钮时将数据存储在数据库表purchase_order_details

,并且还与Purchase_orders表相关,

这是我的代码

@extends('layouts.app')
@section('content')

<div class="box">
    <div class="box-header">
        <h4 class="page-title">
            {{ $siteTitle }}
            <a href="{{ route('admin.purchase_orders.index') }}" class="btn btn-info pull-right" style="vertical-align:middle; margin-top:-1%" data-toggle="tooltip" title="Back"><i class="fa fa-arrow-left"></i> Back</a> 
        </h4>
    </div>

    {{-- Form po start here --}}

    {!! Form::open(['method' => 'POST', 'autocomplete' => 'off', 'id' => 'Simpan','data-toggle' => 'validator', 'files' => true,]) !!}
   
    <div class="panel-body boxs">
        <div class="row">

            <div class="col-md-3 form-group">
                <label for="kode_trx" class="control-label">Kode<span class="text-red">*</span></label>
                <input class="form-control" required name="kode_trx" type="text" id="kode_trx" title="Kode Barang" readonly value="{{ $kodeTrx }}">
                <span class="help-block with-errors"></span>
            </div>

            <div class="col-md-3 form-group">
                <label for="no_po" class="control-label">No. PO<span class="text-red">*</span></label>
                <input class="form-control" required name="no_po" type="text" id="no_po" title="No PO" readonly value="{{ $kodeOto }}">
                <span class="help-block with-errors"></span>             
            </div>

            <div class="col-md-3 form-group">
                <label for="supplier" class="control-label">Supplier<span class="text-red">*</span></label>
                {{-- <input type="hidden" name="supplier_id" required id="supplier_id">
                <input type="hidden" name="supplier_kode" required id="supplier_kode"> --}}

                {!! Form::hidden('supplier_id', old('supplier_id'), ['class' => 'form-control', 'required' => '', 'id' =>'supplier_id']) !!}
                {!! Form::hidden('supplier_kode', old('supplier_kode'), ['class' => 'form-control', 'required' => '', 'id' =>'supplier_kode']) !!}
                {!! Form::text('nama_supplier', old('nama_supplier'), ['class' => 'form-control', 'required' => '', 'data-target' => '#datasupplier', 'id' => 'nama_supplier', 'data-toggle' => 'modal', 'title' => 'Supplier', 'data-error' => 'Silahkan pilih supplier', 'onkeydown' => 'return false']) !!} 
                <span class="help-block with-errors" aria-invalid="Pilih Supplier Dulu"></span>      
            </div>

            <div class="col-md-3 form-group">
                <label class="control-label">Contact Person</label>
                <input class="form-control" required name="pic" type="text" id="pic" title="Kontak Supplier" data-toggle="tooltip" pattern="[a-zA-Z_\-.,/ ]*$" data-error="hanya huruf  (A-Z), dot (.), coma(,) and dash (-) yang dibolehkan." data-required-error="Field kontak person dibutuhkan">              
                <input class="form-control" required name="telp_pic" type="hidden" id="telp_pic" title="Telp PIC">
                <span class="help-block with-errors"></span>

            </div>
        </div>

        <div class="row">

          <div class="form-group col-md-3">
            <label for="delivery_date">Tgl Order<span class="text-red">*</span></label>
            <div class="input-group date" oninput="setCustomValidity('')">
              <div class="input-group-addon" oninput="setCustomValidity('')"> 
                <i class="fa fa-calendar" oninput="setCustomValidity('')"></i>
              </div>
              
              <input type="text" oninput="setCustomValidity('')" data-toggle="tooltip" title="tgl order" class="form-control pull-right" name="tanggal" id="tanggal" pattern="(0[1-9]|1[0-9]|2[0-9]|3[01]).(0[1-9]|1[012]).[0-9]{4}" required data-required-error="Tanggal order kosong" data-error="format tanggal dd-mm-yyyy" onkeydown="return false" placeholder="{{ date('d-m-Y') }}">
              <span class="help-block with-errors"></span>
            </div>
          </div>

          <div class="form-group col-md-3">
            <label for="top">T.O.P</label><span class="text-red">*</span>
            <input type="text" name="top" required id="top" class="form-control" data-required-error="T.O.P Order kosong." >
            <span class="help-block with-errors"></span>
          </div>

          <div class="form-group col-md-6">
            <label class="control-label">Notes <span class="text-red"></span> </label>
            <textarea class="form-control " placeholder="" title="" data-toggle="tooltip" name="keterangan" cols="50" rows="10" id="keterangan" data-original-title="Catatan Untuk Supplier" style="height:60px!important;"></textarea>
          </div>
          <input type="hidden" name="created_by" id="created_by" value="{{ Auth::user()->role->id }}">
        </div>

<hr style="border: 0; border-top: 3px double #995ca8;">
<h4 class="page-title">Pilih Barang</h4>
<hr style="border: 0; border-top: 3px double #995ca8;">

        <div class="row">
          <div class="col-md-3 form-group">
            <label for="nama_barang" class="control-label">Nama Barang<span class="text-red">*</span></label>
            <input class="form-control cacat" required name="nama_barang" type="text" id="nama_barang" title="Nama Barang" oninput="setCustomValidity('')" data-toggle="modal" data-target="#databarang" data-error="hanya huruf (A-Z), angka (0-9), dot (.), trademark (™) dan dash (-) yang dibolehkan" maxlength="70" onkeydown="return false">
            <span class="help-block with-errors"></span>
            
            <input class="form-control cacat" required name="id_barang" type="hidden" id="id_barang" title="ID Barang">
            <input class="form-control cacat" required name="kode_barang" type="hidden" id="kode_barang" title="Kode Barang">
          </div>
            {{-- pattern="[a-zA-Z_\-.™/^(|[1-9][0-9]*)]*$" 
            pattern="^(|[1-9][0-9]*)$"--}}
            
        {{-- </div>
            
        <div class="row"> --}}
          <div class="col-md-3 form-group">
              <label for="model_barang" class="control-label">Model Barang<span class="text-red">*</span></label>
              <input type="text" name="model_barang" required id="model_barang" readonly class="form-control cacat" data-required-error="Model barang kosong." required>
              <span class="help-block with-errors"></span>
          </div>
        {{-- </div>
        <div class="row"> --}}
                
          <div class="col-md-2 form-group">
              <label for="width">Width</label>
              <input type="text" name="width" id="width" pattern="^(|[1-9][0-9]*)$" maxlength="4" data-error="Hanya angka dan awalan bukan 0" oninput="setCustomValidity('')" class="form-control cacat" data-required-error="Ukuran Barang Diperlukan" required>
              <span class="help-block with-errors"></span>
          </div>
          <div class="col-md-2 form-group">
              <label for="depth">Depth</label>
              <input type="text" name="depth" id="depth" pattern="^(|[1-9][0-9]*)$" maxlength="4" data-error="Hanya angka dan awalan bukan 0" oninput="setCustomValidity('')" class="form-control cacat" data-required-error="Ukuran Barang Diperlukan" required>
              <span class="help-block with-errors"></span>
          </div>
          <div class="col-md-2 form-group">
              <label for="height">Height</label>
              <input type="text" name="height" id="height" pattern="^(|[1-9][0-9]*)$" maxlength="4" data-error="Hanya angka dan awalan bukan 0" oninput="setCustomValidity('')" class="form-control cacat" data-required-error="Ukuran Barang Diperlukan" required>
              <span class="help-block with-errors"></span>
          </div>
        </div>

        <div class="row">
            <div class="col-md-3 form-group">
                <label class="control-label">Qty <span class="text-red">*</span> </label>
                <input type="text" name="qty" id="qty" required class="form-control cacat" oninvalid="this.setCustomValidity('Input Qty Order')" data-required-error="Qty order diperlukan" pattern="^(|[1-9][0-9]*)$" maxlength="10" onkeypress="isInputNumber(event)" oninput="setCustomValidity('')">
                <span class="help-block with-errors"></span>
            </div>

            <div class="col-md-3 form-group">
                <label for="harga_beli"  class="control-label">Harga Beli <span class="text-red">*</span> </label>
                <input type="text" name="harga_beli" id="harga_beli" required class="form-control cacat" oninvalid="this.setCustomValidity('Harga beli')" data-required-error="Harga beli diperlukan" pattern="^(|[1-9][0-9]*)$" maxlength="10" onkeypress="isInputNumber(event)" oninput="setCustomValidity('')">
                <span class="help-block with-errors"></span>
            </div>
            
            <div class="col-md-2 form-group" >
                <label class="control-label" for="tambah"> &nbsp; <span class="text-red"></span></label>
                <a class="btn btn-success disabled tambahkan pull-right col-md-12" id="tambah" data-toggle="tooltip" title="Tambah Barang"><i class="fa fa-plus"></i> Tambah</a> 

                {{-- <a class="btn btn-success col-md-2 form-control btn-flat tambahkan disabled" id="tambah"><i class='fa fa-plus'></i>  Add  </a>  --}}
            </div>
        </div>
        <div class="row">
		    </div>
		
		<hr style="border: 0; border-top: 3px double #995ca8;">
		<h4>Detail PO</h4>
		<hr style="border: 0; border-top: 3px double #995ca8;">
		
		<div class="table-responsive">
        <table id="tb_detail_po" class="table table-bordered table-striped">
            <thead>
                <tr style="">
                    <th width="10%" style="text-align: center">Kode</th>
                    <th width="20%">Nama</th>
                    <th width="10%" style="text-align: center">Tipe</th>
                    <th width="5%" style="text-align: center">W</th>
                    <th width="5%" style="text-align: center">D</th>
                    <th width="5%" style="text-align: center">H</th>
                    <th width="5%" style="text-align: center">Qty</th>
                    <th width="20%" style="text-align: center">Harga Beli</th>
                    <th width="20%" style="text-align: center">Total</th>
                    <th width="2px">Action</th>
                    <th hidden>ID Barang</th>
                    <th hidden>Kode PO</th>
                    <th hidden>No PO</th>
                    <th hidden>No User</th>
                </tr>
            </thead>
            <tbody id="mytbody" style="white-space:nowrap"></tbody>
            <tfoot style="border">
                <tr style="font-size: 15px;font-weight: 600; background-color: #ecf0f5;">
                  <td colspan='8' style="border-top: 1px solid #dddddd; padding: 8px 10px 8px 10px"><span style="float:right;">Qty Item</span></td>
                  <td colspan='1' style="border-top: 1px solid #dddddd; padding: 8px 10px 8px 10px"><span style="float:right;" id ="totalItem"></span> </td>
                  <td colspan='1' style="border-top: 1px solid #dddddd; padding: 8px 10px 8px 10px"><span style="float:right;"></span></td>
                </tr>
                <tr style="font-size: 15px;font-weight: 600; background-color: #ecf0f5;">
                  <td colspan='8' style="border-top: 0px solid #ffffff; padding: 8px 10px 8px 10px"><span style="float:right;">Total PO</span></td>
                  <td colspan='1' style="border-top: 0px solid #ffffff; padding: 8px 10px 8px 10px"><span style="float:right;" id="totalPurchaseOrder"></span> </td>
                  <td colspan='1' style="border-top: 0px solid #ffffff; padding: 8px 10px 8px 10px"><span style="float:right;"></span></td>
                </tr>
            </tfoot>
        </table>
    </div>  
      {{-- <table width="100%" cellspacing="0" cellpadding="0" style="width: 100%; border-collapse: collapse;" id="total_po">
          <tbody>
              <tr style="font-size: 15px;">
                  <td style="padding: 15px 0 15px 30px; font-weight: 600; text-align: right">Total Item</td>
                  <td style="padding: 15px 30px 15px 0; font-weight: 600; text-align: right;" id="TotalItem"></td>
              </tr>
              <tr style="font-size: 15px; color: rgb(66,181,73); background-color: rgba(111, 107, 107, 0.1);">
                  <td colspan="6" rowspan="2" style="padding: 15px 0 15px 30px; font-weight: 600; text-align: right">Total Price</td>
                  <td style="padding: 15px 30px 15px 0; font-weight: 600; text-align: right;" id="totalSalary">
                    
                  </td>
              </tr>
              
              <tr>
                  <td style="padding: 0 0 10px 30px"></td>
                  <td style="padding: 0 0 10px 30px"></td>
              </tr>
              
          </tbody>
      </table> --}}
        <button type="submit" class="btn btn-success btn-lg fa fa-save save pull-right" id="Simpan"> Simpan</button>
    </div>
    {!! Form::close() !!}
</div>


@include('admin.purchase_orders.datasuplai')
@include('admin.purchase_orders.databarang')

@stop

@section('javascript')
@parent

<script src="https://cdn.datatables.net/plug-ins/1.10.15/api/sum().js"></script>

<script>

// $(document).ready(function() {
//     $('#tb_detail_po').dataTable({	
// 		"sPaginationType": "full_numbers",
// 		"footerCallback": function ( row, data, start, end, display ) {
// 				var api = this.api(), data;	 
// 				// Remove the formatting to get integer data for summation
// 				var intVal = function ( i ) {
// 					return typeof i === 'string' ? i.replace(/[\$,]/g, '')*1 : typeof i === 'number' ?	i : 0;
// 				};
	 
// 				// total_salary over all pages
// 				total_salary = api.column( 8 ).data().reduce( function (a, b) {
// 					return intVal(a) + intVal(b);
// 				},0 );
				
// 				// total_page_salary over this page
// 				total_page_salary = api.column( 8, { page: 'current'} ).data().reduce( function (a, b) {
// 					return intVal(a) + intVal(b);
// 				}, 0 );
				
// 				total_page_salary = parseFloat(total_page_salary);
// 				total_salary = parseFloat(total_salary);
// 				// Update footer
// 				$('#totalSalary').html(total_page_salary.toFixed(2)+"/"+total_salary.toFixed(2));				
// 			},		
// 	});
// });

</script>
<script type="text/javascript">
    $(function () {
      $("#tb_detail_po").DataTable({
        paging:false, searching:false, ordering:false,
      //   columnDefs:[{
      //   targets:[7,8], render: $.fn.dataTable.render.number('.',',','','Rp. ')
      // }],
      "sPaginationType": "full_numbers",
		  "footerCallback": function ( row, data, start, end, display ) {
				var api = this.api(), data;	 
				// Remove the formatting to get integer data for summation
				var intVal = function ( i ) {
					return typeof i === 'string' ? i.replace(/[\$,]/g, '')*1 : typeof i === 'number' ?	i : 0;
				};
	 
				// total_salary over all pages
				total_salary = api.column( 8 ).data().reduce( function (a, b) {
					return intVal(a) + intVal(b);
				},0 );

        total_item = api.column( 6 ).data().reduce( function (a, b) {
					return intVal(a) + intVal(b);
				},0 );
				
				// total_page_salary over this page
				total_page_salary = api.column( 8, { page: 'current'} ).data().reduce( function (a, b) {
					return intVal(a) + intVal(b);
				}, 0 );
				
				total_page_salary = parseFloat(total_page_salary);
				total_salary = parseFloat(total_salary);
        total_item = parseInt(total_item);
				// Update footer

        if (total_salary <= 0 && total_item <=0 ){
          $('#totalPurchaseOrder').html('');
          $('#totalItem').html('');
        }
        else{
				  // $('#totalPurchaseOrder').html(total_page_salary.toFixed()+"/"+total_salary.toFixed());				
				  $('#totalPurchaseOrder').html(total_salary.toFixed());				
				  $('#totalItem').html(total_item.toFixed());				
        }
			}

      });
      $("#tblSupliers2").DataTable({
        paging:false, searching:true
      });
      $("#tblBarang").DataTable({
        paging:false, searching:true
	  });

    //AJAX SAVE HERE 
    // alert(tabel.rows().data().toArray());
    $(document).on('submit',function(e){
      if(!e.isDefaultPrevented())
      {
        var tabel = $("#tb_detail_po").DataTable();
        var jumlah = tabel.rows().count();
        if(jumlah == 0)
        {
          swal({
                  title: "Barang PO Tidak Boleh Kosong",
                  text: "Masukan Data Barang Yang Akan di PO",
                  type: "warning",
                  timer: 2000
              });
        }
        else
        {
          var _data = {
            no_nota: $('input[name=id]').val(),
            customer: $('input[name=IdCustomer]').val(),
            karyawan: $('input[name=karyawan]').val(),
            notelp: $('input[name=notelp]').val(),
            tanggal: $('input[name=tanggal]').val(),
            tb_detail_po: tabel.rows().data().toArray(),
          };
  
          $.ajax({
            url: "{{ url('admin.purchase_orders.store') }}",
            type: "POST",
            method: "POST",
            data: _data,
            success: function(data)
            {
              obj = JSON.parse(data);
              swal({
                  title: "Sukses",
                  text: obj.message,
                  type: "success",
                  timer: 2000,
                  button: "ok",
              }, function(){
                window.location = "{{ route('admin.purchase_orders.index') }}";
              });
            }
          });
        }
      }
      return false;
    });

  });

</script>

{{-- Panggil FUnction --}}
{{-- Isi Value Field Supplier Dari Modal --}}
<script type="text/javascript">
   $('.pilihsupplier').on('click',function(){
       $('.help-block').text('');
       $('.form-group').removeClass('has-error');
       var IDSupplier=$(this).closest('tr').children('td.IDSupplier').text();
       var KodeSupplier=$(this).closest('tr').children('td.KodeSupplier').text();
       var NamaSupplier =$(this).closest('tr').children('td.NamaSupplier').text();
       var PICSupplier =$(this).closest('tr').children('td.PICSupplier').text();
       var TelpSupplier =$(this).closest('tr').children('td.TelpSupplier').text();
       // var EmailSupplier =$(this).closest('tr').children('td.EmailSupplier').text();
       var datasupplier = $('#datasupplier');
       
         $('#supplier_id').val(IDSupplier);
         $('#supplier_kode').val(KodeSupplier);
         $('#nama_supplier').val(NamaSupplier);
         $('#pic').val(PICSupplier);
         $('#telp_pic').val(TelpSupplier);
         // $('#EmailSupplier').val(EmailSupplier);
         panggil(); /*Function untuk cek konidisi pada buttom simpan di detail barang po*/
         datasupplier.modal('hide'); 
        // $('#tanggal').focus();
        $('#top').focus();
   });
</script>


<script type="text/javascript">
	$('.pilihbarang').on('click',function(){
		$('.help-block').text('');
		$('.form-group').removeClass('has-error');
		var IdBarang=$(this).closest('tr').children('td.IdBarang').text();
		var KdBarang=$(this).closest('tr').children('td.KdBarang').text();
		var NamaBarang =$(this).closest('tr').children('td.NamaBarang').text();
		var ModelBarang =$(this).closest('tr').children('td.ModelBarang').text();
		var Width =$(this).closest('tr').children('td.Width').text();
		var Depth =$(this).closest('tr').children('td.Depth').text();
		var Height =$(this).closest('tr').children('td.Height').text();
		var Stock =$(this).closest('tr').children('td.Stock').text();
		var Harga =$(this).closest('tr').children('td.Harga').text();
		var databarang = $('#databarang');
		
		  $('#id_barang').val(IdBarang);
		  $('#kode_barang').val(KdBarang);
		  $('#nama_barang').val(NamaBarang);
		  $('#model_barang').val(ModelBarang);
		  $('#width').val(Width);
		  $('#depth').val(Depth);
		  $('#height').val(Height);
		  $('#harga_beli').val('');

		  databarang.modal('hide');

      $('#qty').val(1);
      $("#qty").focus();
      panggil(); /*Function untuk cek konidisi pada buttom simpan di detail barang po*/
	});
</script>
    
<script>
  $('#nama_barang,#width,#depth,#height,#qty, #harga_beli').keyup(function (){
      if ($('#nama_barang').val()=='' || $('#width').val()=='' || $('#depth').val()=='' || $('#height').val()=='' || $('#qty').val()=='' || $('#harga_beli').val()=='') {
        $('#tambah').addClass('disabled');
      };

      if ($('#nama_barang').val()!='' && $('#width').val()!='' && $('#depth').val()!='' && $('#height').val()!='' && $('#qty').val()!='' && $('#harga_beli').val()!='') {
        $('#tambah').removeClass('disabled');
      };
  });
</script>

<script>
    $(document).ready(function(){
      $('#tambah').on('click', function(){
        $('#tambah').addClass('disabled');
           hapusReq();
      });
    });


</script>

{{-- Script Untuk Tambah data table detail  --}}
<script type="text/javascript">
    $(document).ready(function(){
      var t = $('#tb_detail_po').DataTable();
      var i = 1;

    $('.tambahkan').on('click', function() {
        var IdBarang = $('#id_barang').val();
        var KdBarang = $('#kode_barang').val();
        var NamaBarang = $('#nama_barang').val();
        var ModelBarang = $('#model_barang').val();
        var Width = $('#width').val();
        var Depth = $('#depth').val();
        var Height = $('#height').val();
        var Qty = $('#qty').val();
        var Harga = $('#harga_beli').val();
        var IdPurchase = $('#kode_trx').val();
        var NoPo = $('#no_po').val();
        var UserId = $('#created_by').val();
        var Total = $('#qty').val() * $('#harga_beli').val();
        var Action = "<a class='btn btn-danger btn-xs Hapus' title='Remove Item'> <span class='fa fa-minus-square'></span> </a>"
        

        var row = t.row.add( [
            KdBarang,
            NamaBarang,
            ModelBarang,
            Width,
            Depth,
            Height,
            Qty,
            Harga,
            Total,
            Action,
            IdBarang,
            IdPurchase,
            NoPo,
            UserId,
        ] ).draw(false);
        t.row(row).column(0).nodes().to$().addClass('IdBarang');
        t.row(row).column(10).nodes().to$().css('display','none');
        t.row(row).column(11).nodes().to$().css('display','none');
        t.row(row).column(12).nodes().to$().css('display','none');
        t.row(row).column(13).nodes().to$().css('display','none');
        t.row(row).column(8).nodes().to$().css('text-align','right');
        t.row(row).column(7).nodes().to$().css('text-align','right');
        


         $('#id_barang').val('');
         $('#kode_barang').val('');
         $('#nama_barang').val('');
         $('#model_barang').val('');
         $('#width').val('');
         $('#depth').val('');
         $('#height').val('');
         $('#qty').val('');
         $('#harga_beli').val('');

    })
  })
    
</script>

<script type="text/javascript">
   $('#tb_detail_po tbody').on('click', '.Hapus', function(e){
      var t = $('#tb_detail_po').DataTable();
      t.row($(this).closest('tr')).remove().draw(false);
 })
</script>

<script>
	    function panggil()
    {
      $('.save').removeClass('disabled');
      
    }

    function hapusReq(argument) {
      var cls = document.getElementsByClassName('cacat');

      for(var i = 0; i < cls.length; i++) {
      cls[i].removeAttribute('required');
      }
    }
</script>

{{-- End Panggil FUnction --}}


<script>
	function isInputNumber(evt) {
		var ch = String.fromCharCode(evt.which);
		if(!(/[0-9]/.test(ch))){
			evt.preventDefault();
		}   
	}
		
	function isInputString(evt) {
		var cg = String.fromCharCode(evt.which);
		if((/[0-9]/.test(cg))){
			evt.preventDefault();
		}   
	}
</script>

<script>
	var dateToday = new Date();
	var dates = $("#tanggal").datepicker({
		defaultDate: "+1w",
		changeMonth: true,
		numberOfMonths: 1,
		dateFormat: 'dd-mm-yy',
		minDate: dateToday,
		onSelect: function(selectedDate) {
			var option = this.id == "from" ? "minDate" : "maxDate",
				instance = $(this).data("datepicker"),
				date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
			dates.not(this).datepicker("option", option, date);
		} , 
		onClose: function(){
			this.focus();
			$("#top").focus();
		}
	});
</script>



@endsection

mycontroller存储方法为空,因为我尚不知道如何保存它,

0 个答案:

没有答案