$messages[] = array('text' => nl2br($e->getMessage()), 'strong' => true, 'color' => 'red'); } } } if (count($messages) == 0) { $reload_site = true; } break; case 'device_parts_apply': // apply new quantities and new mountnames, or remove parts from this device for ($i = 0; $i < $device_parts_rowcount; $i++) { $part_id = isset($_REQUEST['id_' . $i]) ? (int) $_REQUEST['id_' . $i] : 0; $quantity = isset($_REQUEST['quantity_' . $i]) ? abs((int) $_REQUEST['quantity_' . $i]) : 0; $mountname = isset($_REQUEST['mountnames_' . $i]) ? trim((string) $_REQUEST['mountnames_' . $i]) : ''; try { $device_part = new DevicePart($database, $current_user, $log, $part_id); if ($quantity > 0) { $device_part->set_attributes(array('quantity' => $quantity, 'mountnames' => $mountname)); } else { $device_part->delete(); } // remove the part from this device } catch (Exception $e) { $messages[] = array('text' => nl2br($e->getMessage()), 'strong' => true, 'color' => 'red'); } } if (count($messages) == 0) { $reload_site = true; } break; case 'book_parts':
/** * @brief Get the minimum quantity which should be ordered * * @param boolean $with_devices @li if true, all parts from devices which are marked as "to order" will be included in the calculation * @li if false, only max(mininstock - instock, 0) will be returned * * @retval integer the minimum order quantity */ public function get_min_order_quantity($with_devices = true) { if ($with_devices) { $count_must_order = 0; // for devices with "order_only_missing_parts == false" $count_should_order = 0; // for devices with "order_only_missing_parts == true" $deviceparts = DevicePart::get_order_device_parts($this->database, $this->current_user, $this->log, $this->get_id()); foreach ($deviceparts as $devicepart) { $device = $devicepart->get_device(); if ($device->get_order_only_missing_parts()) { $count_should_order += $device->get_order_quantity() * $devicepart->get_mount_quantity(); } else { $count_must_order += $device->get_order_quantity() * $devicepart->get_mount_quantity(); } } return $count_must_order + max(0, $this->get_mininstock() - $this->get_instock() + $count_should_order); } else { return max(0, $this->get_mininstock() - $this->get_instock()); } }
/** * @brief Create a new device-part * * @param Database &$database reference to the database object * @param User &$current_user reference to the current user which is logged in * @param Log &$log reference to the Log-object * @param integer $device_id the ID of the device * @param integer $part_id the ID of the part * @param integer $quantity the mount quantity (see DevicePart::set_mount_quantity()) * @param string $mountnames the mountname(s) (see DevicePart::set_mount_name()) * @param boolean $increase_if_exist @li if true, and there is already a DevicePart with the same * part ID + device ID, the mount quantity of the existing * DevicePart will be incremented by $quantity. In addition, * the new mount name ($mountname) will be attached (with a * comma) at the end of the mount name of the existing DevicePart. * @li if false, and there is already a DevicePart with the same * part ID + device ID, this method will throw an exception. * * @retval DevicePart the new device-part * @retval DevicePart the existing device-part, if there is already a DevicePart with * the same part ID + device ID and "$increment_if_exist == true" * * @throws Exception if (this combination of) values is not valid * @throws Exception if there was an error * * @see DBElement::add() */ public static function add(&$database, &$current_user, &$log, $device_id, $part_id, $quantity, $mountnames = '', $increase_if_exist = false) { $existing_devicepart = DevicePart::get_device_part($database, $current_user, $log, $device_id, $part_id); if (is_object($existing_devicepart)) { if ($increase_if_exist) { if (!is_int($quantity) && !ctype_digit($quantity) || $quantity < 0) { debug('error', 'quantity = "' . $quantity . '"', __FILE__, __LINE__, __METHOD__); throw new Exception('Die Bestückungs-Anzahl ist ungültig!'); } $quantity = $existing_devicepart->get_mount_quantity() + $quantity; $old_mountnames = $existing_devicepart->get_mount_names(); if (strlen($mountnames) > 0) { if (strlen($old_mountnames) > 0) { $mountnames = $old_mountnames . ', ' . $mountnames; } } else { $mountnames = $old_mountnames; } $existing_devicepart->set_attributes(array('quantity' => $quantity, 'mountnames' => $mountnames)); return $existing_devicepart; } else { $device = new Device($database, $current_user, $log, $device_id); $part = new Part($database, $current_user, $log, $part_id); throw new Exception('Die Baugruppe "' . $device->get_name() . '" enthält bereits das Bauteil "' . $part->get_name() . '"!'); } } // there is no such DevicePart, so we will create it return parent::add($database, $current_user, $log, 'device_parts', array('id_device' => $device_id, 'id_part' => $part_id, 'quantity' => $quantity, 'mountnames' => $mountnames)); }
/** * @brief Import DeviceParts in a Device * * @note This function uses database transactions. If an error occurs, all changes will be rolled back. * * @note If there are already parts in the device, which are also in the import data array, * the quantity and the mountnames of the part to import will be added to the already existing device-part. * * @param Database &$database reference to the database object * @param User &$current_user reference to the user which is logged in * @param Log &$log reference to the Log-object * @param integer $device_id The ID of the device where the parts should be imported * @param array $data The import data array from "extract_import_data_from_request()" * @param boolean $only_check_data If true, this function will only check if all values in "$data" are valid. * In this case, no parts will be imported! * * @throws Exception if there was an error (maybe the passed data is not valid) */ function import_device_parts(&$database, &$current_user, &$log, $device_id, $data, $only_check_data = false) { try { $transaction_id = $database->begin_transaction(); // start transaction foreach ($data as $row) { $part_id = $row['devicepart_part_id']; $quantity = $row['devicepart_mount_quantity']; $mountnames = $row['devicepart_mount_names']; if ($quantity > 0) { $new_devicepart = DevicePart::add($database, $current_user, $log, $device_id, $part_id, $quantity, $mountnames, true); } } if ($only_check_data) { $database->rollback(); } else { $database->commit($transaction_id); } // commit transaction } catch (Exception $e) { $database->rollback(); // rollback transaction throw new Exception($e->getMessage()); } }
/** * @brief Create a new Device as a copy from this one. All DeviceParts will be copied too. * * @param string $name The name of the new device * @param integer $parent_id The ID of the new device's parent device * @param boolean $with_subdevices If true, all subdevices will be copied too * * @throws Exception if there was an error */ public function copy($name, $parent_id, $with_subdevices = false) { try { if ($with_subdevices && $parent_id > 0) { // check if $parent_id is NOT a child of this device $parent_device = new Device($this->database, $this->current_user, $this->log, $parent_id); if ($parent_device->get_id() == $this->get_id() || $parent_device->is_child_of($this)) { throw new Exception('Eine Baugruppe kann nicht in sich selber kopiert werden!'); } } $transaction_id = $this->database->begin_transaction(); // start transaction $new_device = Device::add($this->database, $this->current_user, $this->log, $name, $parent_id); $device_parts = $this->get_parts(); foreach ($device_parts as $part) { $new_part = DevicePart::add($this->database, $this->current_user, $this->log, $new_device->get_id(), $part->get_part()->get_id(), $part->get_mount_quantity(), $part->get_mount_names()); } if ($with_subdevices) { $subdevices = $this->get_subelements(false); foreach ($subdevices as $device) { $device->copy($device->get_name(), $new_device->get_id(), true); } } $this->database->commit($transaction_id); // commit transaction } catch (Exception $e) { $this->database->rollback(); // rollback transaction throw new Exception("Die Baugruppe \"" . $this->get_name() . "\"konnte nicht kopiert werden!\nGrund: " . $e->getMessage()); } }