def handle(self, *args, **options):
        cronjob = cron_create(name='shop_check_latlng', type='shop')
        desc = {}
        try:
            self.stdout.write(self.style.SUCCESS('Start command'))
            count = 0
            shops = Shop.objects.filter(Q(activated=1)).exclude(
                Q(latitude__isnull=True))
            for s in shops:
                if s.wards:
                    s.geo_check = checkInside(s.wards.wards_code, s.latitude,
                                              s.longitude)
                else:
                    s.geo_check = -1
                s.save()
                count = count + 1
                if count % 100 == 0:
                    print('Checked: ' + str(count))

            cron_update(cronjob, description=desc)
            self.stdout.write(self.style.SUCCESS('Successfully command'))
        except Exception as e:
            logging.ERROR("Exception shop_check_latlng: {}".format(e))
            desc.update(error_log=str(e))
            cron_update(cronjob, description=desc)
Exemplo n.º 2
0
    def handle(self, *args, **options):
        cronjob = cron_create(name='shop_update_latlng_daily', type='shop')
        desc = {}
        count_success = 0
        count_failed = 0
        list_failed = []
        try:
            self.stdout.write(
                self.style.SUCCESS('Start shop geodata daily processing...'))
            exception_desctiption = []
            limit = settings.LIMIT_QUERY_PER_GOOGLE_ACCOUNT_TOKEN
            list_token = settings.LIST_GEODATA_GOOGLE_ACCOUNT_TOKEN

            for token in list_token:
                shops = Shop.objects.filter(
                    Q(activated=1) & Q(latitude__isnull=True)).exclude(
                        Q(address='') | Q(address='0')
                        | Q(geo_generate=-1))[:limit]

                if shops is None or len(shops) == 0:
                    raise Exception('Do not have any shop that need to query')
                geodecode_url = 'https://maps.googleapis.com/maps/api/geocode/json'
                for s in shops:
                    params = {'key': token, 'address': s.address}
                    try:
                        r = requests.get(geodecode_url, params=params)
                        results = r.json()['results']
                        location = results[0]['geometry']['location']
                        s.latitude = location['lat']
                        s.longitude = location['lng']
                        if s.wards:
                            s.geo_check = checkInside(s.wards.wards_code,
                                                      s.latitude, s.longitude)
                        else:
                            s.geo_check = -1
                        s.geo_generate = 1
                        s.save()
                        count_success = count_success + 1
                        desc.update(count_success=count_success)
                        print('Update success shop: ' + str(s.code))
                    except Exception as inst:
                        s.geo_generate = -1
                        s.save()
                        count_failed = count_failed + 1
                        list_failed.append(s.code)
                        desc.update(count_failed=count_failed)
                        desc.update(list_failed=list_failed)
                        print('Update ' + str(s.code) + ' failed: ' +
                              str(inst))
            cron_update(cronjob, description=desc)
            self.stdout.write(self.style.SUCCESS('Successfully command'))

        except Exception as e:
            desc.update(error_log=str(e))
            cron_update(cronjob, description=desc)
Exemplo n.º 3
0
    def handle(self, *args, **options):
        desc = {}
        cronjob = cron_create(name='export_terminal_daily', type='terminal')

        try:
            self.export_terminal_id()

            cron_update(cronjob, status=1)

        except Exception as e:
            desc.update(error_log=str(e))
            cron_update(cronjob, status=2, description=desc)
Exemplo n.º 4
0
    def handle(self, *args, **options):
        cronjob = cron_create(name='qr_type_merchant_sync', type='merchant')

        try:

            self.stdout.write(
                self.style.WARNING(
                    'Start qr_type_merchant sync daily processing...'))

            count_qr_type_merchant = self.get_count_qr_type_merchant()
            if count_qr_type_merchant == 0:
                raise Exception('Exception: qr_type_merchant count == 0')

            # Truncate table qr_staff before synchronize all data from MMS
            cursor = connection.cursor()
            cursor.execute('TRUNCATE TABLE "{0}" RESTART IDENTITY'.format(
                QrTypeMerchant._meta.db_table))

            print(
                'Truncate table qr_type_merchant before synchronize all data from MMS'
            )

            query = self.get_query()
            with connections['mms'].cursor() as cursor:
                cursor.execute(query)
                columns = [col[0] for col in cursor.description]
                data_cursor = [
                    dict(zip(columns, row)) for row in cursor.fetchall()
                ]
            objs = (QrTypeMerchant(
                id=int(item['ID']),
                type_code=item['TYPE_CODE'],
                brand_name=item['BRAND_NAME'],
                full_name=item['FULL_NAME'],
                description=item['DESCRIPTION'],
                created_date=item['CREATED_DATE'],
                updated_date=item['UPDATED_DATE'],
                status=item['STATUS'],
            ) for item in data_cursor)

            batch = list(islice(objs, count_qr_type_merchant))

            QrTypeMerchant.objects.bulk_create(batch, count_qr_type_merchant)

            self.stdout.write(
                self.style.SUCCESS(
                    'Finish qr_type_merchant synchronize processing!'))

            cron_update(cronjob, status=1)

        except Exception as e:
            cron_update(cronjob, status=2, description=str(e))
