Assignment 1 (SQL CC - Basic): Difference between revisions

From eStream Software
Line 5: Line 5:


===Assignment : To Create the Unit Price Calculation===
===Assignment : To Create the Unit Price Calculation===
Below is Example are doing following actions
* Create a '''UDF_mUnitPrice''' DIY fields in Sales documents (eg. sales invoice)
* Create a '''UDF_mUnitPrice''' DIY fields in Sales documents (eg. sales invoice)
* Purpose: User will key-in the base unit price to convert into new unit price based on the UOM rate in Maintain Stock Item.
* Purpose: User will key-in the base unit price to convert into new unit price based on the UOM rate in Maintain Stock Item.

Revision as of 02:24, 9 November 2015

Update Info

Last Script Update : 09 Nov 2015
Level : Basic


Assignment : To Create the Unit Price Calculation

  • Create a UDF_mUnitPrice DIY fields in Sales documents (eg. sales invoice)
  • Purpose: User will key-in the base unit price to convert into new unit price based on the UOM rate in Maintain Stock Item.
01 Sales Invoice Entry-UDF mUnitPrice.jpg


  • Calculation for Unit Price := UDF_mUnitPrice x Rate

Steps

01. Click Tools | DIY | SQL Control Center...
02. At the left panel look for Sales Invoice
03. Right Click the Sales Invoice
DIYField-03.jpg
04. Select New Event
DIYScript-01.jpg
05. Enter any name (eg Calc) in the Name field (Only Alphanumeric & no spacing)
06. Select OnGridColumnValueChanged for Event field
07. Click OK
08. Click the Calc (name create at Step 5 above) on the left panel
DIYScript-02.jpg


09. Copy below script & paste to the Right Panel (Script Section)
var FComServer, lBizObj : Variant;
    cdsData : TClientDataSet;

function ComServer: Variant;
begin
  if FComServer = Null then begin
    FComServer := CreateOleObject('SQLAcc.BizApp');
  end;
  Result := FComServer;
end;

procedure GetStockInfo;
var lSQL, lCode  : String;
begin
  FComServer := null;
  cdsData    := TClientDataSet.Create(nil); // Create & preparing Component
  try
    lCode := Dataset.FindField('ItemCode').AsString;     
    lSQL  := Format('SELECT UDF_Length, UDF_Width FROM ST_ITEM WHERE Code=%s',[QuotedStr(lCode)]);

    cdsData.Data := ComServer.DBManager.Execute(lSQL);
  finally
    lBizObj    := null;
    FComServer := null;
  end;
end;

begin
  if SameText(EditingField, 'ItemCode') OR // when selecting or change itemcode field
     SameText(EditingField, 'UDF_Rate') then begin // when change UDF_Rate field
    try
      GetStockInfo; // Get UDF_Length & UDF_Width from Maintain Item

      // Below is Set the Invoice detial UDF Fields from Maintain Item UDF Fields
      DataSet.FindField('UDF_Length').AsFloat := cdsData.FindField('UDF_Length').AsFloat;
      DataSet.FindField('UDF_Width').AsFloat  := cdsData.FindField('UDF_Width').AsFloat;

      // Below is doing calculation for Qty := UDF_Length * UDF_Width * UDF_Rate
      DataSet.FindField('Qty').AsFloat        := DataSet.FindField('UDF_Length').AsFloat *
                                                 DataSet.FindField('UDF_Width').AsFloat *
                                                 DataSet.FindField('UDF_Rate').AsFloat;
    finally
      cdsData.Free; // Free the Component after used
    end;
  end;
end.
10. Click Save button
----------------------------------------------------------------------------------------------------------------------------------------------------
Template.Warning-01.jpg
  • Avoid update below field in same time as will cause unlimited looping updating each other
- Qty
- UnitPrice
- TaxAmt
- Amount
----------------------------------------------------------------------------------------------------------------------------------------------------