Exemple #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Produk::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'type_id' => $this->type_id, 'harga' => $this->harga, 'stok' => $this->stok]);
     $query->andFilterWhere(['like', 'nama', $this->nama]);
     return $dataProvider;
 }
Exemple #2
0
 public function barang()
 {
     $listData = ArrayHelper::map(Produk::find()->all(), 'id', 'nama');
     return $listData;
 }
 public function actionInputTransaksi()
 {
     $modelTransaksi = new Transaksi();
     $modelsTransaksiItem = [new TransaksiItem()];
     if ($modelTransaksi->load(Yii::$app->request->post())) {
         $modelTransaksi->kode_transaksi = 'TRNS';
         $modelsTransaksiItem = Model::createMultiple(TransaksiItem::classname());
         Model::loadMultiple($modelsTransaksiItem, Yii::$app->request->post());
         // ajax validation
         if (Yii::$app->request->isAjax) {
             Yii::$app->response->format = Response::FORMAT_JSON;
             return ArrayHelper::merge(ActiveForm::validateMultiple($modelsTransaksiItem), ActiveForm::validate($modelTransaksi));
         }
         // validate all models
         $valid = $modelTransaksi->validate();
         $valid = Model::validateMultiple($modelsTransaksiItem) && $valid;
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 $modelTransaksi->tanggal = date('Y-m-d', strtotime($_POST['Transaksi']['tanggal']));
                 if ($flag = $modelTransaksi->save(false)) {
                     $i = 0;
                     foreach ($modelsTransaksiItem as $modelTransaksiItem) {
                         $modelTransaksiItem->transaksi_id = $modelTransaksi->id;
                         $modelTransaksiItem->produk_id = $_POST['TransaksiItem'][$i]['produk_id'];
                         $modelTransaksiItem->subtotal = Produk::findOne($_POST['TransaksiItem'][$i]['produk_id'])->harga * $modelTransaksiItem->jumlah;
                         $i++;
                         if (!($flag = $modelTransaksiItem->save(false))) {
                             $transaction->rollBack();
                             break;
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $modelTransaksi->id]);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     }
     return $this->render('_formD', ['modelTransaksi' => $modelTransaksi, 'modelsTransaksiItem' => empty($modelsTransaksiItem) ? [new TransaksiItem()] : $modelsTransaksiItem]);
 }
Exemple #4
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getIdProduk()
 {
     return $this->hasOne(Produk::className(), ['id' => 'id_produk']);
 }
Exemple #5
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getProduks()
 {
     return $this->hasMany(Produk::className(), ['type_id' => 'id']);
 }
Exemple #6
0
 /**
  * Finds the Produk model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Produk the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Produk::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }