#-*- coding: utf-8 -*-
from datetime import datetime as dt
import os
from sqlalchemy import *
from sqlalchemy import Column
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relation
from sqlalchemy.types import Time
from sqlalchemy.types import DateTime
from sqlalchemy.types import Integer
from sqlalchemy.types import Unicode
from tg import request
from vatsystem.model import *
from vatsystem.util.common import *
from vatsystem.util.db_extend import *

os.environ["NLS_LANG"] = "american_america.al32utf8"

def getUserID():
    return request.identity["user"].user_id

class SysMixin(object):
    create_time  = Column(DateTime, default=dt.now)
    create_by_id = Column(Integer, default=getUserID)
    update_time  = Column(DateTime, default=dt.now, onupdate=dt.now)
    update_by_id = Column(Integer, default=getUserID, onupdate=getUserID)
    active       = Column(Integer, default=0) # 0 is active ,1 is inactive
    log          = Column(Unicode(1000))

    @classmethod
    def get(cls, id):
        return DBSession.query(cls).get(int(id))

    @classmethod
    def find_all(cls):
        return DBSession.query(cls).all()

    @classmethod
    def find_by_ids(cls, ids, ** args):
        return DBSession.query(cls).filter(cls.id.in_([int(i) for i in ids.split(',')])).all()

    @property
    def create_by(self):
        return DBSession.query(User).get(self.create_by_id)
    
    @classmethod
    def user_name_id(self, username):
        return DBSession.query(User).filter(User.display_name==username).all()

    @property
    def update_by(self):
        return DBSession.query(User).get(self.update_by_id)
    
    
