mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 01:54:29 -04:00
Merge pull request #7182 from turbo124/v5-develop
Fixes for zero percent line item taxes
This commit is contained in:
commit
49d4196482
@ -134,21 +134,22 @@ class InvoiceItemSum
|
||||
|
||||
$item_tax += $item_tax_rate1_total;
|
||||
|
||||
if($item_tax_rate1_total != 0)
|
||||
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
|
||||
// if($item_tax_rate1_total != 0)
|
||||
if (strlen($this->item->tax_name1) > 1)
|
||||
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
|
||||
|
||||
$item_tax_rate2_total = $this->calcAmountLineTax($this->item->tax_rate2, $amount);
|
||||
|
||||
$item_tax += $item_tax_rate2_total;
|
||||
|
||||
if($item_tax_rate2_total != 0)
|
||||
if (strlen($this->item->tax_name2) > 1)
|
||||
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total);
|
||||
|
||||
$item_tax_rate3_total = $this->calcAmountLineTax($this->item->tax_rate3, $amount);
|
||||
|
||||
$item_tax += $item_tax_rate3_total;
|
||||
|
||||
if($item_tax_rate3_total != 0)
|
||||
if (strlen($this->item->tax_name3) > 1)
|
||||
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
|
||||
|
||||
$this->setTotalTaxes($this->formatValue($item_tax, $this->currency->precision));
|
||||
|
@ -119,21 +119,22 @@ class InvoiceItemSumInclusive
|
||||
|
||||
$item_tax += $this->formatValue($item_tax_rate1_total, $this->currency->precision);
|
||||
|
||||
if($item_tax_rate1_total != 0)
|
||||
// if($item_tax_rate1_total != 0)
|
||||
if (strlen($this->item->tax_name1) > 1)
|
||||
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
|
||||
|
||||
$item_tax_rate2_total = $this->calcInclusiveLineTax($this->item->tax_rate2, $amount);
|
||||
|
||||
$item_tax += $this->formatValue($item_tax_rate2_total, $this->currency->precision);
|
||||
|
||||
if($item_tax_rate2_total != 0)
|
||||
if (strlen($this->item->tax_name2) > 1)
|
||||
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total);
|
||||
|
||||
$item_tax_rate3_total = $this->calcInclusiveLineTax($this->item->tax_rate3, $amount);
|
||||
|
||||
$item_tax += $this->formatValue($item_tax_rate3_total, $this->currency->precision);
|
||||
|
||||
if($item_tax_rate3_total != 0)
|
||||
if (strlen($this->item->tax_name3) > 1)
|
||||
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
|
||||
|
||||
|
||||
|
@ -35,17 +35,17 @@ class ExpenseMap
|
||||
{
|
||||
return [
|
||||
0 => 'texts.vendor',
|
||||
1 => 'texts.client',
|
||||
2 => 'texts.project',
|
||||
3 => 'texts.category',
|
||||
4 => 'texts.amount',
|
||||
5 => 'texts.currency',
|
||||
6 => 'texts.date',
|
||||
7 => 'texts.payment_type',
|
||||
8 => 'texts.payment_date',
|
||||
9 => 'texts.transaction_reference',
|
||||
10 => 'texts.public_notes',
|
||||
11 => 'texts.private_notes',
|
||||
1 => 'texts.client',
|
||||
2 => 'texts.project',
|
||||
3 => 'texts.category',
|
||||
4 => 'texts.amount',
|
||||
5 => 'texts.currency',
|
||||
6 => 'texts.date',
|
||||
7 => 'texts.payment_type',
|
||||
8 => 'texts.payment_date',
|
||||
9 => 'texts.transaction_reference',
|
||||
10 => 'texts.public_notes',
|
||||
11 => 'texts.private_notes',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -30,10 +30,10 @@ use League\Csv\Reader;
|
||||
use League\Csv\Statement;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
class BaseImport {
|
||||
|
||||
class BaseImport
|
||||
{
|
||||
use CleanLineItems;
|
||||
|
||||
|
||||
public Company $company;
|
||||
|
||||
public array $request;
|
||||
@ -42,54 +42,62 @@ class BaseImport {
|
||||
|
||||
public $request_name;
|
||||
|
||||
public $repository_name;
|
||||
public $repository_name;
|
||||
|
||||
public $factory_name;
|
||||
public $factory_name;
|
||||
|
||||
public $repository;
|
||||
public $repository;
|
||||
|
||||
public $transformer;
|
||||
|
||||
public $transformer;
|
||||
|
||||
public function __construct( array $request, Company $company ) {
|
||||
$this->company = $company;
|
||||
$this->request = $request;
|
||||
$this->hash = $request['hash'];
|
||||
$this->import_type = $request['import_type'];
|
||||
$this->skip_header = $request['skip_header'] ?? null;
|
||||
$this->column_map =
|
||||
! empty( $request['column_map'] ) ?
|
||||
array_combine( array_keys( $request['column_map'] ), array_column( $request['column_map'], 'mapping' ) ) : null;
|
||||
public function __construct(array $request, Company $company)
|
||||
{
|
||||
$this->company = $company;
|
||||
$this->request = $request;
|
||||
$this->hash = $request['hash'];
|
||||
$this->import_type = $request['import_type'];
|
||||
$this->skip_header = $request['skip_header'] ?? null;
|
||||
$this->column_map = !empty($request['column_map'])
|
||||
? array_combine(
|
||||
array_keys($request['column_map']),
|
||||
array_column($request['column_map'], 'mapping')
|
||||
)
|
||||
: null;
|
||||
|
||||
auth()->login( $this->company->owner(), true );
|
||||
|
||||
auth()->user()->setCompany($this->company);
|
||||
}
|
||||
auth()->login($this->company->owner(), true);
|
||||
|
||||
auth()
|
||||
->user()
|
||||
->setCompany($this->company);
|
||||
}
|
||||
|
||||
protected function getCsvData( $entity_type ) {
|
||||
|
||||
$base64_encoded_csv = Cache::pull( $this->hash . '-' . $entity_type );
|
||||
if ( empty( $base64_encoded_csv ) ) {
|
||||
protected function getCsvData($entity_type)
|
||||
{
|
||||
$base64_encoded_csv = Cache::pull($this->hash . '-' . $entity_type);
|
||||
if (empty($base64_encoded_csv)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$csv = base64_decode( $base64_encoded_csv );
|
||||
$csv = Reader::createFromString( $csv );
|
||||
$csv = base64_decode($base64_encoded_csv);
|
||||
$csv = Reader::createFromString($csv);
|
||||
|
||||
$stmt = new Statement();
|
||||
$data = iterator_to_array( $stmt->process( $csv ) );
|
||||
$data = iterator_to_array($stmt->process($csv));
|
||||
|
||||
if ( count( $data ) > 0 ) {
|
||||
if (count($data) > 0) {
|
||||
$headers = $data[0];
|
||||
|
||||
// Remove Invoice Ninja headers
|
||||
if ( count( $headers ) && count( $data ) > 4 && $this->import_type === 'csv' ) {
|
||||
if (
|
||||
count($headers) &&
|
||||
count($data) > 4 &&
|
||||
$this->import_type === 'csv'
|
||||
) {
|
||||
$first_cell = $headers[0];
|
||||
if ( strstr( $first_cell, config( 'ninja.app_name' ) ) ) {
|
||||
array_shift( $data ); // Invoice Ninja...
|
||||
array_shift( $data ); // <blank line>
|
||||
array_shift( $data ); // Enitty Type Header
|
||||
if (strstr($first_cell, config('ninja.app_name'))) {
|
||||
array_shift($data); // Invoice Ninja...
|
||||
array_shift($data); // <blank line>
|
||||
array_shift($data); // Enitty Type Header
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -97,23 +105,28 @@ class BaseImport {
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function mapCSVHeaderToKeys( $csvData ) {
|
||||
$keys = array_shift( $csvData );
|
||||
public function mapCSVHeaderToKeys($csvData)
|
||||
{
|
||||
$keys = array_shift($csvData);
|
||||
|
||||
return array_map( function ( $values ) use ( $keys ) {
|
||||
return array_combine( $keys, $values );
|
||||
}, $csvData );
|
||||
return array_map(function ($values) use ($keys) {
|
||||
return array_combine($keys, $values);
|
||||
}, $csvData);
|
||||
}
|
||||
|
||||
private function groupInvoices( $csvData, $key ) {
|
||||
private function groupInvoices($csvData, $key)
|
||||
{
|
||||
// Group by invoice.
|
||||
$grouped = [];
|
||||
|
||||
foreach ( $csvData as $line_item ) {
|
||||
if ( empty( $line_item[ $key ] ) ) {
|
||||
$this->error_array['invoice'][] = [ 'invoice' => $line_item, 'error' => 'No invoice number' ];
|
||||
foreach ($csvData as $line_item) {
|
||||
if (empty($line_item[$key])) {
|
||||
$this->error_array['invoice'][] = [
|
||||
'invoice' => $line_item,
|
||||
'error' => 'No invoice number',
|
||||
];
|
||||
} else {
|
||||
$grouped[ $line_item[ $key ] ][] = $line_item;
|
||||
$grouped[$line_item[$key]][] = $line_item;
|
||||
}
|
||||
}
|
||||
|
||||
@ -129,208 +142,241 @@ class BaseImport {
|
||||
{
|
||||
$count = 0;
|
||||
|
||||
foreach ( $data as $record ) {
|
||||
try {
|
||||
$entity = $this->transformer->transform( $record );
|
||||
foreach ($data as $key => $record) {
|
||||
try {
|
||||
|
||||
/** @var \App\Http\Requests\Request $request */
|
||||
$request = new $this->request_name();
|
||||
$entity = $this->transformer->transform($record);
|
||||
|
||||
// Pass entity data to request so it can be validated
|
||||
$request->query = $request->request = new ParameterBag( $entity );
|
||||
$validator = Validator::make( $entity, $request->rules() );
|
||||
/** @var \App\Http\Requests\Request $request */
|
||||
$request = new $this->request_name();
|
||||
|
||||
if ( $validator->fails() ) {
|
||||
$this->error_array[ $entity_type ][] =
|
||||
[ $entity_type => $record, 'error' => $validator->errors()->all() ];
|
||||
} else {
|
||||
$entity =
|
||||
$this->repository->save(
|
||||
array_diff_key( $entity, [ 'user_id' => false ] ),
|
||||
$this->factory_name::create( $this->company->id, $this->getUserIDForRecord( $entity ) ) );
|
||||
// Pass entity data to request so it can be validated
|
||||
$request->query = $request->request = new ParameterBag($entity);
|
||||
$validator = Validator::make($entity, $request->rules());
|
||||
|
||||
$entity->saveQuietly();
|
||||
$count++;
|
||||
if ($validator->fails()) {
|
||||
$this->error_array[$entity_type][] = [
|
||||
$entity_type => $record,
|
||||
'error' => $validator->errors()->all(),
|
||||
];
|
||||
} else {
|
||||
$entity = $this->repository->save(
|
||||
array_diff_key($entity, ['user_id' => false]),
|
||||
$this->factory_name::create(
|
||||
$this->company->id,
|
||||
$this->getUserIDForRecord($entity)
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
} catch ( \Exception $ex ) {
|
||||
if ( $ex instanceof ImportException ) {
|
||||
$message = $ex->getMessage();
|
||||
} else {
|
||||
report( $ex );
|
||||
$message = 'Unknown error';
|
||||
}
|
||||
$entity->saveQuietly();
|
||||
$count++;
|
||||
|
||||
$this->error_array[ $entity_type ][] = [ $entity_type => $record, 'error' => $message ];
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $ex) {
|
||||
|
||||
if ($ex instanceof ImportException) {
|
||||
$message = $ex->getMessage();
|
||||
} else {
|
||||
report($ex);
|
||||
$message = 'Unknown error';
|
||||
}
|
||||
|
||||
$this->error_array[$entity_type][] = [
|
||||
$entity_type => $record,
|
||||
'error' => $message,
|
||||
];
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
}
|
||||
|
||||
public function ingestInvoices( $invoices , $invoice_number_key) {
|
||||
public function ingestInvoices($invoices, $invoice_number_key)
|
||||
{
|
||||
$invoice_transformer = $this->transformer;
|
||||
|
||||
/** @var PaymentRepository $payment_repository */
|
||||
$payment_repository = app()->make( PaymentRepository::class );
|
||||
$payment_repository = app()->make(PaymentRepository::class);
|
||||
$payment_repository->import_mode = true;
|
||||
|
||||
/** @var ClientRepository $client_repository */
|
||||
$client_repository = app()->make( ClientRepository::class );
|
||||
$client_repository = app()->make(ClientRepository::class);
|
||||
$client_repository->import_mode = true;
|
||||
|
||||
$invoice_repository = new InvoiceRepository();
|
||||
$invoice_repository = new InvoiceRepository();
|
||||
$invoice_repository->import_mode = true;
|
||||
|
||||
$invoices = $this->groupInvoices($invoices, $invoice_number_key);
|
||||
|
||||
foreach ( $invoices as $raw_invoice ) {
|
||||
foreach ($invoices as $raw_invoice) {
|
||||
try {
|
||||
|
||||
$invoice_data = $invoice_transformer->transform( $raw_invoice );
|
||||
$invoice_data['line_items'] = $this->cleanItems( $invoice_data['line_items'] ?? [] );
|
||||
$invoice_data = $invoice_transformer->transform($raw_invoice);
|
||||
$invoice_data['line_items'] = $this->cleanItems(
|
||||
$invoice_data['line_items'] ?? []
|
||||
);
|
||||
|
||||
// If we don't have a client ID, but we do have client data, go ahead and create the client.
|
||||
if ( empty( $invoice_data['client_id'] ) && ! empty( $invoice_data['client'] ) ) {
|
||||
$client_data = $invoice_data['client'];
|
||||
$client_data['user_id'] = $this->getUserIDForRecord( $invoice_data );
|
||||
if (
|
||||
empty($invoice_data['client_id']) &&
|
||||
!empty($invoice_data['client'])
|
||||
) {
|
||||
$client_data = $invoice_data['client'];
|
||||
$client_data['user_id'] = $this->getUserIDForRecord(
|
||||
$invoice_data
|
||||
);
|
||||
|
||||
$client_repository->save(
|
||||
$client_data,
|
||||
$client = ClientFactory::create( $this->company->id, $client_data['user_id'] )
|
||||
$client = ClientFactory::create(
|
||||
$this->company->id,
|
||||
$client_data['user_id']
|
||||
)
|
||||
);
|
||||
$invoice_data['client_id'] = $client->id;
|
||||
unset( $invoice_data['client'] );
|
||||
unset($invoice_data['client']);
|
||||
}
|
||||
|
||||
$validator = Validator::make( $invoice_data, ( new StoreInvoiceRequest() )->rules() );
|
||||
if ( $validator->fails() ) {
|
||||
$this->error_array['invoice'][] =
|
||||
[ 'invoice' => $invoice_data, 'error' => $validator->errors()->all() ];
|
||||
$validator = Validator::make(
|
||||
$invoice_data,
|
||||
(new StoreInvoiceRequest())->rules()
|
||||
);
|
||||
if ($validator->fails()) {
|
||||
$this->error_array['invoice'][] = [
|
||||
'invoice' => $invoice_data,
|
||||
'error' => $validator->errors()->all(),
|
||||
];
|
||||
} else {
|
||||
$invoice = InvoiceFactory::create( $this->company->id, $this->getUserIDForRecord( $invoice_data ) );
|
||||
if ( ! empty( $invoice_data['status_id'] ) ) {
|
||||
$invoice = InvoiceFactory::create(
|
||||
$this->company->id,
|
||||
$this->getUserIDForRecord($invoice_data)
|
||||
);
|
||||
if (!empty($invoice_data['status_id'])) {
|
||||
$invoice->status_id = $invoice_data['status_id'];
|
||||
}
|
||||
$invoice_repository->save( $invoice_data, $invoice );
|
||||
$invoice_repository->save($invoice_data, $invoice);
|
||||
|
||||
// If we're doing a generic CSV import, only import payment data if we're not importing a payment CSV.
|
||||
// If we're doing a platform-specific import, trust the platform to only return payment info if there's not a separate payment CSV.
|
||||
if ( $this->import_type !== 'csv' || empty( $this->column_map['payment'] ) ) {
|
||||
if (
|
||||
$this->import_type !== 'csv' ||
|
||||
empty($this->column_map['payment'])
|
||||
) {
|
||||
// Check for payment columns
|
||||
if ( ! empty( $invoice_data['payments'] ) ) {
|
||||
foreach ( $invoice_data['payments'] as $payment_data ) {
|
||||
$payment_data['user_id'] = $invoice->user_id;
|
||||
$payment_data['client_id'] = $invoice->client_id;
|
||||
$payment_data['invoices'] = [
|
||||
if (!empty($invoice_data['payments'])) {
|
||||
foreach (
|
||||
$invoice_data['payments']
|
||||
as $payment_data
|
||||
) {
|
||||
$payment_data['user_id'] = $invoice->user_id;
|
||||
$payment_data['client_id'] =
|
||||
$invoice->client_id;
|
||||
$payment_data['invoices'] = [
|
||||
[
|
||||
'invoice_id' => $invoice->id,
|
||||
'amount' => $payment_data['amount'] ?? null,
|
||||
'amount' =>
|
||||
$payment_data['amount'] ?? null,
|
||||
],
|
||||
];
|
||||
|
||||
/* Make sure we don't apply any payments to invoices with a Zero Amount*/
|
||||
if($invoice->amount > 0)
|
||||
{
|
||||
if ($invoice->amount > 0) {
|
||||
$payment_repository->save(
|
||||
$payment_data,
|
||||
PaymentFactory::create( $this->company->id, $invoice->user_id, $invoice->client_id )
|
||||
PaymentFactory::create(
|
||||
$this->company->id,
|
||||
$invoice->user_id,
|
||||
$invoice->client_id
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->actionInvoiceStatus( $invoice, $invoice_data, $invoice_repository );
|
||||
$this->actionInvoiceStatus(
|
||||
$invoice,
|
||||
$invoice_data,
|
||||
$invoice_repository
|
||||
);
|
||||
}
|
||||
} catch ( \Exception $ex ) {
|
||||
if ( $ex instanceof ImportException ) {
|
||||
} catch (\Exception $ex) {
|
||||
if ($ex instanceof ImportException) {
|
||||
$message = $ex->getMessage();
|
||||
} else {
|
||||
report( $ex );
|
||||
report($ex);
|
||||
$message = 'Unknown error';
|
||||
}
|
||||
|
||||
$this->error_array['invoice'][] = [ 'invoice' => $raw_invoice, 'error' => $message ];
|
||||
$this->error_array['invoice'][] = [
|
||||
'invoice' => $raw_invoice,
|
||||
'error' => $message,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private function actionInvoiceStatus( $invoice, $invoice_data, $invoice_repository ) {
|
||||
if ( ! empty( $invoice_data['archived'] ) ) {
|
||||
$invoice_repository->archive( $invoice );
|
||||
private function actionInvoiceStatus(
|
||||
$invoice,
|
||||
$invoice_data,
|
||||
$invoice_repository
|
||||
) {
|
||||
if (!empty($invoice_data['archived'])) {
|
||||
$invoice_repository->archive($invoice);
|
||||
$invoice->fresh();
|
||||
}
|
||||
|
||||
if ( ! empty( $invoice_data['viewed'] ) ) {
|
||||
$invoice = $invoice->service()->markViewed()->save();
|
||||
if (!empty($invoice_data['viewed'])) {
|
||||
$invoice = $invoice
|
||||
->service()
|
||||
->markViewed()
|
||||
->save();
|
||||
}
|
||||
|
||||
if( $invoice->status_id === Invoice::STATUS_DRAFT ){
|
||||
|
||||
}
|
||||
elseif ( $invoice->status_id === Invoice::STATUS_SENT ) {
|
||||
$invoice = $invoice->service()->markSent()->save();
|
||||
}
|
||||
elseif ( $invoice->status_id <= Invoice::STATUS_SENT && $invoice->amount > 0 ) {
|
||||
if ( $invoice->balance <= 0 ) {
|
||||
if ($invoice->status_id === Invoice::STATUS_DRAFT) {
|
||||
} elseif ($invoice->status_id === Invoice::STATUS_SENT) {
|
||||
$invoice = $invoice
|
||||
->service()
|
||||
->markSent()
|
||||
->save();
|
||||
} elseif (
|
||||
$invoice->status_id <= Invoice::STATUS_SENT &&
|
||||
$invoice->amount > 0
|
||||
) {
|
||||
if ($invoice->balance <= 0) {
|
||||
$invoice->status_id = Invoice::STATUS_PAID;
|
||||
$invoice->save();
|
||||
}
|
||||
elseif ( $invoice->balance != $invoice->amount ) {
|
||||
} elseif ($invoice->balance != $invoice->amount) {
|
||||
$invoice->status_id = Invoice::STATUS_PARTIAL;
|
||||
$invoice->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $invoice;
|
||||
}
|
||||
|
||||
protected function getUserIDForRecord($record)
|
||||
{
|
||||
if (!empty($record['user_id'])) {
|
||||
return $this->findUser($record['user_id']);
|
||||
} else {
|
||||
return $this->company->owner()->id;
|
||||
}
|
||||
}
|
||||
|
||||
protected function findUser($user_hash)
|
||||
{
|
||||
$user = User::where('account_id', $this->company->account->id)
|
||||
->where(
|
||||
\DB::raw('CONCAT_WS(" ", first_name, last_name)'),
|
||||
'like',
|
||||
'%' . $user_hash . '%'
|
||||
)
|
||||
->first();
|
||||
|
||||
protected function getUserIDForRecord( $record ) {
|
||||
if ( ! empty( $record['user_id'] ) ) {
|
||||
return $this->findUser( $record['user_id'] );
|
||||
} else {
|
||||
return $this->company->owner()->id;
|
||||
}
|
||||
}
|
||||
|
||||
protected function findUser( $user_hash ) {
|
||||
$user = User::where( 'account_id', $this->company->account->id )
|
||||
->where( \DB::raw( 'CONCAT_WS(" ", first_name, last_name)' ), 'like', '%' . $user_hash . '%' )
|
||||
->first();
|
||||
|
||||
if ( $user ) {
|
||||
return $user->id;
|
||||
} else {
|
||||
return $this->company->owner()->id;
|
||||
}
|
||||
}
|
||||
|
||||
if ($user) {
|
||||
return $user->id;
|
||||
} else {
|
||||
return $this->company->owner()->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,51 +8,70 @@
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Import\Providers;
|
||||
|
||||
use App\Factory\ClientFactory;
|
||||
use App\Factory\ExpenseFactory;
|
||||
use App\Factory\InvoiceFactory;
|
||||
use App\Factory\PaymentFactory;
|
||||
use App\Factory\ProductFactory;
|
||||
use App\Factory\VendorFactory;
|
||||
use App\Http\Requests\Client\StoreClientRequest;
|
||||
use App\Http\Requests\Expense\StoreExpenseRequest;
|
||||
use App\Http\Requests\Invoice\StoreInvoiceRequest;
|
||||
use App\Http\Requests\Payment\StorePaymentRequest;
|
||||
use App\Http\Requests\Product\StoreProductRequest;
|
||||
use App\Http\Requests\Vendor\StoreVendorRequest;
|
||||
use App\Import\ImportException;
|
||||
use App\Import\Providers\BaseImport;
|
||||
use App\Import\Providers\ImportInterface;
|
||||
use App\Import\Transformer\Csv\ClientTransformer;
|
||||
use App\Import\Transformer\Csv\ExpenseTransformer;
|
||||
use App\Import\Transformer\Csv\InvoiceTransformer;
|
||||
use App\Import\Transformer\Csv\PaymentTransformer;
|
||||
use App\Import\Transformer\Csv\ProductTransformer;
|
||||
use App\Import\Transformer\Csv\VendorTransformer;
|
||||
use App\Repositories\ClientRepository;
|
||||
use App\Repositories\ExpenseRepository;
|
||||
use App\Repositories\InvoiceRepository;
|
||||
use App\Repositories\PaymentRepository;
|
||||
use App\Repositories\ProductRepository;
|
||||
use App\Repositories\VendorRepository;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
class Csv extends BaseImport implements ImportInterface
|
||||
{
|
||||
|
||||
public array $entity_count = [];
|
||||
|
||||
public function import(string $entity)
|
||||
{
|
||||
|
||||
if(in_array($entity, [ 'client', 'product', 'invoice', 'payment', 'vendor', 'expense' ]))
|
||||
public function import(string $entity)
|
||||
{
|
||||
if (
|
||||
in_array($entity, [
|
||||
'client',
|
||||
'product',
|
||||
'invoice',
|
||||
'payment',
|
||||
'vendor',
|
||||
'expense',
|
||||
])
|
||||
) {
|
||||
$this->{$entity}();
|
||||
|
||||
}
|
||||
|
||||
//collate any errors
|
||||
}
|
||||
|
||||
|
||||
private function client()
|
||||
{
|
||||
|
||||
$entity_type = 'client';
|
||||
|
||||
$data = $this->getCsvData($entity_type);
|
||||
|
||||
$data = $this->preTransform($data, $entity_type);
|
||||
|
||||
if(empty($data)){
|
||||
|
||||
if (empty($data)) {
|
||||
$this->entity_count['clients'] = 0;
|
||||
return;
|
||||
}
|
||||
@ -61,7 +80,7 @@ class Csv extends BaseImport implements ImportInterface
|
||||
$this->repository_name = ClientRepository::class;
|
||||
$this->factory_name = ClientFactory::class;
|
||||
|
||||
$this->repository = app()->make( $this->repository_name );
|
||||
$this->repository = app()->make($this->repository_name);
|
||||
$this->repository->import_mode = true;
|
||||
|
||||
$this->transformer = new ClientTransformer($this->company);
|
||||
@ -69,20 +88,17 @@ class Csv extends BaseImport implements ImportInterface
|
||||
$client_count = $this->ingest($data, $entity_type);
|
||||
|
||||
$this->entity_count['clients'] = $client_count;
|
||||
|
||||
}
|
||||
|
||||
private function product()
|
||||
{
|
||||
|
||||
$entity_type = 'product';
|
||||
|
||||
$data = $this->getCsvData($entity_type);
|
||||
|
||||
$data = $this->preTransform($data, $entity_type);
|
||||
|
||||
if(empty($data)){
|
||||
|
||||
if (empty($data)) {
|
||||
$this->entity_count['products'] = 0;
|
||||
return;
|
||||
}
|
||||
@ -91,7 +107,7 @@ class Csv extends BaseImport implements ImportInterface
|
||||
$this->repository_name = ProductRepository::class;
|
||||
$this->factory_name = ProductFactory::class;
|
||||
|
||||
$this->repository = app()->make( $this->repository_name );
|
||||
$this->repository = app()->make($this->repository_name);
|
||||
$this->repository->import_mode = true;
|
||||
|
||||
$this->transformer = new ProductTransformer($this->company);
|
||||
@ -99,20 +115,17 @@ class Csv extends BaseImport implements ImportInterface
|
||||
$product_count = $this->ingest($data, $entity_type);
|
||||
|
||||
$this->entity_count['products'] = $product_count;
|
||||
|
||||
}
|
||||
|
||||
private function invoice()
|
||||
{
|
||||
|
||||
$entity_type = 'invoice';
|
||||
|
||||
$data = $this->getCsvData($entity_type);
|
||||
|
||||
$data = $this->preTransform($data, $entity_type);
|
||||
|
||||
if(empty($data)){
|
||||
|
||||
if (empty($data)) {
|
||||
$this->entity_count['invoices'] = 0;
|
||||
return;
|
||||
}
|
||||
@ -121,7 +134,7 @@ class Csv extends BaseImport implements ImportInterface
|
||||
$this->repository_name = InvoiceRepository::class;
|
||||
$this->factory_name = InvoiceFactory::class;
|
||||
|
||||
$this->repository = app()->make( $this->repository_name );
|
||||
$this->repository = app()->make($this->repository_name);
|
||||
$this->repository->import_mode = true;
|
||||
|
||||
$this->transformer = new InvoiceTransformer($this->company);
|
||||
@ -129,39 +142,119 @@ class Csv extends BaseImport implements ImportInterface
|
||||
$invoice_count = $this->ingestInvoices($data, 'invoice.number');
|
||||
|
||||
$this->entity_count['invoices'] = $invoice_count;
|
||||
|
||||
}
|
||||
|
||||
private function payment()
|
||||
{
|
||||
$entity_type = 'payment';
|
||||
|
||||
public function preTransform(array $data, $entity_type)
|
||||
{
|
||||
$data = $this->getCsvData($entity_type);
|
||||
|
||||
$data = $this->preTransform($data, $entity_type);
|
||||
|
||||
if ( empty( $this->column_map[ $entity_type ] ) ) {
|
||||
if (empty($data)) {
|
||||
$this->entity_count['payments'] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->request_name = StorePaymentRequest::class;
|
||||
$this->repository_name = PaymentRepository::class;
|
||||
$this->factory_name = PaymentFactory::class;
|
||||
|
||||
$this->repository = app()->make($this->repository_name);
|
||||
$this->repository->import_mode = true;
|
||||
|
||||
$this->transformer = new PaymentTransformer($this->company);
|
||||
|
||||
$payment_count = $this->ingest($data, $entity_type);
|
||||
|
||||
$this->entity_count['payments'] = $payment_count;
|
||||
}
|
||||
|
||||
private function vendor()
|
||||
{
|
||||
$entity_type = 'vendor';
|
||||
|
||||
$data = $this->getCsvData($entity_type);
|
||||
|
||||
$data = $this->preTransform($data, $entity_type);
|
||||
|
||||
if (empty($data)) {
|
||||
$this->entity_count['vendors'] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->request_name = StoreVendorRequest::class;
|
||||
$this->repository_name = VendorRepository::class;
|
||||
$this->factory_name = VendorFactory::class;
|
||||
|
||||
$this->repository = app()->make($this->repository_name);
|
||||
$this->repository->import_mode = true;
|
||||
|
||||
$this->transformer = new VendorTransformer($this->company);
|
||||
|
||||
$vendor_count = $this->ingest($data, $entity_type);
|
||||
|
||||
$this->entity_count['vendors'] = $vendor_count;
|
||||
}
|
||||
|
||||
private function expense()
|
||||
{
|
||||
$entity_type = 'expense';
|
||||
|
||||
$data = $this->getCsvData($entity_type);
|
||||
|
||||
$data = $this->preTransform($data, $entity_type);
|
||||
|
||||
if (empty($data)) {
|
||||
$this->entity_count['expenses'] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->request_name = StoreExpenseRequest::class;
|
||||
$this->repository_name = ExpenseRepository::class;
|
||||
$this->factory_name = ExpenseFactory::class;
|
||||
|
||||
$this->repository = app()->make($this->repository_name);
|
||||
$this->repository->import_mode = true;
|
||||
|
||||
$this->transformer = new ExpenseTransformer($this->company);
|
||||
|
||||
$expense_count = $this->ingest($data, $entity_type);
|
||||
|
||||
$this->entity_count['expenses'] = $expense_count;
|
||||
}
|
||||
|
||||
private function quote()
|
||||
{
|
||||
}
|
||||
|
||||
private function task()
|
||||
{
|
||||
}
|
||||
|
||||
public function preTransform(array $data, $entity_type)
|
||||
{
|
||||
if (empty($this->column_map[$entity_type])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( $this->skip_header ) {
|
||||
array_shift( $data );
|
||||
if ($this->skip_header) {
|
||||
array_shift($data);
|
||||
}
|
||||
|
||||
//sort the array by key
|
||||
$keys = $this->column_map[ $entity_type ];
|
||||
ksort( $keys );
|
||||
|
||||
$data = array_map( function ( $row ) use ( $keys ) {
|
||||
return array_combine( $keys, array_intersect_key( $row, $keys ) );
|
||||
}, $data );
|
||||
$keys = $this->column_map[$entity_type];
|
||||
ksort($keys);
|
||||
|
||||
$data = array_map(function ($row) use ($keys) {
|
||||
return array_combine($keys, array_intersect_key($row, $keys));
|
||||
}, $data);
|
||||
|
||||
return $data;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function transform(array $data)
|
||||
{
|
||||
|
||||
public function transform(array $data)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -12,5 +12,4 @@ namespace App\Import\Providers;
|
||||
|
||||
class Freshbooks extends BaseImport
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
namespace App\Import\Transformer;
|
||||
|
||||
use App\Factory\ProjectFactory;
|
||||
use App\Models\ClientContact;
|
||||
use App\Models\Country;
|
||||
use App\Models\PaymentType;
|
||||
@ -25,7 +26,6 @@ use Illuminate\Support\Facades\Cache;
|
||||
*/
|
||||
class BaseTransformer
|
||||
{
|
||||
|
||||
protected $company;
|
||||
|
||||
public function __construct($company)
|
||||
@ -35,59 +35,64 @@ class BaseTransformer
|
||||
|
||||
public function getString($data, $field)
|
||||
{
|
||||
return (isset($data[$field]) && $data[$field]) ? $data[$field] : '';
|
||||
return isset($data[$field]) && $data[$field] ? $data[$field] : '';
|
||||
}
|
||||
|
||||
public function getCurrencyByCode( $data, $key = 'client.currency_id' )
|
||||
public function getCurrencyByCode($data, $key = 'client.currency_id')
|
||||
{
|
||||
|
||||
$code = array_key_exists( $key, $data ) ? $data[ $key ] : false;
|
||||
$code = array_key_exists($key, $data) ? $data[$key] : false;
|
||||
|
||||
$currencies = Cache::get('currencies');
|
||||
|
||||
$currency = $currencies->filter(function ($item) use($code) {
|
||||
return $item->code == $code;
|
||||
})->first();
|
||||
|
||||
return $currency ? $currency->id : $this->company->settings->currency_id;
|
||||
$currency = $currencies
|
||||
->filter(function ($item) use ($code) {
|
||||
return $item->code == $code;
|
||||
})
|
||||
->first();
|
||||
|
||||
return $currency
|
||||
? $currency->id
|
||||
: $this->company->settings->currency_id;
|
||||
}
|
||||
|
||||
public function getClient($client_name, $client_email)
|
||||
public function getClient($client_name, $client_email)
|
||||
{
|
||||
|
||||
// nlog("searching for {$client_name} with email {$client_email}");
|
||||
|
||||
$client_id_search = $this->company->clients()->where( 'id_number', $client_name );
|
||||
|
||||
if ( $client_id_search->count() >= 1 ) {
|
||||
$client_id_search = $this->company
|
||||
->clients()
|
||||
->where('id_number', $client_name);
|
||||
|
||||
if ($client_id_search->count() >= 1) {
|
||||
// nlog("found via id number => {$client_id_search->first()->id}");
|
||||
return $client_id_search->first()->id;
|
||||
}
|
||||
return $client_id_search->first()->id;
|
||||
}
|
||||
|
||||
$client_name_search = $this->company->clients()->where( 'name', $client_name );
|
||||
$client_name_search = $this->company
|
||||
->clients()
|
||||
->where('name', $client_name);
|
||||
|
||||
if ( $client_name_search->count() >= 1 ) {
|
||||
if ($client_name_search->count() >= 1) {
|
||||
// nlog("found via name {$client_name_search->first()->id}");
|
||||
return $client_name_search->first()->id;
|
||||
}
|
||||
|
||||
if ( ! empty( $client_email ) ) {
|
||||
$contacts = ClientContact::where( 'company_id', $this->company->id )
|
||||
->where( 'email', $client_email );
|
||||
if (!empty($client_email)) {
|
||||
$contacts = ClientContact::where(
|
||||
'company_id',
|
||||
$this->company->id
|
||||
)->where('email', $client_email);
|
||||
|
||||
if ( $contacts->count() >= 1 ) {
|
||||
if ($contacts->count() >= 1) {
|
||||
// nlog("found via contact {$contacts->first()->client_id}");
|
||||
return $contacts->first()->client_id;
|
||||
}
|
||||
}
|
||||
|
||||
return $contacts->first()->client_id;
|
||||
}
|
||||
}
|
||||
|
||||
// nlog("did not find client");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
@ -97,7 +102,12 @@ class BaseTransformer
|
||||
*/
|
||||
public function hasClient($name)
|
||||
{
|
||||
return $this->company->clients()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->exists();
|
||||
return $this->company
|
||||
->clients()
|
||||
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $name)),
|
||||
])
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -107,7 +117,27 @@ class BaseTransformer
|
||||
*/
|
||||
public function hasVendor($name)
|
||||
{
|
||||
return $this->company->vendors()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->exists();
|
||||
return $this->company
|
||||
->vendors()
|
||||
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $name)),
|
||||
])
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasProject($name)
|
||||
{
|
||||
return $this->company
|
||||
->projects()
|
||||
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $name)),
|
||||
])
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -117,7 +147,12 @@ class BaseTransformer
|
||||
*/
|
||||
public function hasProduct($key)
|
||||
{
|
||||
return $this->company->products()->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $key))])->exists();
|
||||
return $this->company
|
||||
->products()
|
||||
->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $key)),
|
||||
])
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -144,7 +179,12 @@ class BaseTransformer
|
||||
*/
|
||||
public function getClientId($name)
|
||||
{
|
||||
$client = $this->company->clients()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first();
|
||||
$client = $this->company
|
||||
->clients()
|
||||
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $name)),
|
||||
])
|
||||
->first();
|
||||
|
||||
return $client ? $client->id : null;
|
||||
}
|
||||
@ -156,14 +196,18 @@ class BaseTransformer
|
||||
*/
|
||||
public function getProduct($data, $key, $field, $default = false)
|
||||
{
|
||||
$product = $this->company
|
||||
->products()
|
||||
->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $data->{$key})),
|
||||
])
|
||||
->first();
|
||||
|
||||
$product = $this->company->products()->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $data->{$key}))])->first();
|
||||
|
||||
if($product)
|
||||
if ($product) {
|
||||
return $product->{$field} ?: $default;
|
||||
}
|
||||
|
||||
return $default;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -173,14 +217,18 @@ class BaseTransformer
|
||||
*/
|
||||
public function getContact($email)
|
||||
{
|
||||
|
||||
$contact = $this->company->client_contacts()->whereRaw("LOWER(REPLACE(`email`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $email))])->first();
|
||||
$contact = $this->company
|
||||
->client_contacts()
|
||||
->whereRaw("LOWER(REPLACE(`email`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $email)),
|
||||
])
|
||||
->first();
|
||||
|
||||
if(!$contact)
|
||||
if (!$contact) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $contact;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -190,10 +238,13 @@ class BaseTransformer
|
||||
*/
|
||||
public function getCountryId($name)
|
||||
{
|
||||
if(strlen($name) == 2)
|
||||
if (strlen($name) == 2) {
|
||||
return $this->getCountryIdBy2($name);
|
||||
}
|
||||
|
||||
$country = Country::whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first();
|
||||
$country = Country::whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $name)),
|
||||
])->first();
|
||||
|
||||
return $country ? $country->id : null;
|
||||
}
|
||||
@ -205,7 +256,9 @@ class BaseTransformer
|
||||
*/
|
||||
public function getCountryIdBy2($name)
|
||||
{
|
||||
return Country::where('iso_3166_2', $name)->exists() ? Country::where('iso_3166_2', $name)->first()->id : null;
|
||||
return Country::where('iso_3166_2', $name)->exists()
|
||||
? Country::where('iso_3166_2', $name)->first()->id
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -217,7 +270,12 @@ class BaseTransformer
|
||||
{
|
||||
$name = strtolower(trim($name));
|
||||
|
||||
$tax_rate = $this->company->tax_rates()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first();
|
||||
$tax_rate = $this->company
|
||||
->tax_rates()
|
||||
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $name)),
|
||||
])
|
||||
->first();
|
||||
|
||||
return $tax_rate ? $tax_rate->rate : 0;
|
||||
}
|
||||
@ -231,10 +289,14 @@ class BaseTransformer
|
||||
{
|
||||
$name = strtolower(trim($name));
|
||||
|
||||
$tax_rate = $this->company->tax_rates()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first();
|
||||
$tax_rate = $this->company
|
||||
->tax_rates()
|
||||
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $name)),
|
||||
])
|
||||
->first();
|
||||
|
||||
return $tax_rate ? $tax_rate->name : '';
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -266,7 +328,7 @@ class BaseTransformer
|
||||
*/
|
||||
public function getInvoiceNumber($number)
|
||||
{
|
||||
return $number ? ltrim( trim( $number ), '0' ) : null;
|
||||
return $number ? ltrim(trim($number), '0') : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -276,9 +338,14 @@ class BaseTransformer
|
||||
*/
|
||||
public function getInvoiceId($invoice_number)
|
||||
{
|
||||
$invoice = $this->company->invoices()->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $invoice_number))])->first();
|
||||
$invoice = $this->company
|
||||
->invoices()
|
||||
->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $invoice_number)),
|
||||
])
|
||||
->first();
|
||||
|
||||
return $invoice ? $invoice->id : null;
|
||||
return $invoice ? $invoice->id : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -288,9 +355,12 @@ class BaseTransformer
|
||||
*/
|
||||
public function hasInvoice($invoice_number)
|
||||
{
|
||||
|
||||
return $this->company->invoices()->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $invoice_number))])->exists();
|
||||
|
||||
return $this->company
|
||||
->invoices()
|
||||
->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $invoice_number)),
|
||||
])
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -300,7 +370,12 @@ class BaseTransformer
|
||||
*/
|
||||
public function getInvoiceClientId($invoice_number)
|
||||
{
|
||||
$invoice = $this->company->invoices()->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $invoice_number))])->first();
|
||||
$invoice = $this->company
|
||||
->invoices()
|
||||
->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $invoice_number)),
|
||||
])
|
||||
->first();
|
||||
|
||||
return $invoice ? $invoice->client_id : null;
|
||||
}
|
||||
@ -312,47 +387,75 @@ class BaseTransformer
|
||||
*/
|
||||
public function getVendorId($name)
|
||||
{
|
||||
$vendor = $this->company->vendors()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first();
|
||||
$vendor = $this->company
|
||||
->vendors()
|
||||
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $name)),
|
||||
])
|
||||
->first();
|
||||
|
||||
return $vendor ? $vendor->id : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getExpenseCategoryId( $name ) {
|
||||
|
||||
$ec = $this->company->expense_categories()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first();
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getExpenseCategoryId($name)
|
||||
{
|
||||
$ec = $this->company
|
||||
->expense_categories()
|
||||
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $name)),
|
||||
])
|
||||
->first();
|
||||
|
||||
return $ec ? $ec->id : null;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getProjectId($name, $clientId = null)
|
||||
{
|
||||
$project = $this->company
|
||||
->projects()
|
||||
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $name)),
|
||||
])
|
||||
->first();
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getProjectId( $name ) {
|
||||
return $project ? $project->id : $this->createProject($name, $clientId);
|
||||
}
|
||||
|
||||
$project = $this->company->projects()->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first();
|
||||
private function createProject($name, $clientId)
|
||||
{
|
||||
$project = ProjectFactory::create($this->company->id, $this->company->owner()->id);
|
||||
$project->name = $name;
|
||||
|
||||
return $project ? $project->id : null;
|
||||
}
|
||||
if($clientId)
|
||||
$project->client_id = $clientId;
|
||||
|
||||
$project->saveQuietly();
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getPaymentTypeId( $name ) {
|
||||
|
||||
$pt = PaymentType::whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [strtolower(str_replace(' ', '', $name))])->first();
|
||||
return $project->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getPaymentTypeId($name)
|
||||
{
|
||||
$pt = PaymentType::whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
|
||||
strtolower(str_replace(' ', '', $name)),
|
||||
])->first();
|
||||
|
||||
return $pt ? $pt->id : null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -19,64 +19,101 @@ use Illuminate\Support\Str;
|
||||
*/
|
||||
class ClientTransformer extends BaseTransformer
|
||||
{
|
||||
/**
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
public function transform($data)
|
||||
{
|
||||
if (isset($data->name) && $this->hasClient($data->name)) {
|
||||
throw new ImportException('Client already exists');
|
||||
}
|
||||
public function transform($data)
|
||||
{
|
||||
if (isset($data->name) && $this->hasClient($data->name)) {
|
||||
throw new ImportException('Client already exists');
|
||||
}
|
||||
|
||||
$settings = new \stdClass;
|
||||
$settings->currency_id = (string)$this->getCurrencyByCode($data);
|
||||
$settings = new \stdClass();
|
||||
$settings->currency_id = (string) $this->getCurrencyByCode($data);
|
||||
|
||||
return [
|
||||
'company_id' => $this->company->id,
|
||||
'name' => $this->getString( $data, 'client.name' ),
|
||||
'work_phone' => $this->getString( $data, 'client.phone' ),
|
||||
'address1' => $this->getString( $data, 'client.address1' ),
|
||||
'address2' => $this->getString( $data, 'client.address2' ),
|
||||
'postal_code' => $this->getString( $data, 'client.postal_code'),
|
||||
'city' => $this->getString( $data, 'client.city' ),
|
||||
'state' => $this->getString( $data, 'client.state' ),
|
||||
'shipping_address1' => $this->getString( $data, 'client.shipping_address1' ),
|
||||
'shipping_address2' => $this->getString( $data, 'client.shipping_address2' ),
|
||||
'shipping_city' => $this->getString( $data, 'client.shipping_city' ),
|
||||
'shipping_state' => $this->getString( $data, 'client.shipping_state' ),
|
||||
'shipping_postal_code' => $this->getString( $data, 'client.shipping_postal_code' ),
|
||||
'public_notes' => $this->getString( $data, 'client.public_notes' ),
|
||||
'private_notes' => $this->getString( $data, 'client.private_notes' ),
|
||||
'website' => $this->getString( $data, 'client.website' ),
|
||||
'vat_number' => $this->getString( $data, 'client.vat_number' ),
|
||||
'id_number' => $this->getString( $data, 'client.id_number' ),
|
||||
'custom_value1' => $this->getString( $data, 'client.custom_value1' ),
|
||||
'custom_value2' => $this->getString( $data, 'client.custom_value2' ),
|
||||
'custom_value3' => $this->getString( $data, 'client.custom_value3' ),
|
||||
'custom_value4' => $this->getString( $data, 'client.custom_value4' ),
|
||||
'balance' => preg_replace( '/[^0-9,.]+/', '', $this->getFloat( $data, 'client.balance' ) ),
|
||||
'paid_to_date' => preg_replace( '/[^0-9,.]+/', '', $this->getFloat( $data, 'client.paid_to_date' ) ),
|
||||
'credit_balance' => 0,
|
||||
'settings' => $settings,
|
||||
'client_hash' => Str::random( 40 ),
|
||||
'contacts' => [
|
||||
'company_id' => $this->company->id,
|
||||
'name' => $this->getString($data, 'client.name'),
|
||||
'work_phone' => $this->getString($data, 'client.phone'),
|
||||
'address1' => $this->getString($data, 'client.address1'),
|
||||
'address2' => $this->getString($data, 'client.address2'),
|
||||
'postal_code' => $this->getString($data, 'client.postal_code'),
|
||||
'city' => $this->getString($data, 'client.city'),
|
||||
'state' => $this->getString($data, 'client.state'),
|
||||
'shipping_address1' => $this->getString(
|
||||
$data,
|
||||
'client.shipping_address1'
|
||||
),
|
||||
'shipping_address2' => $this->getString(
|
||||
$data,
|
||||
'client.shipping_address2'
|
||||
),
|
||||
'shipping_city' => $this->getString($data, 'client.shipping_city'),
|
||||
'shipping_state' => $this->getString(
|
||||
$data,
|
||||
'client.shipping_state'
|
||||
),
|
||||
'shipping_postal_code' => $this->getString(
|
||||
$data,
|
||||
'client.shipping_postal_code'
|
||||
),
|
||||
'public_notes' => $this->getString($data, 'client.public_notes'),
|
||||
'private_notes' => $this->getString($data, 'client.private_notes'),
|
||||
'website' => $this->getString($data, 'client.website'),
|
||||
'vat_number' => $this->getString($data, 'client.vat_number'),
|
||||
'id_number' => $this->getString($data, 'client.id_number'),
|
||||
'custom_value1' => $this->getString($data, 'client.custom_value1'),
|
||||
'custom_value2' => $this->getString($data, 'client.custom_value2'),
|
||||
'custom_value3' => $this->getString($data, 'client.custom_value3'),
|
||||
'custom_value4' => $this->getString($data, 'client.custom_value4'),
|
||||
'balance' => preg_replace(
|
||||
'/[^0-9,.]+/',
|
||||
'',
|
||||
$this->getFloat($data, 'client.balance')
|
||||
),
|
||||
'paid_to_date' => preg_replace(
|
||||
'/[^0-9,.]+/',
|
||||
'',
|
||||
$this->getFloat($data, 'client.paid_to_date')
|
||||
),
|
||||
'credit_balance' => 0,
|
||||
'settings' => $settings,
|
||||
'client_hash' => Str::random(40),
|
||||
'contacts' => [
|
||||
[
|
||||
'first_name' => $this->getString( $data, 'contact.first_name' ),
|
||||
'last_name' => $this->getString( $data, 'contact.last_name' ),
|
||||
'email' => $this->getString( $data, 'contact.email' ),
|
||||
'phone' => $this->getString( $data, 'contact.phone' ),
|
||||
'custom_value1' => $this->getString( $data, 'contact.custom_value1' ),
|
||||
'custom_value2' => $this->getString( $data, 'contact.custom_value2' ),
|
||||
'custom_value3' => $this->getString( $data, 'contact.custom_value3' ),
|
||||
'custom_value4' => $this->getString( $data, 'contact.custom_value4' ),
|
||||
'first_name' => $this->getString(
|
||||
$data,
|
||||
'contact.first_name'
|
||||
),
|
||||
'last_name' => $this->getString($data, 'contact.last_name'),
|
||||
'email' => $this->getString($data, 'contact.email'),
|
||||
'phone' => $this->getString($data, 'contact.phone'),
|
||||
'custom_value1' => $this->getString(
|
||||
$data,
|
||||
'contact.custom_value1'
|
||||
),
|
||||
'custom_value2' => $this->getString(
|
||||
$data,
|
||||
'contact.custom_value2'
|
||||
),
|
||||
'custom_value3' => $this->getString(
|
||||
$data,
|
||||
'contact.custom_value3'
|
||||
),
|
||||
'custom_value4' => $this->getString(
|
||||
$data,
|
||||
'contact.custom_value4'
|
||||
),
|
||||
],
|
||||
],
|
||||
'country_id' => isset( $data['client.country'] ) ? $this->getCountryId( $data['client.country']) : null,
|
||||
'shipping_country_id' => isset($data['client.shipping_country'] ) ? $this->getCountryId( $data['client.shipping_country'] ) : null,
|
||||
'country_id' => isset($data['client.country'])
|
||||
? $this->getCountryId($data['client.country'])
|
||||
: null,
|
||||
'shipping_country_id' => isset($data['client.shipping_country'])
|
||||
? $this->getCountryId($data['client.shipping_country'])
|
||||
: null,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
70
app/Import/Transformer/Csv/ExpenseTransformer.php
Normal file
70
app/Import/Transformer/Csv/ExpenseTransformer.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* client Ninja (https://clientninja.com).
|
||||
*
|
||||
* @link https://github.com/clientninja/clientninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2021. client Ninja LLC (https://clientninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
namespace App\Import\Transformer\Csv;
|
||||
|
||||
use App\Import\Transformer\BaseTransformer;
|
||||
|
||||
/**
|
||||
* Class InvoiceTransformer.
|
||||
*/
|
||||
class ExpenseTransformer extends BaseTransformer
|
||||
{
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return bool|array
|
||||
*/
|
||||
public function transform($data)
|
||||
{
|
||||
$clientId = isset($data['expense.client'])
|
||||
? $this->getClientId($data['expense.client'])
|
||||
: null;
|
||||
|
||||
return [
|
||||
'company_id' => $this->company->id,
|
||||
'amount' => $this->getFloat($data, 'expense.amount'),
|
||||
'currency_id' => $this->getCurrencyByCode(
|
||||
$data,
|
||||
'expense.currency_id'
|
||||
),
|
||||
'vendor_id' => isset($data['expense.vendor'])
|
||||
? $this->getVendorId($data['expense.vendor'])
|
||||
: null,
|
||||
'client_id' => isset($data['expense.client'])
|
||||
? $this->getClientId($data['expense.client'])
|
||||
: null,
|
||||
'date' => $clientId,
|
||||
'public_notes' => $this->getString($data, 'expense.public_notes'),
|
||||
'private_notes' => $this->getString($data, 'expense.private_notes'),
|
||||
'category_id' => isset($data['expense.category'])
|
||||
? $this->getExpenseCategoryId($data['expense.category'])
|
||||
: null,
|
||||
'project_id' => isset($data['expense.project'])
|
||||
? $this->getProjectId($data['expense.project'], $clientId)
|
||||
: null,
|
||||
'payment_type_id' => isset($data['expense.payment_type'])
|
||||
? $this->getPaymentTypeId($data['expense.payment_type'])
|
||||
: null,
|
||||
'payment_date' => isset($data['expense.payment_date'])
|
||||
? date('Y-m-d', strtotime($data['expense.payment_date']))
|
||||
: null,
|
||||
'custom_value1' => $this->getString($data, 'expense.custom_value1'),
|
||||
'custom_value2' => $this->getString($data, 'expense.custom_value2'),
|
||||
'custom_value3' => $this->getString($data, 'expense.custom_value3'),
|
||||
'custom_value4' => $this->getString($data, 'expense.custom_value4'),
|
||||
'transaction_reference' => $this->getString(
|
||||
$data,
|
||||
'expense.transaction_reference'
|
||||
),
|
||||
'should_be_invoiced' => $clientId ? true : false,
|
||||
];
|
||||
}
|
||||
}
|
@ -19,121 +19,221 @@ use App\Models\Invoice;
|
||||
/**
|
||||
* Class InvoiceTransformer.
|
||||
*/
|
||||
class InvoiceTransformer extends BaseTransformer {
|
||||
|
||||
|
||||
class InvoiceTransformer extends BaseTransformer
|
||||
{
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return bool|array
|
||||
*/
|
||||
public function transform( $line_items_data ) {
|
||||
public function transform($line_items_data)
|
||||
{
|
||||
$invoice_data = reset($line_items_data);
|
||||
|
||||
$invoice_data = reset( $line_items_data );
|
||||
|
||||
if ( $this->hasInvoice( $invoice_data['invoice.number'] ) ) {
|
||||
throw new ImportException( 'Invoice number already exists' );
|
||||
if ($this->hasInvoice($invoice_data['invoice.number'])) {
|
||||
throw new ImportException('Invoice number already exists');
|
||||
}
|
||||
|
||||
$invoiceStatusMap = [
|
||||
'sent' => Invoice::STATUS_SENT,
|
||||
'sent' => Invoice::STATUS_SENT,
|
||||
'draft' => Invoice::STATUS_DRAFT,
|
||||
];
|
||||
|
||||
$transformed = [
|
||||
'company_id' => $this->company->id,
|
||||
'number' => $this->getString( $invoice_data, 'invoice.number' ),
|
||||
'user_id' => $this->getString( $invoice_data, 'invoice.user_id' ),
|
||||
'amount' => $amount = $this->getFloat( $invoice_data, 'invoice.amount' ),
|
||||
'balance' => isset( $invoice_data['invoice.balance'] ) ? $this->getFloat( $invoice_data, 'invoice.balance' ) : $amount,
|
||||
'client_id' => $this->getClient( $this->getString( $invoice_data, 'client.name' ), $this->getString( $invoice_data, 'client.email' ) ),
|
||||
'discount' => $this->getFloat( $invoice_data, 'invoice.discount' ),
|
||||
'po_number' => $this->getString( $invoice_data, 'invoice.po_number' ),
|
||||
'date' => isset( $invoice_data['invoice.date'] ) ? date( 'Y-m-d', strtotime( $invoice_data['invoice.date'] ) ) : now()->format('Y-m-d'),
|
||||
'due_date' => isset( $invoice_data['invoice.due_date'] ) ? date( 'Y-m-d', strtotime( $invoice_data['invoice.due_date'] ) ) : null,
|
||||
'terms' => $this->getString( $invoice_data, 'invoice.terms' ),
|
||||
'public_notes' => $this->getString( $invoice_data, 'invoice.public_notes' ),
|
||||
'private_notes' => $this->getString( $invoice_data, 'invoice.private_notes' ),
|
||||
'tax_name1' => $this->getString( $invoice_data, 'invoice.tax_name1' ),
|
||||
'tax_rate1' => $this->getFloat( $invoice_data, 'invoice.tax_rate1' ),
|
||||
'tax_name2' => $this->getString( $invoice_data, 'invoice.tax_name2' ),
|
||||
'tax_rate2' => $this->getFloat( $invoice_data, 'invoice.tax_rate2' ),
|
||||
'tax_name3' => $this->getString( $invoice_data, 'invoice.tax_name3' ),
|
||||
'tax_rate3' => $this->getFloat( $invoice_data, 'invoice.tax_rate3' ),
|
||||
'custom_value1' => $this->getString( $invoice_data, 'invoice.custom_value1' ),
|
||||
'custom_value2' => $this->getString( $invoice_data, 'invoice.custom_value2' ),
|
||||
'custom_value3' => $this->getString( $invoice_data, 'invoice.custom_value3' ),
|
||||
'custom_value4' => $this->getString( $invoice_data, 'invoice.custom_value4' ),
|
||||
'footer' => $this->getString( $invoice_data, 'invoice.footer' ),
|
||||
'partial' => $this->getFloat( $invoice_data, 'invoice.partial' ),
|
||||
'partial_due_date' => $this->getString( $invoice_data, 'invoice.partial_due_date' ),
|
||||
'custom_surcharge1' => $this->getString( $invoice_data, 'invoice.custom_surcharge1' ),
|
||||
'custom_surcharge2' => $this->getString( $invoice_data, 'invoice.custom_surcharge2' ),
|
||||
'custom_surcharge3' => $this->getString( $invoice_data, 'invoice.custom_surcharge3' ),
|
||||
'custom_surcharge4' => $this->getString( $invoice_data, 'invoice.custom_surcharge4' ),
|
||||
'exchange_rate' => $this->getString( $invoice_data, 'invoice.exchange_rate' ),
|
||||
'status_id' => $invoiceStatusMap[ $status =
|
||||
strtolower( $this->getString( $invoice_data, 'invoice.status' ) ) ] ??
|
||||
Invoice::STATUS_SENT,
|
||||
'archived' => $status === 'archived',
|
||||
'company_id' => $this->company->id,
|
||||
'number' => $this->getString($invoice_data, 'invoice.number'),
|
||||
'user_id' => $this->getString($invoice_data, 'invoice.user_id'),
|
||||
'amount' => ($amount = $this->getFloat(
|
||||
$invoice_data,
|
||||
'invoice.amount'
|
||||
)),
|
||||
'balance' => isset($invoice_data['invoice.balance'])
|
||||
? $this->getFloat($invoice_data, 'invoice.balance')
|
||||
: $amount,
|
||||
'client_id' => $this->getClient(
|
||||
$this->getString($invoice_data, 'client.name'),
|
||||
$this->getString($invoice_data, 'client.email')
|
||||
),
|
||||
'discount' => $this->getFloat($invoice_data, 'invoice.discount'),
|
||||
'po_number' => $this->getString($invoice_data, 'invoice.po_number'),
|
||||
'date' => isset($invoice_data['invoice.date'])
|
||||
? date('Y-m-d', strtotime($invoice_data['invoice.date']))
|
||||
: now()->format('Y-m-d'),
|
||||
'due_date' => isset($invoice_data['invoice.due_date'])
|
||||
? date('Y-m-d', strtotime($invoice_data['invoice.due_date']))
|
||||
: null,
|
||||
'terms' => $this->getString($invoice_data, 'invoice.terms'),
|
||||
'public_notes' => $this->getString(
|
||||
$invoice_data,
|
||||
'invoice.public_notes'
|
||||
),
|
||||
'private_notes' => $this->getString(
|
||||
$invoice_data,
|
||||
'invoice.private_notes'
|
||||
),
|
||||
'tax_name1' => $this->getString($invoice_data, 'invoice.tax_name1'),
|
||||
'tax_rate1' => $this->getFloat($invoice_data, 'invoice.tax_rate1'),
|
||||
'tax_name2' => $this->getString($invoice_data, 'invoice.tax_name2'),
|
||||
'tax_rate2' => $this->getFloat($invoice_data, 'invoice.tax_rate2'),
|
||||
'tax_name3' => $this->getString($invoice_data, 'invoice.tax_name3'),
|
||||
'tax_rate3' => $this->getFloat($invoice_data, 'invoice.tax_rate3'),
|
||||
'custom_value1' => $this->getString(
|
||||
$invoice_data,
|
||||
'invoice.custom_value1'
|
||||
),
|
||||
'custom_value2' => $this->getString(
|
||||
$invoice_data,
|
||||
'invoice.custom_value2'
|
||||
),
|
||||
'custom_value3' => $this->getString(
|
||||
$invoice_data,
|
||||
'invoice.custom_value3'
|
||||
),
|
||||
'custom_value4' => $this->getString(
|
||||
$invoice_data,
|
||||
'invoice.custom_value4'
|
||||
),
|
||||
'footer' => $this->getString($invoice_data, 'invoice.footer'),
|
||||
'partial' => $this->getFloat($invoice_data, 'invoice.partial'),
|
||||
'partial_due_date' => $this->getString(
|
||||
$invoice_data,
|
||||
'invoice.partial_due_date'
|
||||
),
|
||||
'custom_surcharge1' => $this->getString(
|
||||
$invoice_data,
|
||||
'invoice.custom_surcharge1'
|
||||
),
|
||||
'custom_surcharge2' => $this->getString(
|
||||
$invoice_data,
|
||||
'invoice.custom_surcharge2'
|
||||
),
|
||||
'custom_surcharge3' => $this->getString(
|
||||
$invoice_data,
|
||||
'invoice.custom_surcharge3'
|
||||
),
|
||||
'custom_surcharge4' => $this->getString(
|
||||
$invoice_data,
|
||||
'invoice.custom_surcharge4'
|
||||
),
|
||||
'exchange_rate' => $this->getString(
|
||||
$invoice_data,
|
||||
'invoice.exchange_rate'
|
||||
),
|
||||
'status_id' =>
|
||||
$invoiceStatusMap[
|
||||
($status = strtolower(
|
||||
$this->getString($invoice_data, 'invoice.status')
|
||||
))
|
||||
] ?? Invoice::STATUS_SENT,
|
||||
'archived' => $status === 'archived',
|
||||
];
|
||||
|
||||
/* If we can't find the client, then lets try and create a client */
|
||||
if(!$transformed['client_id']){
|
||||
|
||||
if (!$transformed['client_id']) {
|
||||
$client_transformer = new ClientTransformer($this->company);
|
||||
|
||||
$transformed['client'] = $client_transformer->transform($invoice_data);
|
||||
|
||||
$transformed['client'] = $client_transformer->transform(
|
||||
$invoice_data
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
if ( isset( $invoice_data['payment.amount'] ) ) {
|
||||
if (isset($invoice_data['payment.amount'])) {
|
||||
$transformed['payments'] = [
|
||||
[
|
||||
'date' => isset( $invoice_data['payment.date'] ) ? date( 'Y-m-d', strtotime( $invoice_data['payment.date'] ) ) : date( 'y-m-d' ),
|
||||
'transaction_reference' => $this->getString( $invoice_data, 'payment.transaction_reference' ),
|
||||
'amount' => $this->getFloat( $invoice_data, 'payment.amount' ),
|
||||
'date' => isset($invoice_data['payment.date'])
|
||||
? date(
|
||||
'Y-m-d',
|
||||
strtotime($invoice_data['payment.date'])
|
||||
)
|
||||
: date('y-m-d'),
|
||||
'transaction_reference' => $this->getString(
|
||||
$invoice_data,
|
||||
'payment.transaction_reference'
|
||||
),
|
||||
'amount' => $this->getFloat(
|
||||
$invoice_data,
|
||||
'payment.amount'
|
||||
),
|
||||
],
|
||||
];
|
||||
} elseif ( $status === 'paid' ) {
|
||||
} elseif ($status === 'paid') {
|
||||
$transformed['payments'] = [
|
||||
[
|
||||
'date' => isset( $invoice_data['payment.date'] ) ? date( 'Y-m-d', strtotime( $invoice_data['payment.date'] ) ) : date( 'y-m-d' ),
|
||||
'transaction_reference' => $this->getString( $invoice_data, 'payment.transaction_reference' ),
|
||||
'amount' => $this->getFloat( $invoice_data, 'invoice.amount' ),
|
||||
'date' => isset($invoice_data['payment.date'])
|
||||
? date(
|
||||
'Y-m-d',
|
||||
strtotime($invoice_data['payment.date'])
|
||||
)
|
||||
: date('y-m-d'),
|
||||
'transaction_reference' => $this->getString(
|
||||
$invoice_data,
|
||||
'payment.transaction_reference'
|
||||
),
|
||||
'amount' => $this->getFloat(
|
||||
$invoice_data,
|
||||
'invoice.amount'
|
||||
),
|
||||
],
|
||||
];
|
||||
} elseif ( isset( $transformed['amount'] ) && isset( $transformed['balance'] ) && ($transformed['amount'] != $transformed['balance'])) {
|
||||
} elseif (
|
||||
isset($transformed['amount']) &&
|
||||
isset($transformed['balance']) &&
|
||||
$transformed['amount'] != $transformed['balance']
|
||||
) {
|
||||
$transformed['payments'] = [
|
||||
[
|
||||
'date' => isset( $invoice_data['payment.date'] ) ? date( 'Y-m-d', strtotime( $invoice_data['payment.date'] ) ) : date( 'y-m-d' ),
|
||||
'transaction_reference' => $this->getString( $invoice_data, 'payment.transaction_reference' ),
|
||||
'amount' => $transformed['amount'] - $transformed['balance'],
|
||||
'date' => isset($invoice_data['payment.date'])
|
||||
? date(
|
||||
'Y-m-d',
|
||||
strtotime($invoice_data['payment.date'])
|
||||
)
|
||||
: date('y-m-d'),
|
||||
'transaction_reference' => $this->getString(
|
||||
$invoice_data,
|
||||
'payment.transaction_reference'
|
||||
),
|
||||
'amount' =>
|
||||
$transformed['amount'] - $transformed['balance'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$line_items = [];
|
||||
foreach ( $line_items_data as $record ) {
|
||||
foreach ($line_items_data as $record) {
|
||||
$line_items[] = [
|
||||
'quantity' => $this->getFloat( $record, 'item.quantity' ),
|
||||
'cost' => $this->getFloat( $record, 'item.cost' ),
|
||||
'product_key' => $this->getString( $record, 'item.product_key' ),
|
||||
'notes' => $this->getString( $record, 'item.notes' ),
|
||||
'discount' => $this->getFloat( $record, 'item.discount' ),
|
||||
'is_amount_discount' => filter_var( $this->getString( $record, 'item.is_amount_discount' ), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ),
|
||||
'tax_name1' => $this->getString( $record, 'item.tax_name1' ),
|
||||
'tax_rate1' => $this->getFloat( $record, 'item.tax_rate1' ),
|
||||
'tax_name2' => $this->getString( $record, 'item.tax_name2' ),
|
||||
'tax_rate2' => $this->getFloat( $record, 'item.tax_rate2' ),
|
||||
'tax_name3' => $this->getString( $record, 'item.tax_name3' ),
|
||||
'tax_rate3' => $this->getFloat( $record, 'item.tax_rate3' ),
|
||||
'custom_value1' => $this->getString( $record, 'item.custom_value1' ),
|
||||
'custom_value2' => $this->getString( $record, 'item.custom_value2' ),
|
||||
'custom_value3' => $this->getString( $record, 'item.custom_value3' ),
|
||||
'custom_value4' => $this->getString( $record, 'item.custom_value4' ),
|
||||
'type_id' => "1", //$this->getInvoiceTypeId( $record, 'item.type_id' ),
|
||||
'quantity' => $this->getFloat($record, 'item.quantity'),
|
||||
'cost' => $this->getFloat($record, 'item.cost'),
|
||||
'product_key' => $this->getString($record, 'item.product_key'),
|
||||
'notes' => $this->getString($record, 'item.notes'),
|
||||
'discount' => $this->getFloat($record, 'item.discount'),
|
||||
'is_amount_discount' => filter_var(
|
||||
$this->getString($record, 'item.is_amount_discount'),
|
||||
FILTER_VALIDATE_BOOLEAN,
|
||||
FILTER_NULL_ON_FAILURE
|
||||
),
|
||||
'tax_name1' => $this->getString($record, 'item.tax_name1'),
|
||||
'tax_rate1' => $this->getFloat($record, 'item.tax_rate1'),
|
||||
'tax_name2' => $this->getString($record, 'item.tax_name2'),
|
||||
'tax_rate2' => $this->getFloat($record, 'item.tax_rate2'),
|
||||
'tax_name3' => $this->getString($record, 'item.tax_name3'),
|
||||
'tax_rate3' => $this->getFloat($record, 'item.tax_rate3'),
|
||||
'custom_value1' => $this->getString(
|
||||
$record,
|
||||
'item.custom_value1'
|
||||
),
|
||||
'custom_value2' => $this->getString(
|
||||
$record,
|
||||
'item.custom_value2'
|
||||
),
|
||||
'custom_value3' => $this->getString(
|
||||
$record,
|
||||
'item.custom_value3'
|
||||
),
|
||||
'custom_value4' => $this->getString(
|
||||
$record,
|
||||
'item.custom_value4'
|
||||
),
|
||||
'type_id' => '1', //$this->getInvoiceTypeId( $record, 'item.type_id' ),
|
||||
];
|
||||
}
|
||||
$transformed['line_items'] = $line_items;
|
||||
|
@ -17,44 +17,52 @@ use App\Import\Transformer\BaseTransformer;
|
||||
/**
|
||||
* Class PaymentTransformer.
|
||||
*/
|
||||
class PaymentTransformer extends BaseTransformer {
|
||||
class PaymentTransformer extends BaseTransformer
|
||||
{
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform( $data ) {
|
||||
|
||||
$client_id = $this->getClient( $this->getString( $data, 'payment.client_id' ), $this->getString( $data, 'payment.client_id' ) );
|
||||
public function transform($data)
|
||||
{
|
||||
$client_id = $this->getClient(
|
||||
$this->getString($data, 'payment.client_id'),
|
||||
$this->getString($data, 'payment.client_id')
|
||||
);
|
||||
|
||||
if ( empty( $client_id ) ) {
|
||||
throw new ImportException( 'Could not find client.' );
|
||||
if (empty($client_id)) {
|
||||
throw new ImportException('Could not find client.');
|
||||
}
|
||||
|
||||
$transformed = [
|
||||
'company_id' => $this->maps['company']->id,
|
||||
'number' => $this->getString( $data, 'payment.number' ),
|
||||
'user_id' => $this->getString( $data, 'payment.user_id' ),
|
||||
'amount' => $this->getFloat( $data, 'payment.amount' ),
|
||||
'refunded' => $this->getFloat( $data, 'payment.refunded' ),
|
||||
'applied' => $this->getFloat( $data, 'payment.applied' ),
|
||||
'transaction_reference' => $this->getString( $data, 'payment.transaction_reference ' ),
|
||||
'date' => $this->getString( $data, 'payment.date' ),
|
||||
'private_notes' => $this->getString( $data, 'payment.private_notes' ),
|
||||
'custom_value1' => $this->getString( $data, 'payment.custom_value1' ),
|
||||
'custom_value2' => $this->getString( $data, 'payment.custom_value2' ),
|
||||
'custom_value3' => $this->getString( $data, 'payment.custom_value3' ),
|
||||
'custom_value4' => $this->getString( $data, 'payment.custom_value4' ),
|
||||
'client_id' => $client_id,
|
||||
'company_id' => $this->maps['company']->id,
|
||||
'number' => $this->getString($data, 'payment.number'),
|
||||
'user_id' => $this->getString($data, 'payment.user_id'),
|
||||
'amount' => $this->getFloat($data, 'payment.amount'),
|
||||
'refunded' => $this->getFloat($data, 'payment.refunded'),
|
||||
'applied' => $this->getFloat($data, 'payment.applied'),
|
||||
'transaction_reference' => $this->getString(
|
||||
$data,
|
||||
'payment.transaction_reference '
|
||||
),
|
||||
'date' => $this->getString($data, 'payment.date'),
|
||||
'private_notes' => $this->getString($data, 'payment.private_notes'),
|
||||
'custom_value1' => $this->getString($data, 'payment.custom_value1'),
|
||||
'custom_value2' => $this->getString($data, 'payment.custom_value2'),
|
||||
'custom_value3' => $this->getString($data, 'payment.custom_value3'),
|
||||
'custom_value4' => $this->getString($data, 'payment.custom_value4'),
|
||||
'client_id' => $client_id,
|
||||
];
|
||||
|
||||
|
||||
if ( isset( $data['payment.invoice_number'] ) &&
|
||||
$invoice_id = $this->getInvoiceId( $data['payment.invoice_number'] ) ) {
|
||||
if (
|
||||
isset($data['payment.invoice_number']) &&
|
||||
($invoice_id = $this->getInvoiceId($data['payment.invoice_number']))
|
||||
) {
|
||||
$transformed['invoices'] = [
|
||||
[
|
||||
'invoice_id' => $invoice_id,
|
||||
'amount' => $transformed['amount'] ?? null,
|
||||
'amount' => $transformed['amount'] ?? null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
70
app/Import/Transformer/Csv/VendorTransformer.php
Normal file
70
app/Import/Transformer/Csv/VendorTransformer.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* client Ninja (https://clientninja.com).
|
||||
*
|
||||
* @link https://github.com/clientninja/clientninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2021. client Ninja LLC (https://clientninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
namespace App\Import\Transformer\Csv;
|
||||
|
||||
use App\Import\ImportException;
|
||||
use App\Import\Transformer\BaseTransformer;
|
||||
|
||||
/**
|
||||
* Class VendorTransformer.
|
||||
*/
|
||||
class VendorTransformer extends BaseTransformer
|
||||
{
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
public function transform($data)
|
||||
{
|
||||
if (isset($data->name) && $this->hasVendor($data->name)) {
|
||||
throw new ImportException('Vendor already exists');
|
||||
}
|
||||
|
||||
return [
|
||||
'company_id' => $this->company->id,
|
||||
'name' => $this->getString($data, 'vendor.name'),
|
||||
'phone' => $this->getString($data, 'vendor.phone'),
|
||||
'id_number' => $this->getString($data, 'vendor.id_number'),
|
||||
'vat_number' => $this->getString($data, 'vendor.vat_number'),
|
||||
'website' => $this->getString($data, 'vendor.website'),
|
||||
'currency_id' => $this->getCurrencyByCode(
|
||||
$data,
|
||||
'vendor.currency_id'
|
||||
),
|
||||
'public_notes' => $this->getString($data, 'vendor.public_notes'),
|
||||
'private_notes' => $this->getString($data, 'vendor.private_notes'),
|
||||
'address1' => $this->getString($data, 'vendor.address1'),
|
||||
'address2' => $this->getString($data, 'vendor.address2'),
|
||||
'city' => $this->getString($data, 'vendor.city'),
|
||||
'state' => $this->getString($data, 'vendor.state'),
|
||||
'postal_code' => $this->getString($data, 'vendor.postal_code'),
|
||||
'custom_value1' => $this->getString($data, 'vendor.custom_value1'),
|
||||
'custom_value2' => $this->getString($data, 'vendor.custom_value2'),
|
||||
'custom_value3' => $this->getString($data, 'vendor.custom_value3'),
|
||||
'custom_value4' => $this->getString($data, 'vendor.custom_value4'),
|
||||
'vendor_contacts' => [
|
||||
[
|
||||
'first_name' => $this->getString(
|
||||
$data,
|
||||
'vendor.first_name'
|
||||
),
|
||||
'last_name' => $this->getString($data, 'vendor.last_name'),
|
||||
'email' => $this->getString($data, 'vendor.email'),
|
||||
'phone' => $this->getString($data, 'vendor.phone'),
|
||||
],
|
||||
],
|
||||
'country_id' => isset($data['vendor.country_id'])
|
||||
? $this->getCountryId($data['vendor.country_id'])
|
||||
: null,
|
||||
];
|
||||
}
|
||||
}
|
@ -85,7 +85,7 @@ class BaseTransformer
|
||||
nlog("found via contact");
|
||||
}
|
||||
}
|
||||
nlog("did not find client");
|
||||
// nlog("did not find client");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
@ -19,62 +19,101 @@ use Illuminate\Support\Str;
|
||||
*/
|
||||
class ClientTransformer extends BaseTransformer
|
||||
{
|
||||
/**
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
public function transform($data)
|
||||
{
|
||||
if (isset($data->name) && $this->hasClient($data->name)) {
|
||||
throw new ImportException('Client already exists');
|
||||
}
|
||||
public function transform($data)
|
||||
{
|
||||
if (isset($data->name) && $this->hasClient($data->name)) {
|
||||
throw new ImportException('Client already exists');
|
||||
}
|
||||
|
||||
$settings = new \stdClass;
|
||||
$settings->currency_id = (string)$this->getCurrencyByCode($data);
|
||||
$settings = new \stdClass();
|
||||
$settings->currency_id = (string) $this->getCurrencyByCode($data);
|
||||
|
||||
return [
|
||||
'company_id' => $this->maps['company']->id,
|
||||
'name' => $this->getString( $data, 'client.name' ),
|
||||
'work_phone' => $this->getString( $data, 'client.phone' ),
|
||||
'address1' => $this->getString( $data, 'client.address1' ),
|
||||
'address2' => $this->getString( $data, 'client.address2' ),
|
||||
'postal_code' => $this->getString( $data, 'client.postal_code'),
|
||||
'city' => $this->getString( $data, 'client.city' ),
|
||||
'state' => $this->getString( $data, 'client.state' ),
|
||||
'shipping_address1' => $this->getString( $data, 'client.shipping_address1' ),
|
||||
'shipping_address2' => $this->getString( $data, 'client.shipping_address2' ),
|
||||
'shipping_city' => $this->getString( $data, 'client.shipping_city' ),
|
||||
'shipping_state' => $this->getString( $data, 'client.shipping_state' ),
|
||||
'shipping_postal_code' => $this->getString( $data, 'client.shipping_postal_code' ),
|
||||
'public_notes' => $this->getString( $data, 'client.public_notes' ),
|
||||
'private_notes' => $this->getString( $data, 'client.private_notes' ),
|
||||
'website' => $this->getString( $data, 'client.website' ),
|
||||
'vat_number' => $this->getString( $data, 'client.vat_number' ),
|
||||
'id_number' => $this->getString( $data, 'client.id_number' ),
|
||||
'custom_value1' => $this->getString( $data, 'client.custom_value1' ),
|
||||
'custom_value2' => $this->getString( $data, 'client.custom_value2' ),
|
||||
'custom_value3' => $this->getString( $data, 'client.custom_value3' ),
|
||||
'custom_value4' => $this->getString( $data, 'client.custom_value4' ),
|
||||
'balance' => preg_replace( '/[^0-9,.]+/', '', $this->getFloat( $data, 'client.balance' ) ),
|
||||
'paid_to_date' => preg_replace( '/[^0-9,.]+/', '', $this->getFloat( $data, 'client.paid_to_date' ) ),
|
||||
'credit_balance' => 0,
|
||||
'settings' => $settings,
|
||||
'client_hash' => Str::random( 40 ),
|
||||
'contacts' => [
|
||||
'company_id' => $this->maps['company']->id,
|
||||
'name' => $this->getString($data, 'client.name'),
|
||||
'work_phone' => $this->getString($data, 'client.phone'),
|
||||
'address1' => $this->getString($data, 'client.address1'),
|
||||
'address2' => $this->getString($data, 'client.address2'),
|
||||
'postal_code' => $this->getString($data, 'client.postal_code'),
|
||||
'city' => $this->getString($data, 'client.city'),
|
||||
'state' => $this->getString($data, 'client.state'),
|
||||
'shipping_address1' => $this->getString(
|
||||
$data,
|
||||
'client.shipping_address1'
|
||||
),
|
||||
'shipping_address2' => $this->getString(
|
||||
$data,
|
||||
'client.shipping_address2'
|
||||
),
|
||||
'shipping_city' => $this->getString($data, 'client.shipping_city'),
|
||||
'shipping_state' => $this->getString(
|
||||
$data,
|
||||
'client.shipping_state'
|
||||
),
|
||||
'shipping_postal_code' => $this->getString(
|
||||
$data,
|
||||
'client.shipping_postal_code'
|
||||
),
|
||||
'public_notes' => $this->getString($data, 'client.public_notes'),
|
||||
'private_notes' => $this->getString($data, 'client.private_notes'),
|
||||
'website' => $this->getString($data, 'client.website'),
|
||||
'vat_number' => $this->getString($data, 'client.vat_number'),
|
||||
'id_number' => $this->getString($data, 'client.id_number'),
|
||||
'custom_value1' => $this->getString($data, 'client.custom_value1'),
|
||||
'custom_value2' => $this->getString($data, 'client.custom_value2'),
|
||||
'custom_value3' => $this->getString($data, 'client.custom_value3'),
|
||||
'custom_value4' => $this->getString($data, 'client.custom_value4'),
|
||||
'balance' => preg_replace(
|
||||
'/[^0-9,.]+/',
|
||||
'',
|
||||
$this->getFloat($data, 'client.balance')
|
||||
),
|
||||
'paid_to_date' => preg_replace(
|
||||
'/[^0-9,.]+/',
|
||||
'',
|
||||
$this->getFloat($data, 'client.paid_to_date')
|
||||
),
|
||||
'credit_balance' => 0,
|
||||
'settings' => $settings,
|
||||
'client_hash' => Str::random(40),
|
||||
'contacts' => [
|
||||
[
|
||||
'first_name' => $this->getString( $data, 'contact.first_name' ),
|
||||
'last_name' => $this->getString( $data, 'contact.last_name' ),
|
||||
'email' => $this->getString( $data, 'contact.email' ),
|
||||
'phone' => $this->getString( $data, 'contact.phone' ),
|
||||
'custom_value1' => $this->getString( $data, 'contact.custom_value1' ),
|
||||
'custom_value2' => $this->getString( $data, 'contact.custom_value2' ),
|
||||
'custom_value3' => $this->getString( $data, 'contact.custom_value3' ),
|
||||
'custom_value4' => $this->getString( $data, 'contact.custom_value4' ),
|
||||
'first_name' => $this->getString(
|
||||
$data,
|
||||
'contact.first_name'
|
||||
),
|
||||
'last_name' => $this->getString($data, 'contact.last_name'),
|
||||
'email' => $this->getString($data, 'contact.email'),
|
||||
'phone' => $this->getString($data, 'contact.phone'),
|
||||
'custom_value1' => $this->getString(
|
||||
$data,
|
||||
'contact.custom_value1'
|
||||
),
|
||||
'custom_value2' => $this->getString(
|
||||
$data,
|
||||
'contact.custom_value2'
|
||||
),
|
||||
'custom_value3' => $this->getString(
|
||||
$data,
|
||||
'contact.custom_value3'
|
||||
),
|
||||
'custom_value4' => $this->getString(
|
||||
$data,
|
||||
'contact.custom_value4'
|
||||
),
|
||||
],
|
||||
],
|
||||
'country_id' => isset( $data['client.country'] ) ? $this->getCountryId( $data['client.country']) : null,
|
||||
'shipping_country_id' => isset($data['client.shipping_country'] ) ? $this->getCountryId( $data['client.shipping_country'] ) : null,
|
||||
'country_id' => isset($data['client.country'])
|
||||
? $this->getCountryId($data['client.country'])
|
||||
: null,
|
||||
'shipping_country_id' => isset($data['client.shipping_country'])
|
||||
? $this->getCountryId($data['client.shipping_country'])
|
||||
: null,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,6 @@ use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class CreateAccountActivity implements ShouldQueue
|
||||
{
|
||||
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
@ -39,11 +38,10 @@ class CreateAccountActivity implements ShouldQueue
|
||||
{
|
||||
MultiDB::setDb($event->company->db);
|
||||
|
||||
if(Ninja::isHosted())
|
||||
{
|
||||
$nmo = new NinjaMailerObject;
|
||||
if (Ninja::isHosted()) {
|
||||
$nmo = new NinjaMailerObject();
|
||||
$nmo->mailable = new \Modules\Admin\Mail\Welcome($event->user);
|
||||
$nmo->company = $event->company;
|
||||
$nmo->company = $event->company;
|
||||
$nmo->settings = $event->company->settings;
|
||||
$nmo->to_user = $event->user;
|
||||
|
||||
|
@ -41,9 +41,11 @@ class ArchivedClientActivity implements ShouldQueue
|
||||
{
|
||||
MultiDB::setDb($event->company->db);
|
||||
|
||||
$fields = new stdClass;
|
||||
$fields = new stdClass();
|
||||
|
||||
$user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->client->user_id;
|
||||
$user_id = array_key_exists('user_id', $event->event_vars)
|
||||
? $event->event_vars['user_id']
|
||||
: $event->client->user_id;
|
||||
|
||||
$fields->client_id = $event->client->id;
|
||||
$fields->user_id = $user_id;
|
||||
|
@ -43,9 +43,11 @@ class ClientUpdatedActivity implements ShouldQueue
|
||||
|
||||
$client = $event->client;
|
||||
|
||||
$fields = new stdClass;
|
||||
$fields = new stdClass();
|
||||
|
||||
$user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->client->user_id;
|
||||
$user_id = array_key_exists('user_id', $event->event_vars)
|
||||
? $event->event_vars['user_id']
|
||||
: $event->client->user_id;
|
||||
|
||||
$fields->client_id = $client->id;
|
||||
$fields->user_id = $user_id;
|
||||
|
@ -43,7 +43,7 @@ class BouncedEmail extends Mailable
|
||||
|
||||
return
|
||||
$this->from(config('mail.from.address'), config('mail.from.name'))
|
||||
->text()
|
||||
->text('bounced mail')
|
||||
->subject($subject);
|
||||
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ class ApplyPayment extends AbstractService
|
||||
->ledger()
|
||||
->updatePaymentBalance($amount_paid);
|
||||
|
||||
nlog("updating client balance by amount {$amount_paid}");
|
||||
// nlog("updating client balance by amount {$amount_paid}");
|
||||
|
||||
$this->invoice
|
||||
->client
|
||||
|
@ -8,6 +8,7 @@
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace Tests\Feature\Import\CSV;
|
||||
|
||||
use App\Import\Providers\Csv;
|
||||
@ -39,26 +40,92 @@ class CsvImportTest extends TestCase
|
||||
use MakesHash;
|
||||
use MockAccountData;
|
||||
|
||||
public function setUp() :void
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->withoutMiddleware(
|
||||
ThrottleRequests::class
|
||||
);
|
||||
$this->withoutMiddleware(ThrottleRequests::class);
|
||||
|
||||
config(['database.default' => config('ninja.db.default')]);
|
||||
|
||||
$this->makeTestData();
|
||||
|
||||
|
||||
$this->withoutExceptionHandling();
|
||||
}
|
||||
|
||||
public function testExpenseCsvImport()
|
||||
{
|
||||
$csv = file_get_contents(
|
||||
base_path() . '/tests/Feature/Import/expenses.csv'
|
||||
);
|
||||
$hash = Str::random(32);
|
||||
$column_map = [
|
||||
0 => 'expense.client',
|
||||
1 => 'expense.project',
|
||||
2 => 'expense.public_notes',
|
||||
3 => 'expense.amount',
|
||||
];
|
||||
|
||||
$data = [
|
||||
'hash' => $hash,
|
||||
'column_map' => ['expense' => ['mapping' => $column_map]],
|
||||
'skip_header' => true,
|
||||
'import_type' => 'csv',
|
||||
];
|
||||
|
||||
Cache::put($hash . '-expense', base64_encode($csv), 360);
|
||||
|
||||
$csv_importer = new Csv($data, $this->company);
|
||||
|
||||
$count = $csv_importer->import('expense');
|
||||
|
||||
$base_transformer = new BaseTransformer($this->company);
|
||||
|
||||
$this->assertTrue($base_transformer->hasProject('officiis'));
|
||||
}
|
||||
|
||||
public function testVendorCsvImport()
|
||||
{
|
||||
$csv = file_get_contents(
|
||||
base_path() . '/tests/Feature/Import/vendors.csv'
|
||||
);
|
||||
$hash = Str::random(32);
|
||||
$column_map = [
|
||||
0 => 'vendor.name',
|
||||
19 => 'vendor.currency_id',
|
||||
20 => 'vendor.public_notes',
|
||||
21 => 'vendor.private_notes',
|
||||
22 => 'vendor.first_name',
|
||||
23 => 'vendor.last_name',
|
||||
];
|
||||
|
||||
$data = [
|
||||
'hash' => $hash,
|
||||
'column_map' => ['vendor' => ['mapping' => $column_map]],
|
||||
'skip_header' => true,
|
||||
'import_type' => 'csv',
|
||||
];
|
||||
|
||||
$pre_import = Vendor::count();
|
||||
|
||||
Cache::put($hash . '-vendor', base64_encode($csv), 360);
|
||||
|
||||
$csv_importer = new Csv($data, $this->company);
|
||||
|
||||
$csv_importer->import('vendor');
|
||||
|
||||
$base_transformer = new BaseTransformer($this->company);
|
||||
|
||||
$this->assertTrue($base_transformer->hasVendor('Ludwig Krajcik DVM'));
|
||||
}
|
||||
|
||||
public function testProductImport()
|
||||
{
|
||||
$csv = file_get_contents( base_path() . '/tests/Feature/Import/products.csv' );
|
||||
$hash = Str::random( 32 );
|
||||
Cache::put( $hash . '-product', base64_encode( $csv ), 360 );
|
||||
$csv = file_get_contents(
|
||||
base_path() . '/tests/Feature/Import/products.csv'
|
||||
);
|
||||
$hash = Str::random(32);
|
||||
Cache::put($hash . '-product', base64_encode($csv), 360);
|
||||
|
||||
$column_map = [
|
||||
1 => 'product.product_key',
|
||||
@ -67,14 +134,14 @@ class CsvImportTest extends TestCase
|
||||
];
|
||||
|
||||
$data = [
|
||||
'hash' => $hash,
|
||||
'column_map' => [ 'product' => [ 'mapping' => $column_map ] ],
|
||||
'hash' => $hash,
|
||||
'column_map' => ['product' => ['mapping' => $column_map]],
|
||||
'skip_header' => true,
|
||||
'import_type' => 'csv',
|
||||
];
|
||||
|
||||
$csv_importer = new Csv($data, $this->company);
|
||||
|
||||
|
||||
$this->assertInstanceOf(Csv::class, $csv_importer);
|
||||
|
||||
$csv_importer->import('product');
|
||||
@ -82,61 +149,66 @@ class CsvImportTest extends TestCase
|
||||
$base_transformer = new BaseTransformer($this->company);
|
||||
|
||||
$this->assertTrue($base_transformer->hasProduct('officiis'));
|
||||
$this->assertTrue($base_transformer->hasProduct('maxime'));
|
||||
|
||||
// $this->assertTrue($base_transformer->hasProduct('maxime'));
|
||||
}
|
||||
|
||||
public function testClientImport()
|
||||
{
|
||||
$csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv');
|
||||
$csv = file_get_contents(
|
||||
base_path() . '/tests/Feature/Import/clients.csv'
|
||||
);
|
||||
$hash = Str::random(32);
|
||||
$column_map = [
|
||||
1 => 'client.balance',
|
||||
2 => 'client.paid_to_date',
|
||||
0 => 'client.name',
|
||||
19 => 'client.currency_id',
|
||||
20 => 'client.public_notes',
|
||||
21 => 'client.private_notes',
|
||||
22 => 'contact.first_name',
|
||||
23 => 'contact.last_name',
|
||||
24 => 'contact.email',
|
||||
1 => 'client.balance',
|
||||
2 => 'client.paid_to_date',
|
||||
0 => 'client.name',
|
||||
19 => 'client.currency_id',
|
||||
20 => 'client.public_notes',
|
||||
21 => 'client.private_notes',
|
||||
22 => 'contact.first_name',
|
||||
23 => 'contact.last_name',
|
||||
24 => 'contact.email',
|
||||
];
|
||||
|
||||
$data = [
|
||||
'hash' => $hash,
|
||||
'column_map' => [ 'client' => [ 'mapping' => $column_map ] ],
|
||||
'hash' => $hash,
|
||||
'column_map' => ['client' => ['mapping' => $column_map]],
|
||||
'skip_header' => true,
|
||||
'import_type' => 'csv',
|
||||
];
|
||||
|
||||
Cache::put( $hash . '-client', base64_encode( $csv ), 360 );
|
||||
Cache::put($hash . '-client', base64_encode($csv), 360);
|
||||
|
||||
$csv_importer = new Csv($data, $this->company);
|
||||
|
||||
|
||||
$this->assertInstanceOf(Csv::class, $csv_importer);
|
||||
|
||||
$csv_importer->import('client');
|
||||
|
||||
$base_transformer = new BaseTransformer($this->company);
|
||||
|
||||
$this->assertTrue($base_transformer->hasClient("Ludwig Krajcik DVM"));
|
||||
$this->assertTrue($base_transformer->hasClient('Ludwig Krajcik DVM'));
|
||||
|
||||
$client_id = $base_transformer->getClient("Ludwig Krajcik DVM", null);
|
||||
$client_id = $base_transformer->getClient('Ludwig Krajcik DVM', null);
|
||||
|
||||
$c = Client::find($client_id);
|
||||
|
||||
$this->assertEquals($client_id, $c->id);
|
||||
|
||||
$client_id = $base_transformer->getClient("a non existent clent", "brook59@example.org");
|
||||
$client_id = $base_transformer->getClient(
|
||||
'a non existent clent',
|
||||
'brook59@example.org'
|
||||
);
|
||||
|
||||
$this->assertEquals($client_id, $c->id);
|
||||
|
||||
}
|
||||
|
||||
public function testInvoiceImport()
|
||||
{
|
||||
/*Need to import clients first*/
|
||||
$csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv');
|
||||
$csv = file_get_contents(
|
||||
base_path() . '/tests/Feature/Import/clients.csv'
|
||||
);
|
||||
$hash = Str::random(32);
|
||||
$column_map = [
|
||||
1 => 'client.balance',
|
||||
@ -150,28 +222,30 @@ class CsvImportTest extends TestCase
|
||||
];
|
||||
|
||||
$data = [
|
||||
'hash' => $hash,
|
||||
'column_map' => [ 'client' => [ 'mapping' => $column_map ] ],
|
||||
'hash' => $hash,
|
||||
'column_map' => ['client' => ['mapping' => $column_map]],
|
||||
'skip_header' => true,
|
||||
'import_type' => 'csv',
|
||||
];
|
||||
|
||||
Cache::put( $hash . '-client', base64_encode( $csv ), 360 );
|
||||
Cache::put($hash . '-client', base64_encode($csv), 360);
|
||||
|
||||
$csv_importer = new Csv($data, $this->company);
|
||||
|
||||
|
||||
$this->assertInstanceOf(Csv::class, $csv_importer);
|
||||
|
||||
$csv_importer->import('client');
|
||||
|
||||
$base_transformer = new BaseTransformer($this->company);
|
||||
|
||||
$this->assertTrue($base_transformer->hasClient("Ludwig Krajcik DVM"));
|
||||
$this->assertTrue($base_transformer->hasClient('Ludwig Krajcik DVM'));
|
||||
|
||||
/* client import verified*/
|
||||
|
||||
/*Now import invoices*/
|
||||
$csv = file_get_contents(base_path().'/tests/Feature/Import/invoice.csv');
|
||||
$csv = file_get_contents(
|
||||
base_path() . '/tests/Feature/Import/invoice.csv'
|
||||
);
|
||||
$hash = Str::random(32);
|
||||
|
||||
$column_map = [
|
||||
@ -193,65 +267,86 @@ class CsvImportTest extends TestCase
|
||||
];
|
||||
|
||||
$data = [
|
||||
'hash' => $hash,
|
||||
'column_map' => [ 'invoice' => [ 'mapping' => $column_map ] ],
|
||||
'hash' => $hash,
|
||||
'column_map' => ['invoice' => ['mapping' => $column_map]],
|
||||
'skip_header' => true,
|
||||
'import_type' => 'csv',
|
||||
];
|
||||
|
||||
Cache::put( $hash . '-invoice', base64_encode( $csv ), 360 );
|
||||
Cache::put($hash . '-invoice', base64_encode($csv), 360);
|
||||
|
||||
$csv_importer = new Csv($data, $this->company);
|
||||
|
||||
$csv_importer->import('invoice');
|
||||
|
||||
$this->assertTrue($base_transformer->hasInvoice("801"));
|
||||
$this->assertTrue($base_transformer->hasInvoice('801'));
|
||||
|
||||
/* Lets piggy back payments tests here to save rebuilding the test multiple times*/
|
||||
|
||||
$csv = file_get_contents(
|
||||
base_path() . '/tests/Feature/Import/payments.csv'
|
||||
);
|
||||
$hash = Str::random(32);
|
||||
|
||||
$column_map = [
|
||||
0 => 'payment.client_id',
|
||||
1 => 'payment.invoice_number',
|
||||
2 => 'payment.amount',
|
||||
3 => 'payment.date',
|
||||
];
|
||||
|
||||
$data = [
|
||||
'hash' => $hash,
|
||||
'column_map' => ['payment' => ['mapping' => $column_map]],
|
||||
'skip_header' => true,
|
||||
'import_type' => 'csv',
|
||||
];
|
||||
|
||||
Cache::put($hash . '-payment', base64_encode($csv), 360);
|
||||
|
||||
$csv_importer = new Csv($data, $this->company);
|
||||
|
||||
$csv_importer->import('payment');
|
||||
|
||||
$this->assertTrue($base_transformer->hasInvoice('801'));
|
||||
|
||||
$invoice_id = $base_transformer->getInvoiceId('801');
|
||||
|
||||
$invoice = Invoice::find($invoice_id);
|
||||
|
||||
$this->assertTrue($invoice->payments()->exists());
|
||||
$this->assertEquals(1, $invoice->payments()->count());
|
||||
$this->assertEquals(400, $invoice->payments()->sum('payments.amount'));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// public function testClientCsvImport()
|
||||
// {
|
||||
// $csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv');
|
||||
// $hash = Str::random(32);
|
||||
// $column_map = [
|
||||
// 1 => 'client.balance',
|
||||
// 2 => 'client.paid_to_date',
|
||||
// 0 => 'client.name',
|
||||
// 19 => 'client.currency_id',
|
||||
// 20 => 'client.public_notes',
|
||||
// 21 => 'client.private_notes',
|
||||
// 22 => 'contact.first_name',
|
||||
// 23 => 'contact.last_name',
|
||||
// ];
|
||||
|
||||
// public function testClientCsvImport()
|
||||
// {
|
||||
// $csv = file_get_contents(base_path().'/tests/Feature/Import/clients.csv');
|
||||
// $hash = Str::random(32);
|
||||
// $column_map = [
|
||||
// 1 => 'client.balance',
|
||||
// 2 => 'client.paid_to_date',
|
||||
// 0 => 'client.name',
|
||||
// 19 => 'client.currency_id',
|
||||
// 20 => 'client.public_notes',
|
||||
// 21 => 'client.private_notes',
|
||||
// 22 => 'contact.first_name',
|
||||
// 23 => 'contact.last_name',
|
||||
// ];
|
||||
// $data = [
|
||||
// 'hash' => $hash,
|
||||
// 'column_map' => [ 'client' => [ 'mapping' => $column_map ] ],
|
||||
// 'skip_header' => true,
|
||||
// 'import_type' => 'csv',
|
||||
// ];
|
||||
|
||||
// $data = [
|
||||
// 'hash' => $hash,
|
||||
// 'column_map' => [ 'client' => [ 'mapping' => $column_map ] ],
|
||||
// 'skip_header' => true,
|
||||
// 'import_type' => 'csv',
|
||||
// ];
|
||||
// $pre_import = Client::count();
|
||||
|
||||
// $pre_import = Client::count();
|
||||
// Cache::put( $hash . '-client', base64_encode( $csv ), 360 );
|
||||
|
||||
// Cache::put( $hash . '-client', base64_encode( $csv ), 360 );
|
||||
// CSVImport::dispatchNow( $data, $this->company );
|
||||
|
||||
// CSVImport::dispatchNow( $data, $this->company );
|
||||
|
||||
// $this->assertGreaterThan( $pre_import, Client::count() );
|
||||
// }
|
||||
// $this->assertGreaterThan( $pre_import, Client::count() );
|
||||
// }
|
||||
|
Loading…
x
Reference in New Issue
Block a user