简体   繁体   English

导出已完成注册但未执行任何操作的客户列表(无发票、费用、提款)

[英]Export a list of customers who completed registration but haven't performed any action (no invoice, expense, withdrawal)

This is the full task:这是完整的任务:

Export a list of customers who completed registration but haven't performed any action (no invoice, expense, withdrawal) last week (3-9 May)

I need to create this type of SQL, but I don't know how to check for actions, what I did for now is我需要创建这种类型的 SQL,但我不知道如何检查操作,我现在所做的是

SELECT user FROM users_user 
WHERE completed_registration=False
AND date_joined BETWEEN '2021-05-03 00:00:00' AND '2021-05-29 00:00:00'
UNION
SELECT user FROM invoice_invoice;

Check for users who had completed the registration, check for the date, and then check the invoice.检查已完成注册的用户,检查日期,然后检查发票。 But as I check for invoice_invoice itself it's an empty table, why do I get one user when I launch this query?但是当我检查 invoice_invoice 本身时,它是一个空表,为什么我在启动此查询时会得到一个用户? The completed_registration and the date fields which are in queryset right now are only for test.现在查询集中的完成注册和日期字段仅用于测试。

在此处输入图像描述

Only users仅限用户在此处输入图像描述

This is when I check only for invoices这是我只检查发票的时候在此处输入图像描述

This is the structure:这是结构:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Expense model:费用 model:

class Merchant(BaseModel):
    company = models.ForeignKey(Company, on_delete=models.PROTECT, related_name='merchants')
    name = models.CharField(max_length=255)
    company_code = models.CharField(max_length=255, default='', blank=True)

    def __str__(self):
        return f'{self.company} {self.name}'


class Expense(Operation):
    category = models.CharField(choices=ExpenseCategories.get_choices(), default=ExpenseCategories.GENERAL.name,
                                db_index=True, blank=True, max_length=255)
    merchant = models.ForeignKey(Merchant, on_delete=models.PROTECT, related_name='expenses', blank=True, null=True)
    amount = models.PositiveIntegerField(default=0, blank=True, help_text='Only taxable amount. In cents')
    full_amount = models.PositiveIntegerField(
        default=0,
        blank=True,
        help_text='Full amount. Most of the time same as amount or bigger. In cents'
    )
    currency = models.ForeignKey(Currency, on_delete=models.PROTECT, related_name='expenses',
                                 default=settings.DEFAULT_CURRENCY_CODE)
    description = models.TextField(default='', blank=True)
    is_taxable = models.BooleanField(blank=True, default=True)
    from_date = models.DateField(null=True, blank=True, help_text='Start date in case of aggregated bulk creation.')
    to_date = models.DateField(null=True, blank=True, help_text='End date in case of aggregated bulk creation.')
    receipt_number = models.CharField(blank=True, default='', max_length=255, help_text='Number from receipt.')

Invoice model:发票 model:

class Invoice(Operation):
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name='invoices')
    number = models.CharField(max_length=255)
    notes = models.TextField(default='', blank=True)
    payment_due = models.DateField()
    total = models.PositiveIntegerField(help_text='In cents', default=0)
    payment_status = models.CharField(choices=InvoiceStatuses.get_choices(), default=InvoiceStatuses.UNPAID,
                                      max_length=20)
    pdf = models.FileField(null=True, blank=True, upload_to='invoices/pdf', max_length=255)
    is_sent = models.BooleanField(default=False, help_text="Is pdf invoice sent")

User model:用户 model:

class User(AbstractUser):
    username = None

    email = models.EmailField('email address', blank=True)
    phone = PhoneNumberField(unique=True)

    is_verified = models.BooleanField(default=False)
    language = models.ForeignKey(
        Language,
        default=settings.DEFAULT_LANGUAGE,
        on_delete=models.SET_DEFAULT,
    )
    avatar = models.ImageField(upload_to='users/avatars', null=True, blank=True)
    companies = models.ManyToManyField(Company, related_name='users')
    active_company = models.OneToOneField(Company, null=True, related_name='active_user', on_delete=models.SET_NULL)
    agreement_text = models.TextField(default='', blank=True)
    agreement_date = models.DateField(null=True, blank=True)
    personal_no = models.CharField(max_length=100, default='', blank=True)
    full_name = models.CharField(max_length=255, help_text='Field holds first and last names.', default='', blank=True)
    completed_registration = models.BooleanField(default=False)
    work_hours_from = models.TimeField(default=settings.DEFAULT_WORK_HOURS_FROM, null=True, blank=True)
    work_hours_until = models.TimeField(default=settings.DEFAULT_WORK_HOURS_UNTIL, null=True, blank=True)

You seem to want not exists .你似乎想not exists I would expect logic like this:我希望这样的逻辑:

SELECT u.*
FROM users_user u
WHERE u.completed_registration AND
      NOT EXISTS (SELECT 1
                  FROM invoice_invoice i
                  WHERE i.user = u.user AND
                        i.invoice_date >= '2021-05-03' AND
                        i.invoice_date < '2021-05-10'
                 );

You would repeat this logic for each table where you want to check an action.您将为要检查操作的每个表重复此逻辑。 Also, it is not clear what date you want to use within the invoice table, so I made one up.另外,不清楚你想在invoice表中使用什么日期,所以我做了一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM