diff --git a/AstuteSystem/sql/astute.sql b/AstuteSystem/sql/astute.sql index 29a07ec..2b7aa27 100644 --- a/AstuteSystem/sql/astute.sql +++ b/AstuteSystem/sql/astute.sql @@ -395,14 +395,30 @@ BEGIN END// DELIMITER ; +CREATE FUNCTION astute.`submit_invoice_fun`(invNo varchar(20)) RETURNS varchar(20) CHARSET utf8 +BEGIN +DECLARE po_no varchar(20); +declare new_invoice_no varchar(20); +-- UPDATE INVOICE SET INV_STATUS = 2 WHERE INV_NO = invNo; +SELECT PO_NUM INTO po_no FROM INVOICE WHERE INV_NO = invNo; +SET new_invoice_no = generate_final_inv_number(po_no); +UPDATE INVOICE SET INV_NO = new_invoice_no, INV_STATUS = 2, submitted_date_time = current_timestamp() WHERE INV_NO = invNo; +UPDATE PO SET INV_SEQ = INV_SEQ + 1 WHERE PO_NUM = po_no; +RETURN new_invoice_no; +END; + + -- Dumping structure for function astute.get_previously_billed_amt DELIMITER // -CREATE DEFINER=`root`@`localhost` FUNCTION `get_previously_billed_amt`(po_no varchar(20)) RETURNS double(10,2) +CREATE FUNCTION astute.`get_previously_billed_amt`(po_no varchar(20), inv_no_in varchar(20)) RETURNS double(10,2) BEGIN declare billed_amt double(10,2); - SELECT sum(bill_amt) INTO billed_amt FROM invoice WHERE invoice.PO_num = po_no and inv_status <> 3; + declare submitted_datetime timestamp; + SELECT submitted_date_time INTO submitted_datetime FROM INVOICE WHERE invoice.inv_no = inv_no_in; + SELECT ifnull(sum(bill_amt),0) INTO billed_amt FROM invoice WHERE invoice.PO_num = po_no and inv_status = 2 and submitted_date_time < submitted_datetime ; return billed_amt; -END// +END; + DELIMITER ; -- Dumping structure for table astute.invoice @@ -730,29 +746,16 @@ DELIMITER ; -- Dumping structure for procedure astute.update_all_remaining_quantities DELIMITER // -CREATE DEFINER=`root`@`localhost` PROCEDURE `update_all_remaining_quantities`(invNo varchar(20)) +DROP PROCEDURE IF EXISTS astute.update_all_remaining_quantities; +CREATE PROCEDURE astute.`update_all_remaining_quantities`(invNo varchar(20)) BEGIN -DECLARE po_line_item_no int; -DECLARE remaining_qty double; -DECLARE finished INTEGER DEFAULT 0; -DECLARE p_finished INTEGER DEFAULT 0; -DECLARE done boolean DEFAULT FALSE; +DECLARE po_num_in VARCHAR(20); + +SELECT PO_NUM INTO po_num_in FROM invoice where inv_no = invNo; +UPDATE PO_DETAIL SET remaining_qty = get_remaining_qty_fun(po_num_in,'',line_item_no); +END; -DECLARE po_line_items CURSOR FOR select distinct po_line_item_num from invoice_detail where inv_num = invNo; -DECLARE CONTINUE HANDLER FOR NOT FOUND SET p_finished = 1; - OPEN po_line_items; - po_loop: LOOP - FETCH po_line_items INTO po_line_item_no; - IF p_finished = 1 THEN - LEAVE po_loop; - END IF; - BEGIN - SET remaining_qty = update_remaining_qty_fun('',invNo,po_line_item_no); - END; - END LOOP po_loop; - CLOSE po_line_items; -END// DELIMITER ; CREATE FUNCTION astute.`get_draft_remaining_qty_fun`(inv_num_in varchar(40), item_no_in int) RETURNS double @@ -771,28 +774,28 @@ END; -- Dumping structure for function astute.update_remaining_qty_fun DELIMITER // -CREATE DEFINER=`root`@`localhost` FUNCTION `update_remaining_qty_fun`(po_no_in varchar(40), inv_num_in varchar(40), item_no_in int) RETURNS double +CREATE FUNCTION astute.`update_remaining_qty_fun`(po_no_in varchar(40), inv_num_in varchar(40), item_no_in int) RETURNS double BEGIN DECLARE rem_qty double; DECLARE po_no varchar(40); if inv_num_in <> null or inv_num_in <> '' THEN SELECT po_num INTO po_no FROM invoice where inv_no = inv_num_in; -else +else set po_no = po_no_in; end if; -select po_detail.qty - ifnull(sum(invoice_detail.qty),0) into rem_qty from invoice_detail, invoice, po_detail +select po_detail.qty - ifnull(sum(invoice_detail.qty),0) into rem_qty from invoice_detail, invoice, po_detail where invoice_detail.inv_num in (select inv_no from invoice where invoice.PO_num = po_no) and invoice_detail.po_line_item_num = item_no_in and po_detail.PO_num = po_no and invoice.PO_num = po_detail.PO_num and invoice.inv_no = invoice_detail.inv_num -and invoice.inv_status = 2 +and (invoice.inv_status = 2) and invoice_detail.po_line_item_num = po_detail.line_item_no; update po_detail set remaining_qty = rem_qty where PO_num = po_no and line_item_no = item_no_in; return rem_qty; -END// +END; DELIMITER ; CREATE FUNCTION astute.`is_po_fulfilled`(po_no varchar(20)) RETURNS int(1) diff --git a/AstuteSystem/src/main/java/com/astute/dao/SqlDAO.java b/AstuteSystem/src/main/java/com/astute/dao/SqlDAO.java index e36afd7..bb422f8 100644 --- a/AstuteSystem/src/main/java/com/astute/dao/SqlDAO.java +++ b/AstuteSystem/src/main/java/com/astute/dao/SqlDAO.java @@ -43,7 +43,7 @@ public class SqlDAO extends DAO { try { List pos = new ArrayList<>(); Statement stmt = conn.createStatement(); - String sql = "SELECT PO_num, contract_num, PO_date, customer_id, contract_amt, astute_project_num , title, get_previously_billed_amt(PO_num), inv_seq, notes, final, isAnyInvInDraft(PO_num), is_po_fulfilled(PO_num) as fulfilled FROM PO "; + String sql = "SELECT PO_num, contract_num, PO_date, customer_id, contract_amt, astute_project_num , title, 0, inv_seq, notes, final, isAnyInvInDraft(PO_num), is_po_fulfilled(PO_num) as fulfilled FROM PO "; if (PONum != null && !PONum.isEmpty()) { sql += "WHERE UPPER(PO_num) = '" + PONum.toUpperCase() + "'"; } else if (contractNum != null && !contractNum.isEmpty()) { @@ -408,16 +408,18 @@ public class SqlDAO extends DAO { =============================== Invoice Methods =============================================== */ - public Double getPreviouslyBilledAmount(String poNum) throws AstuteException { + public Double getPreviouslyBilledAmount(String poNum, String invoiceNumber) throws AstuteException { try { - Double billedAmt = new Double(0); - Statement stmt = conn.createStatement(); - String sql = "SELECT sum(bill_amt) FROM invoice WHERE invoice.PO_num = '" + poNum + "'"; - ResultSet rs = stmt.executeQuery(sql); - while (rs.next()) { - billedAmt = rs.getDouble(1); - } - return billedAmt; + // TODO remainingQuantity not used, need to take it out from signature + System.out.println("Calling create_po_detail Procedure"); + double billAmt=0.0; + CallableStatement stmt = conn.prepareCall("{call get_previously_billed_amt(?,?)}"); + stmt.registerOutParameter(1,Types.DOUBLE); + stmt.setString(2, poNum); + stmt.setString(3, invoiceNumber); + stmt.execute(); + billAmt = stmt.getDouble(1); + return billAmt; } catch (SQLException e) { e.printStackTrace(); throw new AstuteException(DB_ERROR,e.getMessage()); @@ -486,7 +488,7 @@ public class SqlDAO extends DAO { String customerId = po.getCustomerId(); Customer customer = getCustomers(customerId).get(0); - Double previouslyBilledAmt = getPreviouslyBilledAmount(PONo); + Double previouslyBilledAmt = getPreviouslyBilledAmount(PONo,invoiceNumber); if (invoice.getInvoiceStatus()==2) { // TODO subtract current invoice's bill amount from previouslyBilledAmt @@ -795,11 +797,14 @@ public class SqlDAO extends DAO { public void submitInvoice(String InvoiceNumber) throws AstuteException { try { - System.out.println("Calling submit_invoice Procedure "); + System.out.println("Calling submit_invoice_fun Procedure "); System.out.println("invoiceNum is "+InvoiceNumber); - CallableStatement stmt = conn.prepareCall("{call submit_invoice(?)}"); - stmt.setString(1, InvoiceNumber); - stmt.executeUpdate(); + CallableStatement stmt = conn.prepareCall("{? = call submit_invoice_fun(?)}"); + stmt.registerOutParameter(1,Types.VARCHAR); + stmt.setString(2, InvoiceNumber); + stmt.execute(); + String newInvoiceNumber = stmt.getString(1); + updateAllRemainingQuantities(newInvoiceNumber); } catch (SQLException e) { e.printStackTrace(); throw new AstuteException(DB_ERROR,e.getMessage());