Company Import|Export for purchase orders

This commit is contained in:
David Bomba 2022-07-16 12:26:55 +10:00
parent 76b307d20f
commit 5246846018
4 changed files with 106 additions and 6 deletions

View File

@ -400,6 +400,7 @@ class CreateSingleAccount extends Command
$vendor = Project::factory()->create([
'user_id' => $client->user->id,
'company_id' => $client->company->id,
'client_id' => $client->id,
]);
}

View File

@ -20,6 +20,7 @@ use App\Mail\DownloadInvoices;
use App\Models\Company;
use App\Models\CreditInvitation;
use App\Models\InvoiceInvitation;
use App\Models\PurchaseOrderInvitation;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoice;
use App\Models\RecurringInvoiceInvitation;
@ -32,8 +33,8 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Storage;
class CompanyExport implements ShouldQueue
{
@ -424,9 +425,9 @@ class CompanyExport implements ShouldQueue
$this->export_data['vendor_contacts'] = VendorContact::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($vendor){
$vendor = $this->transformBasicEntities($vendor);
$vendor->vendor_id = $this->encodePrimaryKey($vendor->vendor_id);
$vendor = $this->transformArrayOfKeys($vendor, ['vendor_id']);
return $vendor->makeVisible(['id']);
return $vendor->makeVisible(['id','user_id']);
})->all();
@ -439,6 +440,31 @@ class CompanyExport implements ShouldQueue
})->makeHidden(['id'])->all();
$this->export_data['purchase_orders'] = $this->company->purchase_orders()->orderBy('number', 'DESC')->cursor()->map(function ($purchase_order){
$purchase_order = $this->transformBasicEntities($purchase_order);
$purchase_order = $this->transformArrayOfKeys($purchase_order, ['expense_id','client_id', 'vendor_id', 'project_id', 'design_id', 'subscription_id','project_id']);
return $purchase_order->makeVisible(['id',
'private_notes',
'user_id',
'client_id',
'vendor_id',
'company_id',]);
})->all();
$this->export_data['purchase_order_invitations'] = PurchaseOrderInvitation::where('company_id', $this->company->id)->withTrashed()->cursor()->map(function ($purchase_order){
$purchase_order = $this->transformArrayOfKeys($purchase_order, ['company_id', 'user_id', 'vendor_contact_id', 'purchase_order_id']);
return $purchase_order->makeVisible(['id']);
})->all();
//write to tmp and email to owner();
$this->zipAndSend();

View File

