コード例 #1
0
 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;
 }
コード例 #2
0
ファイル: CashesAPI.php プロジェクト: booko/pasteque-server
 /** Run the service and set result. */
 protected function proceed()
 {
     $srv = new CashesService();
     $db = DB::get();
     switch ($this->action) {
         case 'get':
             if (isset($this->params['id'])) {
                 $ret = $srv->get($this->params['id']);
             } else {
                 $ret = $srv->getCashRegister($this->params['cashRegisterId']);
                 if ($ret === null || $ret->isClosed()) {
                     $ret = null;
                 }
             }
             $this->succeed($ret);
             break;
         case 'zticket':
             $ret = $srv->getZTicket($this->params['id']);
             $this->succeed($ret);
             break;
         case 'search':
             $cashRegisterId = $this->getParam("cashRegisterId");
             $dateStart = $this->getParam("dateStart");
             $dateStop = $this->getParam("dateStop");
             $conditions = array();
             if ($cashRegisterId !== null) {
                 $conditions[] = array("cashRegisterId", "=", $cashRegisterId);
             }
             if ($dateStart !== null) {
                 $conditions[] = array("openDate", ">=", $db->dateVal($dateStart));
             }
             if ($dateStop !== null) {
                 $conditions[] = array("closeDate", "<=", $db->dateVal($dateStop));
             }
             $this->succeed($srv->search($conditions));
             break;
         case 'update':
             $json = json_decode($this->params['cash']);
             $open = null;
             $id = null;
             if (property_exists($json, 'id')) {
                 $id = $json->id;
             }
             if (property_exists($json, 'openDate')) {
                 $open = $json->openDate;
             }
             $close = null;
             if (property_exists($json, 'closeDate')) {
                 $close = $json->closeDate;
             }
             $openCash = null;
             if (property_exists($json, 'openCash')) {
                 $openCash = $json->openCash;
             }
             $closeCash = null;
             if (property_exists($json, 'closeCash')) {
                 $closeCash = $json->closeCash;
             }
             $expectedCash = null;
             if (property_exists($json, 'expectedCash')) {
                 $expectedCash = $json->expectedCash;
             }
             $cashRegisterId = $json->cashRegisterId;
             $sequence = null;
             if (property_exists($json, 'sequence')) {
                 $sequence = $json->sequence;
             }
             if ($id !== null) {
                 // Update an existing cash
                 $cash = Cash::__build($id, $cashRegisterId, $sequence, $open, $close, $openCash, $closeCash, $expectedCash);
                 if ($srv->update($cash)) {
                     $this->succeed($cash);
                 } else {
                     $this->fail(APIError::$ERR_GENERIC);
                 }
             } else {
                 // Create a cash and update with given data
                 if ($srv->add($cashRegisterId)) {
                     $cash = $srv->getCashRegister($cashRegisterId);
                     $cash->openDate = $open;
                     $cash->closeDate = $close;
                     $cash->openCash = $openCash;
                     $cash->closeCash = $closeCash;
                     $cash->expectedCash = $expectedCash;
                     if ($srv->update($cash)) {
                         $this->succeed($cash);
                     } else {
                         $this->fail(APIError::$ERR_GENERIC);
                     }
                 } else {
                     $this->fail(APIError::$ERR_GENERIC);
                 }
             }
             break;
     }
 }
コード例 #3
0
 public function testGetInexistent()
 {
     $srv = new CashesService();
     $cashReg = $srv->get("none");
     $this->assertNull($cashReg, "Inexistent cash register found");
 }
