Debug mailer finished and debug messages removed:
[shipsimu.git] / application / ship-simu / main / companies / class_ShippingCompany.php
index 322a61a2a1e0065d72b40bf0d8ad42afb7a8b8d8..a7f12ef410e559594d6730f2b07a8ad5088e0e44 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, this is free software
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -38,14 +38,14 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
        private $founderInstance = null;
 
        /**
-        * Employed people by this company
+        * Headquarter harbor instance
         */
-       private $employeeList    = null;
+       private $hqInstance      = null;
 
        /**
-        * Headquarter harbor instance
+        * Employed people by this company
         */
-       private $hqInstance      = null;
+       private $employeeList    = null;
 
        /**
         * List of all assigned shipyards
@@ -62,8 +62,11 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
         */
        private $contractList    = null;
 
+       // Exception constants
+       const EXCEPTION_USER_OWNS_NO_COMPANY = 0x200;
+
        /**
-        * Main constructor
+        * Protected constructor
         *
         * @return      void
         */
@@ -72,7 +75,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                parent::__construct(__CLASS__);
 
                // Set description
-               $this->setObjectDescription("Reederei");
+               $this->setObjectDescription("A shipping company class");
 
                // Generate unique ID number
                $this->generateUniqueId();
@@ -81,73 +84,144 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                $this->removeSystemArray();
        }
 
-       // Reederei gruenden (create wegen Namenskonvention)
-       public final static function createShippingCompany ($companyName, Harbor $hqInstance) {
+       /**
+        * Creates an instance of this company class or throws an exception if the
+        * given user owns no company.
+        *
+        * @param       $userInstance           A user class
+        * @return      $companyInstance        Prepared company instance
+        */
+       public final static function createShippingCompany (BaseUser $userInstance) {
                // Get new instance
                $companyInstance = new ShippingCompany();
 
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $companyInstance->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> wird gegr&uuml;ndet.",
-                       __CLASS__,
-                       __LINE__,
-                       $companyName
-               ));
+               // Does the given user owns a company?
+               if ($companyInstance->ifUserParticipatesInCompany($userInstance)) {
+                       // Then do some nastly caching here but don't throw an exception
+                       // because then you will hurt our web helpers... :/
+                       $companyInstance->partialStub("Don&#39;t throw exceptions here.");
+               } // END - if
 
-               // Firmennamen setzen
-               $companyInstance->setCompanyName($companyName);
+               // Init all lists
+               $companyInstance->initCompanyLists();
 
-               // Kuerzel setzen
-               $companyInstance->createShortName();
+               // Return instance
+               return $companyInstance;
+       }
 
-               // Sitz festlegen
-               $companyInstance->setHQInstance($hqInstance);
+       /**
+        * Checks wether the given user participates in a company
+        *
+        * @param       $userInstance   An instance of a user class
+        * @return      $participates   Wether the user participates at lease in one company
+        */
+       protected function ifUserParticipatesInCompany (BaseUser $userInstance)  {
+               // By default no user owns any company... ;)
+               $participates = false;
 
-               // Werftenliste erstellen
-               $companyInstance->createshipyardList();
+               // Get a company database wrapper class
+               $wrapperInstance = ObjectFactory::createObjectByConfiguredName('company_db_wrapper_class');
 
-               // Angestellten-Liste erstellen
-               $companyInstance->createEmployeeList();
+               // Ask the wrapper if this user participates
+               $participates = $wrapperInstance->ifUserParticipatesInCompany($userInstance);
 
-               // Auftragsliste erstellen
-               $companyInstance->createContractList();
+               // Get the result instance
+               $resultInstance = $wrapperInstance->getResultInstance();
 
-               // Clean up a little
-               $companyInstance->removeWidth();
-               $companyInstance->removeHeight();
-               $companyInstance->removeLength();
-               $companyInstance->removeDraught();
-               $companyInstance->removePartInstance();
+               // Caches the result instance here, if set (we don't the wrapper anymore!)
+               if ($resultInstance instanceof SearchableResult) {
+                       // Set the result instance
+                       $this->setResultInstance($resultInstance);
+               } // END - if
 
-               // Instanz zurueckgeben
-               return $companyInstance;
+               // Return result
+               return $participates;
        }
 
-       // Angestellten-Liste erstellen
-       private function createEmployeeList () {
-               if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erh&auml;lt eine Angestelltenliste.",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName()
-               ));
-               $this->employeeList = new FrameworkArrayObject("FakedEmployeeList");
+       /**
+        * Checks wether the current user in registry is the company founder
+        *
+        * @return      $isFounder      Wether the current user is the company founder
+        */
+       public function ifUserIsFounder () {
+               // Default is not the founder
+               $isFounder = false;
+
+               // Get result instance
+               $resultInstance = $this->getResultInstance();
+
+               // Is it set?
+               if ($resultInstance instanceof SearchableResult) {
+                       // Result found so analyse it
+                       $this->partialStub("Check if user is company founder.");
+               } // END - if
+
+               // Return result
+               return $isFounder;
        }
 
