def test_so_billed_amount_against_return_entry(self): from erpnext.accounts.doctype.sales_invoice.sales_invoice import make_sales_return so = make_sales_order(do_not_submit=True) so.submit() si = make_sales_invoice(so.name) si.insert() si.submit() si1 = make_sales_return(si.name) si1.update_billed_amount_in_sales_order = 1 si1.submit() so.load_from_db() self.assertEquals(so.per_billed, 0)
def create_sales_return(shop_name: str, shopify_order_id: int, shopify_financial_status: str, sales_invoice: "SalesInvoice"): """ Create a Sales Invoice return for the given Shopify order. Args: shop_name (str): The name of the Shopify configuration for the store. shopify_order_id (int): The Shopify order ID. shopify_financial_status (str): The financial status of the Shopify order. Should be one of: refunded, partially_refunded. sales_invoice (SalesInvoice): The Sales Invoice document. Returns: SalesInvoice: The Sales Invoice return document. If no refunds are found, returns None. """ shopify_settings: "ShopifySettings" = frappe.get_doc( "Shopify Settings", shop_name) refunds = shopify_settings.get_refunds(order_id=shopify_order_id) refund_dates = [ refund.processed_at or refund.created_at for refund in refunds if refund.processed_at or refund.created_at ] if not refund_dates: return refund_datetime = min([get_datetime(date) for date in refund_dates]) if not refund_datetime: return return_invoice: "SalesInvoice" = make_sales_return(sales_invoice.name) return_invoice.set_posting_time = True return_invoice.posting_date = refund_datetime.date() return_invoice.posting_time = refund_datetime.time() if shopify_financial_status == "partially_refunded": for refund in refunds: refunded_items = [ item.line_item.product_id for item in refund.refund_line_items if item.line_item.product_id ] refunded_variants = [ item.line_item.variant_id for item in refund.refund_line_items if item.line_item.variant_id ] for item in return_invoice.items: # for partial refunds, check each item for refunds shopify_product_id = frappe.db.get_value( "Item", item.item_code, "shopify_product_id") shopify_variant_id = frappe.db.get_value( "Item", item.item_code, "shopify_variant_id") if shopify_product_id in refunded_items or shopify_variant_id in refunded_variants: continue # set item values for non-refunded items to zero; # preferring this over removal of the item to avoid zero-item # refunds and downstream effects for other documents item.qty = 0 item.discount_percentage = 100 # add any additional adjustments as charges return_invoice.set("taxes", []) adjustments = refund.order_adjustments for adjustment in adjustments: return_invoice.append( "taxes", { "charge_type": "Actual", "account_head": get_tax_account_head( shop_name, "refund"), "description": adjustment.reason, "tax_amount": flt(adjustment.amount) }) return_invoice.save() return_invoice.submit() return return_invoice
def make_sales_return(source_name, target_doc=None): from erpnext.accounts.doctype.sales_invoice.sales_invoice import make_sales_return sales_return = make_sales_return(source_name, target_doc) return _prepend_returned_si(sales_return)