<?php
namespace Bill\Service;

use Bill\Entity\Bill;

/**
 * This service is responsible for adding/editing Bills
 * and changing Bill password.
 */
class BillManager
{
    /**
     * Doctrine entity manager.
     * @var Doctrine\ORM\EntityManager
     */
    private $entityManager;  
    
    /**
     * Constructs the service.
     */
    public function __construct($entityManager) 
    {
        $this->entityManager = $entityManager;
    }
    
    /**
     * This method adds a new Bill.
     */
    public function addBill($data) 
    {
      
        
        // Create new Bill entity.
        $bill = new bill();
        $bill->setDate($data['date']);
        $bill->setValue($data['value']);        
		$bill->setServiceid($data['serviceid']); 
        $bill->setName($data['name']['name']);  
                
        // Add the entity to the entity manager.
        $this->entityManager->persist($bill);
        
        // Apply changes to database.
        $this->entityManager->flush();
        
        return $bill;
    }
    
    /**
     * This method updates data of an existing Bill.
     */
    public function updateBill($bill, $data) 
    {
        // Do not allow to change Bill email if another Bill with such email already exits.
        
        $bill->setDate($data['date']);
        $bill->setValue($data['value']);        
		$bill->setServiceid($data['serviceid']);        
        
        // Apply changes to database.
        $this->entityManager->flush();

        return true;
    }

   
}