Exemplo n.º 5
0
    def handle(self, *args, **options):
        desc = {}
        cronjob = cron_create(name='export_qr_merchant_new_daily', type='merchant')

        try:
            self.export_merchant_new()

            self.export_shop_new()

            cron_update(cronjob, status=1)

        except Exception as e:
            desc.update(error_log=str(e))
            cron_update(cronjob, status=2, description=desc)
    def handle(self, *args, **options):
        desc = {}
        cronjob = cron_create(name='kpi_sync_daily', type='kpi')
        self.stdout.write(self.style.WARNING('Start kpi sync daily processing...'))
        try:
            spreadObj = GoogleSheetWithVerifyCode()
            values = self.read(spreadObj=spreadObj, sheet_name='KPI', range='A:F')
            list_email = []
            data = []
            for value in values:
                if self.represents_int(value[1]) and self.represents_int(value[2]) and self.represents_int(value[3]) \
                        and self.represents_int(value[4]) and self.represents_int(value[5]):
                    if int(value[4]) == datetime.now().month and int(value[5]) == datetime.now().year:
                        list_email.append(value[0])
                        data.append({
                            'email': value[0],
                            'kpi_target': int(value[1]),
                            'kpi_point_lcc': int(value[2]),
                            'kpi_point_other': int(value[3]),
                            'month': int(value[4]),
                            'year': int(value[5])
                        })

            desc.update(info_log=str(f'Delete & create kpi row with email in: {list_email}'))
            Kpi.objects.filter(
                email__in=list_email,
                month=datetime.now().month,
                year=datetime.now().year,
            ).delete()

            kpi_objs = [
                Kpi(
                    email=kpi_val['email'],
                    kpi_target=kpi_val['kpi_target'],
                    kpi_point_lcc=kpi_val['kpi_point_lcc'],
                    kpi_point_other=kpi_val['kpi_point_other'],
                    month=kpi_val['month'],
                    year=kpi_val['year']
                )
                for kpi_val in data
            ]
            Kpi.objects.bulk_create(kpi_objs)
            self.stdout.write(self.style.SUCCESS('Finish kpi synchronize processing!'))

            cron_update(cronjob, status=1, description=desc)
        except Exception as e:
            desc.update(error_log=str(e))
            cron_update(cronjob, status=2, description=desc)
Exemplo n.º 7
0
    def handle(self, *args, **options):
        cronjob = cron_create(name='qr_status_sync', type='system')

        try:
            self.stdout.write(
                self.style.WARNING(
                    'Start qr_status synchronize processing...'))
            count_qr_status = self.get_count()
            if count_qr_status > 0:
                self.truncate_table()
                query = self.get_query()
                with connections['mms'].cursor() as cursor:
                    cursor.execute(query)
                    columns = [col[0] for col in cursor.description]
                    data_cursor = [
                        dict(zip(columns, row)) for row in cursor.fetchall()
                    ]
                objs = (QrStatus(
                    id=int(item['ID']),
                    code=item['CODE'],
                    description=item['DESCRIPTION'],
                    type=item['TYPE'],
                    created_date=item['CREATED_DATE'],
                    note=item['NOTE'],
                    icon=item['ICON'],
                ) for item in data_cursor)
                batch = list(islice(objs, 200))
                QrStatus.objects.bulk_create(batch)
                self.stdout.write(
                    self.style.SUCCESS(
                        'Finish qr_status synchronize processing!'))
            else:
                cron_update(
                    cronjob,
                    description=
                    'Count qr_status = 0.Force stop qr_status synchronize')
                self.stdout.write(
                    self.style.ERROR(
                        'Count qr_status = 0.Force stop qr_status synchronize!'
                    ))

            cron_update(cronjob, status=1)

        except Exception as e:
            cron_update(cronjob, status=2, description=str(e))
Exemplo n.º 8
0
    def handle(self, *args, **options):
        cronjob = cron_create(name='qr_merchant_sync_daily', type='merchant')

        try:

            self.stdout.write(
                self.style.WARNING(
                    'Start qr_merchant sync daily processing...'))

            limit, offset = 1000, 0

            count_qr_merchant = self.get_count_qr_merchant()
            if count_qr_merchant == 0:
                raise Exception('Exception: qr_merchant count == 0')

            # Truncate table qr_merchant before synchronize all data from MMS
            cursor = connection.cursor()
            cursor.execute('TRUNCATE TABLE "{0}" RESTART IDENTITY'.format(
                QrMerchant._meta.db_table))

            print(
                'Truncate table qr_merchant before synchronize all data from MMS'
            )

            while offset < count_qr_merchant:
                query = self.get_query(limit=limit, offset=offset)
                with connections['mms'].cursor() as cursor:
                    cursor.execute(query)
                    columns = [col[0] for col in cursor.description]
                    data_cursor = [
                        dict(zip(columns, row)) for row in cursor.fetchall()
                    ]
                objs = (QrMerchant(
                    id=int(item['ID']),
                    merchant_code=item['MERCHANT_CODE'],
                    service_code=item['SERVICE_CODE'],
                    merchant_brand=item['MERCHANT_BRAND'],
                    merchant_name=item['MERCHANT_NAME'],
                    merchant_type=item['MERCHANT_TYPE'],
                    address=item['ADDRESS'],
                    description=item['DESCRIPTION'],
                    status=item['STATUS'],
                    website=item['WEBSITE'],
                    master_merchant_code=item['MASTER_MERCHANT_CODE'],
                    province_code=item['PROVINCE_CODE'],
                    district_code=item['DISTRICT_CODE'],
                    department=item['DEPARTMENT_ID'],
                    staff=item['STAFF_ID'],
                    genqr_checksum=item['GENQR_CHECKSUM'],
                    genqr_accesskey=item['GENQR_ACCESSKEY'],
                    switch_code=item['SWITCH_CODE'],
                    created_date=item['CREATED_DATE'],
                    modify_date=item['MODIFY_DATE'],
                    process_user=item['PROCESS_USER'],
                    denied_approve_desc=item['DENIED_APPROVE_DESC'],
                    create_user=item['CREATE_USER'],
                    org_status=item['ORG_STATUS'],
                    email_vnpay=item['EMAIL_VNPAY'],
                    pass_email_vnpay=item['PASS_EMAIL_VNPAY'],
                    process_addition=item['PROCESS_ADDITION'],
                    denied_approve_code=item['DENIED_APPROVE_CODE'],
                    business_address=item['BUSINESS_ADDRESS'],
                    app_user=item['APP_USER'],
                    pin_code=item['PIN_CODE'],
                    provider_code=item['PROVIDER_CODE'],
                    wards_code=item['WARDS_CODE'],
                ) for item in data_cursor)

                batch = list(islice(objs, limit))

                QrMerchant.objects.bulk_create(batch, limit)

                print('QrMerchant synchronize processing. Row: ', offset)

                offset = offset + limit

            self.stdout.write(
                self.style.SUCCESS(
                    'Finish qr_merchant synchronize processing!'))

            cron_update(cronjob, status=1)

        except Exception as e:
            logging.error('Job qr_merchant_sync_daily exception: %s', e)
            cron_update(cronjob, status=2, description=str(e))
Exemplo n.º 9
0
    def handle(self, *args, **options):
        cronjob = cron_create(name='qr_terminal_sync_daily', type='terminal')

        try:

            self.stdout.write(
                self.style.WARNING(
                    'Start qr_terminal sync daily processing...'))

            limit, offset = 1500, 0

            count_qr_terminal = self.get_count_qr_terminal()
            if count_qr_terminal == 0:
                raise Exception('Exception: qr_terminal count == 0')

            # Truncate table qr_terminal before synchronize all data from MMS
            cursor = connection.cursor()
            cursor.execute('TRUNCATE TABLE "{0}" RESTART IDENTITY'.format(
                QrTerminal._meta.db_table))

            print(
                'Truncate table qr_terminal before synchronize all data from MMS'
            )

            while offset < count_qr_terminal:
                query = self.get_query(limit=limit, offset=offset)
                with connections['mms'].cursor() as cursor:
                    cursor.execute(query)
                    columns = [col[0] for col in cursor.description]
                    data_cursor = [
                        dict(zip(columns, row)) for row in cursor.fetchall()
                    ]

                objs = (QrTerminal(
                    id=int(item['ID']),
                    terminal_id=item['TERMINAL_ID'],
                    merchant_id=item['MERCHANT_ID'],
                    terminal_name=item['TERMINAL_NAME'],
                    terminal_address=item['TERMINAL_ADDRESS'],
                    tax_code=item['TAX_CODE'],
                    website=item['WEBSITE'],
                    website_business=item['WEBSITE_BUSINESS'],
                    facebook=item['FACEBOOK'],
                    business_product=item['BUSINESS_PRODUCT'],
                    product_description=item['PRODUCT_DESCRIPTION'],
                    register_qr=item['REGISTER_QR'],
                    register_vnpayment=item['REGISTER_VNPAYMENT'],
                    account_id=item['ACCOUNT_ID'],
                    account_vnmart_id=item['ACCOUNT_VNMART_ID'],
                    status=item['STATUS'],
                    created_date=item['CREATED_DATE'],
                    modify_date=item['MODIFY_DATE'],
                    the_first=item['THE_FIRST'],
                    process_user=item['PROCESS_USER'],
                    denied_approve_desc=item['DENIED_APPROVE_DESC'],
                    process_addition=item['PROCESS_ADDITION'],
                    user_lock=item['USER_LOCK'],
                    denied_approve_code=item['DENIED_APPROVE_CODE'],
                    business_address=item['BUSINESS_ADDRESS'],
                    register_sms=item['REGISTER_SMS'],
                    register_ott=item['REGISTER_OTT'],
                    terminal_app_user=item['TERMINAL_APP_USER'],
                    terminal_document=item['TERMINAL_DOCUMENT'],
                    service_code=item['SERVICE_CODE'],
                    create_user=item['CREATE_USER'],
                    visa_pan=item['VISA_PAN'],
                    master_pan=item['MASTER_PAN'],
                    unionpay_pan=item['UNIONPAY_PAN'],
                    file_name=item['FILE_NAME'],
                    province_code=item['PROVINCE_CODE'],
                    district_code=item['DISTRICT_CODE'],
                    wards_code=item['WARDS_CODE'],
                ) for item in data_cursor)

                batch = list(islice(objs, limit))

                QrTerminal.objects.bulk_create(batch, limit)

                print('QrTerminal sync daily processing. Row: ', offset)

                offset = offset + limit

            self.stdout.write(
                self.style.SUCCESS(
                    'Finish qr_terminal sync daily processing!'))

            cron_update(cronjob, status=1)

        except Exception as e:
            logging.error('Job qr_terminal_sync_daily exception: %s', e)
            cron_update(cronjob, status=2, description=str(e))
Exemplo n.º 10
0
    def handle(self, *args, **options):
        cronjob = cron_create(name='terminal_synchronize_change',
                              type='terminal')
        try:
            self.stdout.write(
                self.style.SUCCESS(
                    'Start terminal synchronize change processing...'))
            created, updated = 0, 0
            with connection.cursor() as cursor:
                cursor.execute(self.get_sql_query())
                columns = [col[0] for col in cursor.description]
                data_cursor = [
                    dict(zip(columns, row)) for row in cursor.fetchall()
                ]

            for data in data_cursor:
                is_update_address = False
                if data['status'] == 0:
                    self.create_new_terminal(terminal_id=data['terminal_id'],
                                             merchant_id=data['merchant_id'])
                    created += 1
                    if created % 100 == 0:
                        print('Terminal created: {}'.format(created))
                else:
                    try:
                        qr_terminal = QrTerminal.objects.filter(
                            terminal_id=data['terminal_id'],
                            merchant_id=data['merchant_id']).first()
                        terminal = Terminal.objects.filter(
                            terminal_id=data['terminal_id'],
                            merchant_id=data['merchant_id']).first()
                        self.create_terminal_log(terminal=terminal,
                                                 qr_terminal=qr_terminal,
                                                 status=data['status'])
                        if terminal.business_address != qr_terminal.business_address or \
                                terminal.province_code != qr_terminal.province_code or \
                                terminal.district_code != qr_terminal.district_code or \
                                terminal.wards_code != qr_terminal.wards_code:
                            is_update_address = True

                        terminal.status = qr_terminal.status
                        terminal.terminal_address = qr_terminal.terminal_address
                        terminal.terminal_name = qr_terminal.terminal_name
                        terminal.register_qr = qr_terminal.register_qr
                        terminal.register_vnpayment = qr_terminal.register_vnpayment
                        terminal.province_code = qr_terminal.province_code
                        terminal.district_code = qr_terminal.district_code
                        terminal.wards_code = qr_terminal.wards_code
                        terminal.business_address = qr_terminal.business_address
                        terminal.created_date = qr_terminal.created_date
                        terminal.modify_date = qr_terminal.modify_date
                        terminal.save()

                        if is_update_address:
                            if Terminal.objects.filter(
                                    shop=terminal.shop).count() == 1:
                                shop = terminal.shop
                                shop.street = None
                                shop.address = qr_terminal.business_address
                                shop.province = terminal.get_province() or None
                                shop.district = terminal.get_district() or None
                                shop.wards = terminal.get_wards() or None
                                shop.save()
                            else:
                                request = HttpRequest()
                                request.method = 'OTHER'
                                request.user = User.objects.get(pk=1)
                                request.terminal = terminal
                                request.address = terminal.business_address
                                request.street = ''
                                if shop_store(request=request):
                                    self.style.WARNING(
                                        'Ter update address => Created new shop - ter_id: {}'
                                        .format(terminal.terminal_id))

                        updated += 1
                        if updated % 100 == 0:
                            print(f'Terminal updated: {updated}')

                    except Terminal.DoesNotExist:
                        raise CommandError(
                            'Terminal with terminal_id: "%s" | merchant_id: "%s" does not exist'
                            % (data['terminal_id'], data['merchant_id']))

            self.stdout.write(
                self.style.SUCCESS(
                    'Successfully command. Created: {}. Updated: {}'.format(
                        created, updated)))

            cron_update(cronjob, status=1)

        except Exception as e:
            logging.error('Job terminal_synchronize_change exception: %s', e)
            cron_update(cronjob, status=2, description=str(e))
    def handle(self, *args, **options):
        cronjob = cron_create(name='qr_staff_sync_daily_minio', type='staff')

        try:

            self.stdout.write(
                self.style.WARNING('Start qr_staff sync daily processing...'))

            minio_client = MinioHandler().get_instance()
            objects = minio_client.client.list_objects(
                minio_client.bucket_name, prefix="raw/qr_staff/")
            object_name = None
            for obj in objects:
                if obj._object_name[-3:] == 'csv':
                    object_name = obj._object_name
                    break
            if not object_name:
                raise Exception('Minio qr_staff not exists')

            column_names = [
                'STAFF_ID', 'NICK_NAME', 'FULL_NAME', 'EMAIL', 'MOBILE',
                'DEPARTMENT_CODE', 'STATUS', 'CREATED_DATE', 'MODIFY_DATE',
                'DEPARTMENT_ID', 'STAFF_CODE'
            ]
            converters = {column: str.strip for column in column_names}
            file = minio_client.client.get_object(minio_client.bucket_name,
                                                  object_name)
            dfs = pd.read_csv(file, converters=converters)

            if len(dfs.columns.tolist()) != len(column_names):
                raise Exception('Format file wrong')

            if len(dfs.values.tolist()) == 0:
                raise Exception('Exception - data qr_staff: COUNT == 0')

            # Truncate table qr_staff before insert all data from Minio
            cursor = connection.cursor()
            cursor.execute('TRUNCATE TABLE "{0}" RESTART IDENTITY'.format(
                QrStaff._meta.db_table))

            print(
                "Step: Truncate table qr_staff before synchronize all data from MMS - MINIO"
            )

            objs = (QrStaff(
                staff_id=int(item[0]),
                nick_name=item[1],
                full_name=item[2],
                email=item[3],
                mobile=item[4],
                department_code=item[5],
                status=item[6],
                created_date=item[7],
                modify_date=item[8],
                department_id=item[9],
                staff_code=item[10],
            ) for item in dfs.values.tolist())

            QrStaff.objects.bulk_create(objs)

            self.stdout.write(
                self.style.SUCCESS('Finish qr_staff sync daily processing!'))

            cron_update(cronjob, status=1)

        except Exception as e:
            logging.error('Job qr_staff_sync_daily exception: %s', e)
            cron_update(cronjob, status=2, description=str(e))
