-- Table structure for table `shipmentDetail`
--

CREATE TABLE `shipmentDetail` (
  `SHIPID` int NOT NULL,
  `SHIPLINE` int NOT NULL AUTO_INCREMENT,
  `ORDERID` int NOT NULL,
  `ORDERLINE` int NOT NULL,
  `SHIPQTY` int NOT NULL,
  PRIMARY KEY (`SHIPLINE`)
) ENGINE=InnoDB AUTO_INCREMENT=665 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

--
-- Dumping data for table `shipmentDetail`
--

INSERT INTO `shipmentDetail` VALUES (1,1,3,3,200),(1,2,3,4,5),(1,3,3,5,250),(1,4,3,6,30),(1,5,3,7,30),(1,6,3,8,250),(2,7,7,16,250),(2,8,7,17,250),(2,9,7,18,1),(2,10,7,19,250),(2,11,7,20,250),(3,12,11,23,250),(4,13,12,25,500),(5,14,6,12,250),(5,15,6,13,250),(5,16,6,14,500),(5,17,6,15,500),(7,18,14,27,500),(7,19,14,28,250),(7,20,14,29,200),(7,21,14,30,500),(7,22,14,31,250),(8,23,15,32,250),(8,24,15,33,250),(9,25,16,34,500),(10,26,4,9,500),(10,27,4,10,1000),(11,28,17,35,500),(11,29,17,36,500),(12,30,20,40,250),(13,31,21,41,250),(13,32,21,42,250),(13,33,21,43,1000),(14,34,22,44,200),(14,35,22,45,250),(14,36,22,46,250),(14,37,22,47,150),(14,38,22,48,5),(14,39,22,49,200),(14,40,22,51,10),(14,41,22,52,200),(14,42,22,53,250),(14,43,22,54,500),(14,44,22,55,250),(14,45,22,56,1000),(15,46,23,57,500),(16,47,23,57,500),(17,48,19,38,250),(17,49,19,39,500),(18,50,19,38,250),(18,51,19,39,500),(19,52,25,65,200),(19,53,25,66,500),(19,54,25,67,500),(19,55,25,68,1000),(19,56,25,69,250),(19,57,25,70,250),(20,58,26,71,500),(21,59,27,72,250),(21,60,27,73,250),(21,61,27,74,250),(21,62,27,75,250),(21,63,27,76,250),(21,64,27,77,250),(22,65,28,78,250),(23,66,30,80,500),(24,67,31,81,250),(25,68,32,82,250),(26,69,36,96,1000),(27,70,37,97,250),(28,71,39,101,200),(28,72,39,102,250),(29,73,40,104,1000),(29,74,40,105,1000),(29,75,40,106,1000),(29,76,40,107,1000),(29,77,40,108,1000),(29,78,40,109,1000),(30,79,41,110,1000),(30,80,41,111,1000),(30,81,41,113,1000),(31,82,38,98,250),(31,83,38,99,250),(32,84,34,88,250),(32,85,34,89,250),(32,86,34,90,1000),(32,87,34,91,250),(32,88,34,92,100),(32,89,34,93,250),(33,90,44,116,500),(33,91,44,117,250),(34,92,45,118,500),(35,93,35,94,500),(36,94,46,119,1000),(36,95,46,120,500),(37,96,49,127,150),(37,97,49,128,250),(37,98,49,129,5),(38,99,50,133,500),(38,100,50,134,250),(38,101,50,135,500),(38,102,50,136,250),(38,103,50,137,100),(38,104,50,138,200),(38,105,50,139,500),(38,106,50,140,1000),(39,107,52,142,250),(40,108,51,141,500),(41,109,53,144,1000),(41,110,53,145,1000),(42,111,55,147,250),(42,112,55,148,250),(43,113,56,149,1000),(44,114,57,150,500),(44,115,57,151,500),(45,116,58,152,200),(45,117,58,153,250),(45,118,58,154,100),(45,119,58,155,250),(46,120,59,156,100),(47,121,62,160,1000),(47,122,62,161,250),(48,123,61,158,500),(48,124,61,159,250),(49,125,64,169,1000),(49,126,64,170,500),(49,127,64,171,500),(49,128,64,172,500),(49,129,64,173,500),(49,130,64,174,1000),(50,131,63,162,500),(50,132,63,163,250),(50,133,63,164,200),(50,134,63,165,250),(50,135,63,166,500),(50,136,63,167,5),(50,137,63,168,1000),(51,138,66,177,250),(52,139,70,187,100),(53,140,69,183,250),(53,141,69,184,250),(53,142,69,185,250),(53,143,69,186,50),(54,144,71,188,250),(55,145,65,175,250),(55,146,65,176,250),(56,147,72,189,100),(57,148,73,190,250),(58,149,74,191,150),(59,150,75,192,250),(60,151,76,194,500),(61,152,76,194,500),(62,153,78,200,1000),(62,154,78,201,1000),(63,155,80,203,250),(63,156,80,204,100),(63,157,80,205,1000),(63,158,80,206,250),(64,159,84,211,100),(64,160,84,212,100),(65,161,83,210,150),(66,162,85,213,100),(67,163,87,217,250),(68,164,86,214,250),(68,165,86,215,250),(68,166,86,216,250),(69,167,88,218,500),(70,168,77,195,50),(70,169,77,196,100),(70,170,77,197,100),(70,171,77,198,250),(70,172,77,199,250),(71,173,89,219,250),(71,174,89,220,250),(71,175,89,221,250),(72,176,90,222,100),(73,177,91,223,150),(74,178,94,228,250),(74,179,94,229,500),(74,180,94,230,100),(75,181,95,231,500),(75,182,95,232,500),(76,183,96,233,250),(77,184,97,234,250),(77,185,97,235,500),(78,186,92,224,250),(78,187,92,225,250),(78,188,92,226,250),(79,189,100,239,1000),(80,190,101,240,500),(80,191,101,241,500),(80,192,101,242,500),(81,193,102,243,250),(82,194,103,244,500),(83,195,104,245,5),(83,196,104,246,250),(83,197,104,247,100),(83,198,104,248,2500),(84,199,105,249,250),(84,200,105,250,250),(84,201,105,251,20),(84,202,105,252,250),(84,203,105,253,250),(84,204,105,254,250),(84,205,105,255,250),(84,206,105,256,250),(85,207,111,275,750),(86,208,112,276,250),(87,209,113,277,500),(87,210,113,278,500),(87,211,113,279,500),(87,212,113,280,250),(88,213,114,281,500),(89,214,115,282,500),(89,215,115,283,250),(89,216,115,284,500),(89,217,115,285,100),(90,218,116,287,500),(91,219,119,290,100),(92,220,126,301,250),(93,221,124,297,250),(93,222,124,298,250),(94,223,123,294,250),(94,224,123,295,250),(94,225,123,296,250),(95,226,125,299,250),(95,227,125,300,250),(96,228,129,313,1000),(96,229,129,314,500),(96,230,129,315,1000),(96,231,129,317,1000),(96,232,129,318,1000),(96,233,129,319,1000),(96,234,129,320,250),(96,235,129,321,1000),(97,236,120,291,250),(98,237,132,325,500),(99,238,109,269,250),(99,239,109,270,250),(99,240,109,271,250),(99,241,109,272,250),(99,242,109,273,250),(100,243,134,327,250),(101,244,133,326,500),(102,245,136,332,250),(103,246,135,328,250),(103,247,135,329,250),(103,248,135,330,250),(103,249,135,331,250),(104,250,137,333,100),(105,251,138,334,500),(106,252,139,335,500),(107,253,140,336,500),(107,254,140,337,500),(108,255,141,338,250),(108,256,141,339,250),(108,257,141,340,250),(109,258,142,341,250),(109,259,142,342,250),(109,260,142,343,250),(110,261,143,344,250),(110,262,143,345,250),(111,263,145,347,250),(112,264,144,346,500),(113,265,146,348,500),(114,266,147,349,500),(114,267,147,350,500),(115,268,150,356,250),(116,269,151,357,250),(117,270,149,352,500),(118,271,152,358,1000),(119,272,148,351,250),(119,273,148,353,250),(119,274,148,354,250),(119,275,148,355,250),(120,276,153,359,2500),(120,277,153,360,250),(121,278,154,361,250),(121,279,154,362,100),(122,280,155,363,250),(122,281,155,364,250),(122,282,155,365,250),(123,283,156,366,250),(124,284,157,367,250),(125,285,158,368,1000),(126,286,159,369,250),(127,287,161,371,500),(127,288,161,372,250),(128,289,166,379,250),(129,290,167,380,100),(130,291,168,381,250),(131,292,164,376,250),(132,293,171,386,250),(132,294,171,387,500),(133,295,172,388,10),(133,296,172,389,250),(134,297,173,390,250),(134,298,173,391,250),(135,299,175,393,250),(136,300,170,383,250),(136,301,170,384,100),(136,302,170,385,250),(137,303,174,392,250),(138,304,176,394,250),(139,305,177,395,500),(139,306,177,396,500),(139,307,177,397,500),(139,308,177,398,500),(139,309,177,399,150),(140,310,179,401,250),(140,311,179,402,250),(140,312,179,403,250),(140,313,179,404,250),(140,314,179,405,250),(140,315,179,406,250),(141,316,180,407,250),(142,317,178,400,250),(143,318,182,409,250),(144,319,181,408,150),(145,320,185,417,250),(145,321,185,418,250),(145,322,185,419,250),(145,323,185,420,250),(146,324,183,410,250),(146,325,183,411,250),(146,326,183,412,250),(146,327,183,413,250),(146,328,183,414,250),(147,329,184,415,250),(147,330,184,416,250),(148,331,186,421,250),(149,332,190,427,10),(150,333,193,438,50),(150,334,193,439,250),(150,335,193,440,250),(150,336,193,441,250),(150,337,193,442,100),(150,338,193,443,50),(150,339,193,444,50),(150,340,193,445,50),(151,341,187,424,500),(152,342,198,450,250),(152,343,198,451,500),(153,344,196,448,250),(154,345,202,455,250),(155,346,200,453,750),(156,347,204,459,500),(157,348,205,460,250),(157,349,205,461,250),(157,350,205,462,250),(158,351,201,454,500),(159,352,203,456,1000),(159,353,203,457,1000),(159,354,203,458,100),(160,355,206,463,200),(161,356,207,464,500),(161,357,207,465,500),(161,358,207,466,500),(162,359,208,467,500),(163,360,209,468,500),(164,361,210,469,250),(165,362,211,470,250),(166,363,212,471,750),(167,364,214,473,1000),(167,365,214,474,1000),(167,366,214,475,1000),(168,367,216,477,500),(169,368,213,472,250),(170,369,217,478,250),(171,370,215,476,1000),(172,371,218,479,5),(173,372,225,487,500),(174,373,226,488,250),(174,374,226,489,250),(175,375,228,491,250),(175,376,228,492,250),(176,377,230,494,1000),(177,378,229,493,500),(178,379,232,497,500),(179,380,231,495,500),(179,381,231,496,500),(180,382,227,490,150),(181,383,233,498,500),(182,384,235,500,500),(182,385,235,501,250),(183,386,238,505,250),(183,387,238,506,250),(184,388,236,502,200),(184,389,236,503,250),(185,390,240,508,50),(186,391,241,509,1000),(187,392,234,499,500),(188,393,242,510,500),(189,394,243,511,1000),(189,395,243,512,250),(189,396,243,513,500),(189,397,243,514,500),(189,398,243,515,500),(189,399,243,516,500),(189,400,243,517,50),(189,401,243,518,250),(190,402,245,521,500),(190,403,245,522,1000),(191,404,246,523,250),(192,405,247,524,250),(193,406,251,532,250),(194,407,250,529,250),(194,408,250,530,500),(194,409,250,531,250),(195,410,249,526,250),(195,411,249,527,250),(196,412,255,539,500),(197,413,257,543,50),(198,414,259,545,100),(198,415,259,546,250),(198,416,259,547,50),(199,417,260,548,500),(200,418,256,540,30),(200,419,256,541,250),(200,420,256,542,250),(201,421,258,544,250),(202,422,262,552,250),(202,423,262,553,250),(203,424,261,549,250),(203,425,261,550,250),(203,426,261,551,100),(204,427,263,554,250),(204,428,263,555,250),(205,429,265,558,150),(206,430,267,562,500),(207,431,269,564,500),(207,432,269,565,500),(208,433,271,567,250),(209,434,273,569,500),(210,435,275,571,250),(210,436,275,572,250),(210,437,275,573,250),(210,438,275,575,250),(211,439,274,570,250),(212,440,276,576,150),(213,441,277,577,250),(213,442,277,578,250),(214,443,281,590,250),(215,444,279,580,250),(215,445,279,581,250),(215,446,279,582,250),(215,447,279,583,250),(215,448,279,584,250),(215,449,279,585,250),(215,450,279,586,250),(215,451,279,587,250),(216,452,280,588,250),(217,453,282,591,500),(218,454,283,593,250),(218,455,283,594,250),(218,456,283,595,250),(219,457,285,597,250),(219,458,285,598,100),(219,459,285,599,250),(219,460,285,600,500),(220,461,286,601,90),(220,462,286,602,500),(220,463,286,603,500),(220,464,286,604,50),(220,465,286,605,100),(221,466,289,609,250),(221,467,289,610,250),(221,468,289,611,250),(222,469,290,612,250),(223,470,291,613,500),(224,471,292,614,250),(225,472,293,615,250),(226,473,294,616,250),(226,474,294,617,250),(226,475,294,618,250),(226,476,294,619,250),(227,477,296,622,500),(228,478,297,623,250),(228,479,297,624,250),(228,480,297,625,250),(229,481,298,626,50),(229,482,298,627,250),(230,483,303,633,250),(230,484,303,634,250),(230,485,303,636,0),(231,486,311,647,250),(231,487,311,648,500),(232,488,316,655,20),(232,489,316,656,100),(232,490,316,657,250),(233,491,315,652,250),(233,492,315,653,250),(233,493,315,654,20),(234,494,319,663,1000),(235,495,321,665,500),(235,496,321,666,500),(235,497,321,668,250),(236,498,318,660,500),(236,499,318,661,250),(236,500,318,662,250),(237,501,317,658,100),(237,502,317,659,250),(238,503,323,670,250),(239,504,330,682,250),(240,505,328,678,250),(241,506,329,679,1000),(241,507,329,680,50),(241,508,329,681,150),(242,509,332,685,250),(243,510,336,691,250),(244,511,337,692,500),(245,512,333,686,250),(245,513,333,687,250),(245,514,333,688,250),(246,515,331,683,250),(246,516,331,684,250),(247,517,342,700,150),(247,518,342,701,500),(247,519,342,702,500),(248,520,346,711,250),(249,521,348,713,200),(250,522,347,712,250),(251,523,341,699,250),(252,524,345,708,100),(252,525,345,709,250),(252,526,345,710,250),(253,527,350,715,300),(254,528,351,716,500),(254,529,351,717,10),(254,530,351,718,100),(254,531,351,719,500),(254,532,351,720,1000),(254,533,351,721,1000),(254,534,351,722,1000),(255,535,339,694,250),(255,536,339,695,250),(255,537,339,696,250),(255,538,339,697,250),(256,539,353,724,250),(256,540,353,725,250),(257,541,354,726,500),(258,542,355,727,500),(259,543,356,728,250),(259,544,356,729,500),(260,545,358,732,250),(261,546,359,733,100),(261,547,359,734,250),(261,548,359,735,250),(262,549,360,736,500),(262,550,360,737,500),(262,551,360,738,90),(262,552,360,739,250),(262,553,360,740,250),(262,554,360,741,250),(263,555,363,744,100),(263,556,363,745,250),(264,557,367,752,250),(265,558,366,748,100),(265,559,366,749,50),(265,560,366,750,250),(265,561,366,751,250),(266,562,364,746,1000),(267,563,368,753,250),(268,564,375,762,500),(268,565,375,763,200),(269,566,379,773,250),(270,567,377,766,250),(270,568,377,767,250),(271,569,380,774,1000),(272,570,378,770,500),(272,571,378,771,250),(272,572,378,772,250),(273,573,386,782,250),(274,574,382,776,1000),(275,575,387,783,250),(276,576,388,784,300),(277,577,389,786,500),(277,578,389,787,500),(277,579,389,788,250),(278,580,383,777,250),(279,581,390,789,250),(279,582,390,790,250),(279,583,390,791,250),(280,584,392,793,250),(281,585,395,799,500),(281,586,395,800,500),(282,587,396,801,250),(283,588,393,794,100),(283,589,393,795,100),(284,590,397,802,250),(285,591,400,807,500),(286,592,399,806,250),(287,593,402,809,100),(288,594,394,796,750),(288,595,394,797,250),(288,596,394,798,750),(289,597,405,813,500),(290,598,408,820,250),(290,599,408,821,250),(290,600,408,822,250),(291,601,403,810,500),(292,602,410,824,150),(293,603,406,814,250),(294,604,411,825,250),(295,605,414,831,500),(295,606,414,832,500),(295,607,414,833,500),(296,608,413,827,50),(296,609,413,828,250),(296,610,413,829,250),(296,611,413,830,150),(297,612,412,826,50),(298,613,418,839,250),(298,614,418,840,250),(299,615,416,835,250),(300,616,420,844,250),(300,617,420,845,250),(300,618,420,847,250),(300,619,420,848,250),(301,620,421,849,250),(302,621,424,858,250),(302,622,424,859,250),(302,623,424,860,250),(303,624,428,869,1000),(304,625,427,868,50),(305,626,426,865,500),(305,627,426,866,250),(305,628,426,867,250),(306,629,429,870,250),(306,630,429,871,1000),(306,631,429,872,150),(306,632,429,873,500),(307,633,432,881,50),(307,634,432,882,125),(308,635,433,883,250),(308,636,433,884,125),(308,637,433,885,250),(309,638,431,877,100),(309,639,431,879,125),(309,640,431,880,250),(310,641,436,888,500),(311,642,434,886,1000),(312,643,438,890,500),(313,644,430,874,250),(313,645,430,875,250),(313,646,430,876,250),(314,647,440,896,150),(314,648,440,897,5),(315,649,439,891,250),(315,650,439,892,1000),(315,651,439,894,250),(315,652,439,895,250),(316,653,443,902,250),(316,654,443,903,250),(317,655,441,898,1000),(317,656,441,899,1000),(317,657,441,900,500),(318,658,444,904,10),(319,659,446,906,250),(319,660,446,907,250),(319,661,446,908,250),(320,662,447,909,250),(320,663,447,910,250),(321,664,445,905,250);

--
-- Table structure for table `shipmentHeader`
--

CREATE TABLE `shipmentHeader` (
  `SHIPID` int NOT NULL AUTO_INCREMENT,
  `ORDERID` int NOT NULL,
  `TRACKNUM` varchar(100) COLLATE utf8mb3_unicode_ci NOT NULL,
  `SHIPDATE` datetime NOT NULL,
  `SHIPMETHOD` varchar(100) COLLATE utf8mb3_unicode_ci NOT NULL,
  `SHIPAMOUNT` decimal(16,2) NOT NULL,
  `SHIPWEIGHT` decimal(16,4) NOT NULL,
  `SHIPPIECES` int NOT NULL,
  `SHIPINSTRUCTIONS` varchar(1000) COLLATE utf8mb3_unicode_ci NOT NULL,
  PRIMARY KEY (`SHIPID`)
) ENGINE=InnoDB AUTO_INCREMENT=322 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

--
-- Dumping data for table `shipmentHeader`
--

INSERT INTO `shipmentHeader` VALUES (1,3,'D576002250000023697001','2019-10-10 19:25:42','Canpar Ground',0.00,0.0000,5,''),(2,7,'D576002250000023775001','2019-10-17 14:44:29','Canpar Ground',0.00,0.0000,2,''),(3,11,'D576002250000023779001','2019-10-17 14:44:48','Canpar Ground',0.00,0.0000,1,''),(4,12,'D576002250000023855001','2019-10-24 20:02:19','Canpar Ground',0.00,0.0000,1,''),(5,6,'D576002250000023854001','2019-10-24 20:02:34','Canpar Ground',0.00,0.0000,3,''),(6,6,'D576002250000023889001','2019-10-24 20:02:50','Canpar Ground',0.00,0.0000,6,''),(7,14,'D576002250000023889001','2019-10-24 20:03:02','Canpar Ground',0.00,0.0000,6,''),(8,15,'D576002250000024052001','2019-10-29 19:40:20','Canpar Ground',0.00,0.0000,1,''),(9,16,'D576002250000024050001','2019-10-29 19:40:30','Canpar Ground',0.00,0.0000,1,''),(10,4,'D576002250000024049001','2019-10-29 19:40:39','Canpar Ground',0.00,0.0000,1,''),(11,17,'D576002250000024583001','2019-11-26 14:43:56','Canpar Ground',0.00,0.0000,1,''),(12,20,'D576002250000024619001','2019-11-26 14:44:11','Canpar Ground',0.00,0.0000,1,''),(13,21,'D576002250000024674001','2019-11-28 16:10:29','Canpar Ground',0.00,0.0000,3,''),(14,22,'D576002250000025035001','2019-12-16 14:30:50','Canpar Ground',0.00,0.0000,5,''),(15,23,'D576002250000024930001','2020-01-09 15:21:41','Canpar Ground',0.00,0.0000,1,''),(16,23,'D576002250000024930001','2020-01-09 15:21:45','Canpar Ground',0.00,0.0000,1,''),(17,19,'D576002250000025180001','2020-01-09 15:22:21','Canpar Ground',0.00,0.0000,1,''),(18,19,'D576002250000025180001','2020-01-09 15:22:25','Canpar Ground',0.00,0.0000,1,''),(19,25,'D576002250000025193001','2020-01-09 15:22:46','Canpar Ground',0.00,0.0000,7,''),(20,26,'D576002250000025192001','2020-01-09 15:23:08','Canpar Ground',0.00,0.0000,1,''),(21,27,'D576002250000025379001','2020-01-09 15:24:27','Canpar Ground',0.00,0.0000,3,''),(22,28,'D576002250000025371001','2020-01-09 15:24:45','Canpar Ground',0.00,0.0000,1,''),(23,30,'D576002250000025538001','2020-01-31 20:47:16','Canpar Ground',0.00,0.0000,1,''),(24,31,'D576002250000025646001','2020-01-31 20:47:26','Canpar Ground',0.00,0.0000,1,''),(25,32,'D576002250000025868001','2020-02-04 17:22:27','Canpar Ground',0.00,0.0000,1,''),(26,36,'606683914109','2020-02-19 20:39:05','Purolator Express',0.00,0.0000,1,''),(27,37,'606683914117','2020-02-19 20:39:24','Purolator Express',0.00,0.0000,1,''),(28,39,'D576002250000026103001','2020-02-26 21:11:26','Canpar Ground',0.00,0.0000,1,''),(29,40,'606683914539','2020-02-26 21:12:13','Purolator Express',0.00,0.0000,2,''),(30,41,'606683914497','2020-02-26 21:12:27','Purolator Express',0.00,0.0000,1,''),(31,38,'D576002250000026134001','2020-02-28 19:36:21','Canpar Ground',0.00,0.0000,1,''),(32,34,'D576002250000026202001','2020-03-09 20:11:59','Canpar Ground',0.00,0.0000,2,''),(33,44,'D576002250000026666001','2020-03-10 19:18:51','Canpar Ground',0.00,0.0000,2,''),(34,45,'D576002250000026666001','2020-03-10 19:19:06','Canpar Ground',0.00,0.0000,2,''),(35,35,'D576002250000026665001','2020-03-10 19:20:05','Canpar Ground',0.00,0.0000,1,''),(36,46,'606683915148','2020-03-16 20:35:14','Purolator Express',0.00,0.0000,2,''),(37,49,'D576002250000027526001','2020-07-15 14:09:07','Canpar Ground',0.00,0.0000,2,''),(38,50,'606683919793','2020-08-05 17:40:25','Purolator Express',0.00,0.0000,5,''),(39,52,'D576002250000028330001','2020-09-18 10:59:49','Canpar Ground',0.00,0.0000,0,''),(40,51,'D576002250000028374001','2020-09-21 17:11:08','Canpar Ground',0.00,0.0000,1,''),(41,53,'D576002250000028526001','2020-10-07 16:25:01','Canpar Ground',0.00,0.0000,3,''),(42,55,'D576002250000028683001','2020-10-30 17:05:39','Canpar Ground',0.00,0.0000,1,''),(43,56,'607255681837','2020-11-09 14:54:16','Purolator Express',0.00,0.0000,2,''),(44,57,'D576002250000028842001','2020-11-09 14:54:26','Canpar Ground',0.00,0.0000,1,''),(45,58,'D576002250000028966001','2020-11-23 10:45:13','Canpar Ground',0.00,0.0000,1,''),(46,59,'607417543925','2020-11-26 16:19:42','Purolator Express',0.00,0.0000,1,''),(47,62,'607439637739','2020-12-09 15:34:50','Purolator Express',0.00,0.0000,1,''),(48,61,'D576002250000029174001','2020-12-11 16:43:15','Canpar Ground',0.00,0.0000,1,''),(49,64,'607439642895','2020-12-23 14:56:57','Purolator Express',0.00,0.0000,2,''),(50,63,'D576002250000029222001','2021-01-08 12:33:43','Canpar Ground',0.00,0.0000,7,''),(51,66,'D576002250000029395001','2021-01-25 16:59:05','Canpar Ground',0.00,0.0000,1,''),(52,70,'D576002250000029542001','2021-02-08 17:21:13','Canpar Ground',0.00,0.0000,1,''),(53,69,'D576002250000029819001','2021-02-22 15:28:40','Canpar Ground',0.00,0.0000,1,''),(54,71,'D576002250000030513001','2021-03-15 17:05:00','Canpar Ground',0.00,0.0000,1,''),(55,65,'D576002250000030574001','2021-03-22 16:18:33','Canpar Ground',0.00,0.0000,1,''),(56,72,'D576002250000030565001','2021-03-22 16:18:50','Canpar Ground',0.00,0.0000,1,''),(57,73,'D576002250000030651001','2021-03-29 16:37:01','Canpar Ground',0.00,0.0000,1,''),(58,74,'D576002250000030765001','2021-03-31 17:32:19','Canpar Ground',0.00,0.0000,1,''),(59,75,'D576002250000031020001','2021-05-10 18:00:12','Canpar Ground',0.00,0.0000,1,''),(60,76,'D576002250000031092001','2021-05-19 16:09:37','Canpar Ground',0.00,0.0000,1,''),(61,76,'D576002250000031092001','2021-05-19 16:09:47','Canpar Ground',0.00,0.0000,1,''),(62,78,'D576002250000031189001','2021-06-02 16:02:14','Canpar Ground',0.00,0.0000,1,''),(63,80,'607676976857','2021-06-28 11:19:19','Express',0.00,0.0000,1,''),(64,84,'D57600225 000 003 168 3001','2021-08-11 11:42:49','Ground',0.00,0.0000,1,''),(65,83,'D57600225 000 003 168 8001','2021-08-11 12:42:17','Ground',0.00,0.0000,1,''),(66,85,'D57600225 000 003 169 4001','2021-08-13 09:21:43','Ground',0.00,0.0000,1,''),(67,87,'D57600225 000 003 172 4001','2021-08-20 12:06:22','Ground',0.00,0.0000,1,''),(68,86,'D57600225 000 003 173 8001','2021-08-23 12:23:07','Ground',0.00,0.0000,1,''),(69,88,'D57600225 000 003 173 7001','2021-08-23 12:24:45','Ground',0.00,0.0000,1,''),(70,77,'D57600225 000 003 187 3001','2021-09-08 13:10:05','Ground',0.00,0.0000,2,''),(71,89,'D57600225 000 003 187 2001','2021-09-08 13:11:03','Ground',0.00,0.0000,3,''),(72,90,'D57600225 000 003 188 0001','2021-09-08 14:03:57','Ground',0.00,0.0000,1,''),(73,91,'D57600225 000 003 190 8001','2021-09-10 11:13:56','Ground',0.00,0.0000,1,''),(74,94,'D57600225 000 003 200 1001','2021-09-22 16:38:37','Ground',0.00,0.0000,2,''),(75,95,'D57600225 000 003 199 3001','2021-09-22 16:42:08','Ground',0.00,0.0000,1,''),(76,96,'D57600225 000 003 202 5001','2021-09-27 10:14:27','Ground',0.00,0.0000,1,''),(77,97,'D57600225 000 003 206 9001','2021-10-04 11:13:16','Ground',0.00,0.0000,1,''),(78,92,'D57600225 000 003 214 7001','2021-10-15 15:45:54','Ground',0.00,0.0000,1,''),(79,100,'D57600225 000 003 220 3001','2021-10-25 11:13:20','Ground',0.00,0.0000,1,''),(80,101,'607872767894','2021-10-27 14:08:26','Express',0.00,0.0000,1,''),(81,102,'D57600225 000 003 237 6001','2021-11-10 14:02:53','Ground',0.00,0.0000,1,''),(82,103,'607908160346','2021-11-15 12:13:45','Express',0.00,0.0000,1,''),(83,104,'607908160429','2021-11-17 14:24:06','Ground',0.00,0.0000,2,''),(84,105,'607908160528','2021-11-19 15:36:08','Express',0.00,0.0000,5,''),(85,111,'D57600225 000 003 261 6001','2021-12-03 14:35:34','Ground',0.00,0.0000,1,''),(86,112,'D57600225 000 003 265 8001','2021-12-10 15:49:15','Ground',0.00,0.0000,1,''),(87,113,'607941276653','2021-12-13 15:24:46','Express',0.00,0.0000,1,''),(88,114,'D57600225 000 003 272 1001','2021-12-21 10:10:55','Ground',0.00,0.0000,1,''),(89,115,'D57600225 000 003 272 8001','2021-12-22 13:01:49','Ground',0.00,0.0000,2,''),(90,116,'D57600225 000 003 274 2001','2021-12-23 15:54:02','Ground',0.00,0.0000,1,''),(91,119,'D57600225 000 003 283 5001','2022-01-14 16:01:12','Ground',0.00,0.0000,1,''),(92,126,'D57600225 000 003 301 7001','2022-02-22 11:52:27','Ground',0.00,0.0000,1,''),(93,124,'608019611862','2022-02-22 11:56:13','Express',0.00,0.0000,1,''),(94,123,'D57600225 000 003 301 8001','2022-02-22 11:59:54','Ground',0.00,0.0000,1,''),(95,125,'D57600225 000 003 304 3001','2022-02-24 10:59:46','Ground',0.00,0.0000,1,''),(96,129,'608054997879','2022-03-24 15:00:03','Express',0.00,0.0000,8,''),(97,120,'D57600225 000 003 328 1001','2022-03-29 14:16:09','Ground',0.00,0.0000,1,''),(98,132,'608055003032','2022-03-31 16:19:51','Ground',0.00,0.0000,1,''),(99,109,'D57600225 000 003 336 2001','2022-04-14 14:26:07','Ground',0.00,0.0000,1,''),(100,134,'D57600225 000 003 336 4001','2022-04-14 15:43:01','Ground',0.00,0.0000,1,''),(101,133,'D57600225 000 003 337 6001','2022-04-19 11:28:03','Ground',0.00,0.0000,1,''),(102,136,'D57600225 000 003 343 8001','2022-04-26 09:30:46','Ground',0.00,0.0000,1,''),(103,135,'D57600225 000 003 345 4001','2022-04-27 14:42:29','Ground',0.00,0.0000,1,''),(104,137,'D57600225 000 003 346 2001','2022-04-28 15:01:04','Ground',0.00,0.0000,4,''),(105,138,'D57600225 000 003 369 5001','2022-05-06 10:20:07','Ground',0.00,0.0000,1,''),(106,139,'D57600225 000 003 372 7001','2022-05-13 11:14:00','Ground',0.00,0.0000,1,''),(107,140,'D57600225 000 003 380 9001','2022-05-30 10:55:28','Ground',0.00,0.0000,1,''),(108,141,'608123605719','2022-05-30 11:18:19','Purolator Express',0.00,0.0000,1,''),(109,142,'D57600225 000 003 382 3001','2022-06-01 11:11:42','Ground',0.00,0.0000,1,''),(110,143,'608123608390','2022-06-06 10:24:41','Purolator Express',0.00,0.0000,1,''),(111,145,'D57600225 000 003 393 4001','2022-06-15 10:20:52','Ground',0.00,0.0000,1,''),(112,144,'608123612004','2022-06-15 11:51:16','Purolator Express',0.00,0.0000,1,''),(113,146,'D57600225 000 003 395 1001','2022-06-17 10:33:13','Ground',0.00,0.0000,1,''),(114,147,'D57600225 000 003 397 7001','2022-06-22 12:14:25','Ground',0.00,0.0000,1,''),(115,150,'D57600225 000 003 404 4001','2022-06-29 10:07:14','Ground',0.00,0.0000,1,''),(116,151,'D57600225 000 003 404 5001','2022-06-29 10:11:14','Ground',0.00,0.0000,1,''),(117,149,'D57600225 000 003 404 6001','2022-06-29 10:15:35','Ground',0.00,0.0000,1,''),(118,152,'D57600225 000 003 405 1001','2022-06-29 10:39:08','Ground',0.00,0.0000,1,''),(119,148,'608163996085','2022-06-29 11:57:19','Purolator Express',0.00,0.0000,1,''),(120,153,'D57600225 000 003 424 1001','2022-07-13 11:49:03','Ground',0.00,0.0000,1,''),(121,154,'D57600225 000 003 428 3001','2022-07-20 10:39:19','Ground',0.00,0.0000,1,''),(122,155,'D57600225 000 003 439 6001','2022-08-05 11:43:55','Ground',0.00,0.0000,1,''),(123,156,'D576002250000034433001','2022-08-11 14:30:06','Canpar Ground',0.00,0.0000,1,''),(124,157,'D576002250000034442001','2022-08-12 15:24:20','Canpar Ground',0.00,0.0000,1,''),(125,158,'D576002250000034442001','2022-08-12 15:24:51','Canpar Ground',0.00,0.0000,1,''),(126,159,'D57600225 000 003 459 3001','2022-09-02 11:24:23','Ground',0.00,0.0000,1,''),(127,161,'D57600225 000 003 468 6001','2022-09-16 10:28:53','Ground',0.00,0.0000,1,''),(128,166,'608261574644','2022-09-28 12:24:04','Purolator Express',0.00,0.0000,1,''),(129,167,'D57600225 000 003 475 9001','2022-09-28 12:47:14','Ground',0.00,0.0000,1,''),(130,168,'608261582498','2022-10-06 11:17:35','Purolator Express',0.00,0.0000,1,''),(131,164,'608285810123','2022-10-14 10:47:44','Purolator Express',0.00,0.0000,1,''),(132,171,'D57600225 000 003 498 6001','2022-10-26 10:40:53','Ground',0.00,0.0000,1,''),(133,172,'608313911679','2022-11-04 10:53:18','Purolator Ground',0.00,0.0000,1,''),(134,173,'608313915209 (LEAD PIN)','2022-11-10 15:11:25','Purolator Express',0.00,0.0000,11,''),(135,175,'L57600225 000 000 011 8001','2022-11-23 12:51:12','Canpar Express',0.00,0.0000,1,''),(136,170,'D57600225 000 003 526 9001','2022-11-30 15:09:52','Ground',0.00,0.0000,1,''),(137,174,'D57600225 000 003 527 0001-02','2022-11-30 15:19:58','Ground',0.00,0.0000,2,''),(138,176,'608340468933','2022-12-12 14:44:59','Purolator Express',0.00,0.0000,1,''),(139,177,'608340473560/578/586','2022-12-19 15:17:47','Purolator Express',0.00,0.0000,3,''),(140,179,'D57600225 000 003 546 6001','2023-01-06 12:19:32','Ground',0.00,0.0000,2,''),(141,180,'D57600225 000 003 547 3001','2023-01-06 14:05:13','Ground',0.00,0.0000,1,''),(142,178,'D57600225 000 003 548 3001','2023-01-06 15:15:22','Ground',0.00,0.0000,1,''),(143,182,'NA','2023-01-18 11:07:15','CP Delivery',0.00,0.0000,1,'Test order'),(144,181,'D57600225 000 003 556 3001','2023-01-20 15:10:59','Ground',0.00,0.0000,1,''),(145,185,'D57600225 000 003 559 4001','2023-01-27 10:12:05','Ground',0.00,0.0000,1,''),(146,183,'608390791499','2023-01-27 10:19:34','Purolator Express',0.00,0.0000,1,''),(147,184,'D57600225 000 003 559 5001','2023-01-27 10:25:41','Ground',0.00,0.0000,1,''),(148,186,'D57600225 000 003 564 1001','2023-02-01 12:19:48','Ground',0.00,0.0000,1,''),(149,190,'14761374619461','2023-02-03 10:49:18','Ground',0.00,0.0000,0,''),(150,193,'D57600225 000 003 570 5001','2023-02-13 11:01:46','Ground',0.00,0.0000,1,'Shipped w/ A04476'),(151,187,'D57600225 000 003 578 9001','2023-02-22 13:24:43','Ground',0.00,0.0000,1,''),(152,198,'D57600225 000 003 579 0001','2023-02-22 13:29:15','Ground',0.00,0.0000,1,''),(153,196,'608422472514','2023-03-01 10:19:39','Purolator Express',0.00,0.0000,1,''),(154,202,'D57600225 000 003 584 1001','2023-03-03 13:37:43','Ground',0.00,0.0000,1,''),(155,200,'608422473744','2023-03-03 13:47:06','Purolator Express',0.00,0.0000,1,''),(156,204,'D 57600225 000 003 590 6001','2023-03-14 13:28:08','Canpar Ground',0.00,0.0000,1,''),(157,205,'D 57600225 000 003 590 5001','2023-03-14 13:28:51','Canpar Ground',0.00,0.0000,1,''),(158,201,'D 57600225 000 003 590 4001','2023-03-14 13:29:33','Canpar Ground',0.00,0.0000,1,''),(159,203,'608422478057, 040','2023-03-22 10:33:57','Purolator Express',0.00,0.0000,2,''),(160,206,'608422484980','2023-03-24 13:58:13','Purolator Express',0.00,0.0000,1,''),(161,207,'608456228014','2023-04-05 13:43:12','Purolator Express',0.00,0.0000,1,''),(162,208,'608456228600','2023-04-12 13:51:48','Purolator Express',0.00,0.0000,1,''),(163,209,'608456228618','2023-04-12 13:52:20','Purolator Express',0.00,0.0000,1,''),(164,210,'D57600225 000 003 634 5001','2023-04-17 10:25:22','Purolator Ground',0.00,0.0000,1,''),(165,211,'608485327324','2023-05-05 10:30:45','Purolator Express',0.00,0.0000,1,''),(166,212,'D57600225 000 003 668 3001','2023-05-12 15:48:30','Canpar Ground',0.00,0.0000,1,''),(167,214,'D57600225 000 003 674 2001','2023-05-17 14:03:30','Canpar Ground',0.00,0.0000,1,''),(168,216,'608501717888','2023-05-24 11:28:24','Purolator Express',0.00,0.0000,1,''),(169,213,'D57600225 000 003 677 5001','2023-05-24 12:08:19','Canpar Ground',0.00,0.0000,1,''),(170,217,'D57600225 000 003 680 5001','2023-05-29 11:02:32','Canpar Ground',0.00,0.0000,1,''),(171,215,'D57600225 000 003 681 5001','2023-05-29 11:56:05','Canpar Ground',0.00,0.0000,1,''),(172,218,'D57600225 000 003 682 9001','2023-05-31 13:54:31','Canpar Ground',0.00,0.0000,1,''),(173,225,'608542744586','2023-07-19 15:00:18','Purolator Express',0.00,0.0000,1,''),(174,226,'D57600225 000 003 708 3001','2023-07-19 15:04:51','Canpar Ground',0.00,0.0000,1,''),(175,228,'D57600225 000 003 721 4001','2023-08-04 14:19:29','Canpar Ground',0.00,0.0000,1,''),(176,230,'D57600225 000 003 733 2001','2023-08-21 11:56:50','Canpar Ground',0.00,0.0000,1,''),(177,229,'D57600225 000 003 733 3001','2023-08-21 12:11:16','Canpar Ground',0.00,0.0000,1,''),(178,232,'D57600225 000 003 752 2001','2023-09-13 11:26:00','Canpar Ground',0.00,0.0000,1,''),(179,231,'608613197771','2023-09-18 12:36:45','Purolator Express',0.00,0.0000,1,''),(180,227,'608613202738','2023-09-18 13:41:49','Purolator Ground',0.00,0.0000,1,''),(181,233,'D576002250000037590001','2023-09-28 13:10:40','Canpar Ground',0.00,0.0000,1,''),(182,235,'D57600225 000 003 784 8001-2','2023-10-25 13:55:11','Canpar Ground',0.00,0.0000,2,''),(183,238,'608632575593','2023-10-27 12:03:14','Purolator Express',0.00,0.0000,1,''),(184,236,'D57600225 000 003 787 1001','2023-10-30 12:16:43','Canpar Ground',0.00,0.0000,1,''),(185,240,'608653204636','2023-11-13 10:47:02','Purolator Express',0.00,0.0000,1,''),(186,241,'608653204644','2023-11-13 10:47:58','Purolator Ground',0.00,0.0000,1,''),(187,234,'D57600225 000 003 801 6001','2023-11-15 13:04:07','Canpar Ground',0.00,0.0000,1,''),(188,242,'D57600225 000 003 804 1001','2023-11-17 11:09:27','Canpar Ground',0.00,0.0000,1,''),(189,243,'608666019112 / 608666019104','2023-11-30 15:13:50','Purolator Express',26.82,0.0000,2,''),(190,245,'D576002250000038165001','2023-12-07 11:24:13','Canpar Ground',0.00,0.0000,1,''),(191,246,'608688769405','2023-12-11 14:01:55','Purolator Ground',0.00,0.0000,1,''),(192,247,'D57600225 000 003 817 5001','2023-12-11 14:07:16','Canpar Ground',0.00,0.0000,1,''),(193,251,'D57600225 000 003 830 0001','2023-12-22 13:52:44','Canpar Ground',0.00,0.0000,1,''),(194,250,'608688769793','2023-12-22 14:01:07','Purolator Express',0.00,0.0000,1,''),(195,249,'D57600225 000 003 830 1001','2023-12-22 14:05:54','Canpar Ground',0.00,0.0000,1,''),(196,255,'D57600225 000 003 840 9001','2024-01-10 13:51:27','Canpar Ground',0.00,0.0000,1,''),(197,257,'D57600225 000 003 844 0001','2024-01-15 14:32:02','Canpar Ground',0.00,0.0000,1,''),(198,259,'D57600225 000 003 844 4001','2024-01-15 14:53:52','Canpar Ground',0.00,0.0000,1,''),(199,260,'D57600225 000 003 845 9001','2024-01-17 12:09:02','Canpar Ground',0.00,0.0000,1,''),(200,256,'D57600225 000 003 846 0001','2024-01-17 12:17:38','Canpar Ground',0.00,0.0000,1,''),(201,258,'608688779875','2024-01-17 12:24:18','Purolator Express',0.00,0.0000,1,''),(202,262,'608720427111','2024-02-02 15:28:16','Purolator Express',0.00,0.0000,1,''),(203,261,'D57600225 000 003 862 9001','2024-02-02 16:09:44','Canpar Ground',0.00,0.0000,1,''),(204,263,'608720427327','2024-02-07 14:05:52','Purolator Express',0.00,0.0000,1,''),(205,265,'608720429018','2024-02-26 15:27:01','Purolator Ground',0.00,0.0000,1,''),(206,267,'D57600225 000 003 880 6001','2024-02-26 15:28:36','Canpar Ground',0.00,0.0000,1,''),(207,269,'608755671328','2024-03-06 13:50:49','Purolator Express',0.00,0.0000,1,''),(208,271,'D57600225 000 003 909 4001','2024-03-25 13:29:21','Canpar Ground',0.00,0.0000,1,''),(209,273,'608755677630','2024-04-03 13:08:28','Purolator Express',0.00,0.0000,1,''),(210,275,'D57600225 000 003 921 3001','2024-04-08 12:47:26','Canpar Ground',0.00,0.0000,1,''),(211,274,'608790905137','2024-04-08 12:57:59','Purolator Ground',0.00,0.0000,1,''),(212,276,'608790905129','2024-04-08 12:58:33','Purolator Express',0.00,0.0000,1,''),(213,277,'D57600225 000 003 926 8001','2024-04-15 13:46:10','Canpar Ground',0.00,0.0000,1,''),(214,281,'608790910699','2024-04-24 13:39:26','Purolator Express',0.00,0.0000,1,''),(215,279,'608790910681','2024-04-24 13:39:57','Purolator Express',0.00,0.0000,1,''),(216,280,'608790910673','2024-04-24 13:40:18','Ground',0.00,0.0000,1,''),(217,282,'D57600225 000 003 950 0001','2024-04-26 16:06:13','Canpar Ground',0.00,0.0000,1,''),(218,283,'608790912661','2024-04-29 13:20:25','Purolator Express',0.00,0.0000,1,''),(219,285,'608790918221/39/47','2024-05-08 12:27:57','Purolator Ground',0.00,0.0000,3,''),(220,286,'608818457889/897','2024-05-13 13:15:11','Purolator Express',0.00,0.0000,2,''),(221,289,'D57600225 000 003 963 0001','2024-05-15 13:57:01','Canpar Ground',0.00,0.0000,1,''),(222,290,'D576002250000039672001 - D576002250000039672002','2024-05-21 10:36:04','Ground',0.00,0.0000,2,''),(223,291,'608818460156','2024-05-22 14:17:32','Purolator Express',0.00,0.0000,1,''),(224,292,'D57600225 000 003 998 3001','2024-06-05 11:37:19','Canpar Ground',0.00,0.0000,1,''),(225,293,'D57600225 000 004 004 3001','2024-06-12 12:19:00','Canpar Ground',0.00,0.0000,1,''),(226,294,'D57600225 000 004 011 3001','2024-06-17 11:52:36','Canpar Ground',0.00,0.0000,1,''),(227,296,'608848831400','2024-06-19 14:53:57','Purolator Express',0.00,0.0000,1,''),(228,297,'D57600225 000 004 016 6001','2024-06-28 14:16:07','Canpar Ground',0.00,0.0000,1,''),(229,298,'608890492838','2024-08-02 15:51:30','Purolator Ground',0.00,0.0000,1,''),(230,303,'D576002250000040503001','2024-08-22 16:00:00','Canpar Ground',0.00,0.0000,1,''),(231,311,'D576002250000040534001','2024-08-23 18:01:17','Canpar Ground',0.00,0.0000,1,''),(232,316,'608912495793','2024-08-27 15:48:21','Purolator Ground',0.00,0.0000,1,''),(233,315,'D576002250000040565001','2024-08-29 17:49:18','Ground',0.00,0.0000,1,''),(234,319,'D576002250000040572001','2024-08-30 14:18:39','Canpar Ground',0.00,0.0000,1,''),(235,321,'D576002250000040571001','2024-08-30 14:20:24','Canpar Ground',0.00,0.0000,1,''),(236,318,'D576002250000040570001','2024-08-30 14:28:18','Canpar Ground',0.00,0.0000,1,''),(237,317,'D57600225 000 004 058 9001','2024-08-30 17:54:06','Canpar Ground',0.00,0.0000,1,''),(238,323,'D57600225 000 004 067 1001','2024-09-13 15:06:02','Canpar Ground',0.00,0.0000,1,''),(239,330,'608912499621','2024-09-16 20:01:19','Purolator Ground',0.00,0.0000,1,''),(240,328,'D57600225 000 004 071 6001','2024-09-18 14:56:24','Canpar Ground',0.00,0.0000,1,''),(241,329,'608912499761','2024-09-18 18:15:10','Purolator Ground',0.00,0.0000,1,''),(242,332,'608912500774','2024-09-25 14:35:46','Purolator Ground',0.00,0.0000,1,''),(243,336,'608912501681','2024-10-07 16:42:19','Purolator Ground',0.00,0.0000,2,''),(244,337,'D57600225 000 004 088 4001','2024-10-09 16:19:37','Canpar Ground',0.00,0.0000,1,'Shipped to: Head office, 3845 BATHURST STREET, SUITE 206, Toronto, On, M3H3N2'),(245,333,'D57600225 000 004 088 8001','2024-10-11 16:09:45','Canpar Ground',0.00,0.0000,1,''),(246,331,'D57600225 000 004 088 9001','2024-10-11 16:16:18','Canpar Ground',0.00,0.0000,1,''),(247,342,'608912502820','2024-11-18 15:49:42','Purolator Express',0.00,0.0000,1,''),(248,346,'D57600225 000 004 149 7001','2024-11-27 17:05:03','Canpar Ground',0.00,0.0000,1,''),(249,348,'608987740339','2024-12-02 16:46:38','Purolator Ground',0.00,0.0000,1,''),(250,347,'608987740370','2024-12-04 15:51:02','Purolator Express',0.00,0.0000,1,''),(251,341,'D57600225 000 004 180 9001','2024-12-04 15:53:58','Canpar Ground',0.00,0.0000,1,''),(252,345,'D57600225 000 004 181 0001','2024-12-04 16:06:14','Canpar Ground',0.00,0.0000,1,''),(253,350,'608987741071','2024-12-13 19:26:08','Purolator Express',0.00,0.0000,1,''),(254,351,'608987741493/85/77/69','2024-12-20 16:06:53','Purolator Express',0.00,0.0000,4,''),(255,339,'D57600225 000 004 188 9001','2024-12-20 16:16:08','Canpar Ground',0.00,0.0000,1,''),(256,353,'6085987741576','2025-01-08 18:02:55','Purolator Express',0.00,0.0000,1,''),(257,354,'D57600225 000 004 190 1001','2025-01-08 18:51:39','Canpar Ground',0.00,0.0000,1,'Shipped to: 3845 BATHURST STREET, SUITE 206, Toronto, On, M3H 3N2'),(258,355,'D57600225 000 004 190 2001','2025-01-08 18:57:26','Canpar Ground',0.00,0.0000,1,''),(259,356,'D57600225 000 004 191 9001-2','2025-01-10 17:34:38','Canpar Ground',0.00,0.0000,2,''),(260,358,'D57600225 000 004 192 0001','2025-01-10 17:38:36','Canpar Ground',0.00,0.0000,1,''),(261,359,'608987741725','2025-01-10 17:48:18','Purolator Express',0.00,0.0000,1,''),(262,360,'D57600225 000 004 194 6001','2025-01-13 18:38:58','Canpar Ground',0.00,0.0000,1,''),(263,363,'608987742517','2025-01-24 18:01:00','Purolator Express',0.00,0.0000,1,''),(264,367,'D576002250000042221001','2025-02-05 19:14:02','Canpar Ground',0.00,0.0000,1,'Shipped to: 477 Millwood Rd, Toronto, On, M4S 1K4'),(265,366,'D57600225 000 004 224 8001','2025-02-07 18:44:25','Canpar Ground',0.00,0.0000,1,''),(266,364,'608987746724','2025-02-10 19:29:54','Purolator Express',0.00,0.0000,1,''),(267,368,'L57600225 000 000 012 0001','2025-02-12 19:13:18','Canpar Express',0.00,0.0000,1,''),(268,375,'609062382104','2025-03-10 18:54:21','Purolator Express',0.00,0.0000,1,''),(269,379,'D57600225 000 004 246 8001','2025-03-19 16:11:55','Canpar Ground',0.00,0.0000,1,''),(270,377,'D57600225 000 004 247 1001','2025-03-19 17:50:43','Canpar Ground',0.00,0.0000,1,''),(271,380,'D57600225 000 004 248 4001','2025-03-21 18:59:51','Canpar Ground',0.00,0.0000,1,''),(272,378,'609062386725','2025-04-07 15:38:56','Purolator Express',0.00,0.0000,1,''),(273,386,'609062391634','2025-05-05 14:50:29','Purolator Express',0.00,0.0000,1,''),(274,382,'D57600225 000 004 278 4001-2','2025-05-05 14:54:28','Canpar Ground',0.00,0.0000,2,''),(275,387,'609062392145','2025-05-12 18:50:38','Purolator Ground',0.00,0.0000,1,''),(276,388,'609062392152','2025-05-12 19:12:48','Purolator Express',0.00,0.0000,1,''),(277,389,'D576002250000042988001','2025-05-15 17:46:35','Canpar Ground',0.00,0.0000,1,''),(278,383,'D576002250000043008001','2025-05-21 16:41:18','Canpar Ground',0.00,0.0000,1,''),(279,390,'609062392442','2025-05-26 18:40:06','Purolator Express',0.00,0.0000,1,''),(280,392,'609128231550','2025-06-02 17:51:57','Purolator Ground',0.00,0.0000,1,''),(281,395,'609128236146','2025-06-13 14:36:39','Purolator Express',0.00,0.0000,1,''),(282,396,'609128236138','2025-06-13 14:37:16','Purolator Ground',0.00,0.0000,1,''),(283,393,'609128236153','2025-06-13 15:44:54','Purolator Ground',0.00,0.0000,1,''),(284,397,'D57600225 000 004 320 4001','2025-06-18 14:47:32','Canpar Ground',0.00,0.0000,1,''),(285,400,'D57600225 000 004 326 3001','2025-06-27 16:24:21','Canpar Ground',0.00,0.0000,1,''),(286,399,'D57600225 000 004 326 4001','2025-06-27 16:27:45','Canpar Ground',0.00,0.0000,1,''),(287,402,'D57600225 000 004 332 8001','2025-07-04 16:21:25','Canpar Ground',0.00,0.0000,1,''),(288,394,'609128241161/179','2025-07-09 18:55:00','Purolator Express',0.00,0.0000,2,''),(289,405,'D57600225 000 004 346 9001','2025-07-28 15:53:06','Canpar Ground',0.00,0.0000,1,''),(290,408,'D57600225 000 004 349 7001','2025-08-06 19:29:26','Canpar Ground',0.00,0.0000,1,''),(291,403,'D57600225 000 004 344 6001','2025-08-13 15:29:22','Canpar Ground',0.00,0.0000,1,'Shipped on 7/23'),(292,410,'D57600225 000 004 361 1001','2025-08-18 16:55:03','Canpar Ground',0.00,0.0000,1,''),(293,406,'D57600225 000 004 370 3001','2025-09-03 18:36:42','Canpar Ground',0.00,0.0000,1,''),(294,411,'D57600225 000 004 370 4001','2025-09-03 18:39:25','Canpar Ground',0.00,0.0000,1,''),(295,414,'D57600225 000 004 370 9001','2025-09-04 14:27:51','Canpar Ground',0.00,0.0000,1,''),(296,413,'D57600225 000 004 371 0001','2025-09-04 14:30:45','Canpar Ground',0.00,0.0000,1,''),(297,412,'D57600225 000 004 371 1001','2025-09-04 14:34:28','Canpar Ground',0.00,0.0000,1,''),(298,418,'D57600225 000 004 371 4001','2025-09-04 14:48:17','Canpar Ground',0.00,0.0000,1,''),(299,416,'D57600225 000 004 371 5001','2025-09-04 14:50:50','Canpar Ground',0.00,0.0000,1,''),(300,420,'D57600225 000 004 382 3001','2025-09-22 18:14:29','Canpar Ground',0.00,0.0000,1,''),(301,421,'D57600225 000 004 376 7001','2025-09-22 19:49:50','Canpar Ground',0.00,0.0000,1,'Shipped on Sep 10'),(302,424,'D57600225 000 004 387 4001','2025-10-01 18:38:55','Canpar Ground',0.00,0.0000,1,''),(303,428,'D57600225 000 004 391 1001','2025-10-08 18:05:52','Canpar Ground',0.00,0.0000,1,''),(304,427,'D57600225 000 004 391 4001','2025-10-08 18:12:53','Canpar Ground',0.00,0.0000,1,''),(305,426,'D57600225 000 004 392 7001-2','2025-10-14 19:30:18','Canpar Ground',0.00,0.0000,2,''),(306,429,'D57600225 000 004 394 0001','2025-10-15 16:07:56','Canpar Ground',0.00,0.0000,1,''),(307,432,'D57600225 000 004 394 2001','2025-10-15 16:23:06','Canpar Ground',0.00,0.0000,1,''),(308,433,'D57600225 000 004 394 3001','2025-10-15 16:28:26','Canpar Ground',0.00,0.0000,1,''),(309,431,'D57600225 000 004 394 5001','2025-10-15 16:35:25','Canpar Ground',0.00,0.0000,1,''),(310,436,'D57600225 000 004 397 9001','2025-10-20 17:50:47','Canpar Ground',0.00,0.0000,1,''),(311,434,'D57600225 000 004 400 4001','2025-10-22 18:56:25','Canpar Ground',0.00,0.0000,1,''),(312,438,'D57600225 000 004 406 8001','2025-10-31 17:54:50','Canpar Ground',0.00,0.0000,1,''),(313,430,'D57600225 000 004 407 0001','2025-10-31 18:17:11','Canpar Ground',0.00,0.0000,1,''),(314,440,'D57600225 000 004 409 9001-2','2025-11-05 19:53:50','Canpar Ground',0.00,0.0000,2,''),(315,439,'D57600225 000 004 411 0001-3','2025-11-10 16:36:07','Canpar Ground',0.00,0.0000,3,''),(316,443,'D57600225 000 004 412 7001','2025-11-10 20:24:00','Canpar Ground',0.00,0.0000,1,''),(317,441,'D57600225 000 004 418 3001-8','2025-11-17 18:55:58','Canpar Ground',0.00,0.0000,8,''),(318,444,'D57600225 000 004 423 2001','2025-11-20 20:54:20','Canpar Ground',0.00,0.0000,1,''),(319,446,'D57600225 000 004 425 1001','2025-11-26 19:38:07','Canpar Ground',0.00,0.0000,1,''),(320,447,'D57600225 000 004 425 2001','2025-11-26 19:41:07','Canpar Ground',0.00,0.0000,1,''),(321,445,'D57600225 000 004 425 3001','2025-11-26 19:43:42','Canpar Ground',0.00,0.0000,1,'');

--
-- Table structure for table `shippingGroup`
--

CREATE TABLE `shippingGroup` (
  `SGRPNAME` varchar(50) COLLATE utf8mb3_unicode_ci NOT NULL,
  `SGRPPIECES` int NOT NULL,
  `SGRPBOXTYPE` varchar(100) COLLATE utf8mb3_unicode_ci NOT NULL,
  `SGRPWEIGHTPIECE` decimal(19,4) NOT NULL,
  PRIMARY KEY (`SGRPNAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

--
-- Dumping data for table `shippingGroup`
--


--
-- Table structure for table `shippingGroupItems`
--

CREATE TABLE `shippingGroupItems` (
  `SGRPNAME` varchar(50) COLLATE utf8mb3_unicode_ci NOT NULL,
  `ITEMID` int NOT NULL,
  PRIMARY KEY (`ITEMID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

--
-- Dumping data for table `shippingGroupItems`
--


--
-- Table structure for table `subcategories`
--

CREATE TABLE `subcategories` (
  `SUBCATID` int NOT NULL AUTO_INCREMENT,
  `CATID` int NOT NULL,
  `NAME` varchar(50) COLLATE utf8mb3_unicode_ci NOT NULL,
  `DESCRIPTION` varchar(200) COLLATE utf8mb3_unicode_ci NOT NULL,
  `PARENTSUBCATID` int NOT NULL DEFAULT '-1',
  `ICONURL` varchar(200) COLLATE utf8mb3_unicode_ci DEFAULT '',
  `USERLEVEL` int NOT NULL DEFAULT '0',
  PRIMARY KEY (`SUBCATID`)
) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

--
-- Dumping data for table `subcategories`
--

INSERT INTO `subcategories` VALUES (24,13,'Letterhead','Letterhead',-1,'',0),(25,13,'Envelope','Envelope',-1,'',0),(26,13,'Memo Pads','Memo Pads',-1,'',0),(27,1,' Generic Material','Label, Pads & Sticker ',-1,'',0),(28,1,'Card','Card',-1,'',0),(34,23,'Customize Item','Customize Item',-1,'icon_collateral.png',0),(35,24,'Business Card','Business Card',-1,'icon_businesscard.png',0),(36,23,'Trifold','Trifold',34,'',0),(37,23,'Magalogue','Book & Insert',34,'',0),(38,1,'Generic Material','  Brochure & Folder',-1,'',0),(39,23,'Brochure, Flyer & Form','Brochure, Flyer & Form',34,'',0),(40,23,'Cards','Cards',34,'',0),(41,23,'Label','Label',34,'',0),(42,23,'Double Gate Fold','Double Gate Fold',34,'',0),(43,1,'Flyer','Flyer',-1,'',0);

--
-- Table structure for table `taxes`
--

CREATE TABLE `taxes` (
  `TAXCODE` varchar(6) COLLATE utf8mb3_unicode_ci NOT NULL,
  `TAXGROUPNAME` varchar(100) COLLATE utf8mb3_unicode_ci NOT NULL,
  `TAXNAME1` varchar(100) COLLATE utf8mb3_unicode_ci NOT NULL DEFAULT '',
  `TAXNAME2` varchar(100) COLLATE utf8mb3_unicode_ci NOT NULL DEFAULT '',
  `TAXNAME3` varchar(100) COLLATE utf8mb3_unicode_ci NOT NULL DEFAULT '',
  `TAXNAME4` varchar(100) COLLATE utf8mb3_unicode_ci NOT NULL DEFAULT '',
  `TAXNAME5` varchar(100) COLLATE utf8mb3_unicode_ci NOT NULL DEFAULT '',
  `TAXRATE1` decimal(19,5) NOT NULL DEFAULT '0.00000',
  `TAXRATE2` decimal(19,5) NOT NULL DEFAULT '0.00000',
  `TAXRATE3` decimal(19,5) NOT NULL DEFAULT '0.00000',
  `TAXRATE4` decimal(19,5) NOT NULL DEFAULT '0.00000',
  `TAXRATE5` decimal(19,5) NOT NULL DEFAULT '0.00000',
  PRIMARY KEY (`TAXCODE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

--
-- Dumping data for table `taxes`
--

INSERT INTO `taxes` VALUES ('AB','AB','GST','','','','',0.05000,0.00000,0.00000,0.00000,0.00000),('BC','BC','GST','PST','','','',0.05000,0.07000,0.00000,0.00000,0.00000),('MB','MB','GST','PST','','','',0.05000,0.07000,0.00000,0.00000,0.00000),('NB','NB','HST','','','','',0.15000,0.00000,0.00000,0.00000,0.00000),('NL','NL','HST','','','','',0.15000,0.00000,0.00000,0.00000,0.00000),('NOTAX','NOTAX','','','','','',0.00000,0.00000,0.00000,0.00000,0.00000),('NS','NS','HST','','','','',0.14000,0.00000,0.00000,0.00000,0.00000),('NT','NT','GST','','','','',0.05000,0.00000,0.00000,0.00000,0.00000),('NU','NU','GST','','','','',0.05000,0.00000,0.00000,0.00000,0.00000),('ON','ON','HST','','','','',0.13000,0.00000,0.00000,0.00000,0.00000),('PE','PE','HST','','','','',0.15000,0.00000,0.00000,0.00000,0.00000),('QC','QC','GST','QST','','','',0.05000,0.09975,0.00000,0.00000,0.00000),('SK','SK','GST','PST','','','',0.05000,0.06000,0.00000,0.00000,0.00000),('YT','YT','GST','','','','',0.05000,0.00000,0.00000,0.00000,0.00000);

--
-- Table structure for table `ticketAttachment`
--

CREATE TABLE `ticketAttachment` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `TICKETID` int DEFAULT NULL,
  `MESSAGEID` int DEFAULT NULL,
  `STOREFILENAME` varchar(200) COLLATE utf8mb3_unicode_ci DEFAULT NULL,
  `ORIGFILENAME` varchar(200) COLLATE utf8mb3_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=95 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

--
-- Dumping data for table `ticketAttachment`
--

INSERT INTO `ticketAttachment` VALUES (1,22,NULL,'SUPPORT_638bd252eb585','/companyResources/VIVA/uploadFiles/SUPPORT_638bd252eb585Seasopns Greetings.png'),(2,26,31,'SUPPORT_638bdba5d5ccf','Seasopns Greetings.png'),(3,26,NULL,'SUPPORT_638bdbf5d275dSeasopns Greetings.png','/var/PrintOSFiles/SUPPORT_638bdbf5d275dSeasopns Greetings.png'),(4,27,33,'SUPPORT_638cec94c7b67','rca-noSponsor-530x100.png'),(5,27,34,'SUPPORT_638cecc60ee0c','Seasopns Greetings.png'),(6,27,35,'SUPPORT_638dd4c8e422f','Seasopns Greetings.png'),(7,27,36,'SUPPORT_638dd5469fd7d','Seasopns Greetings.png'),(8,27,37,'SUPPORT_638dd55b3995d','Seasopns Greetings.png'),(9,27,38,'SUPPORT_638dd74e9867c','Seasopns Greetings.png'),(10,27,39,'SUPPORT_638dd78aa66c4','Seasopns Greetings.png'),(11,27,NULL,'SUPPORT_638dfcae4357b','Seasopns Greetings.png'),(12,27,42,'SUPPORT_638e84d93b07d','Seasopns Greetings.png'),(13,28,46,'SUPPORT_639038217416c','Seasopns Greetings.png'),(14,28,47,'SUPPORT_63903838077f9','Seasopns Greetings.png'),(15,28,52,'SUPPORT_63903d1534906','Seasopns Greetings.png'),(16,2,53,'SUPPORT_6390af904b6bb','A00941_1925_CLUB_AARS Sign_CPDI.pdf'),(17,29,54,'SUPPORT_6390b39ec3c23','Mississauga Dec 2022 Flyer.xlsx'),(18,29,55,'SUPPORT_6390b51f5cf15','Victoria Pictionary.jpg'),(19,29,56,'SUPPORT_6390b5440367b','Victoria Pictionary.jpg'),(20,29,56,'SUPPORT_6390b54405a47','Terry photobooth.jpg'),(21,29,57,'SUPPORT_6390b5a2db966','Dont Miss Out October.docx'),(22,29,57,'SUPPORT_6390b5a2dc206','October 2022 Calendar - Oakville.pdf'),(23,29,57,'SUPPORT_6390b5a2de51c','Doris V!VA Oakvilles newest cheerleader.HEIC'),(24,29,58,'SUPPORT_6390b5d9eb9a6','VO Newsletter OCT 2022.pdf'),(25,29,59,'SUPPORT_6390b62cde444','VO Newsletter OCT 2022.pdf'),(26,30,61,'SUPPORT_6390d6e1d8dcb','Seasopns Greetings.png'),(27,30,62,'SUPPORT_6390d6fe7ad41','fml-Dec5-2022.jpeg'),(28,32,65,'SUPPORT_63c84fd77fb74','SEHC_RN_007 (1216) - Data Collection.pdf'),(29,33,66,'SUPPORT_63d2a3c15a7bc','Betty having a blast at the photo booth.jpg'),(30,33,67,'SUPPORT_63d2adef8a6ce','Doris V!VA Oakvilles newest cheerleader.jpg'),(31,34,68,'SUPPORT_63d2f3ed16b8d','GEN_MinorAilments_Dangler.pdf'),(32,35,69,'SUPPORT_63d2f423d21dc','MCK_HolidaySign_Gen.pdf'),(33,35,69,'SUPPORT_63d2f423da349','Screen Shot 2023-01-20 at 11.59.59 AM.png'),(34,36,70,'SUPPORT_63d2f453aafe0','AMI-445-amico-ALT_BC_Single-00016.pdf'),(35,36,70,'SUPPORT_63d2f453ab9ef','AMI-466-amico-AA_BC_Double-00054.pdf'),(36,36,70,'SUPPORT_63d2f453ac67d','AMI-467-amico-AA_BC_Double-00055.pdf'),(37,36,70,'SUPPORT_63d2f453acf13','AMI-469-amico-ALT_BC_Single-00014.pdf'),(38,36,70,'SUPPORT_63d2f453adaae','AMI-470-amico-ALT_BC_Single-00015.pdf'),(39,37,71,'SUPPORT_63d41c7c0e8fc','Matthew_Rafferty_Double.pdf'),(40,37,71,'SUPPORT_63d41c7c1e5a6','Kyle Umgelder_Double.pdf'),(41,37,71,'SUPPORT_63d41c7c20676','Kelly Micheletti_Double.pdf'),(42,37,71,'SUPPORT_63d41c7c216cd','Drago_Alilovic_Double.pdf'),(43,37,71,'SUPPORT_63d41c7c22769','Danielle Miller_Double.pdf'),(44,37,72,'SUPPORT_63d41d4c0e5f9','AMI-10075-Riley_Malcolmson_Single.pdf'),(45,37,72,'SUPPORT_63d41d4c0f448','AMI-10075-Nicole_Szlendak_Single.pdf'),(46,37,72,'SUPPORT_63d41d4c10c5e','AMI-10075-Kareem_Kamal_Single.pdf'),(47,37,72,'SUPPORT_63d41d4c116f9','AMI-10075-John_Koo_Single.pdf'),(48,37,72,'SUPPORT_63d41d4c123e2','AMI-10075-Abby_Gomes_Single.pdf'),(49,38,73,'SUPPORT_63d41e249a4a3','Screen Shot 2023-01-27 at 1.52.13 PM.png'),(50,39,74,'SUPPORT_63d41f501765b','IDA_6up_E.pdf'),(51,39,74,'SUPPORT_63d41f501833d','GDN_6up_E.pdf'),(52,39,74,'SUPPORT_63d41f5019017','GDN_1up_E.pdf'),(53,39,74,'SUPPORT_63d41f5019f4e','CPDI_Tempprary Hours Sign.pdf'),(54,39,74,'SUPPORT_63d41f501a7af','CPDI_Signs.pdf'),(55,41,76,'SUPPORT_63da9633187c0','WHIT_magrate23.pdf'),(56,41,76,'SUPPORT_63da96338aed5','WHIT_magrate.png'),(57,41,76,'SUPPORT_63da963398993','MISS_magrate.pdf'),(58,41,76,'SUPPORT_63da9633aae44','MISS_magrate.png'),(59,41,76,'SUPPORT_63da9633bd46a','BARR_magrate.pdf'),(60,42,77,'SUPPORT_63da974cf2893','Betty having a blast at the photo booth.heic'),(61,42,77,'SUPPORT_63da974d0e9c2','Deo striking a pose for National Photograph Day.HEIC'),(62,42,77,'SUPPORT_63da974d16daf','Dont Miss Out October.docx'),(63,42,77,'SUPPORT_63da974d26a15','Doris V!VA Oakvilles newest cheerleader.HEIC'),(64,42,78,'SUPPORT_63da97797ab44','October 2022 Calendar - Oakville (1).pdf'),(65,42,80,'SUPPORT_63da98b92f9ed','Terry photobooth.jpg'),(66,42,82,'SUPPORT_63da990b37b91','BARR_MovesBooklet.pdf'),(67,42,84,'SUPPORT_63da9953eccca','BARR_MovesBooklet.pdf'),(68,43,85,'SUPPORT_63da9d72134e5','5125-20e.pdf'),(69,43,86,'SUPPORT_63da9e4ca421b','_Job Tickets_A03774 - VIVA Newsletter & Calendar_Month (Thornhill Woods, Mississauga, Waterside, Oakville).xls'),(70,43,86,'SUPPORT_63da9e4cc6973','_Job Tickets_A03775 - VIVA Newsletter & Calendar_Month (Meadowlands, Whitby Shores, Pickering & Barrhaven).xls'),(71,43,89,'SUPPORT_63da9f0e543d4','BabiesRus_Coupon book 2022_HR.pdf'),(72,44,90,'SUPPORT_63da9f3697bdf','Kimberly Lepard (Age 10).pdf'),(73,43,91,'SUPPORT_63daa1c41f7b7','Doris V!VA Oakvilles newest cheerleader.jpg'),(74,43,91,'SUPPORT_63daa1c435d5c','Jacqueline and Samson from Partnres with Paws preforming dog tricks for our Community Members.jpg'),(75,43,92,'SUPPORT_63daa2da066c3','Doris V!VA Oakvilles newest cheerleader.jpg'),(76,45,93,'SUPPORT_63daa2fd3bb5e','Austin Medeiros (Age 12).pdf'),(77,45,93,'SUPPORT_63daa2fd43c51','Mikayla Reese Barrera (Age 7).pdf'),(78,43,94,'SUPPORT_63daa337dd531','Deo striking a pose for National Photograph Day.jpg'),(79,43,94,'SUPPORT_63daa337e4baf','IMG_0175_Website1600px.jpg'),(80,43,95,'SUPPORT_63daa35b34b50','Betty having a blast at the photo booth.jpg'),(81,43,95,'SUPPORT_63daa35b963fe','IMG_0175_Website1600px.jpg'),(82,46,98,'SUPPORT_63dabe59ad50a','Akasaka_logo.jpg'),(83,46,98,'SUPPORT_63dabe59b759c','AMC to the moon.jpg'),(84,46,98,'SUPPORT_63dabe59bd7d3','Sales Manager Post.docx'),(85,46,98,'SUPPORT_63dabe59c3bb7','w_defa3464.pdf'),(86,46,98,'SUPPORT_63dabe59da69a','What You Should Know About Family Law in Ontario - Ministry of the Attorney General.pdf'),(87,47,99,'SUPPORT_63dabf746905f','Betty having a blast at the photo booth.heic'),(88,47,99,'SUPPORT_63dabf747ccd5','Deo striking a pose for National Photograph Day.HEIC'),(89,47,99,'SUPPORT_63dabf74890eb','Dont Miss Out October.docx'),(90,47,99,'SUPPORT_63dabf74b3432','Doris V!VA Oakvilles newest cheerleader.HEIC'),(91,47,101,'SUPPORT_63dabfdeb52a1','October 2022 Calendar - Oakville (1).pdf'),(92,47,101,'SUPPORT_63dabfdecc6aa','October 2022 Calendar - Oakville.pdf'),(93,48,102,'SUPPORT_63dac322d3b3d','hide the pain harold.jpg'),(94,48,105,'SUPPORT_63dac433529e5','October Calendar -Oakville (2)[79].pdf');

--
-- Table structure for table `ticketDetail`
--

CREATE TABLE `ticketDetail` (
  `MESSAGEID` int NOT NULL AUTO_INCREMENT,
  `TICKETID` int NOT NULL,
  `DATEENTERED` datetime NOT NULL,
  `ENTEREDBY` varchar(50) COLLATE utf8mb3_unicode_ci NOT NULL,
  `CLIENTMESSAGE` int NOT NULL,
  `MESSAGE` varchar(1000) COLLATE utf8mb3_unicode_ci NOT NULL,
  PRIMARY KEY (`MESSAGEID`,`TICKETID`)
) ENGINE=InnoDB AUTO_INCREMENT=126 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

--
-- Dumping data for table `ticketDetail`
--

INSERT INTO `ticketDetail` VALUES (1,1,'2022-03-23 15:51:07','WHIT',1,'Hi,\r\n\r\nI am just following up on the order I placed March 16th - VIV-20092.  Can you please give me an eta on delivery.\r\n\r\nThanks\r\n\r\nCheryl'),(2,1,'2022-03-23 17:20:16','peggy',0,'Hi, \n\nThe order should be shipped out coming Friday. We will send you the tacking number once shipped. Thanks'),(3,2,'2022-11-24 11:05:17','Demo',1,'This is a test '),(4,3,'2022-11-24 11:29:12','peggy',1,'Test'),(33,27,'2022-12-04 13:53:08','Sam',1,'This is test number 1'),(34,27,'2022-12-04 13:53:58','Sam',0,'This is a reply'),(35,27,'2022-12-05 06:23:52','Sam',0,'this is a reply'),(36,27,'2022-12-05 06:25:58','Sam',0,'this is a reply'),(37,27,'2022-12-05 06:26:19','Sam',0,'this is a reply'),(38,27,'2022-12-05 06:34:38','Sam',0,'this is a reply'),(39,27,'2022-12-05 06:35:38','Sam',0,'this is a reply'),(40,27,'2022-12-05 09:14:06','Sam',0,'this is a reply'),(41,27,'2022-12-05 10:50:30','Sam',0,'this is a reply'),(42,27,'2022-12-05 18:55:05','Sam',0,'test 123'),(43,27,'2022-12-06 23:25:19','Sam',0,'Hello'),(44,27,'2022-12-07 01:27:10','Sam',0,'hello'),(45,27,'2022-12-07 01:28:22','Sam',0,'bla bla'),(46,28,'2022-12-07 01:52:17','Sam',1,'this is a test again'),(47,28,'2022-12-07 01:52:40','Sam',0,'this is a reply'),(48,28,'2022-12-07 01:58:42','Sam',0,'this is a reply'),(49,28,'2022-12-07 02:00:36','Sam',0,'this is a reply'),(50,28,'2022-12-07 02:01:46','Sam',0,'hello there'),(51,28,'2022-12-07 02:12:43','Sam',0,'this is a reply'),(52,28,'2022-12-07 02:13:25','Sam',0,'this is a reply with an attachment'),(53,2,'2022-12-07 10:21:52','patrick',0,'Test file'),(54,29,'2022-12-07 10:39:10','VIHO',1,'test'),(55,29,'2022-12-07 10:45:35','VIHO',1,'supporting doc'),(56,29,'2022-12-07 10:46:12','VIHO',1,'testing two files'),(57,29,'2022-12-07 10:47:46','VIHO',1,'Hi Winnie!'),(58,29,'2022-12-07 10:48:41','patrick',0,'First Proof'),(59,29,'2022-12-07 10:50:04','patrick',0,'The image for Winnie is low-res'),(60,29,'2022-12-07 11:02:00','VIHO',1,'Chatty chat chat chat!'),(61,30,'2022-12-07 13:09:37','Sam',1,'this is a test.'),(62,30,'2022-12-07 13:10:06','Sam',0,'This is a reply'),(63,31,'2022-12-19 13:52:01','OAKV',1,'Hi there, my director approved business cards at the end of November.. any idea on when they will be delivered? thanks'),(64,31,'2022-12-22 16:39:36','peggy',0,'Hi \r\n\r\nDo you have the order number? Have your order appoved by the apporver yet? Thanks'),(65,32,'2023-01-18 15:00:23','peggy',1,'TEST'),(66,33,'2023-01-26 11:01:05','peggy',1,'Images attached'),(67,33,'2023-01-26 11:44:31','patrick',0,'more imahges'),(68,34,'2023-01-26 16:43:09','peggy',1,'TEST'),(69,35,'2023-01-26 16:44:03','peggy',1,'TEst 2 files'),(70,36,'2023-01-26 16:44:51','peggy',1,'Test 7 files'),(71,37,'2023-01-27 13:48:28','peggy',1,'8 Files'),(72,37,'2023-01-27 13:51:56','peggy',0,'try 5 again'),(73,38,'2023-01-27 13:55:32','Demo',1,'Test'),(74,39,'2023-01-27 14:00:32','peggy',1,'Windows Chrome'),(75,40,'2023-02-01 11:34:43','winnie',1,'Test'),(76,41,'2023-02-01 11:41:22','Demo',1,'test'),(77,42,'2023-02-01 11:46:04','winnie',1,'dfkjflkaf'),(78,42,'2023-02-01 11:46:49','winnie',1,'dffs'),(79,42,'2023-02-01 11:49:29','peggy',0,'one of the files is low-res'),(80,42,'2023-02-01 11:52:09','winnie',1,'nwew file arrache'),(81,42,'2023-02-01 11:52:51','peggy',0,'Thank youdjdjdhdfdhdshjdf'),(82,42,'2023-02-01 11:53:31','peggy',0,'first draft pdf attacherd. '),(83,42,'2023-02-01 11:54:15','winnie',1,'Approved'),(84,42,'2023-02-01 11:54:43','peggy',0,'Thank you for your approval. will process to print. '),(85,43,'2023-02-01 12:12:17','winnie',1,'hihi'),(86,43,'2023-02-01 12:15:56','winnie',1,'hgf'),(87,43,'2023-02-01 12:17:49','winnie',1,'the file I am tryimng to load is 103mb what do I do'),(88,43,'2023-02-01 12:18:28','patrick',0,'In that case you will need to send to me via wetranfer\r\n\r\ngo to wetrnfer.com'),(89,43,'2023-02-01 12:19:10','winnie',1,'asdas'),(90,44,'2023-02-01 12:19:50','peggy',1,'testsdf'),(91,43,'2023-02-01 12:30:44','winnie',1,'s'),(92,43,'2023-02-01 12:35:21','winnie',1,'6.5mb'),(93,45,'2023-02-01 12:35:57','peggy',1,'test'),(94,43,'2023-02-01 12:36:55','winnie',1,'7.5mb'),(95,43,'2023-02-01 12:37:29','winnie',1,'8.3'),(96,43,'2023-02-01 12:44:08','peggy',0,'ok'),(97,46,'2023-02-01 12:52:14','',1,'test'),(98,46,'2023-02-01 14:32:41','patrick',0,'dsfd'),(99,47,'2023-02-01 14:37:24','winnie',1,'dfgdf'),(100,47,'2023-02-01 14:38:17','winnie',0,'hi'),(101,47,'2023-02-01 14:39:10','patrick',0,'sdste'),(102,48,'2023-02-01 14:53:06','Demo',1,'Testy McTestface'),(103,48,'2023-02-01 14:54:31','patrick',0,'Thank you for submission, I will review and get working on the layout'),(104,48,'2023-02-01 14:56:32','Demo',1,'What is a sailor\'s favourite breakfast?\r\n\r\n...\r\n\r\n...boatmeal.'),(105,48,'2023-02-01 14:57:39','patrick',0,'Please see attached PDF and reply with your approival'),(106,48,'2023-02-01 14:59:02','Demo',1,'Approved. Please proceed with same QTY as last time.'),(107,48,'2023-02-01 14:59:20','patrick',0,'Perfect'),(108,48,'2023-02-01 15:00:37','Demo',1,'test?'),(109,49,'2023-02-01 15:39:09','winnie',1,'new'),(110,49,'2023-02-01 15:39:52','peggy',0,'hi'),(111,50,'2024-08-29 13:14:08','WATE',1,'I am needing to order business cards for a coworker however his title is not listed in the drop down menu. It would need to added to be able for me to generate the template. His title is Executive Culinary Manager.'),(112,50,'2024-08-29 16:44:32','peggy',0,'Hi Rik, \r\n\r\nThe title has been added to the drop-down. Please take a look and let us know if you have any other issues. Thank you.'),(113,51,'2024-10-16 19:52:33','MDLA',1,'Can we please update the website on this card to www.vivalife.ca'),(114,52,'2024-10-16 20:01:20','MDLA',1,'Can we please update the contact info to Amanda (replace Donna)'),(115,53,'2024-10-16 20:03:31','MDLA',1,'Can we please update all proofs from Donna Carlesso to Amanda Tachauer amanda.tachauer@vivalife.ca'),(116,52,'2024-10-17 19:44:56','peggy',0,'Hi, Could you provide the email address of Amanda and Last Name? Thank you'),(117,53,'2024-10-17 19:46:09','peggy',0,'Hi, approver updated to Amanda Tachauer. Thanks '),(118,52,'2024-10-17 19:46:24','peggy',0,'Hi, approver updated to Amanda Tachauer. Thanks'),(119,52,'2024-10-18 10:24:01','MDLA',1,'Please confirm the update from Donna Carlesso to Amanda Tachauer has occurred and all orders have been processed'),(120,51,'2024-10-22 21:37:09','peggy',0,'Hi, Do you mean to remove the www.ilovemeadowlands.com? The card already has the www.vivalife.ca. Thanks '),(121,50,'2024-10-22 21:40:02','peggy',0,'Hi, The Ticket will be closed. If you need another assist, please create a new ticket. Thank you'),(122,53,'2024-10-22 21:40:31','peggy',0,'Hi, The Ticket will be closed. If you need another assist, please create a new ticket. Thank you'),(123,54,'2025-02-11 14:46:20','OAKV',1,'We ordered 100 copies of our Physician Statement but we only received 55, will the remaining be shipped at a later date?'),(124,54,'2025-02-11 16:38:06','peggy',0,'Hi, We apologize for the missing qty. We will reship the Physician Statement ASAP and provide tracking once shipped. Thanks'),(125,54,'2025-02-12 19:03:35','peggy',0,'Hi, your order has been reprinted and will be shipped out today. Here is the new tracking number D57600225 000 004 227 6001 via Canpar. Thanks');

--
-- Table structure for table `ticketHeader`
--

CREATE TABLE `ticketHeader` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `ENTEREDBY` varchar(50) COLLATE utf8mb3_unicode_ci NOT NULL,
  `DATEENTERED` datetime NOT NULL,
  `STATUS` int NOT NULL,
  `TITLE` varchar(1000) COLLATE utf8mb3_unicode_ci NOT NULL,
  `DATECLOSED` datetime DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=55 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

--
-- Dumping data for table `ticketHeader`
--

INSERT INTO `ticketHeader` VALUES (1,'WHIT','2022-03-23 15:51:07',0,'VIV-20092','2022-05-02 16:17:41'),(2,'Demo','2022-11-24 11:05:17',0,'Newsletter','2022-12-07 10:52:10'),(3,'peggy','2022-11-24 11:29:12',0,'TEst','2022-12-07 10:52:16'),(27,'Sam','2022-12-04 13:53:08',0,'testing 123','2022-12-22 16:39:48'),(28,'Sam','2022-12-07 01:52:17',0,'testing 12345','2022-12-22 16:39:57'),(29,'VIHO','2022-12-07 10:39:10',0,'NL mrm test DEC72022','2022-12-07 11:02:46'),(30,'Sam','2022-12-07 13:09:37',0,'new support ticket test','2022-12-22 16:40:11'),(31,'OAKV','2022-12-19 13:52:01',0,'missing business cards','2023-01-18 14:59:49'),(32,'peggy','2023-01-18 15:00:23',0,'TEST','2023-01-18 15:01:00'),(33,'peggy','2023-01-26 11:01:05',0,'Test','2023-02-01 11:33:57'),(34,'peggy','2023-01-26 16:43:09',0,'TEST','2023-02-01 11:34:06'),(35,'peggy','2023-01-26 16:44:03',0,'Test 2','2023-02-01 11:34:13'),(36,'peggy','2023-01-26 16:44:51',0,'test 3','2023-02-01 11:34:21'),(37,'peggy','2023-01-27 13:48:28',0,'Test 4','2023-02-01 11:34:28'),(38,'Demo','2023-01-27 13:55:32',0,'Test','2023-02-01 11:34:35'),(39,'peggy','2023-01-27 14:00:32',0,'Windows Upload test','2023-02-01 11:34:41'),(40,'winnie','2023-02-01 11:34:43',0,'Test','2023-02-01 11:35:21'),(41,'Demo','2023-02-01 11:41:22',0,'test','2023-02-01 12:49:41'),(42,'winnie','2023-02-01 11:46:04',0,'Newsletter March 2023','2023-02-01 11:55:30'),(43,'winnie','2023-02-01 12:12:17',0,'test2','2023-02-01 12:49:52'),(44,'peggy','2023-02-01 12:19:50',0,'test','2023-02-01 12:47:16'),(45,'peggy','2023-02-01 12:35:57',0,'test','2023-02-01 12:49:27'),(46,'','2023-02-01 12:52:14',0,'test','2024-08-29 16:44:38'),(47,'winnie','2023-02-01 14:37:24',0,'test','2024-08-29 16:46:37'),(48,'Demo','2023-02-01 14:53:06',0,'Another Test','2023-02-01 14:59:47'),(49,'winnie','2023-02-01 15:39:09',0,'new','2024-08-29 16:46:33'),(50,'WATE','2024-08-29 13:14:08',0,'Waterside Business Card Order','2024-10-22 21:40:19'),(51,'MDLA','2024-10-16 19:52:33',1,'Housewarming Card',NULL),(52,'MDLA','2024-10-16 20:01:20',0,'Golfzon Card','2024-10-17 19:46:27'),(53,'MDLA','2024-10-16 20:03:31',0,'MDLA_Housewa','2024-10-22 21:40:40'),(54,'OAKV','2025-02-11 14:46:20',1,'Missing items',NULL);

--
-- Table structure for table `user`
--

CREATE TABLE `user` (
  `USERNAME` varchar(50) COLLATE utf8mb3_unicode_ci NOT NULL,
  `BRANCH` varchar(20) COLLATE utf8mb3_unicode_ci NOT NULL,
  `PASSWORD` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci NOT NULL,
  `PLAINPASSWORD` varchar(50) COLLATE utf8mb3_unicode_ci NOT NULL,
  `FIRSTNAME` varchar(100) COLLATE utf8mb3_unicode_ci NOT NULL,
  `LASTNAME` varchar(100) COLLATE utf8mb3_unicode_ci NOT NULL,
  `EMAIL` varchar(100) COLLATE utf8mb3_unicode_ci NOT NULL,
  `LANGUAGE` varchar(2) COLLATE utf8mb3_unicode_ci NOT NULL DEFAULT 'EN',
  `USERLEVEL` int NOT NULL DEFAULT '0',
  `CREATEDDATE` datetime DEFAULT NULL,
  `MODIFIEDDATE` datetime DEFAULT NULL,
  `STATUS` int NOT NULL,
  `COMMENTS` varchar(1000) COLLATE utf8mb3_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`USERNAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

--
-- Dumping data for table `user`
--

INSERT INTO `user` VALUES ('BARR','BARR','b4882a1582a66f369dece70ff15049fa6b1692f88126f13874b52da8c2cc6186','b0df012','Karen','Timmons ','karen.timmons@vivalife.ca','EN',1,'2016-01-22 00:00:00','2022-04-11 11:44:34',1,''),('Demo','CPDI','8a2cc0673b1c428315fe84c0138d95c3ddda30baf81e7d9aa821f1ca47098193','Demo','Demo','Demo','peggy@creativepath.com','EN',9,NULL,'2023-02-07 14:05:15',1,''),('eric','CPDI','1f754b4b535c77d151bd3235b378d39d497f793313e30d7d41dfaf61348ca560','E6359S','Eric','SIn','eric@creativepath.com','EN',81,'2020-03-26 16:06:46','2021-04-15 14:03:26',1,'CPDI'),('Jen','CPDI','6fc96f1007fa0f49999499c97e7a2b5cdeae3e60fc5cd32ae742d7fcdd763c93','Hl5982','Jen','Lor','jennifer@cpfulfillment.com','EN',81,'2022-04-22 12:44:45','2022-04-22 12:45:05',1,''),('LEAS','LEAS','8d3881bc22e5587aec548563b6b3d73e4d543e7c52d8b67893ab2a48da27bbfd','Y9cBx2','Sasha','Zivkovic','Sasha@vivalife.ca','EN',1,NULL,NULL,1,''),('MDLA','MDLA','c87380dc973a7bdf9a7b84b039edd735d80b7883db3844f9da3b2aedb36a5f6b','l9a1p5','Amanda','Tachauer','amanda.tachauer@vivalife.ca','EN',1,'2014-01-10 00:00:00','2024-10-17 19:45:38',1,''),('miir','CPDI','50bd495636e1bd85f769c7d5b27fa1506e99d365d3c3be56f872c30c9be2c395','miir2280','Miranda','Lee','miranda@creativepath.com','EN',81,'2024-07-24 15:39:47','2024-07-24 15:40:09',1,''),('MISS','MISS','f66f5899cce0ac3de4003542cd03ba5cb7e856ae0b2abeab3ee43aecdaa7078a','m5s6n8','Deepak','Bhatia','Deepak.bhatia@vivalife.ca','EN',1,'2014-01-10 00:00:00','2022-11-03 11:39:19',1,'jon@vivalife.ca'),('OAKV','OAKV','e8f137d8c84440a3fbcc6eae5725ee9b833e1cf30d9902f3a7ba6107f3ff7c59','s8dtw3','Robert','Bertin','robert.bertin@vivalife.ca','EN',1,'2018-05-16 00:00:00','2024-11-29 17:53:47',1,''),('patrick','CPDI','d8883a3e841365332988e039725801783843470ae745daa5f92ef3ee2a7d7566','cpdi905711','Patrick','Sinn','patrick@creativepath.com','EN',99,'2017-01-21 20:14:11','2021-04-15 14:04:08',1,'CPDI'),('peggy','CPDI','0a8b1aa48e055dde384643814bc1bc0aee3f37613a8a96179e28d1982ed9b0d2','momo73449','Peggy','Cheng','peggy@creativepath.com','EN',99,'2017-01-21 20:14:11','2024-05-27 13:44:28',1,'CPDI'),('PICK','PICK','de4b965c662bc2994380ed9db65305621d328ac8de3e557a11fd2f797e630471','p1k0c6','Fletcher','Elliott','fletcher.elliott@vivalife.ca','EN',1,'2014-01-10 00:00:00','2025-10-07 15:30:40',1,''),('Sam','CPDI','4f85b1955701bc2ffa7fe9de4bd9dc09df48d554b2cfb14ce2d798f838ec9d5d','sam8500','Sam','Stratigeas','sam@samstratigeas.ca','EN',99,'2022-04-18 14:37:41','2022-04-18 14:37:59',1,'CPDI'),('THWO','THWO','b4c320133bf9046dbc55cd549f3297d21878ee6cd39531aadf2cf72c111fc194','t6w4v2','Magda','Di Maria','magda.dimaria@vivalife.ca','EN',1,'2014-01-10 00:00:00','2024-01-30 15:14:14',1,''),('VIHO','VIHO','fab742690bc5ad31af7a219a2025cfe43353939c897c42249f982f8079268a71','h3o3n2','Krista','Pelton','Krista@vivalife.ca ','EN',1,'2014-01-10 00:00:00','2025-01-13 20:23:09',1,''),('VIHOAM','VIHOAM','ec55710d94785a04ee3ff5438f200d4207ca5cd77580322a4b1a2a14a166d835','ho2VI3','Krista','Pelton','Krista@vivalife.ca ','EN',51,'2024-06-13 12:50:05','2024-06-13 12:50:22',1,''),('WATE','WATE','d6ee415602bf70a69eaf3a5b3a77a28ae5f52b3a0fae9dec3889bac0bd953bf1','w7s0b6','Rik','Luneburg','rikl@thewaterside.ca','EN',1,'2014-01-10 00:00:00','2022-04-11 11:44:04',1,''),('WHIT','WHIT','68b1b7c87d6b7bba2b1c83f945636d9a5808a53c603645062814ea61a9eb1979','U4w6cY','Amanda','Lynde','amanda.lynde@vivalife.ca','EN',1,'2019-04-22 00:00:00','2020-02-20 18:16:08',1,''),('winnie','CPDI','6b5196461ff750b26b88ab8ede24ffe31a8f70298e99af379286b3260e2655cc','cpdi236','Winnie','Wu','winnie@creativepath.com','EN',81,'2023-01-30 10:34:42','2025-01-15 01:58:27',1,'CPDI'),('zoeyc','CPDI','09938e8ea47664181a2fa3b1a211543d4c3455ac7edec18879c66559e9e58acf','zc5689','Zoey','C','zoeyc@cpfulfillment.com','EN',99,'2023-06-26 13:25:21','2023-06-26 13:25:41',1,'CPDI');

--
-- Dumping routines for database 'cpfulfillment_PrintOS_Viva'
--
/*!50003 DROP PROCEDURE IF EXISTS `AddDownloadLogEntry` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `AddDownloadLogEntry`(__USERNAME VARCHAR(50), __ITEMID INT)
BEGIN
	INSERT INTO 
		downloadLog
        (USERNAME,BRANCHCODE,ITEMID,LOGTIME)
	SELECT
		USERNAME,
        BRANCH,
        __ITEMID,
        CURRENT_TIMESTAMP
	FROM
		user
	WHERE
		USERNAME = __USERNAME;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `AddItemToCart` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `AddItemToCart`(__USERNAME VARCHAR(50), __ITEMID INT, __PROOF VARCHAR(200), __QTY INT, __ITEMOPTION VARCHAR(30), __ADDCOST DECIMAL)
BEGIN



SELECT @ORDERNUM := COUNT(*) FROM orderHeader WHERE USERNAME = __USERNAME AND ORDERSTATUS = 'CART';


SELECT @LOTPRICE := COUNT(*) FROM itemPricing WHERE ITEMID = __ITEMID AND QTY = __QTY AND ITEMOPTION = __ITEMOPTION;
SELECT @VARPRICE := COUNT(*) FROM itemPricing WHERE ITEMID = __ITEMID AND QTY = -1 AND ITEMOPTION = __ITEMOPTION;


IF @ORDERNUM = 0 THEN
 INSERT INTO
 orderHeader
 (USERNAME,ORDERDATE,ORDERMODIFIED,PONUMBER,SHIPCONTACTFIRSTNAME,SHIPCONTACTLASTNAME,SHIPCONTACTPHONE,SHIPCONTACTFAX,SHIPCONTACTEMAIL,
 SHIPNAME,SHIPADDRESS,SHIPADDRESS2,SHIPCITY,SHIPPROVINCE,SHIPCOUNTRY,SHIPPOSTAL,SHIPTAXGROUP,ORDERSTATUS,SHIPSTOREID,
 REQCONTACTFIRSTNAME,REQCONTACTLASTNAME,REQCONTACTPHONE,REQCONTACTFAX,
 REQNAME,REQADDRESS,REQADDRESS2,REQCITY,REQPROVINCE,REQCOUNTRY,REQPOSTAL)
 SELECT
 __USERNAME AS USERNAME ,
 CURRENT_TIMESTAMP AS ORDERDATE,
 CURRENT_TIMESTAMP AS ORDERMODIFIED,
 '' AS PONUMBER,
 USER.FIRSTNAME AS SHIPCONTACTFIRSTNAME,
 USER.LASTNAME AS SHIPCONTACTLASTNAME,
 BADDR.PHONE AS SHIPCONTACTPHONE,
 BADDR.FAX AS SHIPCONTACTFAX,
 USER.EMAIL AS SHIPCONTACTEMAIL,
 BINFO.NAME AS SHIPNAME,
 BADDR.ADDRESS AS SHIPADDRESS,
 BADDR.ADDRESS2 AS SHIPADDRESS2,
 BADDR.CITY AS SHIPCITY,
 BADDR.PROVINCE AS SHIPPROVINCE,
 BADDR.COUNTRY AS SHIPCOUNTRY,
 BADDR.POSTALCODE AS SHIPPOSTAL,
 BINFO.TAXGROUP AS SHIPTAXGROUP,
        'CART' AS ORDERSTATUS,
        BINFO.CODE AS SHIPSTOREID,
		USER.FIRSTNAME AS REQCONTACTFIRSTNAME,
        USER.LASTNAME AS REQCONTACTLASTNAME,
        BADDR.PHONE AS REQCONTACTPHONE,
        BADDR.FAX AS REQCONTACTFAX,
		BINFO.NAME AS REQNAME,
        BADDR.ADDRESS AS REQADDRESS,
        BADDR.ADDRESS2 AS REQADDRESS2,
        BADDR.CITY AS REQCITY,
        BADDR.PROVINCE AS REQPROVINCE,
        BADDR.COUNTRY AS REQCOUNTRY,
        BADDR.POSTALCODE AS REQPOSTAL        
	FROM
		user USER
	INNER JOIN
		branches BINFO ON 
			USER.BRANCH = BINFO.CODE
	INNER JOIN
		branchesAddresses BADDR ON 
			USER.BRANCH = BADDR.CODE 
	WHERE
		USER.USERNAME  = __USERNAME AND
        BADDR.ISMAIN = 1
    ;
END IF;


SELECT @ORDERID := ORDERID FROM orderHeader WHERE USERNAME = __USERNAME AND ORDERSTATUS = 'CART';


IF @LOTPRICE > 0 THEN

	INSERT INTO
		orderDetail
			(ORDERID,ITEMID,ITEMCODE,ITEMDESC,ORDERQTY,SHIPTODATEQTY,UNITPRICE,EXTPRICE,
             TAXAMT1,TAXAMT2,TAXAMT3,TAXAMT4,TAXAMT5,ITEMLENGTH,ITEMWIDTH,ITEMHEIGHT,ITEMWEIGHT,ITEMPIECES,PROOFCONFIRMATION,ITEMOPTION)
	SELECT
		@ORDERID AS ORDERID,
        ITEM.ID AS ITEMID,
        ITEM.CODE AS ITEMCODE,
        ITEM.DESCRIPTION AS ITEMDESC,
        ITEMPRICE.QTY AS ORDERQTY,
        0 AS SHIPTODATEQTY,
        (ITEMPRICE.PRICE / ITEMPRICE.QTY) + (__ADDCOST / ITEMPRICE.QTY) AS UNITPRICE,
        ITEMPRICE.PRICE + __ADDCOST AS EXTPRICE,
		0 AS TAXAMT1,
        0 AS TAXAMT2,
        0 AS TAXAMT3,
        0 AS TAXAMT4,
        0 AS TAXAMT5,
        ITEMPRICE.LENGTH AS ITEMLENGTH,
        ITEMPRICE.WIDTH AS ITEMWIDTH,
        ITEMPRICE.HEIGHT AS ITEMHEIGHT,
        ITEMPRICE.WEIGHT AS ITEMWEIGHT,
        ITEMPRICE.PIECES AS ITEMPIECES,
        __PROOF AS PROOFCONFIRMATION,
        ITEMPRICE.ITEMOPTION AS ITEMOPTION
	FROM
		items ITEM
	INNER JOIN
		itemPricing ITEMPRICE ON 
			ITEM.ID = ITEMPRICE.ITEMID
	WHERE
		ITEM.ID = __ITEMID AND
        ITEMPRICE.QTY = __QTY AND
        ITEMPRICE.ITEMOPTION = __ITEMOPTION;
        
ELSEIF @VARPRICE > 0 AND @LOTPRICE = 0 THEN

INSERT INTO
		orderDetail
			(ORDERID,ITEMID,ITEMCODE,ITEMDESC,ORDERQTY,SHIPTODATEQTY,UNITPRICE,EXTPRICE,
             TAXAMT1,TAXAMT2,TAXAMT3,TAXAMT4,TAXAMT5,ITEMLENGTH,ITEMWIDTH,ITEMHEIGHT,ITEMWEIGHT,ITEMPIECES,PROOFCONFIRMATION,ITEMOPTION)
	SELECT
		@ORDERID AS ORDERID,
        ITEM.ID AS ITEMID,
        ITEM.CODE AS ITEMCODE,
        ITEM.DESCRIPTION AS ITEMDESC,
        __QTY AS ORDERQTY,
        0 AS SHIPTODATEQTY,
        ITEMPRICE.PRICE + __ADDCOST AS UNITPRICE,
        (ITEMPRICE.PRICE + __ADDCOST) * __QTY AS EXTPRICE,
		0 AS TAXAMT1,
        0 AS TAXAMT2,
        0 AS TAXAMT3,
        0 AS TAXAMT4,
        0 AS TAXAMT5,
        ITEMPRICE.LENGTH AS ITEMLENGTH,
        ITEMPRICE.WIDTH AS ITEMWIDTH,
        ITEMPRICE.HEIGHT AS ITEMHEIGHT,
        ITEMPRICE.WEIGHT * __QTY AS ITEMWEIGHT,
        ITEMPRICE.PIECES AS ITEMPIECES,
        __PROOF AS PROOFCONFIRMATION,
        ITEMPRICE.ITEMOPTION AS ITEMOPTION
	FROM
		items ITEM
	INNER JOIN
		itemPricing ITEMPRICE ON 
			ITEM.ID = ITEMPRICE.ITEMID
	WHERE
		ITEM.ID = __ITEMID AND
        ITEMPRICE.QTY = -1 AND 
        ITEMPRICE.ITEMOPTION = __ITEMOPTION;

ELSE 
	SIGNAL SQLSTATE '45000'
		SET MESSAGE_TEXT = 'Invalid Pricing Found For Item';
END IF;  

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `AddReportingExtraData` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `AddReportingExtraData`(__USERNAME VARCHAR(20), __EXTRADATA VARCHAR(100))
BEGIN

SET SQL_SAFE_UPDATES = 0;

UPDATE
	orderHeader
SET
	EXTRADATA1 = __EXTRADATA
WHERE 
	USERNAME  = __USERNAME AND 
    ORDERSTATUS = 'CART';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `AddShipmentCostEntry` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `AddShipmentCostEntry`(__CARRIER VARCHAR(100), __METHOD VARCHAR(100),
__SHIPFROM_NAME VARCHAR(100), __SHIPFROM_STREETNUM VARCHAR(100), __SHIPFROM_STREET VARCHAR(100),
__SHIPFROM_CITY VARCHAR(100), __SHIPFROM_PROVINCE VARCHAR(100), __SHIPFROM_COUNTRY VARCHAR(100),
__SHIPFROM_POSTAL VARCHAR(100), __TOTALWEIGHT DECIMAL(19,4), __TOTALBOXES DECIMAL(19,4),
__SHIPTO_PROVINCE VARCHAR(100), __SHIPTO_POSTAL VARCHAR(100), __SHIPTO_CITY VARCHAR(100),
__TOTALPRICE DECIMAL(19,4), __OURMARUP DECIMAL(19,4), __OURSLEEVECHARGE DECIMAL(19,4),
__ORDERID INT)
BEGIN
INSERT INTO `shipmentCostLog`
(
`CARRIER`,
`METHOD`,
`SHIPFROM_NAME`,
`SHIPFROM_STREETNUM`,
`SHIPFROM_STREET`,
`SHIPFROM_CITY`,
`SHIPFROM_PROVINCE`,
`SHIPFROM_COUNTRY`,
`SHIPFROM_POSTAL`,
`TOTALWEIGHT`,
`TOTALBOXES`,
`SHIPTO_PROVINCE`,
`SHIPTO_POSTAL`,
`SHIPTO_CITY`,
`TOTALPRICE`,
`OURMARKUP`,
`OURSLEEVECHARGE`,
`ORDERID`,
`CREATEDATETIME`)
VALUES
(
__CARRIER,
__METHOD,
__SHIPFROM_NAME,
__SHIPFROM_STREETNUM,
__SHIPFROM_STREET,
__SHIPFROM_CITY,
__SHIPFROM_PROVINCE,
__SHIPFROM_COUNTRY,
__SHIPFROM_POSTAL,
__TOTALWEIGHT,
__TOTALBOXES,
__SHIPTO_PROVINCE,
__SHIPTO_POSTAL,
__SHIPTO_CITY,
__TOTALPRICE,
__OURMARUP,
__OURSLEEVECHARGE,
__ORDERID,
CURRENT_TIMESTAMP);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `AdjustItemQty` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `AdjustItemQty`(__ITEMID INT, __QTY INT, __REASON VARCHAR(200))
BEGIN
	UPDATE 
		itemsInventory
	SET
		QTYONHAND = QTYONHAND + __QTY
	WHERE
		ITEMID = __ITEMID;
            
	INSERT INTO 
		itemsAdjustmentLog
       (ITEMID,QTY,ADJDATE,REASON)
	VALUES 
		(__ITEMID,__QTY,CURRENT_TIMESTAMP,__REASON);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ApplyPromoCode` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `ApplyPromoCode`(__USERNAME VARCHAR(50), __PROMO VARCHAR(50))
BEGIN
 SET @RESULT = '';
 
 SET @ORDERID = (SELECT ORDERID FROM orderHeader WHERE ORDERSTATUS = 'CART' AND USERNAME = __USERNAME);
 SET @PROMOTYPE= (SELECT CODETYPE FROM promos WHERE CODE = __PROMO);
 SET @CHKCOUNT= (SELECT COUNT(*) FROM promos WHERE CODE = __PROMO);
 SET @CHKREDEEMED= (SELECT COUNT(*) FROM promos WHERE CODE = __PROMO AND REDEEMED = 0);
 SET @CHKDATES= (SELECT COUNT(*) FROM promos WHERE CODE = __PROMO AND STARTDATE <= CURRENT_TIMESTAMP AND ENDDATE >= CURRENT_TIMESTAMP);
 SET @CHKPROMOCOUNT= (SELECT COUNT(*) FROM orderPromos WHERE ORDERID = @ORDERID);
 SET @CHKITEMCOUNT= (SELECT COUNT(*) 
 FROM orderDetail D 
 INNER JOIN items I ON D.ITEMID = I.ID 
 INNER JOIN promosItems PI ON (PI.ITEMID = I.ID OR PI.ITEMID = I.PARENTITEM) 
 INNER JOIN promos PIHDR ON PI.CODE = PIHDR.CODE
 WHERE 
 ORDERID = @ORDERID AND 
 PI.CODE = __PROMO AND
 ((PIHDR.QTYTYPE = '' AND (PIHDR.MAXQTY = 0 OR ORDERQTY <= PIHDR.MAXQTY)) OR PIHDR.QTYTYPE = 'NOLIMIT' OR (PIHDR.QTYTYPE = 'EXACT' AND ORDERQTY = PIHDR.EXACTQTY) OR (PIHDR.QTYTYPE = 'RANGE' AND PIHDR.MINQTY <= ORDERQTY AND ORDERQTY <= PIHDR.MAXQTY))
 ); 
SET @CHKALLCATCOUNT= (SELECT
COUNT(*)
FROM orderDetail D 
INNER JOIN items I ON D.ITEMID = I.ID 
INNER JOIN itemsSubcategoryLink ISL ON I.ID = ISL.ITEMID
INNER JOIN subcategories SUBCATS ON ISL.SUBCATID = SUBCATS.SUBCATID
CROSS JOIN (SELECT * FROM promos WHERE CODE = __PROMO) PROMO 
WHERE
D.ORDERID = @ORDERID AND 
SUBCATS.CATID NOT IN (SELECT CATID FROM promos PIHDR INNER JOIN promosCategories PC ON PC.CODE = PIHDR.CODE WHERE PC.CODE = __PROMO) AND
((PROMO.QTYTYPE = '' AND (PROMO.MAXQTY = 0 OR ORDERQTY <= PROMO.MAXQTY)) OR PROMO.QTYTYPE = 'NOLIMIT' OR (PROMO.QTYTYPE = 'EXACT' AND ORDERQTY = PROMO.EXACTQTY) OR (PROMO.QTYTYPE = 'RANGE' AND PROMO.MINQTY <= ORDERQTY AND ORDERQTY <= PROMO.MAXQTY))
);
SET @CHKNOTITEMCOUNT= (SELECT COUNT(*)
 FROM orderDetail D 
 INNER JOIN items I ON D.ITEMID = I.ID 
 INNER JOIN promos PIHDR ON PIHDR.CODE = __PROMO
 LEFT OUTER JOIN promosItems PI ON (PI.ITEMID = I.ID OR PI.ITEMID = I.PARENTITEM) 
 WHERE 
 ORDERID = @ORDERID AND 
 PI.ITEMID IS NULL AND 
 ((PIHDR.QTYTYPE = '' AND (PIHDR.MAXQTY = 0 OR ORDERQTY <= PIHDR.MAXQTY)) OR PIHDR.QTYTYPE = 'NOLIMIT' OR (PIHDR.QTYTYPE = 'EXACT' AND ORDERQTY = PIHDR.EXACTQTY) OR (PIHDR.QTYTYPE = 'RANGE' AND PIHDR.MINQTY <= ORDERQTY AND ORDERQTY <= PIHDR.MAXQTY))
 ); 

  IF @CHKCOUNT=0 THEN
    SET @RESULT = 'NOEXIST';
  ELSEIF @CHKDATES = 0 THEN 
    SET @RESULT = 'NOTACTIVEDATE';
  ELSEIF @CHKREDEEMED = 0 THEN 
    SET @RESULT = 'ALREADYREDEEMED';        
  ELSEIF @CHKPROMOCOUNT > 0 THEN 
    SET @RESULT = 'ALREADYPROMO';   
  ELSEIF @CHKITEMCOUNT = 0 AND (@PROMOTYPE = 'ITM$DISC' OR @PROMOTYPE = 'ITM%DISC') THEN
    SET @RESULT = 'NOVALIDITEMS';
  ELSEIF @CHKALLCATCOUNT = 0 AND (@PROMOTYPE = 'ALLCAT$DISC' OR @PROMOTYPE = 'ALLCAT%DISC') THEN
    SET @RESULT = 'NOVALIDITEMS'; 
  ELSEIF @CHKNOTITEMCOUNT = 0 AND (@PROMOTYPE = 'ALLITM$DISC' OR @PROMOTYPE = 'ALLITM%DISC') THEN
    SET @RESULT = 'NOVALIDITEMS';
  ELSE
    SET @RESULT = 'OK';
        INSERT INTO orderPromos
        (ORDERID,PROMOCODE,PROMONAME,SINGLEITEM,PROMOTYPE,PROMOVALUE,CALCDISCOUNT,
         TAXAMT1,TAXAMT2,TAXAMT3,TAXAMT4,TAXAMT5)
    SELECT
      @ORDERID,CODE,NAME,SINGLEITEM,CODETYPE,PROMOVALUE,0.00,0.00,0.00,0.00,0.00,0.00
    FROM
      promos
    WHERE 
      CODE = __PROMO;
        
        UPDATE promos SET REDEEMED = 1 WHERE CODE = __PROMO AND SINGLEUSE = 1;
        
  END IF;

  SELECT @RESULT AS RESULT;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ApproveItem` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ApproveItem`(__CODE VARCHAR(200))
BEGIN

	SELECT
		ITEM.ID AS ITEMID,
        USERS.EMAIL AS USEREMAIL,
        ITEM.DISPLAYIMAGEPATH
	FROM
		items ITEM 
	INNER JOIN
		user USERS ON 
			USERS.USERNAME = ITEM.GENERATEDBY
	WHERE 
		APPROVALLINK = __CODE;

	UPDATE
		items
	SET
		ASSETAPPROVED = 1,
        APPROVALLINK = NULL
	WHERE
		APPROVALLINK = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ApproveOrder` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `ApproveOrder`(__APPROVALLINK NVARCHAR(100))
BEGIN

SET @ORDERREF = IFNULL((SELECT MAX(ORDERREF)+1 FROM orderHeader),10000);
UPDATE orderDetail
    SET 
        SHIPTODATEQTY = ORDERQTY,
        DOWNLOADLINK = CONCAT(ORDERID,'-',ITEMLINE,'-',MD5(MD5(MD5(NOW())+ORDERID)+ITEMLINE))
  WHERE
    ORDERQTY = -2 AND
        ORDERID IN (SELECT ORDERID FROM orderHeader WHERE APPROVALLINK  COLLATE utf8_unicode_ci = __APPROVALLINK AND APPROVALLINK IS NOT NULL);    

UPDATE
  orderHeader
SET
  ORDERREF = @ORDERREF
WHERE
  APPROVALLINK  COLLATE utf8_unicode_ci = __APPROVALLINK AND
    APPROVALLINK IS NOT NULL;


SELECT
  HDR.*,
    DTL.*,
    ITEM.TYPE,
    USER.FIRSTNAME AS USER_FIRSTNAME,
    USER.LASTNAME AS USER_LASTNAME,
    USER.EMAIL AS USER_EMAIL,
    USER.LANGUAGE AS USER_LANGUAGE,
    BRANCH.NAME AS BRANCH_NAME,
    BRANCH.MANAGEMENTEMAIL AS BRANCH_MANAGEMENTEMAIL,
    COALESCE(BRANCHADDRESS.LANGUAGE,'EN') AS BRANCH_LANGUAGE,
    'ACTIVE' AS REALSTATUS,
    COALESCE(ITEM.DLSUGGESTIONS,'') AS DLSUGGESTIONS
FROM
  orderHeader HDR
INNER JOIN
  orderDetail DTL ON 
    HDR.ORDERID = DTL.ORDERID
INNER JOIN
  items ITEM ON 
    DTL.ITEMID = ITEM.ID        
INNER JOIN
  user USER ON 
    HDR.USERNAME = USER.USERNAME
INNER JOIN
  branches BRANCH ON 
    USER.BRANCH = BRANCH.CODE
LEFT OUTER JOIN
  (SELECT * FROM branchesAddresses WHERE ISMAIN = 1) BRANCHADDRESS ON 
    BRANCH.CODE = BRANCHADDRESS.CODE
WHERE
  HDR.APPROVALLINK  COLLATE utf8_unicode_ci = __APPROVALLINK AND
    HDR.APPROVALLINK IS NOT NULL;
      
SELECT @SHIPLINECOUNT := COUNT(*) FROM orderDetail 
    WHERE ORDERQTY <> -2 AND
        ORDERID IN (SELECT ORDERID FROM orderHeader WHERE APPROVALLINK  COLLATE utf8_unicode_ci = __APPROVALLINK AND APPROVALLINK IS NOT NULL);
      
      
UPDATE
  orderHeader
SET
  APPROVALLINK = NULL,
    ORDERSTATUS = CASE WHEN @SHIPLINECOUNT = 0 THEN 'SHIPPED' ELSE 'ACTIVE' END,
    ORDERMODIFIED = CURRENT_TIMESTAMP,
    APPROVALDATE = CURRENT_TIMESTAMP
WHERE
  APPROVALLINK  COLLATE utf8_unicode_ci = __APPROVALLINK AND
    APPROVALLINK IS NOT NULL; 

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ArchiveItem` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ArchiveItem`(__ITEMID INT, __USERNAME VARCHAR(50))
BEGIN

	UPDATE
		items
	SET
		ARCHIVED = 1
	WHERE
		ID = __ITEMID AND
        ID IN (SELECT ITEMID FROM itemsBranchesLink LINK INNER JOIN user USR ON LINK.BRANCHCODE = USR.BRANCH WHERE USR.USERNAME = __USERNAME);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CancelRemaindingOrder` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CancelRemaindingOrder`(__ORDERID INT)
BEGIN

UPDATE
	orderHeader
SET
	ORDERSTATUS = 'CANCELLED',
    SPECIALINSTRUCTIONS = 'Order Cancelled on ' + CURRENT_TIMESTAMP
    
WHERE
	ORDERID = __ORDERID;

UPDATE
	orderDetail
SET
	ORDERQTY = SHIPTODATEQTY
WHERE
	ORDERID = __ORDERID;
    
SELECT * FROM orderHeader WHERE ORDERID = __ORDERID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ChangeAccountTicketStatus` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ChangeAccountTicketStatus`(__TICKETID INT, __STATUS INT)
BEGIN

	UPDATE
		accountMgrHeader
	SET
		STATUS = __STATUS,
        DATECLOSED = CURRENT_TIMESTAMP
	WHERE
		ID = __TICKETID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ChangeOrderShipToAddress` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ChangeOrderShipToAddress`(__USERNAME VARCHAR(50), __ID VARCHAR(50))
BEGIN


SET SQL_SAFE_UPDATES = 0;

UPDATE
	orderHeader
SET
	SHIPNAME = ( SELECT NAME FROM ( SELECT 'S' AS RC, ID,NAME FROM shipToAddresses UNION ALL SELECT 'B' AS RC,B.CODE,NAME FROM branches B INNER JOIN branchesAddresses BB ON B.CODE = BB.CODE WHERE ISMAIN = 1) ADDR WHERE CONCAT(ADDR.RC,'-',ADDR.ID) = __ID ),
    SHIPADDRESS = ( SELECT ADDRESS FROM ( SELECT 'S' AS RC, ID,ADDRESS FROM shipToAddresses UNION ALL SELECT 'B' AS RC,CODE,ADDRESS FROM branchesAddresses WHERE ISMAIN = 1) ADDR WHERE CONCAT(ADDR.RC,'-',ADDR.ID) = __ID ),
    SHIPADDRESS2 = ( SELECT ADDRESS2 FROM ( SELECT 'S' AS RC, ID,ADDRESS2 FROM shipToAddresses UNION ALL SELECT 'B' AS RC,CODE,ADDRESS2 FROM branchesAddresses WHERE ISMAIN = 1) ADDR WHERE CONCAT(ADDR.RC,'-',ADDR.ID) = __ID ),
    SHIPCITY = ( SELECT CITY FROM ( SELECT 'S' AS RC, ID,CITY FROM shipToAddresses UNION ALL SELECT 'B' AS RC,CODE,CITY FROM branchesAddresses WHERE ISMAIN = 1) ADDR WHERE CONCAT(ADDR.RC,'-',ADDR.ID) = __ID ),
	SHIPPROVINCE = ( SELECT PROVINCE FROM ( SELECT 'S' AS RC, ID,PROVINCE FROM shipToAddresses UNION ALL SELECT 'B' AS RC,CODE,PROVINCE FROM branchesAddresses WHERE ISMAIN = 1) ADDR WHERE CONCAT(ADDR.RC,'-',ADDR.ID) = __ID ),
    SHIPCOUNTRY = ( SELECT COUNTRY FROM ( SELECT 'S' AS RC, ID,COUNTRY FROM shipToAddresses UNION ALL SELECT 'B' AS RC,CODE,COUNTRY FROM branchesAddresses WHERE ISMAIN = 1) ADDR WHERE CONCAT(ADDR.RC,'-',ADDR.ID) = __ID ),
    SHIPPOSTAL = ( SELECT POSTAL FROM ( SELECT 'S' AS RC, ID,POSTAL FROM shipToAddresses UNION ALL SELECT 'B' AS RC,CODE,POSTALCODE FROM branchesAddresses WHERE ISMAIN = 1) ADDR WHERE CONCAT(ADDR.RC,'-',ADDR.ID) = __ID ),
	SHIPCONTACTPHONE = ( SELECT PHONE FROM ( SELECT 'S' AS RC, ID,PHONE FROM shipToAddresses UNION ALL SELECT 'B' AS RC,CODE,PHONE FROM branchesAddresses WHERE ISMAIN = 1) ADDR WHERE CONCAT(ADDR.RC,'-',ADDR.ID) = __ID ),
    SHIPCONTACTFAX = ( SELECT FAX FROM ( SELECT 'S' AS RC, ID,FAX FROM shipToAddresses UNION ALL SELECT 'B' AS RC,CODE,FAX FROM branchesAddresses WHERE ISMAIN = 1) ADDR WHERE CONCAT(ADDR.RC,'-',ADDR.ID) = __ID ),
    SHIPTAXGROUP = ( SELECT TAXGROUP FROM ( SELECT 'S' AS RC, ID,TAXGROUP FROM shipToAddresses UNION ALL SELECT 'B' AS RC,B.CODE,TAXGROUP FROM branches B INNER JOIN branchesAddresses BB ON B.CODE = BB.CODE WHERE ISMAIN = 1) ADDR WHERE CONCAT(ADDR.RC,'-',ADDR.ID) = __ID ),
    SHIPSTOREID = ( SELECT STOREID FROM ( SELECT 'S' AS RC, ID,STOREID FROM shipToAddresses UNION ALL SELECT 'B' AS RC,B.CODE,B.CODE AS STOREID FROM branches B INNER JOIN branchesAddresses BB ON B.CODE = BB.CODE WHERE ISMAIN = 1) ADDR WHERE CONCAT(ADDR.RC,'-',ADDR.ID) = __ID )
WHERE 
	USERNAME  = __USERNAME AND 
    ORDERSTATUS = 'CART';


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ChangeTicketStatus` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ChangeTicketStatus`(__TICKETID INT, __STATUS INT)
BEGIN

	UPDATE
		ticketHeader
	SET
		STATUS = __STATUS,
        DATECLOSED = CURRENT_TIMESTAMP
	WHERE
		ID = __TICKETID;

	
    SELECT 
		HDR.*,
        USR.EMAIL,
        USR.LANGUAGE
	FROM 
		ticketHeader HDR 
	LEFT OUTER JOIN
		user USR ON 
			HDR.ENTEREDBY = USR.USERNAME
	WHERE 
    ID = __TICKETID; 

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CheckCartItemDuplicates` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CheckCartItemDuplicates`(__USERNAME VARCHAR(50), __ITEMID INT)
BEGIN

SET @RECCNT := (SELECT 
					COUNT(*) 
				FROM 
					orderHeader HDR 
				INNER JOIN 
					orderDetail DTL ON 
						HDR.ORDERID = DTL.ORDERID 
				WHERE 
					HDR.USERNAME = __USERNAME AND
                    HDR.ORDERSTATUS = 'CART' AND
                    DTL.ITEMID = __ITEMID
				);

IF @RECCNT > 0 THEN
	SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Cannot add the same item to the cart.';
END IF;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CheckCartItemLowInventory` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CheckCartItemLowInventory`(__USERNAME VARCHAR(50), __ITEMID INT, __QTY INT)
BEGIN

	SELECT
		ITM.ID,
        IFNULL(ITMINV.QTYONHAND,0) AS INVQTY,
        IFNULL(ONORDER.ONORDER,0) AS ALLOCATEDQTY,
        CASE 
			WHEN 
				ITM.TYPE <> 0 THEN 1
			ELSE
				CASE WHEN __QTY <= IFNULL(ITMINV.QTYONHAND,0) - IFNULL(ONORDER.ONORDER,0) THEN 1 ELSE 0 END
		END AS ENOUGHQTY
	FROM
		items ITM
	LEFT OUTER JOIN
		itemsInventory ITMINV ON 
			ITM.ID = ITMINV.ITEMID
	LEFT OUTER JOIN
		( SELECT ITEMID,SUM(ORDERQTY-SHIPTODATEQTY) AS ONORDER FROM orderDetail
		  WHERE ORDERID IN (SELECT ORDERID FROM orderHeader WHERE ORDERSTATUS IN ('ACTIVE'))
          GROUP BY ITEMID) ONORDER ON
			ITM.ID = ONORDER.ITEMID
	WHERE
		ITM.ID = __ITEMID;


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CheckForLowLevelItems` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CheckForLowLevelItems`(__ORDERID INT)
BEGIN
	SELECT
		THISORDER.ORDERID,
        THISORDER.ORDERREF,
		ITEM.ID,
		ITEM.CODE,
        ITEM.DESCRIPTION,
        THISORDER.REMQTY AS ORDERQTY,
        COALESCE(OTHORDER.REMQTY,0) AS OTHQTY,
        ITEM.TYPE,
        COALESCE(ITEM.MINLEVEL,0) AS MINLEVEL,
        COALESCE(INV.QTYONHAND,0) AS QTYONHAND
	FROM
		( SELECT H.ORDERID, H.ORDERREF, ITEMID,SUM(ORDERQTY-SHIPTODATEQTY) AS REMQTY FROM orderHeader H INNER JOIN orderDetail D ON H.ORDERID = D.ORDERID WHERE  H.ORDERID = __ORDERID GROUP BY H.ORDERID, H.ORDERREF, D.ITEMID) THISORDER  
	INNER JOIN
		items ITEM ON 
			THISORDER.ITEMID = ITEM.ID
	LEFT OUTER JOIN
		itemsInventory INV ON 
			THISORDER.ITEMID = INV.ITEMID
	LEFT OUTER JOIN
		( SELECT ITEMID,SUM(ORDERQTY-SHIPTODATEQTY) AS REMQTY FROM orderHeader H INNER JOIN orderDetail D ON H.ORDERID = D.ORDERID WHERE ORDERSTATUS = 'ACTIVE' AND H.ORDERID <> __ORDERID GROUP BY D.ITEMID) OTHORDER ON 
			OTHORDER.ITEMID = THISORDER.ITEMID
	WHERE
		COALESCE(INV.QTYONHAND,0) - COALESCE(ITEM.MINLEVEL,0) - COALESCE(OTHORDER.REMQTY,0) - THISORDER.REMQTY <= 0
	ORDER BY
		ITEM.TYPE,
		ITEM.CODE;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CheckIfItemAppliesPromo` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CheckIfItemAppliesPromo`(__ORDERID INT, __PROMO VARCHAR(50), __ITEMID INT)
BEGIN

SELECT 
 PI.* 
FROM 
 orderDetail D 
INNER JOIN 
 items I ON D.ITEMID = I.ID 
INNER JOIN 
 promosItems PI ON (PI.ITEMID = I.ID OR PI.ITEMID = I.PARENTITEM)
INNER JOIN 
	promos PIHDR ON PI.CODE = PIHDR.CODE AND (PIHDR.MAXQTY = 0 OR D.ORDERQTY <= PIHDR.MAXQTY)
WHERE 
	ORDERID = __ORDERID AND PI.CODE = __PROMO AND D.ITEMID = __ITEMID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CheckIfItemAppliesPromoCat` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `CheckIfItemAppliesPromoCat`(__ORDERID INT, __PROMO VARCHAR(50), __ITEMID INT)
BEGIN

SELECT
 D.*
FROM orderDetail D 
INNER JOIN items I ON D.ITEMID = I.ID 
INNER JOIN itemsSubcategoryLink ISL ON I.ID = ISL.ITEMID
INNER JOIN subcategories SUBCATS ON ISL.SUBCATID = SUBCATS.SUBCATID
CROSS JOIN (SELECT * FROM promos WHERE CODE = __PROMO) PROMO 
WHERE
D.ORDERID = __ORDERID AND 
D.ITEMID = __ITEMID AND
SUBCATS.CATID NOT IN (SELECT CATID FROM promos PIHDR INNER JOIN promosCategories PC ON PC.CODE = PIHDR.CODE WHERE PC.CODE = __PROMO) AND
(PROMO.MAXQTY = 0 OR ORDERQTY <= PROMO.MAXQTY);


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CheckIfItemAppliesPromoExceptionList` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `CheckIfItemAppliesPromoExceptionList`(__ORDERID INT, __PROMO VARCHAR(50), __ITEMID INT)
BEGIN

SELECT 
 PIHDR.* 
FROM 
 orderDetail D 
INNER JOIN 
 items I ON D.ITEMID = I.ID 
INNER JOIN 
  promos PIHDR ON PIHDR.CODE = __PROMO AND (PIHDR.MAXQTY = 0 OR D.ORDERQTY <= PIHDR.MAXQTY)
LEFT OUTER JOIN 
  promosItems PI ON (PI.ITEMID = I.ID OR PI.ITEMID = I.PARENTITEM)
WHERE 
  ORDERID = __ORDERID AND D.ITEMID = __ITEMID AND PI.ITEMID IS NULL;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CheckUserAccount` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CheckUserAccount`(IN `UNAME` VARCHAR(50), IN `PASS` VARCHAR(50))
BEGIN
	SELECT 
		USERS.*,
        COALESCE(SKINDEF.STYLEFILENAME,'DEFAULT') AS SKIN,
        COALESCE(BRANCH.SKIN,'DEFAULT') AS BANNER
	FROM 
		user USERS
	LEFT OUTER JOIN
		branches BRANCH ON 
			USERS.BRANCH = BRANCH.CODE
	LEFT OUTER JOIN
		branchSkinDefinition SKINDEF ON 
			BRANCH.SKIN = SKINDEF.SKINNAME
	WHERE USERNAME=UNAME AND PASSWORD=SHA2(PASS, 256) AND USERS.STATUS=1;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CheckUserAccount_ByIdBranch` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CheckUserAccount_ByIdBranch`(__USERNAME VARCHAR(50), __BRANCHNAME VARCHAR(100))
BEGIN

	SELECT 
		USERS.*,
        COALESCE(SKINDEF.STYLEFILENAME,'DEFAULT') AS SKIN,
        COALESCE(BRANCH.SKIN,'DEFAULT') AS BANNER
	FROM 
		user USERS
	LEFT OUTER JOIN
		branches BRANCH ON 
			USERS.BRANCH = BRANCH.CODE
	LEFT OUTER JOIN
		branchSkinDefinition SKINDEF ON 
			BRANCH.SKIN = SKINDEF.SKINNAME
	WHERE 
        USERNAME=__USERNAME AND 
        USERS.STATUS=1 AND 
        USERS.USERLEVEL < 90;
    
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ClearAllAdBrandLink` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ClearAllAdBrandLink`(__ID INT)
BEGIN
	DELETE FROM AdItemsBrand WHERE ID = __ID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ClearAllCategoryBrandLink` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ClearAllCategoryBrandLink`(__CATID INT)
BEGIN
	DELETE FROM categoriesBrand WHERE CATID = __CATID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateAccountTicket` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateAccountTicket`(__ENTEREDBY VARCHAR(50), __LANGUAGE VARCHAR(20), __ACCOUNT VARCHAR(20), __BANNER VARCHAR(20),
__BRANCHNUMBER VARCHAR(20), __BRANCHNAME VARCHAR(100), __ADDRESS VARCHAR(1000), __CITY VARCHAR(100), __PROVINCE VARCHAR(100),
__POSTAL VARCHAR(100), __COUNTRY VARCHAR(100), __PHONE VARCHAR(100), __FAX VARCHAR(100), __APPOINTMENTPHONE VARCHAR(100),
__CONTACTFIRST VARCHAR(100), __CONTACTLAST VARCHAR(100), __EMAIL VARCHAR(100), __NOTE VARCHAR(1000),
__BOUTIQUEFIRST VARCHAR(100), __BOUTIQUELAST VARCHAR(100), __TAGLINENAME VARCHAR(100), __AFFILIATEDNAME VARCHAR(100),
__PHARMLEGALNAME VARCHAR(100), __PHARMPHOTONAME VARCHAR(100))
BEGIN

DECLARE __TICKETID INT;

INSERT INTO accountMgrHeader
(ENTEREDBY,DATEENTERED,STATUS,TITLE,DATECLOSED,LANGUAGE,ACCOUNT,BANNER,BRANCHNUMBER,ADDRESS,CITY,PROVINCE,COUNTRY,POSTAL,
PHONE,FAX,APPOINTMENTPHONE,CONTACTFIRST,CONTACTLAST,EMAIL,NOTE,BOUTIQUEFIRST,BOUTIQUELAST,TAGLINENAME,AFFILIATEDNAME,PHARMLEGALNAME,PHARMPHOTONAME)
VALUES
(__ENTEREDBY,CURRENT_TIMESTAMP,1,__BRANCHNAME,NULL,__LANGUAGE,__ACCOUNT,__BANNER,__BRANCHNUMBER,
__ADDRESS,__CITY,__PROVINCE,__COUNTRY,__POSTAL,__PHONE,__FAX,__APPOINTMENTPHONE,__CONTACTFIRST,__CONTACTLAST,__EMAIL,__NOTE,
__BOUTIQUEFIRST,__BOUTIQUELAST,__TAGLINENAME,__AFFILIATEDNAME, __PHARMLEGALNAME, __PHARMPHOTONAME);

SET __TICKETID = LAST_INSERT_ID();

SELECT *,UNIX_TIMESTAMP(DATEENTERED) AS UNIX_DATEENTERED FROM accountMgrHeader WHERE ID = __TICKETID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateAccountTicketReply` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateAccountTicketReply`(__TICKETID INT, __MESSAGE VARCHAR(1000), __ENTEREDBY VARCHAR(50))
BEGIN

 DECLARE __TICCHECK INT;
 SELECT COUNT(*) INTO __TICCHECK FROM accountMgrHeader WHERE ( ENTEREDBY = __ENTEREDBY OR __ENTEREDBY IN (SELECT USERNAME FROM user WHERE USERLEVEL >= 99)) AND ID = __TICKETID;
 
 IF __TICCHECK > 0 THEN

 INSERT INTO
 accountMgrMessages
 (TICKETID,DATEENTERED,ENTEREDBY,CLIENTMESSAGE,MESSAGE)
 VALUES
 (
 __TICKETID,
 CURRENT_TIMESTAMP,
 __ENTEREDBY,
 CASE WHEN __ENTEREDBY IN (SELECT USERNAME FROM user WHERE USERLEVEL >= 99) THEN 0 ELSE 1 END,
 __MESSAGE
 );
 
 SELECT 
 HDR.*,
 DTL.MESSAGE,
 DTL.DATEENTERED AS RDATEENTERED,
 USR.FIRSTNAME,
 USR.LASTNAME,
 USR.EMAIL,
 USR.LANGUAGE,
 USR2.FIRSTNAME AS RFIRSTNAME, 
 USR2.LASTNAME AS RLASTNAME,
 USR2.EMAIL AS REMAIL 
 FROM 
 accountMgrHeader HDR 
 LEFT OUTER JOIN
 accountMgrMessages DTL ON 
 HDR.ID = DTL.TICKETID
 LEFT OUTER JOIN
 user USR ON 
 HDR.ENTEREDBY = USR.USERNAME
 LEFT OUTER JOIN
 user USR2 ON 
 DTL.ENTEREDBY = USR2.USERNAME                
		WHERE 
		ID = __TICKETID AND
        DTL.MESSAGEID = LAST_INSERT_ID();
        
	END IF;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateAdBrandLink` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateAdBrandLink`(__ADID INT, __BRANDNAME VARCHAR(20))
BEGIN
	INSERT INTO AdItemsBrand (ID,BRANDNAME)
    VALUES (__ADID,__BRANDNAME);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateCategoryBrandLink` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateCategoryBrandLink`(__CATID INT, __BRANDNAME VARCHAR(20))
BEGIN
	INSERT INTO categoriesBrand (CATID,BRANDNAME)
    VALUES (__CATID,__BRANDNAME);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateItemBranchLink` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateItemBranchLink`(__ITEMID INT, __BRANCHCODE VARCHAR(20))
BEGIN
	INSERT INTO
		itemsBranchesLink
		(ITEMID,BRANCHCODE)
	VALUES
		(__ITEMID,__BRANCHCODE);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateItemCategoryLink` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateItemCategoryLink`(__ITEMID INT, __SUBCATID INT)
BEGIN
	INSERT INTO 
		itemsSubcategoryLink
		(ITEMID,SUBCATID)
	VALUES
		(__ITEMID,__SUBCATID);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateItemPricing` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateItemPricing`(__ITEMID INT, __QTY INT, __ITEMOPTION VARCHAR(30), __PRICE DECIMAL(19,4), __LENGTH DECIMAL(19,4), __WIDTH DECIMAL(19,4), __HEIGHT DECIMAL(19,4), __WEIGHT DECIMAL(19,4), __PIECES INT)
BEGIN
	INSERT INTO
		itemPricing
		(ITEMID,QTY,PRICE,LENGTH,WIDTH,HEIGHT,WEIGHT,PIECES,ITEMOPTION)
	VALUES
		(__ITEMID,__QTY,__PRICE,__LENGTH,__WIDTH,__HEIGHT,__WEIGHT,__PIECES,__ITEMOPTION);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateNewAd` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateNewAd`(__NAME VARCHAR(1000), __STARTDATE DATE, __ENDDATE DATE, __LANGUAGE VARCHAR(2), __SORTORDER INT, __URL VARCHAR(500), __ADPATH VARCHAR(200))
BEGIN

	INSERT INTO 
		AdItems
        (`NAME`,STARTDATE,ENDDATE,`LANGUAGE`,SORTORDER,IMAGEPATH,URL)
	VALUES
		(__NAME,__STARTDATE,__ENDDATE,__LANGUAGE,__SORTORDER,__ADPATH,__URL);

	SELECT LAST_INSERT_ID() AS ADID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateNewBranch` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateNewBranch`(__CODE VARCHAR(20), __NAME VARCHAR(100), __MGREMAIL VARCHAR(100), __TREEMAIL VARCHAR(100), __TAXGROUP VARCHAR(6), __SKIN VARCHAR(20), __STATUS INTEGER, __COMMENTS VARCHAR(1000), __SHIPZONE VARCHAR(20))
BEGIN
INSERT INTO
	branches (CODE,NAME,MANAGEMENTEMAIL,TREASUREREMAIL,TAXGROUP,SKIN,STATUS,COMMENTS,SHIPZONE)
VALUES
	(__CODE, __NAME, __MGREMAIL, __TREEMAIL,__TAXGROUP,__SKIN, __STATUS, __COMMENTS, __SHIPZONE);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateNewBranchAddress` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateNewBranchAddress`(__CODE VARCHAR(20), __LANG VARCHAR(2), __ISMAIN INTEGER, __ADDRESS VARCHAR(1000), __ADDRESS2 VARCHAR(1000), __CITY VARCHAR(100), __PROVINCE VARCHAR(100), __COUNTRY VARCHAR(100), __POSTALCODE VARCHAR(20), __PHONE VARCHAR(20), __FAX VARCHAR(20))
BEGIN

INSERT INTO
	branchesAddresses
		(CODE, LANGUAGE, ISMAIN, ADDRESS, ADDRESS2, CITY, PROVINCE, COUNTRY, POSTALCODE, PHONE, FAX)
VALUES
	(__CODE, __LANG, __ISMAIN, __ADDRESS, __ADDRESS2, __CITY, __PROVINCE, __COUNTRY, __POSTALCODE, __PHONE, __FAX);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateNewCategory` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateNewCategory`(__NAME VARCHAR(50), __DESCRIPTION VARCHAR(200), __LANGUAGE VARCHAR(2), __ICON VARCHAR(200), __USERLEVEL INTEGER, __COMMENTS VARCHAR(1000))
BEGIN

	INSERT	INTO
		categories
        (NAME,DESCRIPTION,ICONURL,LANGUAGE,USERLEVEL,COMMENTS,CREATEDDATE,MODIFIEDDATE)
	VALUES
		(__NAME,__DESCRIPTION,__ICON,__LANGUAGE,__USERLEVEL,__COMMENTS,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP);

	SELECT LAST_INSERT_ID() AS CATID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateNewItem` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `CreateNewItem`(IN `__TYPE` INT, IN `__CODE` VARCHAR(50), IN `__DESCRIPTION` VARCHAR(200), IN `__PRODUCEDETAIL` VARCHAR(1000), IN `__SIZE` VARCHAR(50), IN `__NUMPAGES` VARCHAR(50), IN `__PRINTINGTYPE` VARCHAR(50), IN `__STOCK` VARCHAR(50), IN `__STATUS` INT, IN `__MINLEVEL` INT, IN `__LEADTIME` INT, IN `__INITIALQTY` INT, IN `__DISPLAYNAME` VARCHAR(1000), IN `__PROOFNAME` VARCHAR(1000), IN `__PARAMETERSNAME` VARCHAR(1000), IN `__UPLOADDIR` VARCHAR(1000), IN `__PREVIEWNAME` VARCHAR(1000), IN `__SHIPBOXINSLEEVE` INT, IN `__SKIPPDFPERMISSIONCHECK` INT, IN `__DLSUGGESTIONS` VARCHAR(1000))
BEGIN
        INSERT INTO 
                items
                (TYPE,CODE,DESCRIPTION,PRODUCEDETAIL,SIZE,NUMPAGES,PRINTINGTYPE,STOCK,STATUS,MINLEVEL,LEADTIME,DISPLAYIMAGENAME,PROOFPDFNAME,PARAMETERSFILENAME,PREVIEWIMAGENAME,DISPLAYIMAGEPATH,PROOFPDFPATH,PARAMETERSFILEPATH,PREVIEWIMAGEPATH,SHIPBOXINSLEEVE,SKIPPDFPERMISSIONCHECK,DLSUGGESTIONS)
        VALUES
                (__TYPE,__CODE,__DESCRIPTION,__PRODUCEDETAIL,__SIZE,__NUMPAGES,__PRINTINGTYPE,__STOCK,__STATUS,__MINLEVEL,__LEADTIME,__DISPLAYNAME,__PROOFNAME,__PARAMETERSNAME,__PREVIEWNAME,
         CASE WHEN LENGTH(__DISPLAYNAME) > 0 THEN CONCAT(__UPLOADDIR,__DISPLAYNAME) ELSE '' END,
         CASE WHEN LENGTH(__PROOFNAME) > 0 THEN CONCAT(__UPLOADDIR,__PROOFNAME) ELSE '' END,
         CASE WHEN LENGTH(__PARAMETERSNAME) > 0 THEN CONCAT(__UPLOADDIR,__PARAMETERSNAME) ELSE '' END,
         CASE WHEN LENGTH(__PREVIEWNAME) > 0 THEN CONCAT(__UPLOADDIR,__PREVIEWNAME) ELSE '' END,
         __SHIPBOXINSLEEVE,
         __SKIPPDFPERMISSIONCHECK,
         __DLSUGGESTIONS
                );
 
        SELECT LAST_INSERT_ID() AS ITEMID;
           
        IF __TYPE = 0 THEN
                INSERT INTO 
                        itemsInventory
                        (ITEMID,QTYONHAND)
                VALUES (LAST_INSERT_ID(),0);
        END IF;
 
        IF __INITIALQTY > 0 THEN
                UPDATE 
                        itemsInventory
                SET
                        QTYONHAND = __INITIALQTY
                WHERE
                        ITEMID = LAST_INSERT_ID();
            
                INSERT INTO 
                        itemsAdjustmentLog
            (ITEMID,QTY,ADJDATE,REASON)
                VALUES (LAST_INSERT_ID(),__INITIALQTY,CURRENT_TIMESTAMP,'Initial Inventory');
    END IF;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateNewNewsItem` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateNewNewsItem`(__STARTDATE DATE, __ENDDATE DATE, __LANGUAGE VARCHAR(2), __TITLE VARCHAR(1000), __MESSAGE VARCHAR(1000))
BEGIN
	INSERT INTO 
		newsItems
		(STARTDATE,ENDDATE,LANGUAGE,TITLE,MESSAGE)
	VALUES
		(__STARTDATE,__ENDDATE,__LANGUAGE,__TITLE,__MESSAGE);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateNewPromo` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `CreateNewPromo`(__NAME VARCHAR(100), __CODE VARCHAR(50), __CODETYPE VARCHAR(12), __PROMOVALUE DECIMAL(19,2), __QTYTYPE VARCHAR(12), __MAXQTY INT, __MINQTY INT, __EXACTQTY INT, __STARTDATE DATE, __ENDDATE DATE, __SINGLEUSE INT, __REDEEMED INT, __SINGLEITEM INT)
BEGIN

 INSERT INTO 
 promos
 (NAME,CODE,CODETYPE,PROMOVALUE,QTYTYPE,MAXQTY,MINQTY,EXACTQTY,STARTDATE,ENDDATE,SINGLEUSE,REDEEMED,SINGLEITEM)
 VALUES
 (__NAME,__CODE,__CODETYPE,__PROMOVALUE,__QTYTYPE,__MAXQTY,__MINQTY,__EXACTQTY,__STARTDATE,__ENDDATE,__SINGLEUSE,__REDEEMED,__SINGLEITEM);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateNewShipToAddress` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `CreateNewShipToAddress`(__NAME VARCHAR(100), __STOREID VARCHAR(20), __ADDRESS VARCHAR(1000), __ADDRESS2 VARCHAR(1000), __CITY VARCHAR(100),
__PROVINCE VARCHAR(100), __COUNTRY VARCHAR(100), __POSTAL VARCHAR(20), __PHONE VARCHAR(20), __FAX VARCHAR(20),
__TAXGROUP VARCHAR(6), __SHIPZONE VARCHAR(6))
BEGIN

INSERT INTO
	shipToAddresses
    (NAME,STOREID,ADDRESS,ADDRESS2,CITY,PROVINCE,COUNTRY,POSTAL,PHONE,FAX,TAXGROUP,SHIPZONE)
VALUES
	(__NAME,__STOREID,__ADDRESS,__ADDRESS2,__CITY,__PROVINCE,__COUNTRY,__POSTAL,__PHONE,__FAX,__TAXGROUP,__SHIPZONE);

SELECT LAST_INSERT_ID() AS SHIPID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateNewSubCategory` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `CreateNewSubCategory`(__CATID INTEGER, __PARENTSUBCATID INTEGER, __NAME VARCHAR(50), __DESCRIPTION VARCHAR(200), __ICONURL VARCHAR(200), __USERLEVEL INT)
BEGIN
    INSERT INTO subcategories (CATID,PARENTSUBCATID,NAME,DESCRIPTION,ICONURL,USERLEVEL)
    VALUES (__CATID,__PARENTSUBCATID,__NAME,__DESCRIPTION, __ICONURL, __USERLEVEL);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateNewUser` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateNewUser`(IN `UNAME` VARCHAR(50), IN `__BRANCH` VARCHAR(20), IN `PASS` VARCHAR(50), IN `FNAME` VARCHAR(50), IN `LNAME` VARCHAR(50), IN `LANG` VARCHAR(2), IN `STAT` INT, IN `USERLVL` INT, IN `COMMENTS` VARCHAR(1000), IN `EMAIL` VARCHAR(100))
BEGIN

INSERT INTO 
	`user` (`USERNAME`,`BRANCH`,`PASSWORD`,`PLAINPASSWORD`,`FIRSTNAME`,`LASTNAME`,`LANGUAGE`,`STATUS`,`USERLEVEL`,`COMMENTS`,`EMAIL`) 
VALUES 
  (UNAME,__BRANCH,SHA2(PASS, 256),PASS,FNAME,LNAME,LANG,STAT,USERLVL,COMMENTS,EMAIL);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateShipmentHeader` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateShipmentHeader`(__ORDERID INT, __TRACKNUM VARCHAR(100), __METHOD VARCHAR(100), __AMOUNT DECIMAL(16,2), __WEIGHT DECIMAL(16,2), __PIECES INT, __INSTRUCTIONS VARCHAR(1000))
BEGIN

	INSERT INTO 
		shipmentHeader
        (ORDERID,TRACKNUM,SHIPDATE,SHIPMETHOD,SHIPAMOUNT,SHIPWEIGHT,SHIPPIECES,SHIPINSTRUCTIONS)
	VALUES
		(__ORDERID,__TRACKNUM,CURRENT_TIMESTAMP,__METHOD,__AMOUNT,__WEIGHT,__PIECES,__INSTRUCTIONS);
    
    SELECT
		SHIPID,
        ORDERID
	FROM
		shipmentHeader
	WHERE
		SHIPID = LAST_INSERT_ID();
    
    
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateShipmentLine` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateShipmentLine`(__SHIPID INT, __ORDERID INT, __ORDERLINE INT, __SHIPQTY INT)
BEGIN

	INSERT INTO
		shipmentDetail
		(SHIPID,ORDERID,ORDERLINE,SHIPQTY)
	VALUES
		(__SHIPID,__ORDERID,__ORDERLINE,__SHIPQTY);
        
	UPDATE
		orderDetail
	SET
		SHIPTODATEQTY = SHIPTODATEQTY + __SHIPQTY
	WHERE
		ORDERID = __ORDERID AND
        ITEMLINE = __ORDERLINE;
        
	UPDATE
		itemsInventory
	SET
		QTYONHAND = QTYONHAND - __SHIPQTY
	WHERE
		ITEMID IN (SELECT ITEMID FROM orderDetail WHERE ORDERID = __ORDERID AND ITEMLINE = __ORDERLINE);
        
	INSERT INTO 
		itemsAdjustmentLog
			(ITEMID,QTY,ADJDATE,REASON)
	SELECT
		ITEMID,
        __SHIPQTY * -1 AS QTY,
        CURRENT_TIMESTAMP AS ADJDATE,
        CONCAT('Shipment for Order# ', ORDERID, ' Ship# ', __SHIPID) AS REASON
	FROM
		orderDetail
	WHERE
		ORDERID = __ORDERID AND
        ITEMLINE = __ORDERLINE;  
        
	UPDATE
		orderHeader
	SET
		ORDERSTATUS = 'SHIPPED'
	WHERE
		ORDERID = __ORDERID AND
        ORDERID IN (SELECT ORDERID FROM orderDetail WHERE ORDERID = __ORDERID GROUP BY ORDERID HAVING SUM(ORDERQTY) = SUM(SHIPTODATEQTY));

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateShipToAddressUserLink` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `CreateShipToAddressUserLink`(__shipid INT, __username VARCHAR(30))
BEGIN

	INSERT INTO shipToAddressesUserList (ID,USERNAME) VALUES (__shipid, __username); 

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateSupportTicket` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateSupportTicket`(__TITLE VARCHAR(1000), __MESSAGE VARCHAR(1000), __ENTEREDBY VARCHAR(50))
BEGIN
 
 DECLARE __TICKETID INT;

 
 INSERT INTO ticketHeader
 (ENTEREDBY,DATEENTERED,STATUS,TITLE,DATECLOSED)
 VALUES
 (__ENTEREDBY,CURRENT_TIMESTAMP,1,__TITLE,NULL);
 
 
 SET __TICKETID = LAST_INSERT_ID();
 
 
 INSERT INTO ticketDetail
 (TICKETID,DATEENTERED,ENTEREDBY,CLIENTMESSAGE,MESSAGE)
 VALUES
 (__TICKETID,CURRENT_TIMESTAMP,__ENTEREDBY,1,__MESSAGE);
 
 
 SELECT 
 HDR.*,
 DTL.MESSAGEID,
 DTL.MESSAGE,
 USR.FIRSTNAME,
 USR.LASTNAME,
 USR.EMAIL,
 USR.LANGUAGE,
 UNIX_TIMESTAMP(DTL.DATEENTERED) AS UNIX_DATEENTERED
 FROM 
 ticketHeader HDR 
 LEFT OUTER JOIN
 ticketDetail DTL ON 
 HDR.ID = DTL.TICKETID
 LEFT OUTER JOIN
 user USR ON 
 HDR.ENTEREDBY = USR.USERNAME
 WHERE 
 ID = __TICKETID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateSupportTicketAttachment` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateSupportTicketAttachment`(__TICKETID INT, __MESSAGEID INT, __STORENAME VARCHAR(200), __FILENAME VARCHAR(200))
BEGIN

INSERT INTO ticketAttachment (TICKETID,MESSAGEID, STOREFILENAME,ORIGFILENAME)
VALUES (__TICKETID, __MESSAGEID, __STORENAME,__FILENAME);
	

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `CreateSupportTicketReply` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `CreateSupportTicketReply`(__TICKETID INT, __MESSAGE VARCHAR(1000), __ENTEREDBY VARCHAR(50))
BEGIN

DECLARE __TICCHECK INT;
 SELECT COUNT(*) INTO __TICCHECK FROM ticketHeader WHERE ( ENTEREDBY = __ENTEREDBY OR __ENTEREDBY IN (SELECT USERNAME FROM user WHERE USERLEVEL >= 81)) AND ID = __TICKETID;
 
 IF __TICCHECK > 0 THEN

		INSERT INTO
			ticketDetail
			(TICKETID,DATEENTERED,ENTEREDBY,CLIENTMESSAGE,MESSAGE)
		VALUES
			(
				__TICKETID,
				CURRENT_TIMESTAMP,
				__ENTEREDBY,
				CASE WHEN __ENTEREDBY IN (SELECT USERNAME FROM user WHERE USERLEVEL >= 81) THEN 0 ELSE 1 END,
				__MESSAGE
			);
            
		SELECT 
			HDR.*,
			DTL.MESSAGE,
			DTL.DATEENTERED AS RDATEENTERED,
            DTL.MESSAGEID,
			USR.FIRSTNAME,
			USR.LASTNAME,
			USR.EMAIL,
            USR.LANGUAGE,
			USR2.FIRSTNAME AS RFIRSTNAME, 
			USR2.LASTNAME AS RLASTNAME,
			USR2.EMAIL AS REMAIL            
		FROM 
			ticketHeader HDR 
		LEFT OUTER JOIN
			ticketDetail DTL ON 
				HDR.ID = DTL.TICKETID
		LEFT OUTER JOIN
			user USR ON 
				HDR.ENTEREDBY = USR.USERNAME
		LEFT OUTER JOIN
			user USR2 ON 
				DTL.ENTEREDBY = USR2.USERNAME                
		WHERE 
		ID = __TICKETID AND
        DTL.MESSAGEID = LAST_INSERT_ID();
        
	END IF;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeleteAdItem` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DeleteAdItem`(__ID INT)
BEGIN
	DELETE FROM AdItems WHERE ID = __ID;
    DELETE FROM AdItemsBrand WHERE ID = __ID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeleteAllBranchLinks` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DeleteAllBranchLinks`(__ITEMID INT)
BEGIN
	DELETE FROM itemsBranchesLink WHERE ITEMID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeleteAllItemCategoryLinks` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DeleteAllItemCategoryLinks`(__ITEMID INT)
BEGIN
	DELETE FROM itemsSubcategoryLink WHERE ITEMID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeleteAllPricingLinks` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DeleteAllPricingLinks`(__ITEMID INT)
BEGIN
	DELETE FROM itemPricing WHERE ITEMID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeleteAllShipToAddressUserLinks` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `DeleteAllShipToAddressUserLinks`(__shipid INT)
BEGIN
	DELETE FROM shipToAddressesUserList WHERE ID = __shipid; 
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeleteBranchAddress` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DeleteBranchAddress`(__CODE VARCHAR(20), __LANG VARCHAR(2))
BEGIN
	DELETE FROM branchesAddresses WHERE CODE = __CODE AND LANGUAGE = __LANG;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeleteBranchProfile` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DeleteBranchProfile`(__CODE VARCHAR(20))
BEGIN
	DELETE FROM branches WHERE CODE = __CODE;
    DELETE FROM branchesAddresses WHERE CODE = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeleteCategoryProfile` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DeleteCategoryProfile`(__CATID INTEGER)
BEGIN
	DELETE FROM categories WHERE CATID = __CATID;
    DELETE FROM subcategories WHERE CATID = __CATID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeleteItem` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DeleteItem`(__ITEMID INT)
BEGIN
  DELETE FROM items WHERE ID = __ITEMID;
    DELETE FROM itemPricing WHERE ITEMID = __ITEMID;
    DELETE FROM itemsBranchesLink WHERE ITEMID = __ITEMID;
    DELETE FROM itemsSubcategoryLink WHERE ITEMID = __ITEMID;
    DELETE FROM itemsInventory WHERE ITEMID = __ITEMID;
    DELETE FROM itemsPrintDefinition WHERE ID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeleteNewsItem` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DeleteNewsItem`(__ID INTEGER)
BEGIN
	DELETE FROM newsItems WHERE ID = __ID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeletePromoCategories` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `DeletePromoCategories`(__CODE VARCHAR(50))
BEGIN
  DELETE FROM promosCategories WHERE CODE = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeletePromoItems` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DeletePromoItems`(__CODE VARCHAR(50))
BEGIN
	DELETE FROM promosItems WHERE CODE = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeletePromoProfile` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `DeletePromoProfile`(__CODE VARCHAR(50))
BEGIN
  DELETE FROM promosCategories WHERE CODE = __CODE;
    DELETE FROM promosItems WHERE CODE = __CODE;
  DELETE FROM promos WHERE CODE = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeleteShipToAddress` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `DeleteShipToAddress`(__ID INT)
BEGIN

DELETE FROM shipToAddressesUserList WHERE ID = __ID;
DELETE FROM shipToAddresses WHERE ID = __ID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeleteSubCategoryProfile` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DeleteSubCategoryProfile`(__SUBCATID INTEGER)
BEGIN
	DELETE FROM subcategories WHERE SUBCATID = __SUBCATID;
    DELETE FROM subcategories WHERE PARENTSUBCATID = __SUBCATID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DeleteUserProfile` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DeleteUserProfile`(UNAME VARCHAR(100))
BEGIN

DELETE FROM user WHERE USERNAME = UNAME;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DuplicateBranchProfile` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DuplicateBranchProfile`(__CODE VARCHAR(20), __NEWCODE VARCHAR(20))
BEGIN

	INSERT INTO
		branches
        (CODE,NAME,MANAGEMENTEMAIL,TREASUREREMAIL,TAXGROUP,STATUS,COMMENTS,CREATEDDATE,MODIFIEDDATE,SHIPZONE)
	SELECT
		__NEWCODE AS CODE,
        NAME,
        MANAGEMENTEMAIL,
        TREASUREREMAIL,
        TAXGROUP,
        STATUS,
        COMMENTS,
        CURRENT_TIMESTAMP AS CREATEDDATE,
        CURRENT_TIMESTAMP AS MODIFIEDDATE,
        SHIPZONE
	FROM
		branches
	WHERE
		CODE = __CODE;
        
	INSERT INTO
		branchesAddresses
        (CODE,LANGUAGE,ISMAIN,ADDRESS,CITY,PROVINCE,COUNTRY,POSTALCODE,PHONE,FAX)
	SELECT
		__NEWCODE AS CODE,
        LANGUAGE,
        ISMAIN,
        ADDRESS,
        CITY,
        PROVINCE,
        COUNTRY,
        POSTALCODE,
        PHONE,
        FAX
	FROM
		branchesAddresses
	WHERE
		CODE = __CODE;

	INSERT INTO
		branchesAssetData
        (BRANCHCODE,TAG,VALUE)
    SELECT
		__NEWCODE AS BRANCHCODE,
        TAG,
        VALUE
	FROM
		branchesAssetData
	WHERE
		BRANCHCODE = __CODE;

	INSERT INTO
		itemsBranchesLink
        (ITEMID,BRANCHCODE)
	SELECT
		ITEMID,
        __NEWCODE AS BRANCHCODE
	FROM
		itemsBranchesLink
	WHERE
		BRANCHCODE = __CODE;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DuplicateCategoryProfile` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DuplicateCategoryProfile`(__CATID INT)
BEGIN

INSERT INTO
	categories
	(NAME,DESCRIPTION,ICONURL,LANGUAGE,USERLEVEL,COMMENTS,CREATEDDATE,MODIFIEDDATE)
SELECT
	CONCAT(NAME,' **DUP**') AS NAME,
    DESCRIPTION,
    ICONURL,
    LANGUAGE,
    USERLEVEL,
    COMMENTS,
    CURRENT_TIMESTAMP AS CREATEDDATE,
    CURRENT_TIMESTAMP AS MODIFIEDDATE
FROM
	categories
WHERE
	CATID = __CATID;
    
SET @CATID = LAST_INSERT_ID();    
   
INSERT INTO categoriesBrand (CATID,BRANDNAME)
SELECT @CATID,BRANDNAME FROM categoriesBrand WHERE CATID = __CATID;
    
SELECT @CATID AS CATID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DuplicateItem` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DuplicateItem`(__ITEMID INT)
BEGIN

  INSERT INTO
    items
        (TYPE,CODE,DESCRIPTION,SIZE,NUMPAGES,STATUS,MINLEVEL,LEADTIME,PARENTITEM,
         ARCHIVED,GENERATEDBY,ASSETAPPROVED,SHIPBOXINSLEEVE,PRINTINGTYPE,STOCK) 
  SELECT
    TYPE,
        CONCAT(CODE,' **DUP**'), 
        DESCRIPTION,
        SIZE,
        NUMPAGES,
        STATUS,
        MINLEVEL,
        LEADTIME,
        PARENTITEM,
        ARCHIVED,
        GENERATEDBY,
        ASSETAPPROVED,
        SHIPBOXINSLEEVE,
        PRINTINGTYPE,
        STOCK
  FROM
    items
  WHERE
    ID = __ITEMID;
        
  SET @ITEMID = LAST_INSERT_ID();
 
  INSERT INTO
    itemPricing
        (ITEMID,QTY,PRICE,LENGTH,WIDTH,HEIGHT,WEIGHT,PIECES,ITEMOPTION)
  SELECT
    @ITEMID AS ITEMID,
        QTY,
        PRICE,
        LENGTH,
        WIDTH,
        HEIGHT,
        WEIGHT,
        PIECES,
        ITEMOPTION
  FROM
    itemPricing
  WHERE
    ITEMID = __ITEMID;
 
  INSERT INTO
    itemsBranchesLink
        (ITEMID,BRANCHCODE)
  SELECT
    @ITEMID AS ITEMID,
        BRANCHCODE
  FROM
    itemsBranchesLink
  WHERE
    ITEMID = __ITEMID;
        
  INSERT INTO
    itemsInventory
        (ITEMID,QTYONHAND)
  SELECT
    @ITEMID AS ITEMID,
        0 AS QTYONHAND
  FROM
    itemsInventory
  WHERE
    ITEMID = __ITEMID;
        
  INSERT INTO
    itemsSubcategoryLink
        (ITEMID,SUBCATID)
  SELECT
    @ITEMID AS ITEMID,
        SUBCATID
  FROM
    itemsSubcategoryLink
  WHERE
    ITEMID = __ITEMID;
        
  INSERT INTO
    itemsPrintDefinition
        (ID,PRESS,NUMPAGES,BLEEDS,COLOURS,FLATSIZE,FINISHSIZE,STOCK,PARENTSHEETSIZE,
         PRESSSHEETSIZE,NUMUP,LAYOUTORIENTATION,COATING,CONTOURCUT,LAMINATION,FINISHING,
         ACCESSORY,SPECIALINSTRUCTIONS,PACKAGELOCATION)
  SELECT
    @ITEMID AS ID,
        PRESS,NUMPAGES,BLEEDS,COLOURS,FLATSIZE,FINISHSIZE,STOCK,PARENTSHEETSIZE,
    PRESSSHEETSIZE,NUMUP,LAYOUTORIENTATION,COATING,CONTOURCUT,LAMINATION,FINISHING,
    ACCESSORY,SPECIALINSTRUCTIONS,PACKAGELOCATION
  FROM
    itemsPrintDefinition
  WHERE
    ID = __ITEMID;        
  
        
  SELECT @ITEMID AS ID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DuplicateShipToAddress` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `DuplicateShipToAddress`(__ID INT)
BEGIN

	INSERT INTO
		shipToAddresses
		(NAME,ADDRESS,CITY,PROVINCE,COUNTRY,POSTAL,PHONE,FAX,TAXGROUP,SHIPZONE)
	SELECT
		NAME,
        ADDRESS,
        CITY,
        PROVINCE,
        COUNTRY,
        POSTAL,
        PHONE,
        FAX,
        TAXGROUP,
        SHIPZONE
	FROM
		shipToAddresses
	WHERE
		ID = __ID;
        
	SELECT @SHIPID := LAST_INSERT_ID() AS ID;
    
    INSERT INTO 
		shipToAddressesUserList
        (ID,USERNAME)
	SELECT
		@SHIPID,
        USERNAME
	FROM
		shipToAddressesUserList
	WHERE
		ID = __ID;
    

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `DuplicateUserProfile` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `DuplicateUserProfile`(__USERNAME VARCHAR(50), __NEWUSERNAME VARCHAR(50))
BEGIN

	INSERT INTO
		user
        (USERNAME,BRANCH,PASSWORD,PLAINPASSWORD,FIRSTNAME,LASTNAME,EMAIL,LANGUAGE,USERLEVEL,CREATEDDATE,MODIFIEDDATE,STATUS,COMMENTS)
	SELECT
		__NEWUSERNAME AS USERNAME,
        BRANCH,
        PASSWORD,
        PLAINPASSWORD,
        FIRSTNAME,
        LASTNAME,
        EMAIL,
        LANGUAGE,
        USERLEVEL,
        CURRENT_TIMESTAMP AS CREATEDDATE,
        CURRENT_TIMESTAMP AS MODIFIEDDATE,
        STATUS,
        COMMENTS
	FROM
		user
	WHERE
		USERNAME = __USERNAME;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ExpireDownloadLinks` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ExpireDownloadLinks`()
BEGIN

UPDATE
	orderDetail
SET
	DOWNLOADLINK = NULL
WHERE
	DOWNLOADLINK IS NOT NULL AND
    ORDERID IN (SELECT ORDERID FROM orderHeader WHERE ORDERDATE < DATE_ADD(CURRENT_TIMESTAMP,INTERVAL -30 DAY));



END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_branchAssetDefinition` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_branchAssetDefinition`()
BEGIN
	SELECT * FROM branchAssetDefinition;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_branches` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_branches`()
BEGIN
	SELECT
		*
	FROM
		branches;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_branchesAddresses` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_branchesAddresses`()
BEGIN
	SELECT
		*
	FROM
		branchesAddresses;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_branchesAssetData` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_branchesAssetData`()
BEGIN
	SELECT
		*
	FROM
		branchesAssetData;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_branchesFixtures` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `extractTableData_branchesFixtures`()
BEGIN
	SELECT * FROM branchesFixtures ORDER BY BRANCHCODE,FIXTURECODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_branchSkinDefinition` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_branchSkinDefinition`()
BEGIN

	SELECT
		*
	FROM
		branchSkinDefinition;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_campaign_fixtures` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `extractTableData_campaign_fixtures`()
BEGIN
	SELECT * FROM campaign_fixtures;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_categories` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_categories`()
BEGIN
	SELECT
		*
	FROM
		categories;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_itemPricing` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_itemPricing`()
BEGIN
SELECT * FROM itemPricing left outer join items on itemPricing.ITEMID = items.ID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_items` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_items`()
BEGIN
SELECT
		*
	FROM
		items;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_itemsBranchesLink` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_itemsBranchesLink`()
BEGIN
SELECT
		*
	FROM
		itemsBranchesLink;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_itemsInventory` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_itemsInventory`()
BEGIN
SELECT
		*
	FROM
		itemsInventory;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_itemsPrintDefinition` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_itemsPrintDefinition`()
BEGIN 
  SELECT items.CODE, items.DESCRIPTION, itemsPrintDefinition.* from itemsPrintDefinition left join items on itemsPrintDefinition.ID = items.ID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_itemsSubcategoryLink` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_itemsSubcategoryLink`()
BEGIN
SELECT
		*
	FROM
		itemsSubcategoryLink;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_newsItems` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_newsItems`()
BEGIN
SELECT
		*
	FROM
		newsItems;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_promos` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_promos`()
BEGIN
	SELECT * FROM promos;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_promosCategories` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `extractTableData_promosCategories`()
BEGIN
SELECT * FROM promosCategories;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_promosItems` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_promosItems`()
BEGIN
	SELECT * FROM promosItems;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_shipToAddresses` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_shipToAddresses`()
BEGIN
	SELECT
		*
	FROM
		shipToAddresses;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_shipToAddressesUserList` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `extractTableData_shipToAddressesUserList`()
BEGIN
SELECT * FROM shipToAddressesUserList;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_shipZoneCosts` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_shipZoneCosts`()
BEGIN
SELECT
		*
	FROM
		shipZoneCosts;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_subcategories` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_subcategories`()
BEGIN
SELECT
		*
	FROM
		subcategories;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_taxes` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_taxes`()
BEGIN
SELECT
		*
	FROM
		taxes;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extractTableData_user` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extractTableData_user`()
BEGIN
SELECT
		*
	FROM
		user;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `extract_TableData_branchAssetDefinition` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `extract_TableData_branchAssetDefinition`()
BEGIN
	SELECT * FROM branchAssetDefinition;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetAccountTicket` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetAccountTicket`(__TICKETID INT)
BEGIN

SELECT 
 HDR.*,
 DTL.*,
 USR.FIRSTNAME,
 USR.LASTNAME,
 UNIX_TIMESTAMP(DTL.DATEENTERED) AS UNIX_DATEENTERED 
FROM 
 accountMgrHeader HDR
LEFT OUTER JOIN
 accountMgrMessages DTL ON 
 HDR.ID = DTL.TICKETID
LEFT OUTER JOIN
user USR ON 
 DTL.ENTEREDBY = USR.USERNAME 
WHERE 
 ID = __TICKETID
ORDER BY
 DTL.DATEENTERED DESC;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetAccountTicketList` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetAccountTicketList`(__USERNAME VARCHAR(50))
BEGIN

	SELECT
		*,
        UNIX_TIMESTAMP(HDR.DATEENTERED) AS UNIX_DATEENTERED
	FROM
		accountMgrHeader HDR
	WHERE
		( HDR.STATUS = 1 OR (HDR.STATUS = 0 AND DATECLOSED >= DATE_ADD(NOW(), INTERVAL -14 DAY)) ) AND
        ( HDR.ENTEREDBY = __USERNAME OR __USERNAME IN (SELECT USERNAME FROM user WHERE USERLEVEL >= 99))
	ORDER BY
		HDR.STATUS DESC,
		HDR.DATEENTERED DESC;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetActiveAdItems` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetActiveAdItems`(__LANGUAGE VARCHAR(2), __SKIN VARCHAR(20))
BEGIN
	SELECT DISTINCT
		ADS.*
	FROM
		AdItems ADS
	INNER JOIN
		AdItemsBrand ADSBRAND ON 
			ADS.ID = ADSBRAND.ID
	WHERE
		(LANGUAGE = '--' OR LANGUAGE = __LANGUAGE) AND
        (ADSBRAND.BRANDNAME = 'ALL' OR ADSBRAND.BRANDNAME = __SKIN) AND
        CURRENT_TIMESTAMP >= STARTDATE AND CURRENT_TIMESTAMP <= ENDDATE
	ORDER BY
		COALESCE(SORTORDER,999);        
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetActiveNewsItems` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetActiveNewsItems`(__LANGUAGE VARCHAR(2))
BEGIN
	SELECT
		*
	FROM
		newsItems
	WHERE
		(LANGUAGE = '--' OR LANGUAGE = __LANGUAGE) AND
        CURRENT_TIMESTAMP >= STARTDATE AND CURRENT_TIMESTAMP <= ENDDATE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetAdBrandInformation` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetAdBrandInformation`(__ID INT)
BEGIN

SELECT 
	BSD.SKINNAME AS BRANDNAME,
    CASE WHEN ADB.BRANDNAME IS NOT NULL THEN 1 ELSE 0 END AS SELECTED
FROM
	(
		SELECT SKINNAME FROM branchSkinDefinition UNION ALL
        SELECT 'ALL' UNION ALL
        SELECT ''
	) BSD
LEFT OUTER JOIN
	(SELECT * FROM AdItemsBrand WHERE ID = __ID ) ADB ON 
		BSD.SKINNAME = ADB.BRANDNAME
ORDER BY 
	BSD.SKINNAME;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetAdItem` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetAdItem`(__ID INT)
BEGIN
	SELECT * FROM AdItems WHERE ID = __ID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetAllAdItems` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetAllAdItems`()
BEGIN
	SELECT 
		*
	FROM	
		AdItems;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetAllNewsItems` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetAllNewsItems`()
BEGIN
	SELECT
		*
	FROM
		newsItems;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetAllShipToAddresses` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetAllShipToAddresses`(__USEBRANCHES BOOLEAN, __USERNAME VARCHAR(30))
BEGIN

  SELECT
    RECTYPE,ID,NAME,ADDRESS,STOREID
  FROM
    (
  SELECT
    'S' AS RECTYPE,SADDR.ID, SADDR.NAME,SADDR.ADDRESS,CASE WHEN IFNULL(SADDR.STOREID,'') = '' THEN 'NA' ELSE STOREID END AS STOREID
  FROM
    shipToAddresses SADDR
  WHERE
    SADDR.ID IN (SELECT ID FROM shipToAddressesUserList WHERE USERNAME = '**ALL**' OR USERNAME = 'ALL' OR USERNAME = __USERNAME) 
        
  UNION ALL
    
    SELECT
    'B' AS RECTYPE,BRANCH.CODE,BRANCH.NAME,BADDR.ADDRESS,BRANCH.CODE AS STOREID
    FROM
    branches BRANCH
  INNER JOIN
    branchesAddresses BADDR ON 
      BRANCH.CODE = BADDR.CODE
  WHERE
    __USEBRANCHES = 1 AND
        BRANCH.STATUS = 1 AND
        BADDR.ISMAIN = 1
    ) ADDR
    ORDER BY
    NAME;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetAvailableShippingZones` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetAvailableShippingZones`()
BEGIN
	SELECT  * FROM shipZoneCosts LIMIT 1;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetBranchAddressInformation` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetBranchAddressInformation`(__CODE VARCHAR(20))
BEGIN
	SELECT
		*
	FROM
		branchesAddresses
	WHERE
		CODE = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetBranchAssets` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetBranchAssets`(__CODE VARCHAR(20))
BEGIN

	SELECT
		BAD.TAG,
        BAD.TYPE,
        BAD.NAME,
	IFNULL(BAD.VALIDATEPATTERN,'') AS VALIDATEPATTERN,
	IFNULL(BAD.VALIDATEERRORMSG,'') AS VALIDATEERRORMSG,
        IFNULL(BDATA.VALUE,'') AS VALUE
	FROM
		branchAssetDefinition BAD
	LEFT OUTER JOIN
		(SELECT * FROM branchesAssetData WHERE BRANCHCODE = __CODE) BDATA ON 
			BAD.TAG = BDATA.TAG
	ORDER BY
		BAD.DISPLAYORDER;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetBranchDropDown` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetBranchDropDown`()
BEGIN
	SELECT CODE,NAME FROM branches WHERE STATUS = 1 ORDER BY NAME;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetBranchInformation` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetBranchInformation`(__CODE VARCHAR(20))
BEGIN
	SELECT * FROM branches WHERE CODE = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetBranchProfile` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetBranchProfile`(IN `__USERNAME` VARCHAR(50))
    NO SQL
SELECT
		BRANCH.NAME,
		BADDR.*
	FROM
		branches BRANCH
	INNER JOIN
		branchesAddresses BADDR ON 
			BRANCH.CODE = BADDR.CODE
	WHERE
		ISMAIN = 1 AND
		BRANCH.CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetCartCheckoutInformation` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetCartCheckoutInformation`(__USERNAME VARCHAR(20))
BEGIN

SET SQL_SAFE_UPDATES = 0;

UPDATE
	orderHeader
SET
	ORDERDATE = CURRENT_TIMESTAMP,
    ORDERMODIFIED = CURRENT_TIMESTAMP,
    EXPDATE = DATE_ADD(CURRENT_TIMESTAMP,INTERVAL 8 DAY),
    TAXNAME1 = (SELECT TAXNAME1 FROM taxes TAXES  WHERE SHIPTAXGROUP = TAXES.TAXCODE),
    TAXNAME2 = (SELECT TAXNAME2 FROM taxes TAXES  WHERE SHIPTAXGROUP = TAXES.TAXCODE),
    TAXNAME3 = (SELECT TAXNAME3 FROM taxes TAXES  WHERE SHIPTAXGROUP = TAXES.TAXCODE),
    TAXNAME4 = (SELECT TAXNAME4 FROM taxes TAXES  WHERE SHIPTAXGROUP = TAXES.TAXCODE),
    TAXNAME5 = (SELECT TAXNAME5 FROM taxes TAXES  WHERE SHIPTAXGROUP = TAXES.TAXCODE),
    TAXRATE1 = (SELECT TAXRATE1 FROM taxes TAXES  WHERE SHIPTAXGROUP = TAXES.TAXCODE),
    TAXRATE2 = (SELECT TAXRATE2 FROM taxes TAXES  WHERE SHIPTAXGROUP = TAXES.TAXCODE),
    TAXRATE3 = (SELECT TAXRATE3 FROM taxes TAXES  WHERE SHIPTAXGROUP = TAXES.TAXCODE),
    TAXRATE4 = (SELECT TAXRATE4 FROM taxes TAXES  WHERE SHIPTAXGROUP = TAXES.TAXCODE),
    TAXRATE5 = (SELECT TAXRATE5 FROM taxes TAXES  WHERE SHIPTAXGROUP = TAXES.TAXCODE)
WHERE 
	USERNAME  = __USERNAME AND 
    ORDERSTATUS = 'CART';

UPDATE 
	orderDetail I 
INNER JOIN 
(
	SELECT
		DTL.ITEMLINE,
        DTL.EXTPRICE * HDR.TAXRATE1 AS TAXAMT1,
        DTL.EXTPRICE * HDR.TAXRATE2 AS TAXAMT2,
        DTL.EXTPRICE * HDR.TAXRATE3 AS TAXAMT3,
        DTL.EXTPRICE * HDR.TAXRATE4 AS TAXAMT4,
        DTL.EXTPRICE * HDR.TAXRATE5 AS TAXAMT5
	FROM
		orderHeader HDR
	INNER JOIN
		orderDetail DTL ON
			HDR.ORDERID = DTL.ORDERID
	WHERE
		USERNAME = __USERNAME AND 
		ORDERSTATUS = 'CART'		
) II ON 
	I.ITEMLINE = II.ITEMLINE
SET
	I.TAXAMT1 = II.TAXAMT1,
    I.TAXAMT2 = II.TAXAMT2,
    I.TAXAMT3 = II.TAXAMT3,
    I.TAXAMT4 = II.TAXAMT4,
    I.TAXAMT5 = II.TAXAMT5
;

SELECT
	HDR.*,
    DTL.*,
    ITEM.*,
    COALESCE(SGROUP.SGRPNAME,'') AS SGRPNAME,
    COALESCE(SGROUP.SGRPPIECES,1) AS SGRPPIECES,
    COALESCE(SGROUP.SGRPBOXTYPE,'') AS SGRPBOXTYPE,
    COALESCE(SGROUP.SGRPWEIGHTPIECE,1.0) AS SGRPWEIGHTPIECE
FROM
	orderHeader HDR
INNER JOIN
	orderDetail DTL ON 
		HDR.ORDERID = DTL.ORDERID
INNER JOIN
	items ITEM ON 
		DTL.ITEMID = ITEM.ID
LEFT OUTER JOIN
	shippingGroupItems SGROUPITEMS ON 
		ITEM.ID = SGROUPITEMS.ITEMID
LEFT OUTER JOIN 
	shippingGroup SGROUP ON 
		SGROUPITEMS.SGRPNAME = SGROUP.SGRPNAME
WHERE 
	USERNAME  = __USERNAME AND 
    ORDERSTATUS = 'CART';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetCartDiscountCodes` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetCartDiscountCodes`(__ORDERID INT)
BEGIN
	SELECT * FROM orderPromos WHERE ORDERID = __ORDERID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetCartItems` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetCartItems`(__USERNAME NVARCHAR(20))
BEGIN

SELECT 
	DTL.*
FROM 
	orderHeader HDR
INNER JOIN
	orderDetail DTL ON 
		HDR.ORDERID = DTL.ORDERID
WHERE 
	USERNAME COLLATE utf8_unicode_ci = __USERNAME AND 
    ORDERSTATUS = 'CART';


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetCategoriesBySearchTerm` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetCategoriesBySearchTerm`(__TERM VARCHAR(50))
BEGIN

SELECT
  CATID,
    CONCAT(NAME, ' - ', DESCRIPTION) AS CATNAME
FROM
  categories
WHERE
  (
  NAME LIKE CONCAT('%',__TERM,'%') OR 
    DESCRIPTION LIKE CONCAT('%',__TERM,'%')
    ) 
ORDER BY 
  NAME,DESCRIPTION
LIMIT 20;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetCategoriesListByUsername` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetCategoriesListByUsername`(__USERNAME VARCHAR(50))
BEGIN
	SELECT
		CATS.*
	FROM
		categories CATS
	CROSS JOIN
		( SELECT * FROM user WHERE USERNAME = __USERNAME) USERS
	WHERE
		USERS.USERLEVEL >= CATS.USERLEVEL AND
        CATS.LANGUAGE IN (SELECT BADDR.LANGUAGE FROM user USERS INNER JOIN branchesAddresses BADDR ON USERS.BRANCH = BADDR.CODE  WHERE USERS.USERNAME = __USERNAME) AND
        CATS.CATID IN (
						SELECT CC.CATID 
						FROM categories CC 
                        INNER JOIN categoriesBrand CCC ON CC.CATID = CCC.CATID
                        INNER JOIN ( SELECT SKIN FROM branches BB INNER JOIN user UU ON BB.CODE = UU.BRANCH WHERE USERNAME = __USERNAME) UBS ON 
							(UBS.SKIN = CCC.BRANDNAME OR CCC.BRANDNAME = 'ALL')
                            )
	ORDER BY
		CATS.NAME
	;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetCategoryAndSubCatDropDown` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetCategoryAndSubCatDropDown`()
BEGIN
	SELECT
		SUBCATS.SUBCATID,
		CASE 
			WHEN SUBCATS_2.SUBCATID IS NOT NULL THEN CONCAT(CATS.NAME," - ", SUBCATS_2.NAME," - ",SUBCATS.NAME)
            ELSE CONCAT(CATS.NAME," - ",SUBCATS.NAME) 
		END AS  NAME
	FROM
		categories CATS
	INNER JOIN
		subcategories SUBCATS ON 
			CATS.CATID = SUBCATS.CATID
	LEFT OUTER JOIN
		subcategories SUBCATS_2 ON 
			SUBCATS.PARENTSUBCATID = SUBCATS_2.SUBCATID
	WHERE
		SUBCATS.SUBCATID NOT IN (SELECT DISTINCT PARENTSUBCATID FROM subcategories)
	ORDER BY
		CATS.NAME,
		SUBCATS_2.NAME,
        SUBCATS.NAME
	;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetCategoryBrandInformation` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetCategoryBrandInformation`(__CATID INT)
BEGIN

SELECT 
	BSD.SKINNAME AS BRANDNAME,
    CASE WHEN CB.BRANDNAME IS NOT NULL THEN 1 ELSE 0 END AS SELECTED
FROM
	(
		SELECT SKINNAME FROM branchSkinDefinition UNION ALL
        SELECT 'ALL' UNION ALL
        SELECT ''
	) BSD
LEFT OUTER JOIN
	(SELECT * FROM categoriesBrand WHERE CATID = __CATID ) CB ON 
		BSD.SKINNAME = CB.BRANDNAME
ORDER BY 
	BSD.SKINNAME;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetCategoryInformation` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetCategoryInformation`(__CATID INTEGER)
BEGIN
	SELECT * FROM categories WHERE CATID = __CATID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetCategoryItems` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetCategoryItems`(__USERNAME VARCHAR(50), __CATID INT, __SUBCATID INT)
BEGIN
    SELECT 
        CATS.NAME AS CATNAME,
        CATS.DESCRIPTION AS CATDESC,
        CASE WHEN ITEMS.TYPE = 2 THEN -99 ELSE SUBCATS.SUBCATID END AS SUBCATID,
        CASE WHEN ITEMS.TYPE = 2 THEN '{{!GENERATE!}}' ELSE SUBCATS.NAME END AS SUBCATNAME,
        CASE WHEN ITEMS.TYPE = 2 THEN CONCAT('{{!CUST_ART!}} ',SUBCATS.NAME) ELSE SUBCATS.DESCRIPTION END AS SUBCATDESC,
      SUBCATS.SUBCATID AS REALSUBCATID,
        ITEMS.ID AS ITEMID,
        ITEMS.CODE AS ITEMCODE,
        ITEMS.DESCRIPTION AS ITEMDESC,
        ITEMS.DISPLAYIMAGEPATH,
        ITEMS.PROOFPDFNAME,
        ITEMS.SIZE,
        ITEMS.NUMPAGES,
        ITEMS.MINLEVEL,
        ITEMS.STATUS,
        ITEMS.TYPE AS ITEMTYPE,
        COALESCE(ONORDERSTAT.TOTORDERQTY,0) AS ONORDER, 
        COALESCE(DATE(ONORDERSTAT.ORDERDATE),'') AS LASTORDERED,
        CASE WHEN ITEMS.TYPE = 3 THEN 1 WHEN BRANCHLINK.ITEMID IS NULL THEN 0 ELSE 1 END AS PARTOFBRANCH,
        COALESCE(ITEMINV.QTYONHAND,0) AS INSTOCK,
        ITEMS.ASSETAPPROVED
    FROM
        categories CATS
    LEFT OUTER JOIN
        subcategories SUBCATS ON 
            CATS.CATID = SUBCATS.CATID
    LEFT OUTER JOIN
        itemsSubcategoryLink SCTOI ON
            SCTOI.SUBCATID = SUBCATS.SUBCATID
    LEFT OUTER JOIN
        items ITEMS ON 
            ITEMS.ID = SCTOI.ITEMID
    LEFT OUTER JOIN
    
    (SELECT DISTINCT 
        ITEMID 
     FROM
        (
            SELECT 
                ITOB.ITEMID 
            FROM 
                user USERS 
            INNER JOIN 
                itemsBranchesLink ITOB ON USERS.BRANCH = ITOB.BRANCHCODE 
            WHERE 
                USERS.USERNAME = __USERNAME
                
            UNION ALL 
            SELECT ITOB.ITEMID FROM itemsBranchesLink ITOB WHERE ITOB.BRANCHCODE = 'ALL_BRANCHES'
            
            UNION ALL
            SELECT ITOB.ITEMID FROM user UU INNER JOIN branches BB ON UU.BRANCH = BB.CODE
            INNER JOIN itemsBranchesLink ITOB ON CONCAT('BRN_',BB.SKIN) = ITOB.BRANCHCODE 
            WHERE UU.USERNAME = __USERNAME
        ) LINKS
    )  BRANCHLINK ON 
            BRANCHLINK.ITEMID = ITEMS.ID
    LEFT OUTER JOIN
        (SELECT ITEMID, SUM(CASE WHEN ORDERQTY-SHIPTODATEQTY < 0 THEN 0 ELSE ORDERQTY-SHIPTODATEQTY END) AS TOTORDERQTY, MAX(ORDERDATE) AS ORDERDATE FROM orderHeader HDR INNER JOIN orderDetail DTL ON HDR.ORDERID = DTL.ORDERID WHERE ORDERSTATUS NOT IN ('WAITMA','CART', 'CANCELLED') GROUP BY ITEMID) ONORDERSTAT ON
            ONORDERSTAT.ITEMID = SCTOI.ITEMID  
    LEFT OUTER JOIN
        itemsInventory ITEMINV ON 
            ITEMS.ID = ITEMINV.ITEMID
    CROSS JOIN
        ( SELECT * FROM user WHERE USERNAME = __USERNAME) USERS                
    WHERE
        (__SUBCATID = -1 OR __SUBCATID = SUBCATS.SUBCATID OR __SUBCATID = SUBCATS.PARENTSUBCATID) AND
        CATS.CATID = __CATID AND
        IFNULL(ITEMS.ARCHIVED,0) = 0 AND
        (BRANCHLINK.ITEMID IS NOT NULL OR (ITEMS.TYPE = 3 AND __USERNAME IN (SELECT USERNAME FROM user WHERE USERLEVEL >= 99))) AND
        USERS.USERLEVEL >= CATS.USERLEVEL AND
        USERS.USERLEVEL >= SUBCATS.USERLEVEL 
    ORDER BY
        CASE WHEN ITEMS.TYPE = 2 THEN 1 ELSE 99 END, 
        SUBCATS.NAME,
        CASE WHEN PARENTITEM = 0 THEN 1 ELSE 99 END, 
        ITEMS.CODE
    ;
    



END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetCategorySubcategoryTiles` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetCategorySubcategoryTiles`(__USERNAME VARCHAR(50), __CATID INT)
BEGIN

    SELECT
        CATS.CATID,
        CATS.NAME AS CATNAME,
        CATS.DESCRIPTION AS CATDESC,
        SUBCATS.SUBCATID  AS SUBCATID,
        SUBCATS.NAME  AS SUBCATNAME,
        SUBCATS.DESCRIPTION  AS SUBCATDESC,
        SUBCATS.ICONURL AS SUBCATICONURL
    FROM
        categories CATS
    LEFT OUTER JOIN
        subcategories SUBCATS ON 
            CATS.CATID = SUBCATS.CATID  
    CROSS JOIN
        ( SELECT * FROM user WHERE USERNAME = __USERNAME) USERS            
    WHERE
        CATS.CATID = __CATID AND
        SUBCATS.PARENTSUBCATID = -1 AND
        USERS.USERLEVEL >= CATS.USERLEVEL AND
        USERS.USERLEVEL >= SUBCATS.USERLEVEL
    ORDER BY
        SUBCATS.NAME
    ;            

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetDownloadFileData` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetDownloadFileData`(__ID VARCHAR(200))
BEGIN
	SELECT * FROM ticketAttachment WHERE STOREFILENAME = __ID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetDownloadItem` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetDownloadItem`(__DOWNLOADLINK VARCHAR(100))
BEGIN

SELECT
  ITEM.*,
    OH.USERNAME
FROM
  orderDetail OD
INNER JOIN
  items ITEM ON 
    ITEM.ID = OD.ITEMID
INNER JOIN 
  orderHeader OH ON OD.ORDERID = OH.ORDERID
WHERE
  OD.DOWNLOADLINK = __DOWNLOADLINK;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetDownloadLog` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetDownloadLog`()
BEGIN
	SELECT 
		LOG.*,
        ITEM.CODE AS ITEMCODE,
        ITEM.DESCRIPTION AS ITEMDESCRIPTION
	FROM
		downloadLog LOG
	INNER JOIN
		items ITEM ON 
			LOG.ITEMID = ITEM.ID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetFaqBySearchTerm` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetFaqBySearchTerm`(__TERM VARCHAR(50),__LANG VARCHAR(2))
BEGIN

  SELECT 
    ID,
        CASE WHEN __LANG = 'EN' THEN NAMEEN ELSE NAMEFR END AS NAME 
  FROM
    faq 
  WHERE
    (
    ID = __TERM OR 
        ( __LANG = 'EN' AND NAMEEN LIKE CONCAT('%',__TERM,'%')) OR 
        ( __LANG = 'FR' AND NAMEFR LIKE CONCAT('%',__TERM,'%')) OR 
        ( __LANG = 'EN' AND CONTENTEN LIKE CONCAT('%',__TERM,'%')) OR 
        ( __LANG = 'FR' AND CONTENTFR LIKE CONCAT('%',__TERM,'%'))
    )
  ORDER BY 
    NAMEEN
  LIMIT 20;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetGeneratorBranchVariables` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetGeneratorBranchVariables`(__USERNAME VARCHAR(50))
BEGIN

SELECT
	'Param-BranchName' AS TAG,
    NAME AS TAGVALUE
FROM
	branches
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) 

UNION ALL

SELECT
	'Param-Address1' AS TAG,
    ADDRESS AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    ISMAIN = 1
    
UNION ALL

SELECT
	'Param-Address1Ext' AS TAG,
    ADDRESS2 AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    ISMAIN = 1    

UNION ALL

SELECT
	'Param-Address2' AS TAG,
    CONCAT(CITY,', ',PROVINCE,', ', COUNTRY, '  ', POSTALCODE) AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    ISMAIN = 1

UNION ALL

SELECT
	'Param-City' AS TAG,
    CITY AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    ISMAIN = 1
   
UNION ALL
   
SELECT
	'Param-Prov' AS TAG,
    PROVINCE AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    ISMAIN = 1   
    
UNION ALL
   
SELECT
	'Param-Country' AS TAG,
    COUNTRY AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    ISMAIN = 1      

UNION ALL

SELECT
	'Param-CityProvPostal' AS TAG,
    CONCAT(CITY,', ',PROVINCE,' ', POSTALCODE) AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    ISMAIN = 1
    
UNION ALL

SELECT
	'Param-CityProv' AS TAG,
    CONCAT(CITY,', ',PROVINCE) AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    ISMAIN = 1 
    
UNION ALL

SELECT
	'Param-Postal' AS TAG,
    POSTALCODE AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    ISMAIN = 1      

UNION ALL

SELECT
	'Param-AddressAll' AS TAG,
    CONCAT(ADDRESS, ', ', CITY,', ',PROVINCE, '  ', POSTALCODE) AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    ISMAIN = 1

UNION ALL

SELECT
	'Param-BranchPhone' AS TAG,
    PHONE AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    ISMAIN = 1
    
UNION ALL

SELECT
	'Param-BranchFax' AS TAG,
    FAX AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    ISMAIN = 1    

UNION ALL
   
SELECT
	'Param-Address1EN' AS TAG,
    ADDRESS AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'EN'
    
UNION ALL
   
SELECT
	'Param-Address1ExtEN' AS TAG,
    ADDRESS2 AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'EN'    

UNION ALL

SELECT
	'Param-Address2EN' AS TAG,
    CONCAT(CITY,', ',PROVINCE,', ', COUNTRY, '  ', POSTALCODE) AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'EN' 
    
UNION ALL

SELECT
	'Param-AddressAllEN' AS TAG,
    CONCAT(ADDRESS, ', ', CITY,', ',PROVINCE, '  ', POSTALCODE) AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'EN' 

UNION ALL

SELECT
	'Param-CityEN' AS TAG,
    CITY AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'EN' 

UNION ALL
   
SELECT
	'Param-ProvEN' AS TAG,
    PROVINCE AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'EN'   
    
UNION ALL
   
SELECT
	'Param-CountryEN' AS TAG,
    COUNTRY AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'EN'   

UNION ALL

SELECT
	'Param-CityProvPostalEN' AS TAG,
    CONCAT(CITY,', ',PROVINCE,' ', POSTALCODE) AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'EN' 
    
UNION ALL

SELECT
	'Param-CityProvEN' AS TAG,
    CONCAT(CITY,', ',PROVINCE) AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'EN'   
    
UNION ALL

SELECT
	'Param-PostalEN' AS TAG,
    POSTALCODE AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'EN'      

UNION ALL

SELECT
	'Param-BranchPhoneEN' AS TAG,
    PHONE AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'EN'
    
UNION ALL

SELECT
	'Param-BranchFaxEN' AS TAG,
    FAX AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'EN'    

UNION ALL
   
SELECT
	'Param-Address1FR' AS TAG,
    ADDRESS AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'FR'
    
UNION ALL
   
SELECT
	'Param-Address1ExtFR' AS TAG,
    ADDRESS2 AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'FR'    

UNION ALL

SELECT
	'Param-Address2FR' AS TAG,
    CONCAT(CITY,', ',PROVINCE,', ', COUNTRY, '  ', POSTALCODE) AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'FR'    
    
UNION ALL

SELECT
	'Param-AddressAllFR' AS TAG,
    CONCAT(ADDRESS, ', ', CITY,', ',PROVINCE, '  ', POSTALCODE) AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'FR'     
 
UNION ALL

SELECT
	'Param-CityFR' AS TAG,
    CITY AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'FR'  

UNION ALL
   
SELECT
	'Param-ProvFR' AS TAG,
    PROVINCE AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'FR'   
    
UNION ALL
   
SELECT
	'Param-CountryFR' AS TAG,
    COUNTRY AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'FR'   
 
UNION ALL

SELECT
	'Param-CityProvPostalFR' AS TAG,
    CONCAT(CITY,', ',PROVINCE,' ', POSTALCODE) AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'FR' 
    
UNION ALL

SELECT
	'Param-CityProvFR' AS TAG,
    CONCAT(CITY,', ',PROVINCE) AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'FR'  
    
UNION ALL

SELECT
	'Param-PostalFR' AS TAG,
    POSTALCODE AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'FR'       
    
UNION ALL

SELECT
	'Param-BranchPhoneFR' AS TAG,
    PHONE AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'FR'
    
UNION ALL

SELECT
	'Param-BranchFaxFR' AS TAG,
    FAX AS TAGVALUE
FROM
	branchesAddresses
WHERE
	CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME) AND
    LANGUAGE = 'FR'    

UNION ALL

SELECT
	CONCAT('Param-',TAG) AS TAG,
	VALUE
FROM
	branchesAssetData
WHERE
	BRANCHCODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME);


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetInventoryData` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetInventoryData`(__INVTYPE INT)
BEGIN

IF __INVTYPE = 1 THEN
	
	SELECT
		ITEMS.ID,
        ITEMS.CODE,
        ITEMS.DESCRIPTION,
        COALESCE(INV.QTYONHAND,0) AS STOCK,
        COALESCE(ONORDERSTAT.TOTORDERQTY,0) AS ONORDER, 
        ITEMS.MINLEVEL,
        COALESCE(DATE(ONORDERSTAT.ORDERDATE),'') AS LASTORDERED,
        COALESCE(DATE(SHIPSTAT.SHIPDATE),'') AS LASTSHIPPED,
        COALESCE(SUBCATNAME.NAME,'') AS L2CAT,
		COALESCE(SUBCATNAME1.NAME,'') AS L1CAT,
		COALESCE(CAT1.NAME,'') AS L0CAT        
	FROM
		items ITEMS
	LEFT OUTER JOIN
		itemsInventory INV ON 
			ITEMS.ID = INV.ITEMID
	LEFT OUTER JOIN
		(SELECT ITEMID, SUM(ORDERQTY-SHIPTODATEQTY) AS TOTORDERQTY, MAX(ORDERDATE) AS ORDERDATE FROM orderHeader HDR INNER JOIN orderDetail DTL ON HDR.ORDERID = DTL.ORDERID WHERE ORDERSTATUS NOT IN ('WAITMA','CART') GROUP BY ITEMID) ONORDERSTAT ON
			ONORDERSTAT.ITEMID = ITEMS.ID
	LEFT OUTER JOIN
		(SELECT ITEMID, MAX(SHIPDATE) AS SHIPDATE FROM orderHeader HDR INNER JOIN orderDetail DTL ON HDR.ORDERID = DTL.ORDERID INNER JOIN shipmentDetail SDTL ON DTL.ORDERID = SDTL.ORDERID AND DTL.ITEMLINE = SDTL.ORDERLINE INNER JOIN shipmentHeader SHDR ON SDTL.SHIPID = SHDR.SHIPID WHERE ORDERSTATUS NOT IN ('WAITMA','CART') GROUP BY ITEMID) SHIPSTAT ON
			SHIPSTAT.ITEMID = ITEMS.ID   
	LEFT OUTER JOIN 
		(SELECT ITEMID,MIN(SUBCATID) AS SUBCATID FROM itemsSubcategoryLink GROUP BY ITEMID) SUBCAT ON 
			ITEMS.ID = SUBCAT.ITEMID
	LEFT OUTER JOIN 
		subcategories SUBCATNAME ON 
			SUBCAT.SUBCATID = SUBCATNAME.SUBCATID
	LEFT OUTER JOIN 
		subcategories SUBCATNAME1 ON 
			SUBCATNAME.PARENTSUBCATID = SUBCATNAME1.SUBCATID            
	LEFT OUTER JOIN 
		categories CAT1 ON 
			SUBCATNAME.CATID = CAT1.CATID 
	;
	
ELSEIF __INVTYPE = 2 THEN 
	
	SELECT
		ITEMS.ID,
        ITEMS.CODE,
        ITEMS.DESCRIPTION,
        COALESCE(INV.QTYONHAND,0) AS STOCK,
        COALESCE(ONORDERSTAT.TOTORDERQTY,0) AS ONORDER, 
        ITEMS.MINLEVEL,
        COALESCE(DATE(ONORDERSTAT.ORDERDATE),'') AS LASTORDERED,
        COALESCE(DATE(SHIPSTAT.SHIPDATE),'') AS LASTSHIPPED,
        COALESCE(SUBCATNAME.NAME,'') AS L2CAT,
		COALESCE(SUBCATNAME1.NAME,'') AS L1CAT,
		COALESCE(CAT1.NAME,'') AS L0CAT        
	FROM
		items ITEMS
	INNER JOIN
		itemsInventory INV ON 
			ITEMS.ID = INV.ITEMID
	LEFT OUTER JOIN
		(SELECT ITEMID, SUM(ORDERQTY-SHIPTODATEQTY) AS TOTORDERQTY, MAX(ORDERDATE) AS ORDERDATE FROM orderHeader HDR INNER JOIN orderDetail DTL ON HDR.ORDERID = DTL.ORDERID WHERE ORDERSTATUS NOT IN ('WAITMA','CART') GROUP BY ITEMID) ONORDERSTAT ON
			ONORDERSTAT.ITEMID = ITEMS.ID 
	LEFT OUTER JOIN
		(SELECT ITEMID, MAX(SHIPDATE) AS SHIPDATE FROM orderHeader HDR INNER JOIN orderDetail DTL ON HDR.ORDERID = DTL.ORDERID INNER JOIN shipmentDetail SDTL ON DTL.ORDERID = SDTL.ORDERID AND DTL.ITEMLINE = SDTL.ORDERLINE INNER JOIN shipmentHeader SHDR ON SDTL.SHIPID = SHDR.SHIPID WHERE ORDERSTATUS NOT IN ('WAITMA','CART') GROUP BY ITEMID) SHIPSTAT ON
			SHIPSTAT.ITEMID = ITEMS.ID
	LEFT OUTER JOIN 
		(SELECT ITEMID,MIN(SUBCATID) AS SUBCATID FROM itemsSubcategoryLink GROUP BY ITEMID) SUBCAT ON 
			ITEMS.ID = SUBCAT.ITEMID
	LEFT OUTER JOIN 
		subcategories SUBCATNAME ON 
			SUBCAT.SUBCATID = SUBCATNAME.SUBCATID
	LEFT OUTER JOIN 
		subcategories SUBCATNAME1 ON 
			SUBCATNAME.PARENTSUBCATID = SUBCATNAME1.SUBCATID            
	LEFT OUTER JOIN 
		categories CAT1 ON 
			SUBCATNAME.CATID = CAT1.CATID             
	WHERE 
		INV.QTYONHAND > 0;
        
        
ELSEIF __INVTYPE = 3 THEN
	
	SELECT
		ITEMS.ID,
        ITEMS.CODE,
        ITEMS.DESCRIPTION,
        COALESCE(INV.QTYONHAND,0) AS STOCK,
        COALESCE(ONORDERSTAT.TOTORDERQTY,0) AS ONORDER, 
        ITEMS.MINLEVEL,
        COALESCE(DATE(ONORDERSTAT.ORDERDATE),'') AS LASTORDERED,
        COALESCE(DATE(SHIPSTAT.SHIPDATE),'') AS LASTSHIPPED,
        COALESCE(SUBCATNAME.NAME,'') AS L2CAT,
		COALESCE(SUBCATNAME1.NAME,'') AS L1CAT,
		COALESCE(CAT1.NAME,'') AS L0CAT        
	FROM
		items ITEMS
	INNER JOIN
		itemsInventory INV ON 
			ITEMS.ID = INV.ITEMID
	LEFT OUTER JOIN
		(SELECT ITEMID, SUM(ORDERQTY-SHIPTODATEQTY) AS TOTORDERQTY, MAX(ORDERDATE) AS ORDERDATE FROM orderHeader HDR INNER JOIN orderDetail DTL ON HDR.ORDERID = DTL.ORDERID WHERE ORDERSTATUS NOT IN ('WAITMA','CART') GROUP BY ITEMID) ONORDERSTAT ON
			ONORDERSTAT.ITEMID = ITEMS.ID  
	LEFT OUTER JOIN
		(SELECT ITEMID, MAX(SHIPDATE) AS SHIPDATE FROM orderHeader HDR INNER JOIN orderDetail DTL ON HDR.ORDERID = DTL.ORDERID INNER JOIN shipmentDetail SDTL ON DTL.ORDERID = SDTL.ORDERID AND DTL.ITEMLINE = SDTL.ORDERLINE INNER JOIN shipmentHeader SHDR ON SDTL.SHIPID = SHDR.SHIPID WHERE ORDERSTATUS NOT IN ('WAITMA','CART') GROUP BY ITEMID) SHIPSTAT ON
			SHIPSTAT.ITEMID = ITEMS.ID
	LEFT OUTER JOIN 
		(SELECT ITEMID,MIN(SUBCATID) AS SUBCATID FROM itemsSubcategoryLink GROUP BY ITEMID) SUBCAT ON 
			ITEMS.ID = SUBCAT.ITEMID
	LEFT OUTER JOIN 
		subcategories SUBCATNAME ON 
			SUBCAT.SUBCATID = SUBCATNAME.SUBCATID
	LEFT OUTER JOIN 
		subcategories SUBCATNAME1 ON 
			SUBCATNAME.PARENTSUBCATID = SUBCATNAME1.SUBCATID            
	LEFT OUTER JOIN 
		categories CAT1 ON 
			SUBCATNAME.CATID = CAT1.CATID             
	WHERE 
		INV.QTYONHAND <= 0;

ELSE

	
	SELECT
		ITEMS.ID,
        ITEMS.CODE,
        ITEMS.DESCRIPTION,
        COALESCE(INV.QTYONHAND,0) AS STOCK,
        COALESCE(ONORDERSTAT.TOTORDERQTY,0) AS ONORDER, 
        ITEMS.MINLEVEL,
        COALESCE(DATE(ONORDERSTAT.ORDERDATE),'') AS LASTORDERED,
        COALESCE(DATE(SHIPSTAT.SHIPDATE),'') AS LASTSHIPPED,
        COALESCE(SUBCATNAME.NAME,'') AS L2CAT,
		COALESCE(SUBCATNAME1.NAME,'') AS L1CAT,
		COALESCE(CAT1.NAME,'') AS L0CAT        
	FROM
		items ITEMS
	INNER JOIN
		itemsInventory INV ON 
			ITEMS.ID = INV.ITEMID
	LEFT OUTER JOIN
		(SELECT ITEMID, SUM(ORDERQTY-SHIPTODATEQTY) AS TOTORDERQTY, MAX(ORDERDATE) AS ORDERDATE FROM orderHeader HDR INNER JOIN orderDetail DTL ON HDR.ORDERID = DTL.ORDERID WHERE ORDERSTATUS NOT IN ('WAITMA','CART') GROUP BY ITEMID) ONORDERSTAT ON
			ONORDERSTAT.ITEMID = ITEMS.ID   
	LEFT OUTER JOIN
		(SELECT ITEMID, MAX(SHIPDATE) AS SHIPDATE FROM orderHeader HDR INNER JOIN orderDetail DTL ON HDR.ORDERID = DTL.ORDERID INNER JOIN shipmentDetail SDTL ON DTL.ORDERID = SDTL.ORDERID AND DTL.ITEMLINE = SDTL.ORDERLINE INNER JOIN shipmentHeader SHDR ON SDTL.SHIPID = SHDR.SHIPID WHERE ORDERSTATUS NOT IN ('WAITMA','CART') GROUP BY ITEMID) SHIPSTAT ON
			SHIPSTAT.ITEMID = ITEMS.ID 
	LEFT OUTER JOIN 
		(SELECT ITEMID,MIN(SUBCATID) AS SUBCATID FROM itemsSubcategoryLink GROUP BY ITEMID) SUBCAT ON 
			ITEMS.ID = SUBCAT.ITEMID
	LEFT OUTER JOIN 
		subcategories SUBCATNAME ON 
			SUBCAT.SUBCATID = SUBCATNAME.SUBCATID
	LEFT OUTER JOIN 
		subcategories SUBCATNAME1 ON 
			SUBCATNAME.PARENTSUBCATID = SUBCATNAME1.SUBCATID            
	LEFT OUTER JOIN 
		categories CAT1 ON 
			SUBCATNAME.CATID = CAT1.CATID             
	WHERE 
		INV.QTYONHAND < ITEMS.MINLEVEL;

END IF;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetInventoryMovementHistory` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetInventoryMovementHistory`(IN `__STARTDATE` DATE, IN `__ENDDATE` DATE)
BEGIN
	SELECT
		ITEM.ID,
        ITEM.CODE,
        ITEM.DESCRIPTION,
        LOG.ADJID,
        LOG.QTY,
        CAST(LOG.ADJDATE AS DATE) AS ADJDATE,
        LOG.REASON
	FROM
		items ITEM
	INNER JOIN
		itemsAdjustmentLog LOG ON 
			ITEM.ID = LOG.ITEMID
	WHERE
		LOG.ADJDATE BETWEEN __STARTDATE AND DATE_ADD(__ENDDATE,INTERVAL 1 DAY) AND
            ITEM.TYPE = 0
	ORDER BY
		ITEM.ID,
        LOG.ADJDATE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetItemBranches` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetItemBranches`(__ITEMID INT)
BEGIN
	SELECT
		*
	FROM
		itemsBranchesLink
	WHERE
		ITEMID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetItemCategories` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetItemCategories`(__ITEMID INT)
BEGIN
	SELECT
		*
	FROM
		itemsSubcategoryLink
	WHERE
		ITEMID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetItemData` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetItemData`(__ITEMID INT)
BEGIN
	SELECT * FROM items WHERE ID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetItemDataByConfirmCode` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetItemDataByConfirmCode`(__CODE VARCHAR(200))
BEGIN
	SELECT * FROM items WHERE APPROVALLINK = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetItemPricing` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetItemPricing`(__ITEMID INT)
BEGIN


	SELECT
		*,
        ROUND((LENGTH*WIDTH*HEIGHT)/172 * PIECES,0) AS DIMWEIGHT
	FROM
		itemPricing
	WHERE
		ITEMID = __ITEMID
	ORDER BY 
		CASE WHEN QTY = -2 THEN 9999 ELSE QTY END;  
        
        
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetItemsBySearchTerm` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetItemsBySearchTerm`(__TERM VARCHAR(50))
BEGIN

SELECT
	ID,
    CONCAT(CODE, ' - ', DESCRIPTION) AS ITEMNAME
FROM
	items
WHERE
	(
	CODE LIKE CONCAT('%',__TERM,'%') OR 
    DESCRIPTION LIKE CONCAT('%',__TERM,'%')
    ) AND
    TYPE NOT IN (3)
ORDER BY 
	CODE,DESCRIPTION
LIMIT 20;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetItemZonePricing` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetItemZonePricing`(__ITEMID INT, __QTY INT, __BRANCH VARCHAR(20), __ITEMOPTION VARCHAR(30))
BEGIN

 SET @BRANCHZONE := (SELECT SHIPZONE FROM branches WHERE CODE = __BRANCH);
 SET @ITEMWEIGHT := (SELECT CASE WHEN ROUND((LENGTH*WIDTH*HEIGHT)/172 * PIECES,0) > WEIGHT THEN ROUND((LENGTH*WIDTH*HEIGHT)/172 * PIECES,0) ELSE WEIGHT END AS WEIGHT FROM itemPricing WHERE ITEMID = __ITEMID AND (QTY = __QTY OR QTY = -1 OR QTY = -2) AND ITEMOPTION = __ITEMOPTION ORDER BY QTY DESC LIMIT 1);

 SELECT
 @BRANCHZONE AS BRANCHZONE,
		ZONECOSTS.*
	FROM
		shipZoneCosts ZONECOSTS
	WHERE
		ZONECOSTS.WEIGHT <= @ITEMWEIGHT
	ORDER BY
		WEIGHT DESC;	

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetNewsItem` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetNewsItem`(__ID INTEGER)
BEGIN
	SELECT * FROM newsItems WHERE ID = __ID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetOrderDataForShipment` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetOrderDataForShipment`(__ORDERID INT)
BEGIN
	SELECT
		HDR.*,
        DTL.*,
        ITEM.*,
        DATE(ORDERDATE) AS FMTORDERDATE,
        DATE(EXPDATE) AS FMTEXPDATE,
		USER.FIRSTNAME AS USER_FIRSTNAME,
		USER.LASTNAME AS USER_LASTNAME,
		USER.EMAIL AS USER_EMAIL,
		USER.LANGUAGE AS USER_LANGUAGE
	FROM
		orderHeader HDR
	INNER JOIN
		orderDetail DTL ON 
			HDR.ORDERID = DTL.ORDERID
	LEFT OUTER JOIN
		items ITEM ON 
			DTL.ITEMID = ITEM.ID
	INNER JOIN
		user USER ON 
			HDR.USERNAME = USER.USERNAME            
	WHERE 
		HDR.ORDERID = __ORDERID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetOrderInformationForEmail` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetOrderInformationForEmail`(__USERNAME VARCHAR(100), __ORDERID INT)
BEGIN

SELECT
	HDR.*,
    DTL.*,
    ITEM.TYPE,
    USER.FIRSTNAME AS USER_FIRSTNAME,
    USER.LASTNAME AS USER_LASTNAME,
    USER.EMAIL AS USER_EMAIL,
    USER.LANGUAGE AS USER_LANGUAGE,
    BRANCH.NAME AS BRANCH_NAME,
    BRANCH.MANAGEMENTEMAIL AS BRANCH_MANAGEMENTEMAIL,
    COALESCE(BRANCHADDRESS.LANGUAGE,'EN') AS BRANCH_LANGUAGE,
    ORDERSTATUS AS REALSTATUS
FROM
	orderHeader HDR
INNER JOIN
	orderDetail DTL ON 
		HDR.ORDERID = DTL.ORDERID
INNER JOIN
	items ITEM ON 
		DTL.ITEMID = ITEM.ID
INNER JOIN
	user USER ON 
		HDR.USERNAME = USER.USERNAME
INNER JOIN
	branches BRANCH ON 
		USER.BRANCH = BRANCH.CODE
LEFT OUTER JOIN
	(SELECT * FROM branchesAddresses WHERE ISMAIN = 1) BRANCHADDRESS ON 
		BRANCH.CODE = BRANCHADDRESS.CODE
WHERE 
	HDR.USERNAME = __USERNAME AND 
    HDR.ORDERID = __ORDERID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetPaypalPaymentInformation` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetPaypalPaymentInformation`(__USERNAME VARCHAR(100))
BEGIN

SELECT
	HDR.ORDERID,
	SHIPCONTACTFIRSTNAME AS FIRSTNAME,
    SHIPCONTACTLASTNAME AS LASTNAME,
    SHIPCONTACTEMAIL AS EMAIL,
    SPECIALINSTRUCTIONS AS SPECINSTR,
    RECEIVERINSTRUCTIONS AS RECINSTR,
    SHIPPINGMETHOD AS SHIPPINGMETHOD,
    SHIPPINGCOST AS SHIPPINGCOST,
	SHIPNAME AS SHIPNAME,
    BILLINGCATEGORY AS BILLINGCATEGORY,
    PONUMBER AS PONUMBER,
    ROUND(COALESCE(PROMODISC,0)+COALESCE(ITEMTOT,0)+SHIPPINGCOST+SHIPPINGCOSTTAXAMT1+SHIPPINGCOSTTAXAMT2+SHIPPINGCOSTTAXAMT3+SHIPPINGCOSTTAXAMT4+SHIPPINGCOSTTAXAMT5,2) AS AMT
FROM
	orderHeader HDR
INNER JOIN
	( SELECT ORDERID,SUM(EXTPRICE+TAXAMT1+TAXAMT2+TAXAMT3+TAXAMT4+TAXAMT5) AS ITEMTOT FROM orderDetail GROUP BY ORDERID) DTL ON 
		HDR.ORDERID = DTL.ORDERID
LEFT OUTER JOIN
	( SELECT ORDERID,SUM(CALCDISCOUNT+TAXAMT1+TAXAMT2+TAXAMT3+TAXAMT4+TAXAMT5) AS PROMODISC FROM 
      orderPromos GROUP BY ORDERID ) PROMOS ON 
		HDR.ORDERID = PROMOS.ORDERID
WHERE 
	USERNAME  = __USERNAME AND 
    ORDERSTATUS = 'CART';
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetPendingOrders` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetPendingOrders`(__USERNAME VARCHAR(20))
BEGIN

	SELECT
		HDR.*,
        CAST(HDR.ORDERDATE AS DATE) AS SHORTDATE,
        DTL.TOTAMT+HDR.SHIPPINGCOST+HDR.SHIPPINGCOSTTAXAMT1+HDR.SHIPPINGCOSTTAXAMT2+HDR.SHIPPINGCOSTTAXAMT3+HDR.SHIPPINGCOSTTAXAMT4+HDR.SHIPPINGCOSTTAXAMT5 AS ORDERAMOUNT
	FROM
		orderHeader HDR
	INNER JOIN
		(SELECT ORDERID, SUM(EXTPRICE+TAXAMT1+TAXAMT2+TAXAMT3+TAXAMT4+TAXAMT5) AS TOTAMT FROM orderDetail GROUP BY ORDERID) DTL ON 
			HDR.ORDERID = DTL.ORDERID
	WHERE
		USERNAME = __USERNAME AND
        ORDERSTATUS IN ('WAITMA');
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetPromoProfile` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetPromoProfile`(__CODE VARCHAR(50))
BEGIN
	SELECT * FROM promos WHERE CODE = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetPromoProfileCategories` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetPromoProfileCategories`(__CODE VARCHAR(50))
BEGIN

  SELECT
    PROMOS.CATID,
        CONCAT(CATS.NAME, ' - ', CATS.DESCRIPTION) AS CATNAME
  FROM
    promosCategories PROMOS
  INNER JOIN 
    categories CATS ON 
      PROMOS.CATID = CATS.CATID
  WHERE
    PROMOS.CODE = __CODE;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetPromoProfileItems` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetPromoProfileItems`(__CODE VARCHAR(50))
BEGIN
	SELECT 
		PROMOS.ITEMID,
		CONCAT(ITEMS.CODE,' - ',DESCRIPTION) AS ITEMNAME
	FROM 
		promosItems PROMOS
	INNER JOIN 
		items ITEMS ON 
			PROMOS.ITEMID = ITEMS.ID
	WHERE 
		PROMOS.CODE = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetSearchItems` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetSearchItems`(__USERNAME VARCHAR(50), __TERM NVARCHAR(200))
BEGIN
    SELECT DISTINCT
        ITEMS.ID AS ITEMID,
        ITEMS.CODE AS ITEMCODE,
        ITEMS.DESCRIPTION AS ITEMDESC,
        ITEMS.DISPLAYIMAGEPATH,
        ITEMS.PROOFPDFNAME,
        ITEMS.SIZE,
        ITEMS.NUMPAGES,
        ITEMS.MINLEVEL,
        ITEMS.STATUS,
        ITEMS.TYPE AS ITEMTYPE,
        COALESCE(ONORDERSTAT.TOTORDERQTY,0) AS ONORDER, 
        COALESCE(DATE(ONORDERSTAT.ORDERDATE),'') AS LASTORDERED,
        COALESCE(ITEMINV.QTYONHAND,0) AS INSTOCK,
        MIN(SUBCATS.CATID) AS CATID,
        MIN(SUBCATS.SUBCATID) AS SUBCATID
    FROM
        categories CATS
    LEFT OUTER JOIN
        subcategories SUBCATS ON 
            CATS.CATID = SUBCATS.CATID
    LEFT OUTER JOIN
        itemsSubcategoryLink SCTOI ON
            SCTOI.SUBCATID = SUBCATS.SUBCATID
    LEFT OUTER JOIN
        items ITEMS ON 
            ITEMS.ID = SCTOI.ITEMID
    LEFT OUTER JOIN
        (SELECT ITEMID, SUM(CASE WHEN ORDERQTY-SHIPTODATEQTY < 0 THEN 0 ELSE ORDERQTY-SHIPTODATEQTY END) AS TOTORDERQTY, MAX(ORDERDATE) AS ORDERDATE FROM orderHeader HDR INNER JOIN orderDetail DTL ON HDR.ORDERID = DTL.ORDERID WHERE ORDERSTATUS NOT IN ('WAITMA','CART','CANCELLED') GROUP BY ITEMID) ONORDERSTAT ON
            ONORDERSTAT.ITEMID = SCTOI.ITEMID  
    LEFT OUTER JOIN
        itemsInventory ITEMINV ON 
            ITEMS.ID = ITEMINV.ITEMID
    INNER JOIN 
        (SELECT DISTINCT 
            ITEMID 
         FROM
            (
                SELECT 
                    ITOB.ITEMID 
                FROM 
                    user USERS 
                INNER JOIN 
                    itemsBranchesLink ITOB ON USERS.BRANCH = ITOB.BRANCHCODE 
                WHERE 
                    USERS.USERNAME = __USERNAME
                    
                UNION ALL 
                SELECT ITOB.ITEMID FROM itemsBranchesLink ITOB WHERE ITOB.BRANCHCODE = 'ALL_BRANCHES'
                
                UNION ALL
                SELECT ITOB.ITEMID FROM user UU INNER JOIN branches BB ON UU.BRANCH = BB.CODE
                INNER JOIN itemsBranchesLink ITOB ON CONCAT('BRN_',BB.SKIN) = ITOB.BRANCHCODE 
                WHERE UU.USERNAME = __USERNAME                
            ) LINKS
        )  BRANCHLINK ON 
                BRANCHLINK.ITEMID = ITEMS.ID  
    CROSS JOIN
        ( SELECT * FROM user WHERE USERNAME = __USERNAME) USERS                        
    WHERE
        (
            ITEMS.CODE COLLATE utf8_unicode_ci LIKE CONCAT('%',__TERM,'%') OR 
            ITEMS.DESCRIPTION COLLATE utf8_unicode_ci LIKE CONCAT('%',__TERM,'%')
        ) AND 
        ITEMS.ARCHIVED = 0 AND
        ITEMS.ASSETAPPROVED = 1 AND
        CATS.CATID IN (
                        SELECT CC.CATID 
                        FROM categories CC 
                        INNER JOIN categoriesBrand CCC ON CC.CATID = CCC.CATID
                        INNER JOIN ( SELECT SKIN FROM branches BB INNER JOIN user UU ON BB.CODE = UU.BRANCH WHERE USERNAME COLLATE utf8_unicode_ci = __USERNAME) UBS ON 
                            (UBS.SKIN = CCC.BRANDNAME OR CCC.BRANDNAME = 'ALL')
                            ) AND
        CATS.USERLEVEL <= COALESCE((SELECT USERLEVEL FROM user WHERE USERNAME = __USERNAME LIMIT 1),0) AND
        USERS.USERLEVEL >= SUBCATS.USERLEVEL
    GROUP BY
        ITEMS.ID ,
        ITEMS.CODE ,
        ITEMS.DESCRIPTION ,
        ITEMS.DISPLAYIMAGEPATH,
        ITEMS.PROOFPDFNAME,
        ITEMS.SIZE,
        ITEMS.NUMPAGES,
        ITEMS.MINLEVEL,
        ITEMS.STATUS,
        ITEMS.TYPE ,
        COALESCE(ONORDERSTAT.TOTORDERQTY,0) , 
        COALESCE(DATE(ONORDERSTAT.ORDERDATE),'') ,
        COALESCE(ITEMINV.QTYONHAND,0) 
    ORDER BY
        ITEMS.CODE
    ;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetSettingsValue` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetSettingsValue`(SETTING_KEY VARCHAR(100))
BEGIN
	SELECT 
		*
	FROM
		settings
	WHERE
		`KEY` = SETTING_KEY;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetShipmentInfo` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetShipmentInfo`(__SHIPID INT)
BEGIN

	SELECT
		SHIPHDR.*,
        SHIPDTL.*,
        ORDHDR.*,
        ORDDTL.*,
        ORDDTL.ORDERQTY - ORDDTL.SHIPTODATEQTY AS BOQTY,
		USER.EMAIL AS USER_EMAIL,
		USER.LANGUAGE AS USER_LANGUAGE,
		BRANCH.MANAGEMENTEMAIL AS BRANCH_MANAGEMENTEMAIL,
		COALESCE(BRANCHADDRESS.LANGUAGE,'EN') AS BRANCH_LANGUAGE,
        ITEM.TYPE
	FROM
		shipmentHeader SHIPHDR
	INNER JOIN
		shipmentDetail SHIPDTL ON
			SHIPHDR.SHIPID = SHIPDTL.SHIPID
	INNER JOIN
		orderDetail ORDDTL ON 
			SHIPDTL.ORDERID = ORDDTL.ORDERID AND
            SHIPDTL.ORDERLINE = ORDDTL.ITEMLINE
	INNER JOIN
		items ITEM ON 
			ORDDTL.ITEMID = ITEM.ID
	INNER JOIN
		orderHeader ORDHDR ON 
			SHIPHDR.ORDERID = ORDHDR.ORDERID 
	INNER JOIN
		user USER ON 
			ORDHDR.USERNAME = USER.USERNAME 
	INNER JOIN
		branches BRANCH ON 
			USER.BRANCH = BRANCH.CODE 
	LEFT OUTER JOIN
		(SELECT * FROM branchesAddresses WHERE ISMAIN = 1) BRANCHADDRESS ON 
			BRANCH.CODE = BRANCHADDRESS.CODE
	WHERE
		SHIPHDR.SHIPID = __SHIPID;            

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetShipToAddress` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetShipToAddress`(__ID VARCHAR(50), __USEBRANCHES BOOLEAN)
BEGIN
	SELECT
		NAME,ADDRESS,ADDRESS2,CITY,PROVINCE,COUNTRY,POSTAL,PHONE,FAX
	FROM
    (
	SELECT
		'S' AS RECTYPE,ID,NAME,ADDRESS,ADDRESS2,CITY,PROVINCE,COUNTRY,POSTAL,PHONE,FAX
	FROM
		shipToAddresses
        
	UNION ALL
    
    SELECT
		'B' AS RECTYPE,BRANCH.CODE,NAME,ADDRESS,ADDRESS2,CITY,PROVINCE,COUNTRY,POSTALCODE,PHONE,FAX
    FROM
		branches BRANCH
	INNER JOIN
		branchesAddresses BADDR ON 
			BRANCH.CODE = BADDR.CODE
	WHERE
		__USEBRANCHES = 1 AND
        BRANCH.STATUS = 1 AND
        BADDR.ISMAIN = 1
    ) ADDR
    WHERE
		CONCAT(ADDR.RECTYPE,'-',ADDR.ID) = __ID
    ;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetShipToAddressInformation` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetShipToAddressInformation`(__ID INT)
BEGIN

	SELECT
		*
	FROM
		shipToAddresses
	WHERE
		ID = __ID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetShipToAddressUsersList` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetShipToAddressUsersList`(__SHIPTOID INT)
BEGIN
	SELECT 
		USERS.USERNAME,
		USERS.FIRSTNAME,
		USERS.LASTNAME,
		CASE WHEN USERADDR.ID IS NOT NULL THEN 1 ELSE 0 END AS SELECTED
	FROM
		( SELECT 'ALL' AS USERNAME, 'All' AS FIRSTNAME, 'Users' AS LASTNAME UNION ALL
          SELECT USERNAME,FIRSTNAME,LASTNAME FROM user
        ) USERS 
	LEFT OUTER JOIN 
		(SELECT * FROM shipToAddressesUserList WHERE ID = __SHIPTOID) USERADDR ON 
			USERS.USERNAME = USERADDR.USERNAME
	ORDER BY 
		USERS.USERNAME;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetShipTosBySearchTerm` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetShipTosBySearchTerm`(__USEBRANCHES BOOLEAN, __TERM VARCHAR(50), __USERNAME VARCHAR(30))
BEGIN

	SELECT
		CONCAT(RECTYPE,'-',ID) AS ID,
        CONCAT(NAME, ' - ', ADDRESS) AS SHIPDESC
	FROM
    (
	SELECT
		'S' AS RECTYPE,
        ID, 
        NAME,
        ADDRESS,
        ADDRESS2,
        CITY,
        POSTAL AS POSTALCODE,        
        CASE WHEN IFNULL(STOREID,'') = '' THEN 'NA' ELSE STOREID END AS STOREID
	FROM
		shipToAddresses
	WHERE
		ID IN (SELECT ID FROM shipToAddressesUserList WHERE USERNAME = '**ALL**' OR USERNAME = __USERNAME) 
        
	UNION ALL
    
    SELECT
		'B' AS RECTYPE,
        BRANCH.CODE,
        BRANCH.NAME,
        BADDR.ADDRESS,
        BADDR.ADDRESS2,
        BADDR.CITY,
        BADDR.POSTALCODE,
        BRANCH.CODE AS STOREID
    FROM
		branches BRANCH
	INNER JOIN
		branchesAddresses BADDR ON 
			BRANCH.CODE = BADDR.CODE
	WHERE
		__USEBRANCHES = 1 AND
        BRANCH.STATUS = 1 AND
        BADDR.ISMAIN = 1
    ) ADDR
    WHERE
	(
	ADDR.NAME LIKE CONCAT('%',__TERM,'%') OR 
    ADDR.ADDRESS LIKE CONCAT('%',__TERM,'%') OR
    ADDR.ADDRESS2 LIKE CONCAT('%',__TERM,'%') OR
    ADDR.CITY LIKE CONCAT('%',__TERM,'%') OR
    ADDR.POSTALCODE LIKE CONCAT('%',__TERM,'%') OR 
    ADDR.STOREID LIKE CONCAT('%',__TERM,'%')
    ) 
    ORDER BY
		NAME;	

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetSkinOptions` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetSkinOptions`()
BEGIN
	SELECT
		*
	FROM
		branchSkinDefinition
	ORDER BY
		SKINNAME;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetSubCategoryInformation` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetSubCategoryInformation`(__SUBCATID INTEGER)
BEGIN
	SELECT * FROM subcategories WHERE SUBCATID = __SUBCATID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetSupportTicketAttachments` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetSupportTicketAttachments`(__TICKETID INT)
BEGIN
	SELECT * FROM ticketAttachment WHERE TICKETID = __TICKETID ORDER BY
    ID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetSupportTicketData` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetSupportTicketData`(__TICKETID INT, __USERNAME VARCHAR(50))
BEGIN

	SELECT
	HDR.TITLE,
	HDR.STATUS,
		DTL.*,
        USR.FIRSTNAME,
        USR.LASTNAME,
        UNIX_TIMESTAMP(DTL.DATEENTERED) AS UNIX_DATEENTERED
	FROM
		ticketHeader HDR
	INNER JOIN
		ticketDetail DTL ON 
			HDR.ID = DTL.TICKETID
	LEFT OUTER JOIN
		user USR ON 
			DTL.ENTEREDBY = USR.USERNAME
	WHERE
        ( HDR.ENTEREDBY = __USERNAME OR __USERNAME IN (SELECT USERNAME FROM user WHERE USERLEVEL >= 81)) AND
        ( DTL.TICKETID = __TICKETID )
	ORDER BY
		DTL.DATEENTERED DESC;
		

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetSupportTicketList` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `GetSupportTicketList`(__USERNAME VARCHAR(50))
BEGIN

	SELECT
		*,
        UNIX_TIMESTAMP(HDR.DATEENTERED) AS UNIX_DATEENTERED
	FROM
		ticketHeader HDR
	WHERE
		( HDR.STATUS = 1 OR (HDR.STATUS = 0 AND DATECLOSED >= DATE_ADD(NOW(), INTERVAL -14 DAY)) ) AND
        ( HDR.ENTEREDBY = __USERNAME OR __USERNAME IN (SELECT USERNAME FROM user WHERE USERLEVEL >= 81))
	ORDER BY
		HDR.STATUS DESC,
		HDR.DATEENTERED DESC;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetTaxRecords` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetTaxRecords`()
BEGIN
	SELECT * FROM taxes;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetUserBranchAssets` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetUserBranchAssets`(__CODE VARCHAR(20))
BEGIN
	SELECT
		BAD.TAG,
        BAD.TYPE,
        BAD.NAME,
	IFNULL(BAD.VALIDATEPATTERN,'') AS VALIDATEPATTERN,
	IFNULL(BAD.VALIDATEERRORMSG,'') AS VALIDATEERRORMSG,
        IFNULL(BDATA.VALUE,'') AS VALUE
	FROM
		branchAssetDefinition BAD
	LEFT OUTER JOIN
		(SELECT * FROM branchesAssetData WHERE BRANCHCODE = __CODE) BDATA ON 
			BAD.TAG = BDATA.TAG
	WHERE
		BAD.USERACCESSIBLE = 1
	ORDER BY
		BAD.DISPLAYORDER;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetUserInformation` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetUserInformation`(UNAME VARCHAR(50))
BEGIN
	SELECT * FROM user WHERE USERNAME=UNAME;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `GetZoneCostByWeight` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetZoneCostByWeight`(__WEIGHT DECIMAL, __BRANCH VARCHAR(20))
BEGIN

	SELECT
		BRANCHZONE.SHIPZONE AS BRANCHZONE,
		ZONECOSTS.*
	FROM
		shipZoneCosts ZONECOSTS
	CROSS JOIN
		(SELECT SHIPZONE FROM branches WHERE CODE = __BRANCH) BRANCHZONE
	WHERE
		ZONECOSTS.WEIGHT <= __WEIGHT 
	ORDER BY
		WEIGHT DESC;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchAssetDefinitionDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchAssetDefinitionDelete`(__TAG VARCHAR(100))
BEGIN
	DELETE FROM branchAssetDefinition WHERE TAG = __TAG;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchAssetDefinitionInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchAssetDefinitionInsert`(__TAG VARCHAR(100), __TYPE VARCHAR(30), __NAME VARCHAR(100), __VALIDATEPATTERN VARCHAR(100), __DISPLAYORDER INT, __VALIDATEERRORMSG VARCHAR(100),__USERACCESSIBLE INT)
BEGIN
	INSERT INTO  
		branchAssetDefinition
        (TAG,TYPE,NAME,VALIDATEPATTERN,DISPLAYORDER,VALIDATEERRORMSG,USERACCESSIBLE)
	VALUES
		(__TAG,__TYPE,__NAME,__VALIDATEPATTERN,__DISPLAYORDER,__VALIDATEERRORMSG,__USERACCESSIBLE);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchAssetDefinitionUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchAssetDefinitionUpdate`(__TAG VARCHAR(100), __TYPE VARCHAR(30), __NAME VARCHAR(100), __VALIDATEPATTERN VARCHAR(100), __DISPLAYORDER INT, __VALIDATEERRORMSG VARCHAR(100),__USERACCESSIBLE INT   )
BEGIN

	UPDATE 
		branchAssetDefinition
	SET
		 TYPE = __TYPE, 
         NAME = __NAME,
         VALIDATEPATTERN = __VALIDATEPATTERN, 
         DISPLAYORDER = __DISPLAYORDER,
         VALIDATEERRORMSG = __VALIDATEERRORMSG,
         USERACCESSIBLE = __USERACCESSIBLE
    WHERE
		TAG = __TAG;
    

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchesAddressesInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchesAddressesInsert`(__CODE VARCHAR(20), __LANGUAGE VARCHAR(2), __ISMAIN INT, __ADDRESS VARCHAR(1000), __ADDRESS2 VARCHAR(1000), __CITY VARCHAR(100), __PROVINCE VARCHAR(100), __COUNTRY VARCHAR(100), __POSTALCODE VARCHAR(20), __PHONE VARCHAR(20), __FAX VARCHAR(20))
BEGIN

	INSERT INTO
		branchesAddresses
        (CODE,LANGUAGE,ISMAIN,ADDRESS,ADDRESS2,CITY,PROVINCE,COUNTRY,POSTALCODE,PHONE,FAX)
	VALUES
		(__CODE,__LANGUAGE,__ISMAIN,__ADDRESS,__ADDRESS2,__CITY,__PROVINCE,__COUNTRY,__POSTALCODE,__PHONE,__FAX);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchesAddressesUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchesAddressesUpdate`(__CODE VARCHAR(20), __LANGUAGE VARCHAR(2), __ISMAIN INT, __ADDRESS VARCHAR(1000), __ADDRESS2 VARCHAR(1000), __CITY VARCHAR(100), __PROVINCE VARCHAR(100), __COUNTRY VARCHAR(100), __POSTALCODE VARCHAR(20), __PHONE VARCHAR(20), __FAX VARCHAR(20))
BEGIN

UPDATE
	branchesAddresses
SET
    ISMAIN = __ISMAIN,
    ADDRESS = __ADDRESS,
    ADDRESS2 = __ADDRESS2,
    CITY = __CITY,
    PROVINCE = __PROVINCE,
    COUNTRY = __COUNTRY,
    POSTALCODE = __POSTALCODE,
    PHONE = __PHONE,
    FAX = __FAX
WHERE
	CODE = __CODE AND
    LANGUAGE = __LANGUAGE;


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchesAssetDataDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchesAssetDataDelete`(__CODE VARCHAR(20), __TAG VARCHAR(100))
BEGIN

	DELETE FROM branchesAssetData WHERE BRANCHCODE = __CODE AND TAG = __TAG;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchesAssetDataInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchesAssetDataInsert`(__CODE VARCHAR(20), __TAG VARCHAR(100), __VALUE VARCHAR(1000))
BEGIN

	INSERT INTO
		branchesAssetData
			(BRANCHCODE,TAG,VALUE)
	VALUES
			(__CODE,__TAG,__VALUE);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchesAssetDataUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchesAssetDataUpdate`(__CODE VARCHAR(20), __TAG VARCHAR(100), __VALUE VARCHAR(1000))
BEGIN

	UPDATE 
		branchesAssetData
	SET
		VALUE = __VALUE
	WHERE
		BRANCHCODE = __CODE AND
        TAG = __TAG;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchesDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchesDelete`(__CODE VARCHAR(20), __LANGUAGE VARCHAR(2))
BEGIN

	DELETE FROM branchesAddresses WHERE CODE = __CODE AND LANGUAGE = __LANGUAGE;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchesInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchesInsert`(__CODE VARCHAR(20), __NAME VARCHAR(100), __MANAGEMENTEMAIL VARCHAR(100), __TREASUREREMAIL VARCHAR(100), __TAXGROUP VARCHAR(6), __SKIN VARCHAR(20), __STATUS INT, __COMMENTS VARCHAR(1000), __SHIPZONE VARCHAR(6))
BEGIN

	INSERT INTO
		branches
        (CODE,NAME,MANAGEMENTEMAIL,TREASUREREMAIL,TAXGROUP,SKIN,STATUS,COMMENTS,SHIPZONE)
	VALUES
		(__CODE,__NAME,__MANAGEMENTEMAIL,__TREASUREREMAIL,__TAXGROUP,__SKIN,__STATUS,__COMMENTS,__SHIPZONE);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchesUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchesUpdate`(__CODE VARCHAR(20), __NAME VARCHAR(100), __MANAGEMENTEMAIL VARCHAR(100), __TREASUREREMAIL VARCHAR(100), __TAXGROUP VARCHAR(6), __SKIN VARCHAR(20), __STATUS INT, __COMMENTS VARCHAR(1000), __SHIPZONE VARCHAR(6))
BEGIN

	UPDATE
		branches
	SET
		NAME = __NAME,
        MANAGEMENTEMAIL = __MANAGEMENTEMAIL,
        TREASUREREMAIL = __TREASUREREMAIL,
        TAXGROUP = __TAXGROUP,
        SKIN = __SKIN,
        STATUS = __STATUS,
        COMMENTS = __COMMENTS,
        SHIPZONE = __SHIPZONE
	WHERE
		CODE = __CODE;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchSkinDefinitionDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchSkinDefinitionDelete`(__SKIN VARCHAR(20))
BEGIN
	DELETE FROM branchSkinDefinition WHERE SKINNAME = __SKIN;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchSkinDefinitionInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchSkinDefinitionInsert`(__SKIN VARCHAR(20), __STYLEFILE VARCHAR(45))
BEGIN
	INSERT INTO
		branchSkinDefinition (SKINNAME, STYLEFILENAME)
	VALUES
		(__SKIN,__STYLEFILE);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_BranchSkinDefinitionUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_BranchSkinDefinitionUpdate`(__SKIN VARCHAR(20), __STYLEFILE VARCHAR(45))
BEGIN
	UPDATE
		branchSkinDefinition
	SET
		STYLEFILENAME = __STYLEFILE
	WHERE
		SKINNAME = __SKIN;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CategoriesCheckCATID` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CategoriesCheckCATID`(__CATID INT)
BEGIN

	SELECT CATID FROM categories WHERE CATID = __CATID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CategoriesDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CategoriesDelete`(__CATID INT)
BEGIN

	DELETE FROM categories WHERE CATID = __CATID;
	DELETE FROM subcategories WHERE CATID = __CATID;
	DELETE FROM itemsSubcategoryLink WHERE 
		SUBCATID NOT IN (SELECT SUBCATID FROM subcategories);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CategoriesInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CategoriesInsert`(__CATID INT, __NAME VARCHAR(50), __DESCRIPTION VARCHAR(200), __ICONURL VARCHAR(200), __LANGUAGE VARCHAR(2), __USERLEVEL INT,__COMMENTS VARCHAR(1000))
BEGIN

	INSERT INTO
		categories
        (CATID,NAME,DESCRIPTION,ICONURL,LANGUAGE,USERLEVEL,COMMENTS,CREATEDDATE,MODIFIEDDATE)
	VALUES
		(__CATID,__NAME,__DESCRIPTION,__ICONURL,__LANGUAGE,__USERLEVEL,__COMMENTS,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CategoriesUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CategoriesUpdate`(__CATID INT, __NAME VARCHAR(50), __DESCRIPTION VARCHAR(200), __ICONURL VARCHAR(200), __LANGUAGE VARCHAR(2), __USERLEVEL INT,__COMMENTS VARCHAR(1000))
BEGIN

	UPDATE
		categories
	SET
		NAME = __NAME,
        DESCRIPTION = __DESCRIPTION,
        ICONURL = __ICONURL,
        LANGUAGE = __LANGUAGE,
        USERLEVEL = __USERLEVEL,
        COMMENTS = __COMMENTS,
        MODIFIEDDATE = CURRENT_TIMESTAMP
	WHERE
		CATID = __CATID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CheckBRANCHASSETTAG` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CheckBRANCHASSETTAG`(__TAG VARCHAR(100))
BEGIN
	SELECT * FROM branchAssetDefinition WHERE TAG = __TAG;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CheckBRANCHCODE` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CheckBRANCHCODE`(__CODE VARCHAR(20))
BEGIN

	SELECT
		CODE 
	FROM
		branches
	WHERE
		CODE = __CODE;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CheckITEMID` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CheckITEMID`(__ITEMID INT)
BEGIN

	SELECT ID FROM items WHERE ID = __ITEMID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CheckNEWSID` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CheckNEWSID`(__NEWSID INT)
BEGIN

	SELECT ID FROM newsItems WHERE ID = __NEWSID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CheckPROMOCODE` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CheckPROMOCODE`(__CODE VARCHAR(50))
BEGIN
	SELECT * FROM promos WHERE CODE = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CheckSHIPID` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CheckSHIPID`(__SHIPID INT)
BEGIN
	SELECT ID FROM shipToAddresses WHERE ID = __SHIPID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CheckSHIPTOUSER` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `importTableData_CheckSHIPTOUSER`(__shipid INT, __username VARCHAR(30))
BEGIN
	SELECT * FROM shipToAddressesUserList WHERE ID = __shipid AND USERNAME = __username;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CheckSHIPWEIGHT` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CheckSHIPWEIGHT`(__WEIGHT INT)
BEGIN

	SELECT WEIGHT FROM shipZoneCosts WHERE WEIGHT = __WEIGHT;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CheckSHIPZONE` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CheckSHIPZONE`(__SHIPZONE VARCHAR(6))
BEGIN

	SELECT 
		COLUMN_NAME 
	FROM 
		information_schema.COLUMNS 
	WHERE 
		TABLE_NAME = 'shipZoneCosts' AND 
        COLUMN_NAME COLLATE utf8_unicode_ci = __SHIPZONE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CheckSKINNAME` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CheckSKINNAME`(__SKIN VARCHAR(20))
BEGIN

	SELECT * FROM branchSkinDefinition WHERE SKINNAME = __SKIN;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CheckSUBCATID` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CheckSUBCATID`(__SUBCATID INT)
BEGIN
	SELECT SUBCATID FROM subcategories WHERE SUBCATID = __SUBCATID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CheckTAXGROUP` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CheckTAXGROUP`(__TAXGROUP VARCHAR(6))
BEGIN

	SELECT TAXCODE FROM taxes WHERE TAXCODE = __TAXGROUP;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_CheckUSERNAME` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_CheckUSERNAME`(__USERNAME VARCHAR(50))
BEGIN

	SELECT USERNAME FROM user WHERE USERNAME = __USERNAME;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ItemPricingDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_ItemPricingDelete`(__ITEMID INT, __QTY INT, __ITEMOPTION VARCHAR(30))
BEGIN

 DELETE FROM itemPricing WHERE ITEMID = __ITEMID AND QTY = __QTY AND ITEMOPTION = __ITEMOPTION;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ItemPricingInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_ItemPricingInsert`(__ITEMID INT, __QTY INT, __ITEMOPTION VARCHAR(30), __PRICE DECIMAL(19,4), __LENGTH DECIMAL(19,4), __WIDTH DECIMAL(19,4), __HEIGHT DECIMAL(19,4), __WEIGHT DECIMAL(19,6), __PIECES INT)
BEGIN

 INSERT INTO 
 itemPricing
 (ITEMID,QTY,PRICE,LENGTH,WIDTH,HEIGHT,WEIGHT,PIECES,ITEMOPTION)
 VALUES
 (__ITEMID,__QTY,__PRICE,__LENGTH,__WIDTH,__HEIGHT,__WEIGHT,__PIECES,__ITEMOPTION);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ItemPricingUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_ItemPricingUpdate`(__ITEMID INT, __QTY INT, __ITEMOPTION VARCHAR(30), __PRICE DECIMAL(19,4),  __LENGTH DECIMAL(19,4), __WIDTH DECIMAL(19,4), __HEIGHT DECIMAL(19,4), __WEIGHT DECIMAL(19,6), __PIECES INT)
BEGIN

 UPDATE
		itemPricing
	SET
		PRICE = __PRICE,
        LENGTH = __LENGTH,
        WIDTH = __WIDTH,
        HEIGHT = __HEIGHT,
        WEIGHT = __WEIGHT,
        PIECES = __PIECES,
        ITEMOPTION = __ITEMOPTION
	WHERE
		ITEMID = __ITEMID AND
        QTY = __QTY AND
        ITEMOPTION = __ITEMOPTION;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ItemPrintDefinitionDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_ItemPrintDefinitionDelete`(__ITEMID INT)
BEGIN
  DELETE FROM itemsPrintDefinition WHERE ID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ItemPrintDefinitionInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_ItemPrintDefinitionInsert`(__ITEMID INT, __PRESS VARCHAR(100),__NUMPAGES VARCHAR(100),__BLEEDS INT, __COLOURS VARCHAR(100),
__FLATSIZE VARCHAR(100), __FINISHSIZE VARCHAR(100), __STOCK VARCHAR(100), __PARENTSHEETSIZE VARCHAR(100),
__PRESSSHEETSIZE VARCHAR(100), __NUMUP VARCHAR(100), __LAYOUTORIENTATION VARCHAR(100),
__COATING VARCHAR(100), __CONTOURCUT VARCHAR(100), __LAMINATION VARCHAR(100), __FINISHING VARCHAR(200),
__ACCESSORY VARCHAR(200), __SPECIALINSTRUCTIONS VARCHAR(200), __MEMO VARCHAR(200), __PACKAGELOCATION VARCHAR(200),
__OUT VARCHAR(100), __FINISHED VARCHAR(100), __OVERS VARCHAR(100), __GROSS VARCHAR(100))
BEGIN

INSERT INTO itemsPrintDefinition
	(ID,PRESS,NUMPAGES,BLEEDS,COLOURS,FLATSIZE,FINISHSIZE,STOCK,PARENTSHEETSIZE,
	 PRESSSHEETSIZE,NUMUP,LAYOUTORIENTATION,COATING,CONTOURCUT,LAMINATION,FINISHING,
	 ACCESSORY,SPECIALINSTRUCTIONS,MEMO,PACKAGELOCATION,`OUT`,FINISHED,OVERS,GROSS)
	VALUES 
	(__ITEMID,__PRESS,__NUMPAGES,__BLEEDS,__COLOURS,__FLATSIZE,__FINISHSIZE,__STOCK,__PARENTSHEETSIZE,
	 __PRESSSHEETSIZE,__NUMUP,__LAYOUTORIENTATION,__COATING,__CONTOURCUT,__LAMINATION,__FINISHING,
	 __ACCESSORY,__SPECIALINSTRUCTIONS,__MEMO,__PACKAGELOCATION,__OUT,__FINISHED,__OVERS,__GROSS);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ItemPrintDefinitionUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_ItemPrintDefinitionUpdate`(__ITEMID INT, __PRESS VARCHAR(100),__NUMPAGES VARCHAR(100),__BLEEDS INT, __COLOURS VARCHAR(100),
__FLATSIZE VARCHAR(100), __FINISHSIZE VARCHAR(100), __STOCK VARCHAR(100), __PARENTSHEETSIZE VARCHAR(100),
__PRESSSHEETSIZE VARCHAR(100), __NUMUP VARCHAR(100), __LAYOUTORIENTATION VARCHAR(100),
__COATING VARCHAR(100), __CONTOURCUT VARCHAR(100), __LAMINATION VARCHAR(100), __FINISHING VARCHAR(200),
__ACCESSORY VARCHAR(200), __SPECIALINSTRUCTIONS VARCHAR(200), __MEMO VARCHAR(200), __PACKAGELOCATION VARCHAR(200),
__OUT VARCHAR(100), __FINISHED VARCHAR(100), __OVERS VARCHAR(100), __GROSS VARCHAR(100)
)
BEGIN
	UPDATE itemsPrintDefinition
	SET 
		PRESS = __PRESS,
		NUMPAGES = __NUMPAGES,
		BLEEDS = __BLEEDS,
		COLOURS = __COLOURS,
		FLATSIZE = __FLATSIZE,
		FINISHSIZE = __FINISHSIZE,
		STOCK = __STOCK,
		PARENTSHEETSIZE = __PARENTSHEETSIZE,
		PRESSSHEETSIZE = __PRESSSHEETSIZE,
		NUMUP = __NUMUP,
		LAYOUTORIENTATION = __LAYOUTORIENTATION,
		COATING = __COATING,
		CONTOURCUT = __CONTOURCUT,
		LAMINATION = __LAMINATION,
		FINISHING = __FINISHING,
		ACCESSORY = __ACCESSORY,
		SPECIALINSTRUCTIONS = __SPECIALINSTRUCTIONS,
        MEMO = __MEMO,
		PACKAGELOCATION = __PACKAGELOCATION,
        `OUT` = __OUT , 
        FINISHED = __FINISHED, 
        OVERS = __OVERS, 
        GROSS = __GROSS
	WHERE ID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_itemsBranchesLinkDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_itemsBranchesLinkDelete`(__ITEMID INT, __BRANCHCODE VARCHAR(20))
BEGIN

	DELETE FROM itemsBranchesLink WHERE ITEMID = __ITEMID AND BRANCHCODE = __BRANCHCODE;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_itemsBranchesLinkInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_itemsBranchesLinkInsert`(__ITEMID INT, __BRANCHCODE VARCHAR(20))
BEGIN

	INSERT INTO 
		itemsBranchesLink
        (ITEMID,BRANCHCODE)
	VALUES
		(__ITEMID,__BRANCHCODE);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ItemsDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_ItemsDelete`(__ID INT)
BEGIN

  DELETE FROM items WHERE ID = __ID;
    DELETE FROM itemPricing WHERE ITEMID = __ID;
    DELETE FROM itemsBranchesLink WHERE ITEMID = __ID;
  DELETE FROM itemsSubcategoryLink WHERE ITEMID = __ID;
  DELETE FROM itemsInventory WHERE ITEMID = __ID;
    DELETE FROM itemsPrintDefinition WHERE ID = __ID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ItemsInsert` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `importTableData_ItemsInsert`(__ID INT, __TYPE INT, __CODE VARCHAR(50),
__DESCRIPTION VARCHAR(200), __PRODUCEDETAIL VARCHAR(1000), __SIZE VARCHAR(50), __NUMPAGES INT, __STATUS INT, __MINLEVEL INT,
__LEADTIME INT, __DISPLAYIMAGENAME VARCHAR(200), __PROOFPDFNAME VARCHAR(200), __PARENTITEM INT,
__ARCHIVED INT, __GENERATEDBY VARCHAR(200), __ASSETAPPROVED INT, __PARAMETERSFILENAME VARCHAR(200),
__UPLOADDIR VARCHAR(200),__PREVIEWIMAGENAME VARCHAR(200), __SHIPBOXINSLEEVE INT, __SKIPPDFPERMISSIONCHECK INT, __DLSUGGESTIONS VARCHAR(1000))
BEGIN

DECLARE __DISPNAME VARCHAR(200) DEFAULT NULL;
DECLARE __PROOFNAME VARCHAR(200) DEFAULT NULL;
DECLARE __PREVIEWNAME VARCHAR(200) DEFAULT NULL;
DECLARE __PARAMETERSNAME VARCHAR(200) DEFAULT NULL;
DECLARE __DISPPATH VARCHAR(200) DEFAULT NULL;
DECLARE __PROOFPATH VARCHAR(200) DEFAULT NULL;
DECLARE __PREVIEWPATH VARCHAR(200) DEFAULT NULL;
DECLARE __PARAMETERSPATH VARCHAR(200) DEFAULT NULL;

IF LENGTH(__DISPLAYIMAGENAME) > 0 THEN
  SET __DISPNAME = __DISPLAYIMAGENAME;
    SET __DISPPATH = CONCAT(__UPLOADDIR,__DISPLAYIMAGENAME);
END IF;
IF LENGTH(__PROOFPDFNAME) > 0 THEN
  SET __PROOFNAME = __PROOFPDFNAME;
    SET __PROOFPATH = CONCAT(__UPLOADDIR,__PROOFPDFNAME);
END IF;
IF LENGTH(__PREVIEWIMAGENAME) > 0 THEN
  SET __PREVIEWNAME = __PREVIEWIMAGENAME;
    SET __PREVIEWPATH = CONCAT(__UPLOADDIR,__PREVIEWIMAGENAME);
END IF;
IF LENGTH(__PARAMETERSFILENAME) > 0 THEN
  SET __PARAMETERSNAME = __PARAMETERSFILENAME;
    SET __PARAMETERSPATH = CONCAT(__UPLOADDIR,__PARAMETERSFILENAME);
END IF;

INSERT
  items
    (ID,TYPE,CODE,DESCRIPTION,PRODUCEDETAIL,SIZE,NUMPAGES,STATUS,MINLEVEL,LEADTIME,DISPLAYIMAGENAME,
   DISPLAYIMAGEPATH,PROOFPDFNAME,PROOFPDFPATH,PARENTITEM,ARCHIVED,GENERATEDBY,
     ASSETAPPROVED,PARAMETERSFILENAME,PARAMETERSFILEPATH, PREVIEWIMAGENAME,PREVIEWIMAGEPATH,
     SHIPBOXINSLEEVE,SKIPPDFPERMISSIONCHECK,DLSUGGESTIONS)
VALUES
  (__ID,__TYPE,__CODE,__DESCRIPTION,__PRODUCEDETAIL,__SIZE,__NUMPAGES,__STATUS,__MINLEVEL,__LEADTIME,
     __DISPNAME,__DISPPATH,__PROOFNAME,__PROOFPATH,__PARENTITEM,__ARCHIVED,__GENERATEDBY,
     __ASSETAPPROVED,__PARAMETERSNAME,__PARAMETERSPATH, __PREVIEWNAME, __PREVIEWPATH,
     __SHIPBOXINSLEEVE,__SKIPPDFPERMISSIONCHECK,__DLSUGGESTIONS);
     
IF __TYPE = 0 THEN
  INSERT INTO 
    itemsInventory
    (ITEMID,QTYONHAND)
  VALUES (__ID,0);
END IF;     

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_itemsInventoryUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_itemsInventoryUpdate`(__ITEMID INT, __QTY INT)
BEGIN

	UPDATE itemsInventory 
    SET QTYONHAND = __QTY 
    WHERE 
		ITEMID = __ITEMID;
        
	INSERT INTO
		itemsAdjustmentLog
			(ITEMID,QTY,ADJDATE,REASON)
	SELECT
		ITEMID,
        __QTY,
        CURRENT_TIMESTAMP,
        'Import File, Set Value Absolutely'
	FROM
		itemsInventory
	WHERE
		ITEMID = __ITEMID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_itemsSubcategoryLinkDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_itemsSubcategoryLinkDelete`(__ITEMID INT, __SUBCATID INT)
BEGIN
	
    DELETE FROM itemsSubcategoryLink WHERE ITEMID = __ITEMID AND SUBCATID = __SUBCATID;
    
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_itemsSubcategoryLinkInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_itemsSubcategoryLinkInsert`(__ITEMID INT, __SUBCATID INT)
BEGIN
	
    INSERT INTO 
		itemsSubcategoryLink
        (ITEMID,SUBCATID)
	VALUES
		(__ITEMID,__SUBCATID);
       
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ItemsUpdate` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `importTableData_ItemsUpdate`(__ID INT, __TYPE INT, __CODE VARCHAR(50),
__DESCRIPTION VARCHAR(200), __PRODUCEDETAIL VARCHAR(1000), __SIZE VARCHAR(50), __NUMPAGES INT, __STATUS INT, __MINLEVEL INT,
__LEADTIME INT, __DISPLAYIMAGENAME VARCHAR(200), __PROOFPDFNAME VARCHAR(200), __PARENTITEM INT,
__ARCHIVED INT, __GENERATEDBY VARCHAR(200), __ASSETAPPROVED INT, __PARAMETERSFILENAME VARCHAR(200),
__UPLOADDIR VARCHAR(200), __PREVIEWIMAGENAME VARCHAR(200), __SHIPBOXINSLEEVE INT, __SKIPPDFPERMISSIONCHECK INT, __DLSUGGESTIONS VARCHAR(1000))
BEGIN

DECLARE __DISPNAME VARCHAR(200) DEFAULT NULL;
DECLARE __PROOFNAME VARCHAR(200) DEFAULT NULL;
DECLARE __PREVIEWNAME VARCHAR(200) DEFAULT NULL;
DECLARE __PARAMETERSNAME VARCHAR(200) DEFAULT NULL;
DECLARE __DISPPATH VARCHAR(200) DEFAULT NULL;
DECLARE __PROOFPATH VARCHAR(200) DEFAULT NULL;
DECLARE __PREVIEWPATH VARCHAR(200) DEFAULT NULL;
DECLARE __PARAMETERSPATH VARCHAR(200) DEFAULT NULL;

IF LENGTH(__DISPLAYIMAGENAME) > 0 THEN
  SET __DISPNAME = __DISPLAYIMAGENAME;
    SET __DISPPATH = CONCAT(__UPLOADDIR,__DISPLAYIMAGENAME);
END IF;
IF LENGTH(__PROOFPDFNAME) > 0 THEN
  SET __PROOFNAME = __PROOFPDFNAME;
    SET __PROOFPATH = CONCAT(__UPLOADDIR,__PROOFPDFNAME);
END IF;
IF LENGTH(__PREVIEWIMAGENAME) > 0 THEN
  SET __PREVIEWNAME = __PREVIEWIMAGENAME;
    SET __PREVIEWPATH = CONCAT(__UPLOADDIR,__PREVIEWIMAGENAME);
END IF;
IF LENGTH(__PARAMETERSFILENAME) > 0 THEN
  SET __PARAMETERSNAME = __PARAMETERSFILENAME;
    SET __PARAMETERSPATH = CONCAT(__UPLOADDIR,__PARAMETERSFILENAME);
END IF;

UPDATE
  items
SET
  CODE = __CODE,
    DESCRIPTION = __DESCRIPTION,
    PRODUCEDETAIL = __PRODUCEDETAIL,
    SIZE = __SIZE,
    NUMPAGES = __NUMPAGES,
    STATUS = __STATUS,
    MINLEVEL = __MINLEVEL,
    LEADTIME = __LEADTIME,
    DISPLAYIMAGENAME = __DISPNAME,
    DISPLAYIMAGEPATH = __DISPPATH,
    PROOFPDFNAME = __PROOFNAME,
    PROOFPDFPATH = __PROOFPATH,
    PREVIEWIMAGENAME = __PREVIEWNAME,
    PREVIEWIMAGEPATH = __PREVIEWPATH,    
    PARENTITEM = __PARENTITEM,
    ARCHIVED = __ARCHIVED,
    GENERATEDBY = __GENERATEDBY,
    ASSETAPPROVED = __ASSETAPPROVED,
    PARAMETERSFILENAME = __PARAMETERSNAME,
    PARAMETERSFILEPATH = __PARAMETERSPATH,
    SHIPBOXINSLEEVE = __SHIPBOXINSLEEVE,
    SKIPPDFPERMISSIONCHECK = __SKIPPDFPERMISSIONCHECK,
    DLSUGGESTIONS = __DLSUGGESTIONS
WHERE
  ID = __ID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_NewsDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_NewsDelete`(__NEWSID INT)
BEGIN

	DELETE FROM newsItems WHERE ID = __NEWSID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_NewsInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_NewsInsert`(__NEWSID INT, __STARTDATE DATE, __ENDDATE DATE,__LANGUAGE VARCHAR(2), __TITLE VARCHAR(1000), __MESSAGE VARCHAR(1000))
BEGIN

	INSERT INTO
		newsItems
        (ID,STARTDATE,ENDDATE,LANGUAGE,TITLE,MESSAGE)
	VALUES
		(__NEWSID,__STARTDATE,__ENDDATE,__LANGUAGE,__TITLE,__MESSAGE);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_NewsUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_NewsUpdate`(__NEWSID INT, __STARTDATE DATE, __ENDDATE DATE,__LANGUAGE VARCHAR(2), __TITLE VARCHAR(1000), __MESSAGE VARCHAR(1000))
BEGIN

	UPDATE
		newsItems
	SET
		STARTDATE = __STARTDATE,
        ENDDATE = __ENDDATE,
        LANGUAGE = __LANGUAGE,
        TITLE = __TITLE,
        MESSAGE = __MESSAGE
	WHERE
		ID = __NEWSID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_PromoDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_PromoDelete`(__CODE VARCHAR(50))
BEGIN
	DELETE FROM promos WHERE CODE = __CODE;
    DELETE FROM promosItems WHERE CODE = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_PromoInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_PromoInsert`(__CODE VARCHAR(50), __NAME VARCHAR(100), __CODETYPE VARCHAR(8), __STARTDATE DATE, __ENDDATE DATE, __SINGLEUSE INT, __REDEEMED INT, __PROMOVALUE DECIMAL(19,2), __MAXQTY INT, __SINGLEITEM INT)
BEGIN
 INSERT INTO 
 promos
 (CODE,NAME,CODETYPE,STARTDATE,ENDDATE,SINGLEUSE,REDEEMED,PROMOVALUE,MAXQTY,SINGLEITEM)
 VALUES
 (__CODE, __NAME, __CODETYPE, __STARTDATE, __ENDDATE, __SINGLEUSE, __REDEEMED, __PROMOVALUE,__MAXQTY, __SINGLEITEM);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_PromoItemsDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_PromoItemsDelete`(__CODE VARCHAR(50), __ITEMID INT)
BEGIN
	DELETE FROM promosItems WHERE CODE = __CODE AND ITEMID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_PromoItemsInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_PromoItemsInsert`(__CODE VARCHAR(50), __ITEMID INT)
BEGIN
	INSERT INTO promosItems
    (CODE,ITEMID)
    VALUES
    (__CODE,__ITEMID);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_PromoItemsUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_PromoItemsUpdate`(__CODE VARCHAR(50), __ITEMID INT)
BEGIN
	UPDATE
		promosItems
	SET
		CODE = __CODE,
        ITEMID = __ITEMID
	WHERE
		CODE = __CODE AND
        ITEMID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_PromoUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_PromoUpdate`(__CODE VARCHAR(50), __NAME VARCHAR(100), __CODETYPE VARCHAR(8), __STARTDATE DATE, __ENDDATE DATE, __SINGLEUSE INT, __REDEEMED INT, __PROMOVALUE DECIMAL(19,2), __MAXQTY INT, __SINGLEITEM INT)
BEGIN
 UPDATE
 promos
 SET
 NAME = __NAME,
        CODETYPE = __CODETYPE,
        STARTDATE = __STARTDATE,
        ENDDATE = __ENDDATE,
        SINGLEUSE = __SINGLEUSE,
        REDEEMED = __REDEEMED,
        PROMOVALUE = __PROMOVALUE,
        MAXQTY = __MAXQTY,
        SINGLEITEM = __SINGLEITEM
	WHERE
		CODE = __CODE;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ShippingCostsDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_ShippingCostsDelete`(__WEIGHT INT)
BEGIN

	DELETE FROM shipZoneCosts WHERE WEIGHT = __WEIGHT;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ShippingCostsInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_ShippingCostsInsert`(__WEIGHT INT, __NONE DECIMAL(16,2),
 __ZONE1 DECIMAL(16,2), __ZONE2 DECIMAL(16,2), __ZONE3 DECIMAL(16,2), __ZONE4 DECIMAL(16,2),
 __ZONE5 DECIMAL(16,2), __ZONE6 DECIMAL(16,2), __ZONE7 DECIMAL(16,2), __ZONE8 DECIMAL(16,2),
 __ZONE9 DECIMAL(16,2), __ZONE10 DECIMAL(16,2), __ZONE11 DECIMAL(16,2), __ZONE12 DECIMAL(16,2),
 __ZONE13 DECIMAL(16,2), __ZONE14 DECIMAL(16,2), __ZONE15 DECIMAL(16,2), __ZONE16 DECIMAL(16,2),
 __ZONE17 DECIMAL(16,2), __ZONE18 DECIMAL(16,2), __ZONE19 DECIMAL(16,2))
BEGIN

	INSERT INTO
		shipZoneCosts
        (WEIGHT,None,Zone1,Zone2,Zone3,Zone4,Zone5,Zone6,Zone7,Zone8,Zone9,Zone10,
         Zone11,Zone12,Zone13,Zone14,Zone15,Zone16,Zone17,Zone18,Zone19)
	VALUES
		(__WEIGHT,__NONE,__ZONE1,__ZONE2,__ZONE3,__ZONE4,__ZONE5,__ZONE6,__ZONE7,__ZONE8,
        __ZONE9,__ZONE10,__ZONE11,__ZONE12,__ZONE13,__ZONE14,__ZONE15,__ZONE16,__ZONE17,
        __ZONE18,__ZONE19);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ShippingCostsUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_ShippingCostsUpdate`(__WEIGHT INT, __NONE DECIMAL(16,2),
 __ZONE1 DECIMAL(16,2), __ZONE2 DECIMAL(16,2), __ZONE3 DECIMAL(16,2), __ZONE4 DECIMAL(16,2),
 __ZONE5 DECIMAL(16,2), __ZONE6 DECIMAL(16,2), __ZONE7 DECIMAL(16,2), __ZONE8 DECIMAL(16,2),
 __ZONE9 DECIMAL(16,2), __ZONE10 DECIMAL(16,2), __ZONE11 DECIMAL(16,2), __ZONE12 DECIMAL(16,2),
 __ZONE13 DECIMAL(16,2), __ZONE14 DECIMAL(16,2), __ZONE15 DECIMAL(16,2), __ZONE16 DECIMAL(16,2),
 __ZONE17 DECIMAL(16,2), __ZONE18 DECIMAL(16,2), __ZONE19 DECIMAL(16,2))
BEGIN

	UPDATE
		shipZoneCosts
	SET
		None  = __NONE,
        Zone1 = __ZONE1,
        Zone2 = __ZONE2,
        Zone3 = __ZONE3,
        Zone4 = __ZONE4,
        Zone5 = __ZONE5,
        Zone6 = __ZONE6,
        Zone7 = __ZONE7,
        Zone8 = __ZONE8,
        Zone9 = __ZONE9,
        Zone10 = __ZONE10,
        Zone11 = __ZONE11,
        Zone12 = __ZONE12,
        Zone13 = __ZONE13,
        Zone14 = __ZONE14,
        Zone15 = __ZONE15,
        Zone16 = __ZONE16,
        Zone17 = __ZONE17,
        Zone18 = __ZONE18,
        Zone19 = __ZONE19
	WHERE
		WEIGHT = __WEIGHT;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ShipToDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_ShipToDelete`(__SHIPID INT)
BEGIN

	DELETE FROM shipToAddresses WHERE ID = __SHIPID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ShipToInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_ShipToInsert`(__SHIPID INT, __NAME VARCHAR(100), __STOREID VARCHAR(20), __ADDRESS VARCHAR(1000), __ADDRESS2 VARCHAR(1000), __CITY VARCHAR(100), __PROVINCE VARCHAR(100), __COUNTRY VARCHAR(100), __POSTAL VARCHAR(20), __PHONE VARCHAR(20), __FAX VARCHAR(20), __TAXGROUP VARCHAR(6), __SHIPZONE VARCHAR(6))
BEGIN

	INSERT INTO
		shipToAddresses
			(ID,NAME,STOREID,ADDRESS,ADDRESS2,CITY,PROVINCE,COUNTRY,POSTAL,PHONE,FAX,TAXGROUP,SHIPZONE)
	VALUES
		(__SHIPID,__NAME,__STOREID,__ADDRESS,__ADDRESS2,__CITY,__PROVINCE,__COUNTRY,__POSTAL,__PHONE,__FAX,__TAXGROUP,__SHIPZONE);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ShipToUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_ShipToUpdate`(__SHIPID INT, __NAME VARCHAR(100), __STOREID VARCHAR(20), __ADDRESS VARCHAR(1000), __ADDRESS2 VARCHAR(1000), __CITY VARCHAR(100), __PROVINCE VARCHAR(100), __COUNTRY VARCHAR(100), __POSTAL VARCHAR(20), __PHONE VARCHAR(20), __FAX VARCHAR(20), __TAXGROUP VARCHAR(6), __SHIPZONE VARCHAR(6))
BEGIN

	UPDATE
		shipToAddresses
	SET
		NAME = __NAME,
        STOREID = __STOREID,
        ADDRESS = __ADDRESS,
        ADDRESS2 = __ADDRESS2,
        CITY = __CITY,
        PROVINCE = __PROVINCE,
        COUNTRY = __COUNTRY,
        POSTAL = __POSTAL,
        PHONE = __PHONE,
        FAX = __FAX,
        TAXGROUP = __TAXGROUP,
        SHIPZONE = __SHIPZONE       
	WHERE
		ID = __SHIPID;


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ShipToUserListDelete` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `importTableData_ShipToUserListDelete`(__shipid INT, __username VARCHAR(30))
BEGIN
	DELETE FROM shipToAddressesUserList WHERE ID = __shipid AND USERNAME = __username; 
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_ShipToUserListInsert` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `importTableData_ShipToUserListInsert`(__shipid INT, __username VARCHAR(30))
BEGIN
	INSERT INTO shipToAddressesUserList (ID,USERNAME) VALUES (__shipid,__username); 
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_SubcategoriesDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_SubcategoriesDelete`(__SUBCATID INT)
BEGIN

	DELETE FROM subcategories WHERE SUBCATID = __SUBCATID;
	DELETE FROM itemsSubcategoryLink WHERE 
		SUBCATID NOT IN (SELECT SUBCATID FROM subcategories);    

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_SubcategoriesInsert` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `importTableData_SubcategoriesInsert`(__SUBCATID INT, __CATID INT, __PARENTSUBCATID INT, __NAME VARCHAR(50), __DESCRIPTION VARCHAR(200), __ICONURL VARCHAR(200), __USERLEVEL INT)
BEGIN

    INSERT INTO
        subcategories
        (SUBCATID,CATID,PARENTSUBCATID,NAME,DESCRIPTION,ICONURL,USERLEVEL)
    VALUES
        (__SUBCATID,__CATID,__PARENTSUBCATID,__NAME,__DESCRIPTION,__ICONURL,__USERLEVEL);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_SubcategoriesUpdate` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `importTableData_SubcategoriesUpdate`(__SUBCATID INT, __CATID INT, __NAME VARCHAR(50), __DESCRIPTION VARCHAR(200), __ICONURL VARCHAR(200), __USERLEVEL INT)
BEGIN

    UPDATE
        subcategories
    SET
        CATID = __CATID,
        NAME = __NAME,
        DESCRIPTION = __DESCRIPTION,
        ICONURL = __ICONURL,
        USERLEVEL = __USERLEVEL
    WHERE
        SUBCATID = __SUBCATID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_TaxesDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_TaxesDelete`(__TAXCODE VARCHAR(6))
BEGIN

	DELETE FROM taxes WHERE TAXCODE = __TAXCODE;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_TaxesInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_TaxesInsert`(__TAXCODE VARCHAR(6), __TAXGROUPNAME VARCHAR(100), __TAXNAME1 VARCHAR(100), __TAXNAME2 VARCHAR(100), __TAXNAME3 VARCHAR(100), __TAXNAME4 VARCHAR(100), __TAXNAME5 VARCHAR(100), __TAXRATE1 DECIMAL(19,5), __TAXRATE2 DECIMAL(19,5), __TAXRATE3 DECIMAL(19,5), __TAXRATE4 DECIMAL(19,5), __TAXRATE5 DECIMAL(19,5))
BEGIN

	INSERT INTO 
		taxes
        (TAXCODE,TAXGROUPNAME,TAXNAME1,TAXNAME2,TAXNAME3,TAXNAME4,TAXNAME5,TAXRATE1,TAXRATE2,TAXRATE3,TAXRATE4,TAXRATE5)
	VALUES
		(__TAXCODE,__TAXGROUPNAME,__TAXNAME1,__TAXNAME2,__TAXNAME3,__TAXNAME4,__TAXNAME5,__TAXRATE1,__TAXRATE2,__TAXRATE3,__TAXRATE4,__TAXRATE5);
        
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_TaxesUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_TaxesUpdate`(__TAXCODE VARCHAR(6), __TAXGROUPNAME VARCHAR(100), __TAXNAME1 VARCHAR(100), __TAXNAME2 VARCHAR(100), __TAXNAME3 VARCHAR(100), __TAXNAME4 VARCHAR(100), __TAXNAME5 VARCHAR(100), __TAXRATE1 DECIMAL(19,5), __TAXRATE2 DECIMAL(19,5), __TAXRATE3 DECIMAL(19,5), __TAXRATE4 DECIMAL(19,5), __TAXRATE5 DECIMAL(19,5))
BEGIN

	UPDATE
		taxes
	SET
		TAXGROUPNAME = __TAXGROUPNAME,
        TAXNAME1 = __TAXNAME1,
        TAXNAME2 = __TAXNAME2,
        TAXNAME3 = __TAXNAME3,
        TAXNAME4 = __TAXNAME4,
        TAXNAME5 = __TAXNAME5,
        TAXRATE1 = __TAXRATE1,
        TAXRATE2 = __TAXRATE2,
        TAXRATE3 = __TAXRATE3,
        TAXRATE4 = __TAXRATE4,
        TAXRATE5 = __TAXRATE5
	WHERE
		TAXCODE = __TAXCODE;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_UserDelete` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_UserDelete`(__USERNAME VARCHAR(20))
BEGIN

	DELETE FROM user WHERE USERNAME = __USERNAME;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_UserInsert` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_UserInsert`(__USERNAME VARCHAR(50), __BRANCH VARCHAR(20), __PLAINPASSWORD VARCHAR(50), __FIRSTNAME VARCHAR(100), __LASTNAME VARCHAR(100), __EMAIL VARCHAR(100), __LANGUAGE VARCHAR(2), __USERLEVEL INT, __STATUS INT, __COMMENTS VARCHAR(1000))
BEGIN

	INSERT INTO
		user
        (USERNAME,BRANCH,PLAINPASSWORD,PASSWORD,FIRSTNAME,LASTNAME,EMAIL,LANGUAGE,
         USERLEVEL,STATUS,COMMENTS,CREATEDDATE,MODIFIEDDATE)
	VALUES
		(__USERNAME,__BRANCH,__PLAINPASSWORD,CONCAT('*', UPPER(SHA1(UNHEX(SHA1(__PLAINPASSWORD))))),__FIRSTNAME,
        __LASTNAME,__EMAIL,__LANGUAGE,__USERLEVEL,__STATUS,__COMMENTS,CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP);


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `importTableData_UserUpdate` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `importTableData_UserUpdate`(__USERNAME VARCHAR(50), __BRANCH VARCHAR(20), __PLAINPASSWORD VARCHAR(50), __FIRSTNAME VARCHAR(100), __LASTNAME VARCHAR(100), __EMAIL VARCHAR(100), __LANGUAGE VARCHAR(2), __USERLEVEL INT, __STATUS INT, __COMMENTS VARCHAR(1000))
BEGIN

	UPDATE
		user
	SET
		BRANCH = __BRANCH,
        PLAINPASSWORD = __PLAINPASSWORD,
        PASSWORD = CONCAT('*', UPPER(SHA1(UNHEX(SHA1(__PLAINPASSWORD))))),
        FIRSTNAME = __FIRSTNAME,
        LASTNAME = __LASTNAME,
        EMAIL = __EMAIL,
        LANGUAGE = __LANGUAGE,
        USERLEVEL = __USERLEVEL,
        STATUS = __STATUS,
        COMMENTS = __COMMENTS
	WHERE
		USERNAME = __USERNAME;


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `InsertPromoCategory` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `InsertPromoCategory`(__CODE VARCHAR(50), __CATID INT)
BEGIN
  INSERT INTO 
    promosCategories 
  (CODE,CATID)
    VALUES 
  (__CODE,__CATID);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `InsertPromoItems` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `InsertPromoItems`(__CODE VARCHAR(50), __ITEMID INT)
BEGIN
	INSERT INTO 
		promosItems 
	(CODE,ITEMID)
    VALUES 
	(__CODE,__ITEMID);
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `MakeGeneratedItem` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `MakeGeneratedItem`(__USERNAME VARCHAR(50), __ITEMID INT, __UPLOADDIR VARCHAR(500), __DESCR VARCHAR(500), __MGRAPPROVAL BIT, __PREFIX VARCHAR(10))
BEGIN

DECLARE __NEXTCOUNT INT;
DECLARE __CHKCODE INT;
DECLARE __APPROVALCODE VARCHAR(50);
SELECT COUNT(*)+1 FROM items WHERE PARENTITEM = __ITEMID LIMIT 1 INTO __NEXTCOUNT;

SET __CHKCODE = 1;
WHILE __CHKCODE >= 1 DO
  SET __APPROVALCODE = MD5(UUID());
    SET __CHKCODE = (SELECT COUNT(*) FROM items WHERE APPROVALLINK = @__APPROVALCODE);
END WHILE;

INSERT INTO
  items
    (TYPE,CODE,DESCRIPTION,PRODUCEDETAIL,SIZE,NUMPAGES,STATUS,MINLEVEL,LEADTIME,
     DISPLAYIMAGENAME,DISPLAYIMAGEPATH,PROOFPDFNAME,PROOFPDFPATH,
     PARENTITEM,ARCHIVED,GENERATEDBY,ASSETAPPROVED,APPROVALLINK,PARAMETERSFILENAME,
     PARAMETERSFILEPATH,SHIPBOXINSLEEVE,SKIPPDFPERMISSIONCHECK,DLSUGGESTIONS)
SELECT
  3 AS TYPE,
    CONCAT(CODE,'-', RIGHT(CONCAT('00000',__NEXTCOUNT),5)) AS CODE,
    CASE 
    WHEN LENGTH(__DESCR) > 0 THEN CONCAT(DESCRIPTION,'  (',__DESCR,')')
        ELSE CONCAT(DESCRIPTION,'')
  END  AS DESCRIPTION,
    PRODUCEDETAIL AS PRODUCEDETAIL,
    SIZE AS SIZE,
    NUMPAGES AS NUMPAGES,
    STATUS AS STATUS,
    MINLEVEL AS MINLEVEL,
    LEADTIME AS LEADTIME,
    'PLACEHOLDER' AS DISPLAYIMAGENAME,
    'PLACEHOLDER' AS DISPLAYIMAGEPATH,
    'PLACEHOLDER' AS PROOFPDFNAME,
    'PLACEHOLDER' AS PROOFPDFPATH,
    ID AS PARENTITEM,
    0 AS ARCHIVED,
    __USERNAME AS GENERATEDBY,
    CASE WHEN __MGRAPPROVAL = 0 THEN 1 ELSE 0 END AS ASSETAPPROVED,
  CASE WHEN __MGRAPPROVAL = 0 THEN NULL ELSE __APPROVALCODE END AS APPROVALLINK,
    '' AS PARAMETERSFILENAME,
    '' AS PARAMETERSFILEPATH,
    SHIPBOXINSLEEVE,
    SKIPPDFPERMISSIONCHECK,
    DLSUGGESTIONS
FROM
  items ITEMDATA
WHERE
  ID = __ITEMID;
    
SET @INSERTID = LAST_INSERT_ID();
SET @BRANCHEMAIL = (SELECT MANAGEMENTEMAIL FROM branches WHERE CODE IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME));
SET @BRANCHCODE = (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME);
SELECT ID AS INSERTID,APPROVALLINK, @BRANCHEMAIL AS BEMAIL, CODE FROM items WHERE ID = @INSERTID;

UPDATE
  items
SET
    DISPLAYIMAGENAME = CONCAT(__PREFIX,@INSERTID,'-',@BRANCHCODE,'-',CODE,'.jpg'),
    DISPLAYIMAGEPATH = CONCAT(__UPLOADDIR,__PREFIX,@INSERTID,'-',@BRANCHCODE,'-',CODE,'.jpg'),
    PROOFPDFNAME = CONCAT(__PREFIX,@INSERTID,'-',@BRANCHCODE,'-',CODE,'.pdf'),
    PROOFPDFPATH = CONCAT(__UPLOADDIR,__PREFIX,@INSERTID,'-',@BRANCHCODE,'-',CODE,'.pdf')
WHERE
  ID = @INSERTID;

INSERT INTO
  itemPricing
    (ITEMID,QTY,PRICE,LENGTH,WIDTH,HEIGHT,WEIGHT,PIECES,ITEMOPTION)
SELECT
  @INSERTID AS ITEMID,
    QTY,
    PRICE,
    LENGTH,
    WIDTH,
    HEIGHT,
    WEIGHT,
    PIECES,
    ITEMOPTION
FROM
  itemPricing
WHERE
  ITEMID = __ITEMID;
    
INSERT INTO
  itemsBranchesLink
    (ITEMID,BRANCHCODE)
SELECT
  @INSERTID AS ITEMID,
    BRANCH AS BRANCHCODE
FROM
  user
WHERE
  USERNAME = __USERNAME;
    
INSERT INTO
  itemsSubcategoryLink
    (ITEMID,SUBCATID)
SELECT
  @INSERTID AS ITEMID,
    SUBCATID
FROM
  itemsSubcategoryLink
WHERE
  ITEMID = __ITEMID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ManualChangeOrderShipToAddress` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ManualChangeOrderShipToAddress`(__USERNAME VARCHAR(50), __NAME VARCHAR(100), __STOREID VARCHAR(20), __ADDRESS VARCHAR(1000), __ADDRESS2 VARCHAR(1000), __CITY VARCHAR(100), __PROVINCE VARCHAR(100), __COUNTRY VARCHAR(100), __POSTAL VARCHAR(20), __PHONE VARCHAR(20), __FAX VARCHAR(20))
BEGIN


SET SQL_SAFE_UPDATES = 0;


SET @TGROUPCHECK = (SELECT COUNT(*) FROM taxes WHERE TAXCODE = __PROVINCE);

IF @TGROUPCHECK <= 0 THEN
	SIGNAL SQLSTATE '45000'
	SET MESSAGE_TEXT = 'No Tax group matching province';	
ELSE 
	UPDATE
		orderHeader
	SET
		SHIPNAME = __NAME,
        SHIPSTOREID = __STOREID,
		SHIPADDRESS = __ADDRESS,
        SHIPADDRESS2 = __ADDRESS2,
		SHIPCITY = __CITY,
		SHIPPROVINCE = __PROVINCE,
		SHIPCOUNTRY = __COUNTRY,
		SHIPPOSTAL = __POSTAL,
		SHIPCONTACTPHONE = __PHONE,
		SHIPCONTACTFAX = __FAX,
		SHIPTAXGROUP = __PROVINCE
	WHERE 
		USERNAME  = __USERNAME AND 
		ORDERSTATUS = 'CART';
END IF;


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ModifyAdItem` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ModifyAdItem`(__ID INT, __NAME VARCHAR(1000), __STARTDATE DATE, __ENDDATE DATE, __LANGUAGE VARCHAR(2), __SORTORDER INT, __URL VARCHAR(500), __ADPATH VARCHAR(200))
BEGIN

	UPDATE 
		AdItems
	SET
		NAME = __NAME,
        STARTDATE = __STARTDATE,
        ENDDATE = __ENDDATE,
        LANGUAGE = __LANGUAGE,
        SORTORDER = __SORTORDER,
        IMAGEPATH = CASE WHEN __ADPATH = 'NOUPDATE' THEN IMAGEPATH ELSE __ADPATH END,
        URL = __URL
	WHERE
		ID = __ID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ModifyBranchAddress` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ModifyBranchAddress`(__CODE VARCHAR(20), __LANG VARCHAR(2), __ISMAIN TINYINT(1), __ADDRESS VARCHAR(1000), __ADDRESS2 VARCHAR(1000), __CITY VARCHAR(100), __PROVINCE VARCHAR(100), __COUNTRY VARCHAR(100), __POSTALCODE VARCHAR(20), __PHONE VARCHAR(20), __FAX VARCHAR(20))
BEGIN

	SET @FOUNDMATCH := 0;
    SELECT @FOUNDMATCH := COUNT(*) FROM branchesAddresses WHERE CODE = __CODE AND LANGUAGE = __LANG;

	IF @FOUNDMATCH > 0 THEN
		UPDATE
			branchesAddresses
		SET
			ISMAIN = __ISMAIN, 
			ADDRESS = __ADDRESS, 
            ADDRESS2 = __ADDRESS2,
			CITY = __CITY, 
			PROVINCE = __PROVINCE, 
			COUNTRY = __COUNTRY, 
			POSTALCODE = __POSTALCODE, 
			PHONE = __PHONE, 
			FAX = __FAX
		WHERE
			CODE = __CODE AND
			LANGUAGE = __LANG;   
		UPDATE 
			orderHeader 
		SET 
			SHIPCONTACTPHONE = __PHONE, 
			SHIPCONTACTFAX = __FAX, 
			SHIPADDRESS = __ADDRESS, 
			SHIPADDRESS2 = __ADDRESS2, 
			SHIPCITY = __CITY, 
			SHIPPROVINCE = __PROVINCE, 
			SHIPCOUNTRY = __COUNTRY, 
			SHIPPOSTAL = __POSTALCODE, 
			REQCONTACTPHONE = __PHONE, 
			REQCONTACTFAX = __FAX, 
			REQADDRESS = __ADDRESS, 
			REQADDRESS2 = __ADDRESS2, 
			REQCITY = __CITY, 
			REQPROVINCE = __PROVINCE, 
			REQCOUNTRY = __COUNTRY, 
			REQPOSTAL = __POSTALCODE
		WHERE 
			USERNAME IN (SELECT USERNAME FROM user WHERE branch = __CODE) AND 
			ORDERSTATUS = 'CART';
    ELSE
		INSERT INTO
			branchesAddresses
			(CODE, LANGUAGE, ISMAIN, ADDRESS, ADDRESS2, CITY, PROVINCE, COUNTRY, POSTALCODE, PHONE, FAX)
		VALUES
			(__CODE, __LANG, __ISMAIN, __ADDRESS, __ADDRESS2, __CITY, __PROVINCE, __COUNTRY, __POSTALCODE, __PHONE, __FAX);
	END IF;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ModifyBranchProfile` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ModifyBranchProfile`(__CODE VARCHAR(20), __NAME VARCHAR(100), __MGREMAIL VARCHAR(100), __TREEMAIL VARCHAR(100), __TAXGROUP VARCHAR(6), __SKIN VARCHAR(20), __STATUS INTEGER, __COMMENTS VARCHAR(1000), __SHIPZONE VARCHAR(20))
BEGIN

UPDATE
	branches
SET
	CODE = __CODE,
    NAME = __NAME,
    MANAGEMENTEMAIL = __MGREMAIL,
    TREASUREREMAIL = __TREEMAIL,
    TAXGROUP = __TAXGROUP,
    SKIN = __SKIN,
    STATUS = __STATUS,
    COMMENTS = __COMMENTS,
    SHIPZONE = __SHIPZONE,
    MODIFIEDDATE = CURRENT_TIMESTAMP
WHERE
	CODE = __CODE;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ModifyCategory` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ModifyCategory`(__CATID INTEGER, __NAME VARCHAR(50), __DESCRIPTION VARCHAR(200), __LANGUAGE VARCHAR(2), __ICON VARCHAR(200), __USERLEVEL INTEGER, __COMMENTS VARCHAR(1000))
BEGIN
	UPDATE
		categories
	SET
		NAME = __NAME,
        DESCRIPTION = __DESCRIPTION,
        ICONURL = __ICON,
        LANGUAGE = __LANGUAGE,
        USERLEVEL = __USERLEVEL,
        COMMENTS  = __COMMENTS,
        MODIFIEDDATE = CURRENT_TIMESTAMP
	WHERE
		CATID = __CATID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ModifyItem` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `ModifyItem`(IN `__ITEMID` INT, IN `__TYPE` INT, IN `__CODE` VARCHAR(50), IN `__DESCRIPTION` VARCHAR(200), IN `__PRODUCEDETAIL` VARCHAR(1000), IN `__SIZE` VARCHAR(50), IN `__NUMPAGES` VARCHAR(50), IN `__PRINTINGTYPE` VARCHAR(50), IN `__STOCK` VARCHAR(50), IN `__STATUS` INT, IN `__MINLEVEL` INT, IN `__LEADTIME` INT, IN `__DISPLAYNAME` VARCHAR(1000), IN `__PROOFNAME` VARCHAR(1000), IN `__PARAMETERSNAME` VARCHAR(1000), IN `__UPLOADDIR` VARCHAR(1000), IN `__PREVIEWNAME` VARCHAR(1000), IN `__SHIPBOXINSLEEVE` INT, IN `__SKIPPDFPERMISSIONCHECK` INT, IN `__DLSUGGESTIONS` VARCHAR(1000))
BEGIN
        UPDATE
                items
        SET
                TYPE = __TYPE,
        CODE = __CODE,
        DESCRIPTION = __DESCRIPTION,
        PRODUCEDETAIL = __PRODUCEDETAIL,
        SIZE = __SIZE,
        NUMPAGES = __NUMPAGES, 
        STATUS = __STATUS,
        MINLEVEL = __MINLEVEL,
        LEADTIME = __LEADTIME,
        DISPLAYIMAGENAME = __DISPLAYNAME,
        PROOFPDFNAME = __PROOFNAME,
        PREVIEWIMAGENAME = __PREVIEWNAME,
        PARAMETERSFILENAME = __PARAMETERSNAME,
        DISPLAYIMAGEPATH = CASE WHEN LENGTH(__DISPLAYNAME) > 0 THEN CONCAT(__UPLOADDIR,__DISPLAYNAME) ELSE '' END,
        PROOFPDFPATH = CASE WHEN LENGTH(__PROOFNAME) > 0 THEN CONCAT(__UPLOADDIR,__PROOFNAME) ELSE '' END,
        PARAMETERSFILEPATH = CASE WHEN LENGTH(__PARAMETERSNAME) > 0 THEN CONCAT(__UPLOADDIR,__PARAMETERSNAME) ELSE '' END,
        PREVIEWIMAGEPATH = CASE WHEN LENGTH(__PREVIEWNAME) > 0 THEN CONCAT(__UPLOADDIR,__PREVIEWNAME) ELSE '' END,
        SHIPBOXINSLEEVE = __SHIPBOXINSLEEVE,
        SKIPPDFPERMISSIONCHECK = __SKIPPDFPERMISSIONCHECK,
        DLSUGGESTIONS = __DLSUGGESTIONS,
        STOCK=__STOCK,
        PRINTINGTYPE=__PRINTINGTYPE
        WHERE
                ID = __ITEMID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ModifyNewsItem` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ModifyNewsItem`(__ID INTEGER, __STARTDATE DATE, __ENDDATE DATE, __LANGUAGE VARCHAR(2), __TITLE VARCHAR(1000), __MESSAGE VARCHAR(1000))
BEGIN
	UPDATE
		newsItems
	SET
		STARTDATE = __STARTDATE,
        ENDDATE = __ENDDATE,
        LANGUAGE = __LANGUAGE,
        TITLE = __TITLE,
        MESSAGE = __MESSAGE
	WHERE
		ID = __ID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ModifyPromo` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `ModifyPromo`(__NAME VARCHAR(100), __CODE VARCHAR(50), __CODETYPE VARCHAR(12), __PROMOVALUE DECIMAL(19,2), __QTYTYPE VARCHAR(12), __MAXQTY INT, __MINQTY INT, __EXACTQTY INT, __STARTDATE DATE, __ENDDATE DATE, __SINGLEUSE INT, __REDEEMED INT, __SINGLEITEM INT)
BEGIN

 UPDATE 
 promos
 SET
 NAME = __NAME,
 CODETYPE = __CODETYPE,
 PROMOVALUE = __PROMOVALUE,
 QTYTYPE = __QTYTYPE,
 MAXQTY = __MAXQTY,
 MINQTY = __MINQTY,
 EXACTQTY = __EXACTQTY,
 STARTDATE = __STARTDATE,
 ENDDATE = __ENDDATE,
 SINGLEUSE = __SINGLEUSE,
 REDEEMED = __REDEEMED,
 SINGLEITEM = __SINGLEITEM
 WHERE
 CODE = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ModifyShipToAddress` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ModifyShipToAddress`(__ID INT, __NAME VARCHAR(100), __STOREID VARCHAR(20), __ADDRESS VARCHAR(1000), __ADDRESS2 VARCHAR(1000), __CITY VARCHAR(100),
__PROVINCE VARCHAR(100), __COUNTRY VARCHAR(100), __POSTAL VARCHAR(20), __PHONE VARCHAR(20), __FAX VARCHAR(20),
__TAXGROUP VARCHAR(6), __SHIPZONE VARCHAR(6))
BEGIN

UPDATE
	shipToAddresses
SET
	NAME = __NAME,
    STOREID = __STOREID,
    ADDRESS = __ADDRESS,
    ADDRESS2 = __ADDRESS2,
    CITY = __CITY,
    PROVINCE = __PROVINCE,
    COUNTRY = __COUNTRY,
    POSTAL = __POSTAL,
    PHONE = __PHONE,
    FAX = __FAX,
    TAXGROUP = __TAXGROUP,
    SHIPZONE = __SHIPZONE
WHERE
	ID = __ID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ModifySubCategory` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `ModifySubCategory`(__SUBCATID INTEGER, __CATID INTEGER, __NAME VARCHAR(50), __DESCRIPTION VARCHAR(200), __ICONURL VARCHAR(200), __USERLEVEL INT)
BEGIN
    UPDATE
        subcategories
    SET
        NAME = __NAME,
        DESCRIPTION = __DESCRIPTION,
        ICONURL = __ICONURL,
        USERLEVEL = __USERLEVEL
    WHERE
        SUBCATID = __SUBCATID AND
        CATID = __CATID ;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ModifyUserProfile` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ModifyUserProfile`(IN `UNAME` VARCHAR(50), IN `__BRANCH` VARCHAR(20), IN `PASS` VARCHAR(50), IN `FNAME` VARCHAR(50), IN `LNAME` VARCHAR(50), IN `LANG` VARCHAR(2), IN `STAT` INT, IN `USERLVL` INT, IN `COMMENTS` VARCHAR(1000), IN `EMAIL` VARCHAR(100))
BEGIN

UPDATE
	`user`
SET
	`BRANCH` = __BRANCH, 
	`PASSWORD` = SHA2(PASS, 256),
    `PLAINPASSWORD` = PASS,
    `FIRSTNAME` = FNAME,
    `LASTNAME` = LNAME,
    `LANGUAGE` = LANG,
    `STATUS` = STAT,
    `USERLEVEL` = USERLVL,
    `COMMENTS` = COMMENTS,
    `EMAIL` = EMAIL,
    `MODIFIEDDATE` = CURRENT_TIMESTAMP
WHERE
	`USERNAME` = UNAME;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RejectItem` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `RejectItem`(__CODE VARCHAR(200))
BEGIN
	SELECT
		ITEM.ID AS ITEMID,
        USERS.EMAIL AS USEREMAIL,
        ITEM.DISPLAYIMAGEPATH
	FROM
		items ITEM 
	INNER JOIN
		user USERS ON 
			USERS.USERNAME = ITEM.GENERATEDBY
	WHERE 
		APPROVALLINK = __CODE;

	UPDATE
		items
	SET
		STATUS = 0,
		ARCHIVED = 1,
		ASSETAPPROVED = 0,
        APPROVALLINK = NULL
	WHERE
		APPROVALLINK = __CODE;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RejectOrder` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `RejectOrder`(__APPROVALLINK NVARCHAR(100))
BEGIN


SELECT
	HDR.*,
    DTL.*,
    ITEM.TYPE,
    USER.FIRSTNAME AS USER_FIRSTNAME,
    USER.LASTNAME AS USER_LASTNAME,
    USER.EMAIL AS USER_EMAIL,
    USER.LANGUAGE AS USER_LANGUAGE,
    BRANCH.NAME AS BRANCH_NAME,
    BRANCH.MANAGEMENTEMAIL AS BRANCH_MANAGEMENTEMAIL,
    COALESCE(BRANCHADDRESS.LANGUAGE,'EN') AS BRANCH_LANGUAGE,
    'CANCELLED' AS REALSTATUS
FROM
	orderHeader HDR
INNER JOIN
	orderDetail DTL ON 
		HDR.ORDERID = DTL.ORDERID
INNER JOIN
	items ITEM ON
		DTL.ITEMID = ITEM.ID
INNER JOIN
	user USER ON 
		HDR.USERNAME = USER.USERNAME
INNER JOIN
	branches BRANCH ON 
		USER.BRANCH = BRANCH.CODE
LEFT OUTER JOIN
	(SELECT * FROM branchesAddresses WHERE ISMAIN = 1) BRANCHADDRESS ON 
		BRANCH.CODE = BRANCHADDRESS.CODE
WHERE
	HDR.APPROVALLINK  COLLATE utf8_unicode_ci = __APPROVALLINK AND
    HDR.APPROVALLINK IS NOT NULL;
    
UPDATE
	orderHeader
SET
	APPROVALLINK = NULL,
    ORDERSTATUS = 'REJECTED',
    ORDERMODIFIED = CURRENT_TIMESTAMP
WHERE
	APPROVALLINK  COLLATE utf8_unicode_ci = __APPROVALLINK AND
    APPROVALLINK IS NOT NULL;	

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RemoveItemFromCart` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `RemoveItemFromCart`(__USERNAME NVARCHAR(20), __ITEMLINE INT)
BEGIN
	
    DELETE
    FROM
		orderDetail
	WHERE
		ITEMLINE = __ITEMLINE AND
        ORDERID IN (SELECT ORDERID FROM orderHeader WHERE USERNAME COLLATE utf8_unicode_ci = __USERNAME AND ORDERSTATUS = 'CART')
	;
    
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RemoveOldCartOrders` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `RemoveOldCartOrders`()
BEGIN

DELETE FROM orderDetail WHERE ORDERID IN (
SELECT ORDERID FROM orderHeader WHERE ORDERSTATUS = 'CART' AND ORDERMODIFIED < DATE_ADD(CURRENT_TIMESTAMP,INTERVAL -24 HOUR)
);

DELETE FROM orderHeader WHERE ORDERSTATUS = 'CART' AND ORDERMODIFIED < DATE_ADD(CURRENT_TIMESTAMP,INTERVAL -24 HOUR);

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `RemovePromoFromCart` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `RemovePromoFromCart`(__USERNAME VARCHAR(20), __PROMO VARCHAR(50))
BEGIN
	
    DELETE
    FROM
		orderPromos
	WHERE
		PROMOCODE = __PROMO AND
        ORDERID IN (SELECT ORDERID FROM orderHeader WHERE USERNAME COLLATE utf8_unicode_ci = __USERNAME AND ORDERSTATUS = 'CART')
	;

    UPDATE
		promos
	SET
		REDEEMED = 0
	WHERE
		CODE = __PROMO AND
        SINGLEUSE = 1;
    
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ReportOrderBranches` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `ReportOrderBranches`(IN `__STARTDATE` DATE, IN `__ENDDATE` DATE, IN `__ACCESS` BIT, IN `__USERNAME` VARCHAR(50))
BEGIN

	SELECT
		HDR.ORDERID,
        HDR.ORDERREF,
        HDR.PAYMENTREF,
        HDR.ORDERSTATUS,
        HDR.PONUMBER,
        HDR.BILLINGCATEGORY,
        HDR.SPECIALINSTRUCTIONS,
        CASE WHEN USER.USERNAME IS NOT NULL THEN CONCAT(USER.FIRSTNAME,' ',USER.LASTNAME) ELSE 'Unknown User' END AS ORDERBY,
        DATE(ORDERDATE) AS ORDERDATE,
        DATE(APPROVALDATE) AS APPROVALDATE,
        HDR.SHIPNAME,
        CONCAT(HDR.SHIPCONTACTFIRSTNAME,' ',HDR.SHIPCONTACTLASTNAME) AS SHIPCONTACT,
        HDR.SHIPCONTACTEMAIL,
        HDR.SHIPADDRESS,
        HDR.SHIPCITY,
        HDR.SHIPPROVINCE,
        HDR.SHIPPOSTAL,
        HDR.SHIPPINGCOST,
        COALESCE(DTLSUM.EXTPRICE,0) AS EXTPRICE,
        COALESCE(DTLSUM.TAXAMT,0) + HDR.SHIPPINGCOSTTAXAMT1 + HDR.SHIPPINGCOSTTAXAMT2 + HDR.SHIPPINGCOSTTAXAMT3 + HDR.SHIPPINGCOSTTAXAMT4 + HDR.SHIPPINGCOSTTAXAMT5 AS TAXAMT,
        COALESCE(DTLSUM.EXTPRICE,0) + COALESCE(DTLSUM.TAXAMT,0) + HDR.SHIPPINGCOST + HDR.SHIPPINGCOSTTAXAMT1 + HDR.SHIPPINGCOSTTAXAMT2 + HDR.SHIPPINGCOSTTAXAMT3 + HDR.SHIPPINGCOSTTAXAMT4 + HDR.SHIPPINGCOSTTAXAMT5 AS GRANDTOTAL,
        COALESCE(SHDR.TRACKNUM,'No Tracking #') AS TRACKNUM,
        COALESCE(DATE(SHDR.SHIPDATE),'') AS SHIPDATE,
        DTL.ITEMID,
        COALESCE(DTL.ITEMCODE,'Unknown Item') AS ITEMCODE,
        COALESCE(DTL.ITEMDESC,'Unknown Item') AS ITEMDESC,
        COALESCE(DTL.ORDERQTY,0) AS ORDERQTY,
        COALESCE(DTL.SHIPTODATEQTY,0) AS SHIPTODATEQTY,
        COALESCE(DTL.EXTPRICE,0) AS ITEMEXTPRICE,
        COALESCE(DTL.TAXAMT1 + DTL.TAXAMT2 + DTL.TAXAMT3 + DTL.TAXAMT4 + DTL.TAXAMT5,0) AS ITEMTAXES,
        COALESCE(DTL.EXTPRICE + DTL.TAXAMT1 + DTL.TAXAMT2 + DTL.TAXAMT3 + DTL.TAXAMT4 + DTL.TAXAMT5,0) AS ITEMTOTAL,
        BRANCH.NAME AS BRANCHNAME,
        BRANCH.CODE AS BRANCHCODE,
        COALESCE(BRANCH.SKIN,'NA') AS SKINBRAND
	FROM
		orderHeader HDR
	LEFT OUTER JOIN
		( SELECT ORDERID,SUM(EXTPRICE) AS EXTPRICE, SUM(TAXAMT1+TAXAMT2+TAXAMT3+TAXAMT4+TAXAMT5) AS TAXAMT FROM orderDetail GROUP BY ORDERID) DTLSUM ON 
			HDR.ORDERID = DTLSUM.ORDERID
	LEFT OUTER JOIN
		orderDetail DTL ON 
			HDR.ORDERID = DTL.ORDERID
	LEFT OUTER JOIN
		( SELECT ORDERID,MAX(TRACKNUM) AS TRACKNUM, MAX(SHIPDATE) AS SHIPDATE FROM shipmentHeader GROUP BY ORDERID ) SHDR ON 
			HDR.ORDERID = SHDR.ORDERID
	LEFT OUTER JOIN
		user USER ON 
			HDR.USERNAME = USER.USERNAME
	LEFT OUTER JOIN
		branches BRANCH ON 
			USER.BRANCH = BRANCH.CODE
	WHERE
		HDR.ORDERDATE BETWEEN __STARTDATE AND DATE_ADD(__ENDDATE,INTERVAL 1 DAY) AND
		HDR.ORDERSTATUS NOT IN ('WAITMA','CART') AND
        (__ACCESS = 1 OR USER.BRANCH IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME))
	ORDER BY
		COALESCE(BRANCH.SKIN,'NA'),
		BRANCH.NAME,
		ORDERDATE,
        HDR.ORDERID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ReportOrderDetailed` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `ReportOrderDetailed`(IN `__STARTDATE` DATE, IN `__ENDDATE` DATE, IN `__ACCESS` BIT, IN `__USERNAME` VARCHAR(50))
BEGIN

	SELECT
		HDR.ORDERID,
        HDR.ORDERREF,
        HDR.ORDERSTATUS,
        HDR.PAYMENTREF,
		HDR.PONUMBER,
        HDR.BILLINGCATEGORY,
        CASE WHEN USER.USERNAME IS NOT NULL THEN CONCAT(USER.FIRSTNAME,' ',USER.LASTNAME) ELSE 'Unknown User' END AS ORDERBY,
        DATE(ORDERDATE) AS ORDERDATE,
        HDR.SHIPNAME,
        CONCAT(HDR.SHIPCONTACTFIRSTNAME,' ',HDR.SHIPCONTACTLASTNAME) AS SHIPCONTACT,
        HDR.SHIPCONTACTEMAIL,
        HDR.SHIPADDRESS,
        HDR.SHIPCITY,
        HDR.SHIPPROVINCE,
        HDR.SHIPPOSTAL,
        HDR.SHIPPINGCOST,
        COALESCE(DTLSUM.EXTPRICE,0) AS EXTPRICE,
        COALESCE(DTLSUM.TAXAMT,0) + HDR.SHIPPINGCOSTTAXAMT1 + HDR.SHIPPINGCOSTTAXAMT2 + HDR.SHIPPINGCOSTTAXAMT3 + HDR.SHIPPINGCOSTTAXAMT4 + HDR.SHIPPINGCOSTTAXAMT5 AS TAXAMT,
        COALESCE(DTLSUM.EXTPRICE,0) + COALESCE(DTLSUM.TAXAMT,0) + HDR.SHIPPINGCOST + HDR.SHIPPINGCOSTTAXAMT1 + HDR.SHIPPINGCOSTTAXAMT2 + HDR.SHIPPINGCOSTTAXAMT3 + HDR.SHIPPINGCOSTTAXAMT4 + HDR.SHIPPINGCOSTTAXAMT5 AS GRANDTOTAL,
        COALESCE(SHDR.TRACKNUM,'No Tracking #') AS TRACKNUM,
        COALESCE(DATE(SHDR.SHIPDATE),'') AS SHIPDATE,
        DTL.ITEMID,
        COALESCE(DTL.ITEMCODE,'Unknown Item') AS ITEMCODE,
        COALESCE(DTL.ITEMDESC,'Unknown Item') AS ITEMDESC,
        COALESCE(DTL.ORDERQTY,0) AS ORDERQTY,
        COALESCE(DTL.SHIPTODATEQTY,0) AS SHIPTODATEQTY,
        COALESCE(DTL.EXTPRICE,0) AS ITEMEXTPRICE,
        COALESCE(DTL.TAXAMT1 + DTL.TAXAMT2 + DTL.TAXAMT3 + DTL.TAXAMT4 + DTL.TAXAMT5,0) AS ITEMTAXES,
        COALESCE(DTL.EXTPRICE + DTL.TAXAMT1 + DTL.TAXAMT2 + DTL.TAXAMT3 + DTL.TAXAMT4 + DTL.TAXAMT5,0) AS ITEMTOTAL,
        COALESCE(BRANCH.SKIN,'N/A') AS SKINBRAND,
	   BRANCH.CODE AS BRANCHCODE
	FROM
		orderHeader HDR
	LEFT OUTER JOIN
		( SELECT ORDERID,SUM(EXTPRICE) AS EXTPRICE, SUM(TAXAMT1+TAXAMT2+TAXAMT3+TAXAMT4+TAXAMT5) AS TAXAMT FROM orderDetail GROUP BY ORDERID) DTLSUM ON 
			HDR.ORDERID = DTLSUM.ORDERID
	LEFT OUTER JOIN
		orderDetail DTL ON 
			HDR.ORDERID = DTL.ORDERID
	LEFT OUTER JOIN
		( SELECT ORDERID,MAX(TRACKNUM) AS TRACKNUM, MAX(SHIPDATE) AS SHIPDATE FROM shipmentHeader GROUP BY ORDERID ) SHDR ON 
			HDR.ORDERID = SHDR.ORDERID
	LEFT OUTER JOIN
		user USER ON 
			HDR.USERNAME = USER.USERNAME
	LEFT OUTER JOIN
		branches BRANCH ON 
			USER.BRANCH = BRANCH.CODE            
	WHERE
		HDR.ORDERDATE BETWEEN __STARTDATE AND DATE_ADD(__ENDDATE,INTERVAL 1 DAY) AND
		HDR.ORDERSTATUS NOT IN ('WAITMA','CART') AND
        (__ACCESS = 1 OR USER.BRANCH IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME))
ORDER BY
        HDR.ORDERID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ReportOrderSummary` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ReportOrderSummary`(__STARTDATE DATE, __ENDDATE DATE, __ACCESS BIT, __USERNAME VARCHAR(50))
BEGIN

	SELECT
		HDR.ORDERID,
        HDR.ORDERREF,
        HDR.PAYMENTREF,
        HDR.ORDERSTATUS,
        CASE WHEN USER.USERNAME IS NOT NULL THEN CONCAT(USER.FIRSTNAME,' ',USER.LASTNAME) ELSE 'Unknown User' END AS ORDERBY,
        DATE(ORDERDATE) AS ORDERDATE,
        HDR.SHIPNAME,
        CONCAT(HDR.SHIPCONTACTFIRSTNAME,' ',HDR.SHIPCONTACTLASTNAME) AS SHIPCONTACT,
        HDR.SHIPADDRESS,
        HDR.SHIPCITY,
        HDR.SHIPPROVINCE,
        HDR.SHIPPOSTAL,
        HDR.SHIPPINGCOST,
        HDR.SHIPSTOREID,
        COALESCE(DTL.EXTPRICE,0) AS EXTPRICE,
        COALESCE(DTL.TAXAMT,0) + HDR.SHIPPINGCOSTTAXAMT1 + HDR.SHIPPINGCOSTTAXAMT2 + HDR.SHIPPINGCOSTTAXAMT3 + HDR.SHIPPINGCOSTTAXAMT4 + HDR.SHIPPINGCOSTTAXAMT5 AS TAXAMT,
        COALESCE(DTL.EXTPRICE,0) + COALESCE(DTL.TAXAMT,0) + HDR.SHIPPINGCOST + HDR.SHIPPINGCOSTTAXAMT1 + HDR.SHIPPINGCOSTTAXAMT2 + HDR.SHIPPINGCOSTTAXAMT3 + HDR.SHIPPINGCOSTTAXAMT4 + HDR.SHIPPINGCOSTTAXAMT5 AS GRANDTOTAL,
        COALESCE(SHDR.TRACKNUM,'No Tracking #') AS TRACKNUM,
        COALESCE(DATE(SHDR.SHIPDATE),'') AS SHIPDATE,
        COALESCE(BRANCH.SKIN,'N/A') AS SKINBRAND,
        BRANCH.CODE AS BRANCHCODE
	FROM
		orderHeader HDR
	LEFT OUTER JOIN
		( SELECT ORDERID,SUM(EXTPRICE) AS EXTPRICE, SUM(TAXAMT1+TAXAMT2+TAXAMT3+TAXAMT4+TAXAMT5) AS TAXAMT FROM orderDetail GROUP BY ORDERID) DTL ON 
			HDR.ORDERID = DTL.ORDERID
	LEFT OUTER JOIN
		( SELECT ORDERID,MAX(TRACKNUM) AS TRACKNUM, MAX(SHIPDATE) AS SHIPDATE FROM shipmentHeader GROUP BY ORDERID ) SHDR ON 
			HDR.ORDERID = SHDR.ORDERID
	LEFT OUTER JOIN
		user USER ON 
			HDR.USERNAME = USER.USERNAME
	LEFT OUTER JOIN
		branches BRANCH ON 
			USER.BRANCH = BRANCH.CODE
	WHERE
		HDR.ORDERDATE BETWEEN __STARTDATE AND DATE_ADD(__ENDDATE,INTERVAL 1 DAY) AND
		HDR.ORDERSTATUS NOT IN ('WAITMA','CART') AND
        (__ACCESS = 1 OR USER.BRANCH IN (SELECT BRANCH FROM user WHERE USERNAME = __USERNAME));

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ResendAssetEmail` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ResendAssetEmail`(__ITEMID INT)
BEGIN

SELECT
	ITEMS.ID AS ITEMID,
    ITEMS.APPROVALLINK,
    BRANCHES.MANAGEMENTEMAIL AS BEMAIL,
    ITEMS.DISPLAYIMAGEPATH
FROM
	items ITEMS
INNER JOIN
	user USER ON 
		ITEMS.GENERATEDBY = USER.USERNAME
INNER JOIN
	branches BRANCHES ON 
		USER.BRANCH = BRANCHES.CODE
WHERE
	ITEMS.ID = __ITEMID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `SaveDiscountCalc` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `SaveDiscountCalc`(__ORDERID INT, __PROMO VARCHAR(50), __CALCAMT DECIMAL(19,2), __TAXAMT1 DECIMAL(19,2), __TAXAMT2 DECIMAL(19,2), __TAXAMT3 DECIMAL(19,2), __TAXAMT4 DECIMAL(19,2), __TAXAMT5 DECIMAL(19,2))
BEGIN

	UPDATE 
		orderPromos
	SET
		CALCDISCOUNT = __CALCAMT,
        TAXAMT1 = __TAXAMT1,
        TAXAMT2 = __TAXAMT2,
        TAXAMT3 = __TAXAMT3,
        TAXAMT4 = __TAXAMT4,
        TAXAMT5 = __TAXAMT5
	WHERE
		ORDERID = __ORDERID AND
        PROMOCODE = __PROMO;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `SearchForBranches` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `SearchForBranches`(SEARCH VARCHAR(1000), SEARCHBY INTEGER)
BEGIN
	SELECT CODE,NAME FROM branches WHERE
    (
 		(CODE LIKE CONCAT('%',SEARCH,'%') AND SEARCHBY IN (0,1)) OR
        (NAME LIKE CONCAT('%',SEARCH,'%') AND SEARCHBY IN (0,2)) 
    );
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `SearchForCategories` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `SearchForCategories`(SEARCH VARCHAR(1000), SEARCHBY INTEGER)
BEGIN
	SELECT
		CATID,NAME,DESCRIPTION,LANGUAGE
	FROM
		categories
	WHERE
    (
		(NAME LIKE CONCAT('%',SEARCH,'%') AND SEARCHBY IN (0,1)) OR
        (DESCRIPTION LIKE CONCAT('%',SEARCH,'%') AND SEARCHBY IN (0,2)) OR
        (LANGUAGE LIKE CONCAT('%',SEARCH,'%') AND SEARCHBY IN (0,3))
    );
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `SearchForItems` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `SearchForItems`(__SEARCH VARCHAR(1000), __SEARCHBY INTEGER)
BEGIN
	SELECT DISTINCT
		ITEMS.ID,
        ITEMS.CODE,
        ITEMS.DESCRIPTION,
        CONCAT(CAT.NAME,'-',SUBCAT.NAME) AS CATNAME,
        ITEMS.TYPE,
        COALESCE(STOCK.QTYONHAND,0) AS QTYONHAND
	FROM
		items ITEMS
	LEFT OUTER JOIN itemsSubcategoryLink CATLINK ON 
		ITEMS.ID = CATLINK.ITEMID
	LEFT OUTER JOIN subcategories SUBCAT ON 
		CATLINK.SUBCATID = SUBCAT.SUBCATID
	LEFT OUTER JOIN categories CAT ON 
		SUBCAT.CATID = CAT.CATID
	LEFT OUTER JOIN itemsInventory STOCK ON
		ITEMS.ID = STOCK.ITEMID
	WHERE
    (
		(ITEMS.ID LIKE CONCAT('%',__SEARCH,'%') AND __SEARCHBY IN (0,1)) OR
        (ITEMS.CODE LIKE CONCAT('%',__SEARCH,'%') AND __SEARCHBY IN (0,2)) OR
        (ITEMS.DESCRIPTION LIKE CONCAT('%',__SEARCH,'%') AND __SEARCHBY IN (0,3)) OR
        (CONCAT(CAT.NAME,'-',SUBCAT.NAME) LIKE CONCAT('%',__SEARCH,'%') AND __SEARCHBY IN (0,4))
    )
    ORDER BY ITEMS.ID, ITEMS.CODE
    ;    
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `SearchForPromos` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `SearchForPromos`(SEARCH VARCHAR(1000), SEARCHBY INTEGER)
BEGIN
	SELECT 
		CODE,
        NAME,
        CODETYPE,
        DATE_FORMAT(STARTDATE,'%Y-%m-%d')  AS STARTDATE,
        DATE_FORMAT(ENDDATE,'%Y-%m-%d') AS ENDDATE
	FROM promos 
    WHERE
    (
		(CODE LIKE CONCAT('%',SEARCH,'%') AND SEARCHBY IN (0,1)) OR
        (NAME LIKE CONCAT('%',SEARCH,'%') AND SEARCHBY IN (0,2))
    )
    ;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `SearchForShipOrders` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `SearchForShipOrders`(IN `__SEARCH` VARCHAR(1000), IN `__SEARCHBY` INT)
BEGIN
	SELECT
		HDR.ORDERID,
        HDR.ORDERREF,
        DATE(HDR.ORDERDATE) AS ORDERDATE,
        HDR.SHIPNAME,
        CONCAT(HDR.SHIPCONTACTFIRSTNAME, ' - ', HDR.SHIPADDRESS, ' ', HDR.SHIPCITY, ' ', HDR.SHIPPROVINCE) AS DESTINATION,
        DATE(HDR.EXPDATE) AS EXPDATE,
        HDR.ORDERSTATUS,
        COALESCE(HDR.PAYMENTREF, '') AS PAYMENTREF,
        COALESCE(SHP.TRACKNUM, '') AS TRACKNUM
	FROM
		orderHeader HDR
		LEFT JOIN shipmentHeader SHP ON SHP.ORDERID = HDR.ORDERID
	WHERE
		(
			CONCAT(HDR.SHIPCONTACTFIRSTNAME, ' - ', HDR.SHIPADDRESS, ' ', HDR.SHIPCITY, ' ', HDR.SHIPPROVINCE) LIKE CONCAT('%', __SEARCH, '%') OR
            HDR.SHIPNAME LIKE CONCAT('%', __SEARCH, '%') OR 
            HDR.ORDERREF LIKE CONCAT('%', __SEARCH, '%') OR 
            HDR.ORDERDATE LIKE CONCAT('%', __SEARCH, '%') OR 
            HDR.EXPDATE LIKE CONCAT('%', __SEARCH, '%')
		) 
        AND (
			(__SEARCHBY = 1 AND HDR.ORDERSTATUS = 'ACTIVE') OR 
            (__SEARCHBY = 2 AND HDR.ORDERSTATUS = 'ACTIVE' AND NOW() >= DATE(HDR.EXPDATE) - INTERVAL 1 DAY) OR 
            (__SEARCHBY = 3 AND HDR.ORDERSTATUS = 'ACTIVE' AND NOW() >= DATE(HDR.EXPDATE) - INTERVAL 2 DAY) OR
            (__SEARCHBY = 4 AND HDR.ORDERSTATUS = 'WAITMA') OR 
            (__SEARCHBY = 5 AND HDR.ORDERSTATUS = 'SHIPPED') OR
            (__SEARCHBY = 6 AND HDR.ORDERSTATUS = 'CANCELLED') OR 
            (__SEARCHBY = 7 AND HDR.ORDERSTATUS = 'REJECTED')
        )
	ORDER BY HDR.ORDERREF;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `SearchForShipToAddresses` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `SearchForShipToAddresses`(SEARCH VARCHAR(1000), SEARCHBY INTEGER)
BEGIN
	SELECT ID,NAME,ADDRESS,STOREID FROM shipToAddresses WHERE
    (
 		(NAME LIKE CONCAT('%',SEARCH,'%') AND SEARCHBY IN (0,1)) OR
        (ADDRESS LIKE CONCAT('%',SEARCH,'%') AND SEARCHBY IN (0,2)) OR 
        (STOREID LIKE CONCAT('%',SEARCH,'%') AND SEARCHBY IN (0,3))
    );
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `SearchForSubCategories` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `SearchForSubCategories`(__CATID INTEGER)
BEGIN
	SELECT * FROM subcategories WHERE CATID = __CATID ORDER BY NAME;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `SearchForUsers` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `SearchForUsers`(SEARCH VARCHAR(1000), SEARCHBY INTEGER, ACCESSLEVEL INTEGER)
BEGIN
    SELECT USERNAME,FIRSTNAME,LASTNAME FROM user WHERE
    (
        (USERNAME LIKE CONCAT('%',SEARCH,'%') AND SEARCHBY IN (0,1)) OR
        (FIRSTNAME LIKE CONCAT('%',SEARCH,'%') AND SEARCHBY IN (0,2)) OR
        (LASTNAME LIKE CONCAT('%',SEARCH,'%') AND SEARCHBY IN (0,3))
    ) AND USERLEVEL <= ACCESSLEVEL
    ;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `SubmitOrder` */;
DELIMITER ;;
CREATE DEFINER=`admin`@`localhost` PROCEDURE `SubmitOrder`(__USERNAME NVARCHAR(50), __FIRSTNAME NVARCHAR(200), __LASTNAME NVARCHAR(200), __EMAIL NVARCHAR(45), __SPECINSTR NVARCHAR(1000), __RECEIVERINSTR NVARCHAR(1000), __SHIPPINGMETHOD VARCHAR(45), __SHIPPINGCOST DECIMAL(19,4), __MGRAPPROVAL INT, __PAYMENTREF VARCHAR(100), __PONUMBER VARCHAR(100),  __BILLINGCATEGORY VARCHAR(100))
BEGIN


SET @ORDERREF = IFNULL((SELECT MAX(ORDERREF)+1 FROM orderHeader),10000);


UPDATE 
  orderHeader
SET
  SHIPCONTACTFIRSTNAME = __FIRSTNAME,
    SHIPCONTACTLASTNAME = __LASTNAME,
    SHIPCONTACTEMAIL = __EMAIL,
    SPECIALINSTRUCTIONS = __SPECINSTR,
    RECEIVERINSTRUCTIONS = __RECEIVERINSTR,
    SHIPPINGMETHOD = __SHIPPINGMETHOD,
    SHIPPINGCOST = __SHIPPINGCOST,
    SHIPPINGCOSTTAXAMT1 = ROUND(__SHIPPINGCOST * TAXRATE1,2),
    SHIPPINGCOSTTAXAMT2 = ROUND(__SHIPPINGCOST * TAXRATE2,2),
    SHIPPINGCOSTTAXAMT3 = ROUND(__SHIPPINGCOST * TAXRATE3,2),
    SHIPPINGCOSTTAXAMT4 = ROUND(__SHIPPINGCOST * TAXRATE4,2),
    SHIPPINGCOSTTAXAMT5 = ROUND(__SHIPPINGCOST * TAXRATE5,2),
    APPROVALLINK = CASE WHEN __MGRAPPROVAL = 0 THEN NULL ELSE CONCAT(USERNAME,ORDERID,MD5(MD5(NOW())+ORDERID)) END,
    ORDERREF = CASE WHEN __MGRAPPROVAL = 0 THEN @ORDERREF ELSE 0 END,
    PAYMENTREF = __PAYMENTREF,
    PONUMBER = __PONUMBER,
    BILLINGCATEGORY = __BILLINGCATEGORY


WHERE 
  USERNAME COLLATE utf8_unicode_ci = __USERNAME AND 
    ORDERSTATUS = 'CART';
    
UPDATE orderDetail
  SET 
  SHIPTODATEQTY = ORDERQTY,
  DOWNLOADLINK = CONCAT(ORDERID,'-',ITEMLINE,'-',MD5(MD5(MD5(NOW())+ORDERID)+ITEMLINE))
WHERE
  ORDERQTY = -2 AND
  ORDERID IN (SELECT ORDERID FROM orderHeader WHERE USERNAME COLLATE utf8_unicode_ci = __USERNAME AND ORDERSTATUS = 'CART');   
    
SELECT
  HDR.*,
    DTL.*,
    ITEM.TYPE,
    USER.FIRSTNAME AS USER_FIRSTNAME,
    USER.LASTNAME AS USER_LASTNAME,
    USER.EMAIL AS USER_EMAIL,
    USER.LANGUAGE AS USER_LANGUAGE,
    BRANCH.NAME AS BRANCH_NAME,
    BRANCH.MANAGEMENTEMAIL AS BRANCH_MANAGEMENTEMAIL,
    COALESCE(BRANCHADDRESS.LANGUAGE,'EN') AS BRANCH_LANGUAGE,
    CASE WHEN __MGRAPPROVAL = 0 THEN 'ACTIVE' ELSE 'WAITMA' END AS REALSTATUS,
    COALESCE(ITEM.DLSUGGESTIONS,'') AS DLSUGGESTIONS
FROM
  orderHeader HDR
INNER JOIN
  orderDetail DTL ON 
    HDR.ORDERID = DTL.ORDERID
INNER JOIN
  items ITEM ON 
    DTL.ITEMID = ITEM.ID
INNER JOIN
  user USER ON 
    HDR.USERNAME = USER.USERNAME
INNER JOIN
  branches BRANCH ON 
    USER.BRANCH = BRANCH.CODE
LEFT OUTER JOIN
  (SELECT * FROM branchesAddresses WHERE ISMAIN = 1) BRANCHADDRESS ON 
    BRANCH.CODE = BRANCHADDRESS.CODE
WHERE 
  HDR.USERNAME COLLATE utf8_unicode_ci = __USERNAME AND 
    ORDERSTATUS = 'CART';

IF __MGRAPPROVAL = 0 THEN

        
    SELECT @SHIPLINECOUNT := COUNT(*) FROM orderDetail 
    WHERE ORDERQTY <> -2 AND
        ORDERID IN (SELECT ORDERID FROM orderHeader WHERE USERNAME COLLATE utf8_unicode_ci = __USERNAME AND ORDERSTATUS = 'CART');  
        
  UPDATE 
    orderHeader
  SET
    ORDERSTATUS = CASE WHEN @SHIPLINECOUNT = 0 THEN 'SHIPPED' ELSE 'ACTIVE' END
  WHERE 
    USERNAME COLLATE utf8_unicode_ci = __USERNAME AND 
    ORDERSTATUS = 'CART';   
    
ELSE

  UPDATE 
    orderHeader
  SET
    ORDERSTATUS = CASE WHEN __MGRAPPROVAL = 0 THEN 'ACTIVE' ELSE 'WAITMA' END
  WHERE 
    USERNAME COLLATE utf8_unicode_ci = __USERNAME AND 
    ORDERSTATUS = 'CART'; 

END IF;



END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `UpdateBranchAsset` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `UpdateBranchAsset`(__BRANCHCODE VARCHAR(20), __TAGNAME VARCHAR(100), __TAGVALUE VARCHAR(1000))
BEGIN

	SET @ROWCNT = (SELECT COUNT(*) FROM branchesAssetData WHERE BRANCHCODE = __BRANCHCODE AND TAG = __TAGNAME);

	IF @ROWCNT = 0 THEN
		INSERT INTO
			branchesAssetData
            (BRANCHCODE,TAG,VALUE)
		VALUES
			(__BRANCHCODE,__TAGNAME,__TAGVALUE);
    ELSE
    
		UPDATE 
			branchesAssetData
		SET
			VALUE = __TAGVALUE
		WHERE
			BRANCHCODE = __BRANCHCODE AND
            TAG = __TAGNAME;
    END IF;


END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `UpdateItemDisplayImage` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `UpdateItemDisplayImage`(__ITEMID INT, __DISPLAYNAME VARCHAR(200), __DISPLAYPATH VARCHAR(200))
BEGIN
	UPDATE
		items
	SET
		DISPLAYIMAGENAME = __DISPLAYNAME,
        DISPLAYIMAGEPATH = __DISPLAYPATH
	WHERE
		ID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `UpdateItemParametersFile` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `UpdateItemParametersFile`(__ITEMID INT, __PARAMETERSFILE VARCHAR(200), __PARAMETERSPATH VARCHAR(200))
BEGIN
	UPDATE
		items
	SET
		PARAMETERSFILENAME = __PARAMETERSFILE,
        PARAMETERSFILEPATH = __PARAMETERSPATH
	WHERE
		ID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `UpdateItemPreviewImage` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `UpdateItemPreviewImage`(__ITEMID INT, __PREVIEWNAME VARCHAR(200), __PREVIEWPATH VARCHAR(200))
BEGIN
	UPDATE
		items
	SET
		PREVIEWIMAGENAME = __PREVIEWNAME,
        PREVIEWIMAGEPATH = __PREVIEWPATH
	WHERE
		ID = __ITEMID;

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `UpdateItemProofPDF` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `UpdateItemProofPDF`(__ITEMID INT, __PDFNAME VARCHAR(200), __PDFPATH VARCHAR(200))
BEGIN
	UPDATE
		items
	SET
		PROOFPDFNAME = __PDFNAME,
        PROOFPDFPATH = __PDFPATH
	WHERE
		ID = __ITEMID;
END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `UpdateOrderInformation` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `UpdateOrderInformation`(__USERNAME NVARCHAR(50), __FIRSTNAME NVARCHAR(200), __LASTNAME NVARCHAR(200), __EMAIL NVARCHAR(45), __SPECINSTR NVARCHAR(1000), __RECEIVERINSTR NVARCHAR(1000), __SHIPPINGMETHOD VARCHAR(45), __SHIPPINGCOST DECIMAL(19,4), __PONUMBER VARCHAR(100), __BILLINGCATEGORY VARCHAR(100))
BEGIN


UPDATE 
	orderHeader
SET
	SHIPCONTACTFIRSTNAME = __FIRSTNAME,
    SHIPCONTACTLASTNAME = __LASTNAME,
    SHIPCONTACTEMAIL = __EMAIL,
    SPECIALINSTRUCTIONS = __SPECINSTR,
    RECEIVERINSTRUCTIONS = __RECEIVERINSTR,
    SHIPPINGMETHOD = __SHIPPINGMETHOD,
    SHIPPINGCOST = __SHIPPINGCOST,
    SHIPPINGCOSTTAXAMT1 = __SHIPPINGCOST * TAXRATE1,
    SHIPPINGCOSTTAXAMT2 = __SHIPPINGCOST * TAXRATE2,
    SHIPPINGCOSTTAXAMT3 = __SHIPPINGCOST * TAXRATE3,
    SHIPPINGCOSTTAXAMT4 = __SHIPPINGCOST * TAXRATE4,
    SHIPPINGCOSTTAXAMT5 = __SHIPPINGCOST * TAXRATE5,
	PONUMBER = __PONUMBER,
    BILLINGCATEGORY = __BILLINGCATEGORY
WHERE 
	USERNAME COLLATE utf8_unicode_ci = __USERNAME AND 
    ORDERSTATUS = 'CART';

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `UserCancelOrder` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `UserCancelOrder`(__USERNAME VARCHAR(100), __ORDERID INT)
BEGIN
 UPDATE
 orderHeader
 SET
 APPROVALLINK = NULL,
 ORDERSTATUS = 'CANCELLED',
 ORDERMODIFIED = CURRENT_TIMESTAMP
 WHERE
 ORDERID = __ORDERID AND
 USERNAME = __USERNAME; 
 
 UPDATE 
 orderDetail
 SET
 ORDERQTY = 0,
 SHIPTODATEQTY = 0
 WHERE
 ORDERID = __ORDERID AND
 ORDERID IN (SELECT ORDERID FROM orderHeader WHERE USERNAME = __USERNAME);
END ;;
DELIMITER ;


