improved contact migration

This commit is contained in:
denisdulici 2020-03-03 23:59:26 +03:00
parent dd0694b828
commit 29483d8c0a

View File

@ -253,6 +253,8 @@ class Version200 extends Listener
public function copyCustomers() public function copyCustomers()
{ {
$contacts = [];
$customers = DB::table('customers')->cursor(); $customers = DB::table('customers')->cursor();
$has_estimates = Schema::hasTable('estimates'); $has_estimates = Schema::hasTable('estimates');
@ -268,21 +270,28 @@ class Version200 extends Listener
$contact_id = DB::table('contacts')->insertGetId($data); $contact_id = DB::table('contacts')->insertGetId($data);
$contacts[$customer->id] = $contact_id;
}
$contacts = array_reverse($contacts, true);
foreach ($contacts as $customer_id => $contact_id) {
DB::table('invoices') DB::table('invoices')
->where('contact_id', $customer->id) ->where('contact_id', $customer_id)
->update([ ->update([
'contact_id' => $contact_id, 'contact_id' => $contact_id,
]); ]);
DB::table('revenues') DB::table('revenues')
->where('customer_id', $customer->id) ->where('customer_id', $customer_id)
->update([ ->update([
'customer_id' => $contact_id, 'customer_id' => $contact_id,
]); ]);
if ($has_estimates) { if ($has_estimates) {
DB::table('estimates') DB::table('estimates')
->where('customer_id', $customer->id) ->where('customer_id', $customer_id)
->update([ ->update([
'customer_id' => $contact_id, 'customer_id' => $contact_id,
]); ]);
@ -290,7 +299,7 @@ class Version200 extends Listener
if ($has_crm_companies) { if ($has_crm_companies) {
DB::table('crm_companies') DB::table('crm_companies')
->where('core_customer_id', $customer->id) ->where('core_customer_id', $customer_id)
->update([ ->update([
'core_customer_id' => $contact_id, 'core_customer_id' => $contact_id,
]); ]);
@ -298,7 +307,7 @@ class Version200 extends Listener
if ($has_crm_contacts) { if ($has_crm_contacts) {
DB::table('crm_contacts') DB::table('crm_contacts')
->where('core_customer_id', $customer->id) ->where('core_customer_id', $customer_id)
->update([ ->update([
'core_customer_id' => $contact_id, 'core_customer_id' => $contact_id,
]); ]);
@ -306,7 +315,7 @@ class Version200 extends Listener
if ($has_idea_soft_histories) { if ($has_idea_soft_histories) {
DB::table('idea_soft_histories') DB::table('idea_soft_histories')
->where('model_id', $customer->id) ->where('model_id', $customer_id)
->where('model_type', 'App\Models\Income\Customer') ->where('model_type', 'App\Models\Income\Customer')
->update([ ->update([
'model_id' => $contact_id, 'model_id' => $contact_id,
@ -316,7 +325,7 @@ class Version200 extends Listener
if ($has_custom_fields_field_values) { if ($has_custom_fields_field_values) {
DB::table('custom_fields_field_values') DB::table('custom_fields_field_values')
->where('model_id', $customer->id) ->where('model_id', $customer_id)
->where('model_type', 'App\Models\Income\Customer') ->where('model_type', 'App\Models\Income\Customer')
->update([ ->update([
'model_id' => $contact_id, 'model_id' => $contact_id,
@ -330,6 +339,8 @@ class Version200 extends Listener
public function copyVendors() public function copyVendors()
{ {
$contacts = [];
$vendors = DB::table('vendors')->cursor(); $vendors = DB::table('vendors')->cursor();
$has_custom_fields_field_values = Schema::hasTable('custom_fields_field_values'); $has_custom_fields_field_values = Schema::hasTable('custom_fields_field_values');
@ -341,20 +352,26 @@ class Version200 extends Listener
$contact_id = DB::table('contacts')->insertGetId($data); $contact_id = DB::table('contacts')->insertGetId($data);
$contacts[$vendor->id] = $contact_id;
}
$contacts = array_reverse($contacts, true);
foreach ($contacts as $vendor_id => $contact_id) {
DB::table('bills') DB::table('bills')
->where('contact_id', $vendor->id) ->where('contact_id', $vendor_id)
->update([ ->update([
'contact_id' => $contact_id, 'contact_id' => $contact_id,
]); ]);
DB::table('payments') DB::table('payments')
->where('vendor_id', $vendor->id) ->where('vendor_id', $vendor_id)
->update([ ->update([
'vendor_id' => $contact_id, 'vendor_id' => $contact_id,
]); ]);
DB::table('mediables') DB::table('mediables')
->where('mediable_id', $vendor->id) ->where('mediable_id', $vendor_id)
->where('mediable_type', 'App\Models\Expense\Vendor') ->where('mediable_type', 'App\Models\Expense\Vendor')
->update([ ->update([
'mediable_id' => $contact_id, 'mediable_id' => $contact_id,
@ -363,7 +380,7 @@ class Version200 extends Listener
if ($has_custom_fields_field_values) { if ($has_custom_fields_field_values) {
DB::table('custom_fields_field_values') DB::table('custom_fields_field_values')
->where('model_id', $vendor->id) ->where('model_id', $vendor_id)
->where('model_type', 'App\Models\Expense\Vendor') ->where('model_type', 'App\Models\Expense\Vendor')
->update([ ->update([
'model_id' => $contact_id, 'model_id' => $contact_id,