recalculate_hash(); } public function __construct( $hash, $price, $qty = 1 ) { $this->hash = (string)$hash; $this->initial_price = (float)$price; $this->price = (float)$price; $this->qty = (int)$qty; $this->immutable = false; $this->readonly_price = false; } public function inc_qty( $qty ) { $this->qty += $qty; $this->recalculate_hash(); } public function set_qty( $qty ) { $this->qty = $qty; $this->recalculate_hash(); } public function dec_qty( $qty ) { if ( $this->qty < $qty ) { throw new Exception( 'Negative item quantity.' ); } $this->qty -= $qty; $this->recalculate_hash(); } public function is_enough_qty( $qty ) { return $this->qty >= $qty; } public function make_immutable() { $this->immutable = true; $this->recalculate_hash(); } public function make_readonly_price() { $this->readonly_price = true; $this->recalculate_hash(); } public function mark_as_temporary() { $this->temporary = true; $this->recalculate_hash(); } public function is_immutable() { return $this->immutable; } public function is_readonly_price() { return $this->readonly_price; } public function is_temporary() { return $this->temporary; } public function get_price() { return $this->price; } public function get_total_price() { return $this->price * $this->qty; } public function get_hash() { return $this->hash; } public function get_calc_hash() { return $this->calc_hash; } private function recalculate_hash() { $data = array( 'prototype_hash' => $this->hash, 'initial_price' => $this->initial_price, 'price' => $this->price, 'qty' => $this->qty, 'immutable' => $this->immutable, 'temporary' => $this->temporary, ); $this->calc_hash = md5( json_encode( $data ) ); } public function get_qty() { return $this->qty; } public function get_initial_price() { return $this->initial_price; } public function set_price( $rule_id, $price ) { if ( $this->readonly_price ) { return; } if ( ! isset( $this->history[ $rule_id ] ) ) { $this->history[ $rule_id ] = array(); } $this->history[$rule_id][] = $this->price - $price; $this->price = $price; $this->recalculate_hash(); } public function get_history() { return $this->history; } }