/**
  * Constructor
  *
  * @return void
  */
 public function construct()
 {
     if ($this->isNew()) {
         $this->setType('rtShopVoucher');
         $this->setCode(rtShopVoucherToolkit::generateVoucherCode());
     }
 }
 /**
  * Return data for check voucher request
  * 
  * @param  string $voucher_code Voucher code
  * @return array
  */
 public function getCheckVoucherArray($voucher_code)
 {
     $numberFormat = new sfNumberFormat(sfContext::getInstance()->getUser()->getCulture());
     $voucher_data = array('error' => false, 'id' => '');
     if ($voucher_code !== '') {
         $voucher = rtShopVoucherToolkit::getApplicable($voucher_code, $this->getItemsCharge());
         if ($voucher) {
             $this->getOrder()->setVoucherCode($voucher->getCode());
             $voucher_data = $voucher->getData();
         } else {
             $this->getOrder()->setVoucherCode(null);
             $voucher_data['error'] = true;
         }
     } else {
         $this->getOrder()->setVoucherCode(null);
     }
     $this->getOrder()->save();
     // Add data
     $voucher_data['shipping_charge'] = $this->getShippingCharge();
     $voucher_data['total_charge'] = $this->getTotalCharge();
     $voucher_data['reduction'] = $this->getVoucherReduction();
     $voucher_data['reduction_formatted'] = $numberFormat->format($this->getVoucherReduction(), 'c', sfConfig::get('app_rt_currency', 'USD'));
     $voucher_data['total_charge_formatted'] = $numberFormat->format($this->getTotalCharge(), 'c', sfConfig::get('app_rt_currency', 'USD'));
     return $voucher_data;
 }
 /**
  * Batch create voucher for users in birthday range
  *
  * @param array $values Voucher details
  * @param integer $batchsize Voucher batch size
  * @return boolean True if successsful
  */
 protected function createBatchVouchers($values, $batchsize)
 {
     try {
         rtShopVoucherToolkit::generateBatch($values, $batchsize, false);
     } catch (Exception $e) {
         throw new sfCommandException('generateBatch() failed.');
     }
 }
$t->is($voucher->getMode(), 'Group', '::getMode() Voucher type is Group.');
$t->is($voucher->getReductionType(), 'dollarOff', '::getReductionType() Voucher reduction_type is dollarOff.');
$voucher->adjustReductionValueBy($order_total);
$t->is($voucher->getReductionValue(), 10, '::getReductionValue() Untouched reduction_value is correct.');
$t->diag('Voucher type: Group, Reduction_type: percentageOff');
$order_total = 130;
$voucher = rtShopVoucherToolkit::getApplicable($code2, $order_total, date("Y-m-d H:i:s"));
$t->is(get_class($voucher), 'rtShopVoucher', '::getApplicable() Returns a rtShopVoucher object correctly.');
$t->is($voucher->getId(), $i4->getId(), '::getApplicable() Found applicable voucher.');
$t->is($voucher->getMode(), 'Group', '::getMode() Voucher type is Group.');
$t->is($voucher->getReductionType(), 'percentageOff', '::getReductionType() Voucher reduction_type is percentageOff.');
$voucher->adjustReductionValueBy($order_total);
$t->is($voucher->getReductionValue(), 15, '::getReductionValue() Untouched reduction_value is correct.');
$t->diag('Apply voucher to order total');
$order_total = 100;
$voucher_total = rtShopVoucherToolkit::applyVoucher($code4, $order_total, date("Y-m-d H:i:s"));
$t->is($voucher_total, 40, '::applyVoucher() Applied voucher correctly and returned new total.');
// Clean up
$i3->delete();
$i4->delete();
$i5->delete();
$i6->delete();
$i7->delete();
$i8->delete();
unset($voucher);
unset($i3, $i4, $i5, $i6, $i7, $i8);
/**
 * rtShopVoucherToolkit Class
 */
class rtShopVoucherTestTools
{
 /**
  * Batch create vouchers
  *
  * @param sfWebRequest $request  Request object
  */
 public function executeBatchCreate(sfWebRequest $request)
 {
     $this->form = new rtShopVoucherBatchForm();
     if ($request->isMethod('post')) {
         $this->processBatchForm($request, $this->form);
         $batch_data = $request->getParameter('rt_shop_voucher_batch');
         if ($this->form->isValid()) {
             $form_values = $this->form->getValues();
             $reference = rtShopVoucherToolkit::generateBatch($this->form->getValues(), $form_values['batchsize']);
             if ($reference) {
                 $this->getUser()->setFlash('notice', $form_values['batchsize'] . ' vouchers created - ref. #' . $reference);
                 $this->redirect('rtShopVoucherAdmin/index');
             } else {
                 $this->getUser()->setFlash('error', 'An error occured while creating batch vouchers', false);
             }
         }
         $this->getUser()->setFlash('default_error', true, false);
     }
 }