Overview

Namespaces

  • App
    • Components
    • Interfaces
    • Lib
      • Files
      • Forms
        • Controls
      • Html
      • Repair
      • Statics
    • Model
    • Modules
      • BaseModule
        • Presenters
      • PartnersModule
        • Presenters
      • ProductModule
        • Presenters
      • SaleModule
        • Model
        • Presenters
      • SettingsModule
        • Model
        • Presenters
      • StoreModule
        • Model
        • Presenters
    • Presenters
    • Router
  • PHP

Classes

  • DateTime
  • SplFileInfo

Interfaces

  • ArrayAccess
  • Countable
  • Exception
  • Iterator
  • Traversable
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Download
  1: <?php
  2: 
  3: /**
  4:  * Project sberp
  5:  *
  6:  * @file Counter 
  7:  * @author Jaromír Polášek
  8:  * verze: 0.9.1.AFTERMATH
  9:  * Encoding UTF-8
 10:  */
 11: 
 12: namespace App\Model;
 13: 
 14: use Nette\Utils\Strings,
 15:     App\Model\Database,
 16:     App\Lib\Container,
 17:     App\Lib\Table,
 18:     App\Lib\Statics\Cons;
 19: 
 20: /**
 21:  * Dvou úrovňové počítadlo
 22:  * - Prefix.počítadlo1.surfix /5 pozic
 23:  * -- Prefix.počídatlo1.surfix.-.počítadlo2 /2 pozice
 24:  */
 25: class Counter extends Container {
 26: 
 27:     const COUNTER = 'counter';
 28: 
 29:     /** @var App\Lib\Table */
 30:     private $seq;
 31: 
 32:     /**
 33:      * Konstrukto vytváří instanci tabulky counter
 34:      * @param \App\Model\Database $database
 35:      */
 36:     public function __construct(Database $database) {
 37:     parent::__construct(array());
 38:     $this->seq = new Table(self::COUNTER, $database);
 39:     }
 40: 
 41:     /**
 42:      * Uloží aktuální stavy počítadel *
 43:      */
 44:     public function save($sale = FALSE) {
 45:     foreach ($this->container as $counter) {
 46:         if (isset($counter['id'])) {
 47:         $this->seq->update($counter['id'],$counter);
 48:         } else if ($sale == FALSE){
 49:         $this->seq->insert($counter);
 50:         }
 51:     }
 52:     }    
 53: 
 54:     /**
 55:      * Vrací aktuální stav počítadla
 56:      * @param string $name
 57:      * @return string
 58:      */
 59:     public function setCounters($name, $edit = NULL) {
 60:     if ($edit === NULL) {
 61:         $this->createSeq($name);
 62:     }else{
 63:         if (strpos($edit,'-') === FALSE ){
 64:         $this->editSeq($name);
 65:         }else{
 66:         $this->editSubString($name, $edit);
 67:         }
 68:     }
 69:     return $this->container;
 70:     }
 71: 
 72:     /**
 73:      * Vrací název počítadla
 74:      * @param array $row
 75:      * @return string
 76:      */
 77:     public function getName($row) {
 78:     return $row['prefix'] . $this->getCounterNumber($row['counter']) . $row['surfix'];
 79:     }
 80: 
 81:     /**
 82:      * Vrací název sub počítadla
 83:      * @param array $row
 84:      * @return string
 85:      */
 86:     public function getSubName($row) {
 87:     return $row['prefix'] .'-'. Strings::padLeft((string) $row['counter'], 2, '0') . $row['surfix'];
 88:     }
 89:     /**
 90:      * Vrátí hodnotu počítadla jako string o délce 8 znaků
 91:      * @param int $number
 92:      * @return string
 93:      */
 94:     private function getCounterNumber($number) {
 95:     return Strings::padLeft((string) $number, 5, '0');
 96:     }
 97: 
 98:     /**
 99:      * Povýší počítadlo o +1 a nastaví sub počítadlo na 0
100:      * @param string $name
101:      */
102:     private function createSeq($name) {
103:     $sequence = $this->getSeq($name);
104:     if ($sequence !== FALSE) {
105:         $sequence['counter'] ++;
106:         $this->container[] = $sequence;
107:         $this->container[] = array(
108:         'prefix' => $this->getName($sequence),
109:         'counter' => 0,
110:         'parent_id' => $sequence['id']
111:         );
112:     }
113:     }
114: 
115:     /**
116:      * Najde požadované počítadlo a nastaví sub počítadlo na další dodnotu
117:      * @param string $name název požítadla
118:      * @param string $edit editovaná položka
119:      */
120:     private function editSeq($name) {
121:     $sequence = $this->getSeq($name, FALSE);
122:     if ($sequence !== FALSE) {     
123:         $this->container[] = $sequence;
124:         $subSequence = $this->getSubSeq($this->getName($sequence));
125:         $subSequence['counter'] ++;
126:         $this->container[] = $subSequence;
127:     }
128:     }
129: 
130:     /**
131:      * Najde požadované sub počítadlo 
132:      * @param string $name název počítadla
133:      * @param string $edit editovaná položka
134:      */
135:     private function editSubString($name, $edit){
136:     $sequence = $this->getSeq($name);
137:     if ($sequence !== FALSE) {
138:         if ($edit !== FALSE) {
139:         $sequence['counter'] = $this->getNumber($sequence, $edit);
140:         }
141:         $this->container[] = $sequence;
142:         $subSequence = $this->getSubSeq($this->getName($sequence));
143:         $subSequence['counter'] = $this->getNumber($sequence, $edit, 1);
144:         $this->container[] = $subSequence;
145:     }
146:     }
147:     
148:     /**
149:      * Načte záznam hlavního počítadla *
150:      * @param string $name
151:      * @return array|boolean
152:      */
153:     private function getSeq($name) {
154:     $sequence = $this->seq->copy->where(Cons::COLUMN_NAME . ' ?', $name)->fetch();
155:     if ($sequence instanceof \Nette\Database\Table\ActiveRow) {
156:         return $sequence->toArray();
157:     } else {
158:         return FALSE;
159:     }
160:     }  
161:     
162:     /**
163:      * Načte záznam subpočítadla počítadla *
164:      * @param string $name
165:      * @return array|boolean
166:      */
167:     private function getSubSeq($name) {
168:     $sequence = $this->seq->copy->where('prefix =?', $name)->fetch();
169:     if ($sequence instanceof \Nette\Database\Table\ActiveRow) {
170:         return $sequence->toArray();
171:     } else {
172:         return FALSE;
173:     }
174:     }  
175:     /**
176:      * extrahuje hodnotu počítadla ze zadaného řetězce
177:      * @param array $sequence
178:      * @param string $name
179:      * @param int $sub
180:      * @return int
181:      */
182:     private function getNumber($sequence, $name, $sub = 0) {
183:     $main = explode('-',$name);
184:     if (count($main)>1 && $sub = 1){
185:         return (int) $main[1];
186:     }else {
187:         $ret = substr($main[0], strlen($sequence['prefix']), 5);                
188:         return (int) $ret;
189:     }
190:     }
191: }
192: 
sberp API API documentation generated by ApiGen