コード例 #1
0
ファイル: prepaid_send_invoice.py プロジェクト: tuian/mq
def generate_html(doc, invoice_header_and_items):
    """given the doc, return an html message for email
    """

    #get the template
    t = Template(file="templates/prepaid_invoice_message.tmpl")

    #assign variables to the template file
    task_client_name = send_task('EmailSender.GetAddressTo',
                                 [doc['client_id']])
    t.client_name = task_client_name.get()
    t.status = doc['status']
    t.client_id = doc['client_id']

    if doc['status'] == 'paid':  #retrieve clients running balance
        #couchdb settings
        s = couchdb.Server(settings.COUCH_DSN)
        db_client_docs = s['client_docs']

        r = db_client_docs.view('client/running_balance', key=doc['client_id'])

        if len(r.rows) == 0:
            running_balance = Decimal('0.00')
        else:
            running_balance = Decimal('%0.2f' % r.rows[0].value)

        #displays a running balance 0 when client has a negative running balance
        if running_balance <= Decimal('0.00'):
            running_balance = Decimal('0.00')

        t.running_balance = locale.format('%0.2f', running_balance, True)

    if doc.has_key('overpayment_from'):
        t.overpayment_from = doc['overpayment_from']
    else:
        t.overpayment_from = False

    if doc.has_key('pay_before_date'):
        x = doc['pay_before_date']
        pay_before_date = date(x[0], x[1], x[2])
        t.pay_before_date = pay_before_date.strftime('%B %d, %Y')
    else:
        t.pay_before_date = None

    #get currency_sign
    sql = text("""SELECT sign from currency_lookup
        WHERE code = :currency
        """)
    conn = engine.connect()
    currency_sign = conn.execute(sql,
                                 currency=doc['currency']).fetchone()['sign']
    t.currency_sign = currency_sign

    #get client_data
    task_client_data = send_task(
        "ClientsWithPrepaidAccounts.get_client_details", [
            doc['client_id'],
        ])
    client_data = task_client_data.get()
    t.company_name = client_data['company_name']
    t.company_address = client_data['company_address']
    t.days_before_suspension = client_data['days_before_suspension']

    #check if invoice items has dates
    items = doc['items']
    flag_has_date = False

    for item in items:
        a = item.copy()
        if a.has_key('start_date') == True or a.has_key('end_date') == True:
            flag_has_date = True

    t.flag_has_date = flag_has_date

    t.order_id = doc['order_id']
    t.sub_total = locale.format('%0.2f', Decimal(doc['sub_total']), True)
    t.currency = doc['currency']
    t.gst_amount = locale.format('%0.2f', Decimal(doc['gst_amount']), True)
    t.total_amount = locale.format('%0.2f', Decimal(doc['total_amount']), True)
    t.DEBUG = settings.DEBUG
    t.doc_id = doc['_id']
    t.invoice_header_and_items = invoice_header_and_items

    conn.close()
    return '%s' % t
