Excel+MSSQL开发金蝶K3欠料报表

将K3所有订单需求、MRP运算、库存及在途数据等数据整体展示,适合计划部门根据报表数据统筹考量物料供给情况,特制作此报表及Excel集成工具,以减少手工汇总统计的工作量。

本代码适用于金蝶K3 WISE 12.2版本

1、K3后台mssql数据库创建存储过程:

 

 
  1. create procedure [dbo].[sp_mymrp_must_sum]

  2. as

  3. set nocount on

  4.  
  5. create table #icmo

  6. (

  7. FTranType int,

  8. FOrgInterID int,

  9. FOrgEntryID int,

  10. id int identity(1,1),

  11. FInterid int default 0,

  12. FEntryid int default 0,

  13. FStatus int default 0,

  14. FBillno nvarchar(200),

  15. FDate datetime,

  16. FType int default 0,

  17. FItemid int default 0,

  18. FQty decimal(18,6) default 0,

  19. FDoQty decimal(18,6) default 0,

  20. FOrderInterid int default 0,

  21. FOrderType int default 0,

  22. FMyStuats int

  23. )

  24.  
  25. create table #icmolist

  26. (

  27. FIndex int default 0,

  28. FFromType int,

  29. id int identity(1,1),

  30. FIcmoInterid int,

  31. FDate datetime,

  32. FItemid int,

  33. FUnitQty decimal(18,6) default 0,

  34. FQty decimal(18,6) default 0,

  35. FOrgQty decimal(21,10) default 0,

  36. FStockedQty decimal(18,6) default 0,

  37. FDistributeQty decimal(18,6) default 0,

  38. FSourceType int,

  39. FSourceBillNO nvarchar(200),

  40. FSourceEntryid int,

  41. FAppItemInfo nvarchar(max) default '',

  42. FUnDistributeQty decimal(21,10) default 0

  43. )

  44.  
  45. create table #Source

  46. (

  47. id int identity(1,1),

  48. FSourceType int,--1 库存 2采购 3生产任务 4申请

  49. FSourceBillNo nvarchar(200),

  50. FDate datetime,

  51. FItemid int,

  52. FQty decimal(18,6),

  53. FDistributedQty decimal(18,6),

  54. FDistributeQty decimal(18,6),

  55. FSourceEntryid int,

  56. FStatus int

  57.  
  58. )

  59.  
  60. create table #result

  61. (

  62. id int identity(1,1),

  63. FicmolistID int,

  64. FSourceID int,

  65. FQty decimal(18,6)

  66. )

  67.  
  68.  
  69. create table #icbom

  70. (

  71. FID int identity(1,1),

  72. FItemid int

  73. )

  74.  
  75.  
  76. create table #icbomEntry

  77. (

  78. FID int identity(1,1),

  79. FParentID int,

  80. FItemID int,

  81. FLevel int,

  82. FQty decimal(21,10)

  83. )

  84.  
  85.  
  86. --导入任务单

  87.  
  88. --下达下任务单,物料不等于5

  89. insert into #icmo

  90. (FTranType,FOrgInterID,FOrgEntryID,FInterID,FEntryid,Ftype,FBillNo,FItemid,Fdate,Fqty,FDoQty,FStatus,FOrderInterid,FOrderType)

  91. select * from

  92. (

  93. select

  94. t1.Ftrantype,

  95. t1.Finterid as FOrgInterID,

  96. 0 as FOrgEntryID,

  97. t1.Finterid,

  98. 0 as FEntryid,

  99. 1054 as Ftype,

  100. t1.Fbillno as FBillNO,

  101. t1.FItemID as FItemid,

  102. t1.FPlanCommitDate as Fdate,

  103. fqty as Fqty,

  104. Fstockqty as FDoQty,

  105. t1.FStatus as FStatus,

  106. FOrderinterid+FPPorderinterid as FOrderInterid,

  107. case when FOrderinterid<>0 then 81 when FPPorderinterid<>0 then 87 else 0 end as FOrderType

  108. from icmo t1

  109. inner join t_icitem t2 on t1.Fitemid=t2.Fitemid

  110. where FMrpClosed=0 and (t1.FStatus in (1,2,5) or (t1.FStatus=0 and t1.FType<>1055))

  111. and t1.FCancellation=0

  112. union

  113. select

  114. t1.FClasstypeID as FTrantype,

  115. t1.FInterid as FOrgInterID,

  116. t2.FEntryID as FOrgEntryID,

  117. t1.Finterid,

  118. t2.FEntryID,

  119. 1067 as FType,

  120. t1.FBillNO as FBillNO,

  121. t2.FItemID,

  122. t2.FPayShipDate as Fdate,

  123. t2.FQty,

  124. t2.FStockQty as FDoQty,

  125. t1.FStatus as FStatus,

  126. 0 as FOrderInterid,

  127. 0 as FOrderType

  128. from ICSubContract t1

  129. inner join ICSubContractEntry t2 on t1.Finterid=t2.Finterid

  130. where FMrpClosed=0 and Fstatus in (1,2) and FCancellation=0

  131. ) a

  132. order by a.Fdate

  133.  
  134.  
  135. insert into #icmo

  136. (FTranType,FOrgInterID,FOrgEntryID,FInterID,FEntryid,Ftype,FBillNo,FItemid,Fdate,Fqty,FDoQty,FStatus,FOrderInterid,FOrderType)

  137. select * from (

  138. select

  139. t1.FClassTypeID as FTranType,

  140. t1.FInterID as FOrgInterID,

  141. t2.FEntryID as FOrgEntryID,

  142. t1.Finterid,

  143. t2.FEntryID,

  144. 1067 as Ftype,

  145. t1.FBillNO,

  146. t2.FItemID,

  147. t2.FPayShipDate as Fdate,

  148. t2.FQty,

  149. FStockQty as FDoQty,

  150. t1.FStatus,

  151. 0 as FOrderInterid,

  152. 0 as FOrderType

  153. from ICSubContract t1

  154. inner join ICSubContractEntry t2 on t1.Finterid=t2.Finterid

  155. inner join t_icitem t3 on t2.Fitemid=t3.fitemid

  156. where FMrpClosed=0 and Fstatus=0 and FCancellation=0

  157. ) a

  158. order by a.Fdate

  159.  
  160. --计划状态下任务单

  161. insert into #ICMO

  162. (FTranType,FOrgInterID,FOrgEntryID,Finterid,FEntryid,Ftype,FBillNo,FItemid,Fdate,Fqty,FDoQty,FStatus,FOrderInterid,FOrderType)

  163. select

  164. t1.FTrantype,

  165. t1.FinterID as FOrgInterID,

  166. t2.FEntryID as FOrgEntryID,

  167. t1.Finterid,

  168. t2.Fentryid,

  169. 70 as Ftype,

  170. t1.FBillNo,

  171. t2.FItemID,

  172. t2.FAPurchTime as FDate,

  173. t2.FQty,

  174. t2.FCommitQty,

  175. 0,

  176. t2.FSourceInterid,

  177. t2.FSourceTrantype

  178. from porequest t1

  179. inner join porequestEntry t2 on t1.Finterid=t2.Finterid

  180. inner join t_icitem t3 on t3.Fitemid=t2.fitemid

  181. where FCancellation=0 and t1.FMrpclosed=0 and t2.FMrpclosed=0 and t1.FBizType=12511

  182. order by t2.FFetchTime

  183.  
  184.  
  185.  
  186.  
  187. --展开BOM表

  188.  
  189. insert into #icbom

  190. (FItemid)

  191. select

  192. distinct Fitemid from #icmo where FStatus=0

  193.  
  194.  
  195. declare @FLevel int

  196. set @FLevel=0

  197.  
  198.  
  199. insert into #icbomEntry

  200. (

  201. FParentID,FItemID,FLevel,FQty

  202. )

  203. select

  204. u1.FID,t2.FItemID,@FLevel,(t2.FQty/t1.FQty)*(1+t2.FScrap/100) from #icbom u1

  205. inner join ICBOM t1 on u1.FItemid=t1.FItemID

  206. inner join ICBOMChild t2 on t2.FInterID=t1.FInterID and t1.FUseStatus=1072

  207.  
  208. while @FLevel<20 and

  209. exists(select 1 from #icbomEntry u1 where u1.Fitemid in (select Fitemid from icbom where Fbomskip=1058) and u1.FLevel=@FLevel)

  210. begin

  211.  
  212.  
  213. set @FLevel=@FLevel+1

  214.  
  215. insert into #icbomEntry

  216. (

  217. FParentID,FItemID,FLevel,FQty

  218. )

  219. select

  220. u1.FParentID,t2.FItemID,@FLevel,u1.FQty*(t2.FQty/t1.FQty)*(1+t2.FScrap/100)

  221. from #icbomEntry u1

  222. inner join ICBOM t1 on u1.FItemid=t1.FItemID and t1.FUseStatus=1072

  223. inner join ICBOMChild t2 on t2.FInterID=t1.FInterID

  224. where u1.FLevel=@FLevel-1 and t1.FBomSkip=1058

  225.  
  226.  
  227. delete u1

  228. from #icbomEntry u1

  229. inner join ICBOM t1 on u1.FItemid=t1.FItemID and t1.FUseStatus=1072

  230. where u1.FLevel=@FLevel-1 and t1.FBomSkip=1058

  231. end

  232.  
  233.  
  234. --生产需发料明细

  235. insert into #icmolist

  236. (FFromType,FIcmoInterid,FDate,FItemid,FUnitQty,FQty,FDistributeQty,FOrgQty)

  237. select FFromType,id,FDate,FItemID,FUnitQty,FQty,FQty,FQty from

  238. (

  239. select

  240. 0 as FFromType,u1.id,u1.FDate,t2.FItemid,t2.FqtyScrap*(1+t2.Fscrap/100) as FUnitQty,t2.FqtyMust-t2.FStockQty+t2.FDiscardQty as FQty

  241. from #icmo u1

  242. inner join PPBOM t1 on u1.FInterid=t1.FICMOInterID and u1.FEntryid=t1.FOrderEntryID and u1.FType=t1.FType

  243. inner join PPBOMEntry t2 on t1.FInterID=t2.FInterID

  244. where u1.FStatus<>0 and t2.FMaterielType=371

  245. union all

  246. select

  247. 1 as FFromType,u1.id,u1.FDate,t2.FItemID,sum(t2.Fqty) as FUnitQty,sum(t2.FQty*(u1.FQty-u1.FDoQty)) as FQty

  248. from #icmo u1

  249. inner join #ICBOM t1 on u1.FItemid=t1.FItemID

  250. inner join #icbomEntry t2 on t1.FID=t2.FParentID

  251. inner join t_icitem t3 on t3.Fitemid=t2.Fitemid

  252. where u1.FStatus=0 and t3.Ferpclsid<>5

  253. group by u1.id,u1.FDate,t2.FItemID

  254. ) a

  255. order by a.id

  256.  
  257.  
  258. --产品预测单

  259. insert into #icmo

  260. (Finterid,FEntryid,Ftype,FBillno,Fitemid,Fdate,Fqty,Fdoqty,FStatus)

  261. select

  262. Finterid,0,Ftrantype,fbillno,0,Fdate,0,0,0

  263. from pporder

  264. where isnull(FHeadSelfY0123,0)=40077 and Finterid in

  265. (

  266. select Finterid from pporderentry where FOrderClosed=0

  267. )

  268.  
  269.  
  270. insert into #icmolist

  271. (FIcmoInterid,FDate,FItemid,FUnitQty,FQty,FDistributeQty)

  272. select

  273. u1.id,t1.FNeedDate,t1.Fitemid,1,t1.Fqty,t1.Fqty

  274. from #icmo u1

  275. inner join pporderentry t1 on u1.Finterid=t1.Finterid and u1.FType=87

  276. where t1.FOrderClosed=0

  277.  
  278. --替代清单调整

  279. --处理任务和委外替代清单

  280. insert into #icmolist

  281. (FFromType,FicmointerID,FDate,FitemID,FUnitQty,FQty,FDistributeQty,FSourceType,FSourceBillno,FsourceEntryID)

  282. select

  283. t1.FFromType,t1.FIcmoInterid,t1.FDate,t4.FSubsItemID,t1.Funitqty*t4.FSubsQty/t1.Fqty,t4.FSubsQty,t4.FSubsQty,t1.FSourceType,t1.FSourceBillNO,t1.FSourceEntryid

  284. from #icmo u1

  285. inner join #icmolist t1 on u1.id=t1.FicmoInterID

  286. inner join ICSubsItemBill t2 on t2.FUpperBillID=u1.FOrgInterID and t2.FUpperType in (85)

  287. inner join ICSubsItemBillEntry t3 on t3.FID=t2.FID and t3.FGroupID=0 and t3.FItemID=t1.FItemid

  288. inner join ICSubsItemBillEntry t4 on t4.fid=t2.FID and t4.FGroupID=1

  289. where t4.FSubsQty<t1.FOrgQty and t2.FStatus=1 and t1.FFromType=1

  290.  
  291.  
  292. update t1

  293. set t1.FQty=t1.FQty-t4.FSubsQty,t1.FUnitQty=t1.FUnitQty*(t1.FQty-t4.FSubsQty)/t1.FQty,

  294. t1.FDistributeQty=t1.FQty-t4.FSubsQty

  295. from #icmo u1

  296. inner join #icmolist t1 on u1.id=t1.FicmoInterID

  297. inner join ICSubsItemBill t2 on t2.FUpperBillID=u1.FOrgInterID and t2.FUpperType in (85)

  298. inner join ICSubsItemBillEntry t3 on t3.FID=t2.FID and t3.FGroupID=0 and t3.FItemID=t1.FItemid

  299. inner join ICSubsItemBillEntry t4 on t4.fid=t2.FID and t4.FGroupID=1

  300. where t4.FSubsQty<t1.FOrgQty and t2.FStatus=1 and t1.FFromType=1

  301.  
  302.  
  303. update t1

  304. set t1.FItemid=t4.FSubsItemID

  305. from #icmo u1

  306. inner join #icmolist t1 on u1.id=t1.FicmoInterID

  307. inner join ICSubsItemBill t2 on t2.FUpperBillID=u1.FOrgInterID and t2.FUpperType in (85)

  308. inner join ICSubsItemBillEntry t3 on t3.FID=t2.FID and t3.FGroupID=0 and t3.FItemID=t1.FItemid

  309. inner join ICSubsItemBillEntry t4 on t4.fid=t2.FID and t4.FGroupID=1

  310. where t4.FSubsQty>=t1.FOrgQty and t2.FStatus=1 and t1.FFromType=1

  311.  
  312.  
  313. --委外

  314. insert into #icmolist

  315. (FFromType,FicmointerID,FDate,FitemID,FUnitQty,FQty,FDistributeQty,FSourceType,FSourceBillno,FsourceEntryID)

  316. select

  317. t1.FFromType,t1.FIcmoInterid,t1.FDate,t4.FSubsItemID,t1.Funitqty*t4.FSubsQty/t1.Fqty,t4.FSubsQty,t4.FSubsQty,t1.FSourceType,t1.FSourceBillNO,t1.FSourceEntryid

  318. from #icmo u1

  319. inner join #icmolist t1 on u1.id=t1.FicmoInterID

  320. inner join ICSubsItemBill t2 on t2.FUpperBillID=u1.FOrgInterID and t2.FUpperType in (1007105) and t2.FUpperEntryID=u1.FOrgEntryID

  321. inner join ICSubsItemBillEntry t3 on t3.FID=t2.FID and t3.FGroupID=0 and t3.FItemID=t1.FItemid

  322. inner join ICSubsItemBillEntry t4 on t4.fid=t2.FID and t4.FGroupID=1

  323. where t4.FSubsQty<t1.FOrgQty and t2.FStatus=1 and t1.FFromType=1

  324.  
  325.  
  326. update t1

  327. set t1.FQty=t1.FQty-t4.FSubsQty,t1.FUnitQty=t1.FUnitQty*(t1.FQty-t4.FSubsQty)/t1.FQty,

  328. t1.FDistributeQty=t1.FQty-t4.FSubsQty

  329. from #icmo u1

  330. inner join #icmolist t1 on u1.id=t1.FicmoInterID

  331. inner join ICSubsItemBill t2 on t2.FUpperBillID=u1.FOrgInterID and t2.FUpperType in (1007105) and t2.FUpperEntryID=u1.FOrgEntryID

  332. inner join ICSubsItemBillEntry t3 on t3.FID=t2.FID and t3.FGroupID=0 and t3.FItemID=t1.FItemid

  333. inner join ICSubsItemBillEntry t4 on t4.fid=t2.FID and t4.FGroupID=1

  334. where t4.FSubsQty<t1.FOrgQty and t2.FStatus=1 and t1.FFromType=1

  335.  
  336.  
  337. update t1

  338. set t1.FItemid=t4.FSubsItemID

  339. from #icmo u1

  340. inner join #icmolist t1 on u1.id=t1.FicmoInterID

  341. inner join ICSubsItemBill t2 on t2.FUpperBillID=u1.FOrgInterID and t2.FUpperType in (1007105) and t2.FUpperEntryID=u1.FOrgEntryID

  342. inner join ICSubsItemBillEntry t3 on t3.FID=t2.FID and t3.FGroupID=0 and t3.FItemID=t1.FItemid

  343. inner join ICSubsItemBillEntry t4 on t4.fid=t2.FID and t4.FGroupID=1

  344. where t4.FSubsQty>=t1.FOrgQty and t2.FStatus=1 and t1.FFromType=1

  345.  
  346.  
  347. --计划订单

  348.  
  349.  
  350.  
  351. insert into #icmolist

  352. (FFromType,FicmointerID,FDate,FitemID,FUnitQty,FQty,FDistributeQty,FSourceType,FSourceBillno,FsourceEntryID)

  353. select

  354. t1.FFromType,t1.FIcmoInterid,t1.FDate,t4.FSubsItemID,t1.Funitqty*t4.FSubsQty/t1.Fqty,t4.FSubsQty,t4.FSubsQty,t1.FSourceType,t1.FSourceBillNO,t1.FSourceEntryid

  355. from #icmo u1

  356. inner join #icmolist t1 on u1.id=t1.FicmoInterID

  357. left join ICMO t5 on t5.FInterID=u1.FOrgInterID and t5.FTranType=u1.FTranType

  358. inner join ICSubsItemBill t2 on (t2.FUpperBillID=u1.FOrgInterID or t2.FUpperBillID=t5.FPlanOrderInterID)

  359. and t2.FUpperType in (500)

  360. inner join ICSubsItemBillEntry t3 on t3.FID=t2.FID and t3.FGroupID=0 and t3.FItemID=t1.FItemid

  361. inner join ICSubsItemBillEntry t4 on t4.fid=t2.FID and t4.FGroupID=1

  362. where t4.FSubsQty<t1.FOrgQty and t2.FStatus=1 and t1.FFromType=1

  363.  
  364.  
  365. update t1

  366. set t1.FQty=t1.FQty-t4.FSubsQty,t1.FUnitQty=t1.FUnitQty*(t1.FQty-t4.FSubsQty)/t1.FQty,

  367. t1.FDistributeQty=t1.FQty-t4.FSubsQty

  368. from #icmo u1

  369. inner join #icmolist t1 on u1.id=t1.FicmoInterID

  370. left join ICMO t5 on t5.FInterID=u1.FOrgInterID and t5.FTranType=u1.FTranType

  371. inner join ICSubsItemBill t2 on (t2.FUpperBillID=u1.FOrgInterID or t2.FUpperBillID=t5.FPlanOrderInterID)

  372. and t2.FUpperType in (500)

  373. inner join ICSubsItemBillEntry t3 on t3.FID=t2.FID and t3.FGroupID=0 and t3.FItemID=t1.FItemid

  374. inner join ICSubsItemBillEntry t4 on t4.fid=t2.FID and t4.FGroupID=1

  375. where t4.FSubsQty<t1.FOrgQty and t2.FStatus=1 and t1.FFromType=1

  376.  
  377.  
  378.  
  379. update t1

  380. set t1.FItemid=t4.FSubsItemID

  381. from #icmo u1

  382. inner join #icmolist t1 on u1.id=t1.FicmoInterID

  383. left join ICMO t5 on t5.FInterID=u1.FOrgInterID and t5.FTranType=u1.FTranType

  384. inner join ICSubsItemBill t2 on (t2.FUpperBillID=u1.FOrgInterID or t2.FUpperBillID=t5.FPlanOrderInterID)

  385. and t2.FUpperType in (500)

  386. inner join ICSubsItemBillEntry t3 on t3.FID=t2.FID and t3.FGroupID=0 and t3.FItemID=t1.FItemid

  387. inner join ICSubsItemBillEntry t4 on t4.fid=t2.FID and t4.FGroupID=1

  388. where t4.FSubsQty>=t1.FOrgQty and t2.FStatus=1 and t1.FFromType=1

  389.  
  390.  
  391. --采购申请

  392.  
  393.  
  394. insert into #icmolist

  395. (FFromType,FicmointerID,FDate,FitemID,FUnitQty,FQty,FDistributeQty,FSourceType,FSourceBillno,FsourceEntryID)

  396. select

  397. t1.FFromType,t1.FIcmoInterid,t1.FDate,t4.FSubsItemID,t1.Funitqty*t4.FSubsQty/t1.Fqty,t4.FSubsQty,t4.FSubsQty,t1.FSourceType,t1.FSourceBillNO,t1.FSourceEntryid

  398. from #icmo u1

  399. inner join #icmolist t1 on u1.id=t1.FicmoInterID

  400. left join ICSubContractEntry t5 on t5.FInterID=u1.FOrgInterID and 1007105=u1.FTranType

  401. inner join ICSubsItemBill t2 on (

  402. (t2.FUpperBillID=u1.FOrgInterID and t2.FUpperEntryID=u1.FOrgEntryID and u1.FTranType=70)

  403. or

  404. (t2.FUpperBillID=t5.FInterID_SRC and t2.FUpperEntryID=t5.FEntryID_SRC and t5.FClassTypeID_SRC=-70)

  405. )

  406. and t2.FUpperType in (70)

  407. inner join ICSubsItemBillEntry t3 on t3.FID=t2.FID and t3.FGroupID=0 and t3.FItemID=t1.FItemid

  408. inner join ICSubsItemBillEntry t4 on t4.fid=t2.FID and t4.FGroupID=1

  409. where t4.FSubsQty<t1.FOrgQty and t2.FStatus=1 and t1.FFromType=1

  410.  
  411.  
  412. update t1

  413. set t1.FQty=t1.FQty-t4.FSubsQty,t1.FUnitQty=t1.FUnitQty*(t1.FQty-t4.FSubsQty)/t1.FQty,

  414. t1.FDistributeQty=t1.FQty-t4.FSubsQty

  415. from #icmo u1

  416. inner join #icmolist t1 on u1.id=t1.FicmoInterID

  417. left join ICSubContractEntry t5 on t5.FInterID=u1.FOrgInterID and 1007105=u1.FTranType

  418. inner join ICSubsItemBill t2 on (

  419. (t2.FUpperBillID=u1.FOrgInterID and t2.FUpperEntryID=u1.FOrgEntryID and u1.FTranType=70)

  420. or

  421. (t2.FUpperBillID=t5.FInterID_SRC and t2.FUpperEntryID=t5.FEntryID_SRC and t5.FClassTypeID_SRC=-70)

  422. )

  423. and t2.FUpperType in (70)

  424. inner join ICSubsItemBillEntry t3 on t3.FID=t2.FID and t3.FGroupID=0 and t3.FItemID=t1.FItemid

  425. inner join ICSubsItemBillEntry t4 on t4.fid=t2.FID and t4.FGroupID=1

  426. where t4.FSubsQty<t1.FOrgQty and t2.FStatus=1 and t1.FFromType=1

  427.  
  428.  
  429.  
  430. update t1

  431. set t1.FItemid=t4.FSubsItemID

  432. from #icmo u1

  433. inner join #icmolist t1 on u1.id=t1.FicmoInterID

  434. left join ICSubContractEntry t5 on t5.FInterID=u1.FOrgInterID and 1007105=u1.FTranType

  435. inner join ICSubsItemBill t2 on (

  436. (t2.FUpperBillID=u1.FOrgInterID and t2.FUpperEntryID=u1.FOrgEntryID and u1.FTranType=70)

  437. or

  438. (t2.FUpperBillID=t5.FInterID_SRC and t2.FUpperEntryID=t5.FEntryID_SRC and t5.FClassTypeID_SRC=-70)

  439. )

  440. and t2.FUpperType in (70)

  441. inner join ICSubsItemBillEntry t3 on t3.FID=t2.FID and t3.FGroupID=0 and t3.FItemID=t1.FItemid

  442. inner join ICSubsItemBillEntry t4 on t4.fid=t2.FID and t4.FGroupID=1

  443. where t4.FSubsQty>=t1.FOrgQty and t2.FStatus=1 and t1.FFromType=1

  444.  
  445.  
  446.  
  447.  
  448. --导入资源

  449. --导入库存

  450. insert into

  451. #Source

  452. (FSourceType,Fdate,Fitemid,Fqty,FDistributedQty)

  453. select

  454. 1,

  455. '1988-08-08',

  456. t1.FItemID,

  457. sum(t1.Fqty) as Fqty,

  458. sum(t1.Fqty)

  459. from ICInventory t1

  460. inner join t_Stock t3 on t1.FStockID=t3.FItemID

  461. where t3.FMRPAvail=1

  462. group by t1.FItemID

  463.  
  464.  
  465.  
  466.  
  467. insert into

  468. #Source

  469. (FSourceType,Fstatus,FItemid,Fdate,Fqty,FDistributedQty,FSourceBillNo,FSourceEntryid)

  470. select

  471. 4,

  472. t2.Fstatus,

  473. t1.FItemID,

  474. t1.Fdate,

  475. t1.fqty-t1.fstockqty,

  476. t1.fqty-t1.fstockqty,

  477. t2.FbillNO,

  478. t1.Fentryid

  479. from poorder t2

  480. inner join poorderentry t1 on t1.finterid=t2.finterid

  481. inner join t_icitem t3 on t1.Fitemid=t3.Fitemid

  482. where t2.FCancellation=0 and t1.FMrpClosed=0 and t1.Fqty-t1.FStockQty<>0

  483. order by t1.FDate

  484.  
  485.  
  486. insert into

  487. #Source

  488. (FSourceType,FItemid,Fdate,Fqty,FDistributedQty,FSourceBillNo,FSourceEntryid)

  489. select

  490. 6,

  491. t2.FItemID,

  492. t2.FFetchTime,

  493. t2.FQty-t2.FCommitQty,

  494. t2.FQty-t2.FCommitQty,

  495. t1.Fbillno,

  496. t2.Fentryid

  497. from porequest t1

  498. inner join porequestEntry t2 on t1.Finterid=t2.Finterid

  499. where FCancellation=0 and t2.FMrpclosed=0 and t1.FBizType=12510

  500.  
  501.  
  502. insert into

  503. #Source

  504. (FSourceType,FStatus,FItemid,Fdate,Fqty,FDistributedQty,FSourceBillNo)

  505. select

  506. 5,

  507. t1.FStatus,

  508. t1.FItemID,

  509. t1.FPlanFinishDate,

  510. fqty-FstockQty,

  511. fqty-FstockQty,

  512. t1.Fbillno

  513. from icmo t1

  514. where t1.FClosed=0 and FMrpClosed=0 and t1.FCancellation=0 and t1.Fstatus in (1,2,5)

  515. order by t1.Fstatus,t1.FPlanFinishDate

  516.  
  517.  
  518. insert into

  519. #Source

  520. (FSourceType,FStatus,FItemid,Fdate,Fqty,FDistributedQty,FSourceBillNo)

  521. select

  522. 5,

  523. t1.FStatus,

  524. t1.FItemID,

  525. t1.FPlanFinishDate,

  526. fqty-FstockQty,

  527. fqty-FstockQty,

  528. t1.Fbillno

  529. from icmo t1

  530. where t1.FClosed=0 and FMrpClosed=0 and t1.FCancellation=0 and t1.Fstatus=0

  531. order by t1.Fstatus,t1.FPlanFinishDate

  532.  
  533. --委外订单

  534. insert into

  535. #Source

  536. (FSourceType,FItemid,Fdate,Fqty,FDistributedQty,FSourceBillNo)

  537. select

  538. 7,

  539. t2.FItemID,

  540. t2.FFetchDate,

  541. t2.FQty-FStockQty,

  542. t2.FQty-FStockQty,

  543. t1.FBillNO

  544. from ICSubContract t1

  545. inner join ICSubContractEntry t2 on t1.Finterid=t2.Finterid

  546. where FClosed=0 and FMrpClosed=0 and FCancellation=0

  547.  
  548. --委外申请

  549. insert into

  550. #Source

  551. (FSourceType,FItemid,Fdate,Fqty,FDistributedQty,FSourceBillNo)

  552. select

  553. 8,

  554. t2.FItemID,

  555. t2.FFetchTime,

  556. t2.FQty-t2.FCommitQty,

  557. t2.FQty-t2.FCommitQty,

  558. t1.FBillNo

  559. from porequest t1

  560. inner join porequestEntry t2 on t1.Finterid=t2.Finterid

  561. inner join t_icitem t3 on t3.Fitemid=t2.fitemid

  562. where FCancellation=0 and t1.FMrpclosed=0 and t2.FMrpclosed=0 and t1.FBizType=12511

  563.  
  564.  
  565.  
  566.  
  567. --分配资源

  568. create table #sn

  569. (id int identity(1,1),

  570. FItemid int default 0)

  571.  
  572. insert #sn(FItemid)

  573. select

  574. distinct

  575. FItemid

  576. from

  577. (

  578. select Fitemid from #Source

  579. union

  580. select Fitemid from #icmolist

  581. ) a

  582.  
  583.  
  584. declare @source_sn int

  585. declare @source_id int

  586. declare @source_qty decimal(18,6)

  587. declare @source_fetch_next int

  588. declare @source_fetch_status int

  589.  
  590. declare @dts_sn int

  591. declare @dts_id int

  592. declare @dts_qty decimal(18,6)

  593. declare @dts_fetch_next int

  594. declare @dts_fetch_status int

  595.  
  596. declare cur_source cursor for

  597. select t1.id,u1.id as FDtsID,u1.FQty from #Source u1

  598. inner join #sn t1 on u1.FItemid=t1.FItemid

  599. where u1.Fqty>0

  600. order by t1.id,u1.id

  601.  
  602.  
  603. declare cur_dts cursor for

  604. select t1.id,u1.id as FSourceID,u1.FQty from #icmolist u1

  605. inner join #sn t1 on u1.FItemid=t1.FItemid

  606. where u1.Fqty>0

  607. order by t1.id,u1.id

  608.  
  609. open cur_source

  610. open cur_dts

  611.  
  612. fetch next from cur_source into @source_sn,@source_id,@source_qty

  613.  
  614. set @source_fetch_status=@@FETCH_STATUS

  615. set @source_fetch_next=0

  616.  
  617. fetch next from cur_dts into @dts_sn,@dts_id,@dts_qty

  618.  
  619. set @dts_fetch_status=@@FETCH_STATUS

  620. set @dts_fetch_next=0

  621.  
  622. while @source_fetch_status=0 and @dts_fetch_status=0

  623. begin

  624.  
  625. if @dts_sn=@source_sn and @dts_sn<>0 and @source_sn<>0

  626. begin

  627.  
  628.  
  629. if @dts_qty>@source_qty and @dts_qty>0 and @source_qty>0

  630. begin

  631. insert into #result

  632. (FicmolistID,FSourceID,FQty)

  633. values

  634. (@dts_id,@source_id,@source_qty)

  635.  
  636.  
  637. set @dts_qty=@dts_qty-@source_qty

  638. set @source_qty=0

  639. set @source_id=0

  640. set @source_sn=0

  641.  
  642. set @source_fetch_next=1

  643.  
  644. end

  645.  
  646. if @dts_qty=@source_qty and @dts_qty>0 and @source_qty>0

  647. begin

  648.  
  649. insert into #result

  650. (FicmolistID,FSourceID,FQty)

  651. values

  652. (@dts_id,@source_id,@source_qty)

  653.  
  654. set @source_qty=0

  655. set @source_id=0

  656. set @source_sn=0

  657.  
  658. set @dts_qty=0

  659. set @dts_id=0

  660. set @dts_sn=0

  661.  
  662. set @source_fetch_next=1

  663. set @dts_fetch_next=1

  664.  
  665. end

  666.  
  667. if @dts_qty<@source_qty and @dts_qty>0 and @source_qty>0

  668. begin

  669.  
  670. insert into #result

  671. (FicmolistID,FSourceID,FQty)

  672. values

  673. (@dts_id,@source_id,@dts_qty)

  674.  
  675. set @source_qty=@source_qty-@dts_qty

  676. set @dts_qty=0

  677. set @dts_sn=0

  678. set @dts_id=0

  679.  
  680. set @dts_fetch_next=1

  681.  
  682. end

  683.  
  684. end

  685.  
  686. if @dts_sn>@source_sn

  687. begin

  688. set @source_fetch_next=1

  689. end

  690.  
  691. if @dts_sn<@source_sn

  692. begin

  693.  
  694. set @dts_fetch_next=1

  695.  
  696. end

  697.  
  698. if @source_qty<=0 or @source_sn=0

  699. begin

  700. set @source_fetch_next=1

  701. end

  702.  
  703. if @dts_qty<=0 or @dts_sn=0

  704. begin

  705. set @dts_fetch_next=1

  706. end

  707.  
  708. if @source_fetch_next=1

  709. begin

  710.  
  711. fetch next from cur_source into @source_sn,@source_id,@source_qty

  712. set @source_fetch_status=@@FETCH_STATUS

  713. set @source_fetch_next=0

  714.  
  715. end

  716.  
  717. if @dts_fetch_next=1

  718. begin

  719.  
  720. fetch next from cur_dts into @dts_sn,@dts_id,@dts_qty

  721.  
  722. set @dts_fetch_status=@@FETCH_STATUS

  723. set @dts_fetch_next=0

  724.  
  725. end

  726.  
  727. end

  728.  
  729. close cur_source

  730. close cur_dts

  731.  
  732. deallocate cur_source

  733. deallocate cur_dts

  734.  
  735. update u1

  736. set u1.Fdistributedqty=u1.FQty-t1.FQty

  737. from #Source u1

  738. inner join

  739. (

  740. select FSourceID,SUM(FQty) as FQty from #result group by FSourceID

  741. ) t1 on u1.id=t1.FSourceID

  742.  
  743.  
  744.  
  745. update u1

  746. set u1.FUnDistributeQty=u1.FQty-isnull(t1.FQty,0)

  747. from #icmolist u1

  748. left join

  749. (

  750. select FicmolistID,SUM(FQty) as FQty from #result

  751. where FSourceID in

  752. (select id from #Source where FSourceType=1)

  753. group by FicmolistID

  754. ) t1 on u1.id=t1.FicmolistID

  755.  
  756.  
  757. update u1

  758. set u1.FDistributeQty=u1.FQty-isnull(t1.FQty,0)

  759. from #icmolist u1

  760. left join

  761. (

  762. select FicmolistID,SUM(FQty) as FQty from #result group by FicmolistID

  763. ) t1 on u1.id=t1.FicmolistID

  764.  
  765.  
  766.  
  767.  
  768. update u1

  769. set u1.FMyStuats=1

  770. from #icmo u1

  771. inner join

  772. (

  773. select FIcmoInterid,max(Fdistributeqty) as FMinDistributeQty from #icmolist group by FIcmoInterid

  774. ) t1 on u1.id=t1.FIcmoInterid and isnull(t1.FMinDistributeQty,0)=0

  775. and u1.id not in

  776. (

  777. select FIcmoInterid from #icmolist

  778. where id in

  779. (

  780. select FicmolistID from #result

  781. where FSourceID in

  782. (

  783. select id from #Source where FSourceType<>1

  784. )

  785. )

  786. )

  787.  
  788.  
  789.  
  790. --跟踪表

  791. select

  792. t1.Findex,

  793. isnull(t8.fbillno,'')+isnull(t9.fbillno,'') as 订单编号,

  794. case u1.Ftype when 1067 then '委外订单' when 1054 then '生产任务单' when 70 then '委外申请' when 500 then '计划订单' when 87 then '预测单' else '' end as 单据类型,

  795. u1.Fbillno as 单据编号,

  796. t7_1.FName as 生产车间,

  797. t501.Fname as 产品PMC负责人,

  798. t1.Fdate as 计划开工时间,

  799. isnull(t6.F_122,0) as 提前期偏置,

  800. year(dbo.FN_getPreWorkDay(t1.Fdate,abs(isnull(t6.F_122,0)))) as FYear,

  801. month(dbo.FN_getPreWorkDay(t1.Fdate,abs(isnull(t6.F_122,0)))) as FMonth,

  802. t7.FPlanFinishdate as 预计完工日期,

  803. t5.Fnumber as 产品代码,

  804. t5.Fname as 产品名称,

  805. t5.Fmodel as 产品规格型号,

  806. u1.Fqty as 生产数量,

  807. u1.Fdoqty as 已生产数量,

  808. case u1.Fstatus when 0 then '计划' when 1 then '下达' when 2 then '下达' when 3 then '结案' when 5 then '确认' else '' end as 状态,

  809. case when u1.FMyStuats=1 then '齐料' else '' end 齐料状态,

  810. t6.Fnumber as 子项物料代码,

  811. t6.Fname as 子项物料名称,

  812. t6.Fmodel as 子项规格型号,

  813. case t6.FerpClsID when 1 then '外购' when 2 then '自制' when 3 then '委外加工' else '' end as 物料类型,

  814. t1.Funitqty as 单位用量,

  815. t1.Fqty as 需发料数量,

  816. t1.FUnDistributeQty 欠料数,

  817. t1.Fdistributeqty as 未分配数量,

  818. a.Fqty as 未分配总数,

  819. t11.FQty as 在检数量,

  820. case t4.FSourceType when 1 then '' when 2 then '在检' when 3 then '电芯搁置' when 4 then '采购订单' when 5 then '生产任务单' when 6 then '采购申请' when 7 then '委外订单' when 8 then '委外申请' else '' end as 源单据类型,

  821. t4.Fsourcebillno as 单据编码,

  822. t4.FSourceEntryid as 行号,

  823. a1.Fdate as 下单日期,

  824. b2.FBillNO as 申购单号,

  825. b2.Fdate as 申购日期,

  826. b1.FFEtchTime as PR到料日期 ,

  827. t4.Fdate as 交货日期,

  828. a2.FRKReplayDate 交期回复,

  829. a2.FRKReplayNote 交期备注,

  830. case t4.FSourceType when 5 then case t4.Fstatus when 0 then '计划' when 1 then '下达' when 2 then '下达' when 3 then '结案' when 5 then '确认' else '' end else '' end as 源单状态,

  831. t4.Fqty as 可分配数量,

  832. t4.Fdistributedqty as 剩余数量,

  833. t3.Fqty as 分配数量,

  834. a3.Fname as 供应商,

  835. --a2.FPrice as 单价,

  836. a2.FNote as 备注信息,

  837. t4.FSourceType

  838. into #report

  839. from #icmo u1

  840.  
  841. inner join #icmolist t1 on u1.id=t1.FIcmoInterid

  842. left join

  843. (

  844. select Fitemid,sum(FDistributeQty) as Fqty from #icmolist group by FItemid

  845. ) a on a.FItemid=t1.FItemid

  846. left join t_ICItem t5 on t5.FItemID=u1.FItemid

  847. left join t_Item t501 on t501.FItemID=t5.FPlanner

  848. left join t_ICItem t6 on t6.FItemID=t1.FItemid

  849. left join t_Item t601 on t601.FItemID=t6.FPlanner

  850. left join t_Item t602 on t602.FItemID=t6.FOrderRector

  851. left join ICMO t7 on t7.FInterID=u1.FInterid and u1.FType=1054--增加1055返工产品

  852. left join t_department t7_1 on t7_1.Fitemid=t7.FWorkShop

  853. left join PPOrder t8 on u1.FOrderInterid=t8.FInterID and u1.FOrderType=87

  854. left join SEOrder t9 on u1.FOrderInterid=t9.FInterID and u1.FOrderType=81

  855. left join #result t3 on t3.FicmolistID=t1.id

  856. left join #Source t4 on t4.id=t3.FSourceID

  857. left join POOrder a1 on a1.FBillNo=t4.FSourceBillNo and t4.FSourceType=4 and a1.FCancellation=0

  858. left join t_Item a3 on a3.FItemID=a1.FSupplyID

  859. left join POOrderEntry a2 on a1.FInterID=a2.FInterID and a2.FEntryID=t4.FSourceEntryid

  860. left join PORequestEntry b1 on b1.FInterID=a2.FSourceInterId and a2.FSourceEntryID=b1.FEntryID

  861. left join PORequest b2 on b1.FInterID=b2.FInterID and b2.FTranType=a2.FSourceTranType

  862. left join

  863. (

  864. select Fitemid,sum(FQty) as FQty from poinventory where FQty>0 group by Fitemid

  865. ) t11 on t11.Fitemid=t1.FItemID

  866. where t1.Fqty<>0 and not ((isnull(t4.FSourceType,0)=1 and isnull(t1.Fdistributeqty,0)=0) or (isnull(t4.FSourceType,0)=1 and t1.Fqty-isnull(t1.Fdistributeqty,0)<>t3.Fqty))

  867. order by u1.id,t4.FSourceType

  868.  
  869.  
  870.  
  871. alter table #report

  872. add 欠料总计 nvarchar(100)

  873.  
  874. insert into #report

  875. (Findex,订单编号,单据类型,子项物料代码,子项物料名称,子项规格型号,欠料数,状态,齐料状态,物料类型,源单据类型,源单状态,在检数量,欠料总计,FYear,FMonth,提前期偏置)

  876. select

  877. 1,'',

  878. case t2.Ftype when 1067 then '委外订单' when 1054 then '生产任务单' when 70 then '委外申请' when 500 then '计划订单' when 87 then '预测单' else '' end as 单据类型,

  879. t1.FNumber,t1.FName,t1.FModel,sum(FUnDistributeQty),'','','','','',isnull(t11.Fqty,0),

  880. convert(nvarchar(50),year(dbo.FN_getPreWorkDay(u1.Fdate,abs(isnull(t1.F_122,0)))))+right('00'+convert(nvarchar(50),month(dbo.FN_getPreWorkDay(u1.Fdate,abs(isnull(t1.F_122,0))))),2)+'小计:',

  881. year(dbo.FN_getPreWorkDay(u1.Fdate,abs(isnull(t1.F_122,0)))),month(dbo.FN_getPreWorkDay(u1.Fdate,abs(isnull(t1.F_122,0)))),0

  882. from #icmolist u1

  883. inner join t_ICItem t1 on u1.FItemid=t1.FItemID

  884. inner join #icmo t2 on t2.id=u1.FIcmoInterid

  885. left join

  886. (

  887. select Fitemid,sum(FQty) as FQty from poinventory where FQty>0 group by Fitemid

  888. ) t11 on t11.Fitemid=t1.FItemID

  889. where u1.FQty<>0 and FUnDistributeQty>0

  890. group by case t2.Ftype when 1067 then '委外订单' when 1054 then '生产任务单' when 70 then '委外申请' when 500 then '计划订单' when 87 then '预测单' else '' end,year(dbo.FN_getPreWorkDay(u1.Fdate,abs(isnull(t1.F_122,0)))),month(dbo.FN_getPreWorkDay(u1.Fdate,abs(isnull(t1.F_122,0)))),t1.FNumber,t1.FName,t1.FModel,isnull(t11.Fqty,0)

  891.  
  892.  
  893. insert into #report

  894. (Findex,订单编号,单据类型,子项物料代码,子项物料名称,子项规格型号,欠料数,状态,齐料状态,物料类型,源单据类型,源单状态,在检数量,欠料总计,提前期偏置)

  895. select

  896. 2,'',

  897. '',t1.FNumber,t1.FName,t1.FModel,sum(FUnDistributeQty),'','','','','',isnull(t11.Fqty,0),'总计:',0

  898. from #icmolist u1

  899. inner join t_ICItem t1 on u1.FItemid=t1.FItemID

  900. inner join #icmo t2 on t2.id=u1.FIcmoInterid

  901. left join

  902. (

  903. select Fitemid,sum(FQty) as FQty from poinventory where FQty>0 group by Fitemid

  904. ) t11 on t11.Fitemid=t1.FItemID

  905. where u1.FQty<>0 and FUnDistributeQty>0

  906. group by t1.FNumber,t1.FName,t1.FModel,isnull(t11.Fqty,0)

  907.  
  908. select

  909. 单据类型,

  910. 单据编号,

  911. 计划开工时间,

  912. 提前期偏置,

  913. dbo.FN_getPreWorkDay(计划开工时间,abs(提前期偏置)) as 要求到料日期,

  914. 预计完工日期,

  915. 产品代码,

  916. 产品名称,

  917. 生产数量,

  918. 已生产数量,

  919. 状态,

  920. 欠料总计,

  921. 子项物料代码,

  922. 子项物料名称,

  923. 物料类型,

  924. 单位用量,

  925. 需发料数量,

  926. 欠料数,

  927. 在检数量,

  928. 未分配数量,

  929. 未分配总数,

  930. 源单据类型,

  931. 单据编码,

  932. 行号,

  933. 下单日期,

  934. 申购单号,

  935. 申购日期,

  936. PR到料日期,

  937. 交货日期,

  938. 交期回复,

  939. 交期备注,

  940. 源单状态,

  941. 可分配数量,

  942. 剩余数量,

  943. 分配数量,

  944. 供应商,

  945. 备注信息

  946. from #report order by 子项物料代码,isnull(Fyear,2050),isnull(Fmonth,1),case when Findex=2 then 1 else 0 end asc,单据类型,Findex asc,dbo.FN_getPreWorkDay(计划开工时间,abs(提前期偏置)),FSourceType

  947.  
  948. drop table #ICMO

  949. drop table #ICMOList

  950. drop table #Source

  951. drop table #sn

  952. drop table #result

  953. drop table #icbom

  954. drop table #icbomEntry

  955. drop table #report

2、新建一个Excel,点击“数据”——“自其他来源”——“来自sql server”;

3、输入K3数据库IP和连接账号,密码,下一步;

4、连接成功后,随便选一个表,点击下一步;

5、点击完成;

6、现有工作表位置选择整个sheet,确定;

7、重新点击连接图标,更改sql连接字符串;

8、点击属性;

9、将命令类型更改为SQL,,将原来选择的表更改为存储视图sp_mymrp_must_sum;

10、弹出警告,选择是;

11、输入sql密码,点击确定;

12、数据更新完毕,欠料报表存储过程数据已经从sql刷新到Excel,可以对数据做后期的加工运算等操作。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值