def _register_payments(self, payment_filter):
        invoice_obj = self.env['account.invoice']
        invoices = invoice_obj.search(payment_filter)
        _logger.debug('Invoices to Register Payment: %s', invoices.ids)
        for invoice in invoices:
            partner_type = invoice.type in ('out_invoice', 'out_refund') and \
                'customer' or 'supplier'
            payment_mode = invoice.payment_mode_id

            if not payment_mode.fixed_journal_id:
                _logger.debug('Unable to Register Payment for invoice %s: '
                              'Payment mode %s must have fixed journal',
                              invoice.id, payment_mode.id)
                return

            with savepoint(self.env.cr):
                payment = self.env['account.payment'].create({
                    'invoice_ids': [(6, 0, invoice.ids)],
                    'amount': invoice.residual,
                    'payment_date': fields.Date.context_today(self),
                    'communication': invoice.reference or invoice.number,
                    'partner_id': invoice.partner_id.id,
                    'partner_type': partner_type,
                    'payment_type': payment_mode.payment_type,
                    'payment_method_id': payment_mode.payment_method_id.id,
                    'journal_id': payment_mode.fixed_journal_id.id,
                })
                payment.post()
        return
Ejemplo n.º 2
0
    def _register_payments(self, payment_filter):
        invoice_obj = self.env["account.move"]
        invoices = invoice_obj.search(payment_filter)
        _logger.debug("Invoices to Register Payment: %s", invoices.ids)

        domain = [
            ("account_internal_type", "in", ("receivable", "payable")),
            ("reconciled", "=", False),
        ]
        for invoice in invoices:
            partner_type = (invoice.move_type in ("out_invoice", "out_refund")
                            and "customer" or "supplier")
            payment_mode = invoice.payment_mode_id

            if not payment_mode.fixed_journal_id:
                _logger.debug(
                    "Unable to Register Payment for invoice %s: "
                    "Payment mode %s must have fixed journal",
                    invoice.id,
                    payment_mode.id,
                )
                continue

            with savepoint(self.env.cr):
                payment = self.env["account.payment"].create({
                    "amount":
                    invoice.amount_residual,
                    "date":
                    fields.Date.context_today(self),
                    "payment_reference":
                    invoice.payment_reference or invoice.name,
                    "partner_id":
                    invoice.partner_id.id,
                    "partner_type":
                    partner_type,
                    "payment_type":
                    payment_mode.payment_type,
                    "payment_method_id":
                    payment_mode.payment_method_id.id,
                    "journal_id":
                    payment_mode.fixed_journal_id.id,
                })
                payment.action_post()

            if payment.state != "posted":
                continue

            payment_lines = payment.line_ids.filtered_domain(domain)
            lines = invoice.line_ids
            for account in payment_lines.account_id:
                (payment_lines + lines).filtered_domain([
                    ("account_id", "=", account.id), ("reconciled", "=", False)
                ]).reconcile()
        return
 def _make_stock_reservation(self):
     sales = self.env["sale.order"].search(
         self._get_domain_for_stock_reservation())
     _logger.debug("Sales orders for which stock will be reserved: %s" %
                   sales)
     today = datetime.now()
     for sale in sales:
         workflow_process = sale.workflow_process_id
         # Check that the date_order is set at least at n days
         # declared at workflow_process. Doing this, we are sure
         # that the reservation.validity_date will be at least
         # at today. If this wasn't like that, the cron of stock_reserve
         # will release this reservation.
         plus_days = timedelta(
             days=workflow_process.stock_reservation_validity)
         min_date_order = today - plus_days
         if sale.date_order <= min_date_order:
             continue
         ctx = dict(self.env.context)
         ctx.update({
             "active_model": "sale.order",
             "active_id": sale.id,
             "active_ids": sale.ids,
         })
         with savepoint(self.env.cr):
             reservation_vals = {}
             if workflow_process.stock_reservation_validity:
                 reserve_until = today + plus_days
                 reservation_vals["date_validity"] = reserve_until
             if workflow_process.stock_reservation_location_id:
                 reservation_vals[
                     "location_id"] = workflow_process.stock_reservation_location_id.id
             if workflow_process.stock_reservation_location_dest_id:
                 reservation_vals[
                     "location_dest_id"] = workflow_process.stock_reservation_location_dest_id.id
             sale_stock_reserve = (
                 self.env["sale.stock.reserve"].with_context(ctx).create(
                     reservation_vals))
             line_ids = [line.id for line in sale.order_line]
             sale_stock_reserve.stock_reserve(line_ids)