예제 #1
0
 public function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     $this->UpdateContactFields();
     $this->objContact->Save();
     // Assign input values to custom fields
     if ($this->arrCustomFields) {
         // Save the values from all of the custom field controls
         CustomField::SaveControls($this->objContact->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objContact->ContactId, EntityQtype::Contact);
     }
     if ($this->ActionParameter) {
         $lstContact = $this->objForm->GetControl($this->ActionParameter);
     } elseif ($this->objForm->lstContact) {
         $lstContact = $this->objForm->lstContact;
     } else {
         $lstContact = null;
     }
     if ($lstContact) {
         $lstContact->AddItem($this->objContact->__toString(), $this->objContact->ContactId);
         $lstContact->SelectedValue = $this->objContact->ContactId;
     }
     $this->ParentControl->RemoveChildControls(true);
     $this->CloseSelf(true);
 }
예제 #2
0
 public function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     $this->UpdateAddressFields();
     $this->objAddress->Save();
     // Assign input values to custom fields
     if ($this->arrCustomFields) {
         // Save the values from all of the custom field controls
         CustomField::SaveControls($this->objAddress->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objAddress->AddressId, EntityQtype::Address);
     }
     if ($this->ActionParameter) {
         $lstAddress = $this->objForm->GetControl($this->ActionParameter);
     } elseif ($this->objForm->lstAddress) {
         $lstAddress = $this->objForm->lstAddress;
     } else {
         $lstAddress = null;
     }
     $lstAddress->AddItem($this->txtShortDescription->Text, $this->objAddress->AddressId);
     $lstAddress->SelectedValue = $this->objAddress->AddressId;
     $this->ParentControl->RemoveChildControls(true);
     $this->CloseSelf(true);
 }
예제 #3
0
 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 to see if the asset code already exists
             $AssetDuplicate = Asset::LoadByAssetCode($this->txtAssetCode->Text);
             if ($AssetDuplicate) {
                 $blnError = true;
                 $this->txtAssetCode->Warning = "That asset code 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 code 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 code (" . $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 code must not be the same as asset code. 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 ($this->arrCustomFields && !$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 code already exists (and is not the asset code 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 code is already in use. Please try another.";
             }
             if (!$blnError && $this->txtParentAssetCode->Text) {
                 // Check if the parent asset code 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 code 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 code 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 code (" . $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 code must not be the same as asset code. Please try another.";
                     }
                 }
             } else {
                 // If txtParentAssetCode is empty
                 $this->objAsset->LinkedFlag = false;
                 $this->objAsset->ParentAssetId = null;
                 $this->chkLockToParent->Checked = false;
             }
             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);
                 // 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);
     }
 }
예제 #4
0
 protected function SaveNewAddress()
 {
     if (!$this->blnEditMode && $this->txtAddressShortDescription->Text) {
         if ($this->objAddress->objCustomFieldArray) {
             $objAddressCustomFieldArray = $this->objAddress->objCustomFieldArray;
         }
         $this->objAddress = new Address();
         $this->objAddress->CompanyId = $this->objCompany->CompanyId;
         $this->objAddress->ShortDescription = $this->txtAddressShortDescription->Text;
         $this->objAddress->Address1 = $this->txtAddress1->Text;
         $this->objAddress->Address2 = $this->txtAddress2->Text;
         $this->objAddress->City = $this->txtCity->Text;
         $this->objAddress->StateProvinceId = $this->lstStateProvince->SelectedValue;
         $this->objAddress->PostalCode = $this->txtPostalCode->Text;
         $this->objAddress->CountryId = $this->lstCountry->SelectedValue;
         $this->objAddress->Save();
         $this->objCompany->AddressId = $this->objAddress->AddressId;
         $this->objCompany->Save();
         if ($this->arrAddressCustomFields) {
             CustomField::SaveControls($objAddressCustomFieldArray, $this->blnEditMode, $this->arrAddressCustomFields, $this->objCompany->Address->AddressId, 9);
         }
     }
 }
예제 #5
0
 public function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     $this->UpdateAssetModelFields();
     $this->objAssetModel->Save();
     // Assign input values to custom fields
     if ($this->arrCustomFields) {
         // Save the values from all of the custom field controls
         CustomField::SaveControls($this->objAssetModel->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objAssetModel->AssetModelId, 4);
     }
     if ($this->ifcImage->FileName) {
         // Retrieve the extension (.jpg, .gif) from the filename
         $explosion = explode(".", $this->ifcImage->FileName);
         // Set the file name to ID_asset_model.ext
         $this->ifcImage->FileName = sprintf('%s%s%s.%s', $this->ifcImage->Prefix, $this->objAssetModel->AssetModelId, $this->ifcImage->Suffix, $explosion[1]);
         // Set the image path for saving the asset model
         $this->txtImagePath->Text = $this->ifcImage->FileName;
         // Upload the file to the server
         $this->ifcImage->ProcessUpload();
         // Save the image path information to the AssetModel object
         $this->objAssetModel->ImagePath = $this->txtImagePath->Text;
         $this->objAssetModel->Save(false, true);
     }
     $lstAssetModel = $this->ParentControl->ParentControl->lstAssetModel;
     $lstAssetModel->AddItem($this->txtShortDescription->Text, $this->objAssetModel->AssetModelId);
     $lstAssetModel->SelectedValue = $this->objAssetModel->AssetModelId;
     $this->ParentControl->ParentControl->lstAssetModel_Select($this->objForm->FormId, $this->ControlId, null);
     $this->ParentControl->RemoveChildControls(true);
     $this->CloseSelf(true);
 }
예제 #6
0
 protected function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     try {
         $this->UpdateAddressFields();
         $this->objAddress->Save();
         // Assign input values to custom fields
         if ($this->arrCustomFields) {
             // Save the values from all of the custom field controls to save the asset
             CustomField::SaveControls($this->objAddress->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objAddress->AddressId, 9);
         }
         if ($this->blnEditMode) {
             $this->UpdateAddressLabels();
             $this->DisplayLabels();
         } elseif (!$this->blnEditMode) {
             QApplication::Redirect('address_edit.php?intAddressId=' . $this->objAddress->AddressId);
         }
     } catch (QExtendedOptimisticLockingException $objExc) {
         $this->btnCancel->Warning = sprintf('This address has been updated by another user. You must <a href="address_edit.php?intAddressId=%s">Refresh</a> to edit this address.', $this->objAddress->AddressId);
     }
 }
 public function btnApply_Click($strFormId, $strControlId, $strParameter)
 {
     $this->EnableSelectedControls();
     $this->ClearWarnings();
     $blnError = false;
     // Make sure at least one checkbox is checked
     if (!$this->chkModel->Checked && !$this->chkParentAssetCode->Checked && !$this->chkChkLockToParent->Checked) {
         $blnChecked = false;
         foreach ($this->arrCheckboxes as $objCheckBox) {
             if ($objCheckBox->Checked) {
                 $blnChecked = true;
                 break;
             }
         }
         if (!$blnChecked) {
             $blnError = true;
             $this->btnCancel->Warning = 'You must select at least one field to edit.';
             return;
         }
     }
     // If Model is checked, make sure a model is selected
     if ($this->chkModel->Checked && $this->lstModel->SelectedValue == null) {
         $blnError = true;
         $this->lstModel->Warning = 'You must select a Model.';
         return;
     }
     // Get an instance of the database
     $objDatabase = QApplication::$Database[1];
     // Begin a MySQL Transaction to be either committed or rolled back
     $objDatabase->TransactionBegin();
     $set = array(sprintf('`modified_by`= %s', QApplication::$objUserAccount->UserAccountId));
     if (count($this->arrCustomFields) > 0) {
         $customFieldIdArray = array();
         foreach ($this->arrCustomFields as $field) {
             if ($this->arrCheckboxes[$field['input']->strControlId]->Checked) {
                 if ($field['input'] instanceof QTextBox && $field['input']->Required && $field['input']->Text == null || $field['input'] instanceof QListBox && $field['input']->Required && $field['input']->SelectedValue == null) {
                     $blnError = true;
                     $field['input']->Warning = "Required.";
                 } else {
                     $this->arrCustomFieldsToEdit[] = $field;
                     $customFieldIdArray[] = (int) str_replace('cf', '', $field['input']->strControlId);
                 }
             }
         }
     }
     foreach ($this->arrAssetToEdit as $intAssetToEditId) {
         $objAsset = Asset::Load($intAssetToEditId);
         // First check that the user is authorized to edit this asset
         if (!QApplication::AuthorizeEntityBoolean($objAsset, 2)) {
             $blnError = true;
             $this->btnCancel->Warning = 'You are not authorized to edit one or more of the selected assets.';
             break;
         }
         if ($this->chkParentAssetCode->Checked && $this->txtParentAssetCode->Text) {
             // Check if the parent asset tag is already a child asset of this asset
             $arrChildAsset = Asset::LoadArrayByParentAssetId($intAssetToEditId);
             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.";
                     break 2;
                 }
             }
             if ($this->txtParentAssetCode->Text != $objAsset->AssetCode) {
                 $objParentAsset = Asset::LoadByAssetCode($this->txtParentAssetCode->Text);
                 if (!$objParentAsset) {
                     $blnError = true;
                     $this->txtParentAssetCode->Warning = "That asset tag does not exist.";
                     break;
                 } else {
                     if ($this->chkLockToParent->Checked && !($objAsset->ParentAssetId == $objParentAsset->AssetId && $objAsset->LinkedFlag == 1) && $objParentAsset->LocationId != $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.';
                         break;
                     } else {
                         if ($this->chkLockToParent->Checked && !($objAsset->ParentAssetId == $objParentAsset->AssetId && $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.";
                             break;
                         } else {
                             if ($this->chkLockToParent->Checked && !($objAsset->ParentAssetId == $objParentAsset->AssetId && $objAsset->LinkedFlag == 1) && ($objAsset->CheckedOutFlag || $objAsset->ReservedFlag || $objAsset->ArchivedFlag || $objAsset->LocationId == 2 || $objAsset->LocationId == 5 || AssetTransaction::PendingTransaction($objAsset->AssetId))) {
                                 $blnError = true;
                                 $this->chkLockToParent->Warning .= "Child asset must not be currently Archived, Checked Out, Pending Shipment, Shipped/TBR, or Reserved.";
                                 break;
                             } else {
                                 $objAsset->ParentAssetId = $objParentAsset->AssetId;
                                 if ($this->chkLockToParent->Checked) {
                                     $objAsset->LinkedFlag = 1;
                                 } else {
                                     $objAsset->LinkedFlag = 0;
                                 }
                             }
                         }
                     }
                 }
             } else {
                 $blnError = true;
                 $this->txtParentAssetCode->Warning = "Asset cannot be assigned as its own parent.";
                 break;
             }
         } else {
             if ($this->chkChkLockToParent->Checked && $this->chkLockToParent->Checked) {
                 // Make sure assets have a parent to lock to if lock is checked and no parent being assigned
                 $objAsset = Asset::Load($intAssetToEditId);
                 if (!$objAsset->ParentAssetId) {
                     $blnError = true;
                     $this->chkLockToParent->Warning = 'Asset cannot be locked without a parent assigned.';
                     break;
                 }
             }
         }
     }
     // Apply checked main_table fields
     if ($this->chkModel->Checked) {
         $set[] = sprintf('`asset_model_id`="%s"', $this->lstModel->SelectedValue);
     }
     if ($this->chkChkLockToParent->Checked) {
         $set[] = sprintf('`linked_flag`=%s', $this->chkLockToParent->Checked ? 1 : "NULL");
     }
     if ($this->chkParentAssetCode->Checked) {
         $parent_asset = Asset::LoadByAssetCode($this->txtParentAssetCode->Text);
         if ($parent_asset instanceof Asset) {
             $parent_asset_id = $parent_asset->AssetId;
         } else {
             $parent_asset_id = "NULL";
             $set[] = sprintf('`linked_flag`=%s', "NULL");
         }
         $set[] = sprintf('`parent_asset_id`=%s', $parent_asset_id);
     }
     // Force modified_date timestamp update
     $set[] = '`modified_date` = NOW()';
     if (!$blnError) {
         try {
             if (count($this->arrCustomFieldsToEdit) > 0) {
                 // preparing data to edit
                 // Save the values from all of the custom field controls to save the asset
                 foreach ($this->arrAssetToEdit as $intAssetId) {
                     $objCustomFieldsArray = CustomField::LoadObjCustomFieldArray(EntityQtype::Asset, false, null, false, 'all');
                     $selectedCustomFieldsArray = array();
                     foreach ($objCustomFieldsArray as $objCustomField) {
                         if (in_array($objCustomField->CustomFieldId, $customFieldIdArray)) {
                             $selectedCustomFieldsArray[] = $objCustomField;
                         }
                     }
                     CustomField::SaveControls($selectedCustomFieldsArray, true, $this->arrCustomFieldsToEdit, $intAssetId, EntityQtype::Asset);
                 }
             }
             // Edit TransAction
             // Update main table
             $strQuery = sprintf("UPDATE `asset`\n\t\t\t\t\t\t\tSET " . implode(",", $set) . " WHERE `asset_id` IN (%s)", implode(",", $this->arrAssetToEdit));
             //print $strQuery; exit;
             $objDatabase->NonQuery($strQuery);
             $objDatabase->TransactionCommit();
             QApplication::Redirect('');
         } catch (QMySqliDatabaseException $objExc) {
             $objDatabase->TransactionRollback();
             throw new QDatabaseException();
         }
     } else {
         $objDatabase->TransactionRollback();
     }
 }
