diff --git a/models/scop_cotisation_cg.py b/models/scop_cotisation_cg.py index 84ddfebe8e12b05719cebd6f3ef3bf552a5b71e9..754fb861c5671903283bdd4a6870be8bbdae7647 100644 --- a/models/scop_cotisation_cg.py +++ b/models/scop_cotisation_cg.py @@ -410,21 +410,32 @@ class ScopCotisation(models.Model): staff_average = 0 date_invoice = date if date else self.date_cotisation - bordereau = self.env["scop.bordereau"].create( - { - "partner_id": member.id, - "payment_mode_id": member.customer_payment_mode_id.id, - "base_cotisation_cg": self.id, - "liasse_fiscale_id": liasse.id if liasse else None, - "year_liasse": liasse.year if liasse else 0, - "date_cotisation": date_invoice, - "nb_quarter": nb_quarter, - "staff_count": staff_count, - "staff_shareholder_count": staff_shareholder_count, - "staff_average": staff_average, - } + existing_bdx = self.env["scop.bordereau"].search( + [ + ("partner_id", "=", member.id), + ("base_cotisation_cg", "=", self.id), + ] ) - bordereau.create_cotiz_and_lines() + if not existing_bdx: + bordereau = self.env["scop.bordereau"].create( + { + "partner_id": member.id, + "payment_mode_id": member.customer_payment_mode_id.id, + "base_cotisation_cg": self.id, + "liasse_fiscale_id": liasse.id if liasse else None, + "year_liasse": liasse.year if liasse else 0, + "date_cotisation": date_invoice, + "nb_quarter": nb_quarter, + "staff_count": staff_count, + "staff_shareholder_count": staff_shareholder_count, + "staff_average": staff_average, + } + ) + bordereau.create_cotiz_and_lines() + else: + raise exceptions.UserError( + _("Un bordereau existe déjà pour cette coopérative et cette campagne") + ) return bordereau.id def bordereau_validate(self): diff --git a/wizard/scop_cotisation_cg_regul.py b/wizard/scop_cotisation_cg_regul.py index 3ccf0aed98a070d044a952e019710b122039cdbe..847f28dbd2faf2869ec933bc357873581380ef9d 100644 --- a/wizard/scop_cotisation_cg_regul.py +++ b/wizard/scop_cotisation_cg_regul.py @@ -143,139 +143,8 @@ class ScopCotisationRegul(models.TransientModel): _("Vous devez renseigner un motif de " "régularisation pour valider.") ) - bordereau_id = self.bordereau_id - partner_id = bordereau_id.partner_id - - # CREATE VERSION - bordereau_id.read(["amount_total_cotiz"]) - self.env["scop.bordereau.version"].create( - { - "bordereau_id": bordereau_id.id, - "date": self.date_regul, - "comment": self.comment, - "version": bordereau_id.version, - "liasse_fiscale_id_old": bordereau_id.liasse_fiscale_id.id, - "type_assiette": bordereau_id.type_assiette, - "montant_assiette": bordereau_id.montant_assiette, - "amount_total_cotiz": bordereau_id.amount_total_cotiz, - } - ) - - # CREATE REGUL - contribs = bordereau_id.invoice_ids.read_group( - [("id", "in", bordereau_id.invoice_ids.ids)], - ["type_contribution_id", "amount_total_signed"], - ["type_contribution_id"], - ) - - ur_hdf = self.env.ref("cgscop_partner.riga_14232").id - if partner_id.ur_id.id == ur_hdf: - amount_ur = self.amount_ur_hdf - product_ur = self.env.company.contribution_hdf_id - journal_ur = self.env.user.company_id.journal_ur_hdf_id - else: # ur = ur_med - amount_ur = self.amount_ur_med - product_ur = self.env.company.contribution_med_id - journal_ur = self.env.user.company_id.journal_ur_med_id - - cotiz_type = { - self.env.ref("cgscop_partner.riga_14397").id: [ - self.amount_cg, - self.env.company.contribution_cg_id, - self.env.company.contribution_journal_id, - partner_id.property_account_receivable_id.id, - ], - self.env.ref("cgscop_partner.riga_14398").id: [ - self.amount_fede_com, - self.env.company.contribution_fede_com_id, - self.env.user.company_id.journal_fede_com_id, - partner_id.property_account_receivable_id.id, - ], - self.env.ref("cgscop_partner.cotiz_fede_cae").id: [ - self.amount_fede_cae, - self.env.company.contribution_fede_cae_id, - self.env.user.company_id.journal_fede_cae_id, - partner_id.property_account_receivable_id.id, - ], - self.env.ref("cgscop_partner.riga_14399").id: [ - amount_ur, - product_ur, - journal_ur, - partner_id.property_account_receivable_id.id, - ], - } - - quarters = [ - bordereau_id.base_cotisation_cg.trimester_1, - bordereau_id.base_cotisation_cg.trimester_2, - bordereau_id.base_cotisation_cg.trimester_3, - bordereau_id.base_cotisation_cg.trimester_4, - ] - - for contrib in contribs: - type_cotiz = contrib.get("type_contribution_id")[0] - amount_cotiz_old = contrib.get("amount_total_signed") - amount_cotiz = cotiz_type.get(type_cotiz)[0] - refund_amount_total = self.env["scop.cotisation"].round_to_closest_multiple( - amount_cotiz_old - amount_cotiz, 4 - ) - product = cotiz_type.get(type_cotiz)[1] - type_invoice = False - - if refund_amount_total < 0: - type_invoice = "out_invoice" - refund_amount_total *= -1 - elif refund_amount_total > 0: - type_invoice = "out_refund" - refund_amount = refund_amount_total / int(bordereau_id.nb_quarter) - - if type_invoice: - for i in range(0, int(bordereau_id.nb_quarter)): - - if self.date_regul < quarters[i]: - date_due = quarters[i] - else: - date_due = self.date_regul - - self.env["account.move"].create( - { - "partner_id": partner_id.id, - "journal_id": cotiz_type.get(type_cotiz)[2].id, - "move_type": type_invoice, - "invoice_date": self.date_regul, - "date": self.date_regul, - "state": "draft", - "invoice_origin": bordereau_id.name, - "ref": self.comment, - "bordereau_id": bordereau_id.id, - "is_contribution": True, - "year": bordereau_id.year, - "liasse_fiscale_id": bordereau_id.liasse_fiscale_id.id, - "type_contribution_id": type_cotiz, - "payment_mode_id": bordereau_id.payment_mode_id.id, - "invoice_date_due": date_due, - "cotiz_quarter": str(i + 1), - "invoice_line_ids": [ - ( - 0, - None, - { - "product_id": product.id, - "account_id": product.property_account_income_id.id, - "tax_ids": [(6, 0, product.taxes_id.ids)], - "name": self.comment - + " - " - + str(i + 1) - + "/" - + bordereau_id.nb_quarter, - "price_unit": refund_amount, - "price_subtotal": refund_amount, - }, - ), - ], - } - ) - bordereau_id.update({"liasse_fiscale_id": self.liasse_fiscale_new_id.id}) - bordereau_id.invoice_ids.update( - {"liasse_fiscale_id": self.liasse_fiscale_new_id.id} + self.bordereau_id.update_bordereau_version( + date_regul=self.date_regul, + comment=self.comment, + liasse_fiscale_new_id=self.liasse_fiscale_new_id, )