Passed
Push — 1.0 ( dbac97...74fb09 )
by Morven
04:14
created

OrderAdmin   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 124
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getExportFields() 0 41 2
B getEditForm() 0 42 5
A getList() 0 13 2
1
<?php
2
3
namespace SilverCommerce\OrdersAdmin\Admin;
4
5
use SilverStripe\Admin\ModelAdmin;
6
use Colymba\BulkManager\BulkManager;
7
use SilverCommerce\OrdersAdmin\Model\Invoice;
8
use SilverCommerce\OrdersAdmin\Model\Estimate;
9
use Colymba\BulkManager\BulkAction\UnlinkHandler;
10
use SilverStripe\Forms\GridField\GridFieldDetailForm;
11
use SilverCommerce\OrdersAdmin\Forms\GridField\OrdersDetailForm;
12
use Colymba\BulkManager\BulkAction\EditHandler;
13
use SilverCommerce\CatalogueAdmin\BulkManager\CancelHandler;
14
use SilverCommerce\CatalogueAdmin\BulkManager\RefundHandler;
15
use SilverCommerce\CatalogueAdmin\BulkManager\PendingHandler;
16
use SilverCommerce\CatalogueAdmin\BulkManager\PartPaidHandler;
17
use SilverCommerce\CatalogueAdmin\BulkManager\PaidHandler;
18
use SilverCommerce\CatalogueAdmin\BulkManager\ProcessingHandler;
19
use SilverCommerce\CatalogueAdmin\BulkManager\DispatchedHandler;
20
21
 /**
22
  * Add interface to manage orders through the CMS
23
  *
24
  * @package Commerce
25
  */
26
class OrderAdmin extends ModelAdmin
27
{
28
29
    private static $url_segment = 'sales';
0 ignored issues
show
introduced by
The private property $url_segment is not used, and could be removed.
Loading history...
30
31
    private static $menu_title = 'Sales';
0 ignored issues
show
introduced by
The private property $menu_title is not used, and could be removed.
Loading history...
32
33
    private static $menu_priority = 4;
0 ignored issues
show
introduced by
The private property $menu_priority is not used, and could be removed.
Loading history...
34
35
    private static $menu_icon_class = 'font-icon-book-open';
0 ignored issues
show
introduced by
The private property $menu_icon_class is not used, and could be removed.
Loading history...
36
    
37
    private static $managed_models = [
0 ignored issues
show
introduced by
The private property $managed_models is not used, and could be removed.
Loading history...
38
        Invoice::class,
39
        Estimate::class
40
    ];
41
42
    private static $model_importers = [];
0 ignored issues
show
introduced by
The private property $model_importers is not used, and could be removed.
Loading history...
43
44
    public $showImportForm = [];
45
46
    /**
47
     * For an order, export all fields by default
48
     * 
49
     */
50
    public function getExportFields()
51
    {
52
        if ($this->modelClass == Invoice::class) {
53
            $return = array(
54
                "OrderNumber"       => "#",
55
                "Status"            => "Status",
56
                "Created"           => "Created",
57
                "Company"           => "Company Name",
58
                "FirstName"         => "First Name(s)",
59
                "Surname"           => "Surname",
60
                "Email"             => "Email",
61
                "PhoneNumber"       => "Phone Number",
62
                "ItemSummary"       => "Items Ordered",
63
                "SubTotal"          => "SubTotal",
64
                "Postage"           => "Postage",
65
                "TaxTotal"          => "TaxTotal",
66
                "Total"             => "Total",
67
                "Address1"          => "Billing Address 1",
68
                "Address2"          => "Billing Address 2",
69
                "City"              => "Billing City",
70
                "PostCode"          => "Billing Post Code",
71
                "CountryFull"       => "Billing Country",
72
                "DeliveryFirstnames"=> "Delivery First Name(s)",
73
                "DeliverySurname"   => "Delivery Surname",
74
                "DeliveryAddress1"  => "Delivery Address 1",
75
                "DeliveryAddress2"  => "Delivery Address 2",
76
                "DeliveryCity"      => "Delivery City",
77
                "DeliveryPostCode"  => "Delivery Post Code",
78
                "DeliveryCountryFull"=> "Delivery Country",
79
                "DiscountAmount"    => "Discount Amount",
80
                "PostageType"       => "Postage Type",
81
                "PostageCost"       => "Postage Cost",
82
                "PostageTax"        => "Postage Tax",
83
            );
84
        } else {
85
            $return = singleton($this->modelClass)->summaryFields();
86
        }
87
88
        $this->extend("updateExportFields", $return);
89
90
        return $return;
91
    }
92
93
    public function getEditForm($id = null, $fields = null)
94
    {
95
        $form = parent::getEditForm($id, $fields);
96
        $fields = $form->Fields();
97
        $gridfield = $fields
98
            ->fieldByName($this->sanitiseClassName($this->modelClass));
99
        $config = null;
100
        
101
        // Bulk manager
102
        $manager = new BulkManager();
103
        $manager->removeBulkAction(EditHandler::class);
104
        $manager->removeBulkAction(UnlinkHandler::class);
105
106
        // Manage orders
107
        if ($this->modelClass == Invoice::class && $gridfield) {
108
            $config = $gridfield->getConfig();
109
110
            $manager->addBulkAction(CancelHandler::class);
111
            $manager->addBulkAction(RefundHandler::class);
112
            $manager->addBulkAction(PendingHandler::class);
113
            $manager->addBulkAction(PartPaidHandler::class);
114
            $manager->addBulkAction(PaidHandler::class);
115
            $manager->addBulkAction(ProcessingHandler::class);
116
            $manager->addBulkAction(DispatchedHandler::class);
117
        }
118
        
119
        // Manage Estimates
120
        if ($this->modelClass == Estimate::class) {
121
            $config = $gridfield->getConfig();
122
        }
123
        
124
        // Set our default detailform and bulk manager
125
        if ($config) {
126
            $config
127
                ->removeComponentsByType(GridFieldDetailForm::class)
128
                ->addComponent($manager)
129
                ->addComponent(new OrdersDetailForm());
130
        }
131
132
        $this->extend("updateEditForm", $form);
133
134
        return $form;
135
    }
136
    
137
    public function getList()
138
    {
139
        $list = parent::getList();
140
        
141
        // Ensure that we only show Order objects in the order tab
142
        if ($this->modelClass == Estimate::class) {
143
            $list = $list
144
                ->addFilter(array("ClassName" => Estimate::class));
145
        }
146
                
147
        $this->extend("updateList", $list);
148
149
        return $list;
150
    }
151
}
152