[POS-commit] r5404 - in stoqlib/trunk/stoqlib: domain domain/test

Johan Dahlin jdahlin at async.com.br
Wed Nov 29 19:23:31 BRST 2006


Author: jdahlin
Date: Wed Nov 29 19:23:31 2006
New Revision: 5404

Modified:
   stoqlib/trunk/stoqlib/domain/test/test_till.py
   stoqlib/trunk/stoqlib/domain/till.py
   stoqlib/trunk/stoqlib/gui/dialogs/tilloperation.py

Log:
#2973: Bloqueios devido ao não fechamento de caixa, stoqlib part

Modified: stoqlib/trunk/stoqlib/domain/test/test_till.py
==============================================================================
--- stoqlib/trunk/stoqlib/domain/test/test_till.py	(original)
+++ stoqlib/trunk/stoqlib/domain/test/test_till.py	Wed Nov 29 19:23:31 2006
@@ -23,13 +23,14 @@
 ##
 """ This module test all class in stoq/domain/station.py """
 
+import datetime
+
 from kiwi.datatypes import currency
 
-from stoqlib.exceptions import StoqlibError
+from stoqlib.exceptions import TillError
 from stoqlib.database.runtime import get_current_station
 from stoqlib.domain.station import BranchStation
-from stoqlib.domain.till import (Till,
-                                 get_last_till_operation_for_current_branch)
+from stoqlib.domain.till import Till
 
 from stoqlib.domain.test.domaintest import DomainTest
 
@@ -44,7 +45,16 @@
         till.open_till()
         self.assertEqual(Till.get_current(self.trans), till)
 
-        self.assertRaises(StoqlibError, till.open_till)
+        self.assertRaises(TillError, till.open_till)
+
+    def testTillOpenOnce(self):
+        station = get_current_station(self.trans)
+        till = Till(connection=self.trans, station=station)
+
+        till.open_till()
+        till.close_till()
+
+        self.assertRaises(TillError, till.open_till)
 
     def testGetCurrentTillClose(self):
         station = get_current_station(self.trans)
@@ -121,11 +131,19 @@
         till.create_credit(currency(5), u"")
         self.assertEqual(till.get_debits_total(), old - 10)
 
+    def testTillOpenYesterday(self):
+        yesterday = (datetime.datetime.today() - datetime.timedelta(1)).date()
 
-    def testGetLastTillOperationForCurrentBranch(self):
+        # Open a till, set the opening_date to yesterday
         till = Till(connection=self.trans, station=get_current_station(self.trans))
         till.open_till()
+        till.opening_date = yesterday
+
+        self.assertRaises(TillError, Till.get_current, self.trans)
+        # This is used to close a till
+        self.assertEqual(Till.get_last_opened(self.trans), till)
+
         till.close_till()
 
-        self.assertEqual(till,
-                         get_last_till_operation_for_current_branch(self.trans))
+        self.assertEqual(Till.get_current(self.trans), None)
+

Modified: stoqlib/trunk/stoqlib/domain/till.py
==============================================================================
--- stoqlib/trunk/stoqlib/domain/till.py	(original)
+++ stoqlib/trunk/stoqlib/domain/till.py	Wed Nov 29 19:23:31 2006
@@ -37,8 +37,7 @@
 from stoqlib.database.columns import PriceCol, AutoIncCol
 from stoqlib.lib.translation import stoqlib_gettext
 from stoqlib.database.runtime import get_current_branch
-from stoqlib.exceptions import (TillError, DatabaseInconsistency,
-                                StoqlibError)
+from stoqlib.exceptions import TillError, DatabaseInconsistency
 from stoqlib.domain.base import Domain, BaseSQLView
 from stoqlib.domain.sale import Sale
 from stoqlib.domain.payment.base import AbstractPaymentGroup, Payment
@@ -85,11 +84,10 @@
     status = IntCol(default=STATUS_PENDING)
     balance_sent = PriceCol(default=0)
     final_cash_amount = PriceCol(default=0)
-    opening_date = DateTimeCol(default=datetime.datetime.now)
+    opening_date = DateTimeCol(default=None)
     closing_date = DateTimeCol(default=None)
     station = ForeignKey('BranchStation')
 
-
     #
     # Classmethods
     #
@@ -104,17 +102,35 @@
         branch = get_current_branch(conn)
         assert branch is not None
 
-        result = cls.select(AND(cls.q.status == Till.STATUS_OPEN,
-                                cls.q.stationID == BranchStation.q.id,
-                                BranchStation.q.branchID == branch.id),
-                            connection=conn)
-        if result.count() > 1:
-            raise TillError(
-                "You should have only one Till opened. Got %d instead." %
-                result.count())
-        elif result.count() == 0:
-            return None
-        return result[0]
+        till = cls.selectOne(AND(cls.q.status == Till.STATUS_OPEN,
+                                 cls.q.stationID == BranchStation.q.id,
+                                 # Bug 2978: Empty till objects are sometimes created
+                                 cls.q.opening_date != None,
+                                 BranchStation.q.branchID == branch.id),
+                             connection=conn)
+
+        if till is not None:
+            # Verify that the currently open till was opened today
+            open_date = till.opening_date.date()
+            if open_date != datetime.datetime.today().date():
+                raise TillError(_("You need to close the till opened at %s before "
+                                  "doing any fiscal operations" % (open_date,)))
+
+        return till
+
+    @classmethod
+    def get_last_opened(cls, conn):
+        """
+        Fetches the last Till which was opened.
+        If in doubt, use Till.get_current instead. This method is a special case
+        which is used to be able to close a till without calling get_current()
+        @param conn: a database connection
+        """
+
+        result = Till.selectBy(status=Till.STATUS_OPEN,
+                               connection=conn).orderBy('opening_date')
+        if result:
+            return result[0]
 
     #
     # Till methods
@@ -122,17 +138,29 @@
 
     def open_till(self):
         if self.status == Till.STATUS_OPEN:
-            raise StoqlibError('till_open(): Till was already open')
+            raise TillError(_('Till is already open'))
 
         conn = self.get_connection()
+
+        # Make sure that the till has not been opened today
+        today = datetime.datetime.today().date()
+        if Till.select(Till.q.opening_date >= today, connection=conn):
+            raise TillError(_("A till has already been opened today"))
+
         last_till = get_last_till_operation_for_current_branch(conn)
         if last_till:
+            if not last_till.closing_date:
+                raise TillError(_("Previous till was not closed"))
+            elif last_till.opening_date.date() == today:
+                raise TillError(_("A till has already been opened today"))
+
             final_cash = last_till.final_cash_amount
             if final_cash > 0:
                 reason = _(u'Cash amount remaining of %s'
                            % last_till.closing_date.strftime('%x'))
                 self.create_credit(final_cash, reason)
 
+            # FIXME: Move to sale.confirm()
             sales = last_till.get_unconfirmed_sales()
             for sale in sales:
                 sale.till = self
@@ -142,6 +170,7 @@
             # available to receive new payments
             self.addFacet(IPaymentGroup, connection=conn)
 
+        self.opening_date = datetime.datetime.now()
         self.status = Till.STATUS_OPEN
 
     def close_till(self):
@@ -152,8 +181,7 @@
         """
 
         if self.status == Till.STATUS_CLOSED:
-            raise StoqlibError("This till is already closed. Open a new till "
-                               "before close it.")
+            raise TillError(_("Till is already closed"))
 
         for sale in self.get_unconfirmed_sales():
             group = IPaymentGroup(sale)

Modified: stoqlib/trunk/stoqlib/gui/dialogs/tilloperation.py
==============================================================================
--- stoqlib/trunk/stoqlib/gui/dialogs/tilloperation.py	(original)
+++ stoqlib/trunk/stoqlib/gui/dialogs/tilloperation.py	Wed Nov 29 19:23:31 2006
@@ -59,16 +59,21 @@
         raise TillError("You already have a till operation opened. "
                         "Close the current Till and open another one.")
 
-    if self.run_dialog(TillOpeningEditor, conn):
-        self.conn.commit()
+    try:
+        model = self.run_dialog(TillOpeningEditor, conn)
+    except TillError, e:
+        warning(e)
+        model = None
+
+    if finish_transaction(self.conn, model):
         self._update_widgets()
         return True
 
+    return False
+
 def verify_and_close_till(self, conn, *args):
-    till = Till.get_current(conn)
-    if till is None:
-        raise ValueError("You should have a till operation opened at "
-                         "this point")
+    till = Till.get_last_opened(conn)
+    assert till
 
     # TillClosingEditor closes the till
     if not self.run_dialog(TillClosingEditor, conn, till):
@@ -242,6 +247,7 @@
 
     def get_extra_query(self):
         current_till = Till.get_current(self.conn)
+        assert current_till
         group = IPaymentGroup(current_till)
         group_ids = [group.id]
         for sale in Sale.get_available_sales(self.conn, current_till):


More information about the POS-commit mailing list