コード例 #4
0
ファイル: TicketsAPI.php プロジェクト: booko/pasteque-server
 protected function proceed()
 {
     switch ($this->action) {
         case 'getShared':
             $tkt = TicketsService::getSharedTicket($this->params['id']);
             $this->succeed($tkt);
             break;
         case 'getAllShared':
             $tkts = TicketsService::getAllSharedTickets();
             $this->succeed($tkts);
             break;
         case 'delShared':
             $this->succeed(TicketsService::deleteSharedTicket($this->params['id']));
             break;
         case 'share':
             $json = json_decode($this->params['ticket']);
             $ticket = SharedTicket::__build($json->id, $json->label, $json->customerId, $json->custCount, $json->tariffAreaId, $json->discountProfileId, $json->discountRate);
             $lines = array();
             foreach ($json->lines as $jsLine) {
                 // Get line info
                 $tktLine = new SharedTicketLines($ticket->id, $jsLine->dispOrder, $jsLine->productId, $jsLine->taxId, $jsLine->quantity, $jsLine->discountRate, $jsLine->price, $jsLine->attributes);
                 $lines[] = $tktLine;
             }
             if (TicketsService::createSharedTicket($ticket, $lines) === false) {
                 $this->succeed(TicketsService::updateSharedTicket($ticket, $lines));
             } else {
                 $this->succeed(true);
             }
             break;
         case 'getOpen':
             $this->succeed(TicketsService::getOpen());
             break;
         case 'search':
             $this->succeed(TicketsService::search($this->getParam("ticketId"), $this->getParam("ticketType"), $this->getParam("cashId"), $this->getParam("dateStart"), $this->getParam("dateStop"), $this->getParam("customerId"), $this->getParam("userId"), $this->getParam("limit")));
             break;
         case 'delete':
             if (!TicketsService::delete($this->getParam("id"))) {
                 $this->fail(APIError::$ERR_GENERIC);
             } else {
                 $this->succeed(true);
             }
             break;
         case 'save':
             // Receive ticket data as json
             // Convert single ticket to array for consistency
             if (isset($this->params['tickets'])) {
                 $json = json_decode($this->params['tickets']);
             } else {
                 $json = array(json_decode($this->params['ticket']));
             }
             // Get location from cash register
             $cashId = $this->params['cashId'];
             $cashSrv = new CashesService();
             $cash = $cashSrv->get($cashId);
             if ($cash === null) {
                 $this->fail(new APIError("Unknown cash session"));
                 break;
             }
             $cashRegSrv = new CashRegistersService();
             $cashReg = $cashRegSrv->get($cash->cashRegisterId);
             if ($cashReg === null) {
                 $this->fail(new APIError("Cash register not found"));
                 break;
             }
             $locationId = $cashReg->locationId;
             if ($locationId === null) {
                 $this->fail(new APIError("Location not set"));
                 break;
             }
             // Register tickets
             $ticketsCount = count($json);
             $successes = 0;
             $pdo = PDOBuilder::getPDO();
             if (!$pdo->beginTransaction()) {
                 $this->fail(APIError::$ERR_GENERIC);
                 break;
             }
             foreach ($json as $jsonTkt) {
                 if ($jsonTkt === null) {
                     $this->fail(new APIError("Unable to decode ticket"));
                     break;
                 }
                 $ticketId = $jsonTkt->ticketId;
                 $userId = $jsonTkt->userId;
                 $customerId = $jsonTkt->customerId;
                 $date = $jsonTkt->date;
                 $tktType = $jsonTkt->type;
                 $custCount = $jsonTkt->custCount;
                 $tariffAreaId = $jsonTkt->tariffAreaId;
                 $discountRate = $jsonTkt->discountRate;
                 $discountProfileId = $jsonTkt->discountProfileId;
                 // Get lines
                 $lines = array();
                 foreach ($jsonTkt->lines as $jsLine) {
                     // Get line info
                     $number = $jsLine->dispOrder;
                     if (property_exists($jsLine, "productId")) {
                         $productId = $jsLine->productId;
                     } else {
                         $productId = null;
                     }
                     $productLabel = $jsLine->productLabel;
                     $quantity = $jsLine->quantity;
                     $price = $jsLine->price;
                     $taxId = $jsLine->taxId;
                     $lineDiscountRate = $jsLine->discountRate;
                     if ($jsLine->attributes !== null) {
                         $jsAttr = $jsLine->attributes;
                         $attrSetId = $jsAttr->attributeSetId;
                         $values = $jsAttr->values;
                         $desc = "";
                         foreach ($values as $val) {
                             $desc .= $val->value . ", ";
                         }
                         $desc = substr($desc, 0, -2);
                         $attrs = new AttributeSetInstance($attrSetId, $desc);
                         foreach ($values as $val) {
                             $attrVal = new AttributeInstance(null, $val->id, $val->value);
                             $attrs->addAttrInst($attrVal);
                         }
                         $attrsId = TicketsService::createAttrSetInst($attrs);
                         if ($attrsId === false) {
                             $this->fail(new APIError("Unknown attributes"));
                             break;
                         }
                     } else {
                         $attrsId = null;
                     }
                     $product = ProductsService::get($productId);
                     if ($product === null) {
                         $product = new Product($productId, $productId, $productId, null, null, null, $taxId, true, false);
                     }
                     $tax = TaxesService::getTax($taxId);
                     if ($tax == null) {
                         $this->fail(new APIError("Unknown tax"));
                         break;
                     }
                     $newLine = new TicketLine($number, $product, $attrsId, $quantity, $price, $tax, $lineDiscountRate, $productLabel);
                     $lines[] = $newLine;
                 }
                 if (count($lines) != count($jsonTkt->lines)) {
                     break;
                 }
                 // Get payments
                 $payments = array();
                 foreach ($jsonTkt->payments as $jspay) {
                     $type = $jspay->type;
                     $amount = $jspay->amount;
                     if (!property_exists($jspay, "currencyId") || $jspay->currencyId === null) {
                         $currSrv = new CurrenciesService();
                         $currencyId = $currSrv->getDefault()->id;
                         $currencyAmount = $amount;
                     } else {
                         $currencyId = $jspay->currencyId;
                         $currencyAmount = $jspay->currencyAmount;
                     }
                     $backType = null;
                     $backAmount = null;
                     if (property_exists($jspay, "back") && $jspay->back !== null) {
                         $backType = $jspay->back->type;
                         $backAmount = $jspay->back->amount;
                     }
                     $payment = new Payment($type, $amount, $currencyId, $currencyAmount, $backType, $backAmount);
                     $payments[] = $payment;
                 }
                 $ticket = new Ticket($tktType, $userId, $date, $lines, $payments, $cashId, $customerId, $custCount, $tariffAreaId, $discountRate, $discountProfileId);
                 $ticket->ticketId = $ticketId;
                 if (isset($jsonTkt->id)) {
                     // Ticket edit
                     $ticket->id = $jsonTkt->id;
                     //Check if cash is still opened
                     $oldTicket = TicketsService::get($id);
                     if ($oldTicket != null) {
                         $cashSrv = new CashesService();
                         $cash = $cashSrv->get($oldTicket->cashId);
                         if ($cash->isClosed()) {
                             $this->fail(new APIError("Cannot edit a ticket from " . "a closed cash"));
                             break;
                         }
                         // Delete the old ticket and recreate
                         if (TicketsService::delete($oldTicket->id) && TicketsService::save($ticket, $locationId)) {
                             $successes++;
                         } else {
                             $this->fail(new APIError("Unable to edit ticket"));
                             break;
                         }
                     }
                 } else {
                     // New ticket
                     if (TicketsService::save($ticket)) {
                         $successes++;
                     } else {
                         $this->fail(new APIError("Unable to save ticket"));
                         break;
                     }
                 }
             }
             // Check if all tickets were saved, if not rollback and error
             $ret = $successes == $ticketsCount;
             if ($ret === true) {
                 if ($pdo->commit()) {
                     $ticketId++;
                     $cashRegSrv->setNextTicketId($ticketId, $cash->cashRegisterId);
                     $this->succeed(array("saved" => $ticketsCount));
                 } else {
                     $this->fail(APIError::$ERR_GENERIC);
                 }
             } else {
                 $pdo->rollback();
                 if ($this->result === null) {
                     $this->fail(APIError::$ERR_GENERIC);
                 }
             }
             break;
     }
 }
