İçereği Atla
Menü
Bu soru işaretlendi
1 Cevapla
2346 Görünümler

I have made a customization that when confirming a sale order, an rfq or more to be created automatically, and when SO is cancelled, the RFQs to be deleted


but I don't want to skip the sequence, for example, if the deleted RFQs have P0004_P0005_P0006

I want the next number to be P0004, not P0007
so i tried to update the sequence next_number but didn't work
here is my code and i would appreciate help

def_action_cancel(self):

​res = super(SaleOrder, self)._action_cancel()

​​purchase_ordersself.env['purchase.order'].search([('source_sale_id',  '='self.id)])

​sequence_code = 'purchase.order'# Update with the correct sequence code
for po in purchase_orders:

​​for order in po:

​for inv in order.invoice_ids:

​ifinvandinv.state notin ('cancel', 'draft'):

​raise UserError(_("Unable to cancel this sale order. You must first cancel the ""vendor bills related to the RFQs."))

​​po_name = po.name 

​po.write({'state': 'cancel', 'mail_reminder_confirmed': False})

​po.unlink()

​​​sequence = self.env['ir.sequence'].search([ ('code', '=', sequence_code), ('company_id', '=', self.env.company.id) ], limit=1)

​if sequence:

​sequence_number = int(po_name.split('/')[-1])

​next_sequence_number = sequence_number - 1

​sequence.sudo().write({'number_next':

​next_sequence_number})sequence.sudo().write({'number_next_actual': next_sequence_number})
return res



Avatar
Vazgeç

Why do you have such a requirement? Why not only cancel the RFQ's and not delete? keep the sequence without any gaps.
Btw. If you set up the MTO process for this, I think the RFQ's will be cancelled automatic, if you cancel the sales order.

En İyi Yanıt

Hi,

This wont be an easy customization, if you don't want a missing sequence better option is to prevent the deletion of RFQ and keep it in cancelled state. You can add necessary code logic for this.

If you go with what you are looking to achieve now,

Lets say we have a sequence values P0001, P0002 ....... P0005 and as per your code, on deleting a record, you are taking the sequence of the record and trying to update the ir.sequence next number value. Think what if you deleted multiple records from different positions ( 0002 0004) at the same ? The method wont be stable, as next number value will be either 0001 or 0003 in order of deletion.


If you need to really achieve this, try to modify and apply your logic in the place of sequence number assignation for the purchase, instead of calling a new sequence from the sequence record, find missing sequence in existing record and assign it for the new records, this will little heavy as you need to check for missing sequence in each creation.


Also in business perspective, if you share the RFQ with some one, and delete it from the system and later when you assign same sequence to different record, it will bring confusion, if later we look into the system with that reference number.


The ideal solution is to keep in cancelled state.


Additionally, you have option inside the ir.sequence called NO GAP in which if user delete the last record, the new number will be on last deleted sequence.


Update:

currently for the new purchase order the sequence is generated by calling this line of code:  vals['name'] = self_comp.env['ir.sequence'].next_by_code('purchase.order', sequence_date=seq_date) or '/'    from the create function.




So you have to rewrite this create method and instead of calling the next number from ir.sequence, check inside your records for missing numbers, if missing number is found, assign that value for newly created record, and only if the missing number is not found, you can get the next number from ir.sequence.


Thanks

Avatar
Vazgeç
Üretici

Thank you for your answer.
I have already tried the no gap but it doesn't do the job,
but can you elaborate more on
"If you need to really achieve this, try to modify and apply your logic in the place of sequence number assignation for the purchase, instead of calling a new sequence from the sequence record, find missing sequence in existing record and assign it for the new records, this will little heavy as you need to check for missing sequence in each creation."

because I don't quite get it.
I am very aware that the requirement is not good in business but sometimes clients want what they want, and I already deployed the cancelling, but it wasn't what they need.

İlgili Gönderiler Cevaplar Görünümler Aktivite
0
May 22
2535
2
Ara 23
28887
0
Tem 24
1947
1
Kas 20
4132
1
Eki 20
4745