@ -45,6 +45,8 @@ use App\Models\PaymentTerm;
use App\Models\Paymentable;
use App\Models\Product;
use App\Models\Project;
use App\Models\PurchaseOrder;
use App\Models\PurchaseOrderInvitation;
use App\Models\Quote;
use App\Models\QuoteInvitation;
use App\Models\RecurringExpense;
@ -74,7 +76,6 @@ use Illuminate\Support\Str;
use JsonMachine\JsonDecoder\ExtJsonDecoder;
use JsonMachine\JsonMachine;
use ZipArchive;
use function GuzzleHttp\json_encode;
class CompanyImport implements ShouldQueue
@ -122,6 +123,7 @@ class CompanyImport implements ShouldQueue
'clients',
'client_contacts',
'vendors',
'vendor_contacts',
'projects',
'products',
'company_gateways',
@ -147,6 +149,8 @@ class CompanyImport implements ShouldQueue
'documents',
'webhooks',
'system_logs',
'purchase_orders',
'purchase_order_invitations'
];
private $company_properties = [
@ -454,7 +458,7 @@ class CompanyImport implements ShouldQueue
$settings->ticket_number_counter = 1;
$settings->payment_number_counter = 1;
$settings->project_number_counter = 1;
$settings->purchase_order_counter = 1;
$this->company->settings = $co->settings;
// $this->company->settings = $this->backup_file->company->settings;
$this->company->save();
@ -471,6 +475,7 @@ class CompanyImport implements ShouldQueue
$this->company->vendors()->forceDelete();
$this->company->expenses()->forceDelete();
$this->company->subscriptions()->forceDelete();
$this->company->purchase_orders()->forceDelete();
$this->company->save();
@ -649,6 +654,19 @@ class CompanyImport implements ShouldQueue
return $this;
}
private function import_vendor_contacts()
{
$this->genericImport(VendorContact::class,
['user_id', 'company_id', 'id', 'hashed_id','company','assigned_user_id'],
[['users' => 'user_id'], ['vendors' => 'vendor_id']],
'vendor_contacts',
'email');
return $this;
}
private function import_projects()
{
@ -796,6 +814,42 @@ class CompanyImport implements ShouldQueue
return $this;
}
private function import_purchase_orders()
{
$this->genericImport(PurchaseOrder::class,
['user_id', 'company_id', 'id', 'hashed_id', 'recurring_id','status', 'vendor_id', 'subscription_id','client_id'],
[
['users' => 'user_id'],
['users' => 'assigned_user_id'],
['recurring_invoices' => 'recurring_id'],
['projects' => 'project_id'],
['vendors' => 'vendor_id'],
],
'purchase_orders',
'number');
return $this;
}
private function import_purchase_order_invitations()
{
$this->genericImport(PurchaseOrderInvitation::class,
['user_id', 'vendor_contact_id', 'company_id', 'id', 'hashed_id', 'purchase_order_id'],
[
['users' => 'user_id'],
['purchase_orders' => 'purchase_order_id'],
['vendor_contacts' => 'vendor_contact_id'],
],
'purchase_order_invitations',
'key');
return $this;
}
private function import_quotes()
{
@ -1425,6 +1479,13 @@ class CompanyImport implements ShouldQueue
$new_obj->save(['timestamps' => false]);
$new_obj->number = $this->getNextInvoiceNumber($client = Client::withTrashed()->find($obj_array['client_id']),$new_obj);
}
elseif($class == 'App\Models\PurchaseOrder' && is_null($obj->{$match_key})){
$new_obj = new PurchaseOrder();
$new_obj->company_id = $this->company->id;
$new_obj->fill($obj_array);
$new_obj->save(['timestamps' => false]);
$new_obj->number = $this->getNextPurchaseOrderNumber($new_obj);
}
elseif($class == 'App\Models\Payment' && is_null($obj->{$match_key})){
$new_obj = new Payment();
$new_obj->company_id = $this->company->id;
@ -1445,6 +1506,12 @@ class CompanyImport implements ShouldQueue
$new_obj->fill($obj_array);
$new_obj->save(['timestamps' => false]);
}
elseif($class == 'App\Models\VendorContact'){
$new_obj = new VendorContact();
$new_obj->company_id = $this->company->id;
$new_obj->fill($obj_array);
$new_obj->save(['timestamps' => false]);
}
elseif($class == 'App\Models\RecurringExpense' && is_null($obj->{$match_key})){
$new_obj = new RecurringExpense();
$new_obj->company_id = $this->company->id;
@ -1466,6 +1533,13 @@ class CompanyImport implements ShouldQueue
$new_obj->save(['timestamps' => false]);
$new_obj->number = $this->getNextTaskNumber($new_obj);
}
elseif($class == 'App\Models\Vendor' && is_null($obj->{$match_key})){
$new_obj = new Vendor();
$new_obj->company_id = $this->company->id;
$new_obj->fill($obj_array);
$new_obj->save(['timestamps' => false]);
$new_obj->number = $this->getNextVendorNumber($new_obj);
}
elseif($class == 'App\Models\CompanyLedger'){
$new_obj = $class::firstOrNew(
[$match_key => $obj->{$match_key}, 'company_id' => $this->company->id],

View File

@ -32,7 +32,6 @@ class PurchaseOrder extends BaseModel
protected $fillable = [
'number',
'discount',
'company_id',
'status_id',
'last_sent_date',
'is_deleted',