3 * The work constract class which can be used for contract parties
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team
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 WorksContract extends BaseFrameworkSystem implements SignableContract {
25 // Zukuenftiger Schiffsname
26 private $shipName = "";
28 // Instanz zum Schiffstypen
29 private $shipInstance = null;
32 private $contractPartner = null;
34 // Other contract partner
35 private $contractParty = null;
37 // Is the contract signed?
38 private $signed = false;
41 private $merchantInstance = null;
44 protected function __construct () {
45 // Call parent constructor
46 parent::__construct(__CLASS__);
49 // Neuen Bauvertrag generieren
50 public final static function createWorksContract ($shipType, $shipName, ContractPartner $partnerInstance) {
52 $shipType = (string) $shipType;
53 $shipName = (string) $shipName;
56 $contractInstance = new WorksContract();
58 // Schiffsnamen setzen
59 $contractInstance->setShipName($shipName);
61 // Existiert die Klasse ueberhaupt?
62 if (!class_exists($shipType)) {
63 // Klasse nicht gefunden
64 throw new ClassNotFoundException ($shipType, self::EXCEPTION_CLASS_NOT_FOUND);
67 // Schiff-Instanz temporaer erzeugen und in den Bauvertrag einfuegen
68 $shipInstance = ObjectFactory::createObjectByName($shipType, array($shipName));
69 $contractInstance->setShipInstance($shipInstance);
71 // Remove the ship instance
74 // Set itself as contract partner
75 $contractInstance->setContractPartner($partnerInstance);
77 // Instanz zurueckgeben
78 return $contractInstance;
81 // Setter for ship instance
82 private final function setShipInstance (ConstructableShip $shipInstance) {
83 $this->shipInstance = $shipInstance;
86 // Setter for ship name
87 private final function setShipName ($shipName) {
88 $this->shipName = (string) $shipName;
91 // Getter for ship name
92 public final function getShipName () {
93 return $this->shipName;
96 // Getter for ship instance
97 public final function getShipInstance () {
98 return $this->shipInstance;
101 // Add detail to the contract
102 public function addContractDetails ($shipPart, $parentPart, array $dataArray) {
104 $shipPart = (string) $shipPart;
105 $parentPart = (string) $parentPart;
108 if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Das Schiffsteil <strong>%s</strong> wird zusammen mit dem Konstruktionsteil <strong>%s</strong> in den Bauvertrag aufgenommen.",
115 // Initialize the instance (shall not be done within dynamic part)
116 $partInstance = null;
118 // Try to get an instance for this ship part
120 $partInstance = ObjectFactory::createObjectByName($shipPart, $dataArray);
121 } catch (DimNotFoundInArrayException $e) {
122 $this->debugOutput(sprintf("[main:] Die <strong>%s</strong> konnte nicht vervollständigt werden. Grund: <strong>%s</strong><br />",
123 $this->getShipInstance()->getShipName(),
128 if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Versuche ein Schiffsteil in den Bauvertrag aufzunehmen.",
133 // Is this ship part constructable?
134 if (!$partInstance instanceof ConstructableShipPart) {
135 // Ship part not constructable!
136 throw new ShipPartNotConstructableException(array($shipPart), self::EXCEPTION_NOT_CONSTRUCTABLE);
137 } elseif ($this->getShipInstance()->createShipPart($partInstance, $parentPart) === false) {
138 // Schiff konnte nicht gebaut werden!
139 throw new ShipNotConstructedException(sprintf("Das Schiff <strong>%s</strong> konnte wegen eines Fehlers nicht gebaut werden. Siehe obere Meldungen.",
140 $this->getShipInstance()->getShipName()
143 } catch (ClassNotFoundException $e) {
145 throw new ClassNotFoundException($e->getMessage(), $e->getCode());
148 // Get price for this item
149 $price = $this->getMerchantInstance()->getPriceFromList($partInstance);
152 $partInstance->setPrice($price);
155 // Setter for contract partner
156 public final function setContractPartner (ContractPartner $partnerInstance) {
157 $this->contractPartner = $partnerInstance;
160 // Getter for contract partner
161 public final function getContractPartner () {
162 return $this->contractPartner;
165 // Setter for contract party
166 public final function setContractParty (ContractPartner $partyInstance) {
167 $this->contractParty = $partyInstance;
170 // Getter for contract party
171 public final function getContractParty () {
172 return $this->contractParty;
175 // Setter for signature
176 public final function setSigned ($signed) {
177 $this->signed = (boolean) $signed;
180 // Getter for signature
181 public function isSigned () {
182 return $this->signed;
186 public function signContract (ContractPartner $partnerInstance, ContractPartner $partyInstance) {
187 // Is this contract already signed?
188 if ($this->isSigned()) {
189 // Throw an exception
190 throw new ContractAllreadySignedException(array($this, $this->getContractPartner(), $this->getContractParty()), self::EXCEPTION_CONTRACT_ALREADY_SIGNED);
193 // Is the first contract partner still the same?
194 if ($partnerInstance->equals($this->getContractPartner())) {
195 // Set contract party (other partner is already set)
196 $this->setContractParty($partyInstance);
199 $this->setSigned(true);
201 // Throw an exception
202 throw new ContractPartnerMismatchException(array($this, $this->getContractPartner(), $partyInstance), self::EXCEPTION_CONTRACT_PARTNER_MISMATCH);
206 if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) {
207 if ($partnerInstance->equals($partyInstance)) {
209 $this->debugOutput(sprintf("[%s:%d] Die <strong>%s</strong> <em><strong>%s</strong></em> stimmt einem Bauvertrag über das <strong>%s</strong> <em><strong>%s</strong></em> zu.",
212 $partnerInstance->getObjectDescription(),
213 $partnerInstance->getCompanyName(),
214 $this->getShipInstance()->getObjectDescription(),
215 $this->getShipInstance()->getShipName()
218 // Other contract party
219 $this->debugOutput(sprintf("[%s:%d] Die <strong>%s</strong> <em><strong>%s</strong></em> geht mit der <strong>%s</strong> <em><strong>%s</strong></em> einen Bauvertrag über das <strong>%s</strong> <em><strong>%s</strong></em> ein.",
222 $partnerInstance->getObjectDescription(),
223 $partnerInstance->getCompanyName(),
224 $partyInstance->getObjectDescription(),
225 $partyInstance->getCompanyName(),
226 $this->getShipInstance()->getObjectDescription(),
227 $this->getShipInstance()->getShipName()
233 // Setter for merchant instance
234 public final function setMerchantInstance (Merchant $merchantInstance) {
235 $this->merchantInstance = $merchantInstance;
238 // Getter for merchant instance
239 public final function getMerchantInstance () {
240 return $this->merchantInstance;
243 // Getter for total price
244 public final function getTotalPrice () {
246 $shipInstance = $this->getShipInstance();
249 if (is_null($shipInstance)) {
250 // Opps! Empty partner instance?
251 throw new NullPointerException($shipInstance, self::EXCEPTION_IS_NULL_POINTER);
252 } elseif (!is_object($shipInstance)) {
253 // Not an object! ;-(
254 throw new InvalidObjectException($shipInstance, self::EXCEPTION_IS_NO_OBJECT);
255 } elseif (!$shipInstance instanceof ConstructableShip) {
256 // Does not have the required feature (method)
257 throw new ShipIsInvalidException(array($shipInstance), self::EXCEPTION_INVALID_SHIP_INSTANCE);
260 // Get the structure array
261 $struct = $shipInstance->getStructuresArray();
264 if (is_null($struct)) {
265 // Opps! Empty partner instance?
266 throw new EmptyStructuresListException($this, self::EXCEPTION_EMPTY_STRUCTURES_ARRAY);
272 // Iterate through the list
273 for ($iter = $struct->getIterator(); $iter->valid(); $iter->next()) {
275 $item = $iter->current();
278 if (is_null($item)) {
279 // Opps! Empty partner instance?
280 throw new NullPointerException($item, self::EXCEPTION_IS_NULL_POINTER);
281 } elseif (!is_object($item)) {
282 // Not an object! ;-(
283 throw new InvalidObjectException($item, self::EXCEPTION_IS_NO_OBJECT);
284 } elseif (!$item instanceof BaseSimulator) {
285 // Does not have the required feature (method)
286 throw new MissingMethodException(array($item, 'getPartInstance'), self::EXCEPTION_MISSING_METHOD);
290 $part = $item->getPartInstance();
293 if (is_null($part)) {
294 // Opps! Empty partner instance?
295 throw new NullPointerException($part, self::EXCEPTION_IS_NULL_POINTER);
296 } elseif (!is_object($part)) {
297 // Not an object! ;-(
298 throw new InvalidObjectException($part, self::EXCEPTION_IS_NO_OBJECT);
299 } elseif (!method_exists($part, 'getPrice')) {
300 // Does not have the required feature (method)
301 throw new MissingMethodException(array($part, 'getPrice'), self::EXCEPTION_MISSING_METHOD);
304 // Get price for one item
305 $price = $part->getPrice();
307 // Is there numCabin() available?
308 if (method_exists($item, 'getNumCabin')) {
309 // Get total cabin and multiply it with the price
310 $price = $price * $item->getNumCabin();
313 // Add price to total price
314 $totalPrice += $price;
317 // Total price calculated?
318 if ($totalPrice === 0) {
320 throw new TotalPriceNotCalculatedException($this, self::EXCEPTION_TOTAL_PRICE_NOT_CALCULATED);
323 // Return total price