Example #1
0
def set_order_as_complete_in_magento(magento_order):
	magento_order_dict = {
		"entity_id": magento_order.get("entity_id"),
		"status": "complete"
	}

	post_request("rest/V1/orders", {"entity": magento_order_dict})
def sync_erpnext_item_attribute_values(erpnext_item_attribute, magento_item_attribute):
	magento_item_attribute_value_list = []
	
	for erpnext_item_attribute_value in erpnext_item_attribute.item_attribute_values:
		if not erpnext_item_attribute_value.get("magento_item_attribute_value_id"):
			magento_item_attribute_value_dict = {
				"label": erpnext_item_attribute_value.get("attribute_value")
			}

			try:
				request_response = post_request(f'products/attributes/{erpnext_item_attribute.magento_item_attribute_id}/options',
					{"option": magento_item_attribute_value_dict})
				
				erpnext_item_attribute_value.magento_item_attribute_value_id = request_response.strip("id_")
				erpnext_item_attribute_value.save()
				frappe.db.commit()

			except Exception as e:
				make_magento_log(title=f'Cannot sync item attribute value "{erpnext_item_attribute_value.attribute_value}" from item attribute "{erpnext_item_attribute.attribute_name}"',
					status="Error", method="sync_erpnext_item_attribute_values", message=e,	request_data=magento_item_attribute_value_dict, exception=True)				
		
		magento_item_attribute_value_dict = {
			"label": erpnext_item_attribute_value.attribute_value,
			"value": erpnext_item_attribute_value.magento_item_attribute_value_id
		}

		magento_item_attribute_value_list.append(magento_item_attribute_value_dict)

	magento_item_attribute_dict = {
		"attribute_id": erpnext_item_attribute.magento_item_attribute_id, 
		"options": magento_item_attribute_value_list
	}

	put_request(f'products/attributes/{erpnext_item_attribute.magento_item_attribute_id}', {"attribute": magento_item_attribute_dict})
Example #3
0
def sync_erpnext_orders(erpnext_order_list):
	for erpnext_delivery_note in get_erpnext_delivery_notes():
		magento_shipment_dict = {
			"items": get_erpnex_delivery_note_items(erpnext_delivery_note.get("delivery_note_name")),
			"notify": 1
		}

		try:
			request_response = post_request(f'rest/V1/order/{erpnext_delivery_note.get("magento_order_id")}/ship', magento_shipment_dict)

			save_magento_properties_to_erpnext(erpnext_delivery_note, request_response)

			# set_order_as_complete_in_magento(magento_order)

			if erpnext_delivery_note.get("sales_order_name") not in erpnext_order_list:
				erpnext_order_list.append(erpnext_delivery_note.get("sales_order_name"))

		except Exception as e:
			make_magento_log(title=e.message, status="Error", method="sync_erpnext_orders", message=frappe.get_traceback(),
				request_data=magento_shipment_dict, exception=True)
def update_item_to_magento(erpnext_item):
	magento_item_dict = {
		"id": erpnext_item.get("magento_product_id") or "",
		"sku": erpnext_item.get("magento_sku") or erpnext_item.get("item_name").replace(" ", "").replace(":", "-").replace("/", "-"),
		"name": erpnext_item.get("item_name"),
		"attribute_set_id": get_magento_item_attribute_set_id_by_name(erpnext_item.get("magento_attribute_set_name")),
		"status": convert_magento_status_to_boolean(erpnext_item.get("magento_status")),
		"weight": 1,
		"extension_attributes": {
			"website_ids": get_magento_website_ids_list(erpnext_item),
			"category_links": get_magento_category_ids_list(erpnext_item)
		},
		"custom_attributes": [
			{
    			"attribute_code": "description",
        		"value": erpnext_item.get("magento_description") or ""
        	}
		]
	}

	if erpnext_item.get("has_variants"): 
		magento_item_dict.update({
			"type_id": "configurable",
			"price": 0})
		magento_item_dict["extension_attributes"].update({"configurable_product_options": get_magento_configurable_product_options(erpnext_item)})
		magento_item_dict["extension_attributes"].update({"configurable_product_links": get_magento_configurable_product_variant_links(erpnext_item)})	
	
	elif erpnext_item.get("variant_of"):
		magento_item_dict.update({
			"type_id": "simple",
			"visibility": 1,
			"price": get_magento_default_item_price(erpnext_item)
		})
		magento_item_dict["custom_attributes"].extend(get_magento_variant_product_attributes(erpnext_item))
		
	else:
		magento_item_dict.update({
			"type_id": "simple",
			"price": get_magento_default_item_price(erpnext_item)
		})

	try:
		if not erpnext_item.get("magento_product_id"):
			del magento_item_dict["id"]

			request_response = post_request("rest/all/V1/products", {"product": magento_item_dict})

			save_new_magento_properties_to_erpnext(erpnext_item.get("name"), request_response)
			erpnext_item["magento_sku"] = magento_item_dict.get("sku")

			if not erpnext_item.get("has_variants"):
				update_item_prices_to_magento(erpnext_item)

		else:
			post_request("rest/all/V1/products", {"product": magento_item_dict})

			erpnext_item["magento_sku"] = magento_item_dict.get("sku")

			if not erpnext_item.get("has_variants"):
				update_item_prices_to_magento(erpnext_item)

	except Exception as e:
		if e.args[0] and e.args[0].startswith("404"):
			erpnext_item.magento_item_id = ""
			erpnext_item.sync_with_magento = 0
			erpnext_item.flags.ignore_mandatory = True
			erpnext_item.save()

		exception_title = f'Failed to sync item "{erpnext_item.get("item_name")}".'
		make_magento_log(title=exception_title, status="Error", method="sync_magento_items", message=frappe.get_traceback(),
						request_data={"product": magento_item_dict}, exception=True)
		raise