【问题标题】:Magento custom reports: Get data from databaseMagento 自定义报告:从数据库中获取数据
【发布时间】:2014-06-06 17:12:33
【问题描述】:

我需要一些帮助。

我在 Magento 中创建了一个自定义报告。 现在我想在我的网格中列出一个月内订购的所有产品。 在我的报告中包含以下列:SKU、名称、订购数量和基本成本。

在“订购数量”列中,我想显示订购产品的频率。 在“基本成本”列中,我想显示总基本成本(订购数量 * 基本成本)。

使用以下代码,我得到了非常正确的产品名称和 skus。 其他列不正确。

有人可以帮我吗?

$this->getSelect()->reset()
        ->from(
            array('order_items' => $this->getTable('sales/order_item')),
            array(
                'ordered_qty' => 'SUM(order_items.qty_ordered)',
                'order_items_name' => 'order_items.name',
                'base_cost' => 'SUM(order_items.base_cost)',
                'sku' => 'order_items.sku'
            ))
        ->where("created_at BETWEEN '".$from."' AND '".$to."'")
        ->where('parent_item_id IS NULL')
        ->group('order_items.product_id')
        ->having('SUM(order_items.qty_ordered) > ?', 0)
        ->order(
            array(
                'SUM(order_items.qty_ordered) DESC'
            ));

【问题讨论】:

  • “不正确”如“空白”、“零”、“随机数”?
  • 订购的商品数量过多,基础费用之和错误(一件商品的基础费用正确)。
  • 尝试输出原始 SQL 查询,参见stackoverflow.com/questions/23834183/…
  • 感谢 R.S 提供的证据!现在我得到了正确的总和。如何格式化成本?这是在我的 Grid.php 中: $this->addColumn('base_cost', array( 'header' =>Mage::helper('reports')->__('Cost'), 'width' =>'120px ', 'align' =>'right', 'index' =>'base_cost', 'total' =>'sum', 'type' =>'price' ));

标签: php magento zend-framework report


【解决方案1】:

这是我的解决方案:

$this->getSelect()->reset()
        ->from(
            array('order_items' => $this->getTable('sales/order_item')),
            array(
                'ordered_qty' => 'order_items.qty_ordered',
                'order_items_name' => 'order_items.name',
                'vendor' => 'attrval.value',
                'base_cost' => '(SUM(order_items.qty_ordered) * order_items.base_cost)',
                'sku' => 'order_items.sku'
            ))
        ->joinLeft(array('p' => 'catalog_product_entity'), 'order_items.product_id = p.entity_id')
        ->joinLeft(array('eav' => 'eav_attribute'), 'p.entity_type_id = eav.entity_type_id')
        ->joinLeft(array('attr' =>'eav_attribute_option'), 'attr.attribute_id = eav.attribute_id')
        ->joinLeft(array('attrval' =>'eav_attribute_option_value'), 'attrval.option_id = attr.option_id')
        ->where("eav.attribute_code='vendor'")
        ->where("order_items.created_at BETWEEN '".$from."' AND '".$to."'")
        ->where('parent_item_id IS NULL')
        ->group('order_items.product_id')
        ->having('SUM(order_items.qty_ordered) > ?', 0)
        ->order(
            array(
                'SUM(order_items.qty_ordered) DESC'
            ));

它包括一个额外的自定义属性,称为“供应商”。

【讨论】:

    【解决方案2】:

    要输出原始 SQL 查询,请参阅 Output raw SQL query from Magento collection

    格式字段,可以使用pricecurrency

    http://code007.wordpress.com/2012/07/16/grid-column-types-in-magento/

      $this->addColumn('some_column_id', array(
              'header' => Mage::helper('core')->__('Some column name'),
              'index' => 'some_column_index',
              'type' => '???',
      ));
    

    类型

    • 动作
    • 复选框
    • 连接
    • 国家
    • 货币
    • 日期
    • 日期时间
    • 输入
    • 界面
    • ip
    • 长文本
    • 大众化
    • 号码
    • 选项
    • 价格
    • 收音机
    • 选择
    • 商店
    • 文字
    • 主题
    • 换行

    查看/app/code/core/Mage/Adminhtml/Block/Widget/Grid/Column/Renderer 文件夹。

    要制作您自己的网格类型,请参阅http://mydons.com/how-to-create-custom-column-renderer-in-magento-grid/

    【讨论】:

    • 我已经通过上面评论的代码完成了。我得到一个像 2618.00000000 这样的数字,我想将其格式化为 2618,00 $
    • 您可能需要添加currency_code 参见stackoverflow.com/questions/4897310/…
    • 谢谢! 'currency_code' => Mage::app()->getStore()->getCurrentCurrencyCode() 是我需要的!
    猜你喜欢
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    相关资源
    最近更新 更多