-       // Werftenliste erstellen
-       public function createShipyardList () {
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erh&auml;lt eine Werftsliste.",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName()
-               ));
-               $this->shipyardList = new FrameworkArrayObject("FakedShipyardList");
+       /**
+        * Checks wether the current user in registry is the company owner
+        *
+        * @return      $isOwner        Wether the current user is the company owner
+        */
+       public function ifUserIsOwner () {
+               // Default is not the owner
+               $isOwner = false;
+
+               // Get result instance
+               $resultInstance = $this->getResultInstance();
+
+               // Is it set?
+               if ($resultInstance instanceof SearchableResult) {
+                       // Result found so analyse it
+                       $this->partialStub("Check if user is company owner.");
+               } // END - if
+
+               // Return result
+               return $isOwner;
        }
 
-       // Auftragsliste erstellen
-       public function createContractList () {
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erh&auml;lt eine Auftragsliste.",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName()
-               ));
+       /**
+        * Checks wether the current user in registry is an employee in this company
+        *
+        * @return      $isOwner        Wether the current user is an employee in this company
+        */
+       public function ifUserIsEmployee () {
+               // Default is no employee
+               $isEmployee = false;
+
+               // Get result instance
+               $resultInstance = $this->getResultInstance();
+
+               // Is it set?
+               if ($resultInstance instanceof SearchableResult) {
+                       // Result found so he is employee
+                       $isEmployee = true;
+               } // END - if
+
+               // Return result
+               return $isEmployee;
+       }
+
+       //----------------------------------------------------------------------------
+       // From here is very old code which needs to be translated and changed heavily
+       //----------------------------------------------------------------------------
+
+       /**
+        * Intialize all lists
+        *
+        * @return      void
+        * @todo        Maybe we don't need these big lists anymore?! So we can deprecate/remove it
+        */
+       protected function initCompanyLists () {
+               // Employees
+               $this->employeeList = new FrameworkArrayObject("FakedEmployeeList");
+
+               // Ship yards
+               $this->shipyardList = new FrameworkArrayObject("FakedShipyardList");
+
+               // Contracts
                $this->contractList = new FrameworkArrayObject("FakedContractList");
        }
 
@@ -167,17 +241,17 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
        }
 
        // Kuerzel setzen
-       private function createShortName () {
+       private function initShortName () {
                // Mindestens eine Leerstelle?
                $dummy = explode(" ", $this->getCompanyName());
                foreach ($dummy as $part) {
                        $this->shortName .= substr($part, 0, 1);
-               }
+               } // END - if
        }
 
        // Reedereien Werften bauen lassen
        public function createShipyardInHarbor($shipyardName, Harbor $harborInstance) {
-               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>.",
+               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>.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -222,7 +296,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                $amount = (int) $amount;
 
                // Debug-Meldung ausgeben
-               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.",
+               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.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -276,7 +350,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                        $employee->increaseSalary((mt_rand(7, 14) * 100)); // Are 700 to 1400 EUR for the begin okay?
 
                        // Debug message
-                       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.",
+                       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.",
                                __CLASS__,
                                __LINE__,
                                $this->getCompanyName(),
@@ -292,7 +366,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                $personellInstance->resetCache();
 
                // Debug-Meldung ausgeben
-               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.",
+               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.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -302,7 +376,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
 
        // Distribute all personells on all shipyards
        public function distributeAllPersonellOnShipyards () {
-               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).",
+               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).",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -356,7 +430,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                $total = $this->employeeList->count();
 
                // Debug message
-               if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat <strong>%d</strong> Mitarbeiter.",
+               if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat <strong>%d</strong> Mitarbeiter.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -369,7 +443,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
 
        // Getter for total shipyards
        public final function getTotalShipyards () {
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] F&uuml;r die Reederei <strong>%s</strong> werden die Anzahl der Werften in allen H&auml;fen ermittelt.",
+               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.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName()
@@ -385,7 +459,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                $total = $this->shipyardList->count();
 
                // Debug message
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat <strong>%d</strong> Werft(en).",
+               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat <strong>%d</strong> Werft(en).",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -414,7 +488,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                ));
 
                // Debug message
-               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>.",
+               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>.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -445,7 +519,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
 
        // Validate the requested ship type with the company if they can construct it
        public function validateWorksContractShipType (SignableContract $contractInstance) {
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> pr&uuml;ft den Bauauftrag der <strong>%s</strong>.",
+               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>.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -468,7 +542,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                $shipType = $shipInstance->__toString();
 
                // Debug message
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> pr&uuml;ft, ob die <strong>%s</strong> (Typ:<strong>%s</strong>) gebaut werden kann.",
+               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.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -486,7 +560,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                $shipType = (string) $shipType;
 
                // Debug message
-               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&ouml;nnen.",
+               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.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -521,7 +595,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                }
 
                // Debug message
-               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.",
+               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.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -534,7 +608,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
 
        // As a customer the shipping company can add new contracts
        public function addNewWorksContract (SignableContract $contractInstance) {
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erstellt einen Bauauftrag f&uuml;r ein <strong>%s</strong> mit dem Namen <strong>%s</strong>.",
+               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>.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -572,7 +646,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                // Determine if company "signs" own contract (must be done) or with an other party
                if ($this->equals($partnerInstance)) {
                        // With itself
-                       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&uuml;r das <strong>%s</strong> &quot;<strong>%s</strong>&quot;.",
+                       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;.",
                                __CLASS__,
                                __LINE__,
                                $this->getCompanyName(),
@@ -581,7 +655,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                        ));
                } else {
                        // Other external company
-                       if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(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>.",
+                       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>.",
                                __CLASS__,
                                __LINE__,
                                $this->getCompanyName(),