BrightSide Workbench Full Report + Source Code
FinancialsCart.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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 
19 package org.turro.financials.cart.www;
20 
21 import java.util.ArrayList;
22 import java.util.Date;
23 import org.turro.action.Contacts;
24 import org.turro.action.MailSenders;
25 import org.turro.contacts.Address;
26 import org.turro.contacts.Contact;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.elephant.util.DateFormats;
30 import org.turro.financials.cart.Cart;
31 import org.turro.financials.cart.CartItem;
32 import org.turro.financials.cart.ShopContext;
33 import org.turro.financials.contract.logic.ContractWrapper;
34 import org.turro.financials.db.FinancialsPU;
35 import org.turro.financials.entity.Contract;
36 import org.turro.financials.entity.ContractDefinition;
37 import org.turro.financials.entity.ContractPreference;
38 import org.turro.financials.entity.Document;
39 import org.turro.financials.entity.DocumentDefinition;
40 import org.turro.financials.entity.DocumentLine;
41 import org.turro.financials.entity.DocumentWorkflow;
42 import org.turro.financials.entity.FollowUp;
43 import org.turro.financials.entity.LineType;
44 import org.turro.financials.entity.OperatingModifier;
45 import org.turro.financials.model.document.DocumentWrapper;
46 import org.turro.i18n.I_;
47 import org.turro.jpa.Dao;
48 import org.turro.plugin.contacts.IContact;
49 
54 public class FinancialsCart {
55 
56  private static final int
57  CONTRACT_TYPE = 56,
58  DOCUMENT_BILL = 1,
59  DOCUMENT_TICKET = 67,
60  DOCUMENT_PAYMENT = 6,
61  SALE_SERVICE = 24,
62  SALE_PRODUCT = 23,
63  SALE_LINETYPE = 1,
64  PAYMENT_LINETYPE = 6;
65 
66  private final Cart cart;
67  private final Dao dao;
68  private final boolean paid;
69 
70  public FinancialsCart(Cart cart, Dao dao, boolean paid) {
71  this.cart = cart;
72  this.dao = dao;
73  this.paid = paid;
74  }
75 
76  public Document generate() {
77  Contract contract = getContract();
78  if(contract == null) {
79  contract = createContract();
80  }
81  return createDocument(contract);
82  }
83 
84  private Contract getContract() {
85  WhereClause wc = new WhereClause();
86  wc.addClause("select c from Contract as c");
87  wc.addClause("where c.contractor = :contactId");
88  wc.addNamedValue("contactId", cart.getContact().getId());
89  wc.addClause("and c.contractDefinition.id = :ctcdef");
90  wc.addNamedValue("ctcdef", Long.valueOf(CONTRACT_TYPE));
91  return (Contract) dao.getSingleResultOrNull(wc);
92  }
93 
94  private Contract createContract() {
95  Contract c = new Contract();
96  c.setContractDefinition(dao.find(ContractDefinition.class, Long.valueOf(CONTRACT_TYPE)));
97  c.setContractor(cart.getContact().getId());
98  c.setActive(true);
99  c.setCash(false);
100  c.getContractPreferences().add(dao.find(ContractPreference.class, Long.valueOf(SALE_PRODUCT)));
101  c.getContractPreferences().add(dao.find(ContractPreference.class, Long.valueOf(SALE_SERVICE)));
102  c.setCurrency(((Contact) cart.getContact().getContact()).getCurrency());
103  c.setOperatingModifier(OperatingModifier.OPMOD_NORMAL);
104  c.setStock(false);
105  return new ContractWrapper(c).save();
106  }
107 
108  private Document createDocument(Contract contract) {
109  DocumentDefinition bill = dao.find(DocumentDefinition.class, Long.valueOf(DOCUMENT_BILL));
110  DocumentDefinition ticket = dao.find(DocumentDefinition.class, Long.valueOf(DOCUMENT_TICKET));
111  ContractPreference saleProduct = dao.find(ContractPreference.class, Long.valueOf(SALE_PRODUCT));
112  ContractPreference saleService = dao.find(ContractPreference.class, Long.valueOf(SALE_SERVICE));
113  LineType lineType = dao.find(LineType.class, Long.valueOf(SALE_LINETYPE));
114  Contract store = ShopContext.getInstance().getContractStore();
115  Document doc = new Document();
116  if(cart.isWantBill()) {
117  doc.setDocumentDefinition(bill);
118  } else {
119  doc.setDocumentDefinition(ticket);
120  }
121  doc.setContract(contract);
122  doc.setCurrency(contract.getCurrency());
123  doc.setDocumentDate(new Date());
124  doc.setReceiptDate(doc.getDocumentDate());
125  doc.setDraft(false);
126  doc.setNotes(ElephantContext.getSiteName() + ":SHOP-ORDER: " + cart.getOrder());
127  if(cart.isPickup()) {
128  doc.setNotes(doc.getNotes() + "\n" + I_.get("Store pickup"));
129  } else {
130  Contact contact = (Contact) doc.getContract().getIContractor().getContact();
131  if(contact != null) {
132  Address address = contact.getAddressMap().get(ShopContext.getInstance().getDelivery());
133  if(address != null) {
134  doc.setNotes(doc.getNotes() + "\n" + address.getFullAddress());
135  }
136  }
137  }
138  int count = 0;
139  for(CartItem ci : cart.getItems()) {
140  if(ci.isValid()) {
141  DocumentLine dl = new DocumentLine();
142  dl.setDocument(doc);
143  if(ci.getProduct().isService()) {
144  dl.setContractPreference(saleService);
145  } else {
146  dl.setContractPreference(saleProduct);
147  }
148  dl.setDiscountMoney(0);
149  dl.setDiscountPerCent(0);
150  dl.setLineOrder(count++);
151  dl.setLineType(lineType);
152  dl.setPrice(ci.getPrice());
153  dl.setProduct(ci.getProduct());
154  dl.setQuantity(ci.getQuantity());
155  dl.setConcept(ci.getConcept());
156  dl.setStore(store);
157  dl.setTax(ci.getTax());
158  dl.setEquivalenceSurcharge(0);
159  dl.setRetention(0);
160  doc.getDocumentLines().add(dl);
161  }
162  }
163  doc = new DocumentWrapper(doc).save(new ArrayList(doc.getDocumentLines()), null);
164  if(paid) {
165  DocumentDefinition payment = dao.find(DocumentDefinition.class, Long.valueOf(DOCUMENT_PAYMENT));
166  LineType linePayment = dao.find(LineType.class, Long.valueOf(PAYMENT_LINETYPE));
167  Document expiry = new Document();
168  expiry.setDocumentDefinition(payment);
169  DocumentWorkflow dw = new DocumentWorkflow();
170  dw.setAncestor(doc.getDocumentDefinition());
171  dw.setDescendant(expiry.getDocumentDefinition());
172  expiry.flowFrom(doc, getDocDescription(doc), dw);
173  expiry.setDocumentNumber(doc.getDocumentNumber() + "/1");
174  expiry.setDocumentDate(doc.getDocumentDate());
175  expiry.setReceiptDate(doc.getReceiptDate());
176  DocumentLine dl = expiry.getDocumentLines().iterator().next();
177  dl.setStore(ShopContext.getInstance().getContractBank());
178  dl.setLineType(linePayment);
179  new DocumentWrapper(expiry).save(new ArrayList(expiry.getDocumentLines()), null);
180  }
181  FollowUp fu = new FollowUp();
182  fu.setDocument(doc);
183  IContact logistic = Contacts.getContactByEmail(ShopContext.getInstance().getLogistic());
184  fu.setIPrepare(logistic);
185  fu.setIDeliver(logistic);
186  new FinancialsPU().saveObject(fu);
187  MailSenders.getPool()
188  .addContact(fu.getIPrepare())
189  .addAdministrators()
190  .send("SHOP-ORDER", doc.getDocumentString());
191  return doc;
192  }
193 
194  public String getDocDescription(Document doc) {
195  return doc.getDocumentDefinition().getName() + ": " +
196  (doc.getForcedView() == null ? "" : "(" + doc.getForcedView().getName() + ") ") +
197  doc.getDocumentNumber() + " " +
198  DateFormats.format(doc.getDocumentDate(), true);
199  }
200 
201 }
void addNamedValue(String name, Object value)
static final String format(Date d, boolean dateOnly)
CartItemSet getItems()
Definition: Cart.java:54
FinancialsCart(Cart cart, Dao dao, boolean paid)
void setOperatingModifier(OperatingModifier operatingModifier)
Definition: Contract.java:232
void setActive(boolean active)
Definition: Contract.java:113
void setContractor(String contractor)
Definition: Contract.java:157
Set< ContractPreference > getContractPreferences()
Definition: Contract.java:145
void setCurrency(Currency currency)
Definition: Contract.java:168
void setContractDefinition(ContractDefinition contractDefinition)
Definition: Contract.java:133
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419