mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Invoice Import
This commit is contained in:
parent
4877d3c2f5
commit
6b02d51080
@ -112,6 +112,9 @@ class InvoiceItemSum
|
||||
{
|
||||
$item_tax = 0;
|
||||
|
||||
info(print_r($this->item,1));
|
||||
info(print_r($this->invoice,1));
|
||||
|
||||
$amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / 100));
|
||||
$item_tax_rate1_total = $this->calcAmountLineTax($this->item->tax_rate1, $amount);
|
||||
|
||||
|
@ -51,10 +51,13 @@ class StoreInvoiceRequest extends Request
|
||||
|
||||
$rules['invitations.*.client_contact_id'] = 'distinct';
|
||||
|
||||
if ($this->input('number')) {
|
||||
$rules['number'] = 'unique:invoices,number,'.$this->id.',id,company_id,'.auth()->user()->company()->id;
|
||||
// if ($this->input('number')) {
|
||||
// $rules['number'] = 'unique:invoices,number,'.$this->id.',id,company_id,'.auth()->user()->company()->id;
|
||||
// }
|
||||
if (isset($this->number)) {
|
||||
$rules['number'] = Rule::unique('invoices')->where('company_id', auth()->user()->company()->id);
|
||||
}
|
||||
// $rules['number'] = new UniqueInvoiceNumberRule($this->all());
|
||||
|
||||
|
||||
$rules['project_id'] = ['bail', 'sometimes', new ValidProjectForClient($this->all())];
|
||||
|
||||
|
@ -47,6 +47,11 @@ class BaseTransformer
|
||||
return (isset($data[$field]) && $data[$field]) ? $data[$field] : '';
|
||||
}
|
||||
|
||||
public function getInvoiceTypeId($data, $field)
|
||||
{
|
||||
return (isset($data[$field]) && $data[$field]) ? $data[$field] : '1';
|
||||
}
|
||||
|
||||
public function getCurrencyByCode($data)
|
||||
{
|
||||
$code = array_key_exists('client.currency_id', $data) ? $data['client.currency_id'] : false;
|
||||
|
@ -54,8 +54,8 @@ class ClientTransformer extends BaseTransformer
|
||||
'custom_value2' => $this->getString($data, 'client.custom2'),
|
||||
'custom_value3' => $this->getString($data, 'client.custom3'),
|
||||
'custom_value4' => $this->getString($data, 'client.custom4'),
|
||||
'balance' => $this->getString($data, 'client.balance'),
|
||||
'paid_to_date' => $this->getString($data, 'client.paid_to_date'),
|
||||
'balance' => $this->getFloat($data, 'client.balance'),
|
||||
'paid_to_date' => $this->getFloat($data, 'client.paid_to_date'),
|
||||
'credit_balance' => 0,
|
||||
'settings' => $settings,
|
||||
'client_hash' => Str::random(40),
|
||||
|
48
app/Import/Transformers/InvoiceItemTransformer.php
Normal file
48
app/Import/Transformers/InvoiceItemTransformer.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* client Ninja (https://clientninja.com).
|
||||
*
|
||||
* @link https://github.com/clientninja/clientninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2020. client Ninja LLC (https://clientninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace App\Import\Transformers;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Class InvoiceItemTransformer.
|
||||
*/
|
||||
class InvoiceItemTransformer extends BaseTransformer
|
||||
{
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return bool|Item
|
||||
*/
|
||||
public function transform($data)
|
||||
{
|
||||
return [
|
||||
'quantity' => $this->getFloat($data, 'item.quantity'),
|
||||
'cost' => $this->getFloat($data, 'item.cost'),
|
||||
'product_key' => $this->getString($data, 'item.product_key'),
|
||||
'notes' => $this->getString($data, 'item.notes'),
|
||||
'discount' => $this->getFloat($data, 'item.discount'),
|
||||
'is_amount_discount' => $this->getString($data, 'item.is_amount_discount'),
|
||||
'tax_name1' => $this->getString($data, 'item.tax_name1'),
|
||||
'tax_rate1' => $this->getFloat($data, 'item.tax_rate1'),
|
||||
'tax_name2' => $this->getString($data, 'item.tax_name2'),
|
||||
'tax_rate2' => $this->getFloat($data, 'item.tax_rate2'),
|
||||
'tax_name3' => $this->getString($data, 'item.tax_name3'),
|
||||
'tax_rate3' => $this->getFloat($data, 'item.tax_rate3'),
|
||||
'custom_value1' => $this->getString($data, 'item.custom_value1'),
|
||||
'custom_value2' => $this->getString($data, 'item.custom_value2'),
|
||||
'custom_value3' => $this->getString($data, 'item.custom_value3'),
|
||||
'custom_value4' => $this->getString($data, 'item.custom_value4'),
|
||||
'type_id' => $this->getInvoiceTypeId($data, 'item.type_id'),
|
||||
];
|
||||
}
|
||||
}
|
@ -29,10 +29,10 @@ class InvoiceTransformer extends BaseTransformer
|
||||
'company_id' => $this->maps['company']->id,
|
||||
'number' => $this->getString($data, 'invoice.number'),
|
||||
'user_id' => $this->getString($data, 'invoice.user_id'),
|
||||
'amount' => $this->getString($data, 'invoice.amount'),
|
||||
'balance' => $this->getString($data, 'invoice.balance'),
|
||||
'amount' => $this->getFloat($data, 'invoice.amount'),
|
||||
'balance' => $this->getFloat($data, 'invoice.balance'),
|
||||
'client_id' => $this->getClient($this->getString($data, 'invoice.client_id')),
|
||||
'discount' => $this->getString($data, 'invoice.discount'),
|
||||
'discount' => $this->getFloat($data, 'invoice.discount'),
|
||||
'po_number' => $this->getString($data, 'invoice.po_number'),
|
||||
'date' => $this->getString($data, 'invoice.date'),
|
||||
'due_date' => $this->getString($data, 'invoice.due_date'),
|
||||
@ -41,17 +41,17 @@ class InvoiceTransformer extends BaseTransformer
|
||||
'is_sent' => $this->getString($data, 'invoice.is_sent'),
|
||||
'private_notes' => $this->getString($data, 'invoice.private_notes'),
|
||||
'tax_name1' => $this->getString($data, 'invoice.tax_name1'),
|
||||
'tax_rate1' => $this->getString($data, 'invoice.tax_rate1'),
|
||||
'tax_rate1' => $this->getFloat($data, 'invoice.tax_rate1'),
|
||||
'tax_name2' => $this->getString($data, 'invoice.tax_name2'),
|
||||
'tax_rate2' => $this->getString($data, 'invoice.tax_rate2'),
|
||||
'tax_rate2' => $this->getFloat($data, 'invoice.tax_rate2'),
|
||||
'tax_name3' => $this->getString($data, 'invoice.tax_name3'),
|
||||
'tax_rate3' => $this->getString($data, 'invoice.tax_rate3'),
|
||||
'tax_rate3' => $this->getFloat($data, 'invoice.tax_rate3'),
|
||||
'custom_value1' => $this->getString($data, 'invoice.custom_value1'),
|
||||
'custom_value2' => $this->getString($data, 'invoice.custom_value2'),
|
||||
'custom_value3' => $this->getString($data, 'invoice.custom_value3'),
|
||||
'custom_value4' => $this->getString($data, 'invoice.custom_value4'),
|
||||
'footer' => $this->getString($data, 'invoice.footer'),
|
||||
'partial' => $this->getString($data, 'invoice.partial'),
|
||||
'partial' => $this->getFloat($data, 'invoice.partial'),
|
||||
'partial_due_date' => $this->getString($data, 'invoice.partial_due_date'),
|
||||
'custom_surcharge1' => $this->getString($data, 'invoice.custom_surcharge1'),
|
||||
'custom_surcharge2' => $this->getString($data, 'invoice.custom_surcharge2'),
|
||||
|
@ -29,9 +29,9 @@ class PaymentTransformer extends BaseTransformer
|
||||
'company_id' => $this->maps['company']->id,
|
||||
'number' => $this->getString($data, 'payment.number'),
|
||||
'user_id' => $this->getString($data, 'payment.user_id'),
|
||||
'amount' => $this->getString($data, 'payment.amount'),
|
||||
'refunded' => $this->getString($data, 'payment.refunded'),
|
||||
'applied' => $this->getString($data, 'payment.applied'),
|
||||
'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'),
|
||||
|
@ -29,15 +29,15 @@ class ProductTransformer extends BaseTransformer
|
||||
'company_id' => $this->maps['company']->id,
|
||||
'product_key' => $this->getString($data, 'product.product_key'),
|
||||
'notes' => $this->getString($data, 'product.notes'),
|
||||
'cost' => $this->getString($data, 'product.cost'),
|
||||
'price' => $this->getString($data, 'product.price'),
|
||||
'quantity' => $this->getString($data, 'product.quantity'),
|
||||
'cost' => $this->getFloat($data, 'product.cost'),
|
||||
'price' => $this->getFloat($data, 'product.price'),
|
||||
'quantity' => $this->getFloat($data, 'product.quantity'),
|
||||
'tax_name1' => $this->getString($data, 'product.tax_name1'),
|
||||
'tax_rate1' => $this->getString($data, 'product.tax_rate1'),
|
||||
'tax_rate1' => $this->getFloat($data, 'product.tax_rate1'),
|
||||
'tax_name2' => $this->getString($data, 'product.tax_name2'),
|
||||
'tax_rate2' => $this->getString($data, 'product.tax_rate2'),
|
||||
'tax_rate2' => $this->getFloat($data, 'product.tax_rate2'),
|
||||
'tax_name3' => $this->getString($data, 'product.tax_name3'),
|
||||
'tax_rate3' => $this->getString($data, 'product.tax_rate3'),
|
||||
'tax_rate3' => $this->getFloat($data, 'product.tax_rate3'),
|
||||
'custom_value1' => $this->getString($data, 'product.custom_value1'),
|
||||
'custom_value2' => $this->getString($data, 'product.custom_value2'),
|
||||
'custom_value3' => $this->getString($data, 'product.custom_value3'),
|
||||
|
@ -18,6 +18,7 @@ use App\Http\Requests\Client\StoreClientRequest;
|
||||
use App\Http\Requests\Invoice\StoreInvoiceRequest;
|
||||
use App\Http\Requests\Product\StoreProductRequest;
|
||||
use App\Import\Transformers\ClientTransformer;
|
||||
use App\Import\Transformers\InvoiceItemTransformer;
|
||||
use App\Import\Transformers\InvoiceTransformer;
|
||||
use App\Import\Transformers\ProductTransformer;
|
||||
use App\Libraries\MultiDB;
|
||||
@ -99,7 +100,8 @@ class CSVImport implements ShouldQueue
|
||||
|
||||
|
||||
info("errors");
|
||||
info(print_r($this->$this->error_array,1));
|
||||
|
||||
info(print_r($this->error_array,1));
|
||||
|
||||
}
|
||||
|
||||
@ -146,11 +148,14 @@ class CSVImport implements ShouldQueue
|
||||
private function importInvoice()
|
||||
{
|
||||
|
||||
$invoice_repository = new InvoiceRepository();
|
||||
$invoice_transformer = new InvoiceTransformer($this->maps);
|
||||
|
||||
info("import invoices");
|
||||
|
||||
info("column_map");
|
||||
|
||||
info(print_r($this->column_map,1));
|
||||
|
||||
$records = $this->getCsvData();
|
||||
|
||||
$invoice_number_key = array_search('Invoice Number', reset($records));
|
||||
@ -160,77 +165,63 @@ class CSVImport implements ShouldQueue
|
||||
if ($this->skip_header)
|
||||
array_shift($records);
|
||||
|
||||
if(!$invoice_number_key){
|
||||
info("no invoice number to use as key - returning");
|
||||
return;
|
||||
}
|
||||
|
||||
$unique_array_filter = array_unique($records[$invoice_number_key]);
|
||||
|
||||
info('unique array_filter');
|
||||
info(print_r($unique_array_filter,1));
|
||||
|
||||
$unique_invoices = array_intersect_key( $records, $unique_array_filter );
|
||||
|
||||
info("unique invoices");
|
||||
foreach($unique_invoices as $unique)
|
||||
{
|
||||
|
||||
info(print_r($unique_invoices,1));
|
||||
$keys = $this->column_map;
|
||||
$values = array_intersect_key($unique, $this->column_map);
|
||||
$invoice_data = array_combine($keys, $values);
|
||||
|
||||
$invoice = $invoice_transformer->transform(reset($records));
|
||||
$invoice = $invoice_transformer->transform($invoice_data);
|
||||
|
||||
foreach($unique_invoices as $val) {
|
||||
foreach($unique_invoices as $val) {
|
||||
|
||||
$invoices = array_filter($records, function($item) use ($val, $invoice_number_key){
|
||||
return $item[$invoice_number_key] == $val[$invoice_number_key];
|
||||
});
|
||||
$invoices = array_filter($records, function($item) use ($val, $invoice_number_key){
|
||||
return $item[$invoice_number_key] == $val[$invoice_number_key];
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
$this->processInvoice($invoices, $invoice);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private function processInvoice($invoices, $invoice)
|
||||
{
|
||||
|
||||
$invoice_repository = new InvoiceRepository();
|
||||
$item_transformer = new InvoiceItemTransformer($this->maps);
|
||||
$items = [];
|
||||
|
||||
info("invoice = ");
|
||||
info(print_r($invoice,1));
|
||||
|
||||
foreach($invoices as $record)
|
||||
{
|
||||
$keys = $this->column_map;
|
||||
|
||||
$item_keys = [
|
||||
36 => 'item.quantity',
|
||||
37 => 'item.cost',
|
||||
38 => 'item.product_key',
|
||||
39 => 'item.notes',
|
||||
40 => 'item.discount',
|
||||
41 => 'item.is_amount_discount',
|
||||
42 => 'item.tax_name1',
|
||||
43 => 'item.tax_rate1',
|
||||
44 => 'item.tax_name2',
|
||||
45 => 'item.tax_rate2',
|
||||
46 => 'item.tax_name3',
|
||||
47 => 'item.tax_rate3',
|
||||
48 => 'item.custom_value1',
|
||||
49 => 'item.custom_value2',
|
||||
50 => 'item.custom_value3',
|
||||
51 => 'item.custom_value4',
|
||||
52 => 'item.type_id',
|
||||
];
|
||||
|
||||
$values = array_intersect_key($record, $item_keys);
|
||||
|
||||
$items[] = array_combine($keys, $values);
|
||||
$keys = $this->column_map;
|
||||
$values = array_intersect_key($record, $this->column_map);
|
||||
$invoice_data = array_combine($keys, $values);
|
||||
|
||||
$items[] = $item_transformer->transform($invoice_data);
|
||||
|
||||
}
|
||||
|
||||
info("items");
|
||||
info(print_r($items,1));
|
||||
$invoice['line_items'] = $items;
|
||||
|
||||
$invoice->line_items = $items;
|
||||
info(print_r($invoice->toArray(),1));
|
||||
|
||||
$validator = Validator::make($invoice, (new StoreInvoiceRequest())->rules());
|
||||
|
||||
if ($validator->fails()) {
|
||||
$this->error_array[] = ['product' => $invoice, 'error' => json_encode($validator->errors())];
|
||||
$this->error_array[] = ['invoice' => $invoice, 'error' => json_encode($validator->errors())];
|
||||
} else {
|
||||
$invoice = $invoice_repository->save($invoice, InvoiceFactory::create($this->company->id, $this->setUser($record)));
|
||||
|
||||
@ -280,6 +271,8 @@ info(print_r($items,1));
|
||||
//clients
|
||||
$records = $this->getCsvData();
|
||||
|
||||
info(print_r($this->column_map,1));
|
||||
|
||||
$contact_repository = new ClientContactRepository();
|
||||
$client_repository = new ClientRepository($contact_repository);
|
||||
$client_transformer = new ClientTransformer($this->maps);
|
||||
|
Loading…
x
Reference in New Issue
Block a user