예제 #8
0
 protected function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     try {
         $this->UpdateManufacturerFields();
         $this->objManufacturer->Save();
         // Assign input values to custom fields
         if ($this->arrCustomFields) {
             // Save the values from all of the custom field controls to save the asset
             CustomField::SaveControls($this->objManufacturer->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objManufacturer->ManufacturerId, 5);
         }
         $this->RedirectToListPage();
     } catch (QExtendedOptimisticLockingException $objExc) {
         $this->btnCancel->Warning = sprintf('This manufacturer has been updated by another user. You must <a href="manufacturer_edit.php?intManufacturerId=%s">Refresh</a> to edit this manufacturer.', $this->objManufacturer->ManufacturerId);
     }
 }
예제 #9
0
 protected function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     try {
         /*				if ($this->pnlNewCompany->Visible) {
         			 if (!$this->txtCompanyShortDescription->Text) {
         			 $this->txtCompanyShortDescription->Warning = 'Company Name is a required field.';
         			 return;
         			 }
         			 else {
         			 $this->SaveNewCompany();
         			 }
         				}*/
         $this->UpdateContactFields();
         $this->objContact->Save();
         // Assign input values to custom fields
         if ($this->arrCustomFields) {
             // Save the values from all of the custom field controls to save the asset
             CustomField::SaveControls($this->objContact->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objContact->ContactId, 8);
         }
         if ($this->blnEditMode) {
             //$this->SetupContact();
             $this->UpdateContactLabels();
             $this->DisplayLabels();
         } elseif (!$this->blnEditMode) {
             QApplication::Redirect('company_edit.php?intCompanyId=' . $this->objContact->CompanyId);
         }
     } catch (QExtendedOptimisticLockingException $objExc) {
         $this->btnCancel->Warning = sprintf('This contact has been updated by another user. You must <a href="contact_edit.php?intContactId=%s">Refresh</a> to edit this contact.', $this->objContact->ContactId);
     }
 }
 public function btnApply_Click($strFormId, $strControlId, $strParameter)
 {
     $this->clearWarnings();
     $blnError = false;
     // Get an instance of the database
     $objDatabase = QApplication::$Database[1];
     // Begin a MySQL Transaction to be either committed or rolled back
     $objDatabase->TransactionBegin();
     if (count($this->arrCustomFields) > 0) {
         $customFieldIdArray = array();
         // preparing data to edit
         foreach ($this->arrCustomFields as $field) {
             if ($this->arrCheckboxes[$field['input']->strControlId]->Checked) {
                 if ($field['input'] instanceof QTextBox && $field['input']->Required && $field['input']->Text == null || $field['input'] instanceof QListBox && $field['input']->Required && $field['input']->SelectedValue == null) {
                     $blnError = true;
                     $field['input']->Warning = "Required.";
                 } else {
                     $this->arrCustomFieldsToEdit[] = $field;
                     $customFieldIdArray[] = (int) str_replace('cf', '', $field['input']->strControlId);
                 }
             }
         }
     }
     // Apply checked main_table fields
     $set = array(sprintf('`modified_by`= %s', QApplication::$objUserAccount->UserAccountId));
     if ($this->chkToCompany->Checked) {
         if ($this->lstToContact->SelectedValue) {
             $set[] = sprintf('`to_contact_id`="%s"', $this->lstToContact->SelectedValue);
         } else {
             $this->lstToContact->Warning = 'Contact name must be chosen';
             $blnError = true;
         }
         if ($this->lstToAddress->SelectedValue) {
             $set[] = sprintf('`to_address_id`="%s"', $this->lstToAddress->SelectedValue);
         } else {
             $this->lstToAddress->Warning = 'Address name must be chosen';
             $blnError = true;
         }
     }
     if ($this->chkFromCompany->Checked) {
         if ($this->lstFromCompany->SelectedValue) {
             $set[] = sprintf('`from_company_id`="%s"', $this->lstFromCompany->SelectedValue);
         } else {
             $this->lstFromCompany->Warning = 'Company name must be chosen';
             $blnError = true;
         }
         if ($this->lstFromContact->SelectedValue) {
             $set[] = sprintf('`from_contact_id`="%s"', $this->lstFromContact->SelectedValue);
         } else {
             $this->lstFromContact->Warning = 'Contact name must be chosen';
             $blnError = true;
         }
     }
     if ($this->chkDateReceived->Checked && $this->calDateReceived->DateTime) {
         // Check all receipts are completed
         if (Receipt::QueryCount(QQ::AndCondition(QQ::Equal(QQN::Receipt()->ReceivedFlag, 0), QQ::In(QQN::Receipt()->ReceiptId, $this->arrReceiptToEdit))) > 0) {
             $this->calDateReceived->Warning = 'Can be set only for completed receipts';
             $blnError = true;
         } else {
             $set[] = sprintf('`receipt_date`="%s"', $this->calDateReceived->DateTime->__toString('YYYY-MM-DD'));
         }
     }
     if ($this->chkDateDue->Checked && $this->calDateDue->DateTime) {
         $set[] = sprintf('`due_date`="%s"', $this->calDateDue->DateTime->__toString('YYYY-MM-DD'));
     }
     if (!$blnError) {
         try {
             // Modifying transactions
             foreach ($this->arrReceiptToEdit as $intReceiptId) {
                 $objTransaction = Transaction::Load(Receipt::Load($intReceiptId)->Transaction->TransactionId);
                 $objTransaction->ModifiedBy = QApplication::$objUserAccount->UserAccountId;
                 if ($this->chkNote->Checked) {
                     $objTransaction->Note = $this->txtNote->Text;
                 }
                 $objTransaction->Save();
             }
             if (count($this->arrCustomFieldsToEdit) > 0) {
                 // Save the values from all of the custom field controls to save the asset
                 foreach ($this->arrReceiptToEdit as $intReceiptId) {
                     $objCustomFieldsArray = CustomField::LoadObjCustomFieldArray(EntityQtype::Receipt, false);
                     $selectedCustomFieldsArray = array();
                     foreach ($objCustomFieldsArray as $objCustomField) {
                         if (in_array($objCustomField->CustomFieldId, $customFieldIdArray)) {
                             $selectedCustomFieldsArray[] = $objCustomField;
                         }
                     }
                     CustomField::SaveControls($selectedCustomFieldsArray, true, $this->arrCustomFieldsToEdit, $intReceiptId, EntityQtype::Receipt);
                 }
                 $this->arrCustomFieldsToEdit = array();
             }
             // Update Transaction
             // Update main table
             $strQuery = sprintf("UPDATE `receipt`\n                                     SET " . implode(",", $set) . "\n                                     WHERE `receipt_id` IN (%s)", implode(",", $this->arrReceiptToEdit));
             $objDatabase->NonQuery($strQuery);
             $objDatabase->TransactionCommit();
             QApplication::Redirect('');
         } catch (QMySqliDatabaseException $objExc) {
             $objDatabase->TransactionRollback();
             throw new QDatabaseException();
         }
     } else {
         $objDatabase->TransactionRollback();
         $this->arrCustomFieldsToEdit = array();
         $this->uncheck();
     }
 }
 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();
         // This happens whether or not they are creating a new one or editing an existing one
         $this->objInventoryModel->ShortDescription = $this->txtShortDescription->Text;
         $this->objInventoryModel->CategoryId = $this->lstCategory->SelectedValue;
         $this->objInventoryModel->ManufacturerId = $this->lstManufacturer->SelectedValue;
         $this->objInventoryModel->LongDescription = $this->txtLongDescription->Text;
         $this->objInventoryModel->InventoryModelCode = $this->txtInventoryModelCode->Text;
         $blnError = false;
         // If a new inventory model is being created
         if (!$this->blnEditMode) {
             // Check to see if the InventoryModelCode already exists
             $InventoryModelDuplicate = InventoryModel::LoadbyInventoryModelCode($this->objInventoryModel->InventoryModelCode);
             if ($InventoryModelDuplicate) {
                 $blnError = true;
                 $this->txtInventoryModelCode->Warning = "That inventory code is already in use. Please try another.";
             }
             if (!$blnError) {
                 // Object should be saved only if it is new, to obtain the proper InventoryModelId to add to the custom field tables
                 $this->objInventoryModel->Save();
             }
         }
         // Assign input values to custom fields
         if ($this->arrCustomFields) {
             // Save the values from all of the custom field controls to save the inventory model
             CustomField::SaveControls($this->objInventoryModel->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objInventoryModel->InventoryModelId, 2);
         }
         if ($this->blnEditMode) {
             // Check to see if the InventoryModelCode already exists and it is not the code for the inventory model that you are currently working with
             $InventoryModelDuplicate = InventoryModel::LoadbyInventoryModelCode($this->objInventoryModel->InventoryModelCode);
             if ($InventoryModelDuplicate && $InventoryModelDuplicate->InventoryModelId != $this->objInventoryModel->InventoryModelId) {
                 $blnError = true;
                 $this->txtInventoryModelCode->Warning = "That inventory code is already in use. Please try another.";
             }
             if (!$blnError) {
                 // Update the values of all fields for an Ajax reload
                 $this->UpdateInventoryFields();
                 // If inventory model is not new, it must be saved after updating the inventoryfields
                 $this->objInventoryModel->Save();
                 // Setup the InventoryModel again to retrieve the latest Modified information
                 $this->objParentObject->SetupInventoryModel($this);
                 // Give the labels their appropriate values before display
                 $this->UpdateInventoryLabels();
                 // This was necessary because it was not saving the changes of a second edit/save in a row
                 // Reload all custom fields
                 $this->objInventoryModel->objCustomFieldArray = CustomField::LoadObjCustomFieldArray(2, $this->blnEditMode, $this->objInventoryModel->InventoryModelId);
                 // Hide inputs and display labels
                 $this->displayLabels();
                 // Enable the appropriate transaction buttons
                 $this->EnableTransactionButtons();
                 // Commit the above transactions to the database
                 $objDatabase->TransactionCommit();
             }
         } elseif (!$blnError) {
             // Commit the above transactions to the database
             $objDatabase->TransactionCommit();
             // Reload the edit inventory page with the newly created model
             $strRedirect = "inventory_edit.php?intInventoryModelId=" . $this->objInventoryModel->InventoryModelId;
             QApplication::Redirect($strRedirect);
         }
     } catch (QOptimisticLockingException $objExc) {
         // Rollback the database
         $objDatabase->TransactionRollback();
         // Output the error
         $this->btnCancel->Warning = sprintf('This inventory has been updated by another user. You must <a href="inventory_edit.php?intInventoryModelId=%s">Refresh</a> to edit this Inventory.', $this->objInventoryModel->InventoryModelId);
     }
 }
 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);
     }
 }
 public function btnApply_Click($strFormId, $strControlId, $strParameter)
 {
     $this->clearWarnings();
     $blnError = false;
     $objDatabase = QApplication::$Database[1];
     // Begin a MySQL Transaction to be either committed or rolled back
     $objDatabase->TransactionBegin();
     // Check "Contact To", "Contact From", "Coutrier" wasn't changed for shipped items
     if (Shipment::QueryCount(QQ::AndCondition(QQ::Equal(QQN::Shipment()->ShippedFlag, 1), QQ::In(QQN::Shipment()->ShipmentId, $this->arrShipmentToEdit))) > 0 && ($this->chkToCompany->Checked || $this->chkFromCompany->Checked || $this->chkCourier->Checked)) {
         $this->lblWarning->Text = '"To Company", "From Company", "Courier" shouldn\'t be changed for already
                                     Shipped items';
         $blnError = true;
     }
     if (!$blnError) {
         // Apply checked main_table fields
         $set = array(sprintf('`modified_by`= %s', QApplication::$objUserAccount->UserAccountId));
         if ($this->chkToCompany->Checked) {
             if ($this->lstToCompany->SelectedValue) {
                 $set[] = sprintf('`to_company_id`="%s"', $this->lstToCompany->SelectedValue);
             } else {
                 $this->lstToCompany->Warning = 'Company name must be chosen';
                 $blnError = true;
             }
             if ($this->lstToContact->SelectedValue) {
                 $set[] = sprintf('`to_contact_id`="%s"', $this->lstToContact->SelectedValue);
             } else {
                 $this->lstToContact->Warning = 'Contact name must be chosen';
                 $blnError = true;
             }
             if ($this->lstToAddress->SelectedValue) {
                 $set[] = sprintf('`to_address_id`="%s"', $this->lstToAddress->SelectedValue);
             } else {
                 $this->lstToContact->Warning = 'Address name must be chosen';
                 $blnError = true;
             }
         }
         if ($this->chkFromCompany->Checked) {
             if ($this->lstFromCompany->SelectedValue) {
                 $set[] = sprintf('`from_company_id`="%s"', $this->lstFromCompany->SelectedValue);
             } else {
                 $this->lstFromCompany->Warning = 'Company name must be chosen';
                 $blnError = true;
             }
             if ($this->lstFromContact->SelectedValue) {
                 $set[] = sprintf('`from_contact_id`="%s"', $this->lstFromContact->SelectedValue);
             } else {
                 $this->lstFromContact->Warning = 'Contact name must be chosen';
                 $blnError = true;
             }
             if ($this->lstFromAddress->SelectedValue) {
                 $set[] = sprintf('`from_address_id`="%s"', $this->lstFromAddress->SelectedValue);
             } else {
                 $this->lstFromAddress->Warning = 'Address name must be chosen';
                 $blnError = true;
             }
         }
         if ($this->chkCourier->Checked) {
             $set[] = sprintf('`courier_id`="%s"', $this->lstCourier->SelectedValue);
         }
         if ($this->chkShipDate->Checked && $this->calShipDate->DateTime) {
             $set[] = sprintf('`ship_date`="%s"', $this->calShipDate->DateTime->__toString('YYYY-MM-DD'));
         }
     }
     if (count($this->arrCustomFields) > 0) {
         $customFieldIdArray = array();
         foreach ($this->arrCustomFields as $field) {
             if ($this->arrCheckboxes[$field['input']->strControlId]->Checked) {
                 if ($field['input'] instanceof QTextBox && $field['input']->Required && $field['input']->Text == null || $field['input'] instanceof QListBox && $field['input']->Required && $field['input']->SelectedValue == null) {
                     $blnError = true;
                     $field['input']->Warning = "Required.";
                 } else {
                     $this->arrCustomFieldsToEdit[] = $field;
                     $customFieldIdArray[] = (int) str_replace('cf', '', $field['input']->strControlId);
                 }
             }
         }
     }
     // Apdate main table
     if (!$blnError) {
         try {
             // Edit Transactions
             foreach ($this->arrShipmentToEdit as $intShipmetId) {
                 $objTransaction = Transaction::Load(Shipment::Load($intShipmetId)->Transaction->TransactionId);
                 $objTransaction->ModifiedBy = QApplication::$objUserAccount->UserAccountId;
                 if ($this->chkNote->Checked) {
                     $objTransaction->Note = $this->txtNote->Text;
                 }
                 $objTransaction->Save();
             }
             if (count($this->arrCustomFieldsToEdit) > 0) {
                 // preparing data to edit
                 // Save the values from all of the custom field controls to save the asset
                 foreach ($this->arrShipmentToEdit as $intShipmentId) {
                     $objCustomFieldsArray = CustomField::LoadObjCustomFieldArray(EntityQtype::Shipment, false);
                     $selectedCustomFieldsArray = array();
                     foreach ($objCustomFieldsArray as $objCustomField) {
                         if (in_array($objCustomField->CustomFieldId, $customFieldIdArray)) {
                             $selectedCustomFieldsArray[] = $objCustomField;
                         }
                     }
                     CustomField::SaveControls($selectedCustomFieldsArray, true, $this->arrCustomFieldsToEdit, $intShipmentId, EntityQtype::Shipment);
                 }
             }
             $strQuery = sprintf("UPDATE `shipment`\n                                     SET " . implode(",", $set) . "\n                                     WHERE `shipment_id` IN (%s)", implode(",", $this->arrShipmentToEdit));
             $objDatabase->NonQuery($strQuery);
             $objDatabase->TransactionCommit();
             $this->ParentControl->HideDialogBox();
             QApplication::Redirect('');
         } catch (QMySqliDatabaseException $objExc) {
             $objDatabase->TransactionRollback();
             throw new QDatabaseException();
         }
     } else {
         $objDatabase->TransactionRollback();
         $this->arrCustomFieldsToEdit = array();
         $this->uncheck();
     }
 }
