~katiekitty/+junk/solidstate

« back to all changes in this revision

Viewing changes to solidstate/branches/v0.4/DBO/.svn/text-base/PurchaseDBO.class.php.svn-base

  • Committer: root
  • Date: 2010-01-13 07:44:31 UTC
  • Revision ID: root@ds3-vamp.cs-monitor.cz.cc-20100113074431-kt8ceoeznpjg22x7
Reviving the project

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * PurchaseDBO.class.php
 
4
 *
 
5
 * This file contains the definition for the PurchaseDBO class.
 
6
 *
 
7
 * @package DBO
 
8
 * @author John Diamond <jdiamond@solid-state.org>
 
9
 * @copyright John Diamond <jdiamond@solid-state.org>
 
10
 * @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
 
11
 */
 
12
 
 
13
// Parent class
 
14
require_once BASE_PATH . "solidworks/DBO.class.php";
 
15
 
 
16
/**
 
17
 * OrderItemDBO
 
18
 *
 
19
 * Represent a Purchase.  This class is abstract, it is implemented by the
 
20
 * DomainServicePurchase, HostingServicePurchase, and ProductPurchase classes.
 
21
 *
 
22
 * @package DBO
 
23
 * @author John Diamond <jdiamond@solid-state.org>
 
24
 */
 
25
class PurchaseDBO extends DBO
 
