modal

mail@pastecode.io avatar
unknown
abap
2 years ago
5.9 kB
4
Indexable
Never
function add_datapemakaian_bahan($kode, $kodemutasi)
  {
    /** INPUT POST KE TABLE */
    $bahan_pakai   =  $this->security->xss_clean($this->input->post('bahan_pakai'));
    $tanggal_pakai     =  date('d-m-Y', 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_kg    =  $this->security->xss_clean($this->input->post('jmlh_pakai_kg'));
    $estimasi_hasil =  $this->security->xss_clean($this->input->post('estimasi_hasil'));
    $keterangan_cutting =  $this->security->xss_clean($this->input->post('keterangan_cutting'));
    $no_konsep =  $this->security->xss_clean($this->input->post('no_konsep'));
    $dibuat =  $this->security->xss_clean($this->input->post('dibuat'));

    /** MENGHITUNG TOTAL STOK */
    // Total Stok Roll
    $sqltotalstok = $this->db->query("SELECT *,SUM(jmlh_roll) AS totalroll FROM tb_bahanbaku 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 tb_bahanbaku WHERE id_bb=id_bb");
    $data = $sqltotalstokkg->row_array();
    $stok_allkg = $data['totalkg'];
    /** MEMILIH STOK TABLE */
    //sql pilih stok Roll
    $sqlpilihroll = $this->db->query("SELECT * FROM tb_bahanbaku 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 tb_bahanbaku WHERE kode_bb=kode_bb AND jmlh_kg > 0 order by kode_bb ASC")->result_array();

    /** PROSES PENGURANGAN TOTAL STOK ROLL */
    if ($jmlh_pakai_roll <= $stok_roll) {
      echo 'masuk';
      foreach ($sqlpilihroll as $row) {
        $id_bb = $row['id_bb'];
        $kode_bb = $row['kode_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('tb_bahanbaku');
              $this->db->set('jmlh_kg', $stok_updatekg);
              $this->db->where('kode_bb', $bahan_pakai);
              $this->db->where('id_bb', $id_bb);
              $this->db->update('tb_bahanbaku');
              var_dump($stok_update);
              var_dump($stok_updatekg);
              $data = array(
                'id_pakai' => $kode,
                'id_stokpakai' => $id_bb,
                'id_mutasibahankeluar' => $kodemutasi,
                'no_konsep' => $no_konsep,
                'bahan_pakai' => $bahan_pakai,
                'tanggal_pakai' => $tanggal_pakai,
                'tanggalsistem' => date('d/m/Y-H:i:s'),
                'jmlh_pakai_roll' => $stok_update1,
                'jmlh_pakai_kg' => $stok_updatekg1,
                'status' => 'CLOSE',
                'keteranganmutasibahan' => $keterangan_cutting
              );
              $this->db->insert('t_pemakaianbahan', $data);
              $data4 = array(
                'no_konsep' => $no_konsep,
                'tanggal' => date('d/m/Y'),
                'bahan_pakai' => $bahan_pakai,
                'jmlh_rollcutting' => $stok_update1,
                'jmlh_kgcutting' => $stok_updatekg1,
                'estimasi_hasil' => $estimasi_hasil,
                'keterangan_cutting' => $keterangan_cutting,
                'dibuat' => $dibuat,
                'status' => 'OPEN'

              );
              $this->db->insert('tb_subcutting', $data4);

              $data2 =  array(
                'id' => NULL,
                'waktu' => date('d/m/Y-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('d/m/Y-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' => $keterangan_cutting

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