0

I did multiple inserts with different qty. when making a transaction, I accommodate it in a temporary or append table. However, only the last qty is stored

this HTML

MODEL

public function simpan_data($data_pickup){
  $this->simpan_pickup($data_pickup);
  $last_key = $this->db->insert_id();
  $tmp = $this->get_temporary();
  foreach($tmp as $t){
  $data_detail = array(
  'id_pickup'       => $last_key,
  'id_barang'       => $t->id_barang,
  'qty_pickup'      => $this->input->post('qty_pickup')
  );
  $this->db->insert('pickup_detail', $data_detail);
}
} 

public function simpan_pickup($data){
$res = $this->db->insert('pickup', $data);
return $res;
}

CONTROLLER

public function add_tem_pickup(){
    $idnya                 = $this->input->post('id_barang');
    $cektmp                = $this->M_pickup->cek_temporary($idnya);
    if ($cektmp > 0) {
        $arr=array(
        'sukses'  => false,
        'pesan'   => 'Barang sudah Pickup'
    );
    //alert
    }else{
    $data_insert      = array(
      'jenis'         => 'PICK',
      'id_barang'     => $idnya
    );
    $this->db->insert('tem_pickup', $data_insert);
    $arr = array(
      'sukses' => true,
      'pesan'   => 'berhasil'
    );
    }
      echo json_encode($arr);
    }

public function simpan_pickup(){
    $data = array(
    'kd_pickup'     => $this->input->post('kd_pickup'),
    'id_cs'         => $this->input->post('id_cs')
    );
    $this->M_pickup->simpan_data($data);
    $this->db->delete('tem_pickup', array('jenis' => 'PICK'));
    echo $this->session->set_flashdata('message','success');
    redirect('backend/pickup');
}

how could this happen ? Please help

IMAGE EXAMPLE

Tito
  • 17
  • 6
  • Could you show your html form or the `qty_pickup` input form? – Hasta Dhana Sep 01 '19 at 15:13
  • i'm edit a post wait – Tito Sep 01 '19 at 15:20
  • https://pastebin.com/EC9ewJth @HastaDhana – Tito Sep 01 '19 at 15:29
  • Could you explain more on your problem, there is only one `qty_pickup` input on your form, not multiple like what you've said – Hasta Dhana Sep 01 '19 at 15:43
  • can u speaking indonesia ? I saw your location in Indonesia :D – Tito Sep 01 '19 at 15:45
  • bisa, tapi di StackOverflow sepertinya dilarang, masalahnya di bagian manakah? – Hasta Dhana Sep 01 '19 at 15:49
  • saya membuat tabel inputan, dimana setiap button (tambah), data disimpan di tabel temporary/ditampung .. (saat disimpan di tabel temporary .. tiap qty yang saya inputkan sesuai mas) saya membuat multi insert apabila transaksi selesai maka data di submit dengan nama button simpan saya mempunyai tabel pickup dan pickup detail (ini hanya untuk histori) saat saya simpan (proses multi insert), qty nya berbeda hanya mengambil qty terahir yg saya inputkan saja mas . need help – Tito Sep 01 '19 at 15:53

1 Answers1

0

You could setup a hidden qty_pickup input after the ajax calls success (using jquery after()), here I renamed this new input to qty_pickup_new[] to differentiate it with the existing qty_pickup input :

// another js codes here...
//
$(".btnadd").click(function(){ //input append 
        var id_barang                         = $("input[name='id_barang']").val();
        var nama_barang                       = $("input[name='nama_barang']").val();
        var qty_pickup                        = $("input[name='qty_pickup']").val();
        var sisa                              = $("input[name='qty_pickup']").val();
        var harga_jual                        = $("input[name='harga_jual']").val();
        var satuan                            = $("input[name='satuan']").val();
        var kd_pickup                         = $("input[name='kd_pickup']").val();
        // var tgl_pickup                        = $("input[name='tgl_pickup']").val();
        var tipe_dimensi                      = $("input[name='tipe_dimensi']").val();
        var tgl_pickup                        = $("input[name='tgl_pickup']").val();

        $.ajax({
            url: '<?php echo base_url();?>backend/pickup/add_tem_pickup',
            type: 'POST',
            dataType: 'JSON',
            data: {
                id_barang: id_barang,
                qty_pickup: qty_pickup,
                sisa: qty_pickup,
                harga_jual: harga_jual, 
                kd_pickup: kd_pickup,  
                // tgl_pickup: tgl_pickup, 
                satuan: satuan, 
                tipe_dimensi: tipe_dimensi,
                tgl_pickup: tgl_pickup, 
                },
            error: function() {
            },
            success: function(data) {
            if(data.sukses==false){
            alert(data.pesan);
            }else{
            $('#tbody').append("<tr><td>"+kd_pickup+"</td><td>"+nama_barang+"</td><td>"+qty_pickup+"</td><td>"+harga_jual+"</td><td>"+satuan+"</td><td>"+tipe_dimensi+"</td></tr>"); 
            //  added below codes, set array input with id_barang as the key
            $('[name="harga_jual"]').after('<input type="hidden" name="qty_pickup_new[' + id_barang + ']" value="' + qty_pickup + '">');
            }
        }
    });
});

Then you could use the qty_pickup_new[] on the model by using the id_barang as the key (as previous javascript codes does) :

public function simpan_data($data_pickup){
    $this->simpan_pickup($data_pickup);
    $last_key = $this->db->insert_id();
    $tmp = $this->get_temporary();
    foreach($tmp as $t){
        $qty_pickup = $this->input->post('qty_pickup_new')[$t->id_barang]; // using id_barang as qty_pickup_new[] key
        $data_detail = array(
            'id_pickup'       => $last_key,
            'id_barang'       => $t->id_barang,
            'qty_pickup'      => !empty($qty_pickup) ? $qty_pickup : '' // sets data as empty if input is empty
        );
        $this->db->insert('pickup_detail', $data_detail);
    }
} 
Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
  • Thank you very much sir, I want to learn a lot, is there a contact that can be contacted, for example telegram? maybe i can give an incentive someday :) – Tito Sep 01 '19 at 16:53