All files / modules/model/objects ticket.ts

85.41% Statements 82/96
62.16% Branches 23/37
92.85% Functions 26/28
85.1% Lines 80/94

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298      8x 8x               8x 8x 8x 8x 8x 8x 8x 8x 8x 8x     8x                                                                                             8x 8x               8x 8x 8x 8x 8x 8x 8x     8x   207x   207x 207x                                 170x   170x 170x 170x 1020x       170x       103x 103x 618x     103x       103x 103x 103x       3x       1x       2x       1x       14x       9x       14x       5x       21x       72x       13x       15x               637x       224x 1x     223x   223x 3x     223x                 223x                     59x     59x     2x   1x   1x     55x       65x   65x 51x   51x 3x 3x       14x         31x           31x       8x       8x                   2x       63x 63x           5x        
import { dataService } from '../../data';
 
import { DataObject, ObjectOptions } from "../base";
import { CreateObject } from "../item";
import { TicketView, TicketViewProxy } from '../views/ticket';
 
export type TicketPayment = 'PAYCASH' | 'PAYCARD' | 'PAYLINE' | 'PAYMIXED' | 'PAYACCOUNT';
 
/****************************/
/* SCHEMA DEFINITION        */
/****************************/
 
import { SessionType } from '../model';
import { PlaceType } from '../model';
import { QrCodeType } from '../model';
import { TicketChangeType } from '../model';
import { TicketInvoiceType } from '../model';
import { TicketProductType } from '../model';
import { TicketOfferType } from '../model';
import { TicketExtraType } from '../model'; 
import { TicketDiscountType } from '../model';
import { MixedPaymentType } from '../model';
import { TicketInvoice } from './ticketinvoice';
 
const _schema = {
    name: "TICKET",
    "fields": [
        { name: "created", type: "date", volatile: true },
        { name: "updated", type: "date", volatile: true },
        { name: "status", type: "string", default: "PR" },
        { name: "session", type: "objid" },
        { name: "paidby", type: "objid" },
        { name: "paidon", type: "date" },
        { name: "paidin", type: "objid" },
        { name: "place", type: "objid" },
        { name: "qrcode", type: "objid" },
        { name: "price", type: "number" },
        { name: "refund", type: "number", default: "0" }, 
        { name: "taxes", type: "number" },
        { name: "orderno", type: "string" },
        { name: "series", type: "string", default: "S00000000" },
        { name: "invceno", type: "string" },
        { name: "elapsed", type: "number" },
        { name: "comments", type: "string" },
        { name: "payment", type: "string" },
        { name: "transaction", type: "objid" },
        { name: "account", type: "objid" },
        { name: "invceac", type: "objid" },
        { name: "given", type: "number" },
        { name: "flags", type: "string" },
        { name: "tmcommit", type: "date" }
    ],
    relate: [
        { direction: ">", target: "SESSION", by: "session", name: "session", reason: "session", child: false, class: SessionType },
        { direction: ">", target: "PLACE", by: "place", name: "place", reason: "place", child: false, class: PlaceType },
        { direction: ">", target: "QRCODE", by: "qrcode", name: "qrcode", reason: "qrcode", child: false, class: QrCodeType },
        { direction: ">", target: "SESSION", by: "paidby", name: "paidby", reason: "paidby", child: false, class: SessionType },
        { direction: ">", target: "PAYACCOUNT", by: "account", name: "account", reason: "account", child: true },            // TODO: more classes!!
        { direction: ">", target: "ACCOUNTINVOICE", by: "invceac", name: "invceac", reason: "invceac", child: true },        // TODO: more classes!!
        { direction: "<", target: "TICKETPRODUCT", by: "ticket", name: "product", reason: "products", child: true, class: TicketProductType },
        { direction: "<", target: "TICKETOFFER", by: "ticket", name: "offer", reason: "offers", child: true, class: TicketOfferType },
        { direction: "<", target: "TICKETEXTRA", by: "ticket", name: "extra", reason: "extras", child: true, class: TicketExtraType },
        { direction: "<", target: "TICKETDISCOUNT", by: "ticket", name: "discount", reason: "discounts", child: true, class: TicketDiscountType },
        { direction: "<", target: "TICKETMIXEDPAY", by: "ticket", name: "mixed", reason: "mixed", child: true, class: MixedPaymentType },
        { direction: "<", target: "ASKWAITER", by: "ticket", name: "request", reason: "requests", child: true },             // TODO: more classes!!
        { direction: "<", target: "TICKETCHANGE", by: "ticket", name: "change", reason: "changes", child: true, class: TicketChangeType },
        { direction: "<", target: "TICKETINVOICE", by: "ticket", name: "invoice", reason: "invoices", child: true, class: TicketInvoiceType },
        { direction: "<", target: "TICKETAUDIT", by: "ticket", name: "audit", reason: "audits", child: true }                // TODO: more classes!!
    ]
} as const;
 
const _TicketClass = CreateObject(_schema);
Iif (!_TicketClass) {
    throw new Error("Failed to create BaseClass for '" + _schema.name + "'");
}
 
/****************************/
/* CLASS DECLARATION        */
/****************************/
 
enum TicketFlags {
    OrderPrinted = 0,       // the 'new ticket' has been sent to printer
    ReadyNotified = 1,      // the 'ticket ready' has been notified to client
    TicketReady = 2,        // the ticket is ready
    TicketToPay = 3,        // ticket payment has been requested
    TicketPaid = 4,         // ticket has been paid
    TicketReopened = 5      // ticket was reopened after payment
};
 
