diff --git a/app/Http/Controllers/SelfUpdateController.php b/app/Http/Controllers/SelfUpdateController.php index bb38bcdb3522..c905d4d39345 100644 --- a/app/Http/Controllers/SelfUpdateController.php +++ b/app/Http/Controllers/SelfUpdateController.php @@ -73,7 +73,6 @@ class SelfUpdateController extends BaseController info($e->getMessage()); return response()->json(['message'=>$e->getMessage()], 500); } - info('Are there any changes to pull? '.$repo->hasChanges()); dispatch(function () { Artisan::call('ninja:post-update'); diff --git a/app/Import/Definitions/ProductMap.php b/app/Import/Definitions/ProductMap.php new file mode 100644 index 000000000000..d79582852819 --- /dev/null +++ b/app/Import/Definitions/ProductMap.php @@ -0,0 +1,57 @@ + 'product.product_key', + 1 => 'product.notes', + 2 => 'product.cost', + 3 => 'product.price', + 4 => 'product.quantity', + 5 => 'product.tax_name1', + 6 => 'product.tax_rate1', + 7 => 'product.tax_name2', + 8 => 'product.tax_rate2', + 9 => 'product.tax_name3', + 10 => 'product.tax_rate3', + 11 => 'product.custom_value1', + 12 => 'product.custom_value2', + 13 => 'product.custom_value3', + 14 => 'product.custom_value4', + ]; + } + + public static function import_keys() + { + return [ + 0 => 'texts.item', + 1 => 'texts.notes', + 2 => 'texts.cost', + 3 => 'texts.price', + 4 => 'texts.quantity', + 5 => 'texts.tax_name', + 6 => 'texts.tax_rate', + 7 => 'texts.tax_name', + 8 => 'texts.tax_rate', + 9 => 'texts.tax_name', + 10 => 'texts.tax_rate', + 11 => 'texts.custom_value', + 12 => 'texts.custom_value', + 13 => 'texts.custom_value', + 14 => 'texts.custom_value', + ]; + } +} diff --git a/app/Import/Transformers/ProductTransformer.php b/app/Import/Transformers/ProductTransformer.php new file mode 100644 index 000000000000..6069bb51b0ac --- /dev/null +++ b/app/Import/Transformers/ProductTransformer.php @@ -0,0 +1,38 @@ + $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'), + 'tax_name1' => $this->getString($data, 'product.tax_name1'), + 'tax_rate1' => $this->getString($data, 'product.tax_rate1'), + 'tax_name2' => $this->getString($data, 'product.tax_name2'), + 'tax_rate2' => $this->getString($data, 'product.tax_rate2'), + 'tax_name3' => $this->getString($data, 'product.tax_name3'), + 'tax_rate3' => $this->getString($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'), + 'custom_value4' => $this->getString($data, 'product.custom_value4'), + ]; + } +} diff --git a/app/Jobs/Import/CSVImport.php b/app/Jobs/Import/CSVImport.php index c3b143e5f5a9..aad48c74e5d1 100644 --- a/app/Jobs/Import/CSVImport.php +++ b/app/Jobs/Import/CSVImport.php @@ -12,8 +12,11 @@ namespace App\Jobs\Import; use App\Factory\ClientFactory; +use App\Factory\ProductFactory; use App\Http\Requests\Client\StoreClientRequest; +use App\Http\Requests\Product\StoreProductRequest; use App\Import\Transformers\ClientTransformer; +use App\Import\Transformers\ProductTransformer; use App\Libraries\MultiDB; use App\Models\Client; use App\Models\Company; @@ -21,6 +24,7 @@ use App\Models\Currency; use App\Models\User; use App\Repositories\ClientContactRepository; use App\Repositories\ClientRepository; +use App\Repositories\ProductRepository; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -54,27 +58,6 @@ class CSVImport implements ShouldQueue public $maps; - /* - [hash] => 2lTm7HVR3i9Zv3y86eQYZIO16yVJ7J6l - [entity_type] => client - [skip_header] => 1 - [column_map] => Array - ( - [0] => client.name - [1] => client.user_id - [2] => client.balance - [3] => client.paid_to_date - [4] => client.address1 - [5] => client.address2 - [6] => client.city - [7] => client.state - [8] => client.postal_code - [9] => client.country_id - [20] => client.currency_id - [21] => client.public_notes - [22] => client.private_notes - ) - */ public function __construct(array $request, Company $company) { $this->company = $company; @@ -106,6 +89,51 @@ class CSVImport implements ShouldQueue //sort the array by key ksort($this->column_map); + $this->{"import".ucfirst($this->entity_type)}(); + } + + public function failed($exception) + { + } + + ////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + private function importProduct() + { + $product_repository = new ProductRepository(); + $product_transformer new ProductTransformer($this->maps); + + $records = $this->getCsvData(); + + if ($this->skip_header) + array_shift($records); + + foreach ($records as $record) + { + $keys = $this->column_map; + $values = array_intersect_key($record, $this->column_map); + + $product_data = array_combine($keys, $values); + + $product = $product_transformer->transform($product_data); + + $validator = Validator::make($client, (new StoreProductRequest())->rules()); + + if ($validator->fails()) { + $this->error_array[] = ['product' => $product, 'error' => json_encode($validator->errors())]; + } else { + $product = $product_repository->save($product, ProductFactory::create($this->company->id, $this->setUser($record))); + + $product->save(); + + $this->maps['products'][] = $product->id; + } + } + } + + //todo limit client imports for hosted version + private function importClient() + { //clients $records = $this->getCsvData(); @@ -113,9 +141,8 @@ class CSVImport implements ShouldQueue $client_repository = new ClientRepository($contact_repository); $client_transformer = new ClientTransformer($this->maps); - if ($this->skip_header) { + if ($this->skip_header) array_shift($records); - } foreach ($records as $record) { $keys = $this->column_map; @@ -142,21 +169,19 @@ class CSVImport implements ShouldQueue $client->save(); - $this->import_array['clients'][] = $client->id; + $this->maps['clients'][] = $client->id; } } } - public function failed($exception) - { - } - ////////////////////////////////////////////////////////////////////////////////////////////////////////////// private function buildMaps() { $this->maps['currencies'] = Currency::all(); $this->maps['users'] = $this->company->users; $this->maps['company'] = $this->company; + $this->maps['clients'] = []; + $this->maps['products'] = []; return $this; } diff --git a/app/Services/Invoice/HandleRestore.php b/app/Services/Invoice/HandleRestore.php index db7aedd9ac03..f3d7eff21a75 100644 --- a/app/Services/Invoice/HandleRestore.php +++ b/app/Services/Invoice/HandleRestore.php @@ -94,6 +94,9 @@ class HandleRestore extends AbstractService $new_invoice_number = substr($this->invoice->number, 0, $pos); + if(strlen($new_invoice_number) == 0) + $new_invoice_number = null; + try { $this->invoice->number = $new_invoice_number; $this->invoice->save(); diff --git a/app/Transformers/ProjectTransformer.php b/app/Transformers/ProjectTransformer.php index 76ac59224a10..c30d25cd9b23 100644 --- a/app/Transformers/ProjectTransformer.php +++ b/app/Transformers/ProjectTransformer.php @@ -48,7 +48,7 @@ class ProjectTransformer extends EntityTransformer 'assigned_user_id' => (string) $this->encodePrimaryKey($project->assigned_user_id), 'client_id' => (string) $this->encodePrimaryKey($project->client_id), 'name' => $project->name ?: '', - 'number' => $project->number, + 'number' => $project->number ?: '', 'created_at' => (int) $project->created_at, 'updated_at' => (int) $project->updated_at, 'archived_at' => (int) $project->deleted_at,