/**
  * Load a single AssetTransactionCheckout object,
  * by AssetId
  * @param integer $intAssetId
  * @return object $objAssetTransactionCheckout
  */
 public function LoadAssetTransactionCheckoutByAssetId($intAssetId = null)
 {
     // Loads objAssetTransaction
     $objClauses = array();
     $objOrderByClause = QQ::OrderBy(QQN::AssetTransaction()->Transaction->CreationDate, false);
     $objLimitClause = QQ::LimitInfo(1, 0);
     array_push($objClauses, $objOrderByClause);
     array_push($objClauses, $objLimitClause);
     $AssetTransactionArray = AssetTransaction::LoadArrayByAssetId($this->AssetId, $objClauses);
     $intLastAssetTransactionId = $AssetTransactionArray[0]->AssetTransactionId;
     $objAssetTransactionCheckout = AssetTransactionCheckout::LoadByAssetTransactionId($intAssetLastTransactionId);
     return $objAssetTransactionCheckout;
 }
Example #2
0
 /**
  * Returns an array of objects that created the most recent transaction for this asset
  *
  * @return array of Object Account, Object AssetTransactionCheckout, string Note
  */
 public function GetLastTransactionCheckoutObjectArray()
 {
     $objClauses = array();
     $objExpansionClause = QQ::Expand(QQN::AssetTransaction()->Transaction->CreatedByObject);
     $objOrderByClause = QQ::OrderBy(QQN::AssetTransaction()->Transaction->CreationDate, false);
     $objLimitClause = QQ::LimitInfo(1, 0);
     array_push($objClauses, $objExpansionClause);
     array_push($objClauses, QQ::Expand(QQN::AssetTransaction()->AssetTransactionCheckout->AssetTransactionCheckoutId));
     array_push($objClauses, $objOrderByClause);
     array_push($objClauses, $objLimitClause);
     $AssetTransactionArray = AssetTransaction::LoadArrayByAssetId($this->AssetId, $objClauses);
     /*$intLastAssetTransactionId = $AssetTransactionArray[0]->AssetTransactionId;
     		$objClauses = array();
     		array_push($objClauses, QQ::Expand(QQN::AssetTransactionCheckout()->ToContact));
     		array_push($objClauses, QQ::Expand(QQN::AssetTransactionCheckout()->ToUser));
     		$objAssetTransactionCheckout = AssetTransactionCheckout::QuerySingle(QQ::Equal(QQN::AssetTransactionCheckout()->AssetTransactionId, $intLastAssetTransactionId), $objClauses);*/
     $objAssetTransactionCheckout = $AssetTransactionArray[0]->AssetTransactionCheckout;
     $objAccount = $AssetTransactionArray[0]->Transaction->CreatedByObject;
     $strReason = $AssetTransactionArray[0]->Transaction->Note;
     return array("objAssetTransactionCheckout" => $objAssetTransactionCheckout, "objAccount" => $objAccount, "strNote" => $strReason);
 }
Example #3
0
 /**
  * Gets all associated AssetTransactions as an array of AssetTransaction objects
  * @param QQClause[] $objOptionalClauses additional optional QQClause objects for this query
  * @return AssetTransaction[]
  */
 public function GetAssetTransactionArray($objOptionalClauses = null)
 {
     if (is_null($this->intAssetId)) {
         return array();
     }
     try {
         return AssetTransaction::LoadArrayByAssetId($this->intAssetId, $objOptionalClauses);
     } catch (QCallerException $objExc) {
         $objExc->IncrementOffset();
         throw $objExc;
     }
 }
