7549af4056c7d5ac0e58a13385f8d89b3d974848
[shipsimu.git] / application / ship-simu / main / companies / class_ShippingCompany.php
1 <?php
2 /**
3  * A shipping company may be founded with this class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class ShippingCompany extends BaseSimulator implements Customer, ContractPartner {
25         /**
26          * Full name of this company
27          */
28         private $companyName     = "Namenlose Reederei";
29
30         /**
31          * Shorted name of this company
32          */
33         private $shortName       = "";
34
35         /**
36          * Instance of the founder
37          */
38         private $founderInstance = null;
39
40         /**
41          * Headquarter harbor instance
42          */
43         private $hqInstance      = null;
44
45         /**
46          * Employed people by this company
47          */
48         private $employeeList    = null;
49
50         /**
51          * List of all assigned shipyards
52          */
53         private $shipyardList   = null;
54
55         /**
56          * List of owned ships
57          */
58         private $ownedShips      = null;
59
60         /**
61          * Work constracts this company is currently working on
62          */
63         private $contractList    = null;
64
65         // Exception constants
66         const EXCEPTION_USER_OWNS_NO_COMPANY = 0x200;
67
68         /**
69          * Protected constructor
70          *
71          * @return      void
72          */
73         protected function __construct () {
74                 // Call parent constructor
75                 parent::__construct(__CLASS__);
76
77                 // Set description
78                 $this->setObjectDescription("A shipping company class");
79
80                 // Generate unique ID number
81                 $this->generateUniqueId();
82
83                 // Clean up a little
84                 $this->removeSystemArray();
85         }
86
87         /**
88          * Creates an instance of this company class or throws an exception if the
89          * given user owns no company.
90          *
91          * @param       $userInstance           A user class
92          * @return      $companyInstance        Prepared company instance
93          */
94         public final static function createShippingCompany (ManageableAccount $userInstance) {
95                 // Get new instance
96                 $companyInstance = new ShippingCompany();
97
98                 // Does the given user owns a company?
99                 if ($companyInstance->ifUserParticipatesInCompany($userInstance)) {
100                         // Then do some nastly caching here but don't throw an exception
101                         // because then you will hurt our web helpers... :/
102                         $companyInstance->partialStub("Don&#39;t throw exceptions here.");
103                 } // END - if
104
105                 // Init all lists
106                 $companyInstance->initCompanyLists();
107
108                 // Return instance
109                 return $companyInstance;
110         }
111
112         /**
113          * Checks wether the given user participates in a company
114          *
115          * @param       $userInstance   An instance of a user class
116          * @return      $participates   Wether the user participates at lease in one company
117          */
118         protected function ifUserParticipatesInCompany (ManageableAccount $userInstance)  {
119                 // By default no user owns any company... ;)
120                 $participates = false;
121
122                 // Get a company database wrapper class
123                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('company_db_wrapper_class');
124
125                 // Ask the wrapper if this user participates
126                 $participates = $wrapperInstance->ifUserParticipatesInCompany($userInstance);
127
128                 // Get the result instance
129                 $resultInstance = $wrapperInstance->getResultInstance();
130
131                 // Caches the result instance here, if set (we don't the wrapper anymore!)
132                 if ($resultInstance instanceof SearchableResult) {
133                         // Set the result instance
134                         $this->setResultInstance($resultInstance);
135                 } // END - if
136
137                 // Return result
138                 return $participates;
139         }
140
141         /**
142          * Checks wether the current user in registry is the company founder
143          *
144          * @return      $isFounder      Wether the current user is the company founder
145          */
146         public function ifUserIsFounder () {
147                 // Default is not the founder
148                 $isFounder = false;
149
150                 // Get result instance
151                 $resultInstance = $this->getResultInstance();
152
153                 // Is it set?
154                 if ($resultInstance instanceof SearchableResult) {
155                         // Result found so analyse it
156                         $this->partialStub("Check if user is company founder.");
157                 } // END - if
158
159                 // Return result
160                 return $isFounder;
161         }
162
163         /**
164          * Checks wether the current user in registry is the company owner
165          *
166          * @return      $isOwner        Wether the current user is the company owner
167          */
168         public function ifUserIsOwner () {
169                 // Default is not the owner
170                 $isOwner = false;
171
172                 // Get result instance
173                 $resultInstance = $this->getResultInstance();
174
175                 // Is it set?
176                 if ($resultInstance instanceof SearchableResult) {
177                         // Result found so analyse it
178                         $this->partialStub("Check if user is company owner.");
179                 } // END - if
180
181                 // Return result
182                 return $isOwner;
183         }
184
185         /**
186          * Checks wether the current user in registry is an employee in this company
187          *
188          * @return      $isOwner        Wether the current user is an employee in this company
189          */
190         public function ifUserIsEmployee () {
191                 // Default is no employee
192                 $isEmployee = false;
193
194                 // Get result instance
195                 $resultInstance = $this->getResultInstance();
196
197                 // Is it set?
198                 if ($resultInstance instanceof SearchableResult) {
199                         // Result found so he is employee
200                         $isEmployee = true;
201                 } // END - if
202
203                 // Return result
204                 return $isEmployee;
205         }
206
207         //----------------------------------------------------------------------------
208         // From here is very old code which needs to be translated and changed heavily
209         //----------------------------------------------------------------------------
210
211         /**
212          * Intialize all lists
213          *
214          * @return      void
215          * @todo        Maybe we don't need these big lists anymore?! So we can deprecate/remove it
216          */
217         protected function initCompanyLists () {
218                 // Employees
219                 $this->employeeList = new FrameworkArrayObject("FakedEmployeeList");
220
221                 // Ship yards
222                 $this->shipyardList = new FrameworkArrayObject("FakedShipyardList");
223
224                 // Contracts
225                 $this->contractList = new FrameworkArrayObject("FakedContractList");
226         }
227
228         // Setter-Methode fuer Firmennamen
229         public final function setCompanyName ($companyName) {
230                 $this->companyName = (string) $companyName;
231         }
232
233         // Getter-Methode fuer Firmennamen
234         public final function getCompanyName () {
235                 return $this->companyName;
236         }
237
238         // Setter-Methode fuer Firmensitz
239         public final function setHQInstance (Harbor $hqInstance) {
240                 $this->hqInstance = $hqInstance;
241         }
242
243         // Kuerzel setzen
244         private function initShortName () {
245                 // Mindestens eine Leerstelle?
246                 $dummy = explode(" ", $this->getCompanyName());
247                 foreach ($dummy as $part) {
248                         $this->shortName .= substr($part, 0, 1);
249                 } // END - if
250         }
251
252         // Reedereien Werften bauen lassen
253         public function createShipyardInHarbor($shipyardName, Harbor $harborInstance) {
254                 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> baut im <strong>%s</strong> eine Werft <strong>%s</strong>.",
255                         __CLASS__,
256                         __LINE__,
257                         $this->getCompanyName(),
258                         $harborInstance->getHarborName(),
259                         $shipyardName
260                 ));
261
262                 // Wird im HQ gebaut?
263                 if ($this->hqInstance->equals($harborInstance)) {
264                         // Die neue Werft wird im HQ gebaut!
265                         $this->hqInstance->addNewShipyardNotify($shipyardName, $this);
266                         // Die Werft drueber in Kenntnis setzen, welcher Reederei sie angehoert
267                 } else {
268                         // Ausserhalb des Heimathafens soll eine Werft gebaut werden
269                         $harborInstance->addNewShipyardNotify($shipyardName, $this);
270                 }
271         }
272
273         // Setter fuer Reederei-Gruender
274         public final function setCompanyFounder(CompanyEmployee $founderInstance) {
275                 $this->founderInstance = $founderInstance;
276         }
277
278         // Getter for founder instance
279         public final function getFounderInstance () {
280                 return $this->founderInstance;
281         }
282
283         // Neue(n) Angestellte(n) in Angestellten-Liste aufnehmen
284         public function addNewEmployee (SimulatorPersonell $employeeInstance) {
285                 $this->employeeList->append($employeeInstance);
286         }
287
288         // Neue Werft in Liste aufnehmen
289         public function addNewShipyard (Shipyard $shipyardInstance) {
290                 $this->shipyardList->append($shipyardInstance);
291         }
292
293         // Neue Mitarbeiter per Zufall einstellen/rekrutieren
294         public function recruitRandomEmployees($amount, SimulatorPersonell $personellInstance) {
295                 // Anzahl Mitarbeiter absichern
296                 $amount = (int) $amount;
297
298                 // Debug-Meldung ausgeben
299                 if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> stellt per Zufall <strong>%d</strong> neue Mitarbeiter ein.",
300                         __CLASS__,
301                         __LINE__,
302                         $this->getCompanyName(),
303                         $amount
304                 ));
305
306                 // Gesamtanzahl verfuegbarer Erwerbsloser holen
307                 $totalUnemployed = $personellInstance->getAllUnemployed();
308
309                 // Existiert die gewuenschte Anzahl freier Arbeiter? (doppelt geht derzeit nicht)
310                 if ($totalUnemployed < $amount) {
311                         // Reichte nicht aus!
312                         throw new ToMuchEmployeesException(array($amount, $personellInstance->getAllUnemployed()), self::EXCEPTION_NOT_ENOUGTH_UNEMPLOYEES);
313                 } // END - if
314
315                 // Get list for all unemployed people
316                 $list = $personellInstance->getSpecialPersonellList(false); // Should be cached
317
318                 // Get iterator of the list
319                 $iterator = $list->getIterator();
320
321                 // Get the requested amount of personell
322                 for ($idx = 0; $idx < $amount; $idx++) {
323                         $employee = null;
324                         // Is this personl unemployed?
325                         while (is_null($employee) || $employee->isEmployed()) {
326                                 // Generate random number
327                                 $pos = mt_rand(0, ($totalUnemployed - 1)); // Don't remove the -1 here:
328                                 // E.g. 100 entries means latest position is 99...
329
330                                 // Seek for the position
331                                 $iterator->seek($pos);
332
333                                 // Is the current position valid?
334                                 if (!$iterator->valid()) {
335                                         // Should normally not happen... :(
336                                         throw new StructuresOutOfBoundsException($idx, self::EXCEPTION_INDEX_OUT_OF_BOUNDS);
337                                 } // END - if
338
339                                 // Get current element
340                                 $employee = $iterator->current();
341                         } // END - while
342
343                         // A dummy just for the description and real class
344                         $dummy = CompanyEmployee::createCompanyEmployee("", "", "M", 1970, 1, 1, $employee->isMarried(), 0);
345
346                         // Make this person employed and give him some money to work
347                         $employee->setEmployed(true);
348                         $employee->setObjectDescription($dummy->getObjectDescription());
349                         $employee->setRealClass($dummy->__toString());
350                         $employee->increaseSalary((mt_rand(7, 14) * 100)); // Are 700 to 1400 EUR for the begin okay?
351
352                         // Debug message
353                         if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> stellt den/die Angestellte(n) <strong>%s %s</strong> ein.",
354                                 __CLASS__,
355                                 __LINE__,
356                                 $this->getCompanyName(),
357                                 $employee->getSurname(),
358                                 $employee->getFamily()
359                         ));
360
361                         // Add this employee
362                         $this->addNewEmployee($employee);
363                 } // End - for
364
365                 // Cache resetten
366                 $personellInstance->resetCache();
367
368                 // Debug-Meldung ausgeben
369                 if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat per Zufall <strong>%d</strong> neue Mitarbeiter eingestellt.",
370                         __CLASS__,
371                         __LINE__,
372                         $this->getCompanyName(),
373                         $amount
374                 ));
375         } // End - method
376
377         // Distribute all personells on all shipyards
378         public function distributeAllPersonellOnShipyards () {
379                 if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> verteilt alle ihre <strong>%d</strong> Mitarbeiter auf alle <strong>%d</strong> Werft(en).",
380                         __CLASS__,
381                         __LINE__,
382                         $this->getCompanyName(),
383                         $this->getTotalEmployee(),
384                         $this->getTotalShipyards()
385                 ));
386
387                 // Do we have some shipyards?
388                 if (is_null($this->shipyardList)) {
389                         // No shipyards created
390                         throw new NoShipyardsConstructedException($this, self::EXCEPTION_HARBOR_HAS_NO_SHIPYARDS);
391                 }
392
393                 // Get iterator for shipyards
394                 $shipyardIter = $this->shipyardList->getIterator();
395
396                 // Iterate through all employees
397                 for ($idx = $this->employeeList->getIterator(); $idx->valid(); $idx->next()) {
398                         // Is the shipyard iterator still okay?
399                         if (!$shipyardIter->valid()) {
400                                 // Rewind to first position
401                                 $shipyardIter->seek(0);
402                         }
403
404                         // Get Shipyard object
405                         $shipyard = $shipyardIter->current();
406
407                         // Is this a Shipyard object?
408                         if (is_null($shipyard)) {
409                                 // No class returned
410                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
411                         } elseif (!is_object($shipyard)) {
412                                 // Not an object! ;-(
413                                 throw new NoObjectException($shipyard, self::EXCEPTION_IS_NO_OBJECT);
414                         } elseif (!$shipyard->isClass("Shipyard")) {
415                                 // Nope, so throw exception
416                                 throw new ClassMismatchException(array($shipyard->__toString(), "Shipyard"), self::EXCEPTION_CLASSES_NOT_MATCHING);
417                         }
418
419                         // Add employee to the shipyard
420                         $shipyard->addNewPersonell($idx->current());
421
422                         // Continue to next shipyard
423                         $shipyardIter->next();
424                 }
425         }
426
427         // Getter for total employees
428         public final function getTotalEmployee () {
429                 // Count all...
430                 $total = $this->employeeList->count();
431
432                 // Debug message
433                 if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat <strong>%d</strong> Mitarbeiter.",
434                         __CLASS__,
435                         __LINE__,
436                         $this->getCompanyName(),
437                         $total
438                 ));
439
440                 // Return amount
441                 return $total;
442         }
443
444         // Getter for total shipyards
445         public final function getTotalShipyards () {
446                 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] F&uuml;r die Reederei <strong>%s</strong> werden die Anzahl der Werften in allen H&auml;fen ermittelt.",
447                         __CLASS__,
448                         __LINE__,
449                         $this->getCompanyName()
450                 ));
451
452                 // Do we have some shipyards?
453                 if (is_null($this->shipyardList)) {
454                         // No shipyards created
455                         throw new NoShipyardsConstructedException($this, self::EXCEPTION_HARBOR_HAS_NO_SHIPYARDS);
456                 }
457
458                 // Get iterator
459                 $total = $this->shipyardList->count();
460
461                 // Debug message
462                 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat <strong>%d</strong> Werft(en).",
463                         __CLASS__,
464                         __LINE__,
465                         $this->getCompanyName(),
466                         $total
467                 ));
468
469                 // Return amount
470                 return $total;
471         }
472
473         // Add a ship type (class) to all shipyards
474         public function addShipTypeToAllShipyards ($shipType) {
475                 // Secure strings
476                 $shipType = (string) $shipType;
477
478                 // Is the class there?
479                 if (!class_exists($shipType)) {
480                         // Throw exception
481                         throw new ClassNotFoundException($shipType, self::EXCEPTION_CLASS_NOT_FOUND);
482                 }
483
484                 // Create dummy ship
485                 eval(sprintf("\$shipInstance = %s::create%s(\"M/S Dummy\");",
486                         $shipType,
487                         $shipType
488                 ));
489
490                 // Debug message
491                 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> baut in allen Werften bald Schiffe vom Typ <strong>%s</strong>.",
492                         __CLASS__,
493                         __LINE__,
494                         $this->getCompanyName(),
495                         $shipInstance->getObjectDescription()
496                 ));
497
498                 // Iterate shipyard list
499                 for ($idx = $this->shipyardList->getIterator(); $idx->valid(); $idx->next()) {
500                         // Get current element
501                         $shipyard = $idx->current();
502
503                         // Is this a shipyard?
504                         if (is_null($shipyard)) {
505                                 // Opps! Empty list?
506                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
507                         } elseif (!is_object($shipyard)) {
508                                 // Not an object! ;-(
509                                 throw new NoObjectException($shipyard, self::EXCEPTION_IS_NO_OBJECT);
510                         } elseif (!$shipyard->isClass("Shipyard")) {
511                                 // Class is not a shipyard
512                                 throw new ClassMismatchException(array($shipyard->__toString(), "Shipyard"), self::EXCEPTION_CLASSES_NOT_MATCHING);
513                         }
514
515                         // Add the new ship type to the shipyard
516                         $shipyard->addNewConstructableShipType($shipType);
517                 }
518         }
519
520         // Validate the requested ship type with the company if they can construct it
521         public function validateWorksContractShipType (SignableContract $contractInstance) {
522                 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> pr&uuml;ft den Bauauftrag der <strong>%s</strong>.",
523                         __CLASS__,
524                         __LINE__,
525                         $this->getCompanyName(),
526                         $contractInstance->getShipName()
527                 ));
528
529                 // First get the ship type
530                 $shipInstance = $contractInstance->getShipInstance();
531
532                 // Ist there a ship instance?
533                 if (is_null($shipInstance)) {
534                         // Opps! Empty entry?
535                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
536                 } elseif (!is_object($shipInstance)) {
537                         // Not an object! ;-(
538                         throw new NoObjectException($shipInstance, self::EXCEPTION_IS_NO_OBJECT);
539                 }
540
541                 // Get it's real class name
542                 $shipType = $shipInstance->__toString();
543
544                 // Debug message
545                 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> pr&uuml;ft, ob die <strong>%s</strong> (Typ:<strong>%s</strong>) gebaut werden kann.",
546                         __CLASS__,
547                         __LINE__,
548                         $this->getCompanyName(),
549                         $contractInstance->getShipName(),
550                         $shipInstance->getObjectDescription()
551                 ));
552
553                 // Now check if the ship type is in any list and return the result
554                 return ($this->isShipTypeConstructable($shipType));
555         }
556
557         // Is the ship type constructable?
558         public function isShipTypeConstructable ($shipType) {
559                 // The type must be a string!
560                 $shipType = (string) $shipType;
561
562                 // Debug message
563                 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> fragt alle Werften ab, ob diese Schiffe vom Typ <strong>%s</strong> bauen k&ouml;nnen.",
564                         __CLASS__,
565                         __LINE__,
566                         $this->getCompanyName(),
567                         $shipType
568                 ));
569
570                 // First everthing is failed...
571                 $result = false;
572
573                 // Iterate through all shipyards
574                 for ($idx = $this->shipyardList->getIterator(); $idx->valid(); $idx->next()) {
575                         // Get current Shipyard instance
576                         $shipyard = $idx->current();
577
578                         // Is this a shipyard?
579                         if (is_null($shipyard)) {
580                                 // Opps! Empty list?
581                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
582                         } elseif (!is_object($shipyard)) {
583                                 // Not an object! ;-(
584                                 throw new NoObjectException($shipyard, self::EXCEPTION_IS_NO_OBJECT);
585                         } elseif (!$shipyard->isClass("Shipyard")) {
586                                 // Class is not a shipyard
587                                 throw new ClassMismatchException(array($shipyard->__toString(), "Shipyard"), self::EXCEPTION_CLASSES_NOT_MATCHING);
588                         }
589
590                         // Validate if the first found shipyard can construct the requested type
591                         $result = $shipyard->isShipTypeConstructable($shipType);
592
593                         // Does this shipyard construct the requested ship type?
594                         if ($result) break; // Then abort the search!
595                 }
596
597                 // Debug message
598                 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat die Suche nach einer Werft beendet, die Schiffe vom Typ <strong>%s</strong> bauen kann.",
599                         __CLASS__,
600                         __LINE__,
601                         $this->getCompanyName(),
602                         $shipType
603                 ));
604
605                 // Return result
606                 return $result;
607         }
608
609         // As a customer the shipping company can add new contracts
610         public function addNewWorksContract (SignableContract $contractInstance) {
611                 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erstellt einen Bauauftrag f&uuml;r ein <strong>%s</strong> mit dem Namen <strong>%s</strong>.",
612                         __CLASS__,
613                         __LINE__,
614                         $this->getCompanyName(),
615                         $contractInstance->getShipInstance()->getObjectDescription(),
616                         $contractInstance->getShipInstance()->getShipName()
617                 ));
618                 $this->contractList->append($contractInstance);
619         }
620
621         // As a customer the shippng company can withdraw from a contract
622         public function withdrawFromContract (SignableContract $contractInstance) {
623                 ApplicationEntryPoint::app_die("WITHDRAW:<pre>".print_r($contractInstance, true)."</pre>");
624         }
625
626         // Get latest added contract instance
627         public final function getLastContractInstance () {
628                 // Get iterator
629                 $iter = $this->contractList->getIterator();
630
631                 // Get latest entry (total - 1)
632                 $iter->seek($iter->count() - 1);
633
634                 // Return entry
635                 return $iter->current();
636         }
637
638         // Sign a contract with an other party which must also implement Customer
639         public function signContract (SignableContract $contractInstance, ContractPartner $partnerInstance) {
640                 // Check wether the other party is our contract partner
641                 if (!$partnerInstance->isContractPartner($contractInstance)) {
642                         // Invalid contract partner!
643                         throw new InvalidContractPartnerException($partnerInstance, self::EXCEPTION_CONTRACT_PARTNER_INVALID);
644                 }
645
646                 // Determine if company "signs" own contract (must be done) or with an other party
647                 if ($this->equals($partnerInstance)) {
648                         // With itself
649                         if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erteilt an sich selbst einen Bauauftrag f&uuml;r das <strong>%s</strong> &quot;<strong>%s</strong>&quot;.",
650                                 __CLASS__,
651                                 __LINE__,
652                                 $this->getCompanyName(),
653                                 $contractInstance->getShipInstance()->getObjectDescription(),
654                                 $contractInstance->getShipInstance()->getShipName()
655                         ));
656                 } else {
657                         // Other external company
658                         if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> unterzeichnet einen Bauauftrag f&uuml;r das <strong>%s</strong> &quot;<strong>%s</strong>&quot; mit der <strong>%s</strong>.",
659                                 __CLASS__,
660                                 __LINE__,
661                                 $this->getCompanyName(),
662                                 $contractInstance->getShipInstance()->getObjectDescription(),
663                                 $contractInstance->getShipInstance()->getShipName(),
664                                 $partnerInstance->getCompanyName()
665                         ));
666                 }
667
668                 // Sign the contract
669                 $contractInstance->signContract($this, $partnerInstance);
670
671                 /**
672                  * @todo        Maybe do something more here...
673                  */
674         }
675
676         // Is this the right contract partner?
677         public function isContractPartner (SignableContract $contractInstance) {
678                 // Get contract partner instance and compare it with $this contract partner
679                 return ($this->equals($contractInstance->getContractPartner()));
680         }
681
682         // Setter for merchant instance
683         public final function setMerchantInstance (Merchant $merchantInstance) {
684                 // Get contract
685                 $contractInstance = $this->getLastContractInstance();
686
687                 if (is_null($contractInstance)) {
688                         // Opps! Empty contract instance?
689                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
690                 } elseif (!is_object($contractInstance)) {
691                         // Not an object! ;-(
692                         throw new NoObjectException($contractInstance, self::EXCEPTION_IS_NO_OBJECT);
693                 } elseif (!$contractInstance->isClass('WorksContract')) {
694                         // Is not a merchant
695                         throw new ClassMismatchException(array($contractInstance->__toString(), "WorksContract"), self::EXCEPTION_CLASSES_NOT_MATCHING);
696                 }
697
698                 // Set the merchant in the contract (for getting prices)
699                 $contractInstance->setMerchantInstance($merchantInstance);
700         }
701 }
702
703 // [EOF]
704 ?>