summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/coretax_fatur.py
diff options
context:
space:
mode:
authorit-fixcomart <it@fixcomart.co.id>2025-07-28 15:09:55 +0700
committerit-fixcomart <it@fixcomart.co.id>2025-07-28 15:09:55 +0700
commitd15ce4e186e2b77f01e8dfd03886298cc733d4c1 (patch)
tree1b32a4c29c4fcea85070fcecb5b77a7d55d30029 /indoteknik_custom/models/coretax_fatur.py
parentdeba962d7368a5c4e30441b5a640102608e3dde6 (diff)
parent36a53535dbdc5777266fd9276b4c557259dab6be (diff)
<hafid> merging odoo-backup
Diffstat (limited to 'indoteknik_custom/models/coretax_fatur.py')
-rw-r--r--indoteknik_custom/models/coretax_fatur.py90
1 files changed, 65 insertions, 25 deletions
diff --git a/indoteknik_custom/models/coretax_fatur.py b/indoteknik_custom/models/coretax_fatur.py
index 92ff1a72..ce94306f 100644
--- a/indoteknik_custom/models/coretax_fatur.py
+++ b/indoteknik_custom/models/coretax_fatur.py
@@ -3,6 +3,9 @@ import xml.etree.ElementTree as ET
from xml.dom import minidom
import base64
import re
+import logging
+
+_logger = logging.getLogger(__name__)
class CoretaxFaktur(models.Model):
@@ -32,7 +35,7 @@ class CoretaxFaktur(models.Model):
return cleaned_number
- def generate_xml(self, invoices=None):
+ def generate_xml(self, invoices=None, down_payments=False):
# Buat root XML
root = ET.Element('TaxInvoiceBulk', {
'xmlns:xsi': "http://www.w3.org/2001/XMLSchema-instance",
@@ -72,59 +75,96 @@ class CoretaxFaktur(models.Model):
ET.SubElement(tax_invoice, 'BuyerEmail').text = invoice.partner_id.email or ''
ET.SubElement(tax_invoice, 'BuyerIDTKU').text = buyerIDTKU
- # Filter product
- product_lines = invoice.invoice_line_ids.filtered(
- lambda l: not l.display_type and hasattr(l, 'account_id') and
- l.account_id and l.product_id and
- l.account_id.id != self.DISCOUNT_ACCOUNT_ID and
- l.quantity != -1
- )
+ _logger.info(" invoice down_payments: %s", invoice.down_payment)
+ # Handle product lines based on down_payments flag
+ if invoice.down_payment and invoice.invoice_origin:
+ # Get from sale.order.line for down payment
+ sale_order = invoice.sale_id
+ if sale_order:
+ product_lines = sale_order.order_line.filtered(
+ lambda l: l.product_id and not l.is_downpayment and not l.display_type and not l.product_id.id == 229625
+ )
+ # Convert sale order lines to invoice-like format
+ converted_lines = []
+ for line in product_lines:
+ converted_lines.append({
+ 'name': line.name,
+ 'product_id': line.product_id,
+ 'price_subtotal': line.price_subtotal,
+ 'quantity': line.product_uom_qty,
+ 'price_unit': line.price_unit,
+ 'account_id': line.order_id.analytic_account_id or False,
+ })
+ product_lines = converted_lines
+ else:
+ product_lines = []
+ else:
+ # Normal case - get from invoice lines
+ product_lines = invoice.invoice_line_ids.filtered(
+ lambda l: not l.display_type and hasattr(l, 'account_id') and
+ l.account_id and l.product_id and
+ l.account_id.id != self.DISCOUNT_ACCOUNT_ID and
+ l.quantity != -1
+ )
- # Filter discount
+ # Filter discount (always from invoice)
discount_lines = invoice.invoice_line_ids.filtered(
lambda l: not l.display_type and (
- (hasattr(l, 'account_id') and l.account_id and
- l.account_id.id == self.DISCOUNT_ACCOUNT_ID) or
- (l.quantity == -1)
+ (hasattr(l, 'account_id') and l.account_id and
+ l.account_id.id == self.DISCOUNT_ACCOUNT_ID) or
+ (l.quantity == -1)
)
)
- # Calculate total product amount (before discount)
- total_product_amount = sum(line.price_subtotal for line in product_lines)
+ # Calculate totals
+ total_product_amount = sum(line.get('price_subtotal', 0) if isinstance(line, dict)
+ else line.price_subtotal for line in product_lines)
if total_product_amount == 0:
total_product_amount = 1 # Avoid division by zero
- # Calculate total discount amount
total_discount_amount = abs(sum(line.price_subtotal for line in discount_lines))
# Tambahkan elemen ListOfGoodService
list_of_good_service = ET.SubElement(tax_invoice, 'ListOfGoodService')
for line in product_lines:
+ # Handle both dict (converted sale lines) and normal invoice lines
+ if isinstance(line, dict):
+ line_price_subtotal = line['price_subtotal']
+ line_quantity = line['quantity']
+ line_name = line['name']
+ line_price_unit = line['price_unit']
+ else:
+ line_price_subtotal = line.price_subtotal
+ line_quantity = line.quantity
+ line_name = line.name
+ line_price_unit = line.price_unit
+
# Calculate prorated discount
- line_proportion = line.price_subtotal / total_product_amount
+ line_proportion = line_price_subtotal / total_product_amount
line_discount = total_discount_amount * line_proportion
- # unit_price = line.price_unit
- subtotal = line.price_subtotal
- quantity = line.quantity
+ subtotal = line_price_subtotal
+ quantity = line_quantity
total_discount = round(line_discount, 2)
# Calculate other tax values
otherTaxBase = round(subtotal * (11 / 12), 2) if subtotal else 0
vat_amount = round(otherTaxBase * 0.12, 2)
+ price_per_unit = round(subtotal / quantity, 2) if quantity else 0
+
# Create the line in XML
good_service = ET.SubElement(list_of_good_service, 'GoodService')
ET.SubElement(good_service, 'Opt').text = 'A'
ET.SubElement(good_service, 'Code').text = '000000'
- ET.SubElement(good_service, 'Name').text = line.name
+ ET.SubElement(good_service, 'Name').text = line_name
ET.SubElement(good_service, 'Unit').text = 'UM.0018'
- ET.SubElement(good_service, 'Price').text = str(round(subtotal / quantity, 2)) if subtotal else '0'
+ # ET.SubElement(good_service, 'Price').text = str(round(line_price_unit, 2)) if line_price_unit else '0'
+ ET.SubElement(good_service, 'Price').text = str(price_per_unit)
ET.SubElement(good_service, 'Qty').text = str(quantity)
ET.SubElement(good_service, 'TotalDiscount').text = str(total_discount)
- ET.SubElement(good_service, 'TaxBase').text = str(
- round(subtotal)) if subtotal else '0'
+ ET.SubElement(good_service, 'TaxBase').text = str(round(subtotal)) if subtotal else '0'
ET.SubElement(good_service, 'OtherTaxBase').text = str(otherTaxBase)
ET.SubElement(good_service, 'VATRate').text = '12'
ET.SubElement(good_service, 'VAT').text = str(vat_amount)
@@ -136,9 +176,9 @@ class CoretaxFaktur(models.Model):
xml_pretty = minidom.parseString(xml_str).toprettyxml(indent=" ")
return xml_pretty
- def export_to_download(self, invoices):
+ def export_to_download(self, invoices, down_payments):
# Generate XML content
- xml_content = self.generate_xml(invoices)
+ xml_content = self.generate_xml(invoices, down_payments)
# Encode content to Base64
xml_encoded = base64.b64encode(xml_content.encode('utf-8'))