Version: current

      1 <?php
      2 // заказ Услуги заказа
      3 class OrderServiceOrder extends ServiceOrder {
      4     static $db = array(
      5         'AdditionalFieldData' => 'Varchar(255)', // Содержимое дополнительного пол¤ услуги заказа (если есть) - напр. у трансфера номер поезда и время прибытия
      6     );
      7
      8     static $has_one = array(
      9         'OrderService' => 'OrderService', // услуга
     10         'Order' => 'BookingOrder', // заказ
     11     );
     12
     13     static $summary_fields = array('Title', 'AdditionalField',  'OrderServicePrice');
     14
     15     function fieldLabels($includerelations = true) {
     16         $labels = parent::fieldLabels($includerelations);
     17         $labels['Title'] = _t('OrderServiceOrder.Title', 'Title');
     18         $labels['AdditionalField'] = _t('OrderServiceOrder.AdditionalField', 'Additional Field');
     19         $labels['OrderServicePrice'] = _t('OrderServiceOrder.OrderServicePrice', 'Order Service Price');
     20         return $labels;
     21     }
     22
     23     function onBeforeWrite() {
     24         parent::onBeforeWrite();
     25
     26         if ($this->OrderServiceID && $this->OrderService()) {
     27             if (!$this->ServiceTitle) {
     28                 $this->ServiceTitle = $this->OrderService()->Title;
     29             }
     30             if (!$this->ServicePrice) {
     31                 $this->ServicePrice = $this->OrderService()->Price;
     32             }
     33         }
     34     }
     35
     36
     37     function onAfterWrite() {
     38         parent::onAfterWrite();
     39         $order = $this->Order();
     40         $order->calculateOrderCost();
     41         $order->write();
     42     }
     43
     44     function onAfterDelete() {
     45         $order = $this->Order();
     46         $order->calculateOrderCost();
     47         $order->write();
     48         parent::onAfterDelete();
     49     }
     50
     51     function OrderServicePrice() {
     52         $cost = 0;
     53         if ($this->ServicePrice) {
     54             $cost = $this->Count * $this->ServicePrice;
     55         } else {
     56             $cost = $this->Count * $this->OrderService()->Price;
     57         }
     58         if ($this->OrderService()->Repeat == 'every_day') {
     59             $cost *= $this->Order()->OrderLength();
     60         }
     61         return $cost;
     62     }
     63
     64     function getTitle() {
     65         if ($this->ServiceTitle) {
     66             return $this->ServiceTitle;
     67         }
     68         return $this->OrderService()->Title;
     69     }
     70
     71     // ????? Количество - по идее мы создаем экземпляр на каждую заказываемую услугу - поэтому количество всегда равно ЕДИНИЦЕ ?????
     72     /*
     73     function getCount() {
     74         return 1;
     75     }
     76     */
     77
     78     function AdditionalField() {
     79         if ($this->OrderService()->HasAdditionalField) {
     80             return $this->OrderService()->AdditionalFieldTitle . ': ' . $this->AdditionalFieldData;
     81         }
     82     }
     83
     84     function getCMSFields() {
     85         $fields = parent::getCMSFields();
     86         if ($this->OrderService()->HasAdditionalField) {
     87             $fields->replaceField('Count', new HiddenField('Count', 'Count', 1)); // количество всегда равно единице - не показываем поле
     88         }
     89         if ($this->OrderService()->HasAdditionalField) {
     90             $fields->dataFieldByName('AdditionalFieldData')->setTitle($this->OrderService()->AdditionalFieldTitle);
     91         } else {
     92             $fields->removeByName('AdditionalFieldData');
     93         }
     94
     95         $rs = array();
     96         if (OrderService::get_active()) {
     97             $rs = OrderService::get_active()->map();
     98         }
     99         $f = new DropdownField('OrderServiceID', $this->fieldLabel('OrderService'), $rs);
    100         $f->setEmptyString('');
    101         $fields->replaceField('OrderServiceID', $f);
    102
    103
    104         if ($this->OrderServiceID) {
    105             $fields->replaceField('OrderServiceID', $fields->dataFieldByName('OrderServiceID')->performReadonlyTransformation());
    106         }
    107         return $fields;
    108     }
    109
    110
    111
    112 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -