1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 12:12:48 +01:00

bug fixes

This commit is contained in:
Hillel Coren 2014-01-14 11:52:56 +00:00
parent 6375f3ae7a
commit d1d3d75915
19 changed files with 289 additions and 78 deletions

View File

@ -53,7 +53,7 @@ class AccountController extends \BaseController {
Auth::login($user, true);
Event::fire('user.login');
return Redirect::to('invoices/create');
}

View File

@ -60,8 +60,8 @@ class CreditController extends \BaseController {
public function create($clientPublicId = 0, $invoicePublicId = 0)
{
$data = array(
'clientPublicId' => $clientPublicId,
'invoicePublicId' => $invoicePublicId,
'clientPublicId' => Input::old('client') ? Input::old('client') : $clientPublicId,
'invoicePublicId' => Input::old('invoice') ? Input::old('invoice') : $invoicePublicId,
'credit' => null,
'method' => 'POST',
'url' => 'credits',
@ -103,7 +103,7 @@ class CreditController extends \BaseController {
{
$rules = array(
'client' => 'required',
'amount' => 'required',
'amount' => 'required|positive',
);
$validator = Validator::make(Input::all(), $rules);

View File

@ -59,13 +59,13 @@ class PaymentController extends \BaseController
public function create($clientPublicId = 0, $invoicePublicId = 0)
{
$data = array(
'clientPublicId' => $clientPublicId,
'invoicePublicId' => $invoicePublicId,
'clientPublicId' => Input::old('client') ? Input::old('client') : $clientPublicId,
'invoicePublicId' => Input::old('invoice') ? Input::old('invoice') : $invoicePublicId,
'invoice' => null,
'invoices' => Invoice::scope()->with('client', 'invoice_status')->where('balance','>',0)->orderBy('invoice_number')->get(),
'payment' => null,
'method' => 'POST',
'url' => 'payments',
'url' => "payments",
'title' => '- New Payment',
'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'paymentTypes' => PaymentType::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
@ -107,7 +107,8 @@ class PaymentController extends \BaseController
{
$rules = array(
'client' => 'required',
'amount' => 'required'
'invoice' => 'required',
'amount' => 'required|positive'
);
$validator = Validator::make(Input::all(), $rules);
@ -136,7 +137,7 @@ class PaymentController extends \BaseController
if ($count > 0)
{
$message = Utils::pluralize('Successfully '.$action.'d ? payment', count($payments));
$message = Utils::pluralize('Successfully '.$action.'d ? payment', $count);
Session::flash('message', $message);
}

View File

@ -68,6 +68,8 @@ return array(
),
"unique" => "The :attribute has already been taken.",
"url" => "The :attribute format is invalid.",
"positive" => "The :attribute must be greater than zero.",
/*
|--------------------------------------------------------------------------

View File

@ -39,6 +39,12 @@ class Utils
*/
}
public static function parseFloat($value)
{
$value = preg_replace('/[^0-9\.\-]/', '', $value);
return floatval($value);
}
public static function formatPhoneNumber($phoneNumber)
{
$phoneNumber = preg_replace('/[^0-9a-zA-Z]/','',$phoneNumber);
@ -144,7 +150,7 @@ class Utils
$timezone = Session::get(SESSION_TIMEZONE);
$format = Session::get(SESSION_DATE_FORMAT);
return DateTime::createFromFormat($format, $date, new DateTimeZone($timezone));
return DateTime::createFromFormat($format, $date, new DateTimeZone($timezone))->format('Y-m-d');
}
public static function fromSqlDate($date)

View File

@ -4,18 +4,23 @@
define("ACTIVITY_TYPE_CREATE_CLIENT", 1);
define("ACTIVITY_TYPE_ARCHIVE_CLIENT", 2);
define("ACTIVITY_TYPE_DELETE_CLIENT", 3);
define("ACTIVITY_TYPE_CREATE_INVOICE", 4);
define("ACTIVITY_TYPE_UPDATE_INVOICE", 5);
define("ACTIVITY_TYPE_EMAIL_INVOICE", 6);
define("ACTIVITY_TYPE_VIEW_INVOICE", 7);
define("ACTIVITY_TYPE_ARCHIVE_INVOICE", 8);
define("ACTIVITY_TYPE_DELETE_INVOICE", 9);
define("ACTIVITY_TYPE_CREATE_PAYMENT", 10);
define("ACTIVITY_TYPE_ARCHIVE_PAYMENT", 11);
define("ACTIVITY_TYPE_DELETE_PAYMENT", 12);
define("ACTIVITY_TYPE_CREATE_CREDIT", 13);
define("ACTIVITY_TYPE_ARCHIVE_CREDIT", 14);
define("ACTIVITY_TYPE_DELETE_CREDIT", 15);
define("ACTIVITY_TYPE_UPDATE_PAYMENT", 11);
define("ACTIVITY_TYPE_ARCHIVE_PAYMENT", 12);
define("ACTIVITY_TYPE_DELETE_PAYMENT", 13);
define("ACTIVITY_TYPE_CREATE_CREDIT", 14);
define("ACTIVITY_TYPE_UPDATE_CREDIT", 15);
define("ACTIVITY_TYPE_ARCHIVE_CREDIT", 16);
define("ACTIVITY_TYPE_DELETE_CREDIT", 17);
class Activity extends Eloquent
@ -37,13 +42,18 @@ class Activity extends Eloquent
{
$activity = new Activity;
if ($entity) {
if ($entity)
{
$activity->user_id = $entity->user_id;
$activity->account_id = $entity->account_id;
} else if (Auth::check()) {
}
else if (Auth::check())
{
$activity->user_id = Auth::user()->id;
$activity->account_id = Auth::user()->account_id;
} else {
}
else
{
Utils::fatalError();
}
@ -208,6 +218,46 @@ class Activity extends Eloquent
}
}
public static function viewInvoice($invitation)
{
if (Session::get($invitation->invitation_key))
{
return;
}
Session::put($invitation->invitation_key, true);
$invoice = $invitation->invoice;
if (!$invoice->isViewed())
{
$invoice->invoice_status_id = INVOICE_STATUS_VIEWED;
$invoice->save();
}
$now = Carbon::now()->toDateTimeString();
$invitation->viewed_date = $now;
$invitation->save();
$client = $invoice->client;
$client->last_login = $now;
$client->save();
$activity = new Activity;
$activity->user_id = $invitation->user_id;
$activity->account_id = $invitation->user->account_id;
$activity->client_id = $invitation->invoice->client_id;
$activity->invitation_id = $invitation->id;
$activity->contact_id = $invitation->contact_id;
$activity->invoice_id = $invitation->invoice_id;
$activity->activity_type_id = ACTIVITY_TYPE_VIEW_INVOICE;
$activity->message = Utils::encodeActivity($invitation->contact, 'viewed', $invitation->invoice);
$activity->balance = $invitation->invoice->client->balance;
$activity->save();
}
public static function createPayment($payment)
{
$client = $payment->client;
@ -246,6 +296,82 @@ class Activity extends Eloquent
$activity->save();
}
public static function updatePayment($payment)
{
if ($payment->is_deleted && !$payment->getOriginal('is_deleted'))
{
$client = $payment->client;
$client->balance = $client->balance + $payment->amount;
$client->paid_to_date = $client->paid_to_date - $payment->amount;
$client->save();
$invoice = $payment->invoice;
$invoice->balance = $invoice->balance + $payment->amount;
$invoice->save();
$activity = Activity::getBlank();
$activity->client_id = $invoice->client_id;
$activity->invoice_id = $invoice->id;
$activity->activity_type_id = ACTIVITY_TYPE_DELETE_PAYMENT;
$activity->message = Utils::encodeActivity(Auth::user(), 'deleted payment');
$activity->balance = $client->balance;
$activity->adjustment = $payment->amount;
$activity->save();
}
else
{
/*
$diff = floatval($invoice->amount) - floatval($invoice->getOriginal('amount'));
if ($diff == 0)
{
return;
}
$client = $invoice->client;
$client->balance = $client->balance + $diff;
$client->save();
$activity = Activity::getBlank($invoice);
$activity->client_id = $invoice->client_id;
$activity->invoice_id = $invoice->id;
$activity->activity_type_id = ACTIVITY_TYPE_UPDATE_INVOICE;
$activity->message = Utils::encodeActivity(Auth::user(), 'updated', $invoice);
$activity->balance = $client->balance;
$activity->adjustment = $diff;
$activity->json_backup = $backupInvoice->hidePrivateFields()->toJSON();
$activity->save();
*/
}
}
public static function archivePayment($payment)
{
if ($payment->is_deleted)
{
return;
}
$client = $payment->client;
$client->balance = $client->balance + $payment->amount;
$client->paid_to_date = $client->paid_to_date - $payment->amount;
$client->save();
$invoice = $payment->invoice;
$invoice->balance = $invoice->balance + $payment->amount;
$invoice->save();
$activity = Activity::getBlank();
$activity->invoice_id = $invoice->id;
$activity->client_id = $client->id;
$activity->activity_type_id = ACTIVITY_TYPE_ARCHIVE_PAYMENT;
$activity->message = Utils::encodeActivity(Auth::user(), 'archived payment');
$activity->balance = $client->balance;
$activity->adjustment = $payment->amount;
$activity->save();
}
public static function createCredit($credit)
{
$client = $credit->client;
@ -273,52 +399,82 @@ class Activity extends Eloquent
$activity->save();
}
public static function archivePayment($payment)
public static function updateCredit($credit)
{
$activity = Activity::getBlank();
$activity->invoice_id = $invoice->id;
$activity->client_id = $invoice->client_id;
$activity->activity_type_id = ACTIVITY_TYPE_ARCHIVE_PAYMENT;
$activity->message = Utils::encodeActivity(Auth::user(), 'archived payment');
$activity->balance = $payment->client->balance;
$activity->save();
}
if ($credit->is_deleted && !$credit->getOriginal('is_deleted'))
{
$client = $credit->client;
$client->balance = $client->balance + $credit->amount;
$client->save();
public static function viewInvoice($invitation)
if ($credit->invoice)
{
$invoice = $credit->invoice;
$invoice->balance = $invoice->balance + $credit->amount;
$invoice->save();
}
$activity = Activity::getBlank();
$activity->client_id = $invoice->client_id;
$activity->invoice_id = $credit->invoice ? $credit->invoice->id : null;
$activity->activity_type_id = ACTIVITY_TYPE_DELETE_CREDIT;
$activity->message = Utils::encodeActivity(Auth::user(), 'deleted credit');
$activity->balance = $client->balance;
$activity->adjustment = $credit->amount;
$activity->save();
}
else
{
/*
$diff = floatval($invoice->amount) - floatval($invoice->getOriginal('amount'));
if ($diff == 0)
{
return;
}
$client = $invoice->client;
$client->balance = $client->balance + $diff;
$client->save();
$activity = Activity::getBlank($invoice);
$activity->client_id = $invoice->client_id;
$activity->invoice_id = $invoice->id;
$activity->activity_type_id = ACTIVITY_TYPE_UPDATE_INVOICE;
$activity->message = Utils::encodeActivity(Auth::user(), 'updated', $invoice);
$activity->balance = $client->balance;
$activity->adjustment = $diff;
$activity->json_backup = $backupInvoice->hidePrivateFields()->toJSON();
$activity->save();
*/
}
}
public static function archiveCredit($credit)
{
if (Session::get($invitation->invitation_key))
if ($credit->is_deleted)
{
return;
}
Session::put($invitation->invitation_key, true);
$invoice = $invitation->invoice;
if (!$invoice->isViewed())
{
$invoice->invoice_status_id = INVOICE_STATUS_VIEWED;
$invoice->save();
}
$now = Carbon::now()->toDateTimeString();
$invitation->viewed_date = $now;
$invitation->save();
$client = $invoice->client;
$client->last_login = $now;
$client = $credit->client;
$client->balance = $client->balance + $credit->amount;
$client->save();
$activity = new Activity;
$activity->user_id = $invitation->user_id;
$activity->account_id = $invitation->user->account_id;
$activity->client_id = $invitation->invoice->client_id;
$activity->invitation_id = $invitation->id;
$activity->contact_id = $invitation->contact_id;
$activity->invoice_id = $invitation->invoice_id;
$activity->activity_type_id = ACTIVITY_TYPE_VIEW_INVOICE;
$activity->message = Utils::encodeActivity($invitation->contact, 'viewed', $invitation->invoice);
$activity->balance = $invitation->invoice->client->balance;
if ($credit->invoice)
{
$invoice = $credit->invoice;
$invoice->balance = $invoice->balance + $credit->amount;
$invoice->save();
}
$activity = Activity::getBlank();
$activity->invoice_id = $credit->invoice ? $credit->invoice->id : null;
$activity->client_id = $client->id;
$activity->activity_type_id = ACTIVITY_TYPE_ARCHIVE_CREDIT;
$activity->message = Utils::encodeActivity(Auth::user(), 'archived credit');
$activity->balance = $client->balance;
$activity->adjustment = $credit->amount;
$activity->save();
}
}

View File

@ -26,4 +26,14 @@ class Credit extends EntityModel
Credit::created(function($credit)
{
Activity::createCredit($credit);
});
Credit::updating(function($credit)
{
Activity::updateCredit($credit);
});
Credit::deleting(function($credit)
{
Activity::archiveCredit($credit);
});

View File

@ -32,4 +32,14 @@ class Payment extends EntityModel
Payment::created(function($payment)
{
Activity::createPayment($payment);
});
Payment::updating(function($payment)
{
Activity::updatePayment($payment);
});
Payment::deleting(function($payment)
{
Activity::archivePayment($payment);
});

View File

@ -13,8 +13,8 @@ class User extends ConfideUser implements UserInterface, RemindableInterface
'username' => 'required|email|unique:users',
'email' => 'required|email|unique:users',
*/
'password' => 'required|between:6,20|confirmed',
'password_confirmation' => 'between:6,20',
'password' => 'required|between:6,32|confirmed',
'password_confirmation' => 'between:6,32',
);
/**

View File

@ -48,7 +48,7 @@ class CreditRepository
$credit->client_id = Client::getPrivateId($input['client']);
$credit->credit_date = Utils::toSqlDate($input['credit_date']);
$credit->invoice_id = isset($input['invoice']) && $input['invoice'] != "-1" ? Invoice::getPrivateId($input['invoice']) : null;
$credit->amount = floatval($input['amount']);
$credit->amount = Utils::parseFloat($input['amount']);
$credit->currency_id = $input['currency_id'] ? $input['currency_id'] : null;
$credit->save();

View File

@ -82,6 +82,12 @@ class InvoiceRepository
$invoice = (array) $input;
$invoiceId = isset($invoice['public_id']) && $invoice['public_id'] ? Invoice::getPrivateId($invoice['public_id']) : null;
$rules = ['invoice_number' => 'unique:invoices,invoice_number,' . $invoiceId . ',id,account_id,' . \Auth::user()->account_id];
if ($invoice['is_recurring'] && $invoice['start_date'] && $invoice['end_date'])
{
$rules['end_date'] = 'after:' . $invoice['start_date'];
}
$validator = \Validator::make($invoice, $rules);
if ($validator->fails())
@ -104,7 +110,7 @@ class InvoiceRepository
}
$invoice->client_id = $data['client_id'];
$invoice->discount = floatval($data['discount']);
$invoice->discount = Utils::parseFloat($data['discount']);
$invoice->invoice_number = trim($data['invoice_number']);
$invoice->invoice_date = Utils::toSqlDate($data['invoice_date']);
$invoice->due_date = Utils::toSqlDate($data['due_date']);
@ -118,9 +124,9 @@ class InvoiceRepository
$invoice->po_number = trim($data['po_number']);
$invoice->currency_id = $data['currency_id'];
if (isset($data['tax_rate']) && floatval($data['tax_rate']) > 0)
if (isset($data['tax_rate']) && Utils::parseFloat($data['tax_rate']) > 0)
{
$invoice->tax_rate = floatval($data['tax_rate']);
$invoice->tax_rate = Utils::parseFloat($data['tax_rate']);
$invoice->tax_name = trim($data['tax_name']);
}
else
@ -164,13 +170,13 @@ class InvoiceRepository
$invoiceItem->product_id = isset($product) ? $product->id : null;
$invoiceItem->product_key = trim($item->product_key);
$invoiceItem->notes = trim($item->notes);
$invoiceItem->cost = floatval($item->cost);
$invoiceItem->qty = floatval($item->qty);
$invoiceItem->cost = Utils::parseFloat($item->cost);
$invoiceItem->qty = Utils::parseFloat($item->qty);
$invoiceItem->tax_rate = 0;
if (isset($item->tax_rate) && floatval($item->tax_rate) > 0)
if (isset($item->tax_rate) && Utils::parseFloat($item->tax_rate) > 0)
{
$invoiceItem->tax_rate = floatval($item->tax_rate);
$invoiceItem->tax_rate = Utils::parseFloat($item->tax_rate);
$invoiceItem->tax_name = trim($item->tax_name);
}

View File

@ -51,7 +51,7 @@ class PaymentRepository
$payment->currency_id = $input['currency_id'] ? $input['currency_id'] : null;
$payment->payment_type_id = $input['payment_type_id'] ? $input['payment_type_id'] : null;
$payment->payment_date = Utils::toSqlDate($input['payment_date']);
$payment->amount = floatval($input['amount']);
$payment->amount = Utils::parseFloat($input['amount']);
$payment->save();
return $payment;

View File

@ -1,6 +1,7 @@
<?php namespace ninja\repositories;
use TaxRate;
use Utils;
class TaxRateRepository
{
@ -15,7 +16,7 @@ class TaxRateRepository
continue;
}
if (!floatval($record->rate) || !trim($record->name))
if (!Utils::parseFloat($record->rate) || !trim($record->name))
{
continue;
}
@ -29,7 +30,7 @@ class TaxRateRepository
$taxRate = TaxRate::createNew();
}
$taxRate->rate = floatval($record->rate);
$taxRate->rate = Utils::parseFloat($record->rate);
$taxRate->name = trim($record->name);
$taxRate->save();

View File

@ -22,7 +22,6 @@
//Log::error('test');
/*
Event::listen('illuminate.query', function($query, $bindings, $time, $name)
{
$data = compact('bindings', 'time', 'name');
@ -46,7 +45,6 @@ Event::listen('illuminate.query', function($query, $bindings, $time, $name)
Log::info($query, $data);
});
*/
/*
@ -215,4 +213,10 @@ define('DEFAULT_QUERY_CACHE', 120);
if (Auth::check() && !Session::has(SESSION_TIMEZONE)) {
Event::fire('user.refresh');
}
}
Validator::extend('positive', function($attribute, $value, $parameters)
{
return Utils::parseFloat($value) > 0;
});

View File

@ -79,7 +79,7 @@
</div>
<div class="col-md-3" id="col_2">
{{ Former::text('po_number')->label('PO&nbsp;number')->data_bind("value: po_number, valueUpdate: 'afterkeydown'") }}
{{ Former::text('po_number')->label('PO #')->data_bind("value: po_number, valueUpdate: 'afterkeydown'") }}
{{ Former::text('discount')->data_bind("value: discount, valueUpdate: 'afterkeydown'") }}
{{ Former::select('currency_id')->label('Currency')->addOption('', '')->fromQuery($currencies, 'name', 'id')->data_bind("value: currency_id") }}
@ -561,14 +561,21 @@
}
*/
var isRefreshing = false;
function refreshPDF() {
var invoice = createInvoiceModel();
var doc = generatePDF(invoice);
if (!doc) return;
var string = doc.output('datauristring');
if (isFirefox || isChrome) {
$('#theFrame').attr('src', string).show();
} else {
if (isRefreshing) {
return;
}
isRefreshing = true;
var pdfAsArray = convertDataURIToBinary(string);
PDFJS.getDocument(pdfAsArray).then(function getPdfHelloWorld(pdf) {
@ -583,6 +590,7 @@
page.render({canvasContext: context, viewport: viewport});
$('#theCanvas').show();
isRefreshing = false;
});
});
}

View File

@ -35,6 +35,7 @@
invoice.imageHeight = {{ $invoice->client->account->getLogoHeight() }};
@endif
var doc = generatePDF(invoice, true);
if (!doc) return;
var string = doc.output('datauristring');
if (isFirefox || isChrome) {

View File

@ -53,10 +53,8 @@
}
function deleteEntity(id) {
if (confirm("Are you sure?")) {
$('#id').val(id);
submitForm('delete');
}
$('#id').val(id);
submitForm('delete');
}
function archiveEntity(id) {

View File

@ -1,3 +1,7 @@
div {
word-break: break-word;
}
div.required > label {
font-weight: bold !important;
}
@ -35,6 +39,10 @@ table.table thead > tr > th {
border-bottom-width: 0px;
}
table td {
max-width: 250px;
}
/*
table.dataTable tr.odd { background-color: white; }
table.dataTable tr.even { background-color: #f9f9f9; }

View File

@ -127,7 +127,7 @@ function generatePDF(invoice, checkMath) {
}
var headerY = headerTop;
doc.text(headerLeft, headerY, 'Invoice #');
doc.text(headerLeft, headerY, 'Invoice Number');
doc.text(invoiceNumberX, headerY, invoiceNumber);
if (invoice.po_number) {