Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function script1()
  2. {
  3.  
  4.     var bo = Get("BusinessObject");
  5.  
  6.  
  7.     if(!bo.Data.Contains("vt_order_detail_delivery")||!bo.Data.Contains("vt_account")||!bo.Data.Contains("vt_order_detail"))
  8.         return;
  9.  
  10.  
  11.     var vt_order_detail_delivery = bo.Data.get_Item("vt_order_detail_delivery").Table;
  12.  
  13.     var vt_order_header= bo.Data.get_Item("vt_order_header").Table;
  14.     var vt_order_detail = bo.Data.get_Item("vt_order_detail").Table;
  15.     var vt_order_delivery = bo.Data.get_Item("vt_order_delivery").Table;
  16.  
  17.     var vt_account_id_table = bo.Data.get_Item("vt_account").Table;
  18.     var account_id = vt_account_id_table.Rows[0]["account_id"];
  19.     var customer_id = vt_order_header.Rows[0]["customer_id"];
  20.     var pricelist_id;
  21.     if(vt_order_header.Rows[0].IsNull("pricelist_id"))
  22.     {
  23.         var orderPricelistTable= bo.Data.get_Item("order_pricelist").Table;
  24.  
  25.         if(orderPricelistTable.Rows.Count > 0)
  26.         {
  27.             pricelist_id = orderPricelistTable.Rows[0]["pricelist_id"];
  28.         }
  29.         else
  30.         {
  31.            
  32.             pricelist_id = vt_order_detail_delivery.Rows[0]["pricelist_id"];
  33.         }
  34.     }
  35.     else
  36.         pricelist_id = vt_order_header.Rows[0]["pricelist_id"];
  37.     var total_net_value = 0;
  38.     var total_gross_value = 0;
  39.  
  40.     var orderDetailHash = DrteHashtable.New();
  41.     for(var vt_order_detailRow in vt_order_detail.Rows)
  42.     {
  43.         if(vt_order_detailRow.RowState == "Deleted")
  44.             continue;
  45.  
  46.         if(!orderDetailHash.Contains(vt_order_detailRow["product_id"].ToString()))
  47.         {
  48.             vt_order_detailRow["net_value"] = 0;
  49.             vt_order_detailRow["gross_value"] = 0;
  50.             vt_order_detailRow["order_quantity"] = 0;
  51.             orderDetailHash.Add(vt_order_detailRow["product_id"].ToString() , vt_order_detailRow);
  52.         }
  53.     }
  54.  
  55.  
  56.     var query = "select cpm.product_id, score \
  57.                     from company_product_measure cpm \
  58.                     inner join product_price pp on pp.product_id = cpm.product_id and pp.pricelist_id = -666 and cpm.customer_id = -777";
  59.     query = query.replace("-666", pricelist_id);
  60.     query = query.replace("-777", customer_id);
  61.     var req = RequestFactory.CreateDataRequest("Search", bo.CreateParams.UserToken, "vt_query");
  62.     req.CreateParams["query_sql"] = query;
  63.     var resp = DataRequest.Execute(req);
  64.     var table_second_discount = resp.Data.Tables["vt_query"];
  65.     var requestHash = DrteHashtable.New();
  66.     for(var table_second_discountRow in table_second_discount.Rows)
  67.     {
  68.         if(!requestHash.Contains(table_second_discountRow["product_id"].ToString()))
  69.         {
  70.             requestHash.Add(table_second_discountRow["product_id"].ToString() , table_second_discountRow["score"]);
  71.         }
  72.     }
  73.  
  74.     for(var vt_order_detail_deliveryRow in vt_order_detail_delivery.Rows)
  75.     {
  76.         if(vt_order_detail_deliveryRow.RowState == "Deleted")
  77.             continue;
  78.         if(vt_order_detail_deliveryRow["quantity"] == null || vt_order_detail_deliveryRow["quantity"] == "" || vt_order_detail_deliveryRow["quantity"] == 0)
  79.             continue;
  80.        
  81.         // id de producto
  82.         var product_id = vt_order_detail_deliveryRow["product_id"].ToString();
  83.         var delivery_id = vt_order_detail_deliveryRow["order_delivery_id"].ToString();
  84.  
  85.         if(!orderDetailHash.Contains(product_id))
  86.             continue;
  87.  
  88.         var net_price_purchased = 0;
  89.         var vtOrderDetailRow = orderDetailHash[product_id];
  90.  
  91.         //gross_value
  92.         vt_order_detail_deliveryRow["gross_value_purchased"] = vtOrderDetailRow["price_per_unit"] * vt_order_detail_deliveryRow["quantity"];
  93.         var gross_value =vt_order_detail_deliveryRow["gross_value_purchased"];
  94.         total_gross_value += gross_value;
  95.         vtOrderDetailRow["gross_value"] += gross_value;
  96.         vtOrderDetailRow["order_quantity"] += vt_order_detail_deliveryRow["quantity"];
  97.  
  98.         net_price_purchased = vt_order_detail_deliveryRow["gross_value_purchased"];
  99.  
  100.         //Discount SAP Fuera Factura
  101.  
  102.         if(requestHash.Contains(product_id))
  103.             net_price_purchased *= ((100-requestHash[product_id])/100);
  104.  
  105.         // Discount added by rep
  106.         var discountRep = 0;
  107.         if(vt_order_detail_deliveryRow["discount_value"] != null && vt_order_detail_deliveryRow["discount_value"] != "" && vt_order_detail_deliveryRow["discount_value"] != 0)
  108.             discountRep += vt_order_detail_deliveryRow["discount_value"];
  109.  
  110.  
  111.         if(vtOrderDetailRow["bonus_value"] != null && vtOrderDetailRow["bonus_value"] != "" && vtOrderDetailRow["bonus_value"] != 0)
  112.             discountRep += vtOrderDetailRow["bonus_value"];
  113.  
  114.  
  115.         net_price_purchased *= ((100-discountRep)/100);
  116.  
  117.         //Save in order detail and order detail delivery
  118.         vt_order_detail_deliveryRow["net_value_purchased"] = net_price_purchased;
  119.        
  120.         vtOrderDetailRow["net_value"] += net_price_purchased;
  121.         total_net_value += net_price_purchased;
  122.  
  123.     }
  124.  
  125.     for(var vt_order_detailRow in vt_order_detail.Rows)
  126.     {
  127.         if(vt_order_detailRow.RowState == "Deleted")
  128.             continue;
  129.         if(vt_order_detailRow.IsNull("order_quantity"))
  130.             continue;
  131.         if(vt_order_detailRow["order_quantity"] == 0)
  132.             continue;
  133.         if(vt_order_detailRow["gross_value"] > 0)
  134.         {
  135.             vt_order_detailRow["discount_value"] = ((vt_order_detailRow["gross_value"] - vt_order_detailRow["net_value"])/vt_order_detailRow["gross_value"])*100;
  136.             vt_order_detailRow["vc_discount_value"] = vt_order_detailRow["discount_value"];
  137.             vt_order_detailRow["vc_total_discount"] = vt_order_detailRow["discount_value"];
  138.         }
  139.     }
  140.  
  141.     if(!vt_order_header.Rows[0].IsNull("discount"))
  142.     {
  143.         var total_header_discount = 0 ;
  144.         total_header_discount = vt_order_header.Rows[0]["discount"];
  145.         total_net_value = total_net_value*((100-total_header_discount)/100);
  146.     }
  147.  
  148.     vt_order_header.Rows[0]["total_price"] = total_net_value;
  149.     vt_order_header.Rows[0]["vc_total_gross"] = total_gross_value; 
  150.     vt_order_header.Rows[0]["total_discount"] = parseFloat(vt_order_header.Rows[0]["vc_total_gross"]).toFixed(2) - total_net_value;
  151.  
  152.  
  153. }
  154.  
  155. script1();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement