Delivery charge: sometimes it works and sometimes it doesn’t

Asked

Viewed 62 times

-3

I have a delivery form that uses a file (form.js) where contain the billing data by zip code, so far so good, my question is, because sometimes it charges and then does not charge, being the same zip code? Example: Some days the CEP 20240-200 he recognizes and charges R $ 4,30 and the other not! Worse, sometimes it has more than one order for the same zip code and it IN THE SAME DAY charges for one and does not charge for another request, as this can happen????

Follow the complete CODE of this file:

 //aqui eu pego o cep
var cep = document.getElementById("cep").value;

//verifica se deve incrementar ou não
if(cep == "20040-010" || cep == "20071-002" || cep == "20090-910" || cep == "20210-010" || cep == "20080-102" || cep == "20230-130" || cep == "20081-250" || cep == "20090-030" || cep == "20090-030" || cep == "20211-340" || cep == "20211-351" || cep == "20221-240" || cep == "20221-250" || cep == "20230-010" || cep == "20230-011" || cep == "20230-025" || cep == "20230-150" || cep == "20230-160" || cep == "20230-240" || cep == "20231-016" || cep == "20231-030" || cep == "20231-031" || cep == "20231-050" || cep == "20211-005" || cep == "20231-085" || cep == "20230-014" || cep == "20231-004" || cep == "20021-180" || cep == "20240-180" || cep == "20240-051" || cep == "20211-010" || cep == "20230-170" || cep == "20230-024" || cep == "20240-050" || cep == "20230-050" || cep == "20231-006" || cep == "20090-010" || cep == "20230-901" || cep == "20040-051" || cep == "20241-080" || cep == "20221-901" || cep == "20051-011" || cep == "20021-190" || cep == "20240-200" || cep == "20230-170" || cep == "20231-015" || cep == "20211-350" || cep == "20231-020" || cep == "20061-030"){

        //se for um dos ceps acima, incrementa 4 no valor final
    taxa = 4.30;


    //verifica se deve incrementar ou não
}if(cep == "20241-220" || cep == "22210-015" || cep == "22220-000" || cep == "20241-160" || cep == "20021-040" || cep == "20241-150" || cep == "22210-010" || cep == "22211-230" || cep == "22220-000" || cep == "22220-070" || cep == "22221-060" || cep == "22211-200" || cep == "22221-020" || cep == "20241-180" || cep == "20240-200"){

        //se for um dos ceps acima, incrementa 5.5 no valor final
    taxa = 6.0;


}if(cep == "20050-091" || cep == "20060-050" || cep == "20071-000" || cep == "20080-003" || cep == "20081-000" || cep == "20081-050" || cep == "20090-000" || cep == "20090-003" || cep == "20231-092" || cep == "24938-600" || cep == "20051-002" || cep == "20090-050" || cep == "20231-046" || cep == "20231-047" || cep == "20231-048" || cep == "20090-001" || cep == "20231-093" || cep == "20051-011" || cep == "20060-010" || cep == "20050-002"){

    //se for um dos ceps acima, incrementa 3.5 no valor final
    taxa = 3.8;


}if(cep == "20031-170" || cep == "20040-004" || cep == "20040-902" | cep == "20021-360" || cep == "20040-020" || cep == "20040-908" || cep == "20071-000" || cep == "20230-070" || cep == "20071-004" || cep == "20040-009" || cep == "20230-060"){

    //se for um dos ceps acima, incrementa 3 no valor final
    taxa = 3.30;

}if(cep == "20010-010" || cep == "20020-906" || cep == "20031-908" || cep == "20031-917" || cep == "20040-001" || cep == "20040-006" || cep == "20050-090" || cep == "20070-022" || cep == "20031-909" || cep == "20031-904" || cep == "20021-390" || cep == "20031-919" || cep == "20031-912" || cep == "20031-905" || cep == "20050-060" || cep == "20031-901" || cep == "20031-924" || cep == "20051-040" || cep == "20031-901" || cep == "20003-191" || cep == "20241-110" || cep == "20050-030"){

    //se for um dos ceps acima, incrementa 2.5 no valor final
    taxa = 2.80;


}if(cep == "20010-020" || cep == "20011-020" || cep == "20011-030" || cep == "20011-040" || cep == "20011-901" || cep == "20020-000" || cep == "20010-170" || cep == "20011-000" || cep == "20021-260" || cep == "20021-350" || cep == "20031-040" || cep == "20031-050" || cep == "20031-130" || cep == "20031-204" || cep == "20021-245" || cep == "20040-002" || cep == "21335-253" || cep == "20040-031" || cep == "20030-041" || cep == "20040-000" || cep == "20004-002" || cep == "20040-003" || cep == "20040-007" || cep == "20011-010"){

    //se for um dos ceps acima, incrementa 1.7 no valor final
    taxa = 2.00;


}if(cep == "20010-090" || cep == "20020-100" || cep == "20021-130" || cep == "20021-315" || cep == "20030-001" || cep == "20031-000" || cep == "20031-003" || cep == "20031-010" || cep == "20031-050" || cep == "20031-141" || cep == "20031-143" || cep == "20031-005" || cep == "20031-001" || cep == "20020-903" || cep == "20031-144" || cep == "20030-080" || cep == "20031-142" || cep == "20031-120" || cep == "20031-007" || cep == "20010-009" || cep == "20031-913" || cep == "20021-370" || cep == "24220-280"){

    //se for um dos ceps acima, incrementa 1.2 no valor final
    taxa = 1.50;


}if(cep == "20020-010" || cep == "20020-040" || cep == "20020-080" || cep == "20021-060" || cep == "20021-120" || cep == "20021-900" || cep == "20021-903" || cep == "20030-002" || cep == "20030-015" || cep == "20030-013" || cep == "20030-020" || cep == "20030-021" || cep == "20030-060" || cep == "20030-070" || cep == "20030-120" || cep == "20002-080" || cep == "20003-021" || cep == "20030-905" || cep == "20030-015"){

    //se for um dos ceps acima, incrementa 0.7 no valor final
    taxa = 1.00;

}

