@@ -225,7 +225,7 @@ class InvoiceController extends \BaseController {
$card = new CreditCard($data);
return [
- 'amount' => $invoice->total,
+ 'amount' => $invoice->amount,
'card' => $card,
'currency' => 'USD',
'returnUrl' => URL::to('complete'),
@@ -255,13 +255,13 @@ class InvoiceController extends \BaseController {
$payment = Payment::createNew();
$payment->invitation_id = $invitation->id;
$payment->invoice_id = $invoice->id;
- $payment->amount = $invoice->total;
+ $payment->amount = $invoice->amount;
$payment->client_id = $invoice->client_id;
//$payment->contact_id = 0; // TODO_FIX
$payment->transaction_reference = $ref;
$payment->save();
- $invoice->balance = floatval($invoice->total) - floatval($paymount->amount);
+ $invoice->balance = floatval($invoice->amount) - floatval($paymount->amount);
if ($response->isSuccessful())
{
@@ -538,11 +538,11 @@ class InvoiceController extends \BaseController {
{
$invoice->invoice_status_id = INVOICE_STATUS_SENT;
- $client->balance = $invoice->client->balance + $invoice->total;
+ $client->balance = $invoice->client->balance + $invoice->amount;
$client->save();
}
- $invoice->total = $total;
+ $invoice->amount = $total;
$invoice->save();
foreach ($contacts as $contact)
diff --git a/app/controllers/PaymentController.php b/app/controllers/PaymentController.php
index c63955c07594..b2c8420f9960 100755
--- a/app/controllers/PaymentController.php
+++ b/app/controllers/PaymentController.php
@@ -48,7 +48,7 @@ class PaymentController extends \BaseController
return $table->addColumn('invoice_number', function($model) { return $model->invoice_public_id ? link_to('invoices/' . $model->invoice_public_id . '/edit', $model->invoice_number) : ''; })
->addColumn('amount', function($model) { return '$' . $model->amount; })
- ->addColumn('payment_date', function($model) { return Utils::timestampToDateString($model->payment_date); })
+ ->addColumn('payment_date', function($model) { return Utils::dateToString($model->payment_date); })
->addColumn('dropdown', function($model)
{
return '
diff --git a/app/controllers/ReportController.php b/app/controllers/ReportController.php
index c9cbc3ce5154..7a9f54984bbb 100755
--- a/app/controllers/ReportController.php
+++ b/app/controllers/ReportController.php
@@ -2,45 +2,99 @@
class ReportController extends \BaseController {
- public function monthly()
+ public function report()
{
- $records = DB::table('invoices')
- ->select(DB::raw('sum(total) as total, month(invoice_date) as month'))
- ->where('invoices.deleted_at', '=', null)
- ->where('invoices.invoice_date', '>', 0)
- ->groupBy('month');
-
- $totals = $records->lists('total');
- $dates = $records->lists('month');
- $data = array_combine($dates, $totals);
-
- $startDate = date_create('2013-06-30');
- $endDate = date_create('2013-12-30');
- $endDate = $endDate->modify('+1 month');
- $interval = new DateInterval('P1M');
- $period = new DatePeriod($startDate, $interval, $endDate);
-
- $totals = [];
- $dates = [];
-
- foreach ($period as $d)
+ if (Input::all())
{
- $date = $d->format('Y-m-d');
- $month = $d->format('n');
-
- $dates[] = $date;
- $totals[] = isset($data[$month]) ? $data[$month] : 0;
+ $groupBy = Input::get('group_by');
+ $chartType = Input::get('chart_type');
+ $startDate = date_create(Input::get('start_date'));
+ $endDate = date_create(Input::get('end_date'));
}
-
- $width = (ceil( max($totals) / 100 ) * 100) / 10;
+ else
+ {
+ $groupBy = 'MONTH';
+ $chartType = 'Bar';
+ $startDate = date_create()->modify('-3 month');
+ $endDate = date_create();
+ }
+
+ $padding = $groupBy == 'DAYOFYEAR' ? 'day' : ($groupBy == 'WEEK' ? 'week' : 'month');
+ $endDate->modify('+1 '.$padding);
+ $datasets = [];
+ $labels = [];
+ $maxTotals = 0;
+
+ foreach ([ENTITY_INVOICE, ENTITY_PAYMENT, ENTITY_CREDIT] as $entityType)
+ {
+ $records = DB::table($entityType.'s')
+ ->select(DB::raw('sum(amount) as total, '.$groupBy.'('.$entityType.'_date) as '.$groupBy))
+ ->where($entityType.'s.deleted_at', '=', null)
+ ->where($entityType.'s.'.$entityType.'_date', '>=', $startDate->format('Y-m-d'))
+ ->where($entityType.'s.'.$entityType.'_date', '<=', $endDate->format('Y-m-d'))
+ ->groupBy($groupBy);
+
+ $totals = $records->lists('total');
+ $dates = $records->lists($groupBy);
+ $data = array_combine($dates, $totals);
+
+ $interval = new DateInterval('P1'.substr($groupBy, 0, 1));
+ $period = new DatePeriod($startDate, $interval, $endDate);
+
+ $totals = [];
+
+ foreach ($period as $d)
+ {
+ $dateFormat = $groupBy == 'DAYOFYEAR' ? 'z' : ($groupBy == 'WEEK' ? 'W' : 'n');
+ $date = $d->format($dateFormat);
+ $totals[] = isset($data[$date]) ? $data[$date] : 0;
+
+ if ($entityType == ENTITY_INVOICE)
+ {
+ $labelFormat = $groupBy == 'DAYOFYEAR' ? 'j' : ($groupBy == 'WEEK' ? 'W' : 'F');
+ $label = $d->format($labelFormat);
+ $labels[] = $label;
+ }
+ }
+
+ $max = max($totals);
+
+ if ($max > 0)
+ {
+ $datasets[] = [
+ 'totals' => $totals,
+ 'colors' => $entityType == ENTITY_INVOICE ? '78,205,196' : ($entityType == ENTITY_CREDIT ? '199,244,100' : '255,107,107')
+ ];
+ $maxTotals = max($max, $maxTotals);
+ }
+ }
+
+ $width = (ceil( $maxTotals / 100 ) * 100) / 10;
+ $width = max($width, 10);
+
+ $dateTypes = [
+ 'DAYOFYEAR' => 'Daily',
+ 'WEEK' => 'Weekly',
+ 'MONTH' => 'Monthly'
+ ];
+
+ $chartTypes = [
+ 'Bar' => 'Bar',
+ 'Line' => 'Line'
+ ];
$params = [
- 'dates' => $dates,
- 'totals' => $totals,
+ 'labels' => $labels,
+ 'datasets' => $datasets,
'scaleStepWidth' => $width,
+ 'dateTypes' => $dateTypes,
+ 'chartTypes' => $chartTypes,
+ 'chartType' => $chartType,
+ 'startDate' => $startDate->format('m/d/Y'),
+ 'endDate' => $endDate->modify('-1'.$padding)->format('m/d/Y'),
+ 'groupBy' => $groupBy
];
- return View::make('reports.monthly', $params);
+ return View::make('reports.report_builder', $params);
}
-
}
\ No newline at end of file
diff --git a/app/database/migrations/2013_11_05_180133_confide_setup_users_table.php b/app/database/migrations/2013_11_05_180133_confide_setup_users_table.php
index 4c6354bf0015..568d3336494b 100755
--- a/app/database/migrations/2013_11_05_180133_confide_setup_users_table.php
+++ b/app/database/migrations/2013_11_05_180133_confide_setup_users_table.php
@@ -270,7 +270,7 @@ class ConfideSetupUsersTable extends Migration {
$t->unsignedInteger('recurring_invoice_id')->index()->nullable();
- $t->decimal('total', 10, 2);
+ $t->decimal('amount', 10, 2);
$t->decimal('balance', 10, 2);
$t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
diff --git a/app/libraries/entity.php b/app/libraries/entity.php
old mode 100644
new mode 100755
diff --git a/app/libraries/utils.php b/app/libraries/utils.php
index 60d15448299a..a6a922a0023f 100755
--- a/app/libraries/utils.php
+++ b/app/libraries/utils.php
@@ -63,15 +63,24 @@ class Utils
return Utils::timestampToString($timestamp, $timezone, $format);
}
- public static function timestampToString($timestamp, $timzone, $format)
+ public static function dateToString($date) {
+ $dateTime = new DateTime($date);
+ $timestamp = $dateTime->getTimestamp();
+ $format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
+ return Utils::timestampToString($timestamp, false, $format);
+ }
+
+ public static function timestampToString($timestamp, $timezone = false, $format)
{
- $date = new Carbon($timestamp);
- $date->tz = $timzone;
+ $date = Carbon::createFromTimeStamp($timestamp);
+ if ($timezone) {
+ $date->tz = $timezone;
+ }
if ($date->year < 1900) {
return '';
}
return $date->format($format);
- }
+ }
public static function toSqlDate($date)
{
diff --git a/app/models/Invitation.php b/app/models/Invitation.php
old mode 100644
new mode 100755
diff --git a/app/routes.php b/app/routes.php
index ae77379a937a..e4e8a383c36b 100755
--- a/app/routes.php
+++ b/app/routes.php
@@ -82,7 +82,8 @@ Route::group(array('before' => 'auth'), function()
Route::get('api/credits/{client_id?}', array('as'=>'api.credits', 'uses'=>'CreditController@getDatatable'));
Route::post('credits/bulk', 'CreditController@bulk');
- Route::get('reports', 'ReportController@monthly');
+ Route::get('reports', 'ReportController@report');
+ Route::post('reports', 'ReportController@report');
});
diff --git a/app/views/reports/report_builder.blade.php b/app/views/reports/report_builder.blade.php
new file mode 100755
index 000000000000..2b7d88af6ce2
--- /dev/null
+++ b/app/views/reports/report_builder.blade.php
@@ -0,0 +1,71 @@
+@extends('header')
+
+@section('head')
+ @parent
+
+
+@stop
+
+@section('content')
+
+
+
+
+
+
+ {{ Former::open() }}
+ {{ Former::populateField('start_date', $startDate) }}
+ {{ Former::populateField('end_date', $endDate) }}
+ {{ Former::select('chart_type')->options($chartTypes, $chartType) }}
+ {{ Former::select('group_by')->options($dateTypes, $groupBy) }}
+ {{ Former::text('start_date') }}
+ {{ Former::text('end_date') }}
+ {{ Former::actions( Button::primary_submit('Generate') ) }}
+ {{ Former::close() }}
+
+
+
+
+
+
+
+
+
+
+@stop
+
+
+@section('onReady')
+
+ $('#start_date, #end_date').datepicker({
+ autoclose: true,
+ todayHighlight: true
+ });
+
+@stop
\ No newline at end of file