~georgeyk/stoqlib/cezar-all

« back to all changes in this revision

Viewing changes to plugins/nfe/sql/patch-00-01.py

  • Committer: georgeyk
  • Date: 2009-09-18 17:57:53 UTC
  • mfrom: (3096.1.40 stoqlib)
  • Revision ID: georgeyk.dev@gmail.com-20090918175753-2vrs9r88erewyfi0
Added NF-e plugin and basic stoq integration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# vi:si:et:sw=4:sts=4:ts=4
 
3
 
 
4
from kiwi.environ import environ
 
5
 
 
6
from nfedomain import NFeCityData
 
7
from utils import remove_accentuation
 
8
 
 
9
def apply_patch(trans):
 
10
    trans.query("""CREATE TABLE nfe_city_data (
 
11
                        id bigserial NOT NULL PRIMARY KEY,
 
12
                        te_created_id bigint UNIQUE REFERENCES transaction_entry(id),
 
13
                        te_modified_id bigint UNIQUE REFERENCES transaction_entry(id),
 
14
 
 
15
                        state_code integer,
 
16
                        state_name text,
 
17
                        city_code integer,
 
18
                        city_name text);
 
19
    """)
 
20
 
 
21
    csv = environ.find_resource('nfecsv', 'dtb_brazilian_city_codes.csv')
 
22
    for line in open(csv, 'r').readlines():
 
23
        state_code, state_name, city_code, city_name = line.split(',')
 
24
        # the first line contain the titles, lets ignore it.
 
25
        if state_code == '"UF"':
 
26
            continue
 
27
 
 
28
        # in *_name attributes we remove the extra spaces and the double-quote
 
29
        # character.
 
30
        state_name = unicode(state_name.strip().strip('"'))
 
31
        city_name = unicode(city_name.strip().strip('"'))
 
32
        NFeCityData(state_code=int(state_code.strip('"')),
 
33
                    state_name=remove_accentuation(state_name),
 
34
                    city_code=int(city_code.strip('"')),
 
35
                    city_name=remove_accentuation(city_name),
 
36
                    connection=trans)
 
37
    trans.query("""CREATE INDEX nfe_city_name_state_code_idx  ON
 
38
                           nfe_city_data (city_name, state_code);""")
 
39
    trans.commit()