/** * Calculate downline snapshots up to requested date (including). * * @param Request\Calc $request * * @return Response\Calc */ public function calc(Request\Calc $request) { $result = new Response\Calc(); $this->_logger->info("New downline snapshot calculation is requested."); $periodTo = $request->getDatestampTo(); $def = $this->_manTrans->begin(); try { /* get the last date with calculated snapshots */ $reqLast = new Request\GetLastDate(); /** @var $resp Response\GetLastDate */ $respLast = $this->getLastDate($reqLast); $lastDatestamp = $respLast->getLastDate(); /* get the snapshot on the last date */ $snapshot = $this->_repoSnap->getStateOnDate($lastDatestamp); /* get change log for the period */ $tsFrom = $this->_toolPeriod->getTimestampNextFrom($lastDatestamp); $tsTo = $this->_toolPeriod->getTimestampTo($periodTo); $changelog = $this->_repoChange->getChangesForPeriod($tsFrom, $tsTo); /* calculate snapshots for the period */ $updates = $this->_subCalc->calcSnapshots($snapshot, $changelog); /* save new snapshots in DB */ $this->_repoSnap->saveCalculatedUpdates($updates); $this->_manTrans->commit($def); $result->markSucceed(); } finally { $this->_manTrans->end($def); } return $result; }
public function calc(Request\Calc $request) { $result = new Response\Calc(); $assetTypeId = $request->getAssetTypeId(); $dateTo = $request->getDateTo(); /* get the last balance date */ $reqLastDate = new Request\GetLastDate(); $reqLastDate->setData(Request\GetLastDate::ASSET_TYPE_ID, $assetTypeId); $respLastDate = $this->getLastDate($reqLastDate); $lastDate = $respLastDate->getLastDate(); $balances = $this->_repoMod->getBalancesOnDate($assetTypeId, $lastDate); /* get transactions for period */ $dtFrom = $this->_toolPeriod->getTimestampFrom($lastDate, IPeriod::TYPE_DAY); $dtTo = $this->_toolPeriod->getTimestampTo($dateTo, IPeriod::TYPE_DAY); $trans = $this->_repoMod->getTransactionsForPeriod($assetTypeId, $dtFrom, $dtTo); $updates = $this->_subCalcSimple->calcBalances($balances, $trans); $this->_repoMod->updateBalances($updates); $result->markSucceed(); return $result; }
/** * SELECT * SUM(pps.total) * FROM `prxgt_pv_sale` AS `pps` * WHERE (pps.date_paid >= '2016-01-01 08:00:00') * AND (pps.date_paid <= '2017-01-01 07:59:59') * * @param string $dsFrom * @param string $dsTo */ function getSalesOrdersPvForPeriod($dsFrom, $dsTo) { $tsFrom = $this->_toolPeriod->getTimestampFrom($dsFrom); $tsTo = $this->_toolPeriod->getTimestampTo($dsTo); /* aliases and tables */ $asSummary = 'summary'; $asPv = 'pps'; $tblPv = $this->_resource->getTableName(PvSale::ENTITY_NAME); // SELECT FROM prxgt_pv_sale pps $query = $this->_conn->select(); $query->from([$asPv => $tblPv], [$asSummary => 'SUM(' . PvSale::ATTR_TOTAL . ')']); // where $whereFrom = $asPv . '.' . PvSale::ATTR_DATE_PAID . '>=' . $this->_conn->quote($tsFrom); $whereTo = $asPv . '.' . PvSale::ATTR_DATE_PAID . '<=' . $this->_conn->quote($tsTo); $query->where("{$whereFrom} AND {$whereTo}"); // $sql = (string)$query; $result = $this->_conn->fetchOne($query); return $result; }
/** * @param Request\PvWriteOff $request * * @return Response\PvWriteOff */ public function pvWriteOff(Request\PvWriteOff $request) { $result = new Response\PvWriteOff(); $datePerformed = $request->getDatePerformed(); $this->_logger->info("'PV Write Off' calculation is started."); $reqGetPeriod = new PeriodGetForWriteOffRequest(); $respGetPeriod = $this->_callPeriod->getForWriteOff($reqGetPeriod); if ($respGetPeriod->isSucceed()) { if ($respGetPeriod->hasNoPvTransactionsYet()) { $this->_logger->info("There is no PV transactions yet. Nothing to calculate."); $result->markSucceed(); } else { $def = $this->_manTrans->begin(); try { /* working vars */ $periodData = $respGetPeriod->getPeriodData(); $periodId = $periodData[Period::ATTR_ID]; $calcData = $respGetPeriod->getCalcData(); $calcId = $calcData[Calculation::ATTR_ID]; $periodBegin = $periodData[Period::ATTR_DSTAMP_BEGIN]; $periodEnd = $periodData[Period::ATTR_DSTAMP_END]; $this->_logger->info("Processing period #{$periodId} ({$periodBegin}-{$periodEnd}), calculation #{$calcId}."); $transData = $this->_subDb->getDataForWriteOff($calcId, $periodBegin, $periodEnd); $updates = $this->_subCalc->pvWriteOff($transData); $dateApplied = $this->_toolPeriod->getTimestampTo($periodEnd); $operId = $this->_subDb->saveOperationPvWriteOff($updates, $datePerformed, $dateApplied); $this->_subDb->saveLogPvWriteOff($transData, $operId, $calcId); $this->_subDb->markCalcComplete($calcId); $this->_manTrans->commit($def); $result->setPeriodId($periodId); $result->setCalcId($calcId); $result->markSucceed(); } finally { $this->_manTrans->end($def); } } } $this->_logMemoryUsage(); $this->_logger->info("'PV Write Off' calculation is completed."); return $result; }
/** * SELECT * pps.sale_id, * pps.date_paid, * sfo.base_grand_total, * ce.entity_id * FROM prxgt_pv_sale pps * LEFT JOIN sales_flat_order sfo * ON pps.sale_id = sfo.entity_id * LEFT JOIN customer_entity ce * ON sfo.customer_id = ce.entity_id * WHERE pps.date_paid >= '2016-01-01 00:00:00' * AND pps.date_paid <= '2016-01-31 23:59:59' * * @param $dsBegin - '20160101' * @param $dsEnd - '20160131' * * @return array [ $custId => [$orderId=>[$amount], ... ], ... ] */ public function getSaleOrdersForRebate($dsBegin, $dsEnd) { $result = []; /* aliases and tables */ $asPvSale = 'pps'; $asMageSale = 'sfo'; $asMageCust = 'ce'; $tblPvSale = $this->_resource->getTableName(PvSale::ENTITY_NAME); $tblMageSale = $this->_resource->getTableName(Cfg::ENTITY_MAGE_SALES_ORDER); $tblMageCust = $this->_resource->getTableName(Cfg::ENTITY_MAGE_CUSTOMER); // FROM prxgt_pv_sale pps $query = $this->_conn->select(); $cols = [PvSale::ATTR_SALE_ID, PvSale::ATTR_DATE_PAID]; $query->from([$asPvSale => $tblPvSale], $cols); // LEFT JOIN sales_flat_order sfo ON pps.sale_id = sfo.entity_id $on = "{$asPvSale}." . PvSale::ATTR_SALE_ID . "={$asMageSale}." . Cfg::E_COMMON_A_ENTITY_ID; $cols = [Cfg::E_SALE_ORDER_A_BASE_GRAND_TOTAL]; $query->joinLeft([$asMageSale => $tblMageSale], $on, $cols); // LEFT JOIN customer_entity ce ON sfo.customer_id = ce.entity_id $on = "{$asMageSale}." . Cfg::E_SALE_ORDER_A_CUSTOMER_ID . "={$asMageCust}." . Cfg::E_CUSTOMER_A_ENTITY_ID; $cols = [Cfg::E_CUSTOMER_A_ENTITY_ID]; $query->joinLeft([$asMageCust => $tblMageCust], $on, $cols); // where $from = $this->_toolPeriod->getTimestampFrom($dsBegin); $to = $this->_toolPeriod->getTimestampTo($dsEnd); $whereFrom = PvSale::ATTR_DATE_PAID . '>=' . $this->_conn->quote($from); $whereTo = PvSale::ATTR_DATE_PAID . '<=' . $this->_conn->quote($to); $wherePv = PvSale::ATTR_TOTAL . ">0"; $query->where("{$whereFrom} AND {$whereTo} AND {$wherePv}"); // $sql = (string)$query; $data = $this->_conn->fetchAll($query); foreach ($data as $item) { $custId = $item[Cfg::E_CUSTOMER_A_ENTITY_ID]; $saleId = $item[PvSale::ATTR_SALE_ID]; $amount = $item[Cfg::E_SALE_ORDER_A_BASE_GRAND_TOTAL]; $result[$custId][$saleId] = $amount; } return $result; }
/** * SELECT * `pps`.`sale_id`, * `sfo`.`customer_id` * FROM `prxgt_pv_sale` AS `pps` * LEFT JOIN `sales_flat_order` AS `sfo` * ON pps.sale_id = sfo.entity_id * WHERE (pps.date_paid >= '2016-01-01 08:00:00' * AND pps.date_paid <= '2017-01-01 07:59:59') * * @param string $dsFrom * @param string $dsTo * * @return array */ function getSalesOrdersForPeriod($dsFrom, $dsTo) { $tsFrom = $this->_toolPeriod->getTimestampFrom($dsFrom); $tsTo = $this->_toolPeriod->getTimestampTo($dsTo); /* aliases and tables */ $asPv = 'pps'; $asOrder = 'sfo'; $tblPv = $this->_resource->getTableName(PvSale::ENTITY_NAME); $tblOrder = $this->_resource->getTableName(Cfg::ENTITY_MAGE_SALES_ORDER); // SELECT FROM prxgt_pv_sale pps $query = $this->_conn->select(); $query->from([$asPv => $tblPv], [PvSale::ATTR_SALE_ID, PvSale::ATTR_TOTAL]); // LEFT JOIN sales_flat_order sfo ON pps.sale_id = sfo.entity_id $on = "{$asPv}." . PvSale::ATTR_SALE_ID . "={$asOrder}." . Cfg::E_SALE_ORDER_A_ENTITY_ID; $cols = [Cfg::E_SALE_ORDER_A_CUSTOMER_ID]; $query->joinLeft([$asOrder => $tblOrder], $on, $cols); // where $whereFrom = $asPv . '.' . PvSale::ATTR_DATE_PAID . '>=' . $this->_conn->quote($tsFrom); $whereTo = $asPv . '.' . PvSale::ATTR_DATE_PAID . '<=' . $this->_conn->quote($tsTo); $query->where("{$whereFrom} AND {$whereTo}"); // $sql = (string)$query; $result = $this->_conn->fetchAll($query); return $result; }