Source of file AccountPage.php
Size: 6,745 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/Pages/AccountPage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 | <?php namespace Sunnysideup\Ecommerce\Pages; use Page; use SilverStripe\Core\Config\Config; use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataObject; use SilverStripe\Security\Member; use SilverStripe\Security\Permission; use Sunnysideup\Ecommerce\Model\Extensions\EcommerceRole; use Sunnysideup\Ecommerce\Model\Order; /** * @description: * The Account Page allows the user to update their details. * You do not need to be logged in to the account page in order to view it... If you are not logged in * then the account page can be a page to create an account. * * @authors: Nicolaas [at] Sunny Side Up .co.nz * @package: ecommerce * @sub-package: Pages */ class AccountPage extends Page { /** * @var float */ protected $calculatedTotal = 0; /** * @var float */ protected $calculatedPaid = 0; /** * @var float */ protected $calculatedOutstanding = 0; /** * @var DataList */ protected $pastOrders; private static $table_name = 'AccountPage'; /** * standard SS variable. * * @var array */ private static $casting = [ 'RunningTotal' => 'Currency', 'RunningPaid' => 'Currency', 'RunningOutstanding' => 'Currency', ]; /** * Standard SS variable. * * @var string */ private static $icon = 'sunnysideup/ecommerce: client/images/icons/AccountPage-file.gif'; /** * standard SS variable. * * @var string */ private static $singular_name = 'Account Page'; /** * standard SS variable. * * @var string */ private static $plural_name = 'Account Pages'; /** * Standard SS variable. * * @var string */ private static $description = 'A page where the customer can view all their orders and update their details.'; /** * Standard SS function, we only allow for one AccountPage to exist * but we do allow for extensions to exist at the same time. * * @param \SilverStripe\Security\Member $member * @param mixed $context * * @return bool */ public function canCreate($member = null, $context = []) { return AccountPage::get()->filter(['ClassName' => AccountPage::class])->exists() ? false : $this->canEdit($member); } /** * Shop Admins can edit. * * @param \SilverStripe\Security\Member $member * @param mixed $context * * @return bool */ public function canEdit($member = null, $context = []) { if (Permission::checkMember($member, Config::inst()->get(EcommerceRole::class, 'admin_permission_code'))) { return true; } return parent::canEdit($member); } /** * Standard SS method. * * @param \SilverStripe\Security\Member $member * * @return bool */ public function canDelete($member = null) { return $this->canEdit($member); } /** * Standard SS method. * * @param \SilverStripe\Security\Member $member * * @return bool */ public function canPublish($member = null) { return $this->canEdit($member); } public function i18n_singular_name() { return _t('AccountPage.SINGULARNAME', 'Account Page'); } public function i18n_plural_name() { return _t('AccountPage.PLURALNAME', 'Account Pages'); } /** * Returns the link to the AccountPage on this site. * * @param string $action [optional] * * @return string (URLSegment) */ public static function find_link(?string $action = null) { $page = DataObject::get_one( AccountPage::class, ['ClassName' => AccountPage::class] ); if ($page) { return $page->Link($action); } return '404-account-page'; } /** * Returns a list of all previous orders for the member / account. * * @return \SilverStripe\ORM\DataList */ public function PastOrders() { $this->calculatePastOrders(); return $this->pastOrders; } /** * casted variable. * * @return float (casted as Currency) */ public function getRunningTotal() { return $this->getRunningTotal(); } public function RunningTotal() { $this->calculatePastOrders(); return $this->calculatedTotal; } /** * casted variable. * * @return float (casted as Currency) */ public function getRunningPaid() { return $this->getRunningPaid(); } public function RunningPaid() { $this->calculatePastOrders(); return $this->calculatedPaid; } /** * casted variable. * * @return float (casted as Currency) */ public function getRunningOutstanding() { return $this->getRunningOutstanding(); } public function RunningOutstanding() { $this->calculatePastOrders(); return $this->calculatedOutstanding; } /** * tells us if the current page is part of e-commerce. * * @return bool */ public function IsEcommercePage() { return true; } /** * retrieves previous orders and adds totals to it... * return DataList. */ protected function calculatePastOrders() { if (! $this->pastOrders) { $this->pastOrders = $this->pastOrdersSelection(); $this->calculatedTotal = 0; $this->calculatedPaid = 0; $this->calculatedOutstanding = 0; if ($this->pastOrders->exists()) { foreach ($this->pastOrders as $order) { $this->calculatedTotal += $order->Total(); $this->calculatedPaid += $order->TotalPaid(); $this->calculatedOutstanding += $order->TotalOutstanding(); } } } return $this->pastOrders; } /** * @return \SilverStripe\ORM\DataList (Orders) */ protected function pastOrdersSelection() { $memberID = (int) Member::currentUserID(); if (! $memberID) { //set t $memberID = rand(0, 1000000) * -1; } if ($memberID) { return Order::get() ->where( '"Order"."MemberID" = ' . $memberID . ' AND ("CancelledByID" = 0 OR "CancelledByID" IS NULL)' ) ->innerJoin('OrderStep', '"Order"."StatusID" = "OrderStep"."ID"') ; } return 0; } } |