Exemple #1
0
def dhen_board(request, context_agent_id=None):
    #import pdb; pdb.set_trace()
    agent = get_agent(request)
    pattern = ProcessPattern.objects.get(name="Herbs")
    selected_resource_type = None
    #filter_form = FilterForm(pattern=pattern, data=request.POST or None,)
    if context_agent_id:
        context_agent = EconomicAgent.objects.get(id=context_agent_id)
    else:
        context_agent = default_context_agent()
    seller = EconomicAgent.objects.get(id=4) #todo: even worse hack!!
    rec_extype = ExchangeType.objects.get(name="Purchase to Drying Site")
    e_date = datetime.date.today()
    init = {"start_date": e_date }
    available_extype = ExchangeType.objects.get(name="Make Available")
    available_form = AvailableForm(initial=init, exchange_type=available_extype, context_agent=context_agent, prefix="AVL")
    init = {"event_date": e_date, "paid": "later", }
    receive_form = ReceiveForm(initial=init, exchange_type=rec_extype, context_agent=context_agent, prefix="REC")
    et = EventType.objects.get(name="Resource Production")
    farm_stage = None  
    #harvester_stage = ExchangeType.objects.get(name="Farm to Harvester")  
    dryer_stage = ExchangeType.objects.get(name="Harvester to Drying Site")  
    seller_stage = ExchangeType.objects.get(name="Drying Site to Seller")
    rts = pattern.get_resource_types(event_type=et)
    for rt in rts:
        init = {"event_date": e_date,}
        rt.farm_commits = rt.commits_for_exchange_stage(stage=farm_stage)
        for com in rt.farm_commits:
            if com.start_date > e_date:
                com.future = True
            prefix = com.form_prefix()
            qty_help = " ".join([com.unit_of_quantity.abbrev, ", up to 2 decimal places"])
            com.transfer_form = ExchangeFlowForm(initial=init, qty_help=qty_help, assoc_type_identifier="DryingSite", context_agent=context_agent, prefix=prefix)
            com.zero_form = ZeroOutForm(prefix=prefix)
            com.lot_form = NewResourceForm(prefix=prefix)
            com.multiple_formset = create_exchange_formset(context_agent=context_agent, assoc_type_identifier="Harvester", prefix=prefix)            
        rt.dryer_resources = rt.onhand_for_exchange_stage(stage=dryer_stage)
        init = {"event_date": e_date, "paid": "later"}
        for res in rt.dryer_resources:
            prefix = res.form_prefix()
            qty_help = " ".join([res.unit_of_quantity().abbrev, ", up to 2 decimal places"])
            res.transfer_form = TransferFlowForm(initial=init, qty_help=qty_help, assoc_type_identifier="Seller", context_agent=context_agent, prefix=prefix)
        rt.seller_resources = rt.onhand_for_exchange_stage(stage=seller_stage)
        if rt.seller_resources:
            init_rt = {"event_date": e_date,} 
            rt.combine_form = CombineResourcesForm(prefix = rt.form_prefix(), initial=init_rt, resource_type=rt, stage=seller_stage)
    
    return render_to_response("board/dhen_board.html", {
        "agent": agent,
        "context_agent": context_agent,
        "seller": seller,
        "available_form": available_form,
        "receive_form": receive_form,
        #"filter_form": filter_form,
        "resource_types": rts,
        "available_extype": available_extype,
    }, context_instance=RequestContext(request))
