protected function proceed() { switch ($this->action) { case 'get': if (isset($this->params['id'])) { $this->succeed(ProductsService::get($this->params['id'])); } else { if (isset($this->params['reference'])) { $this->succeed(ProductsService::getByRef($this->params['reference'])); } else { $this->succeed(ProductsService::getByCode($this->params['code'])); } } break; case 'getAll': if (isset($this->params['all']) && in_array($this->params['all'], array(true, 1))) { $this->succeed(ProductsService::getAll(true)); } else { $this->succeed(ProductsService::getAll()); } break; case 'getCategory': $this->succeed(ProductsService::getByCategory($this->params['id'])); break; } }
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); }
protected function preRemoveProductHook() { if ($_GET['action'] !== 'remove') { return false; } $id = (int) $_GET['id']; return $this->productService->removeProduct($id); }
protected function setUp() { $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); $prd = new Product("REF", "product", 1.0, $cat->id, null, 1, $taxCat->id, true, false); $this->prdId = ProductsService::create($prd); }
/** Add 0 quantities to all on sales and missing products in inventory */ public function fillZero() { $prds = ProductsService::getAll(false); foreach ($this->items as $item) { foreach ($prds as $i => $prd) { if ($prd->id == $item->productId) { array_splice($prds, $i, 1); break; } } } // Registered products removed, add 0 items foreach ($prds as $prd) { $this->addItem(new InventoryItem($this->id, $prd->id, null, 0, 0, 0, null, null)); } }
protected function setUp() { $this->products = array(); $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); $prd = new Product("REF", "product", 1.0, $cat->id, null, 1, $taxCat->id, true, true, 0.3, null, "12345", false, true, 0.2); $prd->id = ProductsService::create($prd); $this->products[] = $prd; $prd2 = new Product("REF2", "product2", 2.0, $cat->id, null, 3, $taxCat->id, true, false); $prd2->id = ProductsService::create($prd2); $this->products[] = $prd2; }
protected function proceed() { switch ($this->action) { case 'getPrd': $this->result = ProductsService::getImage($this->params['id']); break; case 'getCat': $this->result = CategoriesService::getImage($this->params['id']); break; case 'getPM': $srv = new PaymentModesService(); $this->result = $srv->getImage($this->params['id']); break; case 'getRes': $this->result = ResourcesService::getImage($this->params['label']); break; } }
protected function setUp() { $this->areas = array(); $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); $prd = new Product("REF", "product", 1.0, $cat->id, null, 1, $taxCat->id, true, false); $prd->id = ProductsService::create($prd); $area = new TariffArea("Area", 1); $area->addPrice($prd->id, 0.8); $srv = new TariffAreasService(); $area->id = $srv->create($area); $area2 = new TariffArea("Area51", 51); $area2->id = $srv->create($area2); $this->areas[] = $area; $this->areas[] = $area2; }
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); }
protected function setUp() { $this->products = array(); $this->compositions = array(); // Setup tax and categories $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); $pdo = PDOBuilder::getPDO(); $id = CompositionsService::CAT_ID; $catCmp = new Category(null, "Compositions", false, 1); $sql = "INSERT INTO CATEGORIES (ID, NAME, PARENTID, DISPORDER, IMAGE) " . "VALUES (:id, :name, :pid, :order, null)"; $stmt = $pdo->prepare($sql); $stmt->bindParam(":name", $catCmp->label, \PDO::PARAM_STR); $stmt->bindParam(":pid", $catCmp->parentId, \PDO::PARAM_INT); $stmt->bindParam(":id", $id, \PDO::PARAM_INT); $stmt->bindParam(":order", $catCmp->dispOrder, \PDO::PARAM_INT); $stmt->execute(); $cat = new Category(null, "Category", false, 2); $cat->id = CategoriesService::createCat($cat); // Set up products $prd = new Product("REF", "product", 1.0, $cat->id, null, 1, $taxCat->id, true, true, 0.3, null, "12345", false, true, 0.2); $prd->id = ProductsService::create($prd, null); $this->products[] = $prd; $prd2 = new Product("REF2", "product2", 2.0, $cat->id, null, 3, $taxCat->id, true, false); $prd2->id = ProductsService::create($prd2, null); $this->products[] = $prd2; $cmp = new Composition("CMP", "composition", 1.0, $id, 1, $taxCat->id, true, true, 0.3, null, "12345", false, true, 0.2); $subgrp = new Subgroup(null, "Subgroup", 1, false); $subgrp->addProduct(new SubgroupProduct($prd->id, null, 1)); $subgrp->addProduct(new SubgroupProduct($prd2->id, null, 2)); $cmp->addGroup($subgrp); $cmp->id = CompositionsService::create($cmp, null, null); $this->compositions[] = $cmp; }
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); }
public function createAction() { return ProductsService::create($this->request->getRawBody()); }
if (isset($font)) { $box = imagettfbbox($fontSize, 0, $font, $data['hri']); $len = $box[2] - $box[0]; \Barcode::rotate(-$len / 2, $data['height'] / 2 + $fontSize + $marge, $angle, $xt, $yt); imagettftext($im, $fontSize, $angle, $x + $xt, $y + $yt, $black, $font, $data['hri']); } header('Content-type: image/gif'); imagegif($im); imagedestroy($im); } switch ($_GET['w']) { case 'product': if (isset($_GET['id'])) { $prd = ProductsService::get($_GET['id']); if ($prd !== null && $prd->hasImage !== false) { echo ProductsService::getImage($prd->id); break; } } echo file_get_contents(PT::$ABSPATH . "/templates/" . $config['template'] . "/img/default_product.png"); break; case 'provider': if (isset($_GET['id'])) { $prov = ProvidersService::get($_GET['id']); if ($prov !== null && $prov->hasImage !== false) { $img = ProvidersService::getImage($prov->id); break; } } echo file_get_contents(PT::$ABSPATH . "/templates/" . $config['template'] . "/img/default_provider.png"); break;
static function create($composition, $img, $groupImgs) { $pdo = PDOBuilder::getPDO(); $newTransaction = !$pdo->inTransaction(); if ($newTransaction) { $pdo->beginTransaction(); } // Create product $id = ProductsService::create($composition, $img); if ($id === false) { if ($newTransaction) { $pdo->rollback(); } return false; } // Create subgroups $srv = new SubgroupsService($id); if (!$srv->create($composition->groups, $groupImgs)) { if ($newTransaction) { $pdo->rollback(); } return false; } else { if ($newTransaction) { $pdo->commit(); } return $id; } }
static function get($id) { $pdo = PDOBuilder::getPDO(); $stmt = $pdo->prepare("SELECT * FROM PRODUCTS LEFT JOIN PRODUCTS_CAT " . "ON PRODUCTS_CAT.PRODUCT = PRODUCTS.ID WHERE ID = :id"); if ($stmt->execute(array(':id' => $id))) { if ($row = $stmt->fetch()) { $prd = ProductsService::buildDBPrd($row, $pdo); return $prd; } } return null; }
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; }
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; } }