Skip to content

Commit 0fd04b3

Browse files
committed
Add person to schema, load into transaction information
1 parent 208addc commit 0fd04b3

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

convert-co.php

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
(active = 't') active,
1515
(deleted = 't') deleted
1616
FROM co.item
17-
WHERE deleted = 'f' AND type = 2
17+
WHERE type = 2
1818
ON DUPLICATE KEY
1919
UPDATE code = VALUES(code),
2020
name = VALUES(name),
@@ -65,29 +65,59 @@
6565
$r= $db->query($q) or die("query failed: ". $db->error);
6666
echo "Loaded ", $db->affected_rows, " barcodes.<br>";
6767

68+
# PERSONS
69+
#
70+
$q= "INSERT INTO person (id, name, company, address, email, phone, tax_id,
71+
active, deleted)
72+
SELECT id,
73+
(SELECT REPLACE(REPLACE(value, '|', ' '), ' ', ' ') FROM co.metavalue WHERE id_item = item.id AND id_metatype = 2 ORDER BY id DESC LIMIT 1) name,
74+
(SELECT value FROM co.metavalue WHERE id_item = item.id AND id_metatype = 3 ORDER BY id DESC LIMIT 1) company,
75+
(SELECT value FROM co.metavalue WHERE id_item = item.id AND id_metatype = 5 ORDER BY id DESC LIMIT 1) address,
76+
(SELECT value FROM co.metavalue WHERE id_item = item.id AND id_metatype = 9 ORDER BY id DESC LIMIT 1) email,
77+
(SELECT value FROM co.metavalue WHERE id_item = item.id AND id_metatype = 8 ORDER BY id DESC LIMIT 1) phone,
78+
(SELECT value FROM co.metavalue WHERE id_item = item.id AND id_metatype = 6 ORDER BY id DESC LIMIT 1) tax_id,
79+
(active = 't') active,
80+
(deleted = 't') deleted
81+
FROM co.item
82+
WHERE type IN (1,3,6)
83+
ON DUPLICATE KEY
84+
UPDATE
85+
name = VALUES(name),
86+
company = VALUES(company),
87+
address = VALUES(address),
88+
email = VALUES(email),
89+
phone = VALUES(phone),
90+
active = VALUES(active),
91+
deleted = VALUES(deleted)";
92+
$r= $db->query($q) or die("query failed: ". $db->error);
93+
echo "Loaded ", $db->affected_rows, " people.<br>";
94+
6895
# TRANSACTIONS
6996
#
7097
$q= "TRUNCATE txn_line";
7198
$r= $db->query($q) or die("query failed: ". $db->error);
7299
echo "Flushed transaction lines.<br>";
73100

74101
# incomplete transactions
75-
$q= "INSERT IGNORE
76-
INTO txn (id, number, created, type)
102+
$q= "INSERT
103+
INTO txn (id, number, created, type, person)
77104
SELECT id AS id,
78105
IFNULL(number, 0) AS number,
79106
date AS created,
80107
CASE type
81108
WHEN 1 THEN 'customer'
82109
WHEN 2 THEN 'vendor'
83110
WHEN 3 THEN 'internal'
84-
END AS type
111+
END AS type,
112+
id_item AS person
85113
FROM co.request
86-
WHERE id_parent IS NULL";
114+
WHERE id_parent IS NULL
115+
ON DUPLICATE KEY
116+
UPDATE
117+
person = VALUES(person)";
87118
$r= $db->query($q) or die("query failed: ". $db->error);
88119
echo "Loaded ", $db->affected_rows, " incomplete transactions.<br>";
89120

90-
91121
# lines from requests (un-received items)
92122
#
93123
# needs the id offset to avoid collisions
@@ -106,8 +136,8 @@
106136
echo "Loaded ", $db->affected_rows, " transaction lines from incomplete orders.<br>";
107137

108138
# basics
109-
$q= "INSERT IGNORE
110-
INTO txn (id, number, created, type)
139+
$q= "INSERT
140+
INTO txn (id, number, created, type, person)
111141
SELECT id_request AS id,
112142
IFNULL(IF(type = 2,
113143
SUBSTRING_INDEX(formatted_request_number, '-', -1),
@@ -118,9 +148,13 @@
118148
WHEN 1 THEN 'customer'
119149
WHEN 2 THEN 'vendor'
120150
WHEN 3 THEN 'internal'
121-
END AS type
151+
END AS type,
152+
id_item AS person
122153
FROM co.transaction
123-
WHERE id_parent IS NULL";
154+
WHERE id_parent IS NULL
155+
ON DUPLICATE KEY
156+
UPDATE
157+
person = VALUES(person)";
124158
$r= $db->query($q) or die("query failed: ". $db->error);
125159
echo "Loaded ", $db->affected_rows, " transactions.<br>";
126160

scat.sql

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,27 @@ CREATE TABLE `item` (
6969
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
7070
/*!40101 SET character_set_client = @saved_cs_client */;
7171

72+
--
73+
-- Table structure for table `person`
74+
--
75+
76+
DROP TABLE IF EXISTS `person`;
77+
/*!40101 SET @saved_cs_client = @@character_set_client */;
78+
/*!40101 SET character_set_client = utf8 */;
79+
CREATE TABLE `person` (
80+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
81+
`name` varchar(255) DEFAULT NULL,
82+
`company` varchar(255) DEFAULT NULL,
83+
`address` text,
84+
`email` varchar(255) DEFAULT NULL,
85+
`phone` varchar(255) DEFAULT NULL,
86+
`tax_id` varchar(255) DEFAULT NULL,
87+
`active` bit(1) DEFAULT NULL,
88+
`deleted` bit(1) DEFAULT NULL,
89+
PRIMARY KEY (`id`)
90+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
91+
/*!40101 SET character_set_client = @saved_cs_client */;
92+
7293
--
7394
-- Table structure for table `txn`
7495
--
@@ -81,8 +102,8 @@ CREATE TABLE `txn` (
81102
`number` int(10) unsigned NOT NULL,
82103
`created` datetime NOT NULL,
83104
`type` enum('internal','vendor','customer') NOT NULL,
84-
PRIMARY KEY (`id`),
85-
UNIQUE KEY `type` (`type`,`number`)
105+
`person` int(10) unsigned DEFAULT NULL,
106+
PRIMARY KEY (`id`)
86107
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
87108
/*!40101 SET character_set_client = @saved_cs_client */;
88109

@@ -115,4 +136,4 @@ CREATE TABLE `txn_line` (
115136
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
116137
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
117138

118-
-- Dump completed on 2011-01-19 19:01:15
139+
-- Dump completed on 2011-02-07 18:13:32

0 commit comments

Comments
 (0)