Advertisement
bytecoded

Top 3 Query (1C)

Nov 9th, 2019
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. SELECT ALLOWED
  2. CurrencyRatesSliceLast.Currency AS Currency,
  3. CurrencyRatesSliceLast.ExchangeRate AS ExchangeRate,
  4. CurrencyRatesSliceLast.Multiplicity AS Multiplicity
  5. INTO Rates
  6. FROM
  7. InformationRegister.CurrencyRates.SliceLast(&Period, ) AS CurrencyRatesSliceLast
  8. ;
  9.  
  10. ////////////////////////////////////////////////////////////////////////////////
  11. SELECT
  12. ContractsLimitsSliceLast.Contract AS Contract,
  13. ContractsLimitsSliceLast.LimitType AS LimitType,
  14. ContractsLimitsSliceLast.Amount AS Amount,
  15. ContractsLimitsSliceLast.Currency AS CurrencyLimit,
  16. ISNULL(Rates.ExchangeRate, 1) AS RateLimit,
  17. ISNULL(Rates.Multiplicity, 1) AS MultiplicityLimit
  18. INTO Limits
  19. FROM
  20. InformationRegister.ContractsLimits.SliceLast(
  21. &Period,
  22. Entity = &Entity
  23. AND Company = &Company
  24. AND LimitType IN (&LimitTypes)
  25. AND Contract IN (&Contract, VALUE(Catalog.CompanyContracts.EmptyRef))) AS ContractsLimitsSliceLast
  26. LEFT JOIN Rates AS Rates
  27. ON ContractsLimitsSliceLast.Currency = Rates.Currency
  28. WHERE
  29. ContractsLimitsSliceLast.Status
  30. ;
  31.  
  32. ////////////////////////////////////////////////////////////////////////////////
  33. SELECT
  34. CustomerAccountsBalance.AmountCurBalance AS AmountCurBalance,
  35. CustomerAccountsBalance.Contract AS Contract
  36. INTO ARWithCheques
  37. FROM
  38. AccumulationRegister.CustomerAccounts.Balance(
  39. &Period,
  40. Entity = &Entity
  41. AND Company = &Company) AS CustomerAccountsBalance
  42.  
  43. UNION ALL
  44.  
  45. SELECT
  46. AccountsReceivableDeferredBalance.AmountCurBalance,
  47. AccountsReceivableDeferredBalance.Contract
  48. FROM
  49. AccumulationRegister.AccountsReceivableDeferred.Balance(
  50. &Period,
  51. Entity = &Entity
  52. AND Company = &Company) AS AccountsReceivableDeferredBalance
  53. ;
  54.  
  55. ////////////////////////////////////////////////////////////////////////////////
  56. SELECT
  57. Limits.LimitType AS LimitType,
  58. Limits.Amount AS Amount,
  59. Limits.CurrencyLimit AS CurrencyLimit,
  60. SUM(ISNULL(CASE
  61. WHEN Limits.Contract = VALUE(Catalog.CompanyContracts.EmptyRef)
  62. THEN CAST(ARWithCheques.AmountCurBalance * Rates.ExchangeRate * Limits.MultiplicityLimit / (Limits.RateLimit * ISNULL(Rates.Multiplicity, 1)) AS NUMBER(15, 2))
  63. ELSE ARWithCheques.AmountCurBalance
  64. END, 0)) AS AmountBalance,
  65. Limits.Contract AS Contract
  66. INTO LimitBalance
  67. FROM
  68. Limits AS Limits
  69. LEFT JOIN ARWithCheques AS ARWithCheques
  70. LEFT JOIN Rates AS Rates
  71. ON ARWithCheques.Contract.AccountsCurrency = Rates.Currency
  72. ON (CASE
  73. WHEN Limits.Contract = VALUE(Catalog.CompanyContracts.EmptyRef)
  74. THEN TRUE
  75. ELSE Limits.Contract = ARWithCheques.Contract
  76. END)
  77. WHERE
  78. Limits.LimitType = VALUE(enum.limittype.debit)
  79.  
  80. GROUP BY
  81. Limits.LimitType,
  82. Limits.Amount,
  83. Limits.Contract,
  84. Limits.CurrencyLimit
  85.  
  86. UNION ALL
  87.  
  88. SELECT
  89. Limits.LimitType,
  90. Limits.Amount,
  91. Limits.CurrencyLimit,
  92. SUM(ISNULL(CASE
  93. WHEN Limits.Contract = VALUE(Catalog.CompanyContracts.EmptyRef)
  94. THEN CAST(VendorAccountsBalance.AmountCurBalance * Rates.ExchangeRate * Limits.MultiplicityLimit / (Limits.RateLimit * ISNULL(Rates.Multiplicity, 1)) AS NUMBER(15, 2))
  95. ELSE VendorAccountsBalance.AmountCurBalance
  96. END, 0)),
  97. Limits.Contract
  98. FROM
  99. Limits AS Limits
  100. LEFT JOIN AccumulationRegister.VendorAccounts.Balance(
  101. &Period,
  102. Entity = &Entity
  103. AND Company = &Company) AS VendorAccountsBalance
  104. LEFT JOIN Rates AS Rates
  105. ON VendorAccountsBalance.Contract.AccountsCurrency = Rates.Currency
  106. ON (CASE
  107. WHEN Limits.Contract = VALUE(Catalog.CompanyContracts.EmptyRef)
  108. THEN TRUE
  109. ELSE Limits.Contract = VendorAccountsBalance.Contract
  110. END)
  111. WHERE
  112. Limits.LimitType = VALUE(enum.limittype.credit)
  113.  
  114. GROUP BY
  115. Limits.LimitType,
  116. Limits.Amount,
  117. Limits.Contract,
  118. Limits.CurrencyLimit
  119.  
  120. UNION ALL
  121.  
  122. SELECT
  123. Limits.LimitType,
  124. Limits.Amount,
  125. Limits.CurrencyLimit,
  126. SUM(ISNULL(CASE
  127. WHEN Limits.Contract = VALUE(Catalog.CompanyContracts.EmptyRef)
  128. THEN CAST(SalesByCustomerOrdersTurnovers.AmountCurTurnover * Rates.ExchangeRate * Limits.MultiplicityLimit / (Limits.RateLimit * ISNULL(Rates.Multiplicity, 1)) AS NUMBER(15, 2))
  129. ELSE SalesByCustomerOrdersTurnovers.AmountCurTurnover
  130. END, 0)),
  131. Limits.Contract
  132. FROM
  133. Limits AS Limits
  134. LEFT JOIN AccumulationRegister.SalesByCustomerOrders.Turnovers(
  135. ,
  136. &Period,
  137. ,
  138. Entity = &Entity
  139. AND Company = &Company) AS SalesByCustomerOrdersTurnovers
  140. LEFT JOIN Rates AS Rates
  141. ON SalesByCustomerOrdersTurnovers.Contract.AccountsCurrency = Rates.Currency
  142. ON (CASE
  143. WHEN Limits.Contract = VALUE(Catalog.CompanyContracts.EmptyRef)
  144. THEN TRUE
  145. ELSE Limits.Contract = SalesByCustomerOrdersTurnovers.Contract
  146. END)
  147. WHERE
  148. Limits.LimitType = VALUE(enum.limittype.income)
  149.  
  150. GROUP BY
  151. Limits.LimitType,
  152. Limits.Amount,
  153. Limits.Contract,
  154. Limits.CurrencyLimit
  155.  
  156. UNION ALL
  157.  
  158. SELECT
  159. Limits.LimitType,
  160. Limits.Amount,
  161. Limits.CurrencyLimit,
  162. SUM(ISNULL(CAST(PurchasingTurnovers.AmountTurnover * Rates.ExchangeRate * Limits.MultiplicityLimit / (Limits.RateLimit * ISNULL(Rates.Multiplicity, 1)) AS NUMBER(15, 2)), 0)),
  163. Limits.Contract
  164. FROM
  165. Limits AS Limits
  166. LEFT JOIN AccumulationRegister.Purchasing.Turnovers(
  167. ,
  168. &Period,
  169. ,
  170. Entity = &Entity
  171. AND Document.Company = &Company) AS PurchasingTurnovers
  172. LEFT JOIN Rates AS Rates
  173. ON (Rates.Currency = &RegCurr)
  174. ON (CASE
  175. WHEN Limits.Contract = VALUE(Catalog.CompanyContracts.EmptyRef)
  176. THEN TRUE
  177. ELSE Limits.Contract = PurchasingTurnovers.Document.Contract
  178. END)
  179. WHERE
  180. Limits.LimitType = VALUE(enum.limittype.expense)
  181.  
  182. GROUP BY
  183. Limits.LimitType,
  184. Limits.Amount,
  185. Limits.Contract,
  186. Limits.CurrencyLimit
  187. ;
  188.  
  189. ////////////////////////////////////////////////////////////////////////////////
  190. SELECT
  191. LimitBalance.LimitType AS LimitType,
  192. LimitBalance.Amount AS Amount,
  193. LimitBalance.CurrencyLimit AS CurrencyLimit,
  194. LimitBalance.AmountBalance AS AmountBalance,
  195. LimitBalance.Amount - LimitBalance.AmountBalance AS ExceededAmount,
  196. (LimitBalance.Amount - LimitBalance.AmountBalance) / LimitBalance.Amount AS CreditLimitBalance,
  197. LimitBalance.Contract AS Contract
  198. FROM
  199. LimitBalance AS LimitBalance
  200. WHERE
  201. (LimitBalance.Amount - LimitBalance.AmountBalance) / LimitBalance.Amount < 1 / 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement