| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * The order class is a databound object for handling Orders | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * within SilverStripe. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @property string|float Currency | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @property string Reference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @property string Placed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @property string Paid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @property string ReceiptSent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @property string Printed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @property string Dispatched | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @property string Status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @property string FirstName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @property string Surname | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @property string Email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @property string Notes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @property string IPAddress | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @property string|bool SeparateBillingAddress | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @property string Locale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @property string|int MemberID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @property string|int ShippingAddressID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @property string|int BillingAddressID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @method Member|ShopMember Member | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @method Address BillingAddress | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @method Address ShippingAddress | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @method OrderItem[]|HasManyList Items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @method OrderModifier[]|HasManyList Modifiers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @method OrderStatusLog[]|HasManyList OrderStatusLogs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @package shop | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | class Order extends DataObject | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Status codes and what they mean: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * Unpaid (default): Order created but no successful payment by customer yet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Query: Order not being processed yet (customer has a query, or could be out of stock) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Paid: Order successfully paid for by customer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Processing: Order paid for, package is currently being processed before shipping to customer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Sent: Order paid for, processed for shipping, and now sent to the customer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Complete: Order completed (paid and shipped). Customer assumed to have received their goods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * AdminCancelled: Order cancelled by the administrator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * MemberCancelled: Order cancelled by the customer (Member) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     private static $db                = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         'Total'                  => 'Currency', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         'Reference'              => 'Varchar', //allow for customised order numbering schemes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         //status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         'Placed'                 => "SS_Datetime", //date the order was placed (went from Cart to Order) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         'Paid'                   => 'SS_Datetime', //no outstanding payment left | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         'ReceiptSent'            => 'SS_Datetime', //receipt emailed to customer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         'Printed'                => 'SS_Datetime', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         'Dispatched'             => 'SS_Datetime', //products have been sent to customer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         'Status'                 => "Enum('Unpaid,Paid,Processing,Sent,Complete,AdminCancelled,MemberCancelled,Cart','Cart')", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         //customer (for guest orders) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         'FirstName'              => 'Varchar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         'Surname'                => 'Varchar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         'Email'                  => 'Varchar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         'Notes'                  => 'Text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         'IPAddress'              => 'Varchar(15)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         //separate shipping | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         'SeparateBillingAddress' => 'Boolean', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         // keep track of customer locale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         'Locale'                 => 'DBLocale', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     private static $has_one           = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         'Member'          => 'Member', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         'ShippingAddress' => 'Address', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         'BillingAddress'  => 'Address', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     private static $has_many          = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         'Items'           => 'OrderItem', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         'Modifiers'       => 'OrderModifier', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         'OrderStatusLogs' => 'OrderStatusLog', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     private static $defaults          = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         'Status' => 'Cart', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     private static $casting           = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         'FullBillingAddress'  => 'Text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         'FullShippingAddress' => 'Text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         'Total'               => 'Currency', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         'SubTotal'            => 'Currency', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         'TotalPaid'           => 'Currency', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         'Shipping'            => 'Currency', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         'TotalOutstanding'    => 'Currency', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     private static $summary_fields    = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         'Reference'   => 'Order No', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         'Placed'      => 'Date', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         'Name'        => 'Customer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         'LatestEmail' => 'Email', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         'Total'       => 'Total', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         'Status'      => 'Status', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     private static $searchable_fields = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         'Reference' => array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         'FirstName' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             'title' => 'Customer Name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         'Email'     => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             'title' => 'Customer Email', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         'Status'    => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             'filter' => 'ExactMatchFilter', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             'field'  => 'CheckboxSetField', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     private static $singular_name     = "Order"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     private static $plural_name       = "Orders"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     private static $default_sort      = "\"Placed\" DESC, \"Created\" DESC"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * Statuses for orders that have been placed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     private static $placed_status = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         'Paid', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         'Unpaid', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         'Processing', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         'Sent', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         'Complete', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         'MemberCancelled', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         'AdminCancelled', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * Statuses for which an order can be paid for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     private static $payable_status = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         'Cart', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         'Unpaid', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         'Processing', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         'Sent', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * Statuses that shouldn't show in user account. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     private static $hidden_status = array('Cart'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * Flags to determine when an order can be cancelled. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     private static $cancel_before_payment    = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     private static $cancel_before_processing = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     private static $cancel_before_sending    = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     private static $cancel_after_sending     = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * Place an order before payment processing begins | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * @var boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     private static $place_before_payment = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * Modifiers represent the additional charges or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * deductions associated to an order, such as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * shipping, taxes, vouchers etc. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     private static $modifiers            = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     private static $rounding_precision   = 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     private static $reference_id_padding = 5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * @var boolean Will allow completion of orders with GrandTotal=0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      * which could be the case for orders paid with loyalty points or vouchers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      * Will send the "Paid" date on the order, even though no actual payment was taken. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      * Will trigger the payment related extension points: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      * Order->onPayment, OrderItem->onPayment, Order->onPaid. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     private static $allow_zero_order_total = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     public static function get_order_status_options() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         $values = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         foreach (singleton('Order')->dbObject('Status')->enumValues(false) as $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |             $values[$value] = _t('Order.STATUS_' . strtoupper($value), $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         return $values; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      * Create CMS fields for cms viewing and editing orders | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     public function getCMSFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         $fields = FieldList::create(TabSet::create('Root', Tab::create('Main'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         $fs = "<div class=\"field\">"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         $fe = "</div>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         $parts = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |             DropdownField::create("Status", _t('Order.db_Status', "Status"), self::get_order_status_options()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |             LiteralField::create('Customer', $fs . $this->renderWith("OrderAdmin_Customer") . $fe), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |             LiteralField::create('Addresses', $fs . $this->renderWith("OrderAdmin_Addresses") . $fe), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |             LiteralField::create('Content', $fs . $this->renderWith("OrderAdmin_Content") . $fe), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |         if ($this->Notes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |             $parts[] = LiteralField::create('Notes', $fs . $this->renderWith("OrderAdmin_Notes") . $fe); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         $fields->addFieldsToTab('Root.Main', $parts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |         $this->extend('updateCMSFields', $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |         if ($payments = $fields->fieldByName("Root.Payments.Payments")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |             $fields->removeByName("Payments"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |             $fields->insertAfter($payments, "Content"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |             $payments->addExtraClass("order-payments"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         return $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |      * Adjust scafolded search context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |      * @return SearchContext the updated search context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |     public function getDefaultSearchContext() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |         $context = parent::getDefaultSearchContext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |         $fields = $context->getFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |         $fields->push( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |             ListboxField::create("Status", _t('Order.db_Status', "Status")) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |                 ->setSource( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |                     array_combine( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |                         self::config()->placed_status, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |                         self::config()->placed_status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |                 ->setMultiple(true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |         // add date range filtering | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         $fields->insertBefore( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |             DateField::create("DateFrom", _t('Order.DateFrom', "Date from")) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |                 ->setConfig('showcalendar', true), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |             'Status' | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |         $fields->insertBefore( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |             DateField::create("DateTo", _t('Order.DateTo', "Date to")) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |                 ->setConfig('showcalendar', true), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |             'Status' | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         // get the array, to maniplulate name, and fullname seperately | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |         $filters = $context->getFilters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |         $filters['DateFrom'] = GreaterThanFilter::create('Placed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |         $filters['DateTo'] = LessThanFilter::create('Placed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |         // filter customer need to use a bunch of different sources | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |         $filters['FirstName'] = new MultiFieldPartialMatchFilter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |             'FirstName', false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |             array('SplitWords'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |             array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |                 'Surname', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |                 'Member.FirstName', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |                 'Member.Surname', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |                 'BillingAddress.FirstName', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |                 'BillingAddress.Surname', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |                 'ShippingAddress.FirstName', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |                 'ShippingAddress.Surname', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |         $context->setFilters($filters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |         $this->extend('updateDefaultSearchContext', $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |         return $context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |      * Hack for swapping out relation list with OrderItemList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 | 42 |  |     public function getComponents($componentName, $filter = "", $sort = "", $join = "", $limit = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 | 42 |  |         $components = parent::getComponents($componentName, $filter = "", $sort = "", $join = "", $limit = null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 | 42 |  |         if ($componentName === "Items" && get_class($components) !== "UnsavedRelationList") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 | 38 |  |             $query = $components->dataQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 | 38 |  |             $components = OrderItemList::create("OrderItem", "OrderID"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 | 38 |  |             if ($this->model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 | 38 |  |                 $components->setDataModel($this->model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 | 38 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 | 38 |  |             $components->setDataQuery($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 | 38 |  |             $components = $components->forForeignID($this->ID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 | 38 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 | 42 |  |         return $components; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |      * Returns the subtotal of the items for this order. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 | 23 |  |     public function SubTotal() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 | 23 |  |         if ($this->Items()->exists()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 | 17 |  |             return $this->Items()->SubTotal(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 | 8 |  |         return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |      * Calculate the total | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |      * @return the final total | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 | 18 |  |     public function calculate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 | 18 |  |         if (!$this->IsCart()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 | 1 |  |             return $this->Total; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 | 17 |  |         $calculator = new OrderTotalCalculator($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 | 17 |  |         return $this->Total = $calculator->calculate(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |      * This is needed to maintain backwards compatiability with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |      * some subsystems using modifiers. eg discounts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |     public function getModifier($className, $forcecreate = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |         $calculator = new OrderTotalCalculator($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |         return $calculator->getModifier($className, $forcecreate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |      * Enforce rounding precision when setting total | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 | 75 |  |     public function setTotal($val) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 | 75 |  |         $this->setField("Total", round($val, self::$rounding_precision)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 | 75 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |      * Get final value of order. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |      * Retrieves value from DataObject's record array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 | 19 |  |     public function Total() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 | 19 |  |         return $this->getField("Total"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |      * Alias for Total. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 | 17 |  |     public function GrandTotal() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 | 17 |  |         return $this->Total(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |      * Calculate how much is left to be paid on the order. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  |      * Enforces rounding precision. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |      * Payments that have been authorized via a non-manual gateway should count towards the total paid amount. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |      * However, it's possible to exclude these by setting the $includeAuthorized parameter to false, which is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |      * useful to determine the status of the Order. Order status should only change to 'Paid' when all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |      * payments are 'Captured'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |      * @param bool $includeAuthorized whether or not to include authorized payments (excluding manual payments) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |      * @return float | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 | 15 |  |     public function TotalOutstanding($includeAuthorized = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 | 15 |  |         return round( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 | 15 |  |             $this->GrandTotal() - ($includeAuthorized ? $this->TotalPaidOrAuthorized() : $this->TotalPaid()), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 | 15 |  |             self::config()->rounding_precision | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 | 15 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |      * Get the order status. This will return a localized value if available. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |      * @return string the payment status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 | 2 |  |     public function getStatusI18N() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 | 2 |  |         return _t('Order.STATUS_' . strtoupper($this->Status), $this->Status); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |      * Get the link for finishing order processing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 | 3 |  |     public function Link() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 | 3 |  |         if (Member::currentUser()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 | 1 |  |             return Controller::join_links(AccountPage::find_link(), 'order', $this->ID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 | 2 |  |         return CheckoutPage::find_link(false, "order", $this->ID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |      * Returns TRUE if the order can be cancelled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |      * PRECONDITION: Order is in the DB. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 | 4 |  |     public function canCancel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 | 4 |  |         switch ($this->Status) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 | 4 |  |             case 'Unpaid' : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 | 4 |  |                 return self::config()->cancel_before_payment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 | 1 |  |             case 'Paid' : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 | 1 |  |                 return self::config()->cancel_before_processing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 | 1 |  |             case 'Processing' : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 | 1 |  |                 return self::config()->cancel_before_sending; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 | 1 |  |             case 'Sent' : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 422 | 1 |  |             case 'Complete' : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 423 | 1 |  |                 return self::config()->cancel_after_sending; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 424 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 425 | 1 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 426 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 427 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 428 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 429 |  |  |      * Check if an order can be paid for. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 430 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 431 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 432 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 433 | 5 |  |     public function canPay($member = null) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 434 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 435 | 5 |  |         if (!in_array($this->Status, self::config()->payable_status)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 436 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 437 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 438 | 5 |  |         if ($this->TotalOutstanding(true) > 0 && empty($this->Paid)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 439 | 5 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 440 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 441 | 1 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 442 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 443 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 444 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 445 |  |  |      * Prevent deleting orders. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 446 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 447 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 448 | 1 |  |     public function canDelete($member = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 449 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 450 | 1 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 451 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 452 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 |  |  |      * Check if an order can be viewed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 |  |  |     public function canView($member = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 459 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 460 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 461 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 462 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 463 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 464 |  |  |      * Check if an order can be edited. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 465 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 466 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 467 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 468 | 1 |  |     public function canEdit($member = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 469 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 470 | 1 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 471 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 472 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 473 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 474 |  |  |      * Prevent standard creation of orders. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 475 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 476 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 477 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 478 | 1 |  |     public function canCreate($member = null, $context = array()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 479 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 480 | 1 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 481 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 482 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 483 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 484 |  |  |      * Return the currency of this order. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 485 |  |  |      * Note: this is a fixed value across the entire site. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 486 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 487 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 488 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 489 | 5 |  |     public function Currency() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 490 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 491 | 5 |  |         return ShopConfig::get_site_currency(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 492 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 493 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 494 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 495 |  |  |      * Get the latest email for this order.z | 
            
                                                                                                            
                            
            
                                    
            
            
                | 496 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 497 | 5 |  |     public function getLatestEmail() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 498 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 499 | 5 |  |         if ($this->MemberID && ($this->Member()->LastEdited > $this->LastEdited || !$this->Email)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 500 | 3 |  |             return $this->Member()->Email; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 501 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 502 | 2 |  |         return $this->getField('Email'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 503 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 504 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 505 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 506 |  |  |      * Gets the name of the customer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 507 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 508 |  |  |     public function getName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 509 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 510 |  |  |         $firstname = $this->FirstName ? $this->FirstName : $this->Member()->FirstName; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 511 |  |  |         $surname = $this->FirstName ? $this->Surname : $this->Member()->Surname; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 512 |  |  |         return implode(" ", array_filter(array($firstname, $surname))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 513 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 514 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 515 |  |  |     public function getTitle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 516 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 517 |  |  |         return $this->Reference . " - " . $this->dbObject('Placed')->Nice(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 518 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 519 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 520 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 521 |  |  |      * Get shipping address, or member default shipping address. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 522 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 523 | 2 |  |     public function getShippingAddress() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 524 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 525 | 2 |  |         return $this->getAddress('Shipping'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 526 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 527 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 528 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 529 |  |  |      * Get billing address, if marked to use seperate address, otherwise use shipping address, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 530 |  |  |      * or the member default billing address. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 531 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 532 | 2 |  |     public function getBillingAddress() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 533 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 534 | 2 |  |         if (!$this->SeparateBillingAddress && $this->ShippingAddressID === $this->BillingAddressID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 535 | 2 |  |             return $this->getShippingAddress(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 536 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 537 |  |  |             return $this->getAddress('Billing'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 538 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 539 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 540 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 541 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 542 |  |  |      * @param string $type - Billing or Shipping | 
            
                                                                                                            
                            
            
                                    
            
            
                | 543 |  |  |      * @return Address | 
            
                                                                                                            
                            
            
                                    
            
            
                | 544 |  |  |      * @throws Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 545 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 546 | 2 |  |     protected function getAddress($type) | 
            
                                                                        
                            
            
                                    
            
            
                | 547 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 548 | 2 |  |         $address = $this->getComponent($type . 'Address'); | 
            
                                                                        
                            
            
                                    
            
            
                | 549 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 550 | 2 |  |         if (!$address || !$address->exists() && $this->Member()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 551 | 2 |  |             $address = $this->Member()->{"Default${type}Address"}(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 552 | 2 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 553 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 554 |  |  |         if (empty($address->Surname) && empty($address->FirstName)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 555 |  |  |             if ($member = $this->Member()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 556 |  |  |                 // If there's a member object, use information from the Member. | 
            
                                                                        
                            
            
                                    
            
            
                | 557 |  |  |                 // The information from Order should have precendence if set though! | 
            
                                                                        
                            
            
                                    
            
            
                | 558 |  |  |                 $address->FirstName = $this->FirstName ?: $member->FirstName; | 
            
                                                                        
                            
            
                                    
            
            
                | 559 |  |  |                 $address->Surname = $this->Surname ?: $member->Surname; | 
            
                                                                        
                            
            
                                    
            
            
                | 560 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 561 |  |  |                 $address->FirstName = $this->FirstName; | 
            
                                                                        
                            
            
                                    
            
            
                | 562 |  |  |                 $address->Surname = $this->Surname; | 
            
                                                                        
                            
            
                                    
            
            
                | 563 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 564 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 565 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 566 |  |  |         return $address; | 
            
                                                                        
                            
            
                                    
            
            
                | 567 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 568 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 569 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 570 |  |  |      * Check if the two addresses saved differ. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 571 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 572 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 573 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 574 |  |  |     public function getAddressesDiffer() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 575 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 576 |  |  |         return $this->SeparateBillingAddress || $this->ShippingAddressID !== $this->BillingAddressID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 577 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 578 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 579 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 580 | 1 |  |      * Has this order been sent to the customer? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 581 |  |  |      * (at "Sent" status). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 582 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 583 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 584 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 585 |  |  |     public function IsSent() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 586 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 587 |  |  |         return $this->Status == 'Sent'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 588 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 589 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 590 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 591 | 1 |  |      * Is this order currently being processed? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 592 |  |  |      * (at "Sent" OR "Processing" status). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 593 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 594 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 595 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 596 |  |  |     public function IsProcessing() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 597 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 598 |  |  |         return $this->IsSent() || $this->Status == 'Processing'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 599 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 600 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 601 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 602 |  |  |      * Return whether this Order has been paid for (Status == Paid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 603 | 2 |  |      * or Status == Processing, where it's been paid for, but is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 604 |  |  |      * currently in a processing state. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 605 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 606 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 607 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 608 | 92 |  |     public function IsPaid() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 609 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 610 |  |  |         return (boolean)$this->Paid || $this->Status == 'Paid'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 611 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 612 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 613 |  |  |     public function IsCart() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 614 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 615 |  |  |         return $this->Status == 'Cart'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 616 | 75 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 617 | 75 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 618 | 75 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 619 |  |  |      * Create a unique reference identifier string for this order. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 620 | 75 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 621 | 75 |  |     public function generateReference() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 622 | 75 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 623 | 75 |  |         $reference = str_pad($this->ID, self::$reference_id_padding, '0', STR_PAD_LEFT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 624 | 75 |  |         $this->extend('generateReference', $reference); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 625 | 75 |  |         $candidate = $reference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 626 | 75 |  |         //prevent generating references that are the same | 
            
                                                                                                            
                            
            
                                    
            
            
                | 627 |  |  |         $count = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 628 |  |  |         while (DataObject::get_one('Order', "\"Reference\" = '$candidate'")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 629 |  |  |             $count++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 630 |  |  |             $candidate = $reference . "" . $count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 631 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 632 |  |  |         $this->Reference = $candidate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 633 | 75 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 634 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 635 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 636 |  |  |      * Get the reference for this order, or fall back to order ID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 637 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 638 |  |  |     public function getReference() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 639 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 640 |  |  |         return $this->getField('Reference') ? $this->getField('Reference') : $this->ID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 641 | 95 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 642 | 95 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 643 | 75 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 644 | 75 |  |      * Force creating an order reference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 645 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 646 |  |  |     protected function onBeforeWrite() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 647 | 95 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 648 | 6 |  |         parent::onBeforeWrite(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 649 | 6 |  |         if (!$this->getField("Reference") && in_array($this->Status, self::$placed_status)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 650 | 6 |  |             $this->generateReference(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 651 | 6 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 652 | 6 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 653 |  |  |         // perform status transition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 654 |  |  |         if ($this->isInDB() && $this->isChanged('Status')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 655 |  |  |             $this->statusTransition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 656 | 95 |  |                 empty($this->original['Status']) ? 'Cart' : $this->original['Status'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 657 | 95 |  |                 $this->Status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 658 | 95 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 659 | 95 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 660 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 661 |  |  |         // While the order is unfinished/cart, always store the current locale with the order. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 662 |  |  |         // We do this everytime an order is saved, because the user might change locale (language-switch). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 663 |  |  |         if ($this->Status == 'Cart') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 664 |  |  |             $this->Locale = ShopTools::get_current_locale(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 665 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 666 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 667 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 668 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 669 | 6 |  |      * Called from @see onBeforeWrite whenever status changes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 670 |  |  |      * @param string $fromStatus status to transition away from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 671 | 6 |  |      * @param string $toStatus target status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 672 | 2 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 673 | 2 |  |     protected function statusTransition($fromStatus, $toStatus) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 674 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 675 | 2 |  |         // Add extension hook to react to order status transitions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 676 |  |  |         $this->extend('onStatusChange', $fromStatus, $toStatus); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 677 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 678 |  |  |         if ($toStatus == 'Paid' && !$this->Paid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 679 | 2 |  |             $this->Paid = SS_Datetime::now()->Rfc2822(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 680 | 2 |  |             foreach ($this->Items() as $item) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 681 | 2 |  |                 $item->onPayment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 682 | 2 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 683 | 2 |  |             //all payment is settled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 684 | 6 |  |             $this->extend('onPaid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 685 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 686 |  |  |             if (!$this->ReceiptSent) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 687 |  |  |                 OrderEmailNotifier::create($this)->sendReceipt(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 688 |  |  |                 $this->ReceiptSent = SS_Datetime::now()->Rfc2822(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 689 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 690 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 691 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 692 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 693 | 2 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 694 |  |  |      * delete attributes, statuslogs, and payments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 695 | 2 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 696 | 1 |  |     protected function onBeforeDelete() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 697 | 2 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 698 |  |  |         foreach ($this->Items() as $item) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 699 | 2 |  |             $item->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 700 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 701 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 702 |  |  |         foreach ($this->Modifiers() as $modifier) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 703 |  |  |             $modifier->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 704 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 705 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 706 |  |  |         foreach ($this->OrderStatusLogs() as $logEntry) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 707 | 2 |  |             $logEntry->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 708 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 709 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 710 |  |  |         // just remove the payment relations… | 
            
                                                                                                            
                            
            
                                    
            
            
                | 711 |  |  |         // that way payment objects still persist (they might be relevant for book-keeping?) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 712 |  |  |         $this->Payments()->removeAll(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 713 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 714 |  |  |         parent::onBeforeDelete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 715 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 716 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 717 |  |  |     public function debug() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 718 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 719 |  |  |         $val = "<div class='order'><h1>$this->class</h1>\n<ul>\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 720 |  |  |         if ($this->record) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 721 |  |  |             foreach ($this->record as $fieldName => $fieldVal) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 722 |  |  |                 $val .= "\t<li>$fieldName: " . Debug::text($fieldVal) . "</li>\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 723 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 724 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 725 |  |  |         $val .= "</ul>\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 726 |  |  |         $val .= "<div class='items'><h2>Items</h2>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 727 |  |  |         if ($items = $this->Items()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 728 |  |  |             $val .= $this->Items()->debug(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 729 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 730 |  |  |         $val .= "</div><div class='modifiers'><h2>Modifiers</h2>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 731 |  |  |         if ($modifiers = $this->Modifiers()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 732 |  |  |             $val .= $modifiers->debug(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 733 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 734 |  |  |         $val .= "</div></div>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 735 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 736 |  |  |         return $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 737 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 738 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 739 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 740 |  |  |      * Provide i18n entities for the order class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 741 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 742 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 743 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 744 |  |  |     public function provideI18nEntities() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 745 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 746 |  |  |         $entities = parent::provideI18nEntities(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 747 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 748 |  |  |         // collect all the payment status values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 749 |  |  |         foreach ($this->dbObject('Status')->enumValues() as $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 750 |  |  |             $key = strtoupper($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 751 |  |  |             $entities["Order.STATUS_$key"] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 752 |  |  |                 $value, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 753 | 2 |  |                 "Translation of the order status '$value'", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 754 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 755 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 756 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 757 |  |  |         return $entities; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 758 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 759 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 760 |  |  |  | 
            
                        
Too many fields generally indicate a class which does too much and does not follow the single responsibility principle.
We suggest taking a look at the “Code” section for further suggestions on how to fix this.