]> git.mxchange.org Git - shipsimu.git/blobdiff - ship-simu/application/ship-simu/main/class_WorksContract.php
Initial import of current development status
[shipsimu.git] / ship-simu / application / ship-simu / main / class_WorksContract.php
diff --git a/ship-simu/application/ship-simu/main/class_WorksContract.php b/ship-simu/application/ship-simu/main/class_WorksContract.php
new file mode 100644 (file)
index 0000000..961ef17
--- /dev/null
@@ -0,0 +1,444 @@
+<?php
+
+// Bauvertrag-Klasse
+class WorksContract extends BaseFrameworkSystem {
+       // 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
+       private function __construct () {
+               // Eltern-Konstruktor aufrufen
+               parent::constructor(__CLASS__);
+
+               // Debug message
+               if (((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) && (defined('DEBUG_CONSTRUCT'))) {
+                       $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruktor erreicht.<br />\n",
+                               __CLASS__,
+                               __LINE__
+                       ));
+               }
+
+               // Beschreibung setzen
+               $this->setPartDescr("Bauvertrag");
+
+               // Unique-ID generieren
+               $this->createUniqueID();
+
+               // Clean up a little
+               $this->removeSystemArray();
+       }
+
+       // Neuen Bauvertrag generieren
+       public static function createWorksContract ($shipType, $shipName, ContractPartner $partnerInstance) {
+               // Strings absichern
+               $shipType = (string) $shipType;
+               $shipName = (string) $shipName;
+
+               // Is the other one a contract partner?
+               if (is_null($partnerInstance)) {
+                       // Opps! Empty partner instance?
+                       throw new NullPointerException($partnerInstance, self::EXCEPTION_IS_NULL_POINTER);
+               } elseif (!is_object($partnerInstance)) {
+                       // Not an object! ;-(
+                       throw new NoObjectException($partnerInstance, self::EXCEPTION_IS_NO_OBJECT);
+               } elseif (!method_exists($partnerInstance, 'isContractPartner')) {
+                       // Does not have the required feature (method)
+                       throw new MissingMethodException(array($partnerInstance, 'isContractPartner'), self::EXCEPTION_MISSING_METHOD);
+               }
+
+               // Instanz holen
+               $contractInstance = new WorksContract();
+
+               // Debug-Meldung ausgeben
+               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $contractInstance->getDebugInstance()->output(sprintf("[%s:%d] Neuer Bauvertrag wird f&uuml;r das Schiff <strong>%s</strong> mit der <strong>%s</strong> <strong>%s</strong> erstellt.<br />\n",
+                       __CLASS__,
+                       __LINE__,
+                       $shipName,
+                       $partnerInstance->getPartDescr(),
+                       $partnerInstance->getCompanyName()
+               ));
+
+               // Schiffsnamen setzen
+               $contractInstance->setShipName($shipName);
+
+               // Existiert die Klasse ueberhaupt?
+               if (!class_exists($shipType)) {
+                       // Klasse nicht gefunden
+                       throw new ClassNotFoundException ($shipType, 0);
+               }
+
+               // Schiff-Instanz temporaer erzeugen und in den Bauvertrag einfuegen
+               $eval = sprintf("\$contractInstance->setShipInstance(%s::create%s(\"%s\"));",
+                       $shipType,
+                       $shipType,
+                       $shipName
+               );
+
+               // Debug message
+               if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $contractInstance->getDebugInstance()->output(sprintf("[%s:%d] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
+                       __CLASS__,
+                       __LINE__,
+                       htmlentities($eval)
+               ));
+
+               // Execute constructed command
+               @eval($eval);
+
+               // Set itself as contract partner
+               $contractInstance->setContractPartner($partnerInstance);
+
+               // Instanz zurueckgeben
+               return $contractInstance;
+       }
+
+       // Setter for ship instance
+       private function setShipInstance (ConstructableShip $shipInstance) {
+               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> wird in den Bauvertrag aufgenommen.<br />\n",
+                       __CLASS__,
+                       __LINE__,
+                       $shipInstance->getShipName()
+               ));
+               $this->shipInstance = $shipInstance;
+       }
+
+       // Setter for ship name
+       private function setShipName ($shipName) {
+               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das neue Schiff wird <strong>%s</strong> heissen.<br />\n",
+                       __CLASS__,
+                       __LINE__,
+                       $shipName
+               ));
+               $this->shipName = (string) $shipName;
+       }
+
+       // Getter for ship name
+       public function getShipName () {
+               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Schiffsname <strong>%s</strong> angefordert.<br />\n",
+                       __CLASS__,
+                       __LINE__,
+                       $this->shipName
+               ));
+               return $this->shipName;
+       }
+
+       // Getter for ship instance
+       public function getShipInstance () {
+               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Schiff-Instanz angefordert.<br />\n",
+                       __CLASS__,
+                       __LINE__
+               ));
+               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->getDebugInstance()->output(sprintf("[%s:%d] Das Schiffsteil <strong>%s</strong> wird zusammen mit dem Konstruktionsteil <strong>%s</strong> in den Bauvertrag aufgenommen.<br />\n",
+                       __CLASS__,
+                       __LINE__,
+                       $shipPart,
+                       $parentPart
+               ));
+
+               // Initialize the instance (shall not be done within dynamic part)
+               $instance = null;
+
+               // Prepare the big command for everthing
+               $eval = "try {
+       \$instance = ".$shipPart."::create".$shipPart."(";
+               foreach ($dataArray as $pIdx => $parts) {
+                       if (is_string($parts)) {
+                               // String einbauen
+                               $eval .= "\"".$parts."\", ";
+                       } elseif (is_array($parts)) {
+                               // Ist ein weiteres Array!
+                               $eval .= "array(";
+                               foreach ($parts as $idx2 => $sub) {
+                                       $eval .= "'".$idx2."' => ";
+                                       if (is_string($sub)) {
+                                               // Ist erneut ein String
+                                               $eval .= "\"".$sub."\"";
+                                       } elseif (is_array($sub)) {
+                                               // Wird nicht mehr unterstuetzt!
+                                               ApplicationEntryPoint::app_die("No more arrays!");
+                                       } else {
+                                               // Direkt anhaengen
+                                               $eval .= $sub;
+                                       }
+                                       $eval .= ", ";
+                               }
+
+                               // Letztes Komma entfernen
+                               $eval = substr($eval, 0, -2);
+                               $eval .= "), ";
+                       } else {
+                               // Etwas anderes direkt!
+                               $eval .= $parts.", ";
+                       }
+               }
+
+               // Letztes Komma abhaengen
+               $eval = substr($eval, 0, -2);
+               $eval .= ");
+} catch (DimNotFoundInArrayException \$e) {
+       \$this->getDebugInstance()->output(sprintf(\"[main:] Die <strong>%s</strong> konnte nicht vervollst&auml;ndigt werden. Grund: <strong>%s</strong><br />\\n\",
+               \$this->getShipInstance()->getShipName(),
+               \$e->getMessage()
+       ));
+       \$instance = null;
+}";
+
+               // Debug message
+               if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[Ship:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
+                       htmlentities($eval)
+               ));
+               
+               // Code ausfuhren
+               @eval($eval);
+
+               // Try to add the ship part to the contract
+               try {
+                       if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Versuche ein Schiffsteil in den Bauvertrag aufzunehmen.<br />\n",
+                               __CLASS__,
+                               __LINE__
+                       ));
+                       if (!$instance instanceof ConstructableShipPart) {
+                               // Ship part not constructable!
+                               throw new ShipPartNotConstructableException(array($shipPart), self::EXCEPTION_NOT_CONSTRUCTABLE);
+                       } elseif (!$this->getShipInstance()->createShipPart($instance, $parentPart)) {
+                               // 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(ClassNotFoundException $e) {
+                       // Throw it again...
+                       throw new ClassNotFoundException($e->getMessage(), $e->getCode());
+               }
+
+               // Get price for this item
+               $price = $this->getMerchantInstance()->getPriceFromList($instance);
+
+               // Final debug message
+               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] <strong>%s</strong> kostet <strong>%s</strong>.<br />\n",
+                       __CLASS__,
+                       __LINE__,
+                       $instance->getPartDescr(),
+                       $this->getMerchantInstance()->formatCurrency($price)
+               ));
+
+               // Add price
+               $instance->setPrice($price);
+
+               // Final debug message
+               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] <strong>%s</strong> wurde in den Bauvertrag aufgenommen.<br />\n",
+                       __CLASS__,
+                       __LINE__,
+                       $instance->getPartDescr()
+               ));
+       }
+
+       // Setter for contract partner
+       public function setContractPartner (ContractPartner $partnerInstance) {
+               $this->contractPartner = $partnerInstance;
+       }
+
+       // Getter for contract partner
+       public function getContractPartner () {
+               return $this->contractPartner;
+       }
+
+       // Setter for contract party
+       public function setContractParty (ContractPartner $partyInstance) {
+               $this->contractParty = $partyInstance;
+       }
+
+       // Getter for contract party
+       public function getContractParty () {
+               return $this->contractParty;
+       }
+
+       // Setter for signature
+       public 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->getDebugInstance()->output(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.<br />\n",
+                                       __CLASS__,
+                                       __LINE__,
+                                       $partnerInstance->getPartDescr(),
+                                       $partnerInstance->getCompanyName(),
+                                       $this->getShipInstance()->getPartDescr(),
+                                       $this->getShipInstance()->getShipName()
+                               ));
+                       } else {
+                               // Other contract party
+                               $this->getDebugInstance()->output(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.<br />\n",
+                                       __CLASS__,
+                                       __LINE__,
+                                       $partnerInstance->getPartDescr(),
+                                       $partnerInstance->getCompanyName(),
+                                       $partyInstance->getPartDescr(),
+                                       $partyInstance->getCompanyName(),
+                                       $this->getShipInstance()->getPartDescr(),
+                                       $this->getShipInstance()->getShipName()
+                               ));
+                       }
+               }
+       }
+
+       // Setter for merchant instance
+       public function setMerchantInstance (Merchant $merchantInstance) {
+               $this->merchantInstance = $merchantInstance;
+       }
+
+       // Getter for merchant instance
+       public function getMerchantInstance () {
+               return $this->merchantInstance;
+       }
+
+       // Getter for total price
+       public 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 NoObjectException($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 NoObjectException($item, self::EXCEPTION_IS_NO_OBJECT);
+                       } elseif (!method_exists($item, 'getPartInstance')) {
+                               // 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 NoObjectException($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;
+       }
+
+       /**
+        * Stub!
+        */
+       public function saveObjectToDatabase () {
+               $this->getDebugInstance()->output(sprintf("[%s:] Stub <strong>%s</strong> erreicht.",
+                       $this->__toString(),
+                       __FUNCTION__
+               ));
+       }
+}
+
+// [EOF]
+?>