/**
  * Retrieve Mage ID for entity that is replicated with Odoo.
  *
  * @param string $entityName
  * @param int $odooId
  * @return int
  */
 protected function _getMageIdByOdooId($entityName, $odooId)
 {
     $result = null;
     $where = IOdooEntity::ATTR_ODOO_REF . '=' . (int) $odooId;
     $items = $this->_repoBasic->getEntities($entityName, null, $where);
     if (is_array($items) && count($items) == 1) {
         $item = reset($items);
         $result = $item[IOdooEntity::ATTR_MAGE_REF];
     }
     return $result;
 }
 public function getConfigParams()
 {
     $result = [];
     $order = Param::ATTR_GV . ' ASC';
     $data = $this->_repoBasic->getEntities(Param::ENTITY_NAME, null, null, $order);
     foreach ($data as $item) {
         $rankId = $item[Param::ATTR_RANK_ID];
         $result[$rankId] = $item;
     }
     return $result;
 }
 private function _validateWalletsAfterTeam()
 {
     $EXPECT_REPRES_AMOUNT = -6.25;
     $EXPECT_BALANCE = [1 => 0.0, 2 => 0, 3 => 6.25, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0, 13 => 0];
     /* get Asset Type ID foe WALLET_ACTIVE */
     $assetTypeId = $this->_repoTypeAsset->getIdByCode(Cfg::CODE_TYPE_ASSET_WALLET_ACTIVE);
     /* get representative account for WALLET_ACTIVE */
     $reqRepresAcc = new AccGetRepresentativeRequest();
     $reqRepresAcc->setAssetTypeId($assetTypeId);
     $respRepresAcc = $this->_callAccAccount->getRepresentative($reqRepresAcc);
     $represAccId = $respRepresAcc->getData(Account::ATTR_ID);
     /* get data for WALLET_ACTIVE accounts */
     $where = Account::ATTR_ASSET_TYPE_ID . '=' . $assetTypeId;
     $balanceData = $this->_repoBasic->getEntities(Account::ENTITY_NAME, null, $where);
     /* convert balances to form that is relative to customer index (not id) */
     foreach ($balanceData as $one) {
         $accId = $one[Account::ATTR_ID];
         $custId = $one[Account::ATTR_CUST_ID];
         $balance = $one[Account::ATTR_BALANCE];
         if ($accId == $represAccId) {
             $this->assertEquals($EXPECT_REPRES_AMOUNT, $balance);
         } else {
             /* get customer index by customer id */
             $index = $this->_mapCustomerIndexByMageId[$custId];
             $expBalance = $EXPECT_BALANCE[$index];
             $this->assertEquals($expBalance, $balance);
         }
     }
 }
 private function getTaxRuleByCode($code)
 {
     $result = null;
     $entity = Cfg::ENTITY_MAGE_TAX_CALC_RULE;
     $cols = [Cfg::E_TAX_CALC_RULE_A_ID];
     $where = EntityTaxRule::KEY_CODE . '=' . $this->repoGeneric->getConnection()->quote($code);
     $rows = $this->repoGeneric->getEntities($entity, $cols, $where);
     if (is_array($rows)) {
         $one = reset($rows);
         $result = $one[Cfg::E_TAX_CALC_RULE_A_ID];
     }
     return $result;
 }
 private function _validateCompression($calcId)
 {
     $EXP_COUNT = 11;
     $EXP_TREE = [1 => 1, 2 => 1, 3 => 1, 4 => 2, 5 => 2, 6 => 3, 8 => 6, 9 => 6, 11 => 3, 12 => 3, 13 => 3];
     $where = Compress::ATTR_CALC_ID . '=' . $calcId;
     $data = $this->_repoCore->getEntities(Compress::ENTITY_NAME, null, $where);
     $this->assertEquals($EXP_COUNT, count($data));
     foreach ($data as $item) {
         $custId = $item[Compress::ATTR_CUSTOMER_ID];
         $parentId = $item[Compress::ATTR_PARENT_ID];
         $custNdx = $this->_mapCustomerIndexByMageId[$custId];
         $parentNdx = $this->_mapCustomerIndexByMageId[$parentId];
         $this->assertEquals($EXP_TREE[$custNdx], $parentNdx);
     }
 }
 /**
  * IDs for customers with forced qualification.
  *
  * @return array [[Customer::ATTR_CUSTOMER_ID=>..., Customer::ATTR_HUMAN_REF=>...], ...]
  */
 private function _getForcedCustomersIds()
 {
     $mlmIds = array_keys($this->QUALIFIED_CUSTOMERS);
     $where = '';
     foreach ($mlmIds as $one) {
         /* skip first iteration */
         if (strlen($where) > 0) {
             $where .= ' OR ';
         }
         $quoted = $this->_conn->quote($one);
         $where .= Customer::ATTR_HUMAN_REF . "=\"{$quoted}\"";
     }
     $cols = [Customer::ATTR_CUSTOMER_ID, Customer::ATTR_HUMAN_REF];
     $result = $this->_repoBasic->getEntities(Customer::ENTITY_NAME, $cols, $where);
     return $result;
 }
 public function getRepresentativeCustomerId()
 {
     if (is_null($this->_cachedRepresCustId)) {
         $conn = $this->_conn;
         /* there is no cached value for the customer ID, select data from DB */
         $where = Cfg::E_CUSTOMER_A_EMAIL . '=' . $conn->quote(self::CUSTOMER_REPRESENTATIVE_EMAIL);
         $data = $this->_repoGeneric->getEntities(Cfg::ENTITY_MAGE_CUSTOMER, Cfg::E_CUSTOMER_A_ENTITY_ID, $where);
         if (count($data) == 0) {
             $bind = [Cfg::E_CUSTOMER_A_WEBSITE_ID => self::ADMIN_WEBSITE_ID, Cfg::E_CUSTOMER_A_EMAIL => self::CUSTOMER_REPRESENTATIVE_EMAIL];
             $id = $this->_repoGeneric->addEntity(Cfg::ENTITY_MAGE_CUSTOMER, $bind);
             if ($id > 0) {
                 $this->_cachedRepresCustId = $id;
             }
         } else {
             $first = reset($data);
             $this->_cachedRepresCustId = $first[Cfg::E_CUSTOMER_A_ENTITY_ID];
         }
     }
     return $this->_cachedRepresCustId;
 }
 function getConfigParams()
 {
     $order = [CfgParam::ATTR_PSAA . ' DESC', CfgParam::ATTR_GV . ' DESC', CfgParam::ATTR_PV . ' DESC'];
     $result = $this->_repoBasic->getEntities(CfgParam::ENTITY_NAME, null, null, $order);
     return $result;
 }
 /** @inheritdoc */
 public function get($where = null, $order = null, $limit = null, $offset = null, $columns = null, $group = null, $having = null)
 {
     $result = $this->_repoGeneric->getEntities($this->_entityName, null, $where, $order, $limit, $offset);
     return $result;
 }
 /**
  * Return array of the Wallet Active transactions related to [$calcId, ...].
  *
  * @param $calcIds array
  *
  * @return array [ [Account::ATTR_CUST_ID=>$id, Transaction::ATTR_VALUE=>$value], ... ]
  */
 private function _selectWalletTransactionsByCalcIds($calcIds)
 {
     /**
      * SELECT
      * paa.customer_id, SUM(pat.value) as value
      * FROM prxgt_acc_transaction pat
      * LEFT JOIN prxgt_acc_account paa
      * ON pat.credit_acc_id = paa.id
      * WHERE (pat.operation_id = operId1) OR (pat.operation_id = operId2) OR ...
      * GROUP BY paa.customer_id
      */
     /* aliases and tables */
     $asAcc = 'paa';
     $asTrn = 'pat';
     $tblAcc = $this->_resource->getTableName(Account::ENTITY_NAME);
     $tblTrn = $this->_resource->getTableName(Transaction::ENTITY_NAME);
     /* FROM prxgt_acc_transaction */
     $query = $this->_conn->select();
     $cols = [Transaction::ATTR_VALUE => 'SUM(' . Transaction::ATTR_VALUE . ')'];
     $query->from([$asTrn => $tblTrn], $cols);
     // JOIN prxgt_acc_account paa  ON pat.credit_acc_id = paa.id
     $on = "{$asTrn}." . Transaction::ATTR_CREDIT_ACC_ID . "={$asAcc}." . Account::ATTR_ID;
     $cols = [Account::ATTR_CUST_ID];
     $query->joinLeft([$asAcc => $tblAcc], $on, $cols);
     // WHERE (pat.operation_id = operId1) OR (pat.operation_id = operId2) OR ...
     $where = '';
     foreach ($calcIds as $calcId) {
         /* get operation ID by calculation ID */
         $whereLog = LogOpers::ATTR_CALC_ID . '=' . (int) $calcId;
         $data = $this->_repoBasic->getEntities(LogOpers::ENTITY_NAME, null, $whereLog);
         $operId = $data[0][LogOpers::ATTR_OPER_ID];
         // get first entry's 'oper_id' attribute
         /* compose WHERE clause */
         if (strlen($where) > 0) {
             $where .= ' OR ';
         }
         $where .= '(' . $asTrn . '.' . Transaction::ATTR_OPERATION_ID . '=' . (int) $operId . ')';
     }
     $query->where($where);
     $query->group(Account::ATTR_CUST_ID);
     // $sql = (string)$query;
     $result = $this->_conn->fetchAll($query);
     return $result;
 }
 /**
  * Get downline customers referential data (flat, without).
  *
  * @return array [ [customer_id, human_ref, country_code], ...]
  */
 public function getDownlineCustomersData()
 {
     $cols = [Customer::ATTR_CUSTOMER_ID, Customer::ATTR_HUMAN_REF, Customer::ATTR_COUNTRY_CODE];
     $result = $this->_repoBasic->getEntities(Customer::ENTITY_NAME, $cols);
     return $result;
 }