예제 #14
0
 protected function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     $blnError = false;
     if ($this->objAssetTransactionArray && $this->objInventoryTransactionArray) {
         $intEntityQtypeId = EntityQtype::AssetInventory;
     } elseif ($this->objAssetTransactionArray) {
         $intEntityQtypeId = EntityQtype::Asset;
     } elseif ($this->objInventoryTransactionArray) {
         $intEntityQtypeId = EntityQtype::Inventory;
     } else {
         $blnError = true;
         $this->btnCancel->Warning = 'There are no assets or inventory in this shipment.';
     }
     if (QApplication::$TracmorSettings->CustomShipmentNumbers) {
         if ($objShipment = Shipment::LoadByShipmentNumber($this->txtShipmentNumber->Text)) {
             if ($objShipment->ShipmentId != $this->objShipment->ShipmentId) {
                 $blnError = true;
                 $this->txtShipmentNumber->Warning = 'That is a duplicate shipment number.';
             }
         }
     }
     if ($this->lstFxServiceType->SelectedValue) {
         $objtoFxAddress = Address::Load($this->lstToAddress->SelectedValue);
         if (!$objtoFxAddress || !$objtoFxAddress->PostalCode || !$objtoFxAddress->CountryId || !$objtoFxAddress->Address1) {
             $blnError = true;
             $this->lstFxServiceType->Warning = "Not a valid To Address.";
         }
         $objfromFxAddress = Address::Load($this->lstFromAddress->SelectedValue);
         if (!$objfromFxAddress || !$objfromFxAddress->PostalCode || !$objfromFxAddress->Address1 || !$objfromFxAddress->CountryId) {
             $blnError = true;
             $this->lstFxServiceType->Warning = "Not a valid From Address.";
         }
         $objfromFxContact = Contact::Load($this->lstFromContact->SelectedValue);
         if (!$objfromFxContact) {
             $blnError = true;
             $this->lstFxServiceType->Warning = "Not a valid From Contact.";
         }
         $objfromFxCompany = Company::Load($this->lstFromCompany->SelectedValue);
         if (!$objfromFxCompany) {
             $blnError = true;
             $this->lstFxServiceType->Warning = "Not a valid From Company.";
         } elseif (!$objfromFxCompany->Telephone) {
             $blnError = true;
             $this->lstFxServiceType->Warning = "The Shipping Company must have a telephone number.";
         }
         $objShippingAccount = ShippingAccount::Load($this->lstShippingAccount->SelectedValue);
         if (!$objShippingAccount) {
             $blnError = true;
             $this->lstFxServiceType->Warning = "Not a valid Shipping Account.";
         }
         /*				$fed = new FedExDC($objShippingAccount->Value);
         
         				$aRet = $fed->subscribe(
         					array(
         						1 => $this->lblShipmentNumber->Text, // Don't really need this but can be used for ref
         						4003 => $objfromFxContact->__toString(),
         						4008 => $objfromFxAddress->Address1,
         						4009 => $objfromFxAddress->Address2,
         						4011 => $objfromFxAddress->City,
         						4012 => $objfromFxAddress->__toStringStateProvinceAbbreviation(),
         						4013 => $objfromFxAddress->PostalCode,
         						4014 => $objfromFxAddress->__toStringCountryAbbreviation(),
         						4015 => $this->FxStrip($objfromFxCompany->Telephone),
         					)
         				);
         
         				if ($error = $fed->getError()) {
         					$blnError = true;
         					$this->lstFxServiceType->Warning = $error;
         				}
         				elseif (!$aRet[498]) {
         					$blnError = true;
         					$this->lstFxServiceType->Warning = "Fedex response is improperly formed.";
         				}
         				else {
         					$this->objShipment->FedexMeterNumber = $aRet[498];
         				}*/
     }
     if (!$blnError) {
         if (!$this->blnEditMode) {
             try {
                 // Get an instance of the database
                 $objDatabase = QApplication::$Database[1];
                 // Begin a MySQL Transaction to be either committed or rolled back
                 $objDatabase->TransactionBegin();
                 // Create the new transaction object and save it
                 $this->objTransaction = new Transaction();
                 $this->objTransaction->EntityQtypeId = $intEntityQtypeId;
                 $this->objTransaction->TransactionTypeId = 6;
                 $this->objTransaction->Note = $this->txtNote->Text;
                 $this->objTransaction->Save();
                 if ($intEntityQtypeId == EntityQtype::AssetInventory || $intEntityQtypeId == EntityQtype::Asset) {
                     // Assign different source and destinations depending on transaction type
                     foreach ($this->objAssetTransactionArray as $objAssetTransaction) {
                         if ($objAssetTransaction->Asset instanceof Asset) {
                             // Save the asset just to update the modified_date field so it can trigger an Optimistic Locking Exception when appropriate
                             $objAssetTransaction->Asset->Save();
                             // Assign the TransactionId
                             $objAssetTransaction->TransactionId = $this->objTransaction->TransactionId;
                             // Create the new asset if it was scheduled for receipt
                             if ($objAssetTransaction->ScheduleReceiptFlag && $objAssetTransaction->NewAsset && $objAssetTransaction->NewAsset instanceof Asset) {
                                 $objReceiptAsset = new Asset();
                                 $objReceiptAsset->AssetModelId = $objAssetTransaction->NewAsset->AssetModelId;
                                 $objReceiptAsset->LocationId = $objAssetTransaction->NewAsset->LocationId;
                                 //if ($objReceiptAsset->AssetCode == '') {
                                 if ($objAssetTransaction->NewAsset->AssetCode == '') {
                                     $objReceiptAsset->AssetCode = Asset::GenerateAssetCode();
                                 } else {
                                     $objReceiptAsset->AssetCode = $objAssetTransaction->NewAsset->AssetCode;
                                 }
                                 $objReceiptAsset->Save();
                                 // Assign any default custom field values
                                 CustomField::AssignNewEntityDefaultValues(1, $objReceiptAsset->AssetId);
                                 // Associate the new Asset with the AssetTransaction
                                 $objAssetTransaction->NewAsset = $objReceiptAsset;
                             }
                             // $objAssetTransaction->DestinationLocationId = $DestinationLocationId;
                             $objAssetTransaction->Save();
                             /*$objLinkedAssetArray = Asset::LoadChildLinkedArrayByParentAssetId($objAssetTransaction->Asset->AssetId);
                             		if ($objLinkedAssetArray) {
                             		  foreach ($objLinkedAssetArray as $objLinkedAsset) {
                             		    $objLinkedAssetTransaction = new AssetTransaction();
                                						$objLinkedAssetTransaction->AssetId = $objLinkedAsset->AssetId;
                                						$objLinkedAssetTransaction->SourceLocationId = $objLinkedAsset->LocationId;
                                						$objLinkedAssetTransaction->TransactionId = $objAssetTransaction->TransactionId;
                                						$objLinkedAssetTransaction->Save();
                             		  }
                             		}*/
                         }
                     }
                 }
                 if ($intEntityQtypeId == EntityQtype::AssetInventory || $intEntityQtypeId == EntityQtype::Inventory) {
                     // Assign different source and destinations depending on transaction type
                     foreach ($this->objInventoryTransactionArray as $objInventoryTransaction) {
                         // Save the inventory location just to update the modified_date field so it can triggern an Optimistic Locking Exception when appropriate
                         $objInventoryTransaction->InventoryLocation->Save();
                         // Assign the TransactionId
                         $objInventoryTransaction->TransactionId = $this->objTransaction->TransactionId;
                         // $objInventoryTransaction->DestinationLocationId = $DestinationLocationId;
                         $objInventoryTransaction->Save();
                     }
                 }
                 $this->UpdateShipmentFields();
                 $this->objShipment->ShippedFlag = false;
                 $this->objShipment->Save();
                 if ($this->arrCustomFields) {
                     // Save the values from all of the custom field controls to save the shipment
                     CustomField::SaveControls($this->objShipment->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objShipment->ShipmentId, 10);
                 }
                 // If the courier is FedEx, create new fedexShipment
                 if ($this->lstCourier->SelectedValue === 1) {
                     $this->objFedexShipment = new FedexShipment();
                     $this->objFedexShipment->Shipment = $this->objShipment;
                     $this->UpdateFedexFields();
                     $this->objFedexShipment->Save();
                 }
                 $objDatabase->TransactionCommit();
                 QApplication::Redirect('shipment_list.php');
             } catch (QExtendedOptimisticLockingException $objExc) {
                 // Rollback the database
                 $objDatabase->TransactionRollback();
                 if ($objExc->Class == 'Asset') {
                     // $this->btnRemoveAssetTransaction_Click($this->FormId, 'btnRemoveAsset' . $objExc->EntityId, $objExc->EntityId);
                     $this->btnRemoveAssetTransaction_Click($this->FormId, null, $objExc->EntityId);
                     $objAsset = Asset::Load($objExc->EntityId);
                     if ($objAsset) {
                         $this->btnCancel->Warning = sprintf('The Asset %s has been modified by another user and removed from this shipment. You may add the asset again or save the transaction without it.', $objAsset->AssetCode);
                     } else {
                         $this->btnCancel->Warning = 'An Asset has been deleted by another user and removed from this shipment.';
                     }
                 }
                 if ($objExc->Class == 'InventoryLocation') {
                     $this->btnRemoveInventory_Click($this->FormId, 'btnRemoveInventory' . $objExc->EntityId, $objExc->EntityId);
                     $objInventoryLocation = InventoryLocation::Load($objExc->EntityId);
                     if ($objInventoryLocation) {
                         $this->btnCancel->Warning = sprintf('The Inventory %s has been modified by another user and removed from this shipment. You may add the inventory again or save the shipment without it.', $objInventoryLocation->InventoryModel->InventoryModelCode);
                     } else {
                         $this->btnCancel->Warning = 'Inventory has been deleted by another user and removed from this shipment.';
                     }
                 }
             }
         } elseif ($this->blnEditMode) {
             try {
                 // Get an instance of the database
                 $objDatabase = QApplication::$Database[1];
                 // Begin a MySQL Transaction to be either committed or rolled back
                 $objDatabase->TransactionBegin();
                 $this->objTransaction = Transaction::Load($this->objShipment->TransactionId);
                 $this->objTransaction->EntityQtypeId = $intEntityQtypeId;
                 $this->objTransaction->Note = $this->txtNote->Text;
                 $this->objTransaction->Save();
                 // Remove AssetTransactions that were removed when editing
                 if ($this->arrAssetTransactionToDelete) {
                     foreach ($this->arrAssetTransactionToDelete as $intAssetTransactionId) {
                         $objAssetTransactionToDelete = AssetTransaction::Load($intAssetTransactionId);
                         // Make sure that it wasn't added and then removed
                         if ($objAssetTransactionToDelete) {
                             // Change back location
                             $objAssetTransactionToDelete->Asset->LocationId = $objAssetTransactionToDelete->SourceLocationId;
                             $objAssetTransactionToDelete->Asset->Save();
                             // Delete the asset that was created for a new receipt
                             if ($objAssetTransactionToDelete->NewAsset && $objAssetTransactionToDelete->NewAsset instanceof Asset && $objAssetTransactionToDelete->ScheduleReceiptFlag) {
                                 $objAssetTransactionToDelete->NewAsset->Delete();
                             }
                             // Delete the asset transaction
                             $objAssetTransactionToDelete->Delete();
                             unset($objAssetTransactionToDelete);
                         }
                     }
                 }
                 // Save new AssetTransactions
                 if ($this->objAssetTransactionArray) {
                     foreach ($this->objAssetTransactionArray as $objAssetTransaction) {
                         // If the AssetTransaction has not been saved
                         if (!$objAssetTransaction->AssetTransactionId) {
                             $objAssetTransaction->TransactionId = $this->objTransaction->TransactionId;
                             // Save the asset just to update the modified_date field so it can trigger an Optimistic Locking Exception when appropriate
                             $objAssetTransaction->Asset->Save();
                             // Create the new asset if it was scheduled for receipt
                             // $DestinationLocationId = 2; // Shipped
                             // $objAssetTransaction->DestinationLocationId = $DestinationLocationId;
                             // $objAssetTransaction->Asset->LocationId = $DestinationLocationId;
                             // $objAssetTransaction->Asset->Save();
                         }
                         if ($objAssetTransaction->ScheduleReceiptFlag && $objAssetTransaction->NewAsset && $objAssetTransaction->NewAsset instanceof Asset && !$objAssetTransaction->NewAssetId) {
                             $objReceiptAsset = new Asset();
                             $objReceiptAsset->AssetModelId = $objAssetTransaction->NewAsset->AssetModelId;
                             $objReceiptAsset->LocationId = $objAssetTransaction->NewAsset->LocationId;
                             if ($objAssetTransaction->NewAsset->AssetCode == '') {
                                 $objReceiptAsset->AssetCode = Asset::GenerateAssetCode();
                             } else {
                                 $objReceiptAsset->AssetCode = $objAssetTransaction->NewAsset->AssetCode;
                             }
                             $objReceiptAsset->Save();
                             // Assign any default custom field values
                             CustomField::AssignNewEntityDefaultValues(1, $objReceiptAsset->AssetId);
                             // Associate the new Asset with the AssetTransaction
                             $objAssetTransaction->NewAsset = $objReceiptAsset;
                         }
                         $objAssetTransaction->Save();
                     }
                 }
                 // Remove InventoryTransactions
                 if ($this->arrInventoryTransactionToDelete) {
                     foreach ($this->arrInventoryTransactionToDelete as $intInventoryTransactionId) {
                         $objInventoryTransactionToDelete = InventoryTransaction::Load($intInventoryTransactionId);
                         // Make sure that it wasn't added then removed
                         if ($objInventoryTransactionToDelete) {
                             // Change back the quantity
                             $objInventoryTransactionToDelete->InventoryLocation->Quantity += $objInventoryTransactionToDelete->Quantity;
                             $objInventoryTransactionToDelete->InventoryLocation->Save();
                             // Delete the InventoryTransaction
                             $objInventoryTransactionToDelete->Delete();
                             unset($objInventoryTransactionToDelete);
                         }
                     }
                 }
                 // Save InventoryTransactions
                 if ($this->objInventoryTransactionArray) {
                     foreach ($this->objInventoryTransactionArray as $objInventoryTransaction) {
                         if (!$objInventoryTransaction->InventoryTransactionId) {
                             // Reload the InventoryLocation. If it was deleted and added in the same save click, then it will throw an Optimistic Locking Exception
                             $objInventoryTransaction->InventoryLocation = InventoryLocation::Load($objInventoryTransaction->InventoryLocationId);
                             $objInventoryTransaction->TransactionId = $this->objTransaction->TransactionId;
                             // Save the inventory location just to update the modified_date field so it can triggern an Optimistic Locking Exception when appropriate
                             $objInventoryTransaction->InventoryLocation->Save();
                             // $DestinationLocationId = 2; // Shipped
                             // $objInventoryTransaction->DestinationLocationId = $DestinationLocationId;
                             // $objInventoryTransaction->InventoryLocation->Quantity -= $objInventoryTransaction->Quantity;
                             // $objInventoryTransaction->InventoryLocation->Save();
                             $objInventoryTransaction->Save();
                         }
                     }
                 }
                 $this->UpdateShipmentFields();
                 // $this->objShipment->Save(false, true);
                 $this->objShipment->Save();
                 if ($this->arrCustomFields) {
                     // Save the values from all of the custom field controls to save the shipment
                     CustomField::SaveControls($this->objShipment->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objShipment->ShipmentId, 10);
                 }
                 // If the courier is FedEx, save the fedexShipment
                 if ($this->lstCourier->SelectedValue === 1) {
                     if ($this->objFedexShipment) {
                         // FedexShipment already exists, so update it
                         $this->UpdateFedexFields();
                         $this->objFedexShipment->Save();
                     } else {
                         // FedexShipment doesn't exist yet, so create it
                         $this->objFedexShipment = new FedexShipment();
                         $this->objFedexShipment->Shipment = $this->objShipment;
                         $this->UpdateFedexFields();
                         $this->objFedexShipment->Save();
                     }
                 } else {
                     if ($this->objFedexShipment) {
                         // FedexShipment exists - delete it because the selected courier is no longer FedEx
                         $this->objFedexShipment->Delete();
                         $this->objFedexShipment = null;
                     }
                 }
                 $objDatabase->TransactionCommit();
                 $this->UpdateShipmentLabels();
                 $this->SetupShipment();
                 $this->DisplayLabels();
                 if ($this->objShipment->CourierId == 1) {
                     $this->txtTrackingNumber->Enabled = false;
                     $this->lstPackageType->Enabled = true;
                 } else {
                     $this->txtTrackingNumber->Enabled = true;
                     $this->lstPackageType->Enabled = false;
                 }
                 // Reload lstPackageType
                 $this->lstPackageType->RemoveAllItems();
                 $this->LoadPackageTypes();
             } catch (QExtendedOptimisticLockingException $objExc) {
                 $objDatabase->TransactionRollback();
                 if ($objExc->Class == 'Shipment') {
                     $this->btnCancel->Warning = sprintf('This shipment has been modified by another user. You must <a href="shipment_edit.php?intShipmentId=%s">Refresh</a> to edit this shipment.', $this->objShipment->ShipmentId);
                 } elseif ($objExc->Class == 'Asset') {
                     //$this->btnRemoveAssetTransaction_Click($this->FormId, 'btnRemoveAsset' . $objExc->EntityId, $objExc->EntityId);
                     $this->btnRemoveAssetTransaction_Click($this->FormId, null, $objExc->EntityId);
                     $objAsset = Asset::Load($objExc->EntityId);
                     if ($objAsset) {
                         $this->btnCancel->Warning = sprintf('The Asset %s has been modified by another user and removed from this shipment. You may add the asset again or save the transaction without it.', $objAsset->AssetCode);
                     } else {
                         $this->btnCancel->Warning = 'An Asset has been deleted by another user and removed from this shipment.';
                     }
                 } elseif ($objExc->Class == 'InventoryLocation') {
                     $this->btnRemoveInventory_Click($this->FormId, 'btnRemoveInventory' . $objExc->EntityId, $objExc->EntityId);
                     $objInventoryLocation = InventoryLocation::Load($objExc->EntityId);
                     if ($objInventoryLocation) {
                         $this->btnCancel->Warning = sprintf('The Inventory %s has been modified by another user and removed from this shipment. You may add the inventory again or save the shipment without it.', $objInventoryLocation->InventoryModel->InventoryModelCode);
                     } else {
                         $this->btnCancel->Warning = 'Inventory has been deleted by another user and removed from this shipment.';
                     }
                 }
             }
         }
     }
 }
 public function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     $this->clearWarnings();
     $blnError = false;
     // Get an instance of the database
     $objDatabase = QApplication::$Database[1];
     // Begin a MySQL Transaction to be either committed or rolled back
     $objDatabase->TransactionBegin();
     $strQuery = sprintf("\n\t\t\tUPDATE `company`\n\t\t\tSET `long_description`='%s'\n\t\t\tWHERE `company_id` IN (%s)\n\t\t\t", $this->txtLongDescription->Text, implode(",", $this->arrCompaniesToEdit));
     try {
         if (count($this->arrCustomFields) > 0) {
             $customFieldIdArray = array();
             foreach ($this->arrCustomFields as $field) {
                 if ($this->arrCheckboxes[$field['input']->strControlId]->Checked) {
                     if ($field['input'] instanceof QTextBox && $field['input']->Required && $field['input']->Text == null || $field['input'] instanceof QListBox && $field['input']->Required && $field['input']->SelectedValue == null) {
                         $blnError = true;
                         $field['input']->Warning = "Required.";
                     } else {
                         $this->arrCustomFieldsToEdit[] = $field;
                         $customFieldIdArray[] = (int) str_replace('cf', '', $field['input']->strControlId);
                     }
                 }
             }
             if (count($this->arrCustomFieldsToEdit) > 0 && !$blnError) {
                 // preparing data to edit
                 // Save the values from all of the custom field controls to save the asset
                 foreach ($this->arrCompaniesToEdit as $intCompanyId) {
                     $objCustomFieldsArray = CustomField::LoadObjCustomFieldArray(EntityQtype::Company, false);
                     $selectedCustomFieldsArray = array();
                     foreach ($objCustomFieldsArray as $objCustomField) {
                         if (in_array($objCustomField->CustomFieldId, $customFieldIdArray)) {
                             $selectedCustomFieldsArray[] = $objCustomField;
                         }
                     }
                     CustomField::SaveControls($selectedCustomFieldsArray, true, $this->arrCustomFieldsToEdit, $intCompanyId, EntityQtype::Company);
                 }
             }
             if ($this->chkLongDescription->Checked && !$blnError) {
                 $objDatabase->NonQuery($strQuery);
             }
         } else {
             $objDatabase->NonQuery($strQuery);
         }
         $objDatabase->TransactionCommit();
     } catch (QMySqliDatabaseException $objExc) {
         $objDatabase->TransactionRollback();
         throw new QDatabaseException();
     }
     if (!$blnError) {
         $this->ParentControl->RemoveChildControls(true);
         $this->CloseSelf(true);
         QApplication::Redirect('');
     } else {
         $objDatabase->TransactionRollback();
         $this->arrCustomFieldsToEdit = array();
         $this->uncheck();
     }
 }