Exemple #2
0
def dhen_board(request, context_agent_id=None):
    #import pdb; pdb.set_trace()
    agent = get_agent(request)
    pattern = ProcessPattern.objects.get(name="Herbs")
    selected_resource_type = None
    #filter_form = FilterForm(pattern=pattern, data=request.POST or None,)
    if context_agent_id:
        context_agent = EconomicAgent.objects.get(id=context_agent_id)
    else:
        context_agent = default_context_agent()
    seller = EconomicAgent.objects.get(id=4) #todo: even worse hack!!
    rec_extype = ExchangeType.objects.get(name="Purchase to Drying Site")
    e_date = datetime.date.today()
    init = {"start_date": e_date }
    available_extype = ExchangeType.objects.get(name="Make Available")
    available_form = AvailableForm(initial=init, exchange_type=available_extype, context_agent=context_agent, prefix="AVL")
    init = {"event_date": e_date, "paid": "later", }
    receive_form = ReceiveForm(initial=init, exchange_type=rec_extype, context_agent=context_agent, prefix="REC")
    et = EventType.objects.get(name="Resource Production")
    farm_stage = None  
    #harvester_stage = ExchangeType.objects.get(name="Farm to Harvester")  
    dryer_stage = ExchangeType.objects.get(name="Harvester to Drying Site")  
    seller_stage = ExchangeType.objects.get(name="Drying Site to Seller")
    rts = pattern.get_resource_types(event_type=et)
    for rt in rts:
        init = {"event_date": e_date,}
        rt.farm_commits = rt.commits_for_exchange_stage(stage=farm_stage)
        for com in rt.farm_commits:
            if com.start_date > e_date:
                com.future = True
            prefix = com.form_prefix()
            qty_help = " ".join([com.unit_of_quantity.abbrev, ", up to 2 decimal places"])
            com.transfer_form = ExchangeFlowForm(initial=init, qty_help=qty_help, assoc_type_identifier="DryingSite", context_agent=context_agent, prefix=prefix)
            com.zero_form = ZeroOutForm(prefix=prefix)
            com.lot_form = NewResourceForm(prefix=prefix)
            com.multiple_formset = create_exchange_formset(context_agent=context_agent, assoc_type_identifier="Harvester", prefix=prefix)            
        rt.dryer_resources = rt.onhand_for_exchange_stage(stage=dryer_stage)
        init = {"event_date": e_date, "paid": "later"}
        for res in rt.dryer_resources:
            prefix = res.form_prefix()
            qty_help = " ".join([res.unit_of_quantity().abbrev, ", up to 2 decimal places"])
            res.transfer_form = TransferFlowForm(initial=init, qty_help=qty_help, assoc_type_identifier="Seller", context_agent=context_agent, prefix=prefix)
        rt.seller_resources = rt.onhand_for_exchange_stage(stage=seller_stage)
        if rt.seller_resources:
            init_rt = {"event_date": e_date,} 
            rt.combine_form = CombineResourcesForm(prefix = rt.form_prefix(), initial=init_rt, resource_type=rt, stage=seller_stage)
    
    return render_to_response("board/dhen_board.html", {
        "agent": agent,
        "context_agent": context_agent,
        "seller": seller,
        "available_form": available_form,
        "receive_form": receive_form,
        #"filter_form": filter_form,
        "resource_types": rts,
        "available_extype": available_extype,
    }, context_instance=RequestContext(request))
Exemple #3
0
def log_equipment_use(request, scenario, equip_resource_id, context_agent_id, pattern_id, 
    sale_pattern_id, equip_svc_rt_id, equip_fee_rt_id, tech_rt_id, consumable_rt_id, 
    payment_rt_id, tech_rel_id, ve_id, va_id, price_id, part_rt_id, cite_rt_id
):
    #scenario: 1=commercial, 2=project, 3=fablab, 4=techshop, 5=other
    equipment = get_object_or_404(EconomicResource, id=equip_resource_id)
    equipment_svc_rt = get_object_or_404(EconomicResourceType, id=equip_svc_rt_id)
    equipment_fee_rt = get_object_or_404(EconomicResourceType, id=equip_fee_rt_id)
    technician_rt = EconomicResourceType.objects.get(id=tech_rt_id)
    payment_rt = EconomicResourceType.objects.get(id=payment_rt_id)
    context_agent = get_object_or_404(EconomicAgent, id=context_agent_id)
    pattern = ProcessPattern.objects.get(id=pattern_id)
    sale_pattern = ProcessPattern.objects.get(id=sale_pattern_id)
    consumable_rt = EconomicResourceType.objects.get(id=consumable_rt_id)
    logged_on_agent = get_agent(request)
    ve = ValueEquation.objects.get(id=ve_id)
    mtnce_virtual_account = EconomicResource.objects.get(id=va_id)
    tech_rel_type = AgentResourceRoleType.objects.get(id=tech_rel_id)
    init = {"event_date": datetime.date.today(), "from_agent": logged_on_agent}
    equip_form = EquipmentUseForm(equip_resource=equipment, context_agent=context_agent, tech_type=tech_rel_type, initial=init, data=request.POST or None)
    formset = consumable_formset(consumable_rt=consumable_rt)
    process_form = ProcessForm(data=request.POST or None)
    
    if request.method == "POST":
        if equip_form.is_valid():
            data = equip_form.cleaned_data
            input_date = data["event_date"]
            who = data["from_agent"]
            quantity = data["quantity"]
            technician = data["technician"]
            technician_quantity = data["technician_hours"]
            et_ship = EventType.objects.get(name="Shipment")
            et_use = EventType.objects.get(name="Resource use")
            et_consume = EventType.objects.get(name="Resource Consumption")
            et_work = EventType.objects.get(name="Time Contribution")
            et_create = EventType.objects.get(name="Resource Production")
            et_fee = EventType.objects.get(name="Fee")
            et_transfer = EventType.objects.get(name="Transfer")
            total_price = 0
            next_process = None
            if scenario == '2':
                if process_form.is_valid():
                    pdata = process_form.cleaned_data
                    next_process = pdata["process"]

            process = Process(
                name="Paid service: Use of " + equipment.identifier,
                end_date=input_date,
                start_date=input_date,
                process_pattern=pattern,
                created_by=request.user,
                context_agent=context_agent,
                started=input_date,
                finished=True,
            )
            process.save()
            formset = consumable_formset(data=request.POST, consumable_rt=consumable_rt)
            for form in formset.forms:
                if form.is_valid():
                    data_cons = form.cleaned_data
                    if data_cons:
                        qty = data_cons["quantity"]
                        if qty:
                            if qty > 0:
                                res_id = data_cons["resource_id"]
                                consumable = EconomicResource.objects.get(id=int(res_id))
                                consume_event = EconomicEvent(
                                    event_type = et_consume,
                                    event_date = input_date,
                                    resource = consumable,
                                    resource_type = consumable.resource_type,
                                    process = process,
                                    from_agent = context_agent,
                                    to_agent = context_agent,
                                    context_agent = context_agent,
                                    quantity = qty,
                                    unit_of_quantity = consumable_rt.unit,
                                    unit_of_value = consumable.resource_type.unit_of_price,
                                    created_by = request.user,
                                )
                                consume_event.save()
                                consumable.quantity = consumable.quantity - qty
                                consumable.save()
            if technician and technician_quantity > 0:
                tech_event = EconomicEvent(
                    event_type = et_work,
                    event_date = input_date,
                    resource_type = technician_rt,
                    process = process,
                    from_agent = technician,
                    to_agent = context_agent,
                    context_agent = process.context_agent,
                    quantity = technician_quantity,
                    unit_of_quantity = technician_rt.unit,
                    unit_of_value = technician_rt.unit_of_price,
                    created_by = request.user,
                    is_contribution = True,
                )
                tech_event.save()
                total_price += tech_event.value
            if scenario == '3' or scenario == '4': #fablab, techshop
                use_event = EconomicEvent(
                    event_type = et_use,
                    event_date = input_date,
                    resource_type = equipment.resource_type,
                    resource = equipment,
                    process = process,
                    from_agent = context_agent,
                    to_agent = who,
                    context_agent = context_agent,
                    quantity = quantity,
                    unit_of_quantity = equipment.resource_type.unit_of_use,
                    price = quantity * ResourceTypeSpecialPrice.objects.get(id=price_id).price_per_unit,
                    unit_of_price = equipment.resource_type.unit_of_price,
                    created_by = request.user,
                )
                use_event.save()
                total_price += use_event.price
                
            #ephemeral output resource
            printer_service = EconomicResource(
                resource_type=equipment_svc_rt,
                identifier="Temporary service resource 3D printing " + str(input_date) + " for " + who.nick,
                quantity=1,
                value_per_unit = total_price,
                created_by=request.user,
            )
            printer_service.save()
            output_event = EconomicEvent(
                event_type = et_create,
                event_date = input_date,
                resource_type = equipment_svc_rt,
                resource = printer_service,
                process = process,
                from_agent = context_agent,
                to_agent = context_agent,
                context_agent = process.context_agent,
                quantity = 1,
                unit_of_quantity = equipment_svc_rt.unit,
                unit_of_value = equipment_svc_rt.unit_of_price,
                created_by = request.user,
            )
            output_event.save()
            total_value = output_event.resource.compute_value_per_unit(value_equation=ve)
            #if scenario == '3' or scenario == '4': #fablab, techshop
            #    total_value += use_event.price
            output_event.value = total_value
            output_event.save()

            if scenario == '2' and next_process:
                cust = next_process.context_agent
            else:
                cust = who
            sale = Exchange(
                name="Use of " + equipment.identifier,
                process_pattern=sale_pattern,
                use_case=UseCase.objects.get(identifier="sale"),
                start_date=input_date,
                customer=cust,
                context_agent=context_agent,
                created_by=request.user,
            )
            sale.save()
            #todo: hardcoded fee event for now
            mtnce_fee_event = EconomicEvent(
                event_type = et_fee,
                event_date = input_date,
                resource_type = equipment_fee_rt,
                resource = mtnce_virtual_account,
                exchange = sale,
                from_agent = context_agent,
                to_agent = cust,
                context_agent = context_agent,
                quantity = quantity * equipment_fee_rt.price_per_unit,
                unit_of_quantity = equipment_fee_rt.unit_of_price,
                value = quantity * equipment_fee_rt.price_per_unit,
                unit_of_value = equipment_fee_rt.unit_of_price,
                created_by = request.user,
            )
            mtnce_fee_event.save()
            mtnce_virtual_account.quantity = mtnce_virtual_account.quantity + mtnce_fee_event.quantity
            mtnce_virtual_account.save()
            ship_event = EconomicEvent(
                event_type = et_transfer,
                event_date = input_date,
                resource_type = equipment_svc_rt,
                resource = printer_service,
                exchange = sale,
                from_agent = context_agent,
                to_agent = cust,
                context_agent = context_agent,
                quantity = 1,
                unit_of_quantity = equipment_svc_rt.unit,
                value = total_value,
                unit_of_value = equipment_svc_rt.unit_of_price,
                created_by = request.user,
            )
            ship_event.save()
            #if scenario == '3' or scenario == '4':
            #    use_ship_event = EconomicEvent(
            #        event_type = et_ship,
            #        event_date = input_date,
            #        resource_type = use_svc_rt,
            #        exchange = sale,
            #        from_agent = context_agent,
            #        to_agent = who,
            #        context_agent = context_agent,
            #        quantity = quantity,
            #        unit_of_quantity = use_svc_rt.unit,
            #        value = quantity * use_svc_rt.price_per_unit,
            #        unit_of_value = use_svc_rt.unit_of_price,
            #        created_by = request.user,
            #    )
            #    use_ship_event.save()
            printer_service.quantity = 0
            printer_service.save()
            
            if scenario == '1': 
                next_process = Process(
                    name="Make commercial 3D printed part",
                    end_date=input_date,
                    start_date=input_date,
                    process_pattern=pattern,
                    created_by=request.user,
                    context_agent=context_agent,
                    started=input_date,
                    finished=True,
                )
                next_process.save()
                part_rt = EconomicResourceType.objects.get(id=part_rt_id)
                printed_part = EconomicResource(
                    resource_type=part_rt,
                    identifier="Commercial 3D printed part " + datetime.datetime.now().strftime("%Y%m%d-%H%M%S"),
                    quantity=1,
                    created_by=request.user,
                )
                printed_part.save()
                output_part_event = EconomicEvent(
                    event_type = et_create,
                    event_date = input_date,
                    resource_type = part_rt,
                    resource = printed_part,
                    process = next_process,
                    from_agent = context_agent,
                    to_agent = context_agent,
                    context_agent = context_agent,
                    quantity = 1,
                    unit_of_quantity = part_rt.unit,
                    unit_of_value = part_rt.unit_of_price,
                    created_by = request.user,
                )
                output_part_event.save()
            if scenario == '2' or scenario == '1': 
                if next_process:
                    use_event = EconomicEvent(
                        event_type = et_use,
                        event_date = input_date,
                        resource_type = equipment.resource_type,
                        resource = equipment,
                        process = next_process,
                        from_agent = context_agent,
                        to_agent = next_process.context_agent,
                        context_agent = next_process.context_agent,
                        quantity = quantity,
                        unit_of_quantity = equipment.resource_type.unit_of_use,
                        value = 0,
                        unit_of_value = payment_rt.unit,
                        created_by = request.user,
                    )
                    use_event.save()
                    svc_input_event = EconomicEvent(
                        event_type = et_consume,
                        event_date = input_date,
                        resource_type = equipment_svc_rt,
                        resource = printer_service,
                        process = next_process,
                        from_agent = next_process.context_agent,
                        to_agent = next_process.context_agent,
                        context_agent = next_process.context_agent,
                        quantity = 1,
                        unit_of_quantity = equipment_svc_rt.unit,
                        unit_of_value = equipment_svc_rt.unit_of_price,
                        created_by = request.user,
                    )
                    svc_input_event.save()             

            if next_process:
                return HttpResponseRedirect('/%s/%s/%s/%s/%s/%s/%s/%s/%s/%s/%s/%s/'
                    % ('equipment/pay-equipment-use', scenario, sale.id, process.id, payment_rt_id, equip_resource_id, mtnce_fee_event.id, ve_id, quantity, who.id, next_process.id, int(cite_rt_id)))
            else:
                return HttpResponseRedirect('/%s/%s/%s/%s/%s/%s/%s/%s/%s/%s/'
                    % ('equipment/pay-equipment-use', scenario, sale.id, process.id, payment_rt_id, equip_resource_id, mtnce_fee_event.id, ve_id, quantity, who.id))
    
    return render_to_response("equipment/log_equipment_use.html", {
        "equip_form": equip_form,
        "process_form": process_form,
        "formset": formset,
        "equipment": equipment,
        "consumable_rt": consumable_rt,
        "scenario": scenario,
    }, context_instance=RequestContext(request))
