Last active
April 5, 2019 11:08
-
-
Save ferminhg/6c1487d6300efbe97fbd0a9b95f5213f to your computer and use it in GitHub Desktop.
Aging data explain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Obtenemos todos los productos con stock > 0 | |
| // creo que esto pueda que tenga un pequeño bug y no se si lo tiraria a todos lo productos | |
| class ProductRepository implements ProductRepositoryInterface | |
| { | |
| /** | |
| * @return ArrayCollection | |
| */ | |
| public function get() | |
| { | |
| $sql = <<<SQL | |
| SELECT | |
| id, stock | |
| FROM | |
| producto | |
| WHERE | |
| stock > 0 | |
| SQL; | |
| $ret = \Yii::app()->db->createCommand($sql)->queryAll(); | |
| return new ArrayCollection($ret); | |
| } | |
| ... | |
| } | |
| // para cada producto calculamos su $aging | |
| private function calculateAllProducts($products) | |
| { | |
| $aginProduct = new ArrayCollection(); | |
| foreach ($products as $productData) { | |
| /** @var ProductStockInterface $product */ | |
| $producto = $this->createProductStock($productData); | |
| $calculateData = $this->calculator->calculate($producto); | |
| $aginProduct->set($producto->id(), $calculateData); | |
| } | |
| return $aginProduct; | |
| } | |
| // para calcular su aging tenemos que obtener los albaranes de cada uno y ver que dias de diferencia hay entre su recepcion | |
| // aqui habría que valorar si te interesa la fecha de recepcion (albaranes) o de ubicacion (orden de ubicacion) | |
| public function calculate(ProductStockInterface $product) | |
| { | |
| $albaranes = $this->repository->get($product->id()); | |
| $data = AgingData::create($product->stock()); | |
| //para cada albaran | |
| foreach ($albaranes as $albaran) { | |
| // comprobamos que estamos hablando de todo su stock | |
| if ($product->stockAdded() >= $product->stock()) { | |
| break; | |
| } | |
| $daysDiference = intval($albaran['daysDiference']); | |
| //unidades que recepciono | |
| $unidades = intval($albaran['unidades']); | |
| // voy restando estas unidades del stock que he ido añadiendo | |
| if ($unidades > ($product->stock() - $product->stockAdded())) { | |
| $unidades = $product->stock() - $product->stockAdded(); | |
| } | |
| // añado ese producto a una depreciacion | |
| $data = $data->addDaysValues($daysDiference, $unidades); | |
| $product = $product->add($unidades); | |
| } | |
| //para stock que no se tiene albaran macheado | |
| if ($product->stockAdded() < $product->stock()) { //unidades sin asignar | |
| $unitsDiference = $product->stock() - $product->stockAdded(); | |
| $data = $data->addDaysUnknown($unitsDiference); | |
| $product = $product->add($unitsDiference); | |
| } | |
| return $data; | |
| } | |
| // para obtener los albaranes usamos esta sql, que devuelve los dias de diferencia | |
| class NoteOrderRepository implements NoteOrderRepositoryInterface | |
| { | |
| /** | |
| * @param $productId | |
| * @return ArrayCollection | |
| */ | |
| public function get($productId) | |
| { | |
| $sql = <<<SQL | |
| SELECT | |
| al.unidades, pa.fecha, datediff(date(now()), pa.fecha) daysDiference | |
| FROM | |
| pafc_albaran_linea al | |
| JOIN | |
| pafc_albaran pa ON pa.id = al.pafc_albaran_id | |
| AND pa.pafc_estado_id >= 4 | |
| WHERE | |
| al.producto_id = $productId | |
| ORDER by pa.fecha DESC | |
| SQL; | |
| $ret = \Yii::app()->db->createCommand($sql)->queryAll(); | |
| return new ArrayCollection($ret); | |
| } | |
| } | |
| // una vez tenemos la depreciacion truncamos la tabla e insertamos los valores. | |
| public function writeAginData(ArrayCollection $aginData) | |
| { | |
| $sql = <<<SQL | |
| INSERT into aging_data_producto (producto_id, d60, d90, d120, d180, d360, d720, old, stock, unknown ) | |
| VALUES | |
| SQL; | |
| $insertValues = []; | |
| foreach ($aginData as $productId => $data) { | |
| /** @var AgingDataProduct $data */ | |
| $insertValues[] = "( $productId , " . | |
| $data->get60() . | |
| ", " . $data->get90() . | |
| ", " . $data->get120() . | |
| ", " . $data->get180() . | |
| ", " . $data->get360() . | |
| ", " . $data->get720() . | |
| ", " . $data->getOld() . | |
| ", " . $data->stock() . | |
| ", " . $data->getUnknow(). " )"; | |
| } | |
| $insertSql = implode(", ", $insertValues); | |
| $sql .= $insertSql; | |
| $this->truncateAginData(); | |
| return \Yii::app()->db->createCommand($sql)->execute(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment