protected function setUp() { $this->currencies = array(); $srv = new CurrenciesService(); $euro = new Currency("Euro", "€", ",", ".", "#,##0.00\$", 1, true, true); $doll = new Currency("Dollar", "\$", ".", ",", "\$0#,##0.00", 1.25, false, true); $yen = new Currency("Yen", "Y", " ", " ", "#\$", 120, false, false); $euro->id = $srv->create($euro); $doll->id = $srv->create($doll); $yen->id = $srv->create($yen); $this->currencies[] = $euro; $this->currencies[] = $doll; $this->currencies[] = $yen; }
protected function proceed() { $srv = new CurrenciesService(); switch ($this->action) { case 'get': $this->succeed($srv->get($this->params['id'])); break; case 'getMain': $main = $srv->getDefault(); $this->succeed($main); break; case 'getAll': $this->succeed($srv->getAll()); break; } }
protected function setUp() { $locSrv = new LocationsService(); $location = new Location("Location"); $location->id = $locSrv->create($location); $cashRegSrv = new CashRegistersService(); $cashReg = new CashRegister("CashReg", $location->id, 1); $cashRegId = $cashRegSrv->create($cashReg); // Create a cash session $srv = new CashesService(); $cash = $srv->add($cashRegId); $this->cashId = $cash->id; $srv = new CurrenciesService(); $eur = new Currency("Eur", "€", ",", ".", "#,##0.00\$", 1, true, false); $this->currencyId = $srv->create($eur); }
static function save($ticket) { $pdo = PDOBuilder::getPDO(); $db = DB::get(); $cashRegSrv = new CashRegistersService(); $cashReg = $cashRegSrv->getFromCashId($ticket->cashId); if ($cashReg === null) { return false; } $locationId = $cashReg->locationId; $newTransaction = !$pdo->inTransaction(); if ($newTransaction) { $pdo->beginTransaction(); } $id = md5(time() . rand()); $stmtRcpt = $pdo->prepare("INSERT INTO RECEIPTS\t(ID, MONEY, DATENEW) " . "VALUES (:id, :money, :date)"); $stmtRcpt->bindParam(":id", $id); $stmtRcpt->bindParam(":money", $ticket->cashId); $stmtRcpt->bindParam(":date", $db->dateVal($ticket->date)); if ($stmtRcpt->execute() === false) { if ($newTransaction) { $pdo->rollback(); } return false; } // Get next ticket number if ($ticket->ticketId === null) { $nextNum = $cashReg->nextTicketId; $cashRegSrv->incrementNextTicketId($cashReg->id); $ticket->ticketId = $nextNum; } // Insert ticket $discountRate = $ticket->discountRate; $stmtTkt = $pdo->prepare("INSERT INTO TICKETS (ID, TICKETID, " . "TICKETTYPE, PERSON, CUSTOMER, CUSTCOUNT, TARIFFAREA, " . "DISCOUNTRATE, DISCOUNTPROFILE_ID) VALUES " . "(:id, :tktId, :tktType, :person, :cust, :custcount, :taId, " . ":discRate, :discProfId)"); $stmtTkt->bindParam(':id', $id, \PDO::PARAM_STR); $stmtTkt->bindParam(':tktId', $ticket->ticketId, \PDO::PARAM_INT); $stmtTkt->bindParam(":tktType", $ticket->type); $stmtTkt->bindParam(':person', $ticket->userId); $stmtTkt->bindParam(':cust', $ticket->customerId); $stmtTkt->bindParam(":custcount", $ticket->custCount); $stmtTkt->bindParam(":taId", $ticket->tariffAreaId); $stmtTkt->bindParam(":discRate", $ticket->discountRate); $stmtTkt->bindParam(":discProfId", $ticket->discountProfileId); if ($stmtTkt->execute() === false) { if ($newTransaction) { $pdo->rollback(); } return false; } // Insert ticket lines // Also check for prepayments refill $stmtLines = $pdo->prepare("INSERT INTO TICKETLINES (TICKET, LINE, " . "PRODUCT, ATTRIBUTESETINSTANCE_ID, UNITS, PRICE, TAXID, " . "DISCOUNTRATE, ATTRIBUTES) VALUES (:id, :line, :prdId, " . ":attrSetInstId, :qty, :price, :taxId, :discRate, :attrs)"); foreach ($ticket->lines as $line) { $fullDiscount = $discountRate + $line->discountRate; $discountPrice = $line->price * (1.0 - $fullDiscount); $stmtLines->bindParam(":id", $id); $stmtLines->bindParam(":line", $line->dispOrder); $stmtLines->bindParam(":prdId", $line->productId); $stmtLines->bindParam(":attrSetInstId", $line->attrSetInstId); $stmtLines->bindParam(":qty", $line->quantity); $stmtLines->bindParam(":price", $line->price); $stmtLines->bindParam(":taxId", $line->taxId); $stmtLines->bindParam(":discRate", $line->discountRate); $stmtLines->bindParam(":attrs", $line->attributes); if ($stmtLines->execute() === false) { if ($newTransaction) { $pdo->rollback(); } return false; } // Update stock if ($line->productId !== null) { $move = new StockMove($ticket->date, StockMove::REASON_OUT_SELL, $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; } } } // Insert payments // Also check for prepayment debit and debt recovery $stmtPay = $pdo->prepare("INSERT INTO PAYMENTS (ID, RECEIPT, PAYMENT, " . "TOTAL, CURRENCY, TOTALCURRENCY, PAIRED_WITH) VALUES " . "(:id, :rcptId, :type, :amount, :currId, :currAmount, " . ":pair)"); foreach ($ticket->payments as $payment) { $paymentId = md5(time() . rand()); $stmtPay->bindParam(":id", $paymentId); $stmtPay->bindParam(":rcptId", $id); $stmtPay->bindParam(":type", $payment->type); $stmtPay->bindParam(":amount", $payment->amount); $stmtPay->bindParam(":currId", $payment->currencyId); $stmtPay->bindParam(":currAmount", $payment->currencyAmount); $stmtPay->bindValue(":pair", null); if ($stmtPay->execute() === false) { if ($newTransaction) { $pdo->rollback(); } return false; } // Insert back payment if any if ($payment->backType !== null) { $backId = md5(time() . rand()); $currSrv = new CurrenciesService(); $defaultCurrencyId = $currSrv->getDefault()->id; $stmtPay->bindParam(":id", $backId); $stmtPay->bindParam(":type", $payment->backType); $stmtPay->bindParam(":amount", $payment->backAmount); $stmtPay->bindParam(":currId", $defaultCurrencyId); $stmtPay->bindParam(":currAmount", $payment->backAmount); $stmtPay->bindValue(":pair", $paymentId); // :rcptId is already bound if ($stmtPay->execute() === false) { if ($newTransaction) { $pdo->rollback(); } return false; } } // Check prepaid if ($payment->type == 'prepaid') { $custSrv = new CustomersService(); $ok = $custSrv->addPrepaid($ticket->customerId, $payment->amount * -1); if ($ok === false) { if ($newTransaction) { $pdo->rollback(); } return false; } } else { if ($payment->type == "debt") { // Debtpaid is a negative total of all payments $custSrv = new CustomersService(); $ok = $custSrv->addDebt($ticket->customerId, $payment->amount, $ticket->date); if ($ok === false) { if ($newTransaction) { $pdo->rollback(); } return false; } } else { if ($payment->type == "debtpaid") { // Debtpaid is a negative total of all payments $custSrv = new CustomersService(); $ok = $custSrv->recoverDebt($ticket->customerId, $payment->amount * -1); if ($ok === false) { if ($newTransaction) { $pdo->rollback(); } return false; } } } } } // Insert taxlines $stmtTax = $pdo->prepare("INSERT INTO TAXLINES (ID, RECEIPT, TAXID, " . "BASE, AMOUNT) VALUES (:id, :rcptId, " . ":taxId, :base, :amount)"); foreach ($ticket->getTaxAmounts() as $ta) { $taxId = md5(time() . rand()); $stmtTax->bindParam(":id", $taxId); $stmtTax->bindParam(":rcptId", $id); $stmtTax->bindParam(":taxId", $ta->taxId); $stmtTax->bindParam(":base", $ta->base); $stmtTax->bindParam(":amount", $ta->getAmount()); if ($stmtTax->execute() === false) { if ($newTransaction) { $pdo->rollback(); } return false; } } if ($newTransaction) { $pdo->commit(); } return $id; }
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 testUpdate */ public function testUpdateMain() { $srv = new CurrenciesService(); $eur = new Currency("Eur", "€", ",", ".", "#,##0.00\$", 1, true, true); $eur->id = $srv->create($eur); $yen = new Currency("Yen", "Y", " ", " ", "#\$", 120, false, false); $yen->id = $srv->create($yen); $yen->isMain = true; $this->assertTrue($srv->update($yen)); $eurRead = $srv->get($eur->id); $yenRead = $srv->get($yen->id); $this->assertTrue($yenRead->isMain, "Main not set"); $this->assertFalse($eurRead->isMain, "Old main not unset"); }
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; } }
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); $this->attr = $attr; $set->addAttribute($attr); $set->id = AttributesService::createSet($set); $this->attrSet = $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); $this->tax = $taxCat->taxes[0]; $this->tax2 = $taxCat2->taxes[0]; $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); $this->prd = $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); $this->prd = $prd; $this->prd2 = $prd2; $this->prdRefill = $prdRefill; // Tariff area $srvArea = new TariffAreasService(); $area = new TariffArea("area", 1); $area->addPrice($this->prd->id, 0.8); $area->id = $srvArea->create($area); $this->area = $area; // Discount profile $srvProfile = new DiscountProfilesService(); $profile = new DiscountProfile("Discount profile", 1.0); $profile->id = $srvProfile->create($profile); $this->discountProfile = $profile; // 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->customer = $cust; // Location $locSrv = new LocationsService(); $loc = new Location("Location"); $loc->id = $locSrv->create($loc); $this->location = $loc; // Cash $srvCashReg = new CashRegistersService(); $cashReg = new CashRegister("CashReg", $loc->id, 1); $cashReg->id = $srvCashReg->create($cashReg); $srvCash = new CashesService(); $cash = $srvCash->add($cashReg->id); $cash->openDate = stdtimefstr("2000-02-02 02:02:02"); $srvCash->update($cash); $this->cash = $cash; // User $srvUsers = new UsersService(); $user = new User("User", null, null, "0", true, false); $user->id = $srvUsers->create($user); $this->user = $user; // Currency $curr = new Currency("Eur", "€", ",", ".", "#,##0.00\$", 1, true, false); $srvCurr = new CurrenciesService(); $curr->id = $srvCurr->create($curr); $this->currency = $curr; }
public function currency($amount) { $srv = new CurrenciesService(); $currency = $srv->getDefault(); return $currency->format($amount); }