19 package org.turro.financials.cart.www;
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;
56 private static final int
66 private final Cart cart;
67 private final Dao dao;
68 private final boolean paid;
78 if(contract ==
null) {
79 contract = createContract();
81 return createDocument(contract);
86 wc.
addClause(
"select c from Contract as c");
87 wc.
addClause(
"where c.contractor = :contactId");
89 wc.
addClause(
"and c.contractDefinition.id = :ctcdef");
105 return new ContractWrapper(c).save();
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();
117 doc.setDocumentDefinition(bill);
119 doc.setDocumentDefinition(ticket);
121 doc.setContract(contract);
122 doc.setCurrency(contract.getCurrency());
123 doc.setDocumentDate(
new Date());
124 doc.setReceiptDate(doc.getDocumentDate());
126 doc.setNotes(ElephantContext.getSiteName() +
":SHOP-ORDER: " + cart.
getOrder());
128 doc.setNotes(doc.getNotes() +
"\n" + I_.get(
"Store pickup"));
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());
139 for(CartItem ci : cart.
getItems()) {
141 DocumentLine dl =
new DocumentLine();
143 if(ci.getProduct().isService()) {
144 dl.setContractPreference(saleService);
146 dl.setContractPreference(saleProduct);
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());
157 dl.setTax(ci.getTax());
158 dl.setEquivalenceSurcharge(0);
160 doc.getDocumentLines().add(dl);
163 doc =
new DocumentWrapper(doc).save(
new ArrayList(doc.getDocumentLines()),
null);
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());
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);
181 FollowUp fu =
new FollowUp();
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())
190 .send(
"SHOP-ORDER", doc.getDocumentString());
void addClause(String clause)
void addNamedValue(String name, Object value)
FinancialsCart(Cart cart, Dao dao, boolean paid)
String getDocDescription(Document doc)
void setOperatingModifier(OperatingModifier operatingModifier)
void setActive(boolean active)
void setStock(boolean stock)
void setContractor(String contractor)
Set< ContractPreference > getContractPreferences()
void setCurrency(Currency currency)
void setCash(boolean cash)
void setContractDefinition(ContractDefinition contractDefinition)
RegisterView getForcedView()
String getDocumentNumber()
DocumentDefinition getDocumentDefinition()
Object getSingleResultOrNull(SqlClause sc)