lib/kashflow_api/models/supplier.rb in kashflow_api-0.0.3 vs lib/kashflow_api/models/supplier.rb in kashflow_api-0.1.0
- old
+ new
@@ -1,62 +1,45 @@
module KashflowApi
- class Supplier < KashflowApi::SoapObject
- def self.find(search)
- self.find_by_supplier_code(search)
- end
+ class Supplier < KashflowApi::SoapObject
+ Keys = [
+ "Code", "Name", "Contact", "Telephone", "Mobile", "Fax", "Email", "Address1", "Address2", "Address3", "Address4", "Postcode", "Website", "VATNumber", "Notes"
+ ]
+
+ Finds = [ "code", "id" ]
+
+ KFObject = { singular: "supplier", plural: "suppliers" }
+
+ XMLKey = "SupplierID"
+
+ def self.build_arguments(action, object, field, argument)
+ if action == "get"
+ expects argument, String
+ return "<Supplier#{field}>#{argument}</Supplier#{field}>" if object == "supplier"
+ return "<#{field}>#{argument}</#{field}>" if object == "suppliers"
+ elsif action == "update"
+ expects argument, KashflowApi::Supplier
+ return "<sup>#{argument.to_xml}</sup>" if object == "supplier"
+ elsif action == "insert"
+ expects argument, KashflowApi::Supplier
+ return "<supl>#{argument.to_xml}</supl>" if object == "supplier"
+ end
+ end
+
+ def save
+ if @hash["SupplierID"] == "0"
+ insert_supplier
+ else
+ update_supplier
+ end
+ end
+
+ private
+
+ def update_supplier
+ KashflowApi.api.update_supplier(self)
+ end
- def self.find_by_supplier_code(search)
- result = KashflowApi.api.get_supplier(search)
- self.build_from_soap(result.hash[:envelope][:body][:get_supplier_response][:get_supplier_result])
- end
-
- def self.find_by_supplier_id(search)
- result = KashflowApi.api.get_supplier_by_id(search)
- self.build_from_soap(result.hash[:envelope][:body][:get_supplier_by_id_response][:get_supplier_by_id_result])
- end
-
- def self.all
- result = KashflowApi.api.get_suppliers
- suppliers = []
- result.hash[:envelope][:body][:get_suppliers_response][:get_suppliers_result][:supplier].each do |supplier|
- suppliers.push self.build_from_soap supplier
- end
- suppliers.sort { |x, y| x.name <=> y.name }
- end
-
- def save
- if @hash["SupplierID"] == "0"
- insert_supplier
- else
- update_supplier
- end
- end
-
- def to_xml
- xml = []
- id_line = ""
- @hash.keys.each do |key|
- if key == "SupplierID"
- id_line = "<#{key}>#{@hash[key]}</#{key}>" unless @hash[key] == "0"
- else
- xml.push("<#{key}>#{@hash[key]}</#{key}>")
- end
- end
- [id_line, xml.join].join
- end
-
- private
-
- def blank_object_hash
- {"Code" => "", "Name" => "", "Contact" => "", "Telephone" => "", "Mobile" => "", "Fax" => "", "Email" => "", "Address1" => "", "Address2" => "",
- "Address3" => "", "Address4" => "", "Postcode" => "", "Website" => "", "VATNumber" => "", "Notes" => ""}.merge(KashflowApi::Supplier.find("").hash)
- end
-
- def update_supplier
- KashflowApi.api.update_supplier(self)
- end
-
- def insert_supplier
- KashflowApi.api.insert_supplier(self)
- end
+ def insert_supplier
+ KashflowApi.api.insert_supplier(self)
end
+ end
end
\ No newline at end of file