user_mcards.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. declare(strict_types=1);
  3. namespace mcard;
  4. use \Exception;
  5. class user_mcards
  6. {
  7. private $mCardModel;
  8. private $mUserId;
  9. private $mCards = [];
  10. public function __construct($userid)
  11. {
  12. $this->mUserId = $userid;
  13. $this->mCardModel = Model('member_card');
  14. $this->load_cards();
  15. }
  16. public function hasCards() {
  17. foreach ($this->mCards as $key => $cards) {
  18. if(!empty($cards)) {
  19. return true;
  20. }
  21. }
  22. return false;
  23. }
  24. public function add_amount($amound)
  25. {
  26. $mod_card = Model('member_card');
  27. $left_amount = $amound;
  28. foreach ($this->mCards as $key => $cards)
  29. {
  30. if($key != 'period')
  31. {
  32. foreach ($cards as $card)
  33. {
  34. if($left_amount <= 0) {
  35. break;
  36. }
  37. $card_id = $card->card_id();
  38. $total = $card->total_amount();
  39. $left = $card->left_amount();
  40. if($total > $left + $left_amount) {
  41. $mod_card->add_amound($card_id,$left_amount);
  42. $left_amount = 0;
  43. }
  44. else {
  45. $amound = $total - $left;
  46. $mod_card->add_amound($card_id,$amound);
  47. $left_amount = $left_amount - $amound;
  48. }
  49. }
  50. }
  51. }
  52. }
  53. public function left_amount()
  54. {
  55. $total = 0.00;
  56. foreach ($this->mCards as $key => $cards) {
  57. if($key != 'period') {
  58. foreach ($cards as $card) {
  59. $total += $card->left_amount();
  60. }
  61. }
  62. }
  63. return round($total,2);
  64. }
  65. public function addCard($params)
  66. {
  67. $type = intval($params['card_type']);
  68. $discount = round(doubleval($params['discount']), 2);
  69. $data = ['member_id' => $this->mUserId, 'card_type' => $type,
  70. 'discount' => $discount,
  71. 'state' => STATE_USING, 'add_time' => time()];
  72. if ($type === AmountType) {
  73. $amount = round(doubleval($params['total_amount']), 2);
  74. $data['total_amount'] = $amount;
  75. $data['left_amount'] = $amount;
  76. } elseif ($type === PeriodType) {
  77. $data['package_type'] = $this->package_type($params['package_type']);
  78. } elseif ($type === BothType) {
  79. $amount = round(doubleval($params['total_amount']), 2);
  80. $data['total_amount'] = $amount;
  81. $data['left_amount'] = $amount;
  82. $data['package_type'] = $this->package_type($params['package_type']);
  83. } else {
  84. return false;
  85. }
  86. if (isset($data['package_type'])) {
  87. [$start, $end] = $this->calc_time($data['package_type']);
  88. $data['start_time'] = $start;
  89. $data['end_time'] = $end;
  90. }
  91. $ret = $this->mCardModel->addCard($data);
  92. if ($ret > 0) {
  93. $this->load_cards();
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. }
  99. private function calc_time($type)
  100. {
  101. $start = time();
  102. if ($type == MonthPachage) {
  103. $end = strtotime('+1 months 1 day', $start);
  104. } elseif ($type == QuarterPackage) {
  105. $end = strtotime('+3 months 1 day', $start);
  106. } else {
  107. $end = strtotime('+1 year 1 day', $start);
  108. }
  109. $end = strtotime(date('Y-m-d', $end));
  110. $start = strtotime(date('Y-m-d', $start));
  111. return [$start, $end];
  112. }
  113. private function package_type($package)
  114. {
  115. $package = strtolower($package);
  116. if ($package == 'month') {
  117. return MonthPachage;
  118. } elseif ($package == 'quarter') {
  119. return QuarterPackage;
  120. } elseif ($package == 'year') {
  121. return YearPackage;
  122. } else {
  123. throw new Exception("会员卡时间类型错误.");
  124. }
  125. }
  126. private function load_cards()
  127. {
  128. $this->mCards['amount'] = [];
  129. $this->mCards['period'] = [];
  130. $this->mCards['aperiod'] = [];
  131. $cards = $this->mCardModel->getUsableCards($this->mUserId);
  132. foreach ($cards as $item)
  133. {
  134. $card_type = intval($item['card_type']);
  135. if ($card_type == AmountType) {
  136. $card = new amount_card($item);
  137. $name = 'amount';
  138. } elseif ($card_type == PeriodType) {
  139. $card = new period_card($item);
  140. $name = 'period';
  141. } else {
  142. $card = new amount_period_card($item);
  143. $name = 'aperiod';
  144. }
  145. if ($card->usable()) {
  146. $this->mCards[$name][] = $card;
  147. }
  148. }
  149. }
  150. public function enough($amount)
  151. {
  152. if (!empty($this->mCards['period'])) return true;
  153. $total = 0.0;
  154. $total += $this->account($this->mCards['aperiod']);
  155. $total += $this->account($this->mCards['amount']);
  156. return intval($total * 100) >= intval($amount * 100);
  157. }
  158. public function calc_price($goods_id,$price)
  159. {
  160. if(canUseVip($goods_id)) {
  161. return $this->_calc_amount($price);
  162. } else {
  163. return $price;
  164. }
  165. }
  166. public function calc_amount($goods_id,$amount)
  167. {
  168. if(canUseVip($goods_id)) {
  169. return $this->_calc_amount($amount);
  170. } else {
  171. return $amount;
  172. }
  173. }
  174. private function _calc_amount($amount)
  175. {
  176. if($this->enough($amount))
  177. {
  178. $amount = intval($amount * 100);
  179. $total_price = 0.00;
  180. $calc_price = function (card $card, int $cent) use(&$total_price)
  181. {
  182. [$consumer,$useup,$price] = $card->calc($cent);
  183. $total_price += $price;
  184. return $consumer;
  185. };
  186. foreach ($this->mCards['aperiod'] as $card) {
  187. if ($amount <= 0) {
  188. break;
  189. } else {
  190. $amount -= $calc_price($card, $amount);
  191. }
  192. }
  193. foreach ($this->mCards['amount'] as $card) {
  194. if ($amount <= 0) {
  195. break;
  196. } else {
  197. $amount -= $calc_price($card, $amount);
  198. }
  199. }
  200. return $total_price;
  201. }
  202. else {
  203. return $amount;
  204. }
  205. }
  206. private function account($cards)
  207. {
  208. $total = 0.0;
  209. foreach ($cards as $card) {
  210. $total += $card->left_amount();
  211. }
  212. return $total;
  213. }
  214. public function deduct($amount)
  215. {
  216. if (!empty($this->mCards['period'])) return true;
  217. $amount = intval($amount * 100);
  218. $self = $this;
  219. $deduct_money = function (card $card, int $cent) use($self)
  220. {
  221. [$consumer,$useup] = $card->deduct($cent);
  222. $self->mCardModel->detuct($card->card_id(),$consumer,$useup);
  223. //增加消费额度减少记录
  224. return $consumer;
  225. };
  226. foreach ($this->mCards['aperiod'] as $card) {
  227. if ($amount <= 0) {
  228. break;
  229. } else {
  230. $amount -= $deduct_money($card, $amount);
  231. }
  232. }
  233. foreach ($this->mCards['amount'] as $card) {
  234. if ($amount <= 0) {
  235. break;
  236. } else {
  237. $amount -= $deduct_money($card, $amount);
  238. }
  239. }
  240. $this->load_cards();
  241. return ($amount <= 0);
  242. }
  243. }