6 |
Este script convierte una base (postgres) de PAPO 0.1.9 a 0.1.10. Se |
Este script convierte una base (postgres) de PAPO 0.1.9 a 0.1.10. Se |
7 |
utiliza de la siguiente manera: |
utiliza de la siguiente manera: |
8 |
|
|
9 |
$0 <base> [<opciones>...] |
$0 <base> |
10 |
|
|
11 |
donde \"<base>\" es el nombre de la base de datos a convertir. Como |
donde \"<base>\" es el nombre de la base de datos a convertir. Como |
12 |
siempre, NO HAY GARANTIA DE QUE NO SE PIERDAN DATOS, y recomendamos |
siempre, NO HAY GARANTIA DE QUE NO SE PIERDAN DATOS, y recomendamos |
19 |
fi |
fi |
20 |
|
|
21 |
BASE="$1" |
BASE="$1" |
22 |
shift |
OPTS="-q -A -t -h localhost" |
|
OPTS="$@ -A -t -h localhost" |
|
23 |
|
|
24 |
DB="psql $OPTS $BASE" |
DB="psql $OPTS $BASE" |
25 |
|
|
37 |
UPDATE _document_type_data SET code = 'DEV' where id = 21; |
UPDATE _document_type_data SET code = 'DEV' where id = 21; |
38 |
|
|
39 |
|
|
40 |
--los que tienen que convertir datos |
-- los que tienen que convertir datos |
41 |
|
|
42 |
|
|
43 |
------------------------------------------- |
-- ----------------------------------------- |
44 |
--creo la tabla pos |
-- creo la tabla pos |
45 |
|
|
46 |
CREATE TABLE own_pos ( |
CREATE TABLE own_pos ( |
47 |
id bigserial PRIMARY KEY |
id bigserial PRIMARY KEY |
60 |
ALTER TABLE _own_pos_data ADD CONSTRAINT _table_fk FOREIGN KEY (_table) REFERENCES own_pos; |
ALTER TABLE _own_pos_data ADD CONSTRAINT _table_fk FOREIGN KEY (_table) REFERENCES own_pos; |
61 |
ALTER TABLE _own_pos_data ADD CONSTRAINT own_entity_fk FOREIGN KEY (own_entity) REFERENCES own_entity; |
ALTER TABLE _own_pos_data ADD CONSTRAINT own_entity_fk FOREIGN KEY (own_entity) REFERENCES own_entity; |
62 |
|
|
63 |
--renombro la tabla branch_document -> own_pos_document |
-- renombro la tabla branch_document -> own_pos_document |
64 |
|
|
65 |
ALTER TABLE branch_document RENAME TO own_pos_document; |
ALTER TABLE branch_document RENAME TO own_pos_document; |
66 |
ALTER TABLE _branch_document_data RENAME TO _own_pos_document_data; |
ALTER TABLE _branch_document_data RENAME TO _own_pos_document_data; |
68 |
ALTER TABLE _own_pos_document_data ADD COLUMN own_pos bigint REFERENCES entity; |
ALTER TABLE _own_pos_document_data ADD COLUMN own_pos bigint REFERENCES entity; |
69 |
ALTER TABLE _own_pos_document_data RENAME COLUMN branch TO DEPRECATED_branch; |
ALTER TABLE _own_pos_document_data RENAME COLUMN branch TO DEPRECATED_branch; |
70 |
|
|
71 |
--cargo un punto de venta |
-- cargo un punto de venta |
72 |
|
|
73 |
insert into entity (id) values (nextval('entity_id_seq')); |
INSERT INTO entity (id) VALUES (nextval('entity_id_seq')); |
74 |
insert into _entity_data (id,_table,_start_t,name) values (nextval('_entity_data_id_seq'),currval('entity_id_seq'),current_timestamp,'punto de ventapor defecto'); |
INSERT INTO _entity_data (_table, _start_t, name ) |
75 |
insert into own_entity (id) values (nextval('own_entity_id_seq')); |
VALUES (currval('entity_id_seq'), current_timestamp, 'punto de venta por defecto'); |
|
insert into _entity_data (id,_table,_start_t,entity) values (nextval('_own_entity_data_id_seq'),currval('own_entity_id_seq'),current_timestamp,currval('entity_id_seq')); |
|
|
insert into own_pos (id) values (nextval('own_pos_id_seq')); |
|
|
insert into _own_pos_data (id,_table,_start_t,number,stock_by_invoice,own_entity) values (nextval('_own_pos_data_id_seq'),currval('own_pos_id_seq'),current_timestamp,true,currval('own_entity_id_seq')); |
|
76 |
|
|
77 |
--les doy el punto de venta por defecto; |
INSERT INTO own_entity (id) VALUES (nextval('own_entity_id_seq')); |
78 |
|
INSERT INTO _own_entity_data (_table, _start_t, entity ) |
79 |
|
VALUES (currval('own_entity_id_seq'), current_timestamp, currval('entity_id_seq')); |
80 |
|
|
81 |
UPDATE _own_pos_data SET own_pos = currval('entity_id_seq') ; |
INSERT INTO own_pos (id) values (nextval('own_pos_id_seq')); |
82 |
|
INSERT INTO _own_pos_data (_table, _start_t, number, stock_by_invoice, own_entity) |
83 |
|
VALUES (currval('own_pos_id_seq'), current_timestamp, 'uno', true, currval('own_entity_id_seq')); |
84 |
|
|
85 |
|
-- carga un storehouse |
86 |
|
|
87 |
|
INSERT INTO entity (id) VALUES (nextval('entity_id_seq')); |
88 |
|
INSERT INTO _entity_data (_table, _start_t, name ) |
89 |
|
VALUES (currval('entity_id_seq'), current_timestamp, 'depósito por defecto'); |
90 |
|
|
91 |
|
INSERT INTO own_entity (id) VALUES (nextval('own_entity_id_seq')); |
92 |
|
INSERT INTO _own_entity_data (_table, _start_t, entity ) |
93 |
|
VALUES (currval('own_entity_id_seq'), current_timestamp, currval('entity_id_seq')); |
94 |
|
|
95 |
|
INSERT INTO own_storehouse (id) values (nextval('own_storehouse_id_seq')); |
96 |
|
INSERT INTO _own_storehouse_data (_table, _start_t, own_entity) |
97 |
|
VALUES (currval('own_storehouse_id_seq'), current_timestamp, currval('own_entity_id_seq')); |
98 |
|
|
99 |
|
-- les doy el punto de venta por defecto; |
100 |
|
|
101 |
|
UPDATE _own_pos_document_data SET own_pos = currval('entity_id_seq') ; |
102 |
|
|
103 |
ALTER TABLE own_document ADD COLUMN net_total numeric(12,4); |
ALTER TABLE own_document ADD COLUMN net_total numeric(12,4); |
104 |
ALTER TABLE own_document ADD COLUMN tax_total numeric(12,4); |
ALTER TABLE own_document ADD COLUMN tax_total numeric(12,4); |
108 |
ALTER TABLE own_document ADD COLUMN due_date timestamp with time zone; |
ALTER TABLE own_document ADD COLUMN due_date timestamp with time zone; |
109 |
ALTER TABLE alien_document ADD COLUMN due_date timestamp with time zone; |
ALTER TABLE alien_document ADD COLUMN due_date timestamp with time zone; |
110 |
|
|
111 |
|
|
112 |
|
-- en 0.1.9, había estas cosas? |
113 |
|
|
114 |
|
CREATE TABLE relation_types (id serial primary key); |
115 |
|
CREATE TABLE _relation_types_data (id bigserial primary key, entity_relation int not null references entity_relation, relation_type int not null references relation_type, |
116 |
|
_start_t timestamp not null, _end_t timestamp, _table int not null references relation_types); |
117 |
|
|
118 |
|
|
119 |
|
-- en 0.1.9 no se usaban las relations |
120 |
|
|
121 |
|
INSERT INTO relation_type VALUES (1); |
122 |
|
INSERT INTO _relation_type_data ("_start_t","_end_t","name",_table) VALUES (current_timestamp,NULL,'branch_of_organization',1); |
123 |
|
INSERT INTO relation_type VALUES (2); |
124 |
|
INSERT INTO _relation_type_data ("_start_t","_end_t","name",_table) VALUES (current_timestamp,NULL,'pos_of_branch',2); |
125 |
|
INSERT INTO relation_type VALUES (3); |
126 |
|
INSERT INTO _relation_type_data ("_start_t","_end_t","name",_table) VALUES (current_timestamp,NULL,'storehouse_of_branch',3); |
127 |
|
INSERT INTO relation_type VALUES (4); |
128 |
|
INSERT INTO _relation_type_data ("_start_t","_end_t","name",_table) VALUES (current_timestamp,NULL,'pos_of_storehouse',4); |
129 |
|
|
130 |
|
-- ni entity_type tenía parent |
131 |
|
|
132 |
|
ALTER TABLE _entity_type_data ADD COLUMN parent bigint REFERENCES entity_type; |
133 |
|
|
134 |
|
EOF |
135 |
|
|
136 |
|
RES=$( $DB -c "SELECT setval('entity_type_id_seq', max(id)) FROM entity_type;" ) |
137 |
|
if [ "$RES" != "10" ]; then |
138 |
|
cat <<EOF |
139 |
|
*** ADVERTENCIA *** |
140 |
|
entity_type es una tabla del sistema, y tiene algo raro! |
141 |
|
Esperábamos que el máximo id fuera de 10, pero en vez es |
142 |
|
$RES |
143 |
|
lo cual nos llama poderosamente la atención! |
144 |
|
|
145 |
|
EOF |
146 |
|
read -p "Desea proseguir? [Y/n] " ans |
147 |
|
case $ans in |
148 |
|
n*|N*) |
149 |
|
exit |
150 |
|
;; |
151 |
|
*) |
152 |
|
;; |
153 |
|
esac |
154 |
|
fi |
155 |
|
|
156 |
|
|
157 |
|
for type in {,own_,alien_}entity organization own_pos; do |
158 |
|
$DB <<EOF |
159 |
|
INSERT INTO entity_type VALUES (nextval('entity_type_id_seq')); |
160 |
|
INSERT INTO _entity_type_data (_start_t, name, _table) |
161 |
|
VALUES (current_timestamp, '$type', currval('entity_type_id_seq')); |
162 |
|
EOF |
163 |
|
done |
164 |
|
|
165 |
|
for type in `$DB -c 'SELECT name FROM _entity_type_data'`; do |
166 |
|
parent="" |
167 |
|
case $type in |
168 |
|
client|provider|bank*) |
169 |
|
parent=persona |
170 |
|
;; |
171 |
|
*_entity) |
172 |
|
parent=entity |
173 |
|
;; |
174 |
|
employee|own_*|organization) |
175 |
|
parent=own_entity |
176 |
|
;; |
177 |
|
alien_*|contact|persona) |
178 |
|
parent=alien_entity |
179 |
|
;; |
180 |
|
*) |
181 |
|
;; |
182 |
|
esac |
183 |
|
|
184 |
|
if [ $parent ]; then |
185 |
|
$DB <<EOF |
186 |
|
UPDATE _entity_type_data SET parent =y._table |
187 |
|
FROM _entity_type_data AS x, _entity_type_data AS y |
188 |
|
WHERE x.name='$type' |
189 |
|
AND x.id=_entity_type_data.id |
190 |
|
AND y.name='$parent' |
191 |
|
AND x._end_t IS NULL |
192 |
|
AND y._end_t IS NULL |
193 |
|
AND _entity_type_data._end_t IS NULL |
194 |
|
EOF |
195 |
|
fi |
196 |
|
done |
197 |
|
|
198 |
|
function id() { |
199 |
|
$DB -c "SELECT _entity_data._table FROM _entity_data, _own_entity_data, _$1_data WHERE _entity_data._end_t IS NULL AND _own_entity_data._end_t IS NULL AND _$1_data._end_t IS NULL AND _entity_data._table = _own_entity_data.entity AND _own_entity_data._table = _$1_data.own_entity;" |
200 |
|
} |
201 |
|
|
202 |
|
MY_BRANCH=`id own_branch` |
203 |
|
MY_STOREHOUSE=`id own_storehouse` |
204 |
|
MY_POS=`id own_pos` |
205 |
|
|
206 |
|
# la papelera tenía mal estas... |
207 |
|
$DB <<EOF |
208 |
|
UPDATE _document_type_data SET name='Nota de Credito de proveedor A', code='NCP' WHERE _table=18; |
209 |
|
UPDATE _document_type_data SET name='Nota de Credito de proveedor B', code='NCP' WHERE _table=19; |
210 |
|
UPDATE _document_type_data SET name='Nota de Credito de proveedor C', code='NCP' WHERE _table=20; |
211 |
|
UPDATE _document_type_data SET name='Devolución de mercadería', code='DEV' WHERE _table=21; |
212 |
|
|
213 |
|
DELETE FROM _product_store_data; |
214 |
|
DELETE FROM product_store; |
215 |
|
SELECT setval('product_store_id_seq', 1, false); |
216 |
|
SELECT setval('_product_store_data_id_seq', 1, false); |
217 |
|
EOF |
218 |
|
|
219 |
|
$DB -c "select product, sum(qty) from alien_product_item, alien_item, alien_document where alien_document.id = alien_item.alien_document and alien_product_item.alien_item = alien_item.id and alien_document.alien_document_type=2 group by product" | tr '|' ' ' | |
220 |
|
while read product level; do |
221 |
|
cat <<EOF |
222 |
|
INSERT INTO product_store VALUES (nextval('product_store_id_seq')); |
223 |
|
INSERT INTO _product_store_data (level, _start_t, _table, product, storehouse) |
224 |
|
SELECT $level, '2002-01-01', currval('product_store_id_seq'), _product_data._table, $MY_STOREHOUSE |
225 |
|
FROM _product_data WHERE id=$product; |
226 |
|
|
227 |
|
EOF |
228 |
|
done | $DB |
229 |
|
|
230 |
|
$DB -c "UPDATE own_document SET own_entity=$MY_POS" |
231 |
|
$DB -c "UPDATE alien_document SET own_entity=$MY_POS" |
232 |
|
|
233 |
|
$DB <<EOF |
234 |
|
INSERT INTO entity_relation VALUES (nextval('entity_relation_id_seq')); |
235 |
|
INSERT INTO _entity_relation_data (_start_t, _table, entity_from, entity_to) |
236 |
|
VALUES ('2002-01-01', currval('entity_relation_id_seq'), $MY_STOREHOUSE, $MY_BRANCH); |
237 |
|
|
238 |
|
INSERT INTO relation_types VALUES (nextval('relation_types_id_seq')); |
239 |
|
INSERT INTO _relation_types_data (_start_t, _table, relation_type, entity_relation) |
240 |
|
SELECT '2002-01-01', currval('relation_types_id_seq'), _table, currval('entity_relation_id_seq') |
241 |
|
FROM _relation_type_data WHERE name = 'storehouse_of_branch' AND _end_t IS NULL; |
242 |
|
|
243 |
|
INSERT INTO entity_relation VALUES (nextval('entity_relation_id_seq')); |
244 |
|
INSERT INTO _entity_relation_data (_start_t, _table, entity_from, entity_to) |
245 |
|
VALUES ('2002-01-01', currval('entity_relation_id_seq'), $MY_POS, $MY_BRANCH); |
246 |
|
|
247 |
|
INSERT INTO relation_types VALUES (nextval('relation_types_id_seq')); |
248 |
|
INSERT INTO _relation_types_data (_start_t, _table, relation_type, entity_relation) |
249 |
|
SELECT '2002-01-01', currval('relation_types_id_seq'), _table, currval('entity_relation_id_seq') |
250 |
|
FROM _relation_type_data WHERE name = 'pos_of_branch' AND _end_t IS NULL; |
251 |
|
|
252 |
|
INSERT INTO entity_relation VALUES (nextval('entity_relation_id_seq')); |
253 |
|
INSERT INTO _entity_relation_data (_start_t, _table, entity_from, entity_to) |
254 |
|
VALUES ('2002-01-01', currval('entity_relation_id_seq'), $MY_POS, $MY_STOREHOUSE); |
255 |
|
|
256 |
|
INSERT INTO relation_types VALUES (nextval('relation_types_id_seq')); |
257 |
|
INSERT INTO _relation_types_data (_start_t, _table, relation_type, entity_relation) |
258 |
|
SELECT '2002-01-01', currval('relation_types_id_seq'), _table, currval('entity_relation_id_seq') |
259 |
|
FROM _relation_type_data WHERE name = 'pos_of_storehouse' AND _end_t IS NULL; |
260 |
EOF |
EOF |
261 |
|
|
262 |
|
|
|
gfcvs 0.1.9-0.1.10.gfd |
|
263 |
|
$DB -c "SELECT alien_document, round(sum(amount),2) AS tax_total, round(sum(qty*unit_price), 2) AS net_total FROM alien_product_item, alien_item, alien_item_tax WHERE alien_product_item.alien_item = alien_item.id AND alien_item_tax.alien_item = alien_item.id GROUP BY alien_document;" | tr "|" " " | |
264 |
|
while read id tax_total net_total; do |
265 |
|
echo "UPDATE alien_document SET tax_total=$tax_total, net_total=$net_total WHERE id=$id;" |
266 |
|
done | $DB |