<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Efficy Overflow Q&amp;A - Recent questions tagged sys_tableviews</title>
<link>https://overflow.efficy.io/?qa=tag/sys_tableviews</link>
<description>Powered by Question2Answer</description>
<item>
<title>Caculated field in SYS_TABLEVIEWS</title>
<link>https://overflow.efficy.io/?qa=5879/caculated-field-in-systableviews</link>
<description>&lt;p&gt;Could we use a calculated field in SYS_TABLEVIEWS?&lt;/p&gt;

&lt;p&gt;Usage in a (invoice) template&lt;/p&gt;

&lt;p&gt;Original SYS_TABLEVIEWS.SELECTFIELDS:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;DOCU_PROD.*, PRODUCTS.NAME, PROD$PRIJZEN.F_EENHEID, PROD$OMSCHRIJVING.F_ARTIKELNUMMER, PROD$OMSCHRIJVING.F_OMSCHRIJVING
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;New SYS_TABLEVIEWS.SELECTFIELDS:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;DOCU_PROD.*, (DOCU_PROD.PRICE - ((DOCU_PROD.PRICE /100) * DOCU_PROD.DISCOUNT)) AS NETTO_PRICE, PRODUCTS.NAME, PROD$PRIJZEN.F_EENHEID, PROD$OMSCHRIJVING.F_ARTIKELNUMMER, PROD$OMSCHRIJVING.F_OMSCHRIJVING
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or should I use a virtual field or how should I approach this?&lt;/p&gt;
</description>
<category>Efficy/ Client side</category>
<guid isPermaLink="true">https://overflow.efficy.io/?qa=5879/caculated-field-in-systableviews</guid>
<pubDate>Wed, 10 Feb 2021 09:57:16 +0000</pubDate>
</item>
<item>
<title>Use MS SQL &quot;CASE&quot; in SYS_TABLEVIEWS</title>
<link>https://overflow.efficy.io/?qa=3469/use-ms-sql-case-in-systableviews</link>
<description>&lt;p&gt;Hi all,&lt;/p&gt;

&lt;p&gt;I put my customization at the bottom&lt;br&gt;
Some product can be a batch (Product that contains other Products) (PROD&lt;em&gt;PROD : K&lt;/em&gt;PRODUCT = Batch, K_PRODUCT2 = Simple product)&lt;br&gt;
Note : a Batch have FAMILY = 1, other product have other value (can be empty)&lt;/p&gt;

&lt;p&gt;I customized the DocuEdit window : when a product is linked, the WF checks if FAMILY=1 and then it link all products to the Document&lt;br&gt;
--&amp;gt; DOCU&lt;em&gt;PROD have a custom field &quot;F&lt;/em&gt;LOT&quot; that contains the K_PRODUCT of the Batch (can be NULL too)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important to notice :&lt;br&gt;
- I configured LK&lt;em&gt;PROD&lt;/em&gt;FAMILY.K&lt;em&gt;SORT to help me to order the display :&lt;br&gt;
ID | K&lt;/em&gt;SORT | FAMILY&lt;br&gt;
1  |   4    |  Batch&lt;br&gt;
2  |   1    |  Presta&lt;br&gt;
3  |   2    |  Option&lt;br&gt;
4  |   3    |  Expense&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I want to customize the grid (macros) and the display order of products on DocuEdit like this :&lt;br&gt;
Products of Batch 1 (Family &amp;lt;&amp;gt; 1; F&lt;em&gt;LOT=K&lt;/em&gt;PRODUCT of Batch 1) order by NAME&lt;br&gt;
Batch 1 (Family = 1; F&lt;em&gt;LOT = its K&lt;/em&gt;PRODUCT)&lt;br&gt;
Products of Batch 2 (Family &amp;lt;&amp;gt; 1; F&lt;em&gt;LOT=K&lt;/em&gt;PRODUCT of Batch 2) order by NAME&lt;br&gt;
Batch 2 (Family = 1; F&lt;em&gt;LOT = its K&lt;/em&gt;PRODUCT)&lt;br&gt;
Product directly linked to Document (F_LOT = NULL) order by NAME&lt;/p&gt;

&lt;p&gt;To do this I have to customize SYS&lt;em&gt;TABLEVIEWS.Docu&lt;/em&gt;Prod (K_TABLE = 31030). The pure MS SQL I need is :&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;select Prod.NAME, Prod.FAMILY, DP.*
    from DOCU_PROD DP
    join PRODUCTS Prod on DP.K_PRODUCT=Prod.K_PRODUCT
    join LK_PROD_FAMILY family on Prod.FAMILY=family.K_PROD_FAMILY
    where DP.K_DOCUMENT=@DocumentID
    order by case when DP.F_LOT is null then 1 else 0 end, /* product linked directly to Document at bottom (F_LOT=NULL) */
        DP.F_LOT,   /* order by Batch */
        family.K_SORT, /* family.K_SORT=1 --&amp;gt; Presta, family.K_SORT=2 --&amp;gt; Option, family.K_SORT=3 --&amp;gt; Expense, family.K_SORT=4 --&amp;gt; Batch */
        Prod.NAME /* the products are listed by NAME */
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I have 2 problems :&lt;/p&gt;

&lt;p&gt;1) USE CASE IN SYS&lt;em&gt;TABLEVIEWS :&lt;br&gt;
To use &quot;CASE&quot; in SYS&lt;/em&gt;TABLEVIEWS I translated it like this :&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;SELECTFROM='DOCU_PROD DOCU_PROD, PRODUCTS PRODUCTS, LK_PROD_FAMILY LK_PROD_FAMILY'
SELECTFIELDS='DOCU_PROD.*, PRODUCTS.NAME, PRODUCTS.OPENED, PRODUCTS.FAMILY'
SELECTJOIN='DOCU_PROD.K_PRODUCT = PRODUCTS.K_PRODUCT, PRODUCTS.FAMILY=LK_PROD_FAMILY.K_PROD_FAMILY'
SELECTORDERBY='&amp;lt;#IFDB DB=&quot;MS&quot; THEN=&quot;case when DP.F_LOT is null then 1 else 0 end, &quot;&amp;gt;DOCU_PROD.F_LOT, LK_PROD_FAMILY.K_SORT, PRODUCTS.NAME'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Problem 1 : SYS&lt;em&gt;TABLEVIEWS don't support neither pure MS SQL (case) nor IFDB query tag. How can I use CASE in SYS&lt;/em&gt;TABLEVIEWS ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I tried to use this query in a RunQuery in MacroEditCustom.txt and call it in TabContentProds, but I can't : this request looks directly in the DB, but when a user add a product, the application don't persist the relations in the DB until the user clicks the save/apply button, so the DocuEdit is refresh but the user can't see nothing.&lt;/p&gt;

