Source of file OrderStepSent.php
Size: 6,587 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/Model/Process/OrderSteps/OrderStepSent.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 | <?php namespace Sunnysideup\Ecommerce\Model\Process\OrderSteps; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\HeaderField; use SilverStripe\Forms\HTMLEditor\HTMLEditorField; use SilverStripe\Forms\TextField; use Sunnysideup\Ecommerce\Email\OrderStatusEmail; use Sunnysideup\Ecommerce\Interfaces\OrderStepInterface; use Sunnysideup\Ecommerce\Model\Order; use Sunnysideup\Ecommerce\Model\Process\OrderStatusLogs\OrderStatusLogDispatchPhysicalOrder; use Sunnysideup\Ecommerce\Model\Process\OrderStep; /** * @authors: Nicolaas [at] Sunny Side Up .co.nz * @package: ecommerce * @sub-package: model */ class OrderStepSent extends OrderStep implements OrderStepInterface { /** * @var string */ protected $emailClassName = OrderStatusEmail::class; /** * The OrderStatusLog that is relevant to the particular step. * * @var string */ protected $relevantLogEntryClassName = OrderStatusLogDispatchPhysicalOrder::class; private static $table_name = 'OrderStepSent'; private static $db = [ 'SendDetailsToCustomer' => 'Boolean', 'EmailSubjectGift' => 'Varchar(200)', 'CustomerMessageGift' => 'HTMLText', ]; private static $defaults = [ 'CustomerCanEdit' => 0, 'CustomerCanCancel' => 0, 'CustomerCanPay' => 0, 'Name' => 'Send Order', 'Code' => 'SENT', 'ShowAsInProcessOrder' => 1, ]; private static $field_labels = [ 'EmailSubjectGift' => 'Email subject', 'CustomerMessageGift' => 'Customer message', ]; public function getCMSFields() { $fields = parent::getCMSFields(); $fields->addFieldToTab( 'Root.Main', HeaderField::create( 'ActuallySendDetails', _t('OrderStep.ACTUALLYSENDDETAILS', 'Send details to the customer?'), 3 ), 'SendDetailsToCustomer' ); $fields->addFieldsToTab( 'Root.CustomerMessage', [ HeaderField::create( 'GiftHeader', _t('OrderStep.SEPARATE_DELIVERY', 'Message for separate shipping address ...') ), TextField::create( 'EmailSubjectGift', _t('OrderStep.EmailSubjectGift', 'Subject') ), HTMLEditorField::create( 'CustomerMessageGift', _t('OrderStep.CustomerMessageGift', 'Message') )->setRows(5), ] ); return $fields; } /** *initStep: * makes sure the step is ready to run.... (e.g. check if the order is ready to be emailed as receipt). * should be able to run this function many times to check if the step is ready. * * @see Order::doNextStatus * * @param Order $order object * * @return bool - true if the current step is ready to be run... */ public function initStep(Order $order): bool { if (false === $this->RelevantLogEntries($order)->exists()) { $className = $this->getRelevantLogEntryClassName(); $log = $className::create(); $log->OrderID = $order->ID; $log->write(); } return true; } /** *doStep: * should only be able to run this function once * (init stops you from running it twice - in theory....) * runs the actual step. * * @see Order::doNextStatus * * @param Order $order object * * @return bool - true if run correctly */ public function doStep(Order $order): bool { $log = $this->RelevantLogEntry($order); if ($log) { if ($log->InternalUseOnly || $this->hasBeenSent($order, false)) { return true; //do nothing } return $order->sendEmail( $this->getEmailClassName(), $subject = $this->CalculatedEmailSubject($order), $message = $this->CalculatedCustomerMessage($order), $resend = false, ! (bool) $this->SendDetailsToCustomer ); } return false; } /** *nextStep: * returns the next step (after it checks if everything is in place for the next step to run...). * * @see Order::doNextStatus * * @return null|OrderStep (next step OrderStep object) */ public function nextStep(Order $order) { $log = $this->RelevantLogEntry($order); if ($log) { if ($log->Sent) { return parent::nextStep($order); } } return null; } /** * Allows the opportunity for the Order Step to add any fields to Order::getCMSFields. * * @return \SilverStripe\Forms\FieldList */ public function addOrderStepFields(FieldList $fields, Order $order, ?bool $nothingToDo = false) { $fields = parent::addOrderStepFields($fields, $order); $title = _t('OrderStep.MUSTENTERDISPATCHRECORD', ' ... To move this order to the next step please enter dispatch details.'); $fields->addFieldsToTab( 'Root.Next', [ $order->getOrderStatusLogsTableField(OrderStatusLogDispatchPhysicalOrder::class, $title), ] ); return $fields; } public function CalculatedEmailSubject(?Order $order = null): string { $v = ''; if ($order && $order->IsSeparateShippingAddress()) { $v = $this->EmailSubjectGift; } if (! $v) { $v = $this->EmailSubject; } return (string) $v; } public function CalculatedCustomerMessage(Order $order = null): string { $v = ''; if ($order && $order->IsSeparateShippingAddress()) { $v = $this->CustomerMessageGift; } if (! $v) { $v = $this->CustomerMessage; } return (string) $v; } /** * For some ordersteps this returns true... * * @return bool */ protected function hasCustomerMessage() { return $this->SendDetailsToCustomer; } /** * Explains the current order step. * * @return string */ protected function myDescription() { return _t('OrderStep.SENT_DESCRIPTION', 'During this step we record the delivery details for the order such as the courrier ticket number and whatever else is relevant.'); } } |