Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Points
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 5
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 calculatePoints
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
30
 insert
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
56
 getPoints
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace BuyerKiosk\FiveStars;
4
5class Points {
6
7    private $id;
8    private $storeDB;
9    private $store;
10    private $log;
11
12    public $date;
13    public $phone;
14    public $totalSales;
15    public $totalBuys;
16    public $totalTax;
17    private $points;
18    public $pointsRedeemed;
19    private $slug;
20
21
22    public function __construct(\Store $store, $storeDB = null)
23    {
24        $this->store = $store;
25        if($storeDB == null) {
26            $this->storeDB = dbConnectByName($this->store->getDbName());
27        } else {
28            $this->storeDB = $storeDB;
29        }
30        $this->log = new \KLogger($_ENV['LOG_DIR']."fiveStars.log", \KLogger::DEBUG);
31    }
32
33    public function calculatePoints() {
34        $dollarsToPointsRatio = $this->store->getFiveStarsPointsRatio();
35        $points = 0;
36        if((int)$this->store->getFiveStarsBuysIncluded() == 1) {
37            $points += $this->totalBuys;
38        }
39        if((int)$this->store->getFiveStarsTaxIncluded() == 1) {
40            $points += $this->totalTax;
41        }
42        $points += $this->totalSales;
43        if($points > 0) {
44            $points = $points/$dollarsToPointsRatio;
45            $this->points = floor($points);
46        } else if($points < 0) {
47            $points = $points/$dollarsToPointsRatio;
48            $this->points = ceil($points);
49        } else {
50            $this->points = 0;
51        }
52    }
53
54    public function insert() {
55        if(isset($this->pointsRedeemed) && $this->pointsRedeemed > 0 && ($this->points == null || !isset($this->points))) {
56            $this->points = 0;
57        }
58        try {
59            $stmt = $this->storeDB->prepare("INSERT INTO fiveStarsPoints (date, phone, totalSales, totalBuys, totalTax, points, pointsRedeemed) VALUES (:date, :phone, :totalSales, :totalBuys, :totalTax, :points, :pointsRedeemed)");
60            $stmt->bindValue(":date", $this->date);
61            $stmt->bindValue(":phone", $this->phone);
62            $stmt->bindValue(":totalSales", $this->totalSales);
63            $stmt->bindValue(":totalBuys", $this->totalBuys);
64            $stmt->bindValue(":totalTax", $this->totalTax);
65            $stmt->bindValue(":points", $this->points);
66            $stmt->bindValue(":pointsRedeemed", $this->pointsRedeemed);
67            if($stmt->execute()) {
68                $this->log->LogDebug("Final Insert into FSP: ".$this->phone);
69                return true;
70            }
71            $this->log->LogDebug("Slug Collision on Phone: ".$this->phone);
72            return false;
73        } catch (\PDOException $e) {
74            $this->log->LogDebug("Caught Error on FSP: ".$this->phone);
75            $this->log->LogError($e->getMessage());
76            return false;
77        }
78    }
79    public function getPoints() {
80        return $this->points;
81    }
82
83    public function getStore() {
84        return $this->store;
85    }
86}