hi Apex guys.
i am new on apex and need a little help with a bug in my next javascript.
i have a pl/sql anonymous block that display me a list of products , so the user can enter some quantityes on desired ones.
i got another HTML Region that contains the next javascript, in this region i check the quantities selected by user (taking product id and quantity entered), and at the end of page there are 2 buttons.
The first one is for generate runing totals, this works perfect.
The second add the products to the Collection (calling an Ajax Callback process ADD_PRODUCTS) and finally in the same button process, i save the order to the final table on my db, for this last one i call another Ajax Callback Process calles SAVE.
The bug i have is that the process is not saving the entire products on my database, some times for example if i have entered quantities for 10 products, the process only saves me 8 or 7, its a random result.
i have been analyzing the code and seems that the problem is on my ADD_PRODUCTS process, because on my javascript where i call it, i displayed on the screen , the quantity and runnign total of the order and it shows perfect, always.
I am using Application Express 4.2.2.00.11 on DB SE
next is :
1.- Javascript
2.- ADD_PRODUCTS ajax
3.- SAVE Ajax
thanks for any tips,
mj
-------
1. JavaScript
-------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Totalizar</title>
<script type="text/javascript">
$(function () {
$("#Calculate").click(
// mueve el valor al campo de cantidad
function () {
var total = 0;
var total_qty = 0;
$("input[name=f_qty]").each(
function () {
var quantity = $(this).val();
var productId = $(this).parents('tr').find("input[name=f_prod_id]").val();
if (quantity !== '') {
var price = $(this).parents('tr').find("input[name=f_unit_price]").val();
var prodtotal = (quantity * price);
total = total + prodtotal;
total_qty = total_qty + 1;
}
}
);
$('#printHere').html(decimalizer(total));
$('#printHereqty').html(total_qty);
}
);
// funcion para formatear el rotulo de resultado a 2 decimales
function decimalizer(input) {
var test1 = 0;
var test2 = 0;
input = Math.round(input * 100);
if (input / 100 == Math.round(input / 100)) {
test1 = 1;
} else if (input / 10 == Math.round(input / 10)) {
test2 = 1;
}
if (test1 == 1 && test2 == 0) {
return (input / 100 + ".00");
} else if (test1 == 0 && test2 == 1) {
return (input / 100 + "0");
} else {
return (input / 100);
}
}
//---------------------------------------Inicia Proceso de aplicacion del pedido----------------------------------------
// Aplica el Pedido
$("#Save").click(
// ----- Agregar los productos a la collection
function () {
$("input[name=f_qty]").each(
function () {
var quantity = $(this).val();
var productId = $(this).parents('tr').find("input[name=f_prod_id]").val();
if (quantity !== '') {
$("#P1_CANTIDAD_PEDIDO").val(quantity);
$("#P1_PRODUCT_ID").val(productId);
apex.server.process("ADD_PRODUCTS", {
pageItems: "#P1_CANTIDAD_PEDIDO,#P1_PRODUCT_ID"
});
}
}
);
placeorder();
});
//---------- Fin de agregar los productos a la Collection -------------
function placeorder() {
apex.server.process("SAVE", {pageItems: "#P1_CODIGO_CLIENTE,#P1_TIPO_CLIENTE"});
var total = 0;
var total_qty = 0;
alert('Orden Creada');
// Poner en 0 las cantidades digitadas
$("input[name=f_qty]").each(
function () {
var quantity = $(this).val();
if (quantity !== '') {
$(this).val('');
}
}
);
$('#printHere').html((total));
$('#printHereqty').html(total_qty);
}
});
</script>
</head>
<body>
<div id="totals"></div>
<p>
<strong>ORDER TOTAL:
<span id="printHere"></span>
</strong>
</p>
<p>
<strong>ORDER PRODUCTS:
<span id="printHereqty"></span>
</strong>
</p>
<p align="center" style="clear: both;">
<button type="button" style="font-weight: bold;background-color:lightgray;margin-left:auto;margin-right:auto;display:block;margin-top:0%;margin-bottom:0%" id="Calculate">Refesh Totals</button>
</p>
<p align="left" style="clear: both;">
<button type="button" style="font-weight: bold;background-color:lightgray;margin-left:auto;margin-right:auto;display:block;margin-top:5%;margin-bottom:5%" id="Save">Place Order</button>
</p>
</body>
</html>
-------
2. ADD_PRODUCTS
-------
begin
apex_collection.add_member(p_collection_name => 'ORDER',
p_c001 => :P1_PRODUCT_ID,
p_c003 => sib_precio_cte_prod('001', '1776', :P1_PRODUCT_ID),
p_c004 => :P1_CANTIDAD_PEDIDO
);
END;
-------
3. SAVE
-------
declare
l_order_id number;
l_count number;
l_customer_id varchar2(30) := :P1_CODIGO_CLIENTE;
begin
-- Insert a row into the Order Header table
insert into cab_orders(customer_id, order_total, order_timestamp, user_name)
values(l_customer_id, null, systimestamp, upper(:APP_USER)) returning order_id into l_order_id;
commit;
insert into det_order(order_item_id, order_id, product_id, unit_price, quantity)
select null, l_order_id, to_number(c001), to_number(c003),to_number(c004) from apex_collections where collection_name = 'ORDER';
commit;
-- Set the item P1_numero_orden to the order which was just placed
:P1_numero_orden := l_order_id;
-- Truncate the collection after the order has been placed
apex_collection.create_or_truncate_collection (p_collection_name => 'ORDER');
l_count := APEX_COLLECTION.COLLECTION_MEMBER_COUNT( p_collection_name => 'ORDER');
:P1_cantidad_registros := l_count;
end;