我正在使用Zoho创建一个采购订单
(order) -
唯一需要的项目似乎是vendor_id和line_items。但是,在创建了一个职务之后,我得到了以下消息:
ZohoBooks::BadRequestError: Purchase order cannot be created for a non-purchase item.
这是密码:
```javascript类CreatePurchaseOrderJob < ApplicationJob
queue_as :默认
def执行(Order_id)
@order = SupplierOrder.find(order_id)create_purchase_order结束
私有
attr_reader :订单
def供应商
name = order.supplier.namebooks.get_contact_with_options(contact_type: :vendor, contact_name: name)结束
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)结束
def line_items
order.rfq_line_item_prices.order(:id).map do |price| line_item(price)end结束
def line_item(价格)
{ 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 } ]}结束
def书籍
@books ||= Registry.books结束
结束
这是相同的代码,我们使用的发票和工作,所以我错过了一些神奇的东西,让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.
发布于 2018-07-06 09:24:05
因此,答案比我想象的要容易,尽管它花了我想要的更长的时间。最后,我决定尝试添加属性,即使它们被定义为可选属性。通过包含项目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
endhttps://stackoverflow.com/questions/51079139
复制相似问题