Inheritance: extends BaseModel
Beispiel #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = InvoiceItem::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'invoice_id' => $this->invoice_id, 'employee_id' => $this->employee_id, 'hotel_rate' => $this->hotel_rate, 'employee_rate' => $this->employee_rate, 'amount' => $this->amount]);
     return $dataProvider;
 }
 public function createRecurringInvoice($recurInvoice)
 {
     $recurInvoice->load('account.timezone', 'invoice_items', 'client', 'user');
     if ($recurInvoice->client->deleted_at) {
         return false;
     }
     if (!$recurInvoice->user->confirmed) {
         return false;
     }
     if (!$recurInvoice->shouldSendToday()) {
         return false;
     }
     $invoice = Invoice::createNew($recurInvoice);
     $invoice->client_id = $recurInvoice->client_id;
     $invoice->recurring_invoice_id = $recurInvoice->id;
     $invoice->invoice_number = $recurInvoice->account->getNextInvoiceNumber(false, 'R');
     $invoice->amount = $recurInvoice->amount;
     $invoice->balance = $recurInvoice->amount;
     $invoice->invoice_date = date_create()->format('Y-m-d');
     $invoice->discount = $recurInvoice->discount;
     $invoice->po_number = $recurInvoice->po_number;
     $invoice->public_notes = Utils::processVariables($recurInvoice->public_notes);
     $invoice->terms = Utils::processVariables($recurInvoice->terms);
     $invoice->invoice_footer = Utils::processVariables($recurInvoice->invoice_footer);
     $invoice->tax_name = $recurInvoice->tax_name;
     $invoice->tax_rate = $recurInvoice->tax_rate;
     $invoice->invoice_design_id = $recurInvoice->invoice_design_id;
     $invoice->custom_value1 = $recurInvoice->custom_value1;
     $invoice->custom_value2 = $recurInvoice->custom_value2;
     $invoice->custom_taxes1 = $recurInvoice->custom_taxes1;
     $invoice->custom_taxes2 = $recurInvoice->custom_taxes2;
     $invoice->is_amount_discount = $recurInvoice->is_amount_discount;
     if ($invoice->client->payment_terms != 0) {
         $days = $invoice->client->payment_terms;
         if ($days == -1) {
             $days = 0;
         }
         $invoice->due_date = date_create()->modify($days . ' day')->format('Y-m-d');
     }
     $invoice->save();
     foreach ($recurInvoice->invoice_items as $recurItem) {
         $item = InvoiceItem::createNew($recurItem);
         $item->product_id = $recurItem->product_id;
         $item->qty = $recurItem->qty;
         $item->cost = $recurItem->cost;
         $item->notes = Utils::processVariables($recurItem->notes);
         $item->product_key = Utils::processVariables($recurItem->product_key);
         $item->tax_name = $recurItem->tax_name;
         $item->tax_rate = $recurItem->tax_rate;
         $invoice->invoice_items()->save($item);
     }
     foreach ($recurInvoice->invitations as $recurInvitation) {
         $invitation = Invitation::createNew($recurInvitation);
         $invitation->contact_id = $recurInvitation->contact_id;
         $invitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
         $invoice->invitations()->save($invitation);
     }
     $recurInvoice->last_sent_date = Carbon::now()->toDateTimeString();
     $recurInvoice->save();
     return $invoice;
 }
 private function export()
 {
     $output = fopen('php://output', 'w') or Utils::fatalError();
     header('Content-Type:application/csv');
     header('Content-Disposition:attachment;filename=export.csv');
     $clients = Client::scope()->get();
     Utils::exportData($output, $clients->toArray());
     $contacts = Contact::scope()->get();
     Utils::exportData($output, $contacts->toArray());
     $invoices = Invoice::scope()->get();
     Utils::exportData($output, $invoices->toArray());
     $invoiceItems = InvoiceItem::scope()->get();
     Utils::exportData($output, $invoiceItems->toArray());
     $payments = Payment::scope()->get();
     Utils::exportData($output, $payments->toArray());
     $credits = Credit::scope()->get();
     Utils::exportData($output, $credits->toArray());
     fclose($output);
     exit;
 }
 public function cloneInvoice($invoice, $quotePublicId = null)
 {
     $invoice->load('invitations', 'invoice_items');
     $account = $invoice->account;
     $clone = Invoice::createNew($invoice);
     $clone->balance = $invoice->amount;
     // if the invoice prefix is diff than quote prefix, use the same number for the invoice
     if (($account->invoice_number_prefix || $account->quote_number_prefix) && $account->invoice_number_prefix != $account->quote_number_prefix && $account->share_counter) {
         $invoiceNumber = $invoice->invoice_number;
         if ($account->quote_number_prefix && strpos($invoiceNumber, $account->quote_number_prefix) === 0) {
             $invoiceNumber = substr($invoiceNumber, strlen($account->quote_number_prefix));
         }
         $clone->invoice_number = $account->invoice_number_prefix . $invoiceNumber;
     } else {
         $clone->invoice_number = $account->getNextInvoiceNumber();
     }
     foreach (['client_id', 'discount', 'is_amount_discount', 'invoice_date', 'po_number', 'due_date', 'is_recurring', 'frequency_id', 'start_date', 'end_date', 'terms', 'invoice_footer', 'public_notes', 'invoice_design_id', 'tax_name', 'tax_rate', 'amount', 'is_quote', 'custom_value1', 'custom_value2', 'custom_taxes1', 'custom_taxes2', 'partial'] as $field) {
         $clone->{$field} = $invoice->{$field};
     }
     if ($quotePublicId) {
         $clone->is_quote = false;
         $clone->quote_id = $quotePublicId;
     }
     $clone->save();
     if ($quotePublicId) {
         $invoice->quote_invoice_id = $clone->public_id;
         $invoice->save();
     }
     foreach ($invoice->invoice_items as $item) {
         $cloneItem = InvoiceItem::createNew($invoice);
         foreach (['product_id', 'product_key', 'notes', 'cost', 'qty', 'tax_name', 'tax_rate'] as $field) {
             $cloneItem->{$field} = $item->{$field};
         }
         $clone->invoice_items()->save($cloneItem);
     }
     foreach ($invoice->invitations as $invitation) {
         $cloneInvitation = Invitation::createNew($invoice);
         $cloneInvitation->contact_id = $invitation->contact_id;
         $cloneInvitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
         $clone->invitations()->save($cloneInvitation);
     }
     return $clone;
 }
 public function createRecurringInvoice($recurInvoice)
 {
     $recurInvoice->load('account.timezone', 'invoice_items', 'client', 'user');
     if ($recurInvoice->client->deleted_at) {
         return false;
     }
     if (!$recurInvoice->user->confirmed) {
         return false;
     }
     if (!$recurInvoice->shouldSendToday()) {
         return false;
     }
     $invoice = Invoice::createNew($recurInvoice);
     $invoice->client_id = $recurInvoice->client_id;
     $invoice->recurring_invoice_id = $recurInvoice->id;
     $invoice->invoice_number = 'R' . $recurInvoice->account->getNextInvoiceNumber($recurInvoice);
     $invoice->amount = $recurInvoice->amount;
     $invoice->balance = $recurInvoice->amount;
     $invoice->invoice_date = date_create()->format('Y-m-d');
     $invoice->discount = $recurInvoice->discount;
     $invoice->po_number = $recurInvoice->po_number;
     $invoice->public_notes = Utils::processVariables($recurInvoice->public_notes);
     $invoice->terms = Utils::processVariables($recurInvoice->terms);
     $invoice->invoice_footer = Utils::processVariables($recurInvoice->invoice_footer);
     $invoice->tax_name = $recurInvoice->tax_name;
     $invoice->tax_rate = $recurInvoice->tax_rate;
     $invoice->invoice_design_id = $recurInvoice->invoice_design_id;
     $invoice->custom_value1 = $recurInvoice->custom_value1 ?: 0;
     $invoice->custom_value2 = $recurInvoice->custom_value2 ?: 0;
     $invoice->custom_taxes1 = $recurInvoice->custom_taxes1 ?: 0;
     $invoice->custom_taxes2 = $recurInvoice->custom_taxes2 ?: 0;
     $invoice->custom_text_value1 = $recurInvoice->custom_text_value1;
     $invoice->custom_text_value2 = $recurInvoice->custom_text_value2;
     $invoice->is_amount_discount = $recurInvoice->is_amount_discount;
     if ($invoice->client->payment_terms != 0) {
         $days = $invoice->client->payment_terms;
         if ($days == -1) {
             $days = 0;
         }
         $invoice->due_date = date_create()->modify($days . ' day')->format('Y-m-d');
     }
     $invoice->save();
     foreach ($recurInvoice->invoice_items as $recurItem) {
         $item = InvoiceItem::createNew($recurItem);
         $item->product_id = $recurItem->product_id;
         $item->qty = $recurItem->qty;
         $item->cost = $recurItem->cost;
         $item->notes = Utils::processVariables($recurItem->notes);
         $item->product_key = Utils::processVariables($recurItem->product_key);
         $item->tax_name = $recurItem->tax_name;
         $item->tax_rate = $recurItem->tax_rate;
         $invoice->invoice_items()->save($item);
     }
     foreach ($recurInvoice->invitations as $recurInvitation) {
         $invitation = Invitation::createNew($recurInvitation);
         $invitation->contact_id = $recurInvitation->contact_id;
         $invitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
         $invoice->invitations()->save($invitation);
     }
     $recurInvoice->last_sent_date = date('Y-m-d');
     $recurInvoice->save();
     if ($recurInvoice->auto_bill) {
         if ($this->paymentService->autoBillInvoice($invoice)) {
             // update the invoice reference to match its actual state
             // this is to ensure a 'payment received' email is sent
             $invoice->invoice_status_id = INVOICE_STATUS_PAID;
         }
     }
     return $invoice;
 }
 public function createNinjaInvoice($client, $clientAccount, $plan, $credit = 0)
 {
     $term = $plan['term'];
     $plan_cost = $plan['price'];
     $num_users = $plan['num_users'];
     $plan = $plan['plan'];
     if ($credit < 0) {
         $credit = 0;
     }
     $account = $this->getNinjaAccount();
     $lastInvoice = Invoice::withTrashed()->whereAccountId($account->id)->orderBy('public_id', 'DESC')->first();
     $publicId = $lastInvoice ? $lastInvoice->public_id + 1 : 1;
     $invoice = new Invoice();
     $invoice->account_id = $account->id;
     $invoice->user_id = $account->users()->first()->id;
     $invoice->public_id = $publicId;
     $invoice->client_id = $client->id;
     $invoice->invoice_number = $account->getNextInvoiceNumber($invoice);
     $invoice->invoice_date = $clientAccount->getRenewalDate();
     $invoice->amount = $invoice->balance = $plan_cost - $credit;
     $invoice->invoice_type_id = INVOICE_TYPE_STANDARD;
     $invoice->save();
     if ($credit) {
         $credit_item = InvoiceItem::createNew($invoice);
         $credit_item->qty = 1;
         $credit_item->cost = -$credit;
         $credit_item->notes = trans('texts.plan_credit_description');
         $credit_item->product_key = trans('texts.plan_credit_product');
         $invoice->invoice_items()->save($credit_item);
     }
     $item = InvoiceItem::createNew($invoice);
     $item->qty = 1;
     $item->cost = $plan_cost;
     $item->notes = trans("texts.{$plan}_plan_{$term}_description");
     if ($plan == PLAN_ENTERPRISE) {
         $min = Utils::getMinNumUsers($num_users);
         $item->notes .= "\n\n###" . trans('texts.min_to_max_users', ['min' => $min, 'max' => $num_users]);
     }
     // Don't change this without updating the regex in PaymentService->createPayment()
     $item->product_key = 'Plan - ' . ucfirst($plan) . ' (' . ucfirst($term) . ')';
     $invoice->invoice_items()->save($item);
     $invitation = new Invitation();
     $invitation->account_id = $account->id;
     $invitation->user_id = $account->users()->first()->id;
     $invitation->public_id = $publicId;
     $invitation->invoice_id = $invoice->id;
     $invitation->contact_id = $client->contacts()->first()->id;
     $invitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
     $invitation->save();
     return $invitation;
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     try {
         Log::error($id);
         $admin = \Auth::User();
         if (!$admin) {
             return $this->failResponse('admin not logged in');
         }
         $invoiceItemObj = InvoiceItem::find($id);
         if (count($invoiceItemObj) == 0) {
             return $this->failResponse('customer id not found');
         }
         $invoiceItemObj->delete();
         return $this->successResponse('customer deleted successfully');
     } catch (\Exception $e) {
         Log::error("error in customer controller");
         return $this->failResponse($e->getMessage());
     }
 }
 public function createNinjaInvoice($client, $clientAccount, $plan = PLAN_PRO, $term = PLAN_TERM_MONTHLY, $credit = 0, $pending_monthly = false)
 {
     if ($credit < 0) {
         $credit = 0;
     }
     $plan_cost = Account::$plan_prices[$plan][$term];
     $account = $this->getNinjaAccount();
     $lastInvoice = Invoice::withTrashed()->whereAccountId($account->id)->orderBy('public_id', 'DESC')->first();
     $publicId = $lastInvoice ? $lastInvoice->public_id + 1 : 1;
     $invoice = new Invoice();
     $invoice->account_id = $account->id;
     $invoice->user_id = $account->users()->first()->id;
     $invoice->public_id = $publicId;
     $invoice->client_id = $client->id;
     $invoice->invoice_number = $account->getNextInvoiceNumber($invoice);
     $invoice->invoice_date = $clientAccount->getRenewalDate();
     $invoice->amount = $invoice->balance = $plan_cost - $credit;
     $invoice->save();
     if ($credit) {
         $credit_item = InvoiceItem::createNew($invoice);
         $credit_item->qty = 1;
         $credit_item->cost = -$credit;
         $credit_item->notes = trans('texts.plan_credit_description');
         $credit_item->product_key = trans('texts.plan_credit_product');
         $invoice->invoice_items()->save($credit_item);
     }
     $item = InvoiceItem::createNew($invoice);
     $item->qty = 1;
     $item->cost = $plan_cost;
     $item->notes = trans("texts.{$plan}_plan_{$term}_description");
     // Don't change this without updating the regex in PaymentService->createPayment()
     $item->product_key = 'Plan - ' . ucfirst($plan) . ' (' . ucfirst($term) . ')';
     $invoice->invoice_items()->save($item);
     if ($pending_monthly) {
         $term_end = $term == PLAN_MONTHLY ? date_create('+1 month') : date_create('+1 year');
         $pending_monthly_item = InvoiceItem::createNew($invoice);
         $item->qty = 1;
         $pending_monthly_item->cost = 0;
         $pending_monthly_item->notes = trans("texts.plan_pending_monthly", array('date', Utils::dateToString($term_end)));
         // Don't change this without updating the text in PaymentService->createPayment()
         $pending_monthly_item->product_key = 'Pending Monthly';
         $invoice->invoice_items()->save($pending_monthly_item);
     }
     $invitation = new Invitation();
     $invitation->account_id = $account->id;
     $invitation->user_id = $account->users()->first()->id;
     $invitation->public_id = $publicId;
     $invitation->invoice_id = $invoice->id;
     $invitation->contact_id = $client->contacts()->first()->id;
     $invitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
     $invitation->save();
     return $invitation;
 }
 /**
  * Finds the InvoiceItem model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return InvoiceItem the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = InvoiceItem::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Beispiel #10
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getWolfInvoiceItems()
 {
     return $this->hasMany(InvoiceItem::className(), ['invoice_id' => 'id']);
 }
 public function fire()
 {
     $this->info(date('Y-m-d') . ' Running SendRecurringInvoices...');
     $today = new DateTime();
     $invoices = Invoice::with('account.timezone', 'invoice_items', 'client', 'user')->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND is_recurring IS TRUE AND start_date <= ? AND (end_date IS NULL OR end_date >= ?)', array($today, $today))->get();
     $this->info(count($invoices) . ' recurring invoice(s) found');
     foreach ($invoices as $recurInvoice) {
         if ($recurInvoice->client->deleted_at) {
             continue;
         }
         if (!$recurInvoice->user->confirmed) {
             continue;
         }
         $this->info('Processing Invoice ' . $recurInvoice->id . ' - Should send ' . ($recurInvoice->shouldSendToday() ? 'YES' : 'NO'));
         if (!$recurInvoice->shouldSendToday()) {
             continue;
         }
         $invoice = Invoice::createNew($recurInvoice);
         $invoice->client_id = $recurInvoice->client_id;
         $invoice->recurring_invoice_id = $recurInvoice->id;
         $invoice->invoice_number = $recurInvoice->account->getNextInvoiceNumber(false, 'R');
         $invoice->amount = $recurInvoice->amount;
         $invoice->balance = $recurInvoice->amount;
         $invoice->invoice_date = date_create()->format('Y-m-d');
         $invoice->discount = $recurInvoice->discount;
         $invoice->po_number = $recurInvoice->po_number;
         $invoice->public_notes = Utils::processVariables($recurInvoice->public_notes);
         $invoice->terms = Utils::processVariables($recurInvoice->terms);
         $invoice->invoice_footer = Utils::processVariables($recurInvoice->invoice_footer);
         $invoice->tax_name = $recurInvoice->tax_name;
         $invoice->tax_rate = $recurInvoice->tax_rate;
         $invoice->invoice_design_id = $recurInvoice->invoice_design_id;
         $invoice->custom_value1 = $recurInvoice->custom_value1;
         $invoice->custom_value2 = $recurInvoice->custom_value2;
         $invoice->custom_taxes1 = $recurInvoice->custom_taxes1;
         $invoice->custom_taxes2 = $recurInvoice->custom_taxes2;
         $invoice->is_amount_discount = $recurInvoice->is_amount_discount;
         if ($invoice->client->payment_terms != 0) {
             $days = $invoice->client->payment_terms;
             if ($days == -1) {
                 $days = 0;
             }
             $invoice->due_date = date_create()->modify($days . ' day')->format('Y-m-d');
         }
         $invoice->save();
         foreach ($recurInvoice->invoice_items as $recurItem) {
             $item = InvoiceItem::createNew($recurItem);
             $item->product_id = $recurItem->product_id;
             $item->qty = $recurItem->qty;
             $item->cost = $recurItem->cost;
             $item->notes = Utils::processVariables($recurItem->notes);
             $item->product_key = Utils::processVariables($recurItem->product_key);
             $item->tax_name = $recurItem->tax_name;
             $item->tax_rate = $recurItem->tax_rate;
             $invoice->invoice_items()->save($item);
         }
         foreach ($recurInvoice->invitations as $recurInvitation) {
             $invitation = Invitation::createNew($recurInvitation);
             $invitation->contact_id = $recurInvitation->contact_id;
             $invitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
             $invoice->invitations()->save($invitation);
         }
         $this->mailer->sendInvoice($invoice);
         $recurInvoice->last_sent_date = Carbon::now()->toDateTimeString();
         $recurInvoice->save();
     }
     $this->info('Done');
 }