Overview

Namespaces

  • Apptus
    • ESales
      • Connector
        • Report
        • Time
    • Util
      • Cache
  • PHP
  • Overview
  • Namespace
  • Class
  • Tree
  1:   2:   3:   4:   5:   6:   7:   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:  35:  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:  96:  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: 189: 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: 
<?php
namespace Apptus\ESales\Connector\Report;

use Apptus\ESales\Connector\Time\TimeInterval;
use Apptus\ESales\Connector\Time\TimePoint;

/**
 * Report containing aggregated sales statistics. This report contains five {@see Timeline}'s:
 * <ul>
 *     <li>purchasedUnits - A timeline with the number of purchased units.</li>
 *     <li>abandonedUnits - A timeline with the number of abandoned units.</li>
 *     <li>revenue - A timeline with aggregated net selling price.</li>
 *     <li>margin - A timeline with the aggregated margin.</li>
 *     <li>discount - A timeline with the aggregated discount.</li>
 * </ul>

 * Each timeline has a number of slots with data. The number of slots depends on the specified time line type.
 */
class ProductSalesByTimeReport extends Report {

    private $discount;
    private $purchasedUnits;
    private $abandonedUnits;
    private $revenue;
    private $margin;
    private $timelineType;

    /**
     * Creates a {@see ProductSalesByTimeReport} from an XML document.
     *
     * @param string
     *            A string with an XML document to parse.
     * @param \DateTimeZone
     *            A time zone for the report.
     * @return ProductSalesByTimeReport
     *            A {@see ProductSalesByTimeReport}.
     * @throws FormatException if the XML document does not match the expected format.
     */
    public static function parse($data, \DateTimeZone $tz) {
        $root = new \SimpleXMLElement($data);

        self::checkElem($root, 'function');
        self::checkElem($root->function, 'report');

        $report = $root->function->report;
        self::checkElem($report, 'time_interval');
        self::checkElem($report, 'timeline_type');
        self::checkElem($report, 'timeline');

        $interval = TimeInterval::parse((string) $report->time_interval);
        $timelineType = TimelineType::parse((string) $report->timeline_type);

        $discount = new Timeline();
        $purchasedUnits = new Timeline();
        $abandonedUnits = new Timeline();
        $revenue = new Timeline();
        $margin = new Timeline();

        if ($report->timeline->slot !== null) {
            $start = null;
            $previous = null;
            foreach ($report->timeline->slot as $slot) {
                $start = $slot->attributes()['start'];
                if ($previous !== null) {
                    $previousEnd = $start === null ? -1 : TimePoint::parse((string) $start, $tz)->toMillis();
                    self::addToTimelines($discount, $purchasedUnits, $abandonedUnits, $revenue, $margin,
                            $previous, $previousEnd, $tz);
                }
                $previous = $slot;
            }
            $previousEnd = $start === null ? -1 : TimePoint::parse((string) $start, $tz)->toMillis();
            self::addToTimelines($discount, $purchasedUnits, $abandonedUnits, $revenue, $margin,
                    $previous, $previousEnd, $tz);
        }
        return new ProductSalesByTimeReport($root, $interval, $discount, $purchasedUnits, $abandonedUnits,
                $revenue, $margin, $timelineType, $tz);
    }

    private static function addToTimelines(Timeline $discount, Timeline $purchasedUnits,
                                       Timeline $abandonedUnits, Timeline $revenue,
                                       Timeline $margin, \SimpleXMLElement $slot, $end,
                                       \DateTimeZone $tz) {
        $attrs = $slot->attributes();
        $slotName = (string) $attrs['name'];

        $startAttr = $attrs['start'];
        $start = $startAttr === null ? -1 : TimePoint::parse((string) $startAttr, $tz)->toMillis();

        $discount[] = new TimeSlot($start, $end, $slotName, self::parseDouble($slot->discount));
        $purchasedUnits[] = new TimeSlot($start, $end, $slotName, self::parseLong($slot->purchased_units));
        $abandonedUnits[] = new TimeSlot($start, $end, $slotName, self::parseLong($slot->abandoned_units));
        $revenue[] = new TimeSlot($start, $end, $slotName, self::parseDouble($slot->revenue));
        $margin[] = new TimeSlot($start, $end, $slotName, self::parseDouble($slot->margin));
    }

