name = 'paragonfaktura'; $this->tab = 'billing_invoicing'; $this->displayName = 'Paragon Faktura'; $this->version = '1.0.1'; $this->author = 'Cypis.net'; $this->need_instance = 1; $this->bootstrap = true; $this->ps_versions_compliancy = array('min' => '1.6.0', 'max' => '1.7'); $this->currencies = true; $this->currencies_mode = 'checkbox'; $this->is_eu_compatible = 1; parent::__construct(); $this->displayName = $this->l('Paragon Faktura 1.7'); $this->description = $this->l('Select sale document on checkout'); $this->confirm_uninstall = $this->l('Are you sure you want to uninstall? You will lose all your settings!'); } /** * * * @return */ public function install() { $db = Db::getInstance()->Execute('CREATE TABLE `'._DB_PREFIX_.'invoice_bill` ( `id_cart` INT(10) NOT NULL, `document_type` INT(1) NOT NULL, PRIMARY KEY (`id_cart`, `document_type`))'); Configuration::updateValue('IVBILL_DEFAULT', 2); Configuration::updateValue('IVBILL_PLACE', 'hookdisplayPaymentTop'); return parent::install() && $this->registerHook('displayPaymentTop') && $this->registerHook('displayHeader') && $this->registerHook('customParagon') && $this->registerHook('actionValidateOrder') && $this->registerHook('displayAdminOrderSideBottom') && $this->registerHook('displayAdminOrderMainBottom') && $this->registerHook('displayAdminOrderTabContent') && $this->registerHook('displayAdminOrderContentOrder'); } /** * * * @return */ public function uninstall() { $db = Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'invoice_bill`;'); return parent::uninstall() && $db ; } /** * * @param undefined $params * * @return */ public function hookdisplayAdminOrderContentOrder($params){ $out=''; $order = new Order($params['order']->id); $cart_id = Order::getCartIdStatic($params['order']->id); $choice = Db::getInstance()->getRow('SELECT document_type FROM `'._DB_PREFIX_.'invoice_bill` WHERE id_cart = '.$cart_id); if($choice) { $out = "

".$this->l('Sale document').": ".($choice['document_type']==1?$this->l('Invoice'):$this->l('Bill'))."

"; } return $out; } public function hookdisplayAdminOrderSideBottom($params){ return ""; } public function hookdisplayAdminOrderMainBottom($params){ return ""; } public function hookdisplayAdminOrderTabContent($params){ $out=''; $order = new Order($params['id_order']); $cart_id = Order::getCartIdStatic($params['id_order']); $choice = Db::getInstance()->getRow('SELECT document_type FROM `'._DB_PREFIX_.'invoice_bill` WHERE id_cart = '.$cart_id); if($choice) { $out = "

".$this->l('Sale document').": ".($choice['document_type']==1?$this->l('Invoice'):$this->l('Bill'))."

"; } return $out.$this->l('Order id').': '.$params['id_order'].' / '.$this->l('Cart id').': '.$cart_id;; } /** * * * @return */ public function hookDisplayHeader(){ $this->context->controller->registerJavascript( 'paragonfaktura', 'modules/'.$this->name.'/save.js', array('position' => 'bottom', 'priority' => 150) ); } public function hookCustomParagon($params){ $pl=Configuration::get('IVBILL_PLACE'); if($pl==='hookCustomParagon' ) { $context = Context::getContext(); $invoice_bill = Configuration::get('IVBILL_DEFAULT'); $choice = Db::getInstance()->ExecuteS('SELECT document_type FROM `'._DB_PREFIX_.'invoice_bill` WHERE id_cart = '.$this->context->cookie->id_cart); if($choice) { if(!empty($choice['document_type'])) $invoice_bill = $choice['document_type']; else{ Db::getInstance()->update('invoice_bill', array( 'document_type' => $invoice_bill ), 'id_cart = '.(int)$this->context->cookie->id_cart); } }else{ Db::getInstance()->insert('invoice_bill', array( 'id_cart' => (int)$this->context->cookie->id_cart, 'document_type' => $invoice_bill, )); } $this->context->smarty->assign( array( 'type' => $invoice_bill, 'id_cart' => $this->context->cookie->id_cart ) ); return $this->display(__FILE__, 'cart.tpl'); } } /** * * @param undefined $params * * @return */ public function hookdisplayPaymentTop($params) { $pl=Configuration::get('IVBILL_PLACE'); if($pl=='hookdisplayPaymentTop') { $context = Context::getContext(); $invoice_bill = Configuration::get('IVBILL_DEFAULT'); $choice = Db::getInstance()->ExecuteS('SELECT document_type FROM `'._DB_PREFIX_.'invoice_bill` WHERE id_cart = '.$this->context->cookie->id_cart); if($choice) { if(!empty($choice['document_type'])) $invoice_bill = $choice['document_type']; else{ Db::getInstance()->update('invoice_bill', array( 'document_type' => $invoice_bill ), 'id_cart = '.(int)$this->context->cookie->id_cart); } }else{ Db::getInstance()->insert('invoice_bill', array( 'id_cart' => (int)$this->context->cookie->id_cart, 'document_type' => $invoice_bill, )); } $this->context->smarty->assign( array( 'type' => $invoice_bill, 'id_cart' => $this->context->cookie->id_cart ) ); return $this->display(__FILE__, 'cart.tpl'); } } /** * * * @return */ public function getContent() { $output = null; if (Tools::isSubmit('submit'.$this->name)) { Configuration::updateValue('IVBILL_DEFAULT', $_POST['IVBILL_DEFAULT']); Configuration::updateValue('IVBILL_PLACE', $_POST['IVBILL_PLACE']); Configuration::updateValue('IVBILL_EMAIL', $_POST['IVBILL_EMAIL']); $output .= $this->displayConfirmation($this->l('Settings updated')); } return $output.$this->displayForm()."
".$this->l('For custom hook display use code').": {hook h='CustomParagon'}
"; } /** * * * @return */ public function displayForm() { // Get default language $default_lang = (int)Configuration::get('PS_LANG_DEFAULT'); $options_place = array( array( 'id_option' => 'hookdisplayPaymentTop', 'name' => $this->l('Payment section') ), array( 'id_option' => 'hookCustomParagon', 'name' => $this->l('Custom section') ), ); $options = array( array( 'id_option' => 1, 'name' => $this->l('Invoice') ), array( 'id_option' => 2, 'name' => $this->l('Bill') ), ); // Init Fields form array $fields_form[0]['form'] = array( 'legend' => array( 'title' => $this->l('Settings'), ), 'input' => array( array( 'type' => 'select', 'label' => $this->l('Default sale document'), 'desc' => $this->l('Select default selected sale document'), 'name' => 'IVBILL_DEFAULT', 'required' => true, 'options' => array( 'query' => $options, 'id' => 'id_option', 'name' => 'name' ) ), array( 'type' => 'select', 'label' => $this->l('Default sale isplay place'), 'desc' => $this->l('Select default display place'), 'name' => 'IVBILL_PLACE', 'required' => true, 'options' => array( 'query' => $options_place, 'id' => 'id_option', 'name' => 'name' ) ), array( 'type' => 'switch', 'label' => $this->l('Send document info mail'), 'name' => 'IVBILL_EMAIL', 'is_bool' => true, 'desc' => $this->l('Send email with information about selected document type'), 'values' => array( array( 'id' => 'active_on', 'value' => 1, 'label' => $this->l('Yes') ), array( 'id' => 'active_off', 'value' => 0, 'label' => $this->l('No') ) ), ) ), 'submit' => array( 'title' => $this->l('Save'), 'class' => 'btn btn-default pull-right' ) ); $helper = new HelperForm(); // Module, token and currentIndex $helper->module = $this; $helper->name_controller = $this->name; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name; // Language $helper->default_form_language = $default_lang; $helper->allow_employee_form_lang = $default_lang; // Title and toolbar $helper->title = $this->displayName; $helper->show_toolbar = true; // false -> remove toolbar $helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen. $helper->submit_action = 'submit'.$this->name; $helper->toolbar_btn = array( 'save' => array( 'desc' => $this->l('Save'), 'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name. '&token='.Tools::getAdminTokenLite('AdminModules'), ), 'back' => array( 'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'), 'desc' => $this->l('Back to list') ) ); // Load current value $helper->fields_value['IVBILL_DEFAULT'] = Configuration::get('IVBILL_DEFAULT'); $helper->fields_value['IVBILL_PLACE'] = Configuration::get('IVBILL_PLACE'); $helper->fields_value['IVBILL_EMAIL'] = Configuration::get('IVBILL_EMAIL'); return $helper->generateForm($fields_form); } public function hookActionValidateOrder($params) { $cart = $params['cart']; // The cart object $order_status = $params['orderStatus']; // The order status $order = $params['order']; // And the order object $choice = Db::getInstance()->getRow('SELECT document_type FROM `'._DB_PREFIX_.'invoice_bill` WHERE id_cart = '.$cart->id); if($choice) { $out = "

".$this->l('Sale document').": ".($choice['document_type']==1?$this->l('Invoice'):$this->l('Bill'))."

"; } if(Configuration::get('IVBILL_EMAIL')!=1) return; $to_email = $customer->email; $iso = Language::getIsoById((int) Configuration::get('PS_LANG_DEFAULT')); $dir_mail = false; if (file_exists(dirname(__FILE__).'/mails/'.$iso.'/document.txt') && file_exists(dirname(__FILE__).'/mails/'.$iso.'/document.html')) { $dir_mail = dirname(__FILE__).'/mails/'; } if (file_exists(_PS_MAIL_DIR_.$iso.'/document.txt') && file_exists(_PS_MAIL_DIR_.$iso.'/document.html')) { $dir_mail = _PS_MAIL_DIR_; } $context = Context::getContext(); $id_lang = (int) $context->language->id; $id_shop = (int) $context->shop->id; $template_vars = array( '{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{shop_name}' => Configuration::get('PS_SHOP_NAME'), '{order_name}' => $order->reference, '{info}' => $out ); //die( Configuration::get('PS_SHOP_EMAIL') ); if ($dir_mail) { if (Validate::isEmail(Configuration::get('PS_SHOP_EMAIL'))) $out = Mail::Send( $id_lang, 'document', $this->l('Document picked for order')."#".$order->id." ".$order->reference, $template_vars, Configuration::get('PS_SHOP_EMAIL'), null, Configuration::get('PS_SHOP_EMAIL'), Configuration::get('PS_SHOP_NAME'), null, null, $dir_mail, null, $id_shop ); } return ; } } ?>