/**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     try {
         $admin = \Auth::User();
         if (!$admin) {
             return $this->failResponse('admin not logged in');
         }
         $customerObj = new Customer();
         $data = $this->getRequestBody();
         $customerObj->first_name = $data['first_name'];
         $customerObj->last_name = $data['last_name'];
         $customerObj->business_name = $data['business_name'];
         $customerObj->mobile = $data['mobile'];
         $customerObj->email = $data['email'];
         $customerObj->address = $data['address'];
         $customerObj->save();
         if (!$customerObj->save()) {
             Log::error("error in saving");
             $errors = $customerObj->getErrors();
             return $this->failResponse('Validation failed. Cannot save data.' . $errors);
         } else {
             return $this->successResponse('updated sucessfully');
         }
     } catch (\Exception $e) {
         Log::error($customerObj->getErrors());
         return $this->failResponse($e->getMessage());
     }
 }
 /**
  * Creates a new Bill model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Bill();
     if (Yii::$app->session['carPartTotal'] <= 0.0) {
         return;
     }
     if (isset($_POST['mode'], $_POST['discount'])) {
         $result = [];
         if ($_POST['mode'] == 0 || $_POST['mode'] == 1) {
             $transaction = $model->getDb()->beginTransaction();
             try {
                 $discount = (double) $_POST['discount'];
                 if ($discount > Yii::$app->session['carPartTotal']) {
                     $discount = 0.0;
                 }
                 $modelPrice = new Price();
                 $modelPrice->price = (double) Yii::$app->session['carPartTotal'] - (double) $discount;
                 $modelPrice->calculate();
                 if (!$modelPrice->save(false)) {
                     throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($modelPrice->tableName())), 'msj' => print_r($modelPrice->getErrors(), true)]), 500);
                 }
                 $model->price_id = $modelPrice->id;
                 $model->discount = $discount;
                 if (isset($_POST['draft']) && !empty($_POST['draft'])) {
                     $model->draft = 1;
                 }
                 if (isset($_POST['Vehicle'])) {
                     $vehicle = new Vehicle();
                     $vehicle->attributes = $_POST['Vehicle'];
                     if (!empty($vehicle->brand) || !empty($vehicle->model) || !empty($vehicle->color) || !empty($vehicle->plaque)) {
                         if (!$vehicle->save(false)) {
                             throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($vehicle->tableName())), 'msj' => print_r($vehicle->getErrors(), true)]), 500);
                         }
                         $model->vehicle_id = $vehicle->id;
                     }
                 }
                 if (isset($_POST['Customer'])) {
                     $customer = new Customer();
                     $customer->attributes = $_POST['Customer'];
                     if (!empty($customer->name) || !empty($customer->serial)) {
                         if (!$customer->save(false)) {
                             throw new Exception(Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($customer->tableName())), 'msj' => print_r($customer->getErrors(), true)]), 500);
                         }
                         $model->customer_id = $customer->id;
                     }
                 }
                 if ($model->save()) {
                     $result['message'] = Yii::t('app', '{modelClass} saved', ['modelClass' => Yii::t('app', ucfirst($model->tableName()))]);
                 } else {
                     $result['message'] = Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($model->tableName())), 'msj' => print_r($model->errors, true)]);
                 }
                 $transaction->commit();
             } catch (\Exception $e) {
                 $transaction->rollBack();
                 $result['message'] = Yii::t('app', 'Error saving {model}: {msj}', ['model' => Yii::t('app', ucfirst($model->tableName())), 'msj' => $e]);
             }
         }
         echo json_encode($result);
     } else {
         if ($model->load(Yii::$app->request->post()) && $model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
             return $this->render('create', ['model' => $model]);
         }
     }
 }