예제 #16
0
 protected function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     $blnError = false;
     if ($this->objAssetTransactionArray && $this->objInventoryTransactionArray) {
         $intEntityQtypeId = EntityQtype::AssetInventory;
     } elseif ($this->objAssetTransactionArray) {
         $intEntityQtypeId = EntityQtype::Asset;
     } elseif ($this->objInventoryTransactionArray) {
         $intEntityQtypeId = EntityQtype::Inventory;
     } else {
         $blnError = true;
         $this->btnCancel->Warning = 'There are no assets or inventory in this shipment.';
     }
     if (QApplication::$TracmorSettings->CustomShipmentNumbers) {
         if ($objShipment = Shipment::LoadByShipmentNumber($this->txtShipmentNumber->Text)) {
             if ($objShipment->ShipmentId != $this->objShipment->ShipmentId) {
                 $blnError = true;
                 $this->txtShipmentNumber->Warning = 'That is a duplicate shipment number.';
             }
         }
     }
     if (!$blnError) {
         if (!$this->blnEditMode) {
             try {
                 // Get an instance of the database
                 $objDatabase = QApplication::$Database[1];
                 // Begin a MySQL Transaction to be either committed or rolled back
                 $objDatabase->TransactionBegin();
                 // Create the new transaction object and save it
                 $this->objTransaction = new Transaction();
                 $this->objTransaction->EntityQtypeId = $intEntityQtypeId;
                 $this->objTransaction->TransactionTypeId = 6;
                 $this->objTransaction->Note = $this->txtNote->Text;
                 $this->objTransaction->Save();
                 if ($intEntityQtypeId == EntityQtype::AssetInventory || $intEntityQtypeId == EntityQtype::Asset) {
                     // Assign different source and destinations depending on transaction type
                     foreach ($this->objAssetTransactionArray as $objAssetTransaction) {
                         if ($objAssetTransaction->Asset instanceof Asset) {
                             // Save the asset just to update the modified_date field so it can trigger an Optimistic Locking Exception when appropriate
                             $objAssetTransaction->Asset->Save();
                             // Assign the TransactionId
                             $objAssetTransaction->TransactionId = $this->objTransaction->TransactionId;
                             // Create the new asset if it was scheduled for receipt
                             if ($objAssetTransaction->ScheduleReceiptFlag && $objAssetTransaction->NewAsset && $objAssetTransaction->NewAsset instanceof Asset) {
                                 $objReceiptAsset = new Asset();
                                 $objReceiptAsset->AssetModelId = $objAssetTransaction->NewAsset->AssetModelId;
                                 $objReceiptAsset->LocationId = $objAssetTransaction->NewAsset->LocationId;
                                 //if ($objReceiptAsset->AssetCode == '') {
                                 if ($objAssetTransaction->NewAsset->AssetCode == '') {
                                     $objReceiptAsset->AssetCode = Asset::GenerateAssetCode();
                                 } else {
                                     $objReceiptAsset->AssetCode = $objAssetTransaction->NewAsset->AssetCode;
                                 }
                                 $objReceiptAsset->Save();
                                 // Assign any default custom field values
                                 CustomField::AssignNewEntityDefaultValues(1, $objReceiptAsset->AssetId);
                                 // Associate the new Asset with the AssetTransaction
                                 $objAssetTransaction->NewAsset = $objReceiptAsset;
                             }
                             // $objAssetTransaction->DestinationLocationId = $DestinationLocationId;
                             $objAssetTransaction->Save();
                             /*$objLinkedAssetArray = Asset::LoadChildLinkedArrayByParentAssetId($objAssetTransaction->Asset->AssetId);
                             		if ($objLinkedAssetArray) {
                             		  foreach ($objLinkedAssetArray as $objLinkedAsset) {
                             		    $objLinkedAssetTransaction = new AssetTransaction();
                                						$objLinkedAssetTransaction->AssetId = $objLinkedAsset->AssetId;
                                						$objLinkedAssetTransaction->SourceLocationId = $objLinkedAsset->LocationId;
                                						$objLinkedAssetTransaction->TransactionId = $objAssetTransaction->TransactionId;
                                						$objLinkedAssetTransaction->Save();
                             		  }
                             		}*/
                         }
                     }
                 }
                 if ($intEntityQtypeId == EntityQtype::AssetInventory || $intEntityQtypeId == EntityQtype::Inventory) {
                     // Assign different source and destinations depending on transaction type
                     foreach ($this->objInventoryTransactionArray as $objInventoryTransaction) {
                         // Save the inventory location just to update the modified_date field so it can triggern an Optimistic Locking Exception when appropriate
                         $objInventoryTransaction->InventoryLocation->Save();
                         // Assign the TransactionId
                         $objInventoryTransaction->TransactionId = $this->objTransaction->TransactionId;
                         // $objInventoryTransaction->DestinationLocationId = $DestinationLocationId;
                         $objInventoryTransaction->Save();
                     }
                 }
                 $this->UpdateShipmentFields();
                 $this->objShipment->ShippedFlag = false;
                 $this->objShipment->Save();
                 if ($this->arrCustomFields) {
                     // Save the values from all of the custom field controls to save the shipment
                     CustomField::SaveControls($this->objShipment->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objShipment->ShipmentId, 10);
                 }
                 $objDatabase->TransactionCommit();
                 QApplication::Redirect('shipment_list.php');
             } catch (QExtendedOptimisticLockingException $objExc) {
                 // Rollback the database
                 $objDatabase->TransactionRollback();
                 if ($objExc->Class == 'Asset') {
                     // $this->btnRemoveAssetTransaction_Click($this->FormId, 'btnRemoveAsset' . $objExc->EntityId, $objExc->EntityId);
                     $this->btnRemoveAssetTransaction_Click($this->FormId, null, $objExc->EntityId);
                     $objAsset = Asset::Load($objExc->EntityId);
                     if ($objAsset) {
                         $this->btnCancel->Warning = sprintf('The Asset %s has been modified by another user and removed from this shipment. You may add the asset again or save the transaction without it.', $objAsset->AssetCode);
                     } else {
                         $this->btnCancel->Warning = 'An Asset has been deleted by another user and removed from this shipment.';
                     }
                 }
                 if ($objExc->Class == 'InventoryLocation') {
                     $this->btnRemoveInventory_Click($this->FormId, 'btnRemoveInventory' . $objExc->EntityId, $objExc->EntityId);
                     $objInventoryLocation = InventoryLocation::Load($objExc->EntityId);
                     if ($objInventoryLocation) {
                         $this->btnCancel->Warning = sprintf('The Inventory %s has been modified by another user and removed from this shipment. You may add the inventory again or save the shipment without it.', $objInventoryLocation->InventoryModel->InventoryModelCode);
                     } else {
                         $this->btnCancel->Warning = 'Inventory has been deleted by another user and removed from this shipment.';
                     }
                 }
             }
         } elseif ($this->blnEditMode) {
             try {
                 // Get an instance of the database
                 $objDatabase = QApplication::$Database[1];
                 // Begin a MySQL Transaction to be either committed or rolled back
                 $objDatabase->TransactionBegin();
                 $this->objTransaction = Transaction::Load($this->objShipment->TransactionId);
                 $this->objTransaction->EntityQtypeId = $intEntityQtypeId;
                 $this->objTransaction->Note = $this->txtNote->Text;
                 $this->objTransaction->Save();
                 // Remove AssetTransactions that were removed when editing
                 if ($this->arrAssetTransactionToDelete) {
                     foreach ($this->arrAssetTransactionToDelete as $intAssetTransactionId) {
                         $objAssetTransactionToDelete = AssetTransaction::Load($intAssetTransactionId);
                         // Make sure that it wasn't added and then removed
                         if ($objAssetTransactionToDelete) {
                             // Change back location
                             $objAssetTransactionToDelete->Asset->LocationId = $objAssetTransactionToDelete->SourceLocationId;
                             $objAssetTransactionToDelete->Asset->Save();
                             // Delete the asset that was created for a new receipt
                             if ($objAssetTransactionToDelete->NewAsset && $objAssetTransactionToDelete->NewAsset instanceof Asset && $objAssetTransactionToDelete->ScheduleReceiptFlag) {
                                 $objAssetTransactionToDelete->NewAsset->Delete();
                             }
                             // Delete the asset transaction
                             $objAssetTransactionToDelete->Delete();
                             unset($objAssetTransactionToDelete);
                         }
                     }
                 }
                 // Save new AssetTransactions
                 if ($this->objAssetTransactionArray) {
                     foreach ($this->objAssetTransactionArray as $objAssetTransaction) {
                         // If the AssetTransaction has not been saved
                         if (!$objAssetTransaction->AssetTransactionId) {
                             $objAssetTransaction->TransactionId = $this->objTransaction->TransactionId;
                             // Save the asset just to update the modified_date field so it can trigger an Optimistic Locking Exception when appropriate
                             $objAssetTransaction->Asset->Save();
                             // Create the new asset if it was scheduled for receipt
                             // $DestinationLocationId = 2; // Shipped
                             // $objAssetTransaction->DestinationLocationId = $DestinationLocationId;
                             // $objAssetTransaction->Asset->LocationId = $DestinationLocationId;
                             // $objAssetTransaction->Asset->Save();
                         }
                         if ($objAssetTransaction->ScheduleReceiptFlag && $objAssetTransaction->NewAsset && $objAssetTransaction->NewAsset instanceof Asset && !$objAssetTransaction->NewAssetId) {
                             $objReceiptAsset = new Asset();
                             $objReceiptAsset->AssetModelId = $objAssetTransaction->NewAsset->AssetModelId;
                             $objReceiptAsset->LocationId = $objAssetTransaction->NewAsset->LocationId;
                             if ($objAssetTransaction->NewAsset->AssetCode == '') {
                                 $objReceiptAsset->AssetCode = Asset::GenerateAssetCode();
                             } else {
                                 $objReceiptAsset->AssetCode = $objAssetTransaction->NewAsset->AssetCode;
                             }
                             $objReceiptAsset->Save();
                             // Assign any default custom field values
                             CustomField::AssignNewEntityDefaultValues(1, $objReceiptAsset->AssetId);
                             // Associate the new Asset with the AssetTransaction
                             $objAssetTransaction->NewAsset = $objReceiptAsset;
                         }
                         $objAssetTransaction->Save();
                     }
                 }
                 // Remove InventoryTransactions
                 if ($this->arrInventoryTransactionToDelete) {
                     foreach ($this->arrInventoryTransactionToDelete as $intInventoryTransactionId) {
                         $objInventoryTransactionToDelete = InventoryTransaction::Load($intInventoryTransactionId);
                         // Make sure that it wasn't added then removed
                         if ($objInventoryTransactionToDelete) {
                             // Change back the quantity
                             //$objInventoryTransactionToDelete->InventoryLocation->Quantity += $objInventoryTransactionToDelete->Quantity;
                             //$objInventoryTransactionToDelete->InventoryLocation->Save();
                             // Delete the InventoryTransaction
                             $objInventoryTransactionToDelete->Delete();
                             unset($objInventoryTransactionToDelete);
                         }
                     }
                 }
                 // Save InventoryTransactions
                 if ($this->objInventoryTransactionArray) {
                     foreach ($this->objInventoryTransactionArray as $objInventoryTransaction) {
                         if (!$objInventoryTransaction->InventoryTransactionId) {
                             // Reload the InventoryLocation. If it was deleted and added in the same save click, then it will throw an Optimistic Locking Exception
                             $objInventoryTransaction->InventoryLocation = InventoryLocation::Load($objInventoryTransaction->InventoryLocationId);
                             $objInventoryTransaction->TransactionId = $this->objTransaction->TransactionId;
                             // Save the inventory location just to update the modified_date field so it can triggern an Optimistic Locking Exception when appropriate
                             $objInventoryTransaction->InventoryLocation->Save();
                             // $DestinationLocationId = 2; // Shipped
                             // $objInventoryTransaction->DestinationLocationId = $DestinationLocationId;
                             // $objInventoryTransaction->InventoryLocation->Quantity -= $objInventoryTransaction->Quantity;
                             // $objInventoryTransaction->InventoryLocation->Save();
                             $objInventoryTransaction->Save();
                         }
                     }
                 }
                 $this->UpdateShipmentFields();
                 // $this->objShipment->Save(false, true);
                 $this->objShipment->Save();
                 if ($this->arrCustomFields) {
                     // Save the values from all of the custom field controls to save the shipment
                     CustomField::SaveControls($this->objShipment->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objShipment->ShipmentId, 10);
                 }
                 $objDatabase->TransactionCommit();
                 //$this->UpdateShipmentLabels();
                 //$this->SetupShipment();
                 //$this->DisplayLabels();
                 //$this->txtTrackingNumber->Enabled = true;
                 QApplication::Redirect('shipment_edit.php?intShipmentId=' . $this->objShipment->ShipmentId);
             } catch (QExtendedOptimisticLockingException $objExc) {
                 $objDatabase->TransactionRollback();
                 if ($objExc->Class == 'Shipment') {
                     $this->btnCancel->Warning = sprintf('This shipment has been modified by another user. You must <a href="shipment_edit.php?intShipmentId=%s">Refresh</a> to edit this shipment.', $this->objShipment->ShipmentId);
                 } elseif ($objExc->Class == 'Asset') {
                     //$this->btnRemoveAssetTransaction_Click($this->FormId, 'btnRemoveAsset' . $objExc->EntityId, $objExc->EntityId);
                     $this->btnRemoveAssetTransaction_Click($this->FormId, null, $objExc->EntityId);
                     $objAsset = Asset::Load($objExc->EntityId);
                     if ($objAsset) {
                         $this->btnCancel->Warning = sprintf('The Asset %s has been modified by another user and removed from this shipment. You may add the asset again or save the transaction without it.', $objAsset->AssetCode);
                     } else {
                         $this->btnCancel->Warning = 'An Asset has been deleted by another user and removed from this shipment.';
                     }
                 } elseif ($objExc->Class == 'InventoryLocation') {
                     $this->btnRemoveInventory_Click($this->FormId, 'btnRemoveInventory' . $objExc->EntityId, $objExc->EntityId);
                     $objInventoryLocation = InventoryLocation::Load($objExc->EntityId);
                     if ($objInventoryLocation) {
                         $this->btnCancel->Warning = sprintf('The Inventory %s has been modified by another user and removed from this shipment. You may add the inventory again or save the shipment without it.', $objInventoryLocation->InventoryModel->InventoryModelCode);
                     } else {
                         $this->btnCancel->Warning = 'Inventory has been deleted by another user and removed from this shipment.';
                     }
                 }
             }
         }
     }
 }
 public function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     $this->EnableSelectedControls();
     $this->ClearWarnings();
     $blnError = false;
     // Make sure at least one checkbox is checked
     if (!$this->chkCategory->Checked && !$this->chkManufacturer->Checked && !$this->chkLongDescription->Checked) {
         $blnChecked = false;
         foreach ($this->arrCheckboxes as $objCheckBox) {
             if ($objCheckBox->Checked) {
                 $blnChecked = true;
                 break;
             }
         }
         if (!$blnChecked) {
             $blnError = true;
             $this->btnCancel->Warning = 'You must select at least one field to edit.';
             return;
         }
     }
     // Get an instance of the database
     $objDatabase = QApplication::$Database[1];
     // Begin a MySQL Transaction to be either committed or rolled back
     $objDatabase->TransactionBegin();
     if (count($this->arrCustomFields) > 0) {
         $customFieldIdArray = array();
         foreach ($this->arrCustomFields as $field) {
             if ($this->arrCheckboxes[$field['input']->strControlId]->Checked) {
                 if ($field['input'] instanceof QTextBox && $field['input']->Required && $field['input']->Text == null || $field['input'] instanceof QListBox && $field['input']->Required && $field['input']->SelectedValue == null) {
                     $blnError = true;
                     $field['input']->Warning = "Required.";
                 } else {
                     $this->arrCustomFieldsToEdit[] = $field;
                     $customFieldIdArray[] = (int) str_replace('cf', '', $field['input']->strControlId);
                 }
             }
         }
     }
     $set = array(sprintf('`modified_by`= %s', QApplication::$objUserAccount->UserAccountId));
     // Force modified_date timestamp update
     $set[] = '`modified_date` = NOW()';
     if ($this->chkLongDescription->Checked) {
         $set[] = sprintf('`long_description`="%s"', $this->txtLongDescription->Text);
     }
     if ($this->chkManufacturer->Checked) {
         if ($this->lstManufacturer->SelectedValue !== null) {
             $set[] = sprintf('`manufacturer_id`=%s', $this->lstManufacturer->SelectedValue);
         } else {
             $blnError = true;
             $this->lstManufacturer->Warning = 'Manufacturer is required.';
         }
     }
     if ($this->chkCategory->Checked) {
         if ($this->lstCategory->SelectedValue !== null) {
             $set[] = sprintf('`category_id`= %s', $this->lstCategory->SelectedValue);
         } else {
             $blnError = true;
             $this->lstCategory->Warning = 'Category is required.';
         }
     }
     // First check that the user is authorized to edit these models
     foreach ($this->arrModelsToEdit as $intModelId) {
         $objAssetModel = AssetModel::Load($intModelId);
         if (!QApplication::AuthorizeEntityBoolean($objAssetModel, 2)) {
             $blnError = true;
             $this->btnCancel->Warning = 'You are not authorized to edit one or more of the selected models.';
             break;
         }
     }
     if (!$blnError) {
         try {
             if (count($this->arrCustomFieldsToEdit) > 0) {
                 // preparing data to edit
                 // Save the values from all of the custom field controls to save the asset
                 foreach ($this->arrModelsToEdit as $intModelId) {
                     $objCustomFieldsArray = CustomField::LoadObjCustomFieldArray(EntityQtype::AssetModel, false);
                     $selectedCustomFieldsArray = array();
                     foreach ($objCustomFieldsArray as $objCustomField) {
                         if (in_array($objCustomField->CustomFieldId, $customFieldIdArray)) {
                             $selectedCustomFieldsArray[] = $objCustomField;
                         }
                     }
                     CustomField::SaveControls($selectedCustomFieldsArray, true, $this->arrCustomFieldsToEdit, $intModelId, EntityQtype::AssetModel);
                 }
             }
             $strQuery = sprintf("UPDATE `asset_model`\n\t\t\t\t\t\t\t\t\t SET " . implode(",", $set) . "\n\t\t\t\t\t\t\t\t\t WHERE `asset_model_id` IN (%s)", implode(",", $this->arrModelsToEdit));
             // print $strQuery; exit;
             $objDatabase->NonQuery($strQuery);
             $objDatabase->TransactionCommit();
             QApplication::Redirect('');
         } catch (QMySqliDatabaseException $objExc) {
             $objDatabase->TransactionRollback();
             throw new QDatabaseException();
         }
     } else {
         $objDatabase->TransactionRollback();
         $this->arrCustomFieldsToEdit = array();
     }
 }