Exemplo n.º 12
0
    def handle(self, *args, **options):
        cronjob = cron_create(name='qr_staff_sync_daily', type='staff')

        try:

            self.stdout.write(
                self.style.WARNING('Start qr_staff sync daily processing...'))

            limit, offset = 100, 0

            count_qr_staff = self.get_count_qr_staff()
            if count_qr_staff == 0:
                raise Exception('Exception: qr_staff count == 0')

            # Truncate table qr_staff before synchronize all data from MMS
            cursor = connection.cursor()
            cursor.execute('TRUNCATE TABLE "{0}" RESTART IDENTITY'.format(
                QrStaff._meta.db_table))

            print(
                'Truncate table qr_staff before synchronize all data from MMS')

            while offset < count_qr_staff:
                query = self.get_query(limit=limit, offset=offset)
                with connections['mms'].cursor() as cursor:
                    cursor.execute(query)
                    columns = [col[0] for col in cursor.description]
                    data_cursor = [
                        dict(zip(columns, row)) for row in cursor.fetchall()
                    ]

                objs = (QrStaff(
                    staff_id=int(item['STAFF_ID']),
                    nick_name=item['NICK_NAME'],
                    full_name=item['FULL_NAME'],
                    email=item['EMAIL'],
                    mobile=item['MOBILE'],
                    department_code=item['DEPARTMENT_CODE'],
                    status=item['STATUS'],
                    created_date=item['CREATED_DATE'],
                    modify_date=item['MODIFY_DATE'],
                    department_id=item['DEPARTMENT_ID'],
                    staff_code=item['STAFF_CODE'],
                ) for item in data_cursor)

                batch = list(islice(objs, limit))

                QrStaff.objects.bulk_create(batch, limit)

                print('QrStaff synchronize processing. Row: ', offset)

                offset = offset + limit

            self.stdout.write(
                self.style.SUCCESS('Finish qr_staff sync daily processing!'))

            cron_update(cronjob, status=1)

        except Exception as e:
            logging.error('Job qr_staff_sync_daily exception: %s', e)
            cron_update(cronjob, status=2, description=str(e))
    def handle(self, *args, **options):
        cronjob = cron_create(name='staff_synchronize_change', type='staff')
        desc = {}

        try:
            self.stdout.write(self.style.SUCCESS('Start staff synchronize change processing...'))
            created, updated, deleted = 0, 0, 0
            with connection.cursor() as cursor:
                cursor.execute(self.get_sql_query())
                columns = [col[0] for col in cursor.description]
                data_cursor = [
                    dict(zip(columns, row))
                    for row in cursor.fetchall()
                ]

            count_qr_staff = QrStaff.objects.count()
            if count_qr_staff == 0:
                raise Exception('ERROR: Table qr_staff is NULL')

            for data in data_cursor:
                if data['status'] == 0:
                    self.create_staff_log(staff=None, qr_staff=data, status=data['status'])
                    staff = Staff(
                        id=data['qr_staff_id'],
                        staff_code=data['qr_staff_code'],
                        nick_name=data['qr_nick_name'],
                        full_name=data['qr_full_name'],
                        email=data['qr_email'],
                        mobile=data['qr_mobile'],
                        department_code=data['qr_department_code'],
                        status=data['qr_status'],
                        department_id=data['qr_department_id'],
                        created_date=data['qr_created_date'],
                        modify_date=data['qr_modify_date'],
                    )
                    staff.save()
                    created += 1

                elif data['status'] == 1:
                    desc[data['s_staff_id']] = 'Log: staff_id {} is deleted in qr_staff'.format(data['s_staff_id'])
                    deleted += 1

                elif data['status'] == 2:
                    try:
                        staff = Staff.objects.filter(pk=data['s_staff_id']).first()
                        self.create_staff_log(staff=staff, qr_staff=data, status=data['status'])

                        staff.staff_code = data['qr_staff_code']
                        staff.nick_name = data['qr_nick_name']
                        staff.full_name = data['qr_full_name']
                        staff.email = data['qr_email']
                        staff.mobile = data['qr_mobile']
                        staff.status = data['qr_status']
                        staff.department_id = data['qr_department_id']
                        staff.department_code = data['qr_department_code']
                        staff.created_date = data['qr_created_date']
                        staff.modify_date = data['qr_modify_date']
                        staff.save()

                        updated += 1

                    except Staff.DoesNotExist:
                        raise CommandError('Staff with staff_id: "%s" does not exist' % (data['s_staff_id']))
                else:
                    pass

            self.stdout.write(
                self.style.SUCCESS(
                    'Successfully command. Created: {}. Updated: {}. Data for Delete: {}'.format(created, updated, deleted)))

            cron_update(cronjob, status=1, description=str(desc))

        except Exception as e:
            logging.error('Job staff_synchronize_change exception: %s', e)
            cron_update(cronjob, status=2, description=str(e))
    def handle(self, *args, **options):
        cronjob = cron_create(name='qr_terminal_contact_sync_daily',
                              type='terminal')

        try:

            self.stdout.write(
                self.style.WARNING(
                    'Start qr_terminal_contact sync daily processing...'))

            limit, offset = 1500, 0

            count_qr_terminal_contact = self.get_count_qr_terminal_contact()

            # Truncate table qr_terminal_contact before synchronize all data from MMS
            cursor = connection.cursor()
            cursor.execute('TRUNCATE TABLE "{0}" RESTART IDENTITY'.format(
                QrTerminalContact._meta.db_table))

            print(
                'Truncate table qr_terminal_contact before synchronize all data from MMS'
            )

            while offset < count_qr_terminal_contact:
                query = self.get_query(limit=limit, offset=offset)
                with connections['mms'].cursor() as cursor:
                    cursor.execute(query)
                    columns = [col[0] for col in cursor.description]
                    data_cursor = [
                        dict(zip(columns, row)) for row in cursor.fetchall()
                    ]

                objs = (QrTerminalContact(
                    id=int(item['ID']),
                    merchant_code=item['MERCHANT_CODE'],
                    terminal_id=item['TERMINAL_ID'],
                    fullname=item['FULLNAME'],
                    phone=item['PHONE'],
                    phone1=item['PHONE1'],
                    phone2=item['PHONE2'],
                    email=item['EMAIL'],
                    email1=item['EMAIL1'],
                    email2=item['EMAIL2'],
                    created_date=item['CREATED_DATE'],
                    status=item['STATUS'],
                    create_terminal_app=item['CREATE_TERMINAL_APP'],
                    to_create_user=item['TO_CREATE_USER'],
                    to_terminal=item['TO_TERMINAL'],
                    receive_phone=item['RECEIVE_PHONE'],
                    receive_mail=item['RECEIVE_MAIL'],
                ) for item in data_cursor)

                batch = list(islice(objs, limit))

                QrTerminalContact.objects.bulk_create(batch, limit)

                print('QrTerminalContact synchronize processing. Row: ',
                      offset)

                offset = offset + limit

            self.stdout.write(
                self.style.SUCCESS(
                    'Finish qr_terminal_contact sync daily processing!'))

            cron_update(cronjob, status=1)

        except Exception as e:
            logging.error('Job qr_terminal_contact_sync_daily exception: %s',
                          e)
            cron_update(cronjob, status=2, description=str(e))
    def handle(self, *args, **options):
        cronjob = cron_create(name='qr_merchant_info_sync', type='merchant')

        try:

            self.stdout.write(
                self.style.WARNING(
                    'Start qr_merchant_info sync daily processing...'))

            limit, offset = 1000, 0

            count_qr_merchant_info = self.get_count_qr_merchant_info()
            if count_qr_merchant_info == 0:
                raise Exception('Exception: qr_merchant_info count == 0')

            # Truncate table qr_staff before synchronize all data from MMS
            cursor = connection.cursor()
            cursor.execute('TRUNCATE TABLE "{0}" RESTART IDENTITY'.format(
                QrMerchantInfo._meta.db_table))

            print(
                'Truncate table qr_merchant_info before synchronize all data from MMS'
            )

            while offset < count_qr_merchant_info:
                query = self.get_query(limit=limit, offset=offset)
                with connections['mms'].cursor() as cursor:
                    cursor.execute(query)
                    columns = [col[0] for col in cursor.description]
                    data_cursor = [
                        dict(zip(columns, row)) for row in cursor.fetchall()
                    ]
                objs = (QrMerchantInfo(
                    id=int(item['ID']),
                    merchant_code=item['MERCHANT_CODE'],
                    rm_auth=item['RM_AUTH'],
                    register_sms=item['REGISTER_SMS'],
                    register_ott=item['REGISTER_OTT'],
                    to_create_user=item['TO_CREATE_USER'],
                    to_merchant=item['TO_MERCHANT'],
                    receive_phone=item['RECEIVE_PHONE'],
                    receive_email=item['RECEIVE_EMAIL'],
                    contact_name=item['CONTACT_NAME'],
                    contact_phone=item['CONTACT_PHONE'],
                    contact_email=item['CONTACT_EMAIL'],
                    contact_phone1=item['CONTACT_PHONE1'],
                    contact_phone2=item['CONTACT_PHONE2'],
                    contact_email1=item['CONTACT_EMAIL1'],
                    contact_email2=item['CONTACT_EMAIL2'],
                ) for item in data_cursor)

                batch = list(islice(objs, limit))

                QrMerchantInfo.objects.bulk_create(batch, limit)

                print('QrMerchantInfo synchronize processing. Row: ', offset)

                offset = offset + limit

            self.stdout.write(
                self.style.SUCCESS(
                    'Finish qr_merchant_info synchronize processing!'))

            cron_update(cronjob, status=1)

        except Exception as e:
            cron_update(cronjob, status=2, description=str(e))
