From 944ef2ccd33b95bf965c5dfa7c142300a957b610 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sat, 12 Apr 2014 21:55:40 +0300 Subject: [PATCH] Working on pro plan --- README.md | 4 +- app/config/app.php | 2 - app/controllers/AccountController.php | 69 ++++++++++++++++---- app/controllers/UserController.php | 4 ++ app/lang/en/texts.php | 3 + app/models/Account.php | 26 ++++++++ app/models/User.php | 26 ++------ app/ninja/mailers/Mailer.php | 6 +- app/ninja/mailers/UserMailer.php | 2 +- app/ninja/repositories/InvoiceRepository.php | 2 +- app/routes.php | 5 ++ app/views/invoices/edit.blade.php | 44 ++++++++++--- composer.json | 13 ++-- 13 files changed, 147 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index a62690ee93a0..f13aadef225f 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,12 @@ Most online invoicing sites are expensive. They shouldn't be. The aim of this project is to provide a free, open-source alternative. Additionally, the hope is the codebase will serve as a sample site for Laravel as well as other JavaScript technologies. -The high level instructions for setting up the site are below but there's also a [setup guide](http://hillelcoren.com/invoice-ninja/laravel-ubuntu-virtualbox/). If you'd like to translate the site please use [caouecs/Laravel4-long](https://github.com/caouecs/Laravel4-lang) for the starter files. +The high level instructions for setting up the site are below but there's also a [setup guide](http://hillelcoren.com/invoice-ninja/laravel-ubuntu-virtualbox/). For updates follow [@invoiceninja](https://twitter.com/invoiceninja) or join the [Facebook Group](https://www.facebook.com/invoiceninja). For discussion of the code please use the [Google Group](https://groups.google.com/d/forum/invoiceninja). +If you'd like to translate the site please use [caouecs/Laravel4-long](https://github.com/caouecs/Laravel4-lang) for the starter files. + Site design by [kantorp-wegl.in](http://kantorp-wegl.in/) diff --git a/app/config/app.php b/app/config/app.php index 39506c499c95..9e09f8dfe957 100755 --- a/app/config/app.php +++ b/app/config/app.php @@ -114,7 +114,6 @@ return array( 'Illuminate\View\ViewServiceProvider', 'Illuminate\Workbench\WorkbenchServiceProvider', 'Illuminate\Remote\RemoteServiceProvider', - 'Basset\BassetServiceProvider', 'Bootstrapper\BootstrapperServiceProvider', 'Zizaco\Confide\ConfideServiceProvider', 'Former\FormerServiceProvider', @@ -187,7 +186,6 @@ return array( 'Validator' => 'Illuminate\Support\Facades\Validator', 'View' => 'Illuminate\Support\Facades\View', 'SSH' => 'Illuminate\Support\Facades\SSH', - 'Basset' => 'Basset\Facade', 'Alert' => 'Bootstrapper\Alert', 'Badge' => 'Bootstrapper\Badge', 'Breadcrumb' => 'Bootstrapper\Breadcrumb', diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php index 6a7c52d688a8..afb2ed9949e7 100755 --- a/app/controllers/AccountController.php +++ b/app/controllers/AccountController.php @@ -1,19 +1,22 @@ accountRepo = $accountRepo; - $this->mailer = $mailer; + $this->userMailer = $userMailer; + $this->contactMailer = $contactMailer; } public function getStarted() @@ -28,7 +31,6 @@ class AccountController extends \BaseController { if ($guestKey) { - //$user = User::where('password', '=', $guestKey)->firstOrFail(); $user = User::where('password', '=', $guestKey)->first(); if ($user && $user->registered) @@ -62,9 +64,48 @@ class AccountController extends \BaseController { $ninjaAccount = $this->getNinjaAccount(); $ninjaClient = $this->getNinjaClient($ninjaAccount); + $invoice = $this->createNinjaInvoice($ninjaAccount, $ninjaClient); - //$invoice = new Invoice(); - //$ninjaClient->invoices()->save($invoice); + $this->contactMailer->sendInvoice($invoice); + + return RESULT_SUCCESS; + } + + private function createNinjaInvoice($account, $client) + { + $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_date = date_create()->format('Y-m-d'); + $invoice->amount = PRO_PLAN_PRICE; + $invoice->balance = PRO_PLAN_PRICE; + $invoice->save(); + + $item = new InvoiceItem(); + $item->account_id = $account->id; + $item->user_id = $account->users()->first()->id; + $item->public_id = $publicId; + $item->qty = 1; + $item->cost = PRO_PLAN_PRICE; + $item->notes = trans('texts.pro_plan_description'); + $item->product_key = trans('texts.pro_plan_product'); + $invoice->invoice_items()->save($item); + + $invitation = new Invitation(); + $invitation->account_id = $account->id; + $invitation->user_id = $account->users()->first()->id; + $invitation->invoice_id = $invoice->id; + $invitation->contact_id = $client->contacts()->first()->id; + $invitation->invitation_key = str_random(RANDOM_KEY_LENGTH); + $invitation->save(); + + return $invoice; } private function getNinjaAccount() @@ -87,6 +128,8 @@ class AccountController extends \BaseController { $random = str_random(RANDOM_KEY_LENGTH); $user = new User(); + $user->registered = true; + $user->confirmed = true; $user->email = 'contact@invoiceninja.com'; $user->password = $random; $user->password_confirmation = $random; @@ -103,12 +146,8 @@ class AccountController extends \BaseController { { $client = Client::whereAccountId($ninjaAccount->id)->wherePublicId(Auth::user()->account_id)->first(); - if ($client) + if (!$client) { - return $client; - } - else - { $client = new Client; $client->public_id = Auth::user()->account_id; $client->user_id = $ninjaAccount->users()->first()->id; @@ -125,8 +164,10 @@ class AccountController extends \BaseController { { $contact->$field = Auth::user()->$field; } - $client->contacts()->save($contact); + $client->contacts()->save($contact); } + + return $client; } public function setTrashVisible($entityType, $visible) @@ -662,7 +703,7 @@ class AccountController extends \BaseController { $user->registered = true; $user->amend(); - $this->mailer->sendConfirmation($user); + $this->userMailer->sendConfirmation($user); $activities = Activity::scope()->get(); foreach ($activities as $activity) diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index 3af275b668b8..987e57c28d59 100755 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -88,6 +88,9 @@ class UserController extends BaseController { Event::fire('user.login'); Session::reflash(); + return Redirect::to('/dashboard'); + + /* $invoice = Invoice::scope()->orderBy('id', 'desc')->first(); if ($invoice) @@ -98,6 +101,7 @@ class UserController extends BaseController { { return Redirect::to('/dashboard'); } + */ } else { diff --git a/app/lang/en/texts.php b/app/lang/en/texts.php index acb0ab6796f1..829f3c7cb2ed 100644 --- a/app/lang/en/texts.php +++ b/app/lang/en/texts.php @@ -306,5 +306,8 @@ return array( 'erase_data' => 'This will permanently erase your data.', 'password' => 'Password', + 'pro_plan_product' => 'Pro Plan', + 'pro_plan_description' => 'One year enrollment in the Invoice Ninja Pro Plan', + 'pro_plan_succes' => 'We\'ve sent you an invoice. Once paid the kfeatures will be enabled.', ); diff --git a/app/models/Account.php b/app/models/Account.php index 1afdedb9dfb0..bd4927970c8b 100755 --- a/app/models/Account.php +++ b/app/models/Account.php @@ -213,4 +213,30 @@ class Account extends Eloquent return $data; } + public function isPro() + { + if ($this->account_key == NINJA_ACCOUNT_KEY) + { + return true; + } + + if (!Auth::check()) + { + return false; + } + + $datePaid = $this->pro_plan_paid; + + if (!$datePaid || $datePaid == '0000-00-00') + { + return false; + } + + $today = new DateTime('now'); + $datePaid = DateTime::createFromFormat('Y-m-d', $datePaid); + $interval = $today->diff($datePaid); + + return $interval->y == 0; + } + } \ No newline at end of file diff --git a/app/models/User.php b/app/models/User.php index caa76ec1ed00..275e4de96491 100755 --- a/app/models/User.php +++ b/app/models/User.php @@ -75,6 +75,11 @@ class User extends ConfideUser implements UserInterface, RemindableInterface return $this->email; } + public function isPro() + { + return $this->account->isPro(); + } + public function getDisplayName() { if ($this->getFullName()) @@ -104,27 +109,6 @@ class User extends ConfideUser implements UserInterface, RemindableInterface } } - public function isPro() - { - if (!Auth::check()) - { - return false; - } - - $datePaid = $this->account->pro_plan_paid; - - if (!$datePaid || $datePaid == '0000-00-00') - { - return false; - } - - $today = new DateTime('now'); - $datePaid = DateTime::createFromFormat('Y-m-d', $datePaid); - $interval = $today->diff($datePaid); - - return $interval->y == 0; - } - public function showGreyBackground() { return !$this->theme_id || in_array($this->theme_id, [2, 3, 5, 6, 7, 8, 10, 11, 12]); diff --git a/app/ninja/mailers/Mailer.php b/app/ninja/mailers/Mailer.php index cbc2e7e43998..162bd8d9fcfa 100755 --- a/app/ninja/mailers/Mailer.php +++ b/app/ninja/mailers/Mailer.php @@ -10,10 +10,8 @@ class Mailer { 'emails.'.$view.'_html', 'emails.'.$view.'_text' ]; - - //$view = 'emails.' . $view; - - Mail::queue($views, $data, function($message) use ($toEmail, $fromEmail, $fromName, $subject) + + Mail::send($views, $data, function($message) use ($toEmail, $fromEmail, $fromName, $subject) { $message->to($toEmail)->from($fromEmail, $fromName)->sender($fromEmail, $fromName) ->replyTo($fromEmail, $fromName)->returnPath($fromEmail)->subject($subject); diff --git a/app/ninja/mailers/UserMailer.php b/app/ninja/mailers/UserMailer.php index ed5f2f3dfd03..3c2c8c526f80 100755 --- a/app/ninja/mailers/UserMailer.php +++ b/app/ninja/mailers/UserMailer.php @@ -21,7 +21,7 @@ class UserMailer extends Mailer { $data = [ 'user' => $user ]; - + $this->sendTo($user->email, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data); } diff --git a/app/ninja/repositories/InvoiceRepository.php b/app/ninja/repositories/InvoiceRepository.php index d3ae3497748e..c02aa9ae94ac 100755 --- a/app/ninja/repositories/InvoiceRepository.php +++ b/app/ninja/repositories/InvoiceRepository.php @@ -136,7 +136,7 @@ class InvoiceRepository $invoice->po_number = trim($data['po_number']); $invoice->invoice_design_id = $data['invoice_design_id']; - if (isset($data['tax_rate']) && Utils::parseFloat($data['tax_rate']) > 0) + if (isset($data['tax_name']) && isset($data['tax_rate']) && Utils::parseFloat($data['tax_rate']) > 0) { $invoice->tax_rate = Utils::parseFloat($data['tax_rate']); $invoice->tax_name = trim($data['tax_name']); diff --git a/app/routes.php b/app/routes.php index 4ba23ef938aa..16a79dba9005 100755 --- a/app/routes.php +++ b/app/routes.php @@ -231,8 +231,13 @@ define('DEFAULT_DATETIME_FORMAT', 'F j, Y, g:i a'); define('DEFAULT_QUERY_CACHE', 120); // minutes define('DEFAULT_LOCALE', 'en'); +define('RESULT_SUCCESS', 'success'); +define('RESULT_FAILURE', 'failure'); + define('GATEWAY_PAYPAL_EXPRESS', 17); define('NINJA_ACCOUNT_KEY', 'zg4ylmzDkdkPOT8yoKQw9LTWaoZJx79h'); +define('PRO_PLAN_PRICE', 40); + if (Auth::check() && !Session::has(SESSION_TIMEZONE)) diff --git a/app/views/invoices/edit.blade.php b/app/views/invoices/edit.blade.php index 00c4d3a2a9b4..b2afdbca94c8 100755 --- a/app/views/invoices/edit.blade.php +++ b/app/views/invoices/edit.blade.php @@ -274,9 +274,8 @@ - @if (!Auth::user()->isPro()) - {{ trans('texts.pro_plan.remove_logo_link') }} - {{ trans('texts.pro_plan.remove_logo') }} + @if (!Auth::user()->account->isPro()) + {{ trans('texts.pro_plan.remove_logo', ['link'=>''.trans('texts.pro_plan.remove_logo_link').'']) }} @endif