/** * Execute a command and return a response. Does not render * @param IRequest $Request * @return IResponse the execution response */ function execute(IRequest $Request) { $TransactionEntry = TransactionEntry::get($this->getTransactionID()); $transactionLog = $TransactionEntry->fetchLog(); $Invoice = $TransactionEntry->getInvoice(); $Product = $Invoice->getProduct(); $Wallet = $Invoice->getWallet(); $accounts = array(); $merchantProfit = $Product->calculateProfit($TransactionEntry->getStatus(), $accounts); $accounts[$Product->getAccountID()] = $merchantProfit; $ProfitTBody = new HTMLSequenceTableBody(new CallbackSequenceMap(function (ISequenceMapper $Map) use($accounts) { foreach ($accounts as $accountID => $profit) { $Map->mapNext(array('account' => $accountID, 'profit' => $profit)); } })); $Form = new HTMLForm(self::FORM_METHOD, $Request->getPath(), self::FORM_NAME, new HTMLMetaTag(HTMLMetaTag::META_TITLE, self::TITLE), new HTMLHeaderScript(__DIR__ . '/assets/transaction.js'), new HTMLHeaderStyleSheet(__DIR__ . '/assets/transaction.css'), new HTMLElement('fieldset', 'fieldset-transaction-info inline', new HTMLElement('legend', 'legend-transaction-info', 'Transaction Information'), new MapRenderer($TransactionEntry)), new HTMLElement('fieldset', 'fieldset-product-profit inline', new HTMLElement('legend', 'legend-product-profit', 'Profit Information'), new HTMLTable($ProfitTBody)), "<br/>", new HTMLElement('fieldset', 'fieldset-product-container inline', new HTMLElement('legend', 'legend-product', 'Order Information'), $Wallet->getFieldSet($Request)->setAttribute('disabled', 'disabled'), $Product->getOrderFieldSet($Request)->setAttribute('disabled', 'disabled'), $Product->getConfigFieldSet($Request)->setAttribute('disabled', 'disabled'), $Product->getFeesFieldSet($Request)->setAttribute('disabled', 'disabled'), "<br/><br/>", new HTMLButton('submit', 'Update', 'submit')), "<br/>", new HTMLElement('fieldset', 'fieldset-transaction-log inline', new HTMLElement('legend', 'legend-transaction-log', 'Transaction Log'), new HTMLTextAreaField(self::PARAM_LOG . '_disabled', $transactionLog, new Attributes('rows', 10, 'cols', 100), new Attributes('disabled', 'disabled')), "<br/>", new HTMLTextAreaField(self::PARAM_LOG, new Attributes('rows', 3, 'cols', 100)), "<br/>", new HTMLButton(self::PARAM_SUBMIT, 'Append', 'append-log')), new HTMLElement('fieldset', 'fieldset-transaction-manage inline', new HTMLElement('legend', 'legend-submit', self::TITLE), new HTMLInputField(self::PARAM_ID, $this->id, 'hidden', new RequiredValidation()), new HTMLElement('label', null, "Status<br/>", $SelectStatus = new HTMLSelectField(self::PARAM_TRANSACTION_STATUS, TransactionEntry::$StatusOptions, new RequiredValidation())), "<br/><br/>", new HTMLButton(self::PARAM_SUBMIT, 'Update', 'submit')), "<br/>"); $SelectStatus->setInputValue($TransactionEntry->getStatus()); if (!$Request instanceof IFormRequest) { return $Form; } switch ($Request[self::PARAM_SUBMIT]) { case 'submit': $status = $Form->validateField($Request, self::PARAM_TRANSACTION_STATUS); $TransactionEntry->update($Request, $status); ProfitEntry::update($Request, $TransactionEntry->getID()); return new RedirectResponse(ManageTransaction::getRequestURL($TransactionEntry->getID()), "Transaction updated successfully. Redirecting...", 5); case 'append-log': $appendLog = $Request[self::PARAM_LOG]; $TransactionEntry->appendLog($appendLog); return new RedirectResponse(ManageTransaction::getRequestURL($TransactionEntry->getID()), "Log appended successfully. Redirecting...", 5); default: throw new \InvalidArgumentException("Invalid Submit"); } }
static function update(IRequest $Request, $id) { $TransactionEntry = TransactionEntry::get($id); $accounts = array(); $Invoice = $TransactionEntry->getInvoice(); $Product = $Invoice->getProduct(); $merchantProfit = $Product->calculateProfit($TransactionEntry->getStatus(), $accounts); $accounts[$Product->getProductID()] = $merchantProfit; foreach ($accounts as $accountID => $profit) { $Table = self::table(); $Table->insert(ProfitTable::COLUMN_TRANSACTION_ID, ProfitTable::COLUMN_ACCOUNT_ID, ProfitTable::COLUMN_PROFIT)->values($TransactionEntry->getID(), $accountID, $profit)->onDuplicateKeyUpdate(ProfitTable::COLUMN_PROFIT, $merchantProfit)->execute($Request); } }