Ejemplo n.º 1
0
    def handle_mail(cls):
        """
        Метод обрабатывает почтовый ящик
        """
        from applications.provider_app.service import ProviderService
        res = []
        debug(u"Начало проверки почты")
        emails = ProviderService.get_all_emails()
        debug(u"Найдено %d почтовых ящиков отправителей", len(emails))
        try:
            count = cls.get_count_new_mails(emails)
        except Exception as err:
            error(u"Произошла ошибка при обработке почты. %s" % unicode(err))
            raise MailInvoiceException(err)
        debug(u"Найдено %d новых писем", count)
        if count > 0 and emails > 0:
            ids = []
            try:
                ids, mails = MailHepls.get_mails(emails)
                for _from in mails:
                    provider = ProviderService.get_provider_by_email(_from)
                    mailss = mails[_from]
                    for mail in mailss:
                        ml = Mail(
                            title=mail.title, date=mail.date_, text=mail.text,
                            from_=mail.from_, to=mail.to_, files=mail.files)
                        ml.provider = provider
                        db.session.add(ml)
                        db.session.commit()
                        try:
                            print "PUBLIC new mail"
                            r = redis.StrictRedis(
                                host='localhost', port=6379, db=0)
                            r.publish("new mail", "")
                        except Exception as exc:
                            error(u"Не удалось подключиться к редису")

                        res.append(ml)
            except GoodArgumentExc as exc:
                mark_as_unseen(ids)
                raise MailInvoiceException(exc)
            except Exception as err:
                try:
                    mark_as_unseen(ids)
                except Exception as err:
                    error(u"Произошла ошибка при пометке писем как "
                          u"непрочитанных. %s", unicode(err))
                    raise
                error(u"Произошла ошибка при обработке почты. %s", unicode(err))
                raise MailInvoiceException(err)
        debug(u"Конец проверки почты")
        return res
Ejemplo n.º 2
0
    def save_from_json(cls, date, items, provider_id=None, invoice=None):
        """
        Сохраняем накладную с позициями.

        Для редактирования надо передать инстанс накладной в переменную invoice.
        """
        from applications.price.service import DataToUpdatePrice, PriceService
        from applications.point_sale.service import PointSaleService
        try:
            if invoice:
                invmodel = invoice
            else:
                provider = ProviderService.get_by_id(provider_id)

                invmodel = InvoiceService.create_invoice(
                    number=InvoiceService.generate_number(date), date=date,
                    provider=provider, sum_without_NDS=None, sum_with_NDS=None,
                    sum_NDS=None, weight=None, responsible=None)

                db.session.add(invmodel)

            if invoice:
                pointsale = invoice.acceptance.pointsale
                for item in invmodel.items:
                    pointsale_item = PointSaleService.item_to_pointsale_good(
                        pointsale.id, item.good_id)
                    pointsale_item.count -= item.count
                    db.session.add(pointsale_item)
                invmodel.items.delete()
                db.session.add(invmodel)

            for item in items:
                good = GoodService.get_good(item['good_id'])
                name = good.commodity.name
                number_local = good.number_local
                number_global = good.number_global
                full_name = GoodService.generate_name(
                    name, number_local, number_global)

                invoice = invmodel
                count_order= None
                count_postorder = None
                count = item['count_invoice'] if 'count_invoice' in item \
                    else None
                price_without_NDS = item['price_pre'] if 'price_pre' in item \
                    else None
                price_with_NDS = item['price_post'] if 'price_post' in item \
                    else None
                sum_without_NDS = None
                sum_NDS = None
                rate_NDS = item['NDS'] if 'NDS' in item else None
                sum_with_NDS = None
                thematic = None
                count_whole_pack = None
                placer = None
                fact_count = None

                cls.handle_invoiceitem(
                    full_name, name, number_local, number_global, invoice,
                    count_order, count_postorder, count, price_without_NDS,
                    price_with_NDS, sum_without_NDS, sum_NDS, rate_NDS,
                    sum_with_NDS, thematic, count_whole_pack, placer, good,
                    fact_count)

                if 'price_retail' in item or 'price_gross' in item:
                    PriceService.create_or_update(good, DataToUpdatePrice(
                        id_commodity=good.commodity_id,
                        price_retail=item['price_retail'],
                        price_gross=item['price_gross'],
                        price_prev=item['price_prev'] if 'price_prev' in item
                        else None,
                        price_post=item['price_post'],
                        NDS=item['NDS'] if 'NDS' in item else None,
                        number_local=number_local, number_global=number_global,
                        invoice=invmodel))

        except Exception as exc:
            db.session.rollback()
            error(unicode(exc))
            raise InvoiceService.InvoiceServiceException(unicode(exc))
        else:
            db.session.commit()
            return invmodel