]> git.mxchange.org Git - shipsimu.git/blobdiff - application/ship-simu/main/companies/class_ShippingCompany.php
Following things are changed: (in order of class names)
[shipsimu.git] / application / ship-simu / main / companies / class_ShippingCompany.php
index 895a19a9ec7a11d8fc1601699ad3bcd5160ff88f..d3ab6985dec4c5d08e602f645dcaa330de337044 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
  *
@@ -63,7 +63,15 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
        private $contractList    = null;
 
        /**
-        * Main constructor
+        * Database result instance
+        */
+       private $resultInstance = null;
+
+       // Exception constants
+       const EXCEPTION_USER_OWNS_NO_COMPANY = 0x200;
+
+       /**
+        * Protected constructor
         *
         * @return      void
         */
@@ -72,7 +80,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,76 +89,79 @@ 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
+        * @throws      NoShippingCompanyOwnedException         If the user owns no shipping companies
+        */
+       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.<br />\n",
-                       __CLASS__,
-                       __LINE__,
-                       $companyName
-               ));
+               // Does the given user owns a company?
+               if (!$companyInstance->ifUserOwnsShippingCompany($userInstance)) {
+                       // Throw an exception here
+                       throw new NoShippingCompanyOwnedException(array($companyInstance, $userInstance), self::EXCEPTION_USER_OWNS_NO_COMPANY);
+               } // END - if
 
-               // Firmennamen setzen
-               $companyInstance->setCompanyName($companyName);
+               // Set the user instance. We don't care here if he is founder or employee.
+               $companyInstance->setUserInstance($userInstance);
 
-               // Kuerzel setzen
-               $companyInstance->createShortName();
+               // Init all lists
+               $companyInstance->initCompanyLists();
 
-               // Sitz festlegen
-               $companyInstance->setHQInstance($hqInstance);
+               // Return instance
+               return $companyInstance;
+       }
 
-               // Werftenliste erstellen
-               $companyInstance->createshipyardList();
+       /**
+        * Checks wether the given user owns a company or not
+        *
+        * @param       $userInstance   An instance of a user class
+        * @return      $ownsCompany    Wether the user owns at least one company
+        */
+       protected function ifUserOwnsShippingCompany (BaseUser $userInstance)  {
+               // By default no user owns any company... ;)
+               $ownsCompany = false;
 
-               // Angestellten-Liste erstellen
-               $companyInstance->createEmployeeList();
+               // Get a company database wrapper class
+               $wrapperInstance = ObjectFactory::createObjectByConfiguredName('company_db_wrapper_class');
 
-               // Auftragsliste erstellen
-               $companyInstance->createContractList();
+               // Get a search criteria class
+               $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
 
-               // Clean up a little
-               $companyInstance->removeWidth();
-               $companyInstance->removeHeight();
-               $companyInstance->removeLength();
-               $companyInstance->removeDraught();
-               $companyInstance->removePartInstance();
+               // Add the user primary key as a search criteria
+               $criteriaInstance->addCriteria('company_owner', $userInstance->getPrimaryKey());
+               $criteriaInstance->setLimit(1);
 
-               // Instanz zurueckgeben
-               return $companyInstance;
+               // Get the result back
+               $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
+               $resultInstance->debugInstance();
        }
 
-       // 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.<br />\n",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName()
-               ));
+       /**
+        * Intialize all lists
+        *
+        * @return      void
+        */
+       protected function initCompanyLists () {
+               // Employees
                $this->employeeList = new FrameworkArrayObject("FakedEmployeeList");
-       }
 
-       // 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.<br />\n",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName()
-               ));
+               // Ship yards
                $this->shipyardList = new FrameworkArrayObject("FakedShipyardList");
-       }
 
-       // 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.<br />\n",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName()
-               ));
+               // Contracts
                $this->contractList = new FrameworkArrayObject("FakedContractList");
        }
 
+       //----------------------------------------------------------------------------
+       // From here is very old code which needs to be translated and changed heavily
+       //----------------------------------------------------------------------------
+
        // Setter-Methode fuer Firmennamen
        public final function setCompanyName ($companyName) {
                $this->companyName = (string) $companyName;
@@ -167,17 +178,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>.<br />\n",
+               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>.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -198,13 +209,6 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
 
        // Setter fuer Reederei-Gruender
        public final function setCompanyFounder(CompanyEmployee $founderInstance) {
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> ist von <strong>%s %s</strong> gegr&uuml;ndet worden.<br />\n",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName(),
-                       $founderInstance->getSurname(),
-                       $founderInstance->getFamily()
-               ));
                $this->founderInstance = $founderInstance;
        }
 
@@ -215,26 +219,11 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
 
        // Neue(n) Angestellte(n) in Angestellten-Liste aufnehmen
        public function addNewEmployee (SimulatorPersonell $employeeInstance) {
-               if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] <strong>%s %s</strong> tritt der Reederei <strong>%s</strong> als <strong>%s</strong> bei und erh&auml;lt ein Gehalt von <strong>%s</strong>.<br />\n",
-                       __CLASS__,
-                       __LINE__,
-                       $employeeInstance->getSurname(),
-                       $employeeInstance->getFamily(),
-                       $this->getCompanyName(),
-                       $employeeInstance->getObjectDescription(),
-                       $this->formatCurrency($employeeInstance->getSalary())
-               ));
                $this->employeeList->append($employeeInstance);
        }
 
        // Neue Werft in Liste aufnehmen
        public function addNewShipyard (Shipyard $shipyardInstance) {
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erh&auml;lt die Werft <strong>%s</strong> hinzugef&uuml;gt.<br />\n",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName(),
-                       $shipyardInstance->getShipyardName()
-               ));
                $this->shipyardList->append($shipyardInstance);
        }
 
@@ -244,7 +233,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.<br />\n",
+               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.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -258,7 +247,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                if ($totalUnemployed < $amount) {
                        // Reichte nicht aus!
                        throw new ToMuchEmployeesException(array($amount, $personellInstance->getAllUnemployed()), self::EXCEPTION_NOT_ENOUGTH_UNEMPLOYEES);
-               }
+               } // END - if
 
                // Get list for all unemployed people
                $list = $personellInstance->getSpecialPersonellList(false); // Should be cached
@@ -298,7 +287,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.<br />\n",
+                       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.",
                                __CLASS__,
                                __LINE__,
                                $this->getCompanyName(),
@@ -314,7 +303,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.<br />\n",
+               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.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -324,7 +313,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).<br />\n",
+               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).",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -378,7 +367,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.<br />\n",
+               if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat <strong>%d</strong> Mitarbeiter.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -391,7 +380,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.<br />\n",
+               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.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName()
@@ -407,7 +396,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).<br />\n",
+               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> hat <strong>%d</strong> Werft(en).",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -436,7 +425,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>.<br />\n",
+               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>.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -467,7 +456,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>.<br />\n",
+               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>.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -490,7 +479,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.<br />\n",
+               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.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -508,7 +497,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.<br />\n",
+               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.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -543,7 +532,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.<br />\n",
+               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.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -556,7 +545,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>.<br />\n",
+               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>.",
                        __CLASS__,
                        __LINE__,
                        $this->getCompanyName(),
@@ -594,7 +583,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;.<br />\n",
+                       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;.",
                                __CLASS__,
                                __LINE__,
                                $this->getCompanyName(),
@@ -603,7 +592,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>.<br />\n",
+                       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>.",
                                __CLASS__,
                                __LINE__,
                                $this->getCompanyName(),