//修改PO取贸易协议价格逻辑-重写取价格方法.
//Soondy @Champway - Silverlit 20070802 last updated.
//Table->Purchline.
server void  setPriceDisc(InventDim inventDim)
{
   ;
   //this.setPriceAgreement(inventDim);
   this.PurchPrice = this.TradAgrmentPrice(inventDim);
   this.LineAmount = this.calcLineAmountForced();

}
//addnew Table Method in purchline.
purchprice TradAgrmentPrice(InventDim inventDim,CurrencyCode TmpcurrencyCode = PurchTable::find(this.PurchId).CurrencyCode)
{
    PriceDiscTable priceDiscTable;
    Boolean        tradagrmentOk;//soondy 20060925
    Boolean        FirstFlag;
    int                CurInt;
    transDate      lastDate1;
    transDate      lastDate2;
    price          inPrice1=0;
    price          minPrice2=0;
    unitId         purchLinePurchUnitId;
    price          TmpPriceofTradAgment;
    PriceUnit   TmpPriceUnit;
    qty            TmpQty;
    PriceUnit      TmpTALinePriceUnit;
    priceUnit      _TMPPriceUnit;
    priceUnit      _TMPPriceUnit1;
    priceUnit      _TMPPriceUnit2;
    PriceMarkup    _TMPPriceMark;
    PriceMarkup    _TMPPriceMark1;
    PriceMarkup    _TMPPriceMark2;

    InventDim       inventDimAllActivated;
    InventDim       inventDimItemDimActivated;
    InventDim       inventDimNoneActivated;
    InventTable     inventTable;
    boolean         findAll     = true;
    boolean         findItemDim = true;
    InventDimId    _inventDimIdAllActivated;
    InventDimId    _inventDimIdItemDim;
    InventDimId    _inventDimIdBlank;
    boolean         foundTradAgrment  = False;
    ;

    inventTable = InventTable::find(this.ItemId);
    inventDimAllActivated.copyActivatePurchPriceAll(inventTable.DimGroupId, inventDim);
    findAll     = !InventDim::isInventDimEqual(inventDimAllActivated, inventDimItemDimActivated);
    findItemDim = !InventDim::isInventDimEqual(inventDimItemDimActivated,inventDimNoneActivated);
    inventDimAllActivated       = findAll       ? InventDim::findDim(inventDimAllActivated)     : inventDimAllActivated;
    inventDimItemDimActivated   = findItemDim   ? InventDim::findDim(inventDimItemDimActivated) : inventDimItemDimActivated;
    _inventDimIdAllActivated    = inventDimAllActivated.InventDimId;
    _inventDimIdItemDim         = inventDimItemDimActivated.InventDimId;
    _inventDimIdBlank           = InventDim::inventDimIdBlank();
//firstly find  _inventDimIdAllActivated
//if not find then secondly find  _inventDimIdItemDim
//if not find then thirdly  find  _inventDimIdBlank

//    tradagrmentOk  =EmplTable::find(EmplTable::userId2EmplId(curUserid())).SLT_BaseofTradAgmentPrice;

//    if ( tradagrmentOk==True)  //soondy 20060914 adding approve purch price disc
//    {

//first search.
        while select priceDiscTable
            index PriceDiscIdx  // equals order by QuantityAmount
            where priceDiscTable.relation           == PriceType::PricePurch                                    &&
                  priceDiscTable.ItemCode           == TableGroupAll::Table                                     &&
                  priceDiscTable.itemRelation       == this.ItemId                                                      &&
                  priceDiscTable.accountCode        == TableGroupAll::Table                                   &&
                  priceDiscTable.accountRelation    == this.VendAccount                                         &&
                  priceDiscTable.currency           == TmpcurrencyCode                                            &&
                  priceDiscTable.QuantityAmount     <= abs(this.QtyOrdered?this.QtyOrdered:1)     &&
                  priceDiscTable.InventDimId        == _inventDimIdAllActivated                               &&
                 (systemdateget()                   >= priceDiscTable.fromDate  || ! priceDiscTable.fromDate)  &&
                 (systemdateget()                   <= priceDiscTable.toDate      || ! priceDiscTable.toDate)      && //added by soondy for approved records
                  priceDiscTable.SLT_Approved       == noyes::Yes

        {
           //the lastest date price
           lastDate1=lastDate2;

           if (priceDiscTable.FromDate>lastDate1)
           {
               lastDate2=priceDiscTable.FromDate;

           }

        }

         CurInt=0;
         while select priceDiscTable
            index PriceDiscIdx  // equals order by QuantityAmount
            where priceDiscTable.relation           == PriceType::PricePurch                                    &&
                  priceDiscTable.ItemCode           == TableGroupAll::Table                                     &&
                  priceDiscTable.itemRelation       == this.ItemId                                                      &&
                  priceDiscTable.accountCode        == TableGroupAll::Table                                   &&
                  priceDiscTable.accountRelation    == this.VendAccount                                         &&
                  priceDiscTable.currency           == TmpcurrencyCode                                           &&
                  priceDiscTable.QuantityAmount     <= abs(this.QtyOrdered?this.QtyOrdered:1)    &&
                  priceDiscTable.InventDimId        == _inventDimIdAllActivated                              &&
                 (priceDiscTable.fromDate == lastDate2 || !priceDiscTable.FromDate)                     &&
                 (systemdateget()                   >= priceDiscTable.fromDate  || ! priceDiscTable.fromDate)   &&
                 (systemdateget()                   <= priceDiscTable.toDate      || ! priceDiscTable.toDate)       && //added by soondy for approved records
                 priceDiscTable.SLT_Approved       == noyes::Yes


         {
            foundTradAgrment = True;
            //the min price
            CurInt =CurInt+1;
            if (CurInt==1)
            {
               FirstFlag=True;
            }

            purchLinePurchUnitId=this.PurchUnit;

            if(priceDiscTable.UnitId && priceDiscTable.UnitId!=purchLinePurchUnitId)
            {

               TmpPriceUnit  = UnitConvert::valueConvert(priceDiscTable.Amount,
                                         purchLinePurchUnitId,
                                         priceDiscTable.UnitId,
                                         priceDiscTable.itemRelation);

               TmpPriceofTradAgment = TmpPriceUnit;
               _TMPPriceUnit = priceDiscTable.PriceUnit;
               _TMPPriceMark = priceDiscTable.Markup;
            }
            else
            {

              TmpPriceUnit= InventTableModule::find(priceDiscTable.itemRelation,ModuleInventPurchSales::Invent).priceUnit() ? InventTableModule::find(priceDiscTable.itemRelation,ModuleInventPurchSales::Invent).priceUnit() : 1;

              TmpPriceofTradAgment = priceDiscTable.Amount/TmpPriceUnit;
              _TMPPriceUnit = priceDiscTable.PriceUnit;
              _TMPPriceMark = priceDiscTable.Markup;

            }

            if (FirstFlag==True)
            {

                minPrice1=TmpPriceofTradAgment;
                FirstFlag=False;
                _TMPPriceUnit1 = _TMPPriceUnit;
                _TMPPriceMark1 = _TMPPriceMark;
            }
            else
            {

                minPrice1=minPrice2;
                _TMPPriceUnit1 = _TMPPriceUnit2;
                _TMPPriceMark1 = _TMPPriceMark2;
            }

            if (TmpPriceofTradAgment<=minPrice1)
            {
               minPrice2=TmpPriceofTradAgment;
               _TMPPriceUnit2 = _TMPPriceUnit;
               _TMPPriceMark2 = _TMPPriceMark;
            }

         }

//second search
    if (foundTradAgrment ==false)
    {
        while select priceDiscTable
            index PriceDiscIdx  // equals order by QuantityAmount
            where priceDiscTable.relation           == PriceType::PricePurch                                     &&
                  priceDiscTable.ItemCode           == TableGroupAll::Table                                      &&
                  priceDiscTable.itemRelation       == this.ItemId                                                       &&
                  priceDiscTable.accountCode        == TableGroupAll::Table                                    &&
                  priceDiscTable.accountRelation    == this.VendAccount                                          &&
                  priceDiscTable.currency           == TmpcurrencyCode                                             &&
                  priceDiscTable.QuantityAmount     <= abs(this.QtyOrdered?this.QtyOrdered:1)      &&
                  priceDiscTable.InventDimId        == _inventDimIdItemDim                                      &&
                 (systemdateget()                   >= priceDiscTable.fromDate  || ! priceDiscTable.fromDate)   &&
                 (systemdateget()                   <= priceDiscTable.toDate      || ! priceDiscTable.toDate)       && //added by soondy for approved records
                  priceDiscTable.SLT_Approved       == noyes::Yes

        {
           //the lastest date price
           lastDate1=lastDate2;

           if (priceDiscTable.FromDate>lastDate1)
           {
               lastDate2=priceDiscTable.FromDate;

           }

        }

         CurInt=0;
         while select priceDiscTable
            index PriceDiscIdx  // equals order by QuantityAmount
            where priceDiscTable.relation           == PriceType::PricePurch                                    &&
                  priceDiscTable.ItemCode           == TableGroupAll::Table                                     &&
                  priceDiscTable.itemRelation       == this.ItemId                                                      &&
                  priceDiscTable.accountCode        == TableGroupAll::Table                                   &&
                  priceDiscTable.accountRelation    == this.VendAccount                                         &&
                  priceDiscTable.currency           == TmpcurrencyCode                                            &&
                  priceDiscTable.QuantityAmount     <= abs(this.QtyOrdered?this.QtyOrdered:1)     &&
                  priceDiscTable.InventDimId        == _inventDimIdItemDim                                     &&
                 (priceDiscTable.fromDate == lastDate2 || !priceDiscTable.FromDate)                      &&
                 (systemdateget()                   >= priceDiscTable.fromDate  || ! priceDiscTable.fromDate)   &&
                 (systemdateget()                   <= priceDiscTable.toDate      || ! priceDiscTable.toDate)       && //added by soondy for approved records
                 priceDiscTable.SLT_Approved       == noyes::Yes


         {
            foundTradAgrment = True;
            //the min price
            CurInt =CurInt+1;
            if (CurInt==1)
            {
               FirstFlag=True;
            }

            purchLinePurchUnitId=this.PurchUnit;

            if(priceDiscTable.UnitId && priceDiscTable.UnitId!=purchLinePurchUnitId)
            {

               TmpPriceUnit  = UnitConvert::valueConvert(priceDiscTable.Amount,
                                         purchLinePurchUnitId,
                                         priceDiscTable.UnitId,
                                         priceDiscTable.itemRelation);

               TmpPriceofTradAgment = TmpPriceUnit;
               _TMPPriceUnit = priceDiscTable.PriceUnit;
               _TMPPriceMark = priceDiscTable.Markup;
            }
            else
            {

              TmpPriceUnit= InventTableModule::find(priceDiscTable.itemRelation,ModuleInventPurchSales::Invent).priceUnit() ? InventTableModule::find(priceDiscTable.itemRelation,ModuleInventPurchSales::Invent).priceUnit() : 1;

              TmpPriceofTradAgment = priceDiscTable.Amount/TmpPriceUnit;
              _TMPPriceUnit = priceDiscTable.PriceUnit;
              _TMPPriceMark = priceDiscTable.Markup;

            }

            if (FirstFlag==True)
            {

                minPrice1=TmpPriceofTradAgment;
                FirstFlag=False;
                _TMPPriceUnit1 = _TMPPriceUnit;
                _TMPPriceMark1 = _TMPPriceMark;
            }
            else
            {

                minPrice1=minPrice2;
                _TMPPriceUnit1 = _TMPPriceUnit2;
                _TMPPriceMark1 = _TMPPriceMark2;
            }

            if (TmpPriceofTradAgment<=minPrice1)
            {
               minPrice2=TmpPriceofTradAgment;
               _TMPPriceUnit2 = _TMPPriceUnit;
               _TMPPriceMark2 = _TMPPriceMark;
            }

         }

    }

//third search
    if (foundTradAgrment ==false)
    {
        while select priceDiscTable
            index PriceDiscIdx  // equals order by QuantityAmount
            where priceDiscTable.relation           == PriceType::PricePurch                                     &&
                  priceDiscTable.ItemCode           == TableGroupAll::Table                                      &&
                  priceDiscTable.itemRelation       == this.ItemId                                                       &&
                  priceDiscTable.accountCode        == TableGroupAll::Table                                    &&
                  priceDiscTable.accountRelation    == this.VendAccount                                          &&
                  priceDiscTable.currency           == TmpcurrencyCode                                            &&
                  priceDiscTable.QuantityAmount     <= abs(this.QtyOrdered?this.QtyOrdered:1)     &&
                  priceDiscTable.InventDimId        == _inventDimIdBlank                                         &&
                 (systemdateget()                   >= priceDiscTable.fromDate  || ! priceDiscTable.fromDate)   &&
                 (systemdateget()                   <= priceDiscTable.toDate      || ! priceDiscTable.toDate)       && //added by soondy for approved records
                  priceDiscTable.SLT_Approved       == noyes::Yes

        {
           //the lastest date price
           lastDate1=lastDate2;

           if (priceDiscTable.FromDate>lastDate1)
           {
               lastDate2=priceDiscTable.FromDate;

           }

        }

         CurInt=0;
         while select priceDiscTable
            index PriceDiscIdx  // equals order by QuantityAmount
            where priceDiscTable.relation           == PriceType::PricePurch                                    &&
                  priceDiscTable.ItemCode           == TableGroupAll::Table                                     &&
                  priceDiscTable.itemRelation       == this.ItemId                                                      &&
                  priceDiscTable.accountCode        == TableGroupAll::Table                                   &&
                  priceDiscTable.accountRelation    == this.VendAccount                                         &&
                  priceDiscTable.currency           == TmpcurrencyCode                                           &&
                  priceDiscTable.QuantityAmount     <= abs(this.QtyOrdered?this.QtyOrdered:1)    &&
                  priceDiscTable.InventDimId        == _inventDimIdBlank                                        &&
                 (priceDiscTable.fromDate == lastDate2 || !priceDiscTable.FromDate)                              &&
                 (systemdateget()                   >= priceDiscTable.fromDate  || ! priceDiscTable.fromDate)  &&
                 (systemdateget()                   <= priceDiscTable.toDate      || ! priceDiscTable.toDate)      && //added by soondy for approved records
                 priceDiscTable.SLT_Approved       == noyes::Yes


         {
            foundTradAgrment = True;
            //the min price
            CurInt =CurInt+1;
            if (CurInt==1)
            {
               FirstFlag=True;
            }

            purchLinePurchUnitId=this.PurchUnit;

            if(priceDiscTable.UnitId && priceDiscTable.UnitId!=purchLinePurchUnitId)
            {

               TmpPriceUnit  = UnitConvert::valueConvert(priceDiscTable.Amount,
                                         purchLinePurchUnitId,
                                         priceDiscTable.UnitId,
                                         priceDiscTable.itemRelation);

               TmpPriceofTradAgment = TmpPriceUnit;
               _TMPPriceUnit = priceDiscTable.PriceUnit;
               _TMPPriceMark = priceDiscTable.Markup;
            }
            else
            {

              TmpPriceUnit= InventTableModule::find(priceDiscTable.itemRelation,ModuleInventPurchSales::Invent).priceUnit() ? InventTableModule::find(priceDiscTable.itemRelation,ModuleInventPurchSales::Invent).priceUnit() : 1;

              TmpPriceofTradAgment = priceDiscTable.Amount/TmpPriceUnit;
              _TMPPriceUnit = priceDiscTable.PriceUnit;
              _TMPPriceMark = priceDiscTable.Markup;

            }

            if (FirstFlag==True)
            {

                minPrice1=TmpPriceofTradAgment;
                FirstFlag=False;
                _TMPPriceUnit1 = _TMPPriceUnit;
                _TMPPriceMark1 = _TMPPriceMark;
            }
            else
            {

                minPrice1=minPrice2;
                _TMPPriceUnit1 = _TMPPriceUnit2;
                _TMPPriceMark1 = _TMPPriceMark2;
            }

            if (TmpPriceofTradAgment<=minPrice1)
            {
               minPrice2=TmpPriceofTradAgment;
               _TMPPriceUnit2 = _TMPPriceUnit;
               _TMPPriceMark2 = _TMPPriceMark;
            }

         }

    }

//soondy 20060928 adding formular for carton box factor beg.
    if(InventTable::find(this.ItemId).SLT_CartonBox==True)
    {
        minPrice2=round(minPrice2 * (InventTable::find(this.ItemId).SLT_Depth+InventTable::find(this.ItemId).SLT_Width+2)*(InventTable::find(this.ItemId).SLT_Width+InventTable::find(this.ItemId).SLT_Height+1)*2/1000,0.01);

     }
//soondy 20060928 adding formular for carton box factor end.

    this.PriceUnit = _TMPPriceUnit2;
    this.PurchMarkup = _TMPPriceMark2;
    return minPrice2;
}


注:
先后有过几次修改的版本,写的比较乱.
控制PO价格的几处需要留意的地方.
1.SO转PO.
2.手工开PO.
3.PPO转PO
其实,在Class ->PuchlineType 这个类也有类似的取PO purchprice的方法.大致也上述的TradAgrmentPrice()雷同.