1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Working on importing data from more providers

This commit is contained in:
Hillel Coren 2015-12-07 23:41:48 +02:00
parent f600cbb6dd
commit e46d804cd7
14 changed files with 181 additions and 20 deletions

View File

@ -90,8 +90,8 @@ class PaymentApiController extends BaseAPIController
$invoice = Invoice::scope($data['invoice_id'])->with('client')->first();
if ($invoice) {
$data['invoice'] = $invoice->public_id;
$data['client'] = $invoice->client->public_id;
$data['invoice_id'] = $invoice->id;
$data['client_id'] = $invoice->client->id;
} else {
$error = trans('validation.not_in', ['attribute' => 'invoice_id']);
}

View File

@ -542,6 +542,8 @@ class PaymentController extends BaseController
public function store(CreatePaymentRequest $request)
{
$input = $request->input();
$input['invoice_id'] = Invoice::getPrivateId($input['invoice']);
$input['client_id'] = Client::getPrivateId($input['client']);
$payment = $this->paymentRepo->save($input);
if (Input::get('email_receipt')) {

View File

@ -75,11 +75,11 @@ class StartupCheck
'releases_link' => link_to(RELEASES_URL, 'Invoice Ninja', ['target' => '_blank']),
];
Session::put('news_feed_id', NEW_VERSION_AVAILABLE);
Session::put('news_feed_message', trans('texts.new_version_available', $params));
Session::flash('news_feed_message', trans('texts.new_version_available', $params));
} else {
Session::put('news_feed_id', $data->id);
if ($data->message && $data->id > Auth::user()->news_feed_id) {
Session::put('news_feed_message', $data->message);
Session::flash('news_feed_message', $data->message);
}
}
} else {

View File

@ -0,0 +1,21 @@
<?php namespace App\Ninja\Import\Harvest;
use League\Fractal\TransformerAbstract;
use App\Models\Country;
use League\Fractal\Resource\Item;
class ClientTransformer extends TransformerAbstract
{
public function transform($data, $maps)
{
if (isset($maps[ENTITY_CLIENT][$data->client_name])) {
return false;
}
return new Item($data, function ($data) use ($maps) {
return [
'name' => $data->client_name,
];
});
}
}

View File

@ -0,0 +1,27 @@
<?php namespace App\Ninja\Import\Harvest;
use League\Fractal\TransformerAbstract;
use App\Models\Country;
use League\Fractal\Resource\Item;
class ContactTransformer extends TransformerAbstract
{
public function transform($data, $maps)
{
if (isset($maps[ENTITY_CLIENT][$data->client])) {
$data->client_id = $maps[ENTITY_CLIENT][$data->client];
} else {
return false;
}
return new Item($data, function ($data) use ($maps) {
return [
'client_id' => $data->client_id,
'first_name' => $data->first_name,
'last_name' => $data->last_name,
'email' => $data->email,
'phone' => $data->office_phone ?: $data->mobile_phone,
];
});
}
}

View File

@ -0,0 +1,44 @@
<?php namespace App\Ninja\Import\Harvest;
use League\Fractal\TransformerAbstract;
use League\Fractal\Resource\Item;
use App\Models\Client;
use DateTime;
class InvoiceTransformer extends TransformerAbstract
{
public function transform($data, $maps)
{
if (isset($maps[ENTITY_INVOICE][$data->id])) {
return false;
}
if (isset($maps[ENTITY_CLIENT][$data->client])) {
$data->client_id = $maps[ENTITY_CLIENT][$data->client];
} else {
return false;
}
return new Item($data, function ($data) use ($maps) {
$invoiceDate = DateTime::createFromFormat('m/d/Y', $data->issue_date);
return [
'invoice_number' => $data->id,
'paid' => (float) $data->paid_amount,
'client_id' => (int) $data->client_id,
'po_number' => $data->po_number,
'invoice_date_sql' => $invoiceDate->format('Y-m-d'),
'tax_rate' => $data->tax ?: null,
'tax_name' => $data->tax ? trans('texts.tax') : null,
'invoice_items' => [
[
'notes' => $data->subject,
'cost' => (float) $data->invoice_amount,
'qty' => 1,
]
],
];
});
}
}

View File

@ -0,0 +1,23 @@
<?php namespace App\Ninja\Import\Harvest;
use League\Fractal\TransformerAbstract;
use League\Fractal\Resource\Item;
use DateTime;
class PaymentTransformer extends TransformerAbstract
{
public function transform($data, $maps)
{
return new Item($data, function ($data) use ($maps) {
$paymentDate = DateTime::createFromFormat('m/d/Y', $data->last_payment_date);
return [
'amount' => $data->paid_amount,
'payment_date_sql' => $paymentDate ? $paymentDate->format('Y-m-d') : null,
'client_id' => $data->client_id,
'invoice_id' => $data->invoice_id,
];
});
}
}

View File

@ -75,6 +75,10 @@ class ClientRepository extends BaseRepository
$client->fill($data);
$client->save();
if ( ! isset($data['contact']) && ! isset($data['contacts'])) {
return $client;
}
$first = true;
$contacts = isset($data['contact']) ? [$data['contact']] : $data['contacts'];
$contactIds = [];

View File

@ -0,0 +1,26 @@
<?php namespace App\Ninja\Repositories;
use App\Models\Client;
use App\Models\Contact;
class ContactRepository extends BaseRepository
{
public function save($data)
{
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
if (!$publicId || $publicId == '-1') {
$contact = Contact::createNew();
$contact->send_invoice = true;
$contact->client_id = $data['client_id'];
$contact->is_primary = Contact::scope()->where('client_id', '=', $contact->client_id)->count() == 0;
} else {
$contact = Contact::scope($publicId)->firstOrFail();
}
$contact->fill($data);
$contact->save();
return $contact;
}
}

View File

@ -253,9 +253,9 @@ class InvoiceRepository extends BaseRepository
$invoice->end_date = null;
}
$invoice->terms = trim($data['terms']) ? trim($data['terms']) : (!$publicId && $account->invoice_terms ? $account->invoice_terms : '');
$invoice->terms = (isset($data['terms']) && trim($data['terms'])) ? trim($data['terms']) : (!$publicId && $account->invoice_terms ? $account->invoice_terms : '');
$invoice->invoice_footer = (isset($data['invoice_footer']) && trim($data['invoice_footer'])) ? trim($data['invoice_footer']) : (!$publicId && $account->invoice_footer ? $account->invoice_footer : '');
$invoice->public_notes = trim($data['public_notes']);
$invoice->public_notes = isset($data['public_notes']) ? trim($data['public_notes']) : null;
// process date variables
$invoice->terms = Utils::processVariables($invoice->terms);

