`

打包订单单据发给货代

    博客分类:
  • RUBY
阅读更多
pdf&excell&png
# render流生成
PerformanceInvoiceExcel.new(r).render
CertificatePdf.new(certificates, chemical, qitem).render
# IO读取文件流
IO.read file
IO.read("#{Rails.root}/tmp/uploads/store/" + @ufile.attachment_id)


gem 'prawn'
gem 'prawn-table' 
pdf自带render方法
---
excell
自定义render方法
require 'write_xlsx'
@io = StringIO.new
@book = WriteXLSX.new(@io)
@sheet = @book.add_worksheet
def render
    set_rows
    default_text
    company_info
    table_info
    footer_content
    @book.close
    @io.string
end


def download
    checked_file = params[:checked_file]&.split ","
    zip = PackageZip.new(@order_items, checked_file)
    zip.generate_zip do |zip_path|
      send_file(
        zip_path,
        filename: "iChemical-Package Document-Order-#{rand(99999)}.zip",
        type: "application/zip",
        disposition: "attachment"
      )
    end
    zip.delete_temp_zip
  end


require 'zip'
class PackageZip

  def initialize(order_items, checked_file)
    @order_items = order_items
    @order = order_items.last&.order
    @checked_file = checked_file
    @temp_dir = Dir.mktmpdir
    @zip_list = []
    @zip_name_list = []
  end

  def generate_zip(&block)
    chooise_file @order
    zip_path = File.join(@temp_dir, 'temp.zip')
    Zip::File.open(zip_path, Zip::File::CREATE) do |zipfile|
      @zip_name_list.each_with_index do |name, index|
        zipfile.get_output_stream(name) do |io|
          io.write @zip_list[index]
        end
      end
    end
    block.call zip_path
    ensure
  end

  def chooise_file(order)
    checked_file = @checked_file || order.order_items&.first&.package_documents&.first&.checked_file&.split(",")
    if !checked_file.nil?
      checked_file.each do |file|
        attachments = order.order_attachments.where(file_type: file)
        @ufile = attachments.last
        case file
        when "coa"
          if @ufile.nil?
            @order_items.each do |item|
              qitem = nil
              if item.order.type == "OfflineOrder"
                chemical = item.chemical
                certificates = item.quotation_item.certificates.pluck(:item, :specification, :result).compact
                qitem = item.quotation_item
              else
                chemical = item.chemical
                certificates = item.certificates.pluck(:item, :specification, :result).compact
                qitem = item
              end
              certificate_pdf = CertificatePdf.new(certificates, chemical, qitem).render
              certificate_pdf_name = "cctv-COA-CAS[#{chemical.cas}]-#{rand(99999)}.pdf"
              @zip_name_list.push certificate_pdf_name
              @zip_list.push certificate_pdf
            end
          else
            certificate_pdf_name = @ufile.attachment_filename
            certificate_pdf = IO.read("#{Rails.root}/tmp/uploads/store/" + @ufile.attachment_id)
            @zip_name_list.push certificate_pdf_name
            @zip_list.push certificate_pdf
          end
        when "msds"
          if attachments.present?
            attachments.zip_file do |zip_path|
              @zip_name_list.push "cctv-MSDS-Package-Order-#{rand(99999)}.zip"
              @zip_list.push IO.read zip_path
            end
          else
            @order_items.each do |item|
              msds = Msds.new(item)
              result, flag = msds.generate
              msds_file_name = 'cctv' << (flag ? '' : '-GT') << '-MSDS-' << item.order_item_no << '.pdf'
              msds_file = msds.render
              @zip_name_list.push msds_file_name
              @zip_list.push msds_file
            end
          end
        when "pi"
          if ProformaInvoiceRecord.updated.find_by(order_id: @order.id).present?
            proforma_invoice_xls_name = "cctv-Proforma Invoice-Order-#{order.uuid}-#{rand(99999)}.xls"
            r = ProformaInvoiceRecord.create_with_auto(@order_items, nil, nil, "modified")
            proforma_invoice_xls = PerformanceInvoiceExcel.new(r).render
          else
            proforma_invoice_xls_name = "cctv-Proforma Invoice-Order-#{order.uuid}-#{rand(99999)}.xls"
            r = ProformaInvoiceRecord.create_with_auto(@order_items, nil, nil, "original")
            proforma_invoice_xls = PerformanceInvoiceExcel.new(r).render
          end
          @zip_name_list.push proforma_invoice_xls_name
          @zip_list.push proforma_invoice_xls
        when "ci"
          if CommercialInvoiceRecord.updated.find_by(order_id: @order.id).present?
            invoice_xls_name = "cctv Invoice-#{order.uuid}-#{rand(99999)}.xls"
            r = CommercialInvoiceRecord.create_with_auto(@order_items, nil, nil, "modified")
            invoice_xls = CommercialInvoiceExcel.new(r).render
          else
            invoice_xls_name = "cctv Invoice-#{order.uuid}-#{rand(99999)}.xls"
            r = CommercialInvoiceRecord.create_with_auto(@order_items, nil, nil, "original")
            invoice_xls = CommercialInvoiceExcel.new(r).render
          end
          @zip_name_list.push invoice_xls_name
          @zip_list.push invoice_xls
        when "pl"
          if PackageListRecord.updated.find_by(order_id: @order.id).present?
            pack_list_xls_name = "cctv-Packing List-Order-#{order.uuid}-#{rand(99999)}.xls"
            r = PackageListRecord.create_with_auto(@order_items, nil, nil, "modified")
            pack_list_xls = PackingListExcel.new(r).render
          else
            pack_list_xls_name = "cctv-Packing List-Order-#{order.uuid}-#{rand(99999)}.xls"
            r = PackageListRecord.create_with_auto(@order_items, nil, nil, "original")
            pack_list_xls = PackingListExcel.new(r).render
          end
          @zip_name_list.push pack_list_xls_name
          @zip_list.push pack_list_xls
        when "qc_report"
          @order_items.each do |item|
            testing_records = item.testing_records
            if testing_records.present?
              testing_records.each do |file|
                if file.file_path.present?
                  file_name = file.file_name
                  file = file.file_path
                  new_file = download_img(file, file_name)
                  if  new_file.present?
                    @zip_name_list.push file_name
                    @zip_list.push IO.read new_file
                    if File.exist?(new_file)
                      File.delete(new_file)
                    end
                  end
                end
              end
            end
          end
        else
          if !@ufile.nil?
            file_name = @ufile.attachment_filename
            file = "#{Rails.root}/tmp/uploads/store/" + @ufile.attachment_id
            @zip_name_list.push file_name
            @zip_list.push IO.read file
          end
        end
      end
    end
  end

  ########下载图片
  def download_img(img_url,file_name)
    image_temp_dir = "tmp/uploads/qc_report/"
    FileUtils.mkdir(image_temp_dir) unless File.exists?(image_temp_dir)
    begin
      _file_path = image_temp_dir+file_name
      unless File.exists?(_file_path)
        img_file = open(img_url) { |f| f.read }
        open(image_temp_dir+file_name, "wb") { |f| f.write(img_file) }
      end
      return _file_path
    rescue => err
      puts err
      return ''
    end
  end


  def delete_temp_zip
    Thread.new do
      sleep 2
      FileUtils.rm_rf @temp_dir if @temp_dir
    end
  end

end
0
0
分享到:
评论

相关推荐

    参考资料-U9销售订单单据类型.zip

    【标题】:“参考资料-U9销售订单单据类型.zip”指的是一个关于用友U9系统中销售订单单据类型的参考资料压缩包。这个压缩包包含了对U9系统中不同销售订单单据类型的详细介绍,帮助用户理解并操作销售流程。 【描述...

    金蝶K3 Wise 用SQL生成单号(单据编号)及单据内码(单据ID)

    在金蝶K3 Wise系统中,管理和生成单据编号与单据内码是企业信息化管理中的重要环节。本文将详细解析如何通过SQL语句来实现这一功能,主要涉及的两个核心存储过程是`p_BM_GetBillNo`和`GetICMaxNum`。 一、生成单据...

    用友最大单据号工具大全

    单据号通常用于区分不同的业务操作,如采购订单、销售发票、入库单等,每个单据号都是独一无二的,反映了特定业务活动的状态和时间。在大量业务数据中,手动管理单据号容易出错,因此,使用专业工具进行自动跟踪和...

    K3BOS开发单据导入

    单据头通常包含订单的总体信息,如订单号、日期、客户或供应商信息等。单据体则包含具体的产品明细,如产品编码、数量、单价和金额等。该工具能够分别处理这两部分,确保所有必要信息都被正确地导入到金蝶K3系统中。...

    捷民-订单-制作单-送货单-单据模板-单据管理系统

    经过几个月的学习和研究,以及农民兄弟的指导和帮助,终于完成了一个单据管理系统(模板),现取名为“捷民单据管理”,旨在便捷利民!在此衷心感谢农民兄弟的帮助。图文广告业的单据部分项目牵涉面积计算,较为难写...

    金蝶K3BOS单据转换-流程设置.docx

    金蝶K3 BOS单据转换流程设置是一个关键的业务操作,它涉及到企业内部的信息化管理,特别是财务和供应链管理的高效协同。金蝶K3是企业资源计划系统(ERP)的一部分,BOS(Business Operation System)则是金蝶提供的...

    单据流转功能参考

    单据流转功能是企业信息化系统中的重要组成部分,它主要用于处理企业内部的各种业务单据,如采购订单、销售发票、库存调整单等。这些单据在不同的业务环节中需要经过审批、传递、执行等多个步骤,单据流转就是模拟并...

    根据当天日期自动生成单据号

    在IT行业中,尤其是在Java编程领域,自动生成单据号是一个常见的需求,特别是在财务系统、订单管理系统或者任何需要唯一标识业务实体的地方。这个需求通常涉及到日期处理、字符串格式化以及可能的序列号生成。下面...

    用友uap开发nc65向导单表单据详细步骤

    1. **需求分析**:明确单表单据所需的数据字段和业务逻辑,例如订单表可能包含订单号、客户名称、商品名称、数量、单价等字段。 2. **设计数据库表**:在用友UAP平台上创建对应的数据库表,定义字段类型、长度、...

    NC65开发课件之主子单据开发

    NC65开发课件之主子单据开发 NC65开发课件之主子单据开发是基于元数据生成主子单据的过程,旨在帮助开发者快速掌握NC65平台下的主子单据开发技术。该课程涵盖了主子单据开发的整个流程,从创建元数据到生成主子单据...

    企业物料单据编码指南

    1. **单据类型代码**:首先根据单据的种类(如采购订单、销售发票、入库单等)进行分类。 2. **年份代码**:采用四位数字表示单据创建的年份,有助于归档管理。 3. **月份代码**:两位数字表示单据创建的月份。 4. *...

    金蝶通过变量选择单据转换规则.doc

    1)业务场景:在日常运营中,企业可能遇到各种各样的业务单据,如采购订单、销售订单等。这些单据可能需要转换成其他类型的单据,如入库单、出库单等。在不同的业务情况下,转换规则可能会有所不同。例如,普通采购...

    NC65开发课件之单据模板

    NC65开发课件之单据模板知识点总结 单据模板属性设置 在NC65开发环境中,单据模板属性设置是指对单据模板的各种属性的设置,包括页签项目编辑、页签属性、字段属性、模板设置和公式设置等。通过单据模板属性设置,...

    单据控件开发使用手册

    单据控件是用户界面中用于展示和编辑数据的一种特定元素,通常用于处理各种商业交易记录,如发票、订单、库存清单等。以下将详细阐述单据控件开发的关键知识点: 1. **控件定义**:在软件开发中,控件是用户界面的...

    U8 7.0 最大单据号检测修复工具

    [工具名称]: U8最大单据号检测修复工具 [详细说明]: 该工具可以针对系统数据库的最大单据号表进行检测修复工作。如果日常业务单据保存失败,或单据保存提示单据号冲突等,可以尝试使用该工具进行检测、修复操作。 ...

    NC65开发课件之单据开发进阶

    NC65开发课件之单据开发进阶 本资源主要介绍NC65单据开发进阶的相关知识点,包括环境信息获取、默认值设置、表头表尾编辑前后事件、金额合计、表体编辑前后事件、默认勾选、业务规则等。 环境信息获取: 环境信息...

    BOS单据外部调用接口

    3. **更新单据**:当单据状态发生变化,比如订单付款完成或发票已核销,外部系统可以通过接口发送更新请求,来同步BOS中的单据状态。 4. **删除单据**:在某些情况下,可能需要从BOS中删除不再需要的单据,接口提供...

    ccflow表单单据模式.docx

    3. **销售管理**:销售团队可以利用单据模式记录销售订单,对每个订单的状态进行实时更新,便于数据分析和决策。 总的来说,Ccflow的单据模式提供了一套全面的解决方案,满足了用户在不同业务环境中对数据管理的...

    海运班轮货代业务流程.pptx

    【海运班轮货代业务流程】是指在国际物流中,货代公司处理海运班轮货物运输的操作步骤。这个过程涉及到揽货接单、定舱、报关报检、货物进出港口、提单交换等多个环节。以下是对这些环节的详细解析: 1. **揽货接单*...

    nc57审批流单据配置(巨详细).doc

    NC57审批流单据配置详解 NC57审批流单据配置是指在NC57系统中配置审批流单据的详细步骤,包括动作脚本生成方法、如何设置审批检查类等内容。下面将详细介绍NC57审批流单据配置的步骤和相关知识点。 步骤一:单据...

Global site tag (gtag.js) - Google Analytics