123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <?php
- declare(strict_types=1);
- namespace mcard;
- use \Exception;
- class user_mcards
- {
- private $mCardModel;
- private $mUserId;
- private $mCards = [];
- public function __construct($userid)
- {
- $this->mUserId = $userid;
- $this->mCardModel = Model('member_card');
- $this->load_cards();
- }
- public function hasCards() {
- foreach ($this->mCards as $key => $cards) {
- if(!empty($cards)) {
- return true;
- }
- }
- return false;
- }
- public function add_amount($amound)
- {
- $mod_card = Model('member_card');
- $left_amount = $amound;
- foreach ($this->mCards as $key => $cards)
- {
- if($key != 'period')
- {
- foreach ($cards as $card)
- {
- if($left_amount <= 0) {
- break;
- }
- $card_id = $card->card_id();
- $total = $card->total_amount();
- $left = $card->left_amount();
- if($total > $left + $left_amount) {
- $mod_card->add_amound($card_id,$left_amount);
- $left_amount = 0;
- }
- else {
- $amound = $total - $left;
- $mod_card->add_amound($card_id,$amound);
- $left_amount = $left_amount - $amound;
- }
- }
- }
- }
- }
- public function left_amount()
- {
- $total = 0.00;
- foreach ($this->mCards as $key => $cards) {
- if($key != 'period') {
- foreach ($cards as $card) {
- $total += $card->left_amount();
- }
- }
- }
- return round($total,2);
- }
- public function addCard($params)
- {
- $type = intval($params['card_type']);
- $discount = round(doubleval($params['discount']), 2);
- $data = ['member_id' => $this->mUserId, 'card_type' => $type,
- 'discount' => $discount,
- 'state' => STATE_USING, 'add_time' => time()];
- if ($type === AmountType) {
- $amount = round(doubleval($params['total_amount']), 2);
- $data['total_amount'] = $amount;
- $data['left_amount'] = $amount;
- } elseif ($type === PeriodType) {
- $data['package_type'] = $this->package_type($params['package_type']);
- } elseif ($type === BothType) {
- $amount = round(doubleval($params['total_amount']), 2);
- $data['total_amount'] = $amount;
- $data['left_amount'] = $amount;
- $data['package_type'] = $this->package_type($params['package_type']);
- } else {
- return false;
- }
- if (isset($data['package_type'])) {
- [$start, $end] = $this->calc_time($data['package_type']);
- $data['start_time'] = $start;
- $data['end_time'] = $end;
- }
- $ret = $this->mCardModel->addCard($data);
- if ($ret > 0) {
- $this->load_cards();
- return true;
- } else {
- return false;
- }
- }
- private function calc_time($type)
- {
- $start = time();
- if ($type == MonthPachage) {
- $end = strtotime('+1 months 1 day', $start);
- } elseif ($type == QuarterPackage) {
- $end = strtotime('+3 months 1 day', $start);
- } else {
- $end = strtotime('+1 year 1 day', $start);
- }
- $end = strtotime(date('Y-m-d', $end));
- $start = strtotime(date('Y-m-d', $start));
- return [$start, $end];
- }
- private function package_type($package)
- {
- $package = strtolower($package);
- if ($package == 'month') {
- return MonthPachage;
- } elseif ($package == 'quarter') {
- return QuarterPackage;
- } elseif ($package == 'year') {
- return YearPackage;
- } else {
- throw new Exception("会员卡时间类型错误.");
- }
- }
- private function load_cards()
- {
- $this->mCards['amount'] = [];
- $this->mCards['period'] = [];
- $this->mCards['aperiod'] = [];
- $cards = $this->mCardModel->getUsableCards($this->mUserId);
- foreach ($cards as $item)
- {
- $card_type = intval($item['card_type']);
- if ($card_type == AmountType) {
- $card = new amount_card($item);
- $name = 'amount';
- } elseif ($card_type == PeriodType) {
- $card = new period_card($item);
- $name = 'period';
- } else {
- $card = new amount_period_card($item);
- $name = 'aperiod';
- }
- if ($card->usable()) {
- $this->mCards[$name][] = $card;
- }
- }
- }
- public function enough($amount)
- {
- if (!empty($this->mCards['period'])) return true;
- $total = 0.0;
- $total += $this->account($this->mCards['aperiod']);
- $total += $this->account($this->mCards['amount']);
- return intval($total * 100) >= intval($amount * 100);
- }
- public function calc_price($goods_id,$price)
- {
- if(canUseVip($goods_id)) {
- return $this->_calc_amount($price);
- } else {
- return $price;
- }
- }
- public function calc_amount($goods_id,$amount)
- {
- if(canUseVip($goods_id)) {
- return $this->_calc_amount($amount);
- } else {
- return $amount;
- }
- }
- private function _calc_amount($amount)
- {
- if($this->enough($amount))
- {
- $amount = intval($amount * 100);
- $total_price = 0.00;
- $calc_price = function (card $card, int $cent) use(&$total_price)
- {
- [$consumer,$useup,$price] = $card->calc($cent);
- $total_price += $price;
- return $consumer;
- };
- foreach ($this->mCards['aperiod'] as $card) {
- if ($amount <= 0) {
- break;
- } else {
- $amount -= $calc_price($card, $amount);
- }
- }
- foreach ($this->mCards['amount'] as $card) {
- if ($amount <= 0) {
- break;
- } else {
- $amount -= $calc_price($card, $amount);
- }
- }
- return $total_price;
- }
- else {
- return $amount;
- }
- }
- private function account($cards)
- {
- $total = 0.0;
- foreach ($cards as $card) {
- $total += $card->left_amount();
- }
- return $total;
- }
- public function deduct($amount)
- {
- if (!empty($this->mCards['period'])) return true;
- $amount = intval($amount * 100);
- $self = $this;
- $deduct_money = function (card $card, int $cent) use($self)
- {
- [$consumer,$useup] = $card->deduct($cent);
- $self->mCardModel->detuct($card->card_id(),$consumer,$useup);
- //增加消费额度减少记录
- return $consumer;
- };
- foreach ($this->mCards['aperiod'] as $card) {
- if ($amount <= 0) {
- break;
- } else {
- $amount -= $deduct_money($card, $amount);
- }
- }
- foreach ($this->mCards['amount'] as $card) {
- if ($amount <= 0) {
- break;
- } else {
- $amount -= $deduct_money($card, $amount);
- }
- }
- $this->load_cards();
- return ($amount <= 0);
- }
- }
|