BrightSide Workbench Full Report + Source Code
Document.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.financials.entity;
19 
20 import java.text.SimpleDateFormat;
21 import java.util.*;
22 import javax.persistence.*;
23 import org.amic.util.date.CheckDate;
24 import org.turro.string.Strings;
25 import org.turro.contacts.Contact;
26 import org.turro.contacts.util.ContactList;
27 import org.turro.elephant.db.IdUtils;
28 import org.turro.entities.Entities;
29 import org.turro.entities.IElephantEntity;
30 import org.turro.financials.db.FinancialsPU;
31 import org.turro.financials.model.RelatedToLineTypeAdapter;
32 import org.turro.financials.model.contract.ContractWrapper;
33 import org.turro.financials.model.document.AmountTaxable;
34 import org.turro.financials.model.document.DocumentAmounts;
35 import org.turro.financials.model.document.DocumentAmountsByBook;
36 import org.turro.financials.model.document.DocumentDefinitionWrapper;
37 import org.turro.financials.model.document.DocumentLineComparator;
38 import org.turro.financials.model.document.LineTypeSet;
39 import org.turro.html.HtmlContent;
40 import org.turro.jpa.Dao;
41 import org.turro.jpa.entity.IDaoEntity;
42 import org.turro.mail.message.MailUtils;
43 import org.turro.plugin.scheduler.ScheduledAction;
44 import org.turro.reflection.MappingSet;
45 import org.turro.util.Chars;
46 import org.turro.util.PhraseBuilder;
47 
52 @Entity
53 @Table(indexes={
54  @Index(name="IndexDocument", columnList="documentNumber,documentDate")
55 })
56 public class Document implements java.io.Serializable, IDaoEntity, ScheduledAction {
57 
58  @Id
59  @GeneratedValue(strategy=GenerationType.IDENTITY)
60  @Column(name="IDENTIFIER")
61  private long id;
62 
63  @ManyToOne(fetch=FetchType.LAZY)
64  private DocumentDefinition documentDefinition;
65 
66  @ManyToOne
67  private Contract contract;
68 
69  @Lob
70  @Column(length=4096)
71  private String notes;
72 
73  @Temporal(value = TemporalType.DATE)
74  private Date receiptDate;
75 
76  @Column(nullable=false)
77  @Temporal(value = TemporalType.DATE)
78  private Date documentDate;
79 
80  @Column(nullable=false)
81  private String documentNumber;
82 
83  private Currency currency;
84 
85  private double quote;
86 
87  private boolean draft, noAncestors = false, noDescendants = false;
88 
89  private String relatedPath;
90 
91  @ManyToOne
92  private RegisterView forcedView;
93 
94  @OneToMany(mappedBy = "descendant", fetch = FetchType.EAGER)
95  private Set<DocumentRelation> ancestors = new HashSet<>();
96 
97  @OneToMany(mappedBy = "ancestor", fetch = FetchType.EAGER)
98  private Set<DocumentRelation> descendants = new HashSet<>();
99 
100  @OneToMany(mappedBy = "document", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
101  private Set<Register> registers = new HashSet<>();
102 
103  @OneToMany(mappedBy = "document", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
104  @OrderBy(value="lineOrder")
105  private Set<DocumentLine> documentLines = new TreeSet<>(new DocumentLineComparator());
106 
107  public Set<DocumentRelation> getAncestors() {
108  Dao dao = new FinancialsPU();
109  if(id > 0 && dao.isNotLoaded(ancestors)) {
110  ancestors = dao.lazyLoader(Document.class, this, "ancestors").ancestors;
111  }
112  return ancestors;
113  }
114 
115  public void setAncestors(Set<DocumentRelation> ancestors) {
116  this.ancestors = ancestors;
117  }
118 
120  return contract;
121  }
122 
123  public void setContract(Contract contract) {
124  this.contract = contract;
125  _stores = null;
126  }
127 
128  public Currency getCurrency() {
129  if(currency == null && contract != null) {
130  currency = contract.getCurrency();
131  }
132  return currency;
133  }
134 
135  public void setCurrency(Currency currency) {
136  this.currency = currency;
137  }
138 
139  public Set<DocumentRelation> getDescendants() {
140  Dao dao = new FinancialsPU();
141  if(id > 0 && dao.isNotLoaded(descendants)) {
142  descendants = dao.lazyLoader(Document.class, this, "descendants").descendants;
143  }
144  return descendants;
145  }
146 
147  public Set<DocumentRelation> getUpdatedDescendants() {
148  Dao dao = new FinancialsPU();
149  if(id > 0) {
150  descendants = dao.lazyLoader(Document.class, this, "descendants").descendants;
151  }
152  return descendants;
153  }
154 
155  public void setDescendants(Set<DocumentRelation> descendants) {
156  this.descendants = descendants;
157  }
158 
159  public Date getDocumentDate() {
160  return documentDate;
161  }
162 
163  public void setDocumentDate(Date documentDate) {
164  this.documentDate = documentDate;
165  }
166 
168  Dao dao = new FinancialsPU();
169  if(id > 0 && dao.isNotLoaded(documentDefinition)) {
170  setDocumentDefinition(dao.lazyLoader(Document.class, this, "documentDefinition").documentDefinition);
171  }
172  return documentDefinition;
173  }
174 
175  public void setDocumentDefinition(DocumentDefinition documentDefinition) {
176  this.documentDefinition = documentDefinition;
177  _stores = null;
178  }
179 
180  public Set<DocumentLine> getDocumentLines() {
181  return documentLines;
182  }
183 
184  public void setDocumentLines(Set<DocumentLine> documentLines) {
185  this.documentLines = documentLines;
186  }
187 
188  public String getDocumentNumber() {
189  return documentNumber;
190  }
191 
192  public void setDocumentNumber(String documentNumber) {
193  this.documentNumber = documentNumber;
194  }
195 
196  public boolean isDraft() {
197  return draft;
198  }
199 
200  public void setDraft(boolean draft) {
201  this.draft = draft;
202  }
203 
205  return forcedView;
206  }
207 
208  public void setForcedView(RegisterView forcedView) {
209  this.forcedView = forcedView;
210  }
211 
212  public long getId() {
213  return id;
214  }
215 
216  public void setId(long id) {
217  this.id = id;
218  }
219 
220  public boolean isNoAncestors() {
221  return noAncestors;
222  }
223 
224  public void setNoAncestors(boolean noAncestors) {
225  this.noAncestors = noAncestors;
226  }
227 
228  public boolean isNoDescendants() {
229  return noDescendants;
230  }
231 
232  public void setNoDescendants(boolean noDescendants) {
233  this.noDescendants = noDescendants;
234  }
235 
236  public String getNotes() {
237  return notes;
238  }
239 
240  public void setNotes(String notes) {
241  this.notes = notes;
242  }
243 
244  public double getQuote() {
245  return quote;
246  }
247 
248  public void setQuote(double quote) {
249  this.quote = quote;
250  }
251 
252  public Date getReceiptDate() {
253  return receiptDate;
254  }
255 
256  public void setReceiptDate(Date receiptDate) {
257  this.receiptDate = receiptDate;
258  }
259 
260  public Set<Register> getRegisters() {
261  return registers;
262  }
263 
264  public void setRegisters(Set<Register> registers) {
265  this.registers = registers;
266  }
267 
268  public String getRelatedPath() {
269  return relatedPath;
270  }
271 
272  public void setRelatedPath(String relatedPath) {
273  this.relatedPath = relatedPath;
274  }
275 
276  /* IDaoEntity */
277 
278  @Override
279  public Object entityId() {
280  return id;
281  }
282 
283  @Override
284  public boolean isEmpty() {
285  return documentLines.isEmpty() ||
286  isBlank() ||
287  !isValid() ||
288  documentDate == null ||
289  receiptDate == null;
290  }
291 
292  @Override
293  public void prepareSave() {
294  IDaoEntity.super.prepareSave();
295  if(Strings.isBlank(documentNumber)) {
296  // Ensure not null value
297  if(draft) {
298  documentNumber = "";
299  } else {
300  if(forcedView == null) {
301  documentNumber = IdUtils.getMaxLongIdFromString(
302  new FinancialsPU(), "Document", "documentNumber",
303  new String[] {
304  "year(receiptDate) = " + new CheckDate(receiptDate).getYear(),
305  "documentDefinition_identifier = " + getDocumentDefinition().getId(),
306  "forcedView_identifier is null"
307  }) + "";
308  } else {
309  documentNumber = IdUtils.getMaxLongIdFromString(
310  new FinancialsPU(), "Document", "documentNumber",
311  new String[] {
312  "year(receiptDate) = " + new CheckDate(receiptDate).getYear(),
313  "documentDefinition_identifier = " + getDocumentDefinition().getId(),
314  "forcedView_identifier = " + forcedView.getId()
315  }) + "";
316  }
317  }
318  }
319  }
320 
321  @Override
322  public Collection<Collection> collections() {
323  return List.of(getRegisters());
324  }
325 
326  /* Helpers */
327 
329  return Entities.getController(relatedPath);
330  }
331 
332  public boolean isValid() {
333  return !(contract == null || getDocumentDefinition() == null);
334  }
335 
336  public boolean isBlank() {
337  Iterator<DocumentLine> itl = getDocumentLines().iterator();
338  while(itl.hasNext()) {
339  if(!itl.next().isEmpty()) {
340  return false;
341  }
342  }
343  return true;
344  }
345 
346  public String getConcept() {
347  PhraseBuilder pb = new PhraseBuilder();
348  for(DocumentLine dl : documentLines) {
349  pb.addWord(dl.getConcept());
350  }
351  return pb.toString();
352  }
353 
354  public String getCleanFullNotes() {
355  return HtmlContent.noTags(MailUtils.cleanLinks(getFullNotes()));
356  }
357 
358  public String getFullNotes() {
359  PhraseBuilder pb = new PhraseBuilder();
360  pb.addWord(notes);
361 // pb.addPendingSeparator(Chars.nl().repeat(2).toString());
362 // pb.addWord(getContract().getNotes());
363  pb.addPendingSeparator(Chars.nl().repeat(2).toString());
364  ContractDefinitionNotes cdNotes = getContract().getContractDefinition().getNotes();
365  if(cdNotes != null) {
366  pb.addWord(cdNotes.getNotes());
367  }
368  return pb.toString();
369  }
370 
371  public void clearEmpties() {
372  Iterator<DocumentLine> itl = getDocumentLines().iterator();
373  while(itl.hasNext()) {
374  if(itl.next().isEmpty()) {
375  itl.remove();
376  }
377  }
378  }
379 
380  public void assignTableOrder(Collection<DocumentLine> lines) {
381  Iterator<DocumentLine> it = lines.iterator();
382  int c = 0;
383  while(it.hasNext()) {
384  it.next().setLineOrder(c++);
385  }
386  }
387 
389  return new DocumentAmounts(this);
390  }
391 
393  return new DocumentAmountsByBook(this, bookDefinition);
394  }
395 
396 // public AmountSet getAmountSet() {
397 // AmountSet as = new AmountSet();
398 // if(getCurrency() != null) {
399 // int fractionDigits = getCurrency().getDefaultFractionDigits();
400 // if(contract != null) {
401 // int count = 0;
402 // for(DocumentLine dl : documentLines) {
403 // dl.setLineOrder(count++);
404 // double tax = dl.getTax();
405 // if(!getContract().getOperatingModifier().isSumTax(dl)) {
406 // tax = 0.0;
407 // }
408 // Amount a = as.getAmount(tax);
409 // if(a == null) {
410 // as.add(new Amount(tax, dl.getSubtotal(), dl.getDiscount(), dl.getRetained(), fractionDigits));
411 // } else {
412 // a.addTax(tax, dl.getSubtotal(), dl.getDiscount(), dl.getRetained());
413 // }
414 // }
415 // as.clearEmpty();
416 // }
417 // }
418 // return as;
419 // }
420 
421  public double getSubTotal() {
422  return getAmounts().getAmount();
423  }
424 
425  public double getTotalAmount() {
426  return getAmounts().getTotal();
427  }
428 
430  if(getContract() != null) {
431  if(getContract().getContractPreferences().isEmpty()) {
432  return getContract().getContractDefinition().getDefaultContractPreference();
433  } else {
434  return getContract().getContractPreferences().iterator().next();
435  }
436  }
437  return null;
438  }
439 
440  public void appendLines(Document relDoc, String header, DocumentWorkflow workflow) {
441  int count = getMaxLineOrder() + 1;
442  if(header != null) {
443  Contract defaultStore = getDefaultStore();
444  DocumentLine dl = new DocumentLine();
445  dl.setDocument(this);
446  dl.setConcept(header);
447  dl.setStore(defaultStore);
448  dl.setLineOrder(count++);
449  documentLines.add(dl);
450  }
451  for(DocumentLine l : relDoc.getDocumentLines()) {
452  DocumentLine dl = new DocumentLine();
453  dl.setDocument(this);
454  dl.setProduct(l.getProduct());
455  dl.setProductByContractor(l.getProductByContractor());
456  dl.setConcept(l.getConcept());
457  dl.setQuantity(l.getQuantity());
458  dl.setPrice(l.getPrice());
459  dl.setDiscountPerCent(l.getDiscountPerCent());
460  dl.setDiscountMoney(l.getDiscountMoney());
461  dl.setTax(l.getTax());
462  dl.setEquivalenceSurcharge(l.getEquivalenceSurcharge());
463  dl.setRetention(l.getRetention());
464  dl.setStore(l.getStore());
465  dl.setLineOrder(count++);
466  if(l.getLineType() != null) {
467  double sc = l.getLineType().getStockCoefficient();
468  dl.setLineType(workflow == null ? null : workflow.getLineType());
469  if(dl.getLineType() == null) {
470  for(RelatedLineType rlt : getDocumentDefinition().getRelatedLineTypes()) {
471  if((sc == 0 && rlt.getLineType().getStockCoefficient() != 0) ||
472  (sc != 0 && rlt.getLineType().getStockCoefficient() == 0)) {
473  dl.setLineType(rlt.getLineType());
474  break;
475  }
476  }
477  }
478  if(dl.getLineType() == null) {
479  dl.setLineType(getDefaultLineType());
480  }
481  } else {
482  dl.setLineType(null);
483  }
484  if(workflow == null) {
485  dl.setContractPreference(getDefaultContractPreference());
486  } else {
487  dl.setContractPreference(l.getContractPreference());
488  }
489  documentLines.add(dl);
490  }
491  }
492 
493  public void appendAmounts(Document relDoc, String header, DocumentWorkflow workflow) {
494  int count = getMaxLineOrder() + 1;
495  LineType lt = workflow == null ? null : workflow.getLineType();
496  if(lt == null) {
497  for(RelatedLineType rlt : getDocumentDefinition().getRelatedLineTypes()) {
498  if(rlt.getLineType().getStockCoefficient() == 0) {
499  lt = rlt.getLineType();
500  break;
501  }
502  }
503  }
504  if(lt == null) {
505  lt = getDefaultLineType();
506  }
507  Contract defaultStore = getDefaultStore();
508  if(getDocumentDefinition().hasColumn(DocumentColumn.DCOL_TAX)) {
509  for(AmountTaxable l : relDoc.getAmounts().getTaxables()) {
510  DocumentLine dl = new DocumentLine();
511  dl.setDocument(this);
512  if(header != null) {
513  dl.setConcept(header + " [" + l.getTax() + "]");
514  }
515  dl.setPrice(l.getAmount());
516  dl.setTax(l.getTax());
517  dl.setEquivalenceSurcharge(l.getReq());
518  dl.setLineType(lt);
519  dl.setContractPreference(getDefaultContractPreference());
520  dl.setStore(defaultStore);
521  dl.setLineOrder(count++);
522  documentLines.add(dl);
523  }
524  } else {
525  DocumentLine dl = new DocumentLine();
526  dl.setDocument(this);
527  if(header != null) {
528  dl.setConcept(header);
529  }
530  dl.setPrice(relDoc.getTotalAmount());
531  dl.setLineType(lt);
532  dl.setContractPreference(getDefaultContractPreference());
533  dl.setStore(defaultStore);
534  dl.setLineOrder(count++);
535  documentLines.add(dl);
536  }
537  }
538 
539  public void flowFrom(Document doc, String header, DocumentWorkflow workflow) {
540  Date now = new Date();
541  setContract(doc.getContract());
542  setCurrency(doc.getCurrency());
543  setDocumentDate(now);
544  setDocumentNumber(null);
545  setReceiptDate(now);
546  setDocumentDefinition(workflow.getDescendant());
547  setDraft(doc.isDraft());
548  setNotes(doc.getNotes());
549  if(workflow.getAncestor().hasDetail() && workflow.getDescendant().hasDetail()) {
550  appendLines(doc, header, workflow);
551  } else {
552  appendAmounts(doc, header, workflow);
553  }
555  dr.setAncestor(doc);
556  dr.setDescendant(this);
557  getAncestors().add(dr);
558  }
559 
560  public void backFrom(Document doc, String header, DocumentWorkflow workflow) {
561  Date now = new Date();
562  setContract(doc.getContract());
563  setCurrency(doc.getCurrency());
564  setDocumentDate(now);
565  setDocumentNumber(null);
566  setReceiptDate(now);
567  setDocumentDefinition(workflow.getAncestor());
568  setDraft(doc.isDraft());
569  setNotes(doc.getNotes());
570  if(workflow.getAncestor().hasDetail() && workflow.getDescendant().hasDetail()) {
571  appendLines(doc, header, null);
572  } else {
573  appendAmounts(doc, header, null);
574  }
576  dr.setAncestor(this);
577  dr.setDescendant(doc);
578  getDescendants().add(dr);
579  }
580 
581  public void copyFrom(Document doc, boolean shallowCopy) {
582  if(!shallowCopy) {
583  setContract(doc.getContract());
584  }
585  setCurrency(doc.getCurrency());
586  setDocumentDate(doc.getDocumentDate());
587  setDocumentNumber(doc.getDocumentNumber() + "*");
588  setReceiptDate(doc.getReceiptDate());
589  setDocumentDefinition(doc.getDocumentDefinition());
590  setDraft(true);
591  setNotes(doc.getNotes());
592  setForcedView(doc.getForcedView());
593  Contract defaultStore = (!shallowCopy) ? null : getDefaultStore();
594  for(DocumentLine dl : doc.getDocumentLines()) {
595  DocumentLine nl = new DocumentLine();
596  nl.setConcept(dl.getConcept());
597  nl.setDiscountMoney(dl.getDiscountMoney());
598  nl.setDiscountPerCent(dl.getDiscountPerCent());
599  nl.setDocument(this);
600  nl.setLineOrder(dl.getLineOrder());
601  nl.setLineType(dl.getLineType());
602  nl.setContractPreference(dl.getContractPreference());
603  nl.setPrice(dl.getPrice());
604  nl.setProduct(dl.getProduct());
605  nl.setProductByContractor(dl.getProductByContractor());
606  nl.setQuantity(dl.getQuantity());
607  nl.setStore(shallowCopy ? defaultStore : dl.getStore());
608  nl.setTax(dl.getTax());
609  nl.setEquivalenceSurcharge(dl.getEquivalenceSurcharge());
610  nl.setRetention(dl.getRetention());
611  documentLines.add(nl);
612  }
613  }
614 
615  public Collection<ContractFlow> getContractFlows() {
616  return contract == null ? null : contract.getFlowFor(this);
617  }
618 
619  public Collection<DocumentWorkflow> getWorkflows() {
620  return getWorkflows(true, true);
621  }
622 
623  public Collection<DocumentWorkflow> getWorkflows(boolean forward, boolean missing) {
624  if(forward && (!missing || getDescendants().isEmpty())) {
625  return new FinancialsPU().getResultList(
626  "select dw from DocumentWorkflow as dw " +
627  "where dw.ancestor = ? " +
628  "and dw.contractDefinition = ?",
629  new Object[] {
630  getDocumentDefinition(),
631  contract.getContractDefinition()
632  });
633  } else if(!forward && (!missing || getAncestors().isEmpty())) {
634  return new FinancialsPU().getResultList(
635  "select dw from DocumentWorkflow as dw " +
636  "where dw.descendant = ? " +
637  "and dw.contractDefinition = ?",
638  new Object[] {
639  getDocumentDefinition(),
640  contract.getContractDefinition()
641  });
642  }
643  return Collections.EMPTY_LIST;
644  }
645 
646  public Collection<Contact> getActualHqContacts() {
647  ContactList l = new ContactList();
648  for(DocumentLine line : getDocumentLines()) {
649  if(!line.isEmpty()) {
650  Contract ctc = line.getStore();
651  if(ctc != null && ctc.getDepartment() != null && ctc.getDepartment().getHeadquarters() != null) {
653  }
654  }
655  }
656  return l;
657  }
658 
659  public String getDocDescription() {
660  return getDocumentDefinition().getName() + ": " +
661  (getForcedView() == null ? "" : "(" + getForcedView().getName() + ") ") +
662  getDocumentNumber() + " " +
663  SimpleDateFormat.getDateInstance().format(documentDate);
664  }
665 
666  public String getDocumentString() {
667  return getDocumentDefinition().getName() + " " + documentNumber + " " +
668  SimpleDateFormat.getDateInstance().format(documentDate);
669  }
670 
671  /* Stores */
672 
673  @Transient
674  private transient Collection<Contract> _stores;
675 
676  public Collection<Contract> getStores() {
677  if(_stores == null) {
678  _stores = new DocumentDefinitionWrapper(getDocumentDefinition())
679  .getRelatedStores(getContract());
680  if(_stores.isEmpty()) {
681  _stores = new DocumentDefinitionWrapper(getDocumentDefinition())
682  .getRelatedStores();
683  }
685  }
686  return _stores;
687  }
688 
690  Collection<Contract> stores = getStores();
691  if(!stores.isEmpty()) {
692  return stores.iterator().next();
693  }
694  return null;
695  }
696 
697  public int getMaxLineOrder() {
698  int result = 0;
699  for(DocumentLine dl : getDocumentLines()) {
700  if(dl.getLineOrder() > result) {
701  result = dl.getLineOrder();
702  }
703  }
704  return result;
705  }
706 
707  /* Usual */
708  public String getUsualPath() {
709  if(contract != null && getDocumentDefinition() != null) {
710  return contract.getUsualPath(getDocumentDefinition());
711  }
712  return "";
713  }
714 
715  /* Line types */
716 
718  LineType lt = null;
719  if(getDocumentDefinition() != null) {
720  for(RelatedLineType rlt : getDocumentDefinition().getRelatedLineTypes()) {
721  if(rlt.isDefaultLineType()) {
722  for(LineType l : getLineTypes()) {
723  if(rlt.getLineType().getId() == l.getId()) {
724  return l;
725  }
726  if(lt == null) lt = l;
727  }
728  }
729  }
730  }
731  return lt;
732  }
733 
734  public Collection<LineType> getLineTypes() {
735  LineTypeSet result = new LineTypeSet();
736  if(getDocumentDefinition() != null && contract != null) {
737  LineTypeSet lts = new RelatedToLineTypeAdapter(getDocumentDefinition().getRelatedLineTypes());
738  for(LineType lt : lts) {
739  if(lt.getName().contains("%n")) {
740  if(contract.getContractPreferences().isEmpty()) {
741  LineType ltt = lt.clone();
742  ltt.setContractPreference(getDefaultContractPreference());
743  result.add(ltt);
744  } else {
745  for(ContractPreference cp : contract.getContractPreferences()) {
746  LineType ltt = lt.clone();
747  ltt.setContractPreference(cp);
748  result.add(ltt);
749  }
750  }
751  } else {
752  LineType ltt = lt.clone();
753  ltt.setContractPreference(getDefaultContractPreference());
754  result.add(ltt);
755  }
756  }
757  }
758  return result;
759  }
760 
761  /* ScheduledAction */
762 
763  @Override
764  public Object doAction(Date now, String data) {
765  if(data.contains("action:clone")) {
766  Document nd = new Document();
767  nd.copyFrom(this, false);
768  nd.setDocumentDate(now);
769  nd.setReceiptDate(now);
771  nd.setDocumentNumber(null);
772  }
773  nd.setDraft(data.contains("clone:draft"));
774  nd.prepareSave();
775  return new FinancialsPU().saveObject(nd);
776  }
777  return null;
778  }
779 
780  /* Books */
781 
782  public long getBookNumber() {
783  if(!getRegisters().isEmpty()) {
784  Register register = getRegisters().iterator().next();
785  if(!register.getBookRegisters().isEmpty()) {
786  return register.getBookRegisters().iterator().next().getBookOrder();
787  }
788  }
789  return 0L;
790  }
791 
792  /* Transient data */
793 
794  @Transient
795  public transient boolean conciliateRegister;
796 
797  public boolean isConciliateRegister() {
798  return conciliateRegister;
799  }
800 
801  public void setConciliateRegister(boolean conciliateRegister) {
802  this.conciliateRegister = conciliateRegister;
803  }
804 
805  @Transient
806  public transient boolean regularizeVAT;
807 
808  public boolean isRegularizeVAT() {
809  return regularizeVAT;
810  }
811 
812  public void setRegularizeVAT(boolean regularizeVAT) {
813  this.regularizeVAT = regularizeVAT;
814  }
815 
816  @Transient
817  public transient boolean regularizeIRPF;
818 
819  public boolean isRegularizeIRPF() {
820  return regularizeIRPF;
821  }
822 
823  public void setRegularizeIRPF(boolean regularizeIRPF) {
824  this.regularizeIRPF = regularizeIRPF;
825  }
826 
827  /* XML Serializer */
828 
829  public MappingSet getSerializerMappings() {
830  MappingSet set = new MappingSet();
831  set.addMapping(Document.class, 1,
832  new String[] { "id", "documentNumber", "documentDate" },
833  new String[] { "documentDefinition", "notes", "receiptDate", "draft", "documentLines",
834  "currency", "contract", "forcedView", "relatedPath", "ancestors", "descendants", "registers",
835  "conciliateRegister", "regularizeVAT", "regularizeIRPF", "noAncestors", "noDescendants" });
836  set.addMapping(DocumentDefinition.class, 2,
837  new String[] { "id", "name" },
838  null);
839  set.addMapping(Contract.class, 2,
840  new String[] { "id", "name" },
841  null);
842  set.addMapping(RegisterView.class, 2,
843  new String[] { "id", "name" },
844  null);
845  set.addMapping(DocumentRelation.class, 2,
846  null,
847  new String[] { "ancestor", "descendant" });
848  set.addMapping(Document.class, 3,
849  new String[] { "id" },
850  null);
851  set.addMapping(DocumentLine.class, 2,
852  new String[] { "quantity", "price", "tax", "retention", "discountPerCent", "discountMoney" },
853  new String[] { "concept", "product", "productByContractor", "store",
854  "lineType", "contractPreference"});
855  set.addMapping(Product.class, 3,
856  new String[] { "id", "productCode" },
857  new String[] { "description" });
858  set.addMapping(ProductByContractor.class, 3,
859  new String[] { "contractorCode" },
860  new String[] { "contract" });
861  set.addMapping(Contract.class, 4,
862  new String[] { "id", "name" },
863  null);
864  set.addMapping(Contract.class, 3,
865  new String[] { "id", "name" },
866  null);
867  set.addMapping(LineType.class, 3,
868  new String[] { "name" },
869  null);
870  set.addMapping(ContractPreference.class, 3,
871  new String[] { "name" },
872  null);
873 // set.addMapping(Register.class, 2,
874 // new String[] { "idRegister", "registerDate" },
875 // new String[] { "view", "registerEntries" });
876 // set.addMapping(RegisterView.class, 3,
877 // new String[] { "id", "name" },
878 // null);
879 // set.addMapping(RegisterEntry.class, 3,
880 // new String[] { "debit", "credit", "approved", "conciliated" },
881 // new String[] { "account" } );
882 // set.addMapping(Account.class, 4,
883 // new String[] { "id", "description" },
884 // null);
885  return set;
886  }
887 
888 }
static long getMaxLongIdFromString(Dao dao, String table, String field)
Definition: IdUtils.java:115
static IElephantEntity getController(String path)
Definition: Entities.java:78
void setEquivalenceSurcharge(double equivalenceSurcharge)
void setContractPreference(ContractPreference contractPreference)
void setDiscountMoney(double discountMoney)
void setDiscountPerCent(double discountPerCent)
void setProductByContractor(ProductByContractor productByContractor)
void setForcedView(RegisterView forcedView)
Definition: Document.java:208
void setDocumentLines(Set< DocumentLine > documentLines)
Definition: Document.java:184
void setDocumentNumber(String documentNumber)
Definition: Document.java:192
Collection< Contract > getStores()
Definition: Document.java:676
void setDocumentDate(Date documentDate)
Definition: Document.java:163
void assignTableOrder(Collection< DocumentLine > lines)
Definition: Document.java:380
Collection< Collection > collections()
Definition: Document.java:322
void setRegularizeIRPF(boolean regularizeIRPF)
Definition: Document.java:823
Set< DocumentRelation > getDescendants()
Definition: Document.java:139
void copyFrom(Document doc, boolean shallowCopy)
Definition: Document.java:581
Object doAction(Date now, String data)
Definition: Document.java:764
void setContract(Contract contract)
Definition: Document.java:123
void setRegularizeVAT(boolean regularizeVAT)
Definition: Document.java:812
void setRegisters(Set< Register > registers)
Definition: Document.java:264
void setNoDescendants(boolean noDescendants)
Definition: Document.java:232
void appendLines(Document relDoc, String header, DocumentWorkflow workflow)
Definition: Document.java:440
void setRelatedPath(String relatedPath)
Definition: Document.java:272
Collection< DocumentWorkflow > getWorkflows()
Definition: Document.java:619
void setCurrency(Currency currency)
Definition: Document.java:135
void setDescendants(Set< DocumentRelation > descendants)
Definition: Document.java:155
Collection< Contact > getActualHqContacts()
Definition: Document.java:646
void setNoAncestors(boolean noAncestors)
Definition: Document.java:224
void setConciliateRegister(boolean conciliateRegister)
Definition: Document.java:801
ContractPreference getDefaultContractPreference()
Definition: Document.java:429
Set< DocumentLine > getDocumentLines()
Definition: Document.java:180
void setDocumentDefinition(DocumentDefinition documentDefinition)
Definition: Document.java:175
void setReceiptDate(Date receiptDate)
Definition: Document.java:256
Collection< DocumentWorkflow > getWorkflows(boolean forward, boolean missing)
Definition: Document.java:623
Set< DocumentRelation > getUpdatedDescendants()
Definition: Document.java:147
Collection< ContractFlow > getContractFlows()
Definition: Document.java:615
DocumentAmountsByBook getAmountsByBook(BookDefinition bookDefinition)
Definition: Document.java:392
void backFrom(Document doc, String header, DocumentWorkflow workflow)
Definition: Document.java:560
transient boolean conciliateRegister
Definition: Document.java:795
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
void setAncestors(Set< DocumentRelation > ancestors)
Definition: Document.java:115
Collection< LineType > getLineTypes()
Definition: Document.java:734
void flowFrom(Document doc, String header, DocumentWorkflow workflow)
Definition: Document.java:539
void appendAmounts(Document relDoc, String header, DocumentWorkflow workflow)
Definition: Document.java:493
void setContractPreference(ContractPreference contractPreference)
Definition: LineType.java:81
Set< BookRegister > getBookRegisters()
Definition: Register.java:75
boolean isNotLoaded(Object o, String attribute)
Definition: Dao.java:216