2

I have a Magento 1.7 Community plataform, connected to an ERP (Bling). We have a file that take all those php information and send as an invoice to Bling.

In our products, we have a field named "gtin", that in the most cases are null/empty. Perhaps, when the invoice are sent to the ERP, the value are changed to 0. This "0" information is the big problem, because with this, we have to open invoice by invoice and delete this number 0 to be able to generate the invoice correctly.

In the code on the bottom, please note the line "gtin". My objective is to simply insert a "IF" condition, like:

if ("gtin" == 0) {"gtin" = null/empty }

If the php logic was more "simple", i could be able to make it work, perhaps it's a php code wich i believe is specific for Magento. Here it is:

private function _addItens()
{

    $this->_xml["itens"] = array();
    $this->_xml["itens"]["item"] = array();

    $cont=0;
    foreach ($this->_orderModel->getItemsCollection() as $itemId => $item) {

        $_product = Mage::getModel('catalog/product')->load( $item->product_id );
                    /*if(!$this->checkProductAttributesRequired($_product)){
            throw new Exception("O produto no pedido ".$this->_orderModel->getIncrementId().", selecionado para emissão de Nf-e não possui os atributos requeridos");               
        }*/
        if ( $_product->getResource()->getAttribute( Lema21_Nfe_Model_TransformToXML::ATTR_CODIGO_ORIGEM )->getFrontend()->getValue( $_product )){
            $res = $_product->getResource()->getAttribute( Lema21_Nfe_Model_TransformToXML::ATTR_CODIGO_ORIGEM )->getFrontend()->getValue( $_product );
        }

        $this->_xml["itens"]["item"][$cont] = 
            array(
                "codigo" => $item->getSku(),
                "descricao" => $_product->getData(Lema21_Nfe_Model_TransformToXML::ATTR_OPERATION_NAME),
                "un"    => $_product->getData(Lema21_Nfe_Model_TransformToXML::ATTR_OPERATION_UNIT),
                "qtde"  => $item->getData("qty_ordered"),
                "vlr_unit" => $item->getPrice(),
                "tipo" => self::PRODUCT_OR_SERVICE,
                "peso_bruto" => $item->getWeight(),
                "peso_liq"  => $item->getWeight(),
                "class_fiscal" => $_product->getData(Lema21_Nfe_Model_TransformToXML::ATTR_CODIGO_NCM),
                "origem"    => $_product->getData( Lema21_Nfe_Model_TransformToXML::ATTR_CODIGO_ORIGEM ),
                "gtin" => $_product->getData(Lema21_Nfe_Model_TransformToXML::ATTR_CODIGO_GTIN),
                "gtinEmbalagem" => $_product->getData(Lema21_Nfe_Model_TransformToXML::ATTR_CODIGO_GTIN_EMBALAGEM)
            );

        $cont++;
    }

    return $this;
}

So, i wish someone could give me a light at this. Thanks a lot!

1

See if it works this way:

private function _addItens()
{

    $this->_xml["itens"] = array();
    $this->_xml["itens"]["item"] = array();

    $cont=0;
    foreach ($this->_orderModel->getItemsCollection() as $itemId => $item) {

        $_product = Mage::getModel('catalog/product')->load( $item->product_id );

        /*if(!$this->checkProductAttributesRequired($_product)){
            throw new Exception("O produto no pedido ".$this->_orderModel->getIncrementId().", selecionado para emissão de Nf-e não possui os atributos requeridos");
        }*/

        if ($_product->getResource()->getAttribute(Lema21_Nfe_Model_TransformToXML::ATTR_CODIGO_ORIGEM)->getFrontend()->getValue($_product)){
            $res = $_product->getResource()->getAttribute(Lema21_Nfe_Model_TransformToXML::ATTR_CODIGO_ORIGEM)->getFrontend()->getValue($_product);
        }

        $gtin = null;

        if ($_product->getData(Lema21_Nfe_Model_TransformToXML::ATTR_CODIGO_GTIN)) {
            $gtin = $_product->getData(Lema21_Nfe_Model_TransformToXML::ATTR_CODIGO_GTIN);
        }

        $this->_xml["itens"]["item"][$cont] =
            array(
                "codigo" => $item->getSku(),
                "descricao" => $_product->getData(Lema21_Nfe_Model_TransformToXML::ATTR_OPERATION_NAME),
                "un"    => $_product->getData(Lema21_Nfe_Model_TransformToXML::ATTR_OPERATION_UNIT),
                "qtde"  => $item->getData("qty_ordered"),
                "vlr_unit" => $item->getPrice(),
                "tipo" => self::PRODUCT_OR_SERVICE,
                "peso_bruto" => $item->getWeight(),
                "peso_liq"  => $item->getWeight(),
                "class_fiscal" => $_product->getData(Lema21_Nfe_Model_TransformToXML::ATTR_CODIGO_NCM),
                "origem"    => $_product->getData( Lema21_Nfe_Model_TransformToXML::ATTR_CODIGO_ORIGEM ),
                "gtin" => $gtin,
                "gtinEmbalagem" => $_product->getData(Lema21_Nfe_Model_TransformToXML::ATTR_CODIGO_GTIN_EMBALAGEM)
            );

        $cont++;
    }

    return $this;
}
  • @Danilo, this code will still send GTIN if the field is filled, or it will force it to be null? – Pedro Gusmao Sep 28 '18 at 20:56
  • @Pedro Gusmao If the value entered in the GTIN is 0 as it was typed in the question, it will keep the variable with the value null, note that the $gtin variable starts with the value null, if the field register is different from 0, then the variable $gtin will have the value that is registered in the field. – Danilo Sep 28 '18 at 21:04
  • Amazing! Actually, the problem is that the field is empty, but when we sent the information to Bling, the invoice gone with the 0 instead of null, so i believe this rule will work. I'll try on monday, but thanks for the help!!! – Pedro Gusmao Sep 28 '18 at 21:10
  • if it works let us know here! – Danilo Sep 28 '18 at 21:12
  • my problem was deeper in some way, but i fixed it. Perhaps, if my problem were only this, the code above would work, i tested. Thanks a lot for your time! – Pedro Gusmao Oct 3 '18 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.