예제 #18
0
 protected function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     $blnError = false;
     if (trim($this->txtShortDescription->Text) == "") {
         $this->txtShortDescription->Warning = 'Short description is required';
         $blnError = true;
     }
     if (trim($this->txtAssetModelCode->Text) == "") {
         $this->txtAssetModelCode->Warning = 'Asset model code is required';
         $blnError = true;
     }
     if ($blnError) {
         return;
     }
     $this->UpdateAssetModelFields();
     $this->objAssetModel->Save();
     // Assign input values to custom fields
     if ($this->arrCustomFields) {
         // Save the values from all of the custom field controls to save the asset
         CustomField::SaveControls($this->objAssetModel->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objAssetModel->AssetModelId, 4);
     }
     if ($this->ifcImage->FileName) {
         // Retrieve the extension (.jpg, .gif) from the filename
         $explosion = explode(".", $this->ifcImage->FileName);
         // Set the file name to ID_asset_model.ext
         $this->ifcImage->FileName = sprintf('%s%s%s.%s', $this->ifcImage->Prefix, $this->objAssetModel->AssetModelId, $this->ifcImage->Suffix, $explosion[1]);
         // Set the image path for saving the asset model
         $this->txtImagePath->Text = $this->ifcImage->FileName;
         // Upload the file to the server
         $this->ifcImage->ProcessUpload();
         // Save the image path information to the AssetModel object
         $this->objAssetModel->ImagePath = $this->txtImagePath->Text;
         $this->objAssetModel->Save(false, true);
     }
     if ($this->blnEditMode) {
         $this->UpdateLabels();
         // This was necessary because it was not saving the changes of a second edit/save in a row
         // Reload all custom fields
         $this->objAssetModel->objCustomFieldArray = CustomField::LoadObjCustomFieldArray(4, $this->blnEditMode, $this->objAssetModel->AssetModelId);
         $this->displayLabels();
     } else {
         // Display the asset model list page
         $strRedirect = "asset_model_list.php";
         QApplication::Redirect($strRedirect);
     }
 }