View File

@ -142,7 +142,7 @@ class PaymentRepository extends BaseRepository
}
if (!$publicId) {
$clientId = Client::getPrivateId(isset($input['client_id']) ? $input['client_id'] : $input['client']);
$clientId = $input['client_id'];
$amount = Utils::parseFloat($input['amount']);
if ($paymentTypeId == PAYMENT_TYPE_CREDIT) {
@ -159,11 +159,9 @@ class PaymentRepository extends BaseRepository
}
}
$payment->invoice_id = $input['invoice_id'];
$payment->client_id = $clientId;
$payment->amount = $amount;
$invoicePublicId = isset($input['invoice_id']) ? $input['invoice_id'] : $input['invoice'];
$payment->invoice_id = Invoice::getPrivateId($invoicePublicId);
}
$payment->save();

View File

@ -9,6 +9,7 @@ use parsecsv;
use Session;
use Validator;
use League\Fractal\Manager;
use App\Ninja\Repositories\ContactRepository;
use App\Ninja\Repositories\ClientRepository;
use App\Ninja\Repositories\InvoiceRepository;
use App\Ninja\Repositories\PaymentRepository;
@ -21,9 +22,11 @@ class ImportService
protected $transformer;
protected $invoiceRepo;
protected $clientRepo;
protected $contactRepo;
public static $entityTypes = [
ENTITY_CLIENT,
ENTITY_CONTACT,
ENTITY_INVOICE,
ENTITY_TASK,
];
@ -31,7 +34,7 @@ class ImportService
public static $sources = [
IMPORT_CSV,
IMPORT_FRESHBOOKS,
//IMPORT_HARVEST,
IMPORT_HARVEST,
//IMPORT_HIVEAGE,
//IMPORT_INVOICEABLE,
//IMPORT_NUTCACHE,
@ -40,7 +43,7 @@ class ImportService
//IMPORT_ZOHO,
];
public function __construct(Manager $manager, ClientRepository $clientRepo, InvoiceRepository $invoiceRepo, PaymentRepository $paymentRepo)
public function __construct(Manager $manager, ClientRepository $clientRepo, InvoiceRepository $invoiceRepo, PaymentRepository $paymentRepo, ContactRepository $contactRepo)
{
$this->fractal = $manager;
$this->fractal->setSerializer(new ArraySerializer());
@ -48,6 +51,7 @@ class ImportService
$this->clientRepo = $clientRepo;
$this->invoiceRepo = $invoiceRepo;
$this->paymentRepo = $paymentRepo;
$this->contactRepo = $contactRepo;
}
public function import($source, $files)
@ -97,16 +101,18 @@ class ImportService
$data['invoice_number'] = $account->getNextInvoiceNumber($invoice);
}
if ($this->validate($data, $entityType) !== true) {
if ($this->validate($source, $data, $entityType) !== true) {
return;
}
$entity = $this->{"{$entityType}Repo"}->save($data);
// if the invoice is paid we'll also create a payment record
if ($entityType === ENTITY_INVOICE && isset($row->paid) && $row->paid) {
$this->createPayment($source, $row, $maps, $data['client_id'], $entity->public_id);
if ($entityType === ENTITY_INVOICE && isset($data['paid']) && $data['paid']) {
$this->createPayment($source, $row, $maps, $data['client_id'], $entity->id);
}
return $entity;
}
private function checkData($entityType, $count)
@ -149,9 +155,10 @@ class ImportService
}
}
private function validate($data, $entityType)
private function validate($source, $data, $entityType)
{
if ($entityType === ENTITY_CLIENT) {
// Harvest's contacts are listed separately
if ($entityType === ENTITY_CLIENT && $source != IMPORT_HARVEST) {
$rules = [
'contacts' => 'valid_contacts',
];
@ -183,13 +190,13 @@ class ImportService
$clientMap = [];
$clients = $this->clientRepo->all();
foreach ($clients as $client) {
$clientMap[$client->name] = $client->public_id;
$clientMap[$client->name] = $client->id;
}
$invoiceMap = [];
$invoices = $this->invoiceRepo->all();
foreach ($invoices as $invoice) {
$invoiceMap[$invoice->invoice_number] = $invoice->public_id;
$invoiceMap[$invoice->invoice_number] = $invoice->id;
}
$countryMap = [];

View File

@ -4,6 +4,7 @@
@parent
<style type="text/css">
.contact-file,
.task-file {
display: none;
}
@ -22,7 +23,9 @@
</div>
<div class="panel-body">
{!! Former::open_for_files('/import') !!}
{!! Former::open_for_files('/import')
->addClass('warn-on-exit') !!}
{!! Former::select('source')
->onchange('setFileTypesVisible()')
->options(array_combine(\App\Services\ImportService::$sources, \App\Services\ImportService::$sources))

View File

@ -1,5 +1,11 @@
@extends('header')
@section('head')
@parent
@include('money_script')
@stop
@section('content')
{!! Former::open($url)->addClass('col-md-10 col-md-offset-1 warn-on-exit')->method($method)->rules(array(