Class: Report::SalesProfit::SalesProfit
- Inherits:
-
Object
- Object
- Report::SalesProfit::SalesProfit
- Defined in:
- app/services/report/sales_profit/sales_profit.rb
Defined Under Namespace
Classes: Result
Class Method Summary collapse
- .data_first_level(start_date, end_date, report_groupings, customer_ids, company_ids, state_codes, sales_ids, granularity) ⇒ Object
- .data_second_level(start_date, end_date, report_groupings, customer_ids, company_ids, state_codes, sales_ids, granularity) ⇒ Object
- .data_third_level(start_date, end_date, report_groupings, customer_ids, company_ids, state_codes, sales_ids, granularity) ⇒ Object
- .data_total(start_date, end_date, report_groupings, customer_ids, company_ids, state_codes, sales_ids, granularity) ⇒ Object
- .result_report(options = {}) ⇒ Object
Class Method Details
.data_first_level(start_date, end_date, report_groupings, customer_ids, company_ids, state_codes, sales_ids, granularity) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'app/services/report/sales_profit/sales_profit.rb', line 97 def self.data_first_level(start_date,end_date,report_groupings,customer_ids,company_ids,state_codes,sales_ids,granularity) start_date_bc = start_date.days_ago(90) end_date_bc = start_date.days_ago(1) report_grouping_sql = " and vs.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" if report_groupings.present? customer_id_sql = " and vs.customer_id in (#{customer_ids.map{|cu| "'#{cu}'" }.join(',')})" if customer_ids.present? company_id_sql = " and vs.company_id in (#{company_ids.map{|co| "'#{co}'" }.join(',')})" if company_ids.present? state_code_sql = " and ad.state_code in (#{state_codes.map{|sc| "'#{sc}'" }.join(',')})" if state_codes.present? sales_type_sql = " and vs.invoice_type in (#{sales_ids.map{|st| "'#{st}'" }.join(',')})" if sales_ids.present? select = "group1" if granularity == 1 select = 'report_grouping' if (granularity == 2 || granularity == 4) select = 'state_name' if granularity == 3 sub_select = "trim(split_part(pl.lineage_expanded,'>',1)) as group1,case when trim(split_part(lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(lineage_expanded,'>',2)) end as breakdown" if granularity == 1 sub_select = "vs.report_grouping,trim(split_part(pl.lineage_expanded,'>',1)) as breakdown" if (granularity == 2 || granularity == 4) sub_select = "case when st.name is null then 'Unknown' else (st.country_iso3 || ' - ' || st.name) end as state_name,vs.report_grouping as breakdown" if granularity == 3 join = ' left join states st on ad.state_code = st.code' if granularity == 3 sql = <<-SQL select #{select}, sum(case when gl_date >= '#{start_date}' then quantity else 0 end) as qty, sum(case when gl_date >= '#{start_date}' then (quantity * coalesce(sqft::decimal,0)) else 0 end) as sqft, sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) as sales, sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) as cogs, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mc, sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * vc_amount) + (quantity * amazon_vc) else 0 end) as variable_cost, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * vc_amount) + (quantity * amazon_vc) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mvc, sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) as sales_tax, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mst, sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) as sales_commission, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as msc, sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) as ccpp_cost, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mcp, sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) as shp_cost, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as msh, sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) as bad_credit, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mbc, sum(case when gl_date >= '#{start_date}' then returns else 0 end) as returns, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then returns else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mr, (sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) - --total with discounts sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) - -- unit_cogs amount sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * vc_amount) + (quantity * amazon_vc) else 0 end) - --variable cost sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) - -- taxes amount sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) - -- commission amount sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) - -- credit card and paypal fees sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) - -- shipping cost sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) - --bad credit sum(case when gl_date >= '#{start_date}' then returns else 0 end) --returns ) as profit, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else (( (sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) - --total with discounts sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) - -- unit_cogs amount sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * vc_amount) + (quantity * amazon_vc) else 0 end) - --variable cost sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) - -- taxes amount sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) - -- commission amount sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) - -- credit card and paypal fees sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) - -- shipping cost sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) - --bad credit sum(case when gl_date >= '#{start_date}' then returns else 0 end) --returns ) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mp, count(distinct breakdown) as rows_num from ( select #{sub_select}, vs.quantity,vs.sqft,vs.exchange_rate,msrp,vs.discounted_price,vs.unit_cogs,commision_rate,vc_ptg,vc_amount,amazon_vc,invoice_state,coalesce(ccp_distributed_cost,0) as ccp_distributed_cost, coalesce(sp_distributed_cost,0) as sp_distributed_cost,gl_date,due_date,tax_total,coalesce(((vri.quantity * vri.exchange_rate * vri.discounted_price) * -1),0) as returns from view_sales vs inner join product_lines pl on vs.primary_product_line_id = pl.id inner join addresses ad on vs.shipping_address_id = ad.id left join view_returned_items vri on vs.order_id = vri.order_id and vs.item_id = vri.item_id and vs.line_item_id = vri.line_item_id #{join} where ((gl_date between '#{start_date}' and '#{end_date}') or (due_date between '#{start_date}' and '#{end_date}' and invoice_state = 'unpaid')) #{report_grouping_sql} #{customer_id_sql} #{company_id_sql} #{state_code_sql} #{sales_type_sql} ) a group by #{select} order by profit desc; SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys } end |
.data_second_level(start_date, end_date, report_groupings, customer_ids, company_ids, state_codes, sales_ids, granularity) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'app/services/report/sales_profit/sales_profit.rb', line 190 def self.data_second_level(start_date,end_date,report_groupings,customer_ids,company_ids,state_codes,sales_ids,granularity) start_date_bc = start_date.days_ago(90) end_date_bc = start_date.days_ago(1) report_grouping_sql = " and vs.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" if report_groupings.present? customer_id_sql = " and vs.customer_id in (#{customer_ids.map{|cu| "'#{cu}'" }.join(',')})" if customer_ids.present? company_id_sql = " and vs.company_id in (#{company_ids.map{|co| "'#{co}'" }.join(',')})" if company_ids.present? state_code_sql = " and ad.state_code in (#{state_codes.map{|sc| "'#{sc}'" }.join(',')})" if state_codes.present? sales_type_sql = " and vs.invoice_type in (#{sales_ids.map{|st| "'#{st}'" }.join(',')})" if sales_ids.present? select = "group1,group2" if granularity == 1 select = "report_grouping,group1" if (granularity == 2 || granularity == 4) select = 'state_name,report_grouping' if granularity == 3 sub_select = "trim(split_part(pl.lineage_expanded,'>',1)) as group1, case when trim(split_part(pl.lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(pl.lineage_expanded,'>',2)) end as group2, vs.item_id as breakdown" if granularity == 1 sub_select = "vs.report_grouping,trim(split_part(pl.lineage_expanded,'>',1)) as group1, vs.customer_id as breakdown" if granularity == 2 sub_select = "case when st.name is null then 'Unknown' else (st.country_iso3 || ' - ' || st.name) end as state_name, vs.report_grouping, trim(split_part(pl.lineage_expanded,'>',1)) as breakdown" if granularity == 3 sub_select = "vs.report_grouping,trim(split_part(pl.lineage_expanded,'>',1)) as group1, case when trim(split_part(pl.lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(pl.lineage_expanded,'>',2)) || case when trim(split_part(pl.lineage_expanded,'>',3)) = '' then '' else ' - ' || trim(split_part(pl.lineage_expanded,'>',3)) || case when trim(split_part(pl.lineage_expanded,'>',4)) = '' then '' else ' - ' || trim(split_part(pl.lineage_expanded,'>',4)) end end end as breakdown" if granularity == 4 join = ' inner join states st on ad.state_code = st.code' if granularity == 3 sql = <<-SQL select #{select}, sum(case when gl_date >= '#{start_date}' then quantity else 0 end) as qty, sum(case when gl_date >= '#{start_date}' then (quantity * coalesce(sqft::decimal,0)) else 0 end) as sqft, sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) as sales, sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) as cogs, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mc, sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * vc_amount) + (quantity * amazon_vc) else 0 end) as variable_cost, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * vc_amount) + (quantity * amazon_vc) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mvc, sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) as sales_tax, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mst, sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) as sales_commission, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as msc, sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) as ccpp_cost, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mcp, sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) as shp_cost, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as msh, sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) as bad_credit, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mbc, sum(case when gl_date >= '#{start_date}' then returns else 0 end) as returns, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then returns else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mr, (sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) - --total with discounts sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) - -- unit_cogs amount sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * vc_amount) + (quantity * amazon_vc) else 0 end) - --variable cost sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) - -- taxes amount sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) - -- commission amount sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) - -- credit card and paypal fees sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) - -- shipping cost sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) - --bad credit sum(case when gl_date >= '#{start_date}' then returns else 0 end) --returns ) as profit, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else (( (sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) - --total with discounts sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) - -- unit_cogs amount sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * vc_amount) + (quantity * amazon_vc) else 0 end) - --variable cost sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) - -- taxes amount sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) - -- commission amount sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) - -- credit card and paypal fees sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) - -- shipping cost sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) - --bad credit sum(case when gl_date >= '#{start_date}' then returns else 0 end) --returns ) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mp, count(distinct breakdown) as rows_num from ( select #{sub_select}, vs.quantity,vs.sqft,vs.exchange_rate,msrp,vs.discounted_price,vs.unit_cogs,commision_rate,vc_ptg,vc_amount,amazon_vc,invoice_state,coalesce(ccp_distributed_cost,0) as ccp_distributed_cost, coalesce(sp_distributed_cost,0) as sp_distributed_cost,gl_date,due_date,tax_total,coalesce(((vri.quantity * vri.exchange_rate * vri.discounted_price) * -1),0) as returns from view_sales vs inner join product_lines pl on vs.primary_product_line_id = pl.id inner join addresses ad on vs.shipping_address_id = ad.id left join view_returned_items vri on vs.order_id = vri.order_id and vs.item_id = vri.item_id and vs.line_item_id = vri.line_item_id #{join} where ((gl_date between '#{start_date}' and '#{end_date}') or (due_date between '#{start_date}' and '#{end_date}' and invoice_state = 'unpaid')) #{report_grouping_sql} #{customer_id_sql} #{company_id_sql} #{state_code_sql} #{sales_type_sql} ) a group by #{select} order by profit desc; SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys } end |
.data_third_level(start_date, end_date, report_groupings, customer_ids, company_ids, state_codes, sales_ids, granularity) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'app/services/report/sales_profit/sales_profit.rb', line 292 def self.data_third_level(start_date,end_date,report_groupings,customer_ids,company_ids,state_codes,sales_ids,granularity) start_date_bc = start_date.days_ago(90) end_date_bc = start_date.days_ago(1) report_grouping_sql = " and vs.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" if report_groupings.present? customer_id_sql = " and vs.customer_id in (#{customer_ids.map{|cu| "'#{cu}'" }.join(',')})" if customer_ids.present? company_id_sql = " and vs.company_id in (#{company_ids.map{|co| "'#{co}'" }.join(',')})" if company_ids.present? state_code_sql = " and ad.state_code in (#{state_codes.map{|sc| "'#{sc}'" }.join(',')})" if state_codes.present? sales_type_sql = " and vs.invoice_type in (#{sales_ids.map{|st| "'#{st}'" }.join(',')})" if sales_ids.present? select = "group1,group2,sku" if granularity == 1 select = "report_grouping,group1,full_name" if granularity == 2 select = "state_name,report_grouping,group1" if granularity == 3 select = "report_grouping,group1,group3" if granularity == 4 sub_select = "trim(split_part(lineage_expanded,'>',1)) as group1, case when trim(split_part(lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(lineage_expanded,'>',2)) end as group2, sku" if granularity == 1 sub_select = "vs.report_grouping, trim(split_part(lineage_expanded,'>',1)) as group1, full_name" if granularity == 2 sub_select = "case when st.name is null then 'Unknown' else (st.country_iso3 || ' - ' || st.name) end as state_name, vs.report_grouping, trim(split_part(lineage_expanded,'>',1)) as group1" if granularity == 3 sub_select = "vs.report_grouping, trim(split_part(lineage_expanded,'>',1)) as group1, case when trim(split_part(pl.lineage_expanded,'>',2)) = '' then 'N/A' else trim(split_part(pl.lineage_expanded,'>',2)) || case when trim(split_part(pl.lineage_expanded,'>',3)) = '' then '' else ' - ' || trim(split_part(pl.lineage_expanded,'>',3)) || case when trim(split_part(pl.lineage_expanded,'>',4)) = '' then '' else ' - ' || trim(split_part(pl.lineage_expanded,'>',4)) end end end as group3" if granularity == 4 join = 'inner join items i on vs.item_id = i.id' if granularity == 1 join = 'inner join parties cu on vs.customer_id = cu.id' if granularity == 2 join = 'left join states st on ad.state_code = st.code' if granularity == 3 sql = <<-SQL select #{select}, sum(case when gl_date >= '#{start_date}' then quantity else 0 end) as qty, sum(case when gl_date >= '#{start_date}' then (quantity * coalesce(sqft::decimal,0)) else 0 end) as sqft, sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) as sales, sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) as cogs, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mc, sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * vc_amount) + (quantity * amazon_vc) else 0 end) as variable_cost, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * vc_amount) + (quantity * amazon_vc) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mvc, sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) as sales_tax, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mst, sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) as sales_commission, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as msc, sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) as ccpp_cost, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mcp, sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) as shp_cost, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as msh, sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) as bad_credit, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mbc, sum(case when gl_date >= '#{start_date}' then returns else 0 end) as returns, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else ((sum(case when gl_date >= '#{start_date}' then returns else 0 end) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mr, (sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) - --total with discounts sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) - -- unit_cogs amount sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * vc_amount) + (quantity * amazon_vc) else 0 end) - --variable cost sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) - -- taxes amount sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) - -- commission amount sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) - -- credit card and paypal fees sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) - -- shipping cost sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) - --bad credit sum(case when gl_date >= '#{start_date}' then returns else 0 end) --returns ) as profit, case when sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) = 0 then 0 else (( (sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) - --total with discounts sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) - -- unit_cogs amount sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * vc_amount) + (quantity * amazon_vc) else 0 end) - --variable cost sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) - -- taxes amount sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) - -- commission amount sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) - -- credit card and paypal fees sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) - -- shipping cost sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) - --bad credit sum(case when gl_date >= '#{start_date}' then returns else 0 end) --returns ) / sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end)) * 100) end as mp from ( select #{sub_select}, vs.quantity,vs.sqft,vs.exchange_rate,msrp,vs.discounted_price,vs.unit_cogs,commision_rate,vc_ptg,vc_amount,amazon_vc,invoice_state,coalesce(ccp_distributed_cost,0) as ccp_distributed_cost, coalesce(sp_distributed_cost,0) as sp_distributed_cost,gl_date,due_date,tax_total,coalesce(((vri.quantity * vri.exchange_rate * vri.discounted_price) * -1),0) as returns from view_sales vs inner join product_lines pl on vs.primary_product_line_id = pl.id inner join addresses ad on vs.shipping_address_id = ad.id left join view_returned_items vri on vs.order_id = vri.order_id and vs.item_id = vri.item_id and vs.line_item_id = vri.line_item_id #{join} where ((gl_date between '#{start_date}' and '#{end_date}') or (due_date between '#{start_date}' and '#{end_date}' and invoice_state = 'unpaid')) #{report_grouping_sql} #{customer_id_sql} #{company_id_sql} #{state_code_sql} #{sales_type_sql} ) a group by #{select} order by profit desc; SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys } end |
.data_total(start_date, end_date, report_groupings, customer_ids, company_ids, state_codes, sales_ids, granularity) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/services/report/sales_profit/sales_profit.rb', line 36 def self.data_total(start_date,end_date,report_groupings,customer_ids,company_ids,state_codes,sales_ids,granularity) start_date_bc = start_date.days_ago(90) end_date_bc = start_date.days_ago(1) report_grouping_sql = " and vs.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')})" if report_groupings.present? customer_id_sql = " and vs.customer_id in (#{customer_ids.map{|cu| "'#{cu}'" }.join(',')})" if customer_ids.present? company_id_sql = " and vs.company_id in (#{company_ids.map{|co| "'#{co}'" }.join(',')})" if company_ids.present? state_code_sql = " and ad.state_code in (#{state_codes.map{|sc| "'#{sc}'" }.join(',')})" if state_codes.present? sales_type_sql = " and vs.invoice_type in (#{sales_ids.map{|st| "'#{st}'" }.join(',')})" if sales_ids.present? select = "trim(split_part(lineage_expanded,'>',1)) as breakdown" if granularity == 1 select = 'vs.report_grouping as breakdown' if (granularity == 2 || granularity == 4) select = "coalesce(st.code, 'Unknown') as breakdown" if granularity == 3 join = ' left join states st on ad.state_code = st.code' if granularity == 3 sql = <<-SQL select sum(case when gl_date >= '#{start_date}' then quantity else 0 end) as qty, sum(case when gl_date >= '#{start_date}' then (quantity * coalesce(sqft::decimal,0)) else 0 end) as sqft, sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) as sales, sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) as cogs, sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * vc_amount) + (quantity * amazon_vc) else 0 end) as variable_cost, sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) as sales_tax, sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) as sales_commission, sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) as ccpp_cost, sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) as shp_cost, sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) as bad_credit, sum(case when gl_date >= '#{start_date}' then returns else 0 end) as returns, (sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price) else 0 end) - --total with discounts sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * unit_cogs) else 0 end) - -- unit_cogs amount sum(case when gl_date >= '#{start_date}' then (quantity * exchange_rate * discounted_price * vc_ptg) + (quantity * amazon_vc) else 0 end) - --variable cost sum(case when gl_date >= '#{start_date}' then (exchange_rate * tax_total) else 0 end) - -- taxes amount sum(case when invoice_state = 'paid' and gl_date >= '#{start_date}' then (((quantity * exchange_rate * discounted_price) - (quantity * exchange_rate * unit_cogs)) * commision_rate) else 0 end) - -- commission amount sum(case when gl_date >= '#{start_date}' then (ccp_distributed_cost * exchange_rate) else 0 end) - -- credit card and paypal fees sum(case when gl_date >= '#{start_date}' then (sp_distributed_cost * exchange_rate) else 0 end) - -- shipping cost sum(case when ('#{end_date}'::date - due_date) > 90 and invoice_state = 'unpaid' then (quantity * exchange_rate * discounted_price) else 0 end) - --bad credit sum(case when gl_date >= '#{start_date}' then returns else 0 end) --returns ) as profit, count(distinct breakdown) as rows_num from ( select #{select}, vs.quantity,vs.sqft,vs.exchange_rate,msrp,vs.discounted_price,vs.unit_cogs,commision_rate,vc_ptg,vc_amount,amazon_vc,invoice_state,coalesce(ccp_distributed_cost,0) as ccp_distributed_cost, coalesce(sp_distributed_cost,0) as sp_distributed_cost,gl_date,due_date,tax_total,coalesce(((vri.quantity * vri.exchange_rate * vri.discounted_price) * -1),0) as returns from view_sales vs inner join product_lines pl on vs.primary_product_line_id = pl.id inner join addresses ad on vs.shipping_address_id = ad.id left join view_returned_items vri on vs.order_id = vri.order_id and vs.item_id = vri.item_id and vs.line_item_id = vri.line_item_id #{join} where ((gl_date between '#{start_date}' and '#{end_date}') or (due_date between '#{start_date}' and '#{end_date}' and invoice_state = 'unpaid')) #{report_grouping_sql} #{customer_id_sql} #{company_id_sql} #{state_code_sql} #{sales_type_sql} ) a; SQL results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys } results.map{ |r| Hash[r.keys.zip(r.values.map(&:to_f))] } end |
.result_report(options = {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/services/report/sales_profit/sales_profit.rb', line 8 def self.result_report( = {}) start_date = [:period1_gteq] end_date = [:period1_lteq] report_groupings = [:report_groupings] customers = [:customer_ids] branches = [:company_ids] states = [:state_codes] sales_types = [:sales_ids] granularity = [:granularity_level_ids][0].to_i order_by = [:order_by_ids][0].to_i total = data_total(start_date,end_date,report_groupings,customers,branches,states,sales_types,granularity) first_level = data_first_level(start_date,end_date,report_groupings,customers,branches,states,sales_types,granularity) second_level = data_second_level(start_date,end_date,report_groupings,customers,branches,states,sales_types,granularity) third_level = data_third_level(start_date,end_date,report_groupings,customers,branches,states,sales_types,granularity) = ['profit','sales','discounts','cogs','variable_cost','sales_tax','sales_commission','ccpp_cost','shp_cost','bad_credit','returns'] first_level = first_level.sort_by { |k| k[[order_by - 1].to_sym].to_f }.reverse second_level = second_level.sort_by { |k| k[[order_by - 1].to_sym].to_f }.reverse third_level = third_level.sort_by { |k| k[[order_by - 1].to_sym].to_f }.reverse top = first_level.first(5) Result.new(success: true, start_date: start_date, end_date: end_date, granularity: granularity, total: total, top: top, first_level: first_level, second_level: second_level, third_level: third_level) end |