1+ <?php
2+
3+ namespace App \Importer ;
4+
5+ use App \Models \AssetModel ;
6+ use App \Models \Depreciation ;
7+ use App \Models \CustomFieldset ;
8+ use Illuminate \Support \Facades \Log ;
9+
10+ /**
11+ * When we are importing users via an Asset/etc import, we use createOrFetchUser() in
12+ * Importer\Importer.php. [ALG]
13+ *
14+ * Class LocationImporter
15+ */
16+ class AssetModelImporter extends ItemImporter
17+ {
18+ protected $ models ;
19+
20+ public function __construct ($ filename )
21+ {
22+ parent ::__construct ($ filename );
23+ }
24+
25+ protected function handle ($ row )
26+ {
27+ parent ::handle ($ row );
28+ $ this ->createAssetModelIfNotExists ($ row );
29+ }
30+
31+ /**
32+ * Create a model if a duplicate does not exist.
33+ * @todo Investigate how this should interact with Importer::createModelIfNotExists
34+ *
35+ * @author A. Gianotto
36+ * @since 6.1.0
37+ * @param array $row
38+ */
39+ public function createAssetModelIfNotExists (array $ row )
40+ {
41+
42+ $ editingAssetModel = false ;
43+ $ assetModel = AssetModel::where ('name ' , '= ' , $ this ->findCsvMatch ($ row , 'name ' ))->first ();
44+
45+ if ($ assetModel ) {
46+ if (! $ this ->updating ) {
47+ $ this ->log ('A matching Model ' .$ this ->item ['name ' ].' already exists ' );
48+ return ;
49+ }
50+
51+ $ this ->log ('Updating Model ' );
52+ $ editingAssetModel = true ;
53+ } else {
54+ $ this ->log ('No Matching Model, Create a new one ' );
55+ $ assetModel = new AssetModel ();
56+ }
57+
58+ // Pull the records from the CSV to determine their values
59+ $ this ->item ['name ' ] = trim ($ this ->findCsvMatch ($ row , 'name ' ));
60+ $ this ->item ['category ' ] = trim ($ this ->findCsvMatch ($ row , 'category ' ));
61+ $ this ->item ['manufacturer ' ] = trim ($ this ->findCsvMatch ($ row , 'manufacturer ' ));
62+ $ this ->item ['min_amt ' ] = trim ($ this ->findCsvMatch ($ row , 'min_amt ' ));
63+ $ this ->item ['model_number ' ] = trim ($ this ->findCsvMatch ($ row , 'model_number ' ));
64+ $ this ->item ['eol ' ] = trim ($ this ->findCsvMatch ($ row , 'eol ' ));
65+ $ this ->item ['notes ' ] = trim ($ this ->findCsvMatch ($ row , 'notes ' ));
66+ $ this ->item ['fieldset ' ] = trim ($ this ->findCsvMatch ($ row , 'fieldset ' ));
67+ $ this ->item ['depreciation ' ] = trim ($ this ->findCsvMatch ($ row , 'depreciation ' ));
68+ $ this ->item ['requestable ' ] = trim (($ this ->fetchHumanBoolean ($ this ->findCsvMatch ($ row , 'requestable ' ))) == 1 ) ? 1 : 0 ;
69+
70+ if (!empty ($ this ->item ['category ' ])) {
71+ if ($ category = $ this ->createOrFetchCategory ($ this ->item ['category ' ])) {
72+ $ this ->item ['category_id ' ] = $ category ;
73+ }
74+ }
75+ if (!empty ($ this ->item ['manufacturer ' ])) {
76+ if ($ manufacturer = $ this ->createOrFetchManufacturer ($ this ->item ['manufacturer ' ])) {
77+ $ this ->item ['manufacturer_id ' ] = $ manufacturer ;
78+ }
79+ }
80+
81+ if (!empty ($ this ->item ['depreciation ' ])) {
82+ if ($ depreciation = $ this ->fetchDepreciation ($ this ->item ['depreciation ' ])) {
83+ $ this ->item ['depreciation_id ' ] = $ depreciation ;
84+ }
85+ }
86+
87+ if (!empty ($ this ->item ['fieldset ' ])) {
88+ if ($ fieldset = $ this ->createOrFetchCustomFieldset ($ this ->item ['fieldset ' ])) {
89+ $ this ->item ['fieldset_id ' ] = $ fieldset ;
90+ }
91+ }
92+
93+ Log::debug ('Item array is: ' );
94+ Log::debug (print_r ($ this ->item , true ));
95+
96+
97+ if ($ editingAssetModel ) {
98+ Log::debug ('Updating existing model ' );
99+ $ assetModel ->update ($ this ->sanitizeItemForUpdating ($ assetModel ));
100+ } else {
101+ Log::debug ('Creating model ' );
102+ $ assetModel ->fill ($ this ->sanitizeItemForStoring ($ assetModel ));
103+ $ assetModel ->created_by = auth ()->id ();
104+ }
105+
106+ if ($ assetModel ->save ()) {
107+ $ this ->log ('AssetModel ' .$ assetModel ->name .' created or updated from CSV import ' );
108+ return $ assetModel ;
109+
110+ } else {
111+ $ this ->log ($ assetModel ->getErrors ()->first ());
112+ $ this ->addErrorToBag ($ assetModel , $ assetModel ->getErrors ()->keys ()[0 ], $ assetModel ->getErrors ()->first ());
113+ return $ assetModel ->getErrors ();
114+ }
115+
116+ }
117+
118+
119+ /**
120+ * Fetch an existing depreciation, or create new if it doesn't exist.
121+ *
122+ * We only do a fetch vs create here since Depreciations have additional fields required
123+ * and cannot be created without them (months, for example.))
124+ *
125+ * @author A. Gianotto
126+ * @since 7.1.3
127+ * @param $depreciation_name string
128+ * @return int id of depreciation created/found
129+ */
130+ public function fetchDepreciation ($ depreciation_name ) : ?int
131+ {
132+ if ($ depreciation_name != '' ) {
133+
134+ if ($ depreciation = Depreciation::where ('name ' , '= ' , $ depreciation_name )->first ()) {
135+ $ this ->log ('A matching Depreciation ' .$ depreciation_name .' already exists ' );
136+ return $ depreciation ->id ;
137+ }
138+ }
139+
140+ return null ;
141+ }
142+
143+ /**
144+ * Fetch an existing fieldset, or create new if it doesn't exist
145+ *
146+ * @author A. Gianotto
147+ * @since 7.1.3
148+ * @param $fieldset_name string
149+ * @return int id of fieldset created/found
150+ */
151+ public function createOrFetchCustomFieldset ($ fieldset_name ) : ?int
152+ {
153+ if ($ fieldset_name != '' ) {
154+ $ fieldset = CustomFieldset::where ('name ' , '= ' , $ fieldset_name )->first ();
155+
156+ if ($ fieldset ) {
157+ $ this ->log ('A matching fieldset ' .$ fieldset_name .' already exists ' );
158+ return $ fieldset ->id ;
159+ }
160+
161+ $ fieldset = new CustomFieldset ();
162+ $ fieldset ->name = $ fieldset_name ;
163+
164+ if ($ fieldset ->save ()) {
165+ $ this ->log ('Fieldset ' .$ fieldset_name .' was created ' );
166+
167+ return $ fieldset ->id ;
168+ }
169+ $ this ->logError ($ fieldset , 'Fieldset ' );
170+ }
171+
172+ return null ;
173+ }
174+ }
0 commit comments