Example #4
0
 protected function btnCompleteShipment_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->btnCompleteShipment->Warning = 'There are no assets or inventory in this shipment.';
     }
     if ($this->objAssetTransactionArray) {
         foreach ($this->objAssetTransactionArray as $objAssetTransaction) {
             if ($objAssetTransaction->Asset instanceof Asset) {
                 $arrAssetTransactions = AssetTransaction::LoadArrayByAssetId($objAssetTransaction->Asset->AssetId, array(QQ::OrderBy(QQN::AssetTransaction()->CreationDate, false)));
                 if (count($arrAssetTransactions) > 0) {
                     $intLastTransactionId = $arrAssetTransactions[0]->TransactionId;
                     $transaction = Transaction::load($intLastTransactionId);
                     if ($transaction->TransactionTypeId == 6 && $transaction->Shipment->ShippedFlag) {
                         $blnError = true;
                         $this->btnCompleteShipment->Warning = $objAssetTransaction->Asset->__toStringWithLink() . ' already shipped.';
                     }
                 }
             }
         }
     }
     if (!$blnError) {
         try {
             // Get an instance of the database
             $objDatabase = QApplication::$Database[1];
             // Begin a MySQL Transaction to be either committed or rolled back
             $objDatabase->TransactionBegin();
             if (!$this->blnEditMode) {
                 // this is a new shipment so save the transaction & shipment before completing
                 // 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();
                 $this->UpdateShipmentFields();
                 $this->objShipment->Save(true);
                 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);
                 }
             }
             // Create receipt transaction for internal shipment
             if ($this->objShipment->ToCompanyId == $this->objShipment->FromCompanyId) {
                 $this->receiveInternalShipmentTransaction = new Transaction();
                 $this->receiveInternalShipmentTransaction->EntityQtypeId = $intEntityQtypeId;
                 $this->receiveInternalShipmentTransaction->TransactionTypeId = 7;
                 $note = sprintf('This receipt was automatically created when creating internal shipment Number %s. ', $this->objShipment->ShipmentNumber);
                 $this->receiveInternalShipmentTransaction->Note = $note . $this->txtNote->Text;
                 $this->receiveInternalShipmentTransaction->Save();
                 // Create a new receipt
                 $objInternalReceipt = new Receipt();
                 $objInternalReceipt->TransactionId = $this->receiveInternalShipmentTransaction->TransactionId;
                 // The receipt will mimic the shipment information
                 $objInternalReceipt->FromCompanyId = $this->objShipment->FromCompanyId;
                 $objInternalReceipt->FromContactId = $this->objShipment->FromContactId;
                 $objInternalReceipt->ToContactId = $this->objShipment->ToContactId;
                 $objInternalReceipt->ToAddressId = $this->objShipment->ToAddressId;
                 $objInternalReceipt->ReceivedFlag = 0;
                 $objInternalReceipt->ReceiptNumber = Receipt::LoadNewReceiptNumber();
                 $objInternalReceipt->Save();
             }
             if ($intEntityQtypeId == EntityQtype::AssetInventory || $intEntityQtypeId == EntityQtype::Asset) {
                 $objTransaction = '';
                 $objReceipt = '';
                 $objNewAssetTransactionArray = array();
                 foreach ($this->objAssetTransactionArray as $objAssetTransaction) {
                     $objNewAssetTransactionArray[$objAssetTransaction->Asset->AssetCode] = $objAssetTransaction;
                 }
                 // Assign a destinationLocation to the AssetTransaction, and change the Location of the asset
                 foreach ($this->objAssetTransactionArray as $objAssetTransaction) {
                     if ($objAssetTransaction->Asset instanceof Asset) {
                         // LocationId #2 == Shipped
                         $DestinationLocationId = 2;
                         if ($objAssetTransaction->ScheduleReceiptFlag && $objAssetTransaction->Asset->LinkedFlag) {
                             $objAssetTransaction = $objNewAssetTransactionArray[$objAssetTransaction->Asset->AssetCode];
                         }
                         $objAssetTransaction->Asset->LocationId = $DestinationLocationId;
                         $objAssetTransaction->Asset->Save();
                         if (!$this->blnEditMode) {
                             // Assign the TransactionId
                             $objAssetTransaction->TransactionId = $this->objTransaction->TransactionId;
                         }
                         $objAssetTransaction->DestinationLocationId = $DestinationLocationId;
                         // No any actions with linked items (LinkedFlag = 1) which have been scheduled for receipt
                         if ($objAssetTransaction->ScheduleReceiptFlag && !$objAssetTransaction->Asset->LinkedFlag) {
                             if ($objAssetTransaction->NewAsset && $objAssetTransaction->NewAsset instanceof Asset && $objAssetTransaction->NewAsset->AssetId == null) {
                                 // We have to create the new asset before we can
                                 $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;
                             }
                             // If it doesn't exist, create a new transaction object and receipt object
                             if (!$objTransaction instanceof Transaction && !$objReceipt instanceof Receipt) {
                                 $objTransaction = new Transaction();
                                 // Transaction is asset only
                                 $objTransaction->EntityQtypeId = 1;
                                 // Transaction is a receipt
                                 $objTransaction->TransactionTypeId = 7;
                                 // Set a note showing how this receipt was created
                                 if (!$objAssetTransaction->NewAssetId) {
                                     $strTransactionType = 'return';
                                 } else {
                                     $strTransactionType = 'exchange';
                                 }
                                 $objTransaction->Note = sprintf('This %s receipt was automatically created when creating Shipment Number %s.', $strTransactionType, $this->objShipment->ShipmentNumber);
                                 // Save the transaction
                                 $objTransaction->Save();
                                 // Create a new receipt
                                 $objReceipt = new Receipt();
                                 $objReceipt->TransactionId = $objTransaction->TransactionId;
                                 // The receipt will be coming from the company that was shipped to
                                 $objReceipt->FromCompanyId = $this->objShipment->ToCompanyId;
                                 $objReceipt->FromContactId = $this->objShipment->ToContactId;
                                 $objReceipt->ToContactId = $this->objShipment->FromContactId;
                                 $objReceipt->ToAddressId = $this->objShipment->FromAddressId;
                                 $objReceipt->ReceivedFlag = 0;
                                 $objReceipt->ReceiptNumber = Receipt::LoadNewReceiptNumber();
                                 if ($objAssetTransaction->ScheduleReceiptDueDate) {
                                     $objReceipt->DueDate = $objAssetTransaction->ScheduleReceiptDueDate;
                                 }
                                 $objReceipt->Save();
                             }
                             $objReceiptAssetTransaction = new AssetTransaction();
                             // If this is a return
                             if (!$objAssetTransaction->NewAssetId) {
                                 $objReceiptAssetTransaction->AssetId = $objAssetTransaction->AssetId;
                             } else {
                                 // Both the shipmentAssetTranscation (objAssetTransaction and the objReceiptAssetTransaction were involved in creating a new asset
                                 // Asset Transactions where NewAssetFlag = true but AssetId is NULL are receipt asset transactions for exchanges.
                                 $objReceiptAssetTransaction->AssetId = $objAssetTransaction->NewAssetId;
                                 $objReceiptAssetTransaction->NewAssetFlag = true;
                                 $objAssetTransaction->NewAssetFlag = true;
                                 $objAssetTransaction->Save();
                             }
                             $objReceiptAssetTransaction->TransactionId = $objTransaction->TransactionId;
                             $objReceiptAssetTransaction->SourceLocationId = $objAssetTransaction->DestinationLocationId;
                             // This is not right. NewAssetFlag should be set only if a new asset was created when creating this AssetTransaction
                             // It should not be true on the new AssetTransaction, but only on the AssetTransaction that caused the new asset to be created.
                             // $objReceiptAssetTransaction->NewAssetFlag = true;
                             $objReceiptAssetTransaction->Save();
                             // Load all child assets
                             if ($objLinkedAssetCodeArray = Asset::LoadChildLinkedArrayByParentAssetId($objAssetTransaction->Asset->AssetId)) {
                                 foreach ($objLinkedAssetCodeArray as $objLinkedAssetCode) {
                                     $objLinkedAssetTransaction = $objNewAssetTransactionArray[$objLinkedAssetCode->AssetCode];
                                     $objLinkedReceiptAssetTransaction = new AssetTransaction();
                                     // If this is a return
                                     if (!$objAssetTransaction->NewAssetId) {
                                         $objLinkedReceiptAssetTransaction->AssetId = $objLinkedAssetTransaction->AssetId;
                                         $objLinkedReceiptAssetTransaction->TransactionId = $objTransaction->TransactionId;
                                         $objLinkedReceiptAssetTransaction->SourceLocationId = $objAssetTransaction->DestinationLocationId;
                                         $objLinkedReceiptAssetTransaction->Save();
                                     } else {
                                         // Both the shipmentAssetTranscation (objAssetTransaction and the objReceiptAssetTransaction were involved in creating a new asset
                                         // Asset Transactions where NewAssetFlag = true but AssetId is NULL are receipt asset transactions for exchanges.
                                         $objLinkedReceiptAssetTransaction->AssetId = $objAssetTransaction->NewAssetId;
                                     }
                                     $objNewAssetTransactionArray[$objLinkedAssetCode->AssetCode] = $objLinkedAssetTransaction;
                                 }
                             }
                         }
                         $objAssetTransaction->Save();
                         if ($objAssetTransaction->ScheduleReceiptFlag) {
                             // Set the Receipt Asset Transaction as child of the Shipment Asset Transaction
                             $objAssetTransaction->AssociateChildAssetTransaction($objReceiptAssetTransaction);
                         }
                         if ($this->objShipment->ToCompanyId == $this->objShipment->FromCompanyId && !$objAssetTransaction->Asset->LinkedFlag) {
                             $objReceiptAssetTransaction = new AssetTransaction();
                             $objReceiptAssetTransaction->AssetId = $objAssetTransaction->AssetId;
                             $objReceiptAssetTransaction->TransactionId = $this->receiveInternalShipmentTransaction->TransactionId;
                             $objReceiptAssetTransaction->SourceLocationId = $objAssetTransaction->SourceLocationId;
                             $objReceiptAssetTransaction->Save();
                             // Load all child assets
                             if ($objLinkedAssetCodeArray = Asset::LoadChildLinkedArrayByParentAssetId($objAssetTransaction->Asset->AssetId)) {
                                 foreach ($objLinkedAssetCodeArray as $objLinkedAssetCode) {
                                     $objLinkedAssetTransaction = $objNewAssetTransactionArray[$objLinkedAssetCode->AssetCode];
                                     $objLinkedReceiptAssetTransaction = new AssetTransaction();
                                     // add data to linked asset
                                     $objLinkedReceiptAssetTransaction->AssetId = $objLinkedAssetTransaction->AssetId;
                                     $objLinkedReceiptAssetTransaction->TransactionId = $this->receiveInternalShipmentTransaction->TransactionId;
                                     $objLinkedReceiptAssetTransaction->SourceLocationId = $objAssetTransaction->SourceLocationId;
                                     $objLinkedReceiptAssetTransaction->Save();
                                 }
                             }
                         }
                         $objReceipt = null;
                         $objTransaction = null;
                     }
                 }
             }
             if ($intEntityQtypeId == EntityQtype::AssetInventory || $intEntityQtypeId == EntityQtype::Inventory) {
                 // Assign different source and destinations depending on transaction type
                 foreach ($this->objInventoryTransactionArray as $objInventoryTransaction) {
                     // LocationId #2 == Shipped
                     $DestinationLocationId = 2;
                     if (!$this->blnEditMode) {
                         $objInventoryTransaction->TransactionId = $this->objTransaction->TransactionId;
                     }
                     // Remove the inventory quantity from the source
                     $objInventoryTransaction->InventoryLocation->Quantity = $objInventoryTransaction->InventoryLocation->Quantity - $objInventoryTransaction->Quantity;
                     $objInventoryTransaction->InventoryLocation->Save();
                     // Finish the InventoryTransaction and save it
                     $objInventoryTransaction->DestinationLocationId = $DestinationLocationId;
                     $objInventoryTransaction->Save();
                     // Add Inventory to receipt if this is an internal shipment
                     if ($this->objShipment->ToCompanyId == $this->objShipment->FromCompanyId) {
                         $objReceiptInventoryLocation = InventoryLocation::LoadByLocationIdInventoryModelId(5, $objInventoryTransaction->InventoryLocation->InventoryModelId);
                         if (!$objReceiptInventoryLocation) {
                             // First create the inventory location if it doesn't exist
                             $objReceiptInventoryLocation = new InventoryLocation();
                             $objReceiptInventoryLocation->InventoryModelId = $objInventoryTransaction->InventoryLocation->InventoryModelId;
                             $objReceiptInventoryLocation->LocationId = 5;
                             $objReceiptInventoryLocation->Quantity = 0;
                         }
                         // Set the To Be Received quantity
                         $objReceiptInventoryLocation->Quantity += $objInventoryTransaction->Quantity;
                         $objReceiptInventoryLocation->Save();
                         // Create the inventory transaction
                         $objReceiptInventoryTransaction = new InventoryTransaction();
                         $objReceiptInventoryTransaction->TransactionId = $this->receiveInternalShipmentTransaction->TransactionId;
                         $objReceiptInventoryTransaction->InventoryLocationId = $objReceiptInventoryLocation->InventoryLocationId;
                         $objReceiptInventoryTransaction->Quantity = $objInventoryTransaction->Quantity;
                         $objReceiptInventoryTransaction->SourceLocationId = 5;
                         $objReceiptInventoryTransaction->Save();
                     }
                 }
             }
             if ($this->blnEditMode) {
                 $this->UpdateShipmentFields();
             }
             $this->objShipment->ShippedFlag = true;
             // $this->objShipment->Save(false, true);
             $this->objShipment->Save();
             $objDatabase->TransactionCommit();
             QApplication::Redirect(sprintf('../shipping/shipment_edit.php?intShipmentId=%s', $this->objShipment->ShipmentId));
         } catch (QExtendedOptimisticLockingException $objExc) {
             $objDatabase->TransactionRollback();
             if ($objExc->Class == 'Shipment') {
                 $this->btnCancelShipment->Warning = sprintf('This shipment has been modified by another user. You must <a href="shipment_edit.php?intShipmentId=%s">Refresh</a> to complete this shipment.', $this->objShipment->ShipmentId);
             } else {
                 throw new QOptimisticLockingException($objExc->Class);
             }
         }
     }
 }
Example #5
0
    /**
     * Deletes all associated AssetTransactions
     * @return void
     */
    public function DeleteAllAssetTransactions()
    {
        if (is_null($this->intAssetId)) {
            throw new QUndefinedPrimaryKeyException('Unable to call UnassociateAssetTransaction on this unsaved Asset.');
        }
        // Get the Database Object for this Class
        $objDatabase = Asset::GetDatabase();
        // Journaling
        if ($objDatabase->JournalingDatabase) {
            foreach (AssetTransaction::LoadArrayByAssetId($this->intAssetId) as $objAssetTransaction) {
                $objAssetTransaction->Journal('DELETE');
            }
        }
        // Perform the SQL Query
        $objDatabase->NonQuery('
				DELETE FROM
					`asset_transaction`
				WHERE
					`asset_id` = ' . $objDatabase->SqlVariable($this->intAssetId) . '
			');
    }