|
| 1 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) |
| 2 | + |
| 3 | +from csv import QUOTE_MINIMAL, Dialect, register_dialect |
| 4 | + |
| 5 | +from odoo.addons.account_move_base_import.parser.file_parser import ( |
| 6 | + FileParser, |
| 7 | + float_or_zero, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +class AdyenDialect(Dialect): |
| 12 | + delimiter = "," |
| 13 | + quotechar = '"' |
| 14 | + doublequote = False |
| 15 | + skipinitialspace = False |
| 16 | + lineterminator = "\n" |
| 17 | + quoting = QUOTE_MINIMAL |
| 18 | + |
| 19 | + |
| 20 | +register_dialect("adyen_dialect", AdyenDialect) |
| 21 | + |
| 22 | + |
| 23 | +class AdyenFileParser(FileParser): |
| 24 | + def __init__(self, journal, ftype="csv", **kwargs): |
| 25 | + conversion_dict = { |
| 26 | + "Payment Method": str, |
| 27 | + "Type": str, |
| 28 | + "Gross Debit (GC)": float_or_zero, |
| 29 | + "Gross Credit (GC)": float_or_zero, |
| 30 | + "Net Debit (NC)": float_or_zero, |
| 31 | + "Commission (NC)": float_or_zero, |
| 32 | + "Markup (NC)": float_or_zero, |
| 33 | + "Scheme Fees (NC)": float_or_zero, |
| 34 | + "Interchange (NC)": float_or_zero, |
| 35 | + "Merchant Reference": str, |
| 36 | + } |
| 37 | + super().__init__( |
| 38 | + journal, |
| 39 | + ftype=ftype, |
| 40 | + extra_fields=conversion_dict, |
| 41 | + dialect=AdyenDialect, |
| 42 | + **kwargs |
| 43 | + ) |
| 44 | + self.commission_field = "Commission (NC)" |
| 45 | + |
| 46 | + @classmethod |
| 47 | + def parser_for(cls, parser_name): |
| 48 | + """ |
| 49 | + Used by the new_bank_statement_parser class factory. Return true if |
| 50 | + the providen name is generic_csvxls_so |
| 51 | + """ |
| 52 | + return parser_name == "adyen_cb_csvparser" |
| 53 | + |
| 54 | + def get_move_line_vals(self, line, *args, **kwargs): |
| 55 | + amount = line["Gross Credit (GC)"] or -line["Gross Debit (GC)"] |
| 56 | + res = { |
| 57 | + "name": line.get("Merchant Reference", ""), |
| 58 | + "credit": amount > 0.0 and amount or 0.0, |
| 59 | + "debit": amount < 0.0 and -amount or 0.0, |
| 60 | + } |
| 61 | + return res |
| 62 | + |
| 63 | + def _post(self, *args, **kwargs): |
| 64 | + res = super()._post(*args, **kwargs) |
| 65 | + # there are some fee line... not linked to a payment, we have to take it into |
| 66 | + # account |
| 67 | + self.extra_commission = 0.0 |
| 68 | + final_rows = [] |
| 69 | + for row in self.result_row_list: |
| 70 | + # account_move_import_base manage only once commission field when |
| 71 | + # adyen may have Commission (NC) with total commission or 3 fields with |
| 72 | + # detailed commission. => We fill the Commission (NC) in that case to have |
| 73 | + # a unique commission field |
| 74 | + if not row.get("Commission (NC)") and ( |
| 75 | + row.get("Markup (NC)") |
| 76 | + or row.get("Scheme Fees (NC)") |
| 77 | + or row.get("Interchange (NC)") |
| 78 | + ): |
| 79 | + row["Commission (NC)"] = ( |
| 80 | + row["Markup (NC)"] |
| 81 | + + row["Scheme Fees (NC)"] |
| 82 | + + row["Interchange (NC)"] |
| 83 | + ) |
| 84 | + if row.get("Type") in ( |
| 85 | + "Settled", |
| 86 | + "Refunded", |
| 87 | + "SentForSettle", |
| 88 | + "SentForRefund", |
| 89 | + ): |
| 90 | + final_rows.append(row) |
| 91 | + elif row["Type"] == "Fee": |
| 92 | + self.extra_commission += row["Net Debit (NC)"] |
| 93 | + create_date = row["Creation Date"].split(" ")[0] |
| 94 | + if not self.move_date or create_date > self.move_date: |
| 95 | + self.move_date = create_date |
| 96 | + self.result_row_list = final_rows |
| 97 | + return res |
| 98 | + |
| 99 | + |
| 100 | +class AdyenPaypalParser(AdyenFileParser): |
| 101 | + def __init__(self, journal, ftype="csv", **kwargs): |
| 102 | + super().__init__(journal, ftype=ftype, **kwargs) |
| 103 | + self.support_multi_moves = True |
| 104 | + |
| 105 | + @classmethod |
| 106 | + def parser_for(cls, parser_name): |
| 107 | + """ |
| 108 | + Used by the new_bank_statement_parser class factory. Return true if |
| 109 | + the providen name is generic_csvxls_so |
| 110 | + """ |
| 111 | + return parser_name == "adyen_multi_move_csvparser" |
0 commit comments