# -*- coding: utf-8 -*-

from odoo import models, fields, api

class SOS_Suppliers(models.Model):
    _name = 'sos_suppliers'
    _description = 'Suppliers of Sosaley'
    _rec_name="supplier_name"
    _order = 'supplier_name'

    supplier_code = fields.Char(string="Supplier Code", required=True, readonly=True,
        default=lambda self: self._generate_id())
    supplier_name = fields.Char(string="Supplier Name", required=True)
    qp_no= fields.Char(string="QP No")
    address = fields.Text(string="Registered Address")
    shipped_from_address = fields.Char(string="Shipped From")
    contact_person = fields.Char(string="Contact Person")
    contact_no = fields.Char(string="Contact no")
    email = fields.Char(string="Email")
    gst_no = fields.Char(string="GST No")
    pan_no = fields.Char(string="PAN No")
    cin_no = fields.Char(string="CIN No")
    msme_no = fields.Char(string="MSME Certificate No")
    bank_details = fields.Text(string="Bank Details")
    iso_no = fields.Char(string="ISO Certificate No")
    iso_date = fields.Date(string="ISO Certificate Validity Date")
    scope = fields.Char(string="Scope of Service")
    approval=fields.Date(string="Approved On")
    last_evaluation_date=fields.Date(string="Last Evaluated On")
    re_evaluation_due_date=fields.Date(string="Re-Evaluation Due on")
    re_evaluation_method=fields.Char(string="Re-Evaluation Method")
    credit_period = fields.Integer(string="Credit period(In days)")
    other_terms = fields.Text(string="Other Terms & Conditions")
    quality_policy = fields.Binary(string="Quality policy")
    quality_policy_filename=fields.Char(string="Quality Policy File Name")
    sarf_doc = fields.Binary(string="SARF Document")
    sarf_doc_filename=fields.Char(string="SARF Document File Name")
    iso_certificate = fields.Binary(string="ISO Certificate")
    iso_certificate_filename=fields.Char(string="ISO Certificate File Name")
    _sql_constraints = [
        ('name_unique', 'UNIQUE(supplier_name)', 'This Supplier is Already In Our Records')
    ]
    def _generate_id(self):
        prefix = "SOS/SARF/SUP/"
        last_record = self.search([
            ('supplier_code', 'like', prefix + '%')
        ], order='supplier_code desc', limit=1)
        
        if last_record:
            last_code = last_record.supplier_code
            next_number = int(last_code[len(prefix):]) + 1
        else:
            next_number = 1

        return f"{prefix}{next_number:03d}"
   



