<commit_before>const { assign, get } = require('lodash')
const numeral = require('numeral')
const chrono = require('chrono-node')
const { format } = require('../../../../../client/utils/date')
const { DATE_LONG_FORMAT_3 } = require('../../../../../common/constants')

const logger = require('../../../../../config/logger')
const { EditController } = require('../../../controllers')
const { Order } = require('../../../models')

class EditPaymentReconciliationController extends EditController {
  async configure(req, res, next) {
    const orderStatus = get(res.locals, 'order.status')

    req.form.options = assign({}, req.form.options, {
      disableFormAction: false,
      buttonText: 'Reconcile payment',
      successMessage: `Payment for ${res.locals.order.reference} reconciled`,
      next: `/omis/reconciliation/${res.locals.order.id}/payment-receipt`,
    })

    if (orderStatus !== 'quote_accepted') {
      req.form.options.hidePrimaryFormAction = true
      req.form.options.returnText = 'Return'
    }

    super.configure(req, res, next)
  }

  process(req, res, next) {
    const paymentDateStr = req.body.received_on
    const parsedDate = chrono.en.GB.parseDate(paymentDateStr)

    if (parsedDate) {
      req.form.values.received_on = format(parsedDate, DATE_LONG_FORMAT_3)
    } else {
      req.form.values.received_on = paymentDateStr
    }

    next()
  }

  validate(req, res, next) {
    const submittedAmount = numeral(req.form.values.amount).value()
    const totalCost = res.locals.order.total_cost

    if (submittedAmount < totalCost) {
      const error = { key: 'amount', type: 'greaterthanamount' }
      const errors = {<commit_msg>Format date before validating<commit_after>const { assign, get } = require('lodash')
const numeral = require('numeral')
const chrono = require('chrono-node')
const { format } = require('../../../../../client/utils/date')
const { DATE_LONG_FORMAT_3 } = require('../../../../../common/constants')

const logger = require('../../../../../config/logger')
const { EditController } = require('../../../controllers')
const { Order } = require('../../../models')

class EditPaymentReconciliationController extends EditController {
  async configure(req, res, next) {
    const orderStatus = get(res.locals, 'order.status')

    req.form.options = assign({}, req.form.options, {
      disableFormAction: false,
      buttonText: 'Reconcile payment',
      successMessage: `Payment for ${res.locals.order.reference} reconciled`,
      next: `/omis/reconciliation/${res.locals.order.id}/payment-receipt`,
    })

    if (orderStatus !== 'quote_accepted') {
      req.form.options.hidePrimaryFormAction = true
      req.form.options.returnText = 'Return'
    }

    super.configure(req, res, next)
  }

  process(req, res, next) {
    const paymentDateStr = req.body.received_on
    const parsedDate = chrono.en.GB.parseDate(paymentDateStr)

    if (parsedDate) {
      const formattedDate = parsedDate.toISOString().slice(0, 10)
      req.form.values.received_on = format(formattedDate, DATE_LONG_FORMAT_3)
    } else {
      req.form.values.received_on = paymentDateStr
    }

    next()
  }

  validate(req, res, next) {
    const submittedAmount = numeral(req.form.values.amount).value()
    const totalCost = res.locals.order.total_cost

    if (submittedAmount < totalCost) {
      const error = { key: 'amount', type: 'greaterthanamount' }
      const errors = {
{'fraction_non_alphanumeric': 0.1615631379410907, 'fraction_numerical': 0.002041411490230388, 'mean_word_length': 3.208588957055215, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1692134', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1035, 'n_words': 207}