-- Table structure for table `orderHeader`
--

CREATE TABLE `orderHeader` (
  `ORDERID` int(10) NOT NULL AUTO_INCREMENT,
  `USERNAME` varchar(50) NOT NULL,
  `ORDERDATE` datetime NOT NULL,
  `ORDERMODIFIED` datetime NOT NULL,
  `PONUMBER` varchar(200) DEFAULT NULL,
  `SHIPCONTACTFIRSTNAME` varchar(200) DEFAULT NULL,
  `SHIPCONTACTLASTNAME` varchar(200) DEFAULT NULL,
  `SHIPCONTACTPHONE` varchar(45) DEFAULT NULL,
  `SHIPCONTACTFAX` varchar(45) DEFAULT NULL,
  `SHIPCONTACTEMAIL` varchar(45) DEFAULT NULL,
  `SHIPNAME` varchar(200) DEFAULT NULL,
  `SHIPADDRESS` varchar(200) DEFAULT NULL,
  `SHIPADDRESS2` varchar(200) DEFAULT NULL,
  `SHIPCITY` varchar(200) DEFAULT NULL,
  `SHIPPROVINCE` varchar(200) DEFAULT NULL,
  `SHIPCOUNTRY` varchar(200) DEFAULT NULL,
  `SHIPPOSTAL` varchar(200) DEFAULT NULL,
  `ORDERSTATUS` varchar(20) NOT NULL,
  `TAXNAME1` varchar(45) DEFAULT NULL,
  `TAXNAME2` varchar(45) DEFAULT NULL,
  `TAXNAME3` varchar(45) DEFAULT NULL,
  `TAXNAME4` varchar(45) DEFAULT NULL,
  `TAXNAME5` varchar(45) DEFAULT NULL,
  `TAXRATE1` decimal(19,4) DEFAULT NULL,
  `TAXRATE2` decimal(19,4) DEFAULT NULL,
  `TAXRATE3` decimal(19,4) DEFAULT NULL,
  `TAXRATE4` decimal(19,4) DEFAULT NULL,
  `TAXRATE5` decimal(19,4) DEFAULT NULL,
  `SHIPPINGCOST` decimal(19,4) NOT NULL DEFAULT 0.0000,
  `SPECIALINSTRUCTIONS` varchar(1000) DEFAULT NULL,
  `RECEIVERINSTRUCTIONS` varchar(1000) DEFAULT NULL,
  `APPROVALLINK` varchar(100) DEFAULT NULL,
  `EXPDATE` datetime DEFAULT NULL,
  `SHIPTAXGROUP` varchar(6) DEFAULT NULL,
  `ORDERREF` int(11) DEFAULT NULL,
  `PAYMENTREF` varchar(100) DEFAULT NULL,
  `SHIPSTOREID` varchar(20) NOT NULL,
  `SHIPPINGCOSTTAXAMT1` decimal(19,4) DEFAULT 0.0000,
  `SHIPPINGCOSTTAXAMT2` decimal(19,4) DEFAULT 0.0000,
  `SHIPPINGCOSTTAXAMT3` decimal(19,4) DEFAULT 0.0000,
  `SHIPPINGCOSTTAXAMT4` decimal(19,4) DEFAULT 0.0000,
  `SHIPPINGCOSTTAXAMT5` decimal(19,4) DEFAULT 0.0000,
  `SHIPPINGMETHOD` varchar(45) DEFAULT 'N/A',
  `REQCONTACTFIRSTNAME` varchar(200) DEFAULT NULL,
  `REQCONTACTLASTNAME` varchar(200) DEFAULT NULL,
  `REQCONTACTPHONE` varchar(45) DEFAULT NULL,
  `REQCONTACTFAX` varchar(45) DEFAULT NULL,
  `REQNAME` varchar(200) DEFAULT NULL,
  `REQADDRESS` varchar(200) DEFAULT NULL,
  `REQADDRESS2` varchar(200) DEFAULT NULL,
  `REQCITY` varchar(200) DEFAULT NULL,
  `REQPROVINCE` varchar(200) DEFAULT NULL,
  `REQCOUNTRY` varchar(200) DEFAULT NULL,
  `REQPOSTAL` varchar(200) DEFAULT NULL,
  `EXTRADATA1` varchar(100) DEFAULT NULL,
  `APPROVALDATE` datetime DEFAULT NULL,
  `BILLINGCATEGORY` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`ORDERID`)
) ENGINE=InnoDB AUTO_INCREMENT=140 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `orderHeader`
--

