FrmYearSalesStatistics.xml 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <Forms>
  3. <Form Name="FrmYearSalesStatistics">
  4. <Client>
  5. <Events>
  6. <Load>
  7. <![CDATA[
  8. @Title="年度销售统计";
  9. GetSchema();
  10. Search("SearchCompany");
  11. ChangeMode("Search");
  12. ]]>
  13. </Load>
  14. <EndLoad>
  15. <![CDATA[
  16. for(@i=0;@i < CLB_Company.GetItemsCount();@i++)
  17. {
  18. CLB_Company.SetItemChecked(@i,true);
  19. }
  20. ]]>
  21. </EndLoad>
  22. <Closing>
  23. ;
  24. </Closing>
  25. <Closed>
  26. ;
  27. </Closed>
  28. </Events>
  29. <Mode>
  30. <Search>
  31. <Lable name="Search"/>
  32. </Search>
  33. </Mode>
  34. <Controls>
  35. <IKDateTimePicker name = "dtDate"></IKDateTimePicker>
  36. <IKCheckedListBox name="CLB_Company" DataSource="Company" DisplayMember="ShortName_Company" ValueMember="ID_Company">
  37. <Events>
  38. <ItemCheck>
  39. if(CLB_Company.SelectedIndex == 0)
  40. CLB_Location.SelectAllItems();
  41. </ItemCheck>
  42. </Events>
  43. </IKCheckedListBox>
  44. <IKButton name="btnView">
  45. <Events>
  46. <Click>
  47. <![CDATA[
  48. @sqlCompany="";
  49. @empty=true;
  50. @first=true;
  51. for(@i=1;@i < CLB_Company.GetItemsCount();@i++){
  52. if(CLB_Company.GetItemChecked(@i)){
  53. @empty=false;
  54. if(@first==true){
  55. @sqlCompany=@sqlCompany+CLB_Company.GetSelectedName(@i);
  56. @first=false;
  57. }else{
  58. @sqlCompany=@sqlCompany+","+CLB_Company.GetSelectedName(@i);
  59. }
  60. }
  61. }
  62. if(@empty==true)
  63. {
  64. MessageBox("请选择公司!",@Title);
  65. return;
  66. }
  67. @DateTime = DBNull();
  68. if (dtDate.Checked)
  69. {
  70. @DateTime = dtDate.Value;
  71. }
  72. else
  73. {
  74. MessageBox("请选择当前日期!",@Title);
  75. return;
  76. }
  77. Search("SearchYearSalesStatistics",@sqlCompany,@DateTime);
  78. if(RowCount("#FrmYearSalesStatistics") ==0)
  79. {
  80. MessageBox("没有查找到与此相匹配的纪录,未产生报表!",@Title);
  81. }
  82. else
  83. {
  84. PrintReport("GetFrmYearSalesStatistics","Get","FrmYearSalesStatistics");
  85. }
  86. ]]>
  87. </Click>
  88. </Events>
  89. </IKButton>
  90. <IKButton name="btnExcel">
  91. <Events>
  92. <Click>
  93. <![CDATA[
  94. @sqlCompany="";
  95. @empty=true;
  96. @first=true;
  97. for(@i=1;@i < CLB_Company.GetItemsCount();@i++){
  98. if(CLB_Company.GetItemChecked(@i)){
  99. @empty=false;
  100. if(@first==true){
  101. @sqlCompany=@sqlCompany+CLB_Company.GetSelectedName(@i);
  102. @first=false;
  103. }else{
  104. @sqlCompany=@sqlCompany+","+CLB_Company.GetSelectedName(@i);
  105. }
  106. }
  107. }
  108. if(@empty==true)
  109. {
  110. MessageBox("请选择公司!",@Title);
  111. return;
  112. }
  113. @DateTime = DBNull();
  114. if (dtDate.Checked)
  115. {
  116. @DateTime = dtDate.Value;
  117. }
  118. else
  119. {
  120. MessageBox("请选择当前日期!",@Title);
  121. return;
  122. }
  123. Search("SearchYearSalesStatistics",@sqlCompany,@DateTime);
  124. if(RowCount("#FrmYearSalesStatistics") ==0)
  125. {
  126. MessageBox("没有查找到与此相匹配的纪录,未产生报表!",@Title);
  127. }
  128. else
  129. {
  130. ExportReport("Excel","GetFrmYearSalesStatistics","Get","FrmYearSalesStatistics");
  131. }
  132. ]]>
  133. </Click>
  134. </Events>
  135. </IKButton>
  136. <IKButton name="btnWord">
  137. <Events>
  138. <Click>
  139. <![CDATA[
  140. @sqlCompany="";
  141. @empty=true;
  142. @first=true;
  143. for(@i=1;@i < CLB_Company.GetItemsCount();@i++){
  144. if(CLB_Company.GetItemChecked(@i)){
  145. @empty=false;
  146. if(@first==true){
  147. @sqlCompany=@sqlCompany+CLB_Company.GetSelectedName(@i);
  148. @first=false;
  149. }else{
  150. @sqlCompany=@sqlCompany+","+CLB_Company.GetSelectedName(@i);
  151. }
  152. }
  153. }
  154. if(@empty==true)
  155. {
  156. MessageBox("请选择公司!",@Title);
  157. return;
  158. }
  159. @DateTime = DBNull();
  160. if (dtDate.Checked)
  161. {
  162. @DateTime = dtDate.Value;
  163. }
  164. else
  165. {
  166. MessageBox("请选择当前日期!",@Title);
  167. return;
  168. }
  169. Search("SearchYearSalesStatistics",@sqlCompany,@DateTime);
  170. if(RowCount("#FrmYearSalesStatistics") ==0)
  171. {
  172. MessageBox("没有查找到与此相匹配的纪录,未产生报表!",@Title);
  173. }
  174. else
  175. {
  176. ExportReport("Word","GetFrmYearSalesStatistics","Get","FrmYearSalesStatistics");
  177. }
  178. ]]>
  179. </Click>
  180. </Events>
  181. </IKButton>
  182. <IKButton name="btnPrint">
  183. <Events>
  184. <Click>
  185. <![CDATA[
  186. @sqlCompany="";
  187. @empty=true;
  188. @first=true;
  189. for(@i=1;@i < CLB_Company.GetItemsCount();@i++){
  190. if(CLB_Company.GetItemChecked(@i)){
  191. @empty=false;
  192. if(@first==true){
  193. @sqlCompany=@sqlCompany+CLB_Company.GetSelectedName(@i);
  194. @first=false;
  195. }else{
  196. @sqlCompany=@sqlCompany+","+CLB_Company.GetSelectedName(@i);
  197. }
  198. }
  199. }
  200. if(@empty==true)
  201. {
  202. MessageBox("请选择公司!",@Title);
  203. return;
  204. }
  205. @DateTime = DBNull();
  206. if (dtDate.Checked)
  207. {
  208. @DateTime = dtDate.Value;
  209. }
  210. else
  211. {
  212. MessageBox("请选择当前日期!",@Title);
  213. return;
  214. }
  215. Search("SearchYearSalesStatistics",@sqlCompany,@DateTime);
  216. if(RowCount("#FrmYearSalesStatistics") ==0)
  217. {
  218. MessageBox("没有查找到与此相匹配的纪录,未产生报表!",@Title);
  219. }
  220. else
  221. {
  222. ExportReport("PrintToPrinter","GetFrmYearSalesStatistics","Get","FrmYearSalesStatistics");
  223. }
  224. ]]>
  225. </Click>
  226. </Events>
  227. </IKButton>
  228. </Controls>
  229. </Client>
  230. <Server>
  231. <Table>
  232. <Table name="Get" Type="Parent">
  233. <GetSchema CmdType="Text" CmdText="SELECT TOP 0 convert(datetime,0) as DateTime"/>
  234. </Table>
  235. <Table name="FrmYearSalesStatistics" Type="Other">
  236. <GetSchema CmdType="Text" CmdText="Select Top 0 Convert(Nvarchar(64),'') As No_Product,
  237. Convert(Nvarchar(64),'') As Name_Product,
  238. Convert(Decimal(14,2),0) As ThreeY_SaleAmount,
  239. Convert(Decimal(14,2),0) As TwoY_SaleAmount,
  240. Convert(Decimal(14,2),0) As OneY_SaleAmount,
  241. Convert(Decimal(14,2),0) As ThreeM_SaleAmount,
  242. Convert(Decimal(14,2),0) As StockQty_ProductStocks,
  243. Convert(Decimal(14,2),0) As NotArrive_Amount,
  244. Convert(Decimal(14,2),0) As Quantity_OnShip,
  245. Convert(Nvarchar(64),0) As No_PurchaseOrderBill,
  246. Convert(Nvarchar(64),'') As RefNo_PurchaseOrderBill,
  247. Convert(Nvarchar(64),0) As No_PurchaseOrderBillProduct,
  248. Convert(Decimal(14,2),0) As PNotArrive_Amount,
  249. Convert(Decimal(14,2),0) As PQuantity_OnShip"/>
  250. </Table>
  251. </Table>
  252. <Search>
  253. <SearchCompany CmdType="Text" CmdText="Use [SunRoxmManager] Select ShortName_Company,ID_Company from Company where Private_Company = 1
  254. ">
  255. <Tables>
  256. <Table name="Company"/>
  257. </Tables>
  258. </SearchCompany>
  259. <SearchYearSalesStatistics CmdType="Text" CmdText="Select [DateTime] = @DateTime
  260. Declare @DataBase_Company Nvarchar(64)
  261. CREATE TABLE [SunRoxmManager].[dbo].[Temp] ( company [Nvarchar] (64))
  262. declare @Company nvarchar(64)
  263. declare @tmpDay varchar(10)
  264. set @tmpDay=''
  265. declare @i int
  266. set @i=0
  267. while @i &lt; len(@sqlCompany)
  268. begin
  269. set @i=@i+1
  270. if SUBSTRING(@sqlCompany,@i,1)=','
  271. begin
  272. insert into [SunRoxmManager].[dbo].[Temp] values(left(@sqlCompany,@i-1))
  273. set @sqlCompany=SUBSTRING(@sqlCompany,@i+1,len(@sqlCompany))
  274. set @i=0
  275. end
  276. end
  277. insert into [SunRoxmManager].[dbo].[Temp] values(@sqlCompany)
  278. declare @sql nvarchar(4000)
  279. set @sql = ''
  280. Set @sql = @sql + ' Select No_Product,Name_Product,P.ID_Product,IsNull(ThreeY_SaleAmount,0) As ThreeY_SaleAmount,IsNull(TwoY_SaleAmount,0) As TwoY_SaleAmount, IsNull(OneY_SaleAmount,0) As OneY_SaleAmount, IsNull(ThreeM_SaleAmount,0) As ThreeM_SaleAmount,IsNull(ps.StockQty_ProductStocks,0) As StockQty_ProductStocks,'
  281. +' IsNull(NotArrive_Amount,0) As NotArrive_Amount,IsNull(Quantity_OnShip,0) As Quantity_OnShip,No_PurchaseOrderBill,RefNo_PurchaseOrderBill,Comment_PurchaseOrderBillProduct,IsNull(PNotArrive_Amount,0) As PNotArrive_Amount,IsNull(PQuantity_OnShip,0) As PQuantity_OnShip From ('
  282. Declare @CountP int
  283. Set @CountP = 0
  284. Declare @sqlP nvarchar(4000)
  285. set @sqlP = ''
  286. Set @sqlP = @sqlP +' Select AP.ID_Product From ('
  287. Declare CurP Cursor for Select DataBase_Company From [SunRoxmManager].dbo.Company Where ShortName_Company In (select * from [SunRoxmManager].[dbo].[Temp])
  288. Open CurP
  289. Fetch Next From CurP Into @DataBase_Company
  290. While @@Fetch_Status = 0
  291. Begin
  292. If(@CountP = 0)
  293. Begin
  294. Set @sqlP = @sqlP + ' Select [' + @DataBase_Company + '].DBO.Product.ID_Product From [' + @DataBase_Company + '].DBO.Product'
  295. End
  296. If(@CountP &gt; 0)
  297. Begin
  298. Set @sqlP = @sqlP + ' Union All Select [' + @DataBase_Company + '].DBO.Product.ID_Product From [' + @DataBase_Company + '].DBO.Product'
  299. End
  300. Set @CountP = @CountP + 1
  301. Fetch Next From CurP Into @DataBase_Company
  302. End
  303. Close CurP
  304. Deallocate CurP
  305. Set @sqlP = @sqlP+' ) AP Group By AP.ID_Product'
  306. Set @sqlP = @sqlP +' ) P '
  307. Declare @Count3 int
  308. Set @Count3 = 0
  309. declare @sql3 nvarchar(4000)
  310. set @sql3 = ''
  311. declare @sql7 nvarchar(4000)
  312. set @sql7 = ''
  313. Set @sql3 = @sql3 + ' Left Join( Select ID_Product,(Sum(Quantity_SaleAmount)-Sum(IsNull(Quantity_SaleBillProduct,0))+Sum(IsNull(Quantity_SaleRtnBillProduct,0))) As ThreeY_SaleAmount From ('
  314. Declare Cur3 Cursor for Select DataBase_Company From [SunRoxmManager].dbo.Company Where ShortName_Company In (select * from [SunRoxmManager].[dbo].[Temp])
  315. Open Cur3
  316. Fetch Next From Cur3 Into @DataBase_Company
  317. While @@Fetch_Status = 0
  318. Begin
  319. If(@Count3 = 0)
  320. Begin
  321. Set @sql3 = @sql3 +' Select D3'+convert(nvarchar,@Count3)+'.ID_Product,D3'+convert(nvarchar,@Count3)+'.Quantity_SaleAmount,B3'+convert(nvarchar,@Count3)+'.Quantity_SaleBillProduct,C3'+convert(nvarchar,@Count3)+'.Quantity_SaleRtnBillProduct From ('
  322. +' Select AA3'+convert(nvarchar,@Count3)+'.ID_Product, (IsNull(AA3'+convert(nvarchar,@Count3)+'.Quantity_SaleBillProduct,0)- IsNull(CC3'+convert(nvarchar,@Count3)+'.Quantity_SaleRtnBillProduct,0)) As Quantity_SaleAmount'
  323. +' From [' + @DataBase_Company + '].DBO.SaleBillProduct AA3'+convert(nvarchar,@Count3)
  324. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BB3'+convert(nvarchar,@Count3)+' On BB3'+convert(nvarchar,@Count3)+'.ID_SaleBill = AA3'+convert(nvarchar,@Count3)+'.ID_SaleBill'
  325. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CC3'+convert(nvarchar,@Count3)+' On CC3'+convert(nvarchar,@Count3)+'.No_FromBill = BB3'+convert(nvarchar,@Count3)+'.No_SaleBill And CC3'+convert(nvarchar,@Count3)+'.No_FromBillProduct = AA3'+convert(nvarchar,@Count3)+'.No_SaleBillProduct'
  326. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DD3'+convert(nvarchar,@Count3)+' On DD3'+convert(nvarchar,@Count3)+'.ID_SaleRtnBill = CC3'+convert(nvarchar,@Count3)+'.ID_SaleRtnBill'
  327. +' And DD3'+convert(nvarchar,@Count3)+'.Date_SaleRtnBill &gt; DateAdd(yy,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  328. +' And DD3'+convert(nvarchar,@Count3)+'.Date_SaleRtnBill &lt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  329. +' And DD3'+convert(nvarchar,@Count3)+'.ID_Assessor Is Not Null'
  330. +' Where BB3'+convert(nvarchar,@Count3)+'.ID_Assessor Is Not Null '
  331. +' And BB3'+convert(nvarchar,@Count3)+'.Date_SaleBill &gt; DateAdd(yy,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  332. +' And BB3'+convert(nvarchar,@Count3)+'.Date_SaleBill &lt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  333. +' And BB3'+convert(nvarchar,@Count3)+'.IsImport_SaleBill = 0) D3'+convert(nvarchar,@Count3)
  334. +' Left Join (Select AAAA3'+convert(nvarchar,@Count3)+'.ID_Product,AAAA3'+convert(nvarchar,@Count3)+'.Quantity_SaleBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAAA3'+convert(nvarchar,@Count3)
  335. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBBB3'+convert(nvarchar,@Count3)+' On BBBB3'+convert(nvarchar,@Count3)+'.ID_SaleBill = AAAA3'+convert(nvarchar,@Count3)+'.ID_SaleBill'
  336. +' Where BBBB3'+convert(nvarchar,@Count3)+'.Date_SaleBill &gt; DateAdd(yy,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  337. +' And BBBB3'+convert(nvarchar,@Count3)+'.Date_SaleBill &lt;DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  338. +' And BBBB3'+convert(nvarchar,@Count3)+'.IsImport_SaleBill = 1 And Disable_SaleBill = 1 ) B3'+convert(nvarchar,@Count3)+' On B3'+convert(nvarchar,@Count3)+'.ID_Product = D3'+convert(nvarchar,@Count3)+'.ID_Product '
  339. +' Left Join( Select AAA3'+convert(nvarchar,@Count3)+'.ID_Product, CCC3'+convert(nvarchar,@Count3)+'.Quantity_SaleRtnBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAA3'+convert(nvarchar,@Count3)
  340. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBB3'+convert(nvarchar,@Count3)+' On BBB3'+convert(nvarchar,@Count3)+'.ID_SaleBill = AAA3'+convert(nvarchar,@Count3)+'.ID_SaleBill'
  341. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CCC3'+convert(nvarchar,@Count3)+' On CCC3'+convert(nvarchar,@Count3)+'.No_FromBill = BBB3'+convert(nvarchar,@Count3)+'.No_SaleBill And CCC3'+convert(nvarchar,@Count3)+'.No_FromBillProduct = AAA3'+convert(nvarchar,@Count3)+'.No_SaleBillProduct'
  342. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DDD3'+convert(nvarchar,@Count3)+' On DDD3'+convert(nvarchar,@Count3)+'.ID_SaleRtnBill = CCC3'+convert(nvarchar,@Count3)+'.ID_SaleRtnBill'
  343. +' And DDD3'+convert(nvarchar,@Count3)+'.Date_SaleRtnBill &gt; DateAdd(yy,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  344. +' And DDD3'+convert(nvarchar,@Count3)+'.Date_SaleRtnBill &lt;DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  345. +' Where BBB3'+convert(nvarchar,@Count3)+'.Date_SaleBill &gt; DateAdd(yy,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  346. +' And BBB3'+convert(nvarchar,@Count3)+'.Date_SaleBill &lt;DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  347. +' And BBB3'+convert(nvarchar,@Count3)+'.IsImport_SaleBill = 1 And Disable_SaleRtnBill = 1 ) C3'+convert(nvarchar,@Count3)+' On C3'+convert(nvarchar,@Count3)+'.ID_Product = D3'+convert(nvarchar,@Count3)+'.ID_Product '
  348. End
  349. If(@Count3 &gt; 0)
  350. Begin
  351. Set @sql7 = @sql7 + ' Union All Select D3'+convert(nvarchar,@Count3)+'.ID_Product,D3'+convert(nvarchar,@Count3)+'.Quantity_SaleAmount,B3'+convert(nvarchar,@Count3)+'.Quantity_SaleBillProduct,C3'+convert(nvarchar,@Count3)+'.Quantity_SaleRtnBillProduct From ('
  352. +'Select AA3'+convert(nvarchar,@Count3)+'.ID_Product, (IsNull(AA3'+convert(nvarchar,@Count3)+'.Quantity_SaleBillProduct,0)- IsNull(CC3'+convert(nvarchar,@Count3)+'.Quantity_SaleRtnBillProduct,0)) As Quantity_SaleAmount'
  353. +' From [' + @DataBase_Company + '].DBO.SaleBillProduct AA3'+convert(nvarchar,@Count3)
  354. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BB3'+convert(nvarchar,@Count3)+' On BB3'+convert(nvarchar,@Count3)+'.ID_SaleBill = AA3'+convert(nvarchar,@Count3)+'.ID_SaleBill'
  355. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CC3'+convert(nvarchar,@Count3)+' On CC3'+convert(nvarchar,@Count3)+'.No_FromBill = BB3'+convert(nvarchar,@Count3)+'.No_SaleBill And CC3'+convert(nvarchar,@Count3)+'.No_FromBillProduct = AA3'+convert(nvarchar,@Count3)+'.No_SaleBillProduct'
  356. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DD3'+convert(nvarchar,@Count3)+' On DD3'+convert(nvarchar,@Count3)+'.ID_SaleRtnBill = CC3'+convert(nvarchar,@Count3)+'.ID_SaleRtnBill'
  357. +' And DD3'+convert(nvarchar,@Count3)+'.Date_SaleRtnBill &gt; DateAdd(yy,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  358. +' And DD3'+convert(nvarchar,@Count3)+'.Date_SaleRtnBill &lt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  359. +' And DD3'+convert(nvarchar,@Count3)+'.ID_Assessor Is Not Null'
  360. +' Where BB3'+convert(nvarchar,@Count3)+'.ID_Assessor Is Not Null '
  361. +' And BB3'+convert(nvarchar,@Count3)+'.Date_SaleBill &gt; DateAdd(yy,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  362. +' And BB3'+convert(nvarchar,@Count3)+'.Date_SaleBill &lt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  363. +' And BB3'+convert(nvarchar,@Count3)+'.IsImport_SaleBill = 0) D3'+convert(nvarchar,@Count3)
  364. +' Left Join (Select AAAA3'+convert(nvarchar,@Count3)+'.ID_Product,AAAA3'+convert(nvarchar,@Count3)+'.Quantity_SaleBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAAA3'+convert(nvarchar,@Count3)
  365. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBBB3'+convert(nvarchar,@Count3)+' On BBBB3'+convert(nvarchar,@Count3)+'.ID_SaleBill = AAAA3'+convert(nvarchar,@Count3)+'.ID_SaleBill'
  366. +' Where BBBB3'+convert(nvarchar,@Count3)+'.Date_SaleBill &gt; DateAdd(yy,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  367. +' And BBBB3'+convert(nvarchar,@Count3)+'.Date_SaleBill &lt;DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  368. +' And BBBB3'+convert(nvarchar,@Count3)+'.IsImport_SaleBill = 1 And Disable_SaleBill = 1 ) B3'+convert(nvarchar,@Count3)+' On B3'+convert(nvarchar,@Count3)+'.ID_Product = D3'+convert(nvarchar,@Count3)+'.ID_Product '
  369. +' Left Join( Select AAA3'+convert(nvarchar,@Count3)+'.ID_Product, CCC3'+convert(nvarchar,@Count3)+'.Quantity_SaleRtnBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAA3'+convert(nvarchar,@Count3)
  370. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBB3'+convert(nvarchar,@Count3)+' On BBB3'+convert(nvarchar,@Count3)+'.ID_SaleBill = AAA3'+convert(nvarchar,@Count3)+'.ID_SaleBill'
  371. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CCC3'+convert(nvarchar,@Count3)+' On CCC3'+convert(nvarchar,@Count3)+'.No_FromBill = BBB3'+convert(nvarchar,@Count3)+'.No_SaleBill And CCC3'+convert(nvarchar,@Count3)+'.No_FromBillProduct = AAA3'+convert(nvarchar,@Count3)+'.No_SaleBillProduct'
  372. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DDD3'+convert(nvarchar,@Count3)+' On DDD3'+convert(nvarchar,@Count3)+'.ID_SaleRtnBill = CCC3'+convert(nvarchar,@Count3)+'.ID_SaleRtnBill'
  373. +' And DDD3'+convert(nvarchar,@Count3)+'.Date_SaleRtnBill &gt; DateAdd(yy,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  374. +' And DDD3'+convert(nvarchar,@Count3)+'.Date_SaleRtnBill &lt;DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  375. +' Where BBB3'+convert(nvarchar,@Count3)+'.Date_SaleBill &gt; DateAdd(yy,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  376. +' And BBB3'+convert(nvarchar,@Count3)+'.Date_SaleBill &lt;DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  377. +' And BBB3'+convert(nvarchar,@Count3)+'.IsImport_SaleBill = 1 And Disable_SaleRtnBill = 1 ) C3'+convert(nvarchar,@Count3)+' On C3'+convert(nvarchar,@Count3)+'.ID_Product = D3'+convert(nvarchar,@Count3)+'.ID_Product '
  378. End
  379. Set @Count3 = @Count3 + 1
  380. Fetch Next From Cur3 Into @DataBase_Company
  381. End
  382. Close Cur3
  383. Deallocate Cur3
  384. Set @sql7 = @sql7+' ) A3 Group By A3.ID_Product'
  385. Set @sql7 = @sql7 +' ) ThreeY On ThreeY.ID_Product = P.ID_Product'
  386. Declare @Count2 int
  387. Set @Count2 = 0
  388. declare @sql2 nvarchar(4000)
  389. set @sql2 = ''
  390. declare @sql8 nvarchar(4000)
  391. set @sql8 = ''
  392. Set @sql2 = @sql2 + ' Left Join( Select ID_Product,(Sum(Quantity_SaleAmount)-Sum(IsNull(Quantity_SaleBillProduct,0))+Sum(IsNull(Quantity_SaleRtnBillProduct,0))) As TwoY_SaleAmount From ('
  393. Declare Cur2 Cursor for Select DataBase_Company From [SunRoxmManager].dbo.Company Where ShortName_Company In (select * from [SunRoxmManager].[dbo].[Temp])
  394. Open Cur2
  395. Fetch Next From Cur2 Into @DataBase_Company
  396. While @@Fetch_Status = 0
  397. Begin
  398. If(@Count2 = 0)
  399. Begin
  400. Set @sql2 = @sql2 +' Select D2'+convert(nvarchar,@Count2)+'.ID_Product,D2'+convert(nvarchar,@Count2)+'.Quantity_SaleAmount,B2'+convert(nvarchar,@Count2)+'.Quantity_SaleBillProduct,C2'+convert(nvarchar,@Count2)+'.Quantity_SaleRtnBillProduct From ('
  401. +' Select AA2'+convert(nvarchar,@Count2)+'.ID_Product, (IsNull(AA2'+convert(nvarchar,@Count2)+'.Quantity_SaleBillProduct,0)- IsNull(CC2'+convert(nvarchar,@Count2)+'.Quantity_SaleRtnBillProduct,0)) As Quantity_SaleAmount'
  402. +' From [' + @DataBase_Company + '].DBO.SaleBillProduct AA2'+convert(nvarchar,@Count2)
  403. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BB2'+convert(nvarchar,@Count2)+' On BB2'+convert(nvarchar,@Count2)+'.ID_SaleBill = AA2'+convert(nvarchar,@Count2)+'.ID_SaleBill'
  404. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CC2'+convert(nvarchar,@Count2)+' On CC2'+convert(nvarchar,@Count2)+'.No_FromBill = BB2'+convert(nvarchar,@Count2)+'.No_SaleBill And CC2'+convert(nvarchar,@Count2)+'.No_FromBillProduct = AA2'+convert(nvarchar,@Count2)+'.No_SaleBillProduct'
  405. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DD2'+convert(nvarchar,@Count2)+' On DD2'+convert(nvarchar,@Count2)+'.ID_SaleRtnBill = CC2'+convert(nvarchar,@Count2)+'.ID_SaleRtnBill'
  406. +' And DD2'+convert(nvarchar,@Count2)+'.Date_SaleRtnBill &gt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  407. +' And DD2'+convert(nvarchar,@Count2)+'.Date_SaleRtnBill &lt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  408. +' And DD2'+convert(nvarchar,@Count2)+'.ID_Assessor Is Not Null'
  409. +' Where BB2'+convert(nvarchar,@Count2)+'.ID_Assessor Is Not Null '
  410. +' And BB2'+convert(nvarchar,@Count2)+'.Date_SaleBill &gt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  411. +' And BB2'+convert(nvarchar,@Count2)+'.Date_SaleBill &lt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  412. +' And BB2'+convert(nvarchar,@Count2)+'.IsImport_SaleBill = 0) D2'+convert(nvarchar,@Count2)
  413. +' Left Join (Select AAAA2'+convert(nvarchar,@Count2)+'.ID_Product,AAAA2'+convert(nvarchar,@Count2)+'.Quantity_SaleBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAAA2'+convert(nvarchar,@Count2)
  414. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBBB2'+convert(nvarchar,@Count2)+' On BBBB2'+convert(nvarchar,@Count2)+'.ID_SaleBill = AAAA2'+convert(nvarchar,@Count2)+'.ID_SaleBill'
  415. +' Where BBBB2'+convert(nvarchar,@Count2)+'.Date_SaleBill &gt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  416. +' And BBBB2'+convert(nvarchar,@Count2)+'.Date_SaleBill &lt;DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  417. +' And BBBB2'+convert(nvarchar,@Count2)+'.IsImport_SaleBill = 1 And Disable_SaleBill = 1 ) B2'+convert(nvarchar,@Count2)+' On B2'+convert(nvarchar,@Count2)+'.ID_Product = D2'+convert(nvarchar,@Count2)+'.ID_Product '
  418. +' Left Join( Select AAA2'+convert(nvarchar,@Count2)+'.ID_Product, CCC2'+convert(nvarchar,@Count2)+'.Quantity_SaleRtnBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAA2'+convert(nvarchar,@Count2)
  419. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBB2'+convert(nvarchar,@Count2)+' On BBB2'+convert(nvarchar,@Count2)+'.ID_SaleBill = AAA2'+convert(nvarchar,@Count2)+'.ID_SaleBill'
  420. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CCC2'+convert(nvarchar,@Count2)+' On CCC2'+convert(nvarchar,@Count2)+'.No_FromBill = BBB2'+convert(nvarchar,@Count2)+'.No_SaleBill And CCC2'+convert(nvarchar,@Count2)+'.No_FromBillProduct = AAA2'+convert(nvarchar,@Count2)+'.No_SaleBillProduct'
  421. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DDD2'+convert(nvarchar,@Count2)+' On DDD2'+convert(nvarchar,@Count2)+'.ID_SaleRtnBill = CCC2'+convert(nvarchar,@Count2)+'.ID_SaleRtnBill'
  422. +' And DDD2'+convert(nvarchar,@Count2)+'.Date_SaleRtnBill &gt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  423. +' And DDD2'+convert(nvarchar,@Count2)+'.Date_SaleRtnBill &lt;DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  424. +' Where BBB2'+convert(nvarchar,@Count2)+'.Date_SaleBill &gt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  425. +' And BBB2'+convert(nvarchar,@Count2)+'.Date_SaleBill &lt;DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  426. +' And BBB2'+convert(nvarchar,@Count2)+'.IsImport_SaleBill = 1 And Disable_SaleRtnBill = 1 ) C2'+convert(nvarchar,@Count2)+' On C2'+convert(nvarchar,@Count2)+'.ID_Product = D2'+convert(nvarchar,@Count2)+'.ID_Product '
  427. End
  428. If(@Count2 &gt; 0)
  429. Begin
  430. Set @sql8 = @sql8 + ' Union All Select D2'+convert(nvarchar,@Count2)+'.ID_Product,D2'+convert(nvarchar,@Count2)+'.Quantity_SaleAmount,B2'+convert(nvarchar,@Count2)+'.Quantity_SaleBillProduct,C2'+convert(nvarchar,@Count2)+'.Quantity_SaleRtnBillProduct From ('
  431. +'Select AA2'+convert(nvarchar,@Count2)+'.ID_Product, (IsNull(AA2'+convert(nvarchar,@Count2)+'.Quantity_SaleBillProduct,0)- IsNull(CC2'+convert(nvarchar,@Count2)+'.Quantity_SaleRtnBillProduct,0)) As Quantity_SaleAmount'
  432. +' From [' + @DataBase_Company + '].DBO.SaleBillProduct AA2'+convert(nvarchar,@Count2)
  433. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BB2'+convert(nvarchar,@Count2)+' On BB2'+convert(nvarchar,@Count2)+'.ID_SaleBill = AA2'+convert(nvarchar,@Count2)+'.ID_SaleBill'
  434. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CC2'+convert(nvarchar,@Count2)+' On CC2'+convert(nvarchar,@Count2)+'.No_FromBill = BB2'+convert(nvarchar,@Count2)+'.No_SaleBill And CC2'+convert(nvarchar,@Count2)+'.No_FromBillProduct = AA2'+convert(nvarchar,@Count2)+'.No_SaleBillProduct'
  435. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DD2'+convert(nvarchar,@Count2)+' On DD2'+convert(nvarchar,@Count2)+'.ID_SaleRtnBill = CC2'+convert(nvarchar,@Count2)+'.ID_SaleRtnBill'
  436. +' And DD2'+convert(nvarchar,@Count2)+'.Date_SaleRtnBill &gt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  437. +' And DD2'+convert(nvarchar,@Count2)+'.Date_SaleRtnBill &lt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  438. +' And DD2'+convert(nvarchar,@Count2)+'.ID_Assessor Is Not Null'
  439. +' Where BB2'+convert(nvarchar,@Count2)+'.ID_Assessor Is Not Null '
  440. +' And BB2'+convert(nvarchar,@Count2)+'.Date_SaleBill &gt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  441. +' And BB2'+convert(nvarchar,@Count2)+'.Date_SaleBill &lt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  442. +' And BB2'+convert(nvarchar,@Count2)+'.IsImport_SaleBill = 0) D2'+convert(nvarchar,@Count2)
  443. +' Left Join (Select AAAA2'+convert(nvarchar,@Count2)+'.ID_Product,AAAA2'+convert(nvarchar,@Count2)+'.Quantity_SaleBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAAA2'+convert(nvarchar,@Count2)
  444. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBBB2'+convert(nvarchar,@Count2)+' On BBBB2'+convert(nvarchar,@Count2)+'.ID_SaleBill = AAAA2'+convert(nvarchar,@Count2)+'.ID_SaleBill'
  445. +' Where BBBB2'+convert(nvarchar,@Count2)+'.Date_SaleBill &gt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  446. +' And BBBB2'+convert(nvarchar,@Count2)+'.Date_SaleBill &lt;DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  447. +' And BBBB2'+convert(nvarchar,@Count2)+'.IsImport_SaleBill = 1 And Disable_SaleBill = 1 ) B2'+convert(nvarchar,@Count2)+' On B2'+convert(nvarchar,@Count2)+'.ID_Product = D2'+convert(nvarchar,@Count2)+'.ID_Product '
  448. +' Left Join( Select AAA2'+convert(nvarchar,@Count2)+'.ID_Product, CCC2'+convert(nvarchar,@Count2)+'.Quantity_SaleRtnBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAA2'+convert(nvarchar,@Count2)
  449. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBB2'+convert(nvarchar,@Count2)+' On BBB2'+convert(nvarchar,@Count2)+'.ID_SaleBill = AAA2'+convert(nvarchar,@Count2)+'.ID_SaleBill'
  450. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CCC2'+convert(nvarchar,@Count2)+' On CCC2'+convert(nvarchar,@Count2)+'.No_FromBill = BBB2'+convert(nvarchar,@Count2)+'.No_SaleBill And CCC2'+convert(nvarchar,@Count2)+'.No_FromBillProduct = AAA2'+convert(nvarchar,@Count2)+'.No_SaleBillProduct'
  451. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DDD2'+convert(nvarchar,@Count2)+' On DDD2'+convert(nvarchar,@Count2)+'.ID_SaleRtnBill = CCC2'+convert(nvarchar,@Count2)+'.ID_SaleRtnBill'
  452. +' And DDD2'+convert(nvarchar,@Count2)+'.Date_SaleRtnBill &gt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  453. +' And DDD2'+convert(nvarchar,@Count2)+'.Date_SaleRtnBill &lt;DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  454. +' Where BBB2'+convert(nvarchar,@Count2)+'.Date_SaleBill &gt; DateAdd(yy,-2,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  455. +' And BBB2'+convert(nvarchar,@Count2)+'.Date_SaleBill &lt;DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  456. +' And BBB2'+convert(nvarchar,@Count2)+'.IsImport_SaleBill = 1 And Disable_SaleRtnBill = 1 ) C2'+convert(nvarchar,@Count2)+' On C2'+convert(nvarchar,@Count2)+'.ID_Product = D2'+convert(nvarchar,@Count2)+'.ID_Product '
  457. End
  458. Set @Count2 = @Count2 + 1
  459. Fetch Next From Cur2 Into @DataBase_Company
  460. End
  461. Close Cur2
  462. Deallocate Cur2
  463. Set @sql8 = @sql8 +' ) A2 Group By A2.ID_Product'
  464. Set @sql8 = @sql8 +' ) TwoY On TwoY.ID_Product = P.ID_Product'
  465. Declare @Count1 int
  466. Set @Count1 = 0
  467. declare @sql1 nvarchar(4000)
  468. set @sql1 = ''
  469. declare @sql9 nvarchar(4000)
  470. set @sql9 = ''
  471. Set @sql1 = @sql1 + ' Left Join( Select ID_Product,(Sum(Quantity_SaleAmount)-Sum(IsNull(Quantity_SaleBillProduct,0))+Sum(IsNull(Quantity_SaleRtnBillProduct,0))) As OneY_SaleAmount From ('
  472. Declare Cur1 Cursor for Select DataBase_Company From [SunRoxmManager].dbo.Company Where ShortName_Company In (select * from [SunRoxmManager].[dbo].[Temp])
  473. Open Cur1
  474. Fetch Next From Cur1 Into @DataBase_Company
  475. While @@Fetch_Status = 0
  476. Begin
  477. If(@Count1 = 0)
  478. Begin
  479. Set @sql1 = @sql1 +' Select D1'+convert(nvarchar,@Count1)+'.ID_Product,D1'+convert(nvarchar,@Count1)+'.Quantity_SaleAmount,B1'+convert(nvarchar,@Count1)+'.Quantity_SaleBillProduct,C1'+convert(nvarchar,@Count1)+'.Quantity_SaleRtnBillProduct From ('
  480. +' Select AA1'+convert(nvarchar,@Count1)+'.ID_Product, (IsNull(AA1'+convert(nvarchar,@Count1)+'.Quantity_SaleBillProduct,0)- IsNull(CC1'+convert(nvarchar,@Count1)+'.Quantity_SaleRtnBillProduct,0)) As Quantity_SaleAmount'
  481. +' From [' + @DataBase_Company + '].DBO.SaleBillProduct AA1'+convert(nvarchar,@Count1)
  482. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BB1'+convert(nvarchar,@Count1)+' On BB1'+convert(nvarchar,@Count1)+'.ID_SaleBill = AA1'+convert(nvarchar,@Count1)+'.ID_SaleBill'
  483. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CC1'+convert(nvarchar,@Count1)+' On CC1'+convert(nvarchar,@Count1)+'.No_FromBill = BB1'+convert(nvarchar,@Count1)+'.No_SaleBill And CC1'+convert(nvarchar,@Count1)+'.No_FromBillProduct = AA1'+convert(nvarchar,@Count1)+'.No_SaleBillProduct'
  484. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DD1'+convert(nvarchar,@Count1)+' On DD1'+convert(nvarchar,@Count1)+'.ID_SaleRtnBill = CC1'+convert(nvarchar,@Count1)+'.ID_SaleRtnBill'
  485. +' And DD1'+convert(nvarchar,@Count1)+'.Date_SaleRtnBill &gt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  486. +' And DD1'+convert(nvarchar,@Count1)+'.Date_SaleRtnBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  487. +' And DD1'+convert(nvarchar,@Count1)+'.ID_Assessor Is Not Null'
  488. +' Where BB1'+convert(nvarchar,@Count1)+'.ID_Assessor Is Not Null '
  489. +' And BB1'+convert(nvarchar,@Count1)+'.Date_SaleBill &gt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  490. +' And BB1'+convert(nvarchar,@Count1)+'.Date_SaleBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  491. +' And BB1'+convert(nvarchar,@Count1)+'.IsImport_SaleBill = 0) D1'+convert(nvarchar,@Count1)
  492. +' Left Join (Select AAAA1'+convert(nvarchar,@Count1)+'.ID_Product,AAAA1'+convert(nvarchar,@Count1)+'.Quantity_SaleBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAAA1'+convert(nvarchar,@Count1)
  493. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBBB1'+convert(nvarchar,@Count1)+' On BBBB1'+convert(nvarchar,@Count1)+'.ID_SaleBill = AAAA1'+convert(nvarchar,@Count1)+'.ID_SaleBill'
  494. +' Where BBBB1'+convert(nvarchar,@Count1)+'.Date_SaleBill &gt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  495. +' And BBBB1'+convert(nvarchar,@Count1)+'.Date_SaleBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  496. +' And BBBB1'+convert(nvarchar,@Count1)+'.IsImport_SaleBill = 1 And Disable_SaleBill = 1 ) B1'+convert(nvarchar,@Count1)+' On B1'+convert(nvarchar,@Count1)+'.ID_Product = D1'+convert(nvarchar,@Count1)+'.ID_Product '
  497. +' Left Join( Select AAA1'+convert(nvarchar,@Count1)+'.ID_Product, CCC1'+convert(nvarchar,@Count1)+'.Quantity_SaleRtnBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAA1'+convert(nvarchar,@Count1)
  498. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBB1'+convert(nvarchar,@Count1)+' On BBB1'+convert(nvarchar,@Count1)+'.ID_SaleBill = AAA1'+convert(nvarchar,@Count1)+'.ID_SaleBill'
  499. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CCC1'+convert(nvarchar,@Count1)+' On CCC1'+convert(nvarchar,@Count1)+'.No_FromBill = BBB1'+convert(nvarchar,@Count1)+'.No_SaleBill And CCC1'+convert(nvarchar,@Count1)+'.No_FromBillProduct = AAA1'+convert(nvarchar,@Count1)+'.No_SaleBillProduct'
  500. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DDD1'+convert(nvarchar,@Count1)+' On DDD1'+convert(nvarchar,@Count1)+'.ID_SaleRtnBill = CCC1'+convert(nvarchar,@Count1)+'.ID_SaleRtnBill'
  501. +' And DDD1'+convert(nvarchar,@Count1)+'.Date_SaleRtnBill &gt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  502. +' And DDD1'+convert(nvarchar,@Count1)+'.Date_SaleRtnBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  503. +' Where BBB1'+convert(nvarchar,@Count1)+'.Date_SaleBill &gt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  504. +' And BBB1'+convert(nvarchar,@Count1)+'.Date_SaleBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  505. +' And BBB1'+convert(nvarchar,@Count1)+'.IsImport_SaleBill = 1 And Disable_SaleRtnBill = 1 ) C1'+convert(nvarchar,@Count1)+' On C1'+convert(nvarchar,@Count1)+'.ID_Product = D1'+convert(nvarchar,@Count1)+'.ID_Product '
  506. End
  507. If(@Count1 &gt; 0)
  508. Begin
  509. Set @sql9 = @sql9 + 'Union All Select D1'+convert(nvarchar,@Count1)+'.ID_Product,D1'+convert(nvarchar,@Count1)+'.Quantity_SaleAmount,B1'+convert(nvarchar,@Count1)+'.Quantity_SaleBillProduct,C1'+convert(nvarchar,@Count1)+'.Quantity_SaleRtnBillProduct From ('
  510. +'Select AA1'+convert(nvarchar,@Count1)+'.ID_Product, (IsNull(AA1'+convert(nvarchar,@Count1)+'.Quantity_SaleBillProduct,0)- IsNull(CC1'+convert(nvarchar,@Count1)+'.Quantity_SaleRtnBillProduct,0)) As Quantity_SaleAmount'
  511. +' From [' + @DataBase_Company + '].DBO.SaleBillProduct AA1'+convert(nvarchar,@Count1)
  512. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BB1'+convert(nvarchar,@Count1)+' On BB1'+convert(nvarchar,@Count1)+'.ID_SaleBill = AA1'+convert(nvarchar,@Count1)+'.ID_SaleBill'
  513. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CC1'+convert(nvarchar,@Count1)+' On CC1'+convert(nvarchar,@Count1)+'.No_FromBill = BB1'+convert(nvarchar,@Count1)+'.No_SaleBill And CC1'+convert(nvarchar,@Count1)+'.No_FromBillProduct = AA1'+convert(nvarchar,@Count1)+'.No_SaleBillProduct'
  514. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DD1'+convert(nvarchar,@Count1)+' On DD1'+convert(nvarchar,@Count1)+'.ID_SaleRtnBill = CC1'+convert(nvarchar,@Count1)+'.ID_SaleRtnBill'
  515. +' And DD1'+convert(nvarchar,@Count1)+'.Date_SaleRtnBill &gt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  516. +' And DD1'+convert(nvarchar,@Count1)+'.Date_SaleRtnBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  517. +' And DD1'+convert(nvarchar,@Count1)+'.ID_Assessor Is Not Null'
  518. +' Where BB1'+convert(nvarchar,@Count1)+'.ID_Assessor Is Not Null '
  519. +' And BB1'+convert(nvarchar,@Count1)+'.Date_SaleBill &gt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  520. +' And BB1'+convert(nvarchar,@Count1)+'.Date_SaleBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  521. +' And BB1'+convert(nvarchar,@Count1)+'.IsImport_SaleBill = 0) D1'+convert(nvarchar,@Count1)
  522. +' Left Join (Select AAAA1'+convert(nvarchar,@Count1)+'.ID_Product,AAAA1'+convert(nvarchar,@Count1)+'.Quantity_SaleBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAAA1'+convert(nvarchar,@Count1)
  523. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBBB1'+convert(nvarchar,@Count1)+' On BBBB1'+convert(nvarchar,@Count1)+'.ID_SaleBill = AAAA1'+convert(nvarchar,@Count1)+'.ID_SaleBill'
  524. +' Where BBBB1'+convert(nvarchar,@Count1)+'.Date_SaleBill &gt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  525. +' And BBBB1'+convert(nvarchar,@Count1)+'.Date_SaleBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  526. +' And BBBB1'+convert(nvarchar,@Count1)+'.IsImport_SaleBill = 1 And Disable_SaleBill = 1 ) B1'+convert(nvarchar,@Count1)+' On B1'+convert(nvarchar,@Count1)+'.ID_Product = D1'+convert(nvarchar,@Count1)+'.ID_Product '
  527. +' Left Join( Select AAA1'+convert(nvarchar,@Count1)+'.ID_Product, CCC1'+convert(nvarchar,@Count1)+'.Quantity_SaleRtnBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAA1'+convert(nvarchar,@Count1)
  528. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBB1'+convert(nvarchar,@Count1)+' On BBB1'+convert(nvarchar,@Count1)+'.ID_SaleBill = AAA1'+convert(nvarchar,@Count1)+'.ID_SaleBill'
  529. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CCC1'+convert(nvarchar,@Count1)+' On CCC1'+convert(nvarchar,@Count1)+'.No_FromBill = BBB1'+convert(nvarchar,@Count1)+'.No_SaleBill And CCC1'+convert(nvarchar,@Count1)+'.No_FromBillProduct = AAA1'+convert(nvarchar,@Count1)+'.No_SaleBillProduct'
  530. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DDD1'+convert(nvarchar,@Count1)+' On DDD1'+convert(nvarchar,@Count1)+'.ID_SaleRtnBill = CCC1'+convert(nvarchar,@Count1)+'.ID_SaleRtnBill'
  531. +' And DDD1'+convert(nvarchar,@Count1)+'.Date_SaleRtnBill &gt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  532. +' And DDD1'+convert(nvarchar,@Count1)+'.Date_SaleRtnBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  533. +' Where BBB1'+convert(nvarchar,@Count1)+'.Date_SaleBill &gt; DateAdd(yy,-1,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  534. +' And BBB1'+convert(nvarchar,@Count1)+'.Date_SaleBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  535. +' And BBB1'+convert(nvarchar,@Count1)+'.IsImport_SaleBill = 1 And Disable_SaleRtnBill = 1 ) C1'+convert(nvarchar,@Count1)+' On C1'+convert(nvarchar,@Count1)+'.ID_Product = D1'+convert(nvarchar,@Count1)+'.ID_Product '
  536. End
  537. Set @Count1 = @Count1 + 1
  538. Fetch Next From Cur1 Into @DataBase_Company
  539. End
  540. Close Cur1
  541. Deallocate Cur1
  542. Set @sql9 = @sql9 +' ) A1 Group By A1.ID_Product'
  543. Set @sql9 = @sql9 +' ) OneY On OneY.ID_Product = P.ID_Product'
  544. Declare @Count4 int
  545. Set @Count4 = 0
  546. declare @sql4 nvarchar(4000)
  547. set @sql4 = ''
  548. declare @sql10 nvarchar(4000)
  549. set @sql10 = ''
  550. Set @sql4 = @sql4 + ' Left Join( Select ID_Product,(Sum(Quantity_SaleAmount)-Sum(IsNull(Quantity_SaleBillProduct,0))+Sum(IsNull(Quantity_SaleRtnBillProduct,0))) As ThreeM_SaleAmount From ('
  551. Declare Cur4 Cursor for Select DataBase_Company From [SunRoxmManager].dbo.Company Where ShortName_Company In (select * from [SunRoxmManager].[dbo].[Temp])
  552. Open Cur4
  553. Fetch Next From Cur4 Into @DataBase_Company
  554. While @@Fetch_Status = 0
  555. Begin
  556. If(@Count4 = 0)
  557. Begin
  558. Set @sql4 = @sql4 +' Select D4'+convert(nvarchar,@Count4)+'.ID_Product,D4'+convert(nvarchar,@Count4)+'.Quantity_SaleAmount,B4'+convert(nvarchar,@Count4)+'.Quantity_SaleBillProduct,C4'+convert(nvarchar,@Count4)+'.Quantity_SaleRtnBillProduct From ('
  559. +' Select AA4'+convert(nvarchar,@Count4)+'.ID_Product, (IsNull(AA4'+convert(nvarchar,@Count4)+'.Quantity_SaleBillProduct,0)- IsNull(CC4'+convert(nvarchar,@Count4)+'.Quantity_SaleRtnBillProduct,0)) As Quantity_SaleAmount'
  560. +' From [' + @DataBase_Company + '].DBO.SaleBillProduct AA4'+convert(nvarchar,@Count4)
  561. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BB4'+convert(nvarchar,@Count4)+' On BB4'+convert(nvarchar,@Count4)+'.ID_SaleBill = AA4'+convert(nvarchar,@Count4)+'.ID_SaleBill'
  562. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CC4'+convert(nvarchar,@Count4)+' On CC4'+convert(nvarchar,@Count4)+'.No_FromBill = BB4'+convert(nvarchar,@Count4)+'.No_SaleBill And CC4'+convert(nvarchar,@Count4)+'.No_FromBillProduct = AA4'+convert(nvarchar,@Count4)+'.No_SaleBillProduct'
  563. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DD4'+convert(nvarchar,@Count4)+' On DD4'+convert(nvarchar,@Count4)+'.ID_SaleRtnBill = CC4'+convert(nvarchar,@Count4)+'.ID_SaleRtnBill'
  564. +' And DD4'+convert(nvarchar,@Count4)+'.Date_SaleRtnBill &gt; DateAdd(mm,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  565. +' And DD4'+convert(nvarchar,@Count4)+'.Date_SaleRtnBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  566. +' And DD4'+convert(nvarchar,@Count4)+'.ID_Assessor Is Not Null'
  567. +' Where BB4'+convert(nvarchar,@Count4)+'.ID_Assessor Is Not Null '
  568. +' And BB4'+convert(nvarchar,@Count4)+'.Date_SaleBill &gt; DateAdd(mm,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  569. +' And BB4'+convert(nvarchar,@Count4)+'.Date_SaleBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  570. +' And BB4'+convert(nvarchar,@Count4)+'.IsImport_SaleBill = 0) D4'+convert(nvarchar,@Count4)
  571. +' Left Join (Select AAAA4'+convert(nvarchar,@Count4)+'.ID_Product,AAAA4'+convert(nvarchar,@Count4)+'.Quantity_SaleBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAAA4'+convert(nvarchar,@Count4)
  572. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBBB4'+convert(nvarchar,@Count4)+' On BBBB4'+convert(nvarchar,@Count4)+'.ID_SaleBill = AAAA4'+convert(nvarchar,@Count4)+'.ID_SaleBill'
  573. +' Where BBBB4'+convert(nvarchar,@Count4)+'.Date_SaleBill &gt; DateAdd(mm,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  574. +' And BBBB4'+convert(nvarchar,@Count4)+'.Date_SaleBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  575. +' And BBBB4'+convert(nvarchar,@Count4)+'.IsImport_SaleBill = 1 And Disable_SaleBill = 1 ) B4'+convert(nvarchar,@Count4)+' On B4'+convert(nvarchar,@Count4)+'.ID_Product = D4'+convert(nvarchar,@Count4)+'.ID_Product '
  576. +' Left Join( Select AAA4'+convert(nvarchar,@Count4)+'.ID_Product, CCC4'+convert(nvarchar,@Count4)+'.Quantity_SaleRtnBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAA4'+convert(nvarchar,@Count4)
  577. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBB4'+convert(nvarchar,@Count4)+' On BBB4'+convert(nvarchar,@Count4)+'.ID_SaleBill = AAA4'+convert(nvarchar,@Count4)+'.ID_SaleBill'
  578. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CCC4'+convert(nvarchar,@Count4)+' On CCC4'+convert(nvarchar,@Count4)+'.No_FromBill = BBB4'+convert(nvarchar,@Count4)+'.No_SaleBill And CCC4'+convert(nvarchar,@Count4)+'.No_FromBillProduct = AAA4'+convert(nvarchar,@Count4)+'.No_SaleBillProduct'
  579. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DDD4'+convert(nvarchar,@Count4)+' On DDD4'+convert(nvarchar,@Count4)+'.ID_SaleRtnBill = CCC4'+convert(nvarchar,@Count4)+'.ID_SaleRtnBill'
  580. +' And DDD4'+convert(nvarchar,@Count4)+'.Date_SaleRtnBill &gt; DateAdd(mm,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  581. +' And DDD4'+convert(nvarchar,@Count4)+'.Date_SaleRtnBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  582. +' Where BBB4'+convert(nvarchar,@Count4)+'.Date_SaleBill &gt; DateAdd(mm,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  583. +' And BBB4'+convert(nvarchar,@Count4)+'.Date_SaleBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  584. +' And BBB4'+convert(nvarchar,@Count4)+'.IsImport_SaleBill = 1 And Disable_SaleRtnBill = 1 ) C4'+convert(nvarchar,@Count4)+' On C4'+convert(nvarchar,@Count4)+'.ID_Product = D4'+convert(nvarchar,@Count4)+'.ID_Product '
  585. End
  586. If(@Count4 &gt; 0)
  587. Begin
  588. Set @sql10 = @sql10 + ' Union All Select D4'+convert(nvarchar,@Count4)+'.ID_Product,D4'+convert(nvarchar,@Count4)+'.Quantity_SaleAmount,B4'+convert(nvarchar,@Count4)+'.Quantity_SaleBillProduct,C4'+convert(nvarchar,@Count4)+'.Quantity_SaleRtnBillProduct From ('
  589. +'Select AA4'+convert(nvarchar,@Count4)+'.ID_Product, (IsNull(AA4'+convert(nvarchar,@Count4)+'.Quantity_SaleBillProduct,0)- IsNull(CC4'+convert(nvarchar,@Count4)+'.Quantity_SaleRtnBillProduct,0)) As Quantity_SaleAmount'
  590. +' From [' + @DataBase_Company + '].DBO.SaleBillProduct AA4'+convert(nvarchar,@Count4)
  591. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BB4'+convert(nvarchar,@Count4)+' On BB4'+convert(nvarchar,@Count4)+'.ID_SaleBill = AA4'+convert(nvarchar,@Count4)+'.ID_SaleBill'
  592. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CC4'+convert(nvarchar,@Count4)+' On CC4'+convert(nvarchar,@Count4)+'.No_FromBill = BB4'+convert(nvarchar,@Count4)+'.No_SaleBill And CC4'+convert(nvarchar,@Count4)+'.No_FromBillProduct = AA4'+convert(nvarchar,@Count4)+'.No_SaleBillProduct'
  593. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DD4'+convert(nvarchar,@Count4)+' On DD4'+convert(nvarchar,@Count4)+'.ID_SaleRtnBill = CC4'+convert(nvarchar,@Count4)+'.ID_SaleRtnBill'
  594. +' And DD4'+convert(nvarchar,@Count4)+'.Date_SaleRtnBill &gt; DateAdd(mm,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  595. +' And DD4'+convert(nvarchar,@Count4)+'.Date_SaleRtnBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  596. +' And DD4'+convert(nvarchar,@Count4)+'.ID_Assessor Is Not Null'
  597. +' Where BB4'+convert(nvarchar,@Count4)+'.ID_Assessor Is Not Null '
  598. +' And BB4'+convert(nvarchar,@Count4)+'.Date_SaleBill &gt; DateAdd(mm,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  599. +' And BB4'+convert(nvarchar,@Count4)+'.Date_SaleBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  600. +' And BB4'+convert(nvarchar,@Count4)+'.IsImport_SaleBill = 0) D4'+convert(nvarchar,@Count4)
  601. +' Left Join (Select AAAA4'+convert(nvarchar,@Count4)+'.ID_Product,AAAA4'+convert(nvarchar,@Count4)+'.Quantity_SaleBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAAA4'+convert(nvarchar,@Count4)
  602. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBBB4'+convert(nvarchar,@Count4)+' On BBBB4'+convert(nvarchar,@Count4)+'.ID_SaleBill = AAAA4'+convert(nvarchar,@Count4)+'.ID_SaleBill'
  603. +' Where BBBB4'+convert(nvarchar,@Count4)+'.Date_SaleBill &gt; DateAdd(mm,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  604. +' And BBBB4'+convert(nvarchar,@Count4)+'.Date_SaleBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  605. +' And BBBB4'+convert(nvarchar,@Count4)+'.IsImport_SaleBill = 1 And Disable_SaleBill = 1 ) B4'+convert(nvarchar,@Count4)+' On B4'+convert(nvarchar,@Count4)+'.ID_Product = D4'+convert(nvarchar,@Count4)+'.ID_Product '
  606. +' Left Join( Select AAA4'+convert(nvarchar,@Count4)+'.ID_Product, CCC4'+convert(nvarchar,@Count4)+'.Quantity_SaleRtnBillProduct From [' + @DataBase_Company + '].DBO.SaleBillProduct AAA4'+convert(nvarchar,@Count4)
  607. +' Left Join [' + @DataBase_Company + '].DBO.SaleBill BBB4'+convert(nvarchar,@Count4)+' On BBB4'+convert(nvarchar,@Count4)+'.ID_SaleBill = AAA4'+convert(nvarchar,@Count4)+'.ID_SaleBill'
  608. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBillProduct CCC4'+convert(nvarchar,@Count4)+' On CCC4'+convert(nvarchar,@Count4)+'.No_FromBill = BBB4'+convert(nvarchar,@Count4)+'.No_SaleBill And CCC4'+convert(nvarchar,@Count4)+'.No_FromBillProduct = AAA4'+convert(nvarchar,@Count4)+'.No_SaleBillProduct'
  609. +' Left Join [' + @DataBase_Company + '].DBO.SaleRtnBill DDD4'+convert(nvarchar,@Count4)+' On DDD4'+convert(nvarchar,@Count4)+'.ID_SaleRtnBill = CCC4'+convert(nvarchar,@Count4)+'.ID_SaleRtnBill'
  610. +' And DDD4'+convert(nvarchar,@Count4)+'.Date_SaleRtnBill &gt; DateAdd(mm,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  611. +' And DDD4'+convert(nvarchar,@Count4)+'.Date_SaleRtnBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  612. +' Where BBB4'+convert(nvarchar,@Count4)+'.Date_SaleBill &gt; DateAdd(mm,-3,'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39) +') '
  613. +' And BBB4'+convert(nvarchar,@Count4)+'.Date_SaleBill &lt;'+nchar(39)+convert(nvarchar,@DateTime)+nchar(39)
  614. +' And BBB4'+convert(nvarchar,@Count4)+'.IsImport_SaleBill = 1 And Disable_SaleRtnBill = 1 ) C4'+convert(nvarchar,@Count4)+' On C4'+convert(nvarchar,@Count4)+'.ID_Product = D4'+convert(nvarchar,@Count4)+'.ID_Product '
  615. End
  616. Set @Count4 = @Count4 + 1
  617. Fetch Next From Cur4 Into @DataBase_Company
  618. End
  619. Close Cur4
  620. Deallocate Cur4
  621. Set @sql10 = @sql10 +' ) A4 Group By A4.ID_Product'
  622. Set @sql10 = @sql10 +' ) ThreeM On ThreeM.ID_Product = P.ID_Product'
  623. declare @sql5 nvarchar(4000)
  624. set @sql5 = ''
  625. Set @sql5 = @sql5 + ' Left Join ('
  626. +' Select M.ID_Product,NotArrive_Amount,Quantity_OnShip From('
  627. +' Select ID_Product,Sum(NotArrive_Amount) As NotArrive_Amount,Sum(Quantity_OnShip) As Quantity_OnShip '
  628. +' From ('
  629. +' Select ta.ID_PurchaseOrderBill,ta.ID_Product,(IsNull(ta.Quantity_PurchaseOrderBillProduct,0)-IsNull(ta.Quantity_InDepot,0)-IsNull(ta.Quantity_OnShip,0)) As NotArrive_Amount,IsNull(ta.Quantity_OnShip,0) As Quantity_OnShip'
  630. +' From ( Select b.ID_PurchaseOrderBill,b.ID_Product, sum(a.Quantity_Order) As Quantity_PurchaseOrderBillProduct,sum(a.Quantity_InDepot) As Quantity_InDepot,sum(a.Quantity_OnShip) As Quantity_OnShip,sum(a.Quantity_EndCase) As Quantity_EndCase,(sum(a.Quantity_Order)-sum(a.Quantity_InDepot)-sum(a.Quantity_OnShip)-sum(a.Quantity_EndCase)) As NoInDepot'
  631. +' From PurchaseSourceBillProduct a'
  632. +' Join PurchaseOrderBillProduct b On a.ID_PurchaseSourceBillProduct=b.ID_PurchaseOrderBillProduct'
  633. +' Group By b.ID_Product ,b.ID_PurchaseOrderBill'
  634. +' Having (sum(a.Quantity_Order)-sum(a.Quantity_InDepot)-sum(a.Quantity_EndCase)) > 0'
  635. +' )ta'
  636. +' ) A5 Group By A5.ID_Product )M'
  637. +' ) SQ On SQ.ID_Product = P.ID_Product'
  638. Declare @sql6 nvarchar(4000)
  639. set @sql6 = ''
  640. set @sql6 = @sql6 + ' Left Join ('
  641. +' Select ta.ID_Product,PurchaseOrderBill.No_PurchaseOrderBill,RefNo_PurchaseOrderBill,Comment_PurchaseOrderBillProduct,(IsNull(ta.Quantity_PurchaseOrderBillProduct,0)-IsNull(ta.Quantity_InDepot,0)-IsNull(ta.Quantity_OnShip,0)) As PNotArrive_Amount,IsNull(ta.Quantity_OnShip,0) As PQuantity_OnShip'
  642. +' From'
  643. +' ( Select b.ID_PurchaseOrderBill,b.ID_Product, sum(a.Quantity_Order) As Quantity_PurchaseOrderBillProduct,sum(a.Quantity_InDepot) As Quantity_InDepot,sum(a.Quantity_OnShip) As Quantity_OnShip,sum(a.Quantity_EndCase) As Quantity_EndCase,(sum(a.Quantity_Order)-sum(a.Quantity_InDepot)-sum(a.Quantity_OnShip)-sum(a.Quantity_EndCase)) As NoInDepot'
  644. +' From PurchaseSourceBillProduct a'
  645. +' Join PurchaseOrderBillProduct b On a.ID_PurchaseSourceBillProduct=b.ID_PurchaseOrderBillProduct'
  646. +' Group By b.ID_Product ,b.ID_PurchaseOrderBill'
  647. +' Having (sum(a.Quantity_Order)-sum(a.Quantity_InDepot)-sum(a.Quantity_EndCase)) > 0'
  648. +' )ta'
  649. +' Join Product On Product.ID_Product=ta.ID_Product'
  650. +' Join PurchaseOrderBillProduct On PurchaseOrderBillProduct.ID_PurchaseOrderBill = ta.ID_PurchaseOrderBill And PurchaseOrderBillProduct.ID_Product = ta.ID_Product'
  651. +' Join PurchaseOrderBill On PurchaseOrderBill.ID_PurchaseOrderBill=ta.ID_PurchaseOrderBill'
  652. +' Where PurchaseOrderBill.ID_Assessor Is Not Null '
  653. +' ) po On po.ID_Product = P.ID_Product'
  654. +' Left Join (Select ID_Product,Sum(StockQty_ProductStocks) As StockQty_ProductStocks From ProductStocks Group By ID_Product) ps On PS.ID_Product = P.ID_Product'
  655. +' Left Join Product On Product.ID_Product = P.ID_Product'
  656. +' Order By No_Product,No_PurchaseOrderBill'
  657. EXEC (@sql+@sqlP+@sql3+@sql7 +@sql2+@sql8+ @sql1+@sql9+@sql4+@sql10+@sql5+@sql6)
  658. drop table [SunRoxmManager].[dbo].[Temp]">
  659. <Params>
  660. <Param name="@sqlCompany" type="PurchaseInDepotBill.Comment_PurchaseInDepotBill" Index="0" />
  661. <Param name="@DateTime" type="SaleBill.Date_SaleBill" Index="1" />
  662. </Params>
  663. <Tables>
  664. <Table name="Get" />
  665. <Table name="FrmYearSalesStatistics"/>
  666. </Tables>
  667. </SearchYearSalesStatistics>
  668. </Search>
  669. </Server>
  670. </Form>
  671. </Forms>