Ejemplo n.º 1
0
 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;
     }
 }
Ejemplo n.º 2
0
 /** @depends testSaveLine */
 function testSaveMultiLines()
 {
     $date = stdtimefstr("2013-01-01 00:00:00");
     $attrSetInst = new AttributeSetInstance($this->attrSet->id, "Value");
     $attrsId = TicketsService::createAttrSetInst($attrSetInst);
     $line1 = new TicketLine(1, $this->prd, $attrSetInst->id, 1, 12, $this->tax);
     $line2 = new TicketLine(2, $this->prd2, null, 2, 10, $this->tax2);
     $line3 = new TicketLine(3, $this->prd, null, 1.5, 10, $this->tax);
     $payment1 = new Payment("cash", 12, $this->currency->id, 14);
     $payment2 = new Payment("cheque", 25, $this->currency->id, 20);
     $ticket = new Ticket(Ticket::TYPE_SELL, $this->user->id, $date, array($line1, $line2, $line3), array($payment1, $payment2), $this->cash->id, null, null);
     $id = TicketsService::save($ticket, $this->location->id);
     $pdo = PDOBuilder::getPDO();
     $db = DB::get();
     // Check sale lines
     $stmtLines = $pdo->prepare("SELECT * FROM TICKETLINES");
     $this->assertNotEquals($stmtLines->execute(), false, "Ticket query failed");
     $toCheck = array($line1, $line2, $line3);
     $count = 0;
     while ($row = $stmtLines->fetch()) {
         $ref = null;
         $count++;
         if ($row['LINE'] == 1) {
             $ref = $line1;
         } else {
             if ($row['LINE'] == 2) {
                 $ref = $line2;
             } else {
                 if ($row['LINE'] == 3) {
                     $ref = $line3;
                 }
             }
         }
         $this->assertNotNull($ref, "Unknown line");
         $this->checkSaleEquality($id, $ref, $row);
         for ($i = 0; $i < count($toCheck); $i++) {
             $l = $toCheck[$i];
             if ($l->dispOrder == $ref->dispOrder) {
                 array_splice($toCheck, $i, 1);
                 break;
             }
         }
     }
     $this->assertEquals(3, $count, "Sale line count mismatch");
     $this->assertEquals(0, count($toCheck), "Duplicated sale lines");
     // Check tax lines
     $stmtTax = $pdo->prepare("SELECT * FROM TAXLINES");
     $this->assertNotEquals($stmtTax->execute(), false, "Tax lines query failed");
     $toCheck = array($this->tax->id, $this->tax2->id);
     for ($i = 0; $i < 2; $i++) {
         $row = $stmtTax->fetch();
         $this->assertNotEquals(false, $row, "Not enough tax line found");
         $ref = null;
         if ($row['TAXID'] == $this->tax->id) {
             $ref = $this->tax->id;
             $this->checkTaxEquality($id, $ref, 27, 2.7, $row);
         } else {
             if ($row['TAXID'] == $this->tax2->id) {
                 $ref = $this->tax2->id;
                 $this->checkTaxEquality($id, $ref, 20, 4, $row);
             }
         }
         $this->assertNotNull($ref, "Unknown tax line");
         foreach ($toCheck as $j => $taxId) {
             if ($ref == $taxId) {
                 array_splice($toCheck, $j, 1);
                 break;
             }
         }
     }
     $this->assertEquals(0, count($toCheck), "Duplicated tax lines");
     $row = $stmtTax->fetch();
     $this->assertFalse($row, "Too much tax lines found");
     // Check payment lines
     $toCheck = array($payment1, $payment2);
     $stmtPmt = $pdo->prepare("SELECT * FROM PAYMENTS");
     $this->assertNotEquals($stmtPmt->execute(), false, "Payment lines query failed");
     $count = 0;
     while ($row = $stmtPmt->fetch()) {
         $ref = null;
         $count++;
         if ($row['PAYMENT'] == "cash") {
             $ref = $payment1;
         } else {
             if ($row['PAYMENT'] == "cheque") {
                 $ref = $payment2;
             }
         }
         $this->assertNotNull($ref, "Unknown line");
         $this->checkPaymentEquality($id, $ref, $row);
         foreach ($toCheck as $i => $pmt) {
             if ($pmt->type == $ref->type) {
                 array_splice($toCheck, $i, 1);
                 break;
             }
         }
     }
     $this->assertEquals(2, $count, "Payment line count mismatch");
     $this->assertEquals(0, count($toCheck), "Duplicated payment lines");
     // Check stocks
     $level = StocksService::getLevel($this->prd->id, $this->location->id, null);
     $this->assertEquals(-1.5, $level->qty);
     $level = StocksService::getLevel($this->prd2->id, $this->location->id, null);
     $this->assertEquals(-2, $level->qty);
     $this->markTestIncomplete("Check stock level with attribute");
 }