INSERT INTO `orderHeader` VALUES (28,'amico','2019-06-20 13:49:29','2019-06-20 13:49:29','','k','k','905.764.0800','','patrick@creativepath.com','Amico','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','CANCELLED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'20190620164412','',NULL,'2019-06-28 13:49:29','ON',10000,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(29,'amico','2019-06-21 14:09:08','2019-06-21 14:09:08','','K','M','905.764.0800','','kmatuska@amico.com','Amico','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','CANCELLED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'20190807133511','THIS IS A TEST - PLEASE DO NOT PRINT THIS \nJUST WANT TO SEE WHAT HAPPENS IF I SELECT ORDER ',NULL,'2019-06-29 14:09:08','ON',10001,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(31,'amico','2019-07-31 13:36:21','2019-07-31 13:36:21','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'For our President, Albert so cards need to be perfect\nFoil on first and last name - just to be done on his card\nFoil on Amico logo and vertical line (standard on all cards)','',NULL,'2019-08-08 13:36:21','ON',10002,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(32,'amico','2019-08-29 19:35:52','2019-08-29 19:35:52','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Bulk order\nPO# AC7004719','',NULL,'2019-09-06 19:35:52','ON',10003,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(33,'amico','2019-08-29 19:43:43','2019-08-29 19:43:43','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Bulk order \nPO# AC7004717','',NULL,'2019-09-06 19:43:43','ON',10004,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(34,'amico','2019-08-29 19:47:52','2019-08-29 19:47:52','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Bulk order\nPO# AIC0034330','',NULL,'2019-09-06 19:47:52','ON',10005,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(35,'amico','2019-08-29 19:50:01','2019-08-29 19:50:01','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Bulk order \nPO# AS729961','',NULL,'2019-09-06 19:50:01','ON',10006,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(36,'amico','2019-08-29 19:57:13','2019-08-29 19:57:13','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Bulk order\nPO# AA0134820','',NULL,'2019-09-06 19:57:13','ON',10007,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(37,'amico','2019-08-29 19:59:50','2019-08-29 19:59:50','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Bulk order\nPO# AP0717996','',NULL,'2019-09-06 19:59:50','ON',10008,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(38,'amico','2019-09-06 13:42:02','2019-09-06 13:42:02','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO#: AC7004749','',NULL,'2019-09-14 13:42:02','ON',10009,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(39,'amico','2019-09-23 16:11:13','2019-09-23 16:11:13','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7004855','',NULL,'2019-10-01 16:11:13','ON',10010,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(40,'amico','2019-10-04 15:22:14','2019-10-04 15:22:14','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AA0135016','',NULL,'2019-10-12 15:22:14','ON',10011,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(41,'amico','2019-10-04 15:26:59','2019-10-04 15:26:59','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AA0135017','',NULL,'2019-10-12 15:26:59','ON',10012,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(42,'amico','2019-10-15 13:35:55','2019-10-15 13:35:55','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7004957','',NULL,'2019-10-23 13:35:55','ON',10013,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(43,'amico','2019-10-18 19:04:21','2019-10-18 19:04:21','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AB0304311','',NULL,'2019-10-26 19:04:21','ON',10014,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(44,'amico','2019-10-21 13:43:59','2019-10-21 13:43:59','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'RUSH ORDER\nI do not have the PO# yet but once i do i will send Peggy an email with the PO #\nNeed the business cards by EOD Tuesday, October 22 - informed by Peggy that this could be done. \nThank you very much for your help! ','',NULL,'2019-10-29 13:43:59','ON',10015,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(45,'amico','2019-10-24 13:44:27','2019-10-24 13:44:27','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# LT0505921','',NULL,'2019-11-01 13:44:27','ON',10016,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(46,'amico','2019-10-24 13:48:03','2019-10-24 13:48:03','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# CS0472680','',NULL,'2019-11-01 13:48:03','ON',10017,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(47,'amico','2019-10-24 13:50:54','2019-10-24 13:50:54','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO#APC7005039','',NULL,'2019-11-01 13:50:54','ON',10018,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(48,'amico','2019-10-24 13:55:59','2019-10-24 13:55:59','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# CS0472693\nIs it possible to get Collin Maronese cards by EOD Monday, Oct 28? Just his, the others can wait for the week turnaround. Thank you ','',NULL,'2019-11-01 13:55:59','ON',10019,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(49,'amico','2019-10-29 18:59:25','2019-10-29 18:59:25','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# APC7005071','',NULL,'2019-11-06 18:59:25','ON',10020,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(50,'amico','2019-10-30 15:18:54','2019-10-30 15:18:54','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7005084\nRush order - have been informed by Peggy that we can have the order by EOD on Friday, Nov 1. \nThank you for your help!  ','',NULL,'2019-11-07 15:18:54','ON',10021,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(51,'amico','2019-11-07 21:17:16','2019-11-07 21:17:16','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AA0135295','',NULL,'2019-11-15 21:17:16','ON',10022,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(52,'amico','2019-11-07 21:20:08','2019-11-07 21:20:08','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7005139','',NULL,'2019-11-15 21:20:08','ON',10023,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(53,'amico','2019-11-07 21:59:15','2019-11-07 21:59:15','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO # AIC0034740','',NULL,'2019-11-15 21:59:15','ON',10024,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(54,'amico','2019-12-16 14:41:55','2019-12-16 14:41:55','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7005391','',NULL,'2019-12-24 14:41:55','ON',10025,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(55,'amico','2020-01-02 14:30:54','2020-01-02 14:30:54','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7005453','',NULL,'2020-01-10 14:30:54','ON',10026,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(56,'amico','2020-01-13 15:18:32','2020-01-13 15:18:32','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'POAA0135647','',NULL,'2020-01-21 15:18:32','ON',10027,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(57,'amico','2020-01-17 13:53:03','2020-01-17 13:53:03','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# MO0302252','',NULL,'2020-01-25 13:53:03','ON',10028,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(58,'amico','2020-01-24 16:29:13','2020-01-24 16:29:13','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AS731156','',NULL,'2020-02-01 16:29:13','ON',10029,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(59,'amico','2020-02-04 20:41:07','2020-02-04 20:41:07','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7005675','',NULL,'2020-02-12 20:41:07','ON',10030,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(60,'amico','2020-02-04 20:42:35','2020-02-04 20:42:35','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AIC0035130','',NULL,'2020-02-12 20:42:35','ON',10031,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(61,'amico','2020-02-11 15:32:59','2020-02-11 15:32:59','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7005735','',NULL,'2020-02-19 15:32:59','ON',10032,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(62,'amico','2020-02-13 19:55:10','2020-02-13 19:55:10','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO#AA0135863','',NULL,'2020-02-21 19:55:10','ON',10033,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(63,'amico','2020-02-25 16:39:18','2020-02-25 16:39:18','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# LT0506367','',NULL,'2020-03-04 16:39:18','ON',10034,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(64,'amico','2020-03-02 16:31:29','2020-03-02 16:31:29','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7005865','',NULL,'2020-03-10 16:31:29','ON',10035,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(65,'amico','2020-03-06 18:55:02','2020-03-06 18:55:02','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# APC7005914','',NULL,'2020-03-14 18:55:02','ON',10036,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(66,'amico','2020-03-13 17:43:02','2020-03-13 17:43:02','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# LT0506465','',NULL,'2020-03-21 17:43:02','ON',10037,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(67,'amico','2020-05-11 10:11:01','2020-05-11 10:11:01','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7006371','',NULL,'2020-05-19 10:11:01','ON',10038,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(68,'amico','2020-07-07 10:51:58','2020-07-07 10:51:58','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# CS0474132','',NULL,'2020-07-15 10:51:58','ON',10039,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(69,'amico','2020-09-18 09:24:26','2020-09-18 09:24:26','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AA0137610','',NULL,'2020-09-26 09:24:26','ON',10040,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(70,'amico','2020-10-23 16:23:56','2020-10-23 16:23:56','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO: AA0137836','',NULL,'2020-10-31 16:23:56','ON',10041,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','85 Fulton Way','','Richmond Hill','ON','Canada','L4B 2N4',NULL,NULL,NULL),(71,'amico','2020-11-03 14:00:43','2020-11-03 14:00:43','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO: AC7007478','',NULL,'2020-11-11 14:00:43','ON',10042,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(72,'amico','2020-11-10 11:05:45','2020-11-10 11:05:45','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7007517','',NULL,'2020-11-18 11:05:45','ON',10043,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(73,'amico','2020-12-03 16:06:50','2020-12-03 16:06:50','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO: APC7007669','',NULL,'2020-12-11 16:06:50','ON',10044,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(74,'amico','2021-02-16 13:51:32','2021-02-16 13:51:32','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AA0138575','',NULL,'2021-02-24 13:51:32','ON',10045,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(75,'amico','2021-02-19 16:28:23','2021-02-19 16:28:23','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO: AA0138600','',NULL,'2021-02-27 16:28:23','ON',10046,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(77,'amico','2021-05-03 10:51:57','2021-05-03 10:51:57','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# CS0475941\nPlease include $15 fee for changing back image so total on invoice will be $380 + tax - thank you ','',NULL,'2021-05-11 10:51:57','ON',10047,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(78,'amico','2021-05-03 11:00:04','2021-05-03 11:00:04','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# MO0303348\nPlease include the $15 fee to change the back image on the invoice so total will be $526+tax - thank you ','',NULL,'2021-05-11 11:00:04','ON',10048,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(79,'amico','2021-05-03 11:07:14','2021-05-03 11:07:14','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# CS0475942\nPlease include the fee of $50 to set-up this new division so invoice will be $561+tax - thank you ','',NULL,'2021-05-11 11:07:14','ON',10049,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(80,'amico','2021-06-03 13:42:45','2021-06-03 13:42:45','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7008792','',NULL,'2021-06-11 13:42:45','ON',10050,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(81,'amico','2021-06-29 10:05:44','2021-06-29 10:05:44','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# APC7008951','',NULL,'2021-07-07 10:05:44','ON',10051,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(82,'amico','2021-07-15 09:49:01','2021-07-15 09:49:01','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AA0139479','',NULL,'2021-07-23 09:49:01','ON',10052,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(83,'amico','2021-07-23 13:45:33','2021-07-23 13:45:33','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# LT0508250','',NULL,'2021-07-31 13:45:33','ON',10053,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(84,'amico','2021-08-04 14:37:04','2021-08-04 14:37:04','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7009155','',NULL,'2021-08-12 14:37:04','ON',10054,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(85,'amico','2021-08-16 12:12:48','2021-08-16 12:12:48','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AC7009215','',NULL,'2021-08-24 12:12:48','ON',10055,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(86,'amico','2021-08-30 09:35:07','2021-08-30 09:35:07','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AS735653','',NULL,'2021-09-07 09:35:07','ON',10056,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(87,'amico','2021-09-13 13:53:40','2021-09-13 13:53:40','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO: AC7009372','',NULL,'2021-09-21 13:53:40','ON',10057,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(88,'amico','2021-09-16 08:39:56','2021-09-16 08:39:56','','Kimberley','Matuska','905.764.0800','','kmatuska@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO: AS735783','',NULL,'2021-09-24 08:39:56','ON',10058,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(89,'amico','2021-09-21 17:11:58','2021-09-21 17:11:58','','Sandy ','Deak','905.764.0800','','marketing@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'PO# AA0139862\nThis is an order for the temporary 250 cards with the Blue Amico Logo, to then be the full order of 500 cards when the foil machine is running.','',NULL,'2021-09-29 17:11:58','ON',10059,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(90,'amico','2021-11-15 15:06:25','2021-11-15 15:06:25','','Sonia','Yaqub','905.764.0800','','marketing@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'New Silver Foil Business Cards order.','Deliver to 55 East Wilmot Street, attn: Sonia Yaqub. ',NULL,'2021-11-23 15:06:25','ON',10060,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(91,'amico','2021-12-17 16:25:48','2021-12-17 16:25:48','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','Doing this for the first time, I will email purchase orders. This is the most I could collect for this batch. Thank you.',NULL,'2021-12-25 16:25:48','ON',10061,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(92,'amico','2021-12-30 11:45:18','2021-12-30 11:45:18','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'I will send you the Purchase order shortly. ','',NULL,'2022-01-07 11:45:18','ON',10062,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(93,'amico','2022-01-28 14:57:10','2022-01-28 14:57:10','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','I will email Purchase orders shortly.',NULL,'2022-02-05 14:57:10','ON',10063,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(94,'amico','2022-01-28 14:58:58','2022-01-28 14:58:58','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','I will send purchase orders shortly. ',NULL,'2022-02-05 14:58:58','ON',10064,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(95,'amico','2022-01-28 15:00:23','2022-01-28 15:00:23','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','I will send you purchase orders shortly. ',NULL,'2022-02-05 15:00:23','ON',10065,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(96,'amico','2022-01-28 15:03:34','2022-01-28 15:03:34','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','I will send you purchase orders shortly. ',NULL,'2022-02-05 15:03:34','ON',10066,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(97,'amico','2022-03-02 13:28:31','2022-03-02 13:28:31','','Sandy','Deak','905.764.0800','','marketing@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'attn: Marketing/Sandy Deak\n','',NULL,'2022-03-10 13:28:31','ON',10067,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(98,'amico','2022-04-22 14:56:57','2022-04-22 14:56:57','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2022-04-30 14:56:57','ON',10068,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(100,'amico','2022-07-22 14:23:54','2022-07-22 14:23:54','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2022-07-30 14:23:54','ON',10069,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Kimberley','Matuska','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(101,'amico','2022-09-28 15:08:05','2022-09-28 15:08:05','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'There are two orders of business cards; the other one is a custom business card. Please see the email I sent to Peggy. ','',NULL,'2022-10-06 15:08:05','ON',10070,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(102,'amico','2022-10-19 14:11:12','2022-10-19 14:11:12','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Delivery to 120 Fulton way receiving department. attn to Sonia Yaqub (Marketing) ','',NULL,'2022-10-27 14:11:12','ON',10071,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(103,'amico','2022-10-20 14:19:20','2022-10-20 14:19:20','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'I will send you the P.O. in the email.','',NULL,'2022-10-28 14:19:20','ON',10072,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(104,'amico','2022-10-26 10:43:58','2022-10-26 10:43:58','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2022-11-03 10:43:58','ON',10073,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(105,'peggy','2022-11-09 16:40:23','2022-11-09 16:40:23','','Peggy','Cheng','905-709-9609','905-709-9610','peggy@creativepath.com','Creative Path Digital Imaging Inc. ','1455-16th Avenue, Unit 1 & 2','','Richmond Hill','ON','Canada','L4B 4W5','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Eric, please see PO for more information. ','',NULL,'2022-11-17 16:40:23','ON',10074,'','CPDI',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Peggy','Cheng','905-709-9609','905-709-9610','Creative Path Digital Imaging Inc. ','1455-16th Avenue, Unit 1 & 2','','Richmond Hill','ON','Canada','L4B 4W5',NULL,NULL,NULL),(107,'amico','2023-01-25 11:26:53','2023-01-25 11:26:53','','Marketin','Department','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Shipping to 120 Fulton Way, c. ','',NULL,'2023-02-02 11:26:53','ON',10075,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(108,'amico','2023-03-06 16:12:43','2023-03-06 16:12:43','','Marketing','Department','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Delivery to 55 East Wilmot St','',NULL,'2023-03-14 16:12:43','ON',10076,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(109,'amico','2023-03-23 12:07:12','2023-03-23 12:07:12','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2023-03-31 12:07:12','ON',10077,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(110,'peggy','2023-04-04 10:56:58','2023-04-04 10:56:58','','Peggy','Cheng','905-709-9609','905-709-9610','peggy@creativepath.com','Creative Path Digital Imaging Inc. ','1455-16th Avenue, Unit 1 & 2','','Richmond Hill','ON','Canada','L4B 4W5','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Offline order','',NULL,'2023-04-12 10:56:58','ON',10078,'','CPDI',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Peggy','Cheng','905-709-9609','905-709-9610','Creative Path Digital Imaging Inc. ','1455-16th Avenue, Unit 1 & 2','','Richmond Hill','ON','Canada','L4B 4W5',NULL,NULL,NULL),(111,'amico','2023-04-06 13:58:59','2023-04-06 13:58:59','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2023-04-14 13:58:59','ON',10079,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(112,'amico','2023-04-14 16:10:37','2023-04-14 16:10:37','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2023-04-22 16:10:37','ON',10080,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(113,'amico','2023-04-28 10:44:07','2023-04-28 10:44:07','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Deliver to 55 East Wilmot ','',NULL,'2023-05-06 10:44:07','ON',10081,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(114,'amico','2023-05-19 15:43:27','2023-05-19 15:43:27','','Marketing','Department','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2023-05-27 15:43:27','ON',10082,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(115,'amico','2023-05-23 14:31:52','2023-05-23 14:31:52','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2023-05-31 14:31:52','ON',10083,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(117,'amico','2023-06-07 16:17:25','2023-06-07 16:17:25','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2023-06-15 16:17:25','ON',10084,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(118,'amico','2023-07-13 14:21:05','2023-07-13 14:21:05','','Sonia ','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2023-07-21 14:21:05','ON',10085,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(119,'amico','2023-08-16 11:03:26','2023-08-16 11:03:26','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2023-08-24 11:03:26','ON',10086,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(120,'amico','2023-09-11 15:48:21','2023-09-11 15:48:21','','Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2023-09-19 15:48:21','ON',10087,'','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(121,'amico','2023-09-27 11:33:42','2023-09-27 11:33:42','','Marketing','Department','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2023-10-05 11:33:42','ON',10088,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(122,'amico','2023-10-26 16:30:22','2023-10-26 16:30:22',NULL,'Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2023-11-03 16:30:22','ON',10089,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(123,'amico','2024-01-19 14:20:33','2024-01-19 14:20:33',NULL,'Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Please deliver to 55 Receiving dock area ','',NULL,'2024-01-27 14:20:33','ON',10090,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(124,'amico','2024-02-09 14:21:37','2024-02-09 14:21:37',NULL,'Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Delivery to Receiving area at 55 East Wilmot','',NULL,'2024-02-17 14:21:37','ON',10091,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(125,'amico','2024-03-08 16:31:34','2024-03-08 16:31:34',NULL,'Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Ship to Receiving area of 55 East Wilmot. ','',NULL,'2024-03-16 16:31:34','ON',10092,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(126,'peggy','2024-06-26 15:56:38','2024-06-26 15:56:38',NULL,'Peggy','Cheng','905-709-9609','905-709-9610','peggy@creativepath.com','Creative Path Digital Imaging Inc. ','1455-16th Avenue, Unit 1 & 2','','Richmond Hill','ON','Canada','L4B 4W5','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'Offline order - custom card','',NULL,'2024-07-04 15:56:38','ON',10093,'BHO','CPDI',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Peggy','Cheng','905-709-9609','905-709-9610','Creative Path Digital Imaging Inc. ','1455-16th Avenue, Unit 1 & 2','','Richmond Hill','ON','Canada','L4B 4W5',NULL,NULL,NULL),(128,'amico','2024-07-19 16:21:58','2024-07-19 16:21:58',NULL,'Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2024-07-27 16:21:58','ON',10094,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(129,'peggy','2024-07-22 12:17:04','2024-07-22 12:17:04',NULL,'Peggy','Cheng','905-709-9609','905-709-9610','peggy@creativepath.com','Creative Path Digital Imaging Inc. ','1455-16th Avenue, Unit 1 & 2','','Richmond Hill','ON','Canada','L4B 4W5','CANCELLED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'20240723104512','',NULL,'2024-07-30 12:17:04','ON',10095,'BHO','CPDI',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Peggy','Cheng','905-709-9609','905-709-9610','Creative Path Digital Imaging Inc. ','1455-16th Avenue, Unit 1 & 2','','Richmond Hill','ON','Canada','L4B 4W5',NULL,NULL,NULL),(130,'amico','2024-07-22 14:22:02','2024-07-22 14:22:02',NULL,'Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2024-07-30 14:22:02','ON',10096,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(131,'amico','2024-07-25 16:12:27','2024-07-25 16:12:27',NULL,'Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2024-08-02 16:12:27','ON',10097,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(132,'amico','2024-08-08 11:39:53','2024-08-08 11:39:53',NULL,'Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2024-08-16 11:39:53','ON',10098,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(133,'amico','2024-08-14 16:44:40','2024-08-14 16:44:40',NULL,'Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2024-08-22 16:44:40','ON',10099,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(134,'peggy','2024-08-27 14:33:48','2024-08-27 14:33:48',NULL,'Peggy','Cheng','905-709-9609','905-709-9610','peggy@creativepath.com','Creative Path Digital Imaging Inc. ','1455-16th Avenue, Unit 1 & 2','','Richmond Hill','ON','Canada','L4B 4W5','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'SEE PO for Invoicing. ','',NULL,'2024-09-04 14:33:48','ON',10100,'BHO','CPDI',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Peggy','Cheng','905-709-9609','905-709-9610','Creative Path Digital Imaging Inc. ','1455-16th Avenue, Unit 1 & 2','','Richmond Hill','ON','Canada','L4B 4W5',NULL,NULL,NULL),(135,'zoeyc','2024-09-07 13:50:15','2024-09-07 13:50:15',NULL,'Zoey','Cui','905-709-9609','905-709-9610','zoeyc@cpfulfillment.com','Creative Path Digital Imaging Inc. ','1455-16th Avenue, Unit 1 & 2','','Richmond Hill','ON','Canada','L4B 4W5','CANCELLED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'20240916110116','',NULL,'2024-09-15 13:50:15','ON',10101,'BHO','CPDI',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Zoey','C','905-709-9609','905-709-9610','Creative Path Digital Imaging Inc. ','1455-16th Avenue, Unit 1 & 2','','Richmond Hill','ON','Canada','L4B 4W5',NULL,NULL,NULL),(136,'amico','2024-09-19 13:27:48','2024-09-19 13:27:48',NULL,'Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2024-09-27 13:27:48','ON',10102,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(137,'amico','2024-09-27 11:58:09','2024-09-27 11:58:09',NULL,'Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2024-10-05 11:58:09','ON',10103,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(138,'amico','2024-10-03 16:53:51','2024-10-03 16:53:51',NULL,'Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','SHIPPED','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2024-10-11 16:53:51','ON',10104,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL),(139,'amico','2024-11-19 14:22:04','2024-11-19 14:22:04',NULL,'Sonia','Yaqub','905.764.0800','','syaqub@amico.com','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3','ACTIVE','HST','','','','',0.1300,0.0000,0.0000,0.0000,0.0000,0.0000,'','',NULL,'2024-11-27 14:22:04','ON',10105,'BHO','amico',0.0000,0.0000,0.0000,0.0000,0.0000,'Ground','Sonia','Yaqub','905.764.0800','','Amico Corporation','55 East Wilmot St','','Richmond Hill','ON','Canada','L4B 1A3',NULL,NULL,NULL);

--
-- Table structure for table `orderPromos`
--

CREATE TABLE `orderPromos` (
  `ORDERID` int(11) NOT NULL,
  `PROMOCODE` varchar(50) NOT NULL,
  `PROMONAME` varchar(100) NOT NULL,
  `PROMOTYPE` varchar(12) NOT NULL,
  `SINGLEITEM` int(11) NOT NULL DEFAULT 0,
  `PROMOVALUE` decimal(19,2) NOT NULL,
  `CALCDISCOUNT` decimal(19,2) NOT NULL,
  `TAXAMT1` decimal(19,2) NOT NULL,
  `TAXAMT2` decimal(19,2) NOT NULL,
  `TAXAMT3` decimal(19,2) NOT NULL,
  `TAXAMT4` decimal(19,2) NOT NULL,
  `TAXAMT5` decimal(19,2) NOT NULL,
  PRIMARY KEY (`ORDERID`,`PROMOCODE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `orderPromos`
--


--
-- Table structure for table `preOrderDefinition`
--

CREATE TABLE `preOrderDefinition` (
  `CODE` varchar(50) NOT NULL,
  `ITEMNAME` varchar(100) NOT NULL,
  `SHORTDESC` varchar(100) NOT NULL DEFAULT '',
  `FULLDESC` varchar(1000) NOT NULL DEFAULT '',
  `COMPONENTINPUTS` varchar(1000) DEFAULT '',
  `DISPLAYIMAGEPATH` varchar(200) NOT NULL DEFAULT '',
  `ACTIVE` int(11) NOT NULL DEFAULT 1,
  `BRAND` varchar(50) NOT NULL DEFAULT 'ALL',
  `EMAILDESC` varchar(1000) NOT NULL DEFAULT '',
  `AVAILABLELANGS` varchar(100) NOT NULL DEFAULT 'English|Bilingual',
  PRIMARY KEY (`CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `preOrderDefinition`
--


--
-- Table structure for table `preOrderDefinitionV2`
--

CREATE TABLE `preOrderDefinitionV2` (
  `ACTIVE` int(11) NOT NULL DEFAULT 1,
  `CODE` varchar(50) NOT NULL,
  `BRAND` varchar(50) NOT NULL DEFAULT 'ALL',
  `ITEMNAME` varchar(100) NOT NULL,
  `INTRODESC` varchar(1000) NOT NULL DEFAULT '',
  `INTRODESC_EMAIL` varchar(1000) NOT NULL DEFAULT '',
  `ELIGIBLEQUESTION` varchar(1000) NOT NULL DEFAULT '',
  `ELIGIBLENO` varchar(1000) NOT NULL DEFAULT '',
  `AVAILABLELANGS` varchar(100) NOT NULL DEFAULT 'English|Bilingual',
  `SECTION1NAME` varchar(1000) DEFAULT '',
  `SECTION1INTRO` varchar(1000) DEFAULT '',
  `SECTION1INTRO_EMAIL` varchar(1000) DEFAULT '',
  `SECTION1COMPONENT` varchar(1000) DEFAULT '',
  `SECTION1FOOTER` varchar(1000) DEFAULT '',
  `SECTION2NAME` varchar(1000) DEFAULT '',
  `SECTION2INTRO` varchar(1000) DEFAULT '',
  `SECTION2INTRO_EMAIL` varchar(1000) DEFAULT '',
  `SECTION2COMPONENT` varchar(1000) DEFAULT '',
  `SECTION2FOOTER` varchar(1000) DEFAULT '',
  `SECTION3NAME` varchar(1000) DEFAULT '',
  `SECTION3INTRO` varchar(1000) DEFAULT '',
  `SECTION3INTRO_EMAIL` varchar(1000) DEFAULT '',
  `SECTION3COMPONENT` varchar(1000) DEFAULT '',
  `SECTION3FOOTER` varchar(1000) DEFAULT '',
  `SUBMITMESSAGE` varchar(1000) NOT NULL DEFAULT '',
  `BANNERIMAGE` varchar(1000) DEFAULT '',
  PRIMARY KEY (`CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `preOrderDefinitionV2`
--


--
-- Table structure for table `preOrders`
--

CREATE TABLE `preOrders` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `CODE` varchar(50) NOT NULL,
  `USERNAME` varchar(50) NOT NULL,
  `BRANCH` varchar(20) NOT NULL,
  `SKIN` varchar(20) NOT NULL,
  `STORENAME` varchar(100) DEFAULT NULL,
  `ADDRESS` varchar(1000) DEFAULT NULL,
  `ADDRESS2` varchar(1000) DEFAULT NULL,
  `CITY` varchar(100) DEFAULT NULL,
  `PROVINCE` varchar(100) DEFAULT NULL,
  `COUNTRY` varchar(100) DEFAULT NULL,
  `POSTALCODE` varchar(20) DEFAULT NULL,
  `CONTACTNAME` varchar(100) NOT NULL,
  `CONTACTEMAIL` varchar(100) NOT NULL,
  `BASEQTY` int(11) NOT NULL DEFAULT 1,
  `COMPONENTS` varchar(1000) DEFAULT NULL,
  `LANGUAGE` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `preOrders`
--


--
-- Table structure for table `preOrdersV2`
--

CREATE TABLE `preOrdersV2` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `CODE` varchar(50) NOT NULL,
  `USERNAME` varchar(50) NOT NULL,
  `BRANCH` varchar(20) NOT NULL,
  `SKIN` varchar(20) NOT NULL,
  `STORENAME` varchar(100) DEFAULT NULL,
  `ADDRESS` varchar(1000) DEFAULT NULL,
  `ADDRESS2` varchar(1000) DEFAULT NULL,
  `CITY` varchar(100) DEFAULT NULL,
  `PROVINCE` varchar(100) DEFAULT NULL,
  `COUNTRY` varchar(100) DEFAULT NULL,
  `POSTALCODE` varchar(20) DEFAULT NULL,
  `CONTACTNAME` varchar(100) NOT NULL,
  `CONTACTEMAIL` varchar(100) NOT NULL,
  `CONTACTPHONE` varchar(100) NOT NULL,
  `COMPONENTS1` varchar(1000) DEFAULT NULL,
  `COMPONENTS2` varchar(1000) DEFAULT NULL,
  `COMPONENTS3` varchar(1000) DEFAULT NULL,
  `LANGUAGE` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `preOrdersV2`
--


--
-- Table structure for table `promos`
--

CREATE TABLE `promos` (
  `CODE` varchar(50) NOT NULL,
  `NAME` varchar(100) NOT NULL,
  `CODETYPE` varchar(12) NOT NULL,
  `STARTDATE` date NOT NULL,
  `ENDDATE` date NOT NULL,
  `SINGLEUSE` int(11) NOT NULL DEFAULT 0,
  `REDEEMED` int(11) NOT NULL DEFAULT 0,
  `PROMOVALUE` decimal(19,2) NOT NULL DEFAULT 0.00,
  `SINGLEITEM` int(11) NOT NULL DEFAULT 0,
  `MAXQTY` int(11) DEFAULT 0,
  `MINQTY` int(11) DEFAULT 0,
  `EXACTQTY` int(11) DEFAULT 0,
  `QTYTYPE` varchar(12) NOT NULL,
  PRIMARY KEY (`CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `promos`
--


--
-- Table structure for table `promosCategories`
--

CREATE TABLE `promosCategories` (
  `CODE` varchar(50) NOT NULL,
  `CATID` int(11) NOT NULL,
  PRIMARY KEY (`CODE`,`CATID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

--
-- Dumping data for table `promosCategories`
--


--
-- Table structure for table `promosItems`
--

CREATE TABLE `promosItems` (
  `CODE` varchar(50) NOT NULL,
  `ITEMID` int(11) NOT NULL,
  PRIMARY KEY (`CODE`,`ITEMID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `promosItems`
--


--
-- Table structure for table `settings`
--

CREATE TABLE `settings` (
  `key` varchar(100) NOT NULL,
  `value` varchar(100) NOT NULL,
  PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `settings`
--

INSERT INTO `settings` VALUES ('site_maint','0');

--
-- Table structure for table `shipToAddresses`
--

CREATE TABLE `shipToAddresses` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `STOREID` varchar(10) DEFAULT NULL,
  `NAME` varchar(100) NOT NULL,
  `ADDRESS` varchar(1000) NOT NULL,
  `ADDRESS2` varchar(1000) NOT NULL DEFAULT '',
  `CITY` varchar(100) NOT NULL,
  `PROVINCE` varchar(100) NOT NULL,
  `COUNTRY` varchar(100) NOT NULL,
  `POSTAL` varchar(20) NOT NULL,
  `PHONE` varchar(20) NOT NULL,
  `FAX` varchar(20) NOT NULL,
  `TAXGROUP` varchar(6) NOT NULL,
  `SHIPZONE` varchar(6) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `shipToAddresses`
--


--
-- Table structure for table `shipToAddressesUserList`
--

CREATE TABLE `shipToAddressesUserList` (
  `ID` int(11) NOT NULL,
  `USERNAME` varchar(50) NOT NULL,
  PRIMARY KEY (`ID`,`USERNAME`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

--
-- Dumping data for table `shipToAddressesUserList`
--


--
-- Table structure for table `shipZoneCosts`
--

CREATE TABLE `shipZoneCosts` (
  `WEIGHT` int(11) NOT NULL,
  `None` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone1` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone2` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone3` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone4` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone5` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone6` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone7` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone8` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone9` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone10` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone11` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone12` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone13` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone14` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone15` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone16` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone17` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone18` decimal(16,2) NOT NULL DEFAULT 0.00,
  `Zone19` decimal(16,2) NOT NULL DEFAULT 0.00,
  PRIMARY KEY (`WEIGHT`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `shipZoneCosts`
--

INSERT INTO `shipZoneCosts` VALUES (0,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00);

--
-- Table structure for table `shipmentCostLog`
--

CREATE TABLE `shipmentCostLog` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `CARRIER` varchar(100) DEFAULT NULL,
  `METHOD` varchar(100) DEFAULT NULL,
  `SHIPFROM_NAME` varchar(100) DEFAULT NULL,
  `SHIPFROM_STREETNUM` varchar(100) DEFAULT NULL,
  `SHIPFROM_STREET` varchar(100) DEFAULT NULL,
  `SHIPFROM_CITY` varchar(100) DEFAULT NULL,
  `SHIPFROM_PROVINCE` varchar(100) DEFAULT NULL,
  `SHIPFROM_COUNTRY` varchar(100) DEFAULT NULL,
  `SHIPFROM_POSTAL` varchar(100) DEFAULT NULL,
  `TOTALWEIGHT` decimal(19,4) DEFAULT NULL,
  `TOTALBOXES` int(11) DEFAULT NULL,
  `SHIPTO_PROVINCE` varchar(100) DEFAULT NULL,
  `SHIPTO_POSTAL` varchar(100) DEFAULT NULL,
  `SHIPTO_CITY` varchar(100) DEFAULT NULL,
  `TOTALPRICE` decimal(19,4) DEFAULT NULL,
  `OURMARKUP` decimal(19,4) DEFAULT NULL,
  `OURSLEEVECHARGE` decimal(19,4) DEFAULT NULL,
  `ORDERID` int(11) DEFAULT NULL,
  `CREATEDATETIME` datetime DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `shipmentCostLog`
--

INSERT INTO `shipmentCostLog` VALUES (1,'PUROLATOR','Express','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',9.6600,1.3000,0.0000,23,'2019-05-24 15:29:57'),(2,'PUROLATOR','Ground','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',9.4900,1.3000,0.0000,23,'2019-05-24 15:29:57'),(3,'CANPAR','Ground','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',8.2000,1.3000,0.0000,23,'2019-05-24 15:29:58'),(4,'CANPAR','Express','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',26.5300,1.3000,0.0000,23,'2019-05-24 15:29:58'),(5,'PUROLATOR','Express','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',9.6600,1.3000,0.0000,23,'2019-05-24 19:53:47'),(6,'PUROLATOR','Ground','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',9.4900,1.3000,0.0000,23,'2019-05-24 19:53:47'),(7,'CANPAR','Ground','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',8.2000,1.3000,0.0000,23,'2019-05-24 19:53:48'),(8,'CANPAR','Express','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',26.5300,1.3000,0.0000,23,'2019-05-24 19:53:48'),(9,'PUROLATOR','Express','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',9.6600,1.3000,0.0000,23,'2019-05-24 19:53:54'),(10,'PUROLATOR','Ground','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',9.4900,1.3000,0.0000,23,'2019-05-24 19:53:54'),(11,'CANPAR','Ground','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',8.2000,1.3000,0.0000,23,'2019-05-24 19:53:55'),(12,'CANPAR','Express','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',26.5300,1.3000,0.0000,23,'2019-05-24 19:53:55'),(13,'PUROLATOR','Express','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',9.7500,1.3000,0.0000,24,'2019-06-11 19:57:22'),(14,'PUROLATOR','Ground','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',9.5700,1.3000,0.0000,24,'2019-06-11 19:57:22'),(15,'CANPAR','Ground','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',8.2000,1.3000,0.0000,24,'2019-06-11 19:57:22'),(16,'CANPAR','Express','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.5000,1,'ON','L4B 2N4','Richmond Hill',26.5300,1.3000,0.0000,24,'2019-06-11 19:57:22'),(17,'PUROLATOR','Express','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.0000,1,'ON','L4B 2N4','Richmond Hill',9.7500,1.3000,0.0000,25,'2019-06-13 18:00:39'),(18,'PUROLATOR','Ground','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.0000,1,'ON','L4B 2N4','Richmond Hill',9.5700,1.3000,0.0000,25,'2019-06-13 18:00:39'),(19,'CANPAR','Ground','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.0000,1,'ON','L4B 2N4','Richmond Hill',8.2000,1.3000,0.0000,25,'2019-06-13 18:00:39'),(20,'CANPAR','Express','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.0000,1,'ON','L4B 2N4','Richmond Hill',26.5300,1.3000,0.0000,25,'2019-06-13 18:00:39'),(21,'PUROLATOR','Express','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.0000,1,'ON','L4B 2N4','Richmond Hill',9.7500,1.3000,0.0000,25,'2019-06-13 18:31:34'),(22,'PUROLATOR','Ground','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.0000,1,'ON','L4B 2N4','Richmond Hill',9.5700,1.3000,0.0000,25,'2019-06-13 18:31:34'),(23,'CANPAR','Ground','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.0000,1,'ON','L4B 2N4','Richmond Hill',8.2000,1.3000,0.0000,25,'2019-06-13 18:31:35'),(24,'CANPAR','Express','Creative Path','1455','16th Avenue','Richmond Hill','ON','CA','L4B4W5',1.0000,1,'ON','L4B 2N4','Richmond Hill',26.5300,1.3000,0.0000,25,'2019-06-13 18:31:35');

--
-- Table structure for table `shipmentDetail`
--

CREATE TABLE `shipmentDetail` (
  `SHIPID` int(11) NOT NULL,
  `SHIPLINE` int(11) NOT NULL AUTO_INCREMENT,
  `ORDERID` int(11) NOT NULL,
  `ORDERLINE` int(11) NOT NULL,
  `SHIPQTY` int(11) NOT NULL,
  PRIMARY KEY (`SHIPLINE`)
) ENGINE=InnoDB AUTO_INCREMENT=254 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

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

INSERT INTO `shipmentDetail` VALUES (1,1,31,50,500),(2,2,35,65,250),(3,3,36,67,500),(3,4,36,68,500),(3,5,36,69,500),(3,6,36,70,500),(3,7,36,71,500),(3,8,36,72,500),(3,9,36,73,500),(3,10,36,74,500),(3,11,36,75,500),(4,12,34,61,500),(4,13,34,62,500),(4,14,34,63,500),(4,15,34,64,500),(5,16,32,51,500),(6,17,33,52,500),(6,18,33,54,500),(6,19,33,55,500),(6,20,33,56,500),(6,21,33,57,500),(6,22,33,58,500),(6,23,33,59,500),(6,24,33,60,500),(7,25,37,76,500),(7,26,37,77,250),(7,27,37,78,500),(8,28,38,79,500),(9,29,39,80,250),(10,30,40,81,250),(10,31,40,82,250),(11,32,41,83,250),(11,33,41,84,250),(11,34,41,85,250),(11,35,41,86,250),(11,36,41,87,250),(11,37,41,88,250),(12,38,42,89,250),(14,39,44,91,500),(15,40,43,90,250),(17,41,48,96,250),(17,42,48,97,500),(17,43,48,98,250),(18,44,45,92,500),(18,45,45,93,500),(19,46,47,95,250),(20,47,46,94,500),(21,48,50,100,500),(21,49,50,101,500),(22,50,49,99,500),(23,51,51,102,500),(24,52,52,103,250),(25,53,53,104,500),(26,54,54,105,250),(27,55,55,106,250),(28,56,56,107,500),(29,57,57,108,500),(30,58,58,109,500),(30,59,58,110,500),(30,60,58,111,500),(31,61,59,112,250),(32,62,60,113,250),(33,63,61,114,250),(33,64,61,115,250),(34,65,62,116,500),(34,66,62,117,500),(35,67,63,118,500),(35,68,63,119,500),(35,69,63,120,250),(36,70,64,121,250),(37,71,65,122,250),(37,72,65,123,250),(38,73,66,124,500),(39,74,67,125,250),(39,75,67,126,250),(39,76,67,127,500),(39,77,67,128,500),(40,78,68,129,500),(41,79,69,130,250),(42,80,70,131,250),(43,81,71,132,500),(44,82,72,133,250),(44,83,72,134,250),(45,84,73,135,250),(46,85,74,136,250),(47,86,75,137,500),(48,87,77,139,250),(48,88,77,140,250),(48,89,77,141,250),(48,90,77,142,250),(48,91,77,143,250),(49,92,78,144,250),(49,93,78,145,250),(49,94,78,146,250),(49,95,78,147,250),(49,96,78,148,250),(49,97,78,149,250),(49,98,78,150,250),(50,99,79,151,250),(50,100,79,152,250),(50,101,79,153,250),(50,102,79,154,250),(50,103,79,155,250),(50,104,79,156,250),(50,105,79,157,250),(51,106,80,158,500),(51,107,80,159,500),(51,108,80,160,500),(52,109,81,161,0),(52,110,81,162,0),(52,111,81,163,0),(53,112,82,165,0),(54,113,81,161,250),(54,114,81,162,500),(54,115,81,163,500),(55,116,82,165,500),(56,117,83,166,500),(56,118,83,167,500),(57,119,84,168,250),(58,120,85,169,250),(59,121,86,170,250),(59,122,86,171,250),(60,123,87,172,250),(61,124,88,173,500),(62,125,89,174,500),(63,126,90,175,250),(63,127,90,176,500),(63,128,90,177,500),(63,129,90,178,250),(63,130,90,179,250),(64,131,91,180,500),(64,132,91,181,500),(64,133,91,182,500),(65,134,92,183,250),(65,135,92,184,250),(65,136,92,185,250),(65,137,92,186,250),(65,138,92,187,250),(65,139,92,188,250),(65,140,92,189,250),(65,141,92,190,250),(65,142,92,191,250),(65,143,92,192,250),(65,144,92,193,250),(65,145,92,194,250),(66,146,93,195,500),(66,147,93,196,500),(66,148,93,197,500),(66,149,93,198,500),(66,150,93,199,500),(66,151,93,200,500),(66,152,93,201,500),(67,153,94,202,250),(67,154,94,203,250),(67,155,94,204,250),(68,156,95,205,500),(68,157,95,206,500),(69,158,96,207,250),(69,159,96,208,250),(70,160,97,209,500),(70,161,97,210,500),(70,162,97,211,500),(71,163,98,212,250),(71,164,98,213,500),(71,165,98,214,500),(71,166,98,215,250),(72,167,100,229,250),(72,168,100,230,250),(72,169,100,231,250),(72,170,100,232,500),(72,171,100,233,500),(72,172,100,234,500),(72,173,100,235,500),(72,174,100,236,500),(72,175,100,237,500),(72,176,100,238,500),(72,177,100,239,500),(72,178,100,240,500),(72,179,100,241,500),(73,180,101,242,250),(74,181,102,243,500),(74,182,102,244,500),(75,183,103,246,500),(76,184,105,250,250),(77,185,104,247,250),(77,186,104,248,500),(77,187,104,249,500),(78,188,107,258,500),(78,189,107,259,500),(78,190,107,260,250),(78,191,107,261,250),(78,192,107,262,250),(78,193,107,263,250),(78,194,107,264,250),(79,195,108,265,500),(79,196,108,266,250),(79,197,108,267,250),(79,198,108,268,250),(79,199,108,269,500),(79,200,108,270,250),(79,201,108,271,250),(80,202,110,276,40),(81,203,109,272,500),(81,204,109,273,500),(81,205,109,274,500),(81,206,109,275,500),(82,207,111,277,250),(83,208,112,278,250),(83,209,112,279,500),(84,210,113,280,250),(85,211,114,281,500),(86,212,115,282,500),(87,213,117,284,250),(88,214,118,285,500),(88,215,118,286,250),(88,216,118,287,250),(88,217,118,288,250),(88,218,118,289,250),(89,219,119,290,250),(89,220,119,291,500),(89,221,119,292,500),(90,222,120,293,250),(90,223,120,294,500),(90,224,120,295,500),(90,225,120,296,250),(91,226,121,297,500),(92,227,122,298,250),(93,228,123,299,250),(93,229,123,300,250),(93,230,123,301,500),(93,231,123,302,500),(93,232,123,303,250),(94,233,124,304,250),(95,234,125,305,250),(95,235,125,306,250),(95,236,125,307,250),(95,237,125,308,250),(95,238,125,309,250),(95,239,125,310,250),(95,240,125,311,250),(96,241,126,312,500),(97,242,128,314,500),(98,243,130,316,250),(98,244,130,317,250),(99,245,131,318,250),(99,246,131,319,250),(100,247,133,321,250),(101,248,134,322,500),(102,249,136,324,500),(103,250,132,320,500),(104,251,137,325,250),(105,252,138,326,250),(105,253,138,327,250);

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

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

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

INSERT INTO `shipmentHeader` VALUES (1,31,'CPDI - 08/06/2019','2019-08-07 13:34:59','CP Delivery',0.00,0.0000,1,''),(2,35,'CPDI - 09/13/2019','2019-09-16 14:17:04','CP Delivery',0.00,0.0000,1,''),(3,36,'CPDI - 09/13/2019','2019-09-16 14:17:18','CP Delivery',0.00,0.0000,1,''),(4,34,'CPDI - 09/13/2019','2019-09-16 14:17:37','CP Delivery',0.00,0.0000,1,''),(5,32,'CPDI - 09/13/2019','2019-09-16 14:17:52','CP Delivery',0.00,0.0000,1,''),(6,33,'CPDI - 09/13/2019','2019-09-16 14:18:04','CP Delivery',0.00,0.0000,1,''),(7,37,'CPDI - 09/13/2019','2019-09-16 14:18:21','CP Delivery',0.00,0.0000,1,''),(8,38,'CPDI - 09/16/2019','2019-09-16 20:59:17','CP Delivery',0.00,0.0000,1,''),(9,39,'CPDI - 09/26/2019','2019-09-26 20:32:22','CP Delivery',0.00,0.0000,1,''),(10,40,'CPDI - 10/10/2019','2019-10-11 14:10:03','CP Delivery',0.00,0.0000,1,''),(11,41,'CPDI - 10/10/2019','2019-10-11 14:18:26','CP Delivery',0.00,0.0000,1,''),(12,42,'CPDI - 10/18/2019','2019-10-21 15:05:10','CP Delivery',0.00,0.0000,1,''),(13,0,'CPDI - 10/22/2019','2019-10-23 13:18:37','CP Delivery',0.00,0.0000,1,''),(14,44,'CPDI - 10/22/2019','2019-10-23 13:18:49','CP Delivery',0.00,0.0000,1,''),(15,43,'CPDI - 10/25/2019','2019-10-28 13:40:30','CP Delivery',0.00,0.0000,1,''),(16,0,'CPDI - 10/28/2019','2019-10-29 19:38:45','CP Delivery',0.00,0.0000,1,''),(17,48,'CPDI','2019-10-31 14:49:25','Ground',0.00,0.0000,1,''),(18,45,'CPDI - 10/30/2019','2019-10-31 15:46:46','CP Delivery',0.00,0.0000,1,''),(19,47,'CPDI - 10/30/2019','2019-10-31 15:47:06','CP Delivery',0.00,0.0000,1,''),(20,46,'CPDI','2019-10-31 16:32:12','Ground',0.00,0.0000,1,''),(21,50,'CPDI','2019-11-04 16:26:08','Ground',0.00,0.0000,1,''),(22,49,'CPDI - 11/06/2019','2019-11-07 21:27:43','CP Delivery',0.00,0.0000,0,''),(23,51,'CPDI - 11/15/2019','2019-11-15 17:24:17','CP Delivery',0.00,0.0000,1,''),(24,52,'CPDI - 11/15/2019','2019-11-15 17:24:28','CP Delivery',0.00,0.0000,1,''),(25,53,'CPDI - 11/15/2019','2019-11-15 17:24:37','CP Delivery',0.00,0.0000,1,''),(26,54,'','2020-01-08 23:31:16','Ground',0.00,0.0000,0,''),(27,55,'CPDI - 01/07/2020','2020-01-09 15:20:21','CP Delivery',0.00,0.0000,1,''),(28,56,'CPDI - 01/20/2020','2020-01-20 15:05:14','CP Delivery',0.00,0.0000,1,''),(29,57,'CPDI - 01/23/2020','2020-01-31 20:35:53','CP Delivery',0.00,0.0000,1,''),(30,58,'CPDI - 01/31/2020','2020-01-31 20:36:29','CP Delivery',0.00,0.0000,1,''),(31,59,'CPDI - 02/10/2020','2020-02-10 19:43:48','CP Delivery',0.00,0.0000,1,''),(32,60,'CPDI - 02/10/2020','2020-02-10 19:43:59','CP Delivery',0.00,0.0000,1,''),(33,61,'CPDI - 02/25/2020','2020-02-26 14:30:07','CP Delivery',0.00,0.0000,0,''),(34,62,'CPDI - 02/25/2020','2020-02-26 14:30:19','CP Delivery',0.00,0.0000,0,''),(35,63,'CPDI - 03/05/2020','2020-03-09 20:49:06','CP Delivery',0.00,0.0000,1,''),(36,64,'CPDI - 03/11/2020','2020-03-11 20:27:48','CP Delivery',0.00,0.0000,1,''),(37,65,'CPDI - 03/11/2020','2020-03-11 20:28:02','CP Delivery',0.00,0.0000,1,''),(38,66,'Deliver by CPDI by EOD Mar 23','2020-03-23 14:04:42','CP Delivery',0.00,0.0000,1,''),(39,67,'','2020-07-17 12:27:37','CP Delivery',0.00,0.0000,0,''),(40,68,'CPDI - 07/17/2020','2020-07-17 12:28:00','CP Delivery',0.00,0.0000,1,''),(41,69,'CP','2020-09-28 17:35:22','CP Delivery',0.00,0.0000,1,''),(42,70,'CPDI - 11/04/2020','2020-11-11 15:12:01','CP Delivery',0.00,0.0000,1,''),(43,71,'CPDI - 11/11/2020','2020-11-11 15:12:19','CP Delivery',0.00,0.0000,1,''),(44,72,'Client Pick-Up - Nov 18/2020','2020-11-26 16:20:28','Ground',0.00,0.0000,1,''),(45,73,'CPDI - 12/07/2020','2020-12-14 14:15:57','CP Delivery',0.00,0.0000,0,''),(46,74,'CPDI - 02/22/2021','2021-02-22 15:29:25','CP Delivery',0.00,0.0000,1,''),(47,75,'CPDI - 03/05/2021','2021-03-08 16:51:38','CP Delivery',0.00,0.0000,1,''),(48,77,'CPDI - 05/12/2021','2021-05-12 14:59:33','CP Delivery',0.00,0.0000,1,''),(49,78,'CPDI - 05/12/2021','2021-05-12 14:59:46','CP Delivery',0.00,0.0000,0,''),(50,79,'CPDI - 05/12/2021','2021-05-12 15:00:00','CP Delivery',0.00,0.0000,0,''),(51,80,'CPDI - 06/14/2021','2021-06-14 13:29:27','CP Delivery',0.00,0.0000,1,''),(52,81,'CPDI - 07/21/2021','2021-07-21 13:57:41','CP Delivery',0.00,0.0000,1,''),(53,82,'CPDI - 07/21/2021`','2021-07-21 13:57:54','CP Delivery',0.00,0.0000,1,''),(54,81,'NA','2021-11-19 17:51:19','CP Delivery',0.00,0.0000,1,''),(55,82,'NA','2021-11-19 17:51:38','CP Delivery',0.00,0.0000,1,''),(56,83,'NA','2021-11-19 17:51:55','CP Delivery',0.00,0.0000,1,''),(57,84,'NA','2021-11-19 17:52:09','CP Delivery',0.00,0.0000,1,''),(58,85,'NA','2021-11-19 17:52:23','CP Delivery',0.00,0.0000,1,''),(59,86,'NA','2021-11-19 17:52:38','CP Delivery',0.00,0.0000,1,''),(60,87,'NA','2021-11-19 17:52:57','CP Delivery',0.00,0.0000,1,''),(61,88,'NA','2021-11-19 17:53:06','CP Delivery',0.00,0.0000,1,''),(62,89,'NA','2021-11-19 17:53:17','CP Delivery',0.00,0.0000,1,''),(63,90,'NA','2021-11-19 17:53:27','CP Delivery',0.00,0.0000,1,''),(64,91,'NA','2021-12-23 17:11:26','CP Delivery',0.00,0.0000,1,''),(65,92,'NA','2022-01-10 15:45:33','CP Delivery',0.00,0.0000,1,''),(66,93,'NA','2022-02-07 18:08:52','CP Delivery',0.00,0.0000,1,''),(67,94,'NA','2022-02-07 18:09:12','CP Delivery',0.00,0.0000,1,''),(68,95,'NA','2022-02-07 18:09:32','CP Delivery',0.00,0.0000,1,''),(69,96,'NA','2022-02-07 18:09:53','CP Delivery',0.00,0.0000,1,''),(70,97,'NA','2022-03-11 17:31:30','CP Delivery',0.00,0.0000,1,''),(71,98,'cpdi','2022-05-02 13:45:38','CP Delivery',0.00,0.0000,1,''),(72,100,'NA','2022-08-08 11:01:31','CP Delivery',0.00,0.0000,1,''),(73,101,'NA','2022-10-28 16:09:13','CP Delivery',0.00,0.0000,1,''),(74,102,'NA','2022-10-28 16:09:27','CP Delivery',0.00,0.0000,1,''),(75,103,'NA','2022-10-28 16:09:39','CP Delivery',0.00,0.0000,1,''),(76,105,'NA','2022-11-11 14:00:10','Purolator Express',0.00,0.0000,1,''),(77,104,'NA','2022-12-09 12:10:24','CP Delivery',0.00,0.0000,1,''),(78,107,'NA','2023-02-08 14:31:51','CP Delivery',0.00,0.0000,1,'With 8 offline orders'),(79,108,'NA','2023-04-04 10:57:31','CP Delivery',0.00,0.0000,1,''),(80,110,'NA','2023-04-10 14:18:02','CP Delivery',0.00,0.0000,1,''),(81,109,'NA','2023-04-20 10:39:02','CP Delivery',0.00,0.0000,1,''),(82,111,'Delivery by CP','2023-04-26 13:57:48','CP Delivery',0.00,0.0000,1,''),(83,112,'NA','2023-05-03 10:40:01','CP Delivery',0.00,0.0000,1,''),(84,113,'NA','2023-05-15 17:53:34','CP Delivery',0.00,0.0000,1,'Delivered to 55 East Wilmot'),(85,114,'NA','2023-06-07 11:28:25','CP Delivery',0.00,0.0000,1,''),(86,115,'na','2023-06-07 11:28:47','CP Delivery',0.00,0.0000,0,''),(87,117,'na','2023-07-31 16:48:01','CP Delivery',0.00,0.0000,0,''),(88,118,'na','2023-07-31 16:48:20','CP Delivery',0.00,0.0000,1,''),(89,119,'NA','2023-08-31 13:36:33','CP Delivery',0.00,0.0000,1,''),(90,120,'NA','2023-09-25 12:39:07','CP Delivery',0.00,0.0000,1,''),(91,121,'na','2023-10-11 13:31:35','CP Delivery',0.00,0.0000,1,''),(92,122,'NA','2023-11-20 13:30:45','CP Delivery',0.00,0.0000,1,''),(93,123,'na','2024-02-05 10:33:48','CP Delivery',0.00,0.0000,0,''),(94,124,'na','2024-02-20 13:47:28','CP Delivery',0.00,0.0000,1,''),(95,125,'NA','2024-03-25 17:00:54','CP Delivery',0.00,0.0000,0,''),(96,126,'Delivery by CP','2024-07-05 11:44:25','CP Delivery',0.00,0.0000,1,''),(97,128,'NA','2024-07-30 12:06:41','CP Delivery',0.00,0.0000,1,''),(98,130,'NA','2024-07-30 12:06:53','CP Delivery',0.00,0.0000,2,''),(99,131,'NA','2024-08-08 13:48:29','CP Delivery',0.00,0.0000,2,''),(100,133,'NA','2024-08-26 12:45:52','CP Delivery',0.00,0.0000,1,''),(101,134,'NA','2024-09-23 15:54:09','Ground',0.00,0.0000,0,''),(102,136,'N/A','2024-10-03 14:52:54','CP Delivery',0.00,0.0000,1,''),(103,132,'','2024-10-07 12:51:49','CP Delivery',0.00,0.0000,0,''),(104,137,'NA','2024-10-16 10:59:15','CP Delivery',0.00,0.0000,1,''),(105,138,'NA','2024-10-16 10:59:26','CP Delivery',0.00,0.0000,1,'');

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

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

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


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

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

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


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

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

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

INSERT INTO `subcategories` VALUES (50,10,'CORP','Amico Corporation',-1,'icon_businesscard.png',0),(51,10,'AA','Accessories',-1,'icon_businesscard.png',0),(52,10,'ACA','Architectural Solutions',-1,'icon_businesscard.png',0),(53,10,'AB','Beds & Stretchers',-1,'icon_businesscard.png',0),(54,10,'ACS','Clinical Solutions',-1,'icon_businesscard.png',0),(56,10,'AIC','Interiors',-1,'icon_businesscard.png',0),(57,10,'ALT','Lighting Solutions',-1,'icon_businesscard.png',0),(58,10,'AMO','Mobility Solutions',-1,'icon_businesscard.png',0),(59,10,'ACP','Pipeline Equipment',-1,'icon_businesscard.png',0),(60,10,'AS','Source Equipment',-1,'icon_businesscard.png',0),(61,10,'APC','Patient Care Corporation',-1,'icon_businesscard.png',0),(62,10,'INTL','Medical Equipment',-1,'icon_businesscard.png',0),(63,10,'ACS & AMO','Clinical & Mobility Solutions',-1,'icon_businesscard.png',0);

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

CREATE TABLE `taxes` (
  `TAXCODE` varchar(6) NOT NULL,
  `TAXGROUPNAME` varchar(100) NOT NULL,
  `TAXNAME1` varchar(100) NOT NULL DEFAULT '',
  `TAXNAME2` varchar(100) NOT NULL DEFAULT '',
  `TAXNAME3` varchar(100) NOT NULL DEFAULT '',
  `TAXNAME4` varchar(100) NOT NULL DEFAULT '',
  `TAXNAME5` varchar(100) 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=utf8 COLLATE=utf8_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.15000,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(11) NOT NULL AUTO_INCREMENT,
  `TICKETID` int(11) DEFAULT NULL,
  `MESSAGEID` int(11) DEFAULT NULL,
  `STOREFILENAME` varchar(200) DEFAULT NULL,
  `ORIGFILENAME` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

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


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

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

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


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

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

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


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

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

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

INSERT INTO `user` VALUES ('amico','amico','*B828EF17A5E81CABB0F098DB7BDB95B83B832D5C','0800','Sonia','Yaqub','SYaqub@amico.com','EN',9,NULL,'2022-07-22 14:32:06',1,''),('demo','CPDI','*C142FB215B6E05B7C134B1A653AD4B455157FD79','demo','demo','demo','peggy@creativepath.com','EN',1,NULL,NULL,1,''),('eric','CPDI','*C5EC559C0EA7C49430B0006B94ACD3B9DD1D4F9D','E6359S','Eric','Sin','eric@creativepath.com','EN',81,'2020-03-26 16:08:45','2021-04-15 15:08:22',1,'CPDI'),('jen','CPDI','*1B971235F80D472237329258498BAF8F1D43BE0D','Hl5982','Jen','Lor','jennifer@cpfulfillment.com','EN',81,'2022-04-22 12:41:28','2022-04-22 17:02:06',1,'CPDI'),('miir','CPDI','*3B06CA329998DCF7B3AD9E850D9D3F120B0AC356','miir2280','Miranda','Lee','miranda@creativepath.com','EN',81,'2024-07-24 15:39:05','2024-07-24 15:39:24',1,'CPDI'),('patrick','CPDI','*04AE7ABE88B8C7F153AD654C1410AFFC6072339B','cpdi905711','Patrick','Sinn','patrick@creativepath.com','EN',99,'2017-01-21 20:14:11','2021-04-15 14:05:55',1,'CPDI'),('peggy','CPDI','*A45967DAC55BE870539FA0BA1B8AF799AD65EBB8','momo73449','Peggy','Cheng','peggy@creativepath.com','EN',99,'2017-01-21 20:14:11','2024-05-27 13:44:13',1,'CPDI'),('Sam','CPDI','*48910925EF9C280FA11348D3968E9F9B2E98E539','sam8500','Sam','Stratigeas','sam@samstratigeas.ca','EN',99,'2022-04-18 14:36:25','2022-04-18 14:36:46',1,'CPDI'),('zoeyc','CPDI','*C7C2E49F12068D26F3FB0709B11EBB4516B3BE98','zc5689','Zoey','C','zoeyc@cpfulfillment.com','EN',99,'2023-06-26 12:14:59','2023-06-26 12:15:22',1,'CPDI');

--
-- Dumping routines for database 'PrintOS_Amico'
--
/*!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`(UNAME VARCHAR(50), 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=CONCAT('*', UPPER(SHA1(UNHEX(SHA1(PASS))))) 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`(__TYPE INT, __CODE VARCHAR(50), __DESCRIPTION VARCHAR(200), __PRODUCEDETAIL VARCHAR(1000), __SIZE VARCHAR(50), __NUMPAGES INT, __PRINTINGTYPE VARCHAR(50), __STOCK VARCHAR(50), __STATUS INT, __MINLEVEL INT, __LEADTIME INT, __INITIALQTY INT, __DISPLAYNAME VARCHAR(1000), __PROOFNAME VARCHAR(1000), __PARAMETERSNAME VARCHAR(1000), __UPLOADDIR VARCHAR(1000), __PREVIEWNAME VARCHAR(1000), __SHIPBOXINSLEEVE INT, __SKIPPDFPERMISSIONCHECK INT, __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`(UNAME VARCHAR(50), __BRANCH VARCHAR(20), PASS VARCHAR(50), FNAME VARCHAR(50), LNAME VARCHAR(50), LANG VARCHAR(2), STAT INTEGER, USERLVL INTEGER, COMMENTS VARCHAR(1000), EMAIL VARCHAR(100))
BEGIN

INSERT INTO 
	`user` (`USERNAME`,`BRANCH`,`PASSWORD`,`PLAINPASSWORD`,`FIRSTNAME`,`LASTNAME`,`LANGUAGE`,`STATUS`,`USERLEVEL`,`COMMENTS`,`EMAIL`) 
VALUES 
  (UNAME,__BRANCH,CONCAT('*', UPPER(SHA1(UNHEX(SHA1(PASS))))),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,
			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;
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`(__STARTDATE DATE, __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 (__ENDDATE+1) 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`(__ITEMID INT, __TYPE INT, __CODE VARCHAR(50), __DESCRIPTION VARCHAR(200), __PRODUCEDETAIL VARCHAR(1000), __SIZE VARCHAR(50), __NUMPAGES INT, __PRINTINGTYPE VARCHAR(50), __STOCK VARCHAR(50), __STATUS INT, __MINLEVEL INT, __LEADTIME INT, __DISPLAYNAME VARCHAR(1000), __PROOFNAME VARCHAR(1000), __PARAMETERSNAME VARCHAR(1000), __UPLOADDIR VARCHAR(1000), __PREVIEWNAME VARCHAR(1000), __SHIPBOXINSLEEVE INT, __SKIPPDFPERMISSIONCHECK INT, __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`(UNAME VARCHAR(50), __BRANCH VARCHAR(20), PASS VARCHAR(50), FNAME VARCHAR(50), LNAME VARCHAR(50), LANG VARCHAR(2), STAT INTEGER, USERLVL INTEGER, COMMENTS VARCHAR(1000), EMAIL VARCHAR(100))
BEGIN

UPDATE
	`user`
SET
	`BRANCH` = __BRANCH, 
	`PASSWORD` = CONCAT('*', UPPER(SHA1(UNHEX(SHA1(PASS))))),
    `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`(__STARTDATE DATE, __ENDDATE DATE, __ACCESS BOOLEAN, __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,
        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,
        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`(__STARTDATE DATE, __ENDDATE DATE, __ACCESS BOOLEAN, __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,
        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));

END ;;
DELIMITER ;
/*!50003 DROP PROCEDURE IF EXISTS `ReportOrderSummary` */;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `ReportOrderSummary`(__STARTDATE DATE, __ENDDATE DATE, __ACCESS BOOLEAN, __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`(__SEARCH VARCHAR(1000), __SEARCHBY INT)
BEGIN
	SELECT
		ORDERID,
        ORDERREF,
        DATE(ORDERDATE) AS ORDERDATE,
        SHIPNAME,
        CONCAT(SHIPCONTACTFIRSTNAME, ' - ',SHIPADDRESS,' ',SHIPCITY,' ',SHIPPROVINCE) AS DESTINATION,
        DATE(EXPDATE) AS EXPDATE,
        ORDERSTATUS,
        COALESCE(PAYMENTREF,'') AS PAYMENTREF
	FROM
		orderHeader HDR
	WHERE
		(
			CONCAT(SHIPCONTACTFIRSTNAME, ' - ',SHIPADDRESS,' ',SHIPCITY,' ',SHIPPROVINCE) LIKE CONCAT('%',__SEARCH,'%') OR
            SHIPNAME LIKE CONCAT('%',__SEARCH,'%') OR 
            ORDERREF LIKE CONCAT('%',__SEARCH,'%') OR 
            ORDERDATE LIKE CONCAT('%',__SEARCH,'%') OR 
            EXPDATE LIKE CONCAT('%',__SEARCH,'%')
		) AND
        (
			(__SEARCHBY = 1 AND ORDERSTATUS = 'ACTIVE') OR 
            (__SEARCHBY = 2 AND ORDERSTATUS = 'ACTIVE' AND  NOW() >= DATE(EXPDATE) - INTERVAL 1 DAY) OR 
            (__SEARCHBY = 3 AND ORDERSTATUS = 'ACTIVE' AND  NOW() >= DATE(EXPDATE) - INTERVAL 2 DAY) OR
            (__SEARCHBY = 4 AND ORDERSTATUS = 'WAITMA') OR 
            (__SEARCHBY = 5 AND ORDERSTATUS = 'SHIPPED') OR
            (__SEARCHBY = 6 AND ORDERSTATUS = 'CANCELLED') OR 
            (__SEARCHBY = 7 AND ORDERSTATUS = 'REJECTED')
        )
	ORDER BY 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 ;


