Esempio n. 1
0
 protected function setUp()
 {
     // Products
     $taxCat = new TaxCat("Tax");
     $tax = new Tax(null, "Tax", stdtimefstr("2001-01-01 00:00:00"), 0.1);
     $taxCat->addTax($tax);
     $taxCat->id = TaxesService::createCat($taxCat);
     $cat = new Category(null, "Category", false, 1);
     $cat->id = CategoriesService::createCat($cat);
     $prd1 = new Product("REF", "product", 1.0, $cat->id, null, 1, $taxCat->id, true, true, 0.3, null, "12345", false, true, 0.2);
     $prd1->id = ProductsService::create($prd1);
     $prd2 = new Product("REF2", "product2", 2.0, $cat->id, null, 3, $taxCat->id, true, false);
     $prd2->id = ProductsService::create($prd2);
     $this->products = array($prd1, $prd2);
     // Locations
     $locSrv = new LocationsService();
     $loc1 = new Location("Location1");
     $loc1->id = $locSrv->create($loc1);
     $loc2 = new Location("Location2");
     $loc2->id = $locSrv->create($loc2);
     $this->locations = array($loc1, $loc2);
     // Stocks
     $lvl11 = new StockLevel($prd1->id, $loc1->id, null, null, 10);
     $lvl11->id = StocksService::createLevel($lvl11);
     $lvl12 = new StockLevel($prd1->id, $loc2->id, null, 5.2, null);
     $lvl12->id = StocksService::createLevel($lvl12);
     $lvl21 = new StockLevel($prd2->id, $loc1->id, null, 7, 20);
     $lvl21->id = StocksService::createLevel($lvl21);
     $move = new StockMove(stdtimefstr("2014-01-01 00:00:00"), StockMove::REASON_IN_BUY, $loc1->id, $prd1->id, null, 3.5, 2.1);
     StocksService::addMove($move);
     $this->levels = array($lvl11, $lvl12, $lvl21);
 }
Esempio n. 2
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;
 }
 /** @depends testCreateMoveBuy
  * @depends testReadLevel
  */
 public function testReadQty()
 {
     $level = new StockLevel($this->products[0]->id, $this->locations[0]->id, null, 1.2, 15.6, 3.5);
     // Quantity is set matching to move
     $level->id = StocksService::createLevel($level);
     $move = new StockMove(stdtimefstr("2014-01-01 00:00:00"), StockMove::REASON_IN_BUY, $level->productId, $level->locationId, $level->attrSetInstId, $level->qty, 10);
     $move->id = StocksService::addMove($move);
     $read = StocksService::getLevel($move->productId, $move->locationId, $move->attrSetInstId);
     $this->checkEquality($read, $level);
 }
 /** @depends testCreateFull */
 public function testCreateGuessMissing()
 {
     $move = new StockMove(stdtimefstr("2014-01-01 00:00:00"), StockMove::REASON_IN_BUY, $this->products[0]->id, $this->locations[0]->id, null, 10, 1);
     $move->id = StocksService::addMove($move);
     $item = new InventoryItem(null, $this->products[0]->id, null, 1, 2, 3, null, 5);
     $inv = new Inventory(stdtimefstr("2001-01-01 00:00:00"), $this->locations[0]->id);
     $inv->addItem($item);
     $srv = new InventoriesService();
     $id = $srv->create($inv);
     $this->assertNotEquals(false, $id, "Creation failed");
     $pdo = PDOBuilder::getPDO();
     $stmt = $pdo->prepare("SELECT * FROM STOCK_INVENTORYITEM");
     $this->assertNotEquals(false, $stmt->execute(), "Query failed");
     if ($row = $stmt->fetch()) {
         $this->assertEquals(4, $row["MISSINGQTY"]);
         $this->markTestIncomplete("Check unit value");
     } else {
         $this->assertTrue(false, "No inventory item found after creation");
     }
 }