001    package market;
002    
003    import market.stdform.ButtonIDs;
004    import market.stdform.FSCheckable;
005    import market.stdform.FSSellerBill;
006    import market.stdform.FSSellerCustomerTable;
007    import market.stdform.FSSellerOrderTable;
008    import market.stdform.MSLogOff;
009    import sale.FormSheet;
010    import sale.FormSheetContentCreator;
011    import sale.Gate;
012    import sale.GateChangeTransition;
013    import sale.SaleProcess;
014    import sale.SalesPoint;
015    import sale.Transition;
016    import sale.UIGate;
017    import sale.stdforms.MsgForm;
018    import users.User;
019    import data.IntegerValue;
020    import data.Value;
021    import data.events.VetoException;
022    import data.stdforms.SingleTableFormSheet;
023    
024    
025    /**
026     * The seller process. This process handles the payment of the orders.
027     */
028    public class SProcessSeller extends SProcessMarket{
029    
030        /**
031             * ID for serialization.
032             */
033            private static final long serialVersionUID = -1568316941887916054L;
034    
035            /**
036         * Gate for selecting customers.
037         */
038        private UIGate uig_customerSelection = new UIGate(null, null);
039    
040        /**
041         * Gate for affirming the cancel of the order of the currently selected customer
042         * and for displaying lost articles in case of shortages.
043         */
044        private UIGate uig_cancelBuy = new UIGate(null, null);
045    
046        /**
047         * Gate for affirming the order of the currently selected customer.
048         */
049        private UIGate uig_order = new UIGate(null, null);
050    
051        /**
052         * Gate for selecting discount and mode of payment.
053         */
054        private UIGate uig_commit = new UIGate(null, null);
055    
056        /**
057         * Gate for displaying that no customer is selected.
058         */
059        private UIGate uig_noCustomer = new UIGate(null, null);
060    
061        /**
062         * Gate for displaying that the payment was successful.
063         */
064        private UIGate uig_log = new UIGate(null, null);
065    
066        /**
067         * Table that shows the queue of customers
068         */
069        private SingleTableFormSheet stfs_customerQueue;
070    
071        /**
072         * FormSheet that shows the bill
073         */
074        private FSCheckable fs_sellerBill;
075    
076        /**
077         * The UCustomer associated to the currently selected SICustomer
078         */
079        private UCustomer uc_customer;
080    
081        /**
082         * The currently selected SICustomer
083         */
084        private SICustomer sic_customer;
085    
086    
087        /**
088         * @param name the name of the process.
089         */
090        public SProcessSeller(String name){
091            super(name);
092        }
093    
094    
095        // ############################ Gates #####################################
096    
097        /**
098         * Attaches {@link FSSellerCustomerTable}, its actions and the menu to {@link #uig_customerSelection}.
099         * @return the set up {@link #uig_customerSelection}.
100         */
101        protected Gate getInitialGate() {
102            stfs_customerQueue = FSSellerCustomerTable.getCustomerTable(SMarket.getTillQueue());
103            stfs_customerQueue.setGate(uig_customerSelection);
104    
105            setTransition(stfs_customerQueue, changeToOrderGate(), ButtonIDs.BTN_OK);
106            setTransition(stfs_customerQueue, changeToCancelBuyGate(), ButtonIDs.BTN_CANCEL);
107    
108            uig_customerSelection.setFormSheet(stfs_customerQueue);
109            uig_customerSelection.setMenuSheet(new MSLogOff());
110            return uig_customerSelection;
111        }
112    
113        /**
114         * Attaches a {@link MsgForm} and its ok-action to {@link #uig_cancelBuy}.
115         * @return the set up {@link #uig_cancelBuy}.
116         */
117        protected Gate getCancelBuyGate(){
118            FormSheet fs = new MsgForm("Auftrag stornieren?",
119                    "Sind Sie sicher, dass sie den Auftrag von "
120                    + uc_customer.getFullName() +
121                    " stornieren wollen?");
122    
123            fs.addContentCreator(new FormSheetContentCreator(){
124                            private static final long serialVersionUID = -2681133639700724980L;
125    
126                            protected void createFormSheetContent(FormSheet fs) {
127                     fs.getButton(FormSheet.BTNID_OK).setAction(new sale.Action(){
128                                            private static final long serialVersionUID = 5209023490808964253L;
129    
130                                            public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
131                            uig_cancelBuy.setNextTransition(cancelBuy());
132                        }
133                    });
134                    fs.addButton("Zurück", FormSheet.BTNID_CANCEL, new sale.Action(){
135                                            private static final long serialVersionUID = 2724880458122504043L;
136    
137                                            public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
138                            uig_cancelBuy.setNextTransition(GateChangeTransition.CHANGE_TO_ROLLBACK_GATE);
139                        }
140                    });
141                }
142            });
143    
144            uig_cancelBuy.setFormSheet(fs);
145            return uig_cancelBuy;
146        }
147    
148        /**
149         * Attaches a {@link MsgForm} and its ok-action to {@link #uig_noCustomer}.
150         * @return the set up {@link #uig_noCustomer}.
151         */
152        protected Gate getNoCustomerGate(){
153            FormSheet fs = new MsgForm("Kein Kunde selektiert", "Sie müssen zunächst einen Kunden auswählen!");
154            setTransition(fs, GateChangeTransition.CHANGE_TO_ROLLBACK_GATE, FormSheet.BTNID_OK);
155            uig_noCustomer.setFormSheet(fs);
156            return uig_noCustomer;
157        }
158    
159        /**
160         * Attaches {@link FSSellerOrderTable} and its actions to {@link #uig_order}.
161         * @return the set up {@link #uig_order}.
162         */
163        protected Gate getOrderGate(){
164            FormSheet fs = FSSellerOrderTable.getOrderTable(uc_customer.getShoppingBasket(),
165                sic_customer);
166            setTransition(fs, GateChangeTransition.CHANGE_TO_COMMIT_GATE, ButtonIDs.BTN_ACCEPT);
167            setTransition(fs, GateChangeTransition.CHANGE_TO_ROLLBACK_GATE, ButtonIDs.BTN_BACK);
168            uig_order.setFormSheet(fs);
169            return uig_order;
170        }
171    
172        /**
173         * Attaches {@link FSSellerBill} and its actions to {@link #uig_commit}.
174         * @return the set up {@link #uig_commit}.
175         */
176        public Gate getCommitGate() {
177            fs_sellerBill = FSSellerBill.create(uc_customer.getShoppingBasket().sumStock(
178                        null,
179                        CIArticle.getCatalogItemValue(),
180                        new IntegerValue(0)),
181                        uc_customer.getDiscount());
182    
183            setAction(fs_sellerBill, new sale.Action(){
184                            private static final long serialVersionUID = -6783322252043885135L;
185    
186                            public void doAction(SaleProcess p, SalesPoint sp) throws Throwable {
187                    if(fs_sellerBill.checkTextFields(FSCheckable.ALL_ERRORMESSAGES_AT_ONCE, false)){
188                        uig_commit.setNextTransition(changeToConfirmationGate());
189                    }
190                }
191            }, ButtonIDs.BTN_ACCEPT);
192            setTransition(fs_sellerBill, new GateChangeTransition(getOrderGate()), ButtonIDs.BTN_BACK);
193    
194            uig_commit.setFormSheet(fs_sellerBill);
195            return uig_commit;
196        }
197    
198        /**
199         * @return the Gate to jump to if the selected customer has to be rolled back.
200         */
201        public Gate getRollbackGate() {
202            return new Gate(){
203                            private static final long serialVersionUID = 3945127902806820359L;
204    
205                            public Transition getNextTransition(SaleProcess pOwner, User usr)
206                        throws InterruptedException {
207                    return new Transition(){
208                                            private static final long serialVersionUID = 461250948438815078L;
209    
210                                            public Gate perform(SaleProcess pOwner, User usr) {
211                            pOwner.getBasket().rollback();
212                            uc_customer = null;
213                            sic_customer = null;
214                            return getInitialGate();
215                        }
216                    };
217                }
218            };
219        }
220    
221        /**
222         * Attaches {@link MsgForm} and its ok-action to {@link #uig_log}.
223         * @return the set up {@link #uig_log}.
224         */
225        public Gate getLogGate() {
226            FormSheet fs = new MsgForm("Bezahlung erfolgt!",
227                        "Der Kaufvorgang wurde abgeschlossen, die Artikel werden so schnell wie möglich ausgeliefert!");
228            setTransition(fs, changeToCustomerSelectionGate(), FormSheet.BTNID_OK);
229            uig_log.setFormSheet(fs);
230            return uig_log;
231        }
232    
233        // ####################### Transitions ##############################
234    
235        /**
236         * @return a Transition that changes to the {@link #getInitialGate()} and logs the process.
237         */
238        private Transition changeToCustomerSelectionGate(){
239            return new Transition(){
240                            private static final long serialVersionUID = -4641847114745645114L;
241    
242                            public Gate perform(SaleProcess p, User usr) {
243                    try {
244                      p.log (p);
245                    }
246                    catch (java.io.IOException ioe) {
247                      throw new Error ("Exception occurred while logging process: " + ioe);
248                    }
249                    return getInitialGate();
250                }
251            };
252        }
253    
254        /**
255         * @return a Transition that changes to the {@link #getInitialGate()},
256         * puts the articles of the selected customer back to the markets offer
257         * and removes the customer permanently from the till-queue.
258         */
259        private Transition cancelBuy(){
260            return new Transition(){
261                            private static final long serialVersionUID = -3580837460303959317L;
262    
263                            public Gate perform(SaleProcess pOwner, User usr) {
264                    SMarket.getOffer().addStock(uc_customer.getShoppingBasket(), null, true);
265                    pOwner.getBasket().commit();
266                    return getInitialGate();
267                }
268            };
269        }
270    
271        /**
272         * @return a Transition that changes to the {@link #getCancelBuyGate()}, if a customer is selected,
273         * otherwise it will change to the {@link #getNoCustomerGate()}
274         */
275        private Transition changeToCancelBuyGate(){
276            return new Transition(){
277                            private static final long serialVersionUID = -2334866371923561395L;
278    
279                            public Gate perform(SaleProcess pOwner, User usr) {
280                    if(stfs_customerQueue.getSelectedRecord()==null){
281                        return getNoCustomerGate();
282                    }
283                    else{
284                        setCustomer();
285                        return getCancelBuyGate();
286                    }
287                }
288            };
289        }
290    
291        /**
292         * @return a Transition that changes to the {@link #getOrderGate()}, if a customer is selected,
293         * otherwise it will change to the {@link #getNoCustomerGate()}
294         */
295        private Transition changeToOrderGate(){
296            return new Transition(){
297                            private static final long serialVersionUID = -6448337564880995546L;
298    
299                            public Gate perform(SaleProcess pOwner, User usr) {
300                    if(stfs_customerQueue.getSelectedRecord()==null){
301                        return getNoCustomerGate();
302                    }
303                    else{
304                        setCustomer();
305                        return getOrderGate();
306                    }
307                 }
308            };
309        }
310    
311        /**
312         * @return a Transition that changes to {@link #getLogGate()},
313         * adds the customers order to order-queue and
314         * adds the payed money to markets account
315         */
316        private Transition changeToConfirmationGate(){
317            return new Transition(){
318                            private static final long serialVersionUID = -8884213994350539278L;
319    
320                            public Gate perform(SaleProcess pOwner, User usr) {
321                    Value v = ((FSSellerBill)fs_sellerBill.getFormSheet()).getEndSum();
322                        double discount = Conversions.round(
323                                Double.valueOf(Conversions.convertComma(
324                                fs_sellerBill.getEntry(FSSellerBill.JTFC_DISCOUNT))).doubleValue(), 3);
325                    //show statistics not until day-end closing, so store them im dailyStats
326                    SMarket.getDailySalesStats().addSales(uc_customer, discount);
327                    SMarket.getDailySalesStats().addRevenue(Conversions.valueToInt(v));
328                    SMarket.getCustomerStats().addSales(uc_customer, v);
329                    SMarket.addToAccount(v);
330                    if(uc_customer.getShoppingBasket().size(null)>0) SICustomer.addToOrderQueue(uc_customer);
331                    pOwner.getBasket().commit();
332                    uc_customer = null;
333                    sic_customer = null;
334                    return getLogGate();
335                }
336            };
337        }
338    
339    
340        // ########################### private methods #########################################
341    
342        /**
343         * Sets customer from tableselection and remove it from customer-queue
344         */
345        private void setCustomer(){
346            sic_customer = (SICustomer)stfs_customerQueue.getSelectedRecord();
347            uc_customer = sic_customer.getCustomer();
348            try {
349                sic_customer.getStock().remove(sic_customer, this.getBasket());
350            } catch (VetoException e) {
351                System.err.println(e.getMessage());
352            }
353        }
354    }