Commit 6b9e956c authored by Romain Courteaud's avatar Romain Courteaud

slapos_accounting: wip: create from multiple invoices

using Entity_getOutstandingAmountList
parent bf0ad382
from Products.ERP5Type.Message import translateString
from zExceptions import Unauthorized from zExceptions import Unauthorized
if REQUEST is not None: if REQUEST is not None:
raise Unauthorized raise Unauthorized
...@@ -7,72 +6,80 @@ portal = context.getPortalObject() ...@@ -7,72 +6,80 @@ portal = context.getPortalObject()
if not invoice_list: if not invoice_list:
raise ValueError('You need to provide at least one Invoice transaction') raise ValueError('You need to provide at least one Invoice transaction')
# For now consider a single value is passed, in future we intend to create activate_kw = {
# a single payment per invoice. 'tag': 'Entity_createPaymentTransaction_%s' % context.getUid()
current_invoice = invoice_list[0] }
payment_tag = "sale_invoice_transaction_create_payment_%s" % current_invoice.getUid()
if context.REQUEST.get(payment_tag, None) is not None:
raise ValueError('This script was already called twice on the same transaction ')
if current_invoice.SaleInvoiceTransaction_isLettered(): # Ensure all invoice use the same arrow and resource
raise ValueError('This invoice is already lettered') first_invoice = invoice_list[0]
identical_dict = {
'getSource': first_invoice.getSource(),
'getSourceSection': first_invoice.getSourceSection(),
'getSourcePayment': first_invoice.getSourcePayment(),
'getDestination': first_invoice.getDestination(),
'getDestinationSection': first_invoice.getDestinationSection(),
'getPriceCurrency': first_invoice.getPriceCurrency(),
'getLedger': first_invoice.getLedger(),
}
context.serialize() price = 0
quantity = 0 causality_uid_list = []
for movement in current_invoice.searchFolder( # Check that all invoice matches
portal_type='Sale Invoice Transaction Line', for invoice in invoice_list:
default_source_uid=[i.uid for i in context.Base_getReceivableAccountList()]): for method_id, method_value in identical_dict.items():
quantity += movement.getQuantity() if getattr(invoice, method_id)() != method_value:
raise ValueError('Invoices do not match on method: %s' % method_id)
if invoice.total_price:
price += invoice.total_price
causality_uid_list.append(invoice.payment_request_uid)
if invoice.SaleInvoiceTransaction_isLettered():
raise ValueError('This invoice is already lettered')
if quantity >= 0: if not price:
raise ValueError('You cannot generate Payment Transaction for zero or negative amounts.') raise ValueError('No total_price to pay')
if first_invoice.getDestinationSection() != context.getRelativeUrl():
raise ValueError('Invoice not related to the context')
current_payment = portal.accounting_module.newContent( # create the payment transaction
portal_type="Payment Transaction", payment_transaction = portal.accounting_module.newContent(
causality=current_invoice.getRelativeUrl(), portal_type='Payment Transaction',
source_section=current_invoice.getSourceSection(), created_by_builder=True,
source_project=current_invoice.getSourceProject(), causality_uid_set=causality_uid_list,
destination_section=current_invoice.getDestinationSection(), source_section=first_invoice.getSourceSection(),
destination_project=current_invoice.getDestinationProject(), source_payment=first_invoice.getSourcePayment(),
resource=current_invoice.getResource(), destination_section=first_invoice.getDestinationSection(),
price_currency=current_invoice.getResource(), destination_section_value=context,
specialise=current_invoice.getSpecialise(), start_date=DateTime(),
payment_mode=current_invoice.getPaymentMode(), #payment_mode=,
ledger=current_invoice.getLedger(), #specialise
start_date=current_invoice.getStartDate(), ledger=first_invoice.getLedger(),
stop_date=current_invoice.getStopDate(), resource=first_invoice.getResource(),
source_payment=current_invoice.getSourcePayment(), destination_administration=destination_administration,
# Workarround to not create default lines. activate_kw=activate_kw
created_by_builder=1
) )
current_payment.newContent( getAccountForUse = context.Base_getAccountForUse
portal_type="Accounting Transaction Line",
quantity=-1 * quantity,
source='account_module/receivable',
destination='account_module/payable',
start_date=current_invoice.getStartDate(),
stop_date=current_invoice.getStopDate())
current_payment.newContent( collection_account = getAccountForUse('collection')
portal_type="Accounting Transaction Line", payment_transaction.newContent(
quantity=1 * quantity, id='bank',
source='account_module/payment_to_encash', portal_type='Accounting Transaction Line',
destination='account_module/payment_to_encash', source_value=collection_account,
start_date=current_invoice.getStartDate(), destination_value=collection_account,
stop_date=current_invoice.getStopDate()) quantity=-price,
activate_kw=activate_kw,
comment = translateString("Initialised by Entity_createPaymentTransaction.") )
# Reindex with a tag to ensure that there will be no generation while the object isn't
# reindexed.
payment_tag ="sale_invoice_transaction_create_payment_%s" % current_invoice.getUid()
current_payment.activate(tag=payment_tag).immediateReindexObject()
comment = translateString("Initialised by Entity_createPaymentTransaction.")
current_payment.PaymentTransaction_start(comment=comment)
# Set a flag on the request for prevent 2 calls on the same transaction for index, line in enumerate(invoice_list):
context.REQUEST.set(payment_tag, 1) if line.total_price:
payment_transaction.newContent(
id="receivable%s" % index,
portal_type='Accounting Transaction Line',
source=line.node_relative_url,
destination_value=getAccountForUse('payable'),
quantity=line.total_price,
activate_kw=activate_kw,
)
return current_payment payment_transaction.stop()
return payment_transaction
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <key> <string>_params</string> </key>
<value> <string>invoice_list, REQUEST=None</string> </value> <value> <string>invoice_list, destination_administration=None, REQUEST=None</string> </value>
</item> </item>
<item> <item>
<key> <string>id</string> </key> <key> <string>id</string> </key>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment