diff --git a/modules/receipt/src/Controller/PrintController.php b/modules/receipt/src/Controller/PrintController.php index c1000ad..2e08eda 100644 --- a/modules/receipt/src/Controller/PrintController.php +++ b/modules/receipt/src/Controller/PrintController.php @@ -10,7 +10,9 @@ use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\HtmlCommand; use Drupal\Core\Ajax\SettingsCommand; use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Core\Url; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Class PrintController. @@ -18,6 +20,32 @@ use Drupal\Core\Url; class PrintController extends ControllerBase { /** + * The date formatter. + * + * @var \Drupal\Core\Datetime\DateFormatterInterface + */ + protected $dateFormatter; + + /** + * Creates a new PrintController object. + * + * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter + * The date formatter. + */ + public function __construct(DateFormatterInterface $date_formatter) { + $this->dateFormatter = $date_formatter; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('date.formatter') + ); + } + + /** * A controller callback. */ public function ajaxReceipt(OrderInterface $commerce_order, $print_or_email) { @@ -62,44 +90,52 @@ class PrintController extends ControllerBase { * A controller callback. */ public function showReceipt(OrderInterface $commerce_order) { - $number_formatter_factory = \Drupal::service('commerce_price.number_formatter_factory'); $number_formatter = $number_formatter_factory->createInstance(); + $is_gift = $commerce_order->getData('gift_order'); + $totals = []; + $gift_order = ''; $sub_total_price = $commerce_order->getSubtotalPrice(); $currency = Currency::load($sub_total_price->getCurrencyCode()); - $formatted_amount = $number_formatter->formatCurrency($sub_total_price->getNumber(), $currency); // In the future add a setting to display group or individual for same skus. $items = $commerce_order->getItems(); foreach ($items as $item) { $totals[] = [ $item->getTitle() . ' (' . $item->getQuantity() . ')', - $number_formatter->formatCurrency($item->getAdjustedTotalPrice()->getNumber(), $currency), + !$is_gift ? $number_formatter->formatCurrency($item->getAdjustedTotalPrice()->getNumber(), $currency) : '', ]; } - $totals[] = ['Subtotal', $formatted_amount]; + if (!$is_gift) { + $formatted_amount = $number_formatter->formatCurrency($sub_total_price->getNumber(), $currency); + $totals[] = ['Subtotal', $formatted_amount]; + } // Commerce appears to have a bug where if no adjustments exist, it will // return a 0 => null array, which will still trigger a foreach loop. - foreach ($commerce_order->collectAdjustments() as $key => $adjustment) { - if (!empty($adjustment)) { - $amount = $adjustment->getAmount(); - $currency = Currency::load($amount->getCurrencyCode()); - $formatted_amount = $number_formatter->formatCurrency($amount->getNumber(), $currency); - - $totals[] = [ - $adjustment->getLabel(), - $formatted_amount, - ]; + if (!$is_gift) { + foreach ($commerce_order->collectAdjustments() as $key => $adjustment) { + if (!empty($adjustment)) { + $amount = $adjustment->getAmount(); + $currency = Currency::load($amount->getCurrencyCode()); + $formatted_amount = $number_formatter->formatCurrency($amount->getNumber(), $currency); + + $totals[] = [ + $adjustment->getLabel(), + $formatted_amount, + ]; + } } } // Collecting the total price on the cart. - $total_price = $commerce_order->getTotalPrice(); - $formatted_amount = $number_formatter->formatCurrency($total_price->getNumber(), $currency); - $totals[] = ['Total', $formatted_amount]; + if (!$is_gift) { + $total_price = $commerce_order->getTotalPrice(); + $formatted_amount = $number_formatter->formatCurrency($total_price->getNumber(), $currency); + $totals[] = ['Total', $formatted_amount]; + } $payment_storage = \Drupal::entityTypeManager()->getStorage('commerce_payment'); $payments = $payment_storage->loadMultipleByOrder($commerce_order); @@ -111,19 +147,22 @@ class PrintController extends ControllerBase { 'class' => ['use-ajax', 'button'], ], ]); - + $order_date = $this->dateFormatter->format($commerce_order->getCompletedTime()); $config = \Drupal::config('commerce_pos_receipt.settings'); + if ($is_gift) { + $gift_order = '' . $this->t('GIFT ORDER') . ''; + } $build = ['#theme' => 'commerce_pos_receipt']; $build['#receipt'] = [ 'header' => [ - '#markup' => check_markup($config->get('header'), $config->get('header_format')), + '#markup' => check_markup($config->get('header'), $config->get('header_format')) . "
$order_date
Order# {$commerce_order->id()}
$gift_order
", ], 'body' => [ '#type' => 'table', '#rows' => $totals, ], 'footer' => [ - '#markup' => check_markup($config->get('footer'), $config->get('footer_format')), + '#markup' => '
' . check_markup($config->get('footer'), $config->get('footer_format')), ], 'print' => [ '#title' => t('Print receipt'), diff --git a/src/Form/POSForm.php b/src/Form/POSForm.php index 927aba4..a095373 100644 --- a/src/Form/POSForm.php +++ b/src/Form/POSForm.php @@ -93,6 +93,8 @@ class POSForm extends ContentEntityForm { /* @var \Drupal\commerce_order\Entity\Order $order */ $order = $this->entity; $form_state->set('commerce_pos_order_id', $order->id()); + // Gift order default value. + $gift_value = $order->getData('gift_order'); $form = parent::buildForm($form, $form_state); $form['#theme'] = 'commerce_pos_form_order'; @@ -107,7 +109,12 @@ class POSForm extends ContentEntityForm { $form['list'] = [ '#type' => 'container', ]; - + // Checkbox if this is gift order. + $form['gift_order'] = [ + '#type' => 'checkbox', + '#title' => $this->t("Gift order"), + '#default_value' => $gift_value, + ]; $form['actions']['submit']['#value'] = t('Payments and Completion'); // Ensure the user is redirected back to this page after deleting an order. if (isset($form['actions']['delete']['#url']) && $form['actions']['delete']['#url'] instanceof Url) { @@ -316,8 +323,10 @@ class POSForm extends ContentEntityForm { public function submitForm(array &$form, FormStateInterface $form_state) { $triggering_element = $form_state->getTriggeringElement(); $step = $form_state->get('step'); + dpm($form_state->getValue('gift_order')); if ($step == 'order') { parent::submitForm($form, $form_state); + $this->entity->setData('gift_order', $form_state->getValue('gift_order')); $this->entity->save(); $form_state->set('step', 'payment'); $form_state->setRebuild(TRUE);