Jump to content

recuperer des information à partir de l'id product


Recommended Posts

bonjour,

je m'appelle franck je devellope un module qui permet de réaliser des dévis sous PretaShop 1.7.6.1 bien enttendu j'ai utiliser prestasshop validator pour obtenir la structure du module j'ai pas mal avance

mais je bloque depuis un petit moment je voudrais à partir d'un formulaire selectionner un product et la quantite  puis  et  remplir un tableau avec les donneés de l'id (produit,référence,quantity,prix et avoir le total du prix automatiquement en enregistrant le formulaire.

merci d'avance si vous avez une petite idéé 

voici mon code pour l'instant

<?php

/**

* 2007-2019 PrestaShop

*

* NOTICE OF LICENSE

*

* This source file is subject to the Academic Free License (AFL 3.0)

* that is bundled with this package in the file LICENSE.txt.

* It is also available through the world-wide-web at this URL:

* http://opensource.org/licenses/afl-3.0.php

* If you did not receive a copy of the license and are unable to

* obtain it through the world-wide-web, please send an email

* to [email protected] so we can send you a copy immediately.

*

* DISCLAIMER

*

* Do not edit or add to this file if you wish to upgrade PrestaShop to newer

* versions in the future. If you wish to customize PrestaShop for your

* needs please refer to http://www.prestashop.com for more information.

*

*  @author    PrestaShop SA <[email protected]>

*  @copyright 2007-2019 PrestaShop SA

*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)

*  International Registered Trademark & Property of PrestaShop SA

*/

$sql = array();

 

$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'devis` (

    `id_devis` int(11) NOT NULL AUTO_INCREMENT,

    `date_add` datetime NOT NULL,

    `active` int(1) default 0,

     PRIMARY KEY  (`id_devis`)

) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';

 

$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'devis_lang` (

   `id_devis` int(11) NOT NULL,

    `id_lang`int(11) UNSIGNED,

    `id_products` int(11) NOT NULL,

    `reference` int(11) NOT NULL,

    `quantity` int(11) NOT NULL,

    `price` float(20) NOT NULL,

     PRIMARY KEY  (`id_devis`,`id_lang`,`id_products`)

) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';




 

foreach ($sql as $query) {

    if (Db::getInstance()->execute($query) == false) {

        return false;

    }

}

<?php

/**

* 2007-2019 PrestaShop

*

* NOTICE OF LICENSE

*

* This source file is subject to the Academic Free License (AFL 3.0)

* that is bundled with this package in the file LICENSE.txt.

* It is also available through the world-wide-web at this URL:

* http://opensource.org/licenses/afl-3.0.php

* If you did not receive a copy of the license and are unable to

* obtain it through the world-wide-web, please send an email

* to [email protected] so we can send you a copy immediately.

*

* DISCLAIMER

*

* Do not edit or add to this file if you wish to upgrade PrestaShop to newer

* versions in the future. If you wish to customize PrestaShop for your

* needs please refer to http://www.prestashop.com for more information.

*

*  @author    PrestaShop SA <[email protected]>

*  @copyright 2007-2019 PrestaShop SA

*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)

*  International Registered Trademark & Property of PrestaShop SA

*/

 

/**

 * In some cases you should not drop the tables.

 * Maybe the merchant will just try to reset the module

 * but does not want to loose all of the data associated to the module.

 */

$sql = array();

 

$sql[] = 'DROP TABLE IF  EXISTS `' . _DB_PREFIX_ . 'devis`';

$sql[] = 'DROP TABLE IF  EXISTS `' . _DB_PREFIX_ . 'devis_lang`';


 

foreach ($sql as $query) {

    if (Db::getInstance()->execute($query) == false) {

        return false;

    }

}

<?php

 

/**

 * 2007-2019 PrestaShop

 *

 * NOTICE OF LICENSE

 *

 * This source file is subject to the Academic Free License (AFL 3.0)

 * that is bundled with this package in the file LICENSE.txt.

 * It is also available through the world-wide-web at this URL:

 * http://opensource.org/licenses/afl-3.0.php

 * If you did not receive a copy of the license and are unable to

 * obtain it through the world-wide-web, please send an email

 * to [email protected] so we can send you a copy immediately.

 *

 * DISCLAIMER

 *

 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer

 * versions in the future. If you wish to customize PrestaShop for your

 * needs please refer to http://www.prestashop.com for more information.

 *

 *  @author    PrestaShop SA <[email protected]>

 *  @copyright 2007-2019 PrestaShop SA

 *  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)

 *  International Registered Trademark & Property of PrestaShop SA

 */

 

if (!defined('_PS_VERSION_')) {

    exit;

}

 

class Devis extends Module

{

    protected $config_form = false;

 

    public function __construct()

