Source of file PaymentsReport.php
Size: 6,418 Bytes - Last Modified: 2021-12-23T10:41:11+00:00
/var/www/docs.ssmods.com/process/src/code/reports/PaymentsReport.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 | <?php /** * @author Nicolaas [at] sunnysideup.co.nz * @package ecommercextras */ class PaymentsReport extends SS_Report { protected $title = 'All Payments'; protected static $payment_array = array(); protected $description = 'Show all payments'; /** * Return a {@link ComplexTableField} that shows * all Order instances that are not printed. That is, * Order instances with the property "Printed" value * set to "0". * * @return ComplexTableField */ public function getReportField() { // Get the fields used for the table columns $fields = array( 'Created' => 'Time', 'Amount' => 'Amount', 'Currency' => 'Currency', 'Message' => 'Note', 'IP' => 'Purchaser IP', 'ProxyIP' => 'Purchaser Proxy' ); $fields['ChangeStatus'] = ''; $table = new TableListField( 'Payments', 'Payment', $fields ); // Customise the SQL query for Order, because we don't want it querying // all the fields. Invoice and Printed are dummy fields that just have some // text in them, which would be automatically queried if we didn't specify // a custom query. $table->setCustomQuery($this->getCustomQuery()); // Set the links to the Invoice and Print fields allowing a user to view // another template for viewing an Order instance $table->setFieldFormatting(array( 'ChangeStatus' => '<a href=\"#\" class=\"statusDropdownChange\" rel=\"$ID\">$Status</a><span class=\"outcome\"></span>' )); $table->setFieldCasting(array( 'Amount' => 'Currency->Nice' )); $table->setPermissions(array( 'edit', 'show', 'export', )); $table->setPageSize(250); $table->setFieldListCsv($this->getExportFields()); $table->setCustomCsvQuery($this->getExportQuery()); //$tableField->removeCsvHeader(); return $table; } public function getCustomQuery() { if ("PaymentsReport" == $this->class) { //user_error('Please implement getCustomQuery() on ' . $this->class, E_USER_ERROR); } else { //buildSQL($filter = "", $sort = "", $limit = "", $join = "", $restrictClasses = true, $having = "") $query = singleton('Payment')->buildSQL('', 'Payment.Created DESC'); $query->groupby[] = 'Payment.ID'; return $query; } } public function getExportFields() { if ("PaymentsReport" == $this->class) { //user_error('Please implement getExportFields() on ' . $this->class, E_USER_ERROR); } else { return array("Order.ID" => "Order ID", "Order.Total" => "Order Total"); } } public function getExportQuery() { if ("PaymentsReport" == $this->class) { //user_error('Please implement getExportFields() on ' . $this->class, E_USER_ERROR); } else { $query = singleton('Payment')->buildSQL('', 'Payment.Created DESC'); $query->groupby[] = 'Payment.ID'; return $query; } } protected function statistic($type) { if (!count(self::$payment_array)) { $data = $this->getCustomQuery()->execute(); if ($data) { $array = array(); foreach ($data as $row) { if ($row["Amount"] && $row["Status"] == "Success") { self::$payment_array[] = $row["Amount"]; } } } } if (count(self::$payment_array)) { switch ($type) { case "count": return count(self::$payment_array); break; case "sum": return array_sum(self::$payment_array); break; case "avg": return array_sum(self::$payment_array) / count(self::$payment_array); break; case "min": asort(self::$payment_array); foreach (self::$payment_array as $item) { return $item; } break; case "max": arsort(self::$payment_array); foreach (self::$payment_array as $item) { return $item; } break; default: user_error("Wrong statistic type speficied in PaymentsReport::statistic", E_USER_ERROR); } } return -1; } public function processform() { if ("PaymentsReport" == $this->class) { //user_error('Please implement processform() on ' . $this->class, E_USER_ERROR); } else { die($_REQUEST); } } protected function currencyFormat($v) { $c = new Currency("currency"); $c->setValue($v); return $c->Nice(); } } class PaymentsReport_Handler extends Controller { public function processform() { $ClassName = Director::URLParam("ID"); $object = new $ClassName; return $object->processform(); } public function setstatus() { $id = $this->urlParams['ID']; if (!is_numeric($id)) { return "could not update payment status"; } $payment = DataObject::get_by_id('Payment', $id); if ($payment) { $oldStatus = $payment->Status; $newStatus = $this->urlParams['OtherID']; if ($oldStatus != $newStatus) { $payment->Status = $newStatus; $payment->write(); $orderLog = new OrderStatusLog(); $orderLog->OrderID = $orderLog->OrderID; $orderLog->Status = "Payment status changed from ".$oldStatus." to ".$newStatus."."; $orderLog->Note = "Payment changed from ".$oldStatus." to ".$newStatus."."; $orderLog->write(); } else { return "no change"; } } else { return "payment not found"; } return "updated to ".$newStatus; } } |