Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SaleContracts(models.Model):
- _name = "sales.contracts"
- _inherit = ["mail.thread", "mail.activity.mixin", "utm.mixin"]
- _order = "id desc"
- @api.depends("payments")
- def get_payments_total(self):
- """Calculate total payments."""
- for rec in self:
- rec.payments_total = sum(line.amount for line in rec.payments)
- @api.depends("payments", "amount")
- def get_balance_total(self):
- """Calculate total balance."""
- for rec in self:
- rec.balance_total = rec.amount - rec.payments_total
- # Check status of the contract
- rec._check_contract_status()
- name = fields.Char(
- string="Contract No.", readonly=True, required=True, copy=False, default="New"
- )
- chassis_no = fields.Char("Chassis No")
- def create_im4_bill(self):
- """Create IM4 Bill."""
- for item in self:
- if not item.im4:
- continue
- return {
- "view_id": self.env.ref("carbiz_client.vehicle_im7_clearance_form1").id,
- "view_mode": "form",
- "res_model": "carbiz.t1",
- "res_id": item.im4.id,
- "type": "ir.actions.act_window",
- "target": "new",
- "context": self._context,
- }
- def add_im4(self):
- for item in self:
- # Skip if IM4 already exists
- if item.im4:
- _logger.info("IM4 already exists, checking its state...")
- if item.state_im4 == "draft":
- _logger.info("IM4 is in draft state...")
- context = dict(self._context or {})
- return {
- # 'view_type': 'form',
- "view_id": self.env.ref(
- "carbiz_client.vehicle_im4_clearance_form1"
- ).id,
- "view_mode": "form",
- "res_model": "carbiz.t1",
- "res_id": item.im4.id,
- "type": "ir.actions.act_window",
- "target": "new",
- "context": context,
- }
- continue
- # Search for car_id
- car_id = self.env["carbiz_erp.vehicle"].search(
- [("product", "=", item.product_id.id)], limit=1
- )
- # Check for existing IM4
- existing_im4 = self.env["carbiz.t1"].search(
- [("car_id", "=", car_id.id), ("state_im7", "=", "im4")], limit=1
- )
- if existing_im4:
- item.im4 = existing_im4.id
- item.im4.add_im4()
- continue
- # If there's no existing IM4, check for IM7
- existing_im7 = self.env["carbiz.t1"].search(
- [
- ("car_id", "=", car_id.id),
- ("state_im7", "not in", ("expired", "im4", "cancel")),
- ],
- limit=1,
- )
- if existing_im7:
- item.im4 = existing_im7.id
- return existing_im7.add_im4()
- # If there's no IM7 or IM4 record for this vehicle, raise an error
- raise UserError(
- _("There's no IM7 or IM4 record for this vehicle!\n Something is wrong!")
- )
- im4 = fields.Many2one("carbiz.t1", string="IM4")
- im4_bill = fields.Many2one(
- "account.move", string="IM4 Bill", related="im4.im4_bill", store=True
- )
- im4_bill_state = fields.Selection(
- related="im4_bill.payment_state", store=True, string="IM4 Bill State"
- )
- state_im4 = fields.Selection(
- [
- ("draft", "Draft"),
- ("confirm", "Validated"),
- ("cancel", "Canceled"),
- ],
- string="IM4 Status",
- related="im4.state_im4",
- store=True,
- )
- model = fields.Many2one("fleet.vehicle.model", "Model")
- product_id = fields.Many2one("product.product", "Vehicle")
- reg_no = fields.Many2one("fleet.vehicle", "Reg No")
- sales_person = fields.Many2one("res.users", "Salesperson")
- reg_type = fields.Selection(
- [
- ("localuse", "Local Use"),
- ("reexport", "Re-export"),
- ("taxfree", "Tax Free"),
- ],
- default="localuse",
- string="Registration Type",
- )
- color = fields.Char("Colour")
- file_no = fields.Char("Origin")
- customer = fields.Many2one("res.partner", string="Purchaser")
- address = fields.Char("City")
- phone = fields.Char("Phone", related="customer.phone")
- po_box = fields.Char("Street", related="customer.street")
- country_id = fields.Many2one("res.country", string="Country")
- sales_order = fields.Many2one("sale.order", string="Origin")
- delivery_count = fields.Integer(related="sales_order.delivery_count", store=True)
- invoice_count = fields.Integer(related="sales_order.invoice_count", store=True)
- start_date = fields.Date("Installment Start Date", default=fields.Date.today())
- def action_view_delivery(self):
- return self.sales_order.action_view_delivery()
- def action_view_invoice(self):
- return self.sales_order.action_view_invoice()
- @api.depends("customer_bills")
- def _compute_customer_bills_count(self):
- """Compute customer bills count."""
- account_move = self.env["account.move"]
- for record in self:
- record.bills_count = account_move.search_count(
- [("contract_id", "=", record.id), ("move_type", "=", "out_invoice")]
- )
- @api.depends("payments")
- def _compute_customer_payments_count(self):
- """Compute customer payments count."""
- account_payment = self.env["account.payment"]
- for record in self:
- record.payments_count = account_payment.search_count(
- [("contract_id", "=", record.id)]
- )
- record._compute_delivery_request()
- @api.depends("down_payment")
- def _compute_delivery_request(self):
- for rec in self:
- # Balance Before Delivery sum
- beforedelivery_payment_amounts = sum(
- line.amount
- for line in rec.payments.filtered(
- lambda pay: pay.carbiz_payment_type == "balbeforedelivery"
- )
- )
- # Down Payments
- down_payment_amounts = sum(
- line.amount
- for line in rec.payments.filtered(
- lambda pay: pay.carbiz_payment_type == "downpayment"
- )
- )
- rec.make_delivery = False
- # Check if the total amount lets to cause delivery
- if down_payment_amounts == rec.down_payment and (rec.balance > 0 and beforedelivery_payment_amounts == rec.balance):
- rec.make_delivery = True
- elif down_payment_amounts == rec.down_payment and not rec.balance:
- rec.make_delivery = True
- # Check the car status, if in bond or not
- picking = rec.product_id.stock_move_ids.filtered(
- lambda car: car.product_id == rec.product_id
- and car.state == "done"
- and car.picking_code == "outgoing"
- )
- if not len(picking) == 0:
- rec.make_delivery = False
- product_id = fields.Many2one("product.product", string="Product")
- amount = fields.Float("Amount in Figures")
- payments_total = fields.Float("Amount Paid", compute=get_payments_total, store=True)
- balance_total = fields.Float(
- "Total Amount Outstanding", compute=get_balance_total, store=True
- )
- amount_words = fields.Char("Amount in Words", tracking=True)
- down_payment = fields.Float("Down Payment")
- balance = fields.Float("Amount Before Delivery")
- days_balance = fields.Integer("Days to clear balance")
- balance_installment = fields.Float("Installment Balance")
- days_installment = fields.Integer("Days to clear installments")
- days = fields.Integer("Days to clear installment")
- date_delivery = fields.Date("Final Day Delivery")
- date_installment = fields.Date("Final Day Installment")
- no_install = fields.Integer("No. of Installments")
- installment_lines = fields.One2many(
- "install.line", "contract_id", string="Installments"
- )
- customer_bills = fields.One2many(
- "account.move", "contract_id", string="Customer Bills"
- )
- payment_lines = fields.One2many(
- "contract.payment", "contract_id", string="Payments"
- )
- payments = fields.One2many(
- "account.payment",
- "contract_id",
- string="Payments",
- domain=[("state", "=", "posted")],
- )
- payments_count = fields.Integer(compute="_compute_customer_payments_count")
- # bills_count = fields.Integer(compute="_compute_customer_bills_count")
- make_delivery = fields.Boolean(
- default=False, compute="_compute_delivery_request", store=True
- )
- assigned = fields.Boolean(default=False)
- is_other_existing_contract = fields.Boolean(default=False)
- state = fields.Selection(
- [
- ("draft", "Draft"),
- ("running", "Running"),
- ("completed", "Completed"),
- ("cancelled", "Cancelled"),
- ],
- default="draft",
- string="State",
- )
- def create_delivery(self):
- """Create a delivery on demand"""
- for rec in self.sales_order.order_line:
- if rec.product_id == self.product_id:
- rec._action_launch_stock_rule()
- self.make_delivery = False
- @api.depends("balance_total")
- def _check_contract_status(self):
- for rec in self:
- if (
- rec.balance_total == 0
- and rec.payments_total == rec.amount
- and rec.state == "running"
- ):
- rec.state = "completed"
- # def create_customer_bills(self):
- # for contract in self:
- # invoice_line_vals = []
- # invoice_line_vals.append((0, 0, {
- # 'product_id': contract.product_id.id,
- # 'price_unit': contract.amount,
- # 'tax_ids': False,
- # # 'currency_id' : contract.billing_currency,
- # 'quantity': 1,
- # }))
- # invoice_vals = {
- # 'partner_id': contract.customer.id,
- # 'move_type': 'out_invoice',
- # 'invoice_date': date.today(),
- # "contract_id": contract.id,
- # 'invoice_line_ids': invoice_line_vals
- # }
- # customer_bills = self.env['account.move'].create(invoice_vals)
- def show_bills(self):
- return {
- "name": "Contracts",
- "domain": [
- ("contract_id", "=", self.id),
- ("move_type", "=", "out_invoice"),
- ],
- "view_type": "form",
- "res_model": "account.move",
- "view_id": False,
- "view_mode": "tree,form",
- "type": "ir.actions.act_window",
- "context": "{'create': False}",
- }
- def show_payments(self):
- return {
- "name": "Payments",
- "domain": [("contract_id", "=", self.id)],
- "view_type": "tree,form",
- "res_model": "account.payment",
- "view_id": False,
- "view_mode": "tree,form",
- "type": "ir.actions.act_window",
- "context": "{'create': False}",
- }
- def show_deliveries(self):
- return {
- "name": "Job Cards",
- "domain": [("contract_id", "=", self.id)],
- "view_type": "form",
- "res_model": "rtt.jobcard",
- "view_id": False,
- "view_mode": "tree,form",
- "type": "ir.actions.act_window",
- "context": "{'create': False}",
- }
- @api.model
- def create(self, vals):
- if vals.get("name", "New") == "New":
- vals["name"] = (
- self.env["ir.sequence"].next_by_code("contract.sequence") or "New"
- )
- return super().create(vals)
- def receive_payment(self):
- pay_journals = self.env["account.journal"].search(
- [("type", "in", ("bank", "cash"))], limit=1
- )
- for rec in self:
- context = dict(
- {
- "default_payment_type": "inbound",
- "default_partner_type": "customer",
- "default_ref": rec.name,
- "default_date": date.today(),
- "default_destination_account_id": rec.customer.property_account_receivable_id.id,
- "default_journal_id": pay_journals.id,
- "default_partner_id": rec.customer.id,
- "default_currency_id": rec.sales_order.currency_id.id,
- "default_carbiz_contract_payment": True,
- "default_contract_id": rec.id,
- }
- )
- return {
- "name": _("Collect Payment"),
- "view_id": self.env.ref("account.view_account_payment_form").id,
- "view_mode": "form",
- "res_model": "account.payment",
- "type": "ir.actions.act_window",
- "target": "new",
- "context": context,
- }
- @api.onchange("days_balance", "days_installment")
- def update_dates(self):
- for rec in self:
- if rec.days_balance > 0:
- rec.date_delivery = date.today() + relativedelta(days=rec.days_balance)
- if rec.days_balance == 0:
- rec.date_delivery = date.today()
- if rec.days_installment:
- rec.date_installment = date.today() + relativedelta(
- days=rec.days_installment
- )
- def start_contract(self):
- # fields_to_check = ['down_payment', 'days_installment', 'no_install', 'days_balance', 'balance']
- fields_to_check = ["balance", "days_balance"]
- for rec in self:
- # Car check in by contracts
- car_search_by_contract_ids = self.search(
- [
- ("product_id", "=", rec.product_id.id),
- ("id", "!=", self.id),
- ("state", "=", "running")
- ]
- )
- if len(car_search_by_contract_ids) > 0:
- rec.is_other_existing_contract = True
- # Add chassis number
- # Search for car_id
- car_id = self.env["carbiz_erp.vehicle"].search(
- [("product", "=", rec.product_id.id)], limit=1
- )
- rec.chassis_no = car_id.name
- # For not-straightforward payment
- if not (rec.amount == rec.down_payment) and rec.balance_installment < 1:
- for field in fields_to_check:
- if getattr(rec, field) < 1:
- raise UserError(
- _(
- "Provide the {} amount.".format(
- field.replace("_", " ").capitalize()
- )
- )
- )
- rec.state = "running"
- if rec.balance_installment > 0:
- rec.compute_installments()
- rec._check_contract_status() # Check if the contract is already fulfilled
- def cancel_contract(self):
- for rec in self:
- if rec.is_other_existing_contract:
- rec.is_other_existing_contract = False
- rec.state = "cancelled"
- def reset_contract(self):
- for rec in self:
- rec.state = "draft"
- # Clear payments information
- for payment in rec.payments:
- payment.action_draft()
- payment.action_cancel()
- rec.payments = [(6, 0, 0)]
- def auto_complete_contract(self):
- for rec in self:
- contract_records = self.env["sales.contracts"].search([])
- for data in contract_records:
- if data.state == "running":
- data.state = "completed"
- @api.onchange("amount", "down_payment", "balance")
- def calc_balance(self):
- for rec in self:
- # if rec.down_payment != 0.0:
- rec.balance_installment = rec.amount - (rec.down_payment + rec.balance)
- def compute_installments(self):
- fields_to_check = ["days_installment", "no_install"]
- for rec in self:
- if not rec.start_date:
- raise UserError("Please state the installment start date.")
- # Validate first fields
- for field in fields_to_check:
- if getattr(rec, field) < 1:
- raise UserError(
- _(
- "Provide the {} amount.".format(
- field.replace("_", " ").capitalize()
- )
- )
- )
- rec.installment_lines.unlink()
- interval = int(rec.days_installment / rec.no_install)
- amounts = rec.balance_installment / rec.no_install
- due_dates = [
- rec.start_date + relativedelta(days=i * interval)
- for i in range(1, rec.no_install + 1)
- ]
- for due_date in due_dates:
- self.env["install.line"].create(
- {
- "name": rec.product_id.name + "-" + str(due_date),
- "amount": amounts,
- "balance": amounts,
- "due_date": due_date,
- "status": "pending",
- "amount_paid": 0.0,
- "contract_id": rec.id,
- }
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement