static function delete($id) { $pdo = PDOBuilder::getPDO(); $db = DB::get(); $ticket = TicketsService::get($id); if ($ticket === null) { return false; } $cashSrv = new CashesService(); $cash = $cashSrv->get($ticket->cashId); if ($cash === null || $cash->isClosed()) { return false; } $cashRegSrv = new CashRegistersService(); $cashReg = $cashRegSrv->getFromCashId($cash->id); // As cash must be opened, cashregister location is considered accurate $locationId = $cashReg->locationId; $newTransaction = !$pdo->inTransaction(); if ($newTransaction) { $pdo->beginTransaction(); } // Delete ticket lines // Also check for prepayments refill $stmtLines = $pdo->prepare("DELETE FROM TICKETLINES " . "WHERE TICKET = :id"); $stmtLines->bindParam(":id", $ticket->id); foreach ($ticket->lines as $line) { // Update stock if ($line->productId !== null) { $discountRate = $ticket->discountRate; $fullDiscount = $discountRate + $line->discountRate; $discountPrice = $line->price * (1.0 - $fullDiscount); $move = new StockMove($ticket->date, StockMove::REASON_IN_REFUND, $line->productId, $locationId, $line->attrSetInstId, $line->quantity, $discountPrice); if (StocksService::addMove($move) === false) { if ($newTransaction) { $pdo->rollback(); } return false; } } // Check prepayment refill // Refill is not affected by discount $prepaidIds = ProductsService::getPrepaidIds(); if ($ticket->customerId !== null && in_array($line->productId, $prepaidIds)) { $custSrv = new CustomersService(); $ok = $custSrv->addPrepaid($ticket->customerId, -$line->price * $line->quantity); if ($ok === false) { if ($newTransaction) { $pdo->rollback(); } return false; } } } if ($stmtLines->execute() === false) { if ($newTransaction) { $pdo->rollback(); } return false; } // Delete payments // Also check for prepayment debit and debt $stmtPay = $pdo->prepare("DELETE FROM PAYMENTS WHERE RECEIPT = :id"); $stmtPay->bindParam(":id", $ticket->id); foreach ($ticket->payments as $payment) { if ($payment->type == 'prepaid' || $payment->type == 'debt') { $custSrv = new CustomersService(); if ($payment->type == 'prepaid') { $ok = $custSrv->addPrepaid($ticket->customerId, $payment->amount); } else { $ok = $custSrv->recoverDebt($ticket->customerId, $payment->amount); } if ($ok === false) { if ($newTransaction) { $pdo->rollback(); } return false; } } } if ($stmtPay->execute() === false) { if ($newTransaction) { $pdo->rollback(); } return false; } // Delete taxlines $stmtTax = $pdo->prepare("DELETE FROM TAXLINES WHERE RECEIPT = :id"); $stmtTax->bindParam(":id", $ticket->id); if ($stmtTax->execute() === false) { if ($newTransaction) { $pdo->rollback(); } return false; } // Delete ticket $discountRate = $ticket->discountRate; $stmtTkt = $pdo->prepare("DELETE FROM TICKETS WHERE ID = :id"); $stmtTkt->bindParam(':id', $ticket->id); if ($stmtTkt->execute() === false) { if ($newTransaction) { $pdo->rollback(); } return false; } // Delete receipt $stmtRcpt = $pdo->prepare("DELETE FROM RECEIPTS WHERE ID = :id"); $stmtRcpt->bindParam(":id", $ticket->id); if ($stmtRcpt->execute() === false) { if ($newTransaction) { $pdo->rollback(); } return false; } if ($newTransaction) { $pdo->commit(); } return true; }