OrderStatusLog::canCreate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SilverShop\Model;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\ORM\DB;
7
use SilverStripe\ORM\FieldType\DBDate;
8
use SilverStripe\Security\Member;
9
use SilverStripe\Security\Security;
10
11
/**
12
 * Data class that keeps a log of a single
13
 * status of an order.
14
 *
15
 * @property string $Title
16
 * @property string $Note
17
 * @property string $DispatchedBy
18
 * @property DBDate $DispatchedOn
19
 * @property string $DispatchTicket
20
 * @property string $PaymentCode
21
 * @property bool $PaymentOK
22
 * @property bool $SentToCustomer Whether or not this entry has been sent to the customer (e.g. via OrderEmailNotifier)
23
 * @property bool $VisibleToCustomer Whether or not this entry should be visible to the customer (e.g. on order details)
24
 * @property int $AuthorID
25
 * @property int $OrderID
26
 *
27
 * @method Member Author()
28
 * @method Order Order()
29
 */
30
class OrderStatusLog extends DataObject
31
{
32
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
33
        'Title' => 'Varchar(100)',
34
        'Note' => 'Text',
35
        'DispatchedBy' => 'Varchar(100)',
36
        'DispatchedOn' => 'Date',
37
        'DispatchTicket' => 'Varchar(100)',
38
        'PaymentCode' => 'Varchar(100)',
39
        'PaymentOK' => 'Boolean',
40
        'SentToCustomer' => 'Boolean',
41
        'VisibleToCustomer' => 'Boolean',
42
    ];
43
44
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
45
        'Author' => Member::class,
46
        'Order' => Order::class,
47
    ];
48
49
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
50
        'Order.Reference' => [
51
            'filter' => 'PartialMatchFilter',
52
            'title' => 'Order No'
53
        ],
54
        'Order.FirstName' => [
55
            'filter' => 'PartialMatchFilter',
56
            'title' => 'First Name'
57
        ],
58
        'Order.Email' => [
59
            'filter' => 'PartialMatchFilter',
60
            'title' => 'Email'
61
        ]
62
    ];
63
64
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
65
        'Order.Reference' => 'Order No',
66
        'Created' => 'Created',
67
        'Order.Name' => 'Name',
68
        'Order.LatestEmail' => 'Email',
69
        'Title' => 'Title',
70
        'SentToCustomer' => 'Emailed',
71
        'VisibleToCustomer' => 'Visible to customer?'
72
    ];
73
74
    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...
75
76
    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...
77
78
    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...
79
80
    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...
81
82
    /**
83
     * @var bool Whether the link between an Order and OrderStatusLog is required (tested during write validation)
84
     * @see static::validate()
85
     * @config
86
     */
87
    private static $order_is_required = true;
88
89
    public function canCreate($member = null, $context = [])
90
    {
91
        return false;
92
    }
93
94
    public function canDelete($member = null)
95
    {
96
        return false;
97
    }
98
99
    public function canEdit($member = null)
100
    {
101
        return false;
102
    }
103
104
    public function populateDefaults()
105
    {
106
        parent::populateDefaults();
107
        $this->updateWithLastInfo();
108
    }
109
110
    public function requireDefaultRecords()
111
    {
112
        parent::requireDefaultRecords();
113
114
        // If there are existing records with SentToCustomer=true and there are no records with VisibleToCustomer=true,
115
        // then we assume this is an upgrade (if a record was sent to the customer, then by definition it's visible to
116
        // the customer. However, we use the count check to ensure we only make the database change up until at least
117
        // one record has VisibleToCustomer = true (to avoid resetting it in future)
118
        if (OrderStatusLog::get()->filter('VisibleToCustomer', true)->count() == 0) {
119
            // We don't have any records with VisibleToCustomer true, so update all records with SentToCustomer = true
120
            $toUpdate = OrderStatusLog::get()->filter('SentToCustomer', true);
121
            $updated = 0;
122
123
            /** @var OrderStatusLog $log */
124
            foreach ($toUpdate as $log) {
125
                $log->VisibleToCustomer = true;
126
                $log->write();
127
                $updated++;
128
            }
129
130
            $message = sprintf(
131
                'Migrated %d records to new format (set VisibleToCustomer=true where SentToCustomer=true)',
132
                $updated
133
            );
134
135
            DB::alteration_message($message, 'changed');
136
        }
137
    }
138
139
    public function onBeforeWrite()
140
    {
141
        parent::onBeforeWrite();
142
        if (!$this->AuthorID && ($member = Security::getCurrentUser())) {
143
            $this->AuthorID = $member->ID;
144
        }
145
        if (!$this->Title) {
146
            $this->Title = 'Order Update';
147
        }
148
    }
149
150
    public function validate()
151
    {
152
        $validationResult = parent::validate();
153
154
        if (!$this->OrderID && $this->config()->order_is_required) {
155
            $validationResult->addError('there is no order id for Order Status Log');
156
        }
157
158
        return $validationResult;
159
    }
160
161
    protected function updateWithLastInfo()
162
    {
163
        if ($this->OrderID) {
164
            /** @var OrderStatusLog $latestLog */
165
            $latestLog = OrderStatusLog::get()
166
                ->filter('OrderID', $this->OrderID)
167
                ->sort('Created', 'DESC')
168
                ->first();
169
170
            if ($latestLog) {
0 ignored issues
show
introduced by
$latestLog is of type SilverShop\Model\OrderStatusLog, thus it always evaluated to true.
Loading history...
171
                $this->setField('DispatchedBy', $latestLog->DispatchedBy);
172
                $this->setField('DispatchedOn', $latestLog->DispatchedOn);
173
                $this->setField('DispatchTicket', $latestLog->DispatchTicket);
174
                $this->setField('PaymentCode', $latestLog->PaymentCode);
175
                $this->setField('PaymentOK', $latestLog->PaymentOK);
176
                $this->setField('SentToCustomer', $latestLog->SentToCustomer);
177
                $this->setField('VisibleToCustomer', $latestLog->VisibleToCustomer);
178
            }
179
        }
180
    }
181
}
182