Exemplo n.º 16
0
    def handle(self, *args, **options):
        cronjob = cron_create(name='merchant_synchronize_change',
                              type='merchant')
        try:
            self.stdout.write(
                self.style.SUCCESS(
                    'Start merchant synchronize change processing...'))
            created, updated = 0, 0
            with connection.cursor() as cursor:
                cursor.execute(self.get_sql_query())
                columns = [col[0] for col in cursor.description]
                data_cursor = [
                    dict(zip(columns, row)) for row in cursor.fetchall()
                ]

            for data in data_cursor:
                if data['status'] == 0:
                    self.create_new_merchant(merchant_id=data['merchant_id'])
                    created += 1
                    if created % 100 == 0:
                        print('Merchant created: {}'.format(created))
                else:
                    try:
                        qr_merchant = QrMerchant.objects.filter(
                            id=data['merchant_id']).first()
                        merchant = Merchant.objects.get(pk=data['merchant_id'])

                        self.create_merchant_log(merchant=merchant,
                                                 qr_merchant=qr_merchant,
                                                 status=data['status'])

                        merchant.merchant_code = qr_merchant.merchant_code
                        merchant.merchant_name = qr_merchant.merchant_name
                        merchant.merchant_brand = qr_merchant.merchant_brand
                        merchant.merchant_type = qr_merchant.merchant_type
                        merchant.address = qr_merchant.address
                        merchant.description = qr_merchant.description
                        merchant.status = qr_merchant.status
                        merchant.department = qr_merchant.department
                        merchant.province_code = qr_merchant.province_code
                        merchant.district_code = qr_merchant.district_code
                        merchant.wards_code = qr_merchant.wards_code
                        merchant.staff = qr_merchant.staff
                        merchant.created_date = qr_merchant.created_date
                        merchant.modify_date = qr_merchant.modify_date
                        merchant.save()

                        updated += 1
                        if updated % 100 == 0:
                            print('Merchant updated: {}'.format(updated))

                    except Merchant.DoesNotExist:
                        raise CommandError(
                            'Merchant with merchant_id: "%s" does not exist' %
                            (data['merchant_id']))

            self.stdout.write(
                self.style.SUCCESS(
                    'Successfully command. Created: {}. Updated: {}'.format(
                        created, updated)))

            cron_update(cronjob, status=1)

        except Exception as e:
            logging.error('Job merchant_synchronize_change exception: %s', e)
            cron_update(cronjob, status=2, description=str(e))