    {

        $this->name = 'Devis';

        $this->tab = 'administration';

        $this->version = '1.3.4';

        $this->author = 'FranckM';

        $this->need_instance = 0;

 

        /**

         * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)

         */

        $this->bootstrap = true;

 

        parent::__construct();

 

        $this->tabs = array(

            array('name' => 'Devis', 'class_name' => 'ParentDevis', 'parent' => ''),

            array('name' => 'Devis', 'class_name' => 'AdminDevis', 'parent' => 'ParentDevis'),

        );

 

        $this->displayName = $this->l('Devis');

        $this->description = $this->l('Module de réalisation de devis à partir du back office ');

 

        $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);

    }

 

    /**

     * Don't forget to create update methods if needed:

     * http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update

     */

    public function install()

    {

 

        require _PS_MODULE_DIR_ . 'devis/sql/install.php';

        return parent::install() &&

            $this->installTab()&&

            $this->registerHook('header') &&

            $this->registerHook('backOfficeHeader') &&

            $this->registerHook('displayHome');

    }

 

    public function uninstall()

    {

        require _PS_MODULE_DIR_ . 'devis/sql/uninstall.php';

        return $this->installTab(false) && parent::uninstall();

    }

 

    public function installTab($install = true)

    {

        if ($install) {

            $languages = Language::getlanguages();

            foreach ($this->tabs as $t) {

                $tab = new Tab();

                $tab->module = $this->name;

                $tab->class_name = $t['class_name'];

                $tab->id_parent = Tab::getIdFromClassName($t['parent']);

                foreach ($languages as $language) {

                    $tab->name[$language['id_lang']]  = $t['name'];

                }

 

                $tab->save();

            }

            return true;

        } else {

 

            foreach ($this->tabs as $t) {

                $id = Tab::getIdFromClassName('class_name');

                if ($id) {

                    $tab = new Tab($id);

                    $tab->delete();

                }

            }


 

            return true;

        }

    }

    /**

     * Load the configuration form

     */

    public function getContent()

    {

        /**

         * If values have been submitted in the form, process.

         */

        if (Tools::isSubmit('submitDevisModule') === true) {

            $this->postProcess();

        }

 

        $this->context->smarty->assign('module_dir', $this->_path);

 

        $output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl');

 

        return $output . $this->renderForm();

    }

 

    /**

     * Create the form that will be displayed in the configuration of your module.

     */

    protected function renderForm()

    {

        $helper = new HelperForm();

 

        $helper->show_toolbar = false;

        $helper->table = $this->table;

        $helper->module = $this;

        $helper->default_form_language = $this->context->language->id;

        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);

 

        $helper->identifier = $this->identifier;

        $helper->submit_action = 'submitDevisModule';

        $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)

            . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;

        $helper->token = Tools::getAdminTokenLite('AdminModules');

 

        $helper->tpl_vars = array(

            'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */

            'languages' => $this->context->controller->getLanguages(),

            'id_language' => $this->context->language->id,

        );

 

        return $helper->generateForm(array($this->getConfigForm()));

    }

 

    /**

     * Create the structure of your form.

     */

    protected function getConfigForm()

    {

        return array(

            'form' => array(

                'legend' => array(

                    'title' => $this->l('Settings'),

                    'icon' => 'icon-cogs',

                ),

                'input' => array(

                    array(

                        'type' => 'switch',

                        'label' => $this->l('Live mode'),

                        'name' => 'DEVIS_LIVE_MODE',

                        'is_bool' => true,

                        'desc' => $this->l('Use this module in live mode'),

                        'values' => array(

                            array(

                                'id' => 'active_on',

                                'value' => true,

                                'label' => $this->l('Enabled'),

                            ),

                            array(

                                'id' => 'active_off',

                                'value' => false,

                                'label' => $this->l('Disabled'),

                            ),

                        ),

                    ),

                    array(

                        'col' => 3,

                        'type' => 'text',

                        'prefix' => '<i class="icon icon-envelope"></i>',

                        'desc' => $this->l('Enter a valid email address'),

                        'name' => 'DEVIS_ACCOUNT_EMAIL',

                        'label' => $this->l('Email'),

                    ),

                    array(

                        'type' => 'password',

                        'name' => 'DEVIS_ACCOUNT_PASSWORD',

                        'label' => $this->l('Password'),

                    ),

                ),

                'submit' => array(

                    'title' => $this->l('Save'),

                ),

            ),

        );

    }

 

    /**

     * Set values for the inputs.

     */

    protected function getConfigFormValues()

    {

        return array(

            'DEVIS_LIVE_MODE' => Configuration::get('DEVIS_LIVE_MODE', true),

            'DEVIS_ACCOUNT_EMAIL' => Configuration::get('DEVIS_ACCOUNT_EMAIL', '[email protected]'),

            'DEVIS_ACCOUNT_PASSWORD' => Configuration::get('DEVIS_ACCOUNT_PASSWORD', null),

        );

    }

 

    /**

     * Save form data.

     */

    protected function postProcess()

    {

        $form_values = $this->getConfigFormValues();


 

        foreach (array_keys($form_values) as $key) {

            Configuration::updateValue($key, Tools::getValue($key));

        }

    }

 

    /**

     * Add the CSS & JavaScript files you want to be loaded in the BO.

     */

    public function hookBackOfficeHeader()

    {

        if (Tools::getValue('module_name') == $this->name) {

            $this->context->controller->addJS($this->_path . 'views/js/back.js');

            $this->context->controller->addCSS($this->_path . 'views/css/back.css');

        }

    }

 

    /**

     * Add the CSS & JavaScript files you want to be added on the FO.

     */

    public function hookHeader()

    {

        $this->context->controller->addJS($this->_path . '/views/js/front.js');

        $this->context->controller->addCSS($this->_path . '/views/css/front.css');

    }

 

    public function hookDisplayHome()

    {

        /* Place your code here. */ }

}

 <?php

 