예제 #19
0
 public function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     $blnError = false;
     if ($this->objAssetTransactionArray && $this->objInventoryTransactionArray) {
         $intEntityQtypeId = EntityQtype::AssetInventory;
     } elseif ($this->objAssetTransactionArray) {
         $intEntityQtypeId = EntityQtype::Asset;
     } elseif ($this->objInventoryTransactionArray) {
         $intEntityQtypeId = EntityQtype::Inventory;
     } else {
         $blnError = true;
         $this->btnCancel->Warning = 'There are no assets nor inventory in this receipt.';
     }
     if (QApplication::$TracmorSettings->CustomReceiptNumbers) {
         if (trim($this->txtReceiptNumber->Text) == '') {
             $blnError = true;
             $this->txtReceiptNumber->Warning = 'Receipt number is a required field.';
         } else {
             if ($objReceipt = Receipt::LoadByReceiptNumber($this->txtReceiptNumber->Text)) {
                 if ($objReceipt->ReceiptId != $this->objReceipt->ReceiptId) {
                     $blnError = true;
                     $this->txtReceiptNumber->Warning = 'That is a duplicate receipt number.';
                 }
             }
         }
     }
     if (!$this->lstFromCompany->SelectedValue) {
         $blnError = true;
         $this->lstFromCompany->Warning = 'You must select a From Company';
     }
     if (!$this->lstFromContact->SelectedValue) {
         $blnError = true;
         $this->lstFromContact->Warning = 'You must select a From Contact';
     }
     if (!$this->lstToContact->SelectedValue) {
         $blnError = true;
         $this->lstToContact->Warning = 'You must select a To Contact';
     }
     if (!$this->lstToAddress->SelectedValue) {
         $blnError = true;
         $this->lstToAddress->Warning = 'You must select a To Address';
     }
     if (!$blnError) {
         if (!$this->blnEditMode) {
             try {
                 // Get an instance of the database
                 $objDatabase = QApplication::$Database[1];
                 // Begin a MySQL Transaction to be either committed or rolled back
                 $objDatabase->TransactionBegin();
                 // Create the new transaction object and save it
                 $this->objTransaction = new Transaction();
                 $this->objTransaction->EntityQtypeId = $intEntityQtypeId;
                 $this->objTransaction->TransactionTypeId = 7;
                 // Receive
                 $this->objTransaction->Note = $this->txtNote->Text;
                 $this->objTransaction->Save();
                 if ($intEntityQtypeId == EntityQtype::AssetInventory || $intEntityQtypeId == EntityQtype::Asset) {
                     // Assign different source and destinations depending on transaction type
                     foreach ($this->objAssetTransactionArray as $objAssetTransaction) {
                         // Save the asset just to update the modified_date field so it can trigger an Optimistic Locking Exception when appropriate
                         if ($objAssetTransaction->Asset instanceof Asset) {
                             // Save the asset to update the modified_date field so it can trigger an Optimistic Locking Exception when appropriate
                             // Also set the location to 5 (TBR). This is in case the current LocationId is 2 (Shipped), because they can be received.
                             $objAssetTransaction->Asset->LocationId = 5;
                             // If the AssetId==0, then it is a newly created asset that hasn't been saved to the db yet
                             // We have to create a new asset object and assign it to the AssetTransaction
                             // Just resetting the values for the existing asset object won't work for some reason (not sure why).
                             if ($objAssetTransaction->Asset->AssetId == 0) {
                                 $objNewAsset = new Asset();
                                 $objNewAsset->AssetModelId = $objAssetTransaction->Asset->AssetModelId;
                                 $objNewAsset->TempId = $objAssetTransaction->Asset->TempId;
                                 $objNewAsset->LocationId = $objAssetTransaction->Asset->LocationId;
                                 // If the asset was selected for autogeneration, it will be blank, so create the asset code here (right before save)
                                 if ($objAssetTransaction->Asset->AssetCode == '') {
                                     $objAssetTransaction->Asset->AssetCode = Asset::GenerateAssetCode();
                                 }
                                 $objNewAsset->AssetCode = $objAssetTransaction->Asset->AssetCode;
                                 // Save the new asset
                                 $objNewAsset->Save();
                                 // Assign any default custom field values
                                 CustomField::AssignNewEntityDefaultValues(1, $objNewAsset->AssetId);
                                 // Assign the new asset to the AssetTransaction
                                 $objAssetTransaction->Asset = $objNewAsset;
                                 $objAssetTransaction->NewAssetFlag = true;
                             } else {
                                 $objAssetTransaction->NewAssetFlag = false;
                                 $objAssetTransaction->Asset->Save();
                             }
                             // Create the new assettransaction object and save it
                             $objAssetTransaction->TransactionId = $this->objTransaction->TransactionId;
                             $objAssetTransaction->Save();
                         }
                     }
                 }
                 if ($intEntityQtypeId == EntityQtype::AssetInventory || $intEntityQtypeId == EntityQtype::Inventory) {
                     // Assign different source and destinations depending on transaction type
                     foreach ($this->objInventoryTransactionArray as &$objInventoryTransaction) {
                         // Finish the InventoryTransaction and save it
                         $objInventoryTransaction->InventoryLocation->Quantity += $objInventoryTransaction->Quantity;
                         $objInventoryTransaction->InventoryLocation->Save();
                         $objInventoryTransaction->TransactionId = $this->objTransaction->TransactionId;
                         $objInventoryTransaction->Save();
                     }
                 }
                 $this->UpdateReceiptFields();
                 $this->objReceipt->ReceivedFlag = false;
                 $this->objReceipt->Save();
                 if ($this->arrCustomFields) {
                     // Save the values from all of the custom field controls to save the shipment
                     CustomField::SaveControls($this->objReceipt->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objReceipt->ReceiptId, EntityQtype::Receipt);
                 }
                 $objDatabase->TransactionCommit();
                 QApplication::Redirect('receipt_list.php');
             } catch (QExtendedOptimisticLockingException $objExc) {
                 // Rollback the database
                 $objDatabase->TransactionRollback();
                 if ($objExc->Class == 'Asset') {
                     // $this->btnRemoveAssetTransaction_Click($this->FormId, 'btnRemoveAsset' . $objExc->EntityId, $objExc->EntityId);
                     $this->btnRemoveAssetTransaction_Click($this->FormId, null, $objExc->EntityId);
                     $objAsset = Asset::Load($objExc->EntityId);
                     if ($objAsset) {
                         $this->btnCancel->Warning = sprintf('The Asset %s has been modified by another user and removed from this shipment. You may add the asset again or save the transaction without it.', $objAsset->AssetCode);
                     } else {
                         $this->btnCancel->Warning = 'An Asset has been deleted by another user and removed from this shipment.';
                     }
                 }
                 if ($objExc->Class == 'AssetTransaction') {
                     $this->btnCancel->Warning = 'This asset transaction has been modified by another user. You may reload the receipt and try your modifications again.';
                 }
                 if ($objExc->Class == 'InventoryLocation') {
                     $this->btnRemoveInventory_Click($this->FormId, 'btnRemoveInventory' . $objExc->EntityId, $objExc->EntityId);
                     $objInventoryLocation = InventoryLocation::Load($objExc->EntityId);
                     if ($objInventoryLocation) {
                         $this->btnCancel->Warning = sprintf('The Inventory %s has been modified by another user and removed from this shipment. You may add the inventory again or save the shipment without it.', $objInventoryLocation->InventoryModel->InventoryModelCode);
                     } else {
                         $this->btnCancel->Warning = 'Inventory has been deleted by another user and removed from this shipment.';
                     }
                 }
             }
         } elseif ($this->blnEditMode) {
             try {
                 // Get an instance of the database
                 $objDatabase = QApplication::$Database[1];
                 // Begin a MySQL Transaction to be either committed or rolled back
                 $objDatabase->TransactionBegin();
                 // This should probably be changed to $this->objReceipt->Transaction
                 $this->objTransaction = Transaction::Load($this->objReceipt->TransactionId);
                 $this->objTransaction->EntityQtypeId = $intEntityQtypeId;
                 $this->objTransaction->Note = $this->txtNote->Text;
                 $this->objTransaction->Save();
                 // Remove AssetTransactions that were removed when editing
                 if ($this->arrAssetTransactionToDelete) {
                     foreach ($this->arrAssetTransactionToDelete as $intAssetTransactionId) {
                         $objAssetTransactionToDelete = AssetTransaction::Load($intAssetTransactionId);
                         // Make sure that it wasn't added and then removed
                         if ($objAssetTransactionToDelete) {
                             // If a new asset was created in this receipt, it needs to be deleted
                             if ($objAssetTransactionToDelete->NewAssetFlag) {
                                 $intAssetIdToDelete = $objAssetTransactionToDelete->Asset->AssetId;
                                 //$objAssetTransactionToDelete->Asset->Delete();
                             } else {
                                 // Change back location
                                 $objAssetTransactionToDelete->Asset->LocationId = $objAssetTransactionToDelete->SourceLocationId;
                                 $objAssetTransactionToDelete->Asset->Save();
                             }
                             // Delete the asset transaction
                             $objAssetTransactionToDelete->Delete();
                             // If a new asset,  delete it
                             if (isset($intAssetIdToDelete)) {
                                 $objAssetToDelete = Asset::LoadByAssetId($intAssetIdToDelete);
                                 $objAssetToDelete->Delete();
                             }
                             unset($objAssetTransactionToDelete);
                         }
                     }
                 }
                 // Save existing AssetTransactions
                 if ($this->objAssetTransactionArray) {
                     foreach ($this->objAssetTransactionArray as $objAssetTransaction) {
                         if (!$objAssetTransaction->AssetTransactionId) {
                             $objAssetTransaction->TransactionId = $this->objTransaction->TransactionId;
                             // This is done in case the original location is 'Shipped'(2), not 'To Be Received'(5)
                             $objAssetTransaction->SourceLocationId = $objAssetTransaction->Asset->LocationId;
                             $objAssetTransaction->Asset->LocationId = 5;
                             // To Be Received
                             // If the AssetId is 0 (it hasn't been saved to the database yet), then create a new Asset in the conventional way
                             // We have to create a new asset object and assign it to the AssetTransaction
                             // Just resetting the values for the existing asset object won't work for some reason (not sure why).
                             if ($objAssetTransaction->Asset->AssetId == 0) {
                                 $objNewAsset = new Asset();
                                 $objNewAsset->AssetModelId = $objAssetTransaction->Asset->AssetModelId;
                                 $objNewAsset->TempId = $objAssetTransaction->Asset->TempId;
                                 $objNewAsset->LocationId = $objAssetTransaction->Asset->LocationId;
                                 // If the asset was selected for autogeneration, it will be blank, so create the asset code here (right before save)
                                 if ($objAssetTransaction->Asset->AssetCode == '') {
                                     $objAssetTransaction->Asset->AssetCode = Asset::GenerateAssetCode();
                                 }
                                 $objNewAsset->AssetCode = $objAssetTransaction->Asset->AssetCode;
                                 // Save the new asset
                                 $objNewAsset->Save();
                                 // Assign any default custom field values
                                 CustomField::AssignNewEntityDefaultValues(1, $objNewAsset->AssetId);
                                 // Associate the new asset with the AssetTransaction
                                 $objAssetTransaction->Asset = $objNewAsset;
                                 $objAssetTransaction->NewAssetFlag = true;
                             } else {
                                 $objAssetTransaction->NewAssetFlag = false;
                                 $objAssetTransaction->Asset->Save();
                             }
                         }
                         // Always save the asset transaction, to generate an Optimistic Locking Exception when appropriate
                         $objAssetTransaction->Save();
                         // Reload AssetTransaction to avoid Optimistic Locking Exception if this receipt is edited and saved.
                         $objAssetTransaction = AssetTransaction::Load($objAssetTransaction->AssetTransactionId);
                     }
                 }
                 // Remove InventoryTransactions
                 if ($this->arrInventoryTransactionToDelete) {
                     foreach ($this->arrInventoryTransactionToDelete as $intInventoryTransactionId) {
                         $objInventoryTransactionToDelete = InventoryTransaction::Load($intInventoryTransactionId);
                         // Make sure that it wasn't added then removed
                         if ($objInventoryTransactionToDelete) {
                             // Change back the quantity
                             $objInventoryTransactionToDelete->InventoryLocation->Quantity -= $objInventoryTransactionToDelete->Quantity;
                             $objInventoryTransactionToDelete->InventoryLocation->Save();
                             // Delete the InventoryTransaction
                             $objInventoryTransactionToDelete->Delete();
                             unset($objInventoryTransactionToDelete);
                         }
                     }
                 }
                 // Save InventoryTransactions
                 if ($this->objInventoryTransactionArray) {
                     foreach ($this->objInventoryTransactionArray as $objInventoryTransaction) {
                         if (!$objInventoryTransaction->InventoryTransactionId) {
                             // Reload the InventoryLocation. If it was deleted and added in the same save click, then it will throw an Optimistic Locking Exception
                             $objInventoryTransaction->InventoryLocation = InventoryLocation::Load($objInventoryTransaction->InventoryLocationId);
                             $objInventoryTransaction->InventoryLocation->Quantity += $objInventoryTransaction->Quantity;
                             $objInventoryTransaction->TransactionId = $this->objTransaction->TransactionId;
                             $objInventoryTransaction->InventoryLocation->Save();
                             $SourceLocationId = 5;
                             // To Be Received
                             $objInventoryTransaction->SourceLocationId = $SourceLocationId;
                         }
                         // Always save the InventoryTransaction, to generate an Optimistic Locking Exception when appropriate
                         $objInventoryTransaction->Save();
                         // Reload the InventoryTransaction to get the new timestamp so that it doesn't generate an optimistic locking exception
                         $objInventoryTransaction = InventoryTransaction::Load($objInventoryTransaction->InventoryTransactionId);
                     }
                 }
                 // Check to see if all Inventory and Assets have been received (if the final entity was removed from the receipt without receiving it).
                 // Only if it hasn't already been received
                 if (!$this->objReceipt->ReceivedFlag) {
                     // Check to see if all assets have been received
                     $blnAllAssetsReceived = true;
                     if ($this->objAssetTransactionArray) {
                         foreach ($this->objAssetTransactionArray as $objAssetTransaction) {
                             if (!$objAssetTransaction->DestinationLocationId) {
                                 $blnAllAssetsReceived = false;
                             }
                         }
                     }
                     // Check to see if all inventory have been received
                     $blnAllInventoryReceived = true;
                     if ($this->objInventoryTransactionArray) {
                         foreach ($this->objInventoryTransactionArray as $objInventoryTransaction) {
                             if (!$objInventoryTransaction->DestinationLocationId) {
                                 $blnAllInventoryReceived = false;
                             }
                         }
                     }
                     // If all Inventory and Assets have been received
                     if ($blnAllAssetsReceived && $blnAllInventoryReceived) {
                         // Flip the received flag for the entire Receipt
                         $this->objReceipt->ReceivedFlag = true;
                         $this->objReceipt->ReceiptDate = new QDateTime(QDateTime::Now);
                     }
                 } else {
                     if ($this->objReceipt->ReceiptDate != $this->calDateReceived->DateTime) {
                         $this->objReceipt->ReceiptDate = $this->calDateReceived->DateTime;
                     }
                 }
                 $this->UpdateReceiptFields();
                 $this->UpdateReceiptLabels();
                 $this->objReceipt->Save();
                 if ($this->arrCustomFields) {
                     // Save the values from all of the custom field controls to save the shipment
                     CustomField::SaveControls($this->objReceipt->objCustomFieldArray, $this->blnEditMode, $this->arrCustomFields, $this->objReceipt->ReceiptId, EntityQtype::Receipt);
                 }
                 // Reload to get new timestamp to avoid optimistic locking if edited/saved again without reload
                 $this->objReceipt = Receipt::Load($this->objReceipt->ReceiptId);
                 $this->DisplayLabels();
                 $objDatabase->TransactionCommit();
             } catch (QExtendedOptimisticLockingException $objExc) {
                 $objDatabase->TransactionRollback();
                 if ($objExc->Class == 'Receipt' || $objExc->Class == 'AssetTransaction' || $objExc->Class == 'InventoryTransaction') {
                     $this->btnCancel->Warning = sprintf('This receipt has been modified by another user. You must <a href="receipt_edit.php?intReceiptId=%s">Refresh</a> to edit this receipt.', $this->objReceipt->ReceiptId);
                 } elseif ($objExc->Class == 'Asset') {
                     // $this->btnRemoveAssetTransaction_Click($this->FormId, 'btnRemoveAsset' . $objExc->EntityId, $objExc->EntityId);
                     $this->btnRemoveAssetTransaction_Click($this->FormId, null, $objExc->EntityId);
                     $objAsset = Asset::Load($objExc->EntityId);
                     if ($objAsset) {
                         $this->btnCancel->Warning = sprintf('The Asset %s has been modified by another user and removed from this shipment. You may add the asset again or save the transaction without it.', $objAsset->AssetCode);
                     } else {
                         $this->btnCancel->Warning = 'An Asset has been deleted by another user and removed from this shipment.';
                     }
                 } elseif ($objExc->Class == 'InventoryLocation') {
                     $this->btnRemoveInventory_Click($this->FormId, 'btnRemoveInventory' . $objExc->EntityId, $objExc->EntityId);
                     $objInventoryLocation = InventoryLocation::Load($objExc->EntityId);
                     if ($objInventoryLocation) {
                         $this->btnCancel->Warning = sprintf('The Inventory %s has been modified by another user and removed from this shipment. You may add the inventory again or save the shipment without it.', $objInventoryLocation->InventoryModel->InventoryModelCode);
                     } else {
                         $this->btnCancel->Warning = 'Inventory has been deleted by another user and removed from this shipment.';
                     }
                 } else {
                     throw new QOptimisticLockingException($objExc->Class);
                 }
             }
         }
     }
 }
 public function btnMassEditApply_Click($strFormId, $strControlId, $strParameter)
 {
     $this->clearWarnings();
     $blnError = false;
     // Get an instance of the database
     $objDatabase = QApplication::$Database[1];
     // Begin a MySQL Transaction to be either committed or rolled back
     $objDatabase->TransactionBegin();
     $set = array(sprintf('`modified_by`= %s', QApplication::$objUserAccount->UserAccountId));
     if ($this->chkDescription->Checked) {
         $set[] = sprintf('`description` ="%s"', $this->txtDescription->Text);
     }
     if ($this->chkCompany->Checked) {
         if ($this->lstCompany->SelectedValue !== null && $this->lstAddress->SelectedValue !== null) {
             $set[] = sprintf('`company_id` = %s', $this->lstCompany->SelectedValue);
             $set[] = sprintf('`address_id` = %s', $this->lstAddress->SelectedValue);
         } else {
             $blnError = true;
             $this->lstCompany->Warning = $this->lstCompany->SelectedValue == null ? 'Company can\'t be empty' : '';
             $this->lstAddress->Warning = $this->lstAddress->SelectedValue == null ? 'Address can\'t be empty' : '';
         }
     }
     $strQuery = sprintf("UPDATE `contact`\n\t\t\t\t\t\t\t SET " . implode(",", $set) . "\n\t\t\t\t\t\t\t WHERE `contact_id` IN (%s)", implode(",", $this->arrContactToEdit));
     // Custom Fields handling
     if (count($this->arrCustomFields) > 0) {
         $customFieldIdArray = array();
         foreach ($this->arrCustomFields as $field) {
             if ($this->arrCheckboxes[$field['input']->strControlId]->Checked) {
                 if ($field['input'] instanceof QTextBox && $field['input']->Required && $field['input']->Text == null || $field['input'] instanceof QListBox && $field['input']->Required && $field['input']->SelectedValue == null) {
                     $blnError = true;
                     $field['input']->Warning = "Required.";
                 } else {
                     $this->arrCustomFieldsToEdit[] = $field;
                     $customFieldIdArray[] = (int) str_replace('cf', '', $field['input']->strControlId);
                 }
             }
         }
     }
     // Transaction
     if (!$blnError) {
         try {
             if (count($this->arrCustomFieldsToEdit) > 0) {
                 // preparing data to edit
                 foreach ($this->arrContactToEdit as $intContactId) {
                     $objCustomFieldsArray = CustomField::LoadObjCustomFieldArray(EntityQtype::Contact, false);
                     $selectedCustomFieldsArray = array();
                     foreach ($objCustomFieldsArray as $objCustomField) {
                         if (in_array($objCustomField->CustomFieldId, $customFieldIdArray)) {
                             $selectedCustomFieldsArray[] = $objCustomField;
                         }
                     }
                     CustomField::SaveControls($selectedCustomFieldsArray, true, $this->arrCustomFieldsToEdit, $intContactId, EntityQtype::Contact);
                 }
             }
             //print $strQuery; exit;
             $objDatabase->NonQuery($strQuery);
             $objDatabase->TransactionCommit();
             QApplication::Redirect('');
         } catch (QMySqliDatabaseException $objExc) {
             $objDatabase->TransactionRollback();
             throw new QDatabaseException();
         }
     } else {
         $objDatabase->TransactionRollback();
         $this->arrCustomFieldsToEdit = array();
         $this->uncheck();
     }
 }