public function execute() { try { $discountcard = waRequest::post('discountcard', array()); $model = new shopDiscountcardsPluginModel(); if (!empty($discountcard['id'])) { $model->updateById($discountcard['id'], $discountcard); $discountcard = $model->getById($discountcard['id']); } elseif (empty($discountcard['discountcard'])) { throw new waException('Ошибка: Не указан номер дисконтной карты'); } else { if ($model->getByField('discountcard', $discountcard['discountcard'])) { throw new waException('Ошибка: Номер дисконтной карты не уникален'); } $id = $model->insert($discountcard); $discountcard = $model->getById($id); } if (!empty($discountcard['contact_id'])) { $contact = new waContact($discountcard['contact_id']); $discountcard['contact_name'] = $contact->get('name'); } $discountcard['amount'] = shop_currency($discountcard['amount']); $this->response = $discountcard; } catch (Exception $ex) { $this->setError($ex->getMessage()); } }
protected function additionalSum($order_id, $refund = false) { $discountcard_order_model = new shopDiscountcardsPluginOrderModel(); if ($discountcard_order = $discountcard_order_model->getByField('order_id', $order_id)) { $discountcard_model = new shopDiscountcardsPluginModel(); if ($discountcard = $discountcard_model->getByField('discountcard', $discountcard_order['discountcard'])) { $order_model = new shopOrderModel(); $order = $order_model->getOrder($order_id); $total = $order['total']; if ($this->getSettings('without_delivery') && !empty($order['shipping'])) { $total -= $order['shipping']; } $def_currency = wa('shop')->getConfig()->getCurrency(true); $total = shop_currency($total, $order['currency'], $def_currency, false); if ($refund) { $amount = $discountcard['amount'] - $total; } else { $amount = $discountcard['amount'] + $total; } $data = array('amount' => $amount); if ($this->getSettings('recalculation')) { $discount = $this->defineDiscount($amount); $data['discount'] = $discount; } $discountcard_model->updateById($discountcard['id'], $data); } } }