github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/parser/sql_example_app/vrestaurant/package.vsql (about)

     1  /*
     2  * Copyright (c) 2023-present unTill Pro, Ltd.
     3  */
     4  
     5  APPLICATION vrestaurant();
     6  
     7  -- Declare tag to assign it later to definition(s)
     8  TAG BackofficeTag;
     9  TAG PosTag;
    10  
    11  -- TABLE BOEntity : is an Abstract base data struct for many CDOC tables
    12  ABSTRACT TABLE BOEntity INHERITS CDoc( -- TODO: ABSTRACT
    13      Name varchar(50) NOT NULL, -- TODO NOT NULL everywhere
    14      Number int NOT NULL -- Number sequence(1) ??? smm
    15  ) WITH Tags=(BackofficeTag);
    16  
    17  -- TABLE Person : is an Abstract data struct for Waiters, Clients, Adminitsrators, Manager
    18  ABSTRACT TABLE Person INHERITS BOEntity ( --TODO:  ABSTRACT
    19      Address varchar(50), 
    20      Email varchar(50) CHECK '^\\S+@\\S+\\.\\S+$',
    21      Phone varchar(20),
    22      Picture blob
    23  ) WITH Tags=(BackofficeTag);
    24  
    25  WORKSPACE Restaurant (
    26      DESCRIPTOR (
    27  	    Address varchar(50),
    28  	    Currency varchar(3),
    29  	    Phone varchar(20),
    30  	    OpenHours    int,
    31  	    OpenMinutes  int,
    32  	    OwnerName varchar(30)
    33      );
    34  
    35      ROLE LocationUser;
    36      ROLE LocationManager;
    37  
    38      -- CDOC data schemes
    39  
    40      -- TABLE Client   : describes restaurant client entity
    41      TABLE Client INHERITS Person(
    42          -- access to alcohol
    43          Datebirth int64, 
    44          -- payment card number, used for payments in Restaurant
    45          Card varchar(20),
    46          -- percent of permanent discount
    47          DiscountPercent int 
    48      );
    49  
    50      -- TABLE Register   : describes payment resgitration devices
    51      TABLE Register INHERITS Person(
    52          Code varchar(20) -- personal code in inner login system    
    53      );
    54  
    55      -- TABLE Position   : Restaurant job list
    56      TABLE Position INHERITS BOEntity(
    57      );
    58  
    59      -- TABLE POSUser   : describes restaurant user entity (Waiter/Administrator/Manager)
    60      TABLE POSUser INHERITS Person(
    61          -- personal code in inner login system    
    62          Code varchar(20), 
    63  	    PositionID ref(Position),
    64          -- wage/salary rate
    65          Wage float32
    66      );
    67  
    68      -- TABLE TablePlan : describes Physical plan of tables/bar counters. etc. in Restaurant.
    69      TABLE TablePlan INHERITS BOEntity (
    70          -- Image of restaurant plan
    71          Picture blob, 
    72          Width int,
    73          Height int,
    74          -- List of tables on table plan
    75          TableItem TABLE TableItem (
    76              Tableno int,  
    77              -- color of empty table
    78      	    Color int,    
    79              -- number of table chairs
    80              Chairs int,   
    81              Left int,
    82              Top int,
    83   	        Width int,
    84  	        Height int
    85          )
    86      );
    87  
    88      -- TABLE departments : defines Restaurant department button entity
    89      TABLE Department INHERITS BOEntity (
    90      );
    91  
    92      -- TABLE Article : defines Restaurant articles button entity
    93      TABLE Article INHERITS BOEntity(
    94          DepartamentID ref(Department),
    95          -- article barcode to order by scanner
    96          Barcode varchar(20),  
    97          -- article sale price 
    98          Price currency, 
    99          -- V.A.T. in percent
   100          VatPercent currency, 
   101          -- Absolut V.A.T. value 
   102          Vat float32         
   103      );
   104  
   105      -- TABLE PaymentType : defines types of payment modes, using in Restaurant
   106      TABLE PaymentType INHERITS BOEntity(
   107          --inner value of type
   108          Kind int 
   109      );
   110  
   111      -- TABLE Transaction   : defines parameters of table, occupied by client
   112      TABLE Transaction INHERITS WDoc(
   113          Name varchar(50),
   114          Number int,
   115          Tableno int, 
   116          --time of very first order on table
   117          OpenTimeStamp timestamp, 
   118          -- time of final payment and closing table transaction
   119          CloseTimeStamp timestamp,
   120          -- POS user, who created made very first order
   121          CreatorID ref(POSUser) NOT NULL, 
   122          -- client, assigned to transaction
   123          Client ref(Client) NOT NULL 
   124      ) WITH Tags=(PosTag);
   125  
   126      -- TABLE Orders     : defines parameters of order on table. One transaction can have several orders
   127      TABLE Order INHERITS ODoc(
   128          TransactionID ref(Transaction) NOT NULL, 
   129          -- time of creating order
   130          OrdTimeStamp timestamp, 
   131          UserID ref(POSUser) NOT NULL, 
   132          -- TABLE order_items : the list articles, options, comments, from which order consists of
   133          OrderItem TABLE OrderItem (
   134              Order ref(Order) NOT NULL,
   135              -- can be null for text comments
   136              ArticleID ref(Article), 
   137              -- number of articles in order
   138              Quantity int,           
   139              -- text message, added to the order
   140              Comment varchar(50),
   141              Price currency,
   142              VatPercent currency,
   143              Vat float32
   144          )
   145      ) WITH Tags=(PosTag);
   146  
   147      -- TABLE Bill          : defines parameters of bill on table. One transaction can have several bills
   148      TABLE Bill INHERITS ODoc(
   149          TransactionID ref(Transaction) NOT NULL, 
   150          --   ref of POSUser, who took created Transaction
   151         	AuthorID ref(POSUser) NOT NULL,
   152  	    RegisterID ref(Register) NOT NULL,
   153          -- bill number, unique per cash register   
   154  	    Number int,  
   155          -- time of Bill creating
   156          PayTimeStamp timestamp,
   157          Tips float32,
   158          -- TABLE BillPayments  : Defines set of payment methods related to bill
   159          BillPayment TABLE BillPayment (
   160              Bill ref(Bill) NOT NULL,
   161              PaymentTypeID ref(PaymentType) NOT NULL,
   162              -- amount of payment
   163              Amount currency
   164          )
   165      ) WITH Tags=(PosTag);
   166  
   167      EXTENSION ENGINE BUILTIN (
   168  	
   169  
   170  	    SYNC PROJECTOR UpdateTableStatus
   171              AFTER INSERT ON Transaction OR
   172  	        AFTER EXECUTE WITH PARAM ON (Order, Bill)
   173  		INTENTS(View(TableStatus));
   174  
   175  	    PROJECTOR UpdateSalesReport
   176              AFTER INSERT ON Transaction OR 
   177  	        AFTER EXECUTE WITH PARAM ON Bill
   178  		INTENTS(View(SalesPerDay));
   179  
   180      );
   181  
   182  -- ACLs
   183      GRANT ALL ON ALL TABLES WITH TAG BackofficeTag TO LocationManager;
   184      -- GRANT INSERT,UPDATE ON ALL TABLES WITH TAG sys.ODoc TO LocationUser;
   185      GRANT SELECT ON TABLE Order TO LocationUser;
   186      -- GRANT EXECUTE ON COMMAND MakeOrder TO LocationUser;
   187      -- GRANT EXECUTE ON COMMAND MakePayment TO LocationUser;
   188      GRANT SELECT ON ALL QUERIES WITH TAG PosTag TO LocationUser;
   189  
   190      -- VIEW TableStatus     : keeps actual status of table(free/occupied)
   191      VIEW TableStatus (
   192          Dummy int,
   193          TableNumber int,
   194          --  status of table(free/occupied)
   195          Status int,
   196          PRIMARY KEY ((Dummy), TableNumber)
   197      ) AS RESULT OF UpdateTableStatus;
   198  
   199      -- VIEW SalesPerDay     : sales report per day
   200      VIEW SalesPerDay(
   201          Year int32,
   202          Month int32, 
   203          Day int32, 
   204          Number int32, 
   205          DepartmentID ref(Department) NOT NULL,
   206          ArticleID ref(Article) NOT NULL,
   207          Quantity int32, --!!! Must be float32
   208          Amount int32,--!!! Must be Currency
   209          Vat int32, --!!! Must be float32
   210          VatPercent int32, --!!! Must be Currency
   211          PaymentTypeID ref(PaymentType) NOT NULL,
   212          PRIMARY KEY ((Year, Month, Day), Number)
   213      ) AS RESULT OF UpdateSalesReport;
   214  );    
   215