savoirfairelinux / sous-chef

Sous-Chef is a web application to help organizations to plan and deliver meals, and to manage clients files.
GNU Affero General Public License v3.0
66 stars 45 forks source link

[CM Step 1] Gets all orders for a given day #224

Closed manumilou closed 8 years ago

manumilou commented 8 years ago

Expected Behaviour

The orders List page should be used actually , as it already lists all orders. Only a date filter is missing. It must list:

An order list without date filter.

manumilou commented 8 years ago

Orders List

img_20160703_164549797

manumilou commented 8 years ago

img_20160703_164615983

manumilou commented 8 years ago

img_20160703_164647612

cormier commented 8 years ago

I have started working on this in https://github.com/cormier/santropol-feast/tree/get-all-orders

So far I have implemented a manager method in Order and improved the tests. I should be able to submit a PR for this work soon. If anyone wants to work on this issue, please consider starting from my branch! :smile:

lamontfr commented 8 years ago

@cormier Please see https://github.com/savoirfairelinux/santropol-feast/wiki/Plan-for-July-2016 for some details on this issue.

cormier commented 8 years ago

@lamontfr thanks! this will be really helpful. I will continue working on this and will keep you updated with any progress I make.

lamontfr commented 8 years ago

@maximest-pierre Here is a preliminary version of code to create orders and order items

# /member.models.py

LOW_INCOME = RATE_TYPE[1]
SOLIDARY = RATE_TYPE[2]
#

# /meal.models.py
COMPONENT_GROUP_CHOICES = (
    ('main dish', _('main dish')),
    ('dessert', _('Dessert')),
    ('diabetic dessert', _('Diabetic dessert')),
    ('fruit salad', _('Fruit salad')),
    ('green salad', _('Green salad')),
    ('pudding', _('Pudding')),
    ('compote', _('Compote')),
)

MAIN_DISH = COMPONENT_GROUP_CHOICES[0]
#

# /delivery/views.py
from member.models import LOW_INCOME, SOLIDARY
from meal.models import COMPONENT_GROUP_CHOICES, MAIN_DISH

def client_get_defaults(client, component_group, day):
    """Get the meal defaults quantity and size for a day.

    Parameters:
      client : client object
      component_group : as in meal.models.COMPONENT_GROUP_CHOICES
      day : day of week where 0 is monday, 6 is sunday

    Assumption:
      client.meal_defaults is a dictionary like
        {0:{'main dish':{'quantity':1, 'size':'R'},
            'pudding':  {'quantity':1, 'size':'R'},
            'compote':  {'quantity':0, 'size':''},
            ...},
         1:{'main dish':{'quantity':0, 'size':''},
            'pudding':  {'quantity':1, 'size':'R'},
            'compote':  {'quantity':0, 'size':''},
           ...},
         ...}
    """

    day_defaults = client.meal_defaults[day]
    quantity = day_defaults[component_group]['quantity']
    size = day_defaults[component_group]['size']
    return (quantity, size)

def create_orders_on_defaults(session, date, clients):
    # session see kcr_view in delivery app

    qcomp = session.query(component.id.label('compid')).\
        select_from(menu).\
        join(menu_component, menu_component.menu_id == menu.id).\
        join(component, component.id == menu_component.component_id).\
        filter(menu.date == date)
    # TODO use try block in case missing data for the date
    components = []
    # TODO use a list comprehension
    for row in qcomp.all():
        components = components.append(Component.objects.get(pk=row.compid))
    day = date.weekday() # Monday is 0, Sunday is 6
    for client in clients:
        if client.rate_type == LOW_INCOME:
            main_price = 7.00  #TODO use decimal constant, check rates
            side_price = 0.75
        elif client.rate_type == SOLIDARY:
            main_price = 6.00
            side_price = 0.50
        else:
            main_price = 8.00
            side_price = 1.00
        # find quantity of free side dishes based on number of main dishes
        free_side_dish_qty = client.get_defaults(client, MAIN_DISH, day)[0]
        if free_side_dish_qty > 0: #client wants meal on this day
            order = Order(client=client,
                          creation_date=datetime.date.today(),
                          delivery_date=date,
                          status='open') #TODO use constant
            order.save()
            for component in components:
                default_qty, default_size = \
                    client.get_defaults(client, component.component_group, day)
                if default_qty > 0:
                    total_quantity = default_qty
                    free_quantity = 0
                    if component.compgroup == MAIN_DISH:
                        unit_price = main_price
                    else:
                        unit_price = side_price
                        while free_side_dish_qty > 0 and default_qty > 0:
                            free_side_dish_qty = free_side_dish_qty - 1
                            default_qty = default_qty - 1
                            free_quantity = free_quantity + 1
                    oi = Order_item(
                        order=order,
                        component=component,
                        price = (total_quantity - free_quantity) * unit_price,
                        billable_flag = True,
                        size = default_size,
                        order_item_type = 'B component' # TODO use constant
                        total_quantity = total_quantity,
                        free_quantity = free_quantity)
                    oi.save()
maximest-pierre commented 8 years ago

I will check it when i get to the office but it looks good to me except the part of the dictionary but i can tweak the field and how the information save.