Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function script1()
- {
- var bo = Get("BusinessObject");
- if(!bo.Data.Contains("vt_order_detail_delivery")||!bo.Data.Contains("vt_account")||!bo.Data.Contains("vt_order_detail"))
- return;
- var vt_order_detail_delivery = bo.Data.get_Item("vt_order_detail_delivery").Table;
- var vt_order_header= bo.Data.get_Item("vt_order_header").Table;
- var vt_order_detail = bo.Data.get_Item("vt_order_detail").Table;
- var vt_order_delivery = bo.Data.get_Item("vt_order_delivery").Table;
- var vt_account_id_table = bo.Data.get_Item("vt_account").Table;
- var account_id = vt_account_id_table.Rows[0]["account_id"];
- var customer_id = vt_order_header.Rows[0]["customer_id"];
- var pricelist_id;
- if(vt_order_header.Rows[0].IsNull("pricelist_id"))
- {
- var orderPricelistTable= bo.Data.get_Item("order_pricelist").Table;
- if(orderPricelistTable.Rows.Count > 0)
- {
- pricelist_id = orderPricelistTable.Rows[0]["pricelist_id"];
- }
- else
- {
- pricelist_id = vt_order_detail_delivery.Rows[0]["pricelist_id"];
- }
- }
- else
- pricelist_id = vt_order_header.Rows[0]["pricelist_id"];
- var total_net_value = 0;
- var total_gross_value = 0;
- var orderDetailHash = DrteHashtable.New();
- for(var vt_order_detailRow in vt_order_detail.Rows)
- {
- if(vt_order_detailRow.RowState == "Deleted")
- continue;
- if(!orderDetailHash.Contains(vt_order_detailRow["product_id"].ToString()))
- {
- vt_order_detailRow["net_value"] = 0;
- vt_order_detailRow["gross_value"] = 0;
- vt_order_detailRow["order_quantity"] = 0;
- orderDetailHash.Add(vt_order_detailRow["product_id"].ToString() , vt_order_detailRow);
- }
- }
- var query = "select cpm.product_id, score \
- from company_product_measure cpm \
- inner join product_price pp on pp.product_id = cpm.product_id and pp.pricelist_id = -666 and cpm.customer_id = -777";
- query = query.replace("-666", pricelist_id);
- query = query.replace("-777", customer_id);
- var req = RequestFactory.CreateDataRequest("Search", bo.CreateParams.UserToken, "vt_query");
- req.CreateParams["query_sql"] = query;
- var resp = DataRequest.Execute(req);
- var table_second_discount = resp.Data.Tables["vt_query"];
- var requestHash = DrteHashtable.New();
- for(var table_second_discountRow in table_second_discount.Rows)
- {
- if(!requestHash.Contains(table_second_discountRow["product_id"].ToString()))
- {
- requestHash.Add(table_second_discountRow["product_id"].ToString() , table_second_discountRow["score"]);
- }
- }
- for(var vt_order_detail_deliveryRow in vt_order_detail_delivery.Rows)
- {
- if(vt_order_detail_deliveryRow.RowState == "Deleted")
- continue;
- if(vt_order_detail_deliveryRow["quantity"] == null || vt_order_detail_deliveryRow["quantity"] == "" || vt_order_detail_deliveryRow["quantity"] == 0)
- continue;
- // id de producto
- var product_id = vt_order_detail_deliveryRow["product_id"].ToString();
- var delivery_id = vt_order_detail_deliveryRow["order_delivery_id"].ToString();
- if(!orderDetailHash.Contains(product_id))
- continue;
- var net_price_purchased = 0;
- var vtOrderDetailRow = orderDetailHash[product_id];
- //gross_value
- vt_order_detail_deliveryRow["gross_value_purchased"] = vtOrderDetailRow["price_per_unit"] * vt_order_detail_deliveryRow["quantity"];
- var gross_value =vt_order_detail_deliveryRow["gross_value_purchased"];
- total_gross_value += gross_value;
- vtOrderDetailRow["gross_value"] += gross_value;
- vtOrderDetailRow["order_quantity"] += vt_order_detail_deliveryRow["quantity"];
- net_price_purchased = vt_order_detail_deliveryRow["gross_value_purchased"];
- //Discount SAP Fuera Factura
- if(requestHash.Contains(product_id))
- net_price_purchased *= ((100-requestHash[product_id])/100);
- // Discount added by rep
- var discountRep = 0;
- if(vt_order_detail_deliveryRow["discount_value"] != null && vt_order_detail_deliveryRow["discount_value"] != "" && vt_order_detail_deliveryRow["discount_value"] != 0)
- discountRep += vt_order_detail_deliveryRow["discount_value"];
- if(vtOrderDetailRow["bonus_value"] != null && vtOrderDetailRow["bonus_value"] != "" && vtOrderDetailRow["bonus_value"] != 0)
- discountRep += vtOrderDetailRow["bonus_value"];
- net_price_purchased *= ((100-discountRep)/100);
- //Save in order detail and order detail delivery
- vt_order_detail_deliveryRow["net_value_purchased"] = net_price_purchased;
- vtOrderDetailRow["net_value"] += net_price_purchased;
- total_net_value += net_price_purchased;
- }
- for(var vt_order_detailRow in vt_order_detail.Rows)
- {
- if(vt_order_detailRow.RowState == "Deleted")
- continue;
- if(vt_order_detailRow.IsNull("order_quantity"))
- continue;
- if(vt_order_detailRow["order_quantity"] == 0)
- continue;
- if(vt_order_detailRow["gross_value"] > 0)
- {
- vt_order_detailRow["discount_value"] = ((vt_order_detailRow["gross_value"] - vt_order_detailRow["net_value"])/vt_order_detailRow["gross_value"])*100;
- vt_order_detailRow["vc_discount_value"] = vt_order_detailRow["discount_value"];
- vt_order_detailRow["vc_total_discount"] = vt_order_detailRow["discount_value"];
- }
- }
- if(!vt_order_header.Rows[0].IsNull("discount"))
- {
- var total_header_discount = 0 ;
- total_header_discount = vt_order_header.Rows[0]["discount"];
- total_net_value = total_net_value*((100-total_header_discount)/100);
- }
- vt_order_header.Rows[0]["total_price"] = total_net_value;
- vt_order_header.Rows[0]["vc_total_gross"] = total_gross_value;
- vt_order_header.Rows[0]["total_discount"] = parseFloat(vt_order_header.Rows[0]["vc_total_gross"]).toFixed(2) - total_net_value;
- }
- script1();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement