Ejemplo n.º 1
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionTambah()
 {
     $model = new KategoriBarang();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['KategoriBarang'])) {
         $model->attributes = $_POST['KategoriBarang'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('tambah', array('model' => $model));
 }
Ejemplo n.º 2
0
 public function simpanCsvKePembelian()
 {
     $csvFileName = $this->csvFile->name;
     $namaFile = explode('-', $csvFileName);
     $refNo = $namaFile[0];
     $refTgl = "{$namaFile[4]}-{$namaFile[3]}-{$namaFile[2]}";
     $profilId = $this->profilId;
     $transaction = Yii::app()->db->beginTransaction();
     $pembelian = new Pembelian();
     $pembelian->profil_id = $profilId;
     $pembelian->referensi = $refNo;
     $pembelian->tanggal_referensi = $refTgl;
     try {
         if ($pembelian->save()) {
             $fp = fopen($this->csvFile->tempName, 'r');
             if ($fp) {
                 $line = fgetcsv($fp, 1000, ",");
                 //  print_r($line); exit;
                 do {
                     if ($line[0] == 'barcode') {
                         continue;
                     }
                     /* field csv
                      * "barcode","idBarang","namaBarang","jumBarang","hargaBeli","hargaJual","RRP","SatuanBarang","KategoriBarang","Supplier","kasir"
                      *  0         1          2            3           4           5           6     7              8                9
                      */
                     $barangAda = Barang::model()->find('barcode=:barcode', array(':barcode' => $line[0]));
                     $barangId = null;
                     if (is_null($barangAda)) {
                         /* Jika belum ada barcode nya, maka buat barang baru */
                         /* Jika belum ada kategori dan satuan, buat baru */
                         $kategoriAda = KategoriBarang::model()->find('nama=:nama', array(':nama' => $line[8]));
                         if (is_null($kategoriAda)) {
                             $kategoriBaru = new KategoriBarang();
                             $kategoriBaru->nama = $line[8];
                             if (!$kategoriBaru->save()) {
                                 throw new Exception("Gagal simpan kategori baru", 500);
                             }
                             $kategoriId = $kategoriBaru->id;
                         } else {
                             $kategoriId = $kategoriAda->id;
                         }
                         $satuanAda = SatuanBarang::model()->find('nama=:nama', array(':nama' => $line[7]));
                         if (is_null($satuanAda)) {
                             $satuanBaru = new SatuanBarang();
                             $satuanBaru->nama = $line[7];
                             if (!$satuanBaru->save()) {
                                 throw new Exception("Gagal simpan satuan baru", 500);
                             }
                             $satuanId = $satuanBaru->id;
                         } else {
                             $satuanId = $satuanAda->id;
                         }
                         $barangBaru = new Barang();
                         $barangBaru->barcode = $line[0];
                         $barangBaru->nama = $line[2];
                         $barangBaru->kategori_id = $kategoriId;
                         $barangBaru->satuan_id = $satuanId;
                         if (!$barangBaru->save()) {
                             throw new Exception("Gagal simpan barang baru", 500);
                         }
                         $barangId = $barangBaru->id;
                         /* Jadikan supplier default ke profil ini */
                         $supplierBarang = new SupplierBarang();
                         $supplierBarang->barang_id = $barangId;
                         $supplierBarang->supplier_id = $profilId;
                         $supplierBarang->default = SupplierBarang::SUPPLIER_DEFAULT;
                         if (!$supplierBarang->save()) {
                             throw new Exception("Gagal simpan Supplier Barang", 500);
                         }
                     } else {
                         $barangId = $barangAda->id;
                     }
                     $detail = new PembelianDetail();
                     $detail->pembelian_id = $pembelian->id;
                     $detail->barang_id = $barangId;
                     $detail->qty = $line[3];
                     $detail->harga_beli = $line[5];
                     $detail->harga_jual = $line[6];
                     if (!$detail->save()) {
                         throw new Exception('Gagal simpan detail pembelian', 500);
                     }
                 } while (($line = fgetcsv($fp, 2000)) != FALSE);
             }
             $transaction->commit();
             return array('sukses' => true, 'pembelianId' => $pembelian->id);
         } else {
             throw new Exception("Gagal Simpan Pembelian");
         }
     } catch (Exception $ex) {
         $transaction->rollback();
         return array('sukses' => false, 'error' => array('msg' => $ex->getMessage(), 'code' => $ex->getCode()));
     }
 }