#!/usr/bin/env python
# coding: utf-8
import  sys
import datetime
sys.path.append('../../')
sys.path.append('../')
from django.core.management import setup_environ
from globalhome import settings
setup_environ(settings)
from billing.models import BillserviceAccount
from data_centr.models import Zakazy, Data_centr_payment
from data_centr.views import send_mail_check, add_document_in_dict_for_send
from account.models import Profile
from findocs.models import Rules_of_drawing_up_documents, Check, Act, Invoice
from django.contrib.auth.models import User
from django.db.models import Q
from django.db import connections
import os
import log
from dateutil.relativedelta import relativedelta
from payment.models import Billservice_transaction
import psycopg2
from settings import DATABASES

conn = psycopg2.connect("dbname='%(NAME)s' user='%(USER)s' host='%(HOST)s' password='%(PASSWORD)s'" % DATABASES['billing'])
cur = conn.cursor()
cur.connection.set_isolation_level(1)

print 'edit CHECK_PAY'
service_queryset = Billservice_transaction.objects.filter(type_id = 'CHECK_PAY')
try:
    for service_obj in service_queryset:
        bill = service_obj.bill
        description_old = service_obj.description
        zakaz_id = ''
        for i in service_obj.description:
            if i.isdigit():
                zakaz_id += i
        zakaz_obj = Zakazy.objects.get(id = int(zakaz_id))
        service_type_id = zakaz_obj.service_type.id
        if service_type_id in (3,):
            spis_ext_number = []
            for j in zakaz_obj.ext_numbers.all():
                spis_ext_number.append(j.number)
            ext_numbers = ', '.join([str(x) for x in spis_ext_number]).rstrip(', ')
            section_name = '%s (%s)' % (zakaz_obj.service_type.service, ext_numbers)
        else:
            section_name = u'%s (Заказ № %s)' % (zakaz_obj.service_type.service, zakaz_obj.id)
            
        cur.execute("UPDATE billservice_transaction SET bill = %s, description = %s WHERE id = %s and created = %s and type_id = %s", \
                    (section_name, '%s, %s' % (bill, description_old), service_obj.id, service_obj.created, "CHECK_PAY"))
        print zakaz_id, section_name 
except Exception, e:
    print e
    cur.connection.rollback()
else:
    cur.connection.commit()
print 'end edit CHECK_PAY'

print 'edit COMEPAY_PAYMENT'
try:
        cur.execute("UPDATE billservice_transaction SET bill = %s, description = %s WHERE type_id = %s", \
                    (u"Пополнение счета через Comepay", "Comepay payment", "COMEPAY_PAYMENT"))
except Exception, e:
    print e
    cur.connection.rollback()
else:
    cur.connection.commit()
print 'end edit COMEPAY_PAYMENT'

print 'edit CHECK_PAY'
service_queryset = Billservice_transaction.objects.filter(type_id = 'ZAKAZ_PAY')
try:
    for service_obj in service_queryset:
        bill = service_obj.bill
        zakaz_id = ''
        for i in service_obj.bill:
            if i.isdigit():
                zakaz_id += i
        zakaz_obj = Zakazy.objects.get(id = int(zakaz_id))
        service_type_id = zakaz_obj.service_type.id
        if service_type_id in (3,):
            spis_ext_number = []
            for j in zakaz_obj.ext_numbers.all():
                spis_ext_number.append(j.number)
            ext_numbers = ', '.join([str(x) for x in spis_ext_number]).rstrip(', ')
            section_name = '%s (%s)' % (zakaz_obj.service_type.service, ext_numbers)
        else:
            section_name = u'%s (Заказ № %s)' % (zakaz_obj.service_type.service, zakaz_obj.id)
            
        cur.execute("UPDATE billservice_transaction SET bill = %s, description = %s WHERE id = %s and created = %s and type_id = %s", \
                    (section_name, bill, service_obj.id, service_obj.created, "ZAKAZ_PAY"))
        print zakaz_id, section_name 
except Exception, e:
    print e
else:
    cur.connection.commit()
print 'end edit CHECK_PAY'

print 'edit WEBMONEY_PAYMENT'
try:
        cur.execute("UPDATE billservice_transaction SET bill = %s, description = %s WHERE type_id = %s", \
                    (u"Пополнение счета через WebMoney", "WebMoney payment", "WEBMONEY_PAYMENT"))
except Exception, e:
    print e
    cur.connection.rollback()
else:
    cur.connection.commit()
print 'end edit WEBMONEY_PAYMENT'

print 'end script'