export class Ticket extends _TicketClass { 
    constructor(objid: string | null, data: dataService, objoptions: ObjectOptions | null = null){
        super(_schema, objid, data, objoptions || {});
 
        if (!this._data['flags']){  // initialize the flags value (cannot be null)
            this._data['flags'] = '0'.repeat(Object.keys(TicketFlags).length / 2);
        }
    }
 
    Copy(store: Array<DataObject> = []): Ticket {
        return this._Copy(store) as Ticket;
    }
 
    override get viewProxy(): TicketView {
        return TicketViewProxy(this, this.data);
    }    
 
    /****************************/
    /* FLAGS MANAGEMENT         */
    /****************************/
 
    protected get flagArray(): Array<boolean> {
        const _array: boolean[] = new Array(Object.keys(TicketFlags).length / 2).fill(false);
        
        if (this.flags){
            const _flags = Array.from(this.flags);
            for(let i=0; i < _flags.length; i++){
                _array[i] = (_flags[i] == '1');
            }        
        }
 
        return _array;
    }
 
    protected set flagArray(value: Array<boolean>) {
        const _array: string[] = [];
        for(let i=0; i < value.length; i++){
            _array[i] = (value[i] === true) ? '1' : '0';
        }
 
        this.flags = _array.join(''); 
    }
 
    protected _setFlagValue(flag: TicketFlags, value: boolean){
        const _flags = this.flagArray;
        _flags[flag] = value;
        this.flagArray = _flags;        
    }
 
    get PrintedFlag(): boolean {
        return this.flagArray[TicketFlags.OrderPrinted];
    }
 
    set PrintedFlag(value: boolean){
        this._setFlagValue(TicketFlags.OrderPrinted, value);
    }
 
    get NotifiedFlag(): boolean {
        return this.flagArray[TicketFlags.ReadyNotified];
    }
 
    set NotifiedFlag(value: boolean){
        this._setFlagValue(TicketFlags.ReadyNotified, value);
    }
 
    get ReadyFlag() : boolean {
        return this.flagArray[TicketFlags.TicketReady];
    }
 
    set ReadyFlag(value: boolean){
        this._setFlagValue(TicketFlags.TicketReady, value);
    }
 
    get ToPayFlag() : boolean {
        return this.flagArray[TicketFlags.TicketToPay];
    }
 
    set ToPayFlag(value: boolean){
        this._setFlagValue(TicketFlags.TicketToPay, value);
    }
 
    get PaidFlag() : boolean {
        return this.flagArray[TicketFlags.TicketPaid];
    }
 
    set PaidFlag(value: boolean){
        this._setFlagValue(TicketFlags.TicketPaid, value);
    }
 
    get ReopenedFlag() : boolean {
        return this.flagArray[TicketFlags.TicketReopened];
    }
 
    set ReopenedFlag(value: boolean){
        this._setFlagValue(TicketFlags.TicketReopened, value);
    }
 
    /****************************/
    /* CLASS MEMBERS            */
    /****************************/
    
    get status(): string | null {
        return this['__base__status'];
    }
 
    set status(value: string){
        if (this.status == 'DE'){
            return;     // cannot modify deleted items
        }
 
        this['__base__status'] = value;
 
        if (this.status == 'RD'){
            this.ReadyFlag = true;
        }
 
        Iif (this.place){
            if ((this.status == 'DE') && !this.Commited){
                this.place.DelTicket(this);
            }
            else {
                this.place.DoRefresh(this.table);
            }    
        }
 
        Iif (this.qrcode){
            if ((this.status == 'DE') && !this.Commited){
                this.qrcode.DelTicket(this);
            }
            else {
                this.qrcode.DoRefresh(this.table);
            }
        }
    }   
 
    get payment(): TicketPayment | null {
        const _payment = this['__base__payment'];
 
        // backwards compatibility
        switch(_payment){
            case 'FORCEPAY':
            case 'CASHPAY':
                return 'PAYCASH';
            case 'CARDPAY':
                return 'PAYCARD';
            case 'PAYPAL':
                return 'PAYLINE';
        }
        
        return _payment;
    }
 
    set payment(value: TicketPayment | null) {
        this['__base__payment'] = value;
 
        if (value){
            this.PaidFlag = true;
 
            if (value == 'PAYACCOUNT'){
                this.PaidFlag = true;
                this.ToPayFlag = true;
            }
        }
        else {
            this.PaidFlag = false;
        }
    }
 
    get invoice(): TicketInvoice | null {
        for(const _invoice of this.invoices){
            Iif (_invoice.IsValid){
                return _invoice;
            }
        }
 
        return null;
    }
 
    set invoice(value: TicketInvoice | null){
        for(const _invoice of this.invoices){
            _invoice.status = 'DE';
        }
 
        Iif (value != null){
            this.AddInvoice(value);
        }
    }
 
    /****************************/
    /* CUSTOM METHODS           */
    /****************************/
 
    get IsValid(): boolean {
        return (!!this.status && (this.status != 'DE'));
    }  
    
    get IsCancelled(): boolean {
        if (this.status){
            return ['CC', 'DE'].includes(this.status);
        }
        return false;
    }
 
    get IsDeleted(): boolean {
        return (!!this.status && (this.status == 'DE'));
    }
}