コード例 #2
0
ファイル: EmailSender.py プロジェクト: tuian/mq
    def run(self, doc_id):
        """given the doc_id, send email
        """
        logging.info('Sending topup order %s ' % doc_id)
        couch_server = couchdb.Server(settings.COUCH_DSN)
        db_client_docs = couch_server['client_docs']
        doc = db_client_docs.get(doc_id)

        #get the template
        t = Template(file="templates/client_topup_invoice_created.tmpl")

        #assign variables to the template file
        t.client_fname = doc['client_fname']
        t.client_lname = doc['client_lname']

        #get currency
        s = text("""SELECT sign from currency_lookup
            WHERE code = :code
            """)
        conn = engine.connect()
        r = conn.execute(s, code=doc['currency']).fetchone()
        t.currency_sign = r.sign

        #get client details
        s = text(
            """SELECT company_name, company_address, acct_dept_email1, acct_dept_email2
            FROM leads
            WHERE id = :client_id
            """)
        client_data = conn.execute(s, client_id=doc['client_id']).fetchone()
        t.client_data = client_data

        items_converted_date = []
        items = doc['items']

        for item in items:
            a = item.copy()
            if a.has_key('start_date'):
                b = a['start_date']
                a['start_date'] = date(b[0], b[1], b[2])
            else:
                a['start_date'] = None

            if a.has_key('end_date'):
                c = a['end_date']
                a['end_date'] = date(c[0], c[1], c[2])
            else:
                a['end_date'] = None

            amount = Decimal(a['amount'])
            a['amount'] = locale.format('%0.2f', amount, True)

            items_converted_date.append(a)

        t.invoice_items = items_converted_date
        t.order_id = doc['order_id']
        t.sub_total = locale.format('%0.2f', Decimal(doc['sub_total']), True)
        t.currency = doc['currency']
        t.gst_amount = locale.format('%0.2f', Decimal(doc['gst_amount']), True)
        t.total_amount = locale.format('%0.2f', Decimal(doc['total_amount']),
                                       True)
        t.DEBUG = settings.DEBUG
        t.doc_id = doc['_id']
        t.status = doc['status']

        html_message = '%s' % t

        msg = MIMEMultipart()
        part1 = MIMEText('%s' % html_message, 'html')
        part1.add_header('Content-Disposition', 'inline')
        msg.attach(part1)

        #check acct_dept_email1 and acct_dept_email2 fields and other sorts using task GetEmails

        if settings.DEBUG:
            result = send_task("EmailSender.GetEmails", [doc['client_id']])
        else:
            celery = Celery()
            celery.config_from_object(sc_celeryconfig)
            result = celery.send_task("EmailSender.GetEmails",
                                      [doc['client_id']])

        email_recipients = result.get()
        msg['To'] = email_recipients['to']
        if email_recipients['cc'] != '':
            msg['Cc'] = email_recipients['cc']

        msg['From'] = '*****@*****.**'
        msg['Reply-To'] = '*****@*****.**'

        subject = 'REMOTE STAFF PREPAID ORDER # %s' % doc['order_id']
        recipients = email_recipients['recipients']
        recipients.append('*****@*****.**')
        recipients.append('*****@*****.**')

        if settings.DEBUG:
            subject = 'TEST %s' % subject
            recipients = [settings.EMAIL_ALERT]

        msg['Subject'] = subject

        s = smtplib.SMTP(host=settings.MAILGUN_CONFIG['server'],
                         port=settings.MAILGUN_CONFIG['port'])
        s.starttls()
        s.login(settings.MAILGUN_CONFIG['username'],
                settings.MAILGUN_CONFIG['password'])

        s.sendmail('*****@*****.**', recipients, msg.as_string())
        s.quit()

        #store the email for archiving purposes
        history = []
        history.append(
            dict(timestamp=get_ph_time().strftime('%F %H:%M:%S'),
                 changes='Order created',
                 by='TopupOrder celery task'))
        doc_order_archive = dict(
            type='order archive',
            email_recipients=email_recipients,
            history=history,
            client_id=int(doc['client_id']),
            subject='REMOTE STAFF PREPAID ORDER # %s' % doc['order_id'],
        )
        db_client_docs.save(doc_order_archive)
        #attach message
        db_client_docs.put_attachment(doc_order_archive, html_message,
                                      'message.html')

        #add history that the document was sent
        doc = db_client_docs.get(doc_id)
        if doc.has_key('history') == False:
            history = []
        else:
            history = doc['history']

        history.append(
            dict(timestamp=get_ph_time().strftime('%F %H:%M:%S'),
                 changes='Email sent to %s.' %
                 string.join(email_recipients['recipients'], ','),
                 by='TopupOrder celery task'))

        doc['history'] = history
        doc['mongo_synced'] = False
        db_client_docs.save(doc)
        conn.close()
コード例 #3
0
    def generate_html(self, doc_id):
        """given the doc_id, return an html message for email
        """
        #couchdb settings
        s = couchdb.Server(settings.CLIENT_DOCS_DSN)
        db = s['client_docs']

        doc = db.get(doc_id)
        if doc == None:
            self.__send_email_alert__(
                'FAILED to create HTML',
                'Please check doc_id:%s sent for generate_html function.' %
                (doc_id))
            return

        #get the template
        t = Template(file="prepaid_invoice_on_deplete_account.tmpl")

        #assign variables to the template file
        t.client_fname = doc['client_fname']
        t.client_lname = doc['client_lname']
        x = doc['added_on']
        latest_payment_date = date(x[0], x[1], x[2])
        latest_payment_date = latest_payment_date + timedelta(
            days=settings.DAYS_BEFORE_INVOICE_ISSUE)
        t.latest_payment_date = latest_payment_date

        #get currency_sign
        db = MySQLdb.connect(**settings.DB_ARGS)
        c = db.cursor()
        c.execute(
            """SELECT sign from currency_lookup
            WHERE code = %s
            """, doc['currency'])
        currency_sign = c.fetchone()[0]
        t.currency_sign = currency_sign

        #get client_data
        c.execute(
            """SELECT company_name, company_address
            FROM leads
            WHERE id = %s
            """, doc['client_id'])
        client_data = c.fetchone()
        t.company_name = client_data[0]
        t.company_address = client_data[1]

        items_converted_date = []
        items = doc['items']
        for item in items:
            a = item.copy()
            b = a['start_date']
            a['start_date'] = date(b[0], b[1], b[2])
            c = a['end_date']
            a['end_date'] = date(c[0], c[1], c[2])
            amount = Decimal(a['amount'])
            a['amount'] = locale.format('%0.2f', amount, True)

            items_converted_date.append(a)

        t.invoice_items = items_converted_date
        t.order_id = doc['order_id']
        t.sub_total = locale.format('%0.2f', Decimal(doc['sub_total']), True)
        t.currency = doc['currency']
        t.gst_amount = locale.format('%0.2f', Decimal(doc['gst_amount']), True)
        t.total_amount = locale.format('%0.2f', Decimal(doc['total_amount']),
                                       True)
        t.DEBUG = settings.DEBUG
        t.doc_id = doc['_id']

        return '%s' % t