diff --git a/app/Console/Commands/CreateSingleAccount.php b/app/Console/Commands/CreateSingleAccount.php index b51415233ce8..0c1d8fa4477a 100644 --- a/app/Console/Commands/CreateSingleAccount.php +++ b/app/Console/Commands/CreateSingleAccount.php @@ -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, ]); } diff --git a/app/Jobs/Company/CompanyExport.php b/app/Jobs/Company/CompanyExport.php index 3cda0e6ad2cf..75b3b98aac5d 100644 --- a/app/Jobs/Company/CompanyExport.php +++ b/app/Jobs/Company/CompanyExport.php @@ -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(); diff --git a/app/Jobs/Company/CompanyImport.php b/app/Jobs/Company/CompanyImport.php index 1d411abf5a4e..4977b11368a6 100644 --- a/app/Jobs/Company/CompanyImport.php +++ b/app/Jobs/Company/CompanyImport.php @@ -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], diff --git a/app/Models/PurchaseOrder.php b/app/Models/PurchaseOrder.php index d34062b513e5..33077d3b937a 100644 --- a/app/Models/PurchaseOrder.php +++ b/app/Models/PurchaseOrder.php @@ -32,7 +32,6 @@ class PurchaseOrder extends BaseModel protected $fillable = [ 'number', 'discount', - 'company_id', 'status_id', 'last_sent_date', 'is_deleted',