#' Functions generating contribution income statements and financial income statements for several products over one period assuming no inventory.
#' @param basevol Numeric. Volume of reference (for all products)
#' @param profitable Logical vector. For each reference to be simulated, whether the segment is profitable or not.
#' @return dataframe with all accounts.
#' \item{company}{Name of the company}
#' \item{product}{Generic name of the product, complete form}
#' \item{singular}{Generic name of the product, singular form}
#' \item{plural}{Generic name of the product, plural form}
#' \item{reference}{Name of the specific reference}
#' \item{p}{Price}
#' \item{cmr}{Contribution Margin Ratio}
#' \item{uvc}{Unit Variable Cost}
#' \item{ucm}{Unit Contribution Margin}
#' \item{vol}{Volume}
#' \item{fc}{Fixed costs}
#' \item{rev}{Revenues}
#' \item{umat}{Unit material costs of production}
#' \item{ulab}{Unit labor costs of production}
#' \item{uper}{Unit period cost}
#' \item{fprod}{Fixed production costs}
#' \item{fprod_batch}{Fixed production costs - batch level}
#' \item{fprod_segm}{Fixed production costs - segment level}
#' \item{fprod_comp}{Fixed production costs - company level}
#' \item{fper}{Fixed period costs}
#' \item{fper_batch}{Fixed period costs - batch level}
#' \item{fper_segm}{Fixed period costs - segment level}
#' \item{fper_comp}{Fixed period costs - company level}
#' \item{vc}{Variable Costs - total}
#' \item{cm}{Contribution Margin - total}
#' \item{oi}{Operating Income}
#' \item{vprod}{Variable costs of production - total}
#' \item{prod}{Production costs - total}
#' \item{gm}{Gross Margin}
#' \item{vper}{Variable period costs - total}
#' \item{per}{Period costs - total}
#' @export
income_statements <- function(basevol = 10000, profitable = c(TRUE,FALSE,TRUE)){
stopifnot(
is.numeric(basevol),
is.logical(profitable),
length(profitable) >= 1,
length(profitable) <= 3
)
refnbr = length(profitable)
company <- sample(manacc::products$company,1)
references <- manacc::products[manacc::products$company == company,]
references <- references[sample(c(1:nrow(references)),refnbr),]
references <- references[order(references$p),]
if (refnbr == 1) mix <- c(1) else {
mix <- c(sample(seq(from = 0.20, to = 1-(0.1*refnbr), by = 0.05),1))
if (refnbr > 2){
for (i in 2:(refnbr-1)){
mix[i] <- sample(seq(from = 0.05, to = 1-(0.1*(refnbr-i))-sum(mix), by = 0.05),1)
}
mix[refnbr] <- 1-sum(mix)
} else {
mix[2] <- 1-sum(mix)
}
}
for (i in 1:nrow(references)) references[i,"p"] <- manacc::wiggle(references[i,"p"])
for (i in 1:nrow(references)) references[i,"cmr"] <- round(manacc::wiggle(references[i,"cmr"], 0.3),1)
references$uvc <- references$p * (1-references$cmr)
references$ucm <- references$p * references$cmr
references$vol <- round(manacc::wiggle(basevol,0.3)/100,0)*100*mix
fc <- c()
for (i in 1:nrow(references)){
if (profitable[i])
fc[i] <- round(references$vol[i] * references$ucm[i] * sample(seq(from = 0.7, to = 0.9, by = 0.01), 1),0) else
fc[i] <- round(references$vol[i] * references$ucm[i] * sample(seq(from = 1.1, to = 1.3, by = 0.01), 1),0)
}
references$fc <- fc
rm(fc, mix)
references$rev <- NA
references$umat <- NA
references$ulab <- NA
references$uper <- NA
references$fprod <- NA
references$fprod_batch <- NA
references$fprod_segm <- NA
references$fprod_comp <- NA
references$fper <- NA
references$fper_batch <- NA
references$fper_segm <- NA
references$fper_comp <- NA
references$vc <- NA
references$cm <- NA
references$oi <- NA
references$vprod <- NA
references$prod <- NA
references$gm <- NA
references$vper <- NA
references$per <- NA
for (i in 1:nrow(references)){
vol <- references[i,"vol"]
p <- references[i,"p"]
uvc <- references[i,"uvc"]
fc <- references[i,"fc"]
references[i,"rev"] <- p * vol
references[i,"umat"] <- round(uvc * sample(seq(from = 0.2, to = 0.6, by = 0.05),1),1)
references[i,"ulab"] <- round((uvc - references[i,"umat"]) * sample(seq(from = 0.4, to = 0.8, by = 0.05),1),1)
references[i,"uper"] <- uvc - references[i,"umat"] - references[i,"ulab"]
references[i,"fprod"] <- round(fc * sample(seq(from = 0.4, to = 0.7, by = 0.05),1),1)
references[i,"fprod_batch"] <- round(references[i,"fprod"] * sample(seq(from = 0.2, to = 0.6, by = 0.05),1)/100,0)*100
references[i,"fprod_segm"] <- round((references[i,"fprod"] - references[i,"fprod_batch"]) * sample(seq(from = 0.2, to = 0.6, by = 0.05),1),1)
references[i,"fprod_comp"] <- references[i,"fprod"] - references[i,"fprod_batch"] - references[i,"fprod_segm"]
references[i,"fper"] <- fc - references[i,"fprod"]
references[i,"fper_batch"] <- round(references[i,"fper"] * sample(seq(from = 0.2, to = 0.6, by = 0.05),1)/100,0)*100
references[i,"fper_segm"] <- round((references[i,"fper"] - references[i,"fper_batch"]) * sample(seq(from = 0.2, to = 0.6, by = 0.05),1),1)
references[i,"fper_comp"] <- references[i,"fper"] - references[i,"fper_batch"] - references[i,"fper_segm"]
references[i,"vc"] <- vol * uvc
references[i,"cm"] <- references[i,"rev"] - references[i,"vc"]
references[i,"oi"] <- references[i,"cm"] - fc
references[i,"vprod"] <- vol * (references[i,"umat"] + references[i,"ulab"])
references[i,"prod"] <- references[i,"vprod"] + references[i,"fprod"]
references[i,"gm"] <- references[i,"rev"] - references[i,"prod"]
references[i,"vper"] <- vol * references[i,"uper"]
references[i,"per"] <- references[i,"vper"] + references[i,"fper"]
}
return(references)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.