protected function applyFilter()
 {
     class_exists('Am_Form', true);
     $filter = (array) $this->vars['filter'];
     $q = $this->grid->getDataSource();
     $dateField = $this->dateField;
     /* @var Am_Query $q */
     if ($filter['dat1']) {
         $q->addWhere("t.{$dateField} >= ?", Am_Form_Element_Date::createFromFormat(null, $filter['dat1'])->format('Y-m-d 00:00:00'));
     }
     if ($filter['dat2']) {
         $q->addWhere("t.{$dateField} <= ?", Am_Form_Element_Date::createFromFormat(null, $filter['dat2'])->format('Y-m-d 23:59:59'));
     }
     if (@$filter['text']) {
         switch (@$filter['type']) {
             case 'invoice':
                 $q->addWhere('t.invoice_id=?d', $filter['text']);
                 break;
             case 'login':
                 $q->addWhere('login=?', $filter['text']);
                 break;
             case 'receipt':
                 $q->addWhere('receipt_id LIKE ?', '%' . $filter['text'] . '%');
                 break;
         }
     }
 }
 protected function applyFilter()
 {
     class_exists('Am_Form', true);
     $filter = (array) $this->vars['filter'];
     $q = $this->grid->getDataSource();
     $dateField = $this->vars['filter']['datf'];
     if (!array_key_exists($dateField, $this->getDateFieldOptions())) {
         throw new Am_Exception_InternalError(sprintf('Unknown date field [%s] submitted in %s::%s', $dateField, __CLASS__, __METHOD__));
     }
     /* @var $q Am_Query */
     if ($filter['dat1']) {
         $q->addWhere("t.{$dateField} >= ?", Am_Form_Element_Date::createFromFormat(null, $filter['dat1'])->format('Y-m-d 00:00:00'));
     }
     if ($filter['dat2']) {
         $q->addWhere("t.{$dateField} <= ?", Am_Form_Element_Date::createFromFormat(null, $filter['dat2'])->format('Y-m-d 23:59:59'));
     }
     if (@$filter['text']) {
         switch (@$filter['type']) {
             case 'invoice':
                 if ($q->getTableName() == '?_invoice') {
                     $q->leftJoin('?_invoice_payment', 'p');
                     $q->leftJoin('?_invoice_refund', 'rf');
                     $q->addWhere('(t.invoice_id=? OR t.public_id=? OR p.display_invoice_id=? or rf.display_invoice_id=?)', $filter['text'], $filter['text'], $filter['text'], $filter['text']);
                 } else {
                     $q->addWhere('(t.invoice_id=? OR t.invoice_public_id=? or t.display_invoice_id=?)', $filter['text'], $filter['text'], $filter['text']);
                 }
                 break;
             case 'login':
                 $q->addWhere('login=?', $filter['text']);
                 break;
             case 'name':
                 $q->addWhere("name_f LIKE ? OR name_l LIKE ?\n                        OR CONCAT(name_f, ' ', name_l) LIKE ?\n                        OR CONCAT(name_l, ' ', name_f) LIKE ?", '%' . $filter['text'] . '%', '%' . $filter['text'] . '%', '%' . $filter['text'] . '%', '%' . $filter['text'] . '%');
                 break;
             case 'receipt':
                 if ($q->getTableName() == '?_invoice') {
                     $q->leftJoin('?_invoice_payment', 'p');
                 }
                 $q->addWhere('receipt_id LIKE ?', '%' . $filter['text'] . '%');
                 break;
             case 'coupon':
                 if ($q->getTableName() != '?_invoice') {
                     $q->leftJoin('?_invoice', 'i', 't.invoice_id=i.invoice_id');
                 }
                 $q->addWhere('coupon_code=?', $filter['text']);
                 break;
         }
     }
     if (@$filter['product_id']) {
         $q->leftJoin('?_invoice_item', 'ii', 't.invoice_id=ii.invoice_id')->addWhere('ii.item_type=?', 'product')->addWhere('ii.item_id in (?a)', $filter['product_id']);
     }
     if (@$filter['paysys_id']) {
         $q->addWhere('paysys_id in (?a)', $filter['paysys_id']);
     }
 }
 protected function applyFilter()
 {
     if ($filter = $this->getParam('filter')) {
         foreach ($this->filterMap as $alias => $fields) {
             foreach ($fields as $field) {
                 $c = new Am_Query_Condition_Field($field, 'LIKE', '%' . $filter . '%', $alias);
                 if (!$condition) {
                     $condition = $c;
                 } else {
                     $condition->_or($c);
                 }
             }
         }
         $this->grid->getDataSource()->getDataSourceQuery()->add($condition);
     }
     if ($filter = $this->getParam('dat1')) {
         $this->grid->getDataSource()->getDataSourceQuery()->addWhere("t.{$this->datField} >= ?", Am_Form_Element_Date::createFromFormat(null, $filter)->format('Y-m-d 00:00:00'));
     }
     if ($filter = $this->getParam('dat2')) {
         $this->grid->getDataSource()->getDataSourceQuery()->addWhere("t.{$this->datField} <= ?", Am_Form_Element_Date::createFromFormat(null, $filter)->format('Y-m-d 23:59:59'));
     }
 }
 function getDates()
 {
     $filter = $this->vars['filter'];
     if ($filter['period'] == 'exact') {
         $dat1 = Am_Form_Element_Date::createFromFormat(null, $filter['dat1'])->format('Y-m-d');
         $dat2 = Am_Form_Element_Date::createFromFormat(null, $filter['dat2'])->format('Y-m-d');
     } else {
         list($dat1, $dat2) = $this->grid->getDi()->interval->getStartStop($filter['period']);
     }
     return array($dat1, $dat2);
 }
 public function refundAction()
 {
     $this->getDi()->authAdmin->getUser()->checkPermission('grid_payment', 'insert');
     do {
         $this->invoice_payment_id = $this->getInt('invoice_payment_id');
         if (!$this->invoice_payment_id) {
             $res = array('success' => false, 'text' => ___("Not payment# submitted"));
             continue;
         }
         $p = $this->getDi()->invoicePaymentTable->load($this->invoice_payment_id);
         /* @var $p InvoicePayment */
         if (!$p) {
             $res = array('success' => false, 'text' => ___("No payment found"));
             continue;
         }
         if ($this->user_id != $p->user_id) {
             $res = array('success' => false, 'text' => ___("Payment belongs to another customer"));
             continue;
         }
         if ($p->isFullRefunded()) {
             $res = array('success' => false, 'text' => ___("Payment is already refunded"));
             continue;
         }
         $amount = sprintf('%.2f', $this->_request->get('amount'));
         if ($p->amount < $amount) {
             $res = array('success' => false, 'text' => ___("Refund amount cannot exceed payment amount"));
             continue;
         }
         if ($this->_request->getInt('manual')) {
             $el = new Am_Form_Element_Date();
             $dattm = $el->convertReadableToSQL($this->_request->get('dattm'));
             if (!$dattm) {
                 $dattm = sqlDate('now');
             }
             $dattm .= ' 00:00:00';
             if ($dattm < $p->dattm) {
                 $res = array('success' => false, 'text' => ___("Refund date cannot be before payment date"));
                 continue;
             }
             switch ($type = $this->_request->getFiltered('type')) {
                 case 'refund':
                 case 'chargeback':
                     $pl = $this->getDi()->plugins_payment->loadEnabled()->get($p->paysys_id);
                     if (!$pl) {
                         $res = array('success' => false, 'text' => ___("Could not load payment plugin [%s]", $pl));
                         continue 2;
                     }
                     $invoice = $p->getInvoice();
                     $transaction = new Am_Paysystem_Transaction_Manual($pl);
                     $transaction->setAmount($amount);
                     $transaction->setReceiptId($p->receipt_id . '-manual-' . $type);
                     $transaction->setTime(new DateTime($dattm));
                     if ($type == 'refund') {
                         $invoice->addRefund($transaction, $p->receipt_id);
                     } else {
                         $invoice->addChargeback($transaction, $p->receipt_id);
                     }
                     break;
                 case 'correction':
                     $this->getDi()->accessTable->deleteBy(array('invoice_payment_id' => $this->invoice_payment_id));
                     $invoice = $p->getInvoice();
                     $p->delete();
                     $invoice->updateStatus();
                     break;
                 default:
                     $res = array('success' => false, 'text' => ___("Incorrect refund [type] passed: %s", $type));
                     continue 2;
             }
             $res = array('success' => true, 'text' => ___("Payment has been successfully refunded"));
         } else {
             // automatic
             /// ok, now we have validated $p here
             $pl = $this->getDi()->plugins_payment->loadEnabled()->get($p->paysys_id);
             if (!$pl) {
                 $res = array('success' => false, 'text' => ___("Could not load payment plugin [%s]", $pl));
                 continue;
             }
             /* @var $pl Am_Paysystem_Abstract */
             $result = new Am_Paysystem_Result();
             $pl->processRefund($p, $result, $amount);
             if ($result->isSuccess()) {
                 if ($transaction = $result->getTransaction()) {
                     $p->getInvoice()->addRefund($result->getTransaction(), $p->receipt_id, $amount);
                 }
                 $res = array('success' => true, 'text' => ___("Payment has been successfully refunded"));
             } elseif ($result->isAction()) {
                 $action = $result->getAction();
                 if ($action instanceof Am_Paysystem_Action_Redirect) {
                     $res = array('success' => 'redirect', 'url' => $result->getUrl());
                 } else {
                     // todo handle other actions if necessary
                     throw new Am_Exception_NotImplemented("Could not handle refund action " . get_class($action));
                 }
             } elseif ($result->isFailure()) {
                 $res = array('success' => false, 'text' => join(";", $result->getErrorMessages()));
             }
         }
     } while (false);
     $this->_response->setHeader("Content-Type", "application/json", true);
     echo $this->getJson($res);
 }