Exemplo n.º 17
0
    def handle(self, *args, **options):
        cronjob = cron_create(name='auto_create_shop_daily', type='shop')
        desc = {}

        total_row = 0

        try:
            self.stdout.write(
                self.style.WARNING(
                    'Start Auto create new shop from terminal daily processing...'
                ))

            # suggestion_lists la danh sach cac ter có thể trùng
            suggestion_lists = []
            suggestion_query = Terminal.objects.raw('''
                            select t.id
                            from terminal t
                                   inner join shop s on t.merchant_id = s.merchant_id and t.wards_code = (select wards_code
                                                                                                          from qr_wards
                                                                                                          where s.wards_id = qr_wards.id)
                            where t.shop_id is null
                              and t.register_vnpayment <> 1
                              and s.activated = 1
                            group by t.id
                        ''')
            for item in suggestion_query:
                suggestion_lists.append(item.id)
            terminals = Terminal.objects.filter(Q(shop=None) & ~Q(status=-1))

            # terminal_for_creates la danh sach terminal co the su dung de tao shop
            terminal_for_creates = terminals.filter(
                ~Q(id__in=suggestion_lists), ~Q(register_vnpayment=1))

            if terminals.count() == 0:
                raise Exception(
                    'Exception: Count Terminal none shop and status = -1 is 0')
            else:
                desc.update(terminal_count=terminals.count())
                desc.update(terminal_warnings=terminals.filter(
                    id__in=suggestion_lists).count())
                desc.update(terminal_for_creates=terminal_for_creates.count())
                cron_update(cronjob, description=desc)

            for terminal in terminal_for_creates:
                if Shop.objects.filter(
                        merchant_id=terminal.merchant_id,
                        activated=ShopActivateType.ACTIVATE,
                        wards__wards_code=terminal.wards_code).first():
                    desc.update(terminal_warnings=desc['terminal_warnings'] +
                                1)
                else:
                    request = HttpRequest()
                    request.method = 'OTHER'
                    request.user = User.objects.get(pk=1)
                    request.terminal = terminal
                    request.address = terminal.business_address
                    request.street = ''

                    if shop_store(request=request):
                        total_row += 1

                    if total_row % 100 == 0:
                        print('Created total: ' + str(total_row) + 'shops')

            self.stdout.write(
                self.style.SUCCESS(
                    'Finish Auto create new shop from terminal daily processing!'
                ))

            refresh_shop_full_data()

            desc.update(total_shop_created=total_row)
            cron_update(cronjob, description=desc, status=1)

        except Exception as e:
            logging.ERROR("Exception auto_create_shop_daily: {}".format(e))
            desc.update(error_log=str(e))
            cron_update(cronjob, description=desc, status=2)
