diff --git a/app/Abstracts/Export.php b/app/Abstracts/Export.php index 3e4eb5adb..068d3c2f3 100644 --- a/app/Abstracts/Export.php +++ b/app/Abstracts/Export.php @@ -48,13 +48,18 @@ abstract class Export implements FromCollection, HasLocalePreference, ShouldAuto { $map = []; - $date_fields = ['paid_at', 'invoiced_at', 'billed_at', 'due_at', 'issued_at', 'created_at', 'transferred_at']; + $date_fields = ['paid_at', 'invoiced_at', 'billed_at', 'due_at', 'issued_at', 'transferred_at']; $evil_chars = ['=', '+', '-', '@']; foreach ($this->fields as $field) { $value = $model->$field; + // created_by is equal to the owner id. Therefore, the value in export is owner email. + if ($field == 'created_by') { + $value = $model->owner->email ?? null; + } + if (in_array($field, $date_fields)) { $value = ExcelDate::PHPToExcel(Date::parse($value)->format('Y-m-d')); } diff --git a/app/Abstracts/Import.php b/app/Abstracts/Import.php index c02e7ed84..eb05b9ab0 100644 --- a/app/Abstracts/Import.php +++ b/app/Abstracts/Import.php @@ -39,7 +39,12 @@ abstract class Import implements HasLocalePreference, ShouldQueue, SkipsEmptyRow public function map($row): array { $row['company_id'] = company_id(); - $row['created_by'] = $this->user->id; + + // created_by is equal to the owner id. Therefore, the value in export is owner email. + if (isset($row['created_by'])) { + $row['created_by'] = $this->getCreatedById($row); + } + $row['created_from'] = $this->getSourcePrefix() . 'import'; // Make enabled field integer diff --git a/app/Exports/Sales/Customers.php b/app/Exports/Sales/Customers.php index d464df2dd..08e3a758b 100644 --- a/app/Exports/Sales/Customers.php +++ b/app/Exports/Sales/Customers.php @@ -22,6 +22,8 @@ class Customers extends Export $model->country = $country; + $model->can_login = $model->user_id ? true : false; + return parent::map($model); } @@ -41,6 +43,7 @@ class Customers extends Export 'currency_code', 'reference', 'enabled', + 'can_login', ]; } } diff --git a/app/Imports/Sales/Customers.php b/app/Imports/Sales/Customers.php index 414a982a2..03c02b9c6 100644 --- a/app/Imports/Sales/Customers.php +++ b/app/Imports/Sales/Customers.php @@ -3,6 +3,7 @@ namespace App\Imports\Sales; use App\Abstracts\Import; +use App\Models\Auth\User; use App\Http\Requests\Common\Contact as Request; use App\Models\Common\Contact as Model; @@ -26,6 +27,10 @@ class Customers extends Import $row['currency_code'] = $this->getCurrencyCode($row); $row['user_id'] = null; + if (isset($row['can_login']) && isset($row['email'])) { + $row['user_id'] = User::where('email', $row['email'])->first()?->id ?? null; + } + return $row; } } diff --git a/app/Traits/Import.php b/app/Traits/Import.php index e04b113ec..8681e785c 100644 --- a/app/Traits/Import.php +++ b/app/Traits/Import.php @@ -14,6 +14,7 @@ use App\Jobs\Common\CreateItem; use App\Jobs\Setting\CreateCategory; use App\Jobs\Setting\CreateCurrency; use App\Jobs\Setting\CreateTax; +use App\Models\Auth\User; use App\Models\Banking\Account; use App\Models\Common\Contact; use App\Models\Common\Item; @@ -110,6 +111,17 @@ trait Import return $currency->code; } + public function getCreatedById($row) + { + $user = User::where('email', $row['created_by'])->first(); + + if (! empty($user)) { + return $user->id; + } + + return $this->user->id; + } + public function getDocumentId($row) { $id = isset($row['document_id']) ? $row['document_id'] : null;