Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def action_resync(self):
- for rec in self:
- def _prepare_invoice_data(self, models, db, uid, password, move, company_id):
- move_lines = []
- partner = False
- for line in move.invoice_line_ids:
- account = line.account_id
- account_name_to_check = account.name
- if account.substitute_account:
- account_name_to_check = account.substitute_account.name
- if account_name_to_check:
- account_id = self._map_account_name_to_remote_company(models, db, uid, password, company_id,
- account_name_to_check)
- remote_analytic_account_id = self._prepare_analytic_distribution(models, db, uid, password,
- line.analytic_account_id)
- tax_ids = [
- self._get_remote_tax_id(
- models, db, uid, password,
- 'account.tax', 'name', tax.name, move.company_id.id
- )
- for tax in line.tax_ids
- ]
- else:
- account_id = None
- remote_analytic_account_id = None
- tax_ids = []
- move_line_data = {
- 'account_id': account_id,
- 'name': line.name,
- 'analytic_distribution': {
- str(remote_analytic_account_id): 100} if remote_analytic_account_id else {} or None,
- 'quantity': line.quantity or None,
- 'price_unit': line.price_unit or None,
- 'tax_ids': [(4, tax) for tax in tax_ids] if tax_ids else None,
- 'display_type': line.display_type if line.display_type in ['line_section',
- 'line_note'] else 'product',
- }
- move_lines.append((0, 0, move_line_data))
- _logger.info(f"Move lines Update ******* {move_lines} ID {move.read(['name'])}")
- currency_id = self._get_remote_id_if_set(models, db, uid, password, 'res.currency', 'name',
- move.currency_id)
- # Ensure partner exists in remote database
- if move.partner_id:
- partner = self._get_remote_id_if_set(models, db, uid, password, 'res.partner', 'name',
- move.partner_id)
- if not partner:
- raise UserError('Partner {partner} from the source is not available!')
- move_data = {
- 'partner_id': self._get_remote_id_if_set(models, db, uid, password, 'res.partner', 'name',
- move.partner_id),
- 'patient': move.patient_id.name or None,
- 'payment_reference': move.payment_reference or None,
- 'ref': move.ref or None,
- 'invoice_date': move.invoice_date or None,
- 'invoice_date_due': move.invoice_date_due or None,
- 'invoice_origin': move.invoice_origin or None,
- 'narration': move.narration or None,
- 'currency_id': currency_id or None,
- 'invoice_line_ids': move_lines,
- }
- _logger.info(f"Moves Data -----------------------------------------\n {move_data}\n")
- return move_data
- # Get configuration parameters
- config_parameters = self.env['ir.config_parameter'].sudo()
- remote_type = config_parameters.get_param('remote_operations.remote_type')
- if remote_type != 'Main Database':
- _logger.info(
- "Database is not configured as 'Main Database'. Skipping sending account moves to remote.")
- return
- url = config_parameters.get_param('remote_operations.url')
- db = config_parameters.get_param('remote_operations.db')
- username = config_parameters.get_param('remote_operations.username')
- password = config_parameters.get_param('remote_operations.password')
- # Validate settings
- if not all([url, db, username, password]):
- raise ValidationError("Remote server settings must be fully configured (URL, DB, Username, Password)")
- # Create XML-RPC connection and send data
- common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url), allow_none=True)
- uid = common.authenticate(db, username, password, {})
- models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url), allow_none=True)
- start_date = date(2024, 7, 1).isoformat()
- account_move = models.execute_kw(db, uid, password, 'account.move', 'search',
- [[['name', '=', rec.ref], \
- ['state', '=', 'posted'], ['move_type', '!=', 'entry'],
- ['posted_to_remote', '=', True],
- ['branch_id', '=', rec.company_id.id],
- ['date', '>=', start_date], {'limit': 1, 'order': 'date asc'}]]),
- # _logger.info(
- # f"Account Data Moves to process......................\n {account_move.read(['name', 'posted_to_remote'])}")
- # if account_move:
- # move_data = _prepare_invoice_data(models, db, uid, password, account_move, account_move.company_id.id)
- # if move_data:
- # new_move = models.execute_kw(db, uid, password, 'account.move', 'write', [move_data])
- # _logger.info(
- # f"----------------------------Account Data Moves Update\n{new_move}\n-------------------------")
- # else:
- # raise UserError("Could not find related invoice from the source(No Invoice Data)..")
- # else:
- # raise UserError("Could not find related invoice from the source..")
Advertisement
Add Comment
Please, Sign In to add comment