function getAll($include_hidden = false) { $customers = array(); $pdo = PDOBuilder::getPDO(); $db = DB::get(); $sql = null; if ($include_hidden) { $sql = "SELECT * FROM CUSTOMERS"; } else { $sql = "SELECT * FROM CUSTOMERS WHERE (EXPIREDATE IS NULL OR EXPIREDATE > NOW()) AND VISIBLE = " . $db->true(); } $sql .= " ORDER BY NAME ASC"; foreach ($pdo->query($sql) as $dbCust) { $cust = CustomersService::build($dbCust); $customers[] = $cust; } return $customers; }
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; }
protected function setUp() { // Attribute set $set = new AttributeSet("set"); $attr = new Attribute("attr", 1); $val = new AttributeValue("value"); $attr->id = AttributesService::createAttribute($attr); $val->id = AttributesService::createValue($val, $attr->id); $attr->addValue($val); $set->addAttribute($attr); $set->id = AttributesService::createSet($set); // Product, tax and category $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); $taxCat2 = new TaxCat("Tax2"); $tax2 = new Tax(null, "Tax2", stdtimefstr("2001-01-01 00:00:00"), 0.2); $taxCat2->addTax($tax2); $taxCat2->id = TaxesService::createCat($taxCat2); $pdo = PDOBuilder::getPDO(); $stmt = $pdo->prepare("INSERT INTO CATEGORIES (ID, NAME) " . "VALUES (:id, :name)"); $stmt->execute(array(":id" => "-1", ":name" => "Refill")); $cat = new Category(null, "Category", false, 1); $cat->id = CategoriesService::createCat($cat); $prd = new Product("REF", "product", 1.0, $cat->id, null, 1, $taxCat->id, true, false, 0.5, $set->id); $prd->id = ProductsService::create($prd); $prd2 = new Product("REF2", "product2", 2.0, $cat->id, null, 1, $taxCat2->id, true, false, 0.5, null); $prd2->id = ProductsService::create($prd2); $prdRefill = new Product("REFILL", "Refill", 1.0, "-1", null, 1, $taxCat->id, true, false); $prdRefill->id = ProductsService::create($prdRefill); // Tariff area $srvArea = new TariffAreasService(); $area = new TariffArea("area", 1); $area->addPrice($prd->id, 0.8); $area->id = $srvArea->create($area); $this->areaId = $area->id; // Customer $srvCust = new CustomersService(); $cust = new Customer(1, "Cust", "It's me", "card", null, null, 50.0, 10.0, 5.0, stdtimefstr("2012-01-01 00:00:00"), "It's", "me", "*****@*****.**", "012345", "23456", "11111", "Address1", "Address2", "59000", "City", "Region", "France", "Note", true); $cust->id = $srvCust->create($cust); $this->custId = $cust->id; // Location $locSrv = new LocationsService(); $loc = new Location("Location"); $loc->id = $locSrv->create($loc); $this->locationId = $loc->id; // Cash register $srvCashReg = new CashRegistersService(); $cashReg = new CashRegister("Cash", $loc->id, 1); $cashReg->id = $srvCashReg->create($cashReg); // Cash $srvCash = new CashesService(); $cash = $srvCash->add($cashReg->id); $cash->openDate = stdtimefstr("2000-02-02 02:02:02"); $srvCash->update($cash); $this->cashId = $cash->id; // User $srvUsers = new UsersService(); $user = new User("User", null, null, "0", true, false); $user->id = $srvUsers->create($user); // Currency $curr = new Currency("Eur", "€", ",", ".", "#,##0.00\$", 1, true, false); $srvCurr = new CurrenciesService(); $curr->id = $srvCurr->create($curr); // Discount profile $profSrv = new DiscountProfilesService(); $prof = new DiscountProfile("Profile", 0.1); $prof->id = $profSrv->create($prof); $this->discountProfilId = $prof->id; // Ticket $tkt1 = array("date" => stdtimefstr("2012-01-01 00:00:00"), "userId" => $user->id, "customerId" => null, "type" => Ticket::TYPE_SELL, "custCount" => 3, "tariffAreaId" => null, "discountRate" => 0.0, "discountProfileId" => null, "payments" => array(array("type" => "cash", "amount" => 10, "currencyId" => $curr->id, "currencyAmount" => 12)), "lines" => array(array("dispOrder" => 1, "productId" => $prd->id, "taxId" => $tax->id, "attributes" => null, "quantity" => 1.0, "price" => 10.0, "discountRate" => 0.0))); $jsAttr = array("attributeSetId" => $set->id, "values" => array(array("id" => $attr->id, "value" => "value"))); $tkt2 = array("date" => stdtimefstr("2012-01-01 00:00:00"), "userId" => $user->id, "customerId" => null, "type" => Ticket::TYPE_SELL, "custCount" => 3, "tariffAreaId" => null, "discountRate" => 0.25, "discountProfileId" => $prof->id, "payments" => array(array("type" => "cash", "amount" => 10, "currencyId" => $curr->id, "currencyAmount" => 12)), "lines" => array(array("dispOrder" => 1, "productId" => $prd->id, "taxId" => $tax->id, "attributes" => $jsAttr, "quantity" => 1.0, "price" => 10.0, "discountRate" => 0.25))); $this->jsTicket1 = json_encode($tkt1); $this->jsTicket2 = json_encode($tkt2); }
/** @depends testGet * @depends testSaveDebt */ public function testDeleteDebt() { $currDebt = $this->customer->currDebt; $date = stdtimefstr("2013-01-01 00:00:00"); $line = new TicketLine(1, $this->prd, null, 1, 10, $this->tax); $payment = new Payment("debt", 3, $this->currency->id, 3); $ticket = new Ticket(Ticket::TYPE_SELL, $this->user->id, $date, array($line), array($payment), $this->cash->id, $this->customer->id); $id = TicketsService::save($ticket, $this->location->id); $this->assertTrue(TicketsService::delete($id), "Delete failed"); $custSrv = new CustomersService(); $cust = $custSrv->get($this->customer->id); $this->assertEquals($currDebt, $cust->currDebt, "Debt not cleared after ticket is deleted"); }
public function testCreate() { $broker = new APIBroker(CustomersAPITest::API); $srv = new CustomersService(); $cust = new Customer(null, "Cust", "It's me", "card", null, null, 12.0, 10.0, 5.0, stdtimefstr("2012-01-01 00:00:00"), "It's", "me", "*****@*****.**", "012345", "23456", "11111", "Address1", "Address2", "59000", "City", "Region", "France", "Note", true); unset($cust->id); $result = $broker->run("save", array("customer" => json_encode($cust))); $this->assertEquals(APIResult::STATUS_CALL_OK, $result->status, "Result status check failed"); $content = $result->content; $id = $content['saved'][0]; $cust->id = $id; $this->assertNotNull($content, "Result not found"); $read = $srv->get($id); $this->checkCustEquality($cust, $read); }
protected function proceed() { $srv = new CustomersService(); switch ($this->action) { case 'get': $this->succeed($srv->get($this->params['id'])); break; case 'getAll': $this->succeed($srv->getAll()); break; case 'addPrepaid': $this->succeed($srv->addPrepaid($this->params['id'], $this->params['amount'])); break; case 'getTop': $this->succeed($srv->getTop($this->getParam("limit"))); break; case 'save': // Convert single customer to array for consistency if ($this->isParamSet('customers')) { $json = json_decode($this->params['customers']); } else { $json = array(json_decode($this->params['customer'])); } $srv = new CustomersService(); $result = array(); // Begin transaction $pdo = PDOBuilder::getPDO(); if (!$pdo->beginTransaction()) { $this->fail(APIError::$ERR_GENERIC); break; } foreach ($json as $customer) { if (isset($customer->id) && $customer->id !== null) { // Edit if (!$srv->update($customer)) { // Error, rollback $pdo->rollback(); $this->fail(APIError::$ERR_GENERIC); return; } else { $result[] = $customer->id; } } else { // Create $id = $srv->create($customer); if ($id !== false) { $result[] = $id; } else { // Error, rollback $pdo->rollback(); $this->fail(APIError::$ERR_GENERIC); return; } } } // Success, commit if ($pdo->commit()) { $this->succeed(array("saved" => $result)); } else { $this->fail(APIError::$ERR_GENERIC); } break; } }
public function testDeleteInexistent() { // TODO: is this behaviour a feature? $srv = new CustomersService(); $this->assertTrue($srv->delete(0)); }