class ModelDevis  extends objectModel

{

  public $id;

  public $date_add;

  public $active = true;

  public $name;

  public $id_products;

  public $reference;

  public $quantity;

  public $price;



 

    /**

     * @see ObjectModel::$definition

     */

    public static $definition = array(

      'table' =>'devis',

      'primary' => 'id_devis',

      'multilang' => true,

 

      'fields' => array(

          'date_add' => array('type' => self::TYPE_DATE),

          'active' => array('type' => self::TYPE_BOOL),

          'date_add'=>array('type'=> self::TYPE_DATE,'validate'=>'isDate','copy_post'=> false),

          'name' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 255),

          'id_products' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),

          'reference' => array('type' => self::TYPE_STRING,'validate' => 'isReference', 'required' => true, 'size' => 255),

          'quantity' => array('type' => self::TYPE_INT, 'validate' => 'isInt','required'=> true, 'size' => 11),

          /* shop fields*/

          'price' => array('type' => self::TYPE_FLOAT,'shop'=>true, 'validate' => 'isNegativePrice','required' => true, 'size' => 20),

 

      ),

  );


 

 }

<?php


 

if (!class_exists('ModelDevis'))

    require_once dirname(dirname(dirname(__FILE__))) . '/classes/ModelDevis.php';

 

class AdminDevisController extends ModuleAdminController

{


 

    public function __construct()

    {

 

        $this->table = 'devis';

        $this->className = 'ModelDevis';

        $this->lang = true;

        $this->deleted = false;

        $this->bootstrap = true;

        $this->list_id = 'devis';

        $this->identifier = 'id_devis';

        $this->_defaultOrderBy = 'name';

        $this->_defaultOrderWay = 'ASC';

        $this->allow_export = true;

 

        $this->context = context::getcontext();


 

        $this->fields_list = array(

            'id_devis' => array(

                'title' => $this->l('ID',array(), 'Admin.Global'),

                'align' => 'center',

                'class' => 'fixed-width-xs',

            ),

          /*  'logo' => array(

                'title' => $this->l('Logo', array(), 'Admin.Global'),

                'image' => 'm',

                'orderby' => false,

                'search' => false,

                'align' => 'center',

            ),*/

            'id_products' => array(

                'title' => $this->l('Products', array(), 'Admin.Catalog.Feature'),

                'search' => false,

                'align' => 'center',

 

           ),

            'date_add' => array(

                'title' => $this->l('Date',array(), 'Admin.Global'),

                'type' => 'date',

 

            ),

 

            'quantity' => array(

                'title' => $this->l('Quantity',array(), 'Admin.Global'),

                'width' => 'auto',

            ),


 

            'reference' => array(

               'title' => $this->l('Reference',array(), 'Admin.Global'),

                'width' => 'auto',

           ),

 

            'price' => array(

                'title' => $this->l('Price',array(), 'Admin.Global'),

                'width' => 'auto',

        ),

 

            'active' => array(

                'title' => $this->l('Enabled',array(), 'Admin.Global'),

                'active' => 'status',

                'type' => 'bool',

                'align' => 'center',

                'class' => 'fixed-width-xs',

               'orderby' => false,

            ),

        );



 

        $this->addRowAction('edit');

        $this->addRowAction('delete');

 

        parent::__construct();

    }

 

    public function renderForm()

    {



 

        if (!($devis = $this->loadObject(true))) {

            return;

        }

 

        /*  $image = _PS_MANU_IMG_DIR_ . $devis->id . '.jpg';

        $image_url = ImageManager::thumbnail(

            $image,

            $this->table . '_' . (int) $devis->id . '.' . $this->imageType,

            350,

            $this->imageType,

            true,

            true

        );

        $image_size = file_exists($image) ? filesize($image) / 1000 : false;*/

        $this->context = Context::getContext();

        $this->context->controller = $this;

        $this->fields_form = array(

            'tinymce' => true,

            'legend' => array(

                'title' => $this->l('Devis', array(), 'Admin.Catalog.Feature'),

                'icon'=> 'icon-certificate',

            ),

            'input' => array(

                array(

                    'type' => 'select',

                    'label' => $this->l('Products',array(), 'Admin.Global'),

                    'name' => 'products',

                    'required'=>true,

                    'default_value'=>1,

                    'options'=>array('query' => Product::getProducts($this->context->cookie->id_lang,1,1000,'name','ASC'),

                    'id'=>'id_product','name' =>'name'),

                    'hint' => $this->l('Invalid characters:',array(), 'Admin.Notifications.Info') . ' &lt;&gt;;=#{}',

 

                  ),

                array(

                    'type' => 'text',

                    'label' => $this->l('Date',array(), 'Admin.Global'),

                    'name' => 'date',

                    'col' => 4,

                    'required' => true,

                    'hint' => $this->l('Invalid characters:', array(), 'Admin.Notifications.Info'). ' &lt;&gt;;=#{}',

                ),

 

                        /*  array(

                    'type' => 'file',

                    'label' => $this->l('Logo', array(), 'Admin.Global'),

                    'name' => 'logo',

                    'image' => $image_url ? $image_url : false,

                    'size' => $image_size,

                    'display_image' => true,

                    'col' => 6,

                    'hint' => $this->l('Upload a brand logo from your computer.', array(), 'Admin.Catalog.Help'),

                ),*/

                array(

                    'type' => 'text',

                    'label' => $this->l('Quantity',array(), 'Admin.Global'),

                    'name' => 'quantity',

                    'col' => 4,

                    'require'=>true,

                    'hint' => $this->l('Invalid characters:',array(), 'Admin.Notifications.Info') . ' &lt;&gt;;=#{}',

                ),

                array(

                    'type' => 'text',

                    'label' => $this->l('Reference',array(), 'Admin.Global'),

                    'name' => 'reference',

                    'col' => 4,

                    'require'=>true,

                    'hint' => $this->l('Invalid characters:',array(), 'Admin.Notifications.Info') . ' &lt;&gt;;=#{}',

                ),

                array(

                    'type' => 'text',

                    'label' => $this->l('Price',array(), 'Admin.Global'),

                    'name' => 'price',

                    'col' => 4,

                    'require'=>true,

                    'hint' => $this->l('Invalid characters:',array(), 'Admin.Notifications.Info') . ' &lt;&gt;;=#{}',

                ),

 

                array(

                    'type' => 'switch',

                    'label' => $this->l('Enable', array(), 'Admin.Global'),

                     'name'=> 'active',

                    'required' => false,

                    'class' => 't',

                    'is_bool' => true,

                    'values' => array(

                        array(

                            'id' => 'active_on',

                            'value' => 1,

                            'label' => $this->l('Enabled', array(), 'Admin.Global'),

                        ),

                        array(

                            'id' => 'active_off',

                            'value' => 0,

                             'label' => $this->l('Disabled', array(), 'Admin.Global'),

 

                        ),

                    ),

                ),

            ),

         );





 

        if (!($devis = $this->loadObject(true))) {

            return;

        }

        if (Shop::isFeatureActive()) {

            $this->fields_form['input'][] = array(

                'type' => 'shop',

                'label' => $this->l('Shop association', array(), 'Admin.Global'),

                'name' => 'checkBoxShopAsso',

            );

        }

 

        $this->fields_form['submit'] = array(

            'title' => $this->l('Save'),

        );

 

        foreach ($this->_languages as $language) {

 

            $this->fields_value['reference_' . $language['id_lang']] = htmlentities(stripslashes($this->getFieldValue(

                $devis,

                'reference',

                $language['id_lang']

            )), ENT_COMPAT, 'UTF-8');

       }

        return parent::renderForm();

 

    }

     /**

     * Surcharge de la fonction de traduction sur PS 1.7 et supérieur.

     * La fonction globale ne fonctionne pas

     * @param type $string

     * @param type $class

     * @param type $addslashes

     * @param type $htmlentities

     * @return type

     */


 

     protected function l($string, $class = null, $addslashes = false, $htmlentities = true)

    {

         if (_PS_VERSION_ >='1.7'){

             return context::getContext()->getTranslator()->trans($string);

         }

         else{

             return parent ::l($string,$class,$addslashes,$htmlentities);

         }

    }




 

}

 

 

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...