summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fixco_api/controllers/api_v1/sale.py37
-rw-r--r--fixco_api/models/__init__.py1
-rw-r--r--fixco_api/models/invoice.py29
-rw-r--r--fixco_api/models/sale.py21
-rw-r--r--fixco_custom/models/sale.py33
5 files changed, 81 insertions, 40 deletions
diff --git a/fixco_api/controllers/api_v1/sale.py b/fixco_api/controllers/api_v1/sale.py
index a4bc7d0..7649407 100644
--- a/fixco_api/controllers/api_v1/sale.py
+++ b/fixco_api/controllers/api_v1/sale.py
@@ -183,8 +183,7 @@ class Sales(controller.Controller):
@http.route(prefix + 'invoice/create', auth='public', methods=['POST', 'OPTIONS'], csrf=False)
@controller.Controller.must_authorized()
- def complete_delivery_order(self, **kw):
- print(1)
+ def create_invoice(self, **kw):
sale_id = int(kw.get('id', '0'))
ref = str(kw.get('ref', '0'))
if sale_id == 0 and ref == '0':
@@ -195,9 +194,35 @@ class Sales(controller.Controller):
query = [('client_order_ref', '=', ref)]
sales = request.env['sale.order'].search(query)
- invoiced = ''
+ invoiced = []
for sale in sales:
invoiced = sale.api_create_invoices(sale.id)
- return self.response([{
- 'invoiced': str(invoiced)
- }])
+ return self.response(invoiced)
+
+ @http.route(prefix + 'invoice/paid', auth='public', methods=['POST', 'OPTIONS'], csrf=False)
+ @controller.Controller.must_authorized()
+ def paid_invoice(self, **kw):
+ # action_create_payments in account_payment_register.py
+ sale_id = int(kw.get('id', '0'))
+ ref = str(kw.get('ref', '0'))
+ if sale_id == 0 and ref == '0':
+ return self.response(code=500, description='Internal Server Error')
+ if sale_id > 0:
+ query = [('id', '=', sale_id)]
+ else:
+ query = [('client_order_ref', '=', ref)]
+
+ sales = request.env['sale.order'].search(query)
+ for sale in sales:
+ for invoice in sale.invoice_ids:
+ invoice._register_payment_automatically()
+ # register = invoice.env['account.payment.register'].create({
+ # 'journal_id': 24,
+ # 'payment_method_id': 1,
+ # 'partner_bank_id': 5,
+ # 'amount': invoice.amount_total,
+ # 'payment_date': '2024-05-31 08:50:00',
+ # 'communication': invoice.name
+ # })
+ # register.action_create_payments()
+ return self.response({'status': True})
diff --git a/fixco_api/models/__init__.py b/fixco_api/models/__init__.py
index 8a0dc04..69c1de8 100644
--- a/fixco_api/models/__init__.py
+++ b/fixco_api/models/__init__.py
@@ -1 +1,2 @@
from . import sale
+from . import invoice
diff --git a/fixco_api/models/invoice.py b/fixco_api/models/invoice.py
new file mode 100644
index 0000000..354d3ea
--- /dev/null
+++ b/fixco_api/models/invoice.py
@@ -0,0 +1,29 @@
+from odoo import models
+
+
+class Invoice(models.Model):
+ _inherit = 'account.move'
+
+ def _register_payment_automatically(self):
+ payment_model = self.env['account.payment']
+
+ for invoice in self:
+ if invoice.state == 'posted' and invoice.amount_residual > 0:
+ payment_vals = {
+ 'payment_type': 'inbound',
+ 'partner_type': 'customer',
+ 'partner_id': invoice.partner_id.id,
+ 'amount': invoice.amount_residual,
+ 'date': invoice.invoice_date,
+ 'journal_id': self.env['account.journal'].search([('type', '=', 'bank')], limit=1).id,
+ 'payment_method_id': self.env.ref('account.account_payment_method_manual_in').id,
+ 'ref': invoice.name,
+ # 'communication': invoice.name,
+ }
+ payment = payment_model.create(payment_vals)
+ payment.action_post()
+
+ # Reconcile payment with invoice
+ (invoice.line_ids + payment.move_id.line_ids) \
+ .filtered(lambda line: line.account_id.reconcile) \
+ .reconcile()
diff --git a/fixco_api/models/sale.py b/fixco_api/models/sale.py
index 4c81fad..5ddd581 100644
--- a/fixco_api/models/sale.py
+++ b/fixco_api/models/sale.py
@@ -1,7 +1,7 @@
from odoo import models
-class SaleOrder(models.Model):
+class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'
def api_single_response(self, line):
@@ -23,3 +23,22 @@ class SaleOrder(models.Model):
'discount': line.discount,
}
return data
+
+
+class SaleOrder(models.Model):
+ _inherit = 'sale.order'
+
+ def api_create_invoices(self, sale_order_id):
+ sale_orders = self.env['sale.order'].search([('id', '=', sale_order_id)])
+
+ # if self.advance_payment_method == 'delivered':
+ invoices = sale_orders._create_invoices(final=False)
+ data = []
+ for invoice in invoices:
+ invoice.action_post()
+ data.append({
+ 'invoice_id': invoice.id,
+ 'invoice': invoice.name,
+ 'state': invoice.state
+ })
+ return data
diff --git a/fixco_custom/models/sale.py b/fixco_custom/models/sale.py
index ceea0ab..b266d6a 100644
--- a/fixco_custom/models/sale.py
+++ b/fixco_custom/models/sale.py
@@ -6,36 +6,3 @@ class SaleOrder(models.Model):
_inherit = "sale.order"
carrier_id = fields.Many2one('delivery.carrier', string='Shipping Method')
-
- def api_create_invoices(self, sale_order_id):
- sale_orders = self.env['sale.order'].search([('id', '=', sale_order_id)])
-
- # if self.advance_payment_method == 'delivered':
- sale_orders._create_invoices(final=False)
- # else:
- # # Create deposit product if necessary
- # if not self.product_id:
- # vals = self._prepare_deposit_product()
- # self.product_id = self.env['product.product'].create(vals)
- # self.env['ir.config_parameter'].sudo().set_param('sale.default_deposit_product_id', self.product_id.id)
- #
- # sale_line_obj = self.env['sale.order.line']
- # for order in sale_orders:
- # amount, name = self._get_advance_details(order)
- #
- # if self.product_id.invoice_policy != 'order':
- # raise UserError(_('The product used to invoice a down payment should have an invoice policy set to "Ordered quantities". Please update your deposit product to be able to create a deposit invoice.'))
- # if self.product_id.type != 'service':
- # raise UserError(_("The product used to invoice a down payment should be of type 'Service'. Please use another product or update this product."))
- # taxes = self.product_id.taxes_id.filtered(lambda r: not order.company_id or r.company_id == order.company_id)
- # tax_ids = order.fiscal_position_id.map_tax(taxes, self.product_id, order.partner_shipping_id).ids
- # analytic_tag_ids = []
- # for line in order.order_line:
- # analytic_tag_ids = [(4, analytic_tag.id, None) for analytic_tag in line.analytic_tag_ids]
- #
- # so_line_values = self._prepare_so_line(order, analytic_tag_ids, tax_ids, amount)
- # so_line = sale_line_obj.create(so_line_values)
- # self._create_invoice(order, so_line, amount)
- # if self._context.get('open_invoices', False):
- # return sale_orders.action_view_invoice()
- return {'type': 'ir.actions.act_window_close'} \ No newline at end of file