package it.integry.rules import it.integry.ems_model.entity.* import it.integry.ems_model.types.OperationType import it.integry.ems.rules.util.* import it.integry.ems.rules.completing.* import it.integry.ems.sync.MultiDBTransaction.Connection; import java.util.Date import java.math.BigDecimal import java.math.RoundingMode import java.util.* import it.integry.ems_model.utility.UtilityString import it.integry.ems_model.utility.UtilityDB import it.integry.ems_model.entity.MtbCols import it.integry.ems_model.entity.MtbColt import it.integry.ems_model.entity.MtbColr import java.time.LocalDate import it.integry.ems_model.utility.UtilityLocalDate; global Connection conn global String username global Boolean completeRulesEnabled global Boolean checkRulesEnabled global Boolean postRulesEnabled //PACKAGES TESTATE rule "completeCodVlis" no-loop salience 10 when eval(completeRulesEnabled) $packages: MtbColt(codVlis == null && codAnag != null && gestione == "V") then String codVlis = PackagesRules.completeCodVlis(conn, $packages.getCodAnag(), $packages.getCodVdes()); modify ( $packages ) { setCodVlis(codVlis) } end rule "completeDimesioneCollo" no-loop salience 10 when eval(completeRulesEnabled) $packages: MtbColt(codTcol != null && (altezzaCm == null || lunghezzaCm == null || larghezzaCm == null || altezzaCm == BigDecimal.ZERO || lunghezzaCm == BigDecimal.ZERO || larghezzaCm == BigDecimal.ZERO )) then HashMap dati = PackagesRules.completeDimensioniCollo(conn, $packages.getCodTcol()); modify ( $packages ) { setAltezzaCm(QueryRules.getMapData(dati, $packages.getAltezzaCm(), "altezza_cm")), setLunghezzaCm(QueryRules.getMapData(dati, $packages.getLunghezzaCm(), "lunghezza_cm")), setLarghezzaCm(QueryRules.getMapData(dati, $packages.getLarghezzaCm(), "larghezza_cm")) } end rule "completeSegno" salience 10 when eval(completeRulesEnabled) $packages: MtbColt(segno == null && gestione != null) then Integer segno = PackagesRules.completeSegno($packages.getGestione()); modify ( $packages ) { setSegno(segno) } end rule "createMtbColsOnMtbColtInsert" no-loop when eval(completeRulesEnabled) $mtbColt: MtbColt((operation == OperationType.INSERT || operation == OperationType.INSERT_OR_UPDATE) && segno == 1) then PackagesRules.insertMtbCols($mtbColt, MtbCols.Causale.CREAZIONE); end rule "createMtbColsOnMtbColtPositionChanged" no-loop when eval(completeRulesEnabled) $mtbColt: MtbColt((operation == OperationType.INSERT_OR_UPDATE || operation == OperationType.UPDATE) && segno == 1) eval(PackagesRules.isPositionChanged(conn, $mtbColt)) then PackagesRules.insertMtbCols($mtbColt, MtbCols.Causale.POSIZIONAMENTO); end rule "completeFlagInsPartitaMag" when eval(completeRulesEnabled) $packages: MtbColt(insPartitaMag == null) then String flagInsPartita = PackagesRules.completeFlagInsPartitaMag(conn, $packages.getGestione()); modify ( $packages ) { setInsPartitaMag(flagInsPartita) } end rule "completeSerCollo" when eval(completeRulesEnabled) $packages: MtbColt(serCollo == null) then String serCollo = PackagesRules.getSerCollo(conn, $packages.getCodMdep()); modify ( $packages ) { setSerCollo(serCollo) } end rule "completeDataCollo" when eval(completeRulesEnabled) $packages: MtbColt(dataCollo == null) then modify ( $packages ) { setDataCollo(UtilityLocalDate.getNow()) } end rule "completeNumCollo" salience 9 when eval(completeRulesEnabled) $packages: MtbColt((numCollo == null || numCollo == 0) && serCollo != null && dataCollo != null && gestione != null) then Integer numCollo = PackagesRules.completeNumCollo(conn, $packages.getSerCollo(), $packages.getDataCollo(), $packages.getGestione()); modify ( $packages ) { setNumCollo(numCollo) } end rule "completePreparatoDa" no-loop when eval(completeRulesEnabled) $mtbColt : MtbColt(preparatoDa == null && operation != OperationType.DELETE) then modify($mtbColt){setPreparatoDa(username)} end //Set calcPeso rule "setCalcPeso" no-loop when eval(completeRulesEnabled) $mtbColt : MtbColt(pesoKg != null && pesoKg != 0 && operation != OperationType.NO_OP && !updateColliFromDoc) then modify($mtbColt){ setCalcPeso(false), setExecuteRecalc(true) } end rule "setDatiOrdMtbColt" no-loop when eval(completeRulesEnabled) $mtbColt : MtbColt(dataOrd == null || numOrd == null || rifOrd == null) then PackagesRules.setDatiOrdMtbColt(conn, $mtbColt); end //PARTITA MAG rule "completeQtaCnfPartitaMag" when eval(completeRulesEnabled) $entity : MtbPartitaMag(qtaCnf == null && codMart != null) then String codMart = $entity.getCodMart(); String sql = "SELECT qta_cnf FROM mtb_aart WHERE cod_mart = " + UtilityDB.valueToString(codMart); BigDecimal qtaCnf = (BigDecimal) QueryRules.getSingleValue(conn, sql); if (qtaCnf == null) { qtaCnf = BigDecimal.ONE; } modify ( $entity ) { setQtaCnf(qtaCnf) } end rule "completeCodDiviContPartitaMag" no-loop when eval(completeRulesEnabled) $partitaMag: MtbPartitaMag(codDiviCont == null || cambioDiviCont == null) then HashMap map = CommonRules.completeCodDivi(conn); modify ( $partitaMag ) { setCodDiviCont(UtilityHashMap.getValueIfExists(map, "cod_divi_contab")), setCambioDiviCont( new BigDecimal(QueryRules.getMapData(map, $partitaMag.getCambioDiviCont(), "cambio").toString())) } end rule "completeRapConv2e3Partita" no-loop when eval(completeRulesEnabled) $entity: MtbPartitaMag((rapConv2 == null || rapConv3 == null) && codMart != null) then HashMap mapDati = PartitaMagRules.completeRapConv2e3(conn, $entity.getCodMart()); BigDecimal rapConv2 = QueryRules.getMapData(mapDati, $entity.getRapConv2(), "rap_conv2"); BigDecimal rapConv3 = QueryRules.getMapData(mapDati, $entity.getRapConv3(), "rap_conv3"); modify ( $entity ) { setRapConv2(rapConv2), setRapConv3(rapConv3) } end rule "checkProgArt" no-loop when eval(completeRulesEnabled) $lotto: MtbPartitaMag(operation == OperationType.DELETE) then PartitaMagRules.deleteProgArt(conn,$lotto); end //PACKAGES RIGHE rule "completeQtaCnfMtbColr" when eval(completeRulesEnabled) $entity : MtbColr(qtaCnf == null && codMart != null) then BigDecimal qtaCnf = CommonRules.completeQtaCnf(conn, $entity); modify ( $entity ) { setQtaCnf(qtaCnf) } end //PACKAGES RIGHE rule "completeArtAPeso" when eval(completeRulesEnabled) $entity : MtbColr(artAPeso == null && codMart != null ) then String sql = "SELECT CASE WHEN dbo.f_getTipoUntMis(mtb_aart.unt_mis) = 'KG' THEN 'S' ELSE 'N' END " + " FROM mtb_aart WHERE cod_mart = " + UtilityDB.valueToString($entity.getCodMart()); String artAPeso = (String) QueryRules.getSingleValue(conn, sql); if (artAPeso == null ) { artAPeso = "N"; } modify ( $entity ) { setArtAPeso(artAPeso) } end //PACKAGES RIGHE rule "completeNumCnfMtbColr" no-loop when eval(completeRulesEnabled) $entity : MtbColr(numCnf == null && qtaCnf != null && codMart != null) then BigDecimal numCnf = PackagesRules.completeNumCnf(conn, $entity); modify ( $entity ) { setNumCnf(numCnf) } end rule "completeQtaColMtbColr" no-loop when eval(completeRulesEnabled) $entity : MtbColr((qtaCol == null || qtaCol == 0) && qtaCnf != null && flagQtaCnfFissa == "S" && numCnf != null && codMart != null) then BigDecimal qtaCol = (BigDecimal) ($entity.getQtaCnf().compareTo(BigDecimal.ZERO) > 0? $entity.getQtaCnf().multiply($entity.getNumCnf()) :$entity.getNumCnf()); modify ( $entity ) { setQtaCol(qtaCol) } end rule "completeDescPartitaMag" when eval(completeRulesEnabled) $row: MtbColr(mtbPartitaMag != null && mtbPartitaMag_descrizione != null) then PackagesRules.completeDescPartitaMag($row.getMtbPartitaMag(), $row.getMtbPartitaMag_descrizione()); end rule "completePartitaMag4DataScad" no-loop when eval(completeRulesEnabled) $row: MtbColr(partitaMag == null && codMart != null && dataScadPartita != null) then String partitaMag = PackagesRules.completePartitaMag4DataScad(conn, $row.getCodMart(),$row.getDataScadPartita()); modify ( $row ) { setPartitaMag(partitaMag) } end rule "completePartitaMag" no-loop when eval(completeRulesEnabled) $row: MtbColr(mtbPartitaMag == null && (gestione != "V" || insPartitaMag == "S") && partitaMag != null && partitaMag != "[NULL]") then MtbPartitaMag mtbPartitaMag = PackagesRules.completeMtbPartitaMag(conn, $row); modify ( $row ) { setMtbPartitaMag(mtbPartitaMag) } end rule "completeFlagQtaCnfFissa" no-loop when eval(completeRulesEnabled) $row: MtbColr(flagQtaCnfFissa == null && codMart != null) then String flagQtaCnfFissa = CommonRules.completeFlagQtaCnfFissa(conn, $row.getCodMart()); modify ( $row ) { setFlagQtaCnfFissa(flagQtaCnfFissa) } end rule "completeBarCodeColli" no-loop when eval(completeRulesEnabled) $row: MtbColr(codBarre == null && codMart != null) then String codBarre = CommonRules.completeCodBarre(conn, $row.getCodMart()); modify ( $row ) { setCodBarre(codBarre) } end rule "completeDatiTcol" no-loop when eval(completeRulesEnabled) $entity: MtbColt((pesoKg == null || pesoKg == 0) && codTcol != null) then String sql = "SELECT peso_kg, lunghezza_cm, larghezza_cm, altezza_cm FROM mtb_tcol WHERE cod_tcol = '" + $entity.getCodTcol() + "'"; HashMap mapDati = QueryRules.execSingleQuery(conn, sql); modify ( $entity ) { setPesoKg(QueryRules.getMapData(mapDati, $entity.getPesoKg(), "peso_kg")), setLunghezzaCm(QueryRules.getMapData(mapDati, $entity.getLunghezzaCm(), "lunghezza_cm")), setLarghezzaCm(QueryRules.getMapData(mapDati, $entity.getLarghezzaCm(), "larghezza_cm")), setAltezzaCm(QueryRules.getMapData(mapDati, $entity.getAltezzaCm(), "altezza_cm")) } end rule "completeCodJcomColli" no-loop when eval(completeRulesEnabled) $entity: MtbColr(codJcom == null && (gestione.equalsIgnoreCase("A") || gestione.equalsIgnoreCase("L")) && segno == 1 && dataOrd != null && numOrd != null && rigaOrd != null) then String codJcom = PackagesRules.completeCodJcom(conn, $entity); modify ( $entity ) { setCodJcom(codJcom) } end rule "completDatiOrdineCollo" no-loop when eval(completeRulesEnabled) $entity: MtbColt((codAnag == null || codVdes == null || rifOrd ==null) && ((Objects.equals(gestione, "A") && segno == 1) || (Objects.equals(gestione, "L")) || (Objects.equals(gestione, "V") && segno == -1)) && dataOrd != null && numOrd != null && operation != OperationType.DELETE) then HashMapdati = PackagesRules.completeDatiOrdine(conn, $entity); modify ( $entity ) { setCodAnag(QueryRules.getMapData(dati, $entity.getCodAnag(), "cod_anag")), setCodVdes(QueryRules.getMapData(dati, $entity.getCodVdes(), "cod_vdes")), setRifOrd(QueryRules.getMapData(dati, $entity.getRifOrd(), "rif_ord")) } end rule "completePesoMtbColr" no-loop when eval(completeRulesEnabled) $entity: MtbColr((pesoNettoKg == null || pesoLordoKg == null) && qtaCol != null && numCnf != null && qtaCnf != null && operation != OperationType.DELETE) then HashMap dati = PackagesRules.calcPeso(conn, $entity); modify ( $entity ) { setPesoNettoKg(QueryRules.getMapData(dati, $entity.getPesoNettoKg(), "peso_netto")), setPesoLordoKg(QueryRules.getMapData(dati, $entity.getPesoLordoKg(), "peso_lordo")) } end rule "completeCodMartMtbColrTx" salience 11 no-loop when eval(completeRulesEnabled) $row: MtbColr(codMart == null && codBarre != null && !CommonRules.barCodeCheck(conn, codBarre) && applicationName == "TEXTILES" ) then String sql ="SELECT cod_style as cod_mart, cod_col, cod_tagl FROM ttb_bar_code WHERE cod_barre = '" + $row.getCodBarre() + "'"; HashMap mapDati = QueryRules.execSingleQuery(conn, sql); modify ( $row ) { setCodMart(QueryRules.getMapData(mapDati, $row.getCodMart(), "cod_mart")), setCodCol(QueryRules.getMapData(mapDati, $row.getCodCol(), "cod_col")), setCodTagl(QueryRules.getMapData(mapDati, $row.getCodTagl(), "cod_tagl")) } end rule "completeDataInsDaColli" no-loop when eval(completeRulesEnabled) $mtbColt: MtbColt(dataIns == null && operation != OperationType.DELETE) then modify ( $mtbColt ) { setDataIns($mtbColt.getDataIns()==null?new Date():$mtbColt.getDataIns()) } end rule "completeModificatoDaColli" no-loop when eval(completeRulesEnabled) $mtbColt: MtbColt(operation != OperationType.DELETE) then modify ( $mtbColt ) { setModificatoDa( username ), setDataUltMod(new Date()) } end rule "completeBarcodeUl" no-loop when eval(completeRulesEnabled) $mtbColt: MtbColt(barcodeUl == null && numCollo != null && (operation == OperationType.INSERT || operation == OperationType.INSERT_OR_UPDATE) ) then String barcode = PackagesRules.completeBarcodeUl(conn, $mtbColt.getGestione(), $mtbColt.getDataCollo(), $mtbColt.getSerCollo(), $mtbColt.getNumCollo(), $mtbColt.getCodJfas(), null); modify ( $mtbColt ) { setBarcodeUl( barcode) } end