Passed
Push — master ( 992bd9...c5fda4 )
by Will
05:25
created

OrderStatusLog::canCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverShop\Model;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\ORM\FieldType\DBDate;
7
use SilverStripe\Security\Member;
8
use SilverStripe\Security\Security;
9
10
/**
11
 * Data class that keeps a log of a single
12
 * status of an order.
13
 *
14
 * @property string $Title
15
 * @property string $Note
16
 * @property string $DispatchedBy
17
 * @property DBDate $DispatchedOn
18
 * @property string $DispatchTicket
19
 * @property string $PaymentCode
20
 * @property bool $PaymentOK
21
 * @property bool $SentToCustomer
22
 * @property int $AuthorID
23
 * @property int $OrderID
24
 *
25
 * @method Member Author()
26
 * @method Order Order()
27
 */
28
class OrderStatusLog extends DataObject
29
{
30
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
31
        'Title' => 'Varchar(100)',
32
        'Note' => 'Text',
33
        'DispatchedBy' => 'Varchar(100)',
34
        'DispatchedOn' => 'Date',
35
        'DispatchTicket' => 'Varchar(100)',
36
        'PaymentCode' => 'Varchar(100)',
37
        'PaymentOK' => 'Boolean',
38
        'SentToCustomer' => 'Boolean',
39
    ];
40
41
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
42
        'Author' => Member::class,
43
        'Order' => Order::class,
44
    ];
45
46
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
47
        'Order.Reference' => [
48
            'filter' => 'PartialMatchFilter',
49
            'title' => 'Order No'
50
        ],
51
        'Order.FirstName' => [
52
            'filter' => 'PartialMatchFilter',
53
            'title' => 'First Name'
54
        ],
55
        'Order.Email' => [
56
            'filter' => 'PartialMatchFilter',
57
            'title' => 'Email'
58
        ]
59
    ];
60
61
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
62
        'Order.Reference' => 'Order No',
63
        'Created' => 'Created',
64
        'Order.Name' => 'Name',
65
        'Order.LatestEmail' => 'Email',
66
        'Title' => 'Title',
67
        'SentToCustomer' => 'Emailed'
68
    ];
69
70
    private static $singular_name = 'Order Log Entry';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
71
72
    private static $plural_name = 'Order Status Log Entries';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
73
74
    private static $default_sort = '"Created" DESC';
0 ignored issues
show
introduced by
The private property $default_sort is not used, and could be removed.
Loading history...
75
76
    private static $table_name = 'SilverShop_OrderStatusLog';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
77
78
    public function canCreate($member = null, $context = [])
79
    {
80
        return false;
81
    }
82
83
    public function canDelete($member = null)
84
    {
85
        return false;
86
    }
87
88
    public function canEdit($member = null)
89
    {
90
        return false;
91
    }
92
93
    public function populateDefaults()
94
    {
95
        parent::populateDefaults();
96
        $this->updateWithLastInfo();
97
    }
98
99
    public function onBeforeWrite()
100
    {
101
        parent::onBeforeWrite();
102
        if (!$this->AuthorID && ($member = Security::getCurrentUser())) {
103
            $this->AuthorID = $member->ID;
104
        }
105
        if (!$this->Title) {
106
            $this->Title = 'Order Update';
107
        }
108
    }
109
110
    public function validate()
111
    {
112
        $validationResult = parent::validate();
113
        if (!$this->OrderID) {
114
            $validationResult->addError('there is no order id for Order Status Log');
115
        }
116
        return $validationResult;
117
    }
118
119
    protected function updateWithLastInfo()
120
    {
121
        if ($this->OrderID) {
122
            if ($latestLog = OrderStatusLog::get()->filter('OrderID', $this->OrderID)->sort('Created', 'DESC')->first()
123
            ) {
124
                $this->DispatchedBy = $latestLog->DispatchedBy;
125
                $this->DispatchedOn = $latestLog->DispatchedOn;
126
                $this->DispatchTicket = $latestLog->DispatchTicket;
127
                $this->PaymentCode = $latestLog->PaymentCode;
128
                $this->PaymentOK = $latestLog->PaymentOK;
129
            }
130
        }
131
    }
132
}
133