account_move.py 210 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. from contextlib import ExitStack, contextmanager
  4. from datetime import date, timedelta
  5. from dateutil.relativedelta import relativedelta
  6. from hashlib import sha256
  7. from json import dumps
  8. import re
  9. from textwrap import shorten
  10. from unittest.mock import patch
  11. from odoo import api, fields, models, _, Command
  12. from odoo.addons.base.models.decimal_precision import DecimalPrecision
  13. from odoo.addons.account.tools import format_rf_reference
  14. from odoo.exceptions import UserError, ValidationError, AccessError, RedirectWarning
  15. from odoo.tools import (
  16. date_utils,
  17. email_re,
  18. email_split,
  19. float_compare,
  20. float_is_zero,
  21. float_repr,
  22. format_amount,
  23. format_date,
  24. formatLang,
  25. frozendict,
  26. get_lang,
  27. is_html_empty,
  28. sql
  29. )
  30. MAX_HASH_VERSION = 3
  31. PAYMENT_STATE_SELECTION = [
  32. ('not_paid', 'Not Paid'),
  33. ('in_payment', 'In Payment'),
  34. ('paid', 'Paid'),
  35. ('partial', 'Partially Paid'),
  36. ('reversed', 'Reversed'),
  37. ('invoicing_legacy', 'Invoicing App Legacy'),
  38. ]
  39. TYPE_REVERSE_MAP = {
  40. 'entry': 'entry',
  41. 'out_invoice': 'out_refund',
  42. 'out_refund': 'entry',
  43. 'in_invoice': 'in_refund',
  44. 'in_refund': 'entry',
  45. 'out_receipt': 'out_refund',
  46. 'in_receipt': 'in_refund',
  47. }
  48. EMPTY = object()
  49. class AccountMove(models.Model):
  50. _name = "account.move"
  51. _inherit = ['portal.mixin', 'mail.thread', 'mail.activity.mixin', 'sequence.mixin']
  52. _description = "Journal Entry"
  53. _order = 'date desc, name desc, id desc'
  54. _mail_post_access = 'read'
  55. _check_company_auto = True
  56. _sequence_index = "journal_id"
  57. _rec_names_search = ['name', 'partner_id.name', 'ref']
  58. @property
  59. def _sequence_monthly_regex(self):
  60. return self.journal_id.sequence_override_regex or super()._sequence_monthly_regex
  61. @property
  62. def _sequence_yearly_regex(self):
  63. return self.journal_id.sequence_override_regex or super()._sequence_yearly_regex
  64. @property
  65. def _sequence_fixed_regex(self):
  66. return self.journal_id.sequence_override_regex or super()._sequence_fixed_regex
  67. # ==============================================================================================
  68. # JOURNAL ENTRY
  69. # ==============================================================================================
  70. # === Accounting fields === #
  71. name = fields.Char(
  72. string='Number',
  73. compute='_compute_name', inverse='_inverse_name', readonly=False, store=True,
  74. copy=False,
  75. tracking=True,
  76. index='trigram',
  77. )
  78. ref = fields.Char(string='Reference', copy=False, tracking=True)
  79. date = fields.Date(
  80. string='Date',
  81. index=True,
  82. compute='_compute_date', store=True, required=True, readonly=False, precompute=True,
  83. states={'posted': [('readonly', True)], 'cancel': [('readonly', True)]},
  84. copy=False,
  85. tracking=True,
  86. )
  87. state = fields.Selection(
  88. selection=[
  89. ('draft', 'Draft'),
  90. ('posted', 'Posted'),
  91. ('cancel', 'Cancelled'),
  92. ],
  93. string='Status',
  94. required=True,
  95. readonly=True,
  96. copy=False,
  97. tracking=True,
  98. default='draft',
  99. )
  100. move_type = fields.Selection(
  101. selection=[
  102. ('entry', 'Journal Entry'),
  103. ('out_invoice', 'Customer Invoice'),
  104. ('out_refund', 'Customer Credit Note'),
  105. ('in_invoice', 'Vendor Bill'),
  106. ('in_refund', 'Vendor Credit Note'),
  107. ('out_receipt', 'Sales Receipt'),
  108. ('in_receipt', 'Purchase Receipt'),
  109. ],
  110. string='Type',
  111. required=True,
  112. readonly=True,
  113. tracking=True,
  114. change_default=True,
  115. index=True,
  116. default="entry",
  117. )
  118. is_storno = fields.Boolean(
  119. compute='_compute_is_storno', store=True, readonly=False,
  120. copy=False,
  121. )
  122. journal_id = fields.Many2one(
  123. 'account.journal',
  124. string='Journal',
  125. compute='_compute_journal_id', inverse='_inverse_journal_id', store=True, readonly=False, precompute=True,
  126. required=True,
  127. states={'draft': [('readonly', False)]},
  128. check_company=True,
  129. domain="[('id', 'in', suitable_journal_ids)]",
  130. )
  131. company_id = fields.Many2one(
  132. comodel_name='res.company',
  133. string='Company',
  134. compute='_compute_company_id', inverse='_inverse_company_id', store=True, readonly=False, precompute=True,
  135. index=True,
  136. )
  137. line_ids = fields.One2many(
  138. 'account.move.line',
  139. 'move_id',
  140. string='Journal Items',
  141. copy=True,
  142. readonly=True,
  143. states={'draft': [('readonly', False)]},
  144. )
  145. # === Payment fields === #
  146. payment_id = fields.Many2one(
  147. comodel_name='account.payment',
  148. string="Payment",
  149. index='btree_not_null',
  150. copy=False,
  151. check_company=True,
  152. )
  153. # === Statement fields === #
  154. statement_line_id = fields.Many2one(
  155. comodel_name='account.bank.statement.line',
  156. string="Statement Line",
  157. copy=False,
  158. check_company=True,
  159. )
  160. # === Cash basis feature fields === #
  161. # used to keep track of the tax cash basis reconciliation. This is needed
  162. # when cancelling the source: it will post the inverse journal entry to
  163. # cancel that part too.
  164. tax_cash_basis_rec_id = fields.Many2one(
  165. comodel_name='account.partial.reconcile',
  166. string='Tax Cash Basis Entry of',
  167. )
  168. tax_cash_basis_origin_move_id = fields.Many2one(
  169. comodel_name='account.move',
  170. index='btree_not_null',
  171. string="Cash Basis Origin",
  172. readonly=True,
  173. help="The journal entry from which this tax cash basis journal entry has been created.",
  174. )
  175. tax_cash_basis_created_move_ids = fields.One2many(
  176. string="Cash Basis Entries",
  177. comodel_name='account.move',
  178. inverse_name='tax_cash_basis_origin_move_id',
  179. help="The cash basis entries created from the taxes on this entry, when reconciling its lines.",
  180. )
  181. # used by cash basis taxes, telling the lines of the move are always
  182. # exigible. This happens if the move contains no payable or receivable line.
  183. always_tax_exigible = fields.Boolean(compute='_compute_always_tax_exigible', store=True, readonly=False)
  184. # === Misc fields === #
  185. auto_post = fields.Selection(
  186. string='Auto-post',
  187. selection=[
  188. ('no', 'No'),
  189. ('at_date', 'At Date'),
  190. ('monthly', 'Monthly'),
  191. ('quarterly', 'Quarterly'),
  192. ('yearly', 'Yearly'),
  193. ],
  194. default='no', required=True, copy=False,
  195. help='Specify whether this entry is posted automatically on its accounting date, and any similar recurring invoices.')
  196. auto_post_until = fields.Date(
  197. string='Auto-post until',
  198. copy=False,
  199. compute='_compute_auto_post_until', store=True, readonly=False,
  200. help='This recurring move will be posted up to and including this date.')
  201. auto_post_origin_id = fields.Many2one(
  202. comodel_name='account.move',
  203. string='First recurring entry',
  204. readonly=True, copy=False,
  205. )
  206. hide_post_button = fields.Boolean(compute='_compute_hide_post_button', readonly=True)
  207. to_check = fields.Boolean(
  208. string='To Check',
  209. tracking=True,
  210. help="If this checkbox is ticked, it means that the user was not sure of all the related "
  211. "information at the time of the creation of the move and that the move needs to be "
  212. "checked again.",
  213. )
  214. posted_before = fields.Boolean(copy=False)
  215. suitable_journal_ids = fields.Many2many(
  216. 'account.journal',
  217. compute='_compute_suitable_journal_ids',
  218. )
  219. highest_name = fields.Char(compute='_compute_highest_name')
  220. made_sequence_hole = fields.Boolean(compute='_compute_made_sequence_hole')
  221. show_name_warning = fields.Boolean(store=False)
  222. type_name = fields.Char('Type Name', compute='_compute_type_name')
  223. country_code = fields.Char(related='company_id.account_fiscal_country_id.code', readonly=True)
  224. attachment_ids = fields.One2many('ir.attachment', 'res_id', domain=[('res_model', '=', 'account.move')], string='Attachments')
  225. # === Hash Fields === #
  226. restrict_mode_hash_table = fields.Boolean(related='journal_id.restrict_mode_hash_table')
  227. secure_sequence_number = fields.Integer(string="Inalteralbility No Gap Sequence #", readonly=True, copy=False, index=True)
  228. inalterable_hash = fields.Char(string="Inalterability Hash", readonly=True, copy=False)
  229. string_to_hash = fields.Char(compute='_compute_string_to_hash', readonly=True)
  230. # ==============================================================================================
  231. # INVOICE
  232. # ==============================================================================================
  233. invoice_line_ids = fields.One2many( # /!\ invoice_line_ids is just a subset of line_ids.
  234. 'account.move.line',
  235. 'move_id',
  236. string='Invoice lines',
  237. copy=False,
  238. readonly=True,
  239. domain=[('display_type', 'in', ('product', 'line_section', 'line_note'))],
  240. states={'draft': [('readonly', False)]},
  241. )
  242. # === Date fields === #
  243. invoice_date = fields.Date(
  244. string='Invoice/Bill Date',
  245. readonly=True,
  246. states={'draft': [('readonly', False)]},
  247. index=True,
  248. copy=False,
  249. )
  250. invoice_date_due = fields.Date(
  251. string='Due Date',
  252. compute='_compute_invoice_date_due', store=True, readonly=False,
  253. states={'draft': [('readonly', False)]},
  254. index=True,
  255. copy=False,
  256. )
  257. invoice_payment_term_id = fields.Many2one(
  258. comodel_name='account.payment.term',
  259. string='Payment Terms',
  260. compute='_compute_invoice_payment_term_id', store=True, readonly=False, precompute=True,
  261. states={'posted': [('readonly', True)], 'cancel': [('readonly', True)]},
  262. check_company=True,
  263. )
  264. needed_terms = fields.Binary(compute='_compute_needed_terms', exportable=False)
  265. needed_terms_dirty = fields.Boolean(compute='_compute_needed_terms')
  266. # === Partner fields === #
  267. partner_id = fields.Many2one(
  268. 'res.partner',
  269. string='Partner',
  270. readonly=True,
  271. tracking=True,
  272. states={'draft': [('readonly', False)]},
  273. inverse='_inverse_partner_id',
  274. check_company=True,
  275. change_default=True,
  276. ondelete='restrict',
  277. )
  278. commercial_partner_id = fields.Many2one(
  279. 'res.partner',
  280. string='Commercial Entity',
  281. compute='_compute_commercial_partner_id', store=True, readonly=True,
  282. ondelete='restrict',
  283. )
  284. partner_shipping_id = fields.Many2one(
  285. comodel_name='res.partner',
  286. string='Delivery Address',
  287. compute='_compute_partner_shipping_id', store=True, readonly=False, precompute=True,
  288. domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",
  289. help="Delivery address for current invoice.",
  290. )
  291. partner_bank_id = fields.Many2one(
  292. 'res.partner.bank',
  293. string='Recipient Bank',
  294. compute='_compute_partner_bank_id', store=True, readonly=False,
  295. help="Bank Account Number to which the invoice will be paid. "
  296. "A Company bank account if this is a Customer Invoice or Vendor Credit Note, "
  297. "otherwise a Partner bank account number.",
  298. check_company=True,
  299. tracking=True,
  300. )
  301. fiscal_position_id = fields.Many2one(
  302. 'account.fiscal.position',
  303. string='Fiscal Position',
  304. check_company=True,
  305. compute='_compute_fiscal_position_id', store=True, readonly=False, precompute=True,
  306. states={'posted': [('readonly', True)], 'cancel': [('readonly', True)]},
  307. domain="[('company_id', '=', company_id)]",
  308. ondelete="restrict",
  309. help="Fiscal positions are used to adapt taxes and accounts for particular "
  310. "customers or sales orders/invoices. The default value comes from the customer.",
  311. )
  312. # === Payment fields === #
  313. payment_reference = fields.Char(
  314. string='Payment Reference',
  315. index='trigram',
  316. copy=False,
  317. help="The payment reference to set on journal items.",
  318. tracking=True,
  319. compute='_compute_payment_reference', inverse='_inverse_payment_reference', store=True, readonly=False,
  320. )
  321. display_qr_code = fields.Boolean(
  322. string="Display QR-code",
  323. compute='_compute_display_qr_code',
  324. )
  325. qr_code_method = fields.Selection(
  326. string="Payment QR-code", copy=False,
  327. selection=lambda self: self.env['res.partner.bank'].get_available_qr_methods_in_sequence(),
  328. help="Type of QR-code to be generated for the payment of this invoice, "
  329. "when printing it. If left blank, the first available and usable method "
  330. "will be used.",
  331. )
  332. # === Payment widget fields === #
  333. invoice_outstanding_credits_debits_widget = fields.Binary(
  334. groups="account.group_account_invoice,account.group_account_readonly",
  335. compute='_compute_payments_widget_to_reconcile_info',
  336. exportable=False,
  337. )
  338. invoice_has_outstanding = fields.Boolean(
  339. groups="account.group_account_invoice,account.group_account_readonly",
  340. compute='_compute_payments_widget_to_reconcile_info',
  341. )
  342. invoice_payments_widget = fields.Binary(
  343. groups="account.group_account_invoice,account.group_account_readonly",
  344. compute='_compute_payments_widget_reconciled_info',
  345. exportable=False,
  346. )
  347. # === Currency fields === #
  348. company_currency_id = fields.Many2one(
  349. string='Company Currency',
  350. related='company_id.currency_id', readonly=True,
  351. )
  352. currency_id = fields.Many2one(
  353. 'res.currency',
  354. string='Currency',
  355. tracking=True,
  356. required=True,
  357. compute='_compute_currency_id', inverse='_inverse_currency_id', store=True, readonly=False, precompute=True,
  358. states={'posted': [('readonly', True)], 'cancel': [('readonly', True)]},
  359. )
  360. # === Amount fields === #
  361. direction_sign = fields.Integer(
  362. compute='_compute_direction_sign',
  363. help="Multiplicator depending on the document type, to convert a price into a balance",
  364. )
  365. amount_untaxed = fields.Monetary(
  366. string='Untaxed Amount',
  367. compute='_compute_amount', store=True, readonly=True,
  368. tracking=True,
  369. )
  370. amount_tax = fields.Monetary(
  371. string='Tax',
  372. compute='_compute_amount', store=True, readonly=True,
  373. )
  374. amount_total = fields.Monetary(
  375. string='Total',
  376. compute='_compute_amount', store=True, readonly=True,
  377. inverse='_inverse_amount_total',
  378. )
  379. amount_residual = fields.Monetary(
  380. string='Amount Due',
  381. compute='_compute_amount', store=True,
  382. )
  383. amount_untaxed_signed = fields.Monetary(
  384. string='Untaxed Amount Signed',
  385. compute='_compute_amount', store=True, readonly=True,
  386. currency_field='company_currency_id',
  387. )
  388. amount_tax_signed = fields.Monetary(
  389. string='Tax Signed',
  390. compute='_compute_amount', store=True, readonly=True,
  391. currency_field='company_currency_id',
  392. )
  393. amount_total_signed = fields.Monetary(
  394. string='Total Signed',
  395. compute='_compute_amount', store=True, readonly=True,
  396. currency_field='company_currency_id',
  397. )
  398. amount_total_in_currency_signed = fields.Monetary(
  399. string='Total in Currency Signed',
  400. compute='_compute_amount', store=True, readonly=True,
  401. currency_field='currency_id',
  402. )
  403. amount_residual_signed = fields.Monetary(
  404. string='Amount Due Signed',
  405. compute='_compute_amount', store=True,
  406. currency_field='company_currency_id',
  407. )
  408. tax_totals = fields.Binary(
  409. string="Invoice Totals",
  410. compute='_compute_tax_totals',
  411. inverse='_inverse_tax_totals',
  412. help='Edit Tax amounts if you encounter rounding issues.',
  413. exportable=False,
  414. )
  415. payment_state = fields.Selection(
  416. selection=PAYMENT_STATE_SELECTION,
  417. string="Payment Status",
  418. compute='_compute_payment_state', store=True, readonly=True,
  419. copy=False,
  420. tracking=True,
  421. )
  422. # === Reverse feature fields === #
  423. reversed_entry_id = fields.Many2one(
  424. comodel_name='account.move',
  425. string="Reversal of",
  426. index='btree_not_null',
  427. readonly=True,
  428. copy=False,
  429. check_company=True,
  430. )
  431. reversal_move_id = fields.One2many('account.move', 'reversed_entry_id')
  432. # === Vendor bill fields === #
  433. invoice_vendor_bill_id = fields.Many2one(
  434. 'account.move',
  435. store=False,
  436. check_company=True,
  437. string='Vendor Bill',
  438. help="Auto-complete from a past bill.",
  439. )
  440. invoice_source_email = fields.Char(string='Source Email', tracking=True)
  441. invoice_partner_display_name = fields.Char(compute='_compute_invoice_partner_display_info', store=True)
  442. # === Fiduciary mode fields === #
  443. quick_edit_mode = fields.Boolean(compute='_compute_quick_edit_mode')
  444. quick_edit_total_amount = fields.Monetary(
  445. string='Total (Tax inc.)',
  446. help='Use this field to encode the total amount of the invoice.\n'
  447. 'Odoo will automatically create one invoice line with default values to match it.',
  448. )
  449. quick_encoding_vals = fields.Binary(compute='_compute_quick_encoding_vals', exportable=False)
  450. # === Misc Information === #
  451. narration = fields.Html(
  452. string='Terms and Conditions',
  453. compute='_compute_narration', store=True, readonly=False,
  454. )
  455. is_move_sent = fields.Boolean(
  456. readonly=True,
  457. default=False,
  458. copy=False,
  459. tracking=True,
  460. help="It indicates that the invoice/payment has been sent.",
  461. )
  462. invoice_user_id = fields.Many2one(
  463. string='Salesperson',
  464. comodel_name='res.users',
  465. copy=False,
  466. tracking=True,
  467. default=lambda self: self.env.user,
  468. )
  469. # Technical field used to fit the generic behavior in mail templates.
  470. user_id = fields.Many2one(string='User', related='invoice_user_id')
  471. invoice_origin = fields.Char(
  472. string='Origin',
  473. readonly=True,
  474. tracking=True,
  475. help="The document(s) that generated the invoice.",
  476. )
  477. invoice_incoterm_id = fields.Many2one(
  478. comodel_name='account.incoterms',
  479. string='Incoterm',
  480. default=lambda self: self.env.company.incoterm_id,
  481. help='International Commercial Terms are a series of predefined commercial '
  482. 'terms used in international transactions.',
  483. )
  484. invoice_cash_rounding_id = fields.Many2one(
  485. comodel_name='account.cash.rounding',
  486. string='Cash Rounding Method',
  487. readonly=True,
  488. states={'draft': [('readonly', False)]},
  489. help='Defines the smallest coinage of the currency that can be used to pay by cash.',
  490. )
  491. # === Display purpose fields === #
  492. # used to have a dynamic domain on journal / taxes in the form view.
  493. invoice_filter_type_domain = fields.Char(compute='_compute_invoice_filter_type_domain')
  494. bank_partner_id = fields.Many2one(
  495. comodel_name='res.partner',
  496. compute='_compute_bank_partner_id',
  497. help='Technical field to get the domain on the bank',
  498. )
  499. # used to display a message when the invoice's accounting date is prior of the tax lock date
  500. tax_lock_date_message = fields.Char(compute='_compute_tax_lock_date_message')
  501. # used for tracking the status of the currency
  502. display_inactive_currency_warning = fields.Boolean(compute="_compute_display_inactive_currency_warning")
  503. tax_country_id = fields.Many2one( # used to filter the available taxes depending on the fiscal country and fiscal position.
  504. comodel_name='res.country',
  505. compute='_compute_tax_country_id',
  506. )
  507. tax_country_code = fields.Char(compute="_compute_tax_country_code")
  508. has_reconciled_entries = fields.Boolean(compute="_compute_has_reconciled_entries")
  509. show_reset_to_draft_button = fields.Boolean(compute='_compute_show_reset_to_draft_button')
  510. partner_credit_warning = fields.Text(
  511. compute='_compute_partner_credit_warning',
  512. groups="account.group_account_invoice,account.group_account_readonly",
  513. )
  514. duplicated_ref_ids = fields.Many2many(comodel_name='account.move', compute='_compute_duplicated_ref_ids')
  515. # used to display the various dates and amount dues on the invoice's PDF
  516. payment_term_details = fields.Binary(compute="_compute_payment_term_details", exportable=False)
  517. show_payment_term_details = fields.Boolean(compute="_compute_show_payment_term_details")
  518. show_discount_details = fields.Boolean(compute="_compute_show_payment_term_details")
  519. def _auto_init(self):
  520. super()._auto_init()
  521. self.env.cr.execute("""
  522. CREATE INDEX IF NOT EXISTS account_move_to_check_idx
  523. ON account_move(journal_id) WHERE to_check = true;
  524. CREATE INDEX IF NOT EXISTS account_move_payment_idx
  525. ON account_move(journal_id, state, payment_state, move_type, date);
  526. -- Used for gap detection in list views
  527. CREATE INDEX IF NOT EXISTS account_move_sequence_index3
  528. ON account_move (journal_id, sequence_prefix desc, (sequence_number+1) desc);
  529. """)
  530. # -------------------------------------------------------------------------
  531. # COMPUTE METHODS
  532. # -------------------------------------------------------------------------
  533. def _compute_payment_reference(self):
  534. for move in self.filtered(lambda m: (
  535. m.state == 'posted'
  536. and m.move_type == 'out_invoice'
  537. and not m.payment_reference
  538. )):
  539. move.payment_reference = move._get_invoice_computed_reference()
  540. self._inverse_payment_reference()
  541. @api.depends('invoice_date', 'company_id')
  542. def _compute_date(self):
  543. for move in self:
  544. if not move.invoice_date:
  545. if not move.date:
  546. move.date = fields.Date.context_today(self)
  547. continue
  548. accounting_date = move.invoice_date
  549. if not move.is_sale_document(include_receipts=True):
  550. accounting_date = move._get_accounting_date(move.invoice_date, move._affect_tax_report())
  551. if accounting_date and accounting_date != move.date:
  552. move.date = accounting_date
  553. # might be protected because `_get_accounting_date` requires the `name`
  554. self.env.add_to_compute(self._fields['name'], move)
  555. @api.depends('auto_post')
  556. def _compute_auto_post_until(self):
  557. for record in self:
  558. if record.auto_post in ('no', 'at_date'):
  559. record.auto_post_until = False
  560. @api.depends('date', 'auto_post')
  561. def _compute_hide_post_button(self):
  562. for record in self:
  563. record.hide_post_button = record.state != 'draft' \
  564. or record.auto_post != 'no' and record.date > fields.Date.today()
  565. @api.depends('journal_id')
  566. def _compute_company_id(self):
  567. for move in self:
  568. company_id = move.journal_id.company_id or self.env.company
  569. if company_id != move.company_id:
  570. move.company_id = company_id
  571. @api.depends('move_type')
  572. def _compute_journal_id(self):
  573. for record in self.filtered(lambda r: r.journal_id.type not in r._get_valid_journal_types()):
  574. record.journal_id = record._search_default_journal()
  575. def _get_valid_journal_types(self):
  576. if self.is_sale_document(include_receipts=True):
  577. return ['sale']
  578. elif self.is_purchase_document(include_receipts=True):
  579. return ['purchase']
  580. elif self.payment_id or self.env.context.get('is_payment'):
  581. return ['bank', 'cash']
  582. return ['general']
  583. def _search_default_journal(self):
  584. if self.payment_id and self.payment_id.journal_id:
  585. return self.payment_id.journal_id
  586. if self.statement_line_id and self.statement_line_id.journal_id:
  587. return self.statement_line_id.journal_id
  588. if self.statement_line_ids.statement_id.journal_id:
  589. return self.statement_line_ids.statement_id.journal_id[:1]
  590. journal_types = self._get_valid_journal_types()
  591. company_id = (self.company_id or self.env.company).id
  592. domain = [('company_id', '=', company_id), ('type', 'in', journal_types)]
  593. journal = None
  594. # the currency is not a hard dependence, it triggers via manual add_to_compute
  595. # avoid computing the currency before all it's dependences are set (like the journal...)
  596. if self.env.cache.contains(self, self._fields['currency_id']):
  597. currency_id = self.currency_id.id or self._context.get('default_currency_id')
  598. if currency_id and currency_id != self.company_id.currency_id.id:
  599. currency_domain = domain + [('currency_id', '=', currency_id)]
  600. journal = self.env['account.journal'].search(currency_domain, limit=1)
  601. if not journal:
  602. journal = self.env['account.journal'].search(domain, limit=1)
  603. if not journal:
  604. company = self.env['res.company'].browse(company_id)
  605. error_msg = _(
  606. "No journal could be found in company %(company_name)s for any of those types: %(journal_types)s",
  607. company_name=company.display_name,
  608. journal_types=', '.join(journal_types),
  609. )
  610. raise UserError(error_msg)
  611. return journal
  612. @api.depends('move_type')
  613. def _compute_is_storno(self):
  614. for move in self:
  615. move.is_storno = move.is_storno or (move.move_type in ('out_refund', 'in_refund') and move.company_id.account_storno)
  616. @api.depends('company_id', 'invoice_filter_type_domain')
  617. def _compute_suitable_journal_ids(self):
  618. for m in self:
  619. journal_type = m.invoice_filter_type_domain or 'general'
  620. company_id = m.company_id.id or self.env.company.id
  621. domain = [('company_id', '=', company_id), ('type', '=', journal_type)]
  622. m.suitable_journal_ids = self.env['account.journal'].search(domain)
  623. @api.depends('posted_before', 'state', 'journal_id', 'date')
  624. def _compute_name(self):
  625. self = self.sorted(lambda m: (m.date, m.ref or '', m.id))
  626. for move in self:
  627. move_has_name = move.name and move.name != '/'
  628. if move_has_name or move.state != 'posted':
  629. if not move.posted_before and not move._sequence_matches_date():
  630. if move._get_last_sequence(lock=False):
  631. # The name does not match the date and the move is not the first in the period:
  632. # Reset to draft
  633. move.name = False
  634. continue
  635. else:
  636. if move_has_name and move.posted_before or not move_has_name and move._get_last_sequence(lock=False):
  637. # The move either
  638. # - has a name and was posted before, or
  639. # - doesn't have a name, but is not the first in the period
  640. # so we don't recompute the name
  641. continue
  642. if move.date and (not move_has_name or not move._sequence_matches_date()):
  643. move._set_next_sequence()
  644. self.filtered(lambda m: not m.name and not move.quick_edit_mode).name = '/'
  645. self._inverse_name()
  646. @api.depends('journal_id', 'date')
  647. def _compute_highest_name(self):
  648. for record in self:
  649. record.highest_name = record._get_last_sequence(lock=False)
  650. @api.depends('name', 'journal_id')
  651. def _compute_made_sequence_hole(self):
  652. self.env.cr.execute("""
  653. SELECT this.id
  654. FROM account_move this
  655. JOIN res_company company ON company.id = this.company_id
  656. LEFT JOIN account_move other ON this.journal_id = other.journal_id
  657. AND this.sequence_prefix = other.sequence_prefix
  658. AND this.sequence_number = other.sequence_number + 1
  659. WHERE other.id IS NULL
  660. AND this.sequence_number != 1
  661. AND this.name != '/'
  662. AND this.id = ANY(%(move_ids)s)
  663. """, {
  664. 'move_ids': self.ids,
  665. })
  666. made_sequence_hole = set(r[0] for r in self.env.cr.fetchall())
  667. for move in self:
  668. move.made_sequence_hole = move.id in made_sequence_hole
  669. @api.depends('move_type')
  670. def _compute_type_name(self):
  671. type_name_mapping = dict(
  672. self._fields['move_type']._description_selection(self.env),
  673. out_invoice=_('Invoice'),
  674. out_refund=_('Credit Note'),
  675. )
  676. for record in self:
  677. record.type_name = type_name_mapping[record.move_type]
  678. @api.depends('line_ids.account_id.account_type')
  679. def _compute_always_tax_exigible(self):
  680. for record in self:
  681. # We need to check is_invoice as well because always_tax_exigible is used to
  682. # set the tags as well, during the encoding. So, if no receivable/payable
  683. # line has been created yet, the invoice would be detected as always exigible,
  684. # and set the tags on some lines ; which would be wrong.
  685. record.always_tax_exigible = not record.is_invoice(True) \
  686. and not record._collect_tax_cash_basis_values()
  687. @api.depends('partner_id')
  688. def _compute_commercial_partner_id(self):
  689. for move in self:
  690. move.commercial_partner_id = move.partner_id.commercial_partner_id
  691. @api.depends('partner_id')
  692. def _compute_partner_shipping_id(self):
  693. for move in self:
  694. if move.is_invoice(include_receipts=True):
  695. addr = move.partner_id.address_get(['delivery'])
  696. move.partner_shipping_id = addr and addr.get('delivery')
  697. else:
  698. move.partner_shipping_id = False
  699. @api.depends('partner_id', 'partner_shipping_id', 'company_id')
  700. def _compute_fiscal_position_id(self):
  701. for move in self:
  702. delivery_partner = self.env['res.partner'].browse(
  703. move.partner_shipping_id.id
  704. or move.partner_id.address_get(['delivery'])['delivery']
  705. )
  706. move.fiscal_position_id = self.env['account.fiscal.position'].with_company(move.company_id)._get_fiscal_position(
  707. move.partner_id, delivery=delivery_partner)
  708. @api.depends('bank_partner_id')
  709. def _compute_partner_bank_id(self):
  710. for move in self:
  711. bank_ids = move.bank_partner_id.bank_ids.filtered(
  712. lambda bank: not bank.company_id or bank.company_id == move.company_id)
  713. move.partner_bank_id = bank_ids[0] if bank_ids else False
  714. @api.depends('partner_id')
  715. def _compute_invoice_payment_term_id(self):
  716. for move in self:
  717. if move.is_sale_document(include_receipts=True) and move.partner_id.property_payment_term_id:
  718. move.invoice_payment_term_id = move.partner_id.property_payment_term_id
  719. elif move.is_purchase_document(include_receipts=True) and move.partner_id.property_supplier_payment_term_id:
  720. move.invoice_payment_term_id = move.partner_id.property_supplier_payment_term_id
  721. else:
  722. move.invoice_payment_term_id = False
  723. @api.depends('needed_terms')
  724. def _compute_invoice_date_due(self):
  725. today = fields.Date.context_today(self)
  726. for move in self:
  727. move.invoice_date_due = move.needed_terms and max(
  728. (k['date_maturity'] for k in move.needed_terms.keys() if k),
  729. default=False,
  730. ) or move.invoice_date_due or today
  731. @api.depends('journal_id', 'statement_line_id')
  732. def _compute_currency_id(self):
  733. for invoice in self:
  734. currency = (
  735. invoice.statement_line_id.foreign_currency_id
  736. or invoice.journal_id.currency_id
  737. or invoice.currency_id
  738. or invoice.journal_id.company_id.currency_id
  739. )
  740. invoice.currency_id = currency
  741. @api.depends('move_type')
  742. def _compute_direction_sign(self):
  743. for invoice in self:
  744. if invoice.move_type == 'entry' or invoice.is_outbound():
  745. invoice.direction_sign = 1
  746. else:
  747. invoice.direction_sign = -1
  748. @api.depends(
  749. 'line_ids.matched_debit_ids.debit_move_id.move_id.payment_id.is_matched',
  750. 'line_ids.matched_debit_ids.debit_move_id.move_id.line_ids.amount_residual',
  751. 'line_ids.matched_debit_ids.debit_move_id.move_id.line_ids.amount_residual_currency',
  752. 'line_ids.matched_credit_ids.credit_move_id.move_id.payment_id.is_matched',
  753. 'line_ids.matched_credit_ids.credit_move_id.move_id.line_ids.amount_residual',
  754. 'line_ids.matched_credit_ids.credit_move_id.move_id.line_ids.amount_residual_currency',
  755. 'line_ids.balance',
  756. 'line_ids.currency_id',
  757. 'line_ids.amount_currency',
  758. 'line_ids.amount_residual',
  759. 'line_ids.amount_residual_currency',
  760. 'line_ids.payment_id.state',
  761. 'line_ids.full_reconcile_id',
  762. 'state')
  763. def _compute_amount(self):
  764. for move in self:
  765. total_untaxed, total_untaxed_currency = 0.0, 0.0
  766. total_tax, total_tax_currency = 0.0, 0.0
  767. total_residual, total_residual_currency = 0.0, 0.0
  768. total, total_currency = 0.0, 0.0
  769. for line in move.line_ids:
  770. if move.is_invoice(True):
  771. # === Invoices ===
  772. if line.display_type == 'tax' or (line.display_type == 'rounding' and line.tax_repartition_line_id):
  773. # Tax amount.
  774. total_tax += line.balance
  775. total_tax_currency += line.amount_currency
  776. total += line.balance
  777. total_currency += line.amount_currency
  778. elif line.display_type in ('product', 'rounding'):
  779. # Untaxed amount.
  780. total_untaxed += line.balance
  781. total_untaxed_currency += line.amount_currency
  782. total += line.balance
  783. total_currency += line.amount_currency
  784. elif line.display_type == 'payment_term':
  785. # Residual amount.
  786. total_residual += line.amount_residual
  787. total_residual_currency += line.amount_residual_currency
  788. else:
  789. # === Miscellaneous journal entry ===
  790. if line.debit:
  791. total += line.balance
  792. total_currency += line.amount_currency
  793. sign = move.direction_sign
  794. move.amount_untaxed = sign * total_untaxed_currency
  795. move.amount_tax = sign * total_tax_currency
  796. move.amount_total = sign * total_currency
  797. move.amount_residual = -sign * total_residual_currency
  798. move.amount_untaxed_signed = -total_untaxed
  799. move.amount_tax_signed = -total_tax
  800. move.amount_total_signed = abs(total) if move.move_type == 'entry' else -total
  801. move.amount_residual_signed = total_residual
  802. move.amount_total_in_currency_signed = abs(move.amount_total) if move.move_type == 'entry' else -(sign * move.amount_total)
  803. @api.depends('amount_residual', 'move_type', 'state', 'company_id')
  804. def _compute_payment_state(self):
  805. stored_ids = tuple(self.ids)
  806. if stored_ids:
  807. self.env['account.partial.reconcile'].flush_model()
  808. self.env['account.payment'].flush_model(['is_matched'])
  809. queries = []
  810. for source_field, counterpart_field in (('debit', 'credit'), ('credit', 'debit')):
  811. queries.append(f'''
  812. SELECT
  813. source_line.id AS source_line_id,
  814. source_line.move_id AS source_move_id,
  815. account.account_type AS source_line_account_type,
  816. ARRAY_AGG(counterpart_move.move_type) AS counterpart_move_types,
  817. COALESCE(BOOL_AND(COALESCE(pay.is_matched, FALSE))
  818. FILTER (WHERE counterpart_move.payment_id IS NOT NULL), TRUE) AS all_payments_matched,
  819. BOOL_OR(COALESCE(BOOL(pay.id), FALSE)) as has_payment,
  820. BOOL_OR(COALESCE(BOOL(counterpart_move.statement_line_id), FALSE)) as has_st_line
  821. FROM account_partial_reconcile part
  822. JOIN account_move_line source_line ON source_line.id = part.{source_field}_move_id
  823. JOIN account_account account ON account.id = source_line.account_id
  824. JOIN account_move_line counterpart_line ON counterpart_line.id = part.{counterpart_field}_move_id
  825. JOIN account_move counterpart_move ON counterpart_move.id = counterpart_line.move_id
  826. LEFT JOIN account_payment pay ON pay.id = counterpart_move.payment_id
  827. WHERE source_line.move_id IN %s AND counterpart_line.move_id != source_line.move_id
  828. GROUP BY source_line_id, source_move_id, source_line_account_type
  829. ''')
  830. self._cr.execute(' UNION ALL '.join(queries), [stored_ids, stored_ids])
  831. payment_data = defaultdict(lambda: [])
  832. for row in self._cr.dictfetchall():
  833. payment_data[row['source_move_id']].append(row)
  834. else:
  835. payment_data = {}
  836. for invoice in self:
  837. if invoice.payment_state == 'invoicing_legacy':
  838. # invoicing_legacy state is set via SQL when setting setting field
  839. # invoicing_switch_threshold (defined in account_accountant).
  840. # The only way of going out of this state is through this setting,
  841. # so we don't recompute it here.
  842. continue
  843. currencies = invoice._get_lines_onchange_currency().currency_id
  844. currency = currencies if len(currencies) == 1 else invoice.company_id.currency_id
  845. reconciliation_vals = payment_data.get(invoice.id, [])
  846. payment_state_matters = invoice.is_invoice(True)
  847. # Restrict on 'receivable'/'payable' lines for invoices/expense entries.
  848. if payment_state_matters:
  849. reconciliation_vals = [x for x in reconciliation_vals if x['source_line_account_type'] in ('asset_receivable', 'liability_payable')]
  850. new_pmt_state = 'not_paid'
  851. if invoice.state == 'posted':
  852. # Posted invoice/expense entry.
  853. if payment_state_matters:
  854. if currency.is_zero(invoice.amount_residual):
  855. if any(x['has_payment'] or x['has_st_line'] for x in reconciliation_vals):
  856. # Check if the invoice/expense entry is fully paid or 'in_payment'.
  857. if all(x['all_payments_matched'] for x in reconciliation_vals):
  858. new_pmt_state = 'paid'
  859. else:
  860. new_pmt_state = invoice._get_invoice_in_payment_state()
  861. else:
  862. new_pmt_state = 'paid'
  863. reverse_move_types = set()
  864. for x in reconciliation_vals:
  865. for move_type in x['counterpart_move_types']:
  866. reverse_move_types.add(move_type)
  867. in_reverse = (invoice.move_type in ('in_invoice', 'in_receipt')
  868. and (reverse_move_types == {'in_refund'} or reverse_move_types == {'in_refund', 'entry'}))
  869. out_reverse = (invoice.move_type in ('out_invoice', 'out_receipt')
  870. and (reverse_move_types == {'out_refund'} or reverse_move_types == {'out_refund', 'entry'}))
  871. misc_reverse = (invoice.move_type in ('entry', 'out_refund', 'in_refund')
  872. and reverse_move_types == {'entry'})
  873. if in_reverse or out_reverse or misc_reverse:
  874. new_pmt_state = 'reversed'
  875. elif reconciliation_vals:
  876. new_pmt_state = 'partial'
  877. invoice.payment_state = new_pmt_state
  878. @api.depends('invoice_payment_term_id', 'invoice_date', 'currency_id', 'amount_total_in_currency_signed', 'invoice_date_due')
  879. def _compute_needed_terms(self):
  880. for invoice in self:
  881. is_draft = invoice.id != invoice._origin.id
  882. invoice.needed_terms = {}
  883. invoice.needed_terms_dirty = True
  884. sign = 1 if invoice.is_inbound(include_receipts=True) else -1
  885. if invoice.is_invoice(True) and invoice.invoice_line_ids:
  886. if invoice.invoice_payment_term_id:
  887. if is_draft:
  888. tax_amount_currency = 0.0
  889. untaxed_amount_currency = 0.0
  890. for line in invoice.invoice_line_ids:
  891. untaxed_amount_currency += line.price_subtotal
  892. for tax_result in (line.compute_all_tax or {}).values():
  893. tax_amount_currency += -sign * tax_result.get('amount_currency', 0.0)
  894. untaxed_amount = untaxed_amount_currency
  895. tax_amount = tax_amount_currency
  896. else:
  897. tax_amount_currency = invoice.amount_tax * sign
  898. tax_amount = invoice.amount_tax_signed
  899. untaxed_amount_currency = invoice.amount_untaxed * sign
  900. untaxed_amount = invoice.amount_untaxed_signed
  901. invoice_payment_terms = invoice.invoice_payment_term_id._compute_terms(
  902. date_ref=invoice.invoice_date or invoice.date or fields.Date.today(),
  903. currency=invoice.currency_id,
  904. tax_amount_currency=tax_amount_currency,
  905. tax_amount=tax_amount,
  906. untaxed_amount_currency=untaxed_amount_currency,
  907. untaxed_amount=untaxed_amount,
  908. company=invoice.company_id,
  909. sign=sign
  910. )
  911. for term in invoice_payment_terms:
  912. key = frozendict({
  913. 'move_id': invoice.id,
  914. 'date_maturity': fields.Date.to_date(term.get('date')),
  915. 'discount_date': term.get('discount_date'),
  916. 'discount_percentage': term.get('discount_percentage'),
  917. })
  918. values = {
  919. 'balance': term['company_amount'],
  920. 'amount_currency': term['foreign_amount'],
  921. 'discount_amount_currency': term['discount_amount_currency'] or 0.0,
  922. 'discount_balance': term['discount_balance'] or 0.0,
  923. 'discount_date': term['discount_date'],
  924. 'discount_percentage': term['discount_percentage'],
  925. }
  926. if key not in invoice.needed_terms:
  927. invoice.needed_terms[key] = values
  928. else:
  929. invoice.needed_terms[key]['balance'] += values['balance']
  930. invoice.needed_terms[key]['amount_currency'] += values['amount_currency']
  931. else:
  932. invoice.needed_terms[frozendict({
  933. 'move_id': invoice.id,
  934. 'date_maturity': fields.Date.to_date(invoice.invoice_date_due),
  935. 'discount_date': False,
  936. 'discount_percentage': 0
  937. })] = {
  938. 'balance': invoice.amount_total_signed,
  939. 'amount_currency': invoice.amount_total_in_currency_signed,
  940. }
  941. def _compute_payments_widget_to_reconcile_info(self):
  942. for move in self:
  943. move.invoice_outstanding_credits_debits_widget = False
  944. move.invoice_has_outstanding = False
  945. if move.state != 'posted' \
  946. or move.payment_state not in ('not_paid', 'partial') \
  947. or not move.is_invoice(include_receipts=True):
  948. continue
  949. pay_term_lines = move.line_ids\
  950. .filtered(lambda line: line.account_id.account_type in ('asset_receivable', 'liability_payable'))
  951. domain = [
  952. ('account_id', 'in', pay_term_lines.account_id.ids),
  953. ('parent_state', '=', 'posted'),
  954. ('partner_id', '=', move.commercial_partner_id.id),
  955. ('reconciled', '=', False),
  956. '|', ('amount_residual', '!=', 0.0), ('amount_residual_currency', '!=', 0.0),
  957. ]
  958. payments_widget_vals = {'outstanding': True, 'content': [], 'move_id': move.id}
  959. if move.is_inbound():
  960. domain.append(('balance', '<', 0.0))
  961. payments_widget_vals['title'] = _('Outstanding credits')
  962. else:
  963. domain.append(('balance', '>', 0.0))
  964. payments_widget_vals['title'] = _('Outstanding debits')
  965. for line in self.env['account.move.line'].search(domain):
  966. if line.currency_id == move.currency_id:
  967. # Same foreign currency.
  968. amount = abs(line.amount_residual_currency)
  969. else:
  970. # Different foreign currencies.
  971. amount = line.company_currency_id._convert(
  972. abs(line.amount_residual),
  973. move.currency_id,
  974. move.company_id,
  975. line.date,
  976. )
  977. if move.currency_id.is_zero(amount):
  978. continue
  979. payments_widget_vals['content'].append({
  980. 'journal_name': line.ref or line.move_id.name,
  981. 'amount': amount,
  982. 'currency_id': move.currency_id.id,
  983. 'id': line.id,
  984. 'move_id': line.move_id.id,
  985. 'date': fields.Date.to_string(line.date),
  986. 'account_payment_id': line.payment_id.id,
  987. })
  988. if not payments_widget_vals['content']:
  989. continue
  990. move.invoice_outstanding_credits_debits_widget = payments_widget_vals
  991. move.invoice_has_outstanding = True
  992. @api.depends('move_type', 'line_ids.amount_residual')
  993. def _compute_payments_widget_reconciled_info(self):
  994. for move in self:
  995. payments_widget_vals = {'title': _('Less Payment'), 'outstanding': False, 'content': []}
  996. if move.state == 'posted' and move.is_invoice(include_receipts=True):
  997. reconciled_vals = []
  998. reconciled_partials = move._get_all_reconciled_invoice_partials()
  999. for reconciled_partial in reconciled_partials:
  1000. counterpart_line = reconciled_partial['aml']
  1001. if counterpart_line.move_id.ref:
  1002. reconciliation_ref = '%s (%s)' % (counterpart_line.move_id.name, counterpart_line.move_id.ref)
  1003. else:
  1004. reconciliation_ref = counterpart_line.move_id.name
  1005. if counterpart_line.amount_currency and counterpart_line.currency_id != counterpart_line.company_id.currency_id:
  1006. foreign_currency = counterpart_line.currency_id
  1007. else:
  1008. foreign_currency = False
  1009. reconciled_vals.append({
  1010. 'name': counterpart_line.name,
  1011. 'journal_name': counterpart_line.journal_id.name,
  1012. 'amount': reconciled_partial['amount'],
  1013. 'currency_id': move.company_id.currency_id.id if reconciled_partial['is_exchange'] else reconciled_partial['currency'].id,
  1014. 'date': counterpart_line.date,
  1015. 'partial_id': reconciled_partial['partial_id'],
  1016. 'account_payment_id': counterpart_line.payment_id.id,
  1017. 'payment_method_name': counterpart_line.payment_id.payment_method_line_id.name,
  1018. 'move_id': counterpart_line.move_id.id,
  1019. 'ref': reconciliation_ref,
  1020. # these are necessary for the views to change depending on the values
  1021. 'is_exchange': reconciled_partial['is_exchange'],
  1022. 'amount_company_currency': formatLang(self.env, abs(counterpart_line.balance), currency_obj=counterpart_line.company_id.currency_id),
  1023. 'amount_foreign_currency': foreign_currency and formatLang(self.env, abs(counterpart_line.amount_currency), currency_obj=foreign_currency)
  1024. })
  1025. payments_widget_vals['content'] = reconciled_vals
  1026. if payments_widget_vals['content']:
  1027. move.invoice_payments_widget = payments_widget_vals
  1028. else:
  1029. move.invoice_payments_widget = False
  1030. @api.depends(
  1031. 'invoice_line_ids.currency_rate',
  1032. 'invoice_line_ids.tax_base_amount',
  1033. 'invoice_line_ids.tax_line_id',
  1034. 'invoice_line_ids.price_total',
  1035. 'invoice_line_ids.price_subtotal',
  1036. 'invoice_payment_term_id',
  1037. 'partner_id',
  1038. 'currency_id',
  1039. )
  1040. def _compute_tax_totals(self):
  1041. """ Computed field used for custom widget's rendering.
  1042. Only set on invoices.
  1043. """
  1044. for move in self:
  1045. if move.is_invoice(include_receipts=True):
  1046. base_lines = move.invoice_line_ids.filtered(lambda line: line.display_type == 'product')
  1047. base_line_values_list = [line._convert_to_tax_base_line_dict() for line in base_lines]
  1048. sign = move.direction_sign
  1049. if move.id:
  1050. # The invoice is stored so we can add the early payment discount lines directly to reduce the
  1051. # tax amount without touching the untaxed amount.
  1052. base_line_values_list += [
  1053. {
  1054. **line._convert_to_tax_base_line_dict(),
  1055. 'handle_price_include': False,
  1056. 'quantity': 1.0,
  1057. 'price_unit': sign * line.amount_currency,
  1058. }
  1059. for line in move.line_ids.filtered(lambda line: line.display_type == 'epd')
  1060. ]
  1061. kwargs = {
  1062. 'base_lines': base_line_values_list,
  1063. 'currency': move.currency_id or move.journal_id.currency_id or move.company_id.currency_id,
  1064. }
  1065. if move.id:
  1066. kwargs['tax_lines'] = [
  1067. line._convert_to_tax_line_dict()
  1068. for line in move.line_ids.filtered(lambda line: line.display_type == 'tax')
  1069. ]
  1070. else:
  1071. # In case the invoice isn't yet stored, the early payment discount lines are not there. Then,
  1072. # we need to simulate them.
  1073. epd_aggregated_values = {}
  1074. for base_line in base_lines:
  1075. if not base_line.epd_needed:
  1076. continue
  1077. for grouping_dict, values in base_line.epd_needed.items():
  1078. epd_values = epd_aggregated_values.setdefault(grouping_dict, {'price_subtotal': 0.0})
  1079. epd_values['price_subtotal'] += values['price_subtotal']
  1080. for grouping_dict, values in epd_aggregated_values.items():
  1081. taxes = None
  1082. if grouping_dict.get('tax_ids'):
  1083. taxes = self.env['account.tax'].browse(grouping_dict['tax_ids'][0][2])
  1084. kwargs['base_lines'].append(self.env['account.tax']._convert_to_tax_base_line_dict(
  1085. None,
  1086. partner=move.partner_id,
  1087. currency=move.currency_id,
  1088. taxes=taxes,
  1089. price_unit=values['price_subtotal'],
  1090. quantity=1.0,
  1091. account=self.env['account.account'].browse(grouping_dict['account_id']),
  1092. analytic_distribution=values.get('analytic_distribution'),
  1093. price_subtotal=values['price_subtotal'],
  1094. is_refund=move.move_type in ('out_refund', 'in_refund'),
  1095. handle_price_include=False,
  1096. ))
  1097. move.tax_totals = self.env['account.tax']._prepare_tax_totals(**kwargs)
  1098. if move.invoice_cash_rounding_id:
  1099. rounding_amount = move.invoice_cash_rounding_id.compute_difference(move.currency_id, move.tax_totals['amount_total'])
  1100. totals = move.tax_totals
  1101. totals['display_rounding'] = True
  1102. if rounding_amount:
  1103. if move.invoice_cash_rounding_id.strategy == 'add_invoice_line':
  1104. totals['rounding_amount'] = rounding_amount
  1105. totals['formatted_rounding_amount'] = formatLang(self.env, totals['rounding_amount'], currency_obj=move.currency_id)
  1106. totals['amount_total_rounded'] = totals['amount_total'] + rounding_amount
  1107. totals['formatted_amount_total_rounded'] = formatLang(self.env, totals['amount_total_rounded'], currency_obj=move.currency_id)
  1108. elif move.invoice_cash_rounding_id.strategy == 'biggest_tax':
  1109. if totals['subtotals_order']:
  1110. max_tax_group = max((
  1111. tax_group
  1112. for tax_groups in totals['groups_by_subtotal'].values()
  1113. for tax_group in tax_groups
  1114. ), key=lambda tax_group: tax_group['tax_group_amount'])
  1115. max_tax_group['tax_group_amount'] += rounding_amount
  1116. max_tax_group['formatted_tax_group_amount'] = formatLang(self.env, max_tax_group['tax_group_amount'], currency_obj=move.currency_id)
  1117. totals['amount_total'] += rounding_amount
  1118. totals['formatted_amount_total'] = formatLang(self.env, totals['amount_total'], currency_obj=move.currency_id)
  1119. else:
  1120. # Non-invoice moves don't support that field (because of multicurrency: all lines of the invoice share the same currency)
  1121. move.tax_totals = None
  1122. @api.depends('show_payment_term_details')
  1123. def _compute_payment_term_details(self):
  1124. '''
  1125. Returns an [] containing the payment term's information to be displayed on the invoice's PDF.
  1126. '''
  1127. for invoice in self:
  1128. invoice.payment_term_details = False
  1129. if invoice.show_payment_term_details:
  1130. sign = 1 if invoice.is_inbound(include_receipts=True) else -1
  1131. payment_term_details = []
  1132. for line in invoice.line_ids.filtered(lambda l: l.display_type == 'payment_term').sorted('date_maturity'):
  1133. payment_term_details.append({
  1134. 'date': format_date(self.env, line.date_maturity),
  1135. 'amount': sign * line.amount_currency,
  1136. 'discount_date': format_date(self.env, line.discount_date),
  1137. 'discount_amount_currency': sign * line.discount_amount_currency,
  1138. })
  1139. invoice.payment_term_details = payment_term_details
  1140. @api.depends('move_type', 'payment_state', 'invoice_payment_term_id')
  1141. def _compute_show_payment_term_details(self):
  1142. '''
  1143. Determines :
  1144. - whether or not an additional table should be added at the end of the invoice to display the various
  1145. - whether or not there is an early pay discount in this invoice that should be displayed
  1146. '''
  1147. for invoice in self:
  1148. if invoice.move_type in ('out_invoice', 'out_receipt', 'in_invoice', 'in_receipt') and invoice.payment_state in ('not_paid', 'partial'):
  1149. payment_term_lines = invoice.line_ids.filtered(lambda l: l.display_type == 'payment_term')
  1150. invoice.show_discount_details = any(line.discount_date for line in payment_term_lines)
  1151. invoice.show_payment_term_details = len(payment_term_lines) > 1 or invoice.show_discount_details
  1152. else:
  1153. invoice.show_discount_details = False
  1154. invoice.show_payment_term_details = False
  1155. @api.depends('partner_id', 'invoice_source_email', 'partner_id.name')
  1156. def _compute_invoice_partner_display_info(self):
  1157. for move in self:
  1158. vendor_display_name = move.partner_id.display_name
  1159. if not vendor_display_name:
  1160. if move.invoice_source_email:
  1161. vendor_display_name = _('@From: %(email)s', email=move.invoice_source_email)
  1162. else:
  1163. vendor_display_name = _('#Created by: %s', move.sudo().create_uid.name or self.env.user.name)
  1164. move.invoice_partner_display_name = vendor_display_name
  1165. @api.depends('move_type')
  1166. def _compute_invoice_filter_type_domain(self):
  1167. for move in self:
  1168. if move.is_sale_document(include_receipts=True):
  1169. move.invoice_filter_type_domain = 'sale'
  1170. elif move.is_purchase_document(include_receipts=True):
  1171. move.invoice_filter_type_domain = 'purchase'
  1172. else:
  1173. move.invoice_filter_type_domain = False
  1174. @api.depends('commercial_partner_id')
  1175. def _compute_bank_partner_id(self):
  1176. for move in self:
  1177. if move.is_inbound():
  1178. move.bank_partner_id = move.company_id.partner_id
  1179. else:
  1180. move.bank_partner_id = move.commercial_partner_id
  1181. @api.depends('date', 'line_ids.debit', 'line_ids.credit', 'line_ids.tax_line_id', 'line_ids.tax_ids', 'line_ids.tax_tag_ids',
  1182. 'invoice_line_ids.debit', 'invoice_line_ids.credit', 'invoice_line_ids.tax_line_id', 'invoice_line_ids.tax_ids', 'invoice_line_ids.tax_tag_ids')
  1183. def _compute_tax_lock_date_message(self):
  1184. for move in self:
  1185. accounting_date = move.date or fields.Date.context_today(move)
  1186. affects_tax_report = move._affect_tax_report()
  1187. move.tax_lock_date_message = move._get_lock_date_message(accounting_date, affects_tax_report)
  1188. @api.depends('currency_id')
  1189. def _compute_display_inactive_currency_warning(self):
  1190. for move in self.with_context(active_test=False):
  1191. move.display_inactive_currency_warning = move.currency_id and not move.currency_id.active
  1192. @api.depends('company_id.account_fiscal_country_id', 'fiscal_position_id', 'fiscal_position_id.country_id', 'fiscal_position_id.foreign_vat')
  1193. def _compute_tax_country_id(self):
  1194. for record in self:
  1195. if record.fiscal_position_id.foreign_vat:
  1196. record.tax_country_id = record.fiscal_position_id.country_id
  1197. else:
  1198. record.tax_country_id = record.company_id.account_fiscal_country_id
  1199. @api.depends('tax_country_id')
  1200. def _compute_tax_country_code(self):
  1201. for record in self:
  1202. record.tax_country_code = record.tax_country_id.code
  1203. @api.depends('line_ids')
  1204. def _compute_has_reconciled_entries(self):
  1205. for move in self:
  1206. move.has_reconciled_entries = len(move.line_ids._reconciled_lines()) > 1
  1207. @api.depends('restrict_mode_hash_table', 'state')
  1208. def _compute_show_reset_to_draft_button(self):
  1209. for move in self:
  1210. move.show_reset_to_draft_button = not move.restrict_mode_hash_table and move.state in ('posted', 'cancel')
  1211. # EXTENDS portal portal.mixin
  1212. def _compute_access_url(self):
  1213. super()._compute_access_url()
  1214. for move in self.filtered(lambda move: move.is_invoice()):
  1215. move.access_url = '/my/invoices/%s' % (move.id)
  1216. @api.depends('move_type', 'partner_id', 'company_id')
  1217. def _compute_narration(self):
  1218. use_invoice_terms = self.env['ir.config_parameter'].sudo().get_param('account.use_invoice_terms')
  1219. for move in self:
  1220. if not move.is_sale_document(include_receipts=True):
  1221. continue
  1222. if not use_invoice_terms:
  1223. move.narration = False
  1224. else:
  1225. lang = move.partner_id.lang or self.env.user.lang
  1226. if not move.company_id.terms_type == 'html':
  1227. narration = move.company_id.with_context(lang=lang).invoice_terms if not is_html_empty(move.company_id.invoice_terms) else ''
  1228. else:
  1229. baseurl = self.env.company.get_base_url() + '/terms'
  1230. context = {'lang': lang}
  1231. narration = _('Terms & Conditions: %s', baseurl)
  1232. del context
  1233. move.narration = narration or False
  1234. @api.depends('company_id', 'partner_id', 'tax_totals', 'currency_id')
  1235. def _compute_partner_credit_warning(self):
  1236. for move in self:
  1237. move.with_company(move.company_id)
  1238. move.partner_credit_warning = ''
  1239. show_warning = move.state == 'draft' and \
  1240. move.move_type == 'out_invoice' and \
  1241. move.company_id.account_use_credit_limit
  1242. if show_warning:
  1243. amount_total_currency = move.currency_id._convert(move.tax_totals['amount_total'], move.company_currency_id, move.company_id, move.date)
  1244. updated_credit = move.partner_id.commercial_partner_id.credit + amount_total_currency
  1245. move.partner_credit_warning = self._build_credit_warning_message(move, updated_credit)
  1246. def _build_credit_warning_message(self, record, updated_credit):
  1247. ''' Build the warning message that will be displayed in a yellow banner on top of the current record
  1248. if the partner exceeds a credit limit (set on the company or the partner itself).
  1249. :param record: The record where the warning will appear (Invoice, Sales Order...).
  1250. :param updated_credit (float): The partner's updated credit limit including the current record.
  1251. :return (str): The warning message to be showed.
  1252. '''
  1253. partner_id = record.partner_id.commercial_partner_id
  1254. if not partner_id.credit_limit or updated_credit <= partner_id.credit_limit:
  1255. return ''
  1256. msg = _('%s has reached its Credit Limit of : %s\nTotal amount due ',
  1257. partner_id.name,
  1258. formatLang(self.env, partner_id.credit_limit, currency_obj=record.company_id.currency_id))
  1259. if updated_credit > partner_id.credit:
  1260. msg += _('(including this document) ')
  1261. msg += ': %s' % formatLang(self.env, updated_credit, currency_obj=record.company_id.currency_id)
  1262. return msg
  1263. @api.depends('journal_id.type', 'company_id')
  1264. def _compute_quick_edit_mode(self):
  1265. for move in self:
  1266. quick_edit_mode = move.company_id.quick_edit_mode
  1267. if move.journal_id.type == 'sale':
  1268. move.quick_edit_mode = quick_edit_mode in ('out_invoices', 'out_and_in_invoices')
  1269. elif move.journal_id.type == 'purchase':
  1270. move.quick_edit_mode = quick_edit_mode in ('in_invoices', 'out_and_in_invoices')
  1271. else:
  1272. move.quick_edit_mode = False
  1273. @api.depends('quick_edit_total_amount', 'invoice_line_ids.price_total', 'tax_totals')
  1274. def _compute_quick_encoding_vals(self):
  1275. for move in self:
  1276. move.quick_encoding_vals = move._get_quick_edit_suggestions()
  1277. @api.depends('ref', 'move_type', 'partner_id', 'invoice_date')
  1278. def _compute_duplicated_ref_ids(self):
  1279. move_to_duplicate_move = self._fetch_duplicate_supplier_reference()
  1280. for move in self:
  1281. move.duplicated_ref_ids = move_to_duplicate_move.get(move, self.env['account.move'])
  1282. def _fetch_duplicate_supplier_reference(self, only_posted=False):
  1283. moves = self.filtered(lambda m: m.is_purchase_document() and m.ref)
  1284. if not moves:
  1285. return {}
  1286. used_fields = ("company_id", "partner_id", "commercial_partner_id", "ref", "move_type", "invoice_date", "state")
  1287. self.env["account.move"].flush_model(used_fields)
  1288. move_table_and_alias = "account_move AS move"
  1289. place_holders = {}
  1290. if not moves.ids:
  1291. # This handles the special case of a record creation in the UI which isn't searchable in the DB
  1292. place_holders = {
  1293. "id": 0,
  1294. **{
  1295. field_name: moves._fields[field_name].convert_to_write(moves[field_name], moves) or None
  1296. for field_name in used_fields
  1297. },
  1298. }
  1299. casted_values = ", ".join([f"%({field_name})s::{moves._fields[field_name].column_type[0]}" for field_name in place_holders])
  1300. move_table_and_alias = f'(VALUES ({casted_values})) AS move({", ".join(place_holders)})'
  1301. self.env.cr.execute(f"""
  1302. SELECT
  1303. move.id AS move_id,
  1304. array_agg(duplicate_move.id) AS duplicate_ids
  1305. FROM {move_table_and_alias}
  1306. JOIN account_move AS duplicate_move ON
  1307. move.company_id = duplicate_move.company_id
  1308. AND move.commercial_partner_id = duplicate_move.commercial_partner_id
  1309. AND move.ref = duplicate_move.ref
  1310. AND move.move_type = duplicate_move.move_type
  1311. AND move.id != duplicate_move.id
  1312. AND (move.invoice_date = duplicate_move.invoice_date OR NOT %(only_posted)s)
  1313. AND duplicate_move.state != 'cancel'
  1314. AND (duplicate_move.state = 'posted' OR NOT %(only_posted)s)
  1315. WHERE move.id IN %(moves)s
  1316. GROUP BY move.id
  1317. """, {
  1318. "only_posted": only_posted,
  1319. "moves": tuple(moves.ids or [0]),
  1320. **place_holders
  1321. })
  1322. return {
  1323. self.env['account.move'].browse(res['move_id']): self.env['account.move'].browse(res['duplicate_ids'])
  1324. for res in self.env.cr.dictfetchall()
  1325. }
  1326. @api.depends('company_id')
  1327. def _compute_display_qr_code(self):
  1328. for record in self:
  1329. record.display_qr_code = (
  1330. record.move_type in ('out_invoice', 'out_receipt', 'in_invoice', 'in_receipt')
  1331. and record.company_id.qr_code
  1332. )
  1333. # -------------------------------------------------------------------------
  1334. # INVERSE METHODS
  1335. # -------------------------------------------------------------------------
  1336. def _inverse_tax_totals(self):
  1337. if self.env.context.get('skip_invoice_sync'):
  1338. return
  1339. with self._sync_dynamic_line(
  1340. existing_key_fname='term_key',
  1341. needed_vals_fname='needed_terms',
  1342. needed_dirty_fname='needed_terms_dirty',
  1343. line_type='payment_term',
  1344. container={'records': self},
  1345. ):
  1346. for move in self:
  1347. if not move.is_invoice(include_receipts=True):
  1348. continue
  1349. invoice_totals = move.tax_totals
  1350. for amount_by_group_list in invoice_totals['groups_by_subtotal'].values():
  1351. for amount_by_group in amount_by_group_list:
  1352. tax_lines = move.line_ids.filtered(lambda line: line.tax_group_id.id == amount_by_group['tax_group_id'])
  1353. if tax_lines:
  1354. first_tax_line = tax_lines[0]
  1355. tax_group_old_amount = sum(tax_lines.mapped('amount_currency'))
  1356. sign = -1 if move.is_inbound() else 1
  1357. delta_amount = tax_group_old_amount * sign - amount_by_group['tax_group_amount']
  1358. if not move.currency_id.is_zero(delta_amount):
  1359. first_tax_line.amount_currency -= delta_amount * sign
  1360. self._compute_amount()
  1361. def _inverse_amount_total(self):
  1362. for move in self:
  1363. if len(move.line_ids) != 2 or move.is_invoice(include_receipts=True):
  1364. continue
  1365. to_write = []
  1366. amount_currency = abs(move.amount_total)
  1367. balance = move.currency_id._convert(amount_currency, move.company_currency_id, move.company_id, move.invoice_date or move.date)
  1368. for line in move.line_ids:
  1369. if not line.currency_id.is_zero(balance - abs(line.balance)):
  1370. to_write.append((1, line.id, {
  1371. 'debit': line.balance > 0.0 and balance or 0.0,
  1372. 'credit': line.balance < 0.0 and balance or 0.0,
  1373. 'amount_currency': line.balance > 0.0 and amount_currency or -amount_currency,
  1374. }))
  1375. move.write({'line_ids': to_write})
  1376. @api.onchange('partner_id')
  1377. def _inverse_partner_id(self):
  1378. for invoice in self:
  1379. if invoice.is_invoice(True):
  1380. for line in invoice.line_ids + invoice.invoice_line_ids:
  1381. if line.partner_id != invoice.commercial_partner_id:
  1382. line.partner_id = invoice.commercial_partner_id
  1383. line._inverse_partner_id()
  1384. @api.onchange('company_id')
  1385. def _inverse_company_id(self):
  1386. self._conditional_add_to_compute('journal_id', lambda m: (
  1387. m.journal_id.company_id != m.company_id
  1388. ))
  1389. @api.onchange('currency_id')
  1390. def _inverse_currency_id(self):
  1391. self._conditional_add_to_compute('journal_id', lambda m: (
  1392. m.journal_id.currency_id
  1393. and m.journal_id.currency_id != m.currency_id
  1394. ))
  1395. (self.line_ids | self.invoice_line_ids)._conditional_add_to_compute('currency_id', lambda l: (
  1396. l.move_id.is_invoice(True)
  1397. and l.move_id.currency_id != l.currency_id
  1398. ))
  1399. @api.onchange('journal_id')
  1400. def _inverse_journal_id(self):
  1401. self._conditional_add_to_compute('company_id', lambda m: (
  1402. not m.company_id
  1403. or m.company_id != m.journal_id.company_id
  1404. ))
  1405. self._conditional_add_to_compute('currency_id', lambda m: (
  1406. not m.currency_id
  1407. or m.journal_id.currency_id and m.currency_id != m.journal_id.currency_id
  1408. ))
  1409. def _inverse_payment_reference(self):
  1410. self.line_ids._conditional_add_to_compute('name', lambda line: (
  1411. line.display_type == 'payment_term'
  1412. ))
  1413. def _inverse_name(self):
  1414. self._conditional_add_to_compute('payment_reference', lambda move: (
  1415. move.name and move.name != '/'
  1416. ))
  1417. # -------------------------------------------------------------------------
  1418. # ONCHANGE METHODS
  1419. # -------------------------------------------------------------------------
  1420. @api.onchange('date')
  1421. def _onchange_date(self):
  1422. if not self.is_invoice(True):
  1423. self.line_ids._inverse_amount_currency()
  1424. @api.onchange('invoice_vendor_bill_id')
  1425. def _onchange_invoice_vendor_bill(self):
  1426. if self.invoice_vendor_bill_id:
  1427. # Copy invoice lines.
  1428. for line in self.invoice_vendor_bill_id.invoice_line_ids:
  1429. copied_vals = line.copy_data()[0]
  1430. self.invoice_line_ids += self.env['account.move.line'].new(copied_vals)
  1431. self.currency_id = self.invoice_vendor_bill_id.currency_id
  1432. self.fiscal_position_id = self.invoice_vendor_bill_id.fiscal_position_id
  1433. # Reset
  1434. self.invoice_vendor_bill_id = False
  1435. @api.onchange('partner_id')
  1436. def _onchange_partner_id(self):
  1437. self = self.with_company(self.journal_id.company_id)
  1438. warning = {}
  1439. if self.partner_id:
  1440. rec_account = self.partner_id.property_account_receivable_id
  1441. pay_account = self.partner_id.property_account_payable_id
  1442. if not rec_account and not pay_account:
  1443. action = self.env.ref('account.action_account_config')
  1444. msg = _('Cannot find a chart of accounts for this company, You should configure it. \nPlease go to Account Configuration.')
  1445. raise RedirectWarning(msg, action.id, _('Go to the configuration panel'))
  1446. p = self.partner_id
  1447. if p.invoice_warn == 'no-message' and p.parent_id:
  1448. p = p.parent_id
  1449. if p.invoice_warn and p.invoice_warn != 'no-message':
  1450. # Block if partner only has warning but parent company is blocked
  1451. if p.invoice_warn != 'block' and p.parent_id and p.parent_id.invoice_warn == 'block':
  1452. p = p.parent_id
  1453. warning = {
  1454. 'title': _("Warning for %s", p.name),
  1455. 'message': p.invoice_warn_msg
  1456. }
  1457. if p.invoice_warn == 'block':
  1458. self.partner_id = False
  1459. return {'warning': warning}
  1460. @api.onchange('name', 'highest_name')
  1461. def _onchange_name_warning(self):
  1462. if self.name and self.name != '/' and self.name <= (self.highest_name or '') and not self.quick_edit_mode:
  1463. self.show_name_warning = True
  1464. else:
  1465. self.show_name_warning = False
  1466. origin_name = self._origin.name
  1467. if not origin_name or origin_name == '/':
  1468. origin_name = self.highest_name
  1469. if (
  1470. self.name and self.name != '/'
  1471. and origin_name and origin_name != '/'
  1472. and self.date == self._origin.date
  1473. and self.journal_id == self._origin.journal_id
  1474. ):
  1475. new_format, new_format_values = self._get_sequence_format_param(self.name)
  1476. origin_format, origin_format_values = self._get_sequence_format_param(origin_name)
  1477. if (
  1478. new_format != origin_format
  1479. or dict(new_format_values, year=0, month=0, seq=0) != dict(origin_format_values, year=0, month=0, seq=0)
  1480. ):
  1481. changed = _(
  1482. "It was previously '%(previous)s' and it is now '%(current)s'.",
  1483. previous=origin_name,
  1484. current=self.name,
  1485. )
  1486. reset = self._deduce_sequence_number_reset(self.name)
  1487. if reset == 'month':
  1488. detected = _(
  1489. "The sequence will restart at 1 at the start of every month.\n"
  1490. "The year detected here is '%(year)s' and the month is '%(month)s'.\n"
  1491. "The incrementing number in this case is '%(formatted_seq)s'."
  1492. )
  1493. elif reset == 'year':
  1494. detected = _(
  1495. "The sequence will restart at 1 at the start of every year.\n"
  1496. "The year detected here is '%(year)s'.\n"
  1497. "The incrementing number in this case is '%(formatted_seq)s'."
  1498. )
  1499. else:
  1500. detected = _(
  1501. "The sequence will never restart.\n"
  1502. "The incrementing number in this case is '%(formatted_seq)s'."
  1503. )
  1504. new_format_values['formatted_seq'] = "{seq:0{seq_length}d}".format(**new_format_values)
  1505. detected = detected % new_format_values
  1506. return {'warning': {
  1507. 'title': _("The sequence format has changed."),
  1508. 'message': "%s\n\n%s" % (changed, detected)
  1509. }}
  1510. @api.onchange('journal_id')
  1511. def _onchange_journal_id(self):
  1512. if not self.quick_edit_mode:
  1513. self.name = '/'
  1514. self._compute_name()
  1515. @api.onchange('invoice_cash_rounding_id')
  1516. def _onchange_invoice_cash_rounding_id(self):
  1517. for move in self:
  1518. if move.invoice_cash_rounding_id.strategy == 'add_invoice_line' and not move.invoice_cash_rounding_id.profit_account_id:
  1519. return {'warning': {
  1520. 'title': _("Warning for Cash Rounding Method: %s", move.invoice_cash_rounding_id.name),
  1521. 'message': _("You must specify the Profit Account (company dependent)")
  1522. }}
  1523. # -------------------------------------------------------------------------
  1524. # CONSTRAINT METHODS
  1525. # -------------------------------------------------------------------------
  1526. @api.constrains('name', 'journal_id', 'state')
  1527. def _check_unique_sequence_number(self):
  1528. moves = self.filtered(lambda move: move.state == 'posted')
  1529. if not moves:
  1530. return
  1531. self.flush_model(['name', 'journal_id', 'move_type', 'state'])
  1532. # /!\ Computed stored fields are not yet inside the database.
  1533. self._cr.execute('''
  1534. SELECT move2.id, move2.name
  1535. FROM account_move move
  1536. INNER JOIN account_move move2 ON
  1537. move2.name = move.name
  1538. AND move2.journal_id = move.journal_id
  1539. AND move2.move_type = move.move_type
  1540. AND move2.id != move.id
  1541. WHERE move.id IN %s AND move2.state = 'posted'
  1542. ''', [tuple(moves.ids)])
  1543. res = self._cr.fetchall()
  1544. if res:
  1545. raise ValidationError(_('Posted journal entry must have an unique sequence number per company.\n'
  1546. 'Problematic numbers: %s\n') % ', '.join(r[1] for r in res))
  1547. @contextmanager
  1548. def _check_balanced(self, container):
  1549. ''' Assert the move is fully balanced debit = credit.
  1550. An error is raised if it's not the case.
  1551. '''
  1552. with self._disable_recursion(container, 'check_move_validity', default=True, target=False) as disabled:
  1553. yield
  1554. if disabled:
  1555. return
  1556. unbalanced_moves = self._get_unbalanced_moves(container)
  1557. if unbalanced_moves:
  1558. error_msg = _("An error has occurred.")
  1559. for move_id, sum_debit, sum_credit in unbalanced_moves:
  1560. move = self.browse(move_id)
  1561. error_msg += _(
  1562. "\n\n"
  1563. "The move (%s) is not balanced.\n"
  1564. "The total of debits equals %s and the total of credits equals %s.\n"
  1565. "You might want to specify a default account on journal \"%s\" to automatically balance each move.",
  1566. move.display_name,
  1567. format_amount(self.env, sum_debit, move.company_id.currency_id),
  1568. format_amount(self.env, sum_credit, move.company_id.currency_id),
  1569. move.journal_id.name)
  1570. raise UserError(error_msg)
  1571. def _get_unbalanced_moves(self, container):
  1572. moves = container['records'].filtered(lambda move: move.line_ids)
  1573. if not moves:
  1574. return
  1575. # /!\ As this method is called in create / write, we can't make the assumption the computed stored fields
  1576. # are already done. Then, this query MUST NOT depend on computed stored fields.
  1577. # It happens as the ORM calls create() with the 'no_recompute' statement.
  1578. self.env['account.move.line'].flush_model(['debit', 'credit', 'balance', 'currency_id', 'move_id'])
  1579. self._cr.execute('''
  1580. SELECT line.move_id,
  1581. ROUND(SUM(line.debit), currency.decimal_places) debit,
  1582. ROUND(SUM(line.credit), currency.decimal_places) credit
  1583. FROM account_move_line line
  1584. JOIN account_move move ON move.id = line.move_id
  1585. JOIN res_company company ON company.id = move.company_id
  1586. JOIN res_currency currency ON currency.id = company.currency_id
  1587. WHERE line.move_id IN %s
  1588. GROUP BY line.move_id, currency.decimal_places
  1589. HAVING ROUND(SUM(line.balance), currency.decimal_places) != 0
  1590. ''', [tuple(moves.ids)])
  1591. return self._cr.fetchall()
  1592. def _check_fiscalyear_lock_date(self):
  1593. for move in self:
  1594. lock_date = move.company_id._get_user_fiscal_lock_date()
  1595. if move.date <= lock_date:
  1596. if self.user_has_groups('account.group_account_manager'):
  1597. message = _("You cannot add/modify entries prior to and inclusive of the lock date %s.", format_date(self.env, lock_date))
  1598. else:
  1599. message = _("You cannot add/modify entries prior to and inclusive of the lock date %s. Check the company settings or ask someone with the 'Adviser' role", format_date(self.env, lock_date))
  1600. raise UserError(message)
  1601. return True
  1602. @api.constrains('auto_post', 'invoice_date')
  1603. def _require_bill_date_for_autopost(self):
  1604. """Vendor bills must have an invoice date set to be posted. Require it for auto-posted bills."""
  1605. for record in self:
  1606. if record.auto_post != 'no' and record.is_purchase_document() and not record.invoice_date:
  1607. raise ValidationError(_("For this entry to be automatically posted, it required a bill date."))
  1608. @api.constrains('journal_id', 'move_type')
  1609. def _check_journal_move_type(self):
  1610. for move in self:
  1611. if move.is_purchase_document(include_receipts=True) and move.journal_id.type != 'purchase':
  1612. raise ValidationError(_("Cannot create a purchase document in a non purchase journal"))
  1613. if move.is_sale_document(include_receipts=True) and move.journal_id.type != 'sale':
  1614. raise ValidationError(_("Cannot create a sale document in a non sale journal"))
  1615. @api.constrains('ref', 'move_type', 'partner_id', 'journal_id', 'invoice_date', 'state')
  1616. def _check_duplicate_supplier_reference(self):
  1617. """ Assert the move which is about to be posted isn't a duplicated move from another posted entry"""
  1618. move_to_duplicate_moves = self.filtered(lambda m: m.state == 'posted')._fetch_duplicate_supplier_reference(only_posted=True)
  1619. if any(duplicate_move for duplicate_move in move_to_duplicate_moves.values()):
  1620. duplicate_move_ids = list(set(
  1621. move_id
  1622. for move_ids in (move.ids + duplicate.ids for move, duplicate in move_to_duplicate_moves.items() if duplicate)
  1623. for move_id in move_ids
  1624. ))
  1625. action = self.env['ir.actions.actions']._for_xml_id('account.action_move_line_form')
  1626. action['domain'] = [('id', 'in', duplicate_move_ids)]
  1627. action['views'] = [((view_id, 'list') if view_type == 'tree' else (view_id, view_type)) for view_id, view_type in action['views']]
  1628. raise RedirectWarning(
  1629. message=_("Duplicated vendor reference detected. You probably encoded twice the same vendor bill/credit note."),
  1630. action=action,
  1631. button_text=_("Open list"),
  1632. )
  1633. @api.constrains('line_ids', 'fiscal_position_id', 'company_id')
  1634. def _validate_taxes_country(self):
  1635. """ By playing with the fiscal position in the form view, it is possible to keep taxes on the invoices from
  1636. a different country than the one allowed by the fiscal country or the fiscal position.
  1637. This contrains ensure such account.move cannot be kept, as they could generate inconsistencies in the reports.
  1638. """
  1639. self._compute_tax_country_id() # We need to ensure this field has been computed, as we use it in our check
  1640. for record in self:
  1641. amls = record.line_ids
  1642. impacted_countries = amls.tax_ids.country_id | amls.tax_line_id.country_id
  1643. if impacted_countries and impacted_countries != record.tax_country_id:
  1644. if record.fiscal_position_id and impacted_countries != record.fiscal_position_id.country_id:
  1645. raise ValidationError(_("This entry contains taxes that are not compatible with your fiscal position. Check the country set in fiscal position and in your tax configuration."))
  1646. raise ValidationError(_("This entry contains one or more taxes that are incompatible with your fiscal country. Check company fiscal country in the settings and tax country in taxes configuration."))
  1647. # -------------------------------------------------------------------------
  1648. # BUSINESS MODELS SYNCHRONIZATION
  1649. # -------------------------------------------------------------------------
  1650. def _synchronize_business_models(self, changed_fields):
  1651. ''' Ensure the consistency between:
  1652. account.payment & account.move
  1653. account.bank.statement.line & account.move
  1654. The idea is to call the method performing the synchronization of the business
  1655. models regarding their related journal entries. To avoid cycling, the
  1656. 'skip_account_move_synchronization' key is used through the context.
  1657. :param changed_fields: A set containing all modified fields on account.move.
  1658. '''
  1659. if self._context.get('skip_account_move_synchronization'):
  1660. return
  1661. self_sudo = self.sudo()
  1662. self_sudo.payment_id._synchronize_from_moves(changed_fields)
  1663. self_sudo.statement_line_id._synchronize_from_moves(changed_fields)
  1664. # -------------------------------------------------------------------------
  1665. # DYNAMIC LINES
  1666. # -------------------------------------------------------------------------
  1667. def _recompute_cash_rounding_lines(self):
  1668. ''' Handle the cash rounding feature on invoices.
  1669. In some countries, the smallest coins do not exist. For example, in Switzerland, there is no coin for 0.01 CHF.
  1670. For this reason, if invoices are paid in cash, you have to round their total amount to the smallest coin that
  1671. exists in the currency. For the CHF, the smallest coin is 0.05 CHF.
  1672. There are two strategies for the rounding:
  1673. 1) Add a line on the invoice for the rounding: The cash rounding line is added as a new invoice line.
  1674. 2) Add the rounding in the biggest tax amount: The cash rounding line is added as a new tax line on the tax
  1675. having the biggest balance.
  1676. '''
  1677. self.ensure_one()
  1678. def _compute_cash_rounding(self, total_amount_currency):
  1679. ''' Compute the amount differences due to the cash rounding.
  1680. :param self: The current account.move record.
  1681. :param total_amount_currency: The invoice's total in invoice's currency.
  1682. :return: The amount differences both in company's currency & invoice's currency.
  1683. '''
  1684. difference = self.invoice_cash_rounding_id.compute_difference(self.currency_id, total_amount_currency)
  1685. if self.currency_id == self.company_id.currency_id:
  1686. diff_amount_currency = diff_balance = difference
  1687. else:
  1688. diff_amount_currency = difference
  1689. diff_balance = self.currency_id._convert(diff_amount_currency, self.company_id.currency_id, self.company_id, self.invoice_date or self.date)
  1690. return diff_balance, diff_amount_currency
  1691. def _apply_cash_rounding(self, diff_balance, diff_amount_currency, cash_rounding_line):
  1692. ''' Apply the cash rounding.
  1693. :param self: The current account.move record.
  1694. :param diff_balance: The computed balance to set on the new rounding line.
  1695. :param diff_amount_currency: The computed amount in invoice's currency to set on the new rounding line.
  1696. :param cash_rounding_line: The existing cash rounding line.
  1697. :return: The newly created rounding line.
  1698. '''
  1699. rounding_line_vals = {
  1700. 'balance': diff_balance,
  1701. 'amount_currency': diff_amount_currency,
  1702. 'partner_id': self.partner_id.id,
  1703. 'move_id': self.id,
  1704. 'currency_id': self.currency_id.id,
  1705. 'company_id': self.company_id.id,
  1706. 'company_currency_id': self.company_id.currency_id.id,
  1707. 'display_type': 'rounding',
  1708. }
  1709. if self.invoice_cash_rounding_id.strategy == 'biggest_tax':
  1710. biggest_tax_line = None
  1711. for tax_line in self.line_ids.filtered('tax_repartition_line_id'):
  1712. if not biggest_tax_line or abs(tax_line.balance) > abs(biggest_tax_line.balance):
  1713. biggest_tax_line = tax_line
  1714. # No tax found.
  1715. if not biggest_tax_line:
  1716. return
  1717. rounding_line_vals.update({
  1718. 'name': _('%s (rounding)', biggest_tax_line.name),
  1719. 'account_id': biggest_tax_line.account_id.id,
  1720. 'tax_repartition_line_id': biggest_tax_line.tax_repartition_line_id.id,
  1721. 'tax_tag_ids': [(6, 0, biggest_tax_line.tax_tag_ids.ids)],
  1722. 'tax_ids': [Command.set(biggest_tax_line.tax_ids.ids)]
  1723. })
  1724. elif self.invoice_cash_rounding_id.strategy == 'add_invoice_line':
  1725. if diff_balance > 0.0 and self.invoice_cash_rounding_id.loss_account_id:
  1726. account_id = self.invoice_cash_rounding_id.loss_account_id.id
  1727. else:
  1728. account_id = self.invoice_cash_rounding_id.profit_account_id.id
  1729. rounding_line_vals.update({
  1730. 'name': self.invoice_cash_rounding_id.name,
  1731. 'account_id': account_id,
  1732. 'tax_ids': [Command.clear()]
  1733. })
  1734. # Create or update the cash rounding line.
  1735. if cash_rounding_line:
  1736. cash_rounding_line.write(rounding_line_vals)
  1737. else:
  1738. cash_rounding_line = self.env['account.move.line'].create(rounding_line_vals)
  1739. existing_cash_rounding_line = self.line_ids.filtered(lambda line: line.display_type == 'rounding')
  1740. # The cash rounding has been removed.
  1741. if not self.invoice_cash_rounding_id:
  1742. existing_cash_rounding_line.unlink()
  1743. # self.line_ids -= existing_cash_rounding_line
  1744. return
  1745. # The cash rounding strategy has changed.
  1746. if self.invoice_cash_rounding_id and existing_cash_rounding_line:
  1747. strategy = self.invoice_cash_rounding_id.strategy
  1748. old_strategy = 'biggest_tax' if existing_cash_rounding_line.tax_line_id else 'add_invoice_line'
  1749. if strategy != old_strategy:
  1750. # self.line_ids -= existing_cash_rounding_line
  1751. existing_cash_rounding_line.unlink()
  1752. existing_cash_rounding_line = self.env['account.move.line']
  1753. others_lines = self.line_ids.filtered(lambda line: line.account_id.account_type not in ('asset_receivable', 'liability_payable'))
  1754. others_lines -= existing_cash_rounding_line
  1755. total_amount_currency = sum(others_lines.mapped('amount_currency'))
  1756. diff_balance, diff_amount_currency = _compute_cash_rounding(self, total_amount_currency)
  1757. # The invoice is already rounded.
  1758. if self.currency_id.is_zero(diff_balance) and self.currency_id.is_zero(diff_amount_currency):
  1759. existing_cash_rounding_line.unlink()
  1760. # self.line_ids -= existing_cash_rounding_line
  1761. return
  1762. # No update needed
  1763. if existing_cash_rounding_line \
  1764. and float_compare(existing_cash_rounding_line.balance, diff_balance, precision_rounding=self.currency_id.rounding) == 0 \
  1765. and float_compare(existing_cash_rounding_line.amount_currency, diff_amount_currency, precision_rounding=self.currency_id.rounding) == 0:
  1766. return
  1767. _apply_cash_rounding(self, diff_balance, diff_amount_currency, existing_cash_rounding_line)
  1768. @contextmanager
  1769. def _sync_unbalanced_lines(self, container):
  1770. yield
  1771. # Skip posted moves.
  1772. for invoice in (x for x in container['records'] if x.state != 'posted'):
  1773. # Unlink tax lines if all taxes have been removed.
  1774. if not invoice.line_ids.tax_ids:
  1775. # if there isn't any tax but there remains a tax_line_id, it means we are currently in the process of
  1776. # removing the taxes from the entry. Thus, we want the automatic balancing to happen in order to have
  1777. # a smooth process for tax deletion
  1778. if not invoice.line_ids.filtered('tax_line_id'):
  1779. continue
  1780. invoice.line_ids.filtered('tax_line_id').unlink()
  1781. # Set the balancing line's balance and amount_currency to zero,
  1782. # so that it does not interfere with _get_unbalanced_moves() below.
  1783. balance_name = _('Automatic Balancing Line')
  1784. existing_balancing_line = invoice.line_ids.filtered(lambda line: line.name == balance_name)
  1785. if existing_balancing_line:
  1786. existing_balancing_line.balance = existing_balancing_line.amount_currency = 0.0
  1787. # Create an automatic balancing line to make sure the entry can be saved/posted.
  1788. # If such a line already exists, we simply update its amounts.
  1789. unbalanced_moves = self._get_unbalanced_moves({'records': invoice})
  1790. if isinstance(unbalanced_moves, list) and len(unbalanced_moves) == 1:
  1791. dummy, debit, credit = unbalanced_moves[0]
  1792. vals = {'balance': credit - debit}
  1793. if existing_balancing_line:
  1794. existing_balancing_line.write(vals)
  1795. else:
  1796. vals.update({
  1797. 'name': balance_name,
  1798. 'move_id': invoice.id,
  1799. 'account_id': invoice.company_id.account_journal_suspense_account_id.id,
  1800. 'currency_id': invoice.currency_id.id,
  1801. })
  1802. self.env['account.move.line'].create(vals)
  1803. @contextmanager
  1804. def _sync_rounding_lines(self, container):
  1805. yield
  1806. for invoice in container['records']:
  1807. invoice._recompute_cash_rounding_lines()
  1808. @contextmanager
  1809. def _sync_dynamic_line(self, existing_key_fname, needed_vals_fname, needed_dirty_fname, line_type, container):
  1810. def existing():
  1811. return {
  1812. line[existing_key_fname]: line
  1813. for line in container['records'].line_ids
  1814. if line[existing_key_fname]
  1815. }
  1816. def needed():
  1817. res = {}
  1818. for computed_needed in container['records'].mapped(needed_vals_fname):
  1819. if computed_needed is False:
  1820. continue # there was an invalidation, let's hope nothing needed to be changed...
  1821. for key, values in computed_needed.items():
  1822. if key not in res:
  1823. res[key] = dict(values)
  1824. else:
  1825. ignore = True
  1826. for fname in res[key]:
  1827. if self.env['account.move.line']._fields[fname].type == 'monetary':
  1828. res[key][fname] += values[fname]
  1829. if res[key][fname]:
  1830. ignore = False
  1831. if ignore:
  1832. del res[key]
  1833. # Convert float values to their "ORM cache" one to prevent different rounding calculations
  1834. for dict_key in res:
  1835. move_id = dict_key.get('move_id')
  1836. if not move_id:
  1837. continue
  1838. record = self.env['account.move'].browse(move_id)
  1839. for fname, current_value in res[dict_key].items():
  1840. field = self.env['account.move.line']._fields[fname]
  1841. if isinstance(current_value, float):
  1842. new_value = field.convert_to_cache(current_value, record)
  1843. res[dict_key][fname] = new_value
  1844. return res
  1845. def dirty():
  1846. *path, dirty_fname = needed_dirty_fname.split('.')
  1847. eligible_recs = container['records'].mapped('.'.join(path))
  1848. if eligible_recs._name == 'account.move.line':
  1849. eligible_recs = eligible_recs.filtered(lambda l: l.display_type != 'cogs')
  1850. dirty_recs = eligible_recs.filtered(dirty_fname)
  1851. return dirty_recs, dirty_fname
  1852. existing_before = existing()
  1853. needed_before = needed()
  1854. dirty_recs_before, dirty_fname = dirty()
  1855. dirty_recs_before[dirty_fname] = False
  1856. yield
  1857. dirty_recs_after, dirty_fname = dirty()
  1858. if dirty_recs_before and not dirty_recs_after: # TODO improve filter
  1859. return
  1860. existing_after = existing()
  1861. needed_after = needed()
  1862. # Filter out deleted lines from `needed_before` to not recompute lines if not necessary or wanted
  1863. line_ids = set(self.env['account.move.line'].browse(k['id'] for k in needed_before if 'id' in k).exists().ids)
  1864. needed_before = {k: v for k, v in needed_before.items() if 'id' not in k or k['id'] in line_ids}
  1865. # old key to new key for the same line
  1866. inv_existing_before = {v: k for k, v in existing_before.items()}
  1867. inv_existing_after = {v: k for k, v in existing_after.items()}
  1868. before2after = {
  1869. before: inv_existing_after[bline]
  1870. for bline, before in inv_existing_before.items()
  1871. if bline in inv_existing_after
  1872. }
  1873. if needed_after == needed_before:
  1874. return
  1875. to_delete = [
  1876. line.id
  1877. for key, line in existing_before.items()
  1878. if key not in needed_after
  1879. and key in existing_after
  1880. and before2after[key] not in needed_after
  1881. ]
  1882. to_delete_set = set(to_delete)
  1883. to_delete.extend(line.id
  1884. for key, line in existing_after.items()
  1885. if key not in needed_after and line.id not in to_delete_set
  1886. )
  1887. to_create = {
  1888. key: values
  1889. for key, values in needed_after.items()
  1890. if key not in existing_after
  1891. }
  1892. to_write = {
  1893. existing_after[key]: values
  1894. for key, values in needed_after.items()
  1895. if key in existing_after
  1896. and any(
  1897. self.env['account.move.line']._fields[fname].convert_to_write(existing_after[key][fname], self)
  1898. != values[fname]
  1899. for fname in values
  1900. )
  1901. }
  1902. while to_delete and to_create:
  1903. key, values = to_create.popitem()
  1904. line_id = to_delete.pop()
  1905. self.env['account.move.line'].browse(line_id).write(
  1906. {**key, **values, 'display_type': line_type}
  1907. )
  1908. if to_delete:
  1909. self.env['account.move.line'].browse(to_delete).with_context(dynamic_unlink=True).unlink()
  1910. if to_create:
  1911. self.env['account.move.line'].create([
  1912. {**key, **values, 'display_type': line_type}
  1913. for key, values in to_create.items()
  1914. ])
  1915. if to_write:
  1916. for line, values in to_write.items():
  1917. line.write(values)
  1918. @contextmanager
  1919. def _sync_invoice(self, container):
  1920. def existing():
  1921. return {
  1922. move: {
  1923. 'commercial_partner_id': move.commercial_partner_id,
  1924. }
  1925. for move in container['records'].filtered(lambda m: m.is_invoice(True))
  1926. }
  1927. def changed(fname):
  1928. return move not in before or before[move][fname] != after[move][fname]
  1929. before = existing()
  1930. yield
  1931. after = existing()
  1932. for move in after:
  1933. if changed('commercial_partner_id'):
  1934. move.line_ids.partner_id = after[move]['commercial_partner_id']
  1935. @contextmanager
  1936. def _sync_dynamic_lines(self, container):
  1937. with self._disable_recursion(container, 'skip_invoice_sync') as disabled:
  1938. if disabled:
  1939. yield
  1940. return
  1941. def update_containers():
  1942. # Only invoice-like and journal entries in "auto tax mode" are synced
  1943. tax_container['records'] = container['records'].filtered(lambda m: (m.is_invoice(True) or m.line_ids.tax_ids and not m.tax_cash_basis_origin_move_id))
  1944. invoice_container['records'] = container['records'].filtered(lambda m: m.is_invoice(True))
  1945. misc_container['records'] = container['records'].filtered(lambda m: m.move_type == 'entry' and not m.tax_cash_basis_origin_move_id)
  1946. tax_container, invoice_container, misc_container = ({} for __ in range(3))
  1947. update_containers()
  1948. with ExitStack() as stack:
  1949. stack.enter_context(self._sync_dynamic_line(
  1950. existing_key_fname='term_key',
  1951. needed_vals_fname='needed_terms',
  1952. needed_dirty_fname='needed_terms_dirty',
  1953. line_type='payment_term',
  1954. container=invoice_container,
  1955. ))
  1956. stack.enter_context(self._sync_unbalanced_lines(misc_container))
  1957. stack.enter_context(self._sync_rounding_lines(invoice_container))
  1958. stack.enter_context(self._sync_dynamic_line(
  1959. existing_key_fname='tax_key',
  1960. needed_vals_fname='line_ids.compute_all_tax',
  1961. needed_dirty_fname='line_ids.compute_all_tax_dirty',
  1962. line_type='tax',
  1963. container=tax_container,
  1964. ))
  1965. stack.enter_context(self._sync_dynamic_line(
  1966. existing_key_fname='epd_key',
  1967. needed_vals_fname='line_ids.epd_needed',
  1968. needed_dirty_fname='line_ids.epd_dirty',
  1969. line_type='epd',
  1970. container=invoice_container,
  1971. ))
  1972. stack.enter_context(self._sync_invoice(invoice_container))
  1973. line_container = {'records': self.line_ids}
  1974. with self.line_ids._sync_invoice(line_container):
  1975. yield
  1976. line_container['records'] = self.line_ids
  1977. update_containers()
  1978. # -------------------------------------------------------------------------
  1979. # LOW-LEVEL METHODS
  1980. # -------------------------------------------------------------------------
  1981. def check_field_access_rights(self, operation, field_names):
  1982. result = super().check_field_access_rights(operation, field_names)
  1983. if not field_names:
  1984. weirdos = ['needed_terms', 'quick_encoding_vals', 'payment_term_details']
  1985. result = [fname for fname in result if fname not in weirdos]
  1986. return result
  1987. def copy_data(self, default=None):
  1988. data_list = super().copy_data(default)
  1989. for move, data in zip(self, data_list):
  1990. if move.move_type in ('out_invoice', 'in_invoice'):
  1991. data['line_ids'] = [
  1992. (command, _id, line_vals)
  1993. for command, _id, line_vals in data['line_ids']
  1994. if command == Command.CREATE
  1995. ]
  1996. elif move.move_type == 'entry':
  1997. if 'partner_id' not in data:
  1998. data['partner_id'] = False
  1999. if not self.journal_id.active and 'journal_id' in data_list:
  2000. del default['journal_id']
  2001. return data_list
  2002. @api.returns('self', lambda value: value.id)
  2003. def copy(self, default=None):
  2004. default = dict(default or {})
  2005. if (fields.Date.to_date(default.get('date')) or self.date) <= self.company_id._get_user_fiscal_lock_date():
  2006. default['date'] = self.company_id._get_user_fiscal_lock_date() + timedelta(days=1)
  2007. copied_am = super().copy(default)
  2008. message_origin = '' if not copied_am.auto_post_origin_id else \
  2009. '<br/>' + _('This recurring entry originated from %s', copied_am.auto_post_origin_id._get_html_link())
  2010. copied_am._message_log(body=_(
  2011. 'This entry has been duplicated from %s%s',
  2012. self._get_html_link(),
  2013. message_origin,
  2014. ))
  2015. return copied_am
  2016. def _sanitize_vals(self, vals):
  2017. if vals.get('invoice_line_ids') and vals.get('line_ids'):
  2018. # values can sometimes be in only one of the two fields, sometimes in
  2019. # both fields, sometimes one field can be explicitely empty while the other
  2020. # one is not, sometimes not...
  2021. update_vals = {
  2022. line_id: line_vals
  2023. for command, line_id, line_vals in vals['invoice_line_ids']
  2024. if command == Command.UPDATE
  2025. }
  2026. for command, line_id, line_vals in vals['line_ids']:
  2027. if command == Command.UPDATE and line_id in update_vals:
  2028. line_vals.update(update_vals.pop(line_id))
  2029. for line_id, line_vals in update_vals.items():
  2030. vals['line_ids'] += [Command.update(line_id, line_vals)]
  2031. for command, line_id, line_vals in vals['invoice_line_ids']:
  2032. assert command not in (Command.SET, Command.CLEAR)
  2033. if [command, line_id, line_vals] not in vals['line_ids']:
  2034. vals['line_ids'] += [(command, line_id, line_vals)]
  2035. del vals['invoice_line_ids']
  2036. return vals
  2037. @api.model_create_multi
  2038. def create(self, vals_list):
  2039. if any('state' in vals and vals.get('state') == 'posted' for vals in vals_list):
  2040. raise UserError(_('You cannot create a move already in the posted state. Please create a draft move and post it after.'))
  2041. container = {'records': self}
  2042. with self._check_balanced(container):
  2043. with self._sync_dynamic_lines(container):
  2044. moves = super().create([self._sanitize_vals(vals) for vals in vals_list])
  2045. container['records'] = moves
  2046. for move, vals in zip(moves, vals_list):
  2047. if 'tax_totals' in vals:
  2048. move.tax_totals = vals['tax_totals']
  2049. return moves
  2050. def write(self, vals):
  2051. if not vals:
  2052. return True
  2053. self._sanitize_vals(vals)
  2054. for move in self:
  2055. if (move.restrict_mode_hash_table and move.state == "posted" and set(vals).intersection(move._get_integrity_hash_fields())):
  2056. raise UserError(_("You cannot edit the following fields due to restrict mode being activated on the journal: %s.") % ', '.join(move._get_integrity_hash_fields()))
  2057. if (move.restrict_mode_hash_table and move.inalterable_hash and 'inalterable_hash' in vals) or (move.secure_sequence_number and 'secure_sequence_number' in vals):
  2058. raise UserError(_('You cannot overwrite the values ensuring the inalterability of the accounting.'))
  2059. if (move.posted_before and 'journal_id' in vals and move.journal_id.id != vals['journal_id']):
  2060. raise UserError(_('You cannot edit the journal of an account move if it has been posted once.'))
  2061. if (move.name and move.name != '/' and move.sequence_number not in (0, 1) and 'journal_id' in vals and move.journal_id.id != vals['journal_id']):
  2062. raise UserError(_('You cannot edit the journal of an account move if it already has a sequence number assigned.'))
  2063. # You can't change the date or name of a move being inside a locked period.
  2064. if move.state == "posted" and (
  2065. ('name' in vals and move.name != vals['name'])
  2066. or ('date' in vals and move.date != vals['date'])
  2067. ):
  2068. move._check_fiscalyear_lock_date()
  2069. move.line_ids._check_tax_lock_date()
  2070. # You can't post subtract a move to a locked period.
  2071. if 'state' in vals and move.state == 'posted' and vals['state'] != 'posted':
  2072. move._check_fiscalyear_lock_date()
  2073. move.line_ids._check_tax_lock_date()
  2074. if move.journal_id.sequence_override_regex and vals.get('name') and vals['name'] != '/' and not re.match(move.journal_id.sequence_override_regex, vals['name']):
  2075. if not self.env.user.has_group('account.group_account_manager'):
  2076. raise UserError(_('The Journal Entry sequence is not conform to the current format. Only the Accountant can change it.'))
  2077. move.journal_id.sequence_override_regex = False
  2078. to_protect = []
  2079. for fname in vals:
  2080. field = self._fields[fname]
  2081. if field.compute and not field.readonly:
  2082. to_protect.append(field)
  2083. container = {'records': self}
  2084. with self.env.protecting(to_protect, self), self._check_balanced(container):
  2085. with self._sync_dynamic_lines(container):
  2086. res = super(AccountMove, self.with_context(
  2087. skip_account_move_synchronization=True,
  2088. )).write(vals)
  2089. # Reset the name of draft moves when changing the journal.
  2090. # Protected against holes in the pre-validation checks.
  2091. if 'journal_id' in vals and 'name' not in vals:
  2092. self.name = False
  2093. self._compute_name()
  2094. # You can't change the date of a not-locked move to a locked period.
  2095. # You can't post a new journal entry inside a locked period.
  2096. if 'date' in vals or 'state' in vals:
  2097. posted_move = self.filtered(lambda m: m.state == 'posted')
  2098. posted_move._check_fiscalyear_lock_date()
  2099. posted_move.line_ids._check_tax_lock_date()
  2100. # Hash the move
  2101. if vals.get('state') == 'posted':
  2102. self.flush_recordset() # Ensure that the name is correctly computed before it is used to generate the hash
  2103. for move in self.filtered(lambda m: m.restrict_mode_hash_table and not(m.secure_sequence_number or m.inalterable_hash)).sorted(lambda m: (m.date, m.ref or '', m.id)):
  2104. new_number = move.journal_id.secure_sequence_id.next_by_id()
  2105. res |= super(AccountMove, move).write({
  2106. 'secure_sequence_number': new_number,
  2107. 'inalterable_hash': move._get_new_hash(new_number),
  2108. })
  2109. self._synchronize_business_models(set(vals.keys()))
  2110. # Apply the rounding on the Quick Edit mode only when adding a new line
  2111. for move in self:
  2112. if 'tax_totals' in vals:
  2113. super(AccountMove, move).write({'tax_totals': vals['tax_totals']})
  2114. if 'journal_id' in vals:
  2115. self.line_ids._check_constrains_account_id_journal_id()
  2116. return res
  2117. def check_move_sequence_chain(self):
  2118. return self.filtered(lambda move: move.name != '/')._is_end_of_seq_chain()
  2119. @api.ondelete(at_uninstall=False)
  2120. def _unlink_forbid_parts_of_chain(self):
  2121. """ For a user with Billing/Bookkeeper rights, when the fidu mode is deactivated,
  2122. moves with a sequence number can only be deleted if they are the last element of a chain of sequence.
  2123. If they are not, deleting them would create a gap. If the user really wants to do this, he still can
  2124. explicitly empty the 'name' field of the move; but we discourage that practice.
  2125. If a user is a Billing Administrator/Accountant or if fidu mode is activated, we show a warning,
  2126. but they can delete the moves even if it creates a sequence gap.
  2127. """
  2128. if not (
  2129. self.user_has_groups('account.group_account_manager')
  2130. or self.company_id.quick_edit_mode
  2131. or self._context.get('force_delete')
  2132. or self.check_move_sequence_chain()
  2133. ):
  2134. raise UserError(_(
  2135. "You cannot delete this entry, as it has already consumed a sequence number and is not the last one in the chain. "
  2136. "You should probably revert it instead."
  2137. ))
  2138. def unlink(self):
  2139. self = self.with_context(skip_invoice_sync=True, dynamic_unlink=True) # no need to sync to delete everything
  2140. self.line_ids.unlink()
  2141. return super().unlink()
  2142. def name_get(self):
  2143. result = []
  2144. for move in self:
  2145. result.append((move.id, move._get_move_display_name(show_ref=True)))
  2146. return result
  2147. def onchange(self, values, field_name, field_onchange):
  2148. if field_name in ('line_ids', 'invoice_line_ids'):
  2149. # Since only one field can be changed at the same time (the record is saved when changing tabs)
  2150. # we can avoid building the snapshots for the other field
  2151. to_del = 'invoice_line_ids' if field_name == 'line_ids' else 'line_ids'
  2152. for key in list(field_onchange):
  2153. if key == to_del or key.startswith(f"{to_del}."):
  2154. del field_onchange[key]
  2155. # test_01_account_tour
  2156. # File "/data/build/odoo/addons/account/models/account_move.py", line 2127, in onchange
  2157. # del values[to_del]
  2158. # KeyError: 'line_ids'
  2159. values.pop(to_del, None)
  2160. if field_name and not isinstance(field_name, list):
  2161. field_name = [field_name]
  2162. with self.env.protecting([self._fields[fname] for fname in field_name or []], self):
  2163. return super().onchange(values, field_name, field_onchange)
  2164. # -------------------------------------------------------------------------
  2165. # RECONCILIATION METHODS
  2166. # -------------------------------------------------------------------------
  2167. def _collect_tax_cash_basis_values(self):
  2168. ''' Collect all information needed to create the tax cash basis journal entries:
  2169. - Determine if a tax cash basis journal entry is needed.
  2170. - Compute the lines to be processed and the amounts needed to compute a percentage.
  2171. :return: A dictionary:
  2172. * move: The current account.move record passed as parameter.
  2173. * to_process_lines: A tuple (caba_treatment, line) where:
  2174. - caba_treatment is either 'tax' or 'base', depending on what should
  2175. be considered on the line when generating the caba entry.
  2176. For example, a line with tax_ids=caba and tax_line_id=non_caba
  2177. will have a 'base' caba treatment, as we only want to treat its base
  2178. part in the caba entry (the tax part is already exigible on the invoice)
  2179. - line is an account.move.line record being not exigible on the tax report.
  2180. * currency: The currency on which the percentage has been computed.
  2181. * total_balance: sum(payment_term_lines.mapped('balance').
  2182. * total_residual: sum(payment_term_lines.mapped('amount_residual').
  2183. * total_amount_currency: sum(payment_term_lines.mapped('amount_currency').
  2184. * total_residual_currency: sum(payment_term_lines.mapped('amount_residual_currency').
  2185. * is_fully_paid: A flag indicating the current move is now fully paid.
  2186. '''
  2187. self.ensure_one()
  2188. values = {
  2189. 'move': self,
  2190. 'to_process_lines': [],
  2191. 'total_balance': 0.0,
  2192. 'total_residual': 0.0,
  2193. 'total_amount_currency': 0.0,
  2194. 'total_residual_currency': 0.0,
  2195. }
  2196. currencies = set()
  2197. has_term_lines = False
  2198. for line in self.line_ids:
  2199. if line.account_type in ('asset_receivable', 'liability_payable'):
  2200. sign = 1 if line.balance > 0.0 else -1
  2201. currencies.add(line.currency_id)
  2202. has_term_lines = True
  2203. values['total_balance'] += sign * line.balance
  2204. values['total_residual'] += sign * line.amount_residual
  2205. values['total_amount_currency'] += sign * line.amount_currency
  2206. values['total_residual_currency'] += sign * line.amount_residual_currency
  2207. elif line.tax_line_id.tax_exigibility == 'on_payment':
  2208. values['to_process_lines'].append(('tax', line))
  2209. currencies.add(line.currency_id)
  2210. elif 'on_payment' in line.tax_ids.flatten_taxes_hierarchy().mapped('tax_exigibility'):
  2211. values['to_process_lines'].append(('base', line))
  2212. currencies.add(line.currency_id)
  2213. if not values['to_process_lines'] or not has_term_lines:
  2214. return None
  2215. # Compute the currency on which made the percentage.
  2216. if len(currencies) == 1:
  2217. values['currency'] = list(currencies)[0]
  2218. else:
  2219. # Don't support the case where there is multiple involved currencies.
  2220. return None
  2221. # Determine whether the move is now fully paid.
  2222. values['is_fully_paid'] = self.company_id.currency_id.is_zero(values['total_residual']) \
  2223. or values['currency'].is_zero(values['total_residual_currency'])
  2224. return values
  2225. # -------------------------------------------------------------------------
  2226. # SEQUENCE MIXIN
  2227. # -------------------------------------------------------------------------
  2228. def _must_check_constrains_date_sequence(self):
  2229. # OVERRIDES sequence.mixin
  2230. return not self.quick_edit_mode
  2231. def _get_last_sequence_domain(self, relaxed=False):
  2232. # EXTENDS account sequence.mixin
  2233. self.ensure_one()
  2234. if not self.date or not self.journal_id:
  2235. return "WHERE FALSE", {}
  2236. where_string = "WHERE journal_id = %(journal_id)s AND name != '/'"
  2237. param = {'journal_id': self.journal_id.id}
  2238. is_payment = self.payment_id or self._context.get('is_payment')
  2239. if not relaxed:
  2240. domain = [('journal_id', '=', self.journal_id.id), ('id', '!=', self.id or self._origin.id), ('name', 'not in', ('/', '', False))]
  2241. if self.journal_id.refund_sequence:
  2242. refund_types = ('out_refund', 'in_refund')
  2243. domain += [('move_type', 'in' if self.move_type in refund_types else 'not in', refund_types)]
  2244. if self.journal_id.payment_sequence:
  2245. domain += [('payment_id', '!=' if is_payment else '=', False)]
  2246. reference_move_name = self.search(domain + [('date', '<=', self.date)], order='date desc', limit=1).name
  2247. if not reference_move_name:
  2248. reference_move_name = self.search(domain, order='date asc', limit=1).name
  2249. sequence_number_reset = self._deduce_sequence_number_reset(reference_move_name)
  2250. if sequence_number_reset == 'year':
  2251. where_string += " AND date_trunc('year', date::timestamp without time zone) = date_trunc('year', %(date)s) "
  2252. param['date'] = self.date
  2253. param['anti_regex'] = re.sub(r"\?P<\w+>", "?:", self._sequence_monthly_regex.split('(?P<seq>')[0]) + '$'
  2254. elif sequence_number_reset == 'month':
  2255. where_string += " AND date_trunc('month', date::timestamp without time zone) = date_trunc('month', %(date)s) "
  2256. param['date'] = self.date
  2257. else:
  2258. param['anti_regex'] = re.sub(r"\?P<\w+>", "?:", self._sequence_yearly_regex.split('(?P<seq>')[0]) + '$'
  2259. if param.get('anti_regex') and not self.journal_id.sequence_override_regex:
  2260. where_string += " AND sequence_prefix !~ %(anti_regex)s "
  2261. if self.journal_id.refund_sequence:
  2262. if self.move_type in ('out_refund', 'in_refund'):
  2263. where_string += " AND move_type IN ('out_refund', 'in_refund') "
  2264. else:
  2265. where_string += " AND move_type NOT IN ('out_refund', 'in_refund') "
  2266. elif self.journal_id.payment_sequence:
  2267. if is_payment:
  2268. where_string += " AND payment_id IS NOT NULL "
  2269. else:
  2270. where_string += " AND payment_id IS NULL "
  2271. return where_string, param
  2272. def _get_starting_sequence(self):
  2273. # EXTENDS account sequence.mixin
  2274. self.ensure_one()
  2275. is_payment = self.payment_id or self._context.get('is_payment')
  2276. if self.journal_id.type in ['sale', 'bank', 'cash']:
  2277. starting_sequence = "%s/%04d/00000" % (self.journal_id.code, self.date.year)
  2278. else:
  2279. starting_sequence = "%s/%04d/%02d/0000" % (self.journal_id.code, self.date.year, self.date.month)
  2280. if self.journal_id.refund_sequence and self.move_type in ('out_refund', 'in_refund'):
  2281. starting_sequence = "R" + starting_sequence
  2282. if self.journal_id.payment_sequence and is_payment:
  2283. starting_sequence = "P" + starting_sequence
  2284. return starting_sequence
  2285. # -------------------------------------------------------------------------
  2286. # PAYMENT REFERENCE
  2287. # -------------------------------------------------------------------------
  2288. def _get_invoice_reference_euro_invoice(self):
  2289. """ This computes the reference based on the RF Creditor Reference.
  2290. The data of the reference is the database id number of the invoice.
  2291. For instance, if an invoice is issued with id 43, the check number
  2292. is 07 so the reference will be 'RF07 43'.
  2293. """
  2294. self.ensure_one()
  2295. return format_rf_reference(self.id)
  2296. def _get_invoice_reference_euro_partner(self):
  2297. """ This computes the reference based on the RF Creditor Reference.
  2298. The data of the reference is the user defined reference of the
  2299. partner or the database id number of the parter.
  2300. For instance, if an invoice is issued for the partner with internal
  2301. reference 'food buyer 654', the digits will be extracted and used as
  2302. the data. This will lead to a check number equal to 00 and the
  2303. reference will be 'RF00 654'.
  2304. If no reference is set for the partner, its id in the database will
  2305. be used.
  2306. """
  2307. self.ensure_one()
  2308. partner_ref = self.partner_id.ref
  2309. partner_ref_nr = re.sub(r'\D', '', partner_ref or '')[-21:] or str(self.partner_id.id)[-21:]
  2310. partner_ref_nr = partner_ref_nr[-21:]
  2311. return format_rf_reference(partner_ref_nr)
  2312. def _get_invoice_reference_odoo_invoice(self):
  2313. """ This computes the reference based on the Odoo format.
  2314. We simply return the number of the invoice, defined on the journal
  2315. sequence.
  2316. """
  2317. self.ensure_one()
  2318. return self.name
  2319. def _get_invoice_reference_odoo_partner(self):
  2320. """ This computes the reference based on the Odoo format.
  2321. The data used is the reference set on the partner or its database
  2322. id otherwise. For instance if the reference of the customer is
  2323. 'dumb customer 97', the reference will be 'CUST/dumb customer 97'.
  2324. """
  2325. ref = self.partner_id.ref or str(self.partner_id.id)
  2326. prefix = _('CUST')
  2327. return '%s/%s' % (prefix, ref)
  2328. def _get_invoice_computed_reference(self):
  2329. self.ensure_one()
  2330. if self.journal_id.invoice_reference_type == 'none':
  2331. return ''
  2332. ref_function = getattr(self, f'_get_invoice_reference_{self.journal_id.invoice_reference_model}_{self.journal_id.invoice_reference_type}', None)
  2333. if ref_function is None:
  2334. raise UserError(_("The combination of reference model and reference type on the journal is not implemented"))
  2335. return ref_function()
  2336. # -------------------------------------------------------------------------
  2337. # QUICK ENCODING
  2338. # -------------------------------------------------------------------------
  2339. @api.model
  2340. def _get_frequent_account_and_taxes(self, company_id, partner_id, move_type):
  2341. """
  2342. Returns the most used accounts and taxes for a given partner and company,
  2343. eventually filtered according to the move type.
  2344. """
  2345. if not partner_id:
  2346. return 0, False, False
  2347. where_internal_group = ""
  2348. if move_type in self.env['account.move'].get_inbound_types(include_receipts=True):
  2349. where_internal_group = "AND account.internal_group = 'income'"
  2350. elif move_type in self.env['account.move'].get_outbound_types(include_receipts=True):
  2351. where_internal_group = "AND account.internal_group = 'expense'"
  2352. self._cr.execute(f"""
  2353. SELECT
  2354. COUNT(foo.id), foo.account_id, foo.taxes
  2355. FROM
  2356. (
  2357. SELECT
  2358. account.id AS account_id,
  2359. account.code,
  2360. aml.id,
  2361. ARRAY_AGG(tax_rel.account_tax_id) AS taxes
  2362. FROM account_account account
  2363. LEFT JOIN account_move_line aml
  2364. ON (account.id = aml.account_id
  2365. AND aml.partner_id = %s
  2366. AND aml.date >= now() - interval '2 years')
  2367. LEFT JOIN account_move_line_account_tax_rel tax_rel ON (aml.id = tax_rel.account_move_line_id)
  2368. WHERE
  2369. account.company_id = %s
  2370. AND account.deprecated = FALSE
  2371. {where_internal_group}
  2372. GROUP BY account.id, account.code, aml.id
  2373. ) AS foo
  2374. GROUP BY foo.account_id, foo.code, foo.taxes
  2375. ORDER BY COUNT(foo.id) DESC, foo.code
  2376. LIMIT 1
  2377. """, [partner_id, company_id])
  2378. return self._cr.fetchone()
  2379. def _get_quick_edit_suggestions(self):
  2380. """
  2381. Returns a dictionnary containing the suggested values when creating a new
  2382. line with the quick_edit_total_amount set. We will compute the price_unit
  2383. that has to be set with the correct that in order to match this total amount.
  2384. If the vendor/customer is set, we will suggest the most frequently used account
  2385. for that partner as the default one, otherwise the default of the journal.
  2386. """
  2387. self.ensure_one()
  2388. if not self.quick_edit_mode or not self.quick_edit_total_amount:
  2389. return False
  2390. count, account_id, tax_ids = self._get_frequent_account_and_taxes(
  2391. self.company_id.id,
  2392. self.partner_id.id,
  2393. self.move_type,
  2394. )
  2395. if count:
  2396. taxes = self.env['account.tax'].browse(tax_ids)
  2397. else:
  2398. account_id = self.journal_id.default_account_id.id
  2399. if self.is_sale_document(include_receipts=True):
  2400. taxes = self.journal_id.default_account_id.tax_ids.filtered(lambda tax: tax.type_tax_use == 'sale')
  2401. else:
  2402. taxes = self.journal_id.default_account_id.tax_ids.filtered(lambda tax: tax.type_tax_use == 'purchase')
  2403. if not taxes:
  2404. taxes = (
  2405. self.journal_id.company_id.account_sale_tax_id
  2406. if self.journal_id.type == 'sale' else
  2407. self.journal_id.company_id.account_purchase_tax_id
  2408. )
  2409. taxes = self.fiscal_position_id.map_tax(taxes)
  2410. # When a payment term has an early payment discount and the company's epd computation is set to 'mixed', recomputing
  2411. # the untaxed amount should take in consideration the discount percentage otherwise we'd get a wrong value.
  2412. # Since in a payment term we can have multiple lines with multiple discounts, handling all cases can get
  2413. # complicated. For this we check that we have only one line with one discount and handle only this case.
  2414. # We also check that we have one percentage tax for the same reason.
  2415. # In one example: let's say: base = 100, discount = 2%, tax = 21%
  2416. # the total will be calculated as: total = base + (base * (1 - discount)) * tax
  2417. # If we manipulate the equation to get the base from the total, we'll have base = total / ((1 - discount) * tax + 1)
  2418. term_lines = self.invoice_payment_term_id.line_ids
  2419. discount_percentage = term_lines.discount_percentage if len(term_lines) == 1 else 0
  2420. remaining_amount = self.quick_edit_total_amount - self.tax_totals['amount_total']
  2421. if (
  2422. discount_percentage
  2423. and self.company_id.early_pay_discount_computation == 'mixed'
  2424. and len(taxes) == 1
  2425. and taxes.amount_type == 'percent'
  2426. ):
  2427. price_untaxed = self.currency_id.round(
  2428. remaining_amount / (((1.0 - discount_percentage / 100.0) * (taxes.amount / 100.0)) + 1.0))
  2429. else:
  2430. price_untaxed = taxes.with_context(force_price_include=True).compute_all(remaining_amount)['total_excluded']
  2431. return {'account_id': account_id, 'tax_ids': taxes.ids, 'price_unit': price_untaxed}
  2432. @api.onchange('quick_edit_mode', 'journal_id', 'company_id')
  2433. def _quick_edit_mode_suggest_invoice_date(self):
  2434. """Suggest the Customer Invoice/Vendor Bill date based on previous invoice and lock dates"""
  2435. for record in self:
  2436. if record.quick_edit_mode and not record.invoice_date:
  2437. invoice_date = fields.Date.context_today(self)
  2438. prev_move = self.search([('state', '=', 'posted'),
  2439. ('journal_id', '=', record.journal_id.id),
  2440. ('company_id', '=', record.company_id.id),
  2441. ('invoice_date', '!=', False)],
  2442. limit=1)
  2443. if prev_move:
  2444. invoice_date = self._get_accounting_date(prev_move.invoice_date, False)
  2445. record.invoice_date = invoice_date
  2446. @api.onchange('quick_edit_total_amount', 'partner_id')
  2447. def _onchange_quick_edit_total_amount(self):
  2448. """
  2449. Creates a new line with the suggested values (for the account, the price_unit,
  2450. and the tax) such that the total amount matches the quick total amount.
  2451. """
  2452. if (
  2453. not self.quick_edit_total_amount
  2454. or not self.quick_edit_mode
  2455. or len(self.invoice_line_ids) > 0
  2456. ):
  2457. return
  2458. suggestions = self.quick_encoding_vals
  2459. self.invoice_line_ids = [Command.clear()]
  2460. self.invoice_line_ids += self.env['account.move.line'].new({
  2461. 'partner_id': self.partner_id,
  2462. 'account_id': suggestions['account_id'],
  2463. 'currency_id': self.currency_id.id,
  2464. 'price_unit': suggestions['price_unit'],
  2465. 'tax_ids': [Command.set(suggestions['tax_ids'])],
  2466. })
  2467. self._check_total_amount(self.quick_edit_total_amount)
  2468. @api.onchange('invoice_line_ids')
  2469. def _onchange_quick_edit_line_ids(self):
  2470. quick_encode_suggestion = self.env.context.get('quick_encoding_vals')
  2471. if (
  2472. not self.quick_edit_total_amount
  2473. or not self.quick_edit_mode
  2474. or not self.invoice_line_ids
  2475. or not quick_encode_suggestion
  2476. or not quick_encode_suggestion['price_unit'] == self.invoice_line_ids[-1].price_unit
  2477. ):
  2478. return
  2479. self._check_total_amount(self.quick_edit_total_amount)
  2480. def _check_total_amount(self, amount_total):
  2481. """
  2482. Verifies that the total amount corresponds to the quick total amount chosen as some
  2483. rounding errors may appear. In such a case, we round up the tax such that the total
  2484. is equal to the quick total amount set
  2485. E.g.: 100€ including 21% tax: base = 82.64, tax = 17.35, total = 99.99
  2486. The tax will be set to 17.36 in order to have a total of 100.00
  2487. """
  2488. if not self.tax_totals or not amount_total:
  2489. return
  2490. totals = self.tax_totals
  2491. tax_amount_rounding_error = amount_total - totals['amount_total']
  2492. if not float_is_zero(tax_amount_rounding_error, precision_rounding=self.currency_id.rounding):
  2493. if _('Untaxed Amount') in totals['groups_by_subtotal']:
  2494. totals['groups_by_subtotal'][_('Untaxed Amount')][0]['tax_group_amount'] += tax_amount_rounding_error
  2495. totals['amount_total'] = amount_total
  2496. self.tax_totals = totals
  2497. # -------------------------------------------------------------------------
  2498. # HASH
  2499. # -------------------------------------------------------------------------
  2500. def _get_integrity_hash_fields(self):
  2501. # Use the latest hash version by default, but keep the old one for backward compatibility when generating the integrity report.
  2502. hash_version = self._context.get('hash_version', MAX_HASH_VERSION)
  2503. if hash_version == 1:
  2504. return ['date', 'journal_id', 'company_id']
  2505. elif hash_version in (2, 3):
  2506. return ['name', 'date', 'journal_id', 'company_id']
  2507. raise NotImplementedError(f"hash_version={hash_version} doesn't exist")
  2508. def _get_integrity_hash_fields_and_subfields(self):
  2509. return self._get_integrity_hash_fields() + [f'line_ids.{subfield}' for subfield in self.line_ids._get_integrity_hash_fields()]
  2510. def _get_new_hash(self, secure_seq_number):
  2511. """ Returns the hash to write on journal entries when they get posted"""
  2512. self.ensure_one()
  2513. #get the only one exact previous move in the securisation sequence
  2514. prev_move = self.sudo().search([('state', '=', 'posted'),
  2515. ('company_id', '=', self.company_id.id),
  2516. ('journal_id', '=', self.journal_id.id),
  2517. ('secure_sequence_number', '!=', 0),
  2518. ('secure_sequence_number', '=', int(secure_seq_number) - 1)])
  2519. if prev_move and len(prev_move) != 1:
  2520. raise UserError(
  2521. _('An error occurred when computing the inalterability. Impossible to get the unique previous posted journal entry.'))
  2522. #build and return the hash
  2523. return self._compute_hash(prev_move.inalterable_hash if prev_move else u'')
  2524. def _compute_hash(self, previous_hash):
  2525. """ Computes the hash of the browse_record given as self, based on the hash
  2526. of the previous record in the company's securisation sequence given as parameter"""
  2527. self.ensure_one()
  2528. hash_string = sha256((previous_hash + self.string_to_hash).encode('utf-8'))
  2529. return hash_string.hexdigest()
  2530. @api.depends(lambda self: self._get_integrity_hash_fields_and_subfields())
  2531. @api.depends_context('hash_version')
  2532. def _compute_string_to_hash(self):
  2533. def _getattrstring(obj, field_str):
  2534. hash_version = self._context.get('hash_version', MAX_HASH_VERSION)
  2535. field_value = obj[field_str]
  2536. if obj._fields[field_str].type == 'many2one':
  2537. field_value = field_value.id
  2538. if obj._fields[field_str].type == 'monetary' and hash_version >= 3:
  2539. return float_repr(field_value, obj.currency_id.decimal_places)
  2540. return str(field_value)
  2541. for move in self:
  2542. values = {}
  2543. for field in move._get_integrity_hash_fields():
  2544. values[field] = _getattrstring(move, field)
  2545. for line in move.line_ids:
  2546. for field in line._get_integrity_hash_fields():
  2547. k = 'line_%d_%s' % (line.id, field)
  2548. values[k] = _getattrstring(line, field)
  2549. #make the json serialization canonical
  2550. # (https://tools.ietf.org/html/draft-staykov-hu-json-canonical-form-00)
  2551. move.string_to_hash = dumps(values, sort_keys=True,
  2552. ensure_ascii=True, indent=None,
  2553. separators=(',', ':'))
  2554. # -------------------------------------------------------------------------
  2555. # RECURRING ENTRIES
  2556. # -------------------------------------------------------------------------
  2557. @api.model
  2558. def _apply_delta_recurring_entries(self, date, date_origin, period):
  2559. '''Advances date by `period` months, maintaining original day of the month if possible.'''
  2560. deltas = {'monthly': 1, 'quarterly': 3, 'yearly': 12}
  2561. prev_months = (date.year - date_origin.year) * 12 + date.month - date_origin.month
  2562. return date_origin + relativedelta(months=deltas[period] + prev_months)
  2563. def _copy_recurring_entries(self):
  2564. ''' Creates a copy of a recurring (periodic) entry and adjusts its dates for the next period.
  2565. Meant to be called right after posting a periodic entry.
  2566. Copies extra fields as defined by _get_fields_to_copy_recurring_entries().
  2567. '''
  2568. for record in self:
  2569. record.auto_post_origin_id = record.auto_post_origin_id or record # original entry references itself
  2570. next_date = self._apply_delta_recurring_entries(record.date, record.auto_post_origin_id.date, record.auto_post)
  2571. if not record.auto_post_until or next_date <= record.auto_post_until: # recurrence continues
  2572. record.copy(default=record._get_fields_to_copy_recurring_entries({'date': next_date}))
  2573. def _get_fields_to_copy_recurring_entries(self, values):
  2574. ''' Determines which extra fields to copy when copying a recurring entry.
  2575. To be extended by modules that add fields with copy=False (implicit or explicit)
  2576. whenever the opposite behavior is expected for recurring invoices.
  2577. '''
  2578. values.update({
  2579. 'auto_post': self.auto_post, # copy=False to avoid mistakes but should be the same in recurring copies
  2580. 'auto_post_until': self.auto_post_until, # same as above
  2581. 'auto_post_origin_id': self.auto_post_origin_id.id, # same as above
  2582. 'invoice_user_id': self.invoice_user_id.id, # otherwise user would be OdooBot
  2583. })
  2584. if self.invoice_date:
  2585. values.update({'invoice_date': self._apply_delta_recurring_entries(self.invoice_date, self.auto_post_origin_id.invoice_date, self.auto_post)})
  2586. if not self.invoice_payment_term_id and self.invoice_date_due:
  2587. # no payment terms: maintain timedelta between due date and accounting date
  2588. values.update({'invoice_date_due': values['date'] + (self.invoice_date_due - self.date)})
  2589. return values
  2590. # -------------------------------------------------------------------------
  2591. # BUSINESS METHODS
  2592. # -------------------------------------------------------------------------
  2593. def _prepare_invoice_aggregated_taxes(self, filter_invl_to_apply=None, filter_tax_values_to_apply=None, grouping_key_generator=None):
  2594. self.ensure_one()
  2595. base_lines = [
  2596. x._convert_to_tax_base_line_dict()
  2597. for x in self.line_ids.filtered(lambda x: x.display_type == 'product' and (not filter_invl_to_apply or filter_invl_to_apply(x)))
  2598. ]
  2599. to_process = []
  2600. for base_line in base_lines:
  2601. to_update_vals, tax_values_list = self.env['account.tax']._compute_taxes_for_single_line(base_line)
  2602. to_process.append((base_line, to_update_vals, tax_values_list))
  2603. return self.env['account.tax']._aggregate_taxes(
  2604. to_process,
  2605. filter_tax_values_to_apply=filter_tax_values_to_apply,
  2606. grouping_key_generator=grouping_key_generator,
  2607. )
  2608. def _get_invoice_counterpart_amls_for_early_payment_discount_per_payment_term_line(self):
  2609. """ Helper to get the values to create the counterpart journal items on the register payment wizard and the
  2610. bank reconciliation widget in case of an early payment discount. When the early payment discount computation
  2611. is included, we need to compute the base amounts / tax amounts for each receivable / payable but we need to
  2612. take care about the rounding issues. For others computations, we need to balance the discount you get.
  2613. :return: A list of values to create the counterpart journal items split in 3 categories:
  2614. * term_lines: The journal items containing the discount amounts for each receivable line when the
  2615. discount computation is excluded / mixed.
  2616. * tax_lines: The journal items acting as tax lines when the discount computation is included.
  2617. * base_lines: The journal items acting as base for tax lines when the discount computation is included.
  2618. """
  2619. self.ensure_one()
  2620. def grouping_key_generator(base_line, tax_values):
  2621. return self.env['account.tax']._get_generation_dict_from_base_line(base_line, tax_values)
  2622. def inverse_tax_rep(tax_rep):
  2623. tax = tax_rep.tax_id
  2624. index = list(tax.invoice_repartition_line_ids).index(tax_rep)
  2625. return tax.refund_repartition_line_ids[index]
  2626. # Get the current tax amounts in the current invoice.
  2627. tax_amounts = {
  2628. inverse_tax_rep(line.tax_repartition_line_id).id: {
  2629. 'amount_currency': line.amount_currency,
  2630. 'balance': line.balance,
  2631. }
  2632. for line in self.line_ids.filtered(lambda x: x.display_type == 'tax')
  2633. }
  2634. product_lines = self.line_ids.filtered(lambda x: x.display_type == 'product')
  2635. base_lines = [
  2636. {
  2637. **x._convert_to_tax_base_line_dict(),
  2638. 'is_refund': True,
  2639. }
  2640. for x in product_lines
  2641. ]
  2642. for base_line in base_lines:
  2643. base_line['taxes'] = base_line['taxes'].filtered(lambda t: t.amount_type != 'fixed')
  2644. if self.is_inbound(include_receipts=True):
  2645. cash_discount_account = self.company_id.account_journal_early_pay_discount_loss_account_id
  2646. else:
  2647. cash_discount_account = self.company_id.account_journal_early_pay_discount_gain_account_id
  2648. res = {
  2649. 'term_lines': defaultdict(lambda: {}),
  2650. 'tax_lines': defaultdict(lambda: {}),
  2651. 'base_lines': defaultdict(lambda: {}),
  2652. }
  2653. early_pay_discount_computation = self.company_id.early_pay_discount_computation
  2654. base_per_percentage = {}
  2655. tax_computation_per_percentage = {}
  2656. for aml in self.line_ids.filtered(lambda x: x.display_type == 'payment_term'):
  2657. if not aml.discount_percentage:
  2658. continue
  2659. term_amount_currency = aml.amount_currency - aml.discount_amount_currency
  2660. term_balance = aml.balance - aml.discount_balance
  2661. if early_pay_discount_computation == 'included' and product_lines.tax_ids:
  2662. # Compute the amounts for each percentage.
  2663. if aml.discount_percentage not in tax_computation_per_percentage:
  2664. # Compute the base amounts.
  2665. base_per_percentage[aml.discount_percentage] = resulting_delta_base_details = {}
  2666. to_process = []
  2667. for base_line in base_lines:
  2668. invoice_line = base_line['record']
  2669. to_update_vals, tax_values_list = self.env['account.tax']._compute_taxes_for_single_line(
  2670. base_line,
  2671. early_pay_discount_computation=early_pay_discount_computation,
  2672. early_pay_discount_percentage=aml.discount_percentage,
  2673. )
  2674. to_process.append((base_line, to_update_vals, tax_values_list))
  2675. grouping_dict = {
  2676. 'tax_ids': [Command.set(base_line['taxes'].ids)],
  2677. 'tax_tag_ids': to_update_vals['tax_tag_ids'],
  2678. 'partner_id': base_line['partner'].id,
  2679. 'currency_id': base_line['currency'].id,
  2680. 'account_id': cash_discount_account.id,
  2681. 'analytic_distribution': base_line['analytic_distribution'],
  2682. }
  2683. base_detail = resulting_delta_base_details.setdefault(frozendict(grouping_dict), {
  2684. 'balance': 0.0,
  2685. 'amount_currency': 0.0,
  2686. })
  2687. amount_currency = self.currency_id\
  2688. .round(self.direction_sign * to_update_vals['price_subtotal'] - invoice_line.amount_currency)
  2689. balance = self.company_currency_id\
  2690. .round(amount_currency / base_line['rate'])
  2691. base_detail['balance'] += balance
  2692. base_detail['amount_currency'] += amount_currency
  2693. # Compute the tax amounts.
  2694. tax_details_with_epd = self.env['account.tax']._aggregate_taxes(
  2695. to_process,
  2696. grouping_key_generator=grouping_key_generator,
  2697. )
  2698. tax_computation_per_percentage[aml.discount_percentage] = resulting_delta_tax_details = {}
  2699. for tax_detail in tax_details_with_epd['tax_details'].values():
  2700. tax_amount_without_epd = tax_amounts.get(tax_detail['tax_repartition_line_id'])
  2701. if not tax_amount_without_epd:
  2702. continue
  2703. tax_amount_currency = self.currency_id\
  2704. .round(self.direction_sign * tax_detail['tax_amount_currency'] - tax_amount_without_epd['amount_currency'])
  2705. tax_amount = self.company_currency_id\
  2706. .round(self.direction_sign * tax_detail['tax_amount'] - tax_amount_without_epd['balance'])
  2707. if self.currency_id.is_zero(tax_amount_currency) and self.company_currency_id.is_zero(tax_amount):
  2708. continue
  2709. resulting_delta_tax_details[tax_detail['tax_repartition_line_id']] = {
  2710. **tax_detail,
  2711. 'amount_currency': tax_amount_currency,
  2712. 'balance': tax_amount,
  2713. }
  2714. # Multiply each amount by the percentage paid by the current payment term line.
  2715. percentage_paid = abs(aml.amount_residual_currency / self.amount_total)
  2716. for tax_detail in tax_computation_per_percentage[aml.discount_percentage].values():
  2717. tax_rep = self.env['account.tax.repartition.line'].browse(tax_detail['tax_repartition_line_id'])
  2718. tax = tax_rep.tax_id
  2719. grouping_dict = {
  2720. 'account_id': tax_detail['account_id'],
  2721. 'partner_id': tax_detail['partner_id'],
  2722. 'currency_id': tax_detail['currency_id'],
  2723. 'analytic_distribution': tax_detail['analytic_distribution'],
  2724. 'tax_repartition_line_id': tax_rep.id,
  2725. 'tax_ids': tax_detail['tax_ids'],
  2726. 'tax_tag_ids': tax_detail['tax_tag_ids'],
  2727. 'group_tax_id': tax_detail['tax_id'] if tax_detail['tax_id'] != tax.id else None,
  2728. }
  2729. res['tax_lines'][aml][frozendict(grouping_dict)] = {
  2730. 'name': _("Early Payment Discount (%s)", tax.name),
  2731. 'amount_currency': aml.currency_id.round(tax_detail['amount_currency'] * percentage_paid),
  2732. 'balance': aml.company_currency_id.round(tax_detail['balance'] * percentage_paid),
  2733. 'tax_tag_invert': True,
  2734. }
  2735. for grouping_dict, base_detail in base_per_percentage[aml.discount_percentage].items():
  2736. res['base_lines'][aml][grouping_dict] = {
  2737. 'name': _("Early Payment Discount"),
  2738. 'amount_currency': aml.currency_id.round(base_detail['amount_currency'] * percentage_paid),
  2739. 'balance': aml.company_currency_id.round(base_detail['balance'] * percentage_paid),
  2740. }
  2741. # Fix the rounding issue if any.
  2742. delta_amount_currency = term_amount_currency \
  2743. - sum(x['amount_currency'] for x in res['base_lines'][aml].values()) \
  2744. - sum(x['amount_currency'] for x in res['tax_lines'][aml].values())
  2745. delta_balance = term_balance \
  2746. - sum(x['balance'] for x in res['base_lines'][aml].values()) \
  2747. - sum(x['balance'] for x in res['tax_lines'][aml].values())
  2748. last_tax_line = (list(res['tax_lines'][aml].values()) or list(res['base_lines'][aml].values()))[-1]
  2749. last_tax_line['amount_currency'] += delta_amount_currency
  2750. last_tax_line['balance'] += delta_balance
  2751. else:
  2752. grouping_dict = {'account_id': cash_discount_account.id}
  2753. res['term_lines'][aml][frozendict(grouping_dict)] = {
  2754. 'name': _("Early Payment Discount"),
  2755. 'partner_id': aml.partner_id.id,
  2756. 'currency_id': aml.currency_id.id,
  2757. 'amount_currency': term_amount_currency,
  2758. 'balance': term_balance,
  2759. }
  2760. return res
  2761. @api.model
  2762. def _get_invoice_counterpart_amls_for_early_payment_discount(self, aml_values_list, open_balance):
  2763. """ Helper to get the values to create the counterpart journal items on the register payment wizard and the
  2764. bank reconciliation widget in case of an early payment discount by taking care of the payment term lines we
  2765. are matching and the exchange difference in case of multi-currencies.
  2766. :param aml_values_list: A list of dictionaries containing:
  2767. * aml: The payment term line we match.
  2768. * amount_currency: The matched amount_currency for this line.
  2769. * balance: The matched balance for this line (could be different in case of multi-currencies).
  2770. :param open_balance: The current open balance to be covered by the early payment discount.
  2771. :return: A list of values to create the counterpart journal items split in 3 categories:
  2772. * term_lines: The journal items containing the discount amounts for each receivable line when the
  2773. discount computation is excluded / mixed.
  2774. * tax_lines: The journal items acting as tax lines when the discount computation is included.
  2775. * base_lines: The journal items acting as base for tax lines when the discount computation is included.
  2776. * exchange_lines: The journal items representing the exchange differences in case of multi-currencies.
  2777. """
  2778. res = {
  2779. 'base_lines': {},
  2780. 'tax_lines': {},
  2781. 'term_lines': {},
  2782. 'exchange_lines': {},
  2783. }
  2784. res_per_invoice = {}
  2785. for aml_values in aml_values_list:
  2786. aml = aml_values['aml']
  2787. invoice = aml.move_id
  2788. if invoice not in res_per_invoice:
  2789. res_per_invoice[invoice] = invoice._get_invoice_counterpart_amls_for_early_payment_discount_per_payment_term_line()
  2790. for key in ('base_lines', 'tax_lines', 'term_lines'):
  2791. for grouping_dict, vals in res_per_invoice[invoice][key][aml].items():
  2792. line_vals = res[key].setdefault(grouping_dict, {
  2793. **vals,
  2794. 'amount_currency': 0.0,
  2795. 'balance': 0.0,
  2796. })
  2797. line_vals['amount_currency'] += vals['amount_currency']
  2798. line_vals['balance'] += vals['balance']
  2799. # Track the balance to handle the exchange difference.
  2800. open_balance -= vals['balance']
  2801. exchange_diff_sign = aml.company_currency_id.compare_amounts(open_balance, 0.0)
  2802. if exchange_diff_sign != 0.0:
  2803. if exchange_diff_sign > 0.0:
  2804. exchange_line_account = aml.company_id.expense_currency_exchange_account_id
  2805. else:
  2806. exchange_line_account = aml.company_id.income_currency_exchange_account_id
  2807. grouping_dict = {
  2808. 'account_id': exchange_line_account.id,
  2809. 'currency_id': aml.currency_id.id,
  2810. 'partner_id': aml.partner_id.id,
  2811. }
  2812. line_vals = res['exchange_lines'].setdefault(frozendict(grouping_dict), {
  2813. **grouping_dict,
  2814. 'name': _("Early Payment Discount (Exchange Difference)"),
  2815. 'amount_currency': 0.0,
  2816. 'balance': 0.0,
  2817. })
  2818. line_vals['balance'] += open_balance
  2819. return {
  2820. key: [
  2821. {
  2822. **grouping_dict,
  2823. **vals,
  2824. }
  2825. for grouping_dict, vals in mapping.items()
  2826. ]
  2827. for key, mapping in res.items()
  2828. }
  2829. def _affect_tax_report(self):
  2830. return any(line._affect_tax_report() for line in (self.line_ids | self.invoice_line_ids))
  2831. def _get_move_display_name(self, show_ref=False):
  2832. ''' Helper to get the display name of an invoice depending of its type.
  2833. :param show_ref: A flag indicating of the display name must include or not the journal entry reference.
  2834. :return: A string representing the invoice.
  2835. '''
  2836. self.ensure_one()
  2837. name = ''
  2838. if self.state == 'draft':
  2839. name += {
  2840. 'out_invoice': _('Draft Invoice'),
  2841. 'out_refund': _('Draft Credit Note'),
  2842. 'in_invoice': _('Draft Bill'),
  2843. 'in_refund': _('Draft Vendor Credit Note'),
  2844. 'out_receipt': _('Draft Sales Receipt'),
  2845. 'in_receipt': _('Draft Purchase Receipt'),
  2846. 'entry': _('Draft Entry'),
  2847. }[self.move_type]
  2848. name += ' '
  2849. if not self.name or self.name == '/':
  2850. name += '(* %s)' % str(self.id)
  2851. else:
  2852. name += self.name
  2853. if self.env.context.get('input_full_display_name'):
  2854. if self.partner_id:
  2855. name += f', {self.partner_id.name}'
  2856. if self.date:
  2857. name += f', {format_date(self.env, self.date)}'
  2858. return name + (f" ({shorten(self.ref, width=50)})" if show_ref and self.ref else '')
  2859. def _get_reconciled_amls(self):
  2860. """Helper used to retrieve the reconciled move lines on this journal entry"""
  2861. reconciled_lines = self.line_ids.filtered(lambda line: line.account_id.account_type in ('asset_receivable', 'liability_payable'))
  2862. return reconciled_lines.mapped('matched_debit_ids.debit_move_id') + reconciled_lines.mapped('matched_credit_ids.credit_move_id')
  2863. def _get_reconciled_payments(self):
  2864. """Helper used to retrieve the reconciled payments on this journal entry"""
  2865. return self._get_reconciled_amls().move_id.payment_id
  2866. def _get_reconciled_statement_lines(self):
  2867. """Helper used to retrieve the reconciled statement lines on this journal entry"""
  2868. return self._get_reconciled_amls().move_id.statement_line_id
  2869. def _get_reconciled_invoices(self):
  2870. """Helper used to retrieve the reconciled invoices on this journal entry"""
  2871. return self._get_reconciled_amls().move_id.filtered(lambda move: move.is_invoice(include_receipts=True))
  2872. def _get_all_reconciled_invoice_partials(self):
  2873. self.ensure_one()
  2874. reconciled_lines = self.line_ids.filtered(lambda line: line.account_id.account_type in ('asset_receivable', 'liability_payable'))
  2875. if not reconciled_lines:
  2876. return {}
  2877. self.env['account.partial.reconcile'].flush_model([
  2878. 'credit_amount_currency', 'credit_move_id', 'debit_amount_currency',
  2879. 'debit_move_id', 'exchange_move_id',
  2880. ])
  2881. query = '''
  2882. SELECT
  2883. part.id,
  2884. part.exchange_move_id,
  2885. part.debit_amount_currency AS amount,
  2886. part.credit_move_id AS counterpart_line_id
  2887. FROM account_partial_reconcile part
  2888. WHERE part.debit_move_id IN %s
  2889. UNION ALL
  2890. SELECT
  2891. part.id,
  2892. part.exchange_move_id,
  2893. part.credit_amount_currency AS amount,
  2894. part.debit_move_id AS counterpart_line_id
  2895. FROM account_partial_reconcile part
  2896. WHERE part.credit_move_id IN %s
  2897. '''
  2898. self._cr.execute(query, [tuple(reconciled_lines.ids)] * 2)
  2899. partial_values_list = []
  2900. counterpart_line_ids = set()
  2901. exchange_move_ids = set()
  2902. for values in self._cr.dictfetchall():
  2903. partial_values_list.append({
  2904. 'aml_id': values['counterpart_line_id'],
  2905. 'partial_id': values['id'],
  2906. 'amount': values['amount'],
  2907. 'currency': self.currency_id,
  2908. })
  2909. counterpart_line_ids.add(values['counterpart_line_id'])
  2910. if values['exchange_move_id']:
  2911. exchange_move_ids.add(values['exchange_move_id'])
  2912. if exchange_move_ids:
  2913. self.env['account.move.line'].flush_model(['move_id'])
  2914. query = '''
  2915. SELECT
  2916. part.id,
  2917. part.credit_move_id AS counterpart_line_id
  2918. FROM account_partial_reconcile part
  2919. JOIN account_move_line credit_line ON credit_line.id = part.credit_move_id
  2920. WHERE credit_line.move_id IN %s AND part.debit_move_id IN %s
  2921. UNION ALL
  2922. SELECT
  2923. part.id,
  2924. part.debit_move_id AS counterpart_line_id
  2925. FROM account_partial_reconcile part
  2926. JOIN account_move_line debit_line ON debit_line.id = part.debit_move_id
  2927. WHERE debit_line.move_id IN %s AND part.credit_move_id IN %s
  2928. '''
  2929. self._cr.execute(query, [tuple(exchange_move_ids), tuple(counterpart_line_ids)] * 2)
  2930. for values in self._cr.dictfetchall():
  2931. counterpart_line_ids.add(values['counterpart_line_id'])
  2932. partial_values_list.append({
  2933. 'aml_id': values['counterpart_line_id'],
  2934. 'partial_id': values['id'],
  2935. 'currency': self.company_id.currency_id,
  2936. })
  2937. counterpart_lines = {x.id: x for x in self.env['account.move.line'].browse(counterpart_line_ids)}
  2938. for partial_values in partial_values_list:
  2939. partial_values['aml'] = counterpart_lines[partial_values['aml_id']]
  2940. partial_values['is_exchange'] = partial_values['aml'].move_id.id in exchange_move_ids
  2941. if partial_values['is_exchange']:
  2942. partial_values['amount'] = abs(partial_values['aml'].balance)
  2943. return partial_values_list
  2944. def _get_reconciled_invoices_partials(self):
  2945. ''' Helper to retrieve the details about reconciled invoices.
  2946. :return A list of tuple (partial, amount, invoice_line).
  2947. '''
  2948. self.ensure_one()
  2949. pay_term_lines = self.line_ids\
  2950. .filtered(lambda line: line.account_type in ('asset_receivable', 'liability_payable'))
  2951. invoice_partials = []
  2952. exchange_diff_moves = []
  2953. for partial in pay_term_lines.matched_debit_ids:
  2954. invoice_partials.append((partial, partial.credit_amount_currency, partial.debit_move_id))
  2955. if partial.exchange_move_id:
  2956. exchange_diff_moves.append(partial.exchange_move_id.id)
  2957. for partial in pay_term_lines.matched_credit_ids:
  2958. invoice_partials.append((partial, partial.debit_amount_currency, partial.credit_move_id))
  2959. if partial.exchange_move_id:
  2960. exchange_diff_moves.append(partial.exchange_move_id.id)
  2961. return invoice_partials, exchange_diff_moves
  2962. def _reverse_moves(self, default_values_list=None, cancel=False):
  2963. ''' Reverse a recordset of account.move.
  2964. If cancel parameter is true, the reconcilable or liquidity lines
  2965. of each original move will be reconciled with its reverse's.
  2966. :param default_values_list: A list of default values to consider per move.
  2967. ('type' & 'reversed_entry_id' are computed in the method).
  2968. :return: An account.move recordset, reverse of the current self.
  2969. '''
  2970. if not default_values_list:
  2971. default_values_list = [{} for move in self]
  2972. if cancel:
  2973. lines = self.mapped('line_ids')
  2974. # Avoid maximum recursion depth.
  2975. if lines:
  2976. lines.remove_move_reconcile()
  2977. reverse_moves = self.env['account.move']
  2978. for move, default_values in zip(self, default_values_list):
  2979. default_values.update({
  2980. 'move_type': TYPE_REVERSE_MAP[move.move_type],
  2981. 'reversed_entry_id': move.id,
  2982. 'partner_id': move.partner_id.id,
  2983. })
  2984. reverse_moves += move.with_context(
  2985. move_reverse_cancel=cancel,
  2986. include_business_fields=True,
  2987. skip_invoice_sync=move.move_type == 'entry',
  2988. ).copy(default_values)
  2989. reverse_moves.with_context(skip_invoice_sync=cancel).write({'line_ids': [
  2990. Command.update(line.id, {
  2991. 'balance': -line.balance,
  2992. 'amount_currency': -line.amount_currency,
  2993. })
  2994. for line in reverse_moves.line_ids
  2995. if line.move_id.move_type == 'entry' or line.display_type == 'cogs'
  2996. ]})
  2997. # Reconcile moves together to cancel the previous one.
  2998. if cancel:
  2999. reverse_moves.with_context(move_reverse_cancel=cancel)._post(soft=False)
  3000. for move, reverse_move in zip(self, reverse_moves):
  3001. group = defaultdict(list)
  3002. for line in (move.line_ids + reverse_move.line_ids).filtered(lambda l: not l.reconciled):
  3003. group[(line.account_id, line.currency_id)].append(line.id)
  3004. for (account, dummy), line_ids in group.items():
  3005. if account.reconcile or account.account_type in ('asset_cash', 'liability_credit_card'):
  3006. self.env['account.move.line'].browse(line_ids).with_context(move_reverse_cancel=cancel).reconcile()
  3007. return reverse_moves
  3008. def _post(self, soft=True):
  3009. """Post/Validate the documents.
  3010. Posting the documents will give it a number, and check that the document is
  3011. complete (some fields might not be required if not posted but are required
  3012. otherwise).
  3013. If the journal is locked with a hash table, it will be impossible to change
  3014. some fields afterwards.
  3015. :param soft (bool): if True, future documents are not immediately posted,
  3016. but are set to be auto posted automatically at the set accounting date.
  3017. Nothing will be performed on those documents before the accounting date.
  3018. :return Model<account.move>: the documents that have been posted
  3019. """
  3020. if not self.env.su and not self.env.user.has_group('account.group_account_invoice'):
  3021. raise AccessError(_("You don't have the access rights to post an invoice."))
  3022. for invoice in self.filtered(lambda move: move.is_invoice(include_receipts=True)):
  3023. if invoice.quick_edit_mode and invoice.quick_edit_total_amount and invoice.quick_edit_total_amount != invoice.amount_total:
  3024. raise UserError(_(
  3025. "The current total is %s but the expected total is %s. In order to post the invoice/bill, "
  3026. "you can adjust its lines or the expected Total (tax inc.).",
  3027. formatLang(self.env, invoice.amount_total, currency_obj=invoice.currency_id),
  3028. formatLang(self.env, invoice.quick_edit_total_amount, currency_obj=invoice.currency_id),
  3029. ))
  3030. if invoice.partner_bank_id and not invoice.partner_bank_id.active:
  3031. raise UserError(_(
  3032. "The recipient bank account linked to this invoice is archived.\n"
  3033. "So you cannot confirm the invoice."
  3034. ))
  3035. if float_compare(invoice.amount_total, 0.0, precision_rounding=invoice.currency_id.rounding) < 0:
  3036. raise UserError(_(
  3037. "You cannot validate an invoice with a negative total amount. "
  3038. "You should create a credit note instead. "
  3039. "Use the action menu to transform it into a credit note or refund."
  3040. ))
  3041. if not invoice.partner_id:
  3042. if invoice.is_sale_document():
  3043. raise UserError(_("The field 'Customer' is required, please complete it to validate the Customer Invoice."))
  3044. elif invoice.is_purchase_document():
  3045. raise UserError(_("The field 'Vendor' is required, please complete it to validate the Vendor Bill."))
  3046. # Handle case when the invoice_date is not set. In that case, the invoice_date is set at today and then,
  3047. # lines are recomputed accordingly.
  3048. if not invoice.invoice_date:
  3049. if invoice.is_sale_document(include_receipts=True):
  3050. invoice.invoice_date = fields.Date.context_today(self)
  3051. elif invoice.is_purchase_document(include_receipts=True):
  3052. raise UserError(_("The Bill/Refund date is required to validate this document."))
  3053. for move in self:
  3054. if move.state == 'posted':
  3055. raise UserError(_('The entry %s (id %s) is already posted.') % (move.name, move.id))
  3056. if not move.line_ids.filtered(lambda line: line.display_type not in ('line_section', 'line_note')):
  3057. raise UserError(_('You need to add a line before posting.'))
  3058. if not soft and move.auto_post != 'no' and move.date > fields.Date.context_today(self):
  3059. date_msg = move.date.strftime(get_lang(self.env).date_format)
  3060. raise UserError(_("This move is configured to be auto-posted on %s", date_msg))
  3061. if not move.journal_id.active:
  3062. raise UserError(_(
  3063. "You cannot post an entry in an archived journal (%(journal)s)",
  3064. journal=move.journal_id.display_name,
  3065. ))
  3066. if move.display_inactive_currency_warning:
  3067. raise UserError(_(
  3068. "You cannot validate a document with an inactive currency: %s",
  3069. move.currency_id.name
  3070. ))
  3071. if move.line_ids.account_id.filtered(lambda account: account.deprecated):
  3072. raise UserError(_("A line of this move is using a deprecated account, you cannot post it."))
  3073. if soft:
  3074. future_moves = self.filtered(lambda move: move.date > fields.Date.context_today(self))
  3075. for move in future_moves:
  3076. if move.auto_post == 'no':
  3077. move.auto_post = 'at_date'
  3078. msg = _('This move will be posted at the accounting date: %(date)s', date=format_date(self.env, move.date))
  3079. move.message_post(body=msg)
  3080. to_post = self - future_moves
  3081. else:
  3082. to_post = self
  3083. for move in to_post:
  3084. affects_tax_report = move._affect_tax_report()
  3085. lock_dates = move._get_violated_lock_dates(move.date, affects_tax_report)
  3086. if lock_dates:
  3087. move.date = move._get_accounting_date(move.invoice_date or move.date, affects_tax_report)
  3088. # Create the analytic lines in batch is faster as it leads to less cache invalidation.
  3089. to_post.line_ids._create_analytic_lines()
  3090. # Trigger copying for recurring invoices
  3091. to_post.filtered(lambda m: m.auto_post not in ('no', 'at_date'))._copy_recurring_entries()
  3092. for invoice in to_post:
  3093. # Fix inconsistencies that may occure if the OCR has been editing the invoice at the same time of a user. We force the
  3094. # partner on the lines to be the same as the one on the move, because that's the only one the user can see/edit.
  3095. wrong_lines = invoice.is_invoice() and invoice.line_ids.filtered(lambda aml:
  3096. aml.partner_id != invoice.commercial_partner_id
  3097. and aml.display_type not in ('line_note', 'line_section')
  3098. )
  3099. if wrong_lines:
  3100. wrong_lines.write({'partner_id': invoice.commercial_partner_id.id})
  3101. to_post.write({
  3102. 'state': 'posted',
  3103. 'posted_before': True,
  3104. })
  3105. for invoice in to_post:
  3106. invoice.message_subscribe([
  3107. p.id
  3108. for p in [invoice.partner_id]
  3109. if p not in invoice.sudo().message_partner_ids
  3110. ])
  3111. if (
  3112. invoice.is_sale_document()
  3113. and invoice.journal_id.sale_activity_type_id
  3114. and (invoice.journal_id.sale_activity_user_id or invoice.invoice_user_id).id not in (self.env.ref('base.user_root').id, False)
  3115. ):
  3116. invoice.activity_schedule(
  3117. date_deadline=min((date for date in invoice.line_ids.mapped('date_maturity') if date), default=invoice.date),
  3118. activity_type_id=invoice.journal_id.sale_activity_type_id.id,
  3119. summary=invoice.journal_id.sale_activity_note,
  3120. user_id=invoice.journal_id.sale_activity_user_id.id or invoice.invoice_user_id.id,
  3121. )
  3122. customer_count, supplier_count = defaultdict(int), defaultdict(int)
  3123. for invoice in to_post:
  3124. if invoice.is_sale_document():
  3125. customer_count[invoice.partner_id] += 1
  3126. elif invoice.is_purchase_document():
  3127. supplier_count[invoice.partner_id] += 1
  3128. elif invoice.move_type == 'entry':
  3129. sale_amls = invoice.line_ids.filtered(lambda line: line.partner_id and line.account_id.account_type == 'asset_receivable')
  3130. for partner in sale_amls.mapped('partner_id'):
  3131. customer_count[partner] += 1
  3132. purchase_amls = invoice.line_ids.filtered(lambda line: line.partner_id and line.account_id.account_type == 'liability_payable')
  3133. for partner in purchase_amls.mapped('partner_id'):
  3134. supplier_count[partner] += 1
  3135. for partner, count in customer_count.items():
  3136. (partner | partner.commercial_partner_id)._increase_rank('customer_rank', count)
  3137. for partner, count in supplier_count.items():
  3138. (partner | partner.commercial_partner_id)._increase_rank('supplier_rank', count)
  3139. # Trigger action for paid invoices if amount is zero
  3140. to_post.filtered(
  3141. lambda m: m.is_invoice(include_receipts=True) and m.currency_id.is_zero(m.amount_total)
  3142. )._invoice_paid_hook()
  3143. return to_post
  3144. def _find_and_set_purchase_orders(self, po_references, partner_id, amount_total, prefer_purchase_line=False, timeout=10):
  3145. # hook to be used with purchase, so that vendor bills are sync/autocompleted with purchase orders
  3146. self.ensure_one()
  3147. def _link_invoice_origin_to_purchase_orders(self, timeout=10):
  3148. for move in self.filtered(lambda m: m.move_type in self.get_purchase_types()):
  3149. references = [move.invoice_origin] if move.invoice_origin else []
  3150. move._find_and_set_purchase_orders(references, move.partner_id.id, move.amount_total, timeout)
  3151. return self
  3152. # -------------------------------------------------------------------------
  3153. # PUBLIC ACTIONS
  3154. # -------------------------------------------------------------------------
  3155. def open_reconcile_view(self):
  3156. return self.line_ids.open_reconcile_view()
  3157. def action_open_business_doc(self):
  3158. self.ensure_one()
  3159. if self.payment_id:
  3160. name = _("Payment")
  3161. res_model = 'account.payment'
  3162. res_id = self.payment_id.id
  3163. elif self.statement_line_id:
  3164. name = _("Bank Transaction")
  3165. res_model = 'account.bank.statement.line'
  3166. res_id = self.statement_line_id.id
  3167. else:
  3168. name = _("Journal Entry")
  3169. res_model = 'account.move'
  3170. res_id = self.id
  3171. return {
  3172. 'name': name,
  3173. 'type': 'ir.actions.act_window',
  3174. 'view_mode': 'form',
  3175. 'views': [(False, 'form')],
  3176. 'res_model': res_model,
  3177. 'res_id': res_id,
  3178. 'target': 'current',
  3179. }
  3180. def open_created_caba_entries(self):
  3181. self.ensure_one()
  3182. return {
  3183. 'type': 'ir.actions.act_window',
  3184. 'name': _("Cash Basis Entries"),
  3185. 'res_model': 'account.move',
  3186. 'view_mode': 'form',
  3187. 'domain': [('id', 'in', self.tax_cash_basis_created_move_ids.ids)],
  3188. 'views': [(self.env.ref('account.view_move_tree').id, 'tree'), (False, 'form')],
  3189. }
  3190. def open_duplicated_ref_bill_view(self):
  3191. moves = self + self.duplicated_ref_ids
  3192. action = self.env["ir.actions.actions"]._for_xml_id("account.action_move_line_form")
  3193. action['domain'] = [('id', 'in', moves.ids)]
  3194. return action
  3195. def action_switch_invoice_into_refund_credit_note(self):
  3196. for move in self:
  3197. if move.posted_before:
  3198. raise ValidationError(_("You cannot switch the type of a posted document."))
  3199. if move.move_type == 'entry':
  3200. raise ValidationError(_("This action isn't available for this document."))
  3201. in_out, old_move_type = move.move_type.split('_')
  3202. new_move_type = f"{in_out}_{'invoice' if old_move_type == 'refund' else 'refund'}"
  3203. move.name = False
  3204. move.write({
  3205. 'move_type': new_move_type,
  3206. 'partner_bank_id': False,
  3207. 'currency_id': move.currency_id.id,
  3208. })
  3209. if move.amount_total < 0:
  3210. move.write({
  3211. 'line_ids': [
  3212. Command.update(line.id, {'quantity': -line.quantity})
  3213. for line in move.line_ids
  3214. if line.display_type == 'product'
  3215. ]
  3216. })
  3217. def action_register_payment(self):
  3218. ''' Open the account.payment.register wizard to pay the selected journal entries.
  3219. :return: An action opening the account.payment.register wizard.
  3220. '''
  3221. return {
  3222. 'name': _('Register Payment'),
  3223. 'res_model': 'account.payment.register',
  3224. 'view_mode': 'form',
  3225. 'context': {
  3226. 'active_model': 'account.move',
  3227. 'active_ids': self.ids,
  3228. },
  3229. 'target': 'new',
  3230. 'type': 'ir.actions.act_window',
  3231. }
  3232. def action_invoice_print(self):
  3233. """ Print the invoice and mark it as sent, so that we can see more
  3234. easily the next step of the workflow
  3235. """
  3236. if any(not move.is_invoice(include_receipts=True) for move in self):
  3237. raise UserError(_("Only invoices could be printed."))
  3238. self.filtered(lambda inv: not inv.is_move_sent).write({'is_move_sent': True})
  3239. if self.user_has_groups('account.group_account_invoice'):
  3240. return self.env.ref('account.account_invoices').report_action(self)
  3241. else:
  3242. return self.env.ref('account.account_invoices_without_payment').report_action(self)
  3243. def action_duplicate(self):
  3244. # offer the possibility to duplicate thanks to a button instead of a hidden menu, which is more visible
  3245. self.ensure_one()
  3246. action = self.env["ir.actions.actions"]._for_xml_id("account.action_move_journal_line")
  3247. action['context'] = dict(self.env.context)
  3248. action['context']['form_view_initial_mode'] = 'edit'
  3249. action['context']['view_no_maturity'] = False
  3250. action['views'] = [(self.env.ref('account.view_move_form').id, 'form')]
  3251. action['res_id'] = self.copy().id
  3252. return action
  3253. def action_send_and_print(self):
  3254. return {
  3255. 'name': _('Send Invoice'),
  3256. 'res_model': 'account.invoice.send',
  3257. 'view_mode': 'form',
  3258. 'context': {
  3259. 'default_email_layout_xmlid': 'mail.mail_notification_layout_with_responsible_signature',
  3260. 'default_template_id': self.env.ref(self._get_mail_template()).id,
  3261. 'mark_invoice_as_sent': True,
  3262. 'active_model': 'account.move',
  3263. # Setting both active_id and active_ids is required, mimicking how direct call to
  3264. # ir.actions.act_window works
  3265. 'active_id': self.ids[0],
  3266. 'active_ids': self.ids,
  3267. },
  3268. 'target': 'new',
  3269. 'type': 'ir.actions.act_window',
  3270. }
  3271. def action_invoice_sent(self):
  3272. """ Open a window to compose an email, with the edi invoice template
  3273. message loaded by default
  3274. """
  3275. self.ensure_one()
  3276. template = self.env.ref(self._get_mail_template(), raise_if_not_found=False)
  3277. lang = False
  3278. if template:
  3279. lang = template._render_lang(self.ids)[self.id]
  3280. if not lang:
  3281. lang = get_lang(self.env).code
  3282. compose_form = self.env.ref('account.account_invoice_send_wizard_form', raise_if_not_found=False)
  3283. ctx = dict(
  3284. default_model='account.move',
  3285. default_res_id=self.id,
  3286. # For the sake of consistency we need a default_res_model if
  3287. # default_res_id is set. Not renaming default_model as it can
  3288. # create many side-effects.
  3289. default_res_model='account.move',
  3290. default_use_template=bool(template),
  3291. default_template_id=template and template.id or False,
  3292. default_composition_mode='comment',
  3293. mark_invoice_as_sent=True,
  3294. default_email_layout_xmlid="mail.mail_notification_layout_with_responsible_signature",
  3295. model_description=self.with_context(lang=lang).type_name,
  3296. force_email=True,
  3297. active_ids=self.ids,
  3298. )
  3299. report_action = {
  3300. 'name': _('Send Invoice'),
  3301. 'type': 'ir.actions.act_window',
  3302. 'view_type': 'form',
  3303. 'view_mode': 'form',
  3304. 'res_model': 'account.invoice.send',
  3305. 'views': [(compose_form.id, 'form')],
  3306. 'view_id': compose_form.id,
  3307. 'target': 'new',
  3308. 'context': ctx,
  3309. }
  3310. if self.env.is_admin() and not self.env.company.external_report_layout_id and not self.env.context.get('discard_logo_check'):
  3311. return self.env['ir.actions.report']._action_configure_external_report_layout(report_action)
  3312. return report_action
  3313. def preview_invoice(self):
  3314. self.ensure_one()
  3315. return {
  3316. 'type': 'ir.actions.act_url',
  3317. 'target': 'self',
  3318. 'url': self.get_portal_url(),
  3319. }
  3320. def action_reverse(self):
  3321. action = self.env["ir.actions.actions"]._for_xml_id("account.action_view_account_move_reversal")
  3322. if self.is_invoice():
  3323. action['name'] = _('Credit Note')
  3324. return action
  3325. def action_post(self):
  3326. moves_with_payments = self.filtered('payment_id')
  3327. other_moves = self - moves_with_payments
  3328. if moves_with_payments:
  3329. moves_with_payments.payment_id.action_post()
  3330. if other_moves:
  3331. other_moves._post(soft=False)
  3332. return False
  3333. def js_assign_outstanding_line(self, line_id):
  3334. ''' Called by the 'payment' widget to reconcile a suggested journal item to the present
  3335. invoice.
  3336. :param line_id: The id of the line to reconcile with the current invoice.
  3337. '''
  3338. self.ensure_one()
  3339. lines = self.env['account.move.line'].browse(line_id)
  3340. lines += self.line_ids.filtered(lambda line: line.account_id == lines[0].account_id and not line.reconciled)
  3341. return lines.reconcile()
  3342. def js_remove_outstanding_partial(self, partial_id):
  3343. ''' Called by the 'payment' widget to remove a reconciled entry to the present invoice.
  3344. :param partial_id: The id of an existing partial reconciled with the current invoice.
  3345. '''
  3346. self.ensure_one()
  3347. partial = self.env['account.partial.reconcile'].browse(partial_id)
  3348. return partial.unlink()
  3349. def button_set_checked(self):
  3350. for move in self:
  3351. move.to_check = False
  3352. def button_draft(self):
  3353. exchange_move_ids = set()
  3354. if self:
  3355. self.env['account.full.reconcile'].flush_model(['exchange_move_id'])
  3356. self.env['account.partial.reconcile'].flush_model(['exchange_move_id'])
  3357. self._cr.execute(
  3358. """
  3359. SELECT DISTINCT sub.exchange_move_id
  3360. FROM (
  3361. SELECT exchange_move_id
  3362. FROM account_full_reconcile
  3363. WHERE exchange_move_id IN %s
  3364. UNION ALL
  3365. SELECT exchange_move_id
  3366. FROM account_partial_reconcile
  3367. WHERE exchange_move_id IN %s
  3368. ) AS sub
  3369. """,
  3370. [tuple(self.ids), tuple(self.ids)],
  3371. )
  3372. exchange_move_ids = set([row[0] for row in self._cr.fetchall()])
  3373. for move in self:
  3374. if move.id in exchange_move_ids:
  3375. raise UserError(_('You cannot reset to draft an exchange difference journal entry.'))
  3376. if move.tax_cash_basis_rec_id or move.tax_cash_basis_origin_move_id:
  3377. # If the reconciliation was undone, move.tax_cash_basis_rec_id will be empty;
  3378. # but we still don't want to allow setting the caba entry to draft
  3379. # (it'll have been reversed automatically, so no manual intervention is required),
  3380. # so we also check tax_cash_basis_origin_move_id, which stays unchanged
  3381. # (we need both, as tax_cash_basis_origin_move_id did not exist in older versions).
  3382. raise UserError(_('You cannot reset to draft a tax cash basis journal entry.'))
  3383. if move.restrict_mode_hash_table and move.state == 'posted':
  3384. raise UserError(_('You cannot modify a posted entry of this journal because it is in strict mode.'))
  3385. # We remove all the analytics entries for this journal
  3386. move.mapped('line_ids.analytic_line_ids').unlink()
  3387. self.mapped('line_ids').remove_move_reconcile()
  3388. self.write({'state': 'draft', 'is_move_sent': False})
  3389. def button_cancel(self):
  3390. self.write({'auto_post': 'no', 'state': 'cancel'})
  3391. def action_activate_currency(self):
  3392. self.currency_id.filtered(lambda currency: not currency.active).write({'active': True})
  3393. def _get_mail_template(self):
  3394. """
  3395. :return: the correct mail template based on the current move type
  3396. """
  3397. return (
  3398. 'account.email_template_edi_credit_note'
  3399. if all(move.move_type == 'out_refund' for move in self)
  3400. else 'account.email_template_edi_invoice'
  3401. )
  3402. def _notify_get_recipients_groups(self, msg_vals=None):
  3403. groups = super()._notify_get_recipients_groups(msg_vals)
  3404. local_msg_vals = dict(msg_vals or {})
  3405. if self.move_type != 'entry':
  3406. # This allows partners added to the email list in the sending wizard to access this document.
  3407. for group_name, _group_method, group_data in groups:
  3408. if group_name == 'customer' and self._portal_ensure_token():
  3409. access_link = self._notify_get_action_link(
  3410. 'view', **local_msg_vals, access_token=self.access_token)
  3411. group_data.update({
  3412. 'has_button_access': True,
  3413. 'button_access': {
  3414. 'url': access_link,
  3415. },
  3416. })
  3417. return groups
  3418. def _get_report_base_filename(self):
  3419. return self._get_move_display_name()
  3420. # -------------------------------------------------------------------------
  3421. # CRON
  3422. # -------------------------------------------------------------------------
  3423. def _autopost_draft_entries(self):
  3424. ''' This method is called from a cron job.
  3425. It is used to post entries such as those created by the module
  3426. account_asset and recurring entries created in _post().
  3427. '''
  3428. moves = self.search([
  3429. ('state', '=', 'draft'),
  3430. ('date', '<=', fields.Date.context_today(self)),
  3431. ('auto_post', '!=', 'no'),
  3432. ('to_check', '=', False),
  3433. ], limit=100)
  3434. try: # try posting in batch
  3435. with self.env.cr.savepoint():
  3436. moves._post()
  3437. except UserError: # if at least one move cannot be posted, handle moves one by one
  3438. for move in moves:
  3439. try:
  3440. with self.env.cr.savepoint():
  3441. move._post()
  3442. except UserError as e:
  3443. move.to_check = True
  3444. msg = _('The move could not be posted for the following reason: %(error_message)s', error_message=e)
  3445. move.message_post(body=msg, message_type='comment')
  3446. if len(moves) == 100: # assumes there are more whenever search hits limit
  3447. self.env.ref('account.ir_cron_auto_post_draft_entry')._trigger()
  3448. # -------------------------------------------------------------------------
  3449. # HELPER METHODS
  3450. # -------------------------------------------------------------------------
  3451. @api.model
  3452. def get_invoice_types(self, include_receipts=False):
  3453. return self.get_sale_types(include_receipts) + self.get_purchase_types(include_receipts)
  3454. def is_invoice(self, include_receipts=False):
  3455. return self.is_sale_document(include_receipts) or self.is_purchase_document(include_receipts)
  3456. @api.model
  3457. def get_sale_types(self, include_receipts=False):
  3458. return ['out_invoice', 'out_refund'] + (include_receipts and ['out_receipt'] or [])
  3459. def is_sale_document(self, include_receipts=False):
  3460. return self.move_type in self.get_sale_types(include_receipts)
  3461. @api.model
  3462. def get_purchase_types(self, include_receipts=False):
  3463. return ['in_invoice', 'in_refund'] + (include_receipts and ['in_receipt'] or [])
  3464. def is_purchase_document(self, include_receipts=False):
  3465. return self.move_type in self.get_purchase_types(include_receipts)
  3466. @api.model
  3467. def get_inbound_types(self, include_receipts=True):
  3468. return ['out_invoice', 'in_refund'] + (include_receipts and ['out_receipt'] or [])
  3469. def is_inbound(self, include_receipts=True):
  3470. return self.move_type in self.get_inbound_types(include_receipts)
  3471. @api.model
  3472. def get_outbound_types(self, include_receipts=True):
  3473. return ['in_invoice', 'out_refund'] + (include_receipts and ['in_receipt'] or [])
  3474. def is_outbound(self, include_receipts=True):
  3475. return self.move_type in self.get_outbound_types(include_receipts)
  3476. def _get_accounting_date(self, invoice_date, has_tax):
  3477. """Get correct accounting date for previous periods, taking tax lock date into account.
  3478. When registering an invoice in the past, we still want the sequence to be increasing.
  3479. We then take the last day of the period, depending on the sequence format.
  3480. If there is a tax lock date and there are taxes involved, we register the invoice at the
  3481. last date of the first open period.
  3482. :param invoice_date (datetime.date): The invoice date
  3483. :param has_tax (bool): Iff any taxes are involved in the lines of the invoice
  3484. :return (datetime.date):
  3485. """
  3486. lock_dates = self._get_violated_lock_dates(invoice_date, has_tax)
  3487. today = fields.Date.today()
  3488. highest_name = self.highest_name or self._get_last_sequence(relaxed=True, lock=False)
  3489. number_reset = self._deduce_sequence_number_reset(highest_name)
  3490. if lock_dates:
  3491. invoice_date = lock_dates[-1][0] + timedelta(days=1)
  3492. if self.is_sale_document(include_receipts=True):
  3493. if lock_dates:
  3494. if not highest_name or number_reset == 'month':
  3495. return min(today, date_utils.get_month(invoice_date)[1])
  3496. elif number_reset == 'year':
  3497. return min(today, date_utils.end_of(invoice_date, 'year'))
  3498. else:
  3499. if not highest_name or number_reset == 'month':
  3500. if (today.year, today.month) > (invoice_date.year, invoice_date.month):
  3501. return date_utils.get_month(invoice_date)[1]
  3502. else:
  3503. return max(invoice_date, today)
  3504. elif number_reset == 'year':
  3505. if today.year > invoice_date.year:
  3506. return date(invoice_date.year, 12, 31)
  3507. else:
  3508. return max(invoice_date, today)
  3509. return invoice_date
  3510. def _get_violated_lock_dates(self, invoice_date, has_tax):
  3511. """Get all the lock dates affecting the current invoice_date.
  3512. :param invoice_date: The invoice date
  3513. :param has_tax: If any taxes are involved in the lines of the invoice
  3514. :return: a list of tuples containing the lock dates affecting this move, ordered chronologically.
  3515. """
  3516. locks = []
  3517. user_lock_date = self.company_id._get_user_fiscal_lock_date()
  3518. if invoice_date and user_lock_date and invoice_date <= user_lock_date:
  3519. locks.append((user_lock_date, _('user')))
  3520. tax_lock_date = self.company_id.tax_lock_date
  3521. if invoice_date and tax_lock_date and has_tax and invoice_date <= tax_lock_date:
  3522. locks.append((tax_lock_date, _('tax')))
  3523. locks.sort()
  3524. return locks
  3525. def _get_lock_date_message(self, invoice_date, has_tax):
  3526. """Get a message describing the latest lock date affecting the specified date.
  3527. :param invoice_date: The date to be checked
  3528. :param has_tax: If any taxes are involved in the lines of the invoice
  3529. :return: a message describing the latest lock date affecting this move and the date it will be
  3530. accounted on if posted, or False if no lock dates affect this move.
  3531. """
  3532. lock_dates = self._get_violated_lock_dates(invoice_date, has_tax)
  3533. if lock_dates:
  3534. invoice_date = self._get_accounting_date(invoice_date, has_tax)
  3535. lock_date, lock_type = lock_dates[-1]
  3536. tax_lock_date_message = _(
  3537. "The date is being set prior to the %(lock_type)s lock date %(lock_date)s. "
  3538. "The Journal Entry will be accounted on %(invoice_date)s upon posting.",
  3539. lock_type=lock_type,
  3540. lock_date=format_date(self.env, lock_date),
  3541. invoice_date=format_date(self.env, invoice_date))
  3542. return tax_lock_date_message
  3543. return False
  3544. @api.model
  3545. def _move_dict_to_preview_vals(self, move_vals, currency_id=None):
  3546. preview_vals = {
  3547. 'group_name': "%s, %s" % (format_date(self.env, move_vals['date']) or _('[Not set]'), move_vals['ref']),
  3548. 'items_vals': move_vals['line_ids'],
  3549. }
  3550. for line in preview_vals['items_vals']:
  3551. if 'partner_id' in line[2]:
  3552. # sudo is needed to compute display_name in a multi companies environment
  3553. line[2]['partner_id'] = self.env['res.partner'].browse(line[2]['partner_id']).sudo().display_name
  3554. line[2]['account_id'] = self.env['account.account'].browse(line[2]['account_id']).display_name or _('Destination Account')
  3555. line[2]['debit'] = currency_id and formatLang(self.env, line[2]['debit'], currency_obj=currency_id) or line[2]['debit']
  3556. line[2]['credit'] = currency_id and formatLang(self.env, line[2]['credit'], currency_obj=currency_id) or line[2]['debit']
  3557. return preview_vals
  3558. def _generate_qr_code(self, silent_errors=False):
  3559. """ Generates and returns a QR-code generation URL for this invoice,
  3560. raising an error message if something is misconfigured.
  3561. The chosen QR generation method is the one set in qr_method field if there is one,
  3562. or the first eligible one found. If this search had to be performed and
  3563. and eligible method was found, qr_method field is set to this method before
  3564. returning the URL. If no eligible QR method could be found, we return None.
  3565. """
  3566. self.ensure_one()
  3567. if not self.display_qr_code:
  3568. return None
  3569. qr_code_method = self.qr_code_method
  3570. if qr_code_method:
  3571. # If the user set a qr code generator manually, we check that we can use it
  3572. if not self.partner_bank_id._eligible_for_qr_code(self.qr_code_method, self.partner_id, self.currency_id):
  3573. raise UserError(_("The chosen QR-code type is not eligible for this invoice."))
  3574. else:
  3575. # Else we find one that's eligible and assign it to the invoice
  3576. for candidate_method, _candidate_name in self.env['res.partner.bank'].get_available_qr_methods_in_sequence():
  3577. if self.partner_bank_id._eligible_for_qr_code(candidate_method, self.partner_id, self.currency_id, raises_error=False):
  3578. qr_code_method = candidate_method
  3579. break
  3580. if not qr_code_method:
  3581. # No eligible method could be found; we can't generate the QR-code
  3582. return None
  3583. unstruct_ref = self.ref if self.ref else self.name
  3584. rslt = self.partner_bank_id.build_qr_code_base64(self.amount_residual, unstruct_ref, self.payment_reference, self.currency_id, self.partner_id, qr_code_method, silent_errors=silent_errors)
  3585. # We only set qr_code_method after generating the url; otherwise, it
  3586. # could be set even in case of a failure in the QR code generation
  3587. # (which would change the field, but not refresh UI, making the displayed data inconsistent with db)
  3588. self.qr_code_method = qr_code_method
  3589. return rslt
  3590. @contextmanager
  3591. def _get_edi_creation(self):
  3592. """Get an environment to import documents from other sources.
  3593. Allow to edit the current move or create a new one.
  3594. This will prevent computing the dynamic lines at each invoice line added and only
  3595. compute everything at the end.
  3596. """
  3597. container = {'records': self}
  3598. with self._check_balanced(container),\
  3599. self._disable_discount_precision(),\
  3600. self._sync_dynamic_lines(container):
  3601. move = self or self.create({})
  3602. yield move
  3603. container['records'] = move
  3604. @contextmanager
  3605. def _disable_discount_precision(self):
  3606. """Disable the user defined precision for discounts.
  3607. This is useful for importing documents coming from other softwares and providers.
  3608. The reasonning is that if the document that we are importing has a discount, it
  3609. shouldn't be rounded to the local settings.
  3610. """
  3611. original_precision_get = DecimalPrecision.precision_get
  3612. def precision_get(self, application):
  3613. if application == 'Discount':
  3614. return 100
  3615. return original_precision_get(self, application)
  3616. with patch('odoo.addons.base.models.decimal_precision.DecimalPrecision.precision_get', new=precision_get):
  3617. yield
  3618. # -------------------------------------------------------------------------
  3619. # TOOLING
  3620. # -------------------------------------------------------------------------
  3621. @api.model
  3622. def _field_will_change(self, record, vals, field_name):
  3623. if field_name not in vals:
  3624. return False
  3625. field = record._fields[field_name]
  3626. if field.type == 'many2one':
  3627. return record[field_name].id != vals[field_name]
  3628. if field.type == 'many2many':
  3629. current_ids = set(record[field_name].ids)
  3630. after_write_ids = set(record.new({field_name: vals[field_name]})[field_name].ids)
  3631. return current_ids != after_write_ids
  3632. if field.type == 'one2many':
  3633. return True
  3634. if field.type == 'monetary' and record[field.get_currency_field(record)]:
  3635. return not record[field.get_currency_field(record)].is_zero(record[field_name] - vals[field_name])
  3636. if field.type == 'float':
  3637. record_value = field.convert_to_cache(record[field_name], record)
  3638. to_write_value = field.convert_to_cache(vals[field_name], record)
  3639. return record_value != to_write_value
  3640. return record[field_name] != vals[field_name]
  3641. @api.model
  3642. def _cleanup_write_orm_values(self, record, vals):
  3643. cleaned_vals = dict(vals)
  3644. for field_name in vals.keys():
  3645. if not self._field_will_change(record, vals, field_name):
  3646. del cleaned_vals[field_name]
  3647. return cleaned_vals
  3648. @contextmanager
  3649. def _disable_recursion(self, container, key, default=None, target=True):
  3650. """Apply the context key to all environments inside this context manager.
  3651. If this context key is already set on the recordsets, yield `True`.
  3652. The recordsets modified are the one in the container, as well as all the
  3653. `self` recordsets of the calling stack.
  3654. This more or less gives the wanted context to all records inside of the
  3655. context manager.
  3656. :param container: A mutable dict that needs to at least contain the key
  3657. `records`. Can contain other items if changing the env
  3658. is needed.
  3659. :param key: The context key to apply to the recordsets.
  3660. :param default: the default value of the context key, if it isn't defined
  3661. yet in the context
  3662. :param target: the value of the context key meaning that we shouldn't
  3663. recurse
  3664. :return: True iff we should just exit the context manager
  3665. """
  3666. disabled = container['records'].env.context.get(key, default) == target
  3667. previous_values = {}
  3668. if not disabled: # it wasn't disabled yet, disable it now
  3669. for env in self.env.transaction.envs:
  3670. previous_values[env] = env.context.get(key, EMPTY)
  3671. env.context = frozendict({**env.context, key: target})
  3672. try:
  3673. yield disabled
  3674. finally:
  3675. for env, val in previous_values.items():
  3676. if val != EMPTY:
  3677. env.context = frozendict({**env.context, key: val})
  3678. else:
  3679. env.context = frozendict({k: v for k, v in env.context.items() if k != key})
  3680. # ------------------------------------------------------------
  3681. # MAIL.THREAD
  3682. # ------------------------------------------------------------
  3683. @api.model
  3684. def message_new(self, msg_dict, custom_values=None):
  3685. # EXTENDS mail mail.thread
  3686. # Add custom behavior when receiving a new invoice through the mail's gateway.
  3687. if (custom_values or {}).get('move_type', 'entry') not in ('out_invoice', 'in_invoice'):
  3688. return super().message_new(msg_dict, custom_values=custom_values)
  3689. company = self.env['res.company'].browse(custom_values['company_id']) if custom_values.get('company_id') else self.env.company
  3690. def is_internal_partner(partner):
  3691. # Helper to know if the partner is an internal one.
  3692. return partner == company.partner_id or (partner.user_ids and all(user._is_internal() for user in partner.user_ids))
  3693. extra_domain = False
  3694. if custom_values.get('company_id'):
  3695. extra_domain = ['|', ('company_id', '=', custom_values['company_id']), ('company_id', '=', False)]
  3696. # Search for partners in copy.
  3697. cc_mail_addresses = email_split(msg_dict.get('cc', ''))
  3698. followers = [partner for partner in self._mail_find_partner_from_emails(cc_mail_addresses, extra_domain) if partner]
  3699. # Search for partner that sent the mail.
  3700. from_mail_addresses = email_split(msg_dict.get('from', ''))
  3701. senders = partners = [partner for partner in self._mail_find_partner_from_emails(from_mail_addresses, extra_domain) if partner]
  3702. # Search for partners using the user.
  3703. if not senders:
  3704. senders = partners = list(self._mail_search_on_user(from_mail_addresses))
  3705. if partners:
  3706. # Check we are not in the case when an internal user forwarded the mail manually.
  3707. if is_internal_partner(partners[0]):
  3708. # Search for partners in the mail's body.
  3709. body_mail_addresses = set(email_re.findall(msg_dict.get('body')))
  3710. partners = [
  3711. partner
  3712. for partner in self._mail_find_partner_from_emails(body_mail_addresses, extra_domain)
  3713. if not is_internal_partner(partner) and partner.company_id.id in (False, company.id)
  3714. ]
  3715. # Little hack: Inject the mail's subject in the body.
  3716. if msg_dict.get('subject') and msg_dict.get('body'):
  3717. msg_dict['body'] = '<div><div><h3>%s</h3></div>%s</div>' % (msg_dict['subject'], msg_dict['body'])
  3718. # Create the invoice.
  3719. values = {
  3720. 'name': '/', # we have to give the name otherwise it will be set to the mail's subject
  3721. 'invoice_source_email': from_mail_addresses[0],
  3722. 'partner_id': partners and partners[0].id or False,
  3723. }
  3724. move_ctx = self.with_context(default_move_type=custom_values['move_type'], default_journal_id=custom_values['journal_id'])
  3725. move = super(AccountMove, move_ctx).message_new(msg_dict, custom_values=values)
  3726. move._compute_name() # because the name is given, we need to recompute in case it is the first invoice of the journal
  3727. # Assign followers.
  3728. all_followers_ids = set(partner.id for partner in followers + senders + partners if is_internal_partner(partner))
  3729. move.message_subscribe(list(all_followers_ids))
  3730. return move
  3731. def _message_post_after_hook(self, new_message, message_values):
  3732. # EXTENDS mail mail.thread
  3733. # When posting a message, check the attachment to see if it's an invoice and update with the imported data.
  3734. res = super()._message_post_after_hook(new_message, message_values)
  3735. attachments = new_message.attachment_ids
  3736. if len(self) != 1 or not attachments or self.env.context.get('no_new_invoice') or not self.is_invoice(include_receipts=True):
  3737. return res
  3738. odoobot = self.env.ref('base.partner_root')
  3739. if attachments and self.state != 'draft':
  3740. self.message_post(body=_('The invoice is not a draft, it was not updated from the attachment.'),
  3741. message_type='comment',
  3742. subtype_xmlid='mail.mt_note',
  3743. author_id=odoobot.id)
  3744. return res
  3745. if attachments and self.invoice_line_ids:
  3746. self.message_post(body=_('The invoice already contains lines, it was not updated from the attachment.'),
  3747. message_type='comment',
  3748. subtype_xmlid='mail.mt_note',
  3749. author_id=odoobot.id)
  3750. return res
  3751. decoders = self.env['account.move']._get_update_invoice_from_attachment_decoders(self)
  3752. with self._disable_discount_precision():
  3753. for decoder in sorted(decoders, key=lambda d: d[0]):
  3754. # start with message_main_attachment_id, that way if OCR is installed, only that one will be parsed.
  3755. # this is based on the fact that the ocr will be the last decoder.
  3756. for attachment in attachments.sorted(lambda x: x != self.message_main_attachment_id):
  3757. invoice = decoder[1](attachment, self)
  3758. if invoice:
  3759. return res
  3760. return res
  3761. def _creation_subtype(self):
  3762. # EXTENDS mail mail.thread
  3763. if self.move_type in ('out_invoice', 'out_receipt'):
  3764. return self.env.ref('account.mt_invoice_created')
  3765. else:
  3766. return super()._creation_subtype()
  3767. def _track_subtype(self, init_values):
  3768. # EXTENDS mail mail.thread
  3769. # add custom subtype depending of the state.
  3770. self.ensure_one()
  3771. if not self.is_invoice(include_receipts=True):
  3772. if self.payment_id and 'state' in init_values:
  3773. self.payment_id._message_track(['state'], {self.payment_id.id: init_values})
  3774. return super()._track_subtype(init_values)
  3775. if 'payment_state' in init_values and self.payment_state == 'paid':
  3776. return self.env.ref('account.mt_invoice_paid')
  3777. elif 'state' in init_values and self.state == 'posted' and self.is_sale_document(include_receipts=True):
  3778. return self.env.ref('account.mt_invoice_validated')
  3779. return super()._track_subtype(init_values)
  3780. def _creation_message(self):
  3781. # EXTENDS mail mail.thread
  3782. if not self.is_invoice(include_receipts=True):
  3783. return super()._creation_message()
  3784. return {
  3785. 'out_invoice': _('Invoice Created'),
  3786. 'out_refund': _('Credit Note Created'),
  3787. 'in_invoice': _('Vendor Bill Created'),
  3788. 'in_refund': _('Refund Created'),
  3789. 'out_receipt': _('Sales Receipt Created'),
  3790. 'in_receipt': _('Purchase Receipt Created'),
  3791. }[self.move_type]
  3792. def _notify_by_email_prepare_rendering_context(self, message, msg_vals, model_description=False,
  3793. force_email_company=False, force_email_lang=False):
  3794. # EXTENDS mail mail.thread
  3795. render_context = super()._notify_by_email_prepare_rendering_context(
  3796. message, msg_vals, model_description=model_description,
  3797. force_email_company=force_email_company, force_email_lang=force_email_lang
  3798. )
  3799. subtitles = [render_context['record'].name]
  3800. if self.invoice_date_due and self.payment_state not in ('in_payment', 'paid'):
  3801. subtitles.append(_('%(amount)s due\N{NO-BREAK SPACE}%(date)s',
  3802. amount=format_amount(self.env, self.amount_total, self.currency_id, lang_code=render_context.get('lang')),
  3803. date=format_date(self.env, self.invoice_date_due, date_format='short', lang_code=render_context.get('lang'))
  3804. ))
  3805. else:
  3806. subtitles.append(format_amount(self.env, self.amount_total, self.currency_id, lang_code=render_context.get('lang')))
  3807. render_context['subtitles'] = subtitles
  3808. return render_context
  3809. # -------------------------------------------------------------------------
  3810. # TOOLING
  3811. # -------------------------------------------------------------------------
  3812. def _conditional_add_to_compute(self, fname, condition):
  3813. field = self._fields[fname]
  3814. to_reset = self.filtered(lambda move:
  3815. condition(move)
  3816. and not self.env.is_protected(field, move._origin)
  3817. and (move._origin or not move[fname])
  3818. )
  3819. to_reset.invalidate_recordset([fname])
  3820. self.env.add_to_compute(field, to_reset)
  3821. # -------------------------------------------------------------------------
  3822. # HOOKS
  3823. # -------------------------------------------------------------------------
  3824. def _action_invoice_ready_to_be_sent(self):
  3825. """ Hook allowing custom code when an invoice becomes ready to be sent by mail to the customer.
  3826. For example, when an EDI document must be sent to the government and be signed by it.
  3827. """
  3828. def _is_ready_to_be_sent(self):
  3829. """ Helper telling if a journal entry is ready to be sent by mail to the customer.
  3830. :return: True if the invoice is ready, False otherwise.
  3831. """
  3832. self.ensure_one()
  3833. return True
  3834. @contextmanager
  3835. def _send_only_when_ready(self):
  3836. moves_not_ready = self.filtered(lambda x: not x._is_ready_to_be_sent())
  3837. try:
  3838. yield
  3839. finally:
  3840. moves_now_ready = moves_not_ready.filtered(lambda x: x._is_ready_to_be_sent())
  3841. if moves_now_ready:
  3842. moves_now_ready._action_invoice_ready_to_be_sent()
  3843. def _invoice_paid_hook(self):
  3844. ''' Hook to be overrided called when the invoice moves to the paid state. '''
  3845. def _get_lines_onchange_currency(self):
  3846. # Override needed for COGS
  3847. return self.line_ids
  3848. @api.model
  3849. def _get_invoice_in_payment_state(self):
  3850. ''' Hook to give the state when the invoice becomes fully paid. This is necessary because the users working
  3851. with only invoicing don't want to see the 'in_payment' state. Then, this method will be overridden in the
  3852. accountant module to enable the 'in_payment' state. '''
  3853. return 'paid'
  3854. def _get_name_invoice_report(self):
  3855. """ This method need to be inherit by the localizations if they want to print a custom invoice report instead of
  3856. the default one. For example please review the l10n_ar module """
  3857. self.ensure_one()
  3858. return 'account.report_invoice_document'
  3859. def _get_create_document_from_attachment_decoders(self):
  3860. """ Returns a list of method that are able to create an invoice from an attachment and a priority.
  3861. :returns: A list of tuples (priority, method) where method takes an attachment as parameter.
  3862. """
  3863. return []
  3864. def _get_update_invoice_from_attachment_decoders(self, invoice):
  3865. """ Returns a list of method that are able to create an invoice from an attachment and a priority.
  3866. :param invoice: The invoice on which to update the data.
  3867. :returns: A list of tuples (priority, method) where method takes an attachment as parameter.
  3868. """
  3869. return []
  3870. def _is_downpayment(self):
  3871. ''' Return true if the invoice is a downpayment.
  3872. Down-payments can be created from a sale order. This method is overridden in the sale order module.
  3873. '''
  3874. return False
  3875. @api.model
  3876. def get_invoice_localisation_fields_required_to_invoice(self, country_id):
  3877. """ Returns the list of fields that needs to be filled when creating an invoice for the selected country.
  3878. This is required for some flows that would allow a user to request an invoice from the portal.
  3879. Using these, we can get their information and dynamically create form inputs based for the fields required legally for the company country_id.
  3880. The returned fields must be of type ir.model.fields in order to handle translations
  3881. :param country_id: The country for which we want the fields.
  3882. :return: an array of ir.model.fields for which the user should provide values.
  3883. """
  3884. return []