/** * Creates a new Sales model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate($price) { $payment_methods = [1 => 'Cash', 2 => 'Bank']; $model = new Sales(['id_branch' => Yii::$app->user->branch, 'status' => Sales::STATUS_DRAFT, 'sales_date' => date('Y-m-d')]); $post = Yii::$app->request->post(); if ($model->load($post)) { if (!empty($post['SalesDtl'])) { try { $transaction = Yii::$app->db->beginTransaction(); $success = $model->save(); $success = $model->saveRelated('salesDtls', $post, $success); if ($success) { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id_sales]); } else { $transaction->rollBack(); } } catch (\Exception $exc) { $transaction->rollBack(); $model->addError('', $exc->getMessage()); } $model->setIsNewRecord(true); } else { $model->addError('', 'Detail can not be blank'); } } return $this->render('create', ['model' => $model, 'price' => $price, 'details' => $model->salesDtls, 'payment_methods' => $payment_methods]); }