]> git.mxchange.org Git - shipsimu.git/blobdiff - application/shipsimu/classes/class_WorksContract.php
Updated 'core' + renamed 'main' -> 'classes'.
[shipsimu.git] / application / shipsimu / classes / class_WorksContract.php
diff --git a/application/shipsimu/classes/class_WorksContract.php b/application/shipsimu/classes/class_WorksContract.php
new file mode 100644 (file)
index 0000000..bbe2f35
--- /dev/null
@@ -0,0 +1,329 @@
+<?php
+/**
+ * The work constract class which can be used for contract parties
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.shipsimu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class WorksContract extends BaseFrameworkSystem implements SignableContract {
+       // Zukuenftiger Schiffsname
+       private $shipName         = "";
+
+       // Instanz zum Schiffstypen
+       private $shipInstance     = null;
+
+       // Contract partner
+       private $contractPartner  = null;
+
+       // Other contract partner
+       private $contractParty    = null;
+
+       // Is the contract signed?
+       private $signed           = false;
+
+       // Merchant instance
+       private $merchantInstance = null;
+
+       // Konstruktor
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       // Neuen Bauvertrag generieren
+       public static final function createWorksContract ($shipType, $shipName, ContractPartner $partnerInstance) {
+               // Strings absichern
+               $shipType = (string) $shipType;
+               $shipName = (string) $shipName;
+
+               // Get new instance
+               $contractInstance = new WorksContract();
+
+               // Schiffsnamen setzen
+               $contractInstance->setShipName($shipName);
+
+               // Existiert die Klasse ueberhaupt?
+               if (!class_exists($shipType)) {
+                       // Klasse nicht gefunden
+                       throw new NoClassException ($shipType, self::EXCEPTION_CLASS_NOT_FOUND);
+               }
+
+               // Schiff-Instanz temporaer erzeugen und in den Bauvertrag einfuegen
+               $shipInstance = ObjectFactory::createObjectByName($shipType, array($shipName));
+               $contractInstance->setShipInstance($shipInstance);
+
+               // Remove the ship instance
+               unset($shipInstance);
+
+               // Set itself as contract partner
+               $contractInstance->setContractPartner($partnerInstance);
+
+               // Instanz zurueckgeben
+               return $contractInstance;
+       }
+
+       // Setter for ship instance
+       private final function setShipInstance (ConstructableShip $shipInstance) {
+               $this->shipInstance = $shipInstance;
+       }
+
+       // Setter for ship name
+       private final function setShipName ($shipName) {
+               $this->shipName = (string) $shipName;
+       }
+
+       // Getter for ship name
+       public final function getShipName () {
+               return $this->shipName;
+       }
+
+       // Getter for ship instance
+       public final function getShipInstance () {
+               return $this->shipInstance;
+       }
+
+       // Add detail to the contract
+       public function addContractDetails ($shipPart, $parentPart, array $dataArray) {
+               // Secure strings
+               $shipPart   = (string) $shipPart;
+               $parentPart = (string) $parentPart;
+
+               // Debug message
+               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Das Schiffsteil <strong>%s</strong> wird zusammen mit dem Konstruktionsteil <strong>%s</strong> in den Bauvertrag aufgenommen.",
+                       __CLASS__,
+                       __LINE__,
+                       $shipPart,
+                       $parentPart
+               ));
+
+               // Initialize the instance (shall not be done within dynamic part)
+               $partInstance = null;
+
+               // Try to get an instance for this ship part
+               try {
+                       $partInstance = ObjectFactory::createObjectByName($shipPart, $dataArray);
+               } catch (DimNotFoundInArrayException $e) {
+                       $this->debugOutput(sprintf("[main:] Die <strong>%s</strong> konnte nicht vervollst&auml;ndigt werden. Grund: <strong>%s</strong><br />",
+                               $this->getShipInstance()->getShipName(),
+                               $e->getMessage()
+                       ));
+
+                       // Debug message
+                       if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Versuche ein Schiffsteil in den Bauvertrag aufzunehmen.",
+                               __CLASS__,
+                               __LINE__
+                       ));
+
+                       // Is this ship part constructable?
+                       if (!$partInstance instanceof ConstructableShipPart) {
+                               // Ship part not constructable!
+                               throw new ShipPartNotConstructableException(array($shipPart), self::EXCEPTION_NOT_CONSTRUCTABLE);
+                       } elseif ($this->getShipInstance()->createShipPart($partInstance, $parentPart) === false) {
+                               // Schiff konnte nicht gebaut werden!
+                               throw new ShipNotConstructedException(sprintf("Das Schiff <strong>%s</strong> konnte wegen eines Fehlers nicht gebaut werden. Siehe obere Meldungen.",
+                                       $this->getShipInstance()->getShipName()
+                               ));
+                       }
+               } catch (NoClassException $e) {
+                       // Throw it again...
+                       throw new NoClassException($e->getMessage(), $e->getCode());
+               }
+
+               // Get price for this item
+               $price = $this->getMerchantInstance()->getPriceFromList($partInstance);
+
+               // Add price
+               $partInstance->setPrice($price);
+       }
+
+       // Setter for contract partner
+       public final function setContractPartner (ContractPartner $partnerInstance) {
+               $this->contractPartner = $partnerInstance;
+       }
+
+       // Getter for contract partner
+       public final function getContractPartner () {
+               return $this->contractPartner;
+       }
+
+       // Setter for contract party
+       public final function setContractParty (ContractPartner $partyInstance) {
+               $this->contractParty = $partyInstance;
+       }
+
+       // Getter for contract party
+       public final function getContractParty () {
+               return $this->contractParty;
+       }
+
+       // Setter for signature
+       public final function setSigned ($signed) {
+               $this->signed = (boolean) $signed;
+       }
+
+       // Getter for signature
+       public function isSigned () {
+               return $this->signed;
+       }
+
+       // Sign the contract
+       public function signContract (ContractPartner $partnerInstance, ContractPartner $partyInstance) {
+               // Is this contract already signed?
+               if ($this->isSigned()) {
+                       // Throw an exception
+                       throw new ContractAllreadySignedException(array($this, $this->getContractPartner(), $this->getContractParty()), self::EXCEPTION_CONTRACT_ALREADY_SIGNED);
+               }
+
+               // Is the first contract partner still the same?
+               if ($partnerInstance->equals($this->getContractPartner())) {
+                       // Set contract party (other partner is already set)
+                       $this->setContractParty($partyInstance);
+
+                       // Finally sign it
+                       $this->setSigned(true);
+               } else {
+                       // Throw an exception
+                       throw new ContractPartnerMismatchException(array($this, $this->getContractPartner(), $partyInstance), self::EXCEPTION_CONTRACT_PARTNER_MISMATCH);
+               }
+
+               // Debug message
+               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) {
+                       if ($partnerInstance->equals($partyInstance)) {
+                               // With itself
+                               $this->debugOutput(sprintf("[%s:%d] Die <strong>%s</strong> <em><strong>%s</strong></em> stimmt einem Bauvertrag &uuml;ber das <strong>%s</strong> <em><strong>%s</strong></em> zu.",
+                                       __CLASS__,
+                                       __LINE__,
+                                       $partnerInstance->getObjectDescription(),
+                                       $partnerInstance->getCompanyName(),
+                                       $this->getShipInstance()->getObjectDescription(),
+                                       $this->getShipInstance()->getShipName()
+                               ));
+                       } else {
+                               // Other contract party
+                               $this->debugOutput(sprintf("[%s:%d] Die <strong>%s</strong> <em><strong>%s</strong></em> geht mit der <strong>%s</strong> <em><strong>%s</strong></em> einen Bauvertrag &uuml;ber das <strong>%s</strong> <em><strong>%s</strong></em> ein.",
+                                       __CLASS__,
+                                       __LINE__,
+                                       $partnerInstance->getObjectDescription(),
+                                       $partnerInstance->getCompanyName(),
+                                       $partyInstance->getObjectDescription(),
+                                       $partyInstance->getCompanyName(),
+                                       $this->getShipInstance()->getObjectDescription(),
+                                       $this->getShipInstance()->getShipName()
+                               ));
+                       }
+               }
+       }
+
+       // Setter for merchant instance
+       public final function setMerchantInstance (Merchant $merchantInstance) {
+               $this->merchantInstance = $merchantInstance;
+       }
+
+       // Getter for merchant instance
+       public final function getMerchantInstance () {
+               return $this->merchantInstance;
+       }
+
+       // Getter for total price
+       public final function getTotalPrice () {
+               // Get ship instance
+               $shipInstance = $this->getShipInstance();
+
+               // Is this a ship?
+               if (is_null($shipInstance)) {
+                       // Opps! Empty partner instance?
+                       throw new NullPointerException($shipInstance, self::EXCEPTION_IS_NULL_POINTER);
+               } elseif (!is_object($shipInstance)) {
+                       // Not an object! ;-(
+                       throw new InvalidObjectException($shipInstance, self::EXCEPTION_IS_NO_OBJECT);
+               } elseif (!$shipInstance instanceof ConstructableShip) {
+                       // Does not have the required feature (method)
+                       throw new ShipIsInvalidException(array($shipInstance), self::EXCEPTION_INVALID_SHIP_INSTANCE);
+               }
+
+               // Get the structure array
+               $struct = $shipInstance->getStructuresArray();
+
+               // Is this a ship?
+               if (is_null($struct)) {
+                       // Opps! Empty partner instance?
+                       throw new EmptyStructuresListException($this, self::EXCEPTION_EMPTY_STRUCTURES_ARRAY);
+               }
+
+               // Init total price
+               $totalPrice = 0;
+
+               // Iterate through the list
+               for ($iter = $struct->getIterator(); $iter->valid(); $iter->next()) {
+                       // Get item
+                       $item = $iter->current();
+
+                       // Is this a ship?
+                       if (is_null($item)) {
+                               // Opps! Empty partner instance?
+                               throw new NullPointerException($item, self::EXCEPTION_IS_NULL_POINTER);
+                       } elseif (!is_object($item)) {
+                               // Not an object! ;-(
+                               throw new InvalidObjectException($item, self::EXCEPTION_IS_NO_OBJECT);
+                       } elseif (!$item instanceof BaseSimulator) {
+                               // Does not have the required feature (method)
+                               throw new MissingMethodException(array($item, 'getPartInstance'), self::EXCEPTION_MISSING_METHOD);
+                       }
+
+                       // Get part instance
+                       $part = $item->getPartInstance();
+
+                       // Is this a ship?
+                       if (is_null($part)) {
+                               // Opps! Empty partner instance?
+                               throw new NullPointerException($part, self::EXCEPTION_IS_NULL_POINTER);
+                       } elseif (!is_object($part)) {
+                               // Not an object! ;-(
+                               throw new InvalidObjectException($part, self::EXCEPTION_IS_NO_OBJECT);
+                       } elseif (!method_exists($part, 'getPrice')) {
+                               // Does not have the required feature (method)
+                               throw new MissingMethodException(array($part, 'getPrice'), self::EXCEPTION_MISSING_METHOD);
+                       }
+
+                       // Get price for one item
+                       $price = $part->getPrice();
+
+                       // Is there numCabin() available?
+                       if (method_exists($item, 'getNumCabin')) {
+                               // Get total cabin and multiply it with the price
+                               $price = $price * $item->getNumCabin();
+                       }
+
+                       // Add price to total price
+                       $totalPrice += $price;
+               }
+
+               // Total price calculated?
+               if ($totalPrice === 0) {
+                       // Throw exception
+                       throw new TotalPriceNotCalculatedException($this, self::EXCEPTION_TOTAL_PRICE_NOT_CALCULATED);
+               }
+
+               // Return total price
+               return $totalPrice;
+       }
+}
+
+// [EOF]
+?>