From c2b84160adc038f1650cb902da6bf7b5e08c2b57 Mon Sep 17 00:00:00 2001 From: Gopi Katwala Date: Sat, 8 Jun 2019 21:12:38 -0400 Subject: [PATCH] fixed new id creation for the first row --- AstuteSystem/sql/astute.sql | 51 +++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/AstuteSystem/sql/astute.sql b/AstuteSystem/sql/astute.sql index 964d86d..44a0b41 100644 --- a/AstuteSystem/sql/astute.sql +++ b/AstuteSystem/sql/astute.sql @@ -17,7 +17,7 @@ DROP DATABASE IF EXISTS `astute`; CREATE DATABASE IF NOT EXISTS `astute` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `astute`; -CREATE USER 'astute_user'@'localhost' IDENTIFIED BY 'password'; +-- CREATE USER 'astute_user'@'localhost' IDENTIFIED BY 'password'; CREATE TABLE IF NOT EXISTS `change_order` ( `PO_num` varchar(20) NOT NULL, @@ -33,21 +33,24 @@ CREATE TABLE IF NOT EXISTS `change_order` ( /*!40000 ALTER TABLE `change_order` DISABLE KEYS */; /*!40000 ALTER TABLE `change_order` ENABLE KEYS */; --- Dumping structure for function astute.create_customer_contact_fun -DELIMITER // -CREATE DEFINER=`root`@`localhost` FUNCTION `create_customer_contact_fun`(customer_id_in varchar(20), name varchar(100), title varchar(50), work_phone int, work_phone_ext int, mobile int, fax int, email varchar(100), address varchar(500)) RETURNS varchar(20) CHARSET utf8 +CREATE FUNCTION astute.`create_customer_contact_fun`(customer_id_in varchar(20), name varchar(100), title varchar(50), work_phone int, work_phone_ext int, mobile int, fax int, email varchar(100), address varchar(500)) RETURNS varchar(20) CHARSET utf8 BEGIN DECLARE last_inserted_id varchar(20); DECLARE new_contact_id int; +DECLARE contactCount int; -SELECT max(contact_id)+1 into new_contact_id FROM customer_contact where customer_id = customer_id_in; +SELECT COUNT(customer_contact_id) INTO contactCount FROM customer_contact WHERE customer_id = customer_id_in; +if contactCount > 0 then + SELECT max(contact_id)+1 into new_contact_id FROM customer_contact where customer_id = customer_id_in; +else + SET new_contact_id = 1; +end if; INSERT INTO astute.customer_contact (customer_id, contact_id, name, title, work_phone, work_phone_ext, mobile, fax, email, address) VALUES (customer_id_in, new_contact_id, name, title, work_phone, work_phone_ext, mobile, fax, email, address); SELECT LAST_INSERT_ID() into last_inserted_id; return last_inserted_id; -END// -DELIMITER ; +END; -- Dumping structure for function astute.create_customer_fun DELIMITER // @@ -72,12 +75,25 @@ DELIMITER ; -- Dumping structure for procedure astute.create_invoice_detail DELIMITER // -CREATE DEFINER=`root`@`localhost` PROCEDURE `create_invoice_detail`(invoiceNum varchar(20), lineItemNum int, POLineItemNum varchar(20), serviceTypeId int, description varchar(500), qty_in double, fee_in double, fee_type_id_in int) +CREATE PROCEDURE astute.`create_invoice_detail`(invoiceNum varchar(20), lineItemNum int, POLineItemNum varchar(20), serviceTypeId int, description varchar(500), qty_in double, fee_in double, fee_type_id_in int) BEGIN +declare maxlineItemNo int; +DECLARE detailCount int; + +SELECT COUNT(line_item_num) INTO detailCount FROM invoice_detail where inv_num = invoiceNum; +if lineItemNum is null or lineItemNum = 0 then + if detailCount > 0 then + select max(lineItemNum) into maxlineItemNo from invoice_detail where inv_num = invoiceNum; + else + set maxlineItemNo = 1; + end if; +else + set maxlineItemNo = lineItemNum; +end if; + INSERT INTO INVOICE_DETAIL (inv_num, line_item_num, PO_line_item_num, service_type_id, description, qty, fee, fee_type_id) -VALUES (invoiceNum, lineItemNum, POLineItemNum, serviceTypeId, description, qty_in, fee_in, fee_type_id_in); -END// -DELIMITER ; +VALUES (invoiceNum, maxlineItemNo, POLineItemNum, serviceTypeId, description, qty_in, fee_in, fee_type_id_in); +END; -- Dumping structure for procedure astute.create_po DELIMITER // @@ -90,12 +106,19 @@ VALUES (next_po_id, PONum, contractNum, PODate, contractAmt, customerId,astute_p END// DELIMITER ; -DROP PROCEDURE IF EXISTS astute.create_po_detail; CREATE PROCEDURE astute.`create_po_detail`(PONum varchar(40), lineItemNo int, servicedesc varchar(500), feetypeid int(11), quantity double, fee_in double, servicetypeid int(1), remaining_qty double) BEGIN declare maxlineItemNo int; +DECLARE podetailCount int; + +SELECT COUNT(customer_contact_id) INTO podetailCount FROM customer_contact WHERE customer_id = customer_id_in; + if lineItemNo is null or lineItemNo = 0 then - select max(lineItemNo) into maxlineItemNo from po_detail where po_num = ponum; + if podetailCount > 0 then + select max(lineItemNo) into maxlineItemNo from po_detail where po_num = ponum; + else + SET maxlineItemNo = 1; + end if; else set maxlineItemNo = lineItemNo; end if; @@ -766,4 +789,4 @@ INSERT INTO `user` (`user_id`, `username`, `password`, `first_name`, `middle_nam /*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; -GRANT ALL PRIVILEGES ON *.* TO 'astute_user'@'localhost' IDENTIFIED BY 'password'; +-- GRANT ALL PRIVILEGES ON *.* TO 'astute_user'@'localhost' IDENTIFIED BY 'password';