public function execute()
 {
     $lazy = waRequest::get('lazy', 0, waRequest::TYPE_INT);
     $offset = waRequest::get('offset', 0, waRequest::TYPE_INT);
     $limit = 30;
     $model = new shopDiscountcardsPluginOrderModel();
     $sql = "SELECT count(*) " . $this->getSql();
     $total_count = (int) $model->query($sql)->fetchField();
     $sql = "SELECT * " . $this->getSql() . " LIMIT {$offset}, {$limit}";
     $discountcards_orders = $model->query($sql)->fetchAll();
     $discountcards_model = new shopDiscountcardsPluginModel();
     $order_model = new shopOrderModel();
     $workflow = new shopWorkflow();
     foreach ($discountcards_orders as &$discountcards_order) {
         $order = $order_model->getOrder($discountcards_order['order_id']);
         $discountcards_order['order'] = $order;
         $discountcards_order['order_id_encode'] = shopHelper::encodeOrderId($order['id']);
         if ($discountcard = $discountcards_model->getByField('discountcard', $discountcards_order['discountcard'])) {
             $discountcards_order['_discountcard'] = $discountcard;
         }
         $discountcards_order['state'] = $workflow->getStateById($order['state_id']);
     }
     unset($discountcards_order);
     $this->view->assign(array('discountcards_orders' => $discountcards_orders, 'offset' => $offset, 'limit' => $limit, 'count' => count($discountcards_orders), 'total_count' => $total_count, 'lazy' => $lazy));
 }
 public function execute()
 {
     try {
         $app_settings_model = new waAppSettingsModel();
         if (waRequest::post('cancel')) {
             wa()->getStorage()->set('shop/discountcard', '');
         } else {
             if ($discountcard_number = waRequest::post('discountcard')) {
                 $model = new shopDiscountcardsPluginModel();
                 if ($app_settings_model->get(shopDiscountcardsPlugin::$plugin_id, 'binding_customer')) {
                     $contact_id = wa()->getUser()->getId();
                     $discountcard = $model->getByField(array('contact_id' => $contact_id, 'discountcard' => $discountcard_number));
                     if (empty($discountcard)) {
                         $discountcard = $model->getByField(array('contact_id' => 0, 'discountcard' => $discountcard_number));
                     }
                 } else {
                     $discountcard = $model->getByField('discountcard', $discountcard_number);
                 }
                 if ($discountcard) {
                     wa()->getStorage()->set('shop/discountcard', $discountcard['discountcard']);
                 } else {
                     throw new waException('Дисконтная карта не найдена');
                 }
             } else {
                 throw new waException('Укажите номер дисконтной карты');
             }
         }
     } catch (Exception $ex) {
         $this->setError($ex->getMessage());
     }
 }
 public function execute()
 {
     try {
         if ($id = waRequest::post('id', 0, waRequest::TYPE_INT)) {
             $model = new shopDiscountcardsPluginModel();
             $this->response = $model->getById($id);
         } else {
             throw new waException('Ошибка: Идентификатор неопределен');
         }
     } catch (Exception $ex) {
         $this->setError($ex->getMessage());
     }
 }
 public function execute()
 {
     try {
         $model = new shopDiscountcardsPluginModel();
         if ($ids = waRequest::post('id', array(), waRequest::TYPE_ARRAY_INT)) {
             foreach ($ids as $id) {
                 $model->deleteById($id);
             }
         } elseif ($id = waRequest::post('id', 0, waRequest::TYPE_INT)) {
             $model->deleteById($id);
         } else {
             throw new waException('Ошибка: Идентификатор неопределен');
         }
     } catch (Exception $ex) {
         $this->setError($ex->getMessage());
     }
 }
 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());
     }
 }
 public function execute()
 {
     $lazy = waRequest::get('lazy', 0, waRequest::TYPE_INT);
     $offset = waRequest::get('offset', 0, waRequest::TYPE_INT);
     $limit = 30;
     $model = new shopDiscountcardsPluginModel();
     $sql = "SELECT count(*) " . $this->getSql();
     $total_count = (int) $model->query($sql)->fetchField();
     $sql = "SELECT * " . $this->getSql() . " LIMIT {$offset}, {$limit}";
     $discountcards = $model->query($sql)->fetchAll();
     foreach ($discountcards as &$discountcard) {
         if (!empty($discountcard['contact_id'])) {
             $discountcard['contact'] = new waContact($discountcard['contact_id']);
             try {
                 $discountcard['contact']->load();
             } catch (Exception $ex) {
                 unset($discountcard['contact']);
             }
         }
     }
     $this->view->assign(array('discountcards' => $discountcards, 'offset' => $offset, 'limit' => $limit, 'count' => count($discountcards), 'total_count' => $total_count, 'lazy' => $lazy));
 }
 public function execute()
 {
     try {
         $app_settings_model = new waAppSettingsModel();
         if (waRequest::post('cancel')) {
             wa()->getStorage()->set('shop/discountcard', '');
             wa()->getStorage()->set('shop/discountcard/customer_id', '');
         } else {
             if ($discountcard_number = waRequest::post('discountcard')) {
                 if (!($customer_id = waRequest::post('customer_id', 0, waRequest::TYPE_INT))) {
                     throw new waException('Укажите покупателя');
                 }
                 $model = new shopDiscountcardsPluginModel();
                 if ($app_settings_model->get(shopDiscountcardsPlugin::$plugin_id, 'binding_customer')) {
                     $discountcard = $model->getByField(array('contact_id' => $customer_id, 'discountcard' => $discountcard_number));
                     if (empty($discountcard)) {
                         $discountcard = $model->getByField(array('contact_id' => 0, 'discountcard' => $discountcard_number));
                     }
                 } else {
                     $discountcard = $model->getByField('discountcard', $discountcard_number);
                 }
                 if ($discountcard) {
                     wa()->getStorage()->set('shop/discountcard', $discountcard['discountcard']);
                     wa()->getStorage()->set('shop/discountcard/customer_id', $customer_id);
                     $discountcard['amount_format'] = shop_currency($discountcard['amount']);
                     $contact = new waContact($discountcard['contact_id']);
                     $discountcard['contact_name'] = $contact->get('name');
                     $this->response = $discountcard;
                 } else {
                     throw new waException('Дисконтная карта не найдена');
                 }
             } else {
                 throw new waException('Укажите номер дисконтной карты');
             }
         }
     } catch (Exception $ex) {
         $this->setError($ex->getMessage());
     }
 }
 public function execute()
 {
     try {
         $model = new shopDiscountcardsPluginModel();
         $discountcards = $model->getAll();
         if (!$discountcards) {
             throw new waException('Список дисконтных карт пуст');
         }
         $filepath = wa()->getCachePath('plugins/discountcards/export-discountcards.csv', 'shop');
         $f = fopen($filepath, 'w');
         if (!$f) {
             throw new waException('Ошибка создания файла: ' . $filepath);
         }
         foreach ($discountcards as $discountcard) {
             unset($discountcard['id']);
             fputcsv($f, $discountcard, ';', '"');
         }
         fclose($f);
         $this->response['html'] = 'Выгрузка успешно завершена. <a href="?plugin=discountcards&module=download"><i class="icon16 download"></i>Скачать</a>';
     } catch (Exception $ex) {
         $this->setError($ex->getMessage());
     }
 }
 public function execute()
 {
     try {
         $filepath = wa()->getCachePath('plugins/discountcards/import-discountcards.csv', 'shop');
         if (!file_exists($filepath)) {
             throw new waException('Ошибка загрузки файла');
         }
         $delimiter = waRequest::post('delimiter');
         $enclosure = waRequest::post('enclosure');
         $inserted = 0;
         $model = new shopDiscountcardsPluginModel();
         if (waRequest::post('empty')) {
             $model->truncate();
         }
         $f = fopen($filepath, "r");
         while (($data = fgetcsv($f, null, $delimiter, $enclosure)) !== FALSE) {
             if (count($data) == 3) {
                 $discountcard = array('discountcard' => $data[0], 'discount' => $data[1], 'amount' => $data[2]);
             } else {
                 $discountcard = array('contact_id' => $data[0], 'discountcard' => $data[1], 'discount' => $data[2], 'amount' => $data[3]);
             }
             if (empty($discountcard['discountcard'])) {
                 continue;
             }
             if ($model->getByField('discountcard', $discountcard['discountcard'])) {
                 throw new waException('Ошибка: Номер дисконтной карты не уникален ' . $discountcard['discountcard']);
             }
             if ($model->insert($discountcard)) {
                 $inserted++;
             }
         }
         fclose($f);
         $this->response['html'] = 'Добавлено записей: ' . $inserted;
     } catch (Exception $ex) {
         $this->setError($ex->getMessage());
     }
 }
 public function backendOrderEdit($order)
 {
     if ($this->getSettings('status')) {
         $view = wa()->getView();
         if ($discountcard_number = wa()->getStorage()->get('shop/discountcard')) {
             $model = new shopDiscountcardsPluginModel();
             if ($discountcard = $model->getByField('discountcard', $discountcard_number)) {
                 $discountcard['contact'] = new waContact($discountcard['contact_id']);
                 $discountcard['amount_format'] = shop_currency($discountcard['amount']);
                 $view->assign('discountcard', $discountcard);
             }
         }
         $template_path = wa()->getAppPath('plugins/discountcards/templates/BackendOrderEdit.html', 'shop');
         $html = $view->fetch($template_path);
         return $html;
     }
 }