4 class WorksContract extends BaseFrameworkSystem {
5 // Zukuenftiger Schiffsname
6 private $shipName = "";
8 // Instanz zum Schiffstypen
9 private $shipInstance = null;
12 private $contractPartner = null;
14 // Other contract partner
15 private $contractParty = null;
17 // Is the contract signed?
18 private $signed = false;
21 private $merchantInstance = null;
24 private function __construct () {
25 // Eltern-Konstruktor aufrufen
26 parent::constructor(__CLASS__);
29 if (((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) && (defined('DEBUG_CONSTRUCT'))) {
30 $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruktor erreicht.<br />\n",
36 // Beschreibung setzen
37 $this->setPartDescr("Bauvertrag");
39 // Unique-ID generieren
40 $this->createUniqueID();
43 $this->removeSystemArray();
46 // Neuen Bauvertrag generieren
47 public static function createWorksContract ($shipType, $shipName, ContractPartner $partnerInstance) {
49 $shipType = (string) $shipType;
50 $shipName = (string) $shipName;
52 // Is the other one a contract partner?
53 if (is_null($partnerInstance)) {
54 // Opps! Empty partner instance?
55 throw new NullPointerException($partnerInstance, self::EXCEPTION_IS_NULL_POINTER);
56 } elseif (!is_object($partnerInstance)) {
58 throw new NoObjectException($partnerInstance, self::EXCEPTION_IS_NO_OBJECT);
59 } elseif (!method_exists($partnerInstance, 'isContractPartner')) {
60 // Does not have the required feature (method)
61 throw new MissingMethodException(array($partnerInstance, 'isContractPartner'), self::EXCEPTION_MISSING_METHOD);
65 $contractInstance = new WorksContract();
67 // Debug-Meldung ausgeben
68 if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $contractInstance->getDebugInstance()->output(sprintf("[%s:%d] Neuer Bauvertrag wird für das Schiff <strong>%s</strong> mit der <strong>%s</strong> <strong>%s</strong> erstellt.<br />\n",
72 $partnerInstance->getPartDescr(),
73 $partnerInstance->getCompanyName()
76 // Schiffsnamen setzen
77 $contractInstance->setShipName($shipName);
79 // Existiert die Klasse ueberhaupt?
80 if (!class_exists($shipType)) {
81 // Klasse nicht gefunden
82 throw new ClassNotFoundException ($shipType, 0);
85 // Schiff-Instanz temporaer erzeugen und in den Bauvertrag einfuegen
86 $eval = sprintf("\$contractInstance->setShipInstance(%s::create%s(\"%s\"));",
93 if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $contractInstance->getDebugInstance()->output(sprintf("[%s:%d] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
99 // Execute constructed command
102 // Set itself as contract partner
103 $contractInstance->setContractPartner($partnerInstance);
105 // Instanz zurueckgeben
106 return $contractInstance;
109 // Setter for ship instance
110 private function setShipInstance (ConstructableShip $shipInstance) {
111 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",
114 $shipInstance->getShipName()
116 $this->shipInstance = $shipInstance;
119 // Setter for ship name
120 private function setShipName ($shipName) {
121 if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das neue Schiff wird <strong>%s</strong> heissen.<br />\n",
126 $this->shipName = (string) $shipName;
129 // Getter for ship name
130 public function getShipName () {
131 if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Schiffsname <strong>%s</strong> angefordert.<br />\n",
136 return $this->shipName;
139 // Getter for ship instance
140 public function getShipInstance () {
141 if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Schiff-Instanz angefordert.<br />\n",
145 return $this->shipInstance;
148 // Add detail to the contract
149 public function addContractDetails ($shipPart, $parentPart, array $dataArray) {
151 $shipPart = (string) $shipPart;
152 $parentPart = (string) $parentPart;
155 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",
162 // Initialize the instance (shall not be done within dynamic part)
165 // Prepare the big command for everthing
167 \$instance = ".$shipPart."::create".$shipPart."(";
168 foreach ($dataArray as $pIdx => $parts) {
169 if (is_string($parts)) {
171 $eval .= "\"".$parts."\", ";
172 } elseif (is_array($parts)) {
173 // Ist ein weiteres Array!
175 foreach ($parts as $idx2 => $sub) {
176 $eval .= "'".$idx2."' => ";
177 if (is_string($sub)) {
178 // Ist erneut ein String
179 $eval .= "\"".$sub."\"";
180 } elseif (is_array($sub)) {
181 // Wird nicht mehr unterstuetzt!
182 ApplicationEntryPoint::app_die("No more arrays!");
190 // Letztes Komma entfernen
191 $eval = substr($eval, 0, -2);
194 // Etwas anderes direkt!
195 $eval .= $parts.", ";
199 // Letztes Komma abhaengen
200 $eval = substr($eval, 0, -2);
202 } catch (DimNotFoundInArrayException \$e) {
203 \$this->getDebugInstance()->output(sprintf(\"[main:] Die <strong>%s</strong> konnte nicht vervollständigt werden. Grund: <strong>%s</strong><br />\\n\",
204 \$this->getShipInstance()->getShipName(),
211 if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[Ship:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
218 // Try to add the ship part to the contract
220 if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Versuche ein Schiffsteil in den Bauvertrag aufzunehmen.<br />\n",
224 if (!$instance instanceof ConstructableShipPart) {
225 // Ship part not constructable!
226 throw new ShipPartNotConstructableException(array($shipPart), self::EXCEPTION_NOT_CONSTRUCTABLE);
227 } elseif (!$this->getShipInstance()->createShipPart($instance, $parentPart)) {
228 // Schiff konnte nicht gebaut werden!
229 throw new ShipNotConstructedException(sprintf("Das Schiff <strong>%s</strong> konnte wegen eines Fehlers nicht gebaut werden. Siehe obere Meldungen.",
230 $this->getShipInstance()->getShipName()
233 } catch(ClassNotFoundException $e) {
235 throw new ClassNotFoundException($e->getMessage(), $e->getCode());
238 // Get price for this item
239 $price = $this->getMerchantInstance()->getPriceFromList($instance);
241 // Final debug message
242 if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] <strong>%s</strong> kostet <strong>%s</strong>.<br />\n",
245 $instance->getPartDescr(),
246 $this->getMerchantInstance()->formatCurrency($price)
250 $instance->setPrice($price);
252 // Final debug message
253 if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] <strong>%s</strong> wurde in den Bauvertrag aufgenommen.<br />\n",
256 $instance->getPartDescr()
260 // Setter for contract partner
261 public function setContractPartner (ContractPartner $partnerInstance) {
262 $this->contractPartner = $partnerInstance;
265 // Getter for contract partner
266 public function getContractPartner () {
267 return $this->contractPartner;
270 // Setter for contract party
271 public function setContractParty (ContractPartner $partyInstance) {
272 $this->contractParty = $partyInstance;
275 // Getter for contract party
276 public function getContractParty () {
277 return $this->contractParty;
280 // Setter for signature
281 public function setSigned ($signed) {
282 $this->signed = (boolean) $signed;
285 // Getter for signature
286 public function isSigned () {
287 return $this->signed;
291 public function signContract (ContractPartner $partnerInstance, ContractPartner $partyInstance) {
292 // Is this contract already signed?
293 if ($this->isSigned()) {
294 // Throw an exception
295 throw new ContractAllreadySignedException(array($this, $this->getContractPartner(), $this->getContractParty()), self::EXCEPTION_CONTRACT_ALREADY_SIGNED);
298 // Is the first contract partner still the same?
299 if ($partnerInstance->equals($this->getContractPartner())) {
300 // Set contract party (other partner is already set)
301 $this->setContractParty($partyInstance);
304 $this->setSigned(true);
306 // Throw an exception
307 throw new ContractPartnerMismatchException(array($this, $this->getContractPartner(), $partyInstance), self::EXCEPTION_CONTRACT_PARTNER_MISMATCH);
311 if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) {
312 if ($partnerInstance->equals($partyInstance)) {
314 $this->getDebugInstance()->output(sprintf("[%s:%d] Die <strong>%s</strong> <em><strong>%s</strong></em> stimmt einem Bauvertrag über das <strong>%s</strong> <em><strong>%s</strong></em> zu.<br />\n",
317 $partnerInstance->getPartDescr(),
318 $partnerInstance->getCompanyName(),
319 $this->getShipInstance()->getPartDescr(),
320 $this->getShipInstance()->getShipName()
323 // Other contract party
324 $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 über das <strong>%s</strong> <em><strong>%s</strong></em> ein.<br />\n",
327 $partnerInstance->getPartDescr(),
328 $partnerInstance->getCompanyName(),
329 $partyInstance->getPartDescr(),
330 $partyInstance->getCompanyName(),
331 $this->getShipInstance()->getPartDescr(),
332 $this->getShipInstance()->getShipName()
338 // Setter for merchant instance
339 public function setMerchantInstance (Merchant $merchantInstance) {
340 $this->merchantInstance = $merchantInstance;
343 // Getter for merchant instance
344 public function getMerchantInstance () {
345 return $this->merchantInstance;
348 // Getter for total price
349 public function getTotalPrice () {
351 $shipInstance = $this->getShipInstance();
354 if (is_null($shipInstance)) {
355 // Opps! Empty partner instance?
356 throw new NullPointerException($shipInstance, self::EXCEPTION_IS_NULL_POINTER);
357 } elseif (!is_object($shipInstance)) {
358 // Not an object! ;-(
359 throw new NoObjectException($shipInstance, self::EXCEPTION_IS_NO_OBJECT);
360 } elseif (!$shipInstance instanceof ConstructableShip) {
361 // Does not have the required feature (method)
362 throw new ShipIsInvalidException(array($shipInstance), self::EXCEPTION_INVALID_SHIP_INSTANCE);
365 // Get the structure array
366 $struct = $shipInstance->getStructuresArray();
369 if (is_null($struct)) {
370 // Opps! Empty partner instance?
371 throw new EmptyStructuresListException($this, self::EXCEPTION_EMPTY_STRUCTURES_ARRAY);
377 // Iterate through the list
378 for ($iter = $struct->getIterator(); $iter->valid(); $iter->next()) {
380 $item = $iter->current();
383 if (is_null($item)) {
384 // Opps! Empty partner instance?
385 throw new NullPointerException($item, self::EXCEPTION_IS_NULL_POINTER);
386 } elseif (!is_object($item)) {
387 // Not an object! ;-(
388 throw new NoObjectException($item, self::EXCEPTION_IS_NO_OBJECT);
389 } elseif (!method_exists($item, 'getPartInstance')) {
390 // Does not have the required feature (method)
391 throw new MissingMethodException(array($item, 'getPartInstance'), self::EXCEPTION_MISSING_METHOD);
395 $part = $item->getPartInstance();
398 if (is_null($part)) {
399 // Opps! Empty partner instance?
400 throw new NullPointerException($part, self::EXCEPTION_IS_NULL_POINTER);
401 } elseif (!is_object($part)) {
402 // Not an object! ;-(
403 throw new NoObjectException($part, self::EXCEPTION_IS_NO_OBJECT);
404 } elseif (!method_exists($part, 'getPrice')) {
405 // Does not have the required feature (method)
406 throw new MissingMethodException(array($part, 'getPrice'), self::EXCEPTION_MISSING_METHOD);
409 // Get price for one item
410 $price = $part->getPrice();
412 // Is there numCabin() available?
413 if (method_exists($item, 'getNumCabin')) {
414 // Get total cabin and multiply it with the price
415 $price = $price * $item->getNumCabin();
418 // Add price to total price
419 $totalPrice += $price;
422 // Total price calculated?
423 if ($totalPrice === 0) {
425 throw new TotalPriceNotCalculatedException($this, self::EXCEPTION_TOTAL_PRICE_NOT_CALCULATED);
428 // Return total price
435 public function saveObjectToDatabase () {
436 $this->getDebugInstance()->output(sprintf("[%s:] Stub <strong>%s</strong> erreicht.",