/**
  * Creates a new Purchase model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Purchase(['status' => Purchase::STATUS_DRAFT, 'id_branch' => Yii::$app->user->branch, 'purchase_date' => date('Y-m-d')]);
     $post = Yii::$app->request->post();
     if ($model->load($post)) {
         if (!empty($post['PurchaseDtl'])) {
             try {
                 $transaction = Yii::$app->db->beginTransaction();
                 $success = $model->save();
                 $success = $model->saveRelated('purchaseDtls', $post, $success);
                 if ($success) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id_purchase]);
                 } else {
                     $transaction->rollBack();
                 }
             } catch (\Exception $exc) {
                 $transaction->rollBack();
                 $model->addError('', $exc->getMessage());
             }
         } else {
             $model->validate();
             $model->addError('', 'Details cannot be blank');
         }
         $model->setIsNewRecord(true);
     }
     return $this->render('create', ['model' => $model, 'details' => $model->purchaseDtls]);
 }