Exemple #1
0
class solt_http_test(osv.osv):
    _name = 'solt.http.test'

    _columns = {
        'name':
        fields.char('URL', size=1024),
        'method':
        fields.selection([('post', 'POST'), ('get', 'GET'), ('put', 'PUT'),
                          ('patch', 'PATCH'), ('delete', 'DELETE')],
                         string='HTTP Method'),
        'user':
        fields.char('User', size=64),
        'password':
        fields.char('Password', size=64),
        'content':
        fields.text('Content'),
        'response':
        fields.text('Response'),
    }

    def action_request(self, cr, uid, ids, context=None):
        for test in self.browse(cr, uid, ids, context):
            auth = None
            if test.user and test.password:
                auth = (test.user, test.password)
            headers = {
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            }
            result = getattr(requests, test.method)(test.name,
                                                    test.content,
                                                    auth=auth,
                                                    headers=headers)
            test.write({'response': result.text})
        return True
Exemple #2
0
class hr_salary_rule_variable(models.Model):
    _name = 'hr.salary.rule.variable'
    _description = 'Variables used on salary rules that change over the years'
    _columns = {
        'salary_rule_id':
        fields.many2one(
            'hr.salary.rule',
            'Salary Rule',
            ondelete='cascade',
            required=True,
        ),
        'date_from':
        fields.date(
            'Date From',
            required=True,
        ),
        'date_to':
        fields.date('Date To', ),
        'type':
        fields.selection([('python', 'Python Code'),
                          ('fixed', 'Fixed Amount')],
                         string='Type'),
        'python_code':
        fields.text('Python Code'),
        'fixed_amount':
        fields.float('Fixed Amount'),
    }
Exemple #3
0
class product_template(osv.osv):
    _inherit = 'product.template'
    _name = 'product.template'
    _columns = {'additional_info': fields.text('additional_info')}


# This line adds to the product relation the field "Additional Info" and assigns its value to the xml-field "additional_info"

# vim:expandtab:smartinddent:tabstop=4:softtabstop=4:shiftwidth=4:
class insert_currency_act(osv.osv_memory):
    _name = 'insert.currency.act'
    _description = 'Descripcion del Wizard'
    _columns = {
        'currency_default': fields.text('Tasa de Cambio'),
    }

    def _get_currency(self, cr, uid, context=None):
        result = ""
        jquery = pq(
            url="http://www.cambiodolar.mx/"
        )  #obtenemos todo el html y lo asignamos a la variable jquery
        currency = jquery('div.valor').text(
        )  #imprime el texto del div que tiene la clase "valor"
        date_act = jquery('p.day').text(
        )  # imprime el texto de la etiqueta p que tiene la clase "day"
        result = currency + "\n" + date_act
        return result

    _defaults = {
        'currency_default': _get_currency,
    }

    def exec_currency(self, cr, uid, ids, context=None):
        jquery = pq(
            url="http://www.cambiodolar.mx/"
        )  #obtenemos todo el html y lo asignamos a la variable jquery
        currency = jquery('div.valor').text()
        active_ids = context['active_ids']
        date_act = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        currency_rep = currency.replace('1 Dólar = $', '').replace(
            'Pesos Mexicanos', '').replace(' ', '').replace(',', '.')
        currency_br = self.pool.get('res.currency').browse(cr,
                                                           uid,
                                                           active_ids,
                                                           context=None)[0]
        if currency_br.name.upper() != 'USD':
            raise osv.except_osv(
                'Error!',
                'Por el momento solo funciona para Dolares (USD).\nDisculpa las Molestias :( '
            )

        try:
            rate = 1 / float(currency_rep)
        except:
            rate = 0.0
        vals = {
            'name': date_act,
            'rate': rate,
            'currency_id': active_ids[0],
        }
        currency_rate = self.pool.get('res.currency.rate').create(cr,
                                                                  uid,
                                                                  vals,
                                                                  context=None)
        return True
Exemple #5
0
class hr_infraction_action(models.Model):

    _name = 'hr.infraction.action'
    _description = 'Action Based on Infraction'
    _columns = {
        'infraction_id': fields.many2one(
            'hr.infraction',
            'Infraction',
            ondelete='cascade',
            required=True,
            readonly=True,
        ),
        'type': fields.selection(
            ACTION_TYPE_SELECTION,
            'Type',
            required=True,
        ),
        'memo': fields.text(
            'Notes',
        ),
        'employee_id': fields.related(
            'infraction_id',
            'employee_id',
            type='many2one',
            store=True,
            obj='hr.employee',
            string='Employee',
            readonly=True,
        ),
        'warning_id': fields.many2one(
            'hr.infraction.warning',
            'Warning',
            readonly=True,
        ),
        'transfer_id': fields.many2one(
            'hr.department.transfer',
            'Transfer',
            readonly=True,
        ),
    }
    _rec_name = 'type'

    def unlink(self, cr, uid, ids, context=None):

        for action in self.browse(cr, uid, ids, context=context):
            if action.infraction_id.state not in ['draft']:
                raise models.except_orm(
                    _('Error'),
                    _('Actions belonging to Infractions not in "Draft" state '
                      'may not be removed.')
                )

        return super(hr_infraction_action, self).unlink(
            cr, uid, ids, context=context
        )
class account_liquidation_line(osv.osv):
    _name = 'account.liquidation.line'
    _description = 'Purchase liquidation lines'
    
    _columns = {
    
        'liquidation_id': fields.many2one('account.liquidation', 'Purchase liquidation'),
        'acc_partner_id': fields.many2one('account.account', 'Partner account'),
        'partner_id': fields.many2one('res.partner', 'Partner'),
        'acc_product_id': fields.many2one('account.account', 'Product account'),
        'product_id': fields.many2one('product.product', 'Product'),
        
        'price_unit': fields.float('Price unit'),
        'quantity': fields.float('Quantity'),
        'name': fields.text('Description'),

    }
Exemple #7
0
class biblioteca_libro(osv.osv):
    _name = 'biblioteca.libro'
    _description = 'biblioteca'
    
   
    
    _columns = {
                
            
    #'libros_id': fields.many2one('tipo.libro', string="tipo de libro",
     #                           required=True, select=True,                    RELACION MUCHOS A UNO 
      #                          help='Tipo de libro', store=True ),
      
      
                #RELACION MUCHOS A MUCHOS CON LA TABLA TIPO.LIBRO
      
    'tipos_id': fields.many2many('tipo.libro', 'tipo_libro_rel', id1='libro_id', id2='tipo_id', string="tipo de libro"),
    
    'prestamo_id': fields.many2one('prestamo.libro', string="prestamo de libro",required=False, select=True, store=True),
      
      
    
    'name':fields.char('nombre', size=64, required=False, readonly=False),
    'description': fields.text('Description'), 
    'date': fields.date('fecha publicacion'),
    'autor':fields.char('autor', size=64, required=False, readonly=False),
    'cantidad':fields.integer('numero de libros disponibles', required=True, store=True),
    

            
                    }
    
    _defaults = {  
        'prestamo_id': "no hay prestamos"
        }
    
    @api.onchange('prestamo_id') # indicamos los campos que van  a cambiar
    
    def _onchange_edad(self):
        
        self.cantidad = self.cantidad -1
class surgery(osv.osv):
    _name = "medical.surgery"
    _description = "Surgery"
    _columns = {
        'name':
        fields.many2one(
            'medical.procedure',
            'Code',
            help=
            "Procedure Code, for example ICD-10-PCS Code 7-character string"),
        'pathology':
        fields.many2one('medical.pathology',
                        'Base condition',
                        help="Base Condition / Reason"),
        'classification':
        fields.selection([
            ('o', 'Optional'),
            ('r', 'Required'),
            ('u', 'Urgent'),
        ],
                         'Surgery Classification',
                         select=True),
        'surgeon':
        fields.many2one('medical.physician',
                        'Surgeon',
                        help="Surgeon who did the procedure"),
        'date':
        fields.datetime('Date of the surgery'),
        'age':
        fields.char(
            'Patient age',
            size=3,
            help='Patient age at the moment of the surgery. Can be estimative'
        ),
        'description':
        fields.char('Description', size=128),
        'extra_info':
        fields.text('Extra Info'),
    }
