Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.2 kB
1
Indexable
Never
 function add_datapemakaian_bahan($kode, $kodemutasi)
  {
    $bahan_pakai   =  $this->security->xss_clean($this->input->post('bahan_pakai'));
    $id_bb    =  $this->security->xss_clean($this->input->post('id_bb'));
    $kode_bb   =  $this->security->xss_clean($this->input->post('kode_bb'));
    $tanggal_pakai     =  date('Y-m-d', strtotime($this->security->xss_clean($this->input->post('tanggal_pakai'))));
    $jmlh_pakai_roll    =  $this->security->xss_clean($this->input->post('jmlh_pakai_roll'));
    $jmlh_pakai_roll1    =  $this->security->xss_clean($this->input->post('jmlh_pakai_roll'));
    $jmlh_pakai_kg    =  $this->security->xss_clean($this->input->post('jmlh_pakai_kg'));
    $jmlh_pakai_kg1    =  $this->security->xss_clean($this->input->post('jmlh_pakai_kg'));
    $keteranganmutasibahan =  $this->security->xss_clean($this->input->post('keteranganmutasibahan'));
    // Total Stok Roll
    $sqltotalstok = $this->db->query("SELECT *,SUM(jmlh_roll) AS totalroll FROM bahan_produksi_bintaro WHERE id_bb='$id_bb'");
    $data = $sqltotalstok->row_array();
    $stok_roll = $data['totalroll'];
    // Total Stok KG
    $sqltotalstokkg = $this->db->query("SELECT *,SUM(jmlh_kg) AS totalkg FROM bahan_produksi_bintaro WHERE id_bb='$id_bb'");
    $data = $sqltotalstokkg->row_array();
    $stok_allkg = $data['totalkg'];
    //sql pilih stok Roll
    $sqlpilihroll = $this->db->query("SELECT * FROM bahan_produksi_bintaro WHERE kode_bb='$kode_bb' AND jmlh_roll > 0 order by kode_bb ASC")->result_array();
    //sql pilih stok kg
    $sqlpilihkg = $this->db->query("SELECT * FROM bahan_produksi_bintaro WHERE kode_bb='$kode_bb' AND jmlh_kg > 0 order by kode_bb ASC")->result_array();


    if ($jmlh_pakai_roll <= $stok_roll) {
      echo 'masuk';
      foreach ($sqlpilihroll as $row) {
        $id_bb = $row['id_bb'];
        $jmlh_roll = $row['jmlh_roll'];

        if ($jmlh_pakai_roll > 0) {
          // buat var $temp sbg. pengurang 
          $temp = $jmlh_pakai_roll;

        /** PROSES PENGURANGAN ROLL */
          $jmlh_pakai_roll = $jmlh_pakai_roll - $jmlh_roll;
          $jmlh_roll - $temp;

          if ($jmlh_pakai_roll > 0) {
            $stok_update = 0;
            $stok_update1 = $jmlh_roll;
          } else {
            $stok_update = $jmlh_roll - $temp;
            $stok_update1 = $temp;
          }
        /** PROSES PENGURANGAN STOCK KG */
          if ($jmlh_pakai_kg <= $stok_allkg) {
            echo 'masuk';
            foreach ($sqlpilihkg as $roww) {
              $id_bb = $roww['id_bb'];
              $jmlh_kg = $roww['jmlh_kg'];
            }
            if ($jmlh_pakai_kg > 0) {
              // buat var $temp sbg. pengurang Jumlah KG
              $temp = $jmlh_pakai_kg;

              //proses pengurangan kg
              $jmlh_pakai_kg = $jmlh_pakai_kg - $jmlh_kg;
              $jmlh_kg - $temp;

              if ($jmlh_pakai_kg > 0) {
                $stok_updatekg = 0;
                $stok_updatekg1 = $jmlh_kg;
              } else {
                $stok_updatekg = $jmlh_kg - $temp;
                $stok_updatekg1 = $temp;
              }

              $this->db->set('jmlh_roll', $stok_update);
              $this->db->where('kode_bb', $bahan_pakai);
              $this->db->where('id_bb', $id_bb);
              $this->db->update('bahan_produksi_bintaro');
              $this->db->set('jmlh_kg', $stok_updatekg);
              $this->db->where('kode_bb', $bahan_pakai);
              $this->db->where('id_bb', $id_bb);
              $this->db->update('bahan_produksi_bintaro');

              $data = array(
                'id_pakai' => $kode,
                'id_stokpakai' => $id_bb,
                'id_mutasibahankeluar' => $kodemutasi,
                'bahan_pakai' => $bahan_pakai,
                'tanggal_pakai' => $tanggal_pakai,
                'tanggalsistem' => date('Y-m-d H:i:s'),
                'jmlh_pakai_roll' => $stok_update1,
                'jmlh_pakai_kg' => $stok_updatekg1,
                'keteranganmutasibahan' => $keteranganmutasibahan

              );
              $this->db->insert('t_pemakaianbahan', $data);


              $data2 =  array(
                'id' => NULL,
                'waktu' => date('Y-m-d H:M:S'),
                'username' => $this->session->userdata('username'),
                'ket' => 'Menambah Data Pemakaian Bahan Baku Dengan ID' . $kode,
                'status' => 'add'
              );

              $this->db->insert('log', $data2);
            }
          }
        }
      }

      $data3 = array(
        'id_mutasibahan' => $kodemutasi,
        'tgl_mutasibahan' => $tanggal_pakai,
        'tgl_sistem' => date('Y-m-d H:i:s'),
        'bahan_masukkeluar' => $bahan_pakai,
        'idtrs_mutasibahan' => $kode,
        'masuk' => 0,
        'keluar' => $jmlh_pakai_roll,
        'masuk_kg' => 0,
        'keluar_kg' => $jmlh_pakai_kg,
        'keteranganmutasibahan' => $keteranganmutasibahan

      );
      $this->db->insert('mutasibahan', $data3);
    } else {
      echo 'gagal';
    }
    return $this->db->affected_rows();
  }