<commit_before>#
# Copyright (C) 2014 by frePPLe bvba
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from datetime import datetime, timedelta
from urllib.parse import urlencode

from django.conf import settings
from django.db import DEFAULT_DB_ALIAS, connections
from django.http import HttpResponse
from django.utils.encoding import force_text
from django.utils.html import escape
from django.utils.http import urlquote
from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy as _

from freppledb.common.middleware import _thread_locals
from freppledb.common.models import Parameter
from freppledb.common.dashboard import Dashboard, Widget
from freppledb.common.report import GridReport, getCurrency
from freppledb.input.models import PurchaseOrder, DistributionOrder, OperationPlanResource


class LateOrdersWidget(Widget):
  name = "late_orders"
  title = _("late orders")
  tooltip = _("Shows orders that will be delivered after their due date")
  permissions = (("view_problem_report", "Can view problem report"),)
  asynchronous = True
  url = '/problem/?entity=demand&name=late&sord=asc&sidx=startdate'
  exporturl = True
  limit = 20

  query = '''
    select
      demand.name, demand.item_id, demand.location_id, demand.customer_id,
      out_problem.startdate, out_problem.enddate, out_problem.weight
    from out_problem
    inner join demand
      on out_problem.owner = demand.name
    where out_problem.name = 'late' and out_problem.entity = 'demand'
    order by out_problem.startdate, out_problem.weight desc
    limit %s
    '''

  def args(self):
    return "?%s" % urlencode({'limit': self.limit})

  @classmethod
  def render(cls, request=None):
    limit = int(request.GET.get('limit', cls.limit))
    try:
      db = _thread_locals.request.database or DEFAULT_DB_ALIAS
    except:
      db = DEFAULT_DB_ALIAS
    cursor = connections[db].cursor()
    result = [
      '<div class="table-responsive"><table class="table table-condensed table-hover">',
      '<thead><tr><th class="alignleft">%s</th><th class="alignleft">%s</th>'
      '<th class="alignleft">%s</th><th class="alignleft">%s</th>'
      '<th class="aligncenter">%s</th><th class="aligncenter">%s</th>'
      '<th class="aligncenter">%s</th></tr></thead>' % (
        capfirst(force_text(_("name"))), capfirst(force_text(_("item"))),
        capfirst(force_text(_("location"))), capfirst(force_text(_("customer"))),
        capfirst(force_text(_("due"))), capfirst(force_text(_("planned date"))),
        capfirst(force_text(_("delay")))
        )
      ]
    alt = False
    cursor.execute(cls.query, (limit,))
    for rec in cursor.fetchall():
      result.append(
        '<tr%s><td class="underline"><a href="%s/demandpegging/%s/">%s</a></td>'
        '<td class="alignleft">%s</td><td class="alignleft">%s</td>'
        '<td class="alignleft">%s</td><td class="alignleft">%s</td>'
        '<td class="aligncenter">%s</td><td class="aligncenter">%s</td></tr>' % (
          alt and ' class="altRow"' or '', request.prefix, urlquote(rec[0]),
          escape(rec[0]), escape(rec[1]), escape(rec[2]), escape(rec[3]),
          rec[4].date(), rec[5].date(), int(rec[6])
        ))
      alt = not alt
    result.append('</table></div>')
    return HttpResponse('\n'.join(result))

Dashboard.register(LateOrdersWidget)


class ShortOrdersWidget(Widget):
  name = "short_orders"
  title = _("short orders")
  tooltip = _("Shows orders that are not planned completely")
  permissions = (("view_problem_report", "Can view problem report"),)
  asynchronous = True
  # Note the gte filter lets pass "short" and "unplanned", and filters out
  # "late" and "early".
  url = '/problem/?entity=demand&name__gte=short&sord=asc&sidx=startdate'
  exporturl = True
  limit = 20

  query = '''
    select
      out_problem.owner, demand.item_id, demand.customer_id, demand.location_id,
      out_problem.startdate, out_problem.weight
    from out_problem
    left outer join demand
      on out_problem.owner = demand.name
    where out_problem.name in ('short', 'unplanned') and out_problem.entity = 'demand'
      and demand.name is not null
    order by out_problem.startdate desc
    limit %s
    '''

  def args(self):
    return "?%s" % urlencode({'limit': self.limit})

  @classmethod
  def render(cls, request=None):
    limit = int(request.GET.get('limit', cls.limit))
    try:
      db = _thread_locals.request.database or DEFAULT_DB_ALIAS
    except:
      db = DEFAULT_DB_ALIAS
    cursor = connections[db].cursor()
    result = [
      '<div class="table-responsive"><table class="table table-condensed table-hover">',
      '<thead><tr><th class="alignleft">%s</th><th class="alignleft">%s</th><th class="alignleft">%s</th>'
      '<th class="alignleft">%s</th><th class="aligncenter">%s</th><th class="aligncenter">%s</th></tr></thead>' % (
        capfirst(force_text(_("name"))), capfirst(force_text(_("item"))), capfirst(force_text(_("location"))),
        capfirst(force_text(_("customer"))), capfirst(force_text(_("due"))), capfirst(force_text(_("short")))
        )
      ]
    alt = False
    cursor.execute(cls.query, (limit,))<commit_msg>swaping location and customer columns of short orders widget<commit_after>#
# Copyright (C) 2014 by <NAME>
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from datetime import datetime, timedelta
from urllib.parse import urlencode

