/**
  * Create a Voucher Rule
  * 
  * @param VoucherGenRule  $oVoucherRule  The Voucher Gen Rule
  * @throws VoucherException if the database query fails or entity has id assigned.
  * @returns boolean true if the insert operation was successful
  */
 public function execute(VoucherGenRule $oVoucherRule)
 {
     $oGateway = $this->oGateway;
     $oBuilder = $oGateway->getEntityBuilder();
     if (false === empty($oVoucherRule->getVoucherGenRuleId())) {
         throw new VoucherException('Unable to create new voucher rule the Entity has a database id assigned already');
     }
     try {
         $oQuery = $oGateway->insertQuery()->start();
         foreach ($oBuilder->demolish($oVoucherRule) as $sColumn => $mValue) {
             if ($sColumn !== 'voucher_gen_rule_id' && $sColumn !== 'date_created') {
                 $oQuery->addColumn($sColumn, $mValue);
             } elseif ($sColumn === 'date_created') {
                 $oQuery->addColumn('date_created', $this->oNow);
             }
         }
         $bSuccess = $oQuery->end()->insert();
         if ($bSuccess) {
             $oVoucherRule->setVoucherGenRuleId($oGateway->lastInsertId());
         }
     } catch (DBALGatewayException $e) {
         throw new VoucherException($e->getMessage(), 0, $e);
     }
     return $bSuccess;
 }
 /**
  * Update a Voucher Rule
  * 
  * @param VoucherGenRule  $oVoucherRule  The Voucher Gen Rule
  * @throws VoucherException if the database query fails or entity has id assigned.
  * @returns boolean true if the insert operation was successful
  */
 public function execute(VoucherGenRule $oVoucherRule)
 {
     $oGateway = $this->oGateway;
     $oBuilder = $oGateway->getEntityBuilder();
     if (true === empty($oVoucherRule->getVoucherGenRuleId())) {
         throw new VoucherException('Unable to update voucher rule the Entity requires a database id be assigned');
     }
     try {
         $oQuery = $oGateway->updateQuery()->start();
         foreach ($oBuilder->demolish($oVoucherRule) as $sColumn => $mValue) {
             if ($sColumn !== 'voucher_gen_rule_id' && $sColumn !== 'date_created') {
                 $oQuery->addColumn($sColumn, $mValue);
             }
         }
         $bSuccess = $oQuery->where()->filterByRule($oVoucherRule->getVoucherGenRuleId())->end()->update();
     } catch (DBALGatewayException $e) {
         throw new VoucherException($e->getMessage(), 0, $e);
     }
     return $bSuccess;
 }
 public function testVoucherRuleSuccess()
 {
     $oRule = new VoucherGenRule();
     $iVoucherGeneratorRuleId = 1;
     $sVoucherRuleNameSlug = 'rule_1';
     $sVoucherRuleName = 'Rule 1';
     $sVoucherPaddingCharacter = 'A';
     $sVoucherSuffix = '###';
     $sVoucherPrefix = '@@@';
     $iVoucherLength = 10;
     $oDateCreated = new DateTime();
     $sSequenceStrategy = 'UUID';
     $oRule->setVoucherGenRuleId($iVoucherGeneratorRuleId);
     $oRule->setSlugRuleName($sVoucherRuleNameSlug);
     $oRule->setVoucherRuleName($sVoucherRuleName);
     $oRule->setVoucherPaddingCharacter($sVoucherPaddingCharacter);
     $oRule->setVoucherSuffix($sVoucherSuffix);
     $oRule->setVoucherPrefix($sVoucherPrefix);
     $oRule->setVoucherLength($iVoucherLength);
     $oRule->setDateCreated($oDateCreated);
     $oRule->setSequenceStrategyName($sSequenceStrategy);
     $this->assertEquals($iVoucherGeneratorRuleId, $oRule->getVoucherGenRuleId());
     $this->assertEquals($sVoucherRuleNameSlug, $oRule->getSlugRuleName());
     $this->assertEquals($sVoucherRuleName, $oRule->getVoucherRuleName());
     $this->assertEquals($sVoucherPaddingCharacter, $oRule->getVoucherPaddingCharacter());
     $this->assertEquals($sVoucherSuffix, $oRule->getVoucherSuffix());
     $this->assertEquals($sVoucherPrefix, $oRule->getVoucherPrefix());
     $this->assertEquals($iVoucherLength, $oRule->getVoucherLength());
     $this->assertEquals($oDateCreated, $oRule->getDateCreated());
     $this->assertEquals($sSequenceStrategy, $oRule->getSequenceStrategyName());
     $this->assertTrue($oRule->validate());
     # validate with no Database ID which be the case in INSERT
     $oRule = new VoucherGenRule();
     $oRule->setSlugRuleName($sVoucherRuleNameSlug);
     $oRule->setVoucherRuleName($sVoucherRuleName);
     $oRule->setVoucherPaddingCharacter($sVoucherPaddingCharacter);
     $oRule->setVoucherSuffix($sVoucherSuffix);
     $oRule->setVoucherPrefix($sVoucherPrefix);
     $oRule->setVoucherLength($iVoucherLength);
     $oRule->setDateCreated($oDateCreated);
     $oRule->setSequenceStrategyName($sSequenceStrategy);
     $this->assertTrue($oRule->validate());
     # valdiate with empty padding, suffix, prefix
     $oRule = new VoucherGenRule();
     $oRule->setSlugRuleName($sVoucherRuleNameSlug);
     $oRule->setVoucherRuleName($sVoucherRuleName);
     $oRule->setVoucherPaddingCharacter('');
     $oRule->setVoucherSuffix('');
     $oRule->setVoucherPrefix('');
     $oRule->setVoucherLength(100);
     $oRule->setDateCreated($oDateCreated);
     $oRule->setSequenceStrategyName($sSequenceStrategy);
     $this->assertTrue($oRule->validate());
 }