Esempio n. 1
0
 public static function create($billType, $data)
 {
     $countryCode = $data[0];
     $denomination = $data[1];
     $enabled = $data[2];
     $security = $data[3];
     if ($countryCode !== '' && $countryCode !== 'BAR') {
         $bill = new Bill();
         $bill->bill_type = $billType;
         $bill->country_code = $countryCode;
         $bill->denomination = $denomination;
         $bill->enabled = $enabled;
         $bill->security = $security;
         $bill->save();
         return $bill;
     }
     return null;
 }
Esempio n. 2
0
 /**
  * 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]);
         }
     }
 }