コード例 #1
0
 /**
  * Creates a new Penjualan model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Penjualan();
     if ($model->load(Yii::$app->request->post())) {
         //if(isset($_POST['Penjualan']['foto_nota']) && isset($_POST['Penjualan']['foto_ktp'])){
         try {
             $picture = UploadedFile::getInstance($model, 'foto_nota');
             $model->foto_nota = $_POST['Penjualan']['tgl'] . '- NOTA' . '.' . $picture->extension;
             $picture2 = UploadedFile::getInstance($model, 'foto_ktp');
             $model->foto_ktp = $_POST['Penjualan']['tgl'] . '- KTP' . '.' . $picture->extension;
             if ($model->save()) {
                 $picture->saveAs('uploads/nota/' . $model->tgl . '- NOTA' . '.' . $picture->extension);
                 $picture2->saveAs('uploads/ktp/' . $model->tgl . '- KTP' . '.' . $picture->extension);
                 Yii::$app->getSession()->setFlash('success', 'Data saved!');
                 //$id_pembeli = $model->id_pembeli;
                 $id_motor = $model->id_motor;
                 $id_penjualan = $model->id;
                 $user = Yii::$app->user->identity->username;
                 Yii::$app->db->createCommand('UPDATE motor SET status="laku" WHERE id=' . $id_motor)->execute();
                 Yii::$app->db->createCommand('UPDATE posisi_motor SET posisi="Lain-lain" WHERE id_motor=' . $id_motor)->execute();
                 Yii::$app->db->createCommand('insert into logs (date, logs) VALUES (now(),"Insert data penjualan dengan id penj : ' . $id_penjualan . ' // oleh user : '******'")')->execute();
                 return $this->redirect(['view', 'id' => $model->id]);
             } else {
                 Yii::$app->getSession()->setFlash('error', 'Data not saved!');
                 //var_dump($_POST);
                 return $this->render('create', ['model' => $model]);
             }
         } catch (Exception $e) {
             Yii::$app->getSession()->setFlash('error', "{$e->getMessage()}");
         }
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }