/**
  * the singleton pattern
  *
  * @return Sales_Controller_Customer
  */
 public static function getInstance()
 {
     if (self::$_instance === NULL) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
 /**
  * export customers
  *
  * @param string $filter JSON encoded string with employee ids for multi export or employee filter
  * @param string $options format or export definition id
  */
 public function exportCustomers($filter, $options)
 {
     $decodedFilter = Zend_Json::decode($filter);
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Export filter: ' . print_r($decodedFilter, TRUE));
     }
     if (!is_array($decodedFilter)) {
         $decodedFilter = array(array('field' => 'id', 'operator' => 'equals', 'value' => $decodedFilter));
     }
     $filter = new Sales_Model_CustomerFilter($decodedFilter);
     parent::_export($filter, Zend_Json::decode($options), Sales_Controller_Customer::getInstance());
 }
 /**
  * resolve address records before setting headers, so we know how much addresses exist
  *
  * @param Sales_Model_CustomerFilter $filter
  * @param Sales_Controller_Customer $controller
  */
 protected function _resolveAddresses($filter, $controller)
 {
     $customers = $controller->search($filter);
     $customerIds = $customers->id;
     $contactIds = array_unique(array_merge($customers->cpextern_id, $customers->cpintern_id));
     unset($customers);
     $be = new Sales_Backend_Address();
     $this->_specialFieldDefinitions = array(array('header' => 'Postal Address', 'identifier' => 'postal_address', 'type' => 'postal'));
     foreach (array('billing', 'delivery') as $type) {
         $maxAddresses = $be->getMaxAddressesByType($customerIds, $type);
         $header = $type == 'billing' ? 'Billing Address' : 'Delivery Address';
         if ($maxAddresses > 0) {
             $i = 0;
             while ($i < $maxAddresses) {
                 $this->_specialFieldDefinitions[] = array('header' => $header, 'identifier' => $type . '_address' . ($i > 0 ? $i + 1 : ''), 'type' => $type, 'index' => $i + 1);
                 $i++;
             }
         }
     }
     $filter = new Sales_Model_AddressFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'customer_id', 'operator' => 'in', 'value' => $customerIds)));
     $this->_addresses = $be->search($filter);
     $this->_contacts = Addressbook_Controller_Contact::getInstance()->getMultiple($contactIds);
 }
 /**
  * create customers and their addresses
  * 
  * @param number $count
  * @return Tinebase_Record_RecordSet
  */
 protected function _createCustomers($count = 4)
 {
     if (!$this->_contactRecords) {
         // each customer may have 5 contacts
         $this->_createContacts($count * 5);
     }
     $this->_customerController = Sales_Controller_Customer::getInstance();
     $this->_customerRecords = new Tinebase_Record_RecordSet('Sales_Model_Customer');
     $this->_addressController = Sales_Controller_Address::getInstance();
     $this->_addressRecords = new Tinebase_Record_RecordSet('Sales_Model_Address');
     $countAll = 0;
     // customers
     $customers = array(array('name' => 'Customer1', 'url' => 'www.customer1.de', 'discount' => 0), array('name' => 'Customer2', 'url' => 'www.customer2.de', 'discount' => 5), array('name' => 'Customer3', 'url' => 'www.customer3.de', 'discount' => 10), array('name' => 'Customer4', 'url' => 'www.customer4.de', 'discount' => 0));
     $i = 0;
     foreach ($customers as $customer) {
         if ($countAll == $count) {
             break;
         }
         $countAll++;
         $customer['cpextern_id'] = $this->_contactRecords->getByIndex($i)->getId();
         $i++;
         $customer['cpintern_id'] = $this->_contactRecords->getByIndex($i)->getId();
         $i++;
         $customer['iban'] = Tinebase_Record_Abstract::generateUID(20);
         $customer['bic'] = Tinebase_Record_Abstract::generateUID(12);
         $customer['credit_term'] = 30;
         $customer['currency'] = 'EUR';
         $customer['currency_trans_rate'] = 1;
         $this->_customerRecords->addRecord($this->_customerController->create(new Sales_Model_Customer($customer)));
     }
     foreach ($this->_customerRecords as $customer) {
         foreach (array('postal', 'billing', 'delivery') as $type) {
             $caddress = $this->_contactRecords->getByIndex($i);
             $address = new Sales_Model_Address(array('customer_id' => $customer->getId(), 'type' => $type, 'prefix1' => $caddress->title, 'prefix2' => $caddress->n_fn, 'street' => $caddress->adr_two_street, 'postalcode' => $caddress->adr_two_postalcode, 'locality' => $caddress->adr_two_locality, 'region' => $caddress->adr_two_region, 'countryname' => $caddress->adr_two_countryname, 'custom1' => $type == 'billing' ? Tinebase_Record_Abstract::generateUID(5) : NULL));
             $this->_addressRecords->addRecord($this->_addressController->create($address));
             $i++;
         }
     }
     $this->_customerRecords->sort('name', 'ASC');
     return $this->_customerRecords;
 }
 /**
  * checks if the number is always set to the correct value
  */
 public function testNumberable()
 {
     $controller = Sales_Controller_Customer::getInstance();
     $record = $controller->create(new Sales_Model_Customer(array('name' => 'auto1')));
     $this->assertEquals(1, $record->number);
     $record = $controller->create(new Sales_Model_Customer(array('name' => 'auto2')));
     $this->assertEquals(2, $record->number);
     // set number to 4, should return the formatted number
     $record = $controller->create(new Sales_Model_Customer(array('name' => 'manu1', 'number' => 4)));
     $this->assertEquals(4, $record->number);
     // the next number should be a number after the manual number
     $record = $controller->create(new Sales_Model_Customer(array('name' => 'auto3')));
     $this->assertEquals(5, $record->number);
 }
 /**
  * deletes existing records
  *
  * @param  array $ids
  * @return string
  */
 public function deleteCustomers($ids)
 {
     return $this->_delete($ids, Sales_Controller_Customer::getInstance());
 }
 /**
  * try to get a customer
  */
 public function testSearchCustomers()
 {
     $customerController = Sales_Controller_Customer::getInstance();
     $i = 0;
     while ($i < 104) {
         $customerController->create(new Sales_Model_Customer(array('name' => Tinebase_Record_Abstract::generateUID())));
         $i++;
     }
     $result = $this->_instance->searchCustomers(array(), array('limit' => 50));
     $this->assertEquals(50, count($result['results']));
     $this->assertGreaterThanOrEqual(104, $result['totalcount']);
 }
 /**
  * test if a user, who has no manage_invoices - right, is able tosave a timeaccount having an invoice linked
  */
 public function testUpdateInvoiceLinkedTimeaccount()
 {
     $this->markTestSkipped('0010492: fix failing invoices and timetracker tests');
     $ta = $this->_getTimeaccount(array('title' => 'to find'), true);
     $cc = Sales_Controller_CostCenter::getInstance()->create(new Sales_Model_CostCenter(array('number' => 1, 'title' => 'test')));
     $customer = Sales_Controller_Customer::getInstance()->create(new Sales_Model_Customer(array('number' => 100, 'name' => 'test', 'description' => 'unittest', 'credit_term' => 1)));
     $address = Sales_Controller_Address::getInstance()->create(new Sales_Model_Address(array('street' => 'teststreet', 'locality' => 'testcity', 'customer_id' => $customer->id, 'postalcode' => 12345)));
     $invoice = Sales_Controller_Invoice::getInstance()->create(new Sales_Model_Invoice(array('description' => 'test', 'address_id' => $address->id, 'date' => Tinebase_DateTime::now(), 'credit_term' => 1, 'type' => 'INVOICE', 'start_date' => Tinebase_DateTime::now(), 'end_date' => Tinebase_DateTime::now()->addMonth(1), 'costcenter_id' => $cc->id)));
     Tinebase_Relations::getInstance()->setRelations('Sales_Model_Invoice', 'Sql', $invoice->id, array(array('related_id' => $ta->id, 'related_model' => 'Timetracker_Model_Timeaccount', 'related_record' => $ta, 'own_degree' => 'sibling', 'type' => 'INVOICE')));
     // fetch user group
     $group = Tinebase_Group::getInstance()->getGroupByName('Users');
     $groupId = $group->getId();
     // create new user
     $user = new Tinebase_Model_FullUser(array('accountLoginName' => 'testuser', 'accountPrimaryGroup' => $groupId, 'accountDisplayName' => 'Test User', 'accountLastName' => 'User', 'accountFirstName' => 'Test', 'accountFullName' => 'Test User', 'accountEmailAddress' => '*****@*****.**'));
     $user = Admin_Controller_User::getInstance()->create($user, 'pw', 'pw');
     // add tt-ta admin right to user role to allow user to update (manage) timeaccounts
     // user has no right to see sales contracts
     $fe = new Admin_Frontend_Json();
     $userRoles = $fe->getRoles('user', array(), array(), 0, 1);
     $userRole = $fe->getRole($userRoles['results'][0]['id']);
     $roleRights = $fe->getRoleRights($userRole['id']);
     $roleMembers = $fe->getRoleMembers($userRole['id']);
     $roleMembers['results'][] = array('name' => 'testuser', 'type' => 'user', 'id' => $user->accountId);
     $app = Tinebase_Application::getInstance()->getApplicationByName('Timetracker');
     $roleRights['results'][] = array('application_id' => $app->getId(), 'right' => Timetracker_Acl_Rights::MANAGE_TIMEACCOUNTS);
     $roleRights['results'][] = array('application_id' => $app->getId(), 'right' => Tinebase_Acl_Rights::ADMIN);
     $fe->saveRole($userRole, $roleMembers['results'], $roleRights['results']);
     // switch to other user
     $this->_testUser = Tinebase_Core::getUser();
     Tinebase_Core::set(Tinebase_Core::USER, $user);
     $ta = $this->_json->getTimeaccount($ta->id);
     $this->assertTrue(empty($ta['relations']), 'relations are not empty: ' . print_r($ta['relations'], true));
     // this must be possible
     $ta = $this->_json->saveTimeaccount($ta);
     Tinebase_Core::set(Tinebase_Core::USER, $this->_testUser);
     $ta = $this->_json->getTimeaccount($ta['id']);
     $this->assertTrue(count($ta['relations']) == 1);
 }
 /**
  * creates some offers
  */
 protected function _createSharedOffers()
 {
     $i = 0;
     $this->_setReferenceDate();
     $customers = Sales_Controller_Customer::getInstance()->getAll('number');
     $orderconfirmations = Sales_Controller_OrderConfirmation::getInstance()->getAll('number');
     foreach ($customers as $customer) {
         $oc = $orderconfirmations->getByIndex($i);
         $i++;
         $relations = array(array('own_model' => 'Sales_Model_Offer', 'own_backend' => Tasks_Backend_Factory::SQL, 'own_id' => NULL, 'own_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'related_model' => 'Sales_Model_Customer', 'related_backend' => Tasks_Backend_Factory::SQL, 'related_id' => $customer->getId(), 'type' => 'OFFER'), array('own_model' => 'Sales_Model_Offer', 'own_backend' => Tasks_Backend_Factory::SQL, 'own_id' => NULL, 'own_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'related_model' => 'Sales_Model_OrderConfirmation', 'related_backend' => Tasks_Backend_Factory::SQL, 'related_id' => $oc->getId(), 'type' => 'OFFER'));
         $customer = $customers->getById($relation->own_id);
         $offer = Sales_Controller_Offer::getInstance()->create(new Sales_Model_Offer(array('number' => $i, 'title' => self::$_de ? 'Angebot für Kunde ' . $customer->name : 'Offer for Customer' . $customer->name, 'description' => 'Created by Tine 2.0 DemoData', 'relations' => $relations)));
     }
 }
 /**
  * save Sales settings
  *
  * @param array config
  * @return array
  */
 public function setConfig($config)
 {
     if (!Tinebase_Core::getUser()->hasRight('Sales', 'admin')) {
         throw new Tinebase_Exception_AccessDenied(_('You do not have admin rights on Sales'));
     }
     Sales_Controller_Customer::validateCurrencyCode($config['ownCurrency']);
     $properties = Sales_Config::getProperties();
     foreach ($config as $configName => $configValue) {
         if (!isset($properties[$configName])) {
             continue;
         }
         if (!isset($properties[$configName]['setByAdminModule'])) {
             continue;
         }
         Sales_Config::getInstance()->set($configName, $configValue);
     }
     return $this->getConfig();
 }