src/Jaxon/Idempiere/Orderline.php line 272

Open in your IDE?
  1. <?php
  2. namespace App\Jaxon\Idempiere;
  3. use App\Constant;
  4. use App\Entity\Idempiere\AdOrg;
  5. use App\Entity\Idempiere\AdSequence;
  6. use App\Entity\Idempiere\AdUser;
  7. use App\Entity\Idempiere\COrder;
  8. use App\Entity\Idempiere\COrderline;
  9. use App\Entity\Idempiere\CTax;
  10. use App\Entity\Idempiere\MProduct;
  11. use App\Entity\Idempiere\MProductprice;
  12. use App\Entity\Idempiere\MWarehouse;
  13. use App\Jaxon\Base;
  14. use App\Repository\Idempiere\AdSequenceRepository;
  15. use App\Repository\Idempiere\MProductpriceRepository;
  16. use App\Repository\Idempiere\MProductRepository;
  17. use Exception;
  18. use Jaxon\Response\Response;
  19. class Orderline extends Base
  20. {
  21.     /**
  22.      * Habilitar campo para cambiar cantidades
  23.      * de una linea en la orden
  24.      * 
  25.      * @param int $C_Orderline_ID Identificador de la linea
  26.      * 
  27.      * @return Response Jaxon Reponse
  28.      */
  29.     public function changeQty(
  30.         Int $C_Orderline_ID
  31.     )
  32.     {
  33.         $ROrderline $this->manager->getRepository(COrderline::class);
  34.         $orderline  $ROrderline->find$C_Orderline_ID );
  35.         $qty $orderline->getQtyentered();
  36.         $jxnr = new Response;
  37.         $input
  38.             '<input 
  39.                 type="number"
  40.                 step="0.01"
  41.                 value="'number_format($qty2) .'"
  42.                 onblur="App.Jaxon.Idempiere.Orderline.setQty(\''$C_Orderline_ID .'\', this.value)"
  43.             >';
  44.         return $jxnr->assign($C_Orderline_ID '_qty''innerHTML'$input);
  45.     }
  46.     /**
  47.      * Habilitar campo para cambiar precios
  48.      * de una linea en la orden
  49.      * 
  50.      * @param int $C_Orderline_ID Identificador de la linea
  51.      * 
  52.      * @return Response Jaxon Reponse
  53.      */
  54.     public function changePrc(
  55.         Int $C_Orderline_ID
  56.     )
  57.     {
  58.         $ROrderline $this->manager->getRepository(COrderline::class);
  59.         $orderline  $ROrderline->find$C_Orderline_ID );
  60.         $prc $orderline->getPriceactual();
  61.         $jxnr = new Response;
  62.         $input
  63.             '<input 
  64.                 type="number"
  65.                 step="0.01"
  66.                 value="'number_format($prc2) .'"
  67.                 onblur="App.Jaxon.Idempiere.Orderline.setPrc(\''$C_Orderline_ID .'\', this.value)"
  68.             >';
  69.         return $jxnr->assign($C_Orderline_ID '_prc''innerHTML'$input);
  70.     }
  71.     /**
  72.      * Crear linea en la orden
  73.      * 
  74.      * @param int $m_productprice_id Identificacdor del precio del producto
  75.      * @param array $formData Datos de la orden
  76.      * 
  77.      * @return Response Jaxon Reponse
  78.      */
  79.     # TODO: Cambiar monto de la orden 
  80.     public function create(
  81.         Int $m_productprice_id
  82.     ): Response
  83.     {
  84.         $jxnr   = new Response;
  85.         /** 
  86.          * Var Session 
  87.          */
  88.         /** @var AdUser */
  89.         $user $this->session->get('user'null);
  90.         /** @var COrder */
  91.         $order $this->session->get('order'null);
  92.         /** @var MWarehouse */
  93.         $warehouse $this->session->get('warehouse'null);
  94.         ##############################################################
  95.         /**
  96.          * Entity Repositorys
  97.          */
  98.         
  99.          /** @var MProductpriceRepository */
  100.         $RProductprice $this->manager->getRepository(MProductprice::class);
  101.         $productprice $RProductprice->find($m_productprice_id);
  102.         /** @var MProductRepository */
  103.         $RProduct $this->manager->getRepository(MProduct::class);
  104.         $product $RProduct->find($productprice->getMProductId());
  105.         ##############################################################
  106.         
  107.         // Verifica si el producto tiene stock
  108.         if( $RProduct->findStorage($product->getId(), $warehouse->getId()) <= 
  109.             return $jxnr->scriptself::displayError("Producto sin Stock""Este producto no cuenta con cantidades disponibles.") );
  110.         // Verifica si el producto ya esta registrado en la orden
  111.         $ROrderline $this->manager->getRepository(COrderline::class);
  112.         if( count$ROrderline->findBy(['c_order_id' => $order->getId(), 'm_product_id' => $product->getId(), 'isactive' => 'Y']) ) > )
  113.             return $jxnr->scriptself::displayError("Producto Repetido""Este producto se encuentra registrado, modifique la cantidad de ser necesario.") );
  114.         $orderlines $ROrderline->findBy(['c_order_id' => $order->getId()]);
  115.         
  116.         /** @var AdSequenceRepository Sequence Repository */
  117.         $RSequence $this->manager->getRepository(AdSequence::class);
  118.         $Orderline = new COrderline();
  119.         $Orderline
  120.             ->setAdClientId$order->getAdClientId() )
  121.             ->setAdOrgId$order->getAdOrgId() )
  122.             ->setCActivityIdConstant::C_Activity_ID )
  123.             ->setCBpartnerId$order->getCBpartner()->getId() )
  124.             ->setCBpartnerLocationId$order->getCBpartnerLocation()->getId() )
  125.             ->setCCurrencyId$order->getCCurrency()->getId() )
  126.             ->setCOrderId$order->getId() )
  127.             ->setCOrderlineId$RSequence->findNextSequence$ROrderline->sequence ) )
  128.             ->setCUomId$product->getCUomId() )
  129.             ->setLine( (count($orderlines) + 1) * 10 )
  130.             ->setLinenetamt$productprice->getPricelist() )
  131.             ->setMProduct$product )
  132.             ->setMWarehouseId$order->getMWarehouseId() )
  133.             ->setPriceactual$productprice->getPricelist() )
  134.             ->setPriceentered$productprice->getPricelist() )
  135.             ->setPricelist$productprice->getPricelist() )
  136.             ->setQtyentered(1)
  137.             ->setQtyordered(1)
  138.             ->setQtyreserved(1);
  139.         
  140.         $date = new \DateTime("now"); 
  141.         $Orderline
  142.             ->setCreated$date )
  143.             ->setCreatedby$user->getId() )
  144.             ->setUpdated$date )
  145.             ->setUpdatedby$user->getId() )
  146.             ->setIsactive('Y')
  147.             ->setDateordered$date )
  148.             ->setCOrderlineUu$RSequence->findNextUU() );
  149.         $RTax $this->manager->getRepository(CTax::class);
  150.         $tax $RTax->findBy([
  151.             'ad_client_id' => $order->getAdClientId(),
  152.             'c_taxcategory_id' => $product->getCTaxcategoryId(),
  153.             'sopotype' => ['S''B'],
  154.             'isdefault' => 'Y',
  155.             'isactive' => 'Y'
  156.         ]);
  157.         $Orderline->setCTaxId$tax[0]->getId() );
  158.         
  159.         $this->manager->persist($Orderline);
  160.         $ROrder $this->manager->getRepository(COrder::class);
  161.         $order $ROrder->find$order->getId() );
  162.         $order->setTotallines$order->getTotallines() + $Orderline->getLinenetamt() );
  163.         $order->setGrandtotal$order->getTotallines() );
  164.         $this->manager->persist($order);
  165.         try {
  166.             $this->manager->flush();
  167.             $this->manager->clear();
  168.             $html 
  169.             '<tr class="text-center orderline" id="'$Orderline->getId() .'">
  170.                 <td class="text-center">'   $Orderline->getLine() .'</td>
  171.                 <td class="text-left">'     $Orderline->getMProduct()->getName() .'</td>
  172.                 <td class="text-center">'   $Orderline->getMProduct()->getValue() .'</td>
  173.                 <td class="text-right prc" id="'$Orderline->getId() . '_prc">
  174.                     <p onclick="App.Jaxon.Idempiere.Orderline.changePrc(\''$Orderline->getId() .'\')">'number_format($Orderline->getPriceactual(), 2) .'</p>
  175.                 </td>
  176.                 <td class="text-right qty" id="'$Orderline->getId() . '_qty">
  177.                     <p onclick="App.Jaxon.Idempiere.Orderline.changeQty(\''$Orderline->getId() .'\')">'number_format($Orderline->getQtyordered(), 2) .'</p>
  178.                 </td>
  179.                 <td class="text-right amt" id="'$Orderline->getId() . '_amt">
  180.                     'number_format($Orderline->getLinenetamt(), 2) .'
  181.                 </td>
  182.                 <td class="btn-action">
  183.                     <i onclick="App.Jaxon.Base.actionConfirm(\'Orderline\', \'delete\', \''$Orderline->getId() .'\')" class="fas fa-trash text-danger"></i>
  184.                 </td>
  185.             </tr>';
  186.             $jxnr
  187.                 ->prepend('tablaProductos''innerHTML'$html)
  188.                 ->assign('totallines''value'number_format($order->getTotallines(), 2))
  189.                 ->assign('grandtotal''value'number_format($order->getGrandtotal(), 2))
  190.                 ->script('$("#productModal").modal("hide")');
  191.         } catch (Exception $e) {
  192.             $jxnr->script(self::displayError("Oops!""Ha fallado al crear la Linea"$e) );
  193.         }
  194.         return  $jxnr;
  195.     }
  196.     /**
  197.      * Borra una linea de una orden temporal
  198.      * 
  199.      * @param int $C_Orderline_ID Identificador de la linea
  200.      * 
  201.      * @return Response Jaxon Reponse
  202.      */
  203.     # TODO: Crear metodo para reordenar lineas
  204.     public function delete(
  205.         Int $C_Orderline_ID
  206.     )
  207.     {
  208.         $ROrderline $this->manager->getRepository(COrderline::class);
  209.         $orderline  $ROrderline->find$C_Orderline_ID );
  210.         $oAmt $orderline->getLinenetamt();
  211.         $orderline->setIsactive('N');
  212.         $this->manager->persist($orderline);
  213.         $ROrder $this->manager->getRepository(COrder::class);
  214.         $order $ROrder->find$orderline->getCOrderId() );
  215.         $order->setTotallines$order->getTotallines() - $oAmt ); // Restar monto viejo de la linea
  216.         $order->setGrandtotal$order->getTotallines() );
  217.         $this->manager->persist($order);
  218.         $jxnr = new Response;
  219.         try {
  220.             $this->manager->flush();
  221.             $this->manager->clear();
  222.             $jxnr
  223.                 ->remove($C_Orderline_ID)
  224.                 ->assign('totallines''value'number_format($order->getTotallines(), 2))
  225.                 ->assign('taxamt''value'number_format($order->getGrandtotal() - $order->getTotallines(), 2))
  226.                 ->assign('grandtotal''value'number_format($order->getGrandtotal(), 2));
  227.         } catch (Exception $e) {
  228.             $jxnr->script(self::displayError("Oops!""Ha fallado al borrar la Linea"$e) );
  229.         }
  230.         return $jxnr;
  231.     }
  232.     
  233.     /**
  234.      * Lista los productos coincidentes
  235.      * 
  236.      * @param string $value Codigo o Nombre del producto
  237.      * @param array $formData Datos del formulario
  238.      * 
  239.      * @return Response Jaxon Reponse
  240.      */
  241.     public function getProducts(
  242.         String $value ''
  243.         Array $formData
  244.     ): Response
  245.     {
  246.         $jxnr   = new Response;
  247.         /** 
  248.          * Session 
  249.          */
  250.         /** @var AdUser */
  251.         $user $this->session->get('user'null);
  252.         /** @var AdOrg */
  253.         $organization $this->session->get('organization'null);
  254.         /** @var MWarehouse */
  255.         $warehouse $this->session->get('warehouse'null);
  256.         ####################################################################
  257.         
  258.         /** 
  259.          * Repositorios 
  260.          */
  261.         /** @var MProductRepository */
  262.         $RProduct $this->manager->getRepository(MProduct::class);
  263.         /** @var MProductpriceRepository */
  264.         $RProductprice $this->manager->getRepository(MProductprice::class);
  265.         ####################################################################
  266.         // La organizacion debe ser seleccionada para listar los productos
  267.         if(!isset($organization) || $formData['organization'] == 0)
  268.             return $jxnr->script('Swal.fire("Oops!", "Primero debe seleccionar una organizaci&oacute;n!", "warning")');
  269.         // La lista de precio debe ser seleccionada para mostrar los precios de los articulos
  270.         if(!isset($formData['pricelist']) || $formData['pricelist'] == 0)
  271.             return $jxnr->script('Swal.fire("Oops!", "No has seleccionado una lista de precio!", "warning")');
  272.         /** Datos */
  273.         $pps $RProductprice->findProductPrices($formData['pricelist'], $value); 
  274.         $html'<table id="productTable" class="table"><thead><tr><th>Marca</th><th>Nombre</th><th>C&oacute;digo</th><th>Cantidad</th><th>Precio</th></tr></thead><tbody>';
  275.         if( count($pps) > 0) {
  276.             foreach ($pps as $pp) {
  277.                 $html .= 
  278.                 '<tr style="font-size: 0.9rem; cursor: pointer;" onclick="App.Jaxon.Idempiere.Orderline.create(\''$pp->getId() .'\', jaxon.getFormValues(\'order\'))">
  279.                     <td>'.$pp->getMProduct()->getSmMarca()->getName().'</td>
  280.                     <td>'.$pp->getMProduct()->getName().'</td>
  281.                     <td>'.$pp->getMProduct()->getValue().'</td>
  282.                     <td>'.  number_format($RProduct->findStorage($pp->getMProductId(), $warehouse->getId()), 2) .'</td>
  283.                     <td>$'.$pp->getPricestd().'</td>
  284.                 </tr>';
  285.             }
  286.         } else {
  287.             $html .= '<tr><td colspan="2">NO SE HA ENCONTRADO NINGUN PRODUCTO</td></tr>';
  288.         }
  289.         $html       .= '</tbody></table>';
  290.         
  291.         $jxnr
  292.             ->assign('products''innerHTML'$html)
  293.             ->script('$("#productTable").dataTable({ columnDefs: [{ orderable: true, targets: 0 }], order: [[1, "asc" ]], responsive: true, lengthChange: false, autoWidth: false, dom: \'<"row"<"col-sm-12 col-md-12"f>><"row"<"col-sm-12"rt>><"row"<"col-sm-12 col-md-5"i><"col-sm-12 col-md-7"p>>\', buttons: ["copy", "csv", "excel", "pdf", "print", "colvis"] })');
  294.         return $jxnr;
  295.     }
  296.     /**
  297.      * Cambiar cantidad de un producto en una linea 
  298.      * 
  299.      * @param int $C_Orderline_ID Identificador de la linea
  300.      * @param float $qty Cantidad
  301.      * 
  302.      * @return Response Jaxon Reponse
  303.      */
  304.     # TODO: Verificar las cantidades disponibles
  305.     public function setQty(
  306.         Int $C_Orderline_ID,
  307.         Float $qty
  308.     )
  309.     {
  310.         $ROrderline $this->manager->getRepository(COrderline::class);
  311.         $orderline  $ROrderline->find$C_Orderline_ID );
  312.         $orderline->setQtyordered$qty );
  313.         $orderline->setQtyentered$qty );
  314.         $orderline->setQtyreserved$qty );
  315.         $oAmt $orderline->getLinenetamt();
  316.         $orderline->setLinenetamt$orderline->getPriceactual() * $qty );
  317.         //
  318.         $this->manager->persist($orderline);
  319.         //
  320.         $ROrder $this->manager->getRepository(COrder::class);
  321.         $order $ROrder->find$orderline->getCOrderId() );
  322.         $amt $order->getTotallines() - $oAmt// Restar monto viejo de la linea
  323.         $order->setTotallines$amt $orderline->getLinenetamt() );
  324.         $order->setGrandtotal$order->getTotallines() );
  325.         
  326.         $this->manager->persist($order);
  327.         
  328.         $jxnr = new Response;
  329.         try {
  330.             $this->manager->flush();
  331.             $this->manager->clear();
  332.             $jxnr
  333.                 ->assign($C_Orderline_ID '_qty''innerHTML''<p onclick="App.Jaxon.Idempiere.Orderline.changeQty(\''$orderline->getId() .'\')">'number_format($qty2) .'</p>')
  334.                 ->assign($C_Orderline_ID '_amt''innerHTML'number_format($orderline->getLinenetamt(), 2))
  335.                 ->assign('totallines''value'number_format($order->getTotallines(), 2))
  336.                 ->assign('taxamt''value'number_format($order->getGrandtotal() - $order->getTotallines(), 2))
  337.                 ->assign('grandtotal''value'number_format($order->getGrandtotal(), 2));
  338.         } catch (Exception $e) {
  339.             $jxnr->script(self::displayError("Oops!""Ha fallado el guardado automatico de la Linea"$e) );
  340.         }
  341.         return $jxnr;
  342.     }
  343.     /**
  344.      * Cambiar precio de un producto en una linea 
  345.      * 
  346.      * @param int $C_Orderline_ID Identificador de la linea
  347.      * @param float $prc Precio
  348.      * 
  349.      * @return Response Jaxon Reponse
  350.      */
  351.     # TODO: Verificar las cantidades disponibles
  352.     public function setPrc(
  353.         Int $C_Orderline_ID,
  354.         Float $prc
  355.     )
  356.     {
  357.         $ROrderline $this->manager->getRepository(COrderline::class);
  358.         $orderline  $ROrderline->find$C_Orderline_ID );
  359.         $orderline->setPriceactual($prc);
  360.         $orderline->setPriceentered($prc);
  361.         $orderline->setPricelist($prc);
  362.         $oAmt $orderline->getLinenetamt();
  363.         $orderline->setLinenetamt$orderline->getPriceactual() * $orderline->getQtyentered() );
  364.         //
  365.         $this->manager->persist($orderline);
  366.         //
  367.         $ROrder $this->manager->getRepository(COrder::class);
  368.         $order $ROrder->find$orderline->getCOrderId() );
  369.         $amt $order->getTotallines() - $oAmt// Restar monto viejo de la linea
  370.         $order->setTotallines$amt $orderline->getLinenetamt() );
  371.         $order->setGrandtotal$order->getTotallines() );
  372.         
  373.         $this->manager->persist($order);
  374.         
  375.         $jxnr = new Response;
  376.         try {
  377.             $this->manager->flush();
  378.             $this->manager->clear();
  379.             $jxnr
  380.                 ->assign($C_Orderline_ID '_prc''innerHTML''<p onclick="App.Jaxon.Idempiere.Orderline.changePrc(\''$orderline->getId() .'\')">'number_format($prc2) .'</p>')
  381.                 ->assign($C_Orderline_ID '_amt''innerHTML'number_format($orderline->getLinenetamt(), 2))
  382.                 ->assign('totallines''value'number_format($order->getTotallines(), 2))
  383.                 ->assign('taxamt''value'number_format($order->getGrandtotal() - $order->getTotallines(), 2))
  384.                 ->assign('grandtotal''value'number_format($order->getGrandtotal(), 2));
  385.         } catch (Exception $e) {
  386.             $jxnr->script(self::displayError("Oops!""Ha fallado el guardado automatico de la Linea"$e) );
  387.         }
  388.         return $jxnr;
  389.     }
  390. }
  391. ?>