&lt;p&gt;2) Use FAMILY and F_LOT to define style&lt;br&gt;
To insert the products on the Document, the WF use InsertDetail() and some UpdateDetail() without CommitChanges (if I use it, the relations will be persisted and the &quot;cancel&quot; button don't delete these links)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem 2 : the overriden columns definitions use F_LOT and FAMILY to set css classes, etc. It seems hat Efficy can't find FAMILY until the relations have not been commited.&lt;/strong&gt;&lt;br&gt;
&lt;img src=&quot;https://overflow.efficy.com/?qa=blob&amp;amp;qa_blobid=12218006850585471763&quot; alt=&quot;enter image description here&quot;&gt;&lt;br&gt;
On the screen :&lt;br&gt;
- FAMILY = R_FAMILY ; LOT = F_LOT ; FAM = LK_PROD_FAMILY.K_SORT&lt;br&gt;
- The first 3 lines are already in the DB, and F_LOT=39, so they must be ordered by FAM&lt;br&gt;
    ==&amp;gt; why '39 - Lot 2' is the first line and not the third ? After search a bit, It like Efficy sort them by K_RELATION and ignore SELECTORDERBY&lt;br&gt;
- others have not been comitted in the DB, so Efficy can't find the value of FAMILY&lt;br&gt;
    ==&amp;gt; how can I set the my css classes in macros ?&lt;/p&gt;

&lt;p&gt;CUSTOMIZATIONS :&lt;br&gt;
Current SYS&lt;em&gt;TABLEVIEWS.Docu&lt;/em&gt;Prod :&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;update SYS_TABLEVIEWS
    set ISCUSTOM=1,
    SELECTFROM='DOCU_PROD DOCU_PROD, PRODUCTS PRODUCTS, LK_PROD_FAMILY LK_PROD_FAMILY',
    SELECTFIELDS='DOCU_PROD.*, PRODUCTS.NAME, PRODUCTS.OPENED, PRODUCTS.FAMILY, LK_PROD_FAMILY.K_SORT FAM',
    SELECTJOIN='DOCU_PROD.K_PRODUCT = PRODUCTS.K_PRODUCT, PRODUCTS.FAMILY=LK_PROD_FAMILY.K_PROD_FAMILY',
    SELECTORDERBY='DOCU_PROD.F_LOT, LK_PROD_FAMILY.K_SORT, PRODUCTS.NAME'
    where NAME='Docu_Prod'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;InsertBatchChildren()&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function OnInsertDetailDocuProd(EditHandle, Detail, DetailKey) { 
  InsertBatchChildren(EditHandle, Detail, DetailKey) 
}
function InsertBatchChildren(EditHandle, Detail, BatchKey) {
  var prodConsult = Database.OpenConsultContext(ntProd);
  try {
    // get family of current product
    var batchDS = Database.Consult(prodConsult, BatchKey, false),
    family = batchDS.FieldByName(&quot;FAMILY&quot;).asInteger;

    // don't do anything if it's not a Batch
    if (family != GLOBAL.LK_PROD_FAMILY.BATCH) return;

    // else set its F_LOT and FAMILY and link its children
    Database.UpdateDetail(EditHandle, ntProd, 0, -1, &quot;F_LOT&quot;, BatchKey); // define Batch F_LOT
    Database.UpdateDetail(EditHandle, ntProd, 0, -1, &quot;FAMILY&quot;, family); // define Batch F_LOT
    //TO DO : define K_SORT Database.UpdateDetail(EditHandle, ntProd, 0, -1, &quot;K_SORT&quot;, int); // define Batch K_SORT

    var queryHandle,
        childrenDS = Database.ConsultDetail(queryHandle, prodConsult, BatchKey, ntProd, false, true, 0);

    childrenDS.First;
    while (!childrenDS.Eof) {
      var k_product = childrenDS.fieldByName(&quot;K_PRODUCT&quot;).AsFloat;
      Logger.Write(&quot;Inserting &quot; + k_product + &quot; from &quot; + BatchKey);
      Database.InsertDetail(EditHandle, ntProd, k_product, -1, false);                                    // insert Batch child on Document
      Database.UpdateDetail(EditHandle, ntProd, 0, -1, &quot;NAME&quot;, childrenDS.fieldByName(&quot;NAME&quot;).asString);  // define child NAME
      Database.UpdateDetail(EditHandle, ntProd, 0, -1, &quot;F_LOT&quot;, BatchKey);                                // define child F_LOT
      Database.UpdateDetail(EditHandle, ntProd, 0, -1, &quot;COMMENT&quot;, &quot;Inserted from WORKFLOW&quot;);              // define child COMMENT

      // Update FAMILY in DOCU_PROD
      var childFamily= childrenDS.fieldByName(&quot;FAMILY&quot;).AsInteger; // ERROR : &quot;Field 'FAMILY' not found&quot;
      Logger.Write(&quot;childFamily=&quot;+childFamily);
      Database.UpdateDetail(EditHandle, ntProd, 0, -1, &quot;FAMILY&quot;, childFamily);

      childrenDS.Next;
    }
  } finally {
    Database.CloseContext(prodConsult);
  }
}
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>How to</category>
<guid isPermaLink="true">https://overflow.efficy.io/?qa=3469/use-ms-sql-case-in-systableviews</guid>
<pubDate>Tue, 14 Aug 2018 09:46:08 +0000</pubDate>
</item>
</channel>
</rss>