Exemple #4
0
def dhen_board(request, context_agent_id=None):
    #import pdb; pdb.set_trace()
    agent = get_agent(request)
    pattern = ProcessPattern.objects.get(name="Transfer")
    selected_resource_type = None
    filter_form = FilterForm(pattern=pattern, data=request.POST or None,)
    #if request.method == "POST":
    #    if filter_form.is_valid():
    #        data = filter_form.cleaned_data
    #        context_agent = data["context_agent"]
    #        selected_resource_type = data["resource_type"]
    #elif context_agent_id:
    #    context_agent = EconomicAgent.objects.get(id=context_agent_id)
    #else:
    #    context_agent = filter_form.fields["context_agent"].queryset[0] #todo: hack, this happens to be DHEN
    if context_agent_id:
        context_agent = EconomicAgent.objects.get(id=context_agent_id)
    else:
        context_agent = EconomicAgent.objects.get(id=3) #todo:  BIG hack alert!!!!
    rec_pattern = ProcessPattern.objects.get(name="Purchase Contribution")
    e_date = datetime.date.today()
    init = {"commitment_date": e_date }
    available_form = AvailableForm(initial=init, pattern=pattern, context_agent=context_agent, prefix="AVL")
    init = {"event_date": e_date, "paid": "later", }
    receive_form = ReceiveForm(initial=init, pattern=rec_pattern, context_agent=context_agent, prefix="REC")
    et = EventType.objects.get(name="Transfer")
    farm_stage = AgentAssociationType.objects.get(identifier="HarvestSite")
    dryer_stage = AgentAssociationType.objects.get(identifier="DryingSite")
    seller_stage = AgentAssociationType.objects.get(identifier="Seller")
    rts = pattern.get_resource_types(event_type=et)
    for rt in rts:
        init = {"event_date": e_date,}
        rt.farm_commits = rt.commits_for_exchange_stage(stage=farm_stage)
        for com in rt.farm_commits:
            if com.commitment_date > e_date:
                com.future = True
            prefix = com.form_prefix()
            qty_help = " ".join([com.unit_of_quantity.abbrev, ", up to 2 decimal places"])
            com.transfer_form = ExchangeFlowForm(initial=init, qty_help=qty_help, assoc_type_identifier="DryingSite", context_agent=context_agent, prefix=prefix)
            com.zero_form = ZeroOutForm(prefix=prefix)
            com.lot_form = NewResourceForm(prefix=prefix)
            com.multiple_formset = create_exchange_formset(context_agent=context_agent, assoc_type_identifier="Harvester", prefix=prefix)
        rt.dryer_resources = rt.onhand_for_exchange_stage(stage=dryer_stage)
        init = {"event_date": e_date, "paid": "later"}
        for res in rt.dryer_resources:
            prefix = res.form_prefix()
            qty_help = " ".join([res.unit_of_quantity().abbrev, ", up to 2 decimal places"])
            res.transfer_form = TransferFlowForm(initial=init, qty_help=qty_help, assoc_type_identifier="Seller", context_agent=context_agent, prefix=prefix)
        rt.seller_resources = rt.onhand_for_exchange_stage(stage=seller_stage)
        if rt.seller_resources:
            init_rt = {"event_date": e_date,} 
            rt.combine_form = CombineResourcesForm(prefix = rt.form_prefix(), initial=init_rt, resource_type=rt, stage=seller_stage)
    
    return render_to_response("board/dhen_board.html", {
        "agent": agent,
        "context_agent": context_agent,
        "available_form": available_form,
        "receive_form": receive_form,
        "filter_form": filter_form,
        "resource_types": rts,
    }, context_instance=RequestContext(request))