Untitled

 avatar
unknown
java
2 years ago
10 kB
7
Indexable
package serviceevent;//Panel_RMA___Zaznaczenie_kosztów_wysyłki_w_zależności_od_kraju.groovy

import java.text.DecimalFormat

import groovy.transform.Field
import pl.com.stream.lib.commons.math.Decimal
import pl.com.stream.next.asen.common.groovy.api.common.GroovyQuery
import pl.com.stream.next.asen.server.groovy.helpers.GroovySQLQueryTypeConverter
import pl.com.stream.panel.verto.rma.server.pub.salecomplaintdocumentrma.SaleComplaintDocumentRmaController
import pl.com.stream.panel.verto.rma.server.pub.salecomplaintdocumentrma.SaleComplaintDocumentRmaDto
import pl.com.stream.panel.verto.rma.server.pub.verificationitem.SaleComplaintVerificationItemController
import pl.com.stream.panel.verto.rma.server.pub.verificationitem.SaleComplaintVerificationItemDto
import pl.com.stream.panel.verto.rma.webservice.salecomplaintdocumentrma.pojo.common.VerificationItemData

List<Object> data = event.getMethodParameters();

SaleComplaintDocumentRmaDto saleCompDocRmaDto = data.get(0);
SaleComplaintDocumentRmaController saleCompDocController = context.getService(SaleComplaintDocumentRmaController.class);

SaleComplaintVerificationItemController verificationItemController = context.getService(SaleComplaintVerificationItemController.class)
SaleComplaintVerificationItemDto verificationItemDto;

Long idSaleComplaintDocumentRma = saleCompDocRmaDto.idSaleComplaintDocumentRma;
Long idSaleComplaintDocument = saleCompDocRmaDto.idSaleComplaintDocument;

@Field
Long single = 13488447L;

// ##############################################################################
// ##############################################################################

// Metoda do wyszukania pozycji na panelu RMA
Long findShippingCostOnVerificationItemList(Long idSaleComplaintDocumentRma) {
    GroovyQuery findShippingCost = database.createSQLQuery("""
        SELECT
            scviv.id_sale_comp_verification_item
        FROM
            rma.sale_comp_verification_item_v scviv
            JOIN good_v gv ON scviv.id_good = gv.id_good
            JOIN good_data_v gdv ON gdv.id_good_data = gv.id_good_data 
        WHERE
            scviv.id_sale_complaint_document_rma = :idSaleComplaintDocumentRma AND gdv.long_name = 'Koszty wysyłki'
    """);
    findShippingCost.setParameter("idSaleComplaintDocumentRma", idSaleComplaintDocumentRma as Long);
    return GroovySQLQueryTypeConverter.convertOut(findShippingCost.getFirstRow());
}

// Sprawdz czy zamówienie splitowe
Boolean isSplitOrder(Long idSaleComplaintDocument) {
    GroovyQuery checkIfSplit = database.createSQLQuery("""
        SELECT
             count(scd.id_sale_document)
        FROM
            sale_connected_documents scd
        WHERE
            scd.connected_doc_id = :idSaleComplaintDocument AND scd.id_doc_def_type IN (1)
    """);
    checkIfSplit.setParameter("idSaleComplaintDocument", idSaleComplaintDocument as Long);
    Long saleDocumentCount = checkIfSplit.getFirstRow();
    if (saleDocumentCount > 1) {
        return true;
    }
    return false;
}

// Pobierz informacje na temat zwrotu kosztów z tabeli radowej
List<Object> getShippingCostData(Long idSaleComplaintDocument) {
    GroovyQuery getData = database.createSQLQuery("""
            SELECT
            scv.is_refundable,
            scv.lowest_cost,
            scv.is_lowest_cost,
            scv.is_full_order_requied,
            scv.id_currency,
            scv.has_notes,
            scv.notes
        FROM
            sale_connected_documents scd
        JOIN sale_document_v sdv ON
            sdv.id_sale_document = scd.id_sale_document
        JOIN r119_rad_koszty_wysyłki.shipping_costs_v scv ON
            scv.id_country = sdv.id_country_of_issue
        WHERE
            scd.connected_doc_id = :idSaleComplaintDocument
    """);
    getData.setParameter("idSaleComplaintDocument", idSaleComplaintDocument as Long);
    return GroovySQLQueryTypeConverter.convertOut(getData.getListResult());
}

