/** * Performs the work of inserting or updating the row in the database. * * If the object is new, it inserts it; otherwise an update is performed. * All related objects are also updated in this method. * * @param PropelPDO $con * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations. * @throws PropelException * @see save() */ protected function doSave(PropelPDO $con) { $affectedRows = 0; // initialize var to track total num of affected rows if (!$this->alreadyInSave) { $this->alreadyInSave = true; // We call the save method on the following object(s) if they // were passed to this object by their corresponding set // method. This object relates to these object(s) by a // foreign key reference. if ($this->aBarang !== null) { if ($this->aBarang->isModified() || $this->aBarang->isNew()) { $affectedRows += $this->aBarang->save($con); } $this->setBarang($this->aBarang); } if ($this->isNew() || $this->isModified()) { // persist changes if ($this->isNew()) { $this->doInsert($con); } else { $this->doUpdate($con); } $affectedRows += 1; $this->resetModified(); } $this->alreadyInSave = false; } return $affectedRows; }
/** * Store a newly created resource in storage. * POST /barang * * @return Response */ public function store() { $validator = Validator::make(Input::all(), Barang::$rules); if ($validator->fails()) { return Redirect::to('barang')->withErrors($validator)->withInput(Input::all()); } else { if (Barang::where('kode_barang', '=', Input::get('kode_barang'))->exists()) { $barang = Barang::find(Input::get('kode_barang')); $barang->nm_barang = Input::get('nm_barang'); $barang->satuan = Input::get('satuan'); $barang->part_number = Input::get('part_number'); $barang->brand = Input::get('brand'); $barang->hrg_satuan = Input::get('hrg_satuan'); $barang->jml_barang = Input::get('jml_barang'); $barang->save(); Session::flash('message', 'Successfully updated barang!'); return Redirect::to('barang'); } else { $barang = new Barang(); $barang->kode_barang = Input::get('kode_barang'); $barang->nm_barang = Input::get('nm_barang'); $barang->part_number = Input::get('part_number'); $barang->satuan = Input::get('satuan'); $barang->brand = Input::get('brand'); $barang->hrg_satuan = Input::get('hrg_satuan'); $barang->jml_barang = Input::get('jml_barang'); $barang->save(); Session::flash('message', 'Successfully created barang!'); return Redirect::to('barang'); } } // }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionTambah() { $this->layout = '//layouts/box_kecil'; $model = new Barang(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Barang'])) { $model->attributes = $_POST['Barang']; if ($model->save()) { $this->redirect(array('view', 'id' => $model->id)); } } $this->render('tambah', array('model' => $model)); }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { if (Yii::app()->user->status !== 'owner' && Yii::app()->user->status !== 'admin') { $this->redirect(array("dashboard/index")); } $model = new Barang(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Barang'])) { $model->attributes = $_POST['Barang']; $model->keuntungan = $_POST['Barang']['harga_jual'] - $_POST['Barang']['harga_pokok'] - $_POST['Barang']['harga_jual'] / 100 * $_POST['Barang']['diskon']; $model->created_at = date('Y-m-d H:i:s'); $model->updated_at = date('Y-m-d H:i:s'); if ($model->save()) { $this->redirect(array('view', 'id' => $model->id_barang)); } } $this->render('create', array('model' => $model)); }
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())); } }
public function executeImportfile(sfWebRequest $request) { $allowed = array('xlsx', 'xls'); $file = $request->getFiles('file'); $fileSize = $file['size']; $fileType = $file['type']; $theFileName = $file['name']; $uploadDir = sfConfig::get("sf_upload_dir"); $import = $uploadDir . '/import'; if (!is_dir($import)) { mkdir($import, 0777); } $ext = pathinfo($theFileName, PATHINFO_EXTENSION); if (!in_array($ext, $allowed)) { $this->msg = 'file type not allowed. File Type Must '; $y = 0; foreach ($allowed as $x) { if ($y != count($allowed) - 1) { $this->msg .= $x . " or "; } else { $this->msg .= $x; } $y++; } $this->setTemplate('import'); } else { move_uploaded_file($file['tmp_name'], "{$import}/{$theFileName}"); $inputFileName = $uploadDir . '/import/' . $theFileName; try { $inputFileType = PHPExcel_IOFactory::identify($inputFileName); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFileName); $data = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true); for ($x = 3; $x <= count($data); $x++) { $c = new Criteria(); $c->add(KategoriPeer::NAMA_KATEGORI, $data[$x]['C'], Criteria::EQUAL); $kategori = $this->getIdColumn(KategoriPeer::doSelect($c)); $k = new Criteria(); $k->add(KemasanPeer::NAMA_KEMASAN, $data[$x]['E'], Criteria::EQUAL); $kemasan = $this->getIdColumn(KemasanPeer::doSelect($k)); $p = new Criteria(); $p->add(ProdusenPeer::NAMA_PRODUSEN, $data[$x]['F'], Criteria::EQUAL); $produsen = $this->getIdColumn(ProdusenPeer::doSelect($p)); $barang = BarangQuery::create()->findOneById($data[$x]['A']); if (isset($barang)) { $barang->setNamaBarang($data[$x]['B']); $barang->setIdKategori("" . $kategori); $barang->setStock($data[$x]['D']); $barang->setIdKemasan("" . $kemasan); $barang->setIdProdusen("" . $produsen); $barang->setDescription($data[$x]['G']); $barang->save(); } else { $barang = new Barang(); $barang->setNamaBarang($data[$x]['B']); $barang->setIdKategori("" . $kategori); $barang->setStock($data[$x]['D']); $barang->setIdKemasan("" . $kemasan); $barang->setIdProdusen("" . $produsen); $barang->setDescription($data[$x]['G']); $barang->save(); } } unlink($inputFileName); $this->getUser()->setFlash('notice', $notice . ' You can add another one below.'); $this->redirect('@barang'); } catch (Exception $e) { die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage()); } } }