26
{
 
27
  /**
 
28
   * @var string Purchase date (MySQL datetime)
 
29
   */
 
30
  var $date = null;
 
31
 
 
32
  /**
 
33
   * @var string Purchase term ("1 month", "3 month", ..., "10 year")
 
34
   */
 
35
  var $term = null;
 
36
 
 
37
  /**
 
38
   * Calculate Tax
 
39
   *
 
40
   * Given a Tax Rule, determine the amount of tax on this purchase
 
41
   *
 
42
   * @param TaxRuleDBO $taxruledbo Tax rule
 
43
   * @return float Amount of tax
 
44
   */
 
45
  function calculateTax( $taxruledbo )
 
46
  {
 
47
    return $this->getPrice() * ($taxruledbo->getRate() / 100.00);
 
48
  }
 
49
 
 
50
  /**
 
51
   * Get Purchase Date
 
52
   *
 
53
   * @return string Purchase date (MySQL datetime)
 
54
   */
 
55
  function getDate()
 
56
  {
 
57
    return $this->date;
 
58
  }
 
59
 
 
60
  /**
 
61
   * Get Price
 
62
   *
 
63
   * @return float Price of this purchase
 
64
   */
 
65
  function getPrice() { return 0; }
 
66
 
 
67
  /**
 
68
   * Get Setup Fee
 
69
   *
 
70
   * @return float Setup fee
 
71
   */
 
72
  function getSetupFee() { return 0; }
 
73
 
 
74
  /**
 
75
   * Get Taxes
 
76
   *
 
77
   * @return array An array of tax rules that apply to this purchase
 
78
   */
 
79
  function getTaxes() 
 
80
  { 
 
81
    global $DB;
 
82
 
 
83
    if( !$this->isTaxable() )
 
84
      {
 
85
        return array(); 
 
86
      }
 
87
 
 
88
    $filter = 
 
89
      "country=" . $DB->quote_smart( $this->accountdbo->getCountry() ) . " AND (" .
 
90
      "allstates=" . $DB->quote_smart( "YES" ) . " OR " .
 
91
      "state=" . $DB->quote_smart( $this->accountdbo->getState() ) . ")";
 
92
    $taxes = load_array_TaxRuleDBO( $filter );
 
93
 
 
94
    return $taxes == null ? array() : $taxes;
 
95
  }
 
96
 
 
97
  /**
 
98
   * Get Purchase Term
 
99
   *
 
100
   * @return string Purchase term (as string: "1 month", "3 month", etc.)
 
101
   */
 
102
  function getTerm() { return $this->term; }
 
103
 
 
104
  /**
 
105
   * Get Purchase Term (in months)
 
106
   *
 
107
   * Gives the length of the purchase terms in months.
 
108
   *
 
109
   * @return integer Length of the purchase term in months
 
110
   */
 
111
  function getTermMonths()
 
112
  {
 
113
    switch( $this->getTerm() )
 
114
      {
 
115
      case "1 month": return 1;
 
116
      case "3 month": return 3;
 
117
      case "6 month": return 6;
 
118
      case "12 month":
 
119
      case "1 year": return 12;
 
120
      case "2 year": return 24;
 
121
      case "3 year": return 36;
 
122
      case "4 year": return 48;
 
123
      case "5 year": return 60;
 
124
      case "6 year": return 72;
 
125
      case "7 year": return 84;
 
126
      case "8 year": return 96;
 
127
      case "9 year": return 108;
 
128
      case "10 year": return 120;
 
129
      default: return 0;
 
130
      }
 
131
  }
 
132
 
 
133
  /**
 
134
   * Get Product/Service Title
 
135
   *
 
136
   * @return string Product/Service title
 
137
   */
 
138
  function getTitle() { return null; }
 
139
 
 
140
  /**
 
141
   * Is Billable This Term
 
142
   *
 
143
   * Takes two timestamps: the beginning of an invoice term and the end of an
 
144
   * invoice term.  If this purchase is billable during the period, return true.
 
145
   *
 
146
   * @param integer $periodBeginTS The beginning of the billing period (timestamp)
 
147
   * @param integer $periodEndTS The end of the billing period (timestamp)
 
148
   * @return boolean True if this purchase should be billed
 
149
   */
 
150
  function isBillable( $periodBeginTS, $periodEndTS )
 
151
  {
 
152
    global $DB;
 
153
 
 
154
    $purchaseTS = $DB->datetime_to_unix( $this->getDate() );
 
155
 
 
156
    if( $periodEndTS < $purchaseTS )
 
157
      {
 
158
        // This invoice bills before this purchases existed
 
159
        return false;
 
160
      }
 
161
    if( $this->isNewThisTerm( $periodBeginTS, $periodEndTS ) )
 
162
      {
 
163
        // This purchase is new this billing term
 
164
        return true;
 
165
      }
 
166
 
 
167
    if( !$this->isRecurring() )
 
168
      {
 
169
        // Only recurring purchases are tested beyond this point
 
170
        return false;
 
171
      }
 
172
 
 
173
    // Calcuate the distance (in months) between the purchase date and the beginning
 
174
    // (and end) of the invoice period
 
175
    $yearDiff1 = date('y', $periodBeginTS) - date('y', $purchaseTS) ;
 
176
    $monthDiff1 = date('m', $periodBeginTS) - date('m', $purchaseTS) + ($yearDiff * 12);
 
177
    $monthDiff1 = $monthDiff1 < 0 ? $monthDiff1 + 12 : $monthDiff1;
 
178
 
 
179
    $yearDiff2 = date('y', $periodEndTS) - date('y', $purchaseTS) ;
 
180
    $monthDiff2 = date('m', $periodEndTS) - date('m', $purchaseTS) + ($yearDiff * 12);
 
181
    $monthDiff2 = $monthDiff2 < 0 ? $monthDiff2 + 12 : $monthDiff2;
 
182
 
 
183
    // These truths help determine if the purchases recurs during the invoice period
 
184
 
 
185
    $purchasedSameMonthButBeforePeriodBegins =
 
186
      ($monthDiff1 == 0) && (date( 'j', $purchaseTS ) < date( 'j', $periodBeginTS ));
 
187
 
 
188
    $recursAfterPeriodBegins = 
 
189
      (($monthDiff1 % $this->getTermMonths() == 0) &&
 
190
       (date( 'j', $purchaseTS ) >= date( 'j', $periodBeginTS ))) ||
 
191
      (($monthDiff1 % $this->getTermMonths() == 0 ||
 
192
        $monthDiff2 % $this->getTermMonths() == 0 ||
 
193
        $monthDiff1 % $this->getTermMonths() == $monthDiff1) &&
 
194
       (date( 'j', $purchaseTS ) < date( 'j', $periodBeginTS )));
 
195
 
 
196
    $recursBeforePeriodEnds =
 
197
      (($monthDiff2 % $this->getTermMonths() == 0) && 
 
198
       (date( 'j', $purchaseTS ) < date( 'j', $periodEndTS ))) ||
 
199
      (($monthDiff2 % $this->getTermMonths() == 0 ||
 
200
        $monthDiff2 % $this->getTermMonths() == 1) &&
 
201
       (date( 'j', $purchaseTS ) >= date( 'j', $periodEndTS )));
 
202
 
 
203
    // Usefull for debugging
 
204
    /*
 
205
    echo $this->getTitle() . ": " .
 
206
      !$purchasedSameMonthButBeforePeriodBegins . "/" .
 
207
      $recursAfterPeriodBegins . "/" .
 
208
      $recursBeforePeriodEnds . " monthDiff2: " . $monthDiff2 . "\n";
 
209
    */
 
210
 
 
211
    // Test if this purchase recurs during the invoice period using the truths
 
212
    // calculated above
 
213
    return (!$purchasedSameMonthButBeforePeriodBegins) && 
 
214
      ($recursAfterPeriodBegins && $recursBeforePeriodEnds);
 
215
  }
 
216
 
 
217
  /**
 
218
   * Is New Purchase This Term
 
219
   *
 
220
   * Takes two timestamps: the beginning of an invoice term and then end of an
 
221
   * invoice term.  If this purchase was made during the period, return true.
 
222
   *
 
223
   * @param integer $periodBeginTS The beginning of the billing period (timestamp)
 
224
   * @param integer $periodEndTS The end of the billing period (timestamp)
 
225
   * @return boolean True if this purchase should be billed
 
226
   */
 
227
  function isNewThisTerm( $periodBeginTS, $periodEndTS )
 
228
  {
 
229
    global $DB;
 
230
 
 
231
    $purchaseTS = $DB->datetime_to_unix( $this->getDate() );
 
232
    return ($purchaseTS >= $periodBeginTS) && ($purchaseTS < $periodEndTS);
 
233
  }
 
234
 
 
235
  /**
 
236
   * Is Recurring
 
237
   *
 
238
   * @return boolean True if this purchase is a recurring purchase
 
239
   */
 
240
  function isRecurring() { return $this->getTermMonths() > 0; }
 
241
 
 
242
  /**
 
243
   * Is Taxable
 
244
   *
 
245
   * @return boolean True if this purchase is taxable
 
246
   */
 
247
  function isTaxable() { return false; }
 
248
 
 
249
  /**
 
250
   * Set Purchase Date
 
251
   *
 
252
   * @param string $date Purchase date (MySQL datetime)
 
253
   */
 
254
  function setDate( $date ) { $this->date = $date; }
 
255
 
 
256
  /**
 
257
   * Set Purchase Term
 
258
   *
 
259
   * @param string $term Purchase term ("1 year", "2 year" ... "10 year")
 
260
   */
 
261
  function setTerm( $term ) { $this->term = $term; }
 
262
}
 
263
 
 
264
?>