// Sprawdz czy wszystkie przedmioty zostały zwrócone
Boolean areAllItemsReturned(Long idSaleComplaintDocument) {
    GroovyQuery getIdSaleItemFromVerificationItemList = database.createSQLQuery("""
        SELECT
            scviv.id_sale_document_item
        FROM
            sale_complaint_item_v sciv
        JOIN rma.sale_comp_verification_item_v scviv ON
            sciv.id_sale_complaint_item = scviv.id_sale_complaint_item
        WHERE
            sciv.id_sale_complaint_document = :idSaleComplaintDocument
    """);
    getIdSaleItemFromVerificationItemList.setParameter("idSaleComplaintDocument", idSaleComplaintDocument as Long);
    List<Long> idSaleDocuemntItemFromVerificationItemList = GroovySQLQueryTypeConverter.convertOut(getIdSaleItemFromVerificationItemList.getListResult());
    
    GroovyQuery getIdSaleItemsFromSaleDocument = database.createSQLQuery("""
        SELECT
            sdiv.id_sale_document_item
        FROM
            sale_connected_documents scd
        JOIN sale_document_item_v sdiv ON
            sdiv.id_sale_document = scd.id_sale_document
        WHERE
            scd.connected_doc_id = :idSaleComplaintDocument
            AND sdiv.id_good = 165601
    """);
    getIdSaleItemsFromSaleDocument.setParameter("idSaleComplaintDocument", idSaleComplaintDocument as Long)
    List<Long> idSaleDocumentItemList = GroovySQLQueryTypeConverter.convertOut(getIdSaleItemsFromSaleDocument.getListResult());
    
    throw new Exception("Lista zweryfikowanych: " + idSaleDocuemntItemFromVerificationItemList.toString() + "------------------- Lista z dokumentu sprzedaży: " + idSaleDocumentItemList.toString());
    
    return idSaleDocumentItemList.retainAll(idSaleDocuemntItemFromVerificationItemList);
    
}
// Pobierz oryginalny koszt dostawy z dokumentu sprzedaży
Decimal getShippingCostValueFromSaleDocument(Long idSaleCompVerificationItem) {
    GroovyQuery getShippingCostValue = database.createSQLQuery("""
        SELECT
            sdiqv.gross_value_after_allowance 
        FROM
            rma.sale_comp_verification_item_v scviv
        JOIN verto.sale_document_item_quota_v sdiqv ON
            sdiqv.id_sale_document_item = scviv.id_sale_document_item
        WHERE
            scviv.id_sale_comp_verification_item = :idSaleCompVerificationItem
    """);
    getShippingCostValue.setParameter("idSaleCompVerificationItem", idSaleCompVerificationItem as Long);
    return GroovySQLQueryTypeConverter.convertOut(getShippingCostValue.getFirstRow());
}

Boolean isSingleOrder(Long idSaleCompVerificationItem) {
    GroovyQuery checkIfSingleOrder = database.createSQLQuery("""
        SELECT
            bdiv.id_business_dictionary_item 
        FROM
            rma.sale_comp_verification_item_v scviv
        JOIN sale_document_item_v sdiv ON
            sdiv.id_sale_document_item = scviv.id_sale_document_item
        JOIN sale_document_v sdv ON
            sdv.id_sale_document = sdiv.id_sale_document
        JOIN attribute_value_v avv ON
            avv.id_attribute_subject = sdv.id_attribute_subject
            AND avv.id_attribute_definition = 102406
        JOIN sale_order_document_v sodv ON
            sodv.document_number = avv.string_value
        JOIN attribute_value_v avv2 ON
            avv2.id_attribute_subject = sodv.id_attribute_subject
            AND avv2.id_attribute_definition = 106203
        JOIN business_dictionary_item_v bdiv ON
            bdiv.id_business_dictionary_item = avv2.id_business_dictionary_item
        WHERE
            scviv.id_sale_comp_verification_item = :idSaleCompVerificationItem
    """);
    checkIfSingleOrder.setParameter("idSaleCompVerificationItem", idSaleCompVerificationItem as Long);
    Long idOrderType = GroovySQLQueryTypeConverter.convertOut(checkIfSingleOrder.getFirstRow());
    if (idOrderType == single) {
        return true;
    }
    return false;

}
// ################################################################################
// ##########################         MAIN()       ################################
// ################################################################################

// Jeżeli zamówienie splitowe, pomijamy
if (isSplitOrder(idSaleComplaintDocument)) {
    return;
}

List<Object> shippingCostData = getShippingCostData(idSaleComplaintDocument);

// Brak zwrotu środków
if (!shippingCostData[0]) {
    return;
}

// Czy zwrócono wszystkie przedmioty
Boolean allItemsReturned = areAllItemsReturned(idSaleComplaintDocument);

// id pozycji kosztów sprzedaży
Long idShippingCost = findShippingCostOnVerificationItemList(idSaleComplaintDocument);

// Jeżeli nie jest zwracany najnizszy koszt wysylki podany na sztywno oraz zostało zwrócone całe zamówienie
if (shippingCostData[2]  == false && allItemsReturned == false) {
    throw new Exception("Halo francja");
    verificationItemDto = verificationItemController.find(idShippingCost);
    verificationItemDto.verified = true;
    verificationItemController.update(verificationItemDto);
}

// Jeżeli jest zwracany koszt przesyłki podany na sztyno i zwrócono całe zamówienie oraz nie posiada notatki
if (shippingCostData[2] && !allItemsReturned && !shippingCostData[5]) {
    Decimal lowestCost = Decimal.from(shippingCostData[1]);
    Decimal costFromSaleDocument = getShippingCostValueFromSaleDocument(idShippingCost);
    Decimal valueToDecrease = costFromSaleDocument.subtract(lowestCost);
    
    verificationItemDto = verificationItemController.find(idShippingCost);
    verificationItemDto.valueToDecrease = valueToDecrease;
    verificationItemDto.verified = true;
    verificationItemController.update(verificationItemDto);
}

// Jeżeli jest zwracany koszt tylko jężeli zwrócono całe zamówienie i jest dpowiednia notatka
if (shippingCostData[2] && !allItemsReturned) {
    // Jest Notatka
    if (shippingCostData[5]) {
        // Notatka "Single" oraz Zamówienie posiada ceche single
        if (shippingCostData[6] == "Single" && isSingleOrder(idShippingCost)) {
            verificationItemDto = verificationItemController.find(idShippingCost);
            verificationItemDto.verified = true;
            verificationItemController.update(verificationItemDto);
        }
    }
}
Editor is loading...