    /**
     * Creates a {@see ProductSalesByTimeReport}. Note that null is illegal for all input parameters.
     *
     * @param \SimpleXMLElement
     *          A XmlTree to be used if the {@see exportToXml()} method is called.
     * @param TimeInterval
     *          The time range for the data in this report.
     * @param Timeline
     *          A {@see Timeline} of discount values.
     * @param Timeline
     *          A {@see Timeline} containing the number of purchased units.
     * @param Timeline
     *          A {@see Timeline} containing the number of abandoned units.
     * @param Timeline
     *          A {@see Timeline} of revenue values.
     * @param Timeline
     *          A {@see Timeline} of margin values.
     * @param TimelineType
     *          The timeline type used for this report.
     * @param \DateTimeZone
     *          The time zone for the data in this report.
     */
    public function __construct(\SimpleXMLElement $asXml, TimeInterval $interval, Timeline $discount,
                                    Timeline $purchasedUnits, Timeline $abandonedUnits, Timeline $revenue,
                                    Timeline $margin, TimelineType $timelineType, \DateTimeZone $tz) {
        parent::__construct($interval, $asXml, $tz);

        $this->discount = $discount;
        $this->purchasedUnits = $purchasedUnits;
        $this->abandonedUnits = $abandonedUnits;
        $this->revenue = $revenue;
        $this->margin = $margin;
        $this->timelineType = $timelineType;
    }

    /**
     * @return Timeline
     *            A {@see Timeline} of discount values.
     */
    public function discount() {
        return $this->discount;
    }

    /**
     * @return Timeline
     *            A {@see Timeline} of purchased units values.
     */
    public function purchasedUnits() {
        return $this->purchasedUnits;
    }

    /**
     * @return Timeline
     *            A {@see Timeline} of abandoned units values.
     */
    public function abandonedUnits() {
        return $this->abandonedUnits;
    }

    /**
     * @return Timeline
     *            A {@see Timeline} of revenue values.
     */
    public function revenue() {
        return $this->revenue;
    }

    /**
     * @return Timeline
     *            A {@see Timeline} of margin values.
     */
    public function margin() {
        return $this->margin;
    }

    /**
     * @return TimelineType
     *            The timeline type used in this report.
     */
    public function timelineType() {
        return $this->timelineType;
    }

    public function exportToExcel($file) {
        $w = new ExcelWriter($file);
        $w->cell('Time aggregated product sales')->nextRow();
        $w->cell('Timeline type')->cell($this->timelineType)->nextRow();

        $w->cell('Time interval')
                ->cell($this->isoDateTimeFormat($this->timeInterval()->start($this->tz)->toDateTime($this->tz)))
                ->cell($this->isoDateTimeFormat($this->timeInterval()->end($this->tz)->toDateTime($this->tz)))->nextRow();

        $w->nextRow();

        $w->cell('Time slot')
                ->cell('Purchased units')
                ->cell('Abandoned units')
                ->cell('Revenue')
                ->cell('Margin')
                ->cell('Discount')
                ->nextRow();

        $n = count($this->discount);
        for ($i = 0; $i < $n; ++$i) {
            $start = $this->discount[$i]->slotStartTime();

            $name = (string) $this->discount[$i];
            if ($name === '') {
                $name = $start < 0 ? (string) ($i + 1) : $this->isoDateTimeFormat(TimePoint::fromMillis($start)->toDateTime($this->tz));
            }

            $purchasedUnits = (string) $this->purchasedUnits[$i]->value();

            $abandonedUnits = (string) $this->abandonedUnits[$i]->value();

            $revenue = $this->revenue[$i]->value();
            $revenue = $revenue === null ? '' : sprintf('%.2f', $revenue);

            $margin = $this->margin[$i]->value();
            $margin = $margin === null ? '' : sprintf('%.2f', $margin);

            $discount = $this->discount[$i]->value();
            $discount = $discount === null ? '' : sprintf('%.2f', $discount);

            $w->cell($name)
                    ->cell($purchasedUnits)
                    ->cell($abandonedUnits)
                    ->cell($revenue)
                    ->cell($margin)
                    ->cell($discount)
                    ->nextRow();
        }

        $w->nextRow();

        $w->flush();
        $w->close();
    }
}
Apptus ESales Connector PHP API documentation generated by ApiGen