diff --git a/plugins/reach/lib/api/data/entryVendorTaks/KalturaEntryVendorTask.php b/plugins/reach/lib/api/data/entryVendorTaks/KalturaEntryVendorTask.php index 50aa41135f..37e97cb989 100644 --- a/plugins/reach/lib/api/data/entryVendorTaks/KalturaEntryVendorTask.php +++ b/plugins/reach/lib/api/data/entryVendorTaks/KalturaEntryVendorTask.php @@ -230,6 +230,14 @@ class KalturaEntryVendorTask extends KalturaObject implements IRelatedFilterable */ public $externalObjectId; + /** + * Indicates if the task is pay-per-use based on the catalog item + * @var bool + * @readonly + */ + public $isPayPerUse; + + private static $map_between_objects = array ( 'id', @@ -264,7 +272,8 @@ class KalturaEntryVendorTask extends KalturaObject implements IRelatedFilterable 'serviceFeature', 'turnAroundTime', 'externalTaskId', - 'externalObjectId' + 'externalObjectId', + 'isPayPerUse' ); /* (non-PHPdoc) @@ -287,6 +296,13 @@ public function toInsertableObject($object_to_fill = null, $props_to_skip = arra $object_to_fill = parent::toInsertableObject($object_to_fill, $props_to_skip); + // Set isPayPerUse flag based on catalog item + $dbVendorCatalogItem = VendorCatalogItemPeer::retrieveByPK($object_to_fill->getCatalogItemId()); + if ($dbVendorCatalogItem) + { + $object_to_fill->setIsPayPerUse($dbVendorCatalogItem->getPayPerUse()); + } + $jobData = $this->taskJobData; if ($this->isScheduled() && !$jobData->scheduledEventId) { diff --git a/plugins/reach/lib/model/EntryVendorTask.php b/plugins/reach/lib/model/EntryVendorTask.php index 6881a1f2aa..859a259ef5 100644 --- a/plugins/reach/lib/model/EntryVendorTask.php +++ b/plugins/reach/lib/model/EntryVendorTask.php @@ -37,6 +37,7 @@ class EntryVendorTask extends BaseEntryVendorTask implements IRelatedObject, IIn const CUSTOM_DATA_SERVICE_FEATURE = 'serviceFeature'; const CUSTOM_DATA_TURN_AROUND_TIME = 'turnAroundTime'; const CUSTOM_DATA_EXTERNAL_TASK_ID = 'externalTaskId'; + const CUSTOM_DATA_IS_PAY_PER_USE = 'isPayPerUse'; const CUSTOM_DATA_EXTERNAL_OBJECT_ID = 'externalObjectId'; const SEVEN_DAYS = 604800; const BUSINESS_DAY_FRIDAY = 5; //Monday is 1 @@ -162,6 +163,11 @@ public function setExternalObjectId($v) $this->putInCustomData(self::CUSTOM_DATA_EXTERNAL_OBJECT_ID, $v); } + public function setIsPayPerUse($v) + { + $this->putInCustomData(self::CUSTOM_DATA_IS_PAY_PER_USE, $v); + } + //getters @@ -285,6 +291,11 @@ public function getExternalTaskId() return $this->getFromCustomData(self::CUSTOM_DATA_EXTERNAL_TASK_ID); } + public function getIsPayPerUse() + { + return $this->getFromCustomData(self::CUSTOM_DATA_IS_PAY_PER_USE, null, false); + } + public function getExternalObjectId() { return $this->getFromCustomData(self::CUSTOM_DATA_EXTERNAL_OBJECT_ID); diff --git a/plugins/reach/lib/model/data/kVendorCredit.php b/plugins/reach/lib/model/data/kVendorCredit.php index 29a39f9a6c..de8cb0c6a0 100644 --- a/plugins/reach/lib/model/data/kVendorCredit.php +++ b/plugins/reach/lib/model/data/kVendorCredit.php @@ -150,23 +150,49 @@ public function isSynced() public function syncCredit($reachProfileId, $partnerId) { - $c = new Criteria(); - $c->add(EntryVendorTaskPeer::REACH_PROFILE_ID, $reachProfileId , Criteria::EQUAL); - $c->add(EntryVendorTaskPeer::STATUS, array(EntryVendorTaskStatus::PENDING, EntryVendorTaskStatus::PROCESSING, EntryVendorTaskStatus::READY), Criteria::IN); - $c->add(EntryVendorTaskPeer::QUEUE_TIME, $this->getSyncCreditStartDate(), Criteria::GREATER_EQUAL); - $c->add(EntryVendorTaskPeer::PRICE, 0, Criteria::NOT_EQUAL); - $c->add(EntryVendorTaskPeer::PARTNER_ID, $partnerId); - $c->addSelectColumn('SUM('. EntryVendorTaskPeer::PRICE .')'); - $this->addAdditionalCriteria($c); - $now = time(); - $stmt = EntryVendorTaskPeer::doSelectStmt($c); - $row = $stmt->fetch(PDO::FETCH_NUM); + $syncStartDate = $this->getSyncCreditStartDate(); $totalUsedCredit = $this->getSyncedCredit(); + $totalPrice = 0; + + /* Query 1: Queue-based (non PPU) */ + $queueC = new Criteria(); + $queueC->add(EntryVendorTaskPeer::REACH_PROFILE_ID, $reachProfileId); + $queueC->add(EntryVendorTaskPeer::PARTNER_ID, $partnerId); + $queueC->add(EntryVendorTaskPeer::PRICE, 0, Criteria::NOT_EQUAL); + $queueC->add(EntryVendorTaskPeer::STATUS, [EntryVendorTaskStatus::PENDING, EntryVendorTaskStatus::PROCESSING, EntryVendorTaskStatus::READY], Criteria::IN); + $queueC->add(EntryVendorTaskPeer::QUEUE_TIME, $syncStartDate, Criteria::GREATER_EQUAL); + + $queueTasks = EntryVendorTaskPeer::doSelect($queueC); + + foreach ($queueTasks as $task) + { + if (!$task->getIsPayPerUse()) + { + $totalPrice += $task->getPrice(); + } + } + + /* Query 2: Finish-based (PPU) */ + $finishC = new Criteria(); + $finishC->add(EntryVendorTaskPeer::REACH_PROFILE_ID, $reachProfileId); + $finishC->add(EntryVendorTaskPeer::PARTNER_ID, $partnerId); + $finishC->add(EntryVendorTaskPeer::PRICE, 0, Criteria::NOT_EQUAL); + $finishC->add(EntryVendorTaskPeer::STATUS, EntryVendorTaskStatus::READY); + $finishC->add(EntryVendorTaskPeer::FINISH_TIME, $syncStartDate, Criteria::GREATER_EQUAL); + + $finishedTasks = EntryVendorTaskPeer::doSelect($finishC); + + foreach ($finishedTasks as $task) + { + if ($task->getIsPayPerUse()) + { + $totalPrice += $task->getPrice(); + } + } - $totalPrice = $row[0]; - if($totalPrice) + if ($totalPrice) { $totalUsedCredit += $totalPrice; }