コード例 #5
0
 /** @depends testAdd
  * @depends testGet
  */
 public function testUpdate()
 {
     $srv = new CashesService();
     $cash = $srv->add($this->cashRegisterId);
     // Edit open date
     $cash->openDate = stdtimefstr("2000-02-02 02:02:02");
     $cash->openCash = 10.0;
     $srv->update($cash);
     $read = $srv->get($cash->id);
     $this->assertNotNull($read, "Created cash not found");
     $this->assertEquals($cash->id, $read->id, "Id was modified");
     $this->assertEquals($cash->cashRegisterId, $read->cashRegisterId, "Cash register id was modified");
     $this->assertEquals($cash->sequence, $read->sequence, "Sequence was modified");
     $this->assertEquals($cash->openDate, $read->openDate, "Open date Mismatch");
     $this->assertEquals($cash->closeDate, $read->closeDate, "Close date was modified");
     $this->assertEquals($cash->openCash, $read->openCash, "Open cash mismatch");
     $this->assertEquals($cash->closeCash, $read->closeCash, "Close cash was modified");
     $this->assertEquals($cash->expectedCash, $read->expectedCash, "Expected cash was modified");
     // Edit close date
     $cash->closeDate = stdtimefstr("2000-02-03 02:02:02");
     $cash->closeCash = 12.0;
     $cash->expectedCash = 25.0;
     $srv->update($cash);
     $read = $srv->get($cash->id);
     $this->assertNotNull($read, "Created cash not found");
     $this->assertEquals($cash->id, $read->id, "Id was modified");
     $this->assertEquals($cash->cashRegisterId, $read->cashRegisterId, "Cash register id was modified");
     $this->assertEquals($cash->sequence, $read->sequence, "Sequence was modified");
     $this->assertEquals($cash->openDate, $read->openDate, "Open date was modified");
     $this->assertEquals($cash->closeDate, $read->closeDate, "Close date was modified");
     $this->assertEquals($cash->openCash, $read->openCash, "Open cash was modified");
     $this->assertEquals($cash->closeCash, $read->closeCash, "Close cash mismatch");
     $this->assertEquals($cash->expectedCash, $read->expectedCash, "Expected cash was modified");
     // Edit open and close date
     $cash->openDate = stdtimefstr("2001-02-02 03:03:03");
     $cash->closeDate = stdtimefstr("2001-02-03 03:03:03");
     $cash->openCash = 9.0;
     $cash->closeCash = 9.1;
     $cash->expectedCash = 9.199999999999999;
     $srv->update($cash);
     $read = $srv->get($cash->id);
     $this->assertNotNull($read, "Created cash not found");
     $this->assertEquals($cash->id, $read->id, "Id was modified");
     $this->assertEquals($cash->cashRegisterId, $read->cashRegisterId, "Cash register id was modified");
     $this->assertEquals($cash->sequence, $read->sequence, "Sequence was modified");
     $this->assertEquals($cash->openDate, $read->openDate, "Open date mismatch");
     $this->assertEquals($cash->closeDate, $read->closeDate, "Close date mismatch");
     $this->assertEquals($cash->openCash, $read->openCash, "Open cash mismatch");
     $this->assertEquals($cash->closeCash, $read->closeCash, "Close cash mismatch");
     $this->assertEquals($cash->expectedCash, $read->expectedCash, "Expected cash was modified");
 }