diff --git a/application/Api/v1/PreorderApicontroller.php b/application/Api/v1/PreorderApicontroller.php index 628886196..390b15758 100644 --- a/application/Api/v1/PreorderApicontroller.php +++ b/application/Api/v1/PreorderApicontroller.php @@ -241,12 +241,13 @@ class PreorderApicontroller extends mfBaseApicontroller { * search wohneinheit */ $update_unit = false; + $assign_unit = false; $where = "1=1 "; $unit = false; if(count($unit_search)) { //var_dump($unit_search);exit; foreach($unit_search as $field => $value) { - if($field == "stock" || $field == "stiege") continue; // only check for block and tuer + if($field == "stock") continue; // only check for block, stiege and tuer $where .= " AND `$field` = '$value'"; } @@ -260,9 +261,9 @@ class PreorderApicontroller extends mfBaseApicontroller { $res = $this->db()->query($sql); if($this->db()->num_rows($res)) { $unit = $this->db()->fetch_object($res); - //return mfResponse::NotFound(['message' => "Wohneinheit nicht gefunden"]); + } else { + $update_unit = true; } - //var_dump($this->db()->num_rows($res), $this->db()->fetch_object($res)); } else { // if all unit values are empty try to find the unit with all empty values // failure is not an error, but must be checked by a human at some point @@ -282,7 +283,9 @@ class PreorderApicontroller extends mfBaseApicontroller { $res = $this->db()->query($sql); if($this->db()->num_rows($res)) { $unit = $this->db()->fetch_object($res); - $update_unit = true; + } else { + // XXX + //$assign_unit = true; } } @@ -434,6 +437,100 @@ class PreorderApicontroller extends mfBaseApicontroller { } } + + + + //var_dump($this->allow_unit_update, $update_unit);exit; + + // update wohneinheit if allowed + // and unit search data was submitted + + $unit_changed = false; + + if($this->allow_unit_update && $update_unit) { + //var_dump($unit_search); + //var_dump($unit); + //exit; + + // get available units + $exclude_wohneinheit_ids = []; + $preorders_in_hausnummer = PreorderModel::search(['adb_hausnummer_id' => $address->hausnummer_id]); + if(count($preorders_in_hausnummer)) { + + foreach($preorders_in_hausnummer as $pih) { + if($pih->adb_wohneinheit_id) { + $exclude_wohneinheit_ids[] = $pih->adb_wohneinheit_id; + } + } + } + // get units excluding unavailable units + $where = "hausnummer_id=".$address->hausnummer_id; + $where .= " AND block IS NULL"; + $where .= " AND stiege IS NULL"; + $where .= " AND stock IS NULL"; + $where .= " AND tuer IS NULL"; + + if(count($exclude_wohneinheit_ids)) { + $where .= " AND wohneinheit_id NOT IN (". implode(",",$exclude_wohneinheit_ids).")"; + } + $sql = "SELECT * FROM view_wohneinheit WHERE $where"; + $this->log->debug($sql); + $res = $this->db()->query($sql); + if(!$this->db()->num_rows($res)) { + return mfResponse::Forbidden(['message' => "Keine Wohneinheiten verfügbar"]); + } + + $unit_row = $this->db()->fetch_object($res); + $unit = new ADBWohneinheit($unit_row->wohneinheit_id); + if(!$unit->id) { + return mfResponse::InternalServerError(['message' => "unit not found"]); + } + //echo "blah";exit; + + $change_note_data = []; + foreach(['block','stiege','stock','tuer'] as $unit_address_part) { + if($unit_search[$unit_address_part]) { + $unit->$unit_address_part = $unit_search[$unit_address_part]; + $unit_changed = true; + $change_note_data[] = [ + 'field' => $unit_address_part, + 'from' => $unit->_old_data->$unit_address_part, + 'to' => $unit_search[$unit_address_part] + ]; + } + } + + if($unit_changed) { + $change_note_text = ($unit->note) ? $unit->note : ""; + $change_note_text .= date("Y-m-d H:i:s").": "; + foreach($change_note_data as $cn) { + $field = $cn['field']; + $from = ($cn['from'] === null) ? "NULL" : "'".$cn['from']."'"; + $to = ($cn['to'] === null) ? "NULL" : "'".$cn['to']."'"; + $change_note_text .= "$field from $from to $to; "; + } + $change_note_text .= "\n"; + $unit->note = $change_note_text; + + $unit->startTransaction(); + $unit->save(); + } + //var_dump($unit);exit; + + $preorder_data['adb_wohneinheit_id'] = $unit->id; + $preorder_data['oaid'] = $unit->oaid; + } + + + // assign next wohneinheit if no unit search data was submitted + /* + if($this->allow_unit_update && $assign_unit) { + var_dump($unit_search); + var_dump($unit); + exit; + }*/ + + /* * create preorder record */ @@ -443,18 +540,16 @@ class PreorderApicontroller extends mfBaseApicontroller { $preorder_id = $preorder->save(); if(!$preorder_id || !$preorder->ucode) { + if($unit_changed) $unit->rollbackTransaction(); return mfResponse::InternalServerError(); } - // update wohneinheit if allowed - /*if($this->allow_unit_update && $update_unit) { - var_dump($unit_search); - var_dump($unit); - exit; - }*/ - + if($unit_changed) $unit->commitTransaction(); $return = ["code" => $preorder->ucode]; + if($preorder->oaid) { + $return ['oaid'] = $preorder->oaid; + } if($addon_data) { $return["additionalData"] = $addon_data; } diff --git a/lib/mvcfronk/mfBase/mfBaseModel.php b/lib/mvcfronk/mfBase/mfBaseModel.php index 951e85d4f..51cb71dc3 100644 --- a/lib/mvcfronk/mfBase/mfBaseModel.php +++ b/lib/mvcfronk/mfBase/mfBaseModel.php @@ -223,6 +223,18 @@ class mfBaseModel { return "update"; } } + + public function startTransaction() { + $this->db->query("START TRANSACTION"); + } + + public function commitTransaction() { + $this->db->query("COMMIT"); + } + + public function rollbackTransaction() { + $this->db->query("ROLLBACK"); + } // generic functions for entity-classes public function toArray() {