/** * Rà soát bảng crm_contracts của office, với status = 1, lấy ra customer_id * Nếu account có officeId tương ứng thì bỏ qua * Đối soát lại bảng lead dựa theo customer_id * Nếu lead đã có accountId, bỏ qua * Nếu không, lấy thông tin của lead rà soát xem đã tồn tại account như vậy chưa * Nếu đã tồn tại account, update 1 số thông tin cho account * Nếu không tạo mới account * Update accountId cho leadUser và lead */ public function officeconvertaccountAction() { list($officeAdapter, $officeSql) = $this->createOfficeAdapter(); $dbAdapter = $this->getServiceLocator()->get('dbAdapter'); /*@var $dbAdapter \Zend\Db\Adapter\Adapter */ $dbSql = $this->getServiceLocator()->get('dbSql'); $company = new \Company\Model\Company(); $companyMapper = $this->getServiceLocator()->get('\\Company\\Model\\CompanyMapper'); $groupCompanyIds = $companyMapper->getAllCompanyIdsInGroup(10); $select = $officeSql->select(['cc' => 'crm_contracts']); $select->where(['customer_id IS NOT NULL']); $select->where(['status' => '1']); $select->group(['customer_id']); $select->order(['customer_id']); $paginatorAdapter = new \Zend\Paginator\Adapter\DbSelect($select, $officeAdapter); $paginator = new \Zend\Paginator\Paginator($paginatorAdapter); $paginator->setItemCountPerPage(100); $page = $this->getRequest()->getQuery('page', 1); $totalCreate = $this->getRequest()->getQuery('totalCreate', 0); $totalUpdate = $this->getRequest()->getQuery('totalUpdate', 0); $paginator->setCurrentPageNumber($page); $leadMapper = $this->getServiceLocator()->get('\\Crm\\Model\\LeadMapper'); $informationMapper = $this->getServiceLocator()->get('\\Contact\\Model\\Contact\\InformationMapper'); $accountMapper = $this->getServiceLocator()->get('\\Crm\\Model\\AccountMapper'); foreach ($paginator as $contractRow) { $contractRow = (array) $contractRow; $oneofficeId = $contractRow['customer_id']; //Đối soát lại bảng lead dựa theo customer_id $lead = new \Crm\Model\Lead(); $lead->setOneofficeId($oneofficeId); if (!$leadMapper->isExistedOneofficeId($lead)) { continue; } //Nếu lead đã có accountId, bỏ qua if ($lead->getAccountId()) { continue; } //Nếu không, lấy thông tin của lead rà soát xem đã tồn tại account như vậy chưa $information = new \Contact\Model\Contact\Information(); $information->setItemId($lead->getId()); $information->setType(\Contact\Model\Contact\Information::TYPE_CRM_LEAD); $informations = $informationMapper->fetchAll($information); $accountId = null; if (count($informations)) { foreach ($informations as $information) { $checkInfo = new \Contact\Model\Contact\Information(); $checkInfo->setContent($information->getContent()); $checkInfo->addOption('companyIds', $groupCompanyIds); $checkInfo->setType(\Contact\Model\Contact\Information::TYPE_CRM_ACCOUNT); $checkInfo->setItemType($information->getItemType()); if ($informationMapper->isExisted($checkInfo)) { $accountId = $checkInfo->getItemId(); break; } } } //Nếu như chưa tồn tại account thì tạo mới dựa trên lead if (!$accountId) { $account = new \Crm\Model\Account(); $account->setCompanyId($lead->getCompanyId()); $account->setCityId($lead->getCityId()); $account->exchangeArray(array('companyId' => $lead->getCompanyId(), 'type' => \Crm\Model\Account::TYPE_PERSONAL, 'name' => $lead->getName() ?: $lead->getCompanyName(), 'cityId' => $lead->getCityId() ?: 2, 'districtId' => $lead->getDistrictId() ?: 1, 'address' => $lead->getAddress() ?: '', 'nhanhStoreId' => $lead->getNhanhStoreId(), 'nhanhStoreName' => $lead->getNhanhStoreName())); $account->setCreatedById($this->user()->getIdentity()); $account->setCreatedDate(DateBase::getCurrentDate()); $account->setCreatedDateTime(DateBase::getCurrentDateTime()); $accountMapper->save($account); } else { $account = new \Crm\Model\Account(); $account->setId($accountId); if (!$accountMapper->get($account)) { continue; } } // update information cho account $infor1 = new \Contact\Model\Contact\Information(); $infor1->setType(\Contact\Model\Contact\Information::TYPE_CRM_LEAD); $infor1->setItemId($lead->getId()); $infor2 = new \Contact\Model\Contact\Information(); $infor2->setType(\Contact\Model\Contact\Information::TYPE_CRM_ACCOUNT); $infor2->setItemId($account->getId()); $infoDiffs = $informationMapper->compare($infor1, $infor2); if (count($infoDiffs)) { foreach ($infoDiffs as $infoData) { $infor = new \Contact\Model\Contact\Information(); $infor->exchangeArray($infoData); $infor->setType(\Contact\Model\Contact\Information::TYPE_CRM_ACCOUNT); $infor->setItemId($account->getId()); $infor->setCompanyId($account->getCompanyId()); if (!$informationMapper->isExisted($infor)) { $informationMapper->save($infor); } } } // update accountId cho leadUser và lead $lead->setAccountId($account->getId()); $leadMapper->save($lead); $leadUser = new \Crm\Model\Lead\User(); $leadUserMapper = $this->getServiceLocator()->get('\\Crm\\Model\\Lead\\UserMapper'); $leadUserMapper->updateColumns(['accountId' => $account->getId()], ['leadId' => $lead->getId(), 'accountId IS NULL']); } $this->getViewModel()->setTerminal(true); $this->getViewModel()->setVariable('paginator', $paginator); $this->getViewModel()->setVariable('page', $page); $this->getViewModel()->setVariable('totalPages', $paginator->count() + 1); $this->getViewModel()->setVariable('totalCreate', $totalCreate); $this->getViewModel()->setVariable('totalUpdate', $totalUpdate); if ($paginator->count()) { $this->getViewModel()->setVariable('redirectUri', Uri::build('/system/tool/officeconvertaccount', ['page' => $page + 1, 'totalCreate' => $totalCreate, 'totalUpdate' => $totalUpdate])); } return $this->getViewModel(); }