from django.conf import settings
from django.db import DEFAULT_DB_ALIAS, connections
from django.http import HttpResponse
from django.utils.encoding import force_text
from django.utils.html import escape
from django.utils.http import urlquote
from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy as _

from freppledb.common.middleware import _thread_locals
from freppledb.common.models import Parameter
from freppledb.common.dashboard import Dashboard, Widget
from freppledb.common.report import GridReport, getCurrency
from freppledb.input.models import PurchaseOrder, DistributionOrder, OperationPlanResource


class LateOrdersWidget(Widget):
  name = "late_orders"
  title = _("late orders")
  tooltip = _("Shows orders that will be delivered after their due date")
  permissions = (("view_problem_report", "Can view problem report"),)
  asynchronous = True
  url = '/problem/?entity=demand&name=late&sord=asc&sidx=startdate'
  exporturl = True
  limit = 20

  query = '''
    select
      demand.name, demand.item_id, demand.location_id, demand.customer_id,
      out_problem.startdate, out_problem.enddate, out_problem.weight
    from out_problem
    inner join demand
      on out_problem.owner = demand.name
    where out_problem.name = 'late' and out_problem.entity = 'demand'
    order by out_problem.startdate, out_problem.weight desc
    limit %s
    '''

  def args(self):
    return "?%s" % urlencode({'limit': self.limit})

  @classmethod
  def render(cls, request=None):
    limit = int(request.GET.get('limit', cls.limit))
    try:
      db = _thread_locals.request.database or DEFAULT_DB_ALIAS
    except:
      db = DEFAULT_DB_ALIAS
    cursor = connections[db].cursor()
    result = [
      '<div class="table-responsive"><table class="table table-condensed table-hover">',
      '<thead><tr><th class="alignleft">%s</th><th class="alignleft">%s</th>'
      '<th class="alignleft">%s</th><th class="alignleft">%s</th>'
      '<th class="aligncenter">%s</th><th class="aligncenter">%s</th>'
      '<th class="aligncenter">%s</th></tr></thead>' % (
        capfirst(force_text(_("name"))), capfirst(force_text(_("item"))),
        capfirst(force_text(_("location"))), capfirst(force_text(_("customer"))),
        capfirst(force_text(_("due"))), capfirst(force_text(_("planned date"))),
        capfirst(force_text(_("delay")))
        )
      ]
    alt = False
    cursor.execute(cls.query, (limit,))
    for rec in cursor.fetchall():
      result.append(
        '<tr%s><td class="underline"><a href="%s/demandpegging/%s/">%s</a></td>'
        '<td class="alignleft">%s</td><td class="alignleft">%s</td>'
        '<td class="alignleft">%s</td><td class="alignleft">%s</td>'
        '<td class="aligncenter">%s</td><td class="aligncenter">%s</td></tr>' % (
          alt and ' class="altRow"' or '', request.prefix, urlquote(rec[0]),
          escape(rec[0]), escape(rec[1]), escape(rec[2]), escape(rec[3]),
          rec[4].date(), rec[5].date(), int(rec[6])
        ))
      alt = not alt
    result.append('</table></div>')
    return HttpResponse('\n'.join(result))

Dashboard.register(LateOrdersWidget)


class ShortOrdersWidget(Widget):
  name = "short_orders"
  title = _("short orders")
  tooltip = _("Shows orders that are not planned completely")
  permissions = (("view_problem_report", "Can view problem report"),)
  asynchronous = True
  # Note the gte filter lets pass "short" and "unplanned", and filters out
  # "late" and "early".
  url = '/problem/?entity=demand&name__gte=short&sord=asc&sidx=startdate'
  exporturl = True
  limit = 20

  query = '''
    select
      out_problem.owner, demand.item_id, demand.location_id, demand.customer_id,
      out_problem.startdate, out_problem.weight
    from out_problem
    left outer join demand
      on out_problem.owner = demand.name
    where out_problem.name in ('short', 'unplanned') and out_problem.entity = 'demand'
      and demand.name is not null
    order by out_problem.startdate desc
    limit %s
    '''

  def args(self):
    return "?%s" % urlencode({'limit': self.limit})

  @classmethod
  def render(cls, request=None):
    limit = int(request.GET.get('limit', cls.limit))
    try:
      db = _thread_locals.request.database or DEFAULT_DB_ALIAS
    except:
      db = DEFAULT_DB_ALIAS
    cursor = connections[db].cursor()
    result = [
      '<div class="table-responsive"><table class="table table-condensed table-hover">',
      '<thead><tr><th class="alignleft">%s</th><th class="alignleft">%s</th><th class="alignleft">%s</th>'
      '<th class="alignleft">%s</th><th class="aligncenter">%s</th><th class="aligncenter">%s</th></tr></thead>' % (
        capfirst(force_text(_("name"))), capfirst(force_text(_("item"))), capfirst(force_text(_("location"))),
        capfirst(force_text(_("customer"))), capfirst(force_text(_("due"))), capfirst(force_text(_("short")))
        )
      ]
    alt = False
    cursor.execute(cls.query, (limit,))
{'fraction_non_alphanumeric': 0.14375592519176075, 'fraction_numerical': 0.0029302766525898475, 'mean_word_length': 4.120917917034422, 'pattern_counts': {'":': 0, '<': 122, '<?xml version=': 0, '>': 122, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5078754', 'n_tokens_mistral': 3673, 'n_tokens_neox': 3589, 'n_words': 914}