Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Bill
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 8
380
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getBillingArray
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 getMonthlyBillAmount
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
42
 checkNextBillingDate
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getDiscount
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getActualNextBillingDate
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 calculateBillAmount
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 checkStoreBuysActive
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace BuyerKiosk\Billing;
4
5
6
7class Bill extends \Store {
8    private $log;
9    private $storeDB;
10
11    public function __construct($id)
12    {
13        parent::__construct($id);
14        $this->log = new \KLogger($_ENV['LOG_DIR']."billing.log", \KLogger::DEBUG);
15        $this->storeDB = dbConnectByName($this->getDbName());
16    }
17    /*  billingType = 0 | Standard Monthly Billing
18     *  billingType = 1 | Yearly Billing. Check nextBillingDate
19     *  billingType = 2 | Open To Buy Only. Check nextBillingDate
20     */
21
22
23    public function getBillingArray($date) {
24        $billingArray = array();
25        $active = $this->checkStoreBuysActive($date);
26        if($active) {
27            $billingArray['active'] = 1;
28            $billingArray['billingAmount'] = $this->getMonthlyBillAmount($date);
29            $billingArray['billingDiscount'] = $this->getDiscount($date);
30            $billingArray['nextBillDate'] = $this->getActualNextBillingDate($date);
31        }   else {
32            $billingArray['active'] = 0;
33            $billingArray['billingAmount'] = 0;
34            $billingArray['billingDiscount'] = 0;
35            $billingArray['nextBillDate'] = null;
36        }
37        return $billingArray;
38    }
39
40    private function getMonthlyBillAmount($date) {
41        $thisDate = new \DateTime($date, new \DateTimeZone($this->getTimeZone()));
42        $thisDate->setTimezone(new \DateTimeZone('UTC'));
43        if($this->billingType == 0) {
44            return $this->calculateBillAmount($date);
45        } else if($this->billingType == 1) {
46            if($this->checkNextBillingDate($date)) {
47                return $this->calculateBillAmount($date);
48            }
49        } else if($this->billingType == 2) {
50            if($this->checkStoreBuysActive($date)) {
51                return $this->calculateBillAmount($date);
52            }
53        }
54        return 0;
55    }
56    private function checkNextBillingDate($date) {
57        $nextBillingDate = new \DateTime($this->nextBillingDate, new \DateTimeZone($this->getTimeZone()));
58        $nextBillingDate->setTimezone(new \DateTimeZone('UTC'));
59
60        $thisBillingDate = new \DateTime($date, new \DateTimeZone($this->getTimeZone()));
61        $thisBillingDate->setTimezone(new \DateTimeZone('UTC'));
62
63        if($thisBillingDate > $nextBillingDate) {
64            return true;
65        }
66        return false;
67    }
68
69    private function getDiscount($date) {
70        $billingDiscountDate = new \DateTime($this->billingDiscountDate, new \DateTimeZone($this->getTimeZone()));
71        $billingDiscountDate->setTimezone(new \DateTimeZone('UTC'));
72
73        $thisDate = new \DateTime($date, new \DateTimeZone($this->getTimeZone()));
74        $thisDate->setTimezone(new \DateTimeZone('UTC'));
75        if($thisDate > $billingDiscountDate) {
76            return 0;
77        } else {
78            return $this->getBillingDiscount();
79        }
80    }
81    private function getActualNextBillingDate($date) {
82        if($this->nextBillingDate == null) {
83            //Next Billing date is the 1st of the next month from $date
84            $nextBillingDate = new \DateTime($date, new \DateTimeZone($this->getTimeZone()));
85            $nextBillingDate->modify('first day of next month');
86            $nextBillingDate->setTimezone(new \DateTimeZone('UTC'));
87            return $nextBillingDate->format("Y-m-d");
88        } else {
89            //if nextBillingDate is before $date then just use the 1st of next month from $date
90            $nextBillingDate = new \DateTime($this->nextBillingDate, new \DateTimeZone($this->getTimeZone()));
91            $nextBillingDate->setTimezone(new \DateTimeZone('UTC'));
92            $thisDate = new \DateTime($date, new \DateTimeZone($this->getTimeZone()));
93            $thisDate->setTimezone(new \DateTimeZone('UTC'));
94            if($thisDate > $nextBillingDate) {
95                $nextBillingDate = new \DateTime($date, new \DateTimeZone($this->getTimeZone()));
96                $nextBillingDate->modify('first day of next month');
97                $nextBillingDate->setTimezone(new \DateTimeZone('UTC'));
98                return $nextBillingDate->format("Y-m-d");
99            } else {
100                return $nextBillingDate->format("Y-m-d");
101            }
102        }
103    }
104
105    private function calculateBillAmount($date) {
106        return (double)$this->billingRate - (double)$this->getDiscount($date);
107    }
108
109    private function checkStoreBuysActive($date) {
110        //get the first day of the previous month from the $date
111        $firstDay = new \DateTime($date, new \DateTimeZone($this->getTimeZone()));
112        $firstDay->modify('first day of last month');
113        $firstDay->setTimezone(new \DateTimeZone('UTC'));
114        $this->log->LogDebug("First Day: ".$firstDay->format("Y-m-d"));
115
116        $stmt = $this->storeDB->prepare("SELECT COUNT(*) FROM buyQueue WHERE timeEntered >= :firstDay");
117        $stmt->bindValue(":firstDay", $firstDay->format("Y-m-d"));
118        $stmt->execute();
119
120        $result = $stmt->fetchColumn();
121        if($result > 5) {
122            $this->log->LogDebug("Store Buys Active: ".$result);
123            return true;
124        }
125        $this->log->LogDebug("Store Buys Inactive: ".$result);
126        return false;
127    }
128
129}