Exemplo n.º 18
0
    def handle(self, *args, **options):
        cronjob = cron_create(name='administrative_unit_sync', type='system')

        try:
            self.stdout.write(
                self.style.WARNING(
                    'Start qr_province synchronize processing...'))
            limit, offset = 1000, 0
            count_qr_province = self.get_count(db_name='qr_province')
            if count_qr_province > 0:
                self.truncate_table(db_name='qr_province')
                while offset < count_qr_province:
                    query = self.get_query(db_name='qr_province',
                                           limit=limit,
                                           offset=offset)
                    with connections['mms'].cursor() as cursor:
                        cursor.execute(query)
                        columns = [col[0] for col in cursor.description]
                        data_cursor = [
                            dict(zip(columns, row))
                            for row in cursor.fetchall()
                        ]
                    objs = (QrProvince(
                        id=int(item['ID']),
                        province_code=item['PROVINCE_CODE'],
                        province_name=item['PROVINCE_NAME'],
                        created_date=item['CREATED_DATE'],
                        brand_name=item['BRAND_NAME'],
                    ) for item in data_cursor)
                    batch = list(islice(objs, limit))
                    QrProvince.objects.bulk_create(batch, limit)
                    offset = offset + limit
                self.stdout.write(
                    self.style.SUCCESS(
                        'Finish qr_province synchronize processing!'))
            else:
                logging.error(
                    'Administrative unit sync: Count qr_province = 0.Force stop qr_province synchronize'
                )
                cron_update(
                    cronjob,
                    description=
                    'Count qr_province = 0.Force stop qr_province synchronize')
                self.stdout.write(
                    self.style.ERROR(
                        'Count qr_province = 0.Force stop qr_province synchronize!'
                    ))

            self.stdout.write(
                self.style.WARNING(
                    'Start qr_district synchronize processing...'))
            limit, offset = 1000, 0
            count_qr_district = self.get_count(db_name='qr_district')
            if count_qr_district > 0:
                self.truncate_table(db_name='qr_district')
                while offset < count_qr_district:
                    query = self.get_query(db_name='qr_district',
                                           limit=limit,
                                           offset=offset)
                    with connections['mms'].cursor() as cursor:
                        cursor.execute(query)
                        columns = [col[0] for col in cursor.description]
                        data_cursor = [
                            dict(zip(columns, row))
                            for row in cursor.fetchall()
                        ]
                    objs = (QrDistrict(
                        id=int(item['ID']),
                        province_code=item['PROVINCE_CODE'],
                        district_code=item['DISTRICT_CODE'],
                        district_name=item['DISTRICT_NAME'],
                        created_date=item['CREATED_DATE'],
                    ) for item in data_cursor)
                    batch = list(islice(objs, limit))
                    QrDistrict.objects.bulk_create(batch, limit)
                    offset = offset + limit
                self.stdout.write(
                    self.style.SUCCESS(
                        'Finish qr_district synchronize processing!'))
            else:
                logging.error(
                    'Administrative unit sync: Count qr_district = 0.Force stop qr_district synchronize'
                )
                cron_update(
                    cronjob,
                    description=
                    'Count qr_district = 0.Force stop qr_district synchronize')
                self.stdout.write(
                    self.style.ERROR(
                        'Count qr_district = 0.Force stop qr_district synchronize!'
                    ))

            self.stdout.write(
                self.style.WARNING('Start qr_wards synchronize processing...'))
            limit, offset = 1000, 0
            count_qr_wards = self.get_count(db_name='qr_wards')
            if count_qr_wards > 0:
                self.truncate_table(db_name='qr_wards')
                while offset < count_qr_wards:
                    query = self.get_query(db_name='qr_wards',
                                           limit=limit,
                                           offset=offset)
                    with connections['mms'].cursor() as cursor:
                        cursor.execute(query)
                        columns = [col[0] for col in cursor.description]
                        data_cursor = [
                            dict(zip(columns, row))
                            for row in cursor.fetchall()
                        ]
                    objs = (QrWards(
                        id=int(item['ID']),
                        province_code=item['PROVINCE_CODE'],
                        district_code=item['DISTRICT_CODE'],
                        wards_code=item['WARDS_CODE'],
                        wards_name=item['WARDS_NAME'],
                        created_date=item['CREATED_DATE'],
                    ) for item in data_cursor)
                    batch = list(islice(objs, limit))
                    QrWards.objects.bulk_create(batch, limit)
                    print('qr_wards synchronize processing. Row: {}'.format(
                        offset))
                    offset = offset + limit
                self.stdout.write(
                    self.style.ERROR(
                        'Finish qr_wards synchronize processing!'))
            else:
                logging.error(
                    'Administrative unit sync: Count qr_wards = 0.Force stop qr_wards synchronize'
                )
                cron_update(
                    cronjob,
                    description=
                    'Count qr_wards = 0.Force stop qr_wards synchronize')
                self.stdout.write(
                    self.style.ERROR(
                        'Count qr_wards = 0.Force stop qr_wards synchronize!'))

            cron_update(cronjob, status=1)

        except Exception as e:
            logging.error('Administrative unit sync: %s', e)
            cron_update(cronjob, status=2, description=str(e))
    def handle(self, *args, **options):
        cronjob = cron_create(name='shop_cube_sync_daily', type='shop')

        try:

            self.stdout.write(
                self.style.WARNING('Start shop_cube sync daily processing...'))

            limit, offset = 1500, 0

            count_shop_cube = self.get_count_shop_cube()

            if count_shop_cube == 0:
                slack_post_message(
                    pretext='Hệ thống Sale Portal V2',
                    title=
                    f'Job Transaction publish_shop lỗi ngày {date.today().strftime("%d/%m/%Y")}'
                )
                raise Exception("number of shop cube is 0")
            print(f'Count DWH shop_cube row: {count_shop_cube}')

            # Truncate table qr_terminal before synchronize all data from MMS
            cursor = connection.cursor()
            cursor.execute('TRUNCATE TABLE "{0}"'.format(
                ShopCube._meta.db_table))

            print(
                'Truncate table shop_cube before synchronize data from datawarehouse'
            )

            while offset < count_shop_cube:
                query = self.get_query(limit=limit, offset=offset)
                with connections['data_warehouse'].cursor() as cursor:
                    cursor.execute(query)
                    columns = [col[0] for col in cursor.description]
                    data_cursor = [
                        dict(zip(columns, row)) for row in cursor.fetchall()
                    ]

                objs = (ShopCube(
                    shop_id=int(item['shop_id'])
                    if item['shop_id'] != 'Unknown' else 0,
                    merchant_code=item['merchant_code'],
                    merchant_group_bussiness_type=item[
                        'merchant_group_bussiness_type'],
                    report_date=item['report_date'],
                    shop_province_name=item['shop_province_name'],
                    shop_district_name=item['shop_district_name'],
                    shop_ward_name=item['shop_ward_name'],
                    department_name=item['department_name'],
                    shop_address=item['shop_address'],
                    number_of_tran_acm=int(item['number_of_tran_acm']),
                    number_of_tran_last_m=int(item['number_of_tran_last_m']),
                    number_of_tran=int(item['number_of_tran']),
                    number_of_tran_w_1_7=int(item['number_of_tran_w_1_7']),
                    number_of_tran_w_8_14=int(item['number_of_tran_w_8_14']),
                    number_of_tran_w_15_21=int(item['number_of_tran_w_15_21']),
                    number_of_tran_w_22_end=int(
                        item['number_of_tran_w_22_end']),
                    point_w_1_7=item['point_w_1_7'],
                    point_w_8_14=item['point_w_8_14'],
                    point_w_15_21=item['point_w_15_21'],
                    point_w_22_end=item['point_w_22_end'],
                    point_last_m_w_1_7=item['point_last_w_1_7'],
                    point_last_m_w_8_14=item['point_last_w_8_14'],
                    point_last_m_w_15_21=item['point_last_w_15_21'],
                    point_last_m_w_22_end=item['point_last_w_22_end'],
                    voucher_code_list=item['voucher_code_list'],
                    _created_date=item['_created_date'],
                    _updated_date=item['_updated_date'],
                ) for item in data_cursor)

                batch = list(islice(objs, limit))

                ShopCube.objects.bulk_create(batch, limit)

                print(
                    f'ShopCube sync daily processing. Row: {offset}/{count_shop_cube}'
                )

                offset = offset + limit

            self.stdout.write(
                self.style.SUCCESS('Finish shop_cube sync daily processing!'))

            refresh_shop_full_data()

            current_day = date.today().day
            if current_day in [1, 8, 15, 22]:
                print(
                    'Start update shop take_care_status at the beginning of each period'
                )
                Shop.objects.all().update(take_care_status=0)
                shop_need_update_id = ShopCube.objects.filter(
                    to_do=1).values('shop_id')
                Shop.objects.filter(pk__in=shop_need_update_id).update(
                    take_care_status=1)
                print('End update shop take_care_status')

            cron_update(cronjob, status=1)

        except Exception as e:
            logging.error('Job shop_cube_sync_daily exception: %s', e)
            cron_update(cronjob, status=2, description=str(e))