protected function substactNotAllowedFields($intAssetModelId, $intCustomFieldId = null) { $arrAllowed = array(); foreach (AssetCustomFieldAssetModel::LoadArrayByAssetModelId($intAssetModelId) as $objAssetCustomField) { $arrAllowed[] = $objAssetCustomField->CustomFieldId; } $arrToClear = array(); foreach (EntityQtypeCustomField::LoadArrayByEntityQtypeId(1) as $objAssetCustomField) { if (!in_array($objAssetCustomField->CustomFieldId, $arrAllowed) && $objAssetCustomField->CustomField->AllAssetModelsFlag != 1) { $arrToClear[] = $objAssetCustomField->CustomFieldId; } } if ($intCustomFieldId) { if ($arrToClear && count($arrToClear) > 0) { foreach ($arrToClear as $idToBeNull) { $arrForQuery[] = sprintf("`cfv_%s`= NULL", $idToBeNull); } return sprintf("UPDATE `asset_custom_field_helper` SET %s WHERE `asset_id`='%s'", str_replace('""', '"', implode(", ", $arrForQuery)), $intCustomFieldId); } } else { return $arrToClear; } }
public function btnSave_Click($strFormId, $strControlId, $strParameter) { try { // Get an instance of the database $objDatabase = QApplication::$Database[1]; // Begin a MySQL Transaction to be either committed or rolled back $objDatabase->TransactionBegin(); // Generate a new AssetCode based on the MinAssetCode value // This happens whether or not they are creating a new one or editing an existing one if ($this->chkAutoGenerateAssetCode->Checked) { $this->txtAssetCode->Text = Asset::GenerateAssetCode(); } $this->objAsset->AssetCode = $this->txtAssetCode->Text; $this->objAsset->AssetModelId = $this->lstAssetModel->SelectedValue; $blnError = false; // If a new asset is being created if (!$this->blnEditMode) { // Do not allow creation of an asset if asset limit will be exceeded $intAssetLimit = is_numeric(QApplication::$TracmorSettings->AssetLimit) ? QApplication::$TracmorSettings->AssetLimit : false; if (!$this->blnEditMode) { if ($intAssetLimit && Asset::CountActive() >= $intAssetLimit) { $blnError = true; $this->txtAssetCode->Warning = "Your asset limit has been reached."; } } // Check Depreciation fields if (QApplication::$TracmorSettings->DepreciationFlag == '1') { if ($this->chkAssetDepreciation->Checked) { if (!preg_match("/\\b\\d{1,3}(?:,?\\d{3})*(?:\\.\\d{2})?\\b/", $this->txtPurchaseCost->Text) || $this->txtPurchaseCost->Text <= 0) { $blnError = true; $this->txtPurchaseCost->Warning = "Purchase Cost value isn't valid"; } elseif (AssetModel::Load($this->lstAssetModel->SelectedValue)->DepreciationClassId != null) { //print $this->calPurchaseDate->DateTime ."||". $this->txtPurchaseCost->Text."|";exit; $this->objAsset->DepreciationFlag = true; $this->objAsset->PurchaseDate = $this->calPurchaseDate->DateTime; $this->objAsset->PurchaseCost = str_replace(',', '', $this->txtPurchaseCost->Text); } else { $blnError = true; $this->chkAssetDepreciation->Warning = "Chosen Model isn't assigned to any Depreciation Class"; } } } // Check to see if the asset tag already exists $AssetDuplicate = Asset::LoadByAssetCode($this->txtAssetCode->Text); if ($AssetDuplicate) { $blnError = true; $this->txtAssetCode->Warning = "That asset tag is already in use. Please try another."; } if (!$blnError && $this->txtParentAssetCode->Text) { if ($this->txtParentAssetCode->Text != $this->objAsset->AssetCode) { $objParentAsset = Asset::LoadByAssetCode($this->txtParentAssetCode->Text); if (!$objParentAsset) { $blnError = true; $this->txtParentAssetCode->Warning = "That asset tag does not exist. Please try another."; } else { if ($this->chkLockToParent->Checked && $objParentAsset->LocationId != $this->lstLocation->SelectedValue) { // If locking child to parent, make sure assets are at the same location $blnError = true; $this->chkLockToParent->Warning = 'Cannot lock to parent asset at another location.'; } else { if ($this->chkLockToParent->Checked && ($objParentAsset->CheckedOutFlag || $objParentAsset->ReservedFlag || $objParentAsset->ArchivedFlag || $objParentAsset->LocationId == 2 || $objParentAsset->LocationId == 5 || AssetTransaction::PendingTransaction($objParentAsset->AssetId))) { $blnError = true; $this->chkLockToParent->Warning = "Parent asset tag (" . $objParentAsset->AssetCode . ") must not be currently Archived, Checked Out, Pending Shipment, Shipped/TBR, or Reserved."; } else { $this->objAsset->ParentAssetId = $objParentAsset->AssetId; if ($this->chkLockToParent->Checked) { $this->objAsset->LinkedFlag = 1; } } } } } else { $blnError = true; $this->txtParentAssetCode->Warning = "Parent asset tag must not be the same as asset tag. Please try another."; } } else { // If txtParentAssetCode is empty $this->objAsset->LinkedFlag = false; $this->objAsset->ParentAssetId = null; } if (!$blnError) { // Location can only be decided when creating an asset. Otherwise they must conduct a transaction. if (!$this->blnEditMode) { $this->objAsset->LocationId = $this->lstLocation->SelectedValue; } // Save child assets $this->SaveChildAssets(); // Object should be saved only if it is new, to obtain the proper AssetId to add to the custom field tables $this->objAsset->Save(); $this->objParentObject->RefreshChildAssets(); } } // Assign input values to custom fields if (is_array($this->arrCustomFields) && count($this->arrCustomFields) > 0 && !$blnError) { // Save the values from all of the custom field controls to save the asset CustomField::SaveControls($this->objAsset->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objAsset->AssetId, 1); } if ($this->blnEditMode) { // Check to see if the asset tag already exists (and is not the asset tag of the asset that the user is currently editing $AssetDuplicate = Asset::LoadByAssetCode($this->txtAssetCode->Text); if ($AssetDuplicate && $AssetDuplicate->AssetId != $this->objAsset->AssetId) { $blnError = true; $this->txtAssetCode->Warning = "That asset tag is already in use. Please try another."; } if (!$blnError && $this->txtParentAssetCode->Text) { // Check if the parent asset tag is already a child asset of this asset $arrChildAsset = Asset::LoadArrayByParentAssetId($this->objAsset->AssetId); foreach ($arrChildAsset as $objChildAsset) { if ($objChildAsset->AssetCode == $this->txtParentAssetCode->Text) { $blnError = true; $this->txtParentAssetCode->Warning = "Parent asset tag is already a child of this asset. Please try another."; break; } } if (!$blnError) { if ($this->txtParentAssetCode->Text != $this->objAsset->AssetCode) { $objParentAsset = Asset::LoadByAssetCode($this->txtParentAssetCode->Text); if (!$objParentAsset) { $blnError = true; $this->txtParentAssetCode->Warning = "That asset tag does not exist. Please try another."; } else { if ($this->chkLockToParent->Checked && !($this->objAsset->ParentAssetId == $objParentAsset->AssetId && $this->objAsset->LinkedFlag == 1) && $objParentAsset->LocationId != $this->objAsset->LocationId) { // If locking child to parent, make sure assets are at the same location $blnError = true; $this->chkLockToParent->Warning = 'Cannot lock to parent asset at another location.'; } else { if ($this->chkLockToParent->Checked && !($this->objAsset->ParentAssetId == $objParentAsset->AssetId && $this->objAsset->LinkedFlag == 1) && ($objParentAsset->CheckedOutFlag || $objParentAsset->ReservedFlag || $objParentAsset->ArchivedFlag || $objParentAsset->LocationId == 2 || $objParentAsset->LocationId == 5 || AssetTransaction::PendingTransaction($objParentAsset->AssetId))) { $blnError = true; $this->chkLockToParent->Warning = "Parent asset tag (" . $objParentAsset->AssetCode . ") must not be currently Archived, Checked Out, Pending Shipment, Shipped/TBR, or Reserved."; } else { if ($this->chkLockToParent->Checked && !($this->objAsset->ParentAssetId == $objParentAsset->AssetId && $this->objAsset->LinkedFlag == 1) && ($this->objAsset->CheckedOutFlag || $this->objAsset->ReservedFlag || $this->objAsset->ArchivedFlag || $this->objAsset->LocationId == 2 || $this->objAsset->LocationId == 5 || AssetTransaction::PendingTransaction($this->objAsset->AssetId))) { $blnError = true; $this->chkLockToParent->Warning .= "Child asset must not be currently Archived, Checked Out, Pending Shipment, Shipped/TBR, or Reserved."; } else { $this->objAsset->ParentAssetId = $objParentAsset->AssetId; if ($this->chkLockToParent->Checked) { $this->objAsset->LinkedFlag = 1; } else { $this->objAsset->LinkedFlag = 0; } } } } } } else { $blnError = true; $this->txtParentAssetCode->Warning = "Parent asset tag must not be the same as asset tag. Please try another."; } } } else { // If txtParentAssetCode is empty $this->objAsset->LinkedFlag = false; $this->objAsset->ParentAssetId = null; $this->chkLockToParent->Checked = false; } // Check Depreciation fields if (QApplication::$TracmorSettings->DepreciationFlag == '1') { if ($this->chkAssetDepreciation->Checked) { if (!preg_match("/\\b\\d{1,3}(?:,?\\d{3})*(?:\\.\\d{2})?\\b/", $this->txtPurchaseCost->Text) || $this->txtPurchaseCost->Text <= 0) { $blnError = true; $this->txtPurchaseCost->Warning = "Purchase Cost isn't valid"; } elseif (AssetModel::Load($this->lstAssetModel->SelectedValue)->DepreciationClassId != null) { //print $this->calPurchaseDate->DateTime ."||". $this->txtPurchaseCost->Text."|";exit; $this->objAsset->DepreciationFlag = true; $this->objAsset->PurchaseDate = $this->calPurchaseDate->DateTime; $this->objAsset->PurchaseCost = str_replace(',', '', $this->txtPurchaseCost->Text); } else { $blnError = true; $this->chkAssetDepreciation->Warning = "Chosen Model isn't assigned to any Depreciation Class"; } } else { $this->objAsset->DepreciationFlag = false; $this->objAsset->PurchaseDate = null; $this->objAsset->PurchaseCost = null; } } if (!$blnError) { // Update the values of all fields for an Ajax reload $this->UpdateAssetFields(); // Save child assets $this->SaveChildAssets(); // If asset is not new, it must be saved after updating the assetfields $this->objAsset->Save(); // This is called to retrieve the new Modified Date and User $this->objParentObject->SetupAsset($this); // Give the labels their appropriate values before display $this->UpdateAssetLabels(); // This was necessary because it was not saving the changes of a second edit/save in a row // Reload all custom fields $this->objAsset->objCustomFieldArray = CustomField::LoadObjCustomFieldArray(1, $this->blnEditMode, $this->objAsset->AssetId, false, $this->objAsset->AssetModelId); // Update not allowed custom fields set to null $arrAllowed = array(); foreach (AssetCustomFieldAssetModel::LoadArrayByAssetModelId($this->objAsset->AssetModelId) as $objAssetCustomField) { $arrAllowed[] = $objAssetCustomField->CustomFieldId; } $arrToClear = array(); foreach (EntityQtypeCustomField::LoadArrayByEntityQtypeId(1) as $objAssetCustomField) { if (!in_array($objAssetCustomField->CustomFieldId, $arrAllowed) && $objAssetCustomField->CustomField->AllAssetModelsFlag != 1) { $arrToClear[] = $objAssetCustomField->CustomFieldId; } } if ($this->objAsset->AssetId && count($arrToClear)) { $arrForQuery = array(); foreach ($arrToClear as $idToBeNull) { $arrForQuery[] = sprintf("`cfv_%s`= NULL", $idToBeNull); } $objDatabase = Asset::GetDatabase(); $strQuery = sprintf("UPDATE `asset_custom_field_helper` SET %s WHERE `asset_id`='%s'", implode(", ", $arrForQuery), $this->objAsset->AssetId); // print($strQuery); exit; $objDatabase->NonQuery($strQuery); } // Commit the above transactions to the database $objDatabase->TransactionCommit(); // Hide inputs and display labels $this->displayLabels(); // Enable the appropriate transaction buttons $this->EnableTransactionButtons(); $this->objParentObject->RefreshChildAssets(); } } elseif (!$blnError) { // Commit the above transactions to the database $objDatabase->TransactionCommit(); // Reload the edit asset page with the newly created asset $strRedirect = sprintf('asset_edit.php?intAssetId=%s', $this->objAsset->AssetId); QApplication::Redirect($strRedirect); } } catch (QOptimisticLockingException $objExc) { // Rollback the database $objDatabase->TransactionRollback(); // Output the error $this->btnCancel->Warning = sprintf('This asset has been updated by another user. You must <a href="asset_edit.php?intAssetId=%s">Refresh</a> to edit this Asset.', $this->objAsset->AssetId); } }
protected function chkCustomFieldsForAll_Create() { $this->chkCustomFieldsForAllModels = new QCheckBoxList($this); $this->chkCustomFieldsForAllModels->Name = 'Asset Custom Fields for all Models:'; $arrAssetCustomFieldOptions = EntityQtypeCustomField::LoadArrayByEntityQtypeId(QApplication::Translate(EntityQtype::Asset)); if (count($arrAssetCustomFieldOptions) > 0) { if ($this->blnEditMode) { $arrChosenCustomFieldId = array(); $arrChosenCustomField = AssetCustomFieldAssetModel::LoadArrayByAssetModelId($this->objAssetModel->AssetModelId); foreach ($arrChosenCustomField as $objChosenCustomField) { $arrChosenCustomFieldId[] = $objChosenCustomField->CustomFieldId; } } foreach ($arrAssetCustomFieldOptions as $arrAssetCustomFieldOption) { $selected = false; if ($this->blnEditMode) { $selected = in_array($arrAssetCustomFieldOption->CustomField->CustomFieldId, $arrChosenCustomFieldId); } $role = RoleEntityQtypeCustomFieldAuthorization::LoadByRoleIdEntityQtypeCustomFieldIdAuthorizationId(QApplication::$objRoleModule->RoleId, $arrAssetCustomFieldOption->EntityQtypeCustomFieldId, 2); if ($role instanceof RoleEntityQtypeCustomFieldAuthorization) { $role = $role->AuthorizedFlag; } if ($arrAssetCustomFieldOption->CustomField->AllAssetModelsFlag && $arrAssetCustomFieldOption->CustomField->ActiveFlag && (int) $role == 1) { $this->chkCustomFieldsForAllModels->AddItem(new QListItem($arrAssetCustomFieldOption->CustomField->ShortDescription, $arrAssetCustomFieldOption->CustomField->CustomFieldId, true)); } } } $this->chkCustomFieldsForAllModels->Enabled = false; }
/** * This loads the array of custom fields, and their selections and values if an existing entity * If it is a new entity, it only loads the custom fields without values. * * @param integer $intEntityQtypeId e.g., 1 == Asset, 2 == Inventory * @param bool $blnEditMode if creating a new entity or editing an existing one * @param integer $intEntityId e.g., AssetId, InventoryId * @return Array $objCustomFieldArray of CustomField objects */ public static function LoadObjCustomFieldArray($intEntityQtypeId, $blnEditMode, $intEntityId = null, $searchable = false, $intAssetModel = null) { $objExpansionMap[CustomField::ExpandDefaultCustomFieldValue] = true; $objCustomFieldArray = CustomField::LoadArrayByActiveFlagEntity(true, $intEntityQtypeId, null, null, $objExpansionMap, $searchable); if ($objCustomFieldArray && $blnEditMode) { foreach ($objCustomFieldArray as $objCustomField) { $objCustomField->LoadExpandedArrayByEntity($intEntityQtypeId, $intEntityId); } } // Custom Fiedls load for Asset in asset_edit if ($intAssetModel) { $arrCustomFieldsForAssetModel = array(); // Select only if (is_int($intAssetModel)) { $arrAssetCustomFields = AssetCustomFieldAssetModel::LoadArrayByAssetModelId($intAssetModel); foreach ($arrAssetCustomFields as $anAssetCustomField) { $arrCustomFieldsForAssetModel[] = $anAssetCustomField->CustomField->CustomFieldId; } } elseif ($intAssetModel == 'all') { $arrAssetCustomFields = EntityQtypeCustomField::LoadArrayByEntityQtypeId(1); foreach ($arrAssetCustomFields as $anAssetCustomField) { if ($anAssetCustomField->CustomField->AllAssetModelsFlag) { $arrCustomFieldsForAssetModel[] = $anAssetCustomField->CustomField->CustomFieldId; } } } $arrForAssetModel = array(); foreach ($objCustomFieldArray as $objCustomField) { if (in_array($objCustomField->CustomFieldId, $arrCustomFieldsForAssetModel) || $objCustomField->AllAssetModelsFlag == 1) { $arrForAssetModel[] = $objCustomField; } } $objCustomFieldArray = $arrForAssetModel; } if ($objCustomFieldArray) { foreach ($objCustomFieldArray as $objCustomField) { $objEntityQtypeCustomField = EntityQtypeCustomField::LoadByEntityQtypeIdCustomFieldId($intEntityQtypeId, $objCustomField->CustomFieldId); if ($objEntityQtypeCustomField) { $objRoleEntityQtypeCustomFieldAuthorization = RoleEntityQtypeCustomFieldAuthorization::LoadByRoleIdEntityQtypeCustomFieldIdAuthorizationId(QApplication::$objUserAccount->RoleId, $objEntityQtypeCustomField->EntityQtypeCustomFieldId, 1); if ($objRoleEntityQtypeCustomFieldAuthorization) { $objCustomField->objRoleAuthView = $objRoleEntityQtypeCustomFieldAuthorization; } $objRoleEntityQtypeCustomFieldAuthorization2 = RoleEntityQtypeCustomFieldAuthorization::LoadByRoleIdEntityQtypeCustomFieldIdAuthorizationId(QApplication::$objUserAccount->RoleId, $objEntityQtypeCustomField->EntityQtypeCustomFieldId, 2); if ($objRoleEntityQtypeCustomFieldAuthorization2) { $objCustomField->objRoleAuthEdit = $objRoleEntityQtypeCustomFieldAuthorization2; } } } } return $objCustomFieldArray; }
protected function UpdateAssetModelCustomFields() { //$arrAssetCustomFieldsToAdd = array(); // $this->chkAssetCustomFields->SelectedValues; // Generate array of Custom Field values for All Asset Models must be presented in all cases /* $arrAllAssetModelsFlaggedObjects = EntityQtypeCustomField::LoadArrayByEntityQtypeId(QApplication::Translate(EntityQtype::Asset)); $arrAllAssetModelsFlag = array(); foreach ($arrAllAssetModelsFlaggedObjects as $arrAllAssetModelsFlaggedObject){ if ($arrAllAssetModelsFlaggedObject->CustomField->AllAssetModelsFlag){ $arrAllAssetModelsFlag[] = $arrAllAssetModelsFlaggedObject->CustomField->CustomFieldId; } } $arrAssetCustomFieldsToAdd = array_merge($this->chkAssetCustomFields->SelectedValues,$arrAllAssetModelsFlag); */ $arrAssetCustomFieldsToAdd = array_unique($this->chkAssetCustomFields->SelectedValues); // If new asset model add AssetCustomFields for All together with selected if (!$this->blnEditMode) { foreach ($arrAssetCustomFieldsToAdd as $keyAssetCustomField) { $newAssetCustomField = new AssetCustomFieldAssetModel(); $newAssetCustomField->CustomFieldId = $keyAssetCustomField; $newAssetCustomField->AssetModelId = $this->objAssetModel->AssetModelId; $newAssetCustomField->Save(); } } else { $currentAssetCustomFields = AssetCustomFieldAssetModel::LoadArrayByAssetModelId($this->objAssetModel->AssetModelId); foreach ($currentAssetCustomFields as $currentAssetCustomField) { if (!$currentAssetCustomField->CustomField->AllAssetModelsFlag && !in_array($currentAssetCustomField->CustomField->CustomFieldId, $arrAssetCustomFieldsToAdd)) { // If blnEditMode some Assets for this Model can be already assigned and them values // for this custom field must be set to null $arrAssetsAssignedToModel = new Asset(); $arrAssetsAssignedToModel = $arrAssetsAssignedToModel->LoadArrayByAssetModelId($this->objAssetModel->AssetModelId); if (count($arrAssetsAssignedToModel) > 0) { $arrAssetOfModel = array(); foreach ($arrAssetsAssignedToModel as $objAssetAssignedToModel) { array_push($arrAssetOfModel, $objAssetAssignedToModel->AssetId); } $arrAssetOfModel = implode(",", $arrAssetOfModel); //print $arrAssetOfModel; exit; $objDatabase = CustomField::GetDatabase(); $strQuery = sprintf("UPDATE `asset_custom_field_helper` SET `cfv_%s`= NULL WHERE `asset_id` IN({$arrAssetOfModel});", $currentAssetCustomField->CustomFieldId); $objDatabase->NonQuery($strQuery); } // then Delete Associations $currentAssetCustomField->Delete(); } } foreach ($arrAssetCustomFieldsToAdd as $keyAssetCustomField) { $blnToAdd = true; foreach ($currentAssetCustomFields as $currentAssetCustomField) { if ($currentAssetCustomField->CustomField->CustomFieldId == $arrAssetCustomFieldsToAdd) { $blnToAdd = false; } } if ($blnToAdd) { $newAssetCustomField = new AssetCustomFieldAssetModel(); $newAssetCustomField->CustomFieldId = $keyAssetCustomField; $newAssetCustomField->AssetModelId = $this->objAssetModel->AssetModelId; $newAssetCustomField->Save(); // If custom field is required add default value to appropriate Assets $objCustomFieldToAdd = new CustomField(); $objCustomFieldToAdd = $objCustomFieldToAdd->Load($keyAssetCustomField); if ($objCustomFieldToAdd->RequiredFlag) { $arrAssetsAssignedToModel = new Asset(); $arrAssetsAssignedToModel = $arrAssetsAssignedToModel->LoadArrayByAssetModelId($this->objAssetModel->AssetModelId); if (count($arrAssetsAssignedToModel) > 0) { $txtDefaultValue = CustomFieldValue::LoadByCustomFieldValueId($objCustomFieldToAdd->DefaultCustomFieldValueId); $arrAssetOfModel = array(); foreach ($arrAssetsAssignedToModel as $objAssetAssignedToModel) { array_push($arrAssetOfModel, $objAssetAssignedToModel->AssetId); } $arrAssetOfModel = implode(",", $arrAssetOfModel); $objDatabase = CustomField::GetDatabase(); $strQuery = sprintf("UPDATE `asset_custom_field_helper` SET `cfv_%s`= %s WHERE `asset_id` IN({$arrAssetOfModel});", $keyAssetCustomField, $txtDefaultValue); $objDatabase->NonQuery($strQuery); } } // } } } }
/** * Deletes all associated AssetCustomFieldAssetModels * @return void */ public function DeleteAllAssetCustomFieldAssetModels() { if (is_null($this->intAssetModelId)) { throw new QUndefinedPrimaryKeyException('Unable to call UnassociateAssetCustomFieldAssetModel on this unsaved AssetModel.'); } // Get the Database Object for this Class $objDatabase = AssetModel::GetDatabase(); // Journaling if ($objDatabase->JournalingDatabase) { foreach (AssetCustomFieldAssetModel::LoadArrayByAssetModelId($this->intAssetModelId) as $objAssetCustomFieldAssetModel) { $objAssetCustomFieldAssetModel->Journal('DELETE'); } } // Perform the SQL Query $objDatabase->NonQuery(' DELETE FROM `asset_custom_field_asset_model` WHERE `asset_model_id` = ' . $objDatabase->SqlVariable($this->intAssetModelId) . ' '); }