Skip to content

Commit

Permalink
[UPD] ebisumart_connector
Browse files Browse the repository at this point in the history
  • Loading branch information
kanda999 committed Dec 22, 2023
1 parent 895226e commit ed75336
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 97 deletions.
2 changes: 1 addition & 1 deletion connector_ebisumart/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# from . import sale_order
# from . import automatic_workflow_job
from . import automatic_workflow_job
from . import stock_rule
from . import res_partner
from . import account_journal
Expand Down
92 changes: 63 additions & 29 deletions connector_ebisumart/models/automatic_workflow_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,66 @@
class AutomaticWorkflowJob(models.Model):
_inherit = 'automatic.workflow.job'

def _do_validate_sale_order(self, sale):
super()._do_validate_sale_order(sale)
# Search purchase order
po = self.env['purchase.order'].search([('origin', '=', sale.name)])
if not po:
return
# map data_planned
for line in po.order_line:
line.date_planned = po.date_order
# Confirm the purchase order.
po.button_confirm()
# Validate the associated receipt (stock picking).
for picking in po.picking_ids:
wiz = self.env['stock.immediate.transfer'].create(
{'pick_ids': [(4, picking.id)]}
)
wiz.process()

po_invoice = {
'partner_id': po.partner_id.id,
'account_id': po.partner_id.property_account_payable_id.id,
'state': 'draft',
'type': 'in_invoice',
'purchase_id': po.id,
}

inv = self.env['account.invoice'].create(po_invoice)
inv.purchase_order_change()
inv.action_invoice_open()
def _do_validate_picking(self, picking):
super()._do_validate_picking(picking)
if picking.sale_id.cancel_in_ebisumart:
self.create_return_picking(picking.sale_id)
purchase_order = self.env['purchase.order'].search([('origin', '=', picking.sale_id.name)])
if not purchase_order:
return
self.create_return_picking(purchase_order)

def _do_validate_invoice(self, invoice):
super()._do_validate_invoice(invoice)
sale_order = invoice.mapped('invoice_line_ids.sale_line_ids.order_id')
if sale_order.cancel_in_ebisumart:
self.create_credit_note(sale_order, invoice_type="out_invoice")
purchase_order = self.env['purchase.order'].search([('origin', '=', sale_order.name)])
if not purchase_order:
return
self.create_credit_note(purchase_order, invoice_type="in_invoice")

def create_return_picking(self, order):
for picking in order.picking_ids.filtered(lambda r: r.state == 'done'):
# Create the reverse transfer
stock_return_picking = self.env['stock.return.picking']
return_wizard = stock_return_picking.with_context(
active_ids=picking.ids, active_id=picking.ids[0]
).create({})
return_result = return_wizard.create_returns()

# Usually, the return_result contains information
# about the newly created return picking(s)
if return_result and 'res_id' in return_result:
new_return_picking = self.env['stock.picking'].browse(
return_result['res_id']
)

# Validate (confirm) the return picking
if new_return_picking.state != 'done':
wiz = self.env['stock.immediate.transfer'].create({
'pick_ids': [(4, new_return_picking.id)]
})
wiz.process()

def create_credit_note(self, order, invoice_type):
for invoice in order.invoice_ids.filtered(
lambda r: r.state not in ['cancel', 'draft'] and r.type == invoice_type
):
# Create the refund (credit note)
account_invoice_refund = self.env['account.invoice.refund']
refund_wizard = account_invoice_refund.create({
'description': 'Credit Note',
'filter_refund': 'refund', # refund the entire invoice
})
refund_result = refund_wizard.with_context(
active_ids=invoice.ids
).invoice_refund()
if refund_result and refund_result.get('domain'):
# Search for the newly created credit note
credit_notes = self.env['account.invoice'].search(
refund_result.get('domain')
)
for credit_note in credit_notes:
if credit_note.state == 'draft':
credit_note.action_invoice_open()
4 changes: 3 additions & 1 deletion connector_ebisumart/models/ebisumart_sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def after_import(self, ebisumart_record, backend_record):
}
inv = self.env['account.invoice'].create(po_invoice)
inv.purchase_order_change()
# Recalculate the invoice lines.
inv._onchange_invoice_line_ids()
inv.action_invoice_open()
# Force assign scheduled_date
# TODO put this line after action_confirm()
Expand Down Expand Up @@ -146,6 +148,6 @@ def read(self, external_id, attributes=None):
'ORDER_NO', 'KESSAI_ID', 'ORDER_DISP_NO', 'SEND_DATE',
'order_details(ORDER_D_NO, ITEM_ID, ITEM_NAME, QUANTITY, '
'TEIKA, SHIRE_PRICE)',
'REGIST_DATE', 'UPDATE_DATE', 'COUPON_WARIBIKI',
'REGIST_DATE', 'UPDATE_DATE', 'COUPON_WARIBIKI', 'CANCEL_DATE',
]
return super().read(f"/orders/{external_id}", attributes=attributes)
72 changes: 6 additions & 66 deletions connector_ebisumart/models/ebisumart_sale_order_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def workflow_process_id(self, record):
@mapping
def backend_id(self, record):
return {'backend_id': self.backend_record.id}