total += taxa;      

if(taxa != 0){
    //caso a taxa seja diferente de 0, mostra ao usuário
    document.getElementById("idTaxa").innerHTML = "Custo adicional: R$ " + taxa;
}

From now on I thank and MUCH the attention of all!!

  • Alessandro will be very difficult for anyone to look at all this code. You can try to isolate the piece that might be giving you trouble and better describe the problem itself?

  • Oi Sergio, the part of the zip code is this:

1 answer

1


I suggest some improvements to this code:

  • You have several Zip Codes to apply the same rate, group these Zip Codes into a data structure, so that it is simpler to understand the code and simpler to manipulate them, I suggest an array or a map in this case.

  • You own several Ifs followed without the use of ELSE. In this case you are losing performance, or even may have a logic error there, given that however much is applied the rate of 4.30 who is in the first IF, the flow of your algorithm will still be checking in ALL the others IFs.

  • In your room if has the following code snippet: cep == "20040-902" | cep == "20021-360". In this case you are using a bit-by-bit operator, where this treats its operands as a set of 32 bits and not the data as a whole, although it does not make much difference in your case given that they will be returned only 1 or 0 and Javascript translates these numbers to true or false, I believe he was not to be there, more details here.

I hope I’ve helped =)

Code with my suggestions:

    var cep = document.getElementById("cep").value;
    var taxa = 0;

    var cepRateMap = (function() {
        var map = new Map();

        map.set(4.30, [
            "20040-010", "20071-002", "20090-910", "20210-010", "20080-102",
            "20230-130", "20081-250", "20090-030", "20090-030", "20211-340",
            "20211-351", "20221-240", "20221-250", "20230-010", "20230-011",
            "20230-025", "20230-150", "20230-160", "20230-240", "20231-016",
            "20231-030", "20231-031", "20231-050", "20211-005", "20231-085",
            "20230-014", "20231-004", "20021-180", "20240-180", "20240-051",
            "20211-010", "20230-170", "20230-024", "20240-050", "20230-050",
            "20231-006", "20090-010", "20230-901", "20040-051", "20241-080",
            "20221-901", "20051-011", "20021-190", "20240-200", "20230-170",
            "20231-015", "20211-350", "20231-020", "20061-030"
        ]);
        map.set(6.0, [
            "20241-220", "22210-015", "22220-000", "20241-160", "20021-040",
            "20241-150", "22210-010", "22211-230", "22220-000", "22220-070",
            "22221-060", "22211-200", "22221-020", "20241-180", "20240-200"
        ]);
        map.set(3.8, [
            "20050-091", "20060-050", "20071-000", "20080-003", "20081-000",
            "20081-050", "20090-000", "20090-003", "20231-092", "24938-600",
            "20051-002", "20090-050", "20231-046", "20231-047", "20231-048",
            "20090-001", "20231-093", "20051-011", "20060-010", "20050-002"
        ]);
        map.set(3.3, [
            "20031-170", "20040-004", "20040-902", "20021-360", "20040-020",
            "20040-908", "20071-000", "20230-070", "20071-004", "20040-009",
            "20230-060"
        ]);
        map.set(2.8, [
            "20010-010", "20020-906", "20031-908", "20031-917", "20040-001",
            "20040-006", "20050-090", "20070-022", "20031-909", "20031-904",
            "20021-390", "20031-919", "20031-912", "20031-905", "20050-060",
            "20031-901", "20031-924", "20051-040", "20031-901", "20003-191",
            "20241-110", "20050-030"
        ]);
        map.set(2.0, [
            "20010-020", "20011-020", "20011-030", "20011-040", "20011-901",
            "20020-000", "20010-170", "20011-000", "20021-260", "20021-350",
            "20031-040", "20031-050", "20031-130", "20031-204", "20021-245",
            "20040-002", "21335-253", "20040-031", "20030-041", "20040-000",
            "20004-002", "20040-003", "20040-007", "20011-010"
        ]);
        map.set(1.5, [
            "20010-090", "20020-100", "20021-130", "20021-315", "20030-001",
            "20031-000", "20031-003", "20031-010", "20031-050", "20031-141",
            "20031-143", "20031-005", "20031-001", "20020-903", "20031-144",
            "20030-080", "20031-142", "20031-120", "20031-007", "20010-009",
            "20031-913", "20021-370", "24220-280"
        ]);
        map.set(1.0, [
            "20020-010", "20020-040", "20020-080", "20021-060", "20021-120",
            "20021-900", "20021-903", "20030-002", "20030-015", "20030-013",
            "20030-020", "20030-021", "20030-060", "20030-070", "20030-120",
            "20002-080", "20003-021", "20030-905", "20030-015"
        ]);

        return map;
    }());

    cepRateMap.forEach(function(values, key) {
        if (values.indexOf(cep) !== -1) {
            taxa = key;
        }
    });

    total += taxa;

    if (taxa != 0) {
        var elementRate = document.getElementById("idTaxa");
        elementRate.innerHTML = "Custo adicional: R$ " + taxa;
    }
  • Pedro Muitoooooooo thank you!!

  • Pedro, painted a problem, do not know why now in the email no longer appears the rate information! $entrega = $_POST["taxadeentrega"]; in the form of the site appears the fee (<tr>&#xA; <td>Taxa de Entrega:</td>&#xA; <td><span id="idTaxa"></span></td>&#xA; </tr>, but in the email that is processed by the mail.php file it is no longer reading, it does not send the information. Is it something in this part of the java code? var taxa = 0;&#xA;var taxadeentrega = total - taxa;&#xA;var total = 0.00;&#xA;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.