Beispiel #1
0
 /**
  * Creates a new Invoice model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @param integer $id  The Current Invoice ID to create the new invoice from
  * @return mixed
  */
 public function actionCreate($id)
 {
     $currentInvoice = $this->findModel($id, false);
     $ticketsIds = ArrayHelper::getColumn($currentInvoice->getTickets()->closed(true)->all(), 'id');
     $billableLaborTotal = Labor::find()->ticket($ticketsIds)->hourly()->total(true);
     $purchaseTotal = PurchasedItem::find()->ticket($ticketsIds)->total();
     $travelFeeTotal = Travel::find()->invoice($id)->before()->totalFee();
     $model = new Invoice();
     $model->loadDefaultValues();
     $model->location_id = $currentInvoice->location_id;
     $model->total = $billableLaborTotal + $purchaseTotal + $travelFeeTotal;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         Ticket::updateAll(['invoice_id' => $model->id], ['id' => $ticketsIds]);
         Schedule::updateAll(['invoice_id' => $model->id], ['and', 'invoice_id=:invoice_id', 'start_time<:start_time'], [':invoice_id' => $model->location_id, ':start_time' => date(Invoice::BILLING_CUTOFF_DAY)]);
         return $this->redirect(['view', 'id' => $model->id]);
     } elseif (!Yii::$app->request->isPost) {
         $model->load(Yii::$app->request->get());
     }
     if (Yii::$app->request->isAjax) {
         return $this->renderAjax('_form', ['model' => $model]);
     }
     return $this->render('create', ['model' => $model]);
 }