CREATE TABLE IF NOT EXISTS `tblcard` (
  `CardID` varchar(255) NOT NULL DEFAULT '',
  `CustID` varchar(255) DEFAULT NULL,
  `CardBalance` double DEFAULT NULL,
  `CardDiscount` double DEFAULT NULL,
  `StartDate` date DEFAULT NULL,
  `ExpireDate` date DEFAULT NULL,
  `IsCreditReset` tinyint(1) DEFAULT NULL,
  `IsLimited` tinyint(1) DEFAULT NULL,
  `IsValid` tinyint(1) DEFAULT NULL,
  `IsDismiss` tinyint(1) DEFAULT NULL,
  `IssuedBy` varchar(255) DEFAULT NULL,
  `CardTypeID` varchar(255) DEFAULT '',
  `CardInfo` varchar(255) DEFAULT NULL,
  `PinNo` varchar(255) DEFAULT NULL,
  `IssuedDate` date DEFAULT NULL,
  `Point` double DEFAULT NULL,
  `LocationID` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`CardID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tblcard
-- 

INSERT INTO `tblcard`(`CardID`,`CustID`,`CardBalance`,`CardDiscount`,`StartDate`,`ExpireDate`,`IsCreditReset`,`IsLimited`,`IsValid`,`IsDismiss`,`IssuedBy`,`CardTypeID`,`CardInfo`,`PinNo`,`IssuedDate`,`Point`,`LocationID`) VALUES
('12312312','C00041',3242.22,0,'2018-08-02 00:00:00','2018-08-02 00:00:00',0,0,1,0,'ADMIN','Silver',NULL,'','2018-08-02 00:00:00',NULL,NULL),
('015955247','C00042',717.52,0,'2018-08-02 00:00:00','2018-08-02 00:00:00',0,0,1,0,'ADMIN','Silver',NULL,'','2018-08-02 00:00:00',NULL,NULL),
('0112222222','C00100',891.3,0,'2018-08-06 00:00:00','2018-08-06 00:00:00',0,0,1,0,'ADMIN','Silver',NULL,'','2018-08-06 00:00:00',NULL,NULL),
('0969767772','C00101',2540,0,'2018-08-09 00:00:00','2018-08-09 00:00:00',0,0,1,0,'ADMIN','Silver',NULL,'','2018-08-09 00:00:00',NULL,NULL),
('C00013','C00013',0,0,'2018-08-09 00:00:00','2018-08-09 00:00:00',0,0,1,0,'ADMIN','Silver',NULL,'','2018-08-09 00:00:00',NULL,NULL),
('C00001','C00001',0,0,'2018-08-09 00:00:00','2018-08-09 00:00:00',0,0,1,0,'ADMIN','Silver',NULL,'','2018-08-09 00:00:00',NULL,NULL),
('1231231231','C00102',0,0,'2018-08-13 00:00:00','2018-08-13 00:00:00',0,0,1,0,'ADMIN','Silver',NULL,'','2018-08-13 00:00:00',NULL,NULL);

-- 
-- Definition of tblcardtransactionhistory
-- 

CREATE TABLE IF NOT EXISTS `tblcardtransactionhistory` (
  `TransID` varchar(255) NOT NULL DEFAULT '',
  `CardID` varchar(255) NOT NULL DEFAULT '',
  `Date` datetime NOT NULL,
  `TransType` varchar(255) NOT NULL DEFAULT '',
  `Amount` double NOT NULL,
  `ToCardID` varchar(255) DEFAULT '',
  `SalID` varchar(255) DEFAULT '',
  `ModifiedBy` varchar(255) NOT NULL DEFAULT '',
  `ClientID` varchar(255) DEFAULT NULL,
  `LocationID` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`TransID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tblcardtransactionhistory
-- 

INSERT INTO `tblcardtransactionhistory`(`TransID`,`CardID`,`Date`,`TransType`,`Amount`,`ToCardID`,`SalID`,`ModifiedBy`,`ClientID`,`LocationID`) VALUES
('CT000010','0969767772','2018-08-09 09:30:05','Refill',10,'','','',NULL,NULL),
('CT000001','0112222222','2018-08-02 10:04:51','Payment',56.54,'','502AUG18-2C00041','',NULL,NULL),
('CT000002','0112222222','2018-08-02 10:04:58','Payment',56.54,'','502AUG18-2C00041','',NULL,NULL),
('CT000003','0112222222','2018-08-02 10:06:04','Refill',500,'','','',NULL,NULL),
('CT000004','0112222222','2018-08-02 10:06:12','Payment',444.7,'','502AUG18-1C00041','',NULL,NULL),
('CT000005','015955247','2018-08-06 10:55:42','Refill',1000,'','','',NULL,NULL),
('CT000006','015955247','2018-08-06 10:57:35','Payment',132.19,'','506AUG18-1C00042','',NULL,NULL),
('CT000007','015955247','2018-08-06 11:05:38','Payment',6,'','506AUG18-1C00042','',NULL,NULL),
('CT000008','015955247','2018-08-06 11:10:04','Payment',30,'','506AUG18-1C00042','',NULL,NULL),
('CT000009','015955247','2018-08-06 11:26:54','Payment',114.29,'','06AUG18-2C00042','',NULL,NULL),
('CT000011','0969767772','2018-08-09 09:32:38','Payment',6.84,'','409AUG18-2C00101','',NULL,NULL),
('CT000012','0969767772','2018-08-09 11:15:13','Refill',10000,'','','',NULL,NULL),
('CT000013','0969767772','2018-08-09 11:16:15','Payment',5416.61,'','409AUG18-4C00101','',NULL,NULL),
('CT000014','0969767772','2018-08-09 11:17:41','Payment',4322.93,'','409AUG18-5C00101','',NULL,NULL),
('CT000015','0969767772','2018-08-09 11:25:51','Refill',9736.38,'','','',NULL,NULL),
('CT000016','0969767772','2018-08-09 11:26:39','Payment',5416.61,'','409AUG18-4C00101','',NULL,NULL),
('CT000017','0969767772','2018-08-09 11:29:00','Payment',3410.24,'','409AUG18-5C00101','',NULL,NULL),
('CT000018','0969767772','2018-08-09 11:33:27','Payment',945.96,'','409AUG18-5C00101','',NULL,NULL),
('CT000019','0969767772','2018-08-09 11:35:32','Refill',2000,'','','',NULL,NULL),
('CT000020','0969767772','2018-08-09 11:35:45','Payment',1826.99,'','409AUG18-1C00101','',NULL,NULL),
('CT000021','0969767772','2018-08-09 11:44:41','Payment',99.99,'','409AUG18-6C00101','',NULL,NULL),
('CT000022','0969767772','2018-08-09 11:45:38','Refill',10000,'','','',NULL,NULL),
('CT000023','0969767772','2018-08-09 11:46:00','Payment',945.96,'','409AUG18-5C00101','',NULL,NULL),
('CT000024','0969767772','2018-08-09 11:51:12','Payment',100.41,'','409AUG18-7C00101','',NULL,NULL),
('CT000025','0969767772','2018-08-09 11:51:28','Payment',945.96,'','409AUG18-5C00101','',NULL,NULL),
('CT000026','0969767772','2018-08-09 11:55:22','Payment',945.96,'','409AUG18-5C00101','',NULL,NULL),
('CT000027','0969767772','2018-08-09 11:55:43','Payment',1826.99,'','409AUG18-1C00101','',NULL,NULL),
('CT000028','0969767772','2018-08-09 11:56:03','Payment',5416.61,'','409AUG18-4C00101','',NULL,NULL),
('CT000029','069606747','2018-08-09 12:03:37','Refill',3000,'','','',NULL,NULL),
('CT000030','0112222222','2018-08-09 12:04:03','Refill',3000,'','','',NULL,NULL),
('CT000031','0112222222','2018-08-09 12:04:58','Payment',2108.7,'','509AUG18-5C00100','',NULL,NULL),
('CT000032','0969767772','2018-08-09 12:14:01','Refill',331,'','','',NULL,NULL),
('CT000033','0969767772','2018-08-09 12:15:21','Refill',0.68,'','','',NULL,NULL),
('CT000034','0969767772','2018-08-09 12:16:09','Payment',450,'','409AUG18-2C00101','',NULL,NULL),
('CT000035','0969767772','2018-08-09 13:45:03','Refill',1000,'','','',NULL,NULL),
('CT000036','0969767772','2018-08-09 13:51:16','Refill',1500,'','','',NULL,NULL),
('CT000037','0969767772','2018-08-09 13:51:46','Refill',200,'','','',NULL,NULL),
('CT000038','0969767772','2018-08-09 13:52:36','Refill',1000,'','','',NULL,NULL),
('CT000039','0969767772','2018-08-09 13:52:52','Payment',2900,'','409AUG18-8C00101','',NULL,NULL),
('CT000040','0969767772','2018-08-09 13:57:26','Refund',580,'','409AUG18-8C00101','',NULL,NULL),
('CT000041','0969767772','2018-08-09 14:40:21','Refund',580,'','409AUG18-8C00101','',NULL,NULL),
('CT000042','0969767772','2018-08-09 14:44:40','Refund',580,'','409AUG18-8C00101','',NULL,NULL);

-- 
-- Definition of tblres_cardtype
-- 

CREATE TABLE IF NOT EXISTS `tblres_cardtype` (
  `SysCode` varchar(255) DEFAULT NULL,
  `CardTypeID` varchar(100) NOT NULL,
  `CardTypeName` varchar(100) NOT NULL,
  `Des` varchar(255) DEFAULT NULL,
  `Dol` double DEFAULT NULL,
  `Point` double DEFAULT NULL,
  `CardDiscPer` double DEFAULT NULL,
  `BeginAmt` double DEFAULT NULL,
  `IsAllItemDisc` tinyint(1) DEFAULT NULL,
  `IsAllItemPay` tinyint(1) DEFAULT NULL,
  `IsRefill` tinyint(1) DEFAULT NULL,
  `IsAdjustAmt` tinyint(1) DEFAULT NULL,
  `IsTransfer` tinyint(1) DEFAULT NULL,
  `IsRefund` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`CardTypeID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tblres_cardtype
-- 

INSERT INTO `tblres_cardtype`(`SysCode`,`CardTypeID`,`CardTypeName`,`Des`,`Dol`,`Point`,`CardDiscPer`,`BeginAmt`,`IsAllItemDisc`,`IsAllItemPay`,`IsRefill`,`IsAdjustAmt`,`IsTransfer`,`IsRefund`) VALUES
('MCT00001','premium','premium Card','premium',10,1,1,0,1,0,1,1,1,1),
('MCT00002','Silver','Silver Card','Silver',25,1,0,0,1,0,1,1,1,1),
('MCT00003','Gold','Gold Card','Gold',1,1,10,NULL,1,0,1,1,1,1),
('MCT00004','SuperGold','Super Gold Card','SuperGold',1,1,50,0,1,0,1,1,1,1),
('MCT00005','GMC','general Membership Card','GMC',1,1,0,NULL,1,1,1,1,1,1),
('MCT00007','VIP','VIP','VIP',1,1,0,0,1,0,1,1,1,1),
('MCT00008','MyCard','My Card','MyCard',1,1,0,600,1,0,1,1,1,1);

-- 
-- Definition of tm_booking
-- 

CREATE TABLE IF NOT EXISTS `tm_booking` (
  `booking_id` varchar(255) NOT NULL,
  `booking_date` datetime DEFAULT NULL,
  `tour_id` varchar(255) NOT NULL,
  `status_id` int(11) NOT NULL,
  `booking_inactive` tinyint(1) DEFAULT '1',
  `booking_description` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`booking_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_booking
-- 

INSERT INTO `tm_booking`(`booking_id`,`booking_date`,`tour_id`,`status_id`,`booking_inactive`,`booking_description`) VALUES
('BK00000000033','2018-07-12 00:00:00','TM-00000000004',3,1,''),
('BK00000000035','2018-07-25 00:00:00','TM-00000000003',1,1,''),
('BK00000000036','2018-07-11 00:00:00','BK00000000001',1,1,''),
('BK00000000038','2018-07-26 00:00:00','TM-00000000002',1,1,'');

-- 
-- Definition of tm_booking_detail
-- 

CREATE TABLE IF NOT EXISTS `tm_booking_detail` (
  `int_id` int(11) NOT NULL AUTO_INCREMENT,
  `var_id` varchar(100) NOT NULL,
  `booking_id` varchar(255) NOT NULL,
  `mode_id` int(11) NOT NULL,
  `company_id` int(11) NOT NULL,
  `flight_no` varchar(255) DEFAULT NULL,
  `dep_date` date DEFAULT NULL,
  `dep_time` time DEFAULT NULL,
  `dep_location` varchar(255) DEFAULT NULL,
  `arr_date` date DEFAULT NULL,
  `arr_time` time DEFAULT NULL,
  `arr_location` varchar(255) DEFAULT NULL,
  `flight_duration` int(11) DEFAULT NULL,
  `transit` varchar(255) DEFAULT NULL,
  `itemid` varchar(255) DEFAULT NULL,
  `net_amt` double(18,3) DEFAULT NULL,
  `air_line` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`int_id`)
) ENGINE=InnoDB AUTO_INCREMENT=87 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_booking_detail
-- 

INSERT INTO `tm_booking_detail`(`int_id`,`var_id`,`booking_id`,`mode_id`,`company_id`,`flight_no`,`dep_date`,`dep_time`,`dep_location`,`arr_date`,`arr_time`,`arr_location`,`flight_duration`,`transit`,`itemid`,`net_amt`,`air_line`) VALUES
(56,'BK-D00000000063','BK00000000035',3,6,'','2018-07-17 00:00:00','00:00:00','PP','2018-07-26 00:00:00','00:00:00','SR',12960,'','P00018',9,''),
(57,'BK-D00000000064','BK00000000036',3,5,'','2018-07-18 00:00:00','00:00:00','PP','2018-08-02 00:00:00','00:00:00','SR',21600,'','P00019',12,''),
(85,'BK-D00000000092','BK00000000033',1,5,'','2018-07-26 00:00:00','00:00:00','PP','2018-07-26 00:00:00','06:00:00','SR',360,'','P00020',9,''),
(86,'BK-D00000000093','BK00000000038',3,5,'','2018-07-16 00:00:00','00:00:00','PP','2018-07-25 00:00:00','00:00:00','SR',12960,'','P00017',9,'');

-- 
-- Definition of tm_booking_detail_uom
-- 

CREATE TABLE IF NOT EXISTS `tm_booking_detail_uom` (
  `int_id` int(11) NOT NULL AUTO_INCREMENT,
  `var_bm_id` varchar(100) NOT NULL,
  `var_bd_id` varchar(100) NOT NULL,
  `var_uom_id` varchar(100) NOT NULL,
  `dou_qty` double(18,0) DEFAULT NULL,
  `dou_price` double(18,3) DEFAULT '0.000',
  `dou_tax` double(5,2) DEFAULT NULL,
  `dou_amt` double(18,3) DEFAULT NULL,
  PRIMARY KEY (`int_id`)
) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_booking_detail_uom
-- 

INSERT INTO `tm_booking_detail_uom`(`int_id`,`var_bm_id`,`var_bd_id`,`var_uom_id`,`dou_qty`,`dou_price`,`dou_tax`,`dou_amt`) VALUES
(62,'BK00000000035','BK-D00000000063','Box',3,3,0,9),
(63,'BK00000000036','BK-D00000000064','Case',4,3,0,12),
(101,'BK00000000033','BK-D00000000092','Ea',3,3,0,9),
(102,'BK00000000038','BK-D00000000093','Box',3,3,0,9);

-- 
-- Definition of tm_company
-- 

CREATE TABLE IF NOT EXISTS `tm_company` (
  `company_id` int(11) NOT NULL AUTO_INCREMENT,
  `company_name` varchar(255) NOT NULL,
  `company_description` text,
  `company_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`company_id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_company
-- 

INSERT INTO `tm_company`(`company_id`,`company_name`,`company_description`,`company_inactive`) VALUES
(5,'Balancika','333',1),
(6,'Iphone','Ros Gold',1),
(7,'Samasong',NULL,1),
(8,'Angkor',NULL,1),
(9,'Leo',NULL,1),
(10,'Angkor',NULL,1),
(11,'Tomaco','',1);

-- 
-- Definition of tm_country
-- 

CREATE TABLE IF NOT EXISTS `tm_country` (
  `country_id` int(11) NOT NULL AUTO_INCREMENT,
  `country_name` varchar(255) NOT NULL,
  `country_description` varchar(255) DEFAULT NULL,
  `country_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`country_id`)
) ENGINE=InnoDB AUTO_INCREMENT=265 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_country
-- 

INSERT INTO `tm_country`(`country_id`,`country_name`,`country_description`,`country_inactive`) VALUES
(1,'Afghanistan',NULL,1),
(2,'Albania',NULL,1),
(3,'Algeria',NULL,1),
(4,'American Samoa',NULL,1),
(5,'Andorra',NULL,1),
(6,'Angola',NULL,1),
(7,'Anguilla',NULL,1),
(8,'Antarctica',NULL,1),
(9,'Antigua and Barbuda',NULL,1),
(10,'Argentina',NULL,1),
(11,'Armenia',NULL,1),
(12,'Aruba',NULL,1),
(13,'Australia',NULL,1),
(14,'Austria',NULL,1),
(15,'Azerbaijan',NULL,1),
(16,'Bahamas',NULL,1),
(17,'Bahrain',NULL,1),
(18,'Bangladesh',NULL,1),
(19,'Barbados',NULL,1),
(20,'Belarus',NULL,1),
(21,'Belgium',NULL,1),
(22,'Belize',NULL,1),
(23,'Benin',NULL,1),
(24,'Bermuda',NULL,1),
(25,'Bhutan',NULL,1),
(26,'Bolivia',NULL,1),
(27,'Bosnia and Herzegovina',NULL,1),
(28,'Botswana',NULL,1),
(29,'Bouvet Island',NULL,1),
(30,'Brazil',NULL,1),
(31,'British Antarctic Territory',NULL,1),
(32,'British Indian Ocean Territory',NULL,1),
(33,'British Virgin Islands',NULL,1),
(34,'Brunei',NULL,1),
(35,'Bulgaria',NULL,1),
(36,'Burkina Faso',NULL,1),
(37,'Burundi',NULL,1),
(38,'Cambodia',NULL,1),
(39,'Cameroon',NULL,1),
(40,'Canada',NULL,1),
(41,'Canton and Enderbury Islands',NULL,1),
(42,'Cape Verde',NULL,1),
(43,'Cayman Islands',NULL,1),
(44,'Central African Republic',NULL,1),
(45,'Chad',NULL,1),
(46,'Chile',NULL,1),
(47,'China',NULL,1),
(48,'Christmas Island',NULL,1),
(49,'Cocos [Keeling] Islands',NULL,1),
(50,'Colombia',NULL,1),
(51,'Comoros',NULL,1),
(52,'Congo - Brazzaville',NULL,1),
(53,'Congo - Kinshasa',NULL,1),
(54,'Cook Islands',NULL,1),
(55,'Costa Rica',NULL,1),
(56,'Croatia',NULL,1),
(57,'Cuba',NULL,1),
(58,'Cyprus',NULL,1),
(59,'Czech Republic',NULL,1),
(60,'Côte d’Ivoire',NULL,1),
(61,'Denmark',NULL,1),
(62,'Djibouti',NULL,1),
(63,'Dominica',NULL,1),
(64,'Dominican Republic',NULL,1),
(65,'Dronning Maud Land',NULL,1),
(66,'East Germany',NULL,1),
(67,'Ecuador',NULL,1),
(68,'Egypt',NULL,1),
(69,'El Salvador',NULL,1),
(70,'Equatorial Guinea',NULL,1),
(71,'Eritrea',NULL,1),
(72,'Estonia',NULL,1),
(73,'Ethiopia',NULL,1),
(74,'Falkland Islands',NULL,1),
(75,'Faroe Islands',NULL,1),
(76,'Fiji',NULL,1),
(77,'Finland',NULL,1),
(78,'France',NULL,1),
(79,'French Guiana',NULL,1),
(80,'French Polynesia',NULL,1),
(81,'French Southern Territories',NULL,1),
(82,'French Southern and Antarctic Territories',NULL,1),
(83,'Gabon',NULL,1),
(84,'Gambia',NULL,1),
(85,'Georgia',NULL,1),
(86,'Germany',NULL,1),
(87,'Ghana',NULL,1),
(88,'Gibraltar',NULL,1),
(89,'Greece',NULL,1),
(90,'Greenland',NULL,1),
(91,'Grenada',NULL,1),
(92,'Guadeloupe',NULL,1),
(93,'Guam',NULL,1),
(94,'Guatemala',NULL,1),
(95,'Guernsey',NULL,1),
(96,'Guinea',NULL,1),
(97,'Guinea-Bissau',NULL,1),
(98,'Guyana',NULL,1),
(99,'Haiti',NULL,1),
(100,'Heard Island and McDonald Islands',NULL,1),
(101,'Honduras',NULL,1),
(102,'Hong Kong SAR China',NULL,1),
(103,'Hungary',NULL,1),
(104,'Iceland',NULL,1),
(105,'India',NULL,1),
(106,'Indonesia',NULL,1),
(107,'Iran',NULL,1),
(108,'Iraq',NULL,1),
(109,'Ireland',NULL,1),
(110,'Isle of Man',NULL,1),
(111,'Israel',NULL,1),
(112,'Italy',NULL,1),
(113,'Jamaica',NULL,1),
(114,'Japan',NULL,1),
(115,'Jersey',NULL,1),
(116,'Johnston Island',NULL,1),
(117,'Jordan',NULL,1),
(118,'Kazakhstan',NULL,1),
(119,'Kenya',NULL,1),
(120,'Kiribati',NULL,1),
(121,'Kuwait',NULL,1),
(122,'Kyrgyzstan',NULL,1),
(123,'Laos',NULL,1),
(124,'Latvia',NULL,1),
(125,'Lebanon',NULL,1),
(126,'Lesotho',NULL,1),
(127,'Liberia',NULL,1),
(128,'Libya',NULL,1),
(129,'Liechtenstein',NULL,1),
(130,'Lithuania',NULL,1),
(131,'Luxembourg',NULL,1),
(132,'Macau SAR China',NULL,1),
(133,'Macedonia',NULL,1),
(134,'Madagascar',NULL,1),
(135,'Malawi',NULL,1),
(136,'Malaysia',NULL,1),
(137,'Maldives',NULL,1),
(138,'Mali',NULL,1),
(139,'Malta',NULL,1),
(140,'Marshall Islands',NULL,1),
(141,'Martinique',NULL,1),
(142,'Mauritania',NULL,1),
(143,'Mauritius',NULL,1),
(144,'Mayotte',NULL,1),
(145,'Metropolitan France',NULL,1),
(146,'Mexico',NULL,1),
(147,'Micronesia',NULL,1),
(148,'Midway Islands',NULL,1),
(149,'Moldova',NULL,1),
(150,'Monaco',NULL,1),
(151,'Mongolia',NULL,1),
(152,'Montenegro',NULL,1),
(153,'Montserrat',NULL,1),
(154,'Morocco',NULL,1),
(155,'Mozambique',NULL,1),
(156,'Myanmar [Burma]',NULL,1),
(157,'Namibia',NULL,1),
(158,'Nauru',NULL,1),
(159,'Nepal',NULL,1),
(160,'Netherlands',NULL,1),
(161,'Netherlands Antilles',NULL,1),
(162,'Neutral Zone',NULL,1),
(163,'New Caledonia',NULL,1),
(164,'New Zealand',NULL,1),
(165,'Nicaragua',NULL,1),
(166,'Niger',NULL,1),
(167,'Nigeria',NULL,1),
(168,'Niue',NULL,1),
(169,'Norfolk Island',NULL,1),
(170,'North Korea',NULL,1),
(171,'North Vietnam',NULL,1),
(172,'Northern Mariana Islands',NULL,1),
(173,'Norway',NULL,1),
(174,'Oman',NULL,1),
(175,'Pacific Islands Trust Territory',NULL,1),
(176,'Pakistan',NULL,1),
(177,'Palau',NULL,1),
(178,'Palestinian Territories',NULL,1),
(179,'Panama',NULL,1),
(180,'Panama Canal Zone',NULL,1),
(181,'Papua New Guinea',NULL,1),
(182,'Paraguay',NULL,1),
(183,'People''s Democratic Republic of Yemen',NULL,1),
(184,'Peru',NULL,1),
(185,'Philippines',NULL,1),
(186,'Pitcairn Islands',NULL,1),
(187,'Poland',NULL,1),
(188,'Portugal',NULL,1),
(189,'Puerto Rico',NULL,1),
(190,'Qatar',NULL,1),
(191,'Romania',NULL,1),
(192,'Russia',NULL,1),
(193,'Rwanda',NULL,1),
(194,'Réunion',NULL,1),
(195,'Saint Barthélemy',NULL,1),
(196,'Saint Helena',NULL,1),
(197,'Saint Kitts and Nevis',NULL,1),
(198,'Saint Lucia',NULL,1),
(199,'Saint Martin',NULL,1),
(200,'Saint Pierre and Miquelon',NULL,1),
(201,'Saint Vincent and the Grenadines',NULL,1),
(202,'Samoa',NULL,1),
(203,'San Marino',NULL,1),
(204,'Saudi Arabia',NULL,1),
(205,'Senegal',NULL,1),
(206,'Serbia',NULL,1),
(207,'Serbia and Montenegro',NULL,1),
(208,'Seychelles',NULL,1),
(209,'Sierra Leone',NULL,1),
(210,'Singapore',NULL,1),
(211,'Slovakia',NULL,1),
(212,'Slovenia',NULL,1),
(213,'Solomon Islands',NULL,1),
(214,'Somalia',NULL,1),
(215,'South Africa',NULL,1),
(216,'South Georgia and the South Sandwich Islands',NULL,1),
(217,'South Korea',NULL,1),
(218,'Spain',NULL,1),
(219,'Sri Lanka',NULL,1),
(220,'Sudan',NULL,1),
(221,'Suriname',NULL,1),
(222,'Svalbard and Jan Mayen',NULL,1),
(223,'Swaziland',NULL,1),
(224,'Sweden',NULL,1),
(225,'Switzerland',NULL,1),
(226,'Syria',NULL,1),
(227,'São Tomé and Príncipe',NULL,1),
(228,'Taiwan',NULL,1),
(229,'Tajikistan',NULL,1),
(230,'Tanzania',NULL,1),
(231,'Thailand',NULL,1),
(232,'Timor-Leste',NULL,1),
(233,'Togo',NULL,1),
(234,'Tokelau',NULL,1),
(235,'Tonga',NULL,1),
(236,'Trinidad and Tobago',NULL,1),
(237,'Tunisia',NULL,1),
(238,'Turkey',NULL,1),
(239,'Turkmenistan',NULL,1),
(240,'Turks and Caicos Islands',NULL,1),
(241,'Tuvalu',NULL,1),
(242,'U.S. Minor Outlying Islands',NULL,1),
(243,'U.S. Miscellaneous Pacific Islands',NULL,1),
(244,'U.S. Virgin Islands',NULL,1),
(245,'Uganda',NULL,1),
(246,'Ukraine',NULL,1),
(247,'Union of Soviet Socialist Republics',NULL,1),
(248,'United Arab Emirates',NULL,1),
(249,'United Kingdom',NULL,1),
(250,'United States',NULL,1),
(251,'Unknown or Invalid Region',NULL,1),
(252,'Uruguay',NULL,1),
(253,'Uzbekistan',NULL,1),
(254,'Vanuatu',NULL,1),
(255,'Vatican City',NULL,1),
(256,'Venezuela',NULL,1),
(257,'Vietnam',NULL,1),
(258,'Wake Island',NULL,1),
(259,'Wallis and Futuna',NULL,1),
(260,'Western Sahara',NULL,1),
(261,'Yemen',NULL,1),
(262,'Zambia',NULL,1),
(263,'Zimbabwe',NULL,1),
(264,'Åland Islands',NULL,1);

-- 
-- Definition of tm_destination
-- 

CREATE TABLE IF NOT EXISTS `tm_destination` (
  `destination_id` int(11) NOT NULL AUTO_INCREMENT,
  `destination_name` varchar(255) NOT NULL,
  `destination_description` varchar(255) DEFAULT NULL,
  `country_id` int(11) NOT NULL,
  `destination_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`destination_id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_destination
-- 

INSERT INTO `tm_destination`(`destination_id`,`destination_name`,`destination_description`,`country_id`,`destination_inactive`) VALUES
(1,'Tokyo',NULL,114,1),
(2,'Yanagawa',NULL,114,1),
(3,'Okawa',NULL,114,1),
(4,'Bangkork',NULL,231,1),
(5,'Chiang Mai',NULL,231,1),
(6,'Pataya',NULL,231,1),
(7,'Barcelona',NULL,218,1),
(8,'Madrid',NULL,218,1),
(9,'Siemreap',NULL,38,1),
(10,'Sihaknouk',NULL,38,1),
(11,'Khos Kong',NULL,38,1),
(12,'Battombong','',38,1),
(13,'Kompong Cham','Turain',38,1);

-- 
-- Definition of tm_language_object
-- 

CREATE TABLE IF NOT EXISTS `tm_language_object` (
  `LangCode` int(11) NOT NULL AUTO_INCREMENT,
  `EN` varchar(255) DEFAULT NULL,
  `KH` varchar(255) DEFAULT NULL,
  `CN` varchar(255) DEFAULT NULL,
  `Description` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`LangCode`)
) ENGINE=InnoDB AUTO_INCREMENT=234 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_language_object
-- 

INSERT INTO `tm_language_object`(`LangCode`,`EN`,`KH`,`CN`,`Description`) VALUES
(1,'Login Company','ការចូលប្រើប្រាស់','',''),
(2,'UserName','ឈ្មោះ​អ្នកប្រើប្រាស់',NULL,NULL),
(3,'Password','លេខសំងាត់',NULL,NULL),
(4,'Log in','ចូល',NULL,NULL),
(5,'You have been log out successfully.','អ្នកបានចាកចេញដោយជោគជ័យ',NULL,NULL),
(6,'Invalid username and password!','ពត័មានមននត្រឹមត្រូវ',NULL,NULL),
(7,'The username is required and can not be empty!','ឈ្មោះអ្នកប្រើប្រាស់តម្រូវឱ្យបំពេញ​ ហើយមននអាចទទេ!',NULL,NULL),
(8,'The password is required and can not be empty!','លេខសម្ងាត់តម្រូវឱ្យបំពេញ ហើយមិនអាចទទេ!',NULL,NULL),
(9,'The company is required and can not be empty!','ក្រុមហ៊ុនតម្រូវឱ្យបំពេញ ហើយមិនអាចទទេ!',NULL,NULL),
(10,'You have no permission! Please contact your admistrator!','អ្នកគ្មានសិទ្ធក្នុងការប្រើប្រាស់! សូមទាក់ទងទៅប្រធានរបស់អ្នកដើម្បីទទួលបានព៍តមាន!',NULL,NULL),
(11,'Back to List','ត្រលប់ក្រោយ',NULL,NULL),
(12,'Welcome','សួស្ដី',NULL,NULL),
(13,'Sign Out','ចាក់ចេញ',NULL,NULL),
(14,'Save','រក្សាទុក',NULL,NULL),
(15,'Clear','លុប',NULL,NULL),
(16,'Back to List','ត្រលប់ក្រោយ',NULL,NULL),
(17,'Name','ឈ្មោះ',NULL,NULL),
(18,'Description','ពណ៌នា',NULL,NULL),
(19,'ID','លេខសម្គាល់',NULL,NULL),
(20,'Action','សកម្មភាព',NULL,NULL),
(21,'Country Name','ឈ្មោះប្រទេស',NULL,NULL),
(22,'Edit Tour Type','កែប្រែពត៌មាន',NULL,NULL),
(23,'Close','បិទ',NULL,NULL),
(24,'Company Name','ឈ្មោះក្រុមហ៊ុន',NULL,NULL),
(25,'Edit Country','កែប្រែពត៌មានប្រទេស',NULL,NULL),
(26,'Edit Company','កែប្រែពត៌មានក្រុមហ៊ុន',NULL,NULL),
(27,'Season','រដូវកាល',NULL,NULL),
(28,'Edit Season','កែប្រែពត៌មានរដូវកាល',NULL,NULL),
(29,'Mode Transport','របៀបដឹកជញ្ជូន',NULL,NULL),
(30,'Edit Mode Transport','កែប្រែពត៌មានដឹកជញ្ជូន',NULL,NULL),
(31,'Status Name','ស្ថានភាព',NULL,NULL),
(32,'Edit Status','កែប្រែស្ថានភាព',NULL,NULL),
(33,'Progress','វឌ្ឍនភាព',NULL,NULL),
(34,'Edit Progress','កែប្រែវឌ្ឍនភាព',NULL,NULL),
(35,'Room Type','ប្រភេទបន្ទប់',NULL,NULL),
(36,'Edit Room Type','កែប្រែពត៌មានបន្ទប់',NULL,NULL),
(37,'Insert Successfully','ការបញ្ចូលជោគជ័យ',NULL,NULL),
(38,'Successful','ជោគជ័យ',NULL,NULL),
(39,'Unsuccessful','បរាជ័យ',NULL,NULL),
(40,'Fail To Insert','ការបញ្ចូលបរាជ័យ',NULL,NULL),
(41,'Update Successfully','កែប្រែបានជោគជ័យ',NULL,NULL),
(42,'Fail to Update','កែប្រែបរាជ័យ',NULL,NULL),
(43,'Delete Successfully','លុបបានជោគជ័យ',NULL,NULL),
(44,'Fail to Delete','ការលុបបរាជ័យ',NULL,NULL),
(45,'You are about to create type.','អ្នកនឹងស្នើរក្នុងការបង្កើតប្រភេទទេសចរណ៍',NULL,NULL),
(46,'Click OK to continue or CANCEL to abort.','ចុចយល់ព្រម ដើម្បីបន្តដំណើរការ ឬចុចបោះបង់ ដើម្បីបញ្ឈប់ដំណើរការ',NULL,NULL),
(47,'OK','យល់ព្រម',NULL,NULL),
(48,'Cancel','បោះបង់',NULL,NULL),
(49,'You are about to Update type.','អ្នកនឹងស្នើរក្នុងការកែប្រែប្រភេទទេសចរណ៍',NULL,NULL),
(50,'You are about to delete type.','អ្នកនឹងស្នើរក្នុងការលុបប្រភេទទេសចរណ៍',NULL,NULL),
(51,'You are about to create Company.','អ្នកនឹងស្នើរក្នុងការបង្កើតក្រុមហ៊ុន',NULL,NULL),
(52,'You are about to Update Company.','អ្នកនឹងស្នើរក្នុងការកែប្រែពត៌មានក្រុមហ៊ុន',NULL,NULL),
(53,'You are about to Delete Company.','អ្នកនឹងស្នើរក្នុងការលុបក្រុមហ៊ុន',NULL,NULL),
(54,'You are about to create Season.','អ្នកនឹងស្នើរក្នុងការបង្កើតរដូវកាល',NULL,NULL),
(55,'You are about to update Season.','អ្នកនឹងស្នើរក្នុងការកែប្រែពត៌មានរដូវកាល',NULL,NULL),
(56,'You are about to delete Season.','អ្នកនឹងស្នើរក្នុងការលុបរដូវកាល',NULL,NULL),
(57,'You are about to create Transport.','អ្នកនឹងស្នើរក្នុងការបង្កើតមធ្យោបាយធ្វើដំណើរ',NULL,NULL),
(58,'You are about to update Transport.','អ្នកនឹងស្នើរក្នុងការកែប្រែពត៌មានមធ្យោបាយធ្វើដំណើរ',NULL,NULL),
(59,'You are about to delete Transport.','អ្នកនឹងស្នើរក្នុងការលុបមធ្យោបាយធ្វើដំណើរ',NULL,NULL),
(60,'You are about to create OP Status.','អ្នកនឹងស្នើរក្នុងការបង្កើតស្ថានភាពប្រតិបត្តិការ',NULL,NULL),
(61,'You are about to update OP Status','អ្នកនឹងស្នើរក្នុងការកែប្រែស្ថានភាពប្រតិបត្តិការ',NULL,NULL),
(62,'You are about to delete OP Status','អ្នកនឹងស្នើរក្នុងការលុបស្ថានភាពប្រតិបត្តិការ',NULL,NULL),
(63,'You are about to create Progress.','អ្នកនឹងស្នើរក្នុងការបង្កើតវឌ្ឍនភាព',NULL,NULL),
(64,'You are about to update progress.','អ្នកនឹងស្នើរក្នុងការកែប្រែវឌ្ឍនភាព',NULL,NULL),
(65,'You are about to delete progress.','អ្នកនឹងស្នើរក្នុងការលុបវឌ្ឍនភាព',NULL,NULL),
(66,'You are about to create Room Type.','អ្នកនឹងស្នើរក្នុងការបង្កើតប្រភេទបន្ទប់',NULL,NULL),
(67,'You are about to update Room Type.','អ្នកនឹងស្នើរក្នុងការកែប្រែប្រភេទបន្ទប់',NULL,NULL),
(68,'You are about to delete Room Type.','អ្នកនឹងស្នើរក្នុងការលុបប្រភេទបន្ទប់',NULL,NULL),
(69,'You are about to create Status.','អ្នកនឹងស្នើរក្នុងការបង្កើតស្ថានភាព',NULL,NULL),
(70,'You are about to update Status.','អ្នកនឹងស្នើរក្នុងការកែប្រែស្ថានភាព',NULL,NULL),
(71,'You are about to delete Status.','អ្នកនឹងស្នើរក្នុងការលុបស្ថានភាព',NULL,NULL),
(72,'Destination','គោលដៅទេសចរណ៍',NULL,NULL),
(73,'You are about to create Destination.','អ្នកនឹងស្នើរក្នុងការបង្កើតគោលដៅទេសចរណ៍',NULL,NULL),
(74,'You are about to update Destination.','អ្នកនឹងស្នើរក្នុងការកែប្រែគោលដៅទេសចរណ៍',NULL,NULL),
(75,'You are about to delete Destination.','អ្នកនឹងស្នើរក្នុងការលុបគោលដៅទេសចរណ៍',NULL,NULL),
(76,'Search','ស្វែងរក',NULL,NULL),
(78,'Create','បង្កើត',NULL,NULL),
(79,'Report','របាយការណ៍',NULL,NULL),
(80,'Setting','ការកំណត់',NULL,NULL),
(81,'Filter','តម្រង',NULL,NULL),
(82,'Reset','កំណត់ឡើងវិញ',NULL,NULL),
(83,'Close','បិទ',NULL,NULL),
(84,'Dashboard','ផ្ទាំងគ្រប់គ្រង','儀表板',NULL),
(85,'Tour','ដំណើរទស្សនកិច្ច',NULL,NULL),
(86,'List Tours','បញ្ជីទេសចរណ៍',NULL,NULL),
(87,'Booking','ការកក់',NULL,NULL),
(88,'List Bookings','បញ្ជីកក់',NULL,NULL),
(89,'Quotation','សម្រង់',NULL,NULL),
(90,'List Quotations','បញ្ជីសម្រង់',NULL,NULL),
(91,'Pricing','តម្លៃ',NULL,NULL),
(92,'List Pricing','បញ្ជីតម្លៃ',NULL,NULL),
(93,'OP Progress','វឌ្ឍនភាពឱកាស',NULL,NULL),
(94,'List Progress','បញ្ជីឪកាស',NULL,NULL),
(95,'Rooming Lists','កក់បន្ទប់',NULL,NULL),
(96,'List Rooming','បញ្ជីបន្ទប់',NULL,NULL),
(97,'Administrator','គ្រប់គ្រង',NULL,NULL),
(98,'Booking Date','ថ្ងែកក់',NULL,NULL),
(99,'Tour','ទេសចរណ៍',NULL,NULL),
(100,'Status','ស្ថានភាព',NULL,NULL),
(101,'Season','រដូវកាល',NULL,NULL),
(102,'Mode','របៀបធ្វើដំណើរ',NULL,NULL),
(103,'Company','ក្រុមហ៊ុន',NULL,NULL),
(104,'Quantity','បរិមាណ',NULL,NULL),
(105,'Price','តម្លៃ',NULL,NULL),
(106,'Tax','ពន្ធ',NULL,NULL),
(107,'Amount','តម្លៃសរុប',NULL,NULL),
(108,'Net Amount','តម្លៃត្រូវបង់',NULL,NULL),
(109,'Number Prefix','បុព្វបទលេខ',NULL,NULL),
(110,'Category Name','ឈ្មោះប្រភេទ',NULL,NULL),
(111,'You are about to create Category.','អ្នកនឹងស្នើរក្នុងការបង្កើតប្រភេទទេសចរណ៍',NULL,NULL),
(112,'You are about to update Category.','អ្នកនឹងស្នើរក្នុងការកែប្រែប្រភេទទេសចរណ៍',NULL,NULL),
(113,'You are about to delete Category.','អ្នកនឹងស្នើរក្នុងការលុបប្រភេទទេសចរណ៍',NULL,NULL),
(114,'You are about to create Number Prefix','អ្នកនឹងស្នើរក្នុងការលុបបុព្វបទលេខ',NULL,NULL),
(115,'You are about to update Number Prefix','អ្នកនឹងស្នើរក្នុងការកែប្រែបុព្វបទលេខ',NULL,NULL),
(116,'You are about to delete Number Prefix','អ្នកនឹងស្នើរក្នុងការលុបបុព្វបទលេខ',NULL,NULL),
(117,'Remark','កត់សំគាល់',NULL,NULL),
(118,'Prefix','បុព្វបទ',NULL,NULL),
(119,'Last ID','លេខសម្គាល់ចុងក្រោយ',NULL,NULL),
(120,'Status','ស្ថានភាព',NULL,NULL),
(121,'No.','លេខរៀង',NULL,NULL),
(122,'Length','ចំនួនតួអក្សរ',NULL,NULL),
(123,'The ID is required and cannot be empty!','លេខសម្គាល់គឺតម្រូវឪ្យបំពេញ និងមិនអាចទទេ',NULL,NULL),
(124,'The Remark is required and cannot be empty!','ការកត់សំគាល់គឺតម្រូវឪ្យបំពេញ និងមិនអាចទទេ',NULL,NULL),
(125,'The Prefix is required and cannot be empty!','បុព្វបទគឺតម្រូវឪ្យបំពេញ និងមិនអាចទទេ',NULL,NULL),
(126,'The Length is required and cannot be empty','ចំនួនតួអក្សរគឺតម្រូវឪ្យបំពេញ និងមិនអាចទទេ',NULL,NULL),
(127,'The Length must be more than 1 and less than or equal 11 characters long','ចំនួនតួអក្សរត្រូវតែធំជាង ១ និងតិចជាងឫស្មើ ១១តួ',NULL,NULL),
(128,'The Last ID is required and cannot be empty','លេខសម្គាល់ចុងក្រោយគឺតម្រូវឪ្យបំពេញ និងមិនអាចទទេ',NULL,NULL),
(129,'The Last ID must be more than 1','លេខសម្គាល់ចុងក្រោយត្រូវតែធំជាង ១តួ',NULL,NULL),
(130,'Module ID','លេខសម្គាល់ម៉ូឌុល',NULL,NULL),
(131,'Type','ប្រភេទ',NULL,NULL),
(132,'The Type is required and cannot be empty!','ប្រភេទគឺតម្រូវឪ្យបំពេញ និងមិនអាចទទេ',NULL,NULL),
(133,'Code','កូដ',NULL,NULL),
(134,'Country','ប្រទេស',NULL,NULL),
(135,'Start Date','ថ្ងៃចាប់ផ្តើម',NULL,NULL),
(136,'End Date','ថ្ងៃបញ្ចប់',NULL,NULL),
(137,'Booking Date','ថ្ងៃកក់',NULL,NULL),
(138,'Category','ប្រភេទ',NULL,NULL),
(139,'Settings','ការកំណត់',NULL,NULL),
(140,'Module','ម៉ូឌុល',NULL,NULL),
(141,'value','តម្លៃ',NULL,NULL),
(142,'Back','ថយក្រោយ',NULL,NULL),
(143,'Forward','ទៅមក',NULL,NULL),
(144,'Required','ទាមទារ',NULL,NULL),
(145,'Tour ID','លេខសម្គាល់',NULL,NULL),
(146,'Tour Code','កូដទេសចរណ៍',NULL,NULL),
(147,'UOMs','ឯកតារង្វាស់',NULL,NULL),
(148,'SELECT Prefix','ជ្រើសរើសបុព្វបទលេខ',NULL,NULL),
(149,'Tour Subject','ឈ្មោះទេសចរណ៍',NULL,NULL),
(150,'SELECT Season','ជ្រើសរើសរដូវកាល',NULL,NULL),
(151,'SELECT Status','ជ្រើសរើសស្ថានភាព',NULL,NULL),
(152,'Information','ព័ត៌មាន',NULL,NULL),
(153,'Units of Measure','ឯកតារង្វាស់',NULL,NULL),
(154,'Year','ឆ្នាំ',NULL,NULL),
(155,'Month','ខែ',NULL,NULL),
(156,'Tour Leader','អ្នកដឹកនាំទេសចរណ៍',NULL,NULL),
(157,'SELECT Tour Leader','ជ្រើសរើសអ្នកដឹកនាំទេសចរណ៍',NULL,NULL),
(158,'Visa Deadline','ថ្ងៃផុតកំណត់ទិដ្ឋាការ',NULL,NULL),
(159,'Travel Instruction','ការណែនាំអំពីការធ្វើដំណើរ',NULL,NULL),
(160,'SELECT Country','ជ្រើសរើសប្រទេស',NULL,NULL),
(161,'SELECT UOM','ជ្រើសរើសឯកតារង្វាស់',NULL,NULL),
(162,'Template','គំរូ',NULL,NULL),
(163,'Reference','ឯកសារយោង',NULL,NULL),
(164,'Customer','អតិថិជន',NULL,NULL),
(165,'Tel.','លេខទូរស័ព្ទ',NULL,NULL),
(166,'Booking Date','ថ្ងៃកក់',NULL,NULL),
(167,'Information Tour','ពត៌មានទេសចរណ៍',NULL,NULL),
(168,'Select Tour','ជ្រើសរើសទេសចរណ៍',NULL,NULL),
(169,'Select Status','ជ្រើសរើសស្ថានភាព',NULL,NULL),
(170,'Tour Leader','អ្នកដឹកនាំ',NULL,NULL),
(171,'DEPT Date','ថ្ងៃមកដល់',NULL,NULL),
(172,'Arrive Date','ថ្ងៃទៅដល់',NULL,NULL),
(173,'Air Line','ផ្លូវអាកាស',NULL,NULL),
(174,'Flight No','ជើងហោះហើរ',NULL,NULL),
(175,'From','ចេញពី',NULL,NULL),
(176,'To','ទៅដល់',NULL,NULL),
(177,'DEPT Time','ម៉ោងមកដល់',NULL,NULL),
(178,'Arrive Time','ម៉ោងទៅដល់',NULL,NULL),
(179,'Price','តម្លៃ',NULL,NULL),
(180,'Please choose tour','សូមជ្រើសរើសទេសចរណ៍',NULL,NULL),
(181,'Booking Detail','ពត៌មានការកក់',NULL,NULL),
(182,'SELECT MODE','ជ្រើសរើសរបៀបធ្វើដំណើរ',NULL,NULL),
(183,'SELECT COMPANY','ជ្រើសរើសក្រុមហ៊ុន',NULL,NULL),
(184,'Airline / Courier Company','ផ្លូវអាកាស',NULL,NULL),
(185,'about to create​​ booking','អ្នកនឹងស្នើរក្នុងការបង្កើតការកក់',NULL,NULL),
(186,'Add','បញ្ចូល',NULL,NULL),
(187,'Transition','ការផ្លាស់ប្តូរ',NULL,NULL),
(188,'Update','កែប្រែ',NULL,NULL),
(189,'Item','ឥវ៉ាន់',NULL,NULL),
(190,'Vendor','អ្នកលក់',NULL,NULL),
(191,'Currency','រូបិយប័ណ្ណ',NULL,NULL),
(192,'Rate','អត្រា​ប្តូ​រ​ប្រាក់\r\n',NULL,NULL),
(193,'SELECT Item','ជ្រើសរើសឥវ៉ាន់',NULL,NULL),
(194,'SELECT Vendor','ជ្រើសរើសអ្នកលក់',NULL,NULL),
(195,'Price (FC)','តម្លៃបរទេស',NULL,NULL),
(196,'Price (USD)','តម្លៃជាដុល្លារ',NULL,NULL),
(197,'SELECT Quotation','ជ្រើសរើសសម្រង់',NULL,NULL),
(198,'SELECT Customer','ជ្រើសរើសអតិថិជន',NULL,NULL),
(199,'Please select one template for generate Item','សូមជ្រើសរើសគំរូមួយដើម្បីបង្កើតជាឥវ៉ាន់',NULL,NULL),
(200,'Vendor Currency!','រូបិយប័ណ្ណរបស់អ្នកលក់!',NULL,NULL),
(201,'Please select Vendor to get currency!','សូមជ្រើសរើសអ្នកលក់ដើម្បីបានរូបិយប័ណ្ណ!',NULL,NULL),
(202,'Price USD must be bigger than Zero.','តម្លៃលុយដុល្លាត្រូវតែធំជាងសូន្យ.',NULL,NULL),
(203,'&quot;Item&quot; cannot empty and must be required!<br>','&quot;ឥវ៉ាន់&quot; តម្រូវឱ្យបំពេញ ហើយមិនអាចទទេ!<br>',NULL,NULL),
(204,'&quot;Vendor&quot; cannot empty and must be required!<br>','&quot;អ្នកលក់&quot; តម្រូវឱ្យបំពេញ ហើយមិនអាចទទេ!<br>',NULL,NULL),
(205,'&quot;UOMs&quot; cannot empty and must be required!','&quot;ឯកតារង្វាស់&quot; តម្រូវឱ្យបំពេញ ហើយមិនអាចទទេ!',NULL,NULL),
(206,'You are about to create Quotation','អ្នកនឹងស្នើរក្នុងការបង្កើតសម្រង់',NULL,NULL),
(207,'The Tour Code is required and cannot empty!','លេខកូដនៃទេសចរណ៍តម្រូវឳ្យបំពេញ ហើយមិនអាចទទេ!',NULL,NULL),
(208,'Missing Item Quotation!','បាត់ឥវ៉ាន់នៃសម្រង់!',NULL,NULL),
(209,'Item Quotatin is required and cannot be empty.','ឥវ៉ាន់នៃសម្រង់តម្រូវឳ្យបំពេញ ហើយមិនអាចទទេ។',NULL,NULL),
(210,'Quotation Item!','Quotation Item!',NULL,NULL),
(211,'Please click button Confirm Item.','សូមចុចប៊ូតុង Confirm Item.',NULL,NULL),
(212,'Quotation Template!','Quotation Template',NULL,NULL),
(213,'Quotation Date','ថ្ងៃសម្រង់',NULL,NULL),
(214,'Qty','ចំនួន',NULL,NULL),
(215,'Create By','បង្កើតដោយ',NULL,NULL),
(216,'FC','រូបិយប័ណ្ណ​បរទេស',NULL,NULL),
(217,'USD','រូបិយប័ណ្ណ​ដុល្លា',NULL,NULL),
(218,'You are about to delete Quotation','អ្នកនឹងស្នើរក្នុងការលុបសម្រង់',NULL,NULL),
(219,'GRAND TOTAL','តំលៃ​បូក​សរុប',NULL,NULL),
(220,'You are about to update Quotation','អ្នកនឹងស្នើរក្នុងការកែប្រែសម្រង់',NULL,NULL),
(221,'History','ប្រវត្តិប្រតិបត្តិការណ៍',NULL,NULL),
(222,'Agent Commission','កម្រៃជើងផ្សារ',NULL,NULL),
(223,'Free Gift','កាដូរ',NULL,NULL),
(224,'Discount','ការបញ្ចុះតម្លៃ',NULL,NULL),
(225,'Total','សរុប',NULL,NULL),
(226,'Cost','តម្លៃ',NULL,NULL),
(227,'Profit','ប្រាក់ចំណេញ',NULL,NULL),
(228,'Selling Price','តម្លៃលក់',NULL,NULL),
(229,'You are about to create pricing','អ្នកនឹងស្នើរក្នុងការបង្កើតតម្លៃលក់',NULL,NULL),
(230,'Promotion','ការបញ្ចុះតម្លៃ',NULL,NULL),
(231,'Are you sure?','តើអ្នកប្រាកដទេ?',NULL,NULL),
(232,'You are about to delete type.','អ្នកនឹងស្នើរក្នុងការលុបតម្លៃ',NULL,NULL),
(233,'You are about to update pricing','អ្នកនឹងស្នើរក្នុងការកែប្រែតម្លៃ',NULL,NULL);

-- 
-- Definition of tm_mode_transport
-- 

CREATE TABLE IF NOT EXISTS `tm_mode_transport` (
  `mode_id` int(11) NOT NULL AUTO_INCREMENT,
  `mode_name` varchar(255) NOT NULL,
  `mode_description` varchar(255) DEFAULT NULL,
  `mode_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`mode_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_mode_transport
-- 

INSERT INTO `tm_mode_transport`(`mode_id`,`mode_name`,`mode_description`,`mode_inactive`) VALUES
(1,'Busss','Good',1),
(3,'Moto','Cheap price',1),
(4,'Tuk Tuk','Middle',1),
(5,'Passapp','AA',1),
(6,'Taxi','1800R',1),
(7,'Grab','Yellow',1);

-- 
-- Definition of tm_nationality
-- 

CREATE TABLE IF NOT EXISTS `tm_nationality` (
  `int_id` int(11) NOT NULL AUTO_INCREMENT,
  `var_name` varchar(255) DEFAULT NULL,
  `tin_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`int_id`)
) ENGINE=InnoDB AUTO_INCREMENT=112 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_nationality
-- 

INSERT INTO `tm_nationality`(`int_id`,`var_name`,`tin_inactive`) VALUES
(1,'Afghan',1),
(2,'Albanian',1),
(3,'Algerian',1),
(4,'American',1),
(5,'Argentinian',1),
(6,'Australian',1),
(7,'Austrian',1),
(8,'Bangladeshi',1),
(9,'Batswana',1),
(10,'Belgian',1),
(11,'Bolivian',1),
(12,'Brazilian',1),
(13,'British',1),
(14,'Bulgarian',1),
(15,'Cambodian',1),
(16,'Cameroonian',1),
(17,'Canadian',1),
(18,'Chilean',1),
(19,'Chinese',1),
(20,'Colombian',1),
(21,'Costa Rican',1),
(22,'Croatian',1),
(23,'Cuban',1),
(24,'Czech',1),
(25,'Danish',1),
(26,'Dominican',1),
(27,'Dutch',1),
(28,'Ecuadorian',1),
(29,'Egyptian',1),
(30,'Emirati',1),
(31,'English',1),
(32,'Estonian',1),
(33,'Ethiopian',1),
(34,'Fijian',1),
(35,'Finnish',1),
(36,'French',1),
(37,'German',1),
(38,'Ghanaian',1),
(39,'Greek',1),
(40,'Guatemalan',1),
(41,'Haitian',1),
(42,'Honduran',1),
(43,'Hungarian',1),
(44,'Icelandic',1),
(45,'Indian',1),
(46,'Indonesian',1),
(47,'Iranian',1),
(48,'Iraqi',1),
(49,'Irish',1),
(50,'Israeli',1),
(51,'Italian',1),
(52,'Jamaican',1),
(53,'Japanese',1),
(54,'Jordanian',1),
(55,'Kenyan',1),
(56,'Korean',1),
(57,'Kuwaiti',1),
(58,'Lao',1),
(59,'Latvian',1),
(60,'Lebanese',1),
(61,'Libyan',1),
(62,'Lithuanian',1),
(63,'Malaysian',1),
(64,'Malian',1),
(65,'Maltese',1),
(66,'Mexican',1),
(67,'Mongolian',1),
(68,'Moroccan',1),
(69,'Mozambican',1),
(70,'Namibian',1),
(71,'Nepalese',1),
(72,'New Zealand',1),
(73,'Nicaraguan',1),
(74,'Nigerian',1),
(75,'Norwegian',1),
(76,'Pakistani',1),
(77,'Panamanian',1),
(78,'Paraguayan',1),
(79,'Peruvian',1),
(80,'Philippine',1),
(81,'Polish',1),
(82,'Portuguese',1),
(83,'Romanian',1),
(84,'Russian',1),
(85,'Salvadorian',1),
(86,'Saudi',1),
(87,'Scottish',1),
(88,'Senegalese',1),
(89,'Serbian',1),
(90,'Singaporean',1),
(91,'Slovak',1),
(92,'South African',1),
(93,'Spanish',1),
(94,'Sri Lankan',1),
(95,'Sudanese',1),
(96,'Swedish',1),
(97,'Swiss',1),
(98,'Syrian',1),
(99,'Taiwanese',1),
(100,'Tajikistani',1),
(101,'Thai',1),
(102,'Tongan',1),
(103,'Tunisian',1),
(104,'Turkish',1),
(105,'Ukrainian',1),
(106,'Uruguayan',1),
(107,'Venezuelan',1),
(108,'Vietnamese',1),
(109,'Welsh',1),
(110,'Zambian',1),
(111,'Zimbabwean',1);

-- 
-- Definition of tm_numberprefix
-- 

CREATE TABLE IF NOT EXISTS `tm_numberprefix` (
  `module_id` varchar(255) NOT NULL,
  `numberprefix` varchar(50) DEFAULT NULL,
  `remark` varchar(255) DEFAULT NULL,
  `length` int(11) DEFAULT NULL,
  `lastid` int(11) DEFAULT NULL,
  `status` tinyint(1) DEFAULT '1',
  `group` varchar(255) DEFAULT NULL,
  `hide` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_numberprefix
-- 

INSERT INTO `tm_numberprefix`(`module_id`,`numberprefix`,`remark`,`length`,`lastid`,`status`,`group`,`hide`) VALUES
('BK','BK','Booking',11,39,1,NULL,0),
('BK-D','BK-D','Booking Detail',11,94,1,NULL,1),
('OP','OP','OP Progress Master',11,17,1,NULL,1),
('OP-TO','OP-TO','OP Progress ft. Tour',11,9,1,NULL,1),
('OPH','OPH','OP Progress History',11,1,1,NULL,1),
('OP_D','OP_D','OP Progress Detail',11,1,1,NULL,1),
('PC','PC','Pricing',11,30,1,NULL,1),
('PH','PH','Pricing History',11,7,1,NULL,1),
('PS','PS','Passenger',11,1,1,NULL,1),
('QU','QU','Quotation',11,9,1,NULL,1),
('RM','RM','Rooming List',11,1,1,NULL,1),
('TEM_QU','TEM_QU','Quotation Template',3,9,1,NULL,1),
('TM','TM','Tour',11,1,1,NULL,0),
('TMP_OP','TEM_OP','OP Template',3,7,1,NULL,1);

-- 
-- Definition of tm_numberprefix_custom
-- 

CREATE TABLE IF NOT EXISTS `tm_numberprefix_custom` (
  `var_modid` varchar(100) NOT NULL,
  `var_type` varchar(100) NOT NULL,
  `var_id` varchar(100) NOT NULL,
  `var_name` varchar(100) DEFAULT NULL,
  `int_length` int(11) DEFAULT NULL,
  `int_lastid` int(11) DEFAULT NULL,
  `var_LocationID` varchar(255) DEFAULT NULL,
  `int_IsUsed` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`var_modid`,`var_type`,`var_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_numberprefix_custom
-- 

INSERT INTO `tm_numberprefix_custom`(`var_modid`,`var_type`,`var_id`,`var_name`,`int_length`,`int_lastid`,`var_LocationID`,`int_IsUsed`) VALUES
('BK','BK','BK','Booking',11,6,NULL,1),
('TM','TM','TM-','Tour',11,6,NULL,1);

-- 
-- Definition of tm_op_progress
-- 

CREATE TABLE IF NOT EXISTS `tm_op_progress` (
  `var_id` varchar(100) NOT NULL,
  `var_op_to_id` varchar(100) DEFAULT NULL,
  `var_process` varchar(255) DEFAULT NULL,
  `tin_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`var_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_op_progress
-- 

INSERT INTO `tm_op_progress`(`var_id`,`var_op_to_id`,`var_process`,`tin_inactive`) VALUES
('OP00000000009','OP-TO00000000005','BM',1),
('OP00000000010','OP-TO00000000006','TKT',1),
('OP00000000011','OP-TO00000000006','LAND',1),
('OP00000000012','OP-TO00000000006','TL',1),
('OP00000000013','OP-TO00000000006','KG',1),
('OP00000000014','OP-TO00000000008','TKT',1),
('OP00000000015','OP-TO00000000008','LAND',1),
('OP00000000016','OP-TO00000000008','TL',1);

-- 
-- Definition of tm_op_progress_detail
-- 

CREATE TABLE IF NOT EXISTS `tm_op_progress_detail` (
  `int_id` int(11) NOT NULL AUTO_INCREMENT,
  `var_id` varchar(100) DEFAULT NULL,
  `var_name` varchar(255) DEFAULT NULL,
  `int_status` int(3) DEFAULT NULL,
  `dat_start` timestamp NULL DEFAULT NULL,
  `dat_completed` timestamp NULL DEFAULT NULL,
  `int_percent` int(3) DEFAULT NULL,
  `var_comment` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`int_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_op_progress_detail
-- 

INSERT INTO `tm_op_progress_detail`(`int_id`,`var_id`,`var_name`,`int_status`,`dat_start`,`dat_completed`,`int_percent`,`var_comment`) VALUES
(1,'O01','Group TKT Booking',NULL,NULL,NULL,10,'asdasdasd');

-- 
-- Definition of tm_op_progress_history
-- 

CREATE TABLE IF NOT EXISTS `tm_op_progress_history` (
  `var_id` varchar(100) NOT NULL,
  `var_op_id` varchar(100) DEFAULT NULL,
  `var_op_detail_id` varchar(100) DEFAULT NULL,
  `int_percent` int(3) DEFAULT NULL,
  `int_status` int(3) DEFAULT NULL,
  `var_comment` varchar(255) DEFAULT NULL,
  `var_create` varchar(100) DEFAULT NULL,
  `dat_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `tin_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`var_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_op_progress_history
-- 

INSERT INTO `tm_op_progress_history`(`var_id`,`var_op_id`,`var_op_detail_id`,`int_percent`,`int_status`,`var_comment`,`var_create`,`dat_create`,`tin_inactive`) VALUES
('OPH01','OP01','O01',10,NULL,'asdasd','balancika','2018-08-01 09:04:04',1),
('OPH02','OP01','O01',20,NULL,'asdasdasd','balancika','2018-08-01 09:04:56',1);

-- 
-- Definition of tm_op_progress_tour
-- 

CREATE TABLE IF NOT EXISTS `tm_op_progress_tour` (
  `var_id` varchar(100) NOT NULL,
  `var_tour_id` varchar(100) DEFAULT NULL,
  `var_create` varchar(100) DEFAULT NULL,
  `dat_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `var_modified` varchar(100) DEFAULT NULL,
  `dat_modified` timestamp NULL DEFAULT NULL,
  `tin_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`var_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_op_progress_tour
-- 

INSERT INTO `tm_op_progress_tour`(`var_id`,`var_tour_id`,`var_create`,`dat_create`,`var_modified`,`dat_modified`,`tin_inactive`) VALUES
('OP-TO00000000005','TM-00000000004','balancika','2018-08-02 16:41:39',NULL,NULL,1),
('OP-TO00000000006','TM-00000000003','balancika','2018-08-02 16:42:05',NULL,NULL,1),
('OP-TO00000000008','TM-00000000002','balancika','2018-08-04 09:20:53',NULL,NULL,1);

-- 
-- Definition of tm_op_status
-- 

CREATE TABLE IF NOT EXISTS `tm_op_status` (
  `status_id` int(11) NOT NULL AUTO_INCREMENT,
  `status_name` varchar(255) NOT NULL,
  `status_description` varchar(255) NOT NULL,
  `status_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`status_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_op_status
-- 

INSERT INTO `tm_op_status`(`status_id`,`status_name`,`status_description`,`status_inactive`) VALUES
(1,'Active','AAA',1),
(3,'Avialable','123',1),
(4,'Booking','456',1),
(5,'In Stock','',1);

-- 
-- Definition of tm_op_template
-- 

CREATE TABLE IF NOT EXISTS `tm_op_template` (
  `var_id` varchar(100) NOT NULL,
  `var_name` varchar(255) DEFAULT NULL,
  `tin_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`var_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_op_template
-- 

INSERT INTO `tm_op_template`(`var_id`,`var_name`,`tin_inactive`) VALUES
('TEM_OP005','Inbound',1),
('TEM_OP006','Outbound',1),
('TMP01','Local',1);

-- 
-- Definition of tm_op_template_detail
-- 

CREATE TABLE IF NOT EXISTS `tm_op_template_detail` (
  `int_id` int(11) NOT NULL AUTO_INCREMENT,
  `var_id` varchar(100) DEFAULT NULL,
  `var_progress` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`int_id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_op_template_detail
-- 

INSERT INTO `tm_op_template_detail`(`int_id`,`var_id`,`var_progress`) VALUES
(17,'TEM_OP006','USA'),
(18,'TEM_OP006','Thailand'),
(19,'TMP01','TKT'),
(20,'TMP01','LAND'),
(21,'TMP01','TL'),
(22,'TEM_OP005','TMT'),
(23,'TEM_OP005','BMW');

-- 
-- Definition of tm_passenger
-- 

CREATE TABLE IF NOT EXISTS `tm_passenger` (
  `var_id` varchar(100) NOT NULL,
  `var_name` varchar(255) DEFAULT NULL,
  `var_gender` varchar(10) DEFAULT NULL,
  `var_uom_id` varchar(100) DEFAULT NULL,
  `dat_dob` timestamp NULL DEFAULT NULL,
  `dat_pp_issued` timestamp NULL DEFAULT NULL,
  `dat_pp_expiry` timestamp NULL DEFAULT NULL,
  `var_pp_number` varchar(255) DEFAULT NULL,
  `var_nationality` varchar(100) DEFAULT NULL,
  `var_tel` varchar(20) DEFAULT NULL,
  `tin_inactive` tinyint(1) DEFAULT '1',
  `var_create` varchar(100) DEFAULT NULL,
  `dat_create` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `var_modified` varchar(100) DEFAULT NULL,
  `dat_modified` time DEFAULT NULL,
  PRIMARY KEY (`var_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_passenger
-- 



-- 
-- Definition of tm_pricing
-- 

CREATE TABLE IF NOT EXISTS `tm_pricing` (
  `var_id` varchar(100) DEFAULT NULL,
  `tour_id` varchar(100) DEFAULT NULL,
  `var_freegift` varchar(255) DEFAULT NULL,
  `var_promotion_disc` varchar(255) DEFAULT NULL,
  `var_agent_comm` varchar(255) DEFAULT NULL,
  `tin_inactive` tinyint(1) DEFAULT '1',
  `var_create_by` varchar(255) DEFAULT NULL,
  `dat_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `var_modified` varchar(255) DEFAULT NULL,
  `dat_modified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_pricing
-- 

INSERT INTO `tm_pricing`(`var_id`,`tour_id`,`var_freegift`,`var_promotion_disc`,`var_agent_comm`,`tin_inactive`,`var_create_by`,`dat_create`,`var_modified`,`dat_modified`) VALUES
('PC00000000024','BK00000000001','Ankor','122','15',1,'balancika','2018-07-26 10:02:37',NULL,'0001-01-01 00:00:00'),
('PC00000000025','TM-00000000002','coca','122','12',1,'dfaf','2018-07-26 13:21:01',NULL,'0001-01-01 00:00:00'),
('PC00000000026','TM-00000000003','coca','122','12',1,'dfaf','2018-07-26 13:22:34',NULL,'0001-01-01 00:00:00'),
('PC00000000027','TM-00000000004','coca','122','12',1,'balancika','2018-07-26 13:30:33',NULL,'0001-01-01 00:00:00');

-- 
-- Definition of tm_pricing_detail
-- 

CREATE TABLE IF NOT EXISTS `tm_pricing_detail` (
  `var_id` varchar(100) DEFAULT NULL,
  `var_uom_id` varchar(100) DEFAULT NULL,
  `dou_profit` double(18,3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_pricing_detail
-- 

INSERT INTO `tm_pricing_detail`(`var_id`,`var_uom_id`,`dou_profit`) VALUES
('PC00000000025','Box',15),
('PC00000000025','Case',7),
('PC00000000026','Box',100),
('PC00000000027','Ea',1000),
('PC00000000024','Case',9),
('PC00000000024','Case 12',8),
('PC00000000024','Ea',7);

-- 
-- Definition of tm_pricing_history
-- 

CREATE TABLE IF NOT EXISTS `tm_pricing_history` (
  `int_id` int(11) NOT NULL AUTO_INCREMENT,
  `var_id` varchar(100) DEFAULT NULL,
  `var_pricing_id` varchar(100) NOT NULL,
  `tour_id` varchar(255) DEFAULT NULL,
  `var_freegift` varchar(255) DEFAULT NULL,
  `var_promotion_disc` varchar(255) DEFAULT NULL,
  `var_agent_comm` varchar(255) DEFAULT NULL,
  `dat_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `var_note` varchar(255) DEFAULT NULL,
  `var_action` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`int_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_pricing_history
-- 

INSERT INTO `tm_pricing_history`(`int_id`,`var_id`,`var_pricing_id`,`tour_id`,`var_freegift`,`var_promotion_disc`,`var_agent_comm`,`dat_updated`,`var_note`,`var_action`) VALUES
(5,'PH00000000001','PC00000000024','BK00000000001','Hat','12','12','2018-07-26 10:02:37',NULL,NULL),
(6,'PH00000000002','PC00000000025','TM-00000000002','3','5','12','2018-07-26 13:21:01',NULL,NULL),
(7,'PH00000000003','PC00000000026','TM-00000000003','1','','12','2018-07-26 13:22:34',NULL,NULL),
(8,'PH00000000004','PC00000000027','TM-00000000004','ABC','10','12','2018-07-26 13:30:33',NULL,'Created'),
(9,'PH00000000005','PC00000000028','TM-00000000005','2','10','10','2018-07-27 09:35:32',NULL,'Created'),
(11,'PH00000000001','PC00000000024','BK00000000001','Ankor','122','15','2018-07-27 14:14:45',NULL,'Updated'),
(12,'PH00000000006','PC00000000029','TM-00000000005','','','','2018-07-27 15:18:38',NULL,'Created'),
(13,'PH00000000004','PC00000000027','TM-00000000004','coca','122','12','2018-07-27 16:24:06',NULL,'Updated'),
(14,'PH00000000001','PC00000000024','BK00000000001','Ankor','122','15','2018-07-31 11:21:15',NULL,'Updated');

-- 
-- Definition of tm_pricing_history_detail
-- 

CREATE TABLE IF NOT EXISTS `tm_pricing_history_detail` (
  `int_id` int(11) NOT NULL AUTO_INCREMENT,
  `var_id` varchar(100) DEFAULT NULL,
  `var_pricing_id` varchar(100) DEFAULT NULL,
  `var_uom_id` varchar(100) DEFAULT NULL,
  `dou_cost` double(18,3) DEFAULT NULL,
  `dou_profit` double(18,3) DEFAULT NULL,
  `dou_selling` double(18,3) DEFAULT NULL,
  `dat_modified` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `var_action` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`int_id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_pricing_history_detail
-- 

INSERT INTO `tm_pricing_history_detail`(`int_id`,`var_id`,`var_pricing_id`,`var_uom_id`,`dou_cost`,`dou_profit`,`dou_selling`,`dat_modified`,`var_action`) VALUES
(13,'PH00000000001','PC00000000024','Case',150,10,160,'2018-07-26 10:02:37',NULL),
(14,'PH00000000001','PC00000000024','Case 12',150,10,160,'2018-07-26 10:02:37',NULL),
(15,'PH00000000001','PC00000000024','Ea',150,5,155,'2018-07-26 10:02:37',NULL),
(16,'PH00000000002','PC00000000025','Box',4,15,19,'2018-07-26 13:21:01',NULL),
(17,'PH00000000002','PC00000000025','Case',2,7,9,'2018-07-26 13:21:01',NULL),
(18,'PH00000000003','PC00000000026','Box',300,100,400,'2018-07-26 13:22:34',NULL),
(19,'PH00000000004','PC00000000027','Ea',200,500,700,'2018-07-26 13:30:33',NULL),
(20,'PH00000000005','PC00000000028','ADL',445,15,460,'2018-07-27 09:35:32',NULL),
(21,'PH00000000001','PC00000000024','Case',150,7,157,'2018-07-27 14:14:45','Updated'),
(22,'PH00000000001','PC00000000024','Case 12',150,7,157,'2018-07-27 14:14:45','Updated'),
(23,'PH00000000001','PC00000000024','Ea',150,7,157,'2018-07-27 14:14:45','Updated'),
(24,'PH00000000006','PC00000000029','ADL',445,0,445,'2018-07-27 15:18:38','Created'),
(25,'PH00000000004','PC00000000027','Ea',200,1000,1200,'2018-07-27 16:24:06','Updated'),
(26,'PH00000000001','PC00000000024','Case',150,9,159,'2018-07-31 11:21:15','Updated'),
(27,'PH00000000001','PC00000000024','Case 12',150,8,158,'2018-07-31 11:21:15','Updated'),
(28,'PH00000000001','PC00000000024','Ea',150,7,157,'2018-07-31 11:21:15','Updated');

-- 
-- Definition of tm_progress
-- 

CREATE TABLE IF NOT EXISTS `tm_progress` (
  `progress_id` int(11) NOT NULL AUTO_INCREMENT,
  `progress_name` varchar(255) NOT NULL,
  `progress_description` varchar(255) DEFAULT NULL,
  `progress_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`progress_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_progress
-- 

INSERT INTO `tm_progress`(`progress_id`,`progress_name`,`progress_description`,`progress_inactive`) VALUES
(1,'progress','50%',1),
(2,'suspend','60%',1);

-- 
-- Definition of tm_quotation_template
-- 

CREATE TABLE IF NOT EXISTS `tm_quotation_template` (
  `var_id` varchar(100) NOT NULL,
  `var_remark` varchar(255) DEFAULT NULL,
  `var_item` varchar(255) DEFAULT NULL,
  `tin_inactive` tinyint(1) DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_quotation_template
-- 

INSERT INTO `tm_quotation_template`(`var_id`,`var_remark`,`var_item`,`tin_inactive`) VALUES
('TEM_QU003','Booking','P00004',1),
('TEM_QU005','Tour','P00006',1),
('TEM_QU005','Tour','P00020',1),
('TEM_QU006','asdasd','P00011',1),
('TEM_QU006','asdasd','P00009',1),
('TEM_QU006','asdasd','P00006',1),
('TEM_QU006','asdasd','P00018',1),
('TEM_QU006','asdasd','P00003',1),
('TEM_QU007','adsadsdasadsdasadsadsads','P00006',1),
('TEM_QU008','dzcczxzcxczx','P00009',1),
('TEM_QU008','dzcczxzcxczx','P00006',1),
('','GGG','P00009',1),
('','GGG','P00006',1);

-- 
-- Definition of tm_quote
-- 

CREATE TABLE IF NOT EXISTS `tm_quote` (
  `quote_id` varchar(255) NOT NULL,
  `quote_remark` varchar(255) DEFAULT NULL,
  `quote_reference` varchar(255) DEFAULT NULL,
  `tour_id` varchar(255) DEFAULT NULL,
  `var_cust_id` varchar(100) DEFAULT NULL,
  `create_by` varchar(255) DEFAULT NULL,
  `create_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `modified_by` varchar(255) DEFAULT NULL,
  `modified_date` timestamp NULL DEFAULT NULL,
  `tin_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`quote_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_quote
-- 

INSERT INTO `tm_quote`(`quote_id`,`quote_remark`,`quote_reference`,`tour_id`,`var_cust_id`,`create_by`,`create_date`,`modified_by`,`modified_date`,`tin_inactive`) VALUES
('QU00000000002','123','QU00000000003','TM-00000000002','C00007','balancika','2018-07-17 09:00:05','balancika','2018-07-23 14:39:09',1),
('QU00000000003','aadsdasdasdas','QU00000000002','TM-00000000003',NULL,'balancika','2018-07-17 09:11:52',NULL,NULL,1),
('QU00000000004','OK','QU00000000003','TM-00000000004','C00026','balancika','2018-07-20 10:13:34',NULL,NULL,1),
('QU00000000006','DSummmmm','','BK00000000001','','balancika','2018-07-20 10:55:00',NULL,NULL,1),
('QU00000000008','adsdasdasadsdasUPDATE','','TM-00000000005','','balancika','2018-07-24 10:13:21','balancika','2018-07-24 13:27:45',1);

-- 
-- Definition of tm_quote_detail
-- 

CREATE TABLE IF NOT EXISTS `tm_quote_detail` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `quote_id` varchar(255) NOT NULL,
  `lineno` int(11) DEFAULT NULL,
  `item_id` varchar(255) NOT NULL,
  `description` varchar(255) DEFAULT NULL,
  `var_vendor` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`,`quote_id`,`item_id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_quote_detail
-- 

INSERT INTO `tm_quote_detail`(`id`,`quote_id`,`lineno`,`item_id`,`description`,`var_vendor`) VALUES
(2,'QU00000000003',1,'P00004','','V00002'),
(3,'QU00000000003',2,'P00003','','V00006'),
(4,'QU00000000004',1,'P00011','','V00003'),
(5,'QU00000000004',2,'P00009','','V00003'),
(6,'QU00000000004',3,'P00006','','V00003'),
(7,'QU00000000004',4,'P00018','','V00003'),
(8,'QU00000000004',5,'P00003','','V00003'),
(9,'QU00000000006',1,'P00011','','V00003'),
(10,'QU00000000006',2,'P00009','','V00003'),
(11,'QU00000000006',3,'P00006','asdasd','V00003'),
(12,'QU00000000006',4,'P00018','asdasd','V00003'),
(13,'QU00000000006',5,'P00003','','V00003'),
(14,'QU00000000002',1,'P00009','1','V00006'),
(15,'QU00000000002',2,'P00006','2','V00004'),
(18,'QU00000000008',1,'P00009','','V00003'),
(19,'QU00000000008',2,'P00006','','V00003'),
(20,'QU00000000008',3,'P00002','','V00003');

-- 
-- Definition of tm_quote_detail_uom
-- 

CREATE TABLE IF NOT EXISTS `tm_quote_detail_uom` (
  `int_id` int(11) NOT NULL AUTO_INCREMENT,
  `var_quote_id` varchar(100) DEFAULT NULL,
  `var_item_id` varchar(100) DEFAULT NULL,
  `var_uom_id` varchar(100) DEFAULT NULL,
  `dou_price_fc` double(18,3) DEFAULT NULL,
  `dou_price_usd` double(18,3) DEFAULT NULL,
  `var_remark` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`int_id`)
) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_quote_detail_uom
-- 

INSERT INTO `tm_quote_detail_uom`(`int_id`,`var_quote_id`,`var_item_id`,`var_uom_id`,`dou_price_fc`,`dou_price_usd`,`var_remark`) VALUES
(3,'QU00000000003','P00004','Box',10,100,'asdasdasd'),
(4,'QU00000000003','P00003','Box',15,200,'asdasdzxcasd'),
(5,'QU00000000004','P00011','Ea',10,10,''),
(6,'QU00000000004','P00009','Ea',10,20,''),
(7,'QU00000000004','P00006','Ea',10,30,''),
(8,'QU00000000004','P00018','Ea',10,40,''),
(9,'QU00000000004','P00018','Ea',10,40,''),
(10,'QU00000000004','P00003','Ea',10,60,''),
(11,'QU00000000006','P00011','Case',10,10,''),
(12,'QU00000000006','P00011','Case 12',10,10,''),
(13,'QU00000000006','P00011','Ea',10,10,''),
(14,'QU00000000006','P00009','Case',10,20,''),
(15,'QU00000000006','P00009','Case 12',10,20,''),
(16,'QU00000000006','P00009','Ea',10,20,''),
(17,'QU00000000006','P00006','Case',10,30,''),
(18,'QU00000000006','P00006','Case 12',10,30,''),
(19,'QU00000000006','P00006','Ea',10,30,''),
(20,'QU00000000006','P00018','Case',10,40,''),
(21,'QU00000000006','P00018','Case 12',10,40,''),
(22,'QU00000000006','P00018','Ea',10,40,''),
(23,'QU00000000006','P00003','Case',10,50,''),
(24,'QU00000000006','P00003','Case 12',10,50,''),
(25,'QU00000000006','P00003','Ea',10,50,''),
(26,'QU00000000002','P00009','Box',1,2,'12'),
(27,'QU00000000002','P00009','Case',2,1,'21'),
(28,'QU00000000002','P00006','Box',1,2,'12'),
(29,'QU00000000002','P00006','Case',2,1,'21'),
(33,'QU00000000008','P00009','ADL',3000,105,''),
(34,'QU00000000008','P00006','ADL',4000,140,''),
(35,'QU00000000008','P00002','ADL',0,200,'');

-- 
-- Definition of tm_room_type
-- 

CREATE TABLE IF NOT EXISTS `tm_room_type` (
  `type_id` int(11) NOT NULL AUTO_INCREMENT,
  `room_type` varchar(255) DEFAULT NULL,
  `room_description` varchar(255) DEFAULT NULL,
  `room_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_room_type
-- 

INSERT INTO `tm_room_type`(`type_id`,`room_type`,`room_description`,`room_inactive`) VALUES
(1,'Couple','Sweet Room',1),
(2,'Two bad','....',1),
(4,'Sigle','Ot lit forever',1),
(5,'One bed','Single',1);

-- 
-- Definition of tm_rooming_list
-- 

CREATE TABLE IF NOT EXISTS `tm_rooming_list` (
  `room_id` varchar(100) NOT NULL,
  `tour_id` varchar(100) NOT NULL,
  `var_create` varchar(100) DEFAULT NULL,
  `dat_create` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `var_modified` varchar(100) DEFAULT NULL,
  `dat_modified` timestamp NULL DEFAULT NULL,
  `tin_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`room_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_rooming_list
-- 



-- 
-- Definition of tm_rooming_list_detail
-- 

CREATE TABLE IF NOT EXISTS `tm_rooming_list_detail` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `room_id` varchar(100) DEFAULT NULL,
  `lineno` int(11) DEFAULT NULL,
  `client_code` varchar(255) DEFAULT NULL,
  `var_pas_id` varchar(100) DEFAULT NULL COMMENT 'Default have Tour Leader 1room',
  `type_id` int(11) NOT NULL,
  `room_code` varchar(10) DEFAULT NULL,
  `remark` varchar(255) DEFAULT NULL,
  `attachment` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_rooming_list_detail
-- 



-- 
-- Definition of tm_roominglist_detail
-- 

CREATE TABLE IF NOT EXISTS `tm_roominglist_detail` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `lineno` int(11) DEFAULT NULL,
  `passenger_name` varchar(255) DEFAULT NULL COMMENT 'Default have Tour Leader 1room',
  `passenger_sex` varchar(20) DEFAULT 'Other',
  `type_id` int(11) NOT NULL,
  `room_code` varchar(10) DEFAULT NULL,
  `passenger_dob` date DEFAULT NULL,
  `pp_issued_date` timestamp NULL DEFAULT NULL,
  `pp_expiry_date` timestamp NULL DEFAULT NULL,
  `pp_number` varchar(20) DEFAULT NULL,
  `nationality` varchar(50) DEFAULT NULL,
  `client_code` varchar(255) DEFAULT NULL,
  `tel_no` varchar(20) DEFAULT NULL,
  `remark` varchar(255) DEFAULT NULL,
  `attachment` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_roominglist_detail
-- 



-- 
-- Definition of tm_sale_opportunity
-- 

CREATE TABLE IF NOT EXISTS `tm_sale_opportunity` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `lineno` int(11) DEFAULT NULL,
  `tour_id` varchar(255) DEFAULT NULL,
  `opportunity_name` varchar(255) DEFAULT NULL,
  `customer_name` varchar(255) DEFAULT NULL,
  `sale_description` varchar(255) DEFAULT NULL,
  `qty_adl` int(11) DEFAULT NULL,
  `qty_chd` int(11) DEFAULT NULL,
  `qty_inf` int(11) DEFAULT NULL,
  `client_code` varchar(255) DEFAULT NULL,
  `client_decision` int(11) DEFAULT NULL,
  `entry_date` timestamp NULL DEFAULT NULL,
  `reported_by` varchar(255) DEFAULT NULL,
  `mgr_date` timestamp NULL DEFAULT NULL,
  `mgr_comment` varchar(255) DEFAULT NULL,
  `result_date` timestamp NULL DEFAULT NULL,
  `result` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_sale_opportunity
-- 



-- 
-- Definition of tm_setting
-- 

CREATE TABLE IF NOT EXISTS `tm_setting` (
  `id` int(18) NOT NULL AUTO_INCREMENT,
  `module` varchar(255) DEFAULT NULL,
  `object` varchar(255) DEFAULT NULL,
  `inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_setting
-- 

INSERT INTO `tm_setting`(`id`,`module`,`object`,`inactive`) VALUES
(1,'Item Type','Non-Inventory Part',1),
(2,'Item UOM','ADL',1),
(3,'Tour Leader Costing','P00003',1),
(4,'Tour Leader UOM','Ea',1),
(5,'Default Price Code','PC002',1),
(6,'Item TKT','P00011',1);

-- 
-- Definition of tm_tour
-- 

CREATE TABLE IF NOT EXISTS `tm_tour` (
  `tour_id` varchar(255) NOT NULL,
  `category_id` int(11) DEFAULT NULL,
  `type_id` int(11) DEFAULT NULL,
  `season_id` int(18) DEFAULT NULL,
  `item` varchar(255) DEFAULT NULL,
  `var_class` varchar(255) DEFAULT NULL,
  `start_date` datetime DEFAULT NULL,
  `end_date` datetime DEFAULT NULL,
  `status_id` int(11) DEFAULT NULL,
  `tour_leader` varchar(255) DEFAULT NULL,
  `tour_instruction` varchar(255) DEFAULT NULL,
  `visa_deadline` date DEFAULT NULL,
  `tour_year` varchar(4) DEFAULT NULL,
  `tour_month` varchar(20) DEFAULT NULL,
  `tour_inactive` tinyint(1) DEFAULT '1',
  `create_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `create_by` varchar(255) DEFAULT NULL,
  `modified_date` timestamp NULL DEFAULT NULL,
  `modified_by` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`tour_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_tour
-- 

INSERT INTO `tm_tour`(`tour_id`,`category_id`,`type_id`,`season_id`,`item`,`var_class`,`start_date`,`end_date`,`status_id`,`tour_leader`,`tour_instruction`,`visa_deadline`,`tour_year`,`tour_month`,`tour_inactive`,`create_date`,`create_by`,`modified_date`,`modified_by`) VALUES
('BK00000000001',1,5,2,'P00019','P00019','2018-08-01 00:00:00','2018-08-06 00:00:00',1,'E-00005','','2018-08-18 00:00:00','2018','Angust',1,'2018-07-07 12:12:11','balancika','2018-07-11 15:38:10','balancika'),
('BK00000000004',2,12,1,'P00025','P00025','2018-07-10 00:00:00','2018-07-16 00:00:00',1,'E00002','','2018-08-24 00:00:00','','',1,'2018-07-10 09:35:00','balancika','2018-07-10 11:36:51','balancika'),
('TM-00000000002',3,2,1,'P00017','P00017','2018-08-01 00:00:00','2018-08-06 00:00:00',1,'E00002','Meet at Balancika Cambodia','2018-08-09 00:00:00','2018','August',1,'2018-07-07 11:33:57','balancika',NULL,NULL),
('TM-00000000003',2,2,2,'P00018','P00018','2018-08-01 00:00:00','2018-08-31 00:00:00',1,'E00001','Meet at Balancika Cambodia','2018-09-07 00:00:00','2018','Angust',1,'2018-07-07 11:59:01','balancika',NULL,NULL),
('TM-00000000004',2,2,1,'P00020','P00020','2018-07-01 00:00:00','2018-07-07 00:00:00',1,'E00003','','2018-08-16 00:00:00','2018','July',1,'2018-07-07 14:36:19','balancika',NULL,NULL),
('TM-00000000005',3,12,4,'P00021','P00021','2018-07-01 00:00:00','2018-07-05 00:00:00',1,'E-00004','','2018-08-30 00:00:00','2018','July',1,'2018-07-09 09:00:49','balancika','2018-07-10 09:48:47','balancika');

-- 
-- Definition of tm_tour_category
-- 

CREATE TABLE IF NOT EXISTS `tm_tour_category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category_name` varchar(255) NOT NULL,
  `category_description` varchar(255) DEFAULT NULL,
  `category_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_tour_category
-- 

INSERT INTO `tm_tour_category`(`id`,`category_name`,`category_description`,`category_inactive`) VALUES
(1,'Inbound','in local',1),
(2,'Outbound','GOOD',1),
(3,'Honeymoon','Sweet room',1),
(4,'Single','ot lit',1);

-- 
-- Definition of tm_tour_pricing
-- 

CREATE TABLE IF NOT EXISTS `tm_tour_pricing` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `tour_id` varchar(255) DEFAULT NULL COMMENT 'It have Tour Code and Tour Description',
  `amount_day` int(3) DEFAULT NULL,
  `cost_adl` double(18,3) DEFAULT NULL,
  `cost_chd` double(18,3) DEFAULT NULL,
  `cost_inf` double(18,3) DEFAULT NULL,
  `cost_sgr` double(18,3) DEFAULT NULL,
  `profit_adl` double(18,3) DEFAULT NULL,
  `profit_chd` double(18,3) DEFAULT NULL,
  `profit_inf` double(18,3) DEFAULT NULL,
  `profit_sgl_room` double(18,3) DEFAULT NULL,
  `sell_adl` double(18,3) DEFAULT NULL,
  `sell_chd` double(18,3) DEFAULT NULL,
  `sell_inf` double(18,3) DEFAULT NULL,
  `sell_sgl_room` double(18,3) DEFAULT NULL,
  `free_gift` varchar(255) DEFAULT NULL,
  `promotional_discount` varchar(255) DEFAULT NULL,
  `agent_comission` double(18,3) DEFAULT NULL,
  `last_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_note` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_tour_pricing
-- 



-- 
-- Definition of tm_tour_season
-- 

CREATE TABLE IF NOT EXISTS `tm_tour_season` (
  `season_id` int(11) NOT NULL AUTO_INCREMENT,
  `season_name` varchar(255) NOT NULL,
  `season_description` varchar(255) DEFAULT NULL,
  `season_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`season_id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_tour_season
-- 

INSERT INTO `tm_tour_season`(`season_id`,`season_name`,`season_description`,`season_inactive`) VALUES
(1,'Spring','33',1),
(2,'Raining','',1),
(3,'Vorsa','cold',1),
(4,'Raining','',1),
(5,'Raining','',1),
(6,'Hello','',1),
(7,'asdasd','',1),
(10,'LLO','',1),
(11,'storm','Happy',1);

-- 
-- Definition of tm_tour_status
-- 

CREATE TABLE IF NOT EXISTS `tm_tour_status` (
  `status_id` int(11) NOT NULL AUTO_INCREMENT,
  `status_name` varchar(255) NOT NULL,
  `status_description` varchar(255) DEFAULT NULL,
  `status_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`status_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_tour_status
-- 

INSERT INTO `tm_tour_status`(`status_id`,`status_name`,`status_description`,`status_inactive`) VALUES
(1,'Not Started','',1),
(2,'Started','',1),
(3,'In Progress','',1),
(4,'Completed','',1),
(5,'Failed','',1);

-- 
-- Definition of tm_tour_trip
-- 

CREATE TABLE IF NOT EXISTS `tm_tour_trip` (
  `id` int(18) NOT NULL AUTO_INCREMENT,
  `tour_id` varchar(255) NOT NULL,
  `country_id` int(18) DEFAULT NULL,
  `destination` int(18) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=75 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_tour_trip
-- 

INSERT INTO `tm_tour_trip`(`id`,`tour_id`,`country_id`,`destination`) VALUES
(1,'TM-00000000002',114,1),
(2,'TM-00000000002',114,2),
(3,'TM-00000000002',114,3),
(4,'TM-00000000003',114,1),
(5,'TM-00000000003',114,2),
(6,'TM-00000000003',114,3),
(10,'TM-00000000004',38,0),
(53,'TM-00000000005',38,9),
(54,'TM-00000000005',38,10),
(55,'TM-00000000005',38,11),
(56,'TM-00000000005',38,12),
(57,'TM-00000000005',38,13),
(70,'BK00000000004',188,0),
(71,'BK00000000004',21,0),
(72,'BK00000000001',3,0),
(73,'BK00000000001',114,2),
(74,'BK00000000001',114,3);

-- 
-- Definition of tm_tour_type
-- 

CREATE TABLE IF NOT EXISTS `tm_tour_type` (
  `type_id` int(11) NOT NULL AUTO_INCREMENT,
  `type_name` varchar(255) NOT NULL,
  `type_description` varchar(255) DEFAULT NULL,
  `type_inactive` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table tm_tour_type
-- 

INSERT INTO `tm_tour_type`(`type_id`,`type_name`,`type_description`,`type_inactive`) VALUES
(2,'Outbound','africa',1),
(3,'Inbound','In Local',1),
(4,'AAAA','asdadsadsdasads',1),
(5,'asdasd','asdasdas',1),
(6,'cc','',1),
(7,'dddd','',1),
(8,'ss','',1),
(9,'ss','',1),
(10,'aaa','',1),
(11,'dfasdf','safdasf',1),
(12,'Honey','moon',1),
(13,'ot lit','',1);

-- 
-- Definition of vb_account_type
-- 

CREATE TABLE IF NOT EXISTS `vb_account_type` (
  `vaid` int(11) NOT NULL AUTO_INCREMENT,
  `accName` varchar(255) DEFAULT NULL,
  `accNumber` varchar(255) DEFAULT NULL,
  `accContactName` varchar(255) DEFAULT NULL,
  `accContactTel` varchar(255) DEFAULT NULL,
  `accDescription` varchar(255) DEFAULT NULL,
  `accDocument` varchar(255) DEFAULT NULL,
  `accContactRegister` date DEFAULT NULL,
  `accContactExpired` date DEFAULT NULL,
  `accCDate` datetime DEFAULT NULL,
  `accCBy` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`vaid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_account_type
-- 

INSERT INTO `vb_account_type`(`vaid`,`accName`,`accNumber`,`accContactName`,`accContactTel`,`accDescription`,`accDocument`,`accContactRegister`,`accContactExpired`,`accCDate`,`accCBy`) VALUES
(1,'ABA Bank','000367666','SOK HEANG ',NULL,NULL,NULL,NULL,NULL,NULL,NULL),
(2,'WING Account','000367666','SOK HEANG ',NULL,NULL,NULL,NULL,NULL,'2018-04-04 10:55:14',NULL),
(3,'ACLEDA Bank','000367666','SOK HEANG ',NULL,NULL,NULL,NULL,NULL,'2018-04-04 10:55:11',NULL),
(4,'CIMB','000367666','SOK HEANG ',NULL,NULL,NULL,NULL,NULL,NULL,NULL);

-- 
-- Definition of vb_administrator
-- 

CREATE TABLE IF NOT EXISTS `vb_administrator` (
  `username` varchar(255) NOT NULL,
  `password` varchar(255) DEFAULT NULL,
  `level` varchar(255) DEFAULT NULL,
  `active` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_administrator
-- 

INSERT INTO `vb_administrator`(`username`,`password`,`level`,`active`) VALUES
('admin','','ADMIN','1');

-- 
-- Definition of vb_config_path
-- 

CREATE TABLE IF NOT EXISTS `vb_config_path` (
  `type` varchar(255) NOT NULL,
  `path` varchar(255) DEFAULT NULL,
  `getPath` varchar(255) DEFAULT NULL,
  `cdate` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_config_path
-- 

INSERT INTO `vb_config_path`(`type`,`path`,`getPath`,`cdate`) VALUES
('Customer','\\\\192.168.123.5\\Image','http://192.168.123.72/VbuyImage/','2018-06-30 09:07:24');

-- 
-- Definition of vb_cust_addon_info
-- 

CREATE TABLE IF NOT EXISTS `vb_cust_addon_info` (
  `var_custId` varchar(100) NOT NULL,
  `var_iden_num` varchar(14) DEFAULT NULL,
  `var_iden_img` varchar(100) DEFAULT NULL,
  `var_fb` varchar(100) DEFAULT NULL,
  `var_whatapp` varchar(100) DEFAULT NULL,
  `var_line` varchar(100) DEFAULT NULL,
  `dat_dob` date DEFAULT NULL,
  `dat_createdate` date DEFAULT NULL,
  `var_aba_acc` varchar(16) DEFAULT NULL,
  `var_wechat` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`var_custId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_cust_addon_info
-- 

INSERT INTO `vb_cust_addon_info`(`var_custId`,`var_iden_num`,`var_iden_img`,`var_fb`,`var_whatapp`,`var_line`,`dat_dob`,`dat_createdate`,`var_aba_acc`,`var_wechat`) VALUES
('C00001','','','','','',NULL,'2018-08-09 00:00:00','',''),
('C00100','12312312312',NULL,'3123','12312','123','2018-07-30 00:00:00','2018-08-06 00:00:00','123123123','123123'),
('C00101','','','','','','2002-02-02 00:00:00','2018-08-09 00:00:00','',''),
('C00102','213123123',NULL,'123123','123123','123123','2018-08-21 00:00:00','2018-08-13 00:00:00','123123123','123123');

-- 
-- Definition of vb_customer_conditions
-- 

CREATE TABLE IF NOT EXISTS `vb_customer_conditions` (
  `vb_conId` int(11) NOT NULL AUTO_INCREMENT,
  `vb_custId` varchar(255) DEFAULT NULL,
  `vb_isNormalGoods` tinyint(1) DEFAULT NULL,
  `vb_isBrandName` tinyint(1) DEFAULT NULL,
  `vb_isConAvailable` tinyint(1) DEFAULT NULL,
  `vb_isConNotAvailable` tinyint(1) DEFAULT NULL,
  `vb_isCusGetProduct` tinyint(1) DEFAULT NULL,
  `vb_isCusNeedShip` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`vb_conId`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_customer_conditions
-- 

INSERT INTO `vb_customer_conditions`(`vb_conId`,`vb_custId`,`vb_isNormalGoods`,`vb_isBrandName`,`vb_isConAvailable`,`vb_isConNotAvailable`,`vb_isCusGetProduct`,`vb_isCusNeedShip`) VALUES
(1,'C00100',0,0,0,0,1,0),
(2,'C00101',0,0,0,0,1,0),
(3,'C00041',0,0,0,0,1,0),
(4,'C00001',0,0,0,0,1,0),
(5,'C00102',0,0,0,0,1,0);

-- 
-- Definition of vb_customer_goods_receiver
-- 

CREATE TABLE IF NOT EXISTS `vb_customer_goods_receiver` (
  `vb_revId` int(11) NOT NULL AUTO_INCREMENT,
  `custId` varchar(255) DEFAULT NULL,
  `vb_revName` varchar(255) DEFAULT NULL,
  `vb_revImg` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`vb_revId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_customer_goods_receiver
-- 



-- 
-- Definition of vb_customershipaddress
-- 

CREATE TABLE IF NOT EXISTS `vb_customershipaddress` (
  `TransID` varchar(255) NOT NULL,
  `ItemId` int(11) DEFAULT NULL,
  `ItemCode` varchar(255) DEFAULT NULL,
  `CustId` varchar(255) DEFAULT NULL,
  `ShipTo` varchar(255) DEFAULT NULL,
  `Location` varchar(255) DEFAULT NULL,
  `Date` datetime DEFAULT NULL,
  `Type` varchar(255) DEFAULT NULL,
  `Description` varchar(255) DEFAULT NULL,
  `Remark` varchar(255) DEFAULT NULL,
  `Status` varchar(255) DEFAULT NULL,
  `Active` int(1) DEFAULT '0',
  PRIMARY KEY (`TransID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_customershipaddress
-- 

INSERT INTO `vb_customershipaddress`(`TransID`,`ItemId`,`ItemCode`,`CustId`,`ShipTo`,`Location`,`Date`,`Type`,`Description`,`Remark`,`Status`,`Active`) VALUES
('SL-00000354',1,'506AUG18-1C00042','C00042',NULL,'LO-001','2018-08-06 10:46:28',NULL,NULL,NULL,NULL,0),
('SL-00000355',2,'06AUG18-2C00042','C00042',NULL,'LO-001','2018-08-06 11:20:18',NULL,NULL,NULL,NULL,0),
('SL-00000356',3,'409AUG18-1C00101','C00101',NULL,'LO-001','2018-08-09 09:18:26',NULL,NULL,NULL,NULL,0),
('SL-00000357',4,'509AUG18-1C00041','C00041',NULL,'LO-001','2018-08-09 09:19:07',NULL,NULL,NULL,NULL,0),
('SL-00000358',5,'409AUG18-2C00101','C00101',NULL,'LO-001','2018-08-09 09:20:58',NULL,NULL,NULL,NULL,0),
('SL-00000359',6,'409AUG18-3C00101','C00101',NULL,'LO-001','2018-08-09 09:26:34',NULL,NULL,NULL,NULL,0),
('SL-00000360',7,'509AUG18-2C00041','C00041',NULL,'LO-001','2018-08-09 09:28:36',NULL,NULL,NULL,NULL,0),
('SL-00000361',8,'509AUG18-1C00100','C00100',NULL,'LO-001','2018-08-09 10:08:39',NULL,NULL,NULL,NULL,0),
('SL-00000362',9,'509AUG18-2C00100','C00100',NULL,'LO-001','2018-08-09 10:09:10',NULL,NULL,NULL,NULL,0),
('SL-00000363',10,'509AUG18-3C00100','C00100',NULL,'LO-001','2018-08-09 10:47:09',NULL,NULL,NULL,NULL,0),
('SL-00000364',11,'409AUG18-4C00101','C00101',NULL,'LO-001','2018-08-09 11:02:19',NULL,NULL,NULL,NULL,0),
('SL-00000365',12,'409AUG18-5C00101','C00101',NULL,'LO-001','2018-08-09 11:02:44',NULL,NULL,NULL,NULL,0),
('SL-00000366',13,'509AUG18-4C00100','C00100',NULL,'LO-001','2018-08-09 11:16:12',NULL,NULL,NULL,NULL,0),
('SL-00000367',14,'409AUG18-6C00101','C00101',NULL,'LO-001','2018-08-09 11:36:22',NULL,NULL,NULL,NULL,0),
('SL-00000368',15,'409AUG18-7C00101','C00101',NULL,'LO-001','2018-08-09 11:48:05',NULL,NULL,NULL,NULL,0),
('SL-00000369',16,'509AUG18-5C00100','C00100',NULL,'LO-001','2018-08-09 11:54:02',NULL,NULL,NULL,NULL,0),
('SL-00000370',17,'409AUG18-8C00101','C00101',NULL,'LO-001','2018-08-09 13:35:52',NULL,NULL,NULL,NULL,0),
('SL-00000371',18,'510AUG18-6C00100','C00100',NULL,'LO-001','2018-08-10 14:16:32',NULL,NULL,NULL,NULL,0),
('SL-00000372',19,'510AUG18-7C00100','C00100',NULL,'LO-001','2018-08-10 14:21:16',NULL,NULL,NULL,NULL,0),
('SL-00000373',20,'510AUG18-8C00100','C00100',NULL,'LO-001','2018-08-10 15:06:01',NULL,NULL,NULL,NULL,0);

-- 
-- Definition of vb_defaultcustomer
-- 

CREATE TABLE IF NOT EXISTS `vb_defaultcustomer` (
  `CustGroup` varchar(255) DEFAULT NULL,
  `CustPriceCode` varchar(255) DEFAULT NULL,
  `CustCard` varchar(255) DEFAULT NULL,
  `DefaultLocationSaleOrder` varchar(255) DEFAULT NULL,
  `AccountCash` varchar(255) DEFAULT NULL,
  `AccountID` varchar(255) DEFAULT NULL,
  `RecNumberPrefix` varchar(255) DEFAULT NULL,
  `RecModule` varchar(255) DEFAULT NULL,
  `ItemAvailable` int(11) DEFAULT NULL,
  `VenLC` varchar(255) DEFAULT NULL,
  `VenService` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_defaultcustomer
-- 

INSERT INTO `vb_defaultcustomer`(`CustGroup`,`CustPriceCode`,`CustCard`,`DefaultLocationSaleOrder`,`AccountCash`,`AccountID`,`RecNumberPrefix`,`RecModule`,`ItemAvailable`,`VenLC`,`VenService`) VALUES
('CG001','Retail','Silver','VBuy','110000','40204000','OR','DEFAULT',25,'V00009','V00006');

-- 
-- Definition of vb_exchangerate
-- 

CREATE TABLE IF NOT EXISTS `vb_exchangerate` (
  `Date` datetime DEFAULT NULL,
  `Dollar` double DEFAULT NULL,
  `Khmer` double DEFAULT NULL,
  `China` double DEFAULT NULL,
  `active` tinyint(4) DEFAULT NULL,
  `cby` int(11) DEFAULT NULL,
  `mby` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_exchangerate
-- 

INSERT INTO `vb_exchangerate`(`Date`,`Dollar`,`Khmer`,`China`,`active`,`cby`,`mby`) VALUES
('2018-08-06 09:24:37',1,4050,6.3,0,2,NULL),
('2018-08-06 09:24:48',1,4000,6.3,1,2,NULL);

-- 
-- Definition of vb_imageslide
-- 

CREATE TABLE IF NOT EXISTS `vb_imageslide` (
  `ImageName` varchar(255) DEFAULT NULL,
  `ImageType` varchar(255) DEFAULT NULL,
  `SortOrder` int(11) DEFAULT '0',
  `CDate` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_imageslide
-- 

INSERT INTO `vb_imageslide`(`ImageName`,`ImageType`,`SortOrder`,`CDate`) VALUES
('0f130b24-56fb-468a-b106-882f604a287b.jpg','Web',1,'2018-08-08 11:33:38');

-- 
-- Definition of vb_item
-- 

CREATE TABLE IF NOT EXISTS `vb_item` (
  `vit_id` int(11) NOT NULL AUTO_INCREMENT,
  `vit_custID` varchar(255) DEFAULT NULL,
  `vit_salID` varchar(255) DEFAULT '',
  `vit_recID` varchar(255) DEFAULT '',
  `vit_code` varchar(255) NOT NULL,
  `vit_shipTo` varchar(255) DEFAULT NULL,
  `vit_refund` int(1) DEFAULT '0',
  `vit_empID` varchar(255) DEFAULT NULL,
  `vit_totalProductFeeINUsd` double DEFAULT NULL,
  `vit_totalProductFeeINYan` double DEFAULT NULL,
  `vit_serviceChargeUsd` double DEFAULT NULL,
  `vit_totalLCFeeINUsd` double DEFAULT NULL,
  `vit_totalINUsd` double DEFAULT NULL,
  `vit_deposit` double DEFAULT NULL,
  `vit_balance` double DEFAULT NULL,
  `vit_exDollar` double DEFAULT '1',
  `vit_exKhmer` double DEFAULT '0',
  `vit_exChina` double DEFAULT '0',
  `vit_chinaWarehouse` varchar(255) DEFAULT NULL,
  `vit_camWarehouse` varchar(255) DEFAULT NULL,
  `vit_noOfDay` varchar(255) DEFAULT NULL,
  `vit_date` datetime DEFAULT NULL,
  `vit_status` int(11) DEFAULT NULL,
  `vit_type` varchar(255) DEFAULT NULL,
  `vit_remark` varchar(255) DEFAULT NULL,
  `vit_document` varchar(255) DEFAULT NULL,
  `vit_paymentDate` datetime DEFAULT NULL,
  `vit_goodsExpired` datetime DEFAULT NULL,
  `vit_expirationDate` datetime DEFAULT NULL,
  `vit_pmtExpireHours` int(3) DEFAULT '0',
  `vit_ordertype` int(2) DEFAULT NULL,
  `vit_priceCode` varchar(255) DEFAULT '',
  `vit_dateAvailable` datetime DEFAULT NULL,
  `vit_dateAvailableNum` int(11) DEFAULT NULL,
  PRIMARY KEY (`vit_id`,`vit_code`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_item
-- 

INSERT INTO `vb_item`(`vit_id`,`vit_custID`,`vit_salID`,`vit_recID`,`vit_code`,`vit_shipTo`,`vit_refund`,`vit_empID`,`vit_totalProductFeeINUsd`,`vit_totalProductFeeINYan`,`vit_serviceChargeUsd`,`vit_totalLCFeeINUsd`,`vit_totalINUsd`,`vit_deposit`,`vit_balance`,`vit_exDollar`,`vit_exKhmer`,`vit_exChina`,`vit_chinaWarehouse`,`vit_camWarehouse`,`vit_noOfDay`,`vit_date`,`vit_status`,`vit_type`,`vit_remark`,`vit_document`,`vit_paymentDate`,`vit_goodsExpired`,`vit_expirationDate`,`vit_pmtExpireHours`,`vit_ordertype`,`vit_priceCode`,`vit_dateAvailable`,`vit_dateAvailableNum`) VALUES
(3,'C00101','SO00016','OR00025','409AUG18-1C00101','',0,'E-00004',1587.3,10000,238.1,1.59,1826.99,0,0,1,4000,6.3,'L001','L001','12','2018-08-09 09:18:26',6,'Web Link',NULL,NULL,'2018-08-09 11:55:43','2018-09-03 00:00:00','2018-08-10 11:34:41',24,0,'PC001',NULL,NULL),
(4,'C00041','','','509AUG18-1C00041','',0,'E-00005',0,0,0,0,74.2900009155273,0,0,1,4000,6.3,'','','','2018-08-09 09:19:07',2,'Web Link',NULL,NULL,NULL,NULL,NULL,0,NULL,'',NULL,NULL),
(5,'C00101','SO00004','OR00008','409AUG18-2C00101','',1,'E-00004',3.18,20,0.48,3.18,6.84,0,0,1,4000,6.3,'L001','L001','12','2018-08-09 09:20:58',11,'Web Link',NULL,NULL,'2018-08-09 09:32:38','2018-09-03 00:00:00','2018-08-10 09:45:23',24,0,'PC001',NULL,NULL),
(6,'C00101','','','409AUG18-3C00101','',0,'E-00004',2.06,13,0.31,0.32,2.69,0,0,1,4000,6.3,'L002','L002','12','2018-08-09 09:26:34',2,'Web Link',NULL,NULL,NULL,'2018-09-03 00:00:00','2018-08-10 04:37:32',12,0,'PC001',NULL,NULL),
(7,'C00041','','','509AUG18-2C00041','',0,'E-00005',61.58,388,12.32,13.17,87.07,0,0,1,4000,6.3,'L002','L002','12','2018-08-09 09:28:36',3,'Web Link',NULL,NULL,NULL,'2018-09-03 00:00:00','2018-08-10 04:38:06',12,0,'PC002',NULL,NULL),
(8,'C00100','','','509AUG18-1C00100','',0,'E-00005',0,0,0,0,253.649993896484,0,0,1,4000,6.3,'','','','2018-08-09 10:08:39',2,'Web Link',NULL,NULL,NULL,NULL,NULL,0,NULL,'',NULL,NULL),
(9,'C00100','','','509AUG18-2C00100','',0,'E-00005',0,0,0,0,412.540008544922,0,0,1,4000,6.3,'','','','2018-08-09 10:09:10',13,'Web Link',NULL,NULL,NULL,NULL,NULL,0,4,'',NULL,NULL),
(10,'C00100','','','509AUG18-3C00100','',0,'E-00005',0,0,0,0,0,0,0,1,4000,6.3,'','','','2018-08-09 10:47:09',2,'Request Quote',NULL,NULL,NULL,NULL,NULL,0,NULL,'',NULL,NULL),
(11,'C00101','SO00017','OR00026','409AUG18-4C00101','',1,'E-00004',4694.44,29575,704.17,18,5416.61,0,0,1,4000,6.3,'L002','L001','12','2018-08-09 11:02:19',6,'Web Link',NULL,NULL,'2018-08-09 11:56:03','2018-09-03 00:00:00','2018-08-10 13:26:33',24,0,'PC001',NULL,NULL),
(12,'C00101','SO00015','OR00024','409AUG18-5C00101','',0,'E-00004',809.53,5100,121.43,15,945.96,0,0,1,4000,6.3,'L002','L001','12','2018-08-09 11:02:44',5,'Web Link',NULL,NULL,'2018-08-09 11:55:22','2018-09-03 00:00:00','2018-08-10 11:33:07',24,0,'PC001',NULL,NULL),
(13,'C00100','','','509AUG18-4C00100','',0,'E-00005',23.17,146,3.48,0.32,26.97,0,0,1,4000,6.3,'L003','L002','12','2018-08-09 11:16:12',13,'Item Expiration Date',NULL,NULL,NULL,'2018-09-03 00:00:00','2018-08-10 03:29:33',12,2,'PC001',NULL,NULL),
(14,'C00101','SO00011','OR00017','409AUG18-6C00101','',0,'E-00004',85.71,540,12.86,1.42,99.99,0,0,1,4000,6.3,'L001','L001','12','2018-08-09 11:36:22',5,'Web Link',NULL,NULL,'2018-08-09 11:44:41','2018-09-03 00:00:00','2018-08-10 11:44:14',24,0,'PC001',NULL,NULL),
(15,'C00101','SO00013','OR00020','409AUG18-7C00101','',0,'E-00004',87.31,550,13.1,0,100.41,0,0,1,4000,6.3,'L001','L001','12','2018-08-09 11:48:05',5,'Web Link',NULL,NULL,'2018-08-09 11:51:12','2018-09-03 00:00:00','2018-08-10 11:50:52',24,0,'PC001',NULL,NULL),
(16,'C00100','SO00018','OR00029','509AUG18-5C00100','',0,'E-00005',1833.65,11552,275.05,0,2108.7,0,0,1,4000,6.3,'L002','L002','12','2018-08-09 11:54:02',5,'Request Quote',NULL,NULL,'2018-08-09 12:04:58','2018-09-03 00:00:00','2018-08-10 00:03:06',12,0,'PC001',NULL,NULL),
(17,'C00101','SO00019','OR00033','409AUG18-8C00101','',1,'E-00004',2000,2.9,300,20,1160,0,0,1,4000,6.3,'L002','L001','12','2018-08-09 13:35:52',9,'Request Quote',NULL,NULL,'2018-08-09 13:52:52','2018-09-03 00:00:00','2018-08-10 14:48:37',24,0,'PC001',NULL,NULL),
(18,'C00100','','','510AUG18-6C00100',NULL,0,'E-00005',0,0,0,0,15.8699998855591,0,0,1,4000,6.3,'','','','2018-08-10 14:16:32',2,'Request Quote',NULL,NULL,NULL,NULL,NULL,0,NULL,'',NULL,NULL),
(19,'C00100','','','510AUG18-7C00100',NULL,0,'E-00005',0,0,0,0,15.8699998855591,0,0,1,4000,6.3,'','','','2018-08-10 14:21:15',2,'Request Quote',NULL,NULL,NULL,NULL,NULL,0,NULL,'',NULL,NULL),
(20,'C00100','','','510AUG18-8C00100',NULL,0,'E-00005',0,0,0,0,158.729995727539,0,0,1,4000,6.3,'','','','2018-08-10 15:06:01',2,'Request Quote',NULL,NULL,NULL,NULL,NULL,0,NULL,'',NULL,NULL);

-- 
-- Definition of vb_itemcart
-- 

CREATE TABLE IF NOT EXISTS `vb_itemcart` (
  `vit_id` int(11) NOT NULL AUTO_INCREMENT,
  `custId` varchar(255) DEFAULT NULL,
  `vit_name` varchar(200) DEFAULT NULL,
  `vit_link` text,
  `vit_photo` varchar(150) DEFAULT NULL,
  `vit_size` varchar(100) DEFAULT NULL,
  `vit_color` varchar(100) DEFAULT NULL,
  `vit_qty` double DEFAULT NULL,
  `vit_unitPriceYan` double DEFAULT NULL,
  `vit_totalProductFeeYan` double DEFAULT NULL,
  `vit_lcFeeYan` double DEFAULT NULL,
  `vit_chinaInvoiceCode` varchar(255) DEFAULT NULL,
  `vit_truckingNoInChina` varchar(255) DEFAULT NULL,
  `vit_remark` varchar(100) DEFAULT NULL,
  `vit_unitPriceDollar` double DEFAULT NULL,
  `vit_amountDollar` varchar(255) DEFAULT NULL,
  `vit_lcFeeDollar` double DEFAULT NULL,
  `vit_document` varchar(255) DEFAULT NULL,
  `vit_vendor` varchar(100) DEFAULT NULL,
  `vit_status` varchar(255) DEFAULT NULL,
  `vit_type` varchar(255) DEFAULT NULL,
  `vit_date` datetime DEFAULT NULL,
  `vit_color_tmp` varchar(255) DEFAULT NULL,
  `vit_size_tmp` varchar(255) DEFAULT NULL,
  `vit_price_tmp` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`vit_id`)
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_itemcart
-- 

INSERT INTO `vb_itemcart`(`vit_id`,`custId`,`vit_name`,`vit_link`,`vit_photo`,`vit_size`,`vit_color`,`vit_qty`,`vit_unitPriceYan`,`vit_totalProductFeeYan`,`vit_lcFeeYan`,`vit_chinaInvoiceCode`,`vit_truckingNoInChina`,`vit_remark`,`vit_unitPriceDollar`,`vit_amountDollar`,`vit_lcFeeDollar`,`vit_document`,`vit_vendor`,`vit_status`,`vit_type`,`vit_date`,`vit_color_tmp`,`vit_size_tmp`,`vit_price_tmp`) VALUES
(15,'C00100','Lenovo/Lenovo Saver Blade 7000 2018 Edition UIY Desktop Computer Gaming Host Eating Chicken Game Machine','https://detail.tmall.com/item.htm?spm=875.7931836/B.20161013.10.66144265iWGpAw&abtest=&pvid=9825573d-081b-41eb-ac89-03c14e50bdab&pos=10&abbucket=&acm=201509290.1003.1.1286473&id=569172403445&scm=1007.12710.81708.100200300000000','http://img02.taobaocdn.com/bao/uploaded/i3/126446588/TB1GrMxlIj_B1NjSZFHXXaDWpXa_!!0-item_pic.jpg','','',1,0,0,0,'','','',825.24,'825.24',0,'','0','Padding Audit','Web Link','2018-08-09 09:32:23',NULL,NULL,NULL),
(18,'C00100','I5 i7 eat chicken computer desktop full set of home games water cooled host high with gtx1060 assembly machine','https://detail.tmall.com/item.htm?spm=a230r.1.14.49.41aa514fSLhjLJ&id=567731722423&ns=1&abbucket=19&skuId=3620178929771','http://img02.taobaocdn.com/bao/uploaded/i4/2012656134/TB2wJv5FFGWBuNjy0FbXXb4sXXa_!!2012656134-0-item_pic.jpg','','',1,0,0,0,'','','',523.65,'523.65',0,'','0','Padding Audit','Web Link','2018-08-09 10:10:50',NULL,NULL,NULL),
(19,'C00100','Celestron star Trang C10_NGT OTA astronomical telescope main mirror','https://detail.tmall.com/item.htm?id=12309656199&spm=875.7931836/B.2017085.2.6b474265dSEiQW&scm=1007.12144.81309.73321_0&pvid=91c3287a-07e2-4baf-b523-5d73d1dad96c&utparam={%22x_hestia_source%22:%2273321%22,%22x_object_type%22:%22item%22,%22x_mt%22:8,%22x_src%22:%2273321%22,%22x_pos%22:1,%22x_pvid%22:%2291c3287a-07e2-4baf-b523-5d73d1dad96c%22,%22x_object_id%22:12309656199}','http://img02.taobaocdn.com/bao/uploaded/i7/T1ZQFPXgxCXXaeSqZU_013630.jpg','','',1,0,0,0,'','','',1264.92,'1264.92',0,'','0','Padding Audit','Web Link','2018-08-09 10:24:33',NULL,NULL,NULL),
(21,'C00101','比多乐来一口PK脆休闲点心面 泰国酱鸡味336g 膨化食品 怀旧零食超市Tmall.com上，就购了理想生活上','https://world.tmall.com/item/557830397214.htm?spm=a223d.7797701.8018832208.10.68a46d86TNZKQW&id=557830397214','http://img02.taobaocdn.com/bao/uploaded/i3/725677994/TB1ukBhov2H8KJjy0FcXXaDlFXa_!!0-item_pic.jpg','','',10,0,0,0,'','','',3.14,'31.400000000000002',0,'','0','Padding Audit','Web Link','2018-08-09 10:32:18',NULL,NULL,NULL),
(22,'C00101','比多乐来一口PK脆休闲点心面 泰国酱鸡味336g 膨化食品 怀旧零食超市Tmall.com上，就购了理想生活上','https://world.tmall.com/item/557830397214.htm?spm=a223d.7797701.8018832208.10.68a46d86TNZKQW&id=557830397214','http://img02.taobaocdn.com/bao/uploaded/i3/725677994/TB1ukBhov2H8KJjy0FcXXaDlFXa_!!0-item_pic.jpg','','',10,0,0,0,'','','',3.14,'31.400000000000002',0,'','0','Padding Audit','Web Link','2018-08-09 10:32:22',NULL,NULL,NULL),
(24,'C00101','I5 chicken game assembly computer desktop computer full set i7 high with water cooling Internet cafe office computer host','https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.33.1f9d4c33sbhI3a&id=19628167605&skuId=3821270746308&user_id=1049122653&cat_id=2&is_b=1&rn=17e35d709126476e52fc5d8d9d80dd97','http://img02.taobaocdn.com/bao/uploaded/i4/1049122653/O1CN011VT7D7Pq2MuHGQG_!!1049122653.jpg','','',1,0,0,0,'','','',633.02,'633.02',0,'','0','Padding Audit','Web Link','2018-08-09 10:44:14',NULL,NULL,NULL),
(25,'C00101','I5 chicken game assembly computer desktop computer full set i7 high with water cooling Internet cafe office computer host','https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.33.1f9d4c33sbhI3a&id=19628167605&skuId=3821270746308&user_id=1049122653&cat_id=2&is_b=1&rn=17e35d709126476e52fc5d8d9d80dd97','http://img02.taobaocdn.com/bao/uploaded/i4/1049122653/O1CN011VT7D7Pq2MuHGQG_!!1049122653.jpg','','',1,0,0,0,'','','',569.52,'569.52',0,'','0','Padding Audit','Web Link','2018-08-09 10:44:18',NULL,NULL,NULL),
(26,'C00101','I5 chicken game assembly computer desktop computer full set i7 high with water cooling Internet cafe office computer host','https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.33.1f9d4c33sbhI3a&id=19628167605&skuId=3821270746308&user_id=1049122653&cat_id=2&is_b=1&rn=17e35d709126476e52fc5d8d9d80dd97','http://img02.taobaocdn.com/bao/uploaded/i4/1049122653/O1CN011VT7D7Pq2MuHGQG_!!1049122653.jpg','','',1,0,0,0,'','','',569.52,'569.52',0,'','0','Padding Audit','Web Link','2018-08-09 10:44:26',NULL,NULL,NULL),
(27,'C00101','I5 chicken game assembly computer desktop computer full set i7 high with water cooling Internet cafe office computer host','https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.33.1f9d4c33sbhI3a&id=19628167605&skuId=3821270746308&user_id=1049122653&cat_id=2&is_b=1&rn=17e35d709126476e52fc5d8d9d80dd97','http://img02.taobaocdn.com/bao/uploaded/i4/1049122653/O1CN011VT7D7Pq2MuHGQG_!!1049122653.jpg','','',1,0,0,0,'','','',569.52,'569.52',0,'','0','Padding Audit','Web Link','2018-08-09 10:44:29',NULL,NULL,NULL),
(28,'C00101','I5 chicken game assembly computer desktop computer full set i7 high with water cooling Internet cafe office computer host','https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.33.1f9d4c33sbhI3a&id=19628167605&skuId=3821270746308&user_id=1049122653&cat_id=2&is_b=1&rn=17e35d709126476e52fc5d8d9d80dd97','http://img02.taobaocdn.com/bao/uploaded/i4/1049122653/O1CN011VT7D7Pq2MuHGQG_!!1049122653.jpg','','',1,0,0,0,'','','',569.52,'569.52',0,'','0','Padding Audit','Web Link','2018-08-09 10:44:31',NULL,NULL,NULL),
(30,'C00101','Australia music childrens playpen baby crawling mat toddler fence safety fence home indoor playground','https://detail.tmall.com/item.htm?spm=a221w.7782398.200001.10.37212631WLlUDj&pos=3&acm=201601215.1003.1.700922&id=38739726083&scm=1003.1.201601215.D2I_38739726083_700922','//img.alicdn.com/imgextra/i4/928800801/TB2Ubf3Xoz.BuNjt_bXXXcQmpXa_!!928800801.jpg','','',1,0,0,0,'','','',63.17,'63.17',0,'','0','Padding Audit','Web Link','2018-08-09 10:49:31',NULL,NULL,NULL),
(38,'C00100','West Railway Light Kinetic Energy Watch Small Blue Needle Watch Watch Watch Watch Watch Watch Waterproof Belt Mens Leisure Watch AO900006Btmall.hk International','https://detail.tmall.hk/hk/item.htm?id=573190196967&spm=875.7931836/B.2017041.8.3a5c4265bP4JJP&scm=1007.12144.81309.73133_0&pvid=f6c0c8cf-79da-431b-85a1-1b696029967f&utparam={%22x_hestia_source%22:%2273133%22,%22x_object_type%22:%22item%22,%22x_mt%22:8,%22x_src%22:%2273133%22,%22x_pos%22:5,%22x_pvid%22:%22f6c0c8cf-79da-431b-85a1-1b696029967f%22,%22x_object_id%22:573190196967}','//img.alicdn.com/imgextra/i4/3925705463/TB274lAkY3nBKNjSZFMXXaUSFXa_!!3925705463.jpg','','',1,0,0,0,'','','',301.43,'301.43',0,'','0','Padding Audit','Web Link','2018-08-11 11:14:07',NULL,NULL,NULL),
(39,'C00100','原创大码女装秋冬200斤胖mm木耳边半高领遮肉针织长袖打底衫8709','https://detail.1688.com/offer/574388902016.html','https://cbu01.alicdn.com/img/ibank/2018/609/609/9160906906_794403737.jpg','','',3,0,0,0,'','','',4.92,'14.76',0,'','0','Padding Audit','Web Link','2018-08-13 10:27:51',NULL,NULL,NULL),
(40,'C00100','Hasee / Shenzhou Zhanshen ZX7CP5S28 Generation i5 Eat Chicken GTX1060 6G Unique Solid State 16G Memory 15.6 inches 72% IPS Student Portable Gamebook Laptop','https://detail.tmall.com/item.htm?spm=a230r.1.14.1.447e57e1zqQGBH&id=565130049379&cm_id=140105335569ed55e27b&abbucket=19&sku_properties=1627207:28341','http://img02.taobaocdn.com/bao/uploaded/i1/255921860/TB2ADoRnBjTBKNjSZFDXXbVgVXa_!!255921860-0-item_pic.jpg','','',1,0,0,0,'','','',1174.44,'1174.44',0,'','0','Padding Audit','Web Link','2018-08-13 10:48:11',NULL,NULL,NULL);

-- 
-- Definition of vb_itemdetail
-- 

CREATE TABLE IF NOT EXISTS `vb_itemdetail` (
  `vit_iddetail` int(11) NOT NULL AUTO_INCREMENT,
  `vit_id` int(11) DEFAULT NULL,
  `vit_name` varchar(200) DEFAULT NULL,
  `vit_link` text,
  `vit_photo` varchar(150) DEFAULT NULL,
  `vit_size` varchar(100) DEFAULT NULL,
  `vit_color` varchar(100) DEFAULT NULL,
  `vit_qty` double(11,0) DEFAULT NULL,
  `vit_unitPriceYan` double DEFAULT NULL,
  `vit_totalProductFeeYan` double DEFAULT NULL,
  `vit_lcFeeYan` double DEFAULT NULL,
  `vit_chinaInvoiceCode` varchar(255) DEFAULT NULL,
  `vit_truckingNoInChina` varchar(255) DEFAULT NULL,
  `vit_remark` varchar(100) DEFAULT NULL,
  `vit_unitPriceDollar` double DEFAULT NULL,
  `vit_amountDollar` double DEFAULT NULL,
  `vit_lcFeeDollar` double DEFAULT NULL,
  `vit_document` varchar(255) DEFAULT NULL,
  `vit_vendor` varchar(100) DEFAULT NULL,
  `vit_status` varchar(255) DEFAULT NULL,
  `vit_type` varchar(255) DEFAULT NULL,
  `vit_date` datetime DEFAULT NULL,
  `vit_someqty` double NOT NULL,
  `vit_itemId` varchar(255) DEFAULT NULL,
  `vit_refund` int(1) DEFAULT '0',
  `dou_vitMeaValue` double DEFAULT NULL,
  `dou_vitMeaType` varchar(10) DEFAULT NULL,
  `dou_vitMeaPrice` double DEFAULT NULL,
  `tin_isPaid` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`vit_iddetail`)
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_itemdetail
-- 

INSERT INTO `vb_itemdetail`(`vit_iddetail`,`vit_id`,`vit_name`,`vit_link`,`vit_photo`,`vit_size`,`vit_color`,`vit_qty`,`vit_unitPriceYan`,`vit_totalProductFeeYan`,`vit_lcFeeYan`,`vit_chinaInvoiceCode`,`vit_truckingNoInChina`,`vit_remark`,`vit_unitPriceDollar`,`vit_amountDollar`,`vit_lcFeeDollar`,`vit_document`,`vit_vendor`,`vit_status`,`vit_type`,`vit_date`,`vit_someqty`,`vit_itemId`,`vit_refund`,`dou_vitMeaValue`,`dou_vitMeaType`,`dou_vitMeaPrice`,`tin_isPaid`) VALUES
(1,1,'2018 summer new Korean fashion temperament small fragrance suit womens summer casual sports fashion two-piece suit','https://item.taobao.com/item.htm?spm=a21bo.7929913.198967.28.52474174joxkA4&id=568911867061','//gd2.alicdn.com/imgextra/i3/1857431477/TB2EucGw1uSBuNjSsplXXbe8pXa_!!1857431477.jpg','','',5,23,115,0,'','','',3.65,18.25,0,'','V00018','Pending peyment','Web Link','2018-08-06 10:46:28',5,'C00042-00001',0,NULL,NULL,NULL,1),
(2,1,'2018 summer new Korean fashion temperament small fragrance suit womens summer casual sports fashion two-piece suit','https://item.taobao.com/item.htm?spm=a21bo.7929913.198967.28.52474174joxkA4&id=568911867061','//gd3.alicdn.com/imgextra/i4/1857431477/TB2rBMMoNGYBuNjy0FnXXX5lpXa_!!1857431477.jpg','','',5,100,500,0,'','','',15.87,79.37,0,'','V00017','Pending peyment','Web Link','2018-08-06 10:46:28',0,'C00042-00002',0,NULL,NULL,NULL,1),
(3,1,'2018 summer new Korean fashion temperament small fragrance suit womens summer casual sports fashion two-piece suit','https://item.taobao.com/item.htm?spm=a21bo.7929913.198967.28.52474174joxkA4&id=568911867061','//gd4.alicdn.com/imgextra/i2/1857431477/TB2XRAMoNGYBuNjy0FnXXX5lpXa_!!1857431477.jpg','','',1,45,45,0,'','','',7.14,7.14,0,'','V00003','Pending peyment','Web Link','2018-08-06 10:46:28',0,'C00042-00003',0,NULL,NULL,NULL,1),
(4,1,'2018 summer new Korean fashion temperament small fragrance suit womens summer casual sports fashion two-piece suit','https://item.taobao.com/item.htm?spm=a21bo.7929913.198967.28.52474174joxkA4&id=568911867061','//gd3.alicdn.com/imgextra/i4/1857431477/TB2gFIfoH5YBuNjSspoXXbeNFXa_!!1857431477.jpg','','',1,34,34,0,'','','',5.4,5.4,0,'','V00017','Pending peyment','Web Link','2018-08-06 10:46:28',1,'C00042-00004',0,NULL,NULL,NULL,1),
(5,2,'Baseball clothing female spring and autumn Korean students bf loose 2018 new tide jacket Harajuku style wild short paragraph thin coat','https://item.taobao.com/item.htm?spm=a21bo.7929913.198967.63.52474174joxkA4&id=547129135767','//gd2.alicdn.com/imgextra/i3/2934856424/TB22rueafImBKNjSZFlXXc43FXa_!!2934856424.jpg','','',1,100,100,0,NULL,'123123345','',15.87,15.87,0,'','V00016','Pending peyment','Web Link','2018-08-06 11:20:18',1,'C00042-00009',0,NULL,NULL,NULL,0),
(6,2,'Baseball clothing female spring and autumn Korean students bf loose 2018 new tide jacket Harajuku style wild short paragraph thin coat','https://item.taobao.com/item.htm?spm=a21bo.7929913.198967.63.52474174joxkA4&id=547129135767','//gd3.alicdn.com/imgextra/i4/2934856424/TB26vXriS0jpuFjy0FlXXc0bpXa_!!2934856424.jpg','','',1,500,500,0,NULL,'12324435435435','',79.37,79.37,0,'','V00007','Pending peyment','Web Link','2018-08-06 11:20:18',1,'C00042-00010',0,NULL,NULL,NULL,0),
(7,3,'小迷糊麦卢卡蜂蜜黑面膜21片春雨清爽补水清洁面膜男女学生超市Tmall.com上，就购了理想生活上','https://world.tmall.com/item/550988772571.htm?id=550988772571&spm=875.7931836/B.2017039.4.66144265rGQv2o&scm=1007.12144.81309.73136_0&pvid=2e1727e8-6aa1-43c0-bcb7-6ccce43aff48&utparam={%22x_hestia_source%22:%2273136%22,%22x_object_type%22:%22item%22,%22x_mt%22:8,%22x_src%22:%2273136%22,%22x_pos%22:1,%22x_pvid%22:%222e1727e8-6aa1-43c0-bcb7-6ccce43aff48%22,%22x_object_id%22:550988772571}','http://img02.taobaocdn.com/bao/uploaded/i4/725677994/TB2ncUVJQSWBuNjSszdXXbeSpXa_!!725677994-0-item_pic.jpg','','',1,10000,10000,10,NULL,NULL,'',1587.3,1587.3,1.5900000333786,'','V00001','Pending peyment','Web Link','2018-08-09 09:18:26',0,'C00101-00057',0,NULL,NULL,NULL,1),
(8,4,'Vatae 960-4g video card technology jia 1060 3G chicken card preparation GTX970 1050TI 1065','https://item.taobao.com/item.htm?spm=a230r.1.14.308.7202173bHkAdhE&id=568456588617&ns=1&abbucket=19#detail','//gd2.alicdn.com/imgextra/i4/195683753/TB2YOsfEL1TBuNjy0FjXXajyXXa_!!195683753.jpg','','',4,0,0,0,'','','',74.29,297.16,0,'','','Padding Audit','Web Link','2018-08-09 09:19:07',0,NULL,0,NULL,NULL,NULL,0),
(9,5,'小迷糊麦卢卡蜂蜜黑面膜21片春雨清爽补水清洁面膜男女学生超市Tmall.com上，就购了理想生活上','https://world.tmall.com/item/550988772571.htm?id=550988772571&spm=875.7931836/B.2017039.4.66144265rGQv2o&scm=1007.12144.81309.73136_0&pvid=2e1727e8-6aa1-43c0-bcb7-6ccce43aff48&utparam={%22x_hestia_source%22:%2273136%22,%22x_object_type%22:%22item%22,%22x_mt%22:8,%22x_src%22:%2273136%22,%22x_pos%22:1,%22x_pvid%22:%222e1727e8-6aa1-43c0-bcb7-6ccce43aff48%22,%22x_object_id%22:550988772571}','http://img02.taobaocdn.com/bao/uploaded/i4/725677994/TB2ncUVJQSWBuNjSszdXXbeSpXa_!!725677994-0-item_pic.jpg','','',1,10,10,10,'','T349259955879','',1.59,1.59,1.5900000333786,'','V00001','Pending peyment','Web Link','2018-08-09 09:20:58',0,'C00101-00001',3,NULL,NULL,NULL,0),
(10,5,'小迷糊麦卢卡蜂蜜黑面膜21片春雨清爽补水清洁面膜男女学生超市Tmall.com上，就购了理想生活上','https://world.tmall.com/item/550988772571.htm?id=550988772571&spm=875.7931836/B.2017039.4.66144265rGQv2o&scm=1007.12144.81309.73136_0&pvid=2e1727e8-6aa1-43c0-bcb7-6ccce43aff48&utparam={%22x_hestia_source%22:%2273136%22,%22x_object_type%22:%22item%22,%22x_mt%22:8,%22x_src%22:%2273136%22,%22x_pos%22:1,%22x_pvid%22:%222e1727e8-6aa1-43c0-bcb7-6ccce43aff48%22,%22x_object_id%22:550988772571}','http://img02.taobaocdn.com/bao/uploaded/i4/725677994/TB2ncUVJQSWBuNjSszdXXbeSpXa_!!725677994-0-item_pic.jpg','','',1,10,10,10,'','T366967438742','',1.59,1.59,1.5900000333786,'','V00021','Pending peyment','Web Link','2018-08-09 09:20:59',0,'C00101-00002',3,NULL,NULL,NULL,0),
(11,6,'小迷糊麦卢卡蜂蜜黑面膜21片春雨清爽补水清洁面膜男女学生超市Tmall.com上，就购了理想生活上','https://world.tmall.com/item/550988772571.htm?id=550988772571&spm=875.7931836/B.2017039.4.66144265rGQv2o&scm=1007.12144.81309.73136_0&pvid=2e1727e8-6aa1-43c0-bcb7-6ccce43aff48&utparam={%22x_hestia_source%22:%2273136%22,%22x_object_type%22:%22item%22,%22x_mt%22:8,%22x_src%22:%2273136%22,%22x_pos%22:1,%22x_pvid%22:%222e1727e8-6aa1-43c0-bcb7-6ccce43aff48%22,%22x_object_id%22:550988772571}','http://img02.taobaocdn.com/bao/uploaded/i4/725677994/TB2ncUVJQSWBuNjSszdXXbeSpXa_!!725677994-0-item_pic.jpg','','',1,13,13,2,'','','',2.06,2.06,0.319999992847443,'','V00003','Pending peyment','Web Link','2018-08-09 09:26:35',0,NULL,0,NULL,NULL,NULL,0),
(12,7,'RKHL xiaowen summer 2018 south Korean version oversize baggy striped T-shirt with matching color, round collar and short sleeves','http://item.taobao.com/item.htm?spm=5706.1529727.a31f1.2.4e956c7exLcpS0&scm=1007.12883.24200.100200300000003&id=567666079799&pvid=ab0a83d8-4ae9-4a89-80b8-a6d61ccf92ad','//gd3.alicdn.com/imgextra/i1/725699016/TB2ONKAlf9TBuNjy0FcXXbeiFXa_!!725699016.jpg','','',1,22,22,45,'','','',3.49,3.49,7.1399998664856,'','V00001','Pending peyment','Web Link','2018-08-09 09:28:36',0,NULL,0,NULL,NULL,NULL,0),
(13,7,'RKHL xiaowen summer 2018 south Korean version oversize baggy striped T-shirt with matching color, round collar and short sleeves','http://item.taobao.com/item.htm?spm=5706.1529727.a31f1.2.4e956c7exLcpS0&scm=1007.12883.24200.100200300000003&id=567666079799&pvid=ab0a83d8-4ae9-4a89-80b8-a6d61ccf92ad','//gd3.alicdn.com/imgextra/i1/725699016/TB2ONKAlf9TBuNjy0FcXXbeiFXa_!!725699016.jpg','','',1,322,322,5,'','','',51.11,51.11,0.790000021457672,'','V00005','Pending peyment','Web Link','2018-08-09 09:28:36',0,NULL,0,NULL,NULL,NULL,0),
(14,7,'Send 24 - inch monitor i5 desktop non - second - hand Internet cafe games assembled computer host complete LOL','https://item.taobao.com/item.htm?spm=a230r.1.14.109.41aa514fSLhjLJ&id=570316959237&ns=1&abbucket=19#detail','//gd3.alicdn.com/imgextra/i1/1087990021/TB2c7LOsStYBeNjSspaXXaOOFXa_!!1087990021.jpg','','',1,44,44,33,'','','',6.98,6.98,5.23999977111816,'','V00005','Pending peyment','Web Link','2018-08-09 09:28:36',0,NULL,0,NULL,NULL,NULL,0),
(15,8,'Xiaomi/ millet millet box 4 HD network smart TV set-top box 4K voice box wifi','https://detail.tmall.com/item.htm?spm=875.7931836/B.20161013.1.66144265iWGpAw&abtest=&pvid=9825573d-081b-41eb-ac89-03c14e50bdab&pos=1&abbucket=&acm=201509290.1003.1.1286473&id=565115417972&scm=1007.12710.81708.100200300000000','http://img02.taobaocdn.com/bao/uploaded/i2/2616970884/TB1JVpCdUz.BuNjt_j7XXX0nFXa_!!0-item_pic.jpg','','',1,0,0,0,'','','',47.46,47.46,0,'','','Padding Audit','Web Link','2018-08-09 10:08:39',0,NULL,0,NULL,NULL,NULL,0),
(16,8,'Samsung Monitor official 27 inch curved screen C27F396FHC LCD curved screen desktop computer screen ps4','https://detail.tmall.com/item.htm?spm=875.7931836/B.20161015.6.66144265iWGpAw&abtest=&pvid=67a491d0-3cb3-4442-a608-f3934052708a&pos=6&abbucket=&acm=201509290.1003.1.1286473&id=536004232121&scm=1007.12710.81708.100200300000000','//img.alicdn.com/imgextra/i3/2769711329/TB2vRCrhg0kpuFjSspdXXX4YXXa_!!2769711329.jpg','','',1,0,0,0,'','','ddadsfdsfdsfdsfdsds',206.19,206.19,0,'','','Padding Audit','Web Link','2018-08-09 10:08:39',0,NULL,0,NULL,NULL,NULL,0),
(17,9,'I5 quad-core eating chicken game assembly diy desktop office computer Jedi survival network cafes high with a full set','https://detail.tmall.com/item.htm?spm=a230r.1.14.155.41aa514fSLhjLJ&id=571795491305&ns=1&abbucket=19','http://img02.taobaocdn.com/bao/uploaded/i1/766439547/TB2Vuemz79WBuNjSspeXXaz5VXa_!!766439547-0-item_pic.jpg','','',1,0,0,0,'','','',412.54,412.54,0,'','','Padding Audit','Web Link','2018-08-09 10:09:10',0,NULL,0,NULL,NULL,NULL,0),
(18,10,'','https://www.tmall.com/?spm=a220o.1000855.0.0.3a793ef0vwzDDR','','324','gfhghghgh',123,0,0,0,'','','dfg',0,0,0,'e9427e52-640c-48a8-adbe-a22ff723b087.jpg','0','Padding Audit','Request Quote','2018-08-09 10:47:09',0,NULL,0,NULL,NULL,NULL,0),
(19,11,'比多乐来一口PK脆休闲点心面 泰国酱鸡味336g 膨化食品 怀旧零食超市Tmall.com上，就购了理想生活上','https://world.tmall.com/item/557830397214.htm?spm=a223d.7797701.8018832208.10.68a46d86TNZKQW&id=557830397214','http://img02.taobaocdn.com/bao/uploaded/i3/725677994/TB1ukBhov2H8KJjy0FcXXaDlFXa_!!0-item_pic.jpg','','',10,25.2,252,18.9,'','','',4,40,3,'','V00002','Pending peyment','Web Link','2018-08-09 11:02:19',0,'C00101-00060',1,NULL,NULL,NULL,1),
(20,11,'I5 chicken game assembly computer desktop computer full set i7 high with water cooling Internet cafe office computer host','https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.33.1f9d4c33sbhI3a&id=19628167605&skuId=3821270746308&user_id=1049122653&cat_id=2&is_b=1&rn=17e35d709126476e52fc5d8d9d80dd97','http://img02.taobaocdn.com/bao/uploaded/i4/1049122653/O1CN011VT7D7Pq2MuHGQG_!!1049122653.jpg','','',10,2000,20000,31.5,'','','',317.46,3174.6,5,'','V00003','Pending peyment','Web Link','2018-08-09 11:02:19',0,'C00101-00061',1,NULL,NULL,NULL,1),
(21,11,'Australia music childrens playpen baby crawling mat toddler fence safety fence home indoor playground','https://detail.tmall.com/item.htm?spm=a221w.7782398.200001.10.37212631WLlUDj&pos=3&acm=201601215.1003.1.700922&id=38739726083&scm=1003.1.201601215.D2I_38739726083_700922','//img.alicdn.com/imgextra/TB1gQcOreuSBuNjy1XcL6UYjFXa','','',10,800,8000,31.5,'','','',126.98,1269.84,5,'','V00001','Pending peyment','Web Link','2018-08-09 11:02:19',0,'C00101-00062',3,NULL,NULL,NULL,1),
(22,11,'300kg Electronic Scale Commercial 100kg Electronic Bench Scale Valuable Accurate Weighing 150 Express Scale 200','https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.33.51f430e4zAukPB&id=39155375550&skuId=3614127393551&user_id=2106234702&cat_id=50036640&is_b=1&rn=93c8a33c4a9407fed56438a8b62e1f1c','//img.alicdn.com/imgextra/i4/2106234702/TB2F7.rnJrJ8KJjSspaXXXuKpXa_!!2106234702.jpg','','',10,132.3,1323,31.5,'','','',21,210,5,'','V00004','Pending peyment','Web Link','2018-08-09 11:02:19',0,'C00101-00063',0,NULL,NULL,NULL,1),
(23,12,'浪琴Longines手表嘉兰嘉岚系列镶钻石英女表L4.209.4.11.2','https://detail.tmall.com/item.htm?id=523070513726&acm=lb-zebra-2076-322472.1003.4.598531&scm=1003.4.lb-zebra-2076-322472.ITEM_523070513726_598531','//img.alicdn.com/imgextra/i1/2467959793/TB2o1ocIgmTBuNjy1XbXXaMrVXa_!!2467959793.png','','',10,150,1500,31.5,NULL,NULL,'',23.81,238.1,5,'','V00001','Pending peyment','Web Link','2018-08-09 11:02:44',0,'C00101-00052',0,NULL,NULL,NULL,1),
(24,12,'300kg Electronic Scale Commercial 100kg Electronic Bench Scale Valuable Accurate Weighing 150 Express Scale 200','https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.33.51f430e4zAukPB&id=39155375550&skuId=3614127393551&user_id=2106234702&cat_id=50036640&is_b=1&rn=93c8a33c4a9407fed56438a8b62e1f1c','//img.alicdn.com/imgextra/i3/2106234702/TB2h.RBcgaTBuNjSszfXXXgfpXa_!!2106234702.jpg','','',10,160,1600,31.5,'','','',25.4,253.97,5,'','V00002','Pending peyment','Web Link','2018-08-09 11:02:44',0,'C00101-00053',0,NULL,NULL,NULL,0),
(25,12,'Australia music childrens playpen baby crawling mat toddler fence safety fence home indoor playground','https://detail.tmall.com/item.htm?spm=a221w.7782398.200001.10.37212631WLlUDj&pos=3&acm=201601215.1003.1.700922&id=38739726083&scm=1003.1.201601215.D2I_38739726083_700922','//img.alicdn.com/imgextra/i4/928800801/TB2Ubf3Xoz.BuNjt_bXXXcQmpXa_!!928800801.jpg','','',10,200,2000,31.5,'','','',31.75,317.46,5,'','V00003','Pending peyment','Web Link','2018-08-09 11:02:44',0,'C00101-00054',0,NULL,NULL,NULL,0),
(26,13,'Celestron star Trang C10_NGT OTA astronomical telescope main mirror','https://detail.tmall.com/item.htm?id=12309656199&spm=875.7931836/B.2017085.2.6b474265dSEiQW&scm=1007.12144.81309.73321_0&pvid=91c3287a-07e2-4baf-b523-5d73d1dad96c&utparam={%22x_hestia_source%22:%2273321%22,%22x_object_type%22:%22item%22,%22x_mt%22:8,%22x_src%22:%2273321%22,%22x_pos%22:1,%22x_pvid%22:%2291c3287a-07e2-4baf-b523-5d73d1dad96c%22,%22x_object_id%22:12309656199}','http://img02.taobaocdn.com/bao/uploaded/i7/T1ZQFPXgxCXXaeSqZU_013630.jpg','null','null',1,134,134,2,NULL,NULL,'asdasdasdasdasdasdas',21.27,21.27,0.319999992847443,'116b4bbb-cde4-4451-a5a6-e71486180a6a.png','V00001','Pending peyment','Request Quote','2018-08-09 11:16:12',0,NULL,0,NULL,NULL,NULL,0),
(27,13,'I7 grade GTX1060 16G eat chicken against the water cold game desktop assembly computer host office complete set','https://item.taobao.com/item.htm?spm=a230r.1.14.23.41aa514fSLhjLJ&id=566962867578&ns=1&abbucket=19#detail','//gd3.alicdn.com/imgextra/i2/2128581861/TB2pZh5JrSYBuNjSspiXXXNzpXa_!!2128581861.jpg','null','null',1,12,12,0,NULL,NULL,'asdasdasdasd',1.9,1.9,0,'c36ea2c5-35a5-4a27-8903-c4e7a54fb2db.jpg','V00002','Pending peyment','Request Quote','2018-08-09 11:16:12',0,NULL,0,NULL,NULL,NULL,0),
(28,14,'浪琴Longines手表嘉兰嘉岚系列镶钻石英女表L4.209.4.11.2','https://detail.tmall.com/item.htm?id=523070513726&acm=lb-zebra-2076-322472.1003.4.598531&scm=1003.4.lb-zebra-2076-322472.ITEM_523070513726_598531','//img.alicdn.com/imgextra/i1/2467959793/TB2o1ocIgmTBuNjy1XbXXaMrVXa_!!2467959793.png','','',1,540,540,8.95,NULL,NULL,'',85.71,85.71,1.41999995708466,'','V00001','Pending peyment','Web Link','2018-08-09 11:36:22',0,'C00101-00035',0,NULL,NULL,NULL,0),
(29,15,'300kg Electronic Scale Commercial 100kg Electronic Bench Scale Valuable Accurate Weighing 150 Express Scale 200','https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.33.51f430e4zAukPB&id=39155375550&skuId=3614127393551&user_id=2106234702&cat_id=50036640&is_b=1&rn=93c8a33c4a9407fed56438a8b62e1f1c','//img.alicdn.com/imgextra/i1/2106234702/TB23Ipnay6guuRkSmLyXXculFXa_!!2106234702.jpg','','',1,350,350,0,NULL,NULL,'',55.56,55.56,0,'','V00001','Pending peyment','Web Link','2018-08-09 11:48:05',0,'C00101-00043',0,NULL,NULL,NULL,0),
(30,15,'Australia music childrens playpen baby crawling mat toddler fence safety fence home indoor playground','https://detail.tmall.com/item.htm?spm=a221w.7782398.200001.10.37212631WLlUDj&pos=3&acm=201601215.1003.1.700922&id=38739726083&scm=1003.1.201601215.D2I_38739726083_700922','//img.alicdn.com/imgextra/i4/928800801/TB2PDZHFQyWBuNjy0FpXXassXXa_!!928800801.jpg','','',1,200,200,0,NULL,NULL,'',31.75,31.75,0,'','V00001','Pending peyment','Web Link','2018-08-09 11:48:05',0,'C00101-00044',0,NULL,NULL,NULL,0),
(31,16,'asd','https://detail.tmall.com/item.htm?id=12309656199&spm=875.7931836/B.2017085.2.6b474265dSEiQW&scm=1007.12144.81309.73321_0&pvid=91c3287a-07e2-4baf-b523-5d73d1dad96c&utparam={%22x_hestia_source%22:%2273321%22,%22x_object_type%22:%22item%22,%22x_mt%22:8,%22x_src%22:%2273321%22,%22x_pos%22:1,%22x_pvid%22:%2291c3287a-07e2-4baf-b523-5d73d1dad96c%22,%22x_object_id%22:12309656199}','','null','null',2,2453,4906,0,NULL,NULL,'asdasdasdasdasd',389.37,778.73,0,'','V00003','Pending peyment','Request Quote','2018-08-09 11:54:02',0,'C00100-00001',0,NULL,NULL,NULL,0),
(32,16,'123','https://detail.tmall.com/item.htm?id=12309656199&spm=875.7931836/B.2017085.2.6b474265dSEiQW&scm=1007.12144.81309.73321_0&pvid=91c3287a-07e2-4baf-b523-5d73d1dad96c&utparam={%22x_hestia_source%22:%2273321%22,%22x_object_type%22:%22item%22,%22x_mt%22:8,%22x_src%22:%2273321%22,%22x_pos%22:1,%22x_pvid%22:%2291c3287a-07e2-4baf-b523-5d73d1dad96c%22,%22x_object_id%22:12309656199}','','null','null',2,3323,6646,0,NULL,NULL,'sdasdasd',527.46,1054.92,0,'',NULL,'Pending peyment','Request Quote','2018-08-09 11:54:02',0,'C00100-00002',0,NULL,NULL,NULL,0),
(33,17,'N1','https://detail.tmall.com/item.htm?spm=a230r.1.14.67.53bdb518ZVoTD7&id=574582547254&ns=1&abbucket=3','','null','null',5,630,3150,31.5,'','T893893742595','',100,500,5,'','V00001','Pending peyment','Request Quote','2018-08-09 13:35:52',0,'C00101-00067',3,NULL,NULL,NULL,0),
(34,17,'N2','https://detail.tmall.com/item.htm?spm=a230r.1.14.67.53bdb518ZVoTD7&id=574582547254&ns=1&abbucket=3','','null','null',5,0,0,0,'','T625232634572','',0,0,0,'','V00001','Pending peyment','Request Quote','2018-08-09 13:35:52',0,'C00101-00068',2,NULL,NULL,NULL,0),
(35,17,'N3','https://detail.tmall.com/item.htm?spm=a230r.1.14.67.53bdb518ZVoTD7&id=574582547254&ns=1&abbucket=3','','null','null',5,630,3150,31.5,'','','',100,500,5,'','V00001','Pending peyment','Request Quote','2018-08-09 13:35:52',0,'C00101-00069',2,NULL,NULL,NULL,0),
(36,17,'N4','https://detail.tmall.com/item.htm?spm=a230r.1.14.67.53bdb518ZVoTD7&id=574582547254&ns=1&abbucket=3','','null','null',5,630,3150,31.5,'','T773445866635','',100,500,5,'','V00001','Pending peyment','Request Quote','2018-08-09 13:35:52',0,'C00101-00070',3,NULL,NULL,NULL,0),
(37,17,'N5','https://detail.tmall.com/item.htm?spm=a230r.1.14.67.53bdb518ZVoTD7&id=574582547254&ns=1&abbucket=3','','null','null',5,630,3150,31.5,'','T345363896265','',100,500,5,'','V00001','Pending peyment','Request Quote','2018-08-09 13:35:52',0,'C00101-00071',2,NULL,NULL,NULL,0),
(38,18,'apple','http://aaa','','null','null',123,0,0,0,'','','',15.87,1952.01,0,'','','Padding Audit','Request Quote','2018-08-10 14:16:32',0,NULL,0,NULL,NULL,NULL,0),
(39,19,'aaa','http://','','null','null',10,0,0,0,'','','',15.87,158.7,0,'','','Padding Audit','Request Quote','2018-08-10 14:21:16',0,NULL,0,NULL,NULL,NULL,0),
(40,20,'11111','http://hahhaha','','null','null',1111,0,0,0,'','','',158.73,31741.27,0,'','','Padding Audit','Request Quote','2018-08-10 15:06:02',0,NULL,0,NULL,NULL,NULL,0);

-- 
-- Definition of vb_itemproperty
-- 

CREATE TABLE IF NOT EXISTS `vb_itemproperty` (
  `ProId` int(11) NOT NULL AUTO_INCREMENT,
  `Vit_Id` int(11) DEFAULT NULL,
  `ProTitle` varchar(255) DEFAULT NULL,
  `ProName` varchar(255) DEFAULT NULL,
  `ProKey` varchar(255) DEFAULT NULL,
  `ProImage` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`ProId`)
) ENGINE=InnoDB AUTO_INCREMENT=66 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_itemproperty
-- 

INSERT INTO `vb_itemproperty`(`ProId`,`Vit_Id`,`ProTitle`,`ProName`,`ProKey`,`ProImage`) VALUES
(18,9,'Cosmetic net content','21 tablets','122216906:3221637',NULL),
(19,17,'Cosmetic net content','21 tablets','122216906:3221637',NULL),
(20,10,'Cosmetic net content','21 tablets','122216906:3221637',NULL),
(21,9,'Cosmetic net content','21 tablets','122216906:3221637',NULL),
(22,8,'Cosmetic net content','21 tablets','122216906:3221637',NULL),
(23,9,'Cosmetic net content','21 tablets','122216906:3221637',NULL),
(24,17,'Memory Capacity','16GB','20105:103646',NULL),
(25,17,'Hard drive capacity','500GB','20122:118107',NULL),
(26,17,'Package Type','Package three','5919063:3266785',NULL),
(27,13,'size','M','20509:28315',NULL),
(28,13,'colour','blue','1627207:28338','//gd3.alicdn.com/imgextra/i1/725699016/TB2ONKAlf9TBuNjy0FcXXbeiFXa_!!725699016.jpg'),
(29,13,'size','M','20509:28315',NULL),
(30,13,'colour','blue','1627207:28338','//gd3.alicdn.com/imgextra/i1/725699016/TB2ONKAlf9TBuNjy0FcXXbeiFXa_!!725699016.jpg'),
(31,17,'Memory Capacity','8GB','20105:41420',NULL),
(32,17,'Hard drive capacity','Configuration four','20122:395038694',NULL),
(33,15,'Color Classification','I5-8400/8g /1T+128G solid /GTX1060 3G','1627207:2231028404',NULL),
(34,16,'Color Classification','black','1627207:28341','//img.alicdn.com/imgextra/i3/2769711329/TB2vRCrhg0kpuFjSspdXXX4YXXa_!!2769711329.jpg'),
(35,16,'Package Type','Official standard','5919063:6536025',NULL),
(36,15,'Color Classification','white','1627207:28320',NULL),
(37,15,'Package Type','Official standard','5919063:6536025',NULL),
(38,18,'Memory Capacity','8GB','20105:41420',NULL),
(39,18,'Hard drive capacity','4TB','20122:118098',NULL),
(40,20,'Memory Capacity','8GB','20105:41420',NULL),
(41,20,'Hard drive capacity','Configuration One','20122:48202439',NULL),
(42,24,'Memory Capacity','8GB','20105:41420',NULL),
(43,24,'Hard drive capacity','Configuration four','20122:395038694',NULL),
(44,25,'Memory Capacity','8GB','20105:41420',NULL),
(45,25,'Hard drive capacity','Configuration five','20122:502944801',NULL),
(46,26,'Memory Capacity','16GB','20105:103646',NULL),
(47,26,'Hard drive capacity','Configuration II','20122:47102287',NULL),
(48,27,'Memory Capacity','16GB','20105:103646',NULL),
(49,27,'Hard drive capacity','Configuration four','20122:395038694',NULL),
(50,28,'Memory Capacity','16GB','20105:103646',NULL),
(51,28,'Hard drive capacity','Configuration three','20122:47101479',NULL),
(52,21,'Color Classification','Running Bear 13 + 2 + 2 corners can be matched with 1.5 * 2 crawling mat','1627207:28320','//img.alicdn.com/imgextra/TB1gQcOreuSBuNjy1XcL6UYjFXa'),
(53,30,'Color Classification','Run Bear 6+2 Plus Purchase Gift','1627207:3232483','//img.alicdn.com/imgextra/i4/928800801/TB2Ubf3Xoz.BuNjt_bXXXcQmpXa_!!928800801.jpg'),
(54,30,'Color Classification','Run the bear 10+2+4+ detergent','1627207:3232482','//img.alicdn.com/imgextra/i4/928800801/TB2PDZHFQyWBuNjy0FpXXassXXa_!!928800801.jpg'),
(55,25,'Color Classification','Run Bear 6+2 Plus Purchase Gift','1627207:3232483','//img.alicdn.com/imgextra/i4/928800801/TB2Ubf3Xoz.BuNjt_bXXXcQmpXa_!!928800801.jpg'),
(56,22,'Color Classification','150kg black and 1.2 special thick','1627207:30156','//img.alicdn.com/imgextra/i4/2106234702/TB2F7.rnJrJ8KJjSspaXXXuKpXa_!!2106234702.jpg'),
(57,29,'Color Classification','100kg red word waterproof scale 602B(rearward)','1627207:3232480','//img.alicdn.com/imgextra/i1/2106234702/TB23Ipnay6guuRkSmLyXXculFXa_!!2106234702.jpg'),
(58,24,'Color Classification','200kg black 35*45 thickening','1627207:3232479','//img.alicdn.com/imgextra/i3/2106234702/TB2h.RBcgaTBuNjSszfXXXgfpXa_!!2106234702.jpg'),
(59,28,'Color Classification','L4.209.4.11.2','1627207:3232482','//img.alicdn.com/imgextra/i1/2467959793/TB2o1ocIgmTBuNjy1XbXXaMrVXa_!!2467959793.png'),
(60,23,'Color Classification','L4.209.4.11.2','1627207:3232482','//img.alicdn.com/imgextra/i1/2467959793/TB2o1ocIgmTBuNjy1XbXXaMrVXa_!!2467959793.png'),
(61,38,'Color Classification','BU2020-02A','1627207:3232484','//img.alicdn.com/imgextra/i4/3925705463/TB274lAkY3nBKNjSZFMXXaUSFXa_!!3925705463.jpg'),
(62,39,'color','black','0:1','https://cbu01.alicdn.com/img/ibank/2018/609/609/9160906906_794403737.jpg'),
(63,39,'size','XL','1:0',''),
(64,40,'Color Classification','black','1627207:28341',NULL),
(65,40,'Package Type','Ares ZX7-CP5SC','5919063:2399700447',NULL);

-- 
-- Definition of vb_itemtype
-- 

CREATE TABLE IF NOT EXISTS `vb_itemtype` (
  `TID` int(11) NOT NULL AUTO_INCREMENT,
  `TName` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`TID`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_itemtype
-- 

INSERT INTO `vb_itemtype`(`TID`,`TName`) VALUES
(1,'Out of Stock'),
(2,'Expiration Date'),
(3,'Invalid Information'),
(4,'Order Closed (customer canceled)'),
(5,'Seller closed deal'),
(6,'Deal completed'),
(7,'Refund Product');

-- 
-- Definition of vb_language_detail
-- 

CREATE TABLE IF NOT EXISTS `vb_language_detail` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `LID` int(11) DEFAULT NULL,
  `Type` varchar(255) DEFAULT NULL,
  `Name` varchar(255) DEFAULT NULL,
  `Description` varchar(255) DEFAULT NULL,
  `Image` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_language_detail
-- 



-- 
-- Definition of vb_language_type
-- 

CREATE TABLE IF NOT EXISTS `vb_language_type` (
  `Type` varchar(255) NOT NULL,
  PRIMARY KEY (`Type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_language_type
-- 

INSERT INTO `vb_language_type`(`Type`) VALUES
('ch'),
('en'),
('kh');

-- 
-- Definition of vb_languages
-- 

CREATE TABLE IF NOT EXISTS `vb_languages` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(255) DEFAULT NULL,
  `Description` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_languages
-- 



-- 
-- Definition of vb_location
-- 

CREATE TABLE IF NOT EXISTS `vb_location` (
  `LocationID` varchar(255) NOT NULL,
  `LocationName` varchar(255) DEFAULT NULL,
  `LocationImage` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`LocationID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_location
-- 

INSERT INTO `vb_location`(`LocationID`,`LocationName`,`LocationImage`) VALUES
('LO-001','ផ្ទះដោនថោន 7, ក្រោយវត្ដអង្គតាមិញ','location1.jpg'),
('LO-002','ផ្ទះលេខ 226 ផ្លូវលេខ 132 (សំរាប់ VBUY កម្ពុជាក្រោម )','location2.jpg');

-- 
-- Definition of vb_logfile
-- 

CREATE TABLE IF NOT EXISTS `vb_logfile` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `url` varchar(255) DEFAULT NULL,
  `action` varchar(255) DEFAULT NULL,
  `status` varchar(255) DEFAULT NULL,
  `tranid` varchar(255) DEFAULT NULL,
  `date` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_logfile
-- 



-- 
-- Definition of vb_measure
-- 

CREATE TABLE IF NOT EXISTS `vb_measure` (
  `var_id` varchar(10) NOT NULL,
  `var_name` varchar(100) DEFAULT NULL,
  `dou_price` double DEFAULT NULL,
  `dou_amt` double DEFAULT NULL,
  `var_cur` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`var_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_measure
-- 

INSERT INTO `vb_measure`(`var_id`,`var_name`,`dou_price`,`dou_amt`,`var_cur`) VALUES
('CBM','Cubic Meter',180,1,'USD'),
('KG','Kilogram',3,1,'USD');

-- 
-- Definition of vb_message
-- 

CREATE TABLE IF NOT EXISTS `vb_message` (
  `mid` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) DEFAULT NULL,
  `msg1` varchar(255) DEFAULT NULL,
  `msg2` varchar(255) DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`mid`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_message
-- 

INSERT INTO `vb_message`(`mid`,`title`,`msg1`,`msg2`,`description`) VALUES
(1,'success','The ','was create successful.','Create'),
(2,'failed','The','was failed.','Failed'),
(3,'used','The','was used.','Used'),
(4,'existed','The','was existed.','Exist'),
(5,'success','The','was list successful.','Get List'),
(6,'success','The','was update scuuessful.','Update'),
(7,'Invalid','The','was invalid.','Invalid'),
(8,'success','The','was cancel order.','Cancel'),
(9,'no permissions','The received payment with ','  not yet posted. please log to AME and posted the receipt id.',''),
(10,'failed','The OF Code :','was already pay.','');

-- 
-- Definition of vb_notification
-- 

CREATE TABLE IF NOT EXISTS `vb_notification` (
  `CNID` varchar(255) NOT NULL,
  `CNName` varchar(255) DEFAULT NULL,
  `CNDescription` varchar(255) DEFAULT NULL,
  `CNImage` varchar(255) DEFAULT NULL,
  `CNCby` varchar(255) DEFAULT NULL,
  `CNMby` varchar(255) DEFAULT NULL,
  `CNDate` datetime DEFAULT NULL,
  PRIMARY KEY (`CNID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_notification
-- 

INSERT INTO `vb_notification`(`CNID`,`CNName`,`CNDescription`,`CNImage`,`CNCby`,`CNMby`,`CNDate`) VALUES
('NT-00001','Order Information',NULL,NULL,NULL,NULL,NULL),
('NT-00002','Recharge Information',NULL,NULL,NULL,NULL,NULL),
('NT-00003','Pick up Information',NULL,NULL,NULL,NULL,NULL),
('NT-00004','Pay for Shipment',NULL,NULL,NULL,NULL,NULL);

-- 
-- Definition of vb_notification_detail
-- 

CREATE TABLE IF NOT EXISTS `vb_notification_detail` (
  `CNDID` int(11) NOT NULL AUTO_INCREMENT,
  `CNID` varchar(255) DEFAULT NULL,
  `CNCustID` varchar(255) DEFAULT NULL,
  `CNDescription` varchar(500) DEFAULT NULL,
  `CNDate` datetime DEFAULT NULL,
  `CNDCby` varchar(255) DEFAULT NULL,
  `CNDMby` varchar(255) DEFAULT NULL,
  `CNSeen` int(1) DEFAULT NULL,
  PRIMARY KEY (`CNDID`)
) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_notification_detail
-- 

INSERT INTO `vb_notification_detail`(`CNDID`,`CNID`,`CNCustID`,`CNDescription`,`CNDate`,`CNDCby`,`CNDMby`,`CNSeen`) VALUES
(1,'NT-00001','C00042','Your order  506AUG18-1C00042 already approved, total amount 132.19 please pay immediately.','2018-08-06 10:48:37',NULL,NULL,1),
(2,'NT-00002','C00042','Respect customer: vichet, your recharge has been credited, the amount credited: 1000','2018-08-06 10:55:42',NULL,NULL,0),
(3,'NT-00001','C00042','Your order  506AUG18-1C00042 already approved, total amount 132.19 please pay immediately.','2018-08-06 10:58:27',NULL,NULL,1),
(4,'NT-00001','C00042','Your order  506AUG18-1C00042 already approved, total amount 132.19 please pay immediately.','2018-08-06 11:04:08',NULL,NULL,1),
(5,'NT-00003','C00042','Dear  Vichet Online Shop your order  506AUG18-1C00042  with pick up code: 127493 . Please copy your order id and pick up code when you come.','2018-08-06 11:04:19',NULL,NULL,1),
(6,'NT-00004','C00042','Dear Vichet Online Shop you need to pay: 6 USD for shipment of OF Code: 506AUG18-1C00042. Please pay as soon as possible!','2018-08-06 11:04:19',NULL,NULL,0),
(7,'NT-00002','C00042','You was pay for shipment OF Code :506AUG18-1C00042. amount :6.00','2018-08-06 11:05:38',NULL,NULL,0),
(8,'NT-00003','C00042','Dear  Vichet Online Shop your order  506AUG18-1C00042  with pick up code: 871579 . Please copy your order id and pick up code when you come.','2018-08-06 11:09:16',NULL,NULL,1),
(9,'NT-00004','C00042','Dear Vichet Online Shop you need to pay: 30 USD for shipment of OF Code: 506AUG18-1C00042. Please pay as soon as possible!','2018-08-06 11:09:16',NULL,NULL,0),
(10,'NT-00002','C00042','You was pay for shipment OF Code :506AUG18-1C00042. amount :30.00','2018-08-06 11:10:04',NULL,NULL,0),
(11,'NT-00001','C00042','Your order  06AUG18-2C00042 already approved, total amount 114.29 please pay immediately.','2018-08-06 11:26:35',NULL,NULL,0),
(12,'NT-00001','C00042','Your order  06AUG18-2C00042 already approved, total amount 114.29 please pay immediately.','2018-08-06 11:26:36',NULL,NULL,0),
(13,'NT-00001','C00101','Your order  409AUG18-1C00101 already approved, total amount 3.42 please pay immediately.','2018-08-09 09:24:21',NULL,NULL,1),
(14,'NT-00001','C00101','Your order  409AUG18-2C00101 already approved, total amount 6.84 please pay immediately.','2018-08-09 09:28:10',NULL,NULL,1),
(15,'NT-00002','C00101','Respect customer: vbuy, your recharge has been credited, the amount credited: 10','2018-08-09 09:30:06',NULL,NULL,1),
(16,'NT-00001','C00101','Your order  409AUG18-2C00101 already approved, total amount 6.84 please pay immediately.','2018-08-09 09:34:17',NULL,NULL,1),
(17,'NT-00001','C00101','Your order  409AUG18-2C00101 already approved, total amount 6.84 please pay immediately.','2018-08-09 09:44:30',NULL,NULL,1),
(18,'NT-00001','C00101','Your order  409AUG18-2C00101 already approved, total amount 6.84 please pay immediately.','2018-08-09 09:45:24',NULL,NULL,1),
(19,'NT-00003','C00101','Dear  Vbuy Kh your order  409AUG18-2C00101  with pick up code: 962348 . Please copy your order id and pick up code when you come.','2018-08-09 09:45:44',NULL,NULL,1),
(20,'NT-00001','C00041','Your order  509AUG18-2C00041 already approved, total amount 21.44 please pay immediately.','2018-08-09 09:51:04',NULL,NULL,0),
(21,'NT-00001','C00101','Your order  409AUG18-5C00101 already approved, total amount 4322.93 please pay immediately.','2018-08-09 11:09:42',NULL,NULL,1),
(22,'NT-00001','C00101','Your order  409AUG18-4C00101 already approved, total amount 5416.61 please pay immediately.','2018-08-09 11:14:25',NULL,NULL,1),
(23,'NT-00002','C00101','Respect customer: vbuy, your recharge has been credited, the amount credited: 10000','2018-08-09 11:15:13',NULL,NULL,1),
(24,'NT-00002','C00101','Respect customer: vbuy, your recharge has been credited, the amount credited: 9736.38','2018-08-09 11:25:51',NULL,NULL,1),
(25,'NT-00001','C00101','Your order  409AUG18-5C00101 already approved, total amount 3410.24 please pay immediately.','2018-08-09 11:28:26',NULL,NULL,1),
(26,'NT-00001','C00101','Your order  409AUG18-5C00101 already approved, total amount 945.96 please pay immediately.','2018-08-09 11:33:08',NULL,NULL,1),
(27,'NT-00001','C00101','Your order  409AUG18-1C00101 already approved, total amount 1826.99 please pay immediately.','2018-08-09 11:34:42',NULL,NULL,1),
(28,'NT-00002','C00101','Respect customer: vbuy, your recharge has been credited, the amount credited: 2000','2018-08-09 11:35:32',NULL,NULL,1),
(29,'NT-00001','C00101','Your order  409AUG18-6C00101 already approved, total amount 99.99 please pay immediately.','2018-08-09 11:44:15',NULL,NULL,1),
(30,'NT-00002','C00101','Respect customer: vbuy, your recharge has been credited, the amount credited: 10000','2018-08-09 11:45:38',NULL,NULL,1),
(31,'NT-00001','C00101','Your order  409AUG18-7C00101 already approved, total amount 100.41 please pay immediately.','2018-08-09 11:50:53',NULL,NULL,1),
(32,'NT-00001','C00100','Your order  509AUG18-5C00100 already approved, total amount 2108.7 please pay immediately.','2018-08-09 12:03:07',NULL,NULL,1),
(33,'NT-00002','C00041','Respect customer: chenda, your recharge has been credited, the amount credited: 3000','2018-08-09 12:03:37',NULL,NULL,0),
(34,'NT-00002','C00100','Respect customer: dada, your recharge has been credited, the amount credited: 3000','2018-08-09 12:04:03',NULL,NULL,0),
(35,'NT-00002','C00101','Respect customer: vbuy, your recharge has been credited, the amount credited: 331','2018-08-09 12:14:01',NULL,NULL,1),
(36,'NT-00002','C00101','Respect customer: vbuy, your recharge has been credited, the amount credited: 0.68','2018-08-09 12:15:21',NULL,NULL,1),
(37,'NT-00002','C00101','You was pay for shipment OF Code :409AUG18-2C00101. amount :450.00','2018-08-09 12:16:09',NULL,NULL,1),
(38,'NT-00001','C00101','Your order  409AUG18-4C00101 already approved, total amount 5416.61 please pay immediately.','2018-08-09 13:26:35',NULL,NULL,1),
(39,'NT-00001','C00101','Your order  409AUG18-8C00101 already approved, total amount 500.0 please pay immediately.','2018-08-09 13:42:33',NULL,NULL,1),
(40,'NT-00002','C00101','Respect customer: vbuy, your recharge has been credited, the amount credited: 1000','2018-08-09 13:45:03',NULL,NULL,1),
(41,'NT-00001','C00101','Your order  409AUG18-8C00101 already approved, total amount 2900.0 please pay immediately.','2018-08-09 13:49:56',NULL,NULL,1),
(42,'NT-00002','C00101','Respect customer: vbuy, your recharge has been credited, the amount credited: 1500','2018-08-09 13:51:16',NULL,NULL,1),
(43,'NT-00002','C00101','Respect customer: vbuy, your recharge has been credited, the amount credited: 200','2018-08-09 13:51:46',NULL,NULL,1),
(44,'NT-00002','C00101','Respect customer: vbuy, your recharge has been credited, the amount credited: 1000','2018-08-09 13:52:36',NULL,NULL,1),
(45,'NT-00001','C00101','Your order  409AUG18-8C00101 already approved, total amount 2900.0 please pay immediately.','2018-08-09 14:43:04',NULL,NULL,0),
(46,'NT-00002','C00101','Your order OF: 409AUG18-8C00101 with link: https://detail.tmall.com/item.htm?spm=a230r.1.14.67.53bdb518ZVoTD7&id=574582547254&ns=1&abbucket=3 was refund to you. amount :580','2018-08-09 14:44:40',NULL,NULL,0),
(47,'NT-00001','C00101','Your order  409AUG18-8C00101 already approved, total amount 1160.0 please pay immediately.','2018-08-09 14:47:38',NULL,NULL,0),
(48,'NT-00001','C00101','Your order  409AUG18-8C00101 already approved, total amount 1160.0 please pay immediately.','2018-08-09 14:48:38',NULL,NULL,0),
(49,'NT-00003','C00101','Dear  Vbuy Kh your order  409AUG18-8C00101  with pick up code: 244677 . Please copy your order id and pick up code when you come.','2018-08-09 14:48:50',NULL,NULL,0),
(50,'NT-00004','C00101','Dear Vbuy Kh you need to pay: 594 USD for shipment of OF Code: 409AUG18-8C00101. Please pay as soon as possible!','2018-08-09 14:48:50',NULL,NULL,0),
(51,'NT-00001','C00100','Your order  509AUG18-4C00100 already approved, total amount 26.97 please pay immediately.','2018-08-09 15:07:59',NULL,NULL,1),
(52,'NT-00001','C00100','Your order  509AUG18-4C00100 already approved, total amount 26.97 please pay immediately.','2018-08-09 15:29:34',NULL,NULL,1),
(53,'NT-00001','C00101','Your order  409AUG18-3C00101 already approved, total amount 2.69 please pay immediately.','2018-08-09 16:37:33',NULL,NULL,0),
(54,'NT-00001','C00041','Your order  509AUG18-2C00041 already approved, total amount 87.07 please pay immediately.','2018-08-09 16:38:07',NULL,NULL,0),
(55,'NT-00001','C00100','Your order 509AUG18-4C00100,was expired','2018-08-10 10:22:57',NULL,NULL,1),
(56,'NT-00001','C00100','Your order 509AUG18-4C00100,was expired','2018-08-10 10:22:57',NULL,NULL,1);

-- 
-- Definition of vb_notification_message
-- 

CREATE TABLE IF NOT EXISTS `vb_notification_message` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` varchar(255) DEFAULT NULL,
  `msg1` varchar(255) DEFAULT NULL,
  `msg2` varchar(255) DEFAULT NULL,
  `msg3` varchar(255) DEFAULT NULL,
  `msg4` varchar(255) DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_notification_message
-- 

INSERT INTO `vb_notification_message`(`id`,`type`,`msg1`,`msg2`,`msg3`,`msg4`,`description`) VALUES
(1,'Order Approved','Your order ','already approved, total amount','please pay immediately.',NULL,'Customer Pay'),
(2,'Order Out of Stock','Your order ','product','is no more available.(Out of stock)',NULL,'Out of stock'),
(3,'Recharge','Respect customer:',',the amount credited:',', your recharge has been credited',NULL,'Recharge'),
(4,'Pick Up','Dear ','your order ',' with pick up code:','. Please copy your order id and pick up code when you come.','Pick Up'),
(5,'Order Expiration','Your order ',',was expired','.',NULL,'Order Expiration'),
(6,'Shipment payment','Dear','you need to pay:','USD for shipment of OF Code: ','. Please pay as soon as possible!','Pay for shipment');

-- 
-- Definition of vb_numberprefix
-- 

CREATE TABLE IF NOT EXISTS `vb_numberprefix` (
  `ModuleID` varchar(200) NOT NULL,
  `NumberPrefix` varchar(255) DEFAULT NULL,
  `Remarks` varchar(255) DEFAULT NULL,
  `Length` int(11) DEFAULT NULL,
  `LastID` int(11) DEFAULT NULL,
  `Status` int(1) NOT NULL DEFAULT '1',
  `Group` varchar(255) DEFAULT NULL,
  `Hide` tinyint(1) DEFAULT '0',
  `sort` int(11) DEFAULT NULL,
  PRIMARY KEY (`ModuleID`),
  KEY `ModuleID` (`ModuleID`) USING BTREE,
  KEY `NumberPrefix` (`NumberPrefix`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_numberprefix
-- 

INSERT INTO `vb_numberprefix`(`ModuleID`,`NumberPrefix`,`Remarks`,`Length`,`LastID`,`Status`,`Group`,`Hide`,`sort`) VALUES
('CUST','CUST','Customer',NULL,NULL,1,NULL,0,2),
('CustomerBalance','CB','Customer Balance',10,3,1,'Customer Balance',0,3),
('EMP','E','Employee',7,1,1,NULL,0,0),
('ER','ER','Exchange Rate',NULL,NULL,1,NULL,0,1),
('GI','GI','Sale Invoice',NULL,NULL,1,NULL,0,6),
('IG','IG','Inventory Group',7,1,1,NULL,1,0),
('IO','IO','Item Order',NULL,NULL,1,NULL,0,5),
('Location','LO','Location Vbuy',5,3,1,'Location Vbuy',1,0),
('Notification','NT','Notification',7,3,1,'Notification',1,0),
('PC','PC','Purchase Credit',NULL,NULL,1,NULL,0,4),
('PurchaseNo','PCN','Transaction Purchase Credit ',10,233,1,'Transaction Purchase Credit',1,0),
('PV','PV','Pay Vendor',NULL,NULL,1,'Pay Vendor',0,11),
('Refund','RF','Item Refund',7,66,1,'Item Refund',0,8),
('SET','SET','Settings',NULL,NULL,1,NULL,0,10),
('ShipLocation','SL','Ship Location',10,374,1,'Ship Location',1,0),
('SLS','SLS','Slide Show',NULL,NULL,1,NULL,0,9),
('SO-AR','SO-AR','Sale Order',NULL,NULL,1,NULL,0,7),
('Transaction','TS','Transaction Number',7,1,1,'Transaction Number',1,0);

-- 
-- Definition of vb_purchasecredit
-- 

CREATE TABLE IF NOT EXISTS `vb_purchasecredit` (
  `vpid` int(11) NOT NULL AUTO_INCREMENT,
  `custId` varchar(255) DEFAULT NULL,
  `vpAccountId` int(11) DEFAULT NULL,
  `vpTransactionNo` varchar(255) DEFAULT NULL,
  `vpAmount` double DEFAULT NULL,
  `vpDate` datetime DEFAULT NULL,
  `vpDocument` varchar(255) DEFAULT NULL,
  `vpRemark` varchar(255) DEFAULT NULL,
  `vpStatus` varchar(255) DEFAULT NULL,
  `vpType` varchar(255) DEFAULT NULL,
  `vpCDate` datetime DEFAULT NULL,
  PRIMARY KEY (`vpid`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_purchasecredit
-- 

INSERT INTO `vb_purchasecredit`(`vpid`,`custId`,`vpAccountId`,`vpTransactionNo`,`vpAmount`,`vpDate`,`vpDocument`,`vpRemark`,`vpStatus`,`vpType`,`vpCDate`) VALUES
(1,'C00041',1,'PCN-0000230',1000,'2018-08-09 00:08:00','d54df2a5-954a-463d-b713-8bbf8afa0225.PNG','sdasdas','Deposit','Recharge','2018-08-06 09:57:07'),
(2,'C00101',1,'PCN-0000231',10,'2018-08-09 00:08:00','8c09a1cb-5a68-4b08-b0bd-c0c5ef9be4c1.PNG','123','Has Been Accounted For','Recharge','2018-08-09 09:10:35'),
(3,'C00100',1,'PCN-0000232',123,'2018-08-13 10:07:57','19833748-fc75-4284-ab1a-fa5dafb20d35.jpg','','Deposit','Recharge','2018-08-13 10:08:50');

-- 
-- Definition of vb_purchaseseen
-- 

CREATE TABLE IF NOT EXISTS `vb_purchaseseen` (
  `vpsid` int(11) NOT NULL AUTO_INCREMENT,
  `userId` int(11) NOT NULL,
  `date` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `remark` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`vpsid`,`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_purchaseseen
-- 



-- 
-- Definition of vb_refunditem
-- 

CREATE TABLE IF NOT EXISTS `vb_refunditem` (
  `vir_id` varchar(255) NOT NULL,
  `vir_orderId` int(11) DEFAULT NULL,
  `vir_ofcode` varchar(255) DEFAULT NULL,
  `vir_custId` varchar(255) DEFAULT NULL,
  `vir_itemId` varchar(255) NOT NULL,
  `vir_salId` varchar(255) DEFAULT NULL,
  `vir_recId` varchar(255) DEFAULT NULL,
  `vir_lc` varchar(255) DEFAULT NULL,
  `vir_service` varchar(255) DEFAULT NULL,
  `vir_amount` varchar(255) DEFAULT NULL,
  `vir_total` varchar(255) DEFAULT NULL,
  `vir_reason` varchar(255) DEFAULT NULL,
  `vir_date` datetime DEFAULT NULL,
  `vir_refauth` varchar(255) DEFAULT NULL,
  `vir_empId` varchar(255) DEFAULT NULL,
  `vir_image` varchar(255) DEFAULT NULL,
  `vir_link` text,
  `vir_status` varchar(255) DEFAULT NULL,
  `vir_approved_by` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`vir_itemId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_refunditem
-- 

INSERT INTO `vb_refunditem`(`vir_id`,`vir_orderId`,`vir_ofcode`,`vir_custId`,`vir_itemId`,`vir_salId`,`vir_recId`,`vir_lc`,`vir_service`,`vir_amount`,`vir_total`,`vir_reason`,`vir_date`,`vir_refauth`,`vir_empId`,`vir_image`,`vir_link`,`vir_status`,`vir_approved_by`) VALUES
('RF-00060',5,'409AUG18-2C00101','C00101','C00101-00001','SO00004','OR00008','1.59','0.24','1.59','3.42','','2018-08-09 09:33:48','','E-00004','http://img02.taobaocdn.com/bao/uploaded/i4/725677994/TB2ncUVJQSWBuNjSszdXXbeSpXa_!!725677994-0-item_pic.jpg','https://world.tmall.com/item/550988772571.htm?id=550988772571&spm=875.7931836/B.2017039.4.66144265rGQv2o&scm=1007.12144.81309.73136_0&pvid=2e1727e8-6aa1-43c0-bcb7-6ccce43aff48&utparam={%22x_hestia_source%22:%2273136%22,%22x_object_type%22:%22item%22,%22x_mt%22:8,%22x_src%22:%2273136%22,%22x_pos%22:1,%22x_pvid%22:%222e1727e8-6aa1-43c0-bcb7-6ccce43aff48%22,%22x_object_id%22:550988772571}','Rejected',''),
('RF-00061',5,'409AUG18-2C00101','C00101','C00101-00002','SO00004','OR00008','1.59','0.24','1.59','3.42','','2018-08-09 09:33:56','','E-00004','http://img02.taobaocdn.com/bao/uploaded/i4/725677994/TB2ncUVJQSWBuNjSszdXXbeSpXa_!!725677994-0-item_pic.jpg','https://world.tmall.com/item/550988772571.htm?id=550988772571&spm=875.7931836/B.2017039.4.66144265rGQv2o&scm=1007.12144.81309.73136_0&pvid=2e1727e8-6aa1-43c0-bcb7-6ccce43aff48&utparam={%22x_hestia_source%22:%2273136%22,%22x_object_type%22:%22item%22,%22x_mt%22:8,%22x_src%22:%2273136%22,%22x_pos%22:1,%22x_pvid%22:%222e1727e8-6aa1-43c0-bcb7-6ccce43aff48%22,%22x_object_id%22:550988772571}','Rejected',''),
('RF-00062',11,'409AUG18-4C00101','C00101','C00101-00060','SO00017','OR00026','3','6','40','49','1','2018-08-09 13:26:19','','E-00004','http://img02.taobaocdn.com/bao/uploaded/i3/725677994/TB1ukBhov2H8KJjy0FcXXaDlFXa_!!0-item_pic.jpg','https://world.tmall.com/item/557830397214.htm?spm=a223d.7797701.8018832208.10.68a46d86TNZKQW&id=557830397214','Pending',''),
('RF-00062',11,'409AUG18-4C00101','C00101','C00101-00061','SO00017','OR00026','5','476.19','3174.6','3655.79','2','2018-08-09 13:26:19','','E-00004','http://img02.taobaocdn.com/bao/uploaded/i4/1049122653/O1CN011VT7D7Pq2MuHGQG_!!1049122653.jpg','https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.33.1f9d4c33sbhI3a&id=19628167605&skuId=3821270746308&user_id=1049122653&cat_id=2&is_b=1&rn=17e35d709126476e52fc5d8d9d80dd97','Pending',''),
('RF-00062',11,'409AUG18-4C00101','C00101','C00101-00062','SO00017','OR00026','5','190.48','1269.84','1465.32','3','2018-08-09 13:26:19','','E-00004','//img.alicdn.com/imgextra/TB1gQcOreuSBuNjy1XcL6UYjFXa','https://detail.tmall.com/item.htm?spm=a221w.7782398.200001.10.37212631WLlUDj&pos=3&acm=201601215.1003.1.700922&id=38739726083&scm=1003.1.201601215.D2I_38739726083_700922','Rejected',''),
('RF-00065',17,'409AUG18-8C00101','C00101','C00101-00067','SO00019','OR00033','5','75','500','580','1','2018-08-09 14:46:31','','E-00004','','https://detail.tmall.com/item.htm?spm=a230r.1.14.67.53bdb518ZVoTD7&id=574582547254&ns=1&abbucket=3','Rejected',''),
('RF-00064',17,'409AUG18-8C00101','C00101','C00101-00068','SO00019','OR00033','5','75','500','580','2','2018-08-09 14:44:05','','E-00004','','https://detail.tmall.com/item.htm?spm=a230r.1.14.67.53bdb518ZVoTD7&id=574582547254&ns=1&abbucket=3','Approved','E00003'),
('RF-00063',17,'409AUG18-8C00101','C00101','C00101-00069','SO00019','OR00033','5','75','500','580','5','2018-08-09 13:56:12','','E-00004','','https://detail.tmall.com/item.htm?spm=a230r.1.14.67.53bdb518ZVoTD7&id=574582547254&ns=1&abbucket=3','Approved','E00003'),
('RF-00063',17,'409AUG18-8C00101','C00101','C00101-00070','SO00019','OR00033','5','75','500','580','4','2018-08-09 13:56:12','','E-00004','','https://detail.tmall.com/item.htm?spm=a230r.1.14.67.53bdb518ZVoTD7&id=574582547254&ns=1&abbucket=3','Rejected',''),
('RF-00063',17,'409AUG18-8C00101','C00101','C00101-00071','SO00019','OR00033','5','75','500','580','3','2018-08-09 13:56:12','','E-00004','','https://detail.tmall.com/item.htm?spm=a230r.1.14.67.53bdb518ZVoTD7&id=574582547254&ns=1&abbucket=3','Approved','');

-- 
-- Definition of vb_status
-- 

CREATE TABLE IF NOT EXISTS `vb_status` (
  `SID` int(11) NOT NULL AUTO_INCREMENT,
  `SName` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`SID`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_status
-- 

INSERT INTO `vb_status`(`SID`,`SName`) VALUES
(1,'All orders'),
(2,'Pending approval'),
(3,'Pending payment'),
(5,'Be ordered'),
(6,'Be Purchase'),
(7,'Goods are available in the Chinese warehouse'),
(8,'Products are shipping'),
(9,'Some goods are available in the Cambodia warehouse'),
(10,'Goods are available in the Cambodia warehouse'),
(11,'The customer have been receiving'),
(12,'Successful trade'),
(13,'Obsolete order');

-- 
-- Definition of vb_transaction
-- 

CREATE TABLE IF NOT EXISTS `vb_transaction` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date` datetime DEFAULT NULL,
  `tranid` varchar(255) DEFAULT NULL,
  `code` varchar(255) DEFAULT NULL,
  `income` double DEFAULT NULL,
  `expense` double DEFAULT NULL,
  `balance` double DEFAULT NULL,
  `status` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_transaction
-- 



-- 
-- Definition of vb_user
-- 

CREATE TABLE IF NOT EXISTS `vb_user` (
  `vb_userId` int(11) NOT NULL AUTO_INCREMENT,
  `CustID` varchar(255) DEFAULT NULL,
  `vb_userName` varchar(255) NOT NULL,
  `vb_passwd` varchar(255) DEFAULT NULL,
  `vb_paymentPasswd` varchar(255) DEFAULT NULL,
  `vb_role` varchar(10) DEFAULT NULL,
  `vb_createDate` datetime DEFAULT NULL,
  `vb_isMobileVerified` tinyint(1) DEFAULT NULL,
  `vb_isEmailVerified` tinyint(1) DEFAULT NULL,
  `vb_email` varchar(255) DEFAULT NULL,
  `vb_mobile` varchar(255) DEFAULT '',
  `vb_isEnable` tinyint(1) DEFAULT '1',
  `vb_isShow` tinyint(1) DEFAULT '1',
  `vb_code` int(4) DEFAULT '0',
  `vb_admin_role` int(11) DEFAULT NULL,
  `vb_empId` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`vb_userId`,`vb_userName`),
  UNIQUE KEY `vb_username` (`vb_userName`) USING BTREE,
  UNIQUE KEY `vb_email` (`vb_email`) USING BTREE,
  UNIQUE KEY `vb_tel` (`vb_mobile`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=77 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_user
-- 

INSERT INTO `vb_user`(`vb_userId`,`CustID`,`vb_userName`,`vb_passwd`,`vb_paymentPasswd`,`vb_role`,`vb_createDate`,`vb_isMobileVerified`,`vb_isEmailVerified`,`vb_email`,`vb_mobile`,`vb_isEnable`,`vb_isShow`,`vb_code`,`vb_admin_role`,`vb_empId`) VALUES
(2,NULL,'admin','c2gzcXFRVVJOU1U0eE1qTXhNak09bmk2Mmg=',NULL,'ADMIN','2018-04-20 10:13:38',0,0,NULL,NULL,1,0,0,16,'E00003'),
(3,NULL,'dev@balancika','bTgzaDhSRVZXUUVKQlRFRk9RMGxMUVdSbGRrQlFZU1FrZHpCeVpBPT0ycXVmaQ==',NULL,'API','2018-04-20 10:16:14',0,0,'dev@balancikacambodia.com','012997373',1,0,0,NULL,NULL),
(63,'C00041','chenda','djA2M2xRMGhGVGtSQk1USXpNVEl6NXJ2cDU=','ZjNrbmtRMGhGVGtSQk1USXpNVEl6bW41MTY=','ADMIN','2018-08-02 00:00:00',1,1,'312312312@ASDAS.OCM','069606747',1,1,8304,16,NULL),
(64,'C00042','vichet','MXUxcHNWa2xEU0VWVU1USXpNVEl6MDAzMm0=','NTAyc3JWa2xEU0VWVU1USXpNVEl6MmtqYWY=','USER','2018-08-02 00:00:00',0,0,'vichet297@gmail.com','015955247',1,1,0,NULL,NULL),
(65,NULL,'C0001','MHZjN2xRekF3TURFeE1qTXhNak09dDMxN2Y=',NULL,'ADMIN',NULL,NULL,NULL,NULL,NULL,1,1,0,16,'E00010'),
(66,NULL,'C002','aWFvNm1RekF3TWpFeU16RXlNdz09MjNnbTg=',NULL,'ADMIN',NULL,NULL,NULL,NULL,NULL,1,1,0,30,'E00002'),
(67,NULL,'C004','ZWlzanFRekF3TkRFeU16RXlNdz09ODVmbjE=',NULL,'ADMIN',NULL,NULL,NULL,NULL,NULL,1,1,0,31,'E00011'),
(68,'C00100','dada','aWdnMG5SRUZFUVRFeU16RXlNdz09Nm4zaDU=','bDJubWRSRUZFUVRFeU16RXlNdz09M2UyNjA=','USER','2018-08-06 00:00:00',0,0,'12312312@ada.coim','0112222222',1,1,3082,NULL,NULL),
(69,'C00101','vbuy','dTJiY2NWa0pWV1RFeU16RXlNdz09ZjZkc2g=','b2s4MWtWa0pWV1RFeU16RXlNdz09cmowbWk=','USER','2018-08-09 00:00:00',1,1,'chamroeunchun101@gmail.com','0969767772',1,1,3331,NULL,NULL),
(72,NULL,'C00096','bGJkb3RRekF3TURrMk1USXpNVEl6bzF0b3A=',NULL,'USER','2018-08-09 00:00:00',0,0,NULL,NULL,1,1,0,NULL,NULL),
(73,'C00038','C00038','MnEyZWpRekF3TURNNE1USXpNVEl6MTJvcmg=',NULL,'USER','2018-08-09 00:00:00',0,0,NULL,NULL,1,1,0,NULL,NULL),
(74,'C00013','C00013','NjBqdnRRekF3TURFek1USXpNVEl6dmx2MnE=',NULL,'USER','2018-08-09 00:00:00',0,0,NULL,'C00013',1,1,0,NULL,NULL),
(75,'C00001','C00001','MTI5amRRekF3TURBeE1USXpNVEl6aTJrdWY=',NULL,'USER','2018-08-09 00:00:00',0,0,NULL,'C00001',1,1,0,NULL,NULL),
(76,'C00102','C00102','OHAzbmJRekF3TVRBeU1USXpNVEl6anVoNm0=',NULL,'USER','2018-08-13 00:00:00',0,0,NULL,'1231231231',1,1,0,NULL,NULL);

-- 
-- Definition of vb_user_role
-- 

CREATE TABLE IF NOT EXISTS `vb_user_role` (
  `vb_roleId` int(11) NOT NULL AUTO_INCREMENT,
  `vb_roleName` varchar(255) DEFAULT NULL,
  `vb_roleType` int(11) DEFAULT NULL,
  PRIMARY KEY (`vb_roleId`)
) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_user_role
-- 

INSERT INTO `vb_user_role`(`vb_roleId`,`vb_roleName`,`vb_roleType`) VALUES
(16,'ADMIN',1),
(27,'Tester',1),
(30,'User',2),
(31,'Service',2),
(32,'Accountant',1);

-- 
-- Definition of vb_user_role_dt
-- 

CREATE TABLE IF NOT EXISTS `vb_user_role_dt` (
  `vb_dtId` int(11) NOT NULL AUTO_INCREMENT,
  `vb_roleId` int(11) NOT NULL,
  `vb_moduleId` varchar(20) NOT NULL,
  `vb_isAccessible` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`vb_dtId`,`vb_roleId`,`vb_moduleId`)
) ENGINE=InnoDB AUTO_INCREMENT=254 DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_user_role_dt
-- 

INSERT INTO `vb_user_role_dt`(`vb_dtId`,`vb_roleId`,`vb_moduleId`,`vb_isAccessible`) VALUES
(136,30,'CUST',1),
(137,30,'IO',1),
(155,16,'CUST',1),
(156,16,'CustomerBalance',1),
(157,16,'ER',1),
(158,16,'GI',1),
(159,16,'IO',1),
(160,16,'PC',1),
(161,16,'Refund',1),
(162,16,'SET',1),
(163,16,'SLS',1),
(164,16,'SO-AR',1),
(165,16,'EMP',1),
(166,16,'PV',1),
(221,27,'GI',1),
(222,27,'CUST',1),
(223,27,'CustomerBalance',1),
(224,27,'SO-AR',1),
(225,27,'Refund',1),
(226,27,'IO',1),
(227,27,'EMP',1),
(228,27,'ER',1),
(229,27,'PC',1),
(230,27,'PV',1),
(231,27,'SET',1),
(232,27,'SLS',1),
(236,31,'CUST',1),
(237,31,'IO',1),
(249,32,'IO',1),
(250,32,'GI',1),
(251,32,'Refund',1),
(252,32,'CustomerBalance',1),
(253,32,'PC',1);

-- 
-- Definition of vb_user_role_type
-- 

CREATE TABLE IF NOT EXISTS `vb_user_role_type` (
  `vb_typeId` int(11) NOT NULL,
  `vb_typeName` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`vb_typeId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table vb_user_role_type
-- 

INSERT INTO `vb_user_role_type`(`vb_typeId`,`vb_typeName`) VALUES
(1,'List all records'),
(2,'List only owned records');

-- 
-- Dumping functions
-- 

DROP FUNCTION IF EXISTS `ameAuthDescApproval`;
DELIMITER |
CREATE FUNCTION `ameAuthDescApproval`(ProName VARCHAR(255), TranID VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
DECLARE countCustApp  INT DEFAULT 0;
DECLARE countCustPad  INT DEFAULT 0;
DECLARE tranName  VARCHAR(255) DEFAULT "";
DECLARE des VARCHAR(255) DEFAULT "" ;
DECLARE gr VARCHAR(200) DEFAULT "";
IF ProName = "Customer" THEN
SELECT COUNT(crm_customer.CustID) INTO countCustApp FROM crm_customer WHERE crm_customer.CustID = TranID LIMIT 0,1;
IF countCustApp <> 0 THEN
SELECT crm_customer.CustName INTO des  FROM crm_customer WHERE crm_customer.CustID = TranID LIMIT 0,1;
ELSE
SELECT tblcustomer.CustName INTO des FROM tblcustomer WHERE tblcustomer.CustID = TranID LIMIT 0,1;
END IF;
ELSEIF ProName = "Over Due Date" THEN
SELECT tblcustomeroverdue.CustID INTO tranName FROM tblcustomeroverdue WHERE tblcustomeroverdue.IssueID = TranID;
SELECT CONCAT("[",tblcustomer.CustID,"] ", tblcustomer.CustName) INTO des FROM tblcustomer WHERE tblcustomer.CustID = tranName;
ELSEIF ProName = "Temporary Credit" THEN
SELECT tblcustomerissue.CustID INTO tranName FROM tblcustomerissue WHERE tblcustomerissue.IssueID = TranID;
SELECT CONCAT("[",tblcustomer.CustID,"] ", tblcustomer.CustName) INTO des FROM tblcustomer WHERE tblcustomer.CustID = tranName;
ELSEIF ProName = "General Requisition" THEN
SELECT ReqType INTO gr FROM crm_frm_general WHERE FrmID = TranID;
SELECT tmpname INTO des FROM crm_template_authorization WHERE tmpid = gr;
ELSE
SET des = "";
END IF;
RETURN des;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `ameAuthGetCustomerName`;
DELIMITER |
CREATE FUNCTION `ameAuthGetCustomerName`(ProName VARCHAR(255), TranID VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
DECLARE tranName  VARCHAR(255) DEFAULT "";
DECLARE des VARCHAR(255) DEFAULT "" ;
IF ProName = "General Requisition" THEN
SELECT crm_frm_general.CustID INTO tranName FROM crm_frm_general WHERE crm_frm_general.frmid = TranID;
IF tranName <> '' THEN
SELECT CONCAT("[",tblcustomer.CustID,"] ", tblcustomer.CustName) INTO des FROM tblcustomer WHERE tblcustomer.CustID = tranName;
ELSE
SET des = "";
END IF;
ELSEIF ProName = "Budget Requisition" THEN
SELECT crm_frm_budget.CustID INTO tranName FROM crm_frm_budget WHERE crm_frm_budget.frmid = TranID;
IF tranName <> '' THEN
SELECT CONCAT("[",tblcustomer.CustID,"] ", tblcustomer.CustName) INTO des FROM tblcustomer WHERE tblcustomer.CustID = tranName;
ELSE
SET des = "";
END IF;
ELSE
SET des = "";
END IF;
RETURN des;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_ACC`;
DELIMITER |
CREATE FUNCTION `AME_FUN_ACC`(P1 varchar(100), P2 varchar(100), P3 varchar(100)) RETURNS varchar(100) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE P_ACC VARCHAR(100) DEFAULT "";
		DECLARE C1 CURSOR FOR SELECT T1.var_accid FROM ame_mt_acc_set T1 WHERE T1.var_type = P1 AND T1.var_groupid = P2 AND T1.var_subgroupid = P3;
		OPEN C1;
				IF FOUND_ROWS() > 0 THEN 
						FETCH C1 INTO P_ACC; 		
				ELSE
						BEGIN
								DECLARE C2 CURSOR FOR SELECT T1.var_accid FROM ame_mt_acc_set T1 WHERE T1.var_type = P1 AND T1.var_groupid = 'DEFAULT' AND T1.var_subgroupid = 'DEFAULT';
								OPEN C2;
										IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_ACC; END IF;	
								CLOSE C2;
						END;
				END IF;
				RETURN P_ACC;
		CLOSE C1;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_ADDI_COST`;
DELIMITER |
CREATE FUNCTION `AME_FUN_ADDI_COST`(P1 varchar(100)) RETURNS varchar(100) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE P_ID VARCHAR(100) DEFAULT '';
		DECLARE C1 CURSOR FOR SELECT var_name FROM ame_sys_add_cost WHERE var_id = P1;
		OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_ID; RETURN P_ID; END IF; CLOSE C1;
		RETURN "";
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_AP_PY_PAID`;
DELIMITER |
CREATE FUNCTION `AME_FUN_AP_PY_PAID`(P1 varchar(100), P2 varchar(100), P3 integer) RETURNS double
    DETERMINISTIC
BEGIN
		DECLARE P_BAL DOUBLE DEFAULT 0.0;
		DECLARE P_DOC_TYPE VARCHAR(100) DEFAULT "";
		IF P1 LIKE "%AP-%" THEN
				CASE P1
				WHEN "AP-IN" THEN			
						BEGIN
								DECLARE P_AMT DOUBLE DEFAULT 0.0;
								DECLARE C2 CURSOR FOR	SELECT COALESCE(SUM(T1.dou_app + T1.dou_disc + T1.dou_amt), 0) FROM ame_ap_py_det T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P2 AND T2.var_status IN('Open', 'Posted');
								OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_AMT; SET P_BAL = ROUND(P_BAL + P_AMT, P3); END IF;CLOSE C2;
								RETURN P_BAL;
						END;
				WHEN "AP-RET" THEN
						SET P_DOC_TYPE = "Purchase Return";
				WHEN "AP-DN" THEN
						SET P_DOC_TYPE = "Debit Note";
				WHEN "AP-PY" THEN
						SET P_DOC_TYPE = "Prepayment";
				ELSE
						SET @Donothing = NULL;
				END CASE;
				BEGIN
						DECLARE P_AMT DOUBLE DEFAULT 0.0;
						DECLARE C2 CURSOR FOR	SELECT COALESCE(SUM(T1.dou_app), 0) FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P2 AND T2.var_status IN('Open', 'Posted') AND T1.var_doctype = P_DOC_TYPE;
						OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_AMT; SET P_BAL = ROUND(P_BAL + P_AMT, P3); END IF;CLOSE C2;
				END;
		ELSE
				CASE P1
				WHEN "AR-IN" THEN			
						BEGIN
								DECLARE P_AMT DOUBLE DEFAULT 0.0;
								DECLARE C2 CURSOR FOR	SELECT COALESCE(SUM(T1.dou_app + T1.dou_disc + T1.dou_amt), 0) FROM ame_ar_py_det T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P2 AND T2.var_status IN('Open', 'Posted');
								OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_AMT; SET P_BAL = ROUND(P_BAL + P_AMT, P3); END IF;CLOSE C2;
								RETURN P_BAL;
						END;
				WHEN "AR-RET" THEN
						SET P_DOC_TYPE = "Sales Return";
				WHEN "AR-CN" THEN
						SET P_DOC_TYPE = "Credit Note";
				WHEN "AR-PY" THEN
						SET P_DOC_TYPE = "Prepayment";
				WHEN "AR-PY-SD1" THEN
						BEGIN
								DECLARE P_AMT DOUBLE DEFAULT 0.0;
								DECLARE C2 CURSOR FOR	SELECT COALESCE(SUM(T1.dou_disc + T1.dou_amt), 0) FROM ame_ar_py_det T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P2 AND T2.tin_paid = 1 AND T2.var_status IN('Open', 'Posted');
								OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_AMT; SET P_BAL = ROUND(P_BAL + P_AMT, P3); END IF;CLOSE C2;
								RETURN P_BAL;
						END;
				WHEN "AR-PY-SD2" THEN
						BEGIN
								DECLARE P_AMT DOUBLE DEFAULT 0.0;
								DECLARE C2 CURSOR FOR	SELECT COALESCE(SUM(T1.dou_disc + T1.dou_amt), 0) FROM ame_ar_py_det T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P2 AND T2.tin_paid <> 1 AND T2.var_status IN('Open', 'Posted');
								OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_AMT; SET P_BAL = ROUND(P_BAL + P_AMT, P3); END IF;CLOSE C2;
								RETURN P_BAL;
						END;
				ELSE
						SET @Donothing = NULL;
				END CASE;
				BEGIN
						DECLARE P_AMT DOUBLE DEFAULT 0.0;
						DECLARE C2 CURSOR FOR	SELECT COALESCE(SUM(T1.dou_app), 0) FROM ame_ar_py_app T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P2 AND T2.var_status IN('Open', 'Posted') AND T1.var_doctype = P_DOC_TYPE;
						OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_AMT; SET P_BAL = ROUND(P_BAL + P_AMT, P3); END IF;CLOSE C2;
				END;
		END IF;
		BEGIN 
				DECLARE P_AMT DOUBLE DEFAULT 0.0;
				DECLARE C3 CURSOR FOR SELECT COALESCE(SUM(T1.dou_amt), 0) FROM ame_refund T1 WHERE T1.var_docid = P2 AND T1.var_doctype = P1 AND T1.var_status IN('Open', 'Posted');
				OPEN C3; IF FOUND_ROWS() > 0 THEN FETCH C3 INTO P_AMT; SET P_BAL = ROUND(P_BAL + P_AMT, P3); END IF;CLOSE C3;
		END;
		RETURN P_BAL;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_BATCH_EXIST`;
DELIMITER |
CREATE FUNCTION `AME_FUN_BATCH_EXIST`(P1 varchar(100), P2 varchar(100)) RETURNS int(11)
    DETERMINISTIC
BEGIN
		DECLARE P_BATCH INTEGER DEFAULT 0;
		DECLARE C1 CURSOR FOR SELECT COALESCE(MIN(T1.int_batchno), 0) FROM ame_sl_ic_batch T1 WHERE T1.var_locid = P1 AND T1.var_itemid = P2 AND T1.dou_qty > 0;
		OPEN C1; FETCH C1 INTO P_BATCH; 		
				IF P_BATCH > 0 THEN
						RETURN P_BATCH;
				ELSE
						BEGIN
								DECLARE C2 CURSOR FOR SELECT COALESCE(MAX(T1.int_batchno), 0) FROM ame_sl_ic_batch T1 WHERE T1.var_locid = P1 AND T1.var_itemid = P2 AND T1.dou_drqty > 0;
								OPEN C2; FETCH C2 INTO P_BATCH; 
										RETURN P_BATCH;
								CLOSE C2;
						END;
				END IF;	
		CLOSE C1;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_DUE_DATE`;
DELIMITER |
CREATE FUNCTION `AME_FUN_DUE_DATE`(P1 varchar(100), P2 date) RETURNS date
    DETERMINISTIC
BEGIN
		DECLARE DAT_RETURN DATE DEFAULT P2;
		DECLARE P_VAR_TYPE VARCHAR(100) DEFAULT "";
		DECLARE P_INT_DAYDUE INTEGER DEFAULT 0;
		DECLARE P_INT_DAYMIN INTEGER DEFAULT 0;
		DECLARE P_INT_LASTDAY INTEGER DEFAULT 0;
		DECLARE C1 CURSOR FOR SELECT T1.var_type, T1.int_daydue, COALESCE(T1.int_dayminpay, 0) + DAY(P2) FROM ame_mt_pmt_term T1 WHERE T1.var_id = P1;
		OPEN C1;
				IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_VAR_TYPE, P_INT_DAYDUE, P_INT_DAYMIN;
						IF P_VAR_TYPE = "Standard" THEN
								SET DAT_RETURN = DATE_ADD(P2, INTERVAL P_INT_DAYDUE DAY);
						ELSE
								SET P_INT_LASTDAY = DAY(LAST_DAY(P2));
								IF P_INT_LASTDAY > P_INT_DAYDUE THEN 
										SET P_INT_LASTDAY = P_INT_DAYDUE; 
								END IF;
								IF P_INT_DAYMIN > P_INT_LASTDAY THEN	
										SET DAT_RETURN = DATE_ADD(DATE_ADD(P2, INTERVAL P_INT_LASTDAY - DAY(P2) DAY), INTERVAL 1 MONTH) ;			
								ELSE			
										SET DAT_RETURN = DATE_ADD(P2, INTERVAL P_INT_LASTDAY - DAY(P2) DAY);
								END IF;	
						END IF;
				END IF;
		CLOSE C1;
		RETURN DAT_RETURN;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_EDIT_ITE`;
DELIMITER |
CREATE FUNCTION `AME_FUN_EDIT_ITE`(P1 varchar(100), P2 varchar(100), P3 integer, P4 varchar(100), P5 varchar(100), P6 varchar(100)) RETURNS varchar(100) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE P_CHECK TINYINT DEFAULT 0;
		CASE 
		WHEN P1 = "AP-IN" THEN
				BEGIN 
						DECLARE C1 CURSOR FOR SELECT T1.var_id FROM ame_ap_ret_det T1 LEFT JOIN ame_ap_ret T2 ON T1.var_id = T2.var_id WHERE T1.var_poid = P2 AND T1.int_poline = P3 AND T1.var_itemid = P4 AND T1.var_locid = P5 AND T1.var_uomid = P6 AND (COALESCE(T1.dou_qty, 0) > 0 OR COALESCE(T1.dou_foc, 0) > 0) AND T2.var_status IN('Open','Posted');
						OPEN C1; IF FOUND_ROWS() > 0 THEN SET P_CHECK = 1; END IF; CLOSE C1;
				END;
		WHEN P1 = "AR-IN" THEN
				BEGIN 
						DECLARE C1 CURSOR FOR SELECT T1.var_id FROM ame_ar_ret_det T1 LEFT JOIN ame_ar_ret T2 ON T1.var_id = T2.var_id WHERE T1.var_poid = P2 AND T1.int_poline = P3 AND T1.var_itemid = P4 AND T1.var_locid = P5 AND T1.var_uomid = P6 AND (COALESCE(T1.dou_qty, 0) > 0 OR COALESCE(T1.dou_foc, 0) > 0) AND T2.var_status IN('Open','Posted');
						OPEN C1; IF FOUND_ROWS() > 0 THEN SET P_CHECK = 1; END IF; CLOSE C1;
				END;
		ELSE
				SET @Donothing = NULL;
		END CASE;
		RETURN P_CHECK;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_EDIT_ITE_1`;
DELIMITER |
CREATE FUNCTION `AME_FUN_EDIT_ITE_1`(P1 varchar(100), P2 varchar(100), P3 varchar(100), P4 varchar(100)) RETURNS varchar(100) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE P_CHECK TINYINT DEFAULT 0;
		DECLARE P_BATCH INTEGER DEFAULT 0;
		CASE 
		WHEN P1 = "AP-IN" THEN
				BEGIN 
						DECLARE C1 CURSOR FOR SELECT COALESCE(T2.int_batchno, 0) FROM ame_ap_in T1 LEFT JOIN ame_ap_in_det T2 ON T1.var_id = T2.var_id WHERE T1.var_id = P2 AND T1.var_status = "Posted" ORDER BY T2.int_line LIMIT 1;
						OPEN C1; 
								IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_BATCH;
										BEGIN
												DECLARE C2 CURSOR FOR SELECT COALESCE(T1.int_id, 0) FROM ame_sl_ic_his T1 WHERE T1.var_itemid = P3 AND T1.var_locid = P4 AND T1.int_batchno = P_BATCH AND (T1.dou_qty < 0 OR (T1.var_modid = "AP-IN" AND T1.var_doctype IN("AC1", "AC2", "AC3", "AC4", "AC5") AND (T1.dou_ac1 <> 0 OR T1.dou_ac2 <> 0 OR T1.dou_ac3 <> 0 OR T1.dou_ac4 <> 0 OR T1.dou_ac5 <> 0))) LIMIT 1;
												OPEN C2; 
														IF FOUND_ROWS() > 0 THEN SET P_CHECK = 1;END IF; 
												CLOSE C2;
										END;
								END IF; 
						CLOSE C1;
				END;
		WHEN P1 = "AR-CN" THEN
				BEGIN
						DECLARE IN_D1 INTEGER DEFAULT 0;
						DECLARE IN_LIN INTEGER DEFAULT 0;
						DECLARE IN_BAT INTEGER DEFAULT 0;
						DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
						DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
						DECLARE VC_LOC VARCHAR(100) DEFAULT "";
						DECLARE VC_ITE VARCHAR(100) DEFAULT "";
						DECLARE VC_UOM VARCHAR(100) DEFAULT "";
						DECLARE DB_QOH DOUBLE DEFAULT 0.0;
						DECLARE DB_QTY DOUBLE DEFAULT 0.0;
						DECLARE C1 CURSOR FOR
						SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
						SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
						SELECT T1.int_line, T1.var_locid AS var_locid, T1.var_itemid AS var_itemid, T1.var_uomid AS var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
						FROM ame_ar_cn_det T1
						LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
						LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
						LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
						LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T1.var_itemid = P3 AND T1.var_locid = P4 AND T2.var_type <> 'Non-Inventory Part'
						ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
						OPEN C1;
								L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
										IF IN_D1 = 1 THEN LEAVE L1; END IF;
										BEGIN
												DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
												OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
												IF COALESCE(DB_QOH, 0) < COALESCE(DB_QTY, 0) THEN
														RETURN 1;
												END IF;
										END;
								END LOOP L1;
						CLOSE C1;
						RETURN 0;
				END;
		ELSE
				SET @Donothing = NULL;
		END CASE;
		RETURN P_CHECK;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_EDIT_VEN_CUS`;
DELIMITER |
CREATE FUNCTION `AME_FUN_EDIT_VEN_CUS`(P1 varchar(100), P2 varchar(100), P3 varchar(100), P4 tinyint(1)) RETURNS varchar(2000) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE P_CONCAT_MAIN VARCHAR(2000) DEFAULT "";
		DECLARE P_DOC_TYPE VARCHAR(100) DEFAULT "";
		CASE 
		WHEN P1 = 'AP-IN' THEN
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DECLARE P_VEN VARCHAR(100) DEFAULT "";
						DECLARE P_CONCAT_ID VARCHAR(2000) DEFAULT "";
						DECLARE C1 CURSOR FOR SELECT DISTINCTROW T1.var_id, T2.var_venid FROM ame_ap_ret_det T1 LEFT JOIN ame_ap_ret T2 ON T1.var_id = T2.var_id WHERE T1.var_poid = P2 AND T2.var_status IN('Open', 'Posted');
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;
								L1: LOOP FETCH C1 INTO P_ID, P_VEN;
										IF D1 = 1 THEN LEAVE L1;END IF;
										SET P_CONCAT_ID = CONCAT(P_CONCAT_ID, P_ID, ", ");
								END LOOP;
								IF ((P3 <> P_VEN AND P_VEN <> "") OR P4 = 1) AND P_CONCAT_ID <> '' THEN
										SET P_CONCAT_MAIN = CONCAT("- Purchase Return : ", SUBSTR(P_CONCAT_ID FROM 1 FOR LENGTH(P_CONCAT_ID) - 2)); 
								END IF;
						CLOSE C1;
				END;
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DECLARE P_VEN VARCHAR(100) DEFAULT "";
						DECLARE P_CONCAT_ID VARCHAR(2000) DEFAULT "";
						DECLARE C1 CURSOR FOR SELECT DISTINCTROW T1.var_id, T2.var_venid FROM ame_ap_py_det T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P2 AND COALESCE(T2.tin_paid, 0) <> 1 AND T2.var_status IN('Open', 'Posted');
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;
								L1: LOOP FETCH C1 INTO P_ID, P_VEN;
										IF D1 = 1 THEN LEAVE L1;END IF;
										SET P_CONCAT_ID = CONCAT(P_CONCAT_ID, P_ID, ", ");
								END LOOP;
								IF ((P3 <> P_VEN AND P_VEN <> "") OR P4 = 1) AND P_CONCAT_ID <> '' THEN
										SET P_CONCAT_MAIN = CONCAT(P_CONCAT_MAIN, "\n- Payment : ", SUBSTR(P_CONCAT_ID FROM 1 FOR LENGTH(P_CONCAT_ID) - 2)); 
								END IF;
						CLOSE C1;
				END;
		WHEN P1 IN('AP-RET', 'AP-DN', 'AP-PY') THEN
				SET @T_PAID = 0;
				IF P1 = 'AP-RET' THEN
						SET P_DOC_TYPE = 'Purchase Return';
						SET @T_PAID = 2;
				ELSEIF P1 = 'AP-DN' THEN
						SET P_DOC_TYPE = 'Debit Note';
						SET @T_PAID = 3;
				ELSEIF P1 = 'AP-PY' THEN
						SET P_DOC_TYPE = 'Prepayment';
						SET @T_PAID = 4;
				END IF;
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DECLARE P_VEN VARCHAR(100) DEFAULT "";
						DECLARE P_CONCAT_ID VARCHAR(2000) DEFAULT "";
						DECLARE C1 CURSOR FOR SELECT DISTINCTROW T1.var_id, T2.var_venid FROM ame_ap_py_app T1 LEFT JOIN ame_ap_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = P_DOC_TYPE AND T1.var_docid = P2 AND COALESCE(T2.tin_paid, 0) <> @T_PAID AND T2.var_status IN('Open', 'Posted');
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;
								L1: LOOP FETCH C1 INTO P_ID, P_VEN;
										IF D1 = 1 THEN LEAVE L1;END IF;
										SET P_CONCAT_ID = CONCAT(P_CONCAT_ID, P_ID, ", ");
								END LOOP;
								IF ((P3 <> P_VEN AND P_VEN <> "") OR P4 = 1) AND P_CONCAT_ID <> '' THEN
										SET P_CONCAT_MAIN = CONCAT("- Payment : ", SUBSTR(P_CONCAT_ID FROM 1 FOR LENGTH(P_CONCAT_ID) - 2)); 
								END IF;
						CLOSE C1;
				END;
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DECLARE P_VEN VARCHAR(100) DEFAULT "";
						DECLARE P_CONCAT_ID VARCHAR(2000) DEFAULT "";
						DECLARE C1 CURSOR FOR SELECT T1.var_id, T1.var_perid FROM ame_refund T1 WHERE T1.var_doctype = P1 AND T1.var_docid = P2 AND T1.var_status IN('Open', 'Posted');
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;
								L1: LOOP FETCH C1 INTO P_ID, P_VEN;
										IF D1 = 1 THEN LEAVE L1;END IF;
										SET P_CONCAT_ID = CONCAT(P_CONCAT_ID, P_ID, ", ");
								END LOOP;
								IF ((P3 <> P_VEN AND P_VEN <> "") OR P4 = 1) AND P_CONCAT_ID <> "" THEN
										SET P_CONCAT_MAIN = CONCAT(P_CONCAT_MAIN,"\n- Refund  : ", SUBSTR(P_CONCAT_ID FROM 1 FOR LENGTH(P_CONCAT_ID) - 2)); 
								END IF;
						CLOSE C1;
				END;
		WHEN P1 = 'AR-IN' THEN
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DECLARE P_VEN VARCHAR(100) DEFAULT "";
						DECLARE P_CONCAT_ID VARCHAR(2000) DEFAULT "";
						DECLARE C1 CURSOR FOR SELECT DISTINCTROW T1.var_id, T2.var_cusid FROM ame_ar_ret_det T1 LEFT JOIN ame_ar_ret T2 ON T1.var_id = T2.var_id WHERE T1.var_poid = P2 AND T2.var_status IN('Open', 'Posted');
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;
								L1: LOOP FETCH C1 INTO P_ID, P_VEN;
										IF D1 = 1 THEN LEAVE L1;END IF;
										SET P_CONCAT_ID = CONCAT(P_CONCAT_ID, P_ID, ", ");
								END LOOP;
								IF ((P3 <> P_VEN AND P_VEN <> "") OR P4 = 1) AND P_CONCAT_ID <> '' THEN
										SET P_CONCAT_MAIN = CONCAT("- Sales Return : ", SUBSTR(P_CONCAT_ID FROM 1 FOR LENGTH(P_CONCAT_ID) - 2)); 
								END IF;
						CLOSE C1;
				END;
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DECLARE P_VEN VARCHAR(100) DEFAULT "";
						DECLARE P_CONCAT_ID VARCHAR(2000) DEFAULT "";
						DECLARE C1 CURSOR FOR SELECT DISTINCTROW T1.var_id, T2.var_cusid FROM ame_ar_py_det T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_docid = P2 AND COALESCE(T2.tin_paid, 0) <> 1 AND T2.var_status IN('Open', 'Posted');
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;
								L1: LOOP FETCH C1 INTO P_ID, P_VEN;
										IF D1 = 1 THEN LEAVE L1;END IF;
										SET P_CONCAT_ID = CONCAT(P_CONCAT_ID, P_ID, ", ");
								END LOOP;
								IF ((P3 <> P_VEN AND P_VEN <> "") OR P4 = 1) AND P_CONCAT_ID <> '' THEN
										SET P_CONCAT_MAIN = CONCAT(P_CONCAT_MAIN, "\n- Receipt : ", SUBSTR(P_CONCAT_ID FROM 1 FOR LENGTH(P_CONCAT_ID) - 2)); 
								END IF;
						CLOSE C1;
				END;
		WHEN P1 IN('AR-RET', 'AR-CN', 'AR-PY') THEN
				SET @T_PAID = 0;
				IF P1 = 'AR-RET' THEN
						SET P_DOC_TYPE = 'Sales Return';
						SET @T_PAID = 2;
				ELSEIF P1 = 'AR-CN' THEN
						SET P_DOC_TYPE = 'Credit Note';
						SET @T_PAID = 3;
				ELSEIF P1 = 'AR-PY' THEN
						SET P_DOC_TYPE = 'Prepayment';
						SET @T_PAID = 4;
				END IF;
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DECLARE P_VEN VARCHAR(100) DEFAULT "";
						DECLARE P_CONCAT_ID VARCHAR(2000) DEFAULT "";
						DECLARE C1 CURSOR FOR SELECT DISTINCTROW T1.var_id, T2.var_cusid FROM ame_ar_py_app T1 LEFT JOIN ame_ar_py T2 ON T1.var_id = T2.var_id WHERE T1.var_doctype = P_DOC_TYPE AND T1.var_docid = P2 AND COALESCE(T2.tin_paid, 0) <> @T_PAID AND T2.var_status IN('Open', 'Posted');
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;
								L1: LOOP FETCH C1 INTO P_ID, P_VEN;
										IF D1 = 1 THEN LEAVE L1;END IF;
										SET P_CONCAT_ID = CONCAT(P_CONCAT_ID, P_ID, ", ");
								END LOOP;
								IF ((P3 <> P_VEN AND P_VEN <> "") OR P4 = 1) AND P_CONCAT_ID <> '' THEN
										SET P_CONCAT_MAIN = CONCAT("- Payment : ", SUBSTR(P_CONCAT_ID FROM 1 FOR LENGTH(P_CONCAT_ID) - 2)); 
								END IF;
						CLOSE C1;
				END;
				BEGIN
						DECLARE D1 INTEGER DEFAULT 0;
						DECLARE P_ID VARCHAR(100) DEFAULT "";
						DECLARE P_VEN VARCHAR(100) DEFAULT "";
						DECLARE P_CONCAT_ID VARCHAR(2000) DEFAULT "";
						DECLARE C1 CURSOR FOR SELECT T1.var_id, T1.var_perid FROM ame_refund T1 WHERE T1.var_doctype = P1 AND T1.var_docid = P2 AND T1.var_status IN('Open', 'Posted');
						DECLARE CONTINUE HANDLER FOR NOT FOUND SET D1 = 1;
						OPEN C1;
								L1: LOOP FETCH C1 INTO P_ID, P_VEN;
										IF D1 = 1 THEN LEAVE L1;END IF;
										SET P_CONCAT_ID = CONCAT(P_CONCAT_ID, P_ID, ", ");
								END LOOP;
								IF ((P3 <> P_VEN AND P_VEN <> "") OR P4 = 1) AND P_CONCAT_ID <> '' THEN
										SET P_CONCAT_MAIN = CONCAT(P_CONCAT_MAIN,"\n- Refund  : ", SUBSTR(P_CONCAT_ID FROM 1 FOR LENGTH(P_CONCAT_ID) - 2)); 
								END IF;
						CLOSE C1;
				END;
		ELSE
				SET @Donothing = NULL;
		END CASE;
		RETURN P_CONCAT_MAIN;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_ITEM_CON`;
DELIMITER |
CREATE FUNCTION `AME_FUN_ITEM_CON`(P1 varchar(100), P2 varchar(100)) RETURNS double
    DETERMINISTIC
BEGIN
		DECLARE P_ID DOUBLE DEFAULT 1;
		DECLARE C1 CURSOR FOR SELECT COALESCE(T.dou_conv, 1) FROM ame_mt_ite_uom T WHERE T.var_id = P1 AND T.var_uomid = P2 LIMIT 1;
		OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_ID; RETURN P_ID; END IF; CLOSE C1;
		RETURN 1;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_ITEM_NEG`;
DELIMITER |
CREATE FUNCTION `AME_FUN_ITEM_NEG`(P1 varchar(100), P2  varchar(100), P3 varchar(10)) RETURNS varchar(500) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE IN_ALLOW_NEG INTEGER DEFAULT 0;
		DECLARE P_DEC_QTY INTEGER DEFAULT 5;
		# DECIMAL FUNCTIONAL CURRENCY
		BEGIN DECLARE C1 CURSOR FOR SELECT T.int_qty FROM ame_sys_decimal T WHERE T.var_id = (SELECT T2.var_id FROM ame_mt_cur T2 WHERE T2.tin_default = 1);OPEN C1;IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_DEC_QTY;  END IF;CLOSE C1;END;
		SET @P_DEC_QTY = P_DEC_QTY;
		CASE P1
		WHEN "AP-IN" THEN	
				CASE P3
				WHEN "REPOST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE IN_BAT INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale, int_batchno FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale, int_batchno FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty, int_batchno
								FROM ame_sys_tem_ap_in T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid, T1.int_batchno ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY, IN_BAT;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT COALESCE(T1.dou_qty, 0) - COALESCE(T2.dou_qty, 0) AS QOH FROM ame_sl_ic_batch T1
														LEFT JOIN (SELECT T1.var_locid, IF(ISNULL(T3.var_itemid), T1.var_itemid, T3.var_itemid) AS var_itemid, SUM(T1.dou_qty * T4.dou_conv * COALESCE(T3.dou_qty * T5.dou_conv, 1)) AS dou_qty
														FROM ame_ap_in_det T1
														LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
														LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
														LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
														LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE AND T2.var_type <> 'Non-Inventory Part') AS T2 ON T1.var_locid = T2.var_locid AND T1.var_itemid = T2.var_itemid
														WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE AND T1.int_batchno = IN_BAT;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) +  COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ap_in;
																RETURN CONCAT("You can't change quantity to ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nMinimum quantity in this location is ", -COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ap_in;
								RETURN "";
						END;
				ELSE
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE IN_BAT INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale, T1.int_batchno FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale, T1.int_batchno FROM (
								SELECT T1.int_line, T1.var_locid AS var_locid, T1.var_itemid AS var_itemid, T1.var_uomid AS var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty, T1.int_batchno
								FROM ame_ap_in_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY, IN_BAT;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(T1.dou_qty, @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE AND T1.int_batchno = IN_BAT;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																RETURN CONCAT("You don't have sufficient quantity available to substract ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								RETURN "";
						END;
				END CASE;
		WHEN "AR-RET" THEN	
				CASE P3
				WHEN "REPOST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE IN_BAT INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ar_ret T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_qty), 0) - COALESCE(T2.dou_qty, 0) AS QOH FROM ame_sl_ic_batch T1
														LEFT JOIN (SELECT T1.var_locid, IF(ISNULL(T3.var_itemid), T1.var_itemid, T3.var_itemid) AS var_itemid, SUM(T1.dou_qty * T4.dou_conv * COALESCE(T3.dou_qty * T5.dou_conv, 1)) AS dou_qty
														FROM ame_ar_ret_det T1
														LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
														LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
														LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
														LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE AND T2.var_type <> 'Non-Inventory Part') AS T2 ON T1.var_locid = T2.var_locid AND T1.var_itemid = T2.var_itemid
														WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) + COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ar_ret;
																RETURN CONCAT("You can't change quantity to ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nMinimum quantity in this location is ", -COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ar_ret;
								RETURN "";
						END;
				ELSE
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE IN_BAT INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line,   T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid AS var_locid, T1.var_itemid AS var_itemid, T1.var_uomid AS var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ar_ret_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																RETURN CONCAT("You don't have sufficient quantity available to substract ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								RETURN "";
						END;
				END CASE;
		WHEN "AR-CN" THEN	
				CASE P3
				WHEN "REPOST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE IN_BAT INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ar_cn T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_qty), 0) - COALESCE(T2.dou_qty, 0) AS QOH FROM ame_sl_ic_batch T1
														LEFT JOIN (SELECT T1.var_locid, IF(ISNULL(T3.var_itemid), T1.var_itemid, T3.var_itemid) AS var_itemid, SUM(T1.dou_qty * T4.dou_conv * COALESCE(T3.dou_qty * T5.dou_conv, 1)) AS dou_qty
														FROM ame_ar_cn_det T1
														LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
														LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
														LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
														LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE AND T2.var_type <> 'Non-Inventory Part') AS T2 ON T1.var_locid = T2.var_locid AND T1.var_itemid = T2.var_itemid
														WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) + COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ar_cn;
																RETURN CONCAT("You can't change quantity to ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nMinimum quantity in this location is ", -COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ar_cn;
								RETURN "";
						END;
				ELSE
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE IN_BAT INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid AS var_locid, T1.var_itemid AS var_itemid, T1.var_uomid AS var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ar_cn_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																RETURN CONCAT("You don't have sufficient quantity available to substract ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								RETURN "";
						END;
				END CASE;
		WHEN "AP-RET" THEN	
				CASE P3
				WHEN "SAVE" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ap_ret T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ap_ret;
																RETURN CONCAT("You don't have sufficient quantity available to return ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ap_ret;
								RETURN "";
						END;
				WHEN "POST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ap_ret_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ap_ret;
																RETURN CONCAT("You don't have sufficient quantity available to return ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ap_ret;
								RETURN "";
						END;
				WHEN "REPOST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ap_ret T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_qty), 0) + COALESCE(T2.dou_qty, 0) AS QOH FROM ame_sl_ic_batch T1
														LEFT JOIN (SELECT T1.var_locid, IF(ISNULL(T3.var_itemid), T1.var_itemid, T3.var_itemid) AS var_itemid, SUM((T1.dou_qty + T1.dou_foc) * T4.dou_conv * COALESCE(T3.dou_qty * T5.dou_conv, 1)) AS dou_qty
														FROM ame_ap_ret_det T1
														LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
														LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
														LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
														LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE AND T2.var_type <> 'Non-Inventory Part') AS T2 ON T1.var_locid = T2.var_locid AND T1.var_itemid = T2.var_itemid
														WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ap_ret;
																RETURN CONCAT("You don't have sufficient quantity available to return ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ap_ret;
								RETURN "";
						END;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "AP-DN" THEN	
				CASE P3
				WHEN "SAVE" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ap_dn T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ap_dn;
																RETURN CONCAT("You don't have sufficient quantity available to return ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ap_dn;
								RETURN "";
						END;
				WHEN "POST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ap_dn_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ap_dn;
																RETURN CONCAT("You don't have sufficient quantity available to return ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ap_dn;
								RETURN "";
						END;
				WHEN "REPOST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ap_dn T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_qty), 0) + COALESCE(T2.dou_qty, 0) AS QOH FROM ame_sl_ic_batch T1
														LEFT JOIN (SELECT T1.var_locid, IF(ISNULL(T3.var_itemid), T1.var_itemid, T3.var_itemid) AS var_itemid, SUM((T1.dou_qty + T1.dou_foc) * T4.dou_conv * COALESCE(T3.dou_qty * T5.dou_conv, 1)) AS dou_qty
														FROM ame_ap_dn_det T1
														LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
														LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
														LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
														LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE AND T2.var_type <> 'Non-Inventory Part') AS T2 ON T1.var_locid = T2.var_locid AND T1.var_itemid = T2.var_itemid
														WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ap_dn;
																RETURN CONCAT("You don't have sufficient quantity available to return ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ap_dn;
								RETURN "";
						END;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "AR-IN" THEN	
				CASE P3
				WHEN "SAVE" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ar_in T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ar_in;
																RETURN CONCAT("You don't have sufficient quantity available to sell ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ar_in;
								RETURN "";
						END;
				WHEN "POST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ar_in_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ar_in;
																RETURN CONCAT("You don't have sufficient quantity available to sell ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ar_in;
								RETURN "";
						END;
				WHEN "REPOST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ar_in T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_qty), 0) + COALESCE(T2.dou_qty, 0) AS QOH FROM ame_sl_ic_batch T1
														LEFT JOIN (SELECT T1.var_locid, IF(ISNULL(T3.var_itemid), T1.var_itemid, T3.var_itemid) AS var_itemid, SUM((T1.dou_qty + T1.dou_foc) * T4.dou_conv * COALESCE(T3.dou_qty * T5.dou_conv, 1)) AS dou_qty
														FROM ame_ar_in_det T1
														LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
														LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
														LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
														LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE AND T2.var_type <> 'Non-Inventory Part') AS T2 ON T1.var_locid = T2.var_locid AND T1.var_itemid = T2.var_itemid
														WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ar_in;
																RETURN CONCAT("You don't have sufficient quantity available to sell ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ar_in;
								RETURN "";
						END;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "IC-TF" THEN	
				CASE P3
				WHEN "SAVE" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ic T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic;
																RETURN CONCAT("You don't have sufficient quantity available to transfer ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic;
								RETURN "";
						END;
				WHEN "POST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ic_tf_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic;
																RETURN CONCAT("You don't have sufficient quantity available to transfer ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic;
								RETURN "";
						END;
				WHEN "REPOST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC_TO VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE DB_OLD DOUBLE DEFAULT 0.0;
								DECLARE DB_NEW DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.var_tolocid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_tolocid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_tolocid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ic T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.var_tolocid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_LOC_TO, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C3 CURSOR FOR SELECT COALESCE(SUM(T1.dou_qty * T4.dou_conv * COALESCE(T3.dou_qty * T5.dou_conv, 1)), 0) AS dou_qty
														FROM ame_ic_tf_det T1
														LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
														LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
														LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
														LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T1.var_locid = VC_LOC AND T1.var_tolocid = VC_LOC_TO AND T1.var_itemid = VC_ITE AND T2.var_type <> 'Non-Inventory Part';
														OPEN C3; IF FOUND_ROWS() > 0 THEN FETCH C3 INTO DB_OLD; ELSE SET DB_OLD = 0;END IF;CLOSE C3;
														SET DB_NEW = AME_FUN_ROUND(DB_OLD - DB_QTY, @P_DEC_QTY);
												END;
												IF DB_NEW > 0 THEN
														BEGIN
																DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_qty), 0) AS QOH FROM ame_sl_ic_batch T1 WHERE T1.var_locid = VC_LOC_TO AND T1.var_itemid = VC_ITE;
																OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
																IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_NEW, 0), @P_DEC_QTY) < 0  THEN
																		DELETE FROM ame_sys_tem_ic;
																		RETURN CONCAT("You don't have sufficient quantity available to transfer ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
																END IF;
														END;
												ELSE
														BEGIN
																DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_qty), 0) AS QOH FROM ame_sl_ic_batch T1 WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
																OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
																IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) + COALESCE(DB_NEW, 0), @P_DEC_QTY) < 0  THEN
																		DELETE FROM ame_sys_tem_ic;
																		RETURN CONCAT("You don't have sufficient quantity available to transfer ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0) + COALESCE(DB_OLD, 0), "\nInvalid item in line number: ", IN_LIN);
																END IF;
														END;
												END IF;
												
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic;
								RETURN "";
						END;
				ELSE
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_tolocid AS var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ic_tf_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0  THEN
																RETURN CONCAT("You don't have sufficient quantity available to transfer ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								RETURN "";
						END;
				END CASE;
		WHEN "IC-IU" THEN	
				CASE P3
				WHEN "SAVE" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ic_iu T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic_iu;
																RETURN CONCAT("You don't have sufficient quantity available to use ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic_iu;
								RETURN "";
						END;
				WHEN "POST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ic_iu_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic_iu;
																RETURN CONCAT("You don't have sufficient quantity available to transfer ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic_iu;
								RETURN "";
						END;
				WHEN "REPOST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ic_iu T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_qty), 0) + COALESCE(T2.dou_qty, 0) AS QOH FROM ame_sl_ic_batch T1
														LEFT JOIN (SELECT T1.var_locid, IF(ISNULL(T3.var_itemid), T1.var_itemid, T3.var_itemid) AS var_itemid, SUM(T1.dou_qty * T4.dou_conv * COALESCE(T3.dou_qty * T5.dou_conv, 1)) AS dou_qty
														FROM ame_ic_iu_det T1
														LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
														LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
														LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
														LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE AND T2.var_type <> 'Non-Inventory Part') AS T2 ON T1.var_locid = T2.var_locid AND T1.var_itemid = T2.var_itemid
														WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic_iu;
																RETURN CONCAT("You don't have sufficient quantity available to use ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic_iu;
								RETURN "";
						END;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		WHEN "IC-ICUOM" THEN	
				CASE P3
				WHEN "SAVE" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ic_icuom T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic_icuom;
																RETURN CONCAT("You don't have sufficient quantity available to convert ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic_icuom;
								RETURN "";
						END;
				WHEN "POST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ic_icuom_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic_icuom;
																RETURN CONCAT("You don't have sufficient quantity available to convert ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic_icuom;
								RETURN "";
						END;
				WHEN "REPOST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ic_icuom T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_qty), 0) + COALESCE(T2.dou_qty, 0) AS QOH FROM ame_sl_ic_batch T1
														LEFT JOIN (SELECT T1.var_locid, IF(ISNULL(T3.var_itemid), T1.var_itemid, T3.var_itemid) AS var_itemid, SUM(T1.dou_qty * T4.dou_conv * COALESCE(T3.dou_qty * T5.dou_conv, 1)) AS dou_qty
														FROM ame_ic_icuom_det T1
														LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
														LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
														LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
														LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE AND T2.var_type <> 'Non-Inventory Part') AS T2 ON T1.var_locid = T2.var_locid AND T1.var_itemid = T2.var_itemid
														WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic_icuom;
																RETURN CONCAT("You don't have sufficient quantity available to convert ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic_icuom;
								RETURN "";
						END;
				ELSE
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_tolocid AS var_locid, T1.var_toitemid AS var_itemid, T1.var_touomid AS var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ic_icuom_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																RETURN CONCAT("You don't have sufficient quantity available to convert ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								RETURN "";
						END;
				END CASE;
		WHEN "IC-IP" THEN	
				CASE P3
				WHEN "SAVE" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ic_ip T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic_ip;
																RETURN CONCAT("You don't have sufficient quantity available to convert ", IF(COALESCE(DB_QTY, 0) > 0, COALESCE(DB_QTY, 0), -COALESCE(DB_QTY, 0)), " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic_ip;
								RETURN "";
						END;
				WHEN "POST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ic_ip_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part' AND T1.tin_pro = 0
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic_ia;
																RETURN CONCAT("You don't have sufficient quantity available to convert ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic_ia;
								RETURN "";
						END;
				WHEN "REPOST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ic_ip T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_qty), 0) + COALESCE(T2.dou_qty, 0) AS QOH FROM ame_sl_ic_batch T1
														LEFT JOIN (SELECT T1.var_locid, IF(ISNULL(T3.var_itemid), T1.var_itemid, T3.var_itemid) AS var_itemid, SUM(T1.dou_qty * T4.dou_conv * COALESCE(T3.dou_qty * T5.dou_conv, 1)) AS dou_qty
														FROM ame_ic_ip_det T1
														LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
														LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
														LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
														LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE AND T2.var_type <> 'Non-Inventory Part') AS T2 ON T1.var_locid = T2.var_locid AND T1.var_itemid = T2.var_itemid
														WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic_ip;
																RETURN CONCAT("You don't have sufficient quantity available to convert ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic_ip;
								RETURN "";
						END;
				ELSE
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_tolocid AS var_locid, T1.var_toitemid AS var_itemid, T1.var_touomid AS var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ic_ip_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																RETURN CONCAT("You don't have sufficient quantity available to remove ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								RETURN "";
						END;
				END CASE;
		WHEN "IC-IA" THEN	
				CASE P3
				WHEN "SAVE" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ic_ia T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part' AND T1.dou_qty < 0
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) + COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic_ia;
																RETURN CONCAT("You don't have sufficient quantity available to adjust ", IF(COALESCE(DB_QTY, 0) > 0, COALESCE(DB_QTY, 0), -COALESCE(DB_QTY, 0)), " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic_ia;
								RETURN "";
						END;
				WHEN "POST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ic_ia_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part' AND T1.dou_qty < 0
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) + COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic_ia;
																RETURN CONCAT("You don't have sufficient quantity available to adjust ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic_ia;
								RETURN "";
						END;
				WHEN "REPOST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_sys_tem_ic_ia T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(COALESCE(SUM(T1.dou_qty), 0) - COALESCE(T2.dou_qty, 0), @P_DEC_QTY)  AS QOH FROM ame_sl_ic_batch T1
														LEFT JOIN (SELECT T1.var_locid, IF(ISNULL(T3.var_itemid), T1.var_itemid, T3.var_itemid) AS var_itemid, SUM(T1.dou_qty * T4.dou_conv * COALESCE(T3.dou_qty * T5.dou_conv, 1)) AS dou_qty
														FROM ame_ic_ia_det T1
														LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
														LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
														LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
														LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE AND T2.var_type <> 'Non-Inventory Part') AS T2 ON T1.var_locid = T2.var_locid AND T1.var_itemid = T2.var_itemid
														WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) +  COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM ame_sys_tem_ic_ia;
																RETURN CONCAT("You don't have sufficient quantity available to adjust ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM ame_sys_tem_ic_ia;
								RETURN "";
						END;
				ELSE
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid AS var_locid, T1.var_itemid AS var_itemid, T1.var_uomid AS var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM ame_ic_ia_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part' AND T1.dou_qty > 0
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																RETURN CONCAT("You don't have sufficient quantity available to adjust ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," from location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								RETURN "";
						END;
				END CASE;
	WHEN "POS-AR-IN" THEN	
				CASE P3
				WHEN "SAVE" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM pos_sys_tem_ar_in T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM pos_sys_tem_ar_in;
																RETURN CONCAT("You don't have sufficient quantity available to sell ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM pos_sys_tem_ar_in;
								RETURN "";
						END;
				WHEN "POST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM pos_ar_in_det T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT AME_FUN_ROUND(SUM(T1.dou_qty), @P_DEC_QTY), T2.var_uomid FROM ame_sl_ic_batch T1 LEFT JOIN ame_mt_ite T2 ON T1.var_itemid = T2.var_id WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH, VC_UOM; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM pos_sys_tem_ar_in;
																RETURN CONCAT("You don't have sufficient quantity available to sell ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM pos_sys_tem_ar_in;
								RETURN "";
						END;
				WHEN "REPOST" THEN
						BEGIN
								DECLARE IN_D1 INTEGER DEFAULT 0;
								DECLARE IN_LIN INTEGER DEFAULT 0;
								DECLARE VC_ASS_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_ASS_UOM VARCHAR(100) DEFAULT "";
								DECLARE VC_LOC VARCHAR(100) DEFAULT "";
								DECLARE VC_ITE VARCHAR(100) DEFAULT "";
								DECLARE VC_UOM VARCHAR(100) DEFAULT "";
								DECLARE DB_QOH DOUBLE DEFAULT 0.0;
								DECLARE DB_QTY DOUBLE DEFAULT 0.0;
								DECLARE C1 CURSOR FOR
								SELECT T1.int_line, T1.var_locid, T1.ass_itemid, T1.ass_uomid, T1.com_itemid, AME_FUN_ROUND(SUM(T1.this_sale), @P_DEC_QTY) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid AS ass_itemid, T1.var_uomid AS ass_uomid, IF(ISNULL(T1.com_itemid), T1.var_itemid, T1.com_itemid) AS com_itemid, T1.ass_qty * COALESCE(T1.com_qty, 1) AS this_sale FROM (
								SELECT T1.int_line, T1.var_locid, T1.var_itemid, T1.var_uomid, T3.var_itemid AS com_itemid, T3.var_uomid AS com_uomid, T1.dou_qty * T4.dou_conv AS ass_qty, T3.dou_qty * T5.dou_conv AS com_qty
								FROM pos_sys_tem_ar_in T1
								LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
								LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
								LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
								LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T2.var_type <> 'Non-Inventory Part'
								ORDER BY T1.int_line) AS T1) T1 GROUP BY T1.var_locid, T1.com_itemid ORDER BY T1.int_line;
								DECLARE CONTINUE HANDLER FOR NOT FOUND SET IN_D1 = 1;
								OPEN C1;
										L1: LOOP FETCH C1 INTO IN_LIN, VC_LOC, VC_ASS_ITE, VC_ASS_UOM, VC_ITE, DB_QTY;
												IF IN_D1 = 1 THEN LEAVE L1; END IF;
												BEGIN
														DECLARE C2 CURSOR FOR SELECT COALESCE(SUM(T1.dou_qty), 0) + COALESCE(T2.dou_qty, 0) AS QOH FROM ame_sl_ic_batch T1
														LEFT JOIN (SELECT T1.var_locid, IF(ISNULL(T3.var_itemid), T1.var_itemid, T3.var_itemid) AS var_itemid, SUM((T1.dou_qty + T1.dou_foc) * T4.dou_conv * COALESCE(T3.dou_qty * T5.dou_conv, 1)) AS dou_qty
														FROM pos_ar_in_det T1
														LEFT JOIN ame_mt_ite T2 ON (T1.var_itemid = T2.var_id)
														LEFT JOIN ame_mt_ite_ass T3 ON (T1.var_itemid = T3.var_id)
														LEFT JOIN ame_mt_ite_uom T4 ON (T1.var_itemid = T4.var_id AND T1.var_uomid = T4.var_uomid)
														LEFT JOIN ame_mt_ite_uom T5 ON (T3.var_itemid = T5.var_id AND T3.var_uomid = T5.var_uomid) WHERE T1.var_id = P2 AND T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE AND T2.var_type <> 'Non-Inventory Part') AS T2 ON T1.var_locid = T2.var_locid AND T1.var_itemid = T2.var_itemid
														WHERE T1.var_locid = VC_LOC AND T1.var_itemid = VC_ITE;
														OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO DB_QOH; ELSE SET DB_QOH = 0;END IF;CLOSE C2;
														IF AME_FUN_ROUND(COALESCE(DB_QOH, 0) - COALESCE(DB_QTY, 0), @P_DEC_QTY) < 0 THEN
																DELETE FROM pos_sys_tem_ar_in;
																RETURN CONCAT("You don't have sufficient quantity available to sell ", DB_QTY, " of the item ", COALESCE(VC_ASS_ITE, '')," in location ", VC_LOC, "\nQuantity Available: ", COALESCE(DB_QOH, 0), "\nInvalid item in line number: ", IN_LIN);
														END IF;
												END;
										END LOOP L1;
								CLOSE C1;
								DELETE FROM pos_sys_tem_ar_in;
								RETURN "";
						END;
				ELSE
						SET @Donothing = NULL;
				END CASE;
		ELSE
				SET @Donothing = NULL;
		END CASE;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_ITEM_TYPE`;
DELIMITER |
CREATE FUNCTION `AME_FUN_ITEM_TYPE`(P1 varchar(100)) RETURNS varchar(100) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE P_ID VARCHAR(100) DEFAULT '';
		DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS var_type FROM ame_mt_ite WHERE var_id = P1;
		OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_ID; RETURN P_ID; END IF; CLOSE C1;
		RETURN '';
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_NEW_BATCH_NUM`;
DELIMITER |
CREATE FUNCTION `AME_FUN_NEW_BATCH_NUM`() RETURNS int(11)
    DETERMINISTIC
BEGIN
		DECLARE P_NEW_NUM INTEGER DEFAULT 0;
		SELECT COALESCE(MAX(int_batchno), 0) + 1 INTO P_NEW_NUM FROM ame_sl_ic_batch;
		RETURN P_NEW_NUM;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_NEW_JE_NUM`;
DELIMITER |
CREATE FUNCTION `AME_FUN_NEW_JE_NUM`() RETURNS int(11)
    DETERMINISTIC
BEGIN
DECLARE P_NEW_NUM INTEGER DEFAULT 0;
SELECT COALESCE(MAX(int_jid), 0) + 1 INTO P_NEW_NUM FROM ame_gl_je;
RETURN P_NEW_NUM;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_NEW_SL_AP_HIS_NUM`;
DELIMITER |
CREATE FUNCTION `AME_FUN_NEW_SL_AP_HIS_NUM`() RETURNS int(11)
    DETERMINISTIC
BEGIN
DECLARE P_NEW_NUM INTEGER DEFAULT 0;
SELECT COALESCE(MAX(int_line), 0) + 1 INTO P_NEW_NUM FROM ame_sl_ap_his;
RETURN P_NEW_NUM;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_NEW_SL_AR_HIS_NUM`;
DELIMITER |
CREATE FUNCTION `AME_FUN_NEW_SL_AR_HIS_NUM`() RETURNS int(11)
    DETERMINISTIC
BEGIN
DECLARE P_NEW_NUM INTEGER DEFAULT 0;
SELECT COALESCE(MAX(int_line), 0) + 1 INTO P_NEW_NUM FROM ame_sl_ar_his;
RETURN P_NEW_NUM;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_NEW_SL_IC_HIS_NUM`;
DELIMITER |
CREATE FUNCTION `AME_FUN_NEW_SL_IC_HIS_NUM`() RETURNS int(11)
    DETERMINISTIC
BEGIN
DECLARE P_NEW_NUM INTEGER DEFAULT 0;
SELECT COALESCE(MAX(int_id), 0) + 1 INTO P_NEW_NUM FROM ame_sl_ic_his;
RETURN P_NEW_NUM;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_POST_ONE_TIME`;
DELIMITER |
CREATE FUNCTION `AME_FUN_POST_ONE_TIME`() RETURNS int(11)
    DETERMINISTIC
BEGIN
		DECLARE C1 CURSOR FOR SELECT T1.var_id FROM ame_sys_config T1 WHERE T1.var_id = "POST_ONE_TIME" AND T1.var_name = "TRUE";
		OPEN C1; IF FOUND_ROWS() > 0 THEN RETURN 1; END IF; CLOSE C1;
		RETURN 0;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_PREFIX`;
DELIMITER |
CREATE FUNCTION `AME_FUN_PREFIX`(P1 varchar(100), P2 tinyint(1)) RETURNS varchar(100) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE P_FORMAT VARCHAR(100) DEFAULT "";
		DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS CONCAT(var_id, LPAD(int_lastid, int_length, '0')) AS 'Format' FROM ame_sys_prefix WHERE var_modid = P1;
		OPEN C1;
				IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_FORMAT;
						IF P2 = 1 THEN UPDATE ame_sys_prefix SET int_lastid = COALESCE(int_lastid, 0) + 1 WHERE var_modid = P1; END IF;
				END IF;
				RETURN P_FORMAT;
		CLOSE C1;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_PREFIX_CUSTOM`;
DELIMITER |
CREATE FUNCTION `AME_FUN_PREFIX_CUSTOM`(P1 varchar(100), P2 varchar(100), P3 tinyint(1)) RETURNS varchar(100) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE P_FORMAT VARCHAR(100) DEFAULT "";
		DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS CONCAT(var_id, LPAD(int_lastid, int_length, '0')) AS 'Format' FROM ame_sys_prefix_custom WHERE var_modid = P1 AND var_type = P2;
		OPEN C1;
		IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_FORMAT;
		IF P3 = 1 THEN UPDATE ame_sys_prefix_custom SET int_lastid = COALESCE(int_lastid, 0) + 1 WHERE var_modid = P1 AND var_type = P2; END IF;
		END IF;
		RETURN P_FORMAT;
		CLOSE C1;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_PREFIX_CUSTOM_DEF`;
DELIMITER |
CREATE FUNCTION `AME_FUN_PREFIX_CUSTOM_DEF`(P1 varchar(100), P2 varchar(100)) RETURNS varchar(100) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
		DECLARE P_ID VARCHAR(100) DEFAULT "";
		DECLARE C1 CURSOR FOR SELECT T1.var_modid FROM ame_user_pro_def_pre T1 WHERE T1.var_id = P1 AND T1.var_type = P2 LIMIT 1;
		OPEN C1;
				IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_ID; END IF;
		CLOSE C1;
		RETURN P_ID;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_PREFIX_ITE`;
DELIMITER |
CREATE FUNCTION `AME_FUN_PREFIX_ITE`(P1 varchar(100)) RETURNS varchar(100) CHARSET utf8
    DETERMINISTIC
BEGIN
DECLARE P_LEN INTEGER DEFAULT 0;
DECLARE P_NUM INTEGER DEFAULT 0;
DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS COALESCE(int_length, 5) FROM ame_sys_prefix WHERE var_modid = 'MT-ITE';
OPEN C1;
IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_LEN;
SELECT CONVERT(COALESCE(MAX(SUBSTRING_INDEX(var_id, '-', -1)), 0), SIGNED) INTO P_NUM FROM ame_mt_ite WHERE LEFT(var_id, LENGTH(var_id) - LOCATE('-', REVERSE(var_id))) = P1;
END IF;
RETURN CONCAT(P1, "-", LPAD(P_NUM + 1, P_LEN, "0"));
CLOSE C1;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_ROUND`;
DELIMITER |
CREATE FUNCTION `AME_FUN_ROUND`(P1 double, P2 integer) RETURNS double
    DETERMINISTIC
BEGIN
		CASE P2
		WHEN 1 THEN
				RETURN CAST(P1 AS DECIMAL(16, 1));
		WHEN 2 THEN
				RETURN CAST(P1 AS DECIMAL(16, 2));
		WHEN 3 THEN
				RETURN CAST(P1 AS DECIMAL(16, 3));
		WHEN 4 THEN
				RETURN CAST(P1 AS DECIMAL(16, 4));
		WHEN 5 THEN
				RETURN CAST(P1 AS DECIMAL(16, 5));
		WHEN 6 THEN
				RETURN CAST(P1 AS DECIMAL(16, 6));
		WHEN 7 THEN
				RETURN CAST(P1 AS DECIMAL(16, 7));
		WHEN 8 THEN
				RETURN CAST(P1 AS DECIMAL(16, 8));
		WHEN 9 THEN
				RETURN CAST(P1 AS DECIMAL(16, 9));
		WHEN 10 THEN
				RETURN CAST(P1 AS DECIMAL(16, 10));
		WHEN 11 THEN
				RETURN CAST(P1 AS DECIMAL(16, 11));
		WHEN 12 THEN
				RETURN CAST(P1 AS DECIMAL(16, 12));
		ELSE
				RETURN CAST(P1 AS DECIMAL(16, 13));
		END CASE;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_SPLIT_STRING`;
DELIMITER |
CREATE FUNCTION `AME_FUN_SPLIT_STRING`(x VARCHAR(255),delim VARCHAR(12),pos INT) RETURNS varchar(2000) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
		IF ISNULL(x) THEN
		RETURN "";
		ELSE
		RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),delim,"");
		END IF;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_SYS_USED`;
DELIMITER |
CREATE FUNCTION `AME_FUN_SYS_USED`(P1 varchar(100), P2 varchar(2000)) RETURNS tinyint(1)
    DETERMINISTIC
BEGIN
		CASE P1
		WHEN 'MT_VEN' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_ap_po T1 WHERE T1.var_venid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_in T1 WHERE T1.var_venid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_dn T1 WHERE T1.var_venid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_py T1 WHERE T1.var_venid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_CUS' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_ar_quo T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_so T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_in T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_cn T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_py T1 WHERE T1.var_cusid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_EMP' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_ar_quo T1 WHERE T1.var_empid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_so T1 WHERE T1.var_empid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_in T1 WHERE T1.var_empid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_cn T1 WHERE T1.var_empid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_ITE_GRO' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_mt_ite T1 WHERE T1.var_groupid = P2 LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_CUS_GRO' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_mt_cus T1 WHERE T1.var_groupid = P2 LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_VEN_GRO' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_mt_ven T1 WHERE T1.var_groupid = P2 LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_EMP_GRO' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_mt_emp T1 WHERE T1.var_groupid = P2 LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_POS' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_mt_emp T1 WHERE T1.var_posid = P2 LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_PMT_TERM' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_mt_ven T1 WHERE T1.var_term = P2 LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_mt_cus T1 WHERE T1.var_term = P2 LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_mt_emp T1 WHERE T1.var_term = P2 LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_po T1 WHERE T1.var_term = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_in T1 WHERE T1.var_term = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_dn T1 WHERE T1.var_term = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_quo T1 WHERE T1.var_term = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_so T1 WHERE T1.var_term = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_in T1 WHERE T1.var_term = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_cn T1 WHERE T1.var_term = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_PMT_METH' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_mt_ven T1 WHERE T1.var_pmttype = P2 LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_mt_cus T1 WHERE T1.var_pmttype = P2 LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_mt_emp T1 WHERE T1.var_pmttype = P2 LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_py T1 WHERE T1.var_pmttype = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_py T1 WHERE T1.var_pmttype = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_refund T1 WHERE T1.var_pmttype = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_PRI_COD' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_mt_cus T1 WHERE T1.var_pricecode = P2 LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_quo T1 WHERE T1.var_pricecode = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_so T1 WHERE T1.var_pricecode = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_in T1 WHERE T1.var_pricecode = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_cn T1 WHERE T1.var_pricecode = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_ITE' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_ap_po T1 RIGHT JOIN ame_ap_po_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_in T1 RIGHT JOIN ame_ap_in_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_dn T1 RIGHT JOIN ame_ap_dn_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_quo T1 RIGHT JOIN ame_ar_quo_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_so T1 RIGHT JOIN ame_ar_so_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_in T1 RIGHT JOIN ame_ar_in_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_cn T1 RIGHT JOIN ame_ar_cn_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_tf T1 RIGHT JOIN ame_ic_tf_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_iu T1 RIGHT JOIN ame_ic_tf_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_ia T1 RIGHT JOIN ame_ic_ia_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_ip T1 RIGHT JOIN ame_ic_ip_det T2 ON T1.var_id = T2.var_id WHERE T2.var_itemid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN "MT_ACC" THEN
				IF EXISTS(SELECT T1.var_id FROM ame_ap_po T1 RIGHT JOIN ame_ap_po_det T2 ON T1.var_id = T2.var_id WHERE T2.var_accid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_in T1 RIGHT JOIN ame_ap_in_det T2 ON T1.var_id = T2.var_id WHERE T2.var_accid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_py T1 RIGHT JOIN ame_ap_py_det T2 ON T1.var_id = T2.var_id WHERE (T1.var_accid = P2 OR T2.var_docid = P2) AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_in T1 RIGHT JOIN ame_ar_in_det T2 ON T1.var_id = T2.var_id WHERE T2.var_accid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_py T1 RIGHT JOIN ame_ar_py_det T2 ON T1.var_id = T2.var_id WHERE(T1.var_accid = P2 OR T2.var_docid = P2) AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_iu T1 RIGHT JOIN ame_ic_iu_det T2 ON T1.var_id = T2.var_id WHERE T2.var_accid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.int_jid FROM ame_gl_je T1 RIGHT JOIN ame_gl_je_det T2 ON T1.int_jid = T2.int_jid WHERE T2.var_accid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN "MT_CLA" THEN
				IF EXISTS(SELECT T1.var_id FROM ame_ap_po T1 RIGHT JOIN ame_ap_po_det T2 ON T1.var_id = T2.var_id WHERE (T1.var_claid = P2 OR T2.var_claid = P2) AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_in T1 RIGHT JOIN ame_ap_in_det T2 ON T1.var_id = T2.var_id WHERE (T1.var_claid = P2 OR T2.var_claid = P2) AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_py T1 RIGHT JOIN ame_ap_py_det T2 ON T1.var_id = T2.var_id WHERE (T1.var_claid = P2 OR T2.var_claid = P2) AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_in T1 RIGHT JOIN ame_ar_in_det T2 ON T1.var_id = T2.var_id WHERE (T1.var_claid = P2 OR T2.var_claid = P2) AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_py T1 RIGHT JOIN ame_ar_py_det T2 ON T1.var_id = T2.var_id WHERE (T1.var_claid = P2 OR T2.var_claid = P2) AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_iu T1 RIGHT JOIN ame_ic_iu_det T2 ON T1.var_id = T2.var_id WHERE (T1.var_claid = P2 OR T2.var_claid = P2) AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_tf T1 RIGHT JOIN ame_ic_tf_det T2 ON T1.var_id = T2.var_id WHERE (T1.var_claid = P2 OR T2.var_claid = P2) AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_ia T1 RIGHT JOIN ame_ic_ia_det T2 ON T1.var_id = T2.var_id WHERE (T1.var_claid = P2 OR T2.var_claid = P2) AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_ip T1 RIGHT JOIN ame_ic_ip_det T2 ON T1.var_id = T2.var_id WHERE (T1.var_claid = P2 OR T2.var_claid = P2) AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.int_jid FROM ame_gl_je T1 RIGHT JOIN ame_gl_je_det T2 ON T1.int_jid = T2.int_jid WHERE T2.var_claid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_LOC' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_ap_po T1 RIGHT JOIN ame_ap_po_det T2 ON T1.var_id = T2.var_id WHERE T2.var_locid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_in T1 RIGHT JOIN ame_ap_in_det T2 ON T1.var_id = T2.var_id WHERE T2.var_locid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_dn T1 RIGHT JOIN ame_ap_dn_det T2 ON T1.var_id = T2.var_id WHERE T2.var_locid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_quo T1 RIGHT JOIN ame_ar_quo_det T2 ON T1.var_id = T2.var_id WHERE T2.var_locid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_so T1 RIGHT JOIN ame_ar_so_det T2 ON T1.var_id = T2.var_id WHERE T2.var_locid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_in T1 RIGHT JOIN ame_ar_in_det T2 ON T1.var_id = T2.var_id WHERE T2.var_locid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_cn T1 RIGHT JOIN ame_ar_cn_det T2 ON T1.var_id = T2.var_id WHERE T2.var_locid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_tf T1 RIGHT JOIN ame_ic_tf_det T2 ON T1.var_id = T2.var_id WHERE (T2.var_locid = P2 or T2.var_tolocid) AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_iu T1 RIGHT JOIN ame_ic_tf_det T2 ON T1.var_id = T2.var_id WHERE T2.var_locid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_ia T1 RIGHT JOIN ame_ic_ia_det T2 ON T1.var_id = T2.var_id WHERE T2.var_locid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_ip T1 RIGHT JOIN ame_ic_ip_det T2 ON T1.var_id = T2.var_id WHERE T2.var_locid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_UOM' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_mt_ite T1 WHERE T1.var_uomid = P2 LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_mt_ite_pri T1 WHERE T1.var_uomid = P2 LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_mt_ite_uom T1 WHERE T1.var_uomid = P2 LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_mt_ite_ass T1 WHERE (T1.var_uomid = P2 OR T1.var_uomid_ass) LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_po T1 RIGHT JOIN ame_ap_po_det T2 ON T1.var_id = T2.var_id WHERE T2.var_uomid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_in T1 RIGHT JOIN ame_ap_in_det T2 ON T1.var_id = T2.var_id WHERE T2.var_uomid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_dn T1 RIGHT JOIN ame_ap_dn_det T2 ON T1.var_id = T2.var_id WHERE T2.var_uomid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_quo T1 RIGHT JOIN ame_ar_quo_det T2 ON T1.var_id = T2.var_id WHERE T2.var_uomid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_so T1 RIGHT JOIN ame_ar_so_det T2 ON T1.var_id = T2.var_id WHERE T2.var_uomid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_in T1 RIGHT JOIN ame_ar_in_det T2 ON T1.var_id = T2.var_id WHERE T2.var_uomid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_cn T1 RIGHT JOIN ame_ar_cn_det T2 ON T1.var_id = T2.var_id WHERE T2.var_uomid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_tf T1 RIGHT JOIN ame_ic_tf_det T2 ON T1.var_id = T2.var_id WHERE (T2.var_uomid = P2) AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_iu T1 RIGHT JOIN ame_ic_tf_det T2 ON T1.var_id = T2.var_id WHERE T2.var_uomid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_ia T1 RIGHT JOIN ame_ic_ia_det T2 ON T1.var_id = T2.var_id WHERE T2.var_uomid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_ip T1 RIGHT JOIN ame_ic_ip_det T2 ON T1.var_id = T2.var_id WHERE T2.var_uomid = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		WHEN 'MT_CUR' THEN
				IF EXISTS(SELECT T1.var_id FROM ame_mt_ven T1 WHERE T1.var_cur = P2 LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_mt_cus T1 WHERE T1.var_cur = P2 LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_mt_emp T1 WHERE T1.var_cur = P2 LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_po T1 WHERE T1.var_cur = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_in T1 WHERE T1.var_cur = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ap_dn T1 WHERE T1.var_cur = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_quo T1 WHERE T1.var_cur = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_so T1 WHERE T1.var_cur = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_in T1 WHERE T1.var_cur = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ar_cn T1 WHERE T1.var_cur = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_tf T1 WHERE T1.var_cur = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_iu T1 WHERE T1.var_cur = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_ia T1 WHERE T1.var_cur = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSEIF EXISTS(SELECT T1.var_id FROM ame_ic_ip T1 WHERE T1.var_cur = P2 AND T1.var_status <> 'Deleted' LIMIT 1) THEN
						RETURN 1;
				ELSE
						RETURN 0;
				END IF;
		ELSE
				RETURN 0;
		END CASE;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_TOTAL_JE_DIFF`;
DELIMITER |
CREATE FUNCTION `AME_FUN_TOTAL_JE_DIFF`(P1 integer) RETURNS double
    DETERMINISTIC
BEGIN
		DECLARE P_NEW_NUM DOUBLE DEFAULT 0.0;
		SELECT COALESCE(SUM(dou_funamt), 0) INTO P_NEW_NUM FROM ame_gl_je_det WHERE int_jid = P1;
		RETURN P_NEW_NUM;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_TOTAL_JE_DIFF_FOR`;
DELIMITER |
CREATE FUNCTION `AME_FUN_TOTAL_JE_DIFF_FOR`(P1 integer) RETURNS double
    DETERMINISTIC
BEGIN
		DECLARE P_NEW_NUM DOUBLE DEFAULT 0.0;
		SELECT COALESCE(SUM(dou_amt), 0) INTO P_NEW_NUM FROM ame_gl_je_det WHERE int_jid = P1;
		RETURN P_NEW_NUM;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_USER_PER`;
DELIMITER |
CREATE FUNCTION `AME_FUN_USER_PER`(P1 varchar(100), P2 varchar(100), P3 varchar(100)) RETURNS tinyint(1)
    DETERMINISTIC
BEGIN
		DECLARE P_ID VARCHAR(100) DEFAULT '';
		DECLARE C1 CURSOR FOR SELECT  var_type FROM ame_mt_ite WHERE var_id = P1;
		OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_ID; RETURN P_ID; END IF; CLOSE C1;
		RETURN '';
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_VALID_GL_JE`;
DELIMITER |
CREATE FUNCTION `AME_FUN_VALID_GL_JE`(P1 varchar(100)) RETURNS tinyint(1)
    DETERMINISTIC
BEGIN
		DECLARE P_CUR VARCHAR(100) DEFAULT "";
		DECLARE P_SUM DOUBLE DEFAULT 0.0;
		DECLARE P_LINE_ACC INTEGER DEFAULT 0;
		DECLARE P_LINE_GL INTEGER DEFAULT 0;
		DECLARE P_DEC_AMT_FOR INTEGER DEFAULT 2;
		BEGIN
				DECLARE C1 CURSOR FOR SELECT T1.var_cur FROM ame_gl_je T1 WHERE T1.int_jid = P1;
				OPEN C1;
						IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_CUR;
								BEGIN DECLARE C2 CURSOR FOR SELECT T1.int_amt FROM ame_sys_decimal T1 WHERE T1.var_id = P_CUR; OPEN C2; IF FOUND_ROWS() > 0 THEN FETCH C2 INTO P_DEC_AMT_FOR; END IF;CLOSE C2;END;
								SELECT COUNT(T1.var_accid) INTO P_LINE_ACC FROM ame_gl_je_det T1 WHERE T1.int_jid = P1 AND T1.var_accid IN(SELECT T2.var_id FROM ame_mt_acc T2 WHERE COALESCE(T2.tin_inactive, 0) = 0);
								SELECT COUNT(T1.var_accid), AME_FUN_ROUND(SUM(T1.dou_amt), P_DEC_AMT_FOR) INTO P_LINE_GL, P_SUM FROM ame_gl_je_det T1 WHERE T1.int_jid = P1;
								IF (P_LINE_ACC <> P_LINE_GL) THEN RETURN 1; END IF;
								IF (P_SUM <> 0) THEN RETURN 2; END IF;
								RETURN 0;
						ELSE
								RETURN 3;
						END IF;
				CLOSE C1;
		END;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `AME_FUN_VEN_BATCH`;
DELIMITER |
CREATE FUNCTION `AME_FUN_VEN_BATCH`(P1 int(11), P2 varchar(100), P3 varchar(100)) RETURNS varchar(100) CHARSET utf8
    DETERMINISTIC
BEGIN
DECLARE P_ID VARCHAR(100) DEFAULT '';
DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS T2.var_venid FROM ame_ap_in_det T1 LEFT JOIN ame_ap_in T2 ON T1.var_id = T2.var_id WHERE T1.int_batchno = P1 AND T1.var_locid = P2 AND T1.var_itemid = P3 LIMIT 1;
OPEN C1; IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_ID; RETURN P_ID; END IF; CLOSE C1;
RETURN '';
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `ame_get_first_last_day`;
DELIMITER |
CREATE FUNCTION `ame_get_first_last_day`(var_peroid varchar(255), var_fl varchar(10)) RETURNS varchar(50) CHARSET utf8
    DETERMINISTIC
BEGIN
	DECLARE var_date VARCHAR(50) DEFAULT "";
	IF var_fl = 'FirstDay' THEN
		IF var_peroid = 'thisWeek' THEN
			SET var_date = (SELECT SUBDATE(DATE(NOW()), WEEKDAY(NOW())));
		ELSEIF var_peroid = 'thisMonth' THEN
			SET var_date = (SELECT DATE_FORMAT(NOW() ,'%Y-%m-01'));
		ELSEIF var_peroid = 'thisQuarter' THEN
			SET var_date = (SELECT MAKEDATE(YEAR(CURDATE()), 1) + INTERVAL QUARTER(CURDATE()) QUARTER - INTERVAL    1 QUARTER);
		ELSEIF var_peroid = 'thisYear' THEN
			SET var_date = (SELECT DATE_FORMAT(NOW() ,'%Y-01-01'));
		ELSEIF var_peroid = 'lastWeek' THEN
			SET var_date = (SELECT (curdate() - INTERVAL((WEEKDAY(curdate()))+7) DAY));
		ELSEIF var_peroid = 'lastMonth' THEN
			SET var_date = DATE(DATE_ADD(DATE_SUB(NOW(),INTERVAL DAYOFMONTH(NOW())-1 DAY),INTERVAL -1 MONTH));
		ELSEIF var_peroid = 'lastQuarter' THEN
			SET var_date = (SELECT MAKEDATE(YEAR(CURDATE()), 1) + INTERVAL QUARTER(CURDATE())-2 QUARTER);
		ELSEIF var_peroid = 'lastYear' THEN
			SET var_date = (SELECT MAKEDATE(YEAR(CURDATE())-1,1));
		ELSEIF var_peroid = 'today' THEN
			SET var_date = (SELECT DATE(NOW()));
		END IF;	
	ELSE
		IF var_peroid = 'thisWeek' THEN
			SET var_date = (SELECT DATE(NOW() + INTERVAL (6 - WEEKDAY(NOW())) DAY));
		ELSEIF var_peroid = 'thisMonth' THEN
			SET var_date = (SELECT DATE(LAST_DAY(NOW())));
		ELSEIF var_peroid = 'thisQuarter' THEN
			SET var_date = (SELECT MAKEDATE(YEAR(CURDATE()), 1) + INTERVAL QUARTER(CURDATE()) QUARTER - INTERVAL 1 DAY);
		ELSEIF var_peroid = 'thisYear' THEN
			SET var_date = (SELECT DATE_FORMAT(NOW(),'%Y-12-31'));
		ELSEIF var_peroid = 'lastWeek' THEN
			SET var_date = (SELECT (curdate() - INTERVAL((WEEKDAY(curdate()))+1) DAY));
		ELSEIF var_peroid = 'lastMonth' THEN
			SET var_date = (SELECT LAST_DAY(now() - interval 1 month ));
		ELSEIF var_peroid = 'lastQuarter' THEN
			SET var_date = (SELECT MAKEDATE(YEAR(CURDATE()), 1) + INTERVAL QUARTER(CURDATE())-1 QUARTER - INTERVAL 1 DAY);
		ELSEIF var_peroid = 'lastYear' THEN
			SET var_date = (SELECT CONCAT((YEAR(NOW())-1),'-12-31'));
		ELSEIF var_peroid = 'today' THEN
			SET var_date = (SELECT DATE(NOW()));
		END IF;
	END IF;
	RETURN var_date;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `ATS_FUN_ameAuthDescApproval`;
DELIMITER |
CREATE FUNCTION `ATS_FUN_ameAuthDescApproval`(ProName VARCHAR(255), TranID VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
DECLARE countCustApp  INT DEFAULT 0;
DECLARE countCustPad  INT DEFAULT 0;
DECLARE tranName  VARCHAR(255) DEFAULT "";
DECLARE des VARCHAR(255) DEFAULT "" ;
DECLARE gr VARCHAR(200) DEFAULT "";

IF ProName = "Customer" THEN
	SELECT COUNT(crm_customer.var_id) INTO countCustApp FROM crm_customer WHERE crm_customer.var_id = TranID LIMIT 0,1;
IF countCustApp <> 0 THEN
	SELECT crm_customer.var_name INTO des  FROM crm_customer WHERE crm_customer.var_id = TranID LIMIT 0,1;
ELSE
	SELECT ame_mt_cus.var_name INTO des FROM ame_mt_cus WHERE ame_mt_cus.var_id = TranID LIMIT 0,1;
END IF;
ELSEIF ProName = "Over Due Date" THEN
	SELECT ame_mt_tem_odd.var_id INTO tranName FROM ame_mt_tem_odd WHERE ame_mt_tem_odd.var_id = TranID;
	SELECT CONCAT("[",ame_mt_cus.var_id,"] ", ame_mt_cus.var_name) INTO des FROM ame_mt_cus WHERE ame_mt_cus.var_id = tranName;
ELSEIF ProName = "Temporary Credit" THEN
	SELECT ame_mt_tem_tc.var_id INTO tranName FROM ame_mt_tem_tc WHERE ame_mt_tem_tc.var_id = TranID;
	SELECT CONCAT("[",ame_mt_cus.var_name,"] ", ame_mt_cus.var_name) INTO des FROM ame_mt_cus WHERE ame_mt_cus.var_id = tranName;
ELSEIF ProName = "General Requisition" THEN
	SELECT crm_frm_general.ReqType INTO gr FROM crm_frm_general WHERE crm_frm_general.FrmID = TranID;
	SELECT crm_template_authorization.tmpname INTO des FROM crm_template_authorization WHERE crm_template_authorization.tmpid = gr;

ELSE
	SET des = "";
END IF;
RETURN des;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `ATS_FUN_ameAuthGetCustomerName`;
DELIMITER |
CREATE FUNCTION `ATS_FUN_ameAuthGetCustomerName`(ProName VARCHAR(255), TranID VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN

	DECLARE tranName  VARCHAR(255) DEFAULT "";
	DECLARE des VARCHAR(255) DEFAULT "" ;

	IF ProName = "General Requisition" THEN
		SELECT crm_frm_general.CustID INTO tranName FROM crm_frm_general WHERE crm_frm_general.frmid = TranID;
		IF tranName <> '' THEN
			SELECT CONCAT("[",ame_mt_cus.var_id,"] ", ame_mt_cus.var_name) INTO des FROM ame_mt_cus WHERE ame_mt_cus.var_id = tranName;
		ELSE
				SET des = "";
		 END IF;
	ELSEIF ProName = "Budget Requisition" THEN
		SELECT crm_frm_budget.CustID INTO tranName FROM crm_frm_budget WHERE crm_frm_budget.frmid = TranID;
		 IF tranName <> '' THEN
				SELECT CONCAT("[",ame_mt_cus.var_id,"] ", ame_mt_cus.var_name) INTO des FROM ame_mt_cus WHERE ame_mt_cus.var_id = tranName;
		 ELSE
				SET des = "";
		 END IF;
	ELSEIF ProName = "Temporary Credit" THEN
		SELECT ame_mt_tem_tc.var_cusid INTO tranName FROM ame_mt_tem_tc WHERE ame_mt_tem_tc.var_id = TranID;
		 IF tranName <> '' THEN
				SELECT CONCAT("[",ame_mt_cus.var_id,"] ", ame_mt_cus.var_name) INTO des FROM ame_mt_cus WHERE ame_mt_cus.var_id = tranName;
		 ELSE
				SET des = "";
		 END IF;
	ELSEIF ProName = "Over Due Date" THEN
		SELECT ame_mt_tem_odd.var_cusid INTO tranName FROM ame_mt_tem_odd WHERE ame_mt_tem_odd.var_id = TranID;
		 IF tranName <> '' THEN
				SELECT CONCAT("[",ame_mt_cus.var_id,"] ", ame_mt_cus.var_name) INTO des FROM ame_mt_cus WHERE ame_mt_cus.var_id = tranName;
		 ELSE
				SET des = "";
		 END IF;
	ELSE
		SET des = "";
	END IF;
RETURN des;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `ATS_FUN_CO`;
DELIMITER |
CREATE FUNCTION `ATS_FUN_CO`(textString varchar(300)) RETURNS varchar(300) CHARSET utf8
    DETERMINISTIC
BEGIN
	RETURN COALESCE(textString,'');
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `ATS_FUN_generateAutoId`;
DELIMITER |
CREATE FUNCTION `ATS_FUN_generateAutoId`(`module_id` varchar(11)) RETURNS varchar(13) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
	DECLARE Len INTEGER DEFAULT 0;
	DECLARE LastID INTEGER DEFAULT 0;
	DECLARE NumberPre VARCHAR(255) DEFAULT "";
	DECLARE NumOfZero VARCHAR(255) DEFAULT "";
	DECLARE LenID INTEGER DEFAULT 0;
	SELECT Length,ats_numberprefix.LastID,ats_numberprefix.NumberPrefix INTO Len,LastID,NumberPre from ats_numberprefix WHERE ats_numberprefix.ModuleID = module_id;
  SET LenID = LENGTH(LastID); -- + LENGTH(NumberPre);
	if  LenID < len THEN
		while LenID < len do
			SET LenID = LenID + 1;
			SET NumOfZero = CONCAT(NumOfZero,"0");
		end while;
		UPDATE ats_numberprefix SET ats_numberprefix.LastID = ats_numberprefix.LastID +1 WHERE ats_numberprefix.ModuleID = module_id;
		RETURN CONCAT(NumberPre,NumOfZero,LastID);
	ELSE
		UPDATE ats_numberprefix SET ats_numberprefix.LastID = ats_numberprefix.LastID +1 WHERE ats_numberprefix.ModuleID = module_id;
		RETURN CONCAT(NumberPre,Lastid);
	END IF;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `ATS_FUN_GetAuthStatus`;
DELIMITER |
CREATE FUNCTION `ATS_FUN_GetAuthStatus`(ID varchar(200), proName varchar(200),SAuthID varchar(200)) RETURNS varchar(200) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE stat VARCHAR(200);
		SELECT  app.Auth_Status as "authStatus"	INTO 	stat		
			FROM hrm_authorisation_detail_temp a
			LEFT JOIN hrm_authorisation_approved app ON (app.Auth_ID = SAuthID AND app.Auth_ProcessID = ID AND app.Auth_ProcessName = proName AND a.Auth_EmpID = app.Auth_EmpID AND a.Auth_GroupID = app.Auth_GroupID AND a.Auth_TranID = app.Auth_ProcessID);
		RETURN stat;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `ATS_FUN_UEmp`;
DELIMITER |
CREATE FUNCTION `ATS_FUN_UEmp`(uid varchar(200)) RETURNS varchar(200) CHARSET utf8
    DETERMINISTIC
BEGIN
	DECLARE empid VARCHAR(200);
	SELECT ame_mt_emp.var_id INTO empid FROM ame_mt_emp WHERE ame_mt_emp.var_id = (SELECT var_empid FROM ame_user WHERE var_id = uid);
	SET empid = COALESCE(empid,'');
	RETURN empid;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `ats_generateAutoId`;
DELIMITER |
CREATE FUNCTION `ats_generateAutoId`(`module_id` varchar(11)) RETURNS varchar(13) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
	DECLARE Len INTEGER DEFAULT 0;
	DECLARE LastID INTEGER DEFAULT 0;
	DECLARE NumberPre VARCHAR(255) DEFAULT "";
	DECLARE NumOfZero VARCHAR(255) DEFAULT "";
	DECLARE LenID INTEGER DEFAULT 0;
	SELECT Length,crm_numberprefix.LastID,crm_numberprefix.NumberPrefix INTO Len,LastID,NumberPre from crm_numberprefix WHERE crm_numberprefix.ModuleID = module_id;
  SET LenID = LENGTH(LastID); -- + LENGTH(NumberPre);
	if  LenID < len THEN
		while LenID < len do
			SET LenID = LenID + 1;
			SET NumOfZero = CONCAT(NumOfZero,"0");
		end while;
		UPDATE crm_numberprefix SET crm_numberprefix.LastID = crm_numberprefix.LastID +1 WHERE crm_numberprefix.ModuleID = module_id;
		RETURN CONCAT(NumberPre,NumOfZero,LastID);
	ELSE
		UPDATE crm_numberprefix SET crm_numberprefix.LastID = crm_numberprefix.LastID +1 WHERE crm_numberprefix.ModuleID = module_id;
		RETURN CONCAT(NumberPre,Lastid);
	END IF;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `crmFnCheckExistID`;
DELIMITER |
CREATE FUNCTION `crmFnCheckExistID`(modid varchar(100)) RETURNS varchar(255) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
	DECLARE custId VARCHAR(255) DEFAULT "";
	DECLARE found INT DEFAULT 0;
	SET custId = CRM_FUN_PREFIX_LAST_ID(modid);
	WHILE found = 0 DO
		IF (exists(SELECT * FROM ame_mt_cus WHERE ame_mt_cus.var_id = custId)) THEN
			SET custId = CRM_FUN_PREFIX_LAST_ID(modid);
		ELSE 
			SET found = 1;
		END IF;
	END WHILE;
	RETURN custId;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `crmGazetteerAutos`;
DELIMITER |
CREATE FUNCTION `crmGazetteerAutos`() RETURNS int(11)
    READS SQL DATA
    DETERMINISTIC
BEGIN

	DECLARE ids INT(11);
	SELECT var_auto INTO ids FROM `crm_gazetteer` WHERE var_auto=(SELECT MAX(var_auto) FROM `crm_gazetteer`);
	SET ids = ids +1;
	RETURN ids;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `crmGenerateEmail`;
DELIMITER |
CREATE FUNCTION `crmGenerateEmail`(var_code varchar(255), var_obj varchar(255),var_tranc varchar(255),var_activity varchar(255),var_auth varchar(255),var_user varchar(255)) RETURNS varchar(4000) CHARSET utf8
    DETERMINISTIC
BEGIN
DECLARE tmp_msg VARCHAR(4000) DEFAULT '';
IF var_code IN ('1045','1046','1047') THEN
SELECT REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_tranc) INTO tmp_msg FROM crm_message WHERE Code = var_code;
ELSEIF var_code = '1048' THEN
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',var_tranc),'#Param4',(SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))),'#Param5', DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p')),'#Param6',var_activity),'#Param7',var_auth) INTO tmp_msg FROM crm_message WHERE Code = var_code;
ELSEIF var_code IN ('1049','1050','1051') THEN
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',var_tranc),'#Param4',(SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))),'#Param5',DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p'))  INTO tmp_msg FROM crm_message WHERE Code = var_code;
ELSEIF var_code IN ('1052','1057','1058') THEN
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',var_tranc),'#Param4',(SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))),'#Param5', DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p')),'#Param6', IF(var_obj = "General Requisition", (SELECT TmpName FROM crm_template_authorization WHERE TmpID = (SELECT ReqType FROM crm_frm_general WHERE FrmID = var_tranc)) ,'')) INTO tmp_msg FROM crm_message WHERE Code = var_code;
ELSEIF var_code = '1053' THEN
SELECT REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_tranc),'#Param3',var_auth) INTO tmp_msg FROM crm_message WHERE Code = var_code;
ELSEIF var_code = '1054' THEN
SELECT REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_tranc),'#Param3',var_activity),'#Param4',var_auth) INTO tmp_msg FROM crm_message WHERE Code = var_code;
ELSEIF var_code = '1055' THEN
IF (fnHRMCheckAuthorisationApprove(var_tranc, var_activity, var_auth) - fnHRMCheckAuthorisationApproved(var_tranc, var_activity, var_auth)) > 0 THEN
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',var_tranc),'#Param4',var_activity),'#Param5',DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p')),'#Param6',(fnHRMCheckAuthorisationApprove(var_tranc, var_activity, var_auth) - fnHRMCheckAuthorisationApproved(var_tranc, var_activity, var_auth))),'#Param7', (SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))) INTO tmp_msg FROM crm_message WHERE Code = var_code;
ELSE
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',var_tranc),'#Param4',var_activity),'#Param5',DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p')), '#Param6', (SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))) INTO tmp_msg FROM crm_message WHERE Code = '1060';
END IF;
ELSEIF var_code = '1056' THEN
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',var_tranc),'#Param4',var_activity),'#Param5',DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p')),'#Param7', (SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))) INTO tmp_msg FROM crm_message WHERE Code = var_code;
ELSEIF var_code = '1059' THEN
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',(SELECT CB_RToID FROM int_collaboration WHERE CB_ID = var_activity)),'#Param4',(SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))),'#Param5', DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p')),'#Param6',(SELECT CB_Des FROM int_collaboration WHERE CB_ID = var_activity)),'#Param7',(SELECT SUBSTRING_INDEX(CB_RToType,'_',1) FROM int_collaboration WHERE CB_ID = var_activity)),'#Param8',var_auth) INTO tmp_msg FROM crm_message WHERE Code = var_code;
END IF;
RETURN tmp_msg;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `crmGenerateMessage`;
DELIMITER |
CREATE FUNCTION `crmGenerateMessage`(`msgCode` varchar(50),`module` varchar(50),`moduleId` varchar(50)) RETURNS varchar(255) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
	DECLARE tmp_msg VARCHAR(4000) DEFAULT "";

	SELECT REPLACE(REPLACE(Msg,"#param1",module),"#param2",moduleId) INTO tmp_msg FROM crm_message WHERE `Code` = msgCode;
	
	RETURN tmp_msg;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `crm_fullname`;
DELIMITER |
CREATE FUNCTION `crm_fullname`(`var_obj_id` varchar(255), `var_obj_name` varchar(500)) RETURNS varchar(500) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
	DECLARE var_full varchar(750);
	SET var_full = '';
	IF var_obj_id = NULL OR var_obj_id = '' THEN
		SET var_full = var_obj_name;
	ELSE
		SET var_full = CONCAT('[',COALESCE(var_obj_id,''),'] ',COALESCE(var_obj_name,''));
	END IF;
	RETURN var_full;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `crm_fullname_adv`;
DELIMITER |
CREATE FUNCTION `crm_fullname_adv`(`var_obj_id` varchar(255), `var_obj_name` varchar(500),var_obj_code varchar(255)) RETURNS varchar(500) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
	DECLARE var_full varchar(750);
	SET var_full = '';
	IF var_obj_id = NULL OR var_obj_id = '' THEN
		SET var_full = '';
	ELSE
		SET var_full = CONCAT('[',COALESCE(var_obj_id,''),'] ',COALESCE(var_obj_name,''));
		IF var_obj_code <> '' THEN
			SET var_full = CONCAT('[',COALESCE(var_obj_id,''),']','[',COALESCE(var_obj_code,''),'] ',COALESCE(var_obj_name,''));
		END IF;
	END IF;
	RETURN var_full;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_actDeleteSaleOrder`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_actDeleteSaleOrder`(ID varchar(100)) RETURNS varchar(100) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
		DECLARE cou INT DEFAULT 0;
		DELETE FROM tblsaleorder WHERE SalID = ID;
		SELECT COUNT(SalID) INTO cou FROM tblsaleorder WHERE SalID = ID;
		IF cou > 0 THEN
				RETURN 'false';
		END IF;
		DELETE FROM tblsaleorderdetails WHERE SalID = ID;
		SELECT COUNT(SalID) INTO cou FROM tblsaleorderdetails WHERE SalID = ID;
		IF cou > 0 THEN
				RETURN 'false';
		END IF;
		RETURN "True";
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_actDeleteSaleOrderDetail`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_actDeleteSaleOrderDetail`(ID varchar(100)) RETURNS varchar(100) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
		DECLARE cou INT DEFAULT 0;
		DELETE FROM tblsaleorderdetails WHERE SalID = ID;
		SELECT COUNT(SalID) INTO cou FROM tblsaleorderdetails WHERE SalID = ID;
		IF cou > 0 THEN
				RETURN 'false';
		END IF;
		RETURN "True";
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_CHECK_EXIST_ID`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_CHECK_EXIST_ID`(modid varchar(100)) RETURNS varchar(255) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
	DECLARE custId VARCHAR(255) DEFAULT "";
	DECLARE found INT DEFAULT 0;
	SET custId = CRM_FUN_PREFIX_LAST_ID(modid);
	WHILE found = 0 DO
		IF (exists(SELECT * FROM ame_mt_cus WHERE ame_mt_cus.var_id = custId)) THEN
			SET custId = CRM_FUN_PREFIX_LAST_ID(modid);
		ELSE 
			SET found = 1;
		END IF;
	END WHILE;
	RETURN custId;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_CONFIG_AME_GET_LASTID`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_CONFIG_AME_GET_LASTID`(P1 varchar(100), P2 tinyint(1)) RETURNS varchar(150) CHARSET utf8
    DETERMINISTIC
BEGIN
	DECLARE P_FORMAT VARCHAR(100) DEFAULT "";
	DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS CONCAT(var_id, LPAD(int_lastid, int_length, '0')) AS 'Format' FROM ame_sys_prefix WHERE var_modid = P1;
	OPEN C1;
			IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_FORMAT;
					IF P2 = 1 THEN UPDATE ame_sys_prefix SET int_lastid = COALESCE(int_lastid, 0) + 1 WHERE var_modid = P1; END IF;
			END IF;
			RETURN P_FORMAT;
	CLOSE C1;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_CONFIG_GENERATE_ID`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_CONFIG_GENERATE_ID`(`module_id` varchar(11)) RETURNS varchar(13) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
	DECLARE Len INTEGER DEFAULT 0;
	DECLARE LastID INTEGER DEFAULT 0;
	DECLARE NumberPre VARCHAR(255) DEFAULT "";
	DECLARE NumOfZero VARCHAR(255) DEFAULT "";
	DECLARE LenID INTEGER DEFAULT 0;
	SELECT Length,crm_numberprefix.LastID,crm_numberprefix.NumberPrefix INTO Len,LastID,NumberPre from crm_numberprefix WHERE crm_numberprefix.ModuleID = module_id;
  SET LenID = LENGTH(LastID); -- + LENGTH(NumberPre);
	if  LenID < len THEN
		while LenID < len do
			SET LenID = LenID + 1;
			SET NumOfZero = CONCAT(NumOfZero,"0");
		end while;
		UPDATE crm_numberprefix SET crm_numberprefix.LastID = crm_numberprefix.LastID +1 WHERE crm_numberprefix.ModuleID = module_id;
		RETURN CONCAT(NumberPre,NumOfZero,LastID);
	ELSE
		UPDATE crm_numberprefix SET crm_numberprefix.LastID = crm_numberprefix.LastID +1 WHERE crm_numberprefix.ModuleID = module_id;
		RETURN CONCAT(NumberPre,Lastid);
	END IF;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_CONFIG_IMG_PATH`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_CONFIG_IMG_PATH`() RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
BEGIN
	RETURN COALESCE((SELECT var_name FROM ame_sys_config WHERE var_id = "IMG_PATH_WEB" LIMIT 1),'');
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_CUR_GET_RATE`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_CUR_GET_RATE`(var_cur_id varchar(150)) RETURNS double
    DETERMINISTIC
BEGIN
	RETURN COALESCE((SELECT dou_rate FROM ame_mt_cur_rate WHERE var_id = var_cur_id),1);
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_CUST_GET_CUR_BY_CID`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_CUST_GET_CUR_BY_CID`(var_custid varchar(100)) RETURNS varchar(100) CHARSET utf8
    DETERMINISTIC
BEGIN
	RETURN COALESCE((SELECT var_cur FROM ame_mt_cus WHERE var_id = var_custid LIMIT 1), '');
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_GENERATE_EMAIL`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_GENERATE_EMAIL`(var_code varchar(255), var_obj varchar(255),var_tranc varchar(255),var_activity varchar(255),var_auth varchar(255),var_user varchar(255)) RETURNS varchar(4000) CHARSET utf8
    DETERMINISTIC
BEGIN
	DECLARE tmp_msg VARCHAR(4000) DEFAULT '';
	IF var_code IN ('1045','1046','1047') THEN
		SELECT REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_tranc) INTO tmp_msg FROM crm_message WHERE Code = var_code;
	ELSEIF var_code = '1048' THEN
		SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',var_tranc),'#Param4',(SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))),'#Param5', DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p')),'#Param6',var_activity),'#Param7',var_auth) INTO tmp_msg FROM crm_message WHERE Code = var_code;
	ELSEIF var_code IN ('1049','1050','1051') THEN
		SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',var_tranc),'#Param4',(SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))),'#Param5',DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p'))  INTO tmp_msg FROM crm_message WHERE Code = var_code;
	ELSEIF var_code IN ('1052','1057','1058') THEN
		SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',var_tranc),'#Param4',(SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))),'#Param5', DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p')),'#Param6', IF(var_obj = "General Requisition", (SELECT TmpName FROM crm_template_authorization WHERE TmpID = (SELECT ReqType FROM crm_frm_general WHERE FrmID = var_tranc)) ,'')) INTO tmp_msg FROM crm_message WHERE Code = var_code;
	ELSEIF var_code = '1053' THEN
		SELECT REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_tranc),'#Param3',var_auth) INTO tmp_msg FROM crm_message WHERE Code = var_code;
	ELSEIF var_code = '1054' THEN
		SELECT REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_tranc),'#Param3',var_activity),'#Param4',var_auth) INTO tmp_msg FROM crm_message WHERE Code = var_code;
	ELSEIF var_code = '1055' THEN
		IF (fnHRMCheckAuthorisationApprove(var_tranc, var_activity, var_auth) - fnHRMCheckAuthorisationApproved(var_tranc, var_activity, var_auth)) > 0 THEN
			SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',var_tranc),'#Param4',var_activity),'#Param5',DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p')),'#Param6',(fnHRMCheckAuthorisationApprove(var_tranc, var_activity, var_auth) - fnHRMCheckAuthorisationApproved(var_tranc, var_activity, var_auth))),'#Param7', (SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))) INTO tmp_msg FROM crm_message WHERE Code = var_code;
		ELSE
			SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',var_tranc),'#Param4',var_activity),'#Param5',DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p')), '#Param6', (SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))) INTO tmp_msg FROM crm_message WHERE Code = '1060';
		END IF;
	ELSEIF var_code = '1056' THEN
		SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',var_tranc),'#Param4',var_activity),'#Param5',DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p')),'#Param7', (SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))) INTO tmp_msg FROM crm_message WHERE Code = var_code;
	ELSEIF var_code = '1059' THEN
		SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Msg,'#Param1',(SELECT Unit FROM tblcompanyreport)),'#Param2',var_obj),'#Param3',(SELECT CB_RToID FROM int_collaboration WHERE CB_ID = var_activity)),'#Param4',(SELECT EmpName FROM tblemployee WHERE EmpID = (SELECT EmpID FROM tbluseremployee WHERE UID = var_user))),'#Param5', DATE_FORMAT(NOW(), '%W, %M %d, %Y %I:%i %p')),'#Param6',(SELECT CB_Des FROM int_collaboration WHERE CB_ID = var_activity)),'#Param7',(SELECT SUBSTRING_INDEX(CB_RToType,'_',1) FROM int_collaboration WHERE CB_ID = var_activity)),'#Param8',var_auth) INTO tmp_msg FROM crm_message WHERE Code = var_code;
	END IF;
	 RETURN tmp_msg;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_GET_EMP_ID`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_GET_EMP_ID`(userId varchar(255)) RETURNS varchar(50) CHARSET utf8
    DETERMINISTIC
BEGIN
	RETURN COALESCE((SELECT var_empid FROM ame_user WHERE var_id=userId LIMIT 1),'');
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_GET_ROLE_BY_UID`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_GET_ROLE_BY_UID`(var_userid varchar(150)) RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
BEGIN
	RETURN COALESCE((SELECT ame_user.var_crm_role FROM ame_user WHERE ame_user.var_id = var_userid LIMIT 1),'');
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_MODULE_BY_RELATED`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_MODULE_BY_RELATED`(`moduleName` varchar(50),`contentId` varchar(11)) RETURNS varchar(255) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
	DECLARE result VARCHAR(255) DEFAULT "";
	IF COALESCE(moduleName, "") <> "" THEN
		CASE UPPER(moduleName)
				WHEN "CAMPAIGN" THEN
					SELECT CA_Name INTO result FROM crm_camp WHERE CA_ID = contentId;

				WHEN "LEAD" THEN
					SELECT CONCAT(LA_Salutation,' ',LA_FirstName,' ',LA_LastName) INTO result FROM crm_lead WHERE LA_ID = contentId;

				WHEN "OPPORTUNITY" THEN	
					SELECT OP_Name INTO result FROM crm_opportunity WHERE OP_ID = contentId;

				WHEN "CASE" THEN
					SELECT CS_Subject INTO result FROM crm_case WHERE CS_ID = contentId;

				WHEN "CONTACT" THEN
					SELECT CONCAT(CO_Salutation,' ',CO_FirstName,' ',CO_LastName) INTO result FROM crm_contact WHERE CO_ID = contentId;

				WHEN "EVENT" THEN 
					SELECT EV_Name INTO result FROM crm_event WHERE EV_ID = contenId;

				WHEN "METTING" THEN
					SELECT M_Subject INTO result FROM crm_meeting WHERE M_ID = contentId;

				WHEN "TASK" THEN
					SELECT TA_Subject INTO result FROM crm_task WHERE TA_ID = contentId;
			
				WHEN "CUSTOMER" THEN	
					SELECT var_name INTO result FROM ame_mt_cus WHERE var_id = contentId;

				WHEN "PROJECT LIST" THEN	
					SELECT PL_Name INTO result FROM crm_projectlist WHERE PL_ID = contentId;
				
				WHEN "PO FORECASTING" THEN	
					SELECT PO_Name INTO result FROM crm_po_forecasting WHERE PO_ID = contentId;
				
				ELSE BEGIN END;
			END CASE;
			RETURN result;
		ELSE 
				RETURN '';
		END IF;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_PREFIX_LAST_ID`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_PREFIX_LAST_ID`(modid varchar(100)) RETURNS varchar(100) CHARSET utf8
    DETERMINISTIC
BEGIN
	DECLARE NumPre VARCHAR(100) DEFAULT "";
	DECLARE R VARCHAR(100) DEFAULT "";
	DECLARE L1 INTEGER DEFAULT 0;
	DECLARE L2 INTEGER DEFAULT 0;	
	SELECT var_id,int_length,int_lastid INTO NumPre,L1,L2 FROM ame_sys_prefix WHERE var_modid = modid;
	SET @C = FOUND_ROWS();
	IF @C > 0 THEN
			UPDATE ame_sys_prefix SET int_lastid = LPAD(L2 + 1,L1,"0") WHERE var_modid = modid;
			RETURN CONCAT(NumPre,LPAD(L2,L1,"0"));
	END IF;
	RETURN "";
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `CRM_FUN_REPORT_HEADER`;
DELIMITER |
CREATE FUNCTION `CRM_FUN_REPORT_HEADER`() RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
BEGIN
	RETURN COALESCE((SELECT var_name2 FROM ame_mt_com LIMIT 1),'');
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `crm_generate_zero`;
DELIMITER |
CREATE FUNCTION `crm_generate_zero`(`int_leng` int,`int_lastId` int) RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
BEGIN
	DECLARE NumberPre VARCHAR(255) DEFAULT "";
	DECLARE NumOfZero VARCHAR(255) DEFAULT "";
	DECLARE LenID INTEGER DEFAULT 0;
	SET LenID = LENGTH(int_lastId);
	if  LenID < int_leng THEN
		while LenID < int_leng do
			SET LenID = LenID + 1;
			SET NumOfZero = CONCAT(NumOfZero,"0");
		end while;
		RETURN CONCAT(NumberPre,NumOfZero,int_lastId);
	ELSE
		RETURN CONCAT(NumberPre,int_lastId);
	END IF;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `crm_get_sales_target`;
DELIMITER |
CREATE FUNCTION `crm_get_sales_target`(var_emp varchar(255)) RETURNS double
    DETERMINISTIC
BEGIN
	DECLARE var_cm INTEGER DEFAULT (MONTH(now()));
	DECLARE var_amt DOUBLE DEFAULT 0;
	IF var_cm = 12 THEN
		SELECT COALESCE(SUM(bs.December),0) INTO var_amt
		FROM tblbudget b
		LEFT JOIN tblbudgetset bs ON bs.BudgetID = b.BudgetID
		WHERE `Year` = YEAR(now()) 
		AND EmpID = var_emp 
		AND TargetType = 'By Sales Amount';
	ELSEIF var_cm = 1 THEN
		SELECT COALESCE(SUM(January),0) INTO var_amt
		FROM tblbudget b
		LEFT JOIN tblbudgetset bs ON bs.BudgetID = b.BudgetID
		WHERE `Year` = YEAR(now()) 
		AND EmpID = var_emp 
		AND TargetType = 'By Sales Amount';
	ELSEIF var_cm = 2 THEN
		SELECT COALESCE(SUM(February),0) INTO var_amt
		FROM tblbudget b
		LEFT JOIN tblbudgetset bs ON bs.BudgetID = b.BudgetID
		WHERE `Year` = YEAR(now()) 
		AND EmpID = var_emp 
		AND TargetType = 'By Sales Amount';
	ELSEIF var_cm = 3 THEN
		SELECT COALESCE(SUM(March),0) INTO var_amt
		FROM tblbudget b
		LEFT JOIN tblbudgetset bs ON bs.BudgetID = b.BudgetID
		WHERE `Year` = YEAR(now()) 
		AND EmpID = var_emp 
		AND TargetType = 'By Sales Amount';
	ELSEIF var_cm = 4 THEN
		SELECT COALESCE(SUM(April),0) INTO var_amt
		FROM tblbudget b
		LEFT JOIN tblbudgetset bs ON bs.BudgetID = b.BudgetID
		WHERE `Year` = YEAR(now()) 
		AND EmpID = var_emp 
		AND TargetType = 'By Sales Amount';
	ELSEIF var_cm = 5 THEN
		SELECT COALESCE(SUM(May),0) INTO var_amt
		FROM tblbudget b
		LEFT JOIN tblbudgetset bs ON bs.BudgetID = b.BudgetID
		WHERE `Year` = YEAR(now()) 
		AND EmpID = var_emp 
		AND TargetType = 'By Sales Amount';
	ELSEIF var_cm = 6 THEN
		SELECT COALESCE(SUM(June),0) INTO var_amt
		FROM tblbudget b
		LEFT JOIN tblbudgetset bs ON bs.BudgetID = b.BudgetID
		WHERE `Year` = YEAR(now()) 
		AND EmpID = var_emp 
		AND TargetType = 'By Sales Amount';
	ELSEIF var_cm = 7 THEN
		SELECT COALESCE(SUM(July),0) INTO var_amt
		FROM tblbudget b
		LEFT JOIN tblbudgetset bs ON bs.BudgetID = b.BudgetID
		WHERE `Year` = YEAR(now()) 
		AND EmpID = var_emp 
		AND TargetType = 'By Sales Amount';
	ELSEIF var_cm = 8 THEN
		SELECT COALESCE(SUM(August),0) INTO var_amt
		FROM tblbudget b
		LEFT JOIN tblbudgetset bs ON bs.BudgetID = b.BudgetID
		WHERE `Year` = YEAR(now()) 
		AND EmpID = var_emp 
		AND TargetType = 'By Sales Amount';
	ELSEIF var_cm = 9 THEN
		SELECT COALESCE(SUM(September),0) INTO var_amt
		FROM tblbudget b
		LEFT JOIN tblbudgetset bs ON bs.BudgetID = b.BudgetID
		WHERE `Year` = YEAR(now()) 
		AND EmpID = var_emp 
		AND TargetType = 'By Sales Amount';
	ELSEIF var_cm = 10 THEN
		SELECT COALESCE(SUM(October),0) INTO var_amt
		FROM tblbudget b
		LEFT JOIN tblbudgetset bs ON bs.BudgetID = b.BudgetID
		WHERE `Year` = YEAR(now()) 
		AND EmpID = var_emp 
		AND TargetType = 'By Sales Amount';
	ELSEIF var_cm = 11 THEN
		SELECT COALESCE(SUM(November),0) INTO var_amt
		FROM tblbudget b
		LEFT JOIN tblbudgetset bs ON bs.BudgetID = b.BudgetID
		WHERE `Year` = YEAR(now()) 
		AND EmpID = var_emp 
		AND TargetType = 'By Sales Amount';
	END IF;
	RETURN var_amt;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `crm_message_generater`;
DELIMITER |
CREATE FUNCTION `crm_message_generater`(var_code varchar(10), var_lang varchar(1),`module` varchar(50),`moduleId` varchar(50)) RETURNS varchar(500) CHARSET utf8
    DETERMINISTIC
BEGIN
DECLARE msg_tv VARCHAR(500);
DECLARE module_obj VARCHAR(255);
IF var_lang = 0 THEN
	SELECT COALESCE(EN,'') INTO module_obj FROM crm_language_object WHERE LangCode = module;
	IF module_obj IS NULL THEN 
		SET module_obj = (SELECT COALESCE(EN,'') FROM crm_language_object WHERE LangCode = 457);
	END IF;
	SELECT REPLACE(REPLACE(Msg,"#param1",module_obj),"#param2",moduleId) INTO msg_tv FROM crm_message WHERE `Code` = var_code;
ELSEIF var_lang = 1 THEN
	SELECT COALESCE(KH,'') INTO module_obj FROM crm_language_object WHERE LangCode = module;
	IF module_obj IS NULL THEN 
		SET module_obj = (SELECT COALESCE(KH,'') FROM crm_language_object WHERE LangCode = 457);
	END IF;
	SELECT REPLACE(REPLACE(MsgKH,"#param1",module_obj),"#param2",moduleId) INTO msg_tv FROM crm_message WHERE `Code` = var_code;
ELSEIF var_lang = 2 THEN
	SELECT COALESCE(CN,'') INTO module_obj FROM crm_language_object WHERE LangCode = module;
	IF module_obj IS NULL THEN 
		SET module_obj = (SELECT COALESCE(CN,'') FROM crm_language_object WHERE LangCode = 457);
	END IF;
	SELECT REPLACE(REPLACE(MsgCN,"#param1",module_obj),"#param2",moduleId) INTO msg_tv FROM crm_message WHERE `Code` = var_code;
ELSE
	SELECT COALESCE(EN,'') INTO module_obj FROM crm_language_object WHERE LangCode = module;
	IF module_obj IS NULL THEN 
		SET module_obj = (SELECT COALESCE(EN,'') FROM crm_language_object WHERE LangCode = 457);
	END IF;
	SELECT REPLACE(REPLACE(Msg,"#param1",module_obj),"#param2",moduleId) INTO msg_tv FROM crm_message WHERE `Code` = var_code;
END IF;
RETURN msg_tv;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `crm_sale_order_generate_ship_to`;
DELIMITER |
CREATE FUNCTION `crm_sale_order_generate_ship_to`(var_ship_to varchar(255), var_cust_id varchar(255)) RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
BEGIN
	DECLARE ship VARCHAR(255) DEFAULT '';
	DECLARE c INTEGER  DEFAULT 0;
	IF var_ship_to = '' THEN
		RETURN "";
	END IF;
	SET c = (SELECT COUNT(*) FROM tblshipaddress WHERE docid = var_cust_id AND inactive = 0 AND shipid = var_ship_to);
	IF c > 0 THEN
		SET ship = var_ship_to;
	ELSE		
		SET ship = (SELECT generateAutoId('DA'));
		INSERT INTO tblshipaddress(`moduleid`, `docid`, `shipid`, `shipname`, `inactive`) VALUES('MT-CUS', var_cust_id, ship,var_ship_to,0);
	END IF;
	RETURN ship;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `crm_sale_order_item_change_latest_price`;
DELIMITER |
CREATE FUNCTION `crm_sale_order_item_change_latest_price`(var_item_id varchar(255), var_uom_id varchar(255), var_cust_id varchar(255)) RETURNS double
    DETERMINISTIC
BEGIN
	DECLARE up DOUBLE DEFAULT 0;
	SET up =  COALESCE((SELECT d.UnitPrice FROM tblsales m INNER JOIN tblsalesdetails d ON m.SalID = d.SalID WHERE m.PostStatus IN('Open','Posted') AND m.CustID = var_cust_id AND d.ItemID = var_item_id AND d.UomID = var_uom_id AND m.SalDate = (SELECT MAX(s.SalDate) FROM tblsales s INNER JOIN tblsalesdetails sd ON s.SalID = sd.SalID WHERE s.PostStatus IN('Open','Posted') AND  s.CustID = var_cust_id AND sd.ItemID = var_item_id AND sd.UomID = var_uom_id) ORDER BY m.SalID DESC LIMIT 1),0);
	RETURN up;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `crm_user_role_type`;
DELIMITER |
CREATE FUNCTION `crm_user_role_type`(`var_userid` varchar(255)) RETURNS tinyint(4)
    DETERMINISTIC
BEGIN
	DECLARE var_rt INTEGER;
	SET var_rt = COALESCE((SELECT RM_TypeID FROM crm_role WHERE RM_ID = (select URoleID FROM tbluser WHERE UID = var_userid LIMIT 1)), 0);
	RETURN var_rt;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `findModuleDetailsByModuleNameAndItsId`;
DELIMITER |
CREATE FUNCTION `findModuleDetailsByModuleNameAndItsId`(`moduleName` varchar(50),`contentId` varchar(11)) RETURNS varchar(255) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
	DECLARE result VARCHAR(255) DEFAULT "";
	IF COALESCE(moduleName, "") <> "" THEN
		CASE UPPER(moduleName)
				WHEN "CAMPAIGN" THEN
					SELECT CA_Name INTO result FROM crm_camp WHERE CA_ID = contentId;

				WHEN "LEAD" THEN
					SELECT CONCAT(LA_Salutation,' ',LA_FirstName,' ',LA_LastName) INTO result FROM crm_lead WHERE LA_ID = contentId;

				WHEN "OPPORTUNITY" THEN	
					SELECT OP_Name INTO result FROM crm_opportunity WHERE OP_ID = contentId;

				WHEN "CASE" THEN
					SELECT CS_Subject INTO result FROM crm_case WHERE CS_ID = contentId;

				WHEN "CONTACT" THEN
					SELECT CONCAT(CO_Salutation,' ',CO_FirstName,' ',CO_LastName) INTO result FROM crm_contact WHERE CO_ID = contentId;

				WHEN "EVENT" THEN 
					SELECT EV_Name INTO result FROM crm_event WHERE EV_ID = contenId;

				WHEN "METTING" THEN
					SELECT M_Subject INTO result FROM crm_meeting WHERE M_ID = contentId;

				WHEN "TASK" THEN
					SELECT TA_Subject INTO result FROM crm_task WHERE TA_ID = contentId;
			
				WHEN "CUSTOMER" THEN	
					SELECT var_name INTO result FROM ame_mt_cus WHERE var_id = contentId;

				WHEN "PROJECT LIST" THEN	
					SELECT PL_Name INTO result FROM crm_projectlist WHERE PL_ID = contentId;
				
				WHEN "PO FORECASTING" THEN	
					SELECT PO_Name INTO result FROM crm_po_forecasting WHERE PO_ID = contentId;
				
				ELSE BEGIN END;
			END CASE;
			RETURN result;
		ELSE 
				RETURN '';
		END IF;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnAMECheckAuthorizationApproved`;
DELIMITER |
CREATE FUNCTION `fnAMECheckAuthorizationApproved`(authProcessID varchar(255), authProcessName varchar(255), authID varchar(255)) RETURNS int(11)
    DETERMINISTIC
BEGIN
	DECLARE percent INT DEFAULT(0);
	SELECT if(subauth.Auth_Type = 'Individual', 
				 if(subauth.Auth_AndOr = 'Or',
					 (SELECT if(COUNT(*) > 0, 1, 0) FROM hrm_authorisation_approved subauthapp WHERE subauthapp.Auth_ProcessID=authProcessID AND subauthapp.Auth_ProcessName=authProcessName AND subauthapp.Auth_ID=authID AND subauthapp.Auth_Status='approved'),
				 if(subauth.Auth_AndOr = 'And',
					 (SELECT COUNT(*) FROM hrm_authorisation_approved subauthapp WHERE subauthapp.Auth_ProcessID=authProcessID AND subauthapp.Auth_ProcessName=authProcessName AND subauthapp.Auth_ID=authID AND subauthapp.Auth_Status='approved'), 0)),
				 if(subauth.Auth_Type = 'Group', 
					 (SELECT COUNT(*) FROM hrm_authorisation_approved subauthapp WHERE subauthapp.Auth_ProcessID=authProcessID AND subauthapp.Auth_ProcessName=authProcessName AND subauthapp.Auth_ID=authID AND subauthapp.Auth_Status='approved'), 0)) INTO percent
	FROM hrm_authorisation subauth WHERE subauth.Auth_ID = authID;
	RETURN percent;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMCheckAuthorisationApprove`;
DELIMITER |
CREATE FUNCTION `fnHRMCheckAuthorisationApprove`(authProcessID varchar(255), authProcessName varchar(255), authID varchar(255)) RETURNS int(11)
    DETERMINISTIC
BEGIN
	DECLARE percent INT DEFAULT(0);
	SELECT if(subauth.Auth_Type = 'Individual', 
						if(subauth.Auth_AndOr = 'Or', 1, if(subauth.Auth_AndOr = 'And', subauth.Auth_Amount , 0))
					, if(subauth.Auth_Type = 'Group', (SELECT SUM(subauthD.Auth_GroupAmount) FROM hrm_authorisation_detail subauthD WHERE subauthD.Auth_ID = subauth.Auth_ID), 0)) INTO percent
	FROM hrm_authorisation_temp subauth WHERE subauth.Auth_ID = authID AND subauth.Auth_TranID = authProcessID;
	RETURN percent;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMCheckAuthorisationApproved`;
DELIMITER |
CREATE FUNCTION `fnHRMCheckAuthorisationApproved`(authProcessID varchar(255), authProcessName varchar(255), authID varchar(255)) RETURNS int(11)
    DETERMINISTIC
BEGIN
	DECLARE percent INT DEFAULT(0);
	SELECT if(subauth.Auth_Type = 'Individual', 
				 if(subauth.Auth_AndOr = 'Or',
					 (SELECT if(COUNT(*) > 0, 1, 0) FROM hrm_authorisation_approved subauthapp WHERE subauthapp.Auth_ProcessID=authProcessID AND subauthapp.Auth_ProcessName=authProcessName AND subauthapp.Auth_ID=authID AND subauthapp.Auth_Status='approved'),
				 if(subauth.Auth_AndOr = 'And',
					 (SELECT COUNT(*) FROM hrm_authorisation_approved subauthapp WHERE subauthapp.Auth_ProcessID=authProcessID AND subauthapp.Auth_ProcessName=authProcessName AND subauthapp.Auth_ID=authID AND subauthapp.Auth_Status='approved'), 0)),
				 if(subauth.Auth_Type = 'Group', 
					 (SELECT COUNT(*) FROM hrm_authorisation_approved subauthapp WHERE subauthapp.Auth_ProcessID=authProcessID AND subauthapp.Auth_ProcessName=authProcessName AND subauthapp.Auth_ID=authID AND subauthapp.Auth_Status='approved'), 0)) INTO percent
	FROM hrm_authorisation_temp subauth WHERE subauth.Auth_ID = authID AND subauth.Auth_TranID = authProcessID;
	RETURN percent;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMCheckAuthorisationApproveGroup`;
DELIMITER |
CREATE FUNCTION `fnHRMCheckAuthorisationApproveGroup`(N int,authProcessID varchar(255), authProcessName varchar(255), authID varchar(255)) RETURNS int(11)
    DETERMINISTIC
BEGIN
	DECLARE result INT DEFAULT(0);
	DECLARE temp INT DEFAULT(0);
	DECLARE GroupID VARCHAR(255);
	DECLARE GroupAndOr VARCHAR(255);
	DECLARE GroupAmount VARCHAR(255);
	DECLARE done INT DEFAULT(0);
  DECLARE cursor_i CURSOR FOR SELECT Auth_GroupID, Auth_GroupAndOr, Auth_GroupAmount FROM hrm_authorisation_detail subauthD WHERE subauthD.Auth_ID = authID;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
	OPEN cursor_i;
	read_loop: LOOP
    FETCH cursor_i INTO GroupID, GroupAndOr, GroupAmount;
    IF done THEN
      LEAVE read_loop;
    END IF;
		SELECT if(GroupAndOr = 'Or', (SELECT if(COUNT(*) > 0, 100, 0) FROM hrm_authorisation_approved subauthapp WHERE subauthapp.Auth_ProcessID=authProcessID AND subauthapp.Auth_ProcessName=authProcessName AND subauthapp.Auth_ID=authID AND subauthapp.Auth_GroupID = GroupID AND subauthapp.Auth_Status='approved')
					, if(GroupAndOr = 'And', (SELECT (COUNT(*)*100)/GroupAmount FROM hrm_authorisation_approved subauthapp WHERE subauthapp.Auth_ProcessID=authProcessID AND subauthapp.Auth_ProcessName=authProcessName AND subauthapp.Auth_ID=authID AND subauthapp.Auth_GroupID = GroupID AND subauthapp.Auth_Status='approved'), 0))
		INTO temp;
		SET result = result + temp;
	END LOOP;
  CLOSE cursor_i;
	RETURN result / N;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMCheckAuthorisationGroupApproved`;
DELIMITER |
CREATE FUNCTION `fnHRMCheckAuthorisationGroupApproved`(authProcessID varchar(255), authProcessName varchar(255), authID varchar(255),authGro varchar(255)) RETURNS int(11)
    DETERMINISTIC
BEGIN
	DECLARE percent INT DEFAULT(0);
	SELECT if(subauth.Auth_Type = 'Individual', 
				 if(subauth.Auth_AndOr = 'Or',
					 (SELECT if(COUNT(*) > 0, 1, 0) FROM hrm_authorisation_approved subauthapp WHERE subauthapp.Auth_ProcessID=authProcessID AND subauthapp.Auth_ProcessName=authProcessName AND subauthapp.Auth_ID=authID AND subauthapp.Auth_Status='approved'),
				 if(subauth.Auth_AndOr = 'And',
					 (SELECT COUNT(*) FROM hrm_authorisation_approved subauthapp WHERE subauthapp.Auth_ProcessID=authProcessID AND subauthapp.Auth_ProcessName=authProcessName AND subauthapp.Auth_ID=authID AND subauthapp.Auth_Status='approved'), 0)),
				 if(subauth.Auth_Type = 'Group', 
					 (SELECT COUNT(*) FROM hrm_authorisation_approved subauthapp WHERE subauthapp.Auth_ProcessID=authProcessID AND subauthapp.Auth_ProcessName=authProcessName AND subauthapp.Auth_ID=authID AND subauthapp.Auth_GroupID = authGro AND subauthapp.Auth_Status='approved'), 0)) INTO percent
	FROM hrm_authorisation_temp subauth WHERE subauth.Auth_ID = authID AND subauth.Auth_TranID = authProcessID;
	RETURN percent;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMCheckBonusProrate`;
DELIMITER |
CREATE FUNCTION `fnHRMCheckBonusProrate`(SEmpJoinDate Date,SEmpProbation tinyint,SEmpEndDateProbation Date, SBDate Date, SSalaryAmount float) RETURNS float
    DETERMINISTIC
BEGIN
	DECLARE SBalance FLOAT DEFAULT(0);
	DECLARE SNow DATE;
	DECLARE tz VARCHAR(20) DEFAULT('');
	
	SELECT Setting_Value INTO tz FROM hrm_setting WHERE Setting_Type = 'TimeZone' AND Setting_Name = 'TimeZone';
	SET SNow = convert_tz(now(), @@session.time_zone, tz);
	
	IF SEmpProbation =  1 THEN
		IF YEAR(SEmpEndDateProbation) = YEAR(SBDate) AND MONTH(SEmpEndDateProbation) < MONTH(SBDate) THEN
			-- SET SBalance = (SSalaryAmount / 12) * (MONTH(SBDate) - MONTH(SEmpEndDateProbation));
			SET SBalance = 0;
		ELSEIF YEAR(SEmpEndDateProbation) < (YEAR(SBDate) -1) THEN
			SET SBalance = SSalaryAmount;
		ELSEIF YEAR(SEmpEndDateProbation) < YEAR(SBDate) THEN
			SET SBalance = (SSalaryAmount / 12) * (13 - (MONTH(SEmpEndDateProbation)));
		ELSE
			SET SBalance = 0;
		END IF;
	ELSE
		IF YEAR(SEmpJoinDate) = YEAR(SBDate) AND MONTH(SEmpJoinDate) < MONTH(SBDate) THEN
			-- SET SBalance = (SSalaryAmount / 12) * (MONTH(SBDate) - MONTH(SEmpJoinDate));
			SET SBalance = 0;
		ELSEIF YEAR(SEmpJoinDate) < (YEAR(SBDate) -1) THEN
			SET SBalance = SSalaryAmount;
		ELSEIF YEAR(SEmpJoinDate) < YEAR(SBDate) THEN
			SET SBalance = (SSalaryAmount / 12) * (13 - (MONTH(SEmpJoinDate)));
		ELSE
			SET SBalance = 0;
		END IF;
	END IF;
	RETURN SBalance;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMCheckBreakTime`;
DELIMITER |
CREATE FUNCTION `fnHRMCheckBreakTime`(FP_Duration Time, FP_Punch Time, BreakTime varchar(255), SStatus varchar(50)) RETURNS time
    DETERMINISTIC
BEGIN
	DECLARE TimeFrom TIME DEFAULT('00:00:00');
	DECLARE TimeTo TIME DEFAULT('00:00:00');
	DECLARE Result TIME DEFAULT('00:00:00');

	IF BreakTime <> '' THEN
		WHILE (LOCATE(';', BreakTime) > 0)
		DO
			SET @value = LEFT(BreakTime, LOCATE(';',BreakTime) - 1);    
			SET BreakTime = SUBSTRING(BreakTime, LOCATE(';',BreakTime) + 1);
			IF @value != "" THEN

				SET TimeFrom = TIME(fnHRMSplit(@value, '-', 1));
				SET TimeTo = TIME(fnHRMSplit(@value, '-', 2));
				IF SStatus = 'TimeIn' THEN
					IF FP_Punch >=  TimeTo THEN
						SET Result = ADDTIME(Result,TIMEDIFF(TimeTo,TimeFrom));
					ELSEIF FP_Punch > TimeFrom THEN
						SET Result = ADDTIME(Result,TIMEDIFF(FP_Punch, TimeFrom));
					END IF;
				ELSEIF SStatus = 'TimeOut' THEN
					IF FP_Punch <=  TimeFrom THEN
						SET Result = ADDTIME(Result,TIMEDIFF(TimeTo,TimeFrom));
					ELSEIF FP_Punch < TimeTo THEN
						SET Result = ADDTIME(Result,TIMEDIFF(TimeTo,FP_Punch));
					END IF;
				ELSEIF SStatus = 'AB' OR SStatus = 'TimeLeave' THEN
					SET Result = ADDTIME(Result,TIMEDIFF(TimeTo,TimeFrom));
				END IF;

			END IF;
		END WHILE;
		RETURN TIMEDIFF(FP_Duration, Result);
	ELSE
		RETURN FP_Duration;
	END IF;
	
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMCheckLateDurationDeduction`;
DELIMITER |
CREATE FUNCTION `fnHRMCheckLateDurationDeduction`(SLateDuration time) RETURNS time
    DETERMINISTIC
BEGIN
	DECLARE LateDurationDeduction INT DEFAULT(0);
	SELECT LD_Deduction INTO LateDurationDeduction FROM hrm_timesheet_late_duration WHERE MINUTE(SLateDuration) >= LD_From AND MINUTE(SLateDuration) <= LD_TO;
	IF LateDurationDeduction = 0 THEN
		RETURN SLateDuration;
	ELSE
		RETURN CONCAT(HOUR(SLateDuration), ':', LateDurationDeduction, ':00');
	END IF;
	
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMCheckLeaveBaanceRate`;
DELIMITER |
CREATE FUNCTION `fnHRMCheckLeaveBaanceRate`(SEmpID varchar(255), TypeLeaveBalance varchar(255), SAmount float) RETURNS float
    DETERMINISTIC
BEGIN
	DECLARE SBalance FLOAT DEFAULT(0);
	DECLARE JoinDate DATE DEFAULT('0000-00-00');
	DECLARE Probation TINYINT DEFAULT(0);
	DECLARE ProbationEndDate DATE DEFAULT('0000-00-00');
	DECLARE SNow DATE;
	DECLARE tz VARCHAR(20) DEFAULT('');
	
	SELECT Setting_Value INTO tz FROM hrm_setting WHERE Setting_Type = 'TimeZone' AND Setting_Name = 'TimeZone';
	SET SNow = convert_tz(now(), @@session.time_zone, tz);
	
	IF LOWER(TypeLeaveBalance) = 'year' THEN
		SET SBalance = SAmount;
  ELSE
		SELECT COALESCE(e.dat_hrm_joinDate, '0000-00-00') AS Emp_JoinDate
		, COALESCE(e.tin_hrm_probation, 0) AS Emp_Probation
		, COALESCE(e.dat_hrm_enddateprobation, '0000-00-00') AS Emp_EndDateProbation INTO JoinDate, Probation, ProbationEndDate FROM ame_mt_emp e WHERE e.var_id = SEmpID;
		IF Probation = 1 THEN
			IF YEAR(ProbationEndDate) < YEAR(SNow) THEN
				SET SBalance = SAmount;
			ELSEIF ProbationEndDate <= SNow THEN
				SET SBalance = FLOOR(((SAmount/12) * ((13 - MONTH(ProbationEndDate)) - (FLOOR(((DAY(ProbationEndDate) -1)  / (30 / (SAmount/12))) * 2) / 2))) * 2 ) / 2;
			ELSE
				SET SBalance = 0;
			END IF;
		ELSE
			IF YEAR(JoinDate) < YEAR(SNow) THEN
				SET SBalance = SAmount;
			ELSE
				SET SBalance = FLOOR(((SAmount/12) * ((13 - MONTH(JoinDate)) - (FLOOR(((DAY(JoinDate) -1) / (30 / (SAmount/12))) * 2) / 2))) * 2) / 2;
			END IF;
		END IF;
	END IF;
	RETURN SBalance;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMCheckLeaveBalanceDaily`;
DELIMITER |
CREATE FUNCTION `fnHRMCheckLeaveBalanceDaily`(SEmpID varchar(255), STypeID varchar(255), SBHourly varchar(255)) RETURNS float
    DETERMINISTIC
BEGIN
	DECLARE SBalance FLOAT DEFAULT(0);

	IF LOWER(SBHourly) = 'half of day' THEN
			SELECT SUM(COALESCE(ld.Leave_Amount)) INTO SBalance 
			FROM hrm_timesheet_leave l 
			LEFT JOIN hrm_timesheet_leave_detail ld ON(l.Leave_ID = ld.LeaveD_ID AND ld.LeaveD_TypeID = STypeID) 
			WHERE l.Leave_EmpID = SEmpID AND LOWER(l.Leave_Status) <> 'rejected';
	ELSEIF LOWER(SBHourly) = 'hourly' THEN
			SELECT COALESCE(SUM(if(LOWER(ld.LeaveD_Duration) = 'full day', ld.Leave_Total, 0)), 0) INTO SBalance 
			FROM hrm_timesheet_leave l 
			LEFT JOIN hrm_timesheet_leave_detail ld ON(l.Leave_ID = ld.LeaveD_ID AND l.Leave_EmpID = SEmpID AND ld.LeaveD_TypeID = STypeID)
			WHERE l.Leave_EmpID = SEmpID AND ld.LeaveD_TypeID = STypeID AND LOWER(l.Leave_Status) <> 'rejected';
	END IF;

	RETURN SBalance;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMCheckLeaveBalanceHourly`;
DELIMITER |
CREATE FUNCTION `fnHRMCheckLeaveBalanceHourly`(SEmpID varchar(255), STypeID varchar(255), SBHourly varchar(255)) RETURNS float
    DETERMINISTIC
BEGIN
	DECLARE SBalance FLOAT DEFAULT(0);

	IF LOWER(SBHourly) = 'hourly' THEN
			SELECT COALESCE(SUM(if(LOWER(ld.LeaveD_Duration) = 'hourly', ld.Leave_Total, 0)), 0) INTO SBalance 
			FROM hrm_timesheet_leave l 
			LEFT JOIN hrm_timesheet_leave_detail ld ON(l.Leave_ID = ld.LeaveD_ID AND l.Leave_EmpID = SEmpID AND ld.LeaveD_TypeID = STypeID)
			WHERE l.Leave_EmpID = SEmpID AND ld.LeaveD_TypeID = STypeID AND LOWER(l.Leave_Status) <> 'rejected';
	END IF;

	RETURN SBalance;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMCheckOutLeave`;
DELIMITER |
CREATE FUNCTION `fnHRMCheckOutLeave`(FP_Punch Time, OutLeave varchar(255)) RETURNS tinyint(4)
    DETERMINISTIC
BEGIN
	DECLARE TimeIn TIME DEFAULT('00:00:00');
	DECLARE TimeOut TIME DEFAULT('00:00:00');
	DECLARE Result TINYINT DEFAULT(0);
	WHILE (LOCATE(';', OutLeave) > 0)
	DO
		SET @value = LEFT(OutLeave, LOCATE(';',OutLeave) - 1);    
		SET OutLeave = SUBSTRING(OutLeave, LOCATE(';',OutLeave) + 1);
		IF @value != "" THEN
			SET TimeIn = TIME(fnHRMSplit(@value, '-', 1));
			SET TimeOut = TIME(fnHRMSplit(@value, '-', 2));
			IF FP_Punch >= TimeIn AND FP_Punch <= TimeOut THEN
				SET Result = Result + 1;
			END IF;
		END IF;
	END WHILE;
	RETURN Result;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMCheckWorkHour`;
DELIMITER |
CREATE FUNCTION `fnHRMCheckWorkHour`(SEmpID varchar(255)) RETURNS double
    DETERMINISTIC
BEGIN
		DECLARE WorkHour FLOAT DEFAULT(8);

		DECLARE Cur CURSOR FOR
		SELECT SQL_CALC_FOUND_ROWS Payroll_AmtWorkingHourPerDay FROM hrm_payroll_salary
		LEFT JOIN hrm_payroll ON(Payroll_ID = Salary_PayrollID)
		WHERE Salary_EmpID = SEmpID AND LOWER(Salary_Status) = 'active';
		OPEN Cur;
			SET @C = FOUND_ROWS();
			IF @C > 0 THEN
					FETCH Cur INTO WorkHour;
			END IF;
		CLOSE Cur;
		RETURN WorkHour;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMRound`;
DELIMITER |
CREATE FUNCTION `fnHRMRound`(P1 double, P2 integer) RETURNS double
    DETERMINISTIC
BEGIN
		CASE P2
		WHEN 1 THEN
				RETURN CAST(P1 AS DECIMAL(16, 1));
		WHEN 2 THEN
				RETURN CAST(P1 AS DECIMAL(16, 2));
		WHEN 3 THEN
				RETURN CAST(P1 AS DECIMAL(16, 3));
		WHEN 4 THEN
				RETURN CAST(P1 AS DECIMAL(16, 4));
		WHEN 5 THEN
				RETURN CAST(P1 AS DECIMAL(16, 5));
		WHEN 6 THEN
				RETURN CAST(P1 AS DECIMAL(16, 6));
		WHEN 7 THEN
				RETURN CAST(P1 AS DECIMAL(16, 7));
		WHEN 8 THEN
				RETURN CAST(P1 AS DECIMAL(16, 8));
		WHEN 9 THEN
				RETURN CAST(P1 AS DECIMAL(16, 9));
		WHEN 10 THEN
				RETURN CAST(P1 AS DECIMAL(16, 10));
		WHEN 11 THEN
				RETURN CAST(P1 AS DECIMAL(16, 11));
		WHEN 12 THEN
				RETURN CAST(P1 AS DECIMAL(16, 12));
		ELSE
				RETURN CAST(P1 AS DECIMAL(16, 13));
		END CASE;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMSplit`;
DELIMITER |
CREATE FUNCTION `fnHRMSplit`(x VARCHAR(255),delim VARCHAR(12),pos INT) RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
BEGIN
	RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, '');
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `fnHRMTimeToFloat`;
DELIMITER |
CREATE FUNCTION `fnHRMTimeToFloat`(N time) RETURNS float
    DETERMINISTIC
BEGIN
	
	RETURN HOUR(N) + (MINUTE(N)/60);
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `generateAutoId`;
DELIMITER |
CREATE FUNCTION `generateAutoId`(`module_id` varchar(11)) RETURNS varchar(13) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
	DECLARE Len INTEGER DEFAULT 0;
	DECLARE LastID INTEGER DEFAULT 0;
	DECLARE NumberPre VARCHAR(255) DEFAULT "";
	DECLARE NumOfZero VARCHAR(255) DEFAULT "";
	DECLARE LenID INTEGER DEFAULT 0;
	SELECT Length,crm_numberprefix.LastID,crm_numberprefix.NumberPrefix INTO Len,LastID,NumberPre from crm_numberprefix WHERE crm_numberprefix.ModuleID = module_id;
  SET LenID = LENGTH(LastID); -- + LENGTH(NumberPre);
	if  LenID < len THEN
		while LenID < len do
			SET LenID = LenID + 1;
			SET NumOfZero = CONCAT(NumOfZero,"0");
		end while;
		UPDATE crm_numberprefix SET crm_numberprefix.LastID = crm_numberprefix.LastID +1 WHERE crm_numberprefix.ModuleID = module_id;
		RETURN CONCAT(NumberPre,NumOfZero,LastID);
	ELSE
		UPDATE crm_numberprefix SET crm_numberprefix.LastID = crm_numberprefix.LastID +1 WHERE crm_numberprefix.ModuleID = module_id;
		RETURN CONCAT(NumberPre,Lastid);
	END IF;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `getUomByItemID`;
DELIMITER |
CREATE FUNCTION `getUomByItemID`(var_item varchar(255), var_status varchar(255)) RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
BEGIN
	DECLARE var_uom VARCHAR(255) DEFAULT '';
	DECLARE var_uoms VARCHAR(255) DEFAULT '';
	IF var_status = 'Owner' THEN
		BEGIN
			DECLARE v_finished INTEGER DEFAULT 0;
			DECLARE v_int INTEGER DEFAULT 0;
			DEClARE CustID_Cursor CURSOR FOR SELECT UomID FROM tblitemuom WHERE ItemID = var_item;
			DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
			OPEN CustID_Cursor;
				GetCustID: LOOP
					FETCH CustID_Cursor INTO var_uom;
						IF v_finished = 1 THEN
							LEAVE GetCustID;
						END IF;
						SET v_int = v_int + 1;
						IF v_int = 1 THEN
							SET var_uoms = var_uom; 
						ELSE
							SET var_uoms = CONCAT(var_uoms,',',var_uom); 
						END IF;						
				END LOOP GetCustID;
			CLOSE CustID_Cursor;
		END;		
	ELSE
		BEGIN
			DECLARE v_finished INTEGER DEFAULT 0;
			DECLARE v_int INTEGER DEFAULT 0;
			DEClARE CustID_Cursor CURSOR FOR SELECT UomID FROM crm_market_survey_competitor_item_uom WHERE ItemID = var_item;
			DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
			OPEN CustID_Cursor;
				GetCustID: LOOP
					FETCH CustID_Cursor INTO var_uom;
						IF v_finished = 1 THEN
							LEAVE GetCustID;
						END IF;
						SET v_int = v_int + 1;
						IF v_int = 1 THEN
							SET var_uoms = var_uom; 
						ELSE
							SET var_uoms = CONCAT(var_uoms,',',var_uom); 
						END IF;
				END LOOP GetCustID;
			CLOSE CustID_Cursor;
		END;
	END IF;
	RETURN var_uoms;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `pos_fun_get_rate`;
DELIMITER |
CREATE FUNCTION `pos_fun_get_rate`(P1 varchar(255)) RETURNS double
    DETERMINISTIC
BEGIN
		DECLARE P_NEW_NUM DOUBLE DEFAULT 0.0;
		DECLARE Cur CURSOR FOR SELECT SQL_CALC_FOUND_ROWS COALESCE(c.dou_rate,0) FROM ame_mt_cur_rate c WHERE c.var_id = P1 LIMIT 1;
		OPEN Cur;
			SET @C = FOUND_ROWS();
			IF @C > 0 THEN
					FETCH Cur INTO P_NEW_NUM;
			ELSE
					SET P_NEW_NUM = 1/4000;
			END IF;
		CLOSE Cur;
		RETURN P_NEW_NUM;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `pos_fun_Isitemdisc`;
DELIMITER |
CREATE FUNCTION `pos_fun_Isitemdisc`(var_ItemID varchar(255)) RETURNS tinyint(1)
    READS SQL DATA
    DETERMINISTIC
BEGIN
		DECLARE dou_Tax TINYINT(1) DEFAULT 0;
		DECLARE Cur CURSOR FOR SELECT SQL_CALC_FOUND_ROWS COALESCE(i.tin_no_disc,0) FROM ame_mt_ite i WHERE i.var_id = var_ItemID;
		OPEN Cur;
			SET @C = FOUND_ROWS();
			IF @C > 0 THEN
					FETCH Cur INTO dou_Tax;
			END IF;
		CLOSE Cur;
		RETURN dou_Tax;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `reportMarketingExecLeadTrendsByStatus`;
DELIMITER |
CREATE FUNCTION `reportMarketingExecLeadTrendsByStatus`() RETURNS varchar(4000) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
	DECLARE v_finished INTEGER DEFAULT 0;
	DECLARE statusId INTEGER DEFAULT 0;
	DECLARE status_list VARCHAR(4000) DEFAULT "";
	DECLARE leadCount INTEGER DEFAULT 0;
	DECLARE createdDate DATETIME DEFAULT NULL; 
	DECLARE lead_cursor CURSOR FOR SELECT DISTINCT LA_CDate FROM crm_lead;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
	OPEN lead_cursor;
		get_lead: LOOP
			FETCH lead_cursor INTO createdDate;
			IF v_finished = 1 THEN
				LEAVE get_lead;
			END IF;

				BEGIN
				DECLARE v2_finished INTEGER DEFAULT 0;
				DECLARE status_cursor CURSOR FOR SELECT LST_ID FROM crm_lead_status ORDER BY LST_ID ASC;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET v2_finished = 1;
				/* Loop for status_cursor*/
				OPEN status_cursor;
				get_status: LOOP
					FETCH status_cursor INTO statusId;
					IF v2_finished = 1 THEN
						LEAVE get_status;
					END IF;

					SELECT COUNT(LA_ID) INTO leadCount FROM crm_lead WHERE LA_StatusID = statusId AND LA_CDate = createdDate;

					SET status_list = CONCAT((SELECT LST_Name FROM crm_lead_status WHERE LST_ID = statusId),",",leadCount,",",status_list);
					END LOOP get_status;
				
				CLOSE status_cursor;
				/*End loop status_cursor*/
				END;

				SET status_list = CONCAT(createdDate,",", status_list);
			END LOOP get_lead;
	CLOSE lead_cursor;
	SET status_list = SUBSTRING(status_list,1, LENGTH(status_list)-1);
	RETURN status_list;

END |
DELIMITER ;

DROP FUNCTION IF EXISTS `TM_FUN_GENERATE_PREFIX`;
DELIMITER |
CREATE FUNCTION `TM_FUN_GENERATE_PREFIX`(P1 varchar(100), P2 tinyint(1)) RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE P_FORMAT VARCHAR(100) DEFAULT "";
		DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS CONCAT(var_id, LPAD(int_lastid, int_length, '0')) AS 'Format' FROM tm_numberprefix_custom WHERE var_modid = P1;
		OPEN C1;
				IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_FORMAT;
						IF P2 = 1 THEN UPDATE tm_numberprefix_custom SET int_lastid = COALESCE(int_lastid, 0) + 1 WHERE var_modid = P1; END IF;
				END IF;
				RETURN P_FORMAT;
		CLOSE C1;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `TM_FUN_SYSTEM_GENERATE_PREFIX`;
DELIMITER |
CREATE FUNCTION `TM_FUN_SYSTEM_GENERATE_PREFIX`(P1 varchar(100), P2 tinyint(1)) RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE P_FORMAT VARCHAR(100) DEFAULT "";
		DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS CONCAT(numberprefix, LPAD(lastid, length, '0')) AS 'Format' FROM tm_numberprefix WHERE module_id = P1;
		OPEN C1;
				IF FOUND_ROWS() > 0 THEN FETCH C1 INTO P_FORMAT;
						IF P2 = 1 THEN UPDATE tm_numberprefix SET lastid = COALESCE(lastid, 0) + 1 WHERE module_id = P1; END IF;
				END IF;
				RETURN P_FORMAT;
		CLOSE C1;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_CAL_SAL`;
DELIMITER |
CREATE FUNCTION `VB_FUN_CAL_SAL`(saleId VARCHAR(200)) RETURNS double
BEGIN

	DECLARE kgCou DOUBLE;
	DECLARE kgPri DOUBLE;

	DECLARE cbmCou DOUBLE;
	DECLARE cbmPri DOUBLE;

	DECLARE sumKg DOUBLE;
	DECLARE sumCbm DOUBLE;

	DECLARE totalAmt DOUBLE;

	SELECT SUM(dou_vb_meavalue),dou_vb_meaprice INTO kgCou,kgPri FROM ame_ar_so_det WHERE var_id = saleId AND LOWER(var_vb_meatype) = "kg" GROUP BY var_vb_meatype;
	SELECT SUM(dou_vb_meavalue),dou_vb_meaprice INTO cbmCou,cbmPri FROM ame_ar_so_det WHERE var_id = saleId AND LOWER(var_vb_meatype) = "cbm" GROUP BY var_vb_meatype;
	 
	SET kgCou = COALESCE(kgCou,0);
	SET cbmCou = COALESCE(cbmCou,0);

	SET kgCou = VB_FUN_ROUND_UP(kgCou);
	
	SET sumKg = COALESCE(kgCou,0) * COALESCE(kgPri,0);
	SET sumCbm = COALESCE(cbmCou,0) * COALESCE(cbmPri,0);

	SET totalAmt = sumKg + sumCbm;
	SET totalAmt = COALESCE(totalAmt,0);
	RETURN totalAmt; 


END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_CHECK_QTY_SAL`;
DELIMITER |
CREATE FUNCTION `VB_FUN_CHECK_QTY_SAL`(id varchar(200), salid varchar(200)) RETURNS double
BEGIN
	
	DECLARE reQty DOUBLE;
	
	SELECT dou_orderqty,dou_vb_availqty,dou_qty INTO @orderqty,@avail,@qqty FROM ame_ar_so_det WHERE var_id = salid AND var_itemid = id;
	
	IF @orderqty <= 0 THEN
			SET reQty = @avail;
	END IF;

	IF @orderqty > 0 THEN 
		SET reQty = @orderqty - @avail;
	END IF;

	IF @orderqty = 0 AND @avail = 0 THEN
		SET reQty = @qqty;
	END IF;

	SET reQty = COALESCE(reQty,0);
	RETURN reQty;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_CUST_BAL`;
DELIMITER |
CREATE FUNCTION `VB_FUN_CUST_BAL`(cust varchar(200)) RETURNS double
    DETERMINISTIC
BEGIN
    DECLARE balance DOUBLE DEFAULT 0;
	  SELECT COALESCE(tblcard.CardBalance,0) INTO balance FROM tblcard WHERE CustID = cust LIMIT 1; 
	  return balance;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_DEL_ROLE`;
DELIMITER |
CREATE FUNCTION `VB_FUN_DEL_ROLE`(`roleId` int) RETURNS varchar(255) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
	DECLARE msg VARCHAR(255) DEFAULT "";
	DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS u.vb_admin_role FROM vb_user u WHERE u.vb_admin_role = roleId;

	OPEN C1;
		IF FOUND_ROWS() > 0 THEN
			SET msg = '{"key":"USED","data":"Sorry, you cannot delete this role because it used by other users!"}';
		ELSE
			SELECT r.vb_roleName INTO @roleName FROM vb_user_role r WHERE r.vb_roleId = roleId;
			DELETE FROM vb_user_role WHERE vb_roleId = roleId;
			DELETE FROM vb_user_role_dt WHERE vb_roleId = roleId;
			SET msg = CONCAT('{"key":"DELETED","data":"',CONCAT('You have deleted ',@roleName,' role successfully!'),'"}');
		END IF;
		RETURN msg;
	CLOSE C1;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_FNUM`;
DELIMITER |
CREATE FUNCTION `VB_FUN_FNUM`(val DOUBLE) RETURNS double
BEGIN
		DECLARE dou DOUBLE;
	 SET dou = convert(val,decimal(20,3));
	 return dou;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_GENERATE_CODE`;
DELIMITER |
CREATE FUNCTION `VB_FUN_GENERATE_CODE`(cusid varchar(255)) RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE codes VARCHAR(255) DEFAULT "";
		DECLARE de INT(11) DEFAULT 0;
		DECLARE emp VARCHAR(200);

		SELECT SUBSTRING(ame_mt_cus_emp.var_empid,LENGTH(ame_mt_cus_emp.var_empid),1) INTO emp  FROM ame_mt_cus_emp WHERE var_cusid = cusid;
		SET emp = COALESCE(emp,"");
		SELECT (COUNT(COALESCE(vit_id,0))+1) INTO de FROM vb_item WHERE vit_custID = cusid;
		SET codes = UPPER(CONCAT(DATE_FORMAT(NOW(),"%d%b%y-"),de,cusid));
		SET codes = CONCAT("",emp,"",codes,"");
		RETURN codes;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_GEN_PREFIX`;
DELIMITER |
CREATE FUNCTION `VB_FUN_GEN_PREFIX`(`GID` varchar(255)) RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
BEGIN
	DECLARE TempID VARCHAR(255) DEFAULT('');
	DECLARE Num LONG DEFAULT 0;
	DECLARE N INT DEFAULT(5);

	
	BEGIN
		DECLARE Done_Over INTEGER DEFAULT 0;
		DECLARE Cur_Over CURSOR FOR SELECT SQL_CALC_FOUND_ROWS (REPLACE(MAX(i.var_id), CONCAT(GID, '-'), '') * 1) + 1 FROM ame_mt_ite i WHERE i.var_id LIKE CONCAT('%', GID, '%');
		DECLARE CONTINUE HANDLER FOR NOT FOUND SET Done_Over = 1;
		OPEN Cur_Over;
			SET @R = FOUND_ROWS();
			IF @R > 0 THEN FETCH Cur_Over INTO Num; END IF;
		CLOSE Cur_Over;
	END;
	SET Num = COALESCE(Num,0);

	IF Num = 0 THEN
		SET Num = 1;
	END IF;
	IF Num <= 99999 THEN
		SET TempID = LPAD(CONCAT(Num,''),N,'0');
	ELSE
		SET TempID = Num;
	END IF;
	RETURN CONCAT(GID,'-',TempID);
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_GET_CUS_CODE`;
DELIMITER |
CREATE FUNCTION `VB_FUN_GET_CUS_CODE`(cus varchar(200)) RETURNS varchar(200) CHARSET utf8
    DETERMINISTIC
BEGIN
		DECLARE re VARCHAR(200);
		SELECT var_pricecode INTO re  FROM ame_mt_cus WHERE var_id = cus;
		RETURN re;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_GET_EX_RATE`;
DELIMITER |
CREATE FUNCTION `VB_FUN_GET_EX_RATE`() RETURNS double
    DETERMINISTIC
BEGIN
	DECLARE cur DOUBLE;
	SELECT dollar INTO cur FROM vb_exchangerate WHERE active = 1;
	RETURN cur;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_GET_ITEM_ONLY_ID`;
DELIMITER |
CREATE FUNCTION `VB_FUN_GET_ITEM_ONLY_ID`(cc varchar(200)) RETURNS int(11)
    READS SQL DATA
    DETERMINISTIC
BEGIN
	SELECT vit_id INTO @vid FROM vb_item WHERE vit_code = cc;
	RETURN @vid;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_GET_LAST_ID`;
DELIMITER |
CREATE FUNCTION `VB_FUN_GET_LAST_ID`(modid varchar(100)) RETURNS varchar(100) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN
		DECLARE NumPre VARCHAR(100) DEFAULT "";
		DECLARE R VARCHAR(100) DEFAULT "";
		DECLARE L1 INTEGER DEFAULT 0;
		DECLARE L2 INTEGER DEFAULT 0;	
		SELECT var_id,int_length,int_lastid INTO NumPre,L1,L2 FROM ame_sys_prefix WHERE var_modid = modid;
		SET @C = FOUND_ROWS();
		IF @C > 0 THEN
				UPDATE ame_sys_prefix SET int_lastid = LPAD(L2 + 1,L1,"0") WHERE var_modid = modid;
				RETURN CONCAT(NumPre,LPAD(L2,L1,"0"));
		END IF;
		RETURN "";
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_GET_PREFIX`;
DELIMITER |
CREATE FUNCTION `VB_FUN_GET_PREFIX`(`module_id` varchar(200)) RETURNS varchar(255) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
    SQL SECURITY INVOKER
BEGIN
	DECLARE Len INTEGER DEFAULT 0;
	DECLARE LastID INTEGER DEFAULT 0;
	DECLARE NumberPre VARCHAR(255) DEFAULT "";
	DECLARE NumOfZero VARCHAR(255) DEFAULT "";
	DECLARE LenID INTEGER DEFAULT 0;
	SELECT Length,vb_numberprefix.LastID,vb_numberprefix.NumberPrefix INTO Len,LastID,NumberPre from vb_numberprefix WHERE vb_numberprefix.ModuleID = module_id;
  SET LenID = LENGTH(LastID) + LENGTH(NumberPre);
	if  LenID< len THEN
	while LenID < len do
		SET LenID = LenID + 1;
 		SET NumOfZero = CONCAT(NumOfZero,"0");
	 	end while;
		UPDATE vb_numberprefix SET vb_numberprefix.LastID = vb_numberprefix.LastID +1 WHERE vb_numberprefix.ModuleID = module_id;
		RETURN CONCAT(NumberPre,"-",NumOfZero,LastID);
	ELSE
		UPDATE vb_numberprefix SET vb_numberprefix.LastID = vb_numberprefix.LastID +1 WHERE vb_numberprefix.ModuleID = module_id;
		RETURN CONCAT(NumberPre,"-",Lastid);
	END IF;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_HTTP`;
DELIMITER |
CREATE FUNCTION `VB_FUN_HTTP`(cid varchar(200),typ varchar(200),name varchar(200)) RETURNS varchar(200) CHARSET utf8
    DETERMINISTIC
BEGIN
	 DECLARE rr VARCHAR(200) DEFAULT 0;
   SELECT COALESCE(var_name,"") INTO rr FROM ame_sys_config WHERE var_id = "IMG_GE_WEB";
   SET rr = CONCAT(rr,"Customer/",cid,typ,`name`);
	RETURN rr;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_MEG_NOTI`;
DELIMITER |
CREATE FUNCTION `VB_FUN_MEG_NOTI`(scode varchar(255),  op1 varchar(255),  op2 varchar(255)) RETURNS varchar(255) CHARSET utf8
    DETERMINISTIC
BEGIN
	DECLARE TempID VARCHAR(255) DEFAULT('');
	SELECT CONCAT(COALESCE(msg1,''),' ',op1, ' ', COALESCE(msg2,''),' ',op2,' ',COALESCE(msg3,"")) INTO TempID FROM vb_notification_message WHERE `id` = scode;
	RETURN TempID;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_FUN_ROUND_UP`;
DELIMITER |
CREATE FUNCTION `VB_FUN_ROUND_UP`(dou_value double) RETURNS double
    READS SQL DATA
    DETERMINISTIC
BEGIN
	SET @intVal = FLOOR(dou_value);
	SET @con = dou_value - @intVal;
	IF @con >= 0.1 AND @con <= 0.5 THEN
		RETURN @intVal + 0.5;
	ELSEIF @con = 0 THEN
		RETURN @intVal;
	ELSE 
		RETURN @intVal + 1;
	END IF;
END |
DELIMITER ;

DROP FUNCTION IF EXISTS `VB_PRO_UPDATE_SO_DET`;
DELIMITER |
CREATE FUNCTION `VB_PRO_UPDATE_SO_DET`(`saleId` varchar(100),`itemCode` varchar(100),`meaType` varchar(100),`meaValue` double,`meaPrice` double,`availQty` double, `itemId` varchar(100)) RETURNS varchar(255) CHARSET utf8
    READS SQL DATA
    DETERMINISTIC
BEGIN

	DECLARE C1 CURSOR FOR SELECT SQL_CALC_FOUND_ROWS var_id FROM ame_mt_ite WHERE var_itemcode = itemCode;
	OPEN C1;
		IF FOUND_ROWS() > 0 THEN
			UPDATE 
				ame_ar_so_det 
			SET 
				var_vb_meatype = meaType, 	
				dou_vb_availqty = availQty,
				dou_vb_meavalue = meaValue, 
				dou_vb_meaprice = meaPrice  
			WHERE var_id = saleId AND var_itemid = itemId;
			UPDATE vb_itemdetail SET vit_someqty = availQty WHERE vit_itemId = itemId;
			SET @result = CONCAT('The data related to "',itemCode,'" updated successfully, but item code cannot update because it has already existed!');
		ELSE 
			UPDATE 
				ame_ar_so_det 
			SET 
				var_vb_meatype = meaType, 	
				dou_vb_availqty = availQty,
				dou_vb_meavalue = meaValue, 
				dou_vb_meaprice = meaPrice  
			WHERE var_id = saleId AND var_itemid = itemId;
			
