#coding=utf-8

from django.db import models
from django.utils import timezone

from apps.exceptions import CustomError
from apps.account.models import User

class Customer(models.Model):
    name = models.CharField(max_length=100, verbose_name=u"姓名")
    mobile = models.CharField(max_length=12, verbose_name=u"手机号")
    company_name = models.CharField(max_length=100, verbose_name=u"公司名称",blank=True,null=True)
    company_tel = models.CharField(max_length=100, verbose_name=u"公司电话",blank=True,null=True)
    opening_bank = models.CharField(max_length=100, verbose_name=u"开户行", null=True, blank=True)
    account = models.CharField(max_length=100, verbose_name=u"账号", null=True, blank=True)
    credit_code = models.CharField(max_length=100, verbose_name=u"信用代码", null=True, blank=True)
    address = models.CharField(max_length=100, verbose_name=u"地址", null=True, blank=True)
    notes = models.CharField(max_length=200, verbose_name=u"备注", blank=True, null=True)
    create_time = models.DateTimeField(verbose_name=u"创建时间", default=timezone.now)
    create_user = models.ForeignKey(User, verbose_name=u"创建人", on_delete=models.PROTECT, editable=False)

    class Meta:
        db_table = "customer"
        verbose_name = u"客户管理"
        ordering = ('-id',)
        index_together = (
            'name',
            'mobile',
        )
        default_permissions = ()
        permissions = (
            ("view_customer", u"浏览"),
            ("add_customer", u"添加"),
            ("import_customer", u"导入"),
            ("export_customer", u"导出"),
            ("delete_customer", u"删除"),
        )

    @staticmethod
    def getById(id):
        instance = Customer.objects.filter(pk=id).first()
        if not instance:
            raise CustomError(u'未找到相应的客户')
        return instance