资源计划导出1.1.sql 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. select a.id,
  2. concat(ifnull(a.proName, ''), ifnull(a.custName, '')) proNamecustName,
  3. a.proName proName,
  4. a.custName custName,
  5. ot.pro_label proTypeName,
  6. DATE_FORMAT(a.validAt, '%Y-%m-%d') validAt,
  7. DATE_FORMAT(a.real_valid_At, '%Y-%m-%d') realValidAt,
  8. a.signStatus signStatus,
  9. a.proStatus proStatus,
  10. DATE_FORMAT(b.actual_check_time, '%Y-%m-%d') actualCheckTime,
  11. DATE_FORMAT(a.closing_item_time, '%Y-%m-%d') closingItemTime,
  12. ctr.hours accumulatedOccurrence,
  13. c.totalAmount ctrAmount,
  14. acc.receiveAmount ctrReceivedPayment,
  15. f.orderAmount purchaseAmount,
  16. f.payAmount purchasePaid,
  17. ctr.cost occurredLaborCost,
  18. d.expenseAmount occurredExpense,
  19. ROUND((IFNULL(c.totalAmount, 0) - IFNULL(acc.receiveAmount, 0) - IFNULL(ctr.cost, 0) -
  20. IFNULL(d.expenseAmount, 0)), 2) currentProRevExp,
  21. ROUND(((IFNULL(c.totalAmount, 0) - IFNULL(acc.receiveAmount, 0) - IFNULL(ctr.cost, 0) -
  22. IFNULL(d.expenseAmount, 0)) + (IFNULL(c.totalAmount, 0) - IFNULL(acc.receiveAmount, 0)) -
  23. (IFNULL(f.orderAmount, 0) - IFNULL(f.payAmount, 0)) - IFNULL(e.cost, 0)), 2) totalProGrossMargin,
  24. ROUND((IFNULL(c.totalAmount, 0) - IFNULL(acc.receiveAmount, 0)), 2) ctrUnpaidPayments,
  25. ROUND((IFNULL(f.orderAmount, 0) - IFNULL(f.payAmount, 0)), 2) purchaseUnpaidPayments,
  26. e.cost planCost,
  27. ROUND(beforeUserMoney, 2) beforeUserMoney,
  28. ROUND(g.beforeCost, 2) beforeCost,
  29. ROUND(h.afterUserMoney, 2) afterUserMoney,
  30. ROUND(h.afterCost, 2) afterCost,
  31. j.*
  32. from pro_main a
  33. -- ===================================================================
  34. left join pro_main_extend b ON a.id = b.pro_id
  35. -- ===================================================================
  36. left join
  37. (select b.pro_id,
  38. sum(a.review_hour) hours,
  39. sum(ROUND(b.post_cost * IFNULL(a.review_hour, 0) / (22 * 8), 2)) cost
  40. from oa_pro_task_hour a
  41. left join oa_pro_task b on a.task_id = b.id
  42. where a.hour_state = '2'
  43. and DATE_FORMAT(a.hour_date, '%Y-%m') <= #{yearMonth}
  44. group by b.pro_id) ctr on a.id = ctr.pro_id
  45. -- ===================================================================
  46. left join
  47. (select proId, sum(receiveAmount) receiveAmount
  48. from acc_receivable
  49. where planStatus = '2'
  50. and DATE_FORMAT(receiveDate, '%Y-%m') <= #{yearMonth}
  51. group by proId) acc on a.id = acc.proId
  52. -- ===================================================================
  53. left join ctr_main c ON a.id = c.proId
  54. -- ===================================================================
  55. left join (select a.pro_id, sum(a.order_amount) orderAmount, sum(b.pay_amount) payAmount
  56. from out_order_main a
  57. left join (select out_order_main_id, sum(pay_amount) pay_amount
  58. from out_order_pay
  59. where status = '2'
  60. and DATE_FORMAT(pay_at, '%Y-%m') <= #{yearMonth}
  61. group by out_order_main_id) b on a.id = b.out_order_main_id
  62. group by a.pro_id) f ON f.pro_id = a.id
  63. -- ===================================================================
  64. left join oa_pro_type ot ON ot.group_id = a.group_id and ot.pro_value = a.proType
  65. -- ===================================================================
  66. left join (select pro_id, sum(expense_amount) expenseAmount
  67. from oa_expense_ledger
  68. where DATE_FORMAT(happen_date, '%Y-%m') <= #{yearMonth}
  69. group by pro_id) d ON d.pro_id = a.id
  70. -- ===================================================================
  71. left join (select a.pro_id, ROUND(sum((c.post_cost / 22) * IFNULL(a.workload, 0)), 2) as cost
  72. from resource_plan_staff_detail a
  73. left join resource_plan_staff b ON a.plan_staff_id = b.id
  74. left join
  75. ( <include refid="oa_user_cost_pro"/> )
  76. c ON b.posts = c.post_id and c.post_level=b.post_level
  77. where year_month_str > #{yearMonth}
  78. group by a.pro_id) e ON e.pro_id = a.id
  79. -- ===================================================================
  80. left join (select a.pro_id,
  81. sum(a.workload) beforeUserMoney,
  82. ROUND(sum((c.post_cost / 22) * IFNULL(a.workload, 0)), 2) as beforeCost
  83. from resource_plan_staff_detail a
  84. left join resource_plan_staff b ON a.plan_staff_id = b.id
  85. left join ( <include refid="oa_user_cost_pro"/> ) c ON b.posts = c.post_id and c.post_level=b.post_level
  86. where year_month_str &lt; #{year}'-01'
  87. group by a.pro_id) g on g.pro_id = a.id
  88. -- ===================================================================
  89. left join (select a.pro_id,
  90. sum(a.workload) afterUserMoney,
  91. ROUND(sum((c.post_cost / 22) * IFNULL(a.workload, 0)), 2) as afterCost
  92. from resource_plan_staff_detail a
  93. left join resource_plan_staff b ON a.plan_staff_id = b.id
  94. left join ( <include refid="oa_user_cost_pro"/> ) c ON b.posts = c.post_id and c.post_level=b.post_level
  95. where year_month_str > #{year}'-12'
  96. group by a.pro_id) h on h.pro_id = a.id
  97. -- ===================================================================
  98. left join (SELECT a.pro_id,
  99. sum(a.workload) userMoney,
  100. ROUND(sum((c.post_cost / 22) * IFNULL(a.workload, 0)),
  101. 2
  102. ) AS cost,
  103. sum(CASE WHEN MOD(year_mon,100) = 1 THEN a.workload ELSE 0 END ) AS userMoney1,
  104. ROUND(sum(CASE WHEN MOD(year_mon,100) = 1 THEN ( c.post_cost / 22 )* IFNULL( a.workload, 0 ) ELSE 0 END ),2) AS cost1,
  105. sum(CASE WHEN MOD(year_mon,100) = 2 THEN a.workload ELSE 0 END ) AS userMoney2,
  106. ROUND(sum(CASE WHEN MOD(year_mon,100) = 2 THEN ( c.post_cost / 22 )* IFNULL( a.workload, 0 ) ELSE 0 END ),2) AS cost2,
  107. sum(CASE WHEN MOD(year_mon,100) = 3 THEN a.workload ELSE 0 END ) AS userMoney3,
  108. ROUND(sum(CASE WHEN MOD(year_mon,100) = 3 THEN ( c.post_cost / 22 )* IFNULL( a.workload, 0 ) ELSE 0 END ),2) AS cost3,
  109. sum(CASE WHEN MOD(year_mon,100) = 4 THEN a.workload ELSE 0 END ) AS userMoney4,
  110. ROUND(sum(CASE WHEN MOD(year_mon,100) = 4 THEN ( c.post_cost / 22 )* IFNULL( a.workload, 0 ) ELSE 0 END ),2) AS cost4,
  111. sum(CASE WHEN MOD(year_mon,100) = 5 THEN a.workload ELSE 0 END ) AS userMoney5,
  112. ROUND(sum(CASE WHEN MOD(year_mon,100) = 5 THEN ( c.post_cost / 22 )* IFNULL( a.workload, 0 ) ELSE 0 END ),2) AS cost5,
  113. sum(CASE WHEN MOD(year_mon,100) = 6 THEN a.workload ELSE 0 END ) AS userMoney6,
  114. ROUND(sum(CASE WHEN MOD(year_mon,100) = 6 THEN ( c.post_cost / 22 )* IFNULL( a.workload, 0 ) ELSE 0 END ),2) AS cost6,
  115. sum(CASE WHEN MOD(year_mon,100) = 7 THEN a.workload ELSE 0 END ) AS userMoney7,
  116. ROUND(sum(CASE WHEN MOD(year_mon,100) = 7 THEN ( c.post_cost / 22 )* IFNULL( a.workload, 0 ) ELSE 0 END ),2) AS cost7,
  117. sum(CASE WHEN MOD(year_mon,100) = 8 THEN a.workload ELSE 0 END ) AS userMoney8,
  118. ROUND(sum(CASE WHEN MOD(year_mon,100) = 8 THEN ( c.post_cost / 22 )* IFNULL( a.workload, 0 ) ELSE 0 END ),2) AS cost8,
  119. sum(CASE WHEN MOD(year_mon,100) = 9 THEN a.workload ELSE 0 END ) AS userMoney9,
  120. ROUND(sum(CASE WHEN MOD(year_mon,100) = 9 THEN ( c.post_cost / 22 )* IFNULL( a.workload, 0 ) ELSE 0 END ),2) AS cost9,
  121. sum(CASE WHEN MOD(year_mon,100) = 10 THEN a.workload ELSE 0 END ) AS userMoney10,
  122. ROUND(sum(CASE WHEN MOD(year_mon,100) = 10 THEN ( c.post_cost / 22 )* IFNULL( a.workload, 0 ) ELSE 0 END ),2) AS cost10,
  123. sum(CASE WHEN MOD(year_mon,100) = 11 THEN a.workload ELSE 0 END ) AS userMoney11,
  124. ROUND(sum(CASE WHEN MOD(year_mon,100) = 11 THEN ( c.post_cost / 22 )* IFNULL( a.workload, 0 ) ELSE 0 END ),2) AS cost11,
  125. sum(CASE WHEN MOD(year_mon,100) = 12 THEN a.workload ELSE 0 END ) AS userMoney12,
  126. ROUND(sum(CASE WHEN MOD(year_mon,100) = 12 THEN ( c.post_cost / 22 )* IFNULL( a.workload, 0 ) ELSE 0 END ),2) AS cost12
  127. FROM resource_plan_staff_detail a
  128. LEFT JOIN resource_plan_staff b ON a.plan_staff_id = b.id
  129. LEFT JOIN ( <include refid="oa_user_cost_pro"/> ) c ON b.posts = c.post_id and c.post_level=b.post_level
  130. WHERE FLOOR(year_mon/100) = #{year}
  131. GROUP BY a.pro_id) j on j.pro_id = a.id
  132. where a.group_id = #{groupId} order by a.id desc