0

我正在使用 Zoho Books API 创建采购订单

https://www.zoho.com/books/api/v3/#Purchase-Order_Create_a_purchase_order)- _

唯一需要的项目似乎是vendor_idline_items。但是,在创建工作后,我收到以下消息:

ZohoBooks::BadRequestError: Purchase order cannot be created for a non-purchase item.

这是代码:

```

class CreatePurchaseOrderJob < ApplicationJob
  queue_as :default

  def perform(order_id)
    @order = SupplierOrder.find(order_id)
    create_purchase_order
  end

  private

  attr_reader :order

  def vendor
    name = order.supplier.name
    books.get_contact_with_options(contact_type: :vendor, contact_name: name)
  end

  def create_purchase_order
    payload = {
      vendor_id: vendor['contact_id'],
      purchaseorder_number: order.reference,
      reference_number: order.reference,
      line_items: line_items
    }

    books.create_purchase_order(payload)
  end

  def line_items
    order.rfq_line_item_prices.order(:id).map do |price|
      line_item(price)
    end
  end

  def line_item(price)
    {
      name: price.current_line_item.shape,
      description: price.current_line_item&.name,
      bcy_rate: price.unit_price.to_f,
      rate: price.unit_price.to_f,
      quantity: price.current_line_item.quantity,
      tax_id: Registry.zoho_vat_tax_id,
      item_custom_fields: [
        { label: 'Grade', value: price.current_line_item&.grade },
        { label: 'Finish', value: price.current_line_item&.finish },
        { label: 'Dimensions', value: price.current_line_item&.dimensions }
      ]
    }
  end

  def books
    @books ||= Registry.books
  end
end

这是我们用来制作发票的相同代码并且有效,所以我错过了一些神奇的东西,让 Zoho 知道这是一个购买项目,没有任何迹象表明它可能是什么。我通过聊天询问并得到以下信息: The reason why you're getting this error message is because, You can create a purchase order only when you add the purchase info for an item.

4

1 回答 1

2

所以答案比我想象的要容易,尽管它花费的时间比我想发现的要长。最终我决定尝试添加属性,即使它们被定义为可选。通过包含项目 ID,它允许创建采购订单。

我的最终代码:

class CreatePurchaseOrderJob < ApplicationJob
  queue_as :default

  def perform(order_id)
    @order = SupplierOrder.find(order_id)
    create_purchase_order
  end

  private

  def payload
    {
      vendor_id: vendor['contact_id'],
      reference_number: order&.reference,
      line_items: line_items
    }
  end

  attr_reader :order

  def vendor
    @vendor ||= books.find_or_create_vendor(order.supplier)
  end

  def create_purchase_order
    begin
      books.create_purchase_order(payload)
    rescue Exception => e
      Airbrake.notify(e)
    end
  end

  def line_items
    order.line_item_prices.order(:id).map do |price|
      line_item(price)
    end
  end

  def item_id(name)
    books.find_or_create_item(name)['item_id']
  end

  def line_item(price)
    {
      item_id: item_id(price.current_line_item.shape),
      description: price.current_line_item&.description,
      bcy_rate: price.unit_price.to_f,
      rate: price.unit_price.to_f,
      quantity: price.current_line_item.quantity,
      tax_id: Registry.vat_tax_id,
      item_custom_fields: [
        { label: 'Grade', value: price.current_line_item&.grade },
        { label: 'Finish', value: price.current_line_item&.finish },
        { label: 'Dimensions', value: price.current_line_item&.dimensions }
      ]
    }
  end

  def books
    @books ||= Registry.books
  end
end
于 2018-07-06T09:24:05.427 回答