3 * A shipping company may be founded with this class
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class ShippingCompany extends BaseSimulator implements Customer, ContractPartner {
26 * Full name of this company
28 private $companyName = "Namenlose Reederei";
31 * Shorted name of this company
33 private $shortName = "";
36 * Instance of the founder
38 private $founderInstance = null;
41 * Employed people by this company
43 private $employeeList = null;
46 * Headquarter harbor instance
48 private $hqInstance = null;
51 * List of all assigned shipyards
53 private $shipyardList = null;
58 private $ownedShips = null;
61 * Work constracts this company is currently working on
63 private $contractList = null;
70 protected function __construct () {
71 // Call parent constructor
72 parent::__construct(__CLASS__);
75 $this->setObjectDescription("Reederei");
77 // Generate unique ID number
78 $this->generateUniqueId();
81 $this->removeSystemArray();
84 // Reederei gruenden (create wegen Namenskonvention)
85 public final static function createShippingCompany ($companyName, Harbor $hqInstance) {
87 $companyInstance = new ShippingCompany();
89 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $companyInstance->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> wird gegründet.",
96 $companyInstance->setCompanyName($companyName);
99 $companyInstance->createShortName();
102 $companyInstance->setHQInstance($hqInstance);
104 // Werftenliste erstellen
105 $companyInstance->createshipyardList();
107 // Angestellten-Liste erstellen
108 $companyInstance->createEmployeeList();
110 // Auftragsliste erstellen
111 $companyInstance->createContractList();
114 $companyInstance->removeWidth();
115 $companyInstance->removeHeight();
116 $companyInstance->removeLength();
117 $companyInstance->removeDraught();
118 $companyInstance->removePartInstance();
120 // Instanz zurueckgeben
121 return $companyInstance;
124 // Angestellten-Liste erstellen
125 private function createEmployeeList () {
126 if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erhält eine Angestelltenliste.",
129 $this->getCompanyName()
131 $this->employeeList = new FrameworkArrayObject("FakedEmployeeList");
134 // Werftenliste erstellen
135 public function createShipyardList () {
136 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erhält eine Werftsliste.",
139 $this->getCompanyName()
141 $this->shipyardList = new FrameworkArrayObject("FakedShipyardList");
144 // Auftragsliste erstellen
145 public function createContractList () {
146 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erhält eine Auftragsliste.",
149 $this->getCompanyName()
151 $this->contractList = new FrameworkArrayObject("FakedContractList");
154 // Setter-Methode fuer Firmennamen
155 public final function setCompanyName ($companyName) {
156 $this->companyName = (string) $companyName;
159 // Getter-Methode fuer Firmennamen
160 public final function getCompanyName () {
161 return $this->companyName;
164 // Setter-Methode fuer Firmensitz
165 public final function setHQInstance (Harbor $hqInstance) {
166 $this->hqInstance = $hqInstance;
170 private function createShortName () {
171 // Mindestens eine Leerstelle?
172 $dummy = explode(" ", $this->getCompanyName());
173 foreach ($dummy as $part) {
174 $this->shortName .= substr($part, 0, 1);
178 // Reedereien Werften bauen lassen
179 public function createShipyardInHarbor($shipyardName, Harbor $harborInstance) {
180 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> baut im <strong>%s</strong> eine Werft <strong>%s</strong>.",
183 $this->getCompanyName(),
184 $harborInstance->getHarborName(),
188 // Wird im HQ gebaut?
189 if ($this->hqInstance->equals($harborInstance)) {
190 // Die neue Werft wird im HQ gebaut!
191 $this->hqInstance->addNewShipyardNotify($shipyardName, $this);
192 // Die Werft drueber in Kenntnis setzen, welcher Reederei sie angehoert
194 // Ausserhalb des Heimathafens soll eine Werft gebaut werden
195 $harborInstance->addNewShipyardNotify($shipyardName, $this);
199 // Setter fuer Reederei-Gruender
200 public final function setCompanyFounder(CompanyEmployee $founderInstance) {
201 $this->founderInstance = $founderInstance;
204 // Getter for founder instance
205 public final function getFounderInstance () {
206 return $this->founderInstance;
209 // Neue(n) Angestellte(n) in Angestellten-Liste aufnehmen
210 public function addNewEmployee (SimulatorPersonell $employeeInstance) {
211 $this->employeeList->append($employeeInstance);
214 // Neue Werft in Liste aufnehmen
215 public function addNewShipyard (Shipyard $shipyardInstance) {
216 $this->shipyardList->append($shipyardInstance);
219 // Neue Mitarbeiter per Zufall einstellen/rekrutieren
220 public function recruitRandomEmployees($amount, SimulatorPersonell $personellInstance) {
221 // Anzahl Mitarbeiter absichern
222 $amount = (int) $amount;
224 // Debug-Meldung ausgeben
225 if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> stellt per Zufall <strong>%d</strong> neue Mitarbeiter ein.",
228 $this->getCompanyName(),
232 // Gesamtanzahl verfuegbarer Erwerbsloser holen
233 $totalUnemployed = $personellInstance->getAllUnemployed();
235 // Existiert die gewuenschte Anzahl freier Arbeiter? (doppelt geht derzeit nicht)
236 if ($totalUnemployed < $amount) {
237 // Reichte nicht aus!
238 throw new ToMuchEmployeesException(array($amount, $personellInstance->getAllUnemployed()), self::EXCEPTION_NOT_ENOUGTH_UNEMPLOYEES);
241 // Get list for all unemployed people
242 $list = $personellInstance->getSpecialPersonellList(false); // Should be cached
244 // Get iterator of the list
245 $iterator = $list->getIterator();
247 // Get the requested amount of personell
248 for ($idx = 0; $idx < $amount; $idx++) {
250 // Is this personl unemployed?
251 while (is_null($employee) || $employee->isEmployed()) {
252 // Generate random number
253 $pos = mt_rand(0, ($totalUnemployed - 1)); // Don't remove the -1 here:
254 // E.g. 100 entries means latest position is 99...
256 // Seek for the position
257 $iterator->seek($pos);
259 // Is the current position valid?
260 if ($iterator->valid()) {
262 $employee = $iterator->current();
264 // Should normally not happen... :(
265 throw new StructuresOutOfBoundsException($idx, self::EXCEPTION_INDEX_OUT_OF_BOUNDS);
269 // A dummy just for the description and real class
270 $dummy = CompanyEmployee::createCompanyEmployee("", "", "M", 1970, 1, 1, $employee->isMarried(), 0);
272 // Make this person employed and give him some money to work
273 $employee->setEmployed(true);
274 $employee->setObjectDescription($dummy->getObjectDescription());
275 $employee->setRealClass($dummy->__toString());
276 $employee->increaseSalary((mt_rand(7, 14) * 100)); // Are 700 to 1400 EUR for the begin okay?
279 if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> stellt den/die Angestellte(n) <strong>%s %s</strong> ein.",
282 $this->getCompanyName(),
283 $employee->getSurname(),
284 $employee->getFamily()
288 $this->addNewEmployee($employee);
292 $personellInstance->resetCache();
294 // Debug-Meldung ausgeben
295 if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat per Zufall <strong>%d</strong> neue Mitarbeiter eingestellt.",
298 $this->getCompanyName(),
303 // Distribute all personells on all shipyards
304 public function distributeAllPersonellOnShipyards () {
305 if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> verteilt alle ihre <strong>%d</strong> Mitarbeiter auf alle <strong>%d</strong> Werft(en).",
308 $this->getCompanyName(),
309 $this->getTotalEmployee(),
310 $this->getTotalShipyards()
313 // Do we have some shipyards?
314 if (is_null($this->shipyardList)) {
315 // No shipyards created
316 throw new NoShipyardsConstructedException($this, self::EXCEPTION_HARBOR_HAS_NO_SHIPYARDS);
319 // Get iterator for shipyards
320 $shipyardIter = $this->shipyardList->getIterator();
322 // Iterate through all employees
323 for ($idx = $this->employeeList->getIterator(); $idx->valid(); $idx->next()) {
324 // Is the shipyard iterator still okay?
325 if (!$shipyardIter->valid()) {
326 // Rewind to first position
327 $shipyardIter->seek(0);
330 // Get Shipyard object
331 $shipyard = $shipyardIter->current();
333 // Is this a Shipyard object?
334 if (is_null($shipyard)) {
336 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
337 } elseif (!is_object($shipyard)) {
338 // Not an object! ;-(
339 throw new NoObjectException($shipyard, self::EXCEPTION_IS_NO_OBJECT);
340 } elseif (!$shipyard->isClass("Shipyard")) {
341 // Nope, so throw exception
342 throw new ClassMismatchException(array($shipyard->__toString(), "Shipyard"), self::EXCEPTION_CLASSES_NOT_MATCHING);
345 // Add employee to the shipyard
346 $shipyard->addNewPersonell($idx->current());
348 // Continue to next shipyard
349 $shipyardIter->next();
353 // Getter for total employees
354 public final function getTotalEmployee () {
356 $total = $this->employeeList->count();
359 if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat <strong>%d</strong> Mitarbeiter.",
362 $this->getCompanyName(),
370 // Getter for total shipyards
371 public final function getTotalShipyards () {
372 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Für die Reederei <strong>%s</strong> werden die Anzahl der Werften in allen Häfen ermittelt.",
375 $this->getCompanyName()
378 // Do we have some shipyards?
379 if (is_null($this->shipyardList)) {
380 // No shipyards created
381 throw new NoShipyardsConstructedException($this, self::EXCEPTION_HARBOR_HAS_NO_SHIPYARDS);
385 $total = $this->shipyardList->count();
388 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat <strong>%d</strong> Werft(en).",
391 $this->getCompanyName(),
399 // Add a ship type (class) to all shipyards
400 public function addShipTypeToAllShipyards ($shipType) {
402 $shipType = (string) $shipType;
404 // Is the class there?
405 if (!class_exists($shipType)) {
407 throw new ClassNotFoundException($shipType, self::EXCEPTION_CLASS_NOT_FOUND);
411 eval(sprintf("\$shipInstance = %s::create%s(\"M/S Dummy\");",
417 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> baut in allen Werften bald Schiffe vom Typ <strong>%s</strong>.",
420 $this->getCompanyName(),
421 $shipInstance->getObjectDescription()
424 // Iterate shipyard list
425 for ($idx = $this->shipyardList->getIterator(); $idx->valid(); $idx->next()) {
426 // Get current element
427 $shipyard = $idx->current();
429 // Is this a shipyard?
430 if (is_null($shipyard)) {
432 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
433 } elseif (!is_object($shipyard)) {
434 // Not an object! ;-(
435 throw new NoObjectException($shipyard, self::EXCEPTION_IS_NO_OBJECT);
436 } elseif (!$shipyard->isClass("Shipyard")) {
437 // Class is not a shipyard
438 throw new ClassMismatchException(array($shipyard->__toString(), "Shipyard"), self::EXCEPTION_CLASSES_NOT_MATCHING);
441 // Add the new ship type to the shipyard
442 $shipyard->addNewConstructableShipType($shipType);
446 // Validate the requested ship type with the company if they can construct it
447 public function validateWorksContractShipType (SignableContract $contractInstance) {
448 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> prüft den Bauauftrag der <strong>%s</strong>.",
451 $this->getCompanyName(),
452 $contractInstance->getShipName()
455 // First get the ship type
456 $shipInstance = $contractInstance->getShipInstance();
458 // Ist there a ship instance?
459 if (is_null($shipInstance)) {
460 // Opps! Empty entry?
461 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
462 } elseif (!is_object($shipInstance)) {
463 // Not an object! ;-(
464 throw new NoObjectException($shipInstance, self::EXCEPTION_IS_NO_OBJECT);
467 // Get it's real class name
468 $shipType = $shipInstance->__toString();
471 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> prüft, ob die <strong>%s</strong> (Typ:<strong>%s</strong>) gebaut werden kann.",
474 $this->getCompanyName(),
475 $contractInstance->getShipName(),
476 $shipInstance->getObjectDescription()
479 // Now check if the ship type is in any list and return the result
480 return ($this->isShipTypeConstructable($shipType));
483 // Is the ship type constructable?
484 public function isShipTypeConstructable ($shipType) {
485 // The type must be a string!
486 $shipType = (string) $shipType;
489 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> fragt alle Werften ab, ob diese Schiffe vom Typ <strong>%s</strong> bauen können.",
492 $this->getCompanyName(),
496 // First everthing is failed...
499 // Iterate through all shipyards
500 for ($idx = $this->shipyardList->getIterator(); $idx->valid(); $idx->next()) {
501 // Get current Shipyard instance
502 $shipyard = $idx->current();
504 // Is this a shipyard?
505 if (is_null($shipyard)) {
507 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
508 } elseif (!is_object($shipyard)) {
509 // Not an object! ;-(
510 throw new NoObjectException($shipyard, self::EXCEPTION_IS_NO_OBJECT);
511 } elseif (!$shipyard->isClass("Shipyard")) {
512 // Class is not a shipyard
513 throw new ClassMismatchException(array($shipyard->__toString(), "Shipyard"), self::EXCEPTION_CLASSES_NOT_MATCHING);
516 // Validate if the first found shipyard can construct the requested type
517 $result = $shipyard->isShipTypeConstructable($shipType);
519 // Does this shipyard construct the requested ship type?
520 if ($result) break; // Then abort the search!
524 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat die Suche nach einer Werft beendet, die Schiffe vom Typ <strong>%s</strong> bauen kann.",
527 $this->getCompanyName(),
535 // As a customer the shipping company can add new contracts
536 public function addNewWorksContract (SignableContract $contractInstance) {
537 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erstellt einen Bauauftrag für ein <strong>%s</strong> mit dem Namen <strong>%s</strong>.",
540 $this->getCompanyName(),
541 $contractInstance->getShipInstance()->getObjectDescription(),
542 $contractInstance->getShipInstance()->getShipName()
544 $this->contractList->append($contractInstance);
547 // As a customer the shippng company can withdraw from a contract
548 public function withdrawFromContract (SignableContract $contractInstance) {
549 ApplicationEntryPoint::app_die("WITHDRAW:<pre>".print_r($contractInstance, true)."</pre>");
552 // Get latest added contract instance
553 public final function getLastContractInstance () {
555 $iter = $this->contractList->getIterator();
557 // Get latest entry (total - 1)
558 $iter->seek($iter->count() - 1);
561 return $iter->current();
564 // Sign a contract with an other party which must also implement Customer
565 public function signContract (SignableContract $contractInstance, ContractPartner $partnerInstance) {
566 // Check wether the other party is our contract partner
567 if (!$partnerInstance->isContractPartner($contractInstance)) {
568 // Invalid contract partner!
569 throw new InvalidContractPartnerException($partnerInstance, self::EXCEPTION_CONTRACT_PARTNER_INVALID);
572 // Determine if company "signs" own contract (must be done) or with an other party
573 if ($this->equals($partnerInstance)) {
575 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erteilt an sich selbst einen Bauauftrag für das <strong>%s</strong> "<strong>%s</strong>".",
578 $this->getCompanyName(),
579 $contractInstance->getShipInstance()->getObjectDescription(),
580 $contractInstance->getShipInstance()->getShipName()
583 // Other external company
584 if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> unterzeichnet einen Bauauftrag für das <strong>%s</strong> "<strong>%s</strong>" mit der <strong>%s</strong>.",
587 $this->getCompanyName(),
588 $contractInstance->getShipInstance()->getObjectDescription(),
589 $contractInstance->getShipInstance()->getShipName(),
590 $partnerInstance->getCompanyName()
595 $contractInstance->signContract($this, $partnerInstance);
598 * @todo Maybe do something more here...
602 // Is this the right contract partner?
603 public function isContractPartner (SignableContract $contractInstance) {
604 // Get contract partner instance and compare it with $this contract partner
605 return ($this->equals($contractInstance->getContractPartner()));
608 // Setter for merchant instance
609 public final function setMerchantInstance (Merchant $merchantInstance) {
611 $contractInstance = $this->getLastContractInstance();
613 if (is_null($contractInstance)) {
614 // Opps! Empty contract instance?
615 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
616 } elseif (!is_object($contractInstance)) {
617 // Not an object! ;-(
618 throw new NoObjectException($contractInstance, self::EXCEPTION_IS_NO_OBJECT);
619 } elseif (!$contractInstance->isClass('WorksContract')) {
621 throw new ClassMismatchException(array($contractInstance->__toString(), "WorksContract"), self::EXCEPTION_CLASSES_NOT_MATCHING);
624 // Set the merchant in the contract (for getting prices)
625 $contractInstance->setMerchantInstance($merchantInstance);