@mapping
def cancel_in_ebisumart(self, record):
if record.get('CANCEL_DATE'):
return {'cancel_in_ebisumart': True}
return {'cancel_in_ebisumart': False}


class SaleOrderLineMapper(Component):
Expand Down Expand Up @@ -104,50 +110,6 @@ class SaleOrderBatchImporter(Component):
_inherit = 'ebisumart.delayed.batch.importer'
_apply_on = ['ebisumart.sale.order']

def create_return_picking(self, order):
for picking in order.picking_ids.filtered(lambda r: r.state == 'done'):
# Create the reverse transfer
stock_return_picking = self.env['stock.return.picking']
return_wizard = stock_return_picking.with_context(
active_ids=picking.ids, active_id=picking.ids[0]
).create({})
return_result = return_wizard.create_returns()

# Usually, the return_result contains information
# about the newly created return picking(s)
if return_result and 'res_id' in return_result:
new_return_picking = self.env['stock.picking'].browse(
return_result['res_id']
)

# Validate (confirm) the return picking
if new_return_picking.state != 'done':
wiz = self.env['stock.immediate.transfer'].create({
'pick_ids': [(4, new_return_picking.id)]
})
wiz.process()

def create_credit_note(self, order, invoice_type):
for invoice in order.invoice_ids.filtered(
lambda r: r.state not in ['cancel', 'draft'] and r.type == invoice_type
):
# Create the refund (credit note)
account_invoice_refund = self.env['account.invoice.refund']
refund_wizard = account_invoice_refund.create({
'description': 'Credit Note',
'filter_refund': 'refund', # refund the entire invoice
})
refund_result = refund_wizard.with_context(
active_ids=invoice.ids
).invoice_refund()
if refund_result and refund_result.get('domain'):
# Search for the newly created credit note
credit_notes = self.env['account.invoice'].search(
refund_result.get('domain')
)
for credit_note in credit_notes:
if credit_note.state == 'draft':
credit_note.action_invoice_open()

def run(self, filters=None):
""" Run the synchronization """
Expand All @@ -159,33 +121,11 @@ def run(self, filters=None):
and order.get('SEND_DATE')
and order.get('FREE_ITEM1')
and not order.get('IS_TEIKI_HEADER_FLG')
and not order.get('CANCEL_DATE')
]
cancel_ids = [
order["ORDER_NO"]
for order in external_datas
if order.get('CANCEL_DATE')
]
for external_id in cancel_ids:
binder = self.binder_for('ebisumart.sale.order')
sale_order = binder.to_internal(external_id, unwrap=True)
if sale_order:
if sale_order.cancel_in_ebisumart:
continue
self.create_return_picking(sale_order)
self.create_credit_note(sale_order, invoice_type="out_invoice")
purchase_order = self.env['purchase.order'].search(
[('origin', '=', sale_order.name)]
)
if purchase_order:
self.create_return_picking(purchase_order)
self.create_credit_note(purchase_order, invoice_type="in_invoice")
sale_order.write({"cancel_in_ebisumart": True})

for external_id in external_ids:
self._import_record(external_id)


class EbisumartSaleOrderImporter(Component):
_name = 'ebisumart.sale.order.importer'
_inherit = 'ebisumart.importer'
Expand Down

0 comments on commit ed75336

Please sign in to comment.