class Table_OHead(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_o_head'

    co_heads = relation('CoHead')
    co_details = relation('CoDetail')
    cp_details = relation('CpDetail')
    cp_charges = relation('CpCharge')
    n_heads = relation('NHead')
    charges = relation('OCharge')
    t_heads = relation('THead')
    c_heads = relation('CHead')
    id                  = Column(Integer, Sequence('o_head_id_seq'), primary_key=True)
    company_code        = Column(Unicode(30))
    head_type           = Column(Unicode(20))
    customer_code       = Column(Unicode(30))
    customer_name       = Column(Unicode(200))
    customer_short_name = Column(Unicode(200))
    ref            = Column(Unicode(20))
    thead_ref      = Column(Unicode(20))
    thead_id       = Column(Integer)
    vat_no         = Column(Unicode(50))
    vat_date       = Column(DateTime)
    status         = Column(Unicode(10))
    export_time    = Column(DateTime, default=dt.now)
    remark         = Column(Unicode(2000))
    relation_si    = Column(Unicode(1000))
    other_charge   = Column(Unicode(200))

class Table_NHead(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_n_head'

    co_heads    = relation('CoHead')
    co_details  = relation('CoDetail')
    charges     = relation('OCharge')
    o_head_id   = Column(Integer, ForeignKey('vat_o_head.id'))
    o_head      = relation('OHead')
    c_heads     = relation('CHead')
    cp_charges  = relation('CpCharge')
    id          = Column(Integer, Sequence('n_head_id_seq'), primary_key=True)
    company_code        = Column(Unicode(30))
    head_type           = Column(Unicode(20))
    customer_code       = Column(Unicode(30))
    customer_name       = Column(Unicode(200))
    customer_short_name = Column(Unicode(200))
    ref       = Column(Unicode(20))
    chead_ref = Column(Unicode(20))
    chead_id  = Column(Integer)
    vat_no    = Column(Unicode(50))
    vat_date  = Column(DateTime)
    status    = Column(Unicode(10))

    export_time  = Column(DateTime, default=dt.now)
    remark       = Column(Unicode(2000))
    other_charge = Column(Unicode(200))

class Table_THead(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_t_head'

    si_heads   = relation('SiHead')
    si_details = relation('SiDetail')
    so_heads   = relation('SoHead')
    so_details = relation('SoDetail')
    c_heads    = relation('CHead')
    charges    = relation('Charge')
    o_head_id  = Column(Integer, ForeignKey('vat_o_head.id'))
    o_head     = relation('OHead', primaryjoin=o_head_id == Table_OHead.id)
    id                  = Column(Integer, Sequence('t_head_id_seq'), primary_key=True)
    head_type           = Column(Unicode(20))
    company_code        = Column(Unicode(30))
    customer_code       = Column(Unicode(30))
    customer_name       = Column(Unicode(200))
    customer_type       = Column(Unicode(30))
    customer_short_name = Column(Unicode(200))
    ref            = Column(Unicode(20))
    vat_no         = Column(Unicode(50))
    vat_date       = Column(DateTime)
    status         = Column(Unicode(10))
    export_time    = Column(DateTime, default=dt.now)
    remark         = Column(Unicode(2000))
    relation_si    = Column(Unicode(1000))
    express_no     = Column(Unicode(100))
    express_date   = Column(Unicode(100))
    rec_month      = Column(Unicode(30))
    payment_status = Column(Unicode(30))
    payment_remark = Column(Unicode(1000))
    

class Table_CHead(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_c_head'

    si_heads   = relation('SiHead')
    so_heads   = relation('SoHead')
    so_details = relation('SoDetail')
    charges    = relation('Charge')
    t_head_id  = Column(Integer, ForeignKey('vat_t_head.id'))
    t_head     = relation('THead')
    n_head_id  = Column(Integer, ForeignKey('vat_n_head.id'))
    n_head     = relation('NHead', primaryjoin=n_head_id == Table_NHead.id)
    o_head_id  = Column(Integer, ForeignKey('vat_o_head.id'))
    o_head     = relation('OHead', primaryjoin=o_head_id == Table_OHead.id)
    id         = Column(Integer, Sequence('c_head_id_seq'), primary_key=True)
    head_type           = Column(Unicode(20))
    company_code        = Column(Unicode(30))
    customer_code       = Column(Unicode(30))
    customer_name       = Column(Unicode(200))
    customer_type       = Column(Unicode(30))
    customer_short_name = Column(Unicode(200))
    thead_ref     = Column(Unicode(20))
    ref           = Column(Unicode(20))
    thead_vat_no  = Column(Unicode(50))
    vat_no        = Column(Unicode(50))
    vat_date      = Column(DateTime)
    status        = Column(Unicode(10))
    export_time   = Column(DateTime, default=dt.now)
    remark        = Column(Unicode(2000))
    relation_si   = Column(Unicode(1000))
    subtraction   = Column(Integer, default = 0)

class Table_PHead(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_p_head'

    pi_heads   = relation('PiHead')
    po_heads   = relation('PoHead')
    s_heads    = relation('SHead')
    m_heads    = relation('MHead')
    pi_details = relation('PiDetail')
    pcharges   = relation('PCharge')
    id                  = Column(Integer, Sequence('p_head_id_seq'), primary_key=True)
    company_code        = Column(Unicode(30))
    supplier            = Column(Unicode(30))
    supplier_name       = Column(Unicode(200))
    supplier_short_name = Column(Unicode(200))
    head_type           = Column(Unicode(20))
    ref                 = Column(Unicode(20))
    dzd                 = Column(Unicode(200))
    category            = Column(Unicode(200))
    vat_no       = Column(Unicode(50))
    vat_date     = Column(DateTime)
    status       = Column(Unicode(10))
    export_time  = Column(DateTime, default=dt.now)
    remark       = Column(Unicode(2000))

class Table_SHead(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_s_head'

    pi_heads  = relation('PiHead')
    po_heads  = relation('PoHead')
    p_head_id = Column(Integer, ForeignKey('vat_p_head.id'))
    p_head    = relation('PHead')
    m_heads   = relation('MHead')
    pi_details = relation('PiDetail')
    pcharges  = relation('PCharge')
    id        = Column(Integer, Sequence('s_head_id_seq'), primary_key=True)
    company_code  = Column(Unicode(30))
    head_type = Column(Unicode(20))
    ref       = Column(Unicode(20))
    dzd       = Column(Unicode(50))
    phead_ref           = Column(Unicode(20))
    phead_vat_no        = Column(Unicode(50))
    supplier            = Column(Unicode(30))
    supplier_name       = Column(Unicode(200))
    supplier_short_name = Column(Unicode(200))
    vat_no       = Column(Unicode(50))
    vat_date     = Column(DateTime)
    status       = Column(Unicode(10))
    export_time  = Column(DateTime, default=dt.now)
    remark       = Column(Unicode(2000))

class Table_SiHead(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_si_head'

    id = Column(Integer, Sequence('si_head_id_seq'), primary_key=True)
    t_head_id           = Column(Integer, ForeignKey('vat_t_head.id'))
    t_head              = relation('THead', primaryjoin=t_head_id == Table_THead.id)
    c_head_id           = Column(Integer, ForeignKey('vat_c_head.id'))
    c_head              = relation('CHead', primaryjoin=c_head_id == Table_CHead.id)
    si_details          = relation('SiDetail')
    charges             = relation('Charge')
    invoice_no          = Column(Unicode(30))
    company_code        = Column(Unicode(30))
    sc_no               = Column(Unicode(30))
    department          = Column(Unicode(30))#ae
    create_date         = Column(DateTime)
    status              = Column(Unicode(30))
    customer            = Column(Unicode(200))
    customer_short_name = Column(Unicode(200))
    cust_po_no          = Column(Unicode(30))
    currency         = Column(Unicode(30))
    order_amt        = Column(Numeric(17, 6), default=0)
    item_amt         = Column(Numeric(17, 6), default=0)
    so_sales_contact = Column(Unicode(200))
    remark           = Column(Unicode(2000))

class Table_SiDetail(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_si_detail'

    id = Column(Integer, Sequence('si_detail_id_seq'), primary_key=True)

    t_head_id        = Column(Integer, ForeignKey('vat_t_head.id'))
    t_head           = relation('THead', primaryjoin=t_head_id == Table_THead.id)
    c_head_id        = Column(Integer, ForeignKey('vat_c_head.id'))
    c_head           = relation('CHead', primaryjoin=c_head_id == Table_CHead.id)
    si_head_id       = Column(Integer, ForeignKey('vat_si_head.id'))
    si_head          = relation('SiHead', primaryjoin=si_head_id == Table_SiHead.id)
    uuid             = Column(Unicode(100))
    desc_zh          = Column(Unicode(2000))
    vat_qty          = Column(Integer, default=0)
    base_ava_qty     = Column(Integer, default=0)
    qty              = Column(Integer, default=0)
    invoice_no       = Column(Unicode(30))
    sc_no            = Column(Unicode(30))
    company_code     = Column(Unicode(30))
    line_no          = Column(Integer)
    sc_item_line_no  = Column(Integer)
    item_no          = Column(Unicode(30))
    item_desc        = Column(Unicode(2000))
    unit_price       = Column(Numeric(17, 6), default=0)
    unit             = Column(Unicode(30))
    remark           = Column(Unicode(100))

class  Table_SoHead(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_so_head'

    id = Column(Integer, Sequence('so_head_id_seq'), primary_key=True)
    t_head_id         = Column(Integer, ForeignKey('vat_t_head.id'))
    t_head            = relation('THead', primaryjoin=t_head_id == Table_THead.id)
    c_head_id         = Column(Integer, ForeignKey('vat_c_head.id'))
    c_head            = relation('CHead', primaryjoin=c_head_id == Table_CHead.id)
    so_details        = relation('SoDetail')
    charges           = relation('Charge')
    sales_contract_no = Column(Unicode(30))
    company_code  = Column(Unicode(30))
    order_dept    = Column(Unicode(30))
    status        = Column(Unicode(30))
    customer_code = Column(Unicode(30))
    customer_name = Column(Unicode(200))
    currency      = Column(Unicode(30))
    create_date = Column(DateTime)
    ae          = Column(Unicode(30))
    cust_po_no  = Column(Unicode(30))
    order_amt   = Column(Numeric(17, 6), default=0)
    item_amt    = Column(Numeric(17, 6), default=0)
    remark      = Column(Unicode(2000))

class Table_SoDetail(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_so_detail'

    id = Column(Integer, Sequence('so_detail_id_seq'), primary_key=True)
    t_head_id  = Column(Integer, ForeignKey('vat_t_head.id'))
    t_head     = relation('THead', primaryjoin=t_head_id == Table_THead.id)
    c_head_id  = Column(Integer, ForeignKey('vat_c_head.id'))
    c_head     = relation('CHead', primaryjoin=c_head_id == Table_CHead.id)
    so_head_id = Column(Integer, ForeignKey('vat_so_head.id'))
    so_head = relation('SoHead', primaryjoin=so_head_id == Table_SoHead.id)
    uuid    = Column(Unicode(100))
    desc_zh = Column(Unicode(2000))
    vat_qty = Column(Integer, default=0)
    qty               = Column(Integer, default=0)
    invoiced_qty      = Column(Integer, default=0)
    ava_qty           = Column(Integer)
    sales_contract_no = Column(Unicode(30))
    company_code      = Column(Unicode(30))
    line_no           = Column(Integer)
    item_no      = Column(Unicode(30))
    description  = Column(Unicode(2000))
    unit_price   = Column(Numeric(17, 6), default=0)
    unit         = Column(Unicode(30))
    remark       = Column(Unicode(100))
    invoice_no   = Column(Unicode(100))
    invoice_qty  = Column(Unicode(1000))
    
class Table_ItemDesc(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_item_desc'

    id          = Column(Integer, Sequence('item_desc_id_seq'), primary_key=True)
    code        = Column(Unicode(30))
    item_no     = Column(Unicode(30))
    description = Column(Unicode(2000))

class Table_Charge(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_charge'

    id = Column(Integer, Sequence('item_charge_id_seq'), primary_key=True)

    t_head_id  = Column(Integer, ForeignKey('vat_t_head.id'))
    t_head     = relation('THead', primaryjoin=t_head_id == Table_THead.id)
    c_head_id  = Column(Integer, ForeignKey('vat_c_head.id'))
    c_head     = relation('CHead', primaryjoin=c_head_id == Table_CHead.id)
    so_head_id = Column(Integer, ForeignKey('vat_so_head.id'))
    so_head    = relation('SoHead', primaryjoin=so_head_id == Table_SoHead.id)
    si_head_id = Column(Integer, ForeignKey('vat_si_head.id'))
    si_head    = relation('SiHead', primaryjoin=si_head_id == Table_SiHead.id)
    
    company_code = Column(Unicode(30))
    line_no      = Column(Integer)
    charge_code = Column(Unicode(2000))
    total       = Column(Numeric(17, 6))
    vat_total  = Column(Numeric(17, 6))
    type       = Column(Unicode(30))
    currency   = Column(Unicode(30))
    cust_po_no = Column(Unicode(30))
    
    invoice_no = Column(Unicode(30))
    sales_contract_no = Column(Unicode(30))
    note_no           = Column(Unicode(30))
    cust_code         = Column(Unicode(30))
    pending           = Column(Unicode(30))
    uuid              = Column(Unicode(100))

class Table_STHead(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_t_sthead'

    p_head_id  = Column(Integer, ForeignKey('vat_p_head.id'))
    p_head     = relation('PHead', primaryjoin=p_head_id == Table_PHead.id)
    st_details = relation('STDetail')
    dn_details = relation('DNDetail')
    
    id         = Column(Integer, primary_key = True)
    name       = Column(Unicode(200))
    supplier            = Column(Unicode(200))
    supplier_name       = Column(Unicode(200))
    supplier_short_name = Column(Unicode(200))
    ref                 = Column(Unicode(100))
    upload_name         = Column(Unicode(100))
    file_name           = Column(Unicode(100))
    i_file_name         = Column(Unicode(100))
    status              = Column(Unicode(30), default='uncomplete')
    
 
class Table_STDetail(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_t_stdetail'
    
    st_head_id  = Column(Integer, ForeignKey('vat_t_sthead.id'))
    st_head     = relation('STHead', primaryjoin=st_head_id == Table_STHead.id)
    pi_details = relation('PiDetail')
    pcharges   = relation('PCharge')
    
    id         = Column(Integer, primary_key = True)
    dn_no      = Column(Unicode(200))
    po_no      = Column(Unicode(200))
    invoice_no = Column(Unicode(200))
    desc_zh    = Column(Unicode(2000))
    item_code  = Column(Unicode(100))
    qty        = Column(Integer)
    unit       = Column(Unicode(100))
    ex         = Column(Unicode(100))
    exchange_rate = Column(Numeric(17, 4), default=0)
    tax_rate   = Column(Numeric(17, 2), default=0)
    rmb_unit   = Column(Numeric(17, 3), default=0)
    rmb_amount = Column(Numeric(17, 3), default=0)
    rmb_tax    = Column(Numeric(17, 3), default=0)
    rmb_amount_p = Column(Numeric(17, 2), default=0)
    status       = Column(Unicode(30), default='uncomplete')
    remark       = Column(Unicode(2000))
    error        = Column(Unicode(100))
    type         = Column(Unicode(10))
    mpi_id       = Column(Integer)
    mpi_ref      = Column(Unicode(200))
    pi_line_no       = Column(Integer)
    posted_status    = Column(Unicode(30))
    department       = Column(Unicode(200))
    issue_date       = Column(DateTime)
    supplier_code    = Column(Unicode(200))
    supplier_name    = Column(Unicode(100))
    supplier_short_name    = Column(Unicode(100))
    currency               = Column(Unicode(10))
    invoice_amount         = Column(Numeric(17, 6))
    invoice_base_amount    = Column(Numeric(17, 6))
    grn_no                 = Column(Unicode(100))
    corporate_customer     = Column(Unicode(200))
    private_brand          = Column(Unicode(50))
    private_brand_sub_line = Column(Unicode(100))
    packaging_category     = Column(Unicode(100))
    packaging_type     = Column(Unicode(20))
    material           = Column(Unicode(20))
    merchandise        = Column(Unicode(200))
    internal_item_code = Column(Unicode(200))
    item_description   = Column(Unicode(10000))
    unit_price         = Column(Numeric(17, 6))
    item_amount        = Column(Numeric(17, 4), default=0)
    item_base_amount   = Column(Numeric(17, 2), default=0)
    billing_month      = Column(Unicode(100))
    kingdee_date       = Column(Unicode(100))
    reconciliation_lot = Column(Unicode(100))
    payment_date       = Column(Unicode(100))
    category           = Column(Unicode(200))
    so_no              = Column(Unicode(100))
    so_date            = Column(DateTime)
    po_date            = Column(DateTime)
    pi_date            = Column(DateTime)
    customer_code      = Column(Unicode(100))


class Table_DNDetail(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_t_dndetail'
    
    st_head_id  = Column(Integer, ForeignKey('vat_t_sthead.id'))
    st_head     = relation('STHead', primaryjoin=st_head_id == Table_STHead.id)
    pi_details = relation('PiDetail')
    pcharges   = relation('PCharge')
    msn_id = Column(Integer)
    msn_ref = Column(Unicode(200))
    id      = Column(Integer, primary_key = True)
    type    = Column(Unicode(100))
    number  = Column(Unicode(100))
    stock_adjustment_no = Column(Unicode(100))
    so_no = Column(Unicode(200))
    po_no = Column(Unicode(100))
    note_type    = Column(Unicode(20))
    other_ref    = Column(Unicode(100))
    note_remark     = Column(Unicode(1000))
    issue_date      =  Column(DateTime)
    department      = Column(Unicode(100))
    supplier_code   = Column(Unicode(100))
    supplier_name   = Column(Unicode(100))
    sales_contact   = Column(Unicode(100))
    currency        = Column(Unicode(10))
    note_amount     = Column(Numeric(17, 6))
    note_base_amount       = Column(Numeric(17, 6))
    corporate_customer     = Column(Unicode(200))
    private_brand          = Column(Unicode(50))
    private_brand_sub_line = Column(Unicode(100))
    sn_line_no         = Column(Integer)
    packaging_category = Column(Unicode(100))
    packaging_type     = Column(Unicode(100))
    material           = Column(Unicode(100))
    merchandise        = Column(Unicode(300))
    internal_item_code = Column(Unicode(100))
    item_code          = Column(Unicode(100))
    item_description   = Column(Unicode(1000))
    order_qty          = Column(Integer)
    unit               = Column(Unicode(300))
    unit_price         = Column(Numeric(17, 6))
    amount             = Column(Numeric(17, 6), default=0)
    base_amount        = Column(Unicode(100), default=0)
    billing_month      = Column(Unicode(100))
    kingdee_date       = Column(Unicode(100))
    reconciliation_lot = Column(Unicode(100))
    payment_date       = Column(Unicode(100))
    status             = Column(Unicode(30), default='uncomplete')
    error              = Column(Unicode(100))
    category           = Column(Unicode(200))
    tax_rate           = Column(Numeric(17, 2), default=0)

    
class Table_MHead(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_m_head'

    pi_heads  = relation('PiHead')
    po_heads  = relation('PoHead')
    p_head_id = Column(Integer, ForeignKey('vat_p_head.id'))
    p_head    = relation('PHead')
    s_head_id = Column(Integer, ForeignKey('vat_s_head.id'))
    s_head    = relation('SHead', primaryjoin = s_head_id == Table_SHead.id)
    pcharges  = relation('PCharge')
    id        = Column(Integer, Sequence('m_head_id_seq'), primary_key=True)
    head_type = Column(Unicode(20))
    ref       = Column(Unicode(20))
    phead_ref           = Column(Unicode(20))
    phead_vat_no        = Column(Unicode(50))
    supplier            = Column(Unicode(30))
    supplier_name       = Column(Unicode(200))
    supplier_short_name = Column(Unicode(200))
    payment_date     = Column(DateTime)
    vat_no       = Column(Unicode(50))
    vat_date     = Column(DateTime)
    status       = Column(Unicode(10))
    export_time  = Column(DateTime, default=dt.now)
    remark       = Column(Unicode(2000))
       
class Table_PiHead(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_pi_head'
    
    p_head_id  = Column(Integer, ForeignKey('vat_p_head.id'))
    p_head     = relation('PHead', primaryjoin=p_head_id == Table_PHead.id)
    s_head_id  = Column(Integer, ForeignKey('vat_s_head.id'))
    s_head     = relation('SHead', primaryjoin = s_head_id == Table_SHead.id)
    m_head_id  = Column(Integer, ForeignKey('vat_m_head.id'))
    m_head     = relation('MHead', primaryjoin = m_head_id == Table_MHead.id)
    pi_details = relation('PiDetail')
    pcharges   = relation('PCharge')
    id         = Column(Integer, Sequence('pi_head_id_seq'), primary_key=True)
    invoice_no          = Column(Unicode(30))
    note_no             = Column(Unicode(100))
    head_type           = Column(Unicode(20))
    charge_type         = Column(Integer)
    company_code        = Column(Unicode(30))
    po_no               = Column(Unicode(200))
    ref                 = Column(Unicode(100))
    supplier            = Column(Unicode(200))
    supplier_name       = Column(Unicode(200))
    supplier_short_name = Column(Unicode(200))
    invoice_date        = Column(DateTime)
    department          = Column(Unicode(30))
    status              = Column(Unicode(10))
    currency            = Column(Unicode(30))
    order_amt           = Column(Numeric(17, 6), default=0)
    item_amt            = Column(Numeric(17, 6), default=0)
    remark              = Column(Unicode(2000))
    

class Table_PiDetail(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_pi_detail'
    
    p_head_id  = Column(Integer, ForeignKey('vat_p_head.id'))
    p_head     = relation('PHead', primaryjoin = p_head_id == Table_PHead.id)
    s_head_id  = Column(Integer, ForeignKey('vat_s_head.id'))
    s_head     = relation('SHead', primaryjoin = s_head_id == Table_SHead.id)
    m_head_id  = Column(Integer, ForeignKey('vat_m_head.id'))
    m_head     = relation('MHead', primaryjoin = m_head_id == Table_MHead.id)
    pi_head_id = Column(Integer, ForeignKey('vat_pi_head.id'))
    pi_head    = relation('PiHead', primaryjoin = pi_head_id == Table_PiHead.id)
    st_detail_id = Column(Integer, ForeignKey('vat_t_stdetail.id'))
    st_detail    = relation('STDetail', primaryjoin = st_detail_id == Table_STDetail.id)
    dn_detail_id = Column(Integer, ForeignKey('vat_t_dndetail.id'))
    dn_detail    = relation('DNDetail', primaryjoin = dn_detail_id == Table_DNDetail.id)
    id = Column(Integer, Sequence('pi_detail_id_seq'), primary_key=True)
    uuid         = Column(Unicode(100))
    uuid2        = Column(Unicode(100))
    invoice_no   = Column(Unicode(30))
    company_code = Column(Unicode(30))
    po_no        = Column(Unicode(200))
    category     = Column(Unicode(100))
    po_line_no   = Column(Integer, default=0)
    desc_zh      = Column(Unicode(2000))
    qty          = Column(Integer, default=0)
    vat_qty      = Column(Integer, default=0)
    line_no      = Column(Integer)
    note_no      = Column(Unicode(100))
    item_no      = Column(Unicode(30))
    item_desc    = Column(Unicode(2000))
    unit_price   = Column(Numeric(17, 6), default=0)
    item_total   = Column(Numeric(17, 6), default=0)
    vat_total    = Column(Numeric(17, 6), default=0)
    unit         = Column(Unicode(30))
    head_type    = Column(Unicode(20))
    tax_rate     = Column(Numeric(17, 6), default=0)
    base_unit_price   = Column(Numeric(17, 6), default=0)
    base_tax_rate     = Column(Numeric(17, 6), default=0)
    include_tax       = Column(Integer, default=0)
    remark            = Column(Unicode(100))
    kingdee_date      = Column(Date)
    so_no             = Column(Unicode(100))
    so_date           = Column(DateTime)
    po_date           = Column(DateTime)
    pi_date           = Column(DateTime)
    customer_code     = Column(Unicode(100))
    flag              = Column(Integer, default=0)
    

class Table_PoHead(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_po_head'
    
    p_head_id  = Column(Integer, ForeignKey('vat_p_head.id'))
    p_head     = relation('PHead', primaryjoin=p_head_id == Table_PHead.id)
    s_head_id  = Column(Integer, ForeignKey('vat_s_head.id'))
    s_head     = relation('SHead', primaryjoin=s_head_id == Table_SHead.id)
    m_head_id  = Column(Integer, ForeignKey('vat_m_head.id'))
    m_head     = relation('MHead', primaryjoin = m_head_id == Table_MHead.id)
    po_details = relation('PoDetail')
    pcharges   = relation('PCharge')
    id           = Column(Integer, Sequence('po_head_id_seq'), primary_key=True)
    item_no      = Column(Unicode(30))
    po_no        = Column(Unicode(200))
    
class Table_PoDetail(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_po_detail'

    p_head_id  = Column(Integer, ForeignKey('vat_p_head.id'))
    p_head     = relation('PHead', primaryjoin=p_head_id == Table_PHead.id)
    s_head_id  = Column(Integer, ForeignKey('vat_s_head.id'))
    s_head     = relation('SHead', primaryjoin=s_head_id == Table_SHead.id)
    po_head_id = Column(Integer, ForeignKey('vat_po_head.id'))
    po_head    = relation('PoHead', primaryjoin=po_head_id == Table_PoHead.id)
    id           = Column(Integer, Sequence('so_detail_id_seq'), primary_key=True)
    item_no      = Column(Unicode(30))
    company_code = Column(Unicode(30))
        
class Table_PCharge(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_pcharge'

    p_head_id  = Column(Integer, ForeignKey('vat_p_head.id'))
    p_head     = relation('PHead', primaryjoin=p_head_id == Table_PHead.id)
    s_head_id  = Column(Integer, ForeignKey('vat_s_head.id'))
    s_head     = relation('SHead', primaryjoin=s_head_id == Table_SHead.id)
    po_head_id = Column(Integer, ForeignKey('vat_po_head.id'))
    po_head    = relation('PoHead', primaryjoin=po_head_id == Table_PoHead.id)
    pi_head_id = Column(Integer, ForeignKey('vat_pi_head.id'))
    pi_head    = relation('PiHead', primaryjoin=pi_head_id == Table_PiHead.id)
    m_head_id  = Column(Integer, ForeignKey('vat_m_head.id'))
    m_head     = relation('MHead', primaryjoin = m_head_id == Table_MHead.id)
    st_detail_id = Column(Integer, ForeignKey('vat_t_stdetail.id'))
    st_detail    = relation('STDetail', primaryjoin = st_detail_id == Table_STDetail.id)
    dn_detail_id = Column(Integer, ForeignKey('vat_t_dndetail.id'))
    dn_detail    = relation('DNDetail', primaryjoin = dn_detail_id == Table_DNDetail.id)
    id = Column(Integer, Sequence('item_scharge_id_seq'), primary_key=True)
    uuid            = Column(Unicode(100))
    uuid2           = Column(Unicode(100))
    company_code    = Column(Unicode(30), nullable=False, primary_key=True)
    invoice_no      = Column(Unicode(30))
    po_no           = Column(Unicode(200))
    type            = Column(Unicode(30))
    line_no         = Column(Integer)
    charge_code     = Column(Unicode(30))
    amount          = Column(Numeric(17, 2), default=0)
    total           = Column(Numeric(17, 2), default=0)
    percentage      = Column(Numeric(17, 6))
    note_no         = Column(Unicode(30))
    currency        = Column(Unicode(30))
    vat_total       = Column(Numeric(17, 2), default=0)
    kingdee_date    = Column(Date)

class  Table_CoHead(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_co_head'

    id = Column(Integer, Sequence('so_head_id_seq'), primary_key=True)
    o_head_id  = Column(Integer, ForeignKey('vat_o_head.id'))
    o_head     = relation('OHead', primaryjoin=o_head_id == Table_OHead.id)
    n_head_id  = Column(Integer, ForeignKey('vat_n_head.id'))
    n_head     = relation('NHead', primaryjoin=n_head_id == Table_NHead.id)
    co_details = relation('CoDetail')
    invoice_no = Column(Unicode(30))
    sales_contract_no = Column(Unicode(30))
    company_code      = Column(Unicode(30))
    po_no             = Column(Unicode(200))
    order_dept        = Column(Unicode(30))
    status            = Column(Unicode(30))
    customer_code = Column(Unicode(30))
    customer_name = Column(Unicode(200))
    currency      = Column(Unicode(30))
    create_date   = Column(DateTime)
    ae            = Column(Unicode(30))
    cust_po_no    = Column(Unicode(30))
    head_ref      = Column(Unicode(20))
    head_vat_no   = Column(Unicode(50))
    order_amt     = Column(Numeric(17, 6), default=0)
    item_amt      = Column(Numeric(17, 6), default=0)
    remark        = Column(Unicode(2000))
    relation_charge  = Column(Unicode(200))

class Table_CoDetail(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_co_detail'

    id = Column(Integer, Sequence('co_detail_id_seq'), primary_key=True)
    o_head_id  = Column(Integer, ForeignKey('vat_o_head.id'))
    o_head     = relation('OHead', primaryjoin=o_head_id == Table_OHead.id)
    n_head_id  = Column(Integer, ForeignKey('vat_n_head.id'))
    n_head     = relation('NHead', primaryjoin=n_head_id == Table_NHead.id)
    co_head_id = Column(Integer, ForeignKey('vat_co_head.id'))
    co_head    = relation('CoHead', primaryjoin=co_head_id == Table_CoHead.id)
    cp_details = relation('CpDetail')
    uuid    = Column(Unicode(100))
    po_no   = Column(Unicode(200))
    pi_no   = Column(Unicode(200))
    desc_zh = Column(Unicode(2000))
    vat_qty = Column(Integer, default=0)
    qty               = Column(Integer, default=0)
    invoiced_qty      = Column(Integer, default=0)
    ava_qty           = Column(Integer)
    invoice_no        = Column(Unicode(30))
    sales_contract_no = Column(Unicode(30))
    company_code      = Column(Unicode(30))
    line_no           = Column(Integer)
    item_no      = Column(Unicode(30))
    description  = Column(Unicode(2000))
    unit_price   = Column(Numeric(17, 6), default=0)
    unit         = Column(Unicode(30))
    remark       = Column(Unicode(100))

class Table_CpDetail(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_cp_detail'

    id = Column(Integer, Sequence('cp_detail_id_seq'), primary_key=True)
    o_head_id         = Column(Integer, ForeignKey('vat_o_head.id'))
    o_head            = relation('OHead', primaryjoin=o_head_id == Table_OHead.id)
    n_head_id         = Column(Integer, ForeignKey('vat_n_head.id'))
    n_head            = relation('NHead', primaryjoin=n_head_id == Table_NHead.id)
    co_detail_id      = Column(Integer, ForeignKey('vat_co_detail.id'))
    co_detail         = relation('CoDetail', primaryjoin = co_detail_id == Table_CoDetail.id)
    company_code      = Column(Unicode(30))
    sales_contract_no = Column(Unicode(30))
    po_no         = Column(Unicode(200))
    po_qty        = Column(Integer, default=0)
    pi_no         = Column(Unicode(200))
    pi_qty        = Column(Integer, default=0)
    supplier      = Column(Unicode(200))
    supplier_name = Column(Unicode(200))
    ae            = Column(Unicode(30))
    currency      = Column(Unicode(30))
    exchange_rate = Column(Numeric(17, 4), default=0)
    line_no       = Column(Integer)
    po_line_no    = Column(Integer)
    grn_no        = Column(Unicode(100))
    grn_line_no   = Column(Integer)
    item_no       = Column(Unicode(30))
    item_qty      = Column(Integer)
    unit_price    = Column(Numeric(17, 6), default=0)
    pi_unit_price = Column(Numeric(17, 6), default=0)
    pi_total      = Column(Numeric(17, 6), default=0)
    unit          = Column(Unicode(30))
    desc_zh       = Column(Unicode(2000))
    description   = Column(Unicode(2000))
    remark        = Column(Unicode(100))
    flag          = Column(Integer, default=0)

class Table_CpCharge(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_cp_charge'

    id = Column(Integer, Sequence('item_cpcharge_id_seq'), primary_key=True)
    o_head_id = Column(Integer, ForeignKey('vat_o_head.id'))
    o_head    = relation('OHead', primaryjoin=o_head_id == Table_OHead.id)
    n_head_id = Column(Integer, ForeignKey('vat_n_head.id'))
    n_head    = relation('NHead', primaryjoin=n_head_id == Table_NHead.id)
    type      = Column(Unicode(30))
    so_no     = Column(Unicode(30))
    si_no     = Column(Unicode(30))
    status    = Column(Unicode(30), default='New')
    company_code        = Column(Unicode(30))
    line_no             = Column(Integer)
    pi_line_no          = Column(Integer)
    supplier            = Column(Unicode(30))
    supplier_name       = Column(Unicode(100))
    supplier_short_name = Column(Unicode(100))
    charge_code   = Column(Unicode(30))
    total         = Column(Numeric(17, 6), default=0)
    all_total     = Column(Numeric(17, 6), default=0)
    po_total      = Column(Numeric(17, 6), default=0)
    vat_total     = Column(Numeric(17, 6), default=0)
    pi_no         = Column(Unicode(30))
    po_no         = Column(Unicode(30))
    ae            = Column(Unicode(30))
    currency      = Column(Unicode(30))
    exchange_rate = Column(Numeric(17, 4), default=0)
    note_no       = Column(Unicode(30))
    note_type     = Column(Unicode(30))
    note_date     = Column(DateTime)
    cust_code     = Column(Unicode(30))
    cust_name     = Column(Unicode(200))
    remark        = Column(Unicode(100))
    
class Table_OCharge(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_ocharge'

    id           = Column(Integer, Sequence('item_charge_id_seq'), primary_key=True)
    o_head_id    = Column(Integer, ForeignKey('vat_o_head.id'))
    o_head       = relation('OHead', primaryjoin=o_head_id == Table_OHead.id)
    n_head_id    = Column(Integer, ForeignKey('vat_n_head.id'))
    n_head       = relation('NHead', primaryjoin=n_head_id == Table_NHead.id)
    type         = Column(Unicode(30))
    uuid         = Column(Unicode(100))
    status       = Column(Unicode(30), default='New')
    company_code = Column(Unicode(30))
    line_no      = Column(Integer)
    charge_code  = Column(Unicode(2000))
    total        = Column(Numeric(17, 6))
    vat_total    = Column(Numeric(17, 6))
    invoice_no   = Column(Unicode(30))
    po_no        = Column(Unicode(30))
    note_no      = Column(Unicode(30))
    cust_code    = Column(Unicode(30))
    

class Table_STatement(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_t_statement'

    id = Column(Integer, primary_key = True)
    st_head_id   = Column(Integer, ForeignKey('vat_t_sthead.id'))
    st_head      = relation('STHead', primaryjoin=st_head_id == Table_STHead.id)
    file_name    = Column(Unicode(100))
    i_file_name  = Column(Unicode(100))
    upload_name  = Column(Unicode(100))
    status       = Column(Unicode(100))
      

class Table_Variance(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_t_variance'
    
    id = Column(Integer, primary_key = True)
    so_no                  = Column(Unicode(30))
    po_no                  = Column(Unicode(30))
    pi_no                  = Column(Unicode(30))
    item_no                = Column(Unicode(30))
    grn_no                 = Column(Unicode(30))
    note_no                = Column(Unicode(30))
    grn_line_no            = Column(Integer)
    supplier_short_name    = Column(Unicode(100))
    designation            = Column(Unicode(100))
    po_qty                 = Column(Integer)
    line_no                = Column(Integer)
    type                   = Column(Unicode(30))
    currency               = Column(Unicode(30))
    exchange_rate          = Column(Unicode(30))
    with_out_price         = Column(Numeric(17, 6), default=0)
    with_out_amount        = Column(Numeric(17, 6), default=0)
    billing_month          = Column(Unicode(200))
    change_project         = Column(Unicode(200))
    change_with_out_price  = Column(Numeric(17, 6), default=0)
    change_with_out_amount = Column(Numeric(17, 6), default=0)
    variance_price         = Column(Numeric(17, 6), default=0) 
    variance_amount        = Column(Numeric(17, 6), default=0) 
    variance_date          = Column(DateTime)
    related                = Column(Unicode(200))


class Table_PIProceeds(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_pi_proceeds'

    id                  = Column(Integer, primary_key = True)
    company_code        = Column(Unicode(30))
    pi_no               = Column(Unicode(30))
    department          = Column(Unicode(30))
    issue_date          = Column(DateTime)
    supplier_code       = Column(Unicode(30))
    supplier_name       = Column(Unicode(200))
    supplier_short_name = Column(Unicode(30))
    remark              = Column(Unicode(3000))
    currency            = Column(Unicode(30))
    posted_status       = Column(Unicode(200))
    invoice_amount      = Column(Numeric(17, 6), default=0)
    invoice_base_amount = Column(Numeric(17, 6), default=0)
    po_no               = Column(Unicode(30))
    grn_no              = Column(Unicode(30))
    program             = Column(Unicode(30))
    brand               = Column(Unicode(30))
    brand_sub_line      = Column(Unicode(30))
    category            = Column(Unicode(30))
    sub_cat_1           = Column(Unicode(30))
    sub_cat_2           = Column(Unicode(30))
    sub_cat_3           = Column(Unicode(30))
    material            = Column(Unicode(30))
    merchandise         = Column(Unicode(30))
    internal_item_code  = Column(Unicode(200))
    item_code           = Column(Unicode(30))
    item_description    = Column(Unicode(2000)) 
    qty                 = Column(Integer)
    unit                = Column(Unicode(30))
    unit_price          = Column(Numeric(17, 6), default=0)
    item_amount         = Column(Numeric(17, 6), default=0)
    item_base_amount    = Column(Numeric(17, 6), default=0) 
    kingdee_date        = Column(Unicode(100))
    flag                = Column(Integer, default=0)
    date                = Column(Unicode(100))
    
class Table_DNProceeds(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_dn_proceeds'

    id                     = Column(Integer, primary_key = True)
    company_code           = Column(Unicode(30))
    number                 = Column(Unicode(30))
    stock_adjustment_no    = Column(Unicode(30))
    po_no                  = Column(Unicode(30))
    note_type              = Column(Unicode(30))
    other_ref              = Column(Unicode(30))
    note_remark            = Column(Unicode(300)) 
    issue_date             = Column(DateTime)
    department             = Column(Unicode(300)) 
    supplier_code          = Column(Unicode(200))
    supplier_name          = Column(Unicode(200))
    sales_contact          = Column(Unicode(30))
    currency               = Column(Unicode(30))
    note_amount            = Column(Unicode(30))
    note_base_amount       = Column(Unicode(30))
    corporate_customer     = Column(Unicode(30)) 
    private_brand          = Column(Unicode(200))
    private_brand_sub_line = Column(Unicode(30))
    packaging_category     = Column(Unicode(30))
    packaging_type         = Column(Unicode(30)) 
    sub_cat_2              = Column(Unicode(30))
    sub_cat_3              = Column(Unicode(30)) 
    material               = Column(Unicode(30))
    merchandise            = Column(Unicode(300)) 
    internal_item_code     = Column(Unicode(2000))  
    item_code              = Column(Unicode(300))
    item_description       = Column(Unicode(2000))
    order_qty              = Column(Integer)
    unit                   = Column(Unicode(30))
    unit_price             = Column(Numeric(17, 6), default=0)
    amount                 = Column(Numeric(17, 6), default=0)
    base_amount            = Column(Numeric(17, 6), default=0)
    sn_line_no             = Column(Integer)
    kingdee_date           = Column(Unicode(100))
    flag                   = Column(Integer, default=0)
    date                = Column(Unicode(100))

class Table_PICost(DeclarativeBase, SysMixin):
    
    __tablename__ = 'vat_pi_cost'

    id                   = Column(Integer, primary_key = True)
    company_code         = Column(Unicode(30))
    invoice_number       = Column(Unicode(30))
    department           = Column(Unicode(30))
    issue_date           = Column(DateTime)
    supplier_short_name  = Column(Unicode(200))
    remark               = Column(Unicode(200))
    currency             = Column(Unicode(30))
    posted_status        = Column(Unicode(2000))
    invoice_amount       = Column(Numeric(17, 6), default=0) 
    invoice_base_amount  = Column(Numeric(17, 6), default=0)  
    po_no                = Column(Unicode(30))
    grn_no               = Column(Unicode(30))
    program              = Column(Unicode(300))
    brand                = Column(Unicode(300))
    category             = Column(Unicode(30))
    sub_cat_1            = Column(Unicode(30))
    sub_cat_2            = Column(Unicode(30))
    sub_cat_3            = Column(Unicode(300))
    material             = Column(Unicode(300))
    merchandise          = Column(Unicode(300))
    internal_item_code   = Column(Unicode(2000))
    item_code            = Column(Unicode(300))
    item_description     = Column(Unicode(2000))
    qty                  = Column(Integer)
    unit                 = Column(Unicode(30))
    unit_price           = Column(Numeric(17, 6), default=0)
    item_amount          = Column(Numeric(17, 6), default=0)
    item_base_amount     = Column(Numeric(17, 6), default=0) 
    so_no                = Column(Unicode(30))
    cust_code            = Column(Unicode(2000))
    billing_month        = Column(Unicode(30))
    a_single_date        = Column(Unicode(3000))
    private_brand_sub_line = Column(Unicode(30))
    kingdee_date           = Column(Unicode(30))
    flag                   = Column(Integer, default=0)
    payment_qty            = Column(Integer, default=0)
    date                = Column(Unicode(100))

class Table_DNCost(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_dn_cost'

    id = Column(Integer, primary_key = True)
    
    company_code             = Column(Unicode(30))
    stock_adjustment_no      = Column(Unicode(30))
    po_no                    = Column(Unicode(30))
    note_type                = Column(Unicode(30))
    other_ref                = Column(Unicode(30))
    note_remark              = Column(Unicode(2000))
    issue_date               = Column(DateTime)
    department               = Column(Unicode(300))
    supplier_code            = Column(Unicode(200))
    supplier_name            = Column(Unicode(200))
    sales_contact            = Column(Unicode(30))
    currency                 = Column(Unicode(30))
    note_amount              = Column(Numeric(17, 6), default=0) 
    note_base_amount         = Column(Numeric(17, 6), default=0) 
    corporate_customer       = Column(Unicode(30))   
    private_brand            = Column(Unicode(30))
    private_brand_sub_line   = Column(Integer) 
    packaging_category       = Column(Unicode(30))
    packaging_type           = Column(Unicode(30))
    sub_cat_2                = Column(Unicode(30))
    sub_cat_3                = Column(Unicode(30))
    material                 = Column(Unicode(30))
    merchandise              = Column(Unicode(300)) 
    internal_item_code       = Column(Unicode(2000)) 
    item_code                = Column(Unicode(200))
    item_description         = Column(Unicode(2000))
    order_qty                = Column(Integer)
    unit                     = Column(Unicode(30))
    unit_price               = Column(Numeric(17, 6), default=0) 
    amount                   = Column(Numeric(17, 6), default=0) 
    base_amount              = Column(Numeric(17, 6), default=0) 
    cost                     = Column(Unicode(30))    
    so_no                    = Column(Unicode(30))
    cust_code                = Column(Unicode(200))
    flag                     = Column(Integer, default=0)
    payment_qty              = Column(Integer, default=0)
    date                     = Column(Unicode(100))
    
class Table_Period(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_t_period'
    
    id = Column(Integer, primary_key = True)
    
    company_code             = Column(Unicode(30))
    piproceeds_qty    = Column(Integer, default=0)
    piproceeds_amount = Column(Numeric(17, 6), default=0)
    dnproceeds_qty    = Column(Integer, default=0)
    dnproceeds_amount = Column(Numeric(17, 6), default=0)
    picost_qty        = Column(Integer, default=0)
    picost_amount     = Column(Numeric(17, 6), default=0)
    dncost_qty        = Column(Integer, default=0)
    dncost_amount     = Column(Numeric(17, 6), default=0)
    date              = Column(Unicode(100))

class Table_PeriodDetaul(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_period_detail'
    
    id = Column(Integer, primary_key = True)
     
    company_code             = Column(Unicode(30))
    item_code         = Column(Unicode(300))
    piproceeds_qty    = Column(Integer, default=0)
    piproceeds_amount = Column(Numeric(17, 6), default=0)
    dnproceeds_qty    = Column(Integer, default=0)
    dnproceeds_amount = Column(Numeric(17, 6), default=0)
    picost_qty        = Column(Integer, default=0)
    picost_amount     = Column(Numeric(17, 6), default=0)
    dncost_qty        = Column(Integer, default=0)
    dncost_amount     = Column(Numeric(17, 6), default=0)
    date              = Column(Unicode(100))
    

class Table_ARLog(DeclarativeBase, SysMixin):
    __tablename__ = 'vat_t_arlog'

    id         = Column(Integer, primary_key = True)
    t_head_id  = Column(Integer, ForeignKey('vat_t_head.id'))
    t_head     = relation('THead', primaryjoin=t_head_id == Table_THead.id)
    c_head_id  = Column(Integer, ForeignKey('vat_c_head.id'))
    c_head     = relation('CHead', primaryjoin=c_head_id == Table_CHead.id)
    p_head_id  = Column(Integer, ForeignKey('vat_p_head.id'))
    p_head     = relation('PHead', primaryjoin=p_head_id == Table_PHead.id)
    s_head_id  = Column(Integer, ForeignKey('vat_s_head.id'))
    s_head     = relation('SHead', primaryjoin=s_head_id == Table_SHead.id)
    m_head_id  = Column(Integer, ForeignKey('vat_m_head.id'))
    m_head     = relation('MHead', primaryjoin = m_head_id == Table_MHead.id)
    
    o_head_id  = Column(Integer, ForeignKey('vat_o_head.id'))
    o_head     = relation('OHead', primaryjoin=o_head_id == Table_OHead.id)
    n_head_id  = Column(Integer, ForeignKey('vat_n_head.id'))
    n_head     = relation('NHead', primaryjoin=n_head_id == Table_NHead.id)
    
    detail_id   = Column(Integer)
    detail_type = Column(Unicode(100))
    action_type = Column(Unicode(100))
    remark      = Column(Unicode(20000))