getAddress(); $invoice->setStreet($address->getStreet()); $invoice->setCity($address->getCity()); $invoice->setZip($address->getZip()); $invoice->setIc($address->getIc()); $invoice->setDic($address->getDic()); $invoice->setUserId($user->getId()); $invoice->setUserEmail($user->getEmailAddress()); $invoice->setUserName($user->getName()); if(!is_null($representant)) { $invoice->setRepresentantId($representant->getId()); $invoice->setRepresentantName($representant->getName()); $invoice->setRepresentantEmail($representant->getEmailAddress()); } $invoice->setNote($this->note); $invoice->save(); foreach($this->getItems() as $item) { $item->setInvoice($invoice); $item->save(); } return $invoice; } /** * */ public function clear() { $this->items = array(); } /** * * @param Product $product * @param array $values */ public function addProduct($product_id, $amount = 1, $gratis = false) { if($amount <= 0) { return false; } if(!$item = $this->getItemByProductIdAndGratis($product_id, $gratis)) { $item = new InvoiceItem(); $item->setProductById($product_id); $item->setAmount($amount); $item->setIsGratis($gratis); $this->items[$product_id.'-'.(int)$gratis] = $item; } else { $item->setAmount($item->getAmount() + $amount); } } public function getItemByProductIdAndGratis($product_id, $gratis) { return isset($this->items[$product_id.'-'.(int)$gratis]) ? $this->items[$product_id.'-'.(int)$gratis] : false; } /** * * @return array */ public function getItems() { return $this->items; } /** * * @return array */ public function hasItems() { return count($this->items) > 0; } /** * * @return integer */ public function getProductsCount() { $result = 0; foreach($this->items as $item) { $result += $item->getCount(); } return $result; } /** * * @return float */ public function getProductsPrice() { $result = 0; foreach($this->items as $item) { $result += $item->getPriceAll(); } return $result; } public function removeItemByProductIdAndGratis($product_id, $gratis) { if(isset($this->items[$product_id.'-'.(int)$gratis])) { unset($this->items[$product_id.'-'.(int)$gratis]); } } public function setAmountByIndex($index, $amount = 1) { if($amount == 0) { unset($this->items[$index]); } if(isset($this->items[$index])) { $this->items[$index]->setAmount($amount); } } public function hasProblem() { if(($this->getCatProductsPrice() / 100 * 20) < $this->getCatTestersGratisPrice()) { return true; } return false; } public function getCatProductsPrice() { $price = 0; foreach($this->items as $item) { $product = $item->getProduct(); if($product->getCategoryId() == Category::PRODUCT) { $price += $product->getPrice() * $item->getAmount(); } } return $price; } public function getCatTestersGratisPrice() { $price = 0; foreach($this->items as $item) { $product = $item->getProduct(); if($product->getCategoryId() == Category::TESTER && $item->getIsGratis()) { $price += $product->getPrice() * $item->getAmount(); } } return $price; } }