1. 程式人生 > >易飛 批量匯出易飛ERP系統目前所有BOM 含本幣未稅單位成本

易飛 批量匯出易飛ERP系統目前所有BOM 含本幣未稅單位成本

               

摘要:匯出ERP系統中所有成品對應的BOM列表含材料成本(本幣)

思路:

         1.查詢已核準的成品品號

         2.撰寫當期匯率函式

         3.撰寫材料最新核價函式

         4.BOM的元件材料的單位是庫存單位,核價單單價是以採購單價為基礎利用系統提供的轉換函式

         5.展BOM程式

         6.建立臨時表,迴圈遍歷需要展算的成品,展算的BOM儲存到臨時表。

1.當前匯率:  GetCurrExchangeRate

--==================================--作者:龔德輝--用途:取得當前匯率--日期:2010-04-16
--==================================ALTER function [dbo].[GetCurrExchangeRate]( @currency AS NCHAR(3))returns decimal(15,7)as begindeclare @price decimal(15,7)        IF(@currency<>'RMB'begin             SELECT @price=MG003 from  CMSMG where MG002=(select MAX(MG002) from CMSMG WHERE [email protected]
) and  [email protected] end else begin      set @price=1.0000000 endreturn @priceend

2.最新核價:PriceNoTaxRMB(此函式請各位優化一下)

[sql] view plain copy print?
  1. <code class="language-sql">--==================================
  2. --作者:龔德輝
  3. --用途:取最新不含稅單價
  4. --日期 :2011-05-28
  5. --==================================
  6. ALTERfunction [dbo].[PriceNoTaxRMBBOM](  
  7.     @Item ASNCHAR(20)  
  8. )returnsdecimal(15,6)  
  9. as
  10. begin
  11. declare @PriceNoTaxRMB decimal(15,6)  
  12. select @PriceNoTaxRMB=CASEWHEN TL008='Y'THENconvert(decimal(18,8),TM010*ChgRate/TaxRate)*  
  13. dbo.DFC_UnitConvert(MB001,1,MB004,TM009)  
  14.  elseconvert(decimal(18,8),TM010*ChgRate)*  
  15. dbo.DFC_UnitConvert(MB001,1,MB004,TM009) Endfrom(  
  16. select
  17. TOP 1 TL005,  
  18. TL004,  
  19. TL008,  
  20. TM010,  
  21. TM014,  
  22. dbo.GetCurrExchangeRate(TL005) AS ChgRate,  
  23. [dbo].[GetVendorTaxRate](TL004)+1 as TaxRate,  
  24. TM009,  
  25. MB004,  
  26. MB001  
  27. from PURTL INNERJOIN PURTM ON TL001=TM001 AND TL002=TM002  
  28.      INNERJOIN  INVMB ON TM004=MB001  
  29. where [email protected] AND TM011='Y'
  30. ORDERBY TM014 desc
  31. ) a  
  32. returnisnull(@PriceNoTaxRMB,0)  
  33. end
  34. </code>  
--==================================--作者:龔德輝--用途:取最新不含稅單價--日期 :2011-05-28--==================================ALTER function [dbo].[PriceNoTaxRMBBOM]( @Item AS NCHAR(20))returns decimal(15,6)as begindeclare @PriceNoTaxRMB decimal(15,6)select @PriceNoTaxRMB=CASE WHEN TL008='Y' THEN convert(decimal(18,8),TM010*ChgRate/TaxRate)*dbo.DFC_UnitConvert(MB001,1,MB004,TM009) else convert(decimal(18,8),TM010*ChgRate)*dbo.DFC_UnitConvert(MB001,1,MB004,TM009) End  from(select TOP 1 TL005,TL004,TL008,TM010,TM014,dbo.GetCurrExchangeRate(TL005) AS ChgRate,[dbo].[GetVendorTaxRate](TL004)+1 as TaxRate,TM009,MB004,MB001from PURTL INNER JOIN PURTM ON TL001=TM001 AND TL002=TM002     INNER JOIN  INVMB ON TM004=MB001where [email protected] AND TM011='Y'ORDER BY TM014 desc) areturn isnull(@PriceNoTaxRMB,0)end

3.展BOM程式

-- =============================================      -- Author: <David Gong>      -- Create date: <2011-07-01>      -- Description: <展BOM>      -- =============================================ALTER PROCEDURE [dbo].[UP_Bom_StandardCheck] (@ITEMNO VARCHAR(80))AS   BEGIN    --BOM展階  [email protected]:查詢品號 --返回:元件品號V1001,主件品號V1002,階碼V1004,階層次V1005,尾階標誌V1006,展開標誌V1007,組成用量V1011,底數V1012,損耗率%V1013 DECLARE @ITEM_CHILD VARCHAR(80), @ITEM_EXPAND VARCHAR(80), @ITEM_LEVELNO INT, @COUNT INT DECLARE @BATCH_NUMBER NUMERIC(18,6CREATE TABLE  #VIEW1 (V1001 VARCHAR(80), V1002 VARCHAR(80), V1004 VARCHAR(80) DEFAULT '', V1005 INT DEFAULT 0, V1006 CHAR(1) DEFAULT '0', V1007 CHAR(1) DEFAULT '0' , V1011 NUMERIC(18,6) DEFAULT 0, V1012 NUMERIC(18,6) DEFAULT 0, V1013 NUMERIC(18,6) DEFAULT 0)  SELECT @ITEM_LEVELNO = 1 INSERT INTO #VIEW1 (V1001, V1002, V1004, V1005, V1011, V1012, V1013) SELECT MD003, @ITEMNO, MD002, 1, MD006 , MD007*MC004, MD008 FROM BOMMC, BOMMD WHERE BOMMC.MC001 = BOMMD.MD001 AND MC001 = @ITEMNO DECLARE BOMEXPAND CURSOR FOR SELECT V1001, V1004, V1005, V1011/V1012 FROM #VIEW1 WHERE V1007 = '0' FOR UPDATE    OPEN BOMEXPAND  /* 開啟遊標 */  FETCH NEXT FROM BOMEXPAND INTO @ITEM_CHILD, @ITEM_EXPAND, @ITEM_LEVELNO, @BATCH_NUMBER WHILE @@FETCH_STATUS = 0   /* 用WHILE迴圈控制遊標活動*/  BEGIN  --SELECT @ITEM_LEVELNO, @ITEM_CHILD  UPDATE #VIEW1 SET V1007 = '1' WHERE CURRENT OF BOMEXPAND  SELECT @COUNT = COUNT(*) FROM BOMMC, BOMMD WHERE BOMMC.MC001 = BOMMD.MD001 AND MC001 = @ITEM_CHILD  IF @COUNT = 0    UPDATE #VIEW1 SET V1006 = '1' WHERE CURRENT OF BOMEXPAND  ELSE   BEGIN    INSERT INTO #VIEW1 (V1001, V1002, V1004, V1005, V1011, V1012, V1013)    SELECT MD003, @ITEM_CHILD, @ITEM_EXPAND + '.' + MD002, @ITEM_LEVELNO + 1, MD006 * @BATCH_NUMBER, MD007*MC004, MD008 FROM BOMMC, BOMMD WHERE BOMMC.MC001 = BOMMD.MD001 AND MC001 = @ITEM_CHILD   END  FETCH NEXT FROM BOMEXPAND INTO @ITEM_CHILD, @ITEM_EXPAND, @ITEM_LEVELNO, @BATCH_NUMBER /* 在迴圈體內將讀取其餘行資料 */ END CLOSE BOMEXPAND   /* 關閉遊標 */  DEALLOCATE BOMEXPAND   /* 刪除遊標 */   /*    SELECT space(V1005*1)+'.'+cast(V1005  as varchar)  as 階層,V1002 as 主件品號,V1001 as 元件品號,case when MB025='P' THEN '採購件'                                                                when MB025='M' THEN '自制件'                                                               when MB025='S' THEN '委外加工件'                                                               when MB025='Y' THEN '虛設件'                                                               else '配置件'END AS  品號屬性,         MB002 品名,MB003 規格,MB004 as 單位,V1011 as 組成用量,V1012 as 底數,V1013 as 損耗率,V1011/V1012*(1+V1013) as 累計組成用量  FROM #VIEW1 INNER JOIN  INVMB ON V1001=MB001 ORDER BY V1004 */ --背光成品只算半成品  SELECT V1002 as Pitem, V1001 as Item,MB025 as Property, MB002 as [Description], MB003 AS Spec, convert(decimal(15,5),V1011/V1012)  as Usage ,MB004 as Unit, ZE.[dbo].[PriceNoTaxRMBBOM](V1001) AS UnitCost  FROM #VIEW1 INNER JOIN  INVMB ON V1001=MB001 --where  (MB025='P' AND left(V1002,3)<>'203') or (MB025='M' AND LEFT(V1001,3)='203') ORDER BY V1004END

4.單位轉換函式:DFC_UnitConvert

ALTER FUNCTION [dbo].[DFC_UnitConvert] ( @ProductID char(20), @OldNum Numeric(16,6), @OldUnit char(4), @NewUnit char(4)='')RETURNS Numeric(16,6)ASBEGIN        DECLARE @mReturnNum Numeric(16,6),@mReturnUnit char(4)        DECLARE @mMA024 char(1),@mMA068 int,@mOldRate Numeric(10,6),@NewRate Numeric(10,6)  DECLARE @mMB004 char(4)  select @[email protected],@[email protected]         --數量表達方式(1.單一單位 2.雙單位 3.製造雙單位), 小數保留位數 SELECT top 1 @mMA024=MA024,@mMA068=MA068 FROM CMSMA with (nolock) if ((@mMA024 IS NULL) OR (@mMA024='2'))  RETURN round(@mReturnNum,@mMA068) if (@mMA068 IS NULL)  select @mMA068=3 --Check 品號是否存在於INVMB SELECT @mMB004=MB004 FROM INVMB WHERE [email protected] if @mMB004 is null  RETURN round(@mReturnNum,@mMA068) --單位(換算後)未傳值則預設為庫存單位(MB004) if (@NewUnit='')  select @[email protected] --如果單位(換算前)=單位(換算後),RETURN if @[email protected]  RETURN round(@mReturnNum,@mMA068) --計算單位(換算前)的比率 SELECT @mOldRate=ISNULL(MD004,0)/CASE WHEN ISNULL(MD003,0)=0 THEN 1 ELSE MD003 end     FROM INVMD WHERE [email protected] AND [email protected] if @[email protected]     select @mOldRate=1 if @mOldRate is null    RETURN round(@mReturnNum,@mMA068)    --RETURN round(@mReturnNum,@mMA068) --計算單位(換算後)的比率 SELECT @NewRate=ISNULL(MD003,0)/CASE WHEN ISNULL(MD004,0)=0 THEN 1 ELSE MD004 end     FROM INVMD WHERE [email protected] AND [email protected] if @[email protected]     select @NewRate=1 if @NewRate is null    RETURN round(@mReturnNum,@mMA068)     --換算後的數量=數量*A*B RETURN round(@OldNum*@mOldRate*@NewRate,@mMA068)END
4. T-SQL(可以封裝成儲存過程,提供給各程式呼叫)
 -- =============================================      -- Author: <David Gong>      -- Create date: <2015-03-15>      -- Description: <匯出所有BOM>      -- =============================================ALTER PROCEDURE [dbo].[UP_ExportBomList]AS   BEGIN  --建立臨時表 create table #AllBomList  (        Pitem  nchar(80) NOT NULL,        Layer  nchar(10) null,        Item   nchar(30)  NULL,        Property  nchar(1) NULL,        [Description]  nchar(256) NULL,        Spec  nchar(256) NULL,          Usage  decimal(18,6) NULL,        Unit  char(4) NULL,        UnitCost  decimal(18,6) NULL               )Declare @item as nchar(80), @Spec as nchar(256), @Description as nchar(256)Declare ItemCusror CURSOR local static FOR Select  MB001,MB002,MB003 from INVMB WHERE  MB109='Y' AND MB025='M' Open ItemCusrorFetch next From ItemCusrorInto @item,     @Description,     @SpecWhile(@@Fetch_Status = 0)      Begin             Begin                insert into #AllBomList(Pitem,[Description],Spec)values(@item,@Description,@Spec)--單獨區分成品                insert into #AllBomList  exec [UP_Bom_StandardCheck] @item --展BOM結果儲存到臨時表             End             Fetch next From ItemCusror             Into @item,  @Description,  @Spec      EndClose ItemCusrorDeallocate ItemCusrorselect * from #AllBomList; --查詢結果drop table #AllBomList;END
5.執行結果 :EXEC [UP_ExportBomList]

總結:執行了一下需要4分半左右的時間,基本可以接受。

問題:1.展BOM程式是採用遊標處理,且沒有考慮分量損耗的因素。2.最新核單也未考慮分量核價因素,且寫法不夠簡潔

 希望拋磚引玉,謝謝!

轉載請註明地址。