Permalink
Browse files

Add person to schema, load into transaction information

  • Loading branch information...
jimwins committed Feb 8, 2011
1 parent 208addc commit 0fd04b3ac11e5f760fb58a11239f0958d12dc42f
Showing with 68 additions and 13 deletions.
  1. +44 −10 convert-co.php
  2. +24 −3 scat.sql
@@ -14,7 +14,7 @@
(active = 't') active,
(deleted = 't') deleted
FROM co.item
WHERE deleted = 'f' AND type = 2
WHERE type = 2
ON DUPLICATE KEY
UPDATE code = VALUES(code),
name = VALUES(name),
@@ -65,29 +65,59 @@
$r= $db->query($q) or die("query failed: ". $db->error);
echo "Loaded ", $db->affected_rows, " barcodes.<br>";
# PERSONS
#
$q= "INSERT INTO person (id, name, company, address, email, phone, tax_id,
active, deleted)
SELECT id,
(SELECT REPLACE(REPLACE(value, '|', ' '), ' ', ' ') FROM co.metavalue WHERE id_item = item.id AND id_metatype = 2 ORDER BY id DESC LIMIT 1) name,
(SELECT value FROM co.metavalue WHERE id_item = item.id AND id_metatype = 3 ORDER BY id DESC LIMIT 1) company,
(SELECT value FROM co.metavalue WHERE id_item = item.id AND id_metatype = 5 ORDER BY id DESC LIMIT 1) address,
(SELECT value FROM co.metavalue WHERE id_item = item.id AND id_metatype = 9 ORDER BY id DESC LIMIT 1) email,
(SELECT value FROM co.metavalue WHERE id_item = item.id AND id_metatype = 8 ORDER BY id DESC LIMIT 1) phone,
(SELECT value FROM co.metavalue WHERE id_item = item.id AND id_metatype = 6 ORDER BY id DESC LIMIT 1) tax_id,
(active = 't') active,
(deleted = 't') deleted
FROM co.item
WHERE type IN (1,3,6)
ON DUPLICATE KEY
UPDATE
name = VALUES(name),
company = VALUES(company),
address = VALUES(address),
email = VALUES(email),
phone = VALUES(phone),
active = VALUES(active),
deleted = VALUES(deleted)";
$r= $db->query($q) or die("query failed: ". $db->error);
echo "Loaded ", $db->affected_rows, " people.<br>";
# TRANSACTIONS
#
$q= "TRUNCATE txn_line";
$r= $db->query($q) or die("query failed: ". $db->error);
echo "Flushed transaction lines.<br>";
# incomplete transactions
$q= "INSERT IGNORE
INTO txn (id, number, created, type)
$q= "INSERT
INTO txn (id, number, created, type, person)
SELECT id AS id,
IFNULL(number, 0) AS number,
date AS created,
CASE type
WHEN 1 THEN 'customer'
WHEN 2 THEN 'vendor'
WHEN 3 THEN 'internal'
END AS type
END AS type,
id_item AS person
FROM co.request
WHERE id_parent IS NULL";
WHERE id_parent IS NULL
ON DUPLICATE KEY
UPDATE
person = VALUES(person)";
$r= $db->query($q) or die("query failed: ". $db->error);
echo "Loaded ", $db->affected_rows, " incomplete transactions.<br>";
# lines from requests (un-received items)
#
# needs the id offset to avoid collisions
@@ -106,8 +136,8 @@
echo "Loaded ", $db->affected_rows, " transaction lines from incomplete orders.<br>";
# basics
$q= "INSERT IGNORE
INTO txn (id, number, created, type)
$q= "INSERT
INTO txn (id, number, created, type, person)
SELECT id_request AS id,
IFNULL(IF(type = 2,
SUBSTRING_INDEX(formatted_request_number, '-', -1),
@@ -118,9 +148,13 @@
WHEN 1 THEN 'customer'
WHEN 2 THEN 'vendor'
WHEN 3 THEN 'internal'
END AS type
END AS type,
id_item AS person
FROM co.transaction
WHERE id_parent IS NULL";
WHERE id_parent IS NULL
ON DUPLICATE KEY
UPDATE
person = VALUES(person)";
$r= $db->query($q) or die("query failed: ". $db->error);
echo "Loaded ", $db->affected_rows, " transactions.<br>";
@@ -69,6 +69,27 @@ CREATE TABLE `item` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `person`
--
DROP TABLE IF EXISTS `person`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `person` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`company` varchar(255) DEFAULT NULL,
`address` text,
`email` varchar(255) DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
`tax_id` varchar(255) DEFAULT NULL,
`active` bit(1) DEFAULT NULL,
`deleted` bit(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `txn`
--
@@ -81,8 +102,8 @@ CREATE TABLE `txn` (
`number` int(10) unsigned NOT NULL,
`created` datetime NOT NULL,
`type` enum('internal','vendor','customer') NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `type` (`type`,`number`)
`person` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
@@ -115,4 +136,4 @@ CREATE TABLE `txn_line` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2011-01-19 19:01:15
-- Dump completed on 2011-02-07 18:13:32

0 comments on commit 0fd04b3

Please sign in to comment.