Пример #1
0
def current_period():
    """ Period of current date """
    from datetime import date
    return MonthPeriod.find_create_by_date(date.today())
Пример #2
0
def nut_consumption(message, args, sub_cmd, cmd):
    """ Incomming:
            nut stock type_health_center code_health_center month year
             #input_type initial received used received #input_type initial
            received used received
            example: nut stock URENAM dasco 1 2012 #nie 11 22 18 2
                     #csb 22 22 22 2 #uni 2 32 22 2 #hui 21 25 45 1
                     #suc 23 12 30 0 #mil 32 15 32 2
        Outgoing:
            [SUCCES] Le rapport de stock de health_center a ete bien
            enregistre.
            or error message """

    try:
        general, reports = args.split('#', 1)
        hc_type, hc_code, month, year = general.split()
    except:
        return resp_error(message, u"stock")

    try:
        hc = HealthCenter.objects.get(code=hc_code)
    except:
        msg = u"[ERREUR] %(hc)s n'est pas un code de Centre valide." % {'hc': hc_code}
        message.respond(msg)
        send_sms(FOL_NUMBER, msg)
        return True

    try:
        period = MonthPeriod.find_create_from(year=int(year),
                                              month=int(month))
    except:
        msg = u"[ERREUR] %s-%s n'est pas une periode valide." % (month, year)
        message.respond(msg)
        send_sms(FOL_NUMBER, msg)
        return True

    now_period = MonthPeriod.find_create_by_date(date.today())
    if period != now_period.previous():
        msg = u"[ERREUR] Impossible d'enregistrer le rapport de conso pour %s.\
                 Envoyez pour %s" % (period.full_name(), now_period.previous().full_name())
        message.respond(msg)
        send_sms(FOL_NUMBER, msg)
        return True

    try:
        all_reports = reports.split('#')
    except:
        return resp_error(message, u"stock")

    success = []
    errors = []
    for areport in all_reports:
        try:
            icode, initial, received, used, lost = areport.split()
            input_type = Input.objects.get(code=icode.lower())
        except:
            errors.append(icode)

        if ConsumptionReport.objects.filter(period=period,
                                            health_center=hc,
                                            input_type=input_type).count():
            cr = ConsumptionReport.objects.get(period=period,
                                               health_center=hc,
                                               input_type=input_type)
        else:
            cr = ConsumptionReport(period=period, health_center=hc,
                                   input_type=input_type)
        cr.initial = int(initial)
        cr.received = int(received)
        cr.used = int(used)
        cr.lost = int(lost)
        try:
            cr.save()
            success.append(cr)
        except:
            errors.append(icode)

    if len(errors):
        msg = u"[ERREUR] %d rapport de conso en erreur. Verifiez toutes les" \
              u" donnees et renvoyez -- %s" % (len(errors), ', '.join(errors))
        message.respond(msg)
        send_sms(FOL_NUMBER, msg)
        return True

    message.respond(u"[SUCCES] %d rapports de conso enregistres pour %s."
                    % (len(success), period.full_name()))
    return True