Exemple #9
0
class hr_employee(models.Model):
    _inherit = 'hr.employee'
    _columns = {
        'biography': fields.text('Biography'),
    }
Exemple #10
0
class certificate(models.Model):
    def _get_status(self, cr, uid, ids, field_name, arg, context=None):
        r = {}
        for cer in self.browse(cr, uid, ids):
            r[cer.id] = 'valid'  #KGB
            continue  #KGB

            if not cer.csr and not cer.crt:
                r[cer.id] = 'empty'
            elif cer.csr and not cer.crt:
                try:
                    req = cer.get_request()[cer.id]
                    pkey = req.get_pubkey()
                    if req.verify(pkey):
                        r[cer.id] = 'valid_request'
                    else:
                        r[cer.id] = 'invalid_request'
                except:
                    r[cer.id] = 'invalid_request'
            elif cer.csr and cer.crt:
                req = cer.get_request()[cer.id]
                pkey = req.get_pubkey()
                try:
                    crt = cer.get_certificate()[cer.id]
                    r[cer.id] = 'valid' if crt.verify() and crt.verify(
                        pkey) else 'invalid'
                except:
                    r[cer.id] = 'invalid'
            elif not cer.csr and cer.pairkey_id and cer.crt:
                pkey = cer.pairkey_id.as_pkey()[cer.pairkey_id.id]
                try:
                    crt = cer.get_certificate()[cer.id]
                    r[cer.id] = 'valid' if crt.verify() and crt.verify(
                        pkey) else 'invalid'
                except:
                    r[cer.id] = 'invalid'

            else:
                r[cer.id] = 'Invalid'
        return r

    _name = "crypto.certificate"
    _columns = {
        'name':
        fields.char('Name', size=256),
        'csr':
        fields.text('Request Certificate',
                    readonly=True,
                    states={'draft': [('readonly', False)]},
                    help='Certificate Request in PEM format.'),
        'crt':
        fields.text('Certificate',
                    readonly=True,
                    states={
                        'draft': [('readonly', False)],
                        'waiting': [('readonly', False)]
                    },
                    help='Certificate in PEM format.'),
        'pairkey_id':
        fields.many2one('crypto.pairkey', 'Key pair'),
        'status':
        fields.function(_get_status,
                        method=True,
                        string='Status',
                        type='char',
                        help='Certificate Status'),
        'state':
        fields.selection(
            [
                ('draft', 'Draft'),
                ('waiting', 'Waiting'),
                ('confirmed', 'Confirmed'),
                ('cancel', 'Cancelled'),
            ],
            'State',
            select=True,
            readonly=True,
            help=
            '* The \'Draft\' state is used when a user is creating a new pair key. Warning: everybody can see the key.\
            \n* The \'Waiting\' state is used when a request has send to Certificate Authority and is waiting for response.\
            \n* The \'Confirmed\' state is used when a certificate is valid.\
            \n* The \'Canceled\' state is used when the key is not more used. You cant use this key again.'
        ),
    }
    _defaults = {
        'state': 'draft',
    }

    def action_validate(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        certs = self.read(cr,
                          uid,
                          ids, ['name', 'status', 'state'],
                          context=context)
        confirm_ids = []
        waiting_ids = []
        for cert in certs:
            status = cert['status']
            state = cert['state']
            if status in 'valid_request' and state == 'draft':
                waiting_ids.append(cert['id'])
            elif status == 'valid' and state in ['draft', 'waiting']:
                confirm_ids.append(cert['id'])
            else:
                #KGB?raise osv.except_osv(_('Invalid action !'),
                #KGB?                     _('Perhaps you want to insert an invalid request or certificate, or you want approve an invalid certificate with an valid request. Status: %s, State: %s'))
                pass
        self.write(cr,
                   uid,
                   confirm_ids, {'state': 'confirmed'},
                   context=context)
        self.write(cr, uid, waiting_ids, {'state': 'waiting'}, context=context)
        return True

    def action_cancel(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        self.write(cr, uid, ids, {'state': 'cancel'}, context=context)
        return True

    def get_request(self, cr, uid, ids, context=None):
        """
        Return Request object.
        """
        r = {}
        for cert in self.browse(cr, uid, ids):
            if cert.csr:
                r[cert.id] = X509.load_request_string(cert.csr.encode('ascii'))
        return r

    def get_certificate(self, cr, uid, ids, context=None):
        """
        Return Certificate object.
        """
        r = {}
        for cert in self.browse(cr, uid, ids):
            if cert.crt:
                r[cert.id] = X509.load_cert_string(cert.crt.encode('ascii'))
        return r

    def generate_certificate(self,
                             cr,
                             uid,
                             ids,
                             issuer,
                             ext=None,
                             serial_number=1,
                             version=2,
                             date_begin=None,
                             date_end=None,
                             expiration=365,
                             context=None):
        """
        Generate certificate
        """
        for item in self.browse(cr, uid, ids):
            if item.status == 'valid_request':
                # Get request data
                pk = item.pairkey_id.as_pkey()[item.pairkey_id.id]
                req = item.get_request()[item.id]
                sub = req.get_subject()
                pkey = req.get_pubkey()
                # Building certificate
                cert = X509.X509()
                cert.set_serial_number(serial_number)
                cert.set_version(version)
                cert.set_subject(sub)

                now = ASN1.ASN1_UTCTIME()
                if date_begin is None:
                    t = long(time.time()) + time.timezone
                    now.set_time(t)
                else:
                    now.set_datetime(date_begin)

                nowPlusYear = ASN1.ASN1_UTCTIME()
                if date_end is None:
                    nowPlusYear.set_time(t + 60 * 60 * 24 * expiration)
                else:
                    nowPlusYear.set_datetime(date_end)

                cert.set_not_before(now)
                cert.set_not_after(nowPlusYear)
                cert.set_issuer(issuer)
                cert.set_pubkey(pkey)
                cert.set_pubkey(cert.get_pubkey())
                if ext:
                    cert.add_ext(ext)
                cert.sign(pk, 'sha1')
                w = {'crt': cert.as_pem()}
                self.write(cr, uid, item.id, w)

    def smime(self, cr, uid, ids, message, context=None):
        """
        Sign message in SMIME format.
        """
        r = {}
        for cert in self.browse(cr, uid, ids):
            #if cert.status == 'valid': # EXTRANGE: Invalid certificates can be used for sign!
            if True:
                smime = SMIME.SMIME()
                ks = BIO.MemoryBuffer(cert.pairkey_id.key.encode('ascii'))
                cs = BIO.MemoryBuffer(cert.crt.encode('ascii'))
                bf = BIO.MemoryBuffer(str(message))
                out = BIO.MemoryBuffer()
                try:
                    smime.load_key_bio(ks, cs)
                except EVP.EVPError:
                    raise except_orm(
                        _('Error in Key and Certificate strings !'),
                        _('Please check if private key and certificate are in ASCII PEM format.'
                          ))
                sbf = smime.sign(bf)
                smime.write(out, sbf)
                r[cert.id] = out.read()
            else:
                raise except_orm(
                    _('This certificate is not ready to sign any message !'),
                    _('Please set a certificate to continue. You must send your certification request to a authoritative certificator to get one, or execute a self sign certification'
                      ))
        return r
Exemple #11
0
class AccountVoucher(osv.osv):
    _inherit = "account.voucher"
    
    @api.one
    @api.depends('currency_id', 'payment_rate_currency_id', 'payment_rate', 'amount', 'date', 'journal_id')
    def _compute_rate_amount(self):
        print "--_compute_rate_amount-"
        if self.currency_id != self.payment_rate_currency_id:
            currency_obj = self.env['res.currency']
            currency_str = payment_rate_str = ''
            if self.currency_id:
                currency_str = currency_obj.browse(self.currency_id.id)
            if self.payment_rate_currency_id:
                payment_rate_str = currency_obj.browse(self.payment_rate_currency_id.id)
            #currency_payment = currency_obj.browse(self.currency_id.id)#.rate
            amount_curr = u'%s\N{NO-BREAK SPACE}%s' % (currency_str.symbol, locale.format("%d", payment_rate_str.rate, grouping=True))#formatLang(self.env, currency_payment.rate, currency_obj=self.currency_id)
            amount_curr_payment = u'%s\N{NO-BREAK SPACE}%s' % (payment_rate_str.symbol, locale.format("%d", currency_str.rate, grouping=True))
            #print "====",self.date,currency_str.rate,payment_rate_str.rate
            currency_help_label = _('The exchange rate was %s = %s') % (amount_curr, amount_curr_payment)
            self.amount_info = self.amount * currency_str.rate
            self.currency_inverse_help_label = currency_help_label
    
    def _get_currency_help_label(self, cr, uid, currency_id, payment_rate, payment_rate_currency_id, context=None):
        rml_parser = report_sxw.rml_parse(cr, uid, 'currency_help_label', context=context)
        currency_pool = self.pool.get('res.currency')
        currency_str = payment_rate_str = ''
        if currency_id:
            currency_str = rml_parser.formatLang(1, currency_obj=currency_pool.browse(cr, uid, currency_id, context=context))
        if payment_rate_currency_id:
            payment_rate_str  = rml_parser.formatLang(currency_pool.browse(cr, uid, currency_id, context=context).rate, currency_obj=currency_pool.browse(cr, uid, payment_rate_currency_id, context=context))
        currency_help_label = _('At the operation date, the exchange rate was\n%s = %s') % (currency_str, payment_rate_str)
        return currency_help_label

    def _fnct_currency_help_label(self, cr, uid, ids, name, args, context=None):
        res = {}
        for voucher in self.browse(cr, uid, ids, context=context):
            res[voucher.id] = self._get_currency_help_label(cr, uid, voucher.currency_id.id, voucher.payment_rate, voucher.company_currency_id.id, context=context)
        return res
    
    def _get_amount_help_label(self, cr, uid, currency_id, payment_rate, payment_rate_currency_id, context=None):
#         rml_parser = report_sxw.rml_parse(cr, uid, 'currency_help_label', context=context)
        currency_pool = self.pool.get('res.currency')
#         currency_str = payment_rate_str = ''
        if currency_id:
            #print "=======",currency_pool.browse(cr, uid, currency_id, context=context).rate
#             currency_str = rml_parser.formatLang(1, currency_obj=currency_pool.browse(cr, uid, currency_id, context=context))
#         if payment_rate_currency_id:
#             payment_rate_str  = rml_parser.formatLang(currency_pool.browse(cr, uid, currency_id, context=context).rate, currency_obj=currency_pool.browse(cr, uid, payment_rate_currency_id, context=context))
#         currency_help_label = _('At the operation date, the exchange rate was\n%s = %s') % (currency_str, payment_rate_str)
            return str(payment_rate*currency_pool.browse(cr, uid, currency_id, context=context).rate)
    
    def _fnct_amount_info_label(self, cr, uid, ids, name, args, context=None):
        res = {}        
        for voucher in self.browse(cr, uid, ids, context=context):
            res[voucher.id] = self._get_amount_help_label(cr, uid, voucher.currency_id.id, voucher.amount, voucher.company_currency_id.id, context=context)
        return res
    
    _columns = {
        'is_currency': fields.boolean('Is multi currency'),
        'amount_info': fields.function(_fnct_amount_info_label, type='float', string='Amount Rate'),
        'currency_inverse_help_label': fields.text('Rate'),
        'company_currency_id': fields.related('company_id','currency_id', type='many2one', relation='res.currency', string='Company Currency'),
        'currency_help_label': fields.function(_fnct_currency_help_label, type='text', string="Helping Sentence", help="This sentence helps you to know how to specify the payment rate by giving you the direct effect it has"), 
    }
#     is_currency = fields.Boolean('Is multi currency')
#     amount_info = fields.Float(string='Amount Rate', compute='_compute_rate_amount') 
#     currency_inverse_help_label = fields.Text(string='Helping Rate Sentence', compute='_compute_rate_amount')
    def onchange_rate(self, cr, uid, ids, rate, amount, currency_id, payment_rate_currency_id, company_id, context=None):
        company_currency = self.pool.get('res.company').browse(cr, uid, company_id, context=context).currency_id
        #currency_pool = self.pool.get('res.currency')
        #rate_view = currency_pool.browse(cr, uid, currency_id, context=context).rate
        #print "==onchange_rate==",amount,rate,rate_view,amount*rate_view
        res =  {'value': {'paid_amount_in_company_currency': amount, 'amount_info':  self._get_amount_help_label(cr, uid, currency_id, amount, company_currency.id, context=context), 'currency_help_label': self._get_currency_help_label(cr, uid, currency_id, rate, payment_rate_currency_id, context=context)}}
        if rate and amount and currency_id:
            company_currency = self.pool.get('res.company').browse(cr, uid, company_id, context=context).currency_id
            #context should contain the date, the payment currency and the payment rate specified on the voucher
            amount_in_company_currency = self.pool.get('res.currency').compute(cr, uid, currency_id, company_currency.id, amount, context=context)
            res['value']['paid_amount_in_company_currency'] = amount_in_company_currency
        return res
        
    def onchange_journal(self, cr, uid, ids, journal_id, line_ids, tax_id, partner_id, date, amount, ttype, company_id, context=None):
        if context is None:
            context = {}
        if not journal_id:
            return False
        journal_pool = self.pool.get('account.journal')
        journal = journal_pool.browse(cr, uid, journal_id, context=context)
        if ttype in ('sale', 'receipt'):
            account_id = journal.default_debit_account_id
        elif ttype in ('purchase', 'payment'):
            account_id = journal.default_credit_account_id
        else:
            account_id = journal.default_credit_account_id or journal.default_debit_account_id
        tax_id = False
        if account_id and account_id.tax_ids:
            tax_id = account_id.tax_ids[0].id
 
        vals = {'value':{} }
        if ttype in ('sale', 'purchase'):
            vals = self.onchange_price(cr, uid, ids, line_ids, tax_id, partner_id, context)
            vals['value'].update({'tax_id':tax_id,'amount': amount})
        currency_id = False
        if journal.currency:
            currency_id = journal.currency.id
        else:
            currency_id = journal.company_id.currency_id.id
 
        period_ids = self.pool['account.period'].find(cr, uid, dt=date, context=dict(context, company_id=company_id))
        is_currency = False
        if journal.currency.id and journal.currency.id != journal.company_id.currency_id.id:
            is_currency = True
        #print "===is_currency====",is_currency,journal.currency.id,journal.company_id.currency_id.id
        vals['value'].update({
            'currency_id': currency_id,
            'payment_rate_currency_id': currency_id,
            'is_currency': is_currency,
            'period_id': period_ids and period_ids[0] or False
        })
        #in case we want to register the payment directly from an invoice, it's confusing to allow to switch the journal 
        #without seeing that the amount is expressed in the journal currency, and not in the invoice currency. So to avoid
        #this common mistake, we simply reset the amount to 0 if the currency is not the invoice currency.
        if context.get('payment_expected_currency') and currency_id != context.get('payment_expected_currency'):
            vals['value']['amount'] = 0
            amount = 0
        if partner_id:
            res = self.onchange_partner_id(cr, uid, ids, partner_id, journal_id, amount, currency_id, ttype, date, context)
            for key in res.keys():
                vals[key].update(res[key])
        return vals
Exemple #12
0
class hr_infraction(models.Model):

    _name = 'hr.infraction'
    _description = 'Infraction'
    _inherit = ['mail.thread', 'ir.needaction_mixin']
    _columns = {
        'name': fields.char(
            'Subject',
            size=256,
            required=True,
            readonly=True,
            states={'draft': [('readonly', False)]},
        ),
        'date': fields.date(
            'Date',
            required=True,
            readonly=True,
            states={'draft': [('readonly', False)]},
        ),
        'employee_id': fields.many2one(
            'hr.employee',
            'Employee',
            required=True,
            readonly=True,
            states={'draft': [('readonly', False)]},
        ),
        'category_id': fields.many2one(
            'hr.infraction.category',
            'Category',
            required=True,
            readonly=True,
            states={'draft': [('readonly', False)]},
        ),
        'action_ids': fields.one2many(
            'hr.infraction.action',
            'infraction_id',
            'Actions',
            readonly=True,
        ),
        'memo': fields.text(
            'Description',
            readonly=True,
            states={'draft': [('readonly', False)]},
        ),
        'state': fields.selection(
            [
                ('draft', 'Draft'),
                ('confirm', 'Confirmed'),
                ('action', 'Actioned'),
                ('noaction', 'No Action'),
            ],
            'State',
            readonly=True,
        ),
    }
    _defaults = {
        'date': time.strftime(DEFAULT_SERVER_DATE_FORMAT),
        'state': 'draft',
    }
    _track = {
        'state': {
            'hr_infraction.mt_alert_infraction_confirmed': (
                lambda self, cr, u, obj, ctx=None: obj['state'] == 'confirm'),
            'hr_infraction.mt_alert_infraction_action': (
                lambda self, cr, u, obj, ctx=None: obj['state'] == 'action'),
            'hr_infraction.mt_alert_infraction_noaction': (
                lambda self, cr, u, obj, ctx=None: obj['state'] == 'noaction'),
        },
    }

    def _needaction_domain_get(self, cr, uid, context=None):
        users_obj = self.pool.get('res.users')
        domain = []
        if users_obj.has_group(cr, uid, 'base.group_hr_manager'):
            domain = [('state', '=', 'confirm')]
        if len(domain) == 0:
            return False
        return domain

    def unlink(self, cr, uid, ids, context=None):
        for infraction in self.browse(cr, uid, ids, context=context):
            if infraction.state not in ['draft']:
                raise models.except_orm(
                    _('Error'),
                    _('Infractions that have progressed beyond "Draft" state '
                      'may not be removed.')
                )
        return super(hr_infraction, self).unlink(cr, uid, ids, context=context)

    def onchange_category(self, cr, uid, ids, category_id, context=None):
        res = {'value': {'name': False}}
        if category_id:
            category = self.pool.get('hr.infraction.category').browse(
                cr, uid, category_id, context=context
            )
            res['value']['name'] = category.name
        return res
class hr_payslip_amendment(models.Model):

    _name = 'hr.payslip.amendment'
    _description = 'Pay Slip Amendment'

    _inherit = ['mail.thread']

    _columns = {
        'name':
        fields.char(
            'Description',
            size=128,
            required=True,
            readonly=True,
            states={'draft': [('readonly', False)]},
        ),
        'input_id':
        fields.many2one(
            'hr.rule.input',
            'Salary Rule Input',
            required=True,
            readonly=True,
            states={'draft': [('readonly', False)]},
        ),
        'employee_id':
        fields.many2one(
            'hr.employee',
            'Employee',
            required=True,
            readonly=True,
            states={'draft': [('readonly', False)]},
        ),
        'amount':
        fields.float(
            'Amount',
            required=True,
            readonly=True,
            states={'draft': [('readonly', False)]},
            help="The meaning of this field is dependant on the salary rule "
            "that uses it."),
        'state':
        fields.selection([
            ('draft', 'Draft'),
            ('validate', 'Confirmed'),
            ('cancel', 'Cancelled'),
            ('done', 'Done'),
        ],
                         'State',
                         required=True,
                         readonly=True),
        'note':
        fields.text('Memo'),
    }

    _defaults = {
        'state': 'draft',
    }

    def onchange_employee(self, cr, uid, ids, employee_id, context=None):

        if not employee_id:
            return {}
        ee = self.pool.get('hr.employee').browse(cr,
                                                 uid,
                                                 employee_id,
                                                 context=context)
        name = _('Pay Slip Amendment: %s (%s)') % (ee.name, ee.employee_no)
        val = {'name': name}
        return {'value': val}

    def unlink(self, cr, uid, ids, context=None):

        for psa in self.browse(cr, uid, ids, context=context):
            if psa.state in ['validate', 'done']:
                raise models.except_orm(
                    _('Invalid Action'),
                    _('A Pay Slip Amendment that has been confirmed cannot be '
                      'deleted!'))

        return super(hr_payslip_amendment, self).unlink(cr,
                                                        uid,
                                                        ids,
                                                        context=context)
Exemple #14
0
class pairkey(models.Model):
    _name = "crypto.pairkey"
    _columns = {
        'name': fields.char('Name', size=256, select=True),
        'user_id': fields.many2one('res.users', 'Owner',
                                   select=True, 
                                   help='Owner of the key. The only who can view, import and export the key.'
                                  ),
        'group_id': fields.many2one('res.groups', 'User group',
                                    select=True,
                                    help='Users who use the pairkey.'),
        'pub': fields.text('Public key', 
                           readonly=True, states={'draft': [('readonly',False)]}, 
                           help='Public key in PEM format.'),
        'key': fields.text('Private key', readonly=True, states={'draft': [('readonly',False)]},
                          help='Private key in PEM format.'),
        'state': fields.selection([
                ('draft', 'Draft'),
                ('confirmed', 'Confirmed'),
                ('cancel', 'Cancelled'),
            ], 'State', select=True, readonly=True,
            help='* The \'Draft\' state is used when a user is creating a new pair key. Warning: everybody can see the key.\
            \n* The \'Confirmed\' state is used when the key is completed with public or private key.\
            \n* The \'Canceled\' state is used when the key is not more used. You cant use this key again.'),
    }
    _defaults = {
        'state': 'draft',
    }

    def action_validate(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        pairkeys = self.read(cr, uid, ids, ['key', 'pub'], context=context)
        confirm_ids = []
        for pk in pairkeys:
            # Check public key
            try:
                PUB = BIO.MemoryBuffer(pk['pub'].encode('ascii'))
                RSA.load_pub_key_bio(PUB)
                pub = True
            except:
                pub = False
            # Check private key
            try:
                RSA.load_key_string(pk['key'].encode('ascii'))
                key = True
            except:
                key = False
            if key or pub:
                confirm_ids.append(pk['id'])
            else:
                raise except_orm(_('Invalid action !'),
                                  _('Cannot confirm invalid pairkeys. '
                                    'You need provide private and public keys in PEM format.'))
        self.write(cr, uid, confirm_ids, {'state': 'confirmed'}, context=context)
        return True

    def action_cancel(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        self.write(cr, uid, ids, {'state': 'cancel'}, context=context)
        return True

    def action_generate(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        self.generate_keys(cr, uid, ids, context=context)
        return self.action_validate(cr, uid, ids, context=context)

    def as_pem(self, cr, uid, ids):
        """
        Return pairkey in pem format. 
        """
        r = {}
        for pairkey in self.browse(cr, uid, ids):
            if pairkey.key: 
                r[pairkey.id] = pairkey.key.encode('ascii')
            else:
                r[pairkey.id] = ''
            if pairkey.pub:
                r[pairkey.id] += pairkey.pub.encode('ascii')
        return r

    def as_rsa(self, cr, uid, ids):
        """
        Return RSA object. 
        """
        return dict((k,RSA.load_key_string(v)) for k,v in self.as_pem(cr, uid, ids).items())

    def as_pkey(self, cr, uid, ids):
        """
        Return PKey object.
        """
        def set_key(rsa):
            pk = EVP.PKey()
            pk.assign_rsa(rsa)
            return pk
        return dict((k,set_key(v)) for k,v in self.as_rsa(cr, uid, ids).items())

    def generate_keys(self, cr, uid, ids, key_length=1024, key_gen_number=0x10001, context=None):
        """
        Generate key pairs: private and public.
        """
        if context is None:
            context = {}
        for signer in self.browse(cr, uid, ids):
            # Random seed
            Rand.rand_seed (os.urandom(key_length))
            # Generate key pair
            key = RSA.gen_key (key_length, key_gen_number, lambda *x: None)
            # Create memory buffers
            pri_mem = BIO.MemoryBuffer()
            pub_mem = BIO.MemoryBuffer()
            # Save keys to buffers
            key.save_key_bio(pri_mem, None)
            key.save_pub_key_bio(pub_mem)

            w = {
                'key': pri_mem.getvalue(),
                'pub': pub_mem.getvalue(),
            }
            self.write(cr, uid, signer.id, w)
        return True

    def generate_certificate_request(self, cr, uid, ids,
                                     x509_name,
                                     context=None):
        """
        Generate new certificate request for pairkey.
        """
        if context is None:
            context = {}
        r = {}
        for signer in self.browse(cr, uid, ids):
            # Create certificate structure
            pk = EVP.PKey()
            req = X509.Request()
            pem_string = signer.key.encode('ascii') + '\n' + signer.pub.encode('ascii')
            rsa = RSA.load_key_string(pem_string)
            pk.assign_rsa(rsa)
            req.set_pubkey(pk)
            req.set_subject(x509_name)

            # Crete certificate object
            certificate_obj = self.pool.get('crypto.certificate')
            w = {
                'name': x509_name.as_text(),
                'csr': req.as_pem(),
                'pairkey_id': signer.id,
            }
            r[signer.id] = certificate_obj.create(cr, uid, w)
        return r
class account_invoice(osv.osv):
    _name = 'account.invoice'
    _inherit = 'account.invoice'

    def fields_view_get(self,
                        cr,
                        uid,
                        view_id=None,
                        view_type='form',
                        context=None,
                        toolbar=False,
                        submenu=False):

        if context is None:
            context = {}
        res = super(account_invoice, self).fields_view_get(cr,
                                                           uid,
                                                           view_id=view_id,
                                                           view_type=view_type,
                                                           context=context,
                                                           toolbar=toolbar,
                                                           submenu=False)
        doc = etree.XML(res['arch'])
        cr.execute("""select uid from res_groups_users_rel where gid=
          (select id  from res_groups where category_id in 
          ( select id from ir_module_category where name = 'Customer Portal' ) and name = 'Manager') and uid = """
                   + str(uid))
        portal_user = cr.fetchone()
        portal_group = portal_user and portal_user[0]

        if uid == portal_group:
            for node in doc.xpath("//field[@name='partner_id']"):
                node.set('options', "{'no_open' : true}")
                setup_modifiers(node, res['fields']['partner_id'])
                res['arch'] = etree.tostring(doc)

            for node in doc.xpath("//field[@name='contact_id']"):
                node.set('options', "{'no_open' : true}")
                setup_modifiers(node, res['fields']['contact_id'])
                res['arch'] = etree.tostring(doc)

        cr.execute("""select uid from res_groups_users_rel where gid=
          (select id  from res_groups where category_id in 
          ( select id from ir_module_category where name = 'Supplier Portal' ) ) and uid = """
                   + str(uid))
        sup_portal = cr.fetchone()
        supportal_group = sup_portal and sup_portal[0]

        if uid == supportal_group:
            for node in doc.xpath("//field[@name='partner_id']"):
                node.set('options', "{'no_open' : true}")
                setup_modifiers(node, res['fields']['partner_id'])
                res['arch'] = etree.tostring(doc)
        return res

    #Overidden
    def _amount_all(self, cr, uid, ids, name, args, context=None):
        res = {}
        without_round = 0.00

        for invoice in self.browse(cr, uid, ids, context=context):
            res[invoice.id] = {
                'amount_untaxed': 0.0,
                'amount_tax': 0.0,
                'amount_total': 0.0,
                'round_off': 0.0,
            }

            for line in invoice.invoice_line:
                res[invoice.id]['amount_untaxed'] += line.price_subtotal

            for line in invoice.tax_line:
                res[invoice.id]['amount_tax'] += line.amount

            res[invoice.id]['amount_total'] = round(
                res[invoice.id]['amount_tax'] +
                res[invoice.id]['amount_untaxed'])
            without_round = res[invoice.id]['amount_tax'] + res[
                invoice.id]['amount_untaxed']
            res[invoice.id]['round_off'] = res[
                invoice.id]['amount_total'] - without_round

        return res

    #Overidden
    def _get_invoice_line(self, cr, uid, ids, context=None):
        result = {}
        for line in self.pool.get('account.invoice.line').browse(
                cr, uid, ids, context=context):
            result[line.invoice_id.id] = True
        return result.keys()

    # Funtion To Convert Amount to Text
    def _amt_in_words(self, cr, uid, ids, field_name, args, context=None):
        res = {}

        for case in self.browse(cr, uid, ids):
            txt = ''
            res[case.id] = ''
            if case.amount_total:
                txt += amount_to_text_softapps._100000000_to_text(
                    int(round(case.amount_total)))
                res[case.id] = txt
        return res

    def _get_type(self, cr, uid, context=None):
        if context is None:
            context = {}
        return context.get('type', 'out_invoice')

    _columns = {
        'contact_id':
        fields.many2one('res.partner', 'Contact Person'),
        'amt_in_words':
        fields.function(_amt_in_words,
                        method=True,
                        string="Amount in Words",
                        type="text",
                        store=True),
        'transport':
        fields.char("Customer PO Reference", size=50),
        'vehicle':
        fields.char("Vehicle", size=20),
        'dc_ref':
        fields.char("DC Reference", size=200),
        'date_from':
        fields.function(lambda *a, **k: {},
                        method=True,
                        type='date',
                        string="From"),
        'date_to':
        fields.function(lambda *a, **k: {},
                        method=True,
                        type='date',
                        string="To"),
        'terms':
        fields.text("Terms And Condition"),
        #Overidden
        'amount_untaxed':
        fields.function(_amount_all,
                        digits_compute=dp.get_precision('Account'),
                        string='Subtotal',
                        track_visibility='always',
                        store=True,
                        multi='all'),
        'amount_tax':
        fields.function(_amount_all,
                        digits_compute=dp.get_precision('Account'),
                        string='Tax',
                        store=True,
                        multi='all'),
        'amount_total':
        fields.function(_amount_all,
                        digits_compute=dp.get_precision('Account'),
                        string='Total',
                        store=True,
                        multi='all'),
        'round_off':
        fields.function(_amount_all,
                        digits_compute=dp.get_precision('Account'),
                        string='Round off',
                        store=True,
                        multi='all'),
    }
    _order = "id desc"

    def unlink(self, cr, uid, ids, context=None):
        pick_obj = self.pool.get("stock.picking")

        for case in self.browse(cr, uid, ids):
            if case.state == 'draft':
                if case.internal_number:
                    self.write(cr, uid, ids, {'internal_number': False})
                pick_ids = pick_obj.search(cr, uid,
                                           [('invoice_id', '=', case.id)])
                pick_obj.write(cr, uid, pick_ids,
                               {'invoice_state': '2binvoiced'})

        return super(account_invoice, self).unlink(cr, uid, ids, context)
Exemple #16
0
class action_wizard(models.TransientModel):

    _name = 'hr.infraction.action.wizard'
    _description = 'Choice of Actions for Infraction'

    _columns = {
        'action_type':
        fields.selection(
            ACTION_TYPE_SELECTION,
            'Action',
            required=True,
        ),
        'memo':
        fields.text('Notes', ),
        'new_job_id':
        fields.many2one(
            'hr.job',
            'New Job',
        ),
        'xfer_effective_date':
        fields.date('Effective Date', ),
        'effective_date':
        fields.date('Effective Date', ),
    }

    def create_action(self, cr, uid, ids, context=None):

        if context is None:
            context = {}
        infraction_id = context.get('active_id', False)
        if not infraction_id:
            return False
        data = self.read(cr, uid, ids[0], context=context)

        vals = {
            'infraction_id': infraction_id,
            'type': data['action_type'],
            'memo': data.get('memo', False),
        }
        action_id = self.pool.get('hr.infraction.action').create(
            cr, uid, vals, context=context)

        # Update state of infraction, if not already done so
        #
        infraction_obj = self.pool.get('hr.infraction')
        infraction_data = infraction_obj.read(cr,
                                              uid,
                                              infraction_id,
                                              ['employee_id', 'state'],
                                              context=context)
        if infraction_data['state'] == 'confirm':
            netsvc.LocalService('workflow').trg_validate(
                uid, 'hr.infraction', infraction_id, 'signal_action', cr)

        infraa_obj = self.pool.get('hr.infraction.action')
        imd_obj = self.pool.get('ir.model.data')
        iaa_obj = self.pool.get('ir.actions.act_window')

        # If the action is a warning create the appropriate record, reference
        # it from the action, and pull it up in the view (in case the user
        # needs to make any changes.
        #
        if data['action_type'] in ['warning_verbal', 'warning_letter']:
            vals = {
                'name': (data['action_type'] == 'warning_verbal' and 'Verbal'
                         or 'Written') + ' Warning',
                'type': (data['action_type'] == 'warning_verbal' and 'verbal'
                         or 'written'),
                'action_id':
                action_id,
            }
            warning_id = self.pool.get('hr.infraction.warning').create(
                cr, uid, vals, context=context)
            infraa_obj.write(cr,
                             uid,
                             action_id, {
                                 'warning_id': warning_id,
                             },
                             context=context)
            res_model, res_id = imd_obj.get_object_reference(
                cr, uid, 'hr_infraction', 'open_hr_infraction_warning')
            dict_act_window = iaa_obj.read(cr, uid, res_id, [])
            dict_act_window['view_mode'] = 'form,tree'
            dict_act_window['domain'] = [('id', '=', warning_id)]
            return dict_act_window

        # If the action is a departmental transfer create the appropriate
        # record, reference it from the action, and pull it up in the view
        # (in case the user needs to make any changes.
        #
        elif data['action_type'] == 'transfer':
            xfer_obj = self.pool.get('hr.department.transfer')
            ee = self.pool.get('hr.employee').browse(
                cr, uid, infraction_data['employee_id'][0], context=context)
            _tmp = xfer_obj.onchange_employee(cr,
                                              uid,
                                              None,
                                              ee.id,
                                              context=context)
            vals = {
                'employee_id': ee.id,
                'src_id': _tmp['value']['src_id'],
                'dst_id': data['new_job_id'][0],
                'src_contract_id': _tmp['value']['src_contract_id'],
                'date': data['xfer_effective_date'],
            }
            xfer_id = xfer_obj.create(cr, uid, vals, context=context)
            infraa_obj.write(cr,
                             uid,
                             action_id, {
                                 'transfer_id': xfer_id,
                             },
                             context=context)

            res_model, res_id = imd_obj.get_object_reference(
                cr,
                uid,
                'hr_transfer',
                'open_hr_department_transfer',
            )
            dict_act_window = iaa_obj.read(cr, uid, res_id, [])
            dict_act_window['view_mode'] = 'form,tree'
            dict_act_window['domain'] = [('id', '=', xfer_id)]
            return dict_act_window

        # The action is dismissal. Begin the termination process.
        #
        elif data['action_type'] == 'dismissal':
            term_obj = self.pool.get('hr.employee.termination')
            wkf = netsvc.LocalService('workflow')
            ee = self.pool.get('hr.employee').browse(
                cr, uid, infraction_data['employee_id'][0], context=context)

            # We must create the employment termination object before we set
            # the contract state to 'done'.
            res_model, res_id = imd_obj.get_object_reference(
                cr, uid, 'hr_infraction', 'term_dismissal')
            vals = {
                'employee_id': ee.id,
                'name': data['effective_date'],
                'reason_id': res_id,
            }
            term_id = term_obj.create(cr, uid, vals, context=context)
            infraa_obj.write(cr,
                             uid,
                             action_id, {'termination_id': term_id},
                             context=context)

            # End any open contracts
            for contract in ee.contract_ids:
                if contract.state not in ['done']:
                    wkf.trg_validate(uid, 'hr.contract', contract.id,
                                     'signal_pending_done', cr)

            # Set employee state to pending deactivation
            wkf.trg_validate(uid, 'hr.employee', ee.id,
                             'signal_pending_inactive', cr)

            # Trigger confirmation of termination record
            wkf.trg_validate(uid, 'hr.employee.termination', term_id,
                             'signal_confirmed', cr)

            res_model, res_id = imd_obj.get_object_reference(
                cr, uid, 'hr_employee_state', 'open_hr_employee_termination')
            dict_act_window = iaa_obj.read(cr, uid, res_id, [])
            dict_act_window['domain'] = [('id', '=', term_id)]
            return dict_act_window

        return True
Exemple #17
0
class SaleQuickInvoiceConfirmLine(osv.TransientModel):
    _name = 'sale.quick.invoice.confirm.line'

    def _get_order_line_info(self,
                             cr,
                             uid,
                             ids,
                             field_name,
                             arg,
                             context=None):
        down_payment_id = self.pool['ir.values'].get_default(
            cr, uid, 'sale.config.settings', 'deposit_product_id_setting')

        res = dict(map(lambda x: (x, 0), ids))
        for line in self.browse(cr, uid, ids, context):
            if line.sale_order_line:
                sol = line.sale_order_line

                if field_name == 'product_id':
                    res[line.id] = sol.product_id.name
                if field_name == 'sequence':
                    res[line.id] = sol.sequence
                if field_name == 'price_unit':
                    res[line.id] = sol.price_unit
                if field_name == 'price_total':
                    if line.identification != 'discount':
                        res[line.id] = sol.price_unit if (
                            sol.product_id.id
                            == down_payment_id) else sol.price_total
                    else:
                        res[line.id] = sol.price_total
                if field_name == 'product_uom_qty':
                    res[line.id] = sol.product_uom_qty
                if field_name == 'qty_invoiced' and line.identification == 'commodity':
                    res[line.id] = sol.qty_invoiced
                if field_name == 'balance':
                    invoice_total = 0.0
                    for invoice in sol.invoice_lines:
                        if line.identification == 'discount':
                            invoice_total += -invoice.price_subtotal
                        else:
                            invoice_total += invoice.price_subtotal
                    res[line.id] = (
                        line.price_total - invoice_total
                    ) if line.identification == 'discount' else (
                        line.price_total - invoice_total)

        return res

    _columns = {
        'quick_invoice_line':
        fields.many2one('quick.invoice.confirm',
                        required=True,
                        ondelete='cascade'),
        'sale_order_line':
        fields.many2one('sale.order.line',
                        string='订单行ID',
                        required=True,
                        ondelete='cascade'),
        'product_id':
        fields.function(_get_order_line_info, string='产品', type='char'),
        'sequence':
        fields.function(_get_order_line_info, string='序号', type='integer'),
        'price_unit':
        fields.function(_get_order_line_info, string='单价', type='float'),
        'price_total':
        fields.function(_get_order_line_info, string='小计', type='float'),
        'product_uom_qty':
        fields.function(_get_order_line_info, string='数量', type='float'),
        'qty_invoiced':
        fields.function(_get_order_line_info, string='收款次数', type='float'),
        'balance':
        fields.function(_get_order_line_info, string='余款', type='float'),
        'amount':
        fields.float(string='本次收款'),
        'comment':
        fields.text(string='说明'),
        'invoice_number':
        fields.char(string='收据号'),
        'date_invoice':
        fields.date(string='收款日期'),
        'identification':
        fields.selection([('down_payment', '预付'), ('discount', '优惠'),
                          ('commodity', '产品')],
                         string='类型'),
    }

    @api.multi
    def create_new_invoice(self):
        imd = self.env['ir.model.data']
        action = imd.xmlid_to_object(
            'account_expend.act_sale_quick_invoice_confirm')
        form_view_id = imd.xmlid_to_res_id(
            'account_expend.view_sale_quick_invoice_confirm_line_from')

        result = {
            'res_id': self.id,
            'name': '收款信息',
            'type': action.type,
            'view_type': 'form',
            'view_model': 'form',
            'res_model': action.res_model,
            'views': [(form_view_id, 'form')],
            'views_id': form_view_id,
            'target': 'new',
        }

        return result

    @api.multi
    def confirm_cancel(self):
        qid = self.quick_invoice_line.id
        return self.env['sale.order'].confirm_cancel(qid)

    # 覆盖sale_make_invoice_advance的发票创建方法
    @api.multi
    def action_invoice_create(self,
                              order_ids,
                              order_line_id,
                              grouped=False,
                              final=False):
        inv_obj = self.env['account.invoice']
        precision = self.env['decimal.precision'].precision_get(
            'Product Unit of Measure')
        orders = self.env['sale.order'].browse(order_ids)
        invoices = {}

        for order in orders:
            group_key = order.id if grouped else (order.partner_invoice_id.id,
                                                  order.currency_id.id)

            line = self.env['sale.order.line'].browse(order_line_id)

            if line:

                # if float_is_zero(line.qty_to_invoice, precision_digits=precision):
                #     continue
                if group_key not in invoices:
                    inv_data = order._prepare_invoice()

                    if self.invoice_number:
                        inv_data['invoice_number'] = self.invoice_number
                    if self.date_invoice:
                        inv_data['date_invoice'] = self.date_invoice
                    if self.comment:
                        inv_data['comment'] = self.comment
                    # 如果产品为优惠,那么建立退款收据
                    if line.product_id.name == u'价格优惠':
                        inv_data['type'] = 'out_refund'
                    invoice = inv_obj.create(inv_data)
                    invoices[group_key] = invoice
                elif group_key in invoices:
                    vals = {}
                    if order.name not in invoices[group_key].origin.split(
                            ', '):
                        vals['origin'] = invoices[
                            group_key].origin + ', ' + order.name
                    if order.client_order_ref and order.client_order_ref not in invoices[
                            group_key].name.split(', '):
                        vals['name'] = invoices[
                            group_key].name + ', ' + order.client_order_ref

                    invoices[group_key].write(vals)

                    # if not float_is_zero(qty, precision_digits=precision):
                vals = self._prepare_invoice_line(qty=line.product_uom_qty)
                vals.update({
                    'invoice_id': invoices[group_key].id,
                    'sale_line_ids': [(6, 0, [line.id])]
                })

                self.env['account.invoice.line'].create(vals)

        if not invoices:
            raise UserError(_('There is no invoicable line.'))

        for invoice in invoices.values():
            if not invoice.invoice_line_ids:
                raise UserError(_('There is no invoicable line.'))

            if invoice.amount_untaxed < 0:
                invoice.type = 'out_refund'
                for line in invoice.invoice_line_ids:
                    line.quantity = line.quantity

            for line in invoice.invoice_line_ids:
                line._set_additional_fields(invoice)

            invoice.compute_taxes()

            # 调用验证按钮的工作流
            if invoice.state not in ('draft', 'proforma', 'proforma2'):
                pass
            invoice.signal_workflow('invoice_open')

        return [inv.id for inv in invoices.values()]

    @api.multi
    def _prepare_invoice_line(self, qty):
        sol = self.sale_order_line
        product = sol.product_id
        order_id = sol.order_id

        res = {}
        account = product.property_account_income_id or product.categ_id.property_account_income_categ_id
        if not account:
            raise UserError(_('Please define income account for this product: "%s" (id:%d) - or for its category: "%s".') % \
                            (product.name, product.id, product.categ_id.name))

        fpos = order_id.fiscal_position_id or order_id.partner_id.property_account_position_id
        if fpos:
            account = fpos.map_account(account)

        # 因为收款改为手写,当出现数量大于1时,一定有计算的必要,变为计算单价的方式
        res = {
            'name': sol.name,
            'sequence': sol.sequence,
            'origin': sol.order_id.name,
            'account_id': account.id,
            'price_unit': self.amount / qty,
            'quantity': qty,
            'price_subtotal': self.amount,
            'discount': sol.discount,
            'uom_id': sol.product_uom.id,
            'product_id': product.id or False,
            'invoice_line_tax_ids': [(6, 0, sol.tax_id.ids)],
            'account_analytic_id': sol.order_id.project_id.id,
        }

        return res

    @api.multi
    def create_invoice(self):
        if self.identification == 'commodity':
            self.amount = abs(self.amount)
        if self.amount <= 0.0:
            raise ValidationError(u'收款不能小于或等于0.0')
        if self.identification != 'discount' and self.amount > self.balance:
            raise ValidationError(u'收款不能大于余款')

        new_invoices = self.action_invoice_create(
            [self.sale_order_line.order_id.id],
            self.sale_order_line.id,
            grouped=True)

        invoices = []
        for new_invoice in new_invoices:
            self.env['account.quick.invoice.confirm.line'].create({
                'quick_invoice_line':
                self.quick_invoice_line.id,
                'invoice_id':
                new_invoice
            })

        return self.confirm_cancel()
class PurchaseRequest(models.Model):

    _name = 'purchase.request'
    _description = 'Purchase Request'
    _inherit = ['mail.thread', 'ir.needaction_mixin']

    @api.model
    def _company_get(self):
        company_id = self.env['res.company']._company_default_get(self._name)
        return self.env['res.company'].browse(company_id.id)

    @api.model
    def _get_default_requested_by(self):
        return self.env['res.users'].browse(self.env.uid)

    @api.model
    def _get_default_name(self):
        return self.env['ir.sequence'].get('purchase.request')

    @api.model
    def _default_picking_type(self):
        type_obj = self.env['stock.picking.type']
        company_id = self.env.context.get('company_id') or \
            self.env.user.company_id.id
        types = type_obj.search([('code', '=', 'incoming'),
                                 ('warehouse_id.company_id', '=', company_id)])
        if not types:
            types = type_obj.search([('code', '=', 'incoming'),
                                     ('warehouse_id', '=', False)])
        return types[:1]

    @api.multi
    @api.depends('state')
    def _compute_is_editable(self):
        for rec in self:
            if rec.state in ('to_approve', 'approved', 'rejected'):
                rec.is_editable = False
            else:
                rec.is_editable = True

    @api.multi
    def _track_subtype(self, init_values):
        for rec in self:
            if 'state' in init_values and rec.state == 'to_approve':
                return 'purchase_request.mt_request_to_approve'
            elif 'state' in init_values and rec.state == 'to_department_manager_approved':
                return 'purchase_request.mt_request_to_department_manager_approved'
            elif 'state' in init_values and rec.state == 'to_accountant_manager_approved':
                return 'purchase_request.mt_request_to_accountant_manager_approved'
            elif 'state' in init_values and rec.state == 'approved':
                return 'purchase_request.mt_request_approved'
            elif 'state' in init_values and rec.state == 'rejected':
                return 'purchase_request.mt_request_rejected'
        return super(PurchaseRequest, self)._track_subtype(init_values)

    name = fields.Char('Request Reference',
                       size=32,
                       required=True,
                       default=_get_default_name,
                       track_visibility='onchange')
    origin = fields.Char('Source Document', size=32)
    date_start = fields.Date('Creation date',
                             help="Date when the user initiated the "
                             "request.",
                             default=fields.Date.context_today,
                             track_visibility='onchange')
    date_finish = fields.Date('Expected date',
                              help="Date when the Request will  "
                              "Finish.",
                              default=fields.Date.context_today,
                              compute='_check_the_date',
                              track_visibility='onchange')
    requested_by = fields.Many2one('res.users',
                                   'Requested by',
                                   required=True,
                                   track_visibility='onchange',
                                   default=_get_default_requested_by)
    assigned_to = fields.Many2one('res.users',
                                  'Department Managers',
                                  domain=[('x_department_manager', '=', True)],
                                  track_visibility='onchange')
    assigned_to_2 = fields.Many2one('res.users',
                                    'Department Managers',
                                    domain=[('x_department_manager', '=', True)
                                            ],
                                    track_visibility='onchange')
    assigned_to_3 = fields.Many2one('res.users',
                                    'Projects or Sections Managers',
                                    domain=[('x_project_manager', '=', True)],
                                    track_visibility='onchange')
    description = fields.Text('Description')
    direct_manager_notes = fields.Text('Direct manager notes')
    department_manager_notes = fields.Text('Director notes')
    accountant_manager_notes = fields.Text('CFO notes')
    executive_manager_notes = fields.text('Executive manager notes')
    treasurer_manager_notes = fields.Text('Treasurer notes')
    president_manager_notes = fields.Text('Chairman notes')
    company_id = fields.Many2one('res.company',
                                 'Company',
                                 required=True,
                                 default=_company_get,
                                 track_visibility='onchange')
    line_ids = fields.One2many('purchase.request.line',
                               'request_id',
                               'Products to Purchase',
                               readonly=False,
                               copy=True,
                               track_visibility='onchange')
    state = fields.Selection(selection=_STATES,
                             string='Status',
                             index=True,
                             track_visibility='onchange',
                             required=True,
                             copy=False,
                             default='draft')
    stage = fields.Selection(selection=_STAGES, default=1, string='Stage')
    steps = fields.Selection(selection=_STEPS,
                             string='Technical Specifications Steps')
    request_type = fields.Selection(selection=_TYPES,
                                    default=1,
                                    string='Request Type')
    type_reason = fields.Text('Reason')

    department = fields.Many2one('hr.department',
                                 'Department',
                                 track_visibility='onchange')
    project = fields.Many2one('hr.department',
                              'Project',
                              track_visibility='onchange')
    is_editable = fields.Boolean(string="Is editable",
                                 compute="_compute_is_editable",
                                 readonly=True)

    picking_type_id = fields.Many2one('stock.picking.type',
                                      'Picking Type',
                                      required=True,
                                      default=_default_picking_type)

    ontime = fields.Integer(string='On Time',
                            compute="change_ontime",
                            readonly=True)
    ontime_stage = fields.Integer(string='On Time Stage',
                                  compute="change_ontime_stage",
                                  readonly=True)
    done1 = fields.Char(string='Done',
                        compute="change_done_stage",
                        default='early')
    progress = fields.Float(string='Progress',
                            compute='change_progress',
                            readonly=True)
    color = fields.Integer('Color Index', compute="change_colore_on_kanban")
    price_alltotal = fields.Float(string='Total Price',
                                  compute='_compute_amount_all',
                                  store=True)

    @api.multi
    @api.depends('line_ids.price_total')
    def _compute_amount_all(self):
        for request in self:
            for line in request.line_ids:
                request.price_alltotal += line.price_total
            if request.price_alltotal > 0:
                request.stage = 5

    @api.multi
    def copy(self, default=None):
        default = dict(default or {})
        self.ensure_one()
        default.update({
            'state': 'draft',
            'name': self.env['ir.sequence'].get('purchase.request'),
        })
        return super(PurchaseRequest, self).copy(default)

    @api.model
    def create(self, vals):
        request = super(PurchaseRequest, self).create(vals)
        if vals.get('assigned_to'):
            request.message_subscribe_users(user_ids=[request.assigned_to.id])
        return request

    @api.multi
    def write(self, vals):
        res = super(PurchaseRequest, self).write(vals)
        for request in self:
            if vals.get('assigned_to'):
                self.message_subscribe_users(user_ids=[request.assigned_to.id])
        return res

    @api.multi
    def button_draft(self):
        for rec in self:
            rec.state = 'draft'
        return True

    @api.multi
    def button_to_approve(self):
        for rec in self:
            rec.state = 'to_approve'
        return True

    @api.multi
    def button_to_department_manager_approved(self):
        for rec in self:
            rec.state = 'to_department_manager_approved'
        return True

    @api.multi
    def button_to_accountant_manager_approved(self):
        for rec in self:
            rec.state = 'to_accountant_manager_approved'
            rec.stage = 2
        return True

    @api.multi
    def button_approved(self):
        for rec in self:
            rec.state = 'approved'
            rec.stage = 6
        return True

    @api.multi
    def button_rejected(self):
        for rec in self:
            rec.state = 'rejected'
        return True

    @api.multi
    def _check_the_date(self):
        #for rec in self:
        #   d1 = datetime.datetime.strptime((rec.date_start),'%Y-%m-%d')
        #  new_date = d1 + datetime.timedelta(days=18)
        # rec.date_finish = new_date
        for rec in self:
            if rec.price_alltotal <= 500:
                d1 = datetime.datetime.strptime((rec.date_start), '%Y-%m-%d')
                new_date = d1 + datetime.timedelta(days=4)
                rec.date_finish = new_date
            elif rec.price_alltotal > 500 and rec.price_alltotal <= 5000:
                d1 = datetime.datetime.strptime((rec.date_start), '%Y-%m-%d')
                new_date = d1 + datetime.timedelta(days=5)
                rec.date_finish = new_date
            elif rec.price_alltotal > 5000 and rec.price_alltotal <= 100000:
                d1 = datetime.datetime.strptime((rec.date_start), '%Y-%m-%d')
                new_date = d1 + datetime.timedelta(days=7)
                rec.date_finish = new_date
            elif rec.price_alltotal > 100000:
                d1 = datetime.datetime.strptime((rec.date_start), '%Y-%m-%d')
                new_date = d1 + datetime.timedelta(days=15)
                rec.date_finish = new_date

    def change_progress(self):
        for rec in self:
            x = rec.stage
            y = x * 100 / 14
            rec.progress = y

    def change_ontime(self):
        for rec in self:
            d1 = datetime.datetime.today()
            d2 = datetime.datetime.strptime((rec.date_finish), '%Y-%m-%d')
            d = str((d2 - d1).days + 1)
            rec.ontime = d

    def change_ontime_stage(self):
        for rec in self:
            d1 = datetime.datetime.today()
            d2 = datetime.datetime.strptime((rec.date_start), '%Y-%m-%d')
            d = str((d1 - d2).days + 1)
            rec.ontime_stage = d

    def change_done_stage(self):
        for rec in self:
            if rec.price_alltotal <= 500:
                if rec.stage == 1 and rec.ontime_stage > 1:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 2 and rec.ontime_stage > 2:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 3 and rec.ontime_stage > 4:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 4 and rec.ontime_stage > 4:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 5 and rec.ontime_stage > 4:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 6 and rec.ontime_stage > 4:
                    dd = "late"
                    rec.done1 = dd
            elif rec.price_alltotal > 500 and rec.price_alltotal <= 5000:
                if rec.stage == 1 and rec.ontime_stage > 1:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 2 and rec.ontime_stage > 2:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 3 and rec.ontime_stage > 5:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 4 and rec.ontime_stage > 5:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 5 and rec.ontime_stage > 5:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 6 and rec.ontime_stage > 5:
                    dd = "late"
                    rec.done1 = dd
            elif rec.price_alltotal > 5000 and rec.price_alltotal <= 100000:
                if rec.stage == 1 and rec.ontime_stage > 1:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 2 and rec.ontime_stage > 2:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 3 and rec.ontime_stage > 5:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 4 and rec.ontime_stage > 5:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 5 and rec.ontime_stage > 5:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 6 and rec.ontime_stage > 5:
                    dd = "late"
                    rec.done1 = dd
            elif rec.price_alltotal > 100000:
                if rec.stage == 1 and rec.ontime_stage > 1:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 2 and rec.ontime_stage > 2:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 3 and rec.ontime_stage > 7:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 4 and rec.ontime_stage > 7:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 5 and rec.ontime_stage > 7:
                    dd = "late"
                    rec.done1 = dd
                elif rec.stage == 6 and rec.ontime_stage > 7:
                    dd = "late"
                    rec.done1 = dd
                elif (rec.stage == 7 or rec.stage == 8 or rec.stage == 9
                      or rec.stage == 10) and rec.ontime_stage > 15:
                    dd = "late"
                    rec.done1 = dd
                elif (rec.stage == 11 or rec.stage == 12
                      or rec.stage == 13) and rec.ontime_stage > 12:
                    dd = "late"
                    rec.done1 = dd

    def change_colore_on_kanban(self):
        for record in self:
            color = 0
            if record.ontime < 0:
                color = 2
            elif record.ontime == 0:
                color = 3
            elif record.ontime > 0:
                color = 5
            record.color = color