public function getKategoriRak($id) { $kategori = Yii::app()->db->createCommand()->selectDistinct('kategori_id, kat.nama')->from(Barang::model()->tableName() . ' bar')->join(KategoriBarang::model()->tableName() . ' kat', 'bar.kategori_id = kat.id')->where('rak_id =:rakId')->order('kat.nama')->bindValue(':rakId', $id)->queryAll(); $arr = []; foreach ($kategori as $kat) { $arr[$kat['kategori_id']] = $kat['nama']; } return $arr; }
<div class="medium-5 large-4 columns"> <?php echo $formInputBaru->labelEx($barang, 'barcode'); ?> <?php echo $formInputBaru->textField($barang, 'barcode', array('size' => 45, 'maxlength' => 45, 'autocomplete' => 'off')); ?> <?php echo $formInputBaru->error($barang, 'barcode', array('class' => 'error')); ?> </div> <div class="medium-7 large-8 columns"> <?php echo $formInputBaru->labelEx($barang, 'nama'); ?> <?php echo $formInputBaru->textField($barang, 'nama', array('size' => 45, 'maxlength' => 45, 'autocomplete' => 'off')); ?> <?php echo $formInputBaru->error($barang, 'nama', array('class' => 'error')); ?> </div> </div> <div class="row"> <div class="medium-6 large-4 columns"> <?php echo $formInputBaru->labelEx($barang, 'kategori_id'); echo $formInputBaru->dropDownList($barang, 'kategori_id', CHtml::listData(KategoriBarang::model()->findAll(array('order' => 'nama')), 'id', 'nama'), array( 'empty' => 'Pilih satu..' )); echo $formInputBaru->error($barang, 'kategori_id', array('class' => 'error')); ?> </div> <div class="medium-6 large-4 columns"> <?php echo $formInputBaru->labelEx($barang, 'satuan_id'); echo $formInputBaru->dropDownList($barang, 'satuan_id', CHtml::listData(SatuanBarang::model()->findAll(array('order' => 'nama')), 'id', 'nama'), array( 'empty' => 'Pilih satu..' )); echo $formInputBaru->error($barang, 'satuan_id', array('class' => 'error')); ?> </div> <div class="medium-6 large-4 columns">
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return KategoriBarang the loaded model * @throws CHttpException */ public function loadModel($id) { $model = KategoriBarang::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
public function filterKategori() { return ['' => '[SEMUA]'] + CHtml::listData(KategoriBarang::model()->findAll(array('order' => 'nama')), 'id', 'nama'); }
public function actionImport() { if (isset($_POST['nomor'])) { $dbAhadPos2 = $_POST['database']; $nomor = $_POST['nomor']; $pembelianPos2 = Yii::app()->db->createCommand("\n SELECT tb.tglTransaksiBeli, s.namaSupplier\n FROM {$dbAhadPos2}.transaksibeli tb\n JOIN {$dbAhadPos2}.supplier s on tb.idSupplier = s.idSupplier\n WHERE idTransaksiBeli = :nomor")->bindValue(':nomor', $nomor)->queryRow(); // print_r($pembelianPos2); $profil = Profil::model()->find('nama=:nama', array('nama' => trim($pembelianPos2['namaSupplier']))); // print_r($supplier); if (!is_null($profil)) { $pembelian = new Pembelian(); $pembelian->profil_id = $profil->id; $pembelian->referensi = $nomor; $pembelian->tanggal_referensi = date_format(date_create_from_format('Y-m-d', $pembelianPos2['tglTransaksiBeli']), 'd-m-Y'); //$pembelianPos2['tglTransaksiBeli'].' 00:00:00'; if ($pembelian->save()) { $pembelianDetailPos2 = Yii::app()->db->createCommand("\n select db.barcode, hargaBeli, gb.hargaJual, RRP, jumBarangAsli, tglExpire, barang.id\n from {$dbAhadPos2}.detail_beli db\n join {$dbAhadPos2}.barang gb on db.barcode = gb.barcode\n left join barang on db.barcode = barang.barcode\n where idTransaksiBeli = :nomor\n ")->bindValue(':nomor', $nomor)->queryAll(); foreach ($pembelianDetailPos2 as $detailPos2) { // Jika barang.id belum ada, buat data barang baru $barangId = $detailPos2['id']; if (is_null($detailPos2['id'])) { $barangBaru = Yii::app()->db->createCommand("\n select b.barcode, b.namaBarang, k.namaKategoriBarang, s.namaSatuanBarang, r.namaRak\n from {$dbAhadPos2}.barang b\n left join {$dbAhadPos2}.kategori_barang k on b.idKategoriBarang=k.idKategoriBarang\n left join {$dbAhadPos2}.satuan_barang s on b.idSatuanBarang=s.idSatuanBarang\n left join {$dbAhadPos2}.rak r on b.idRak = r.idRak\n where barcode = :barcode\n ")->bindValue(':barcode', $detailPos2['barcode'])->queryRow(); $kategoriBarang = KategoriBarang::model()->find("nama='{$barangBaru['namaKategoriBarang']}'"); if (is_null($kategoriBarang)) { $kategoriId = 1; } else { $kategoriId = $kategoriBarang->id; } $satuanBarang = SatuanBarang::model()->find("nama='{$barangBaru['namaSatuanBarang']}'"); if (is_null($satuanBarang)) { $satuanId = 1; } else { $satuanId = $satuanBarang->id; } $rakBarang = RakBarang::model()->find("nama='{$barangBaru['namaRak']}'"); if (is_null($rakBarang)) { $rakId = 1; } else { $rakId = $rakBarang->id; } $barang = new Barang(); $barang->barcode = $barangBaru['barcode']; $barang->nama = $barangBaru['namaBarang']; $barang->kategori_id = $kategoriId; $barang->satuan_id = $satuanId; $barang->rak_id = $rakId; if ($barang->save()) { $barangId = $barang->id; $supplierBarang = new SupplierBarang(); $supplierBarang->barang_id = $barangId; $supplierBarang->supplier_id = $profil->id; $supplierBarang->save(); } } $detail = new PembelianDetail(); $detail->pembelian_id = $pembelian->id; $detail->barang_id = $barangId; $detail->qty = $detailPos2['jumBarangAsli']; $detail->harga_beli = $detailPos2['hargaBeli']; $detail->harga_jual = $detailPos2['hargaJual']; $detail->harga_jual_rekomendasi = $detailPos2['RRP']; $detail->tanggal_kadaluwarsa = $detailPos2['tglExpire']; $detail->save(); } $this->redirect('index'); } } } $modelCsvForm = new UploadCsvPembelianForm(); $supplierList = Profil::model()->profilTrx()->tipeSupplier()->orderByNama()->findAll(array('select' => 'id, nama')); if (isset($_POST['UploadCsvPembelianForm'])) { $modelCsvForm->attributes = $_POST['UploadCsvPembelianForm']; if (!empty($_FILES['UploadCsvPembelianForm']['tmp_name']['csvFile'])) { $modelCsvForm->csvFile = CUploadedFile::getInstance($modelCsvForm, 'csvFile'); $return = $modelCsvForm->simpanCsvKePembelian(); if ($return['sukses']) { $this->redirect($this->createUrl('ubah', array('id' => $return['pembelianId'], 'pilihb' => FALSE))); } } } $this->render('import', array('modelCsvForm' => $modelCsvForm, 'supplierList' => $supplierList)); }
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())); } }