Advertisement
Guest User

Untitled

a guest
May 11th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.78 KB | None | 0 0
  1. Dratf.php  (lo he medio adaptado  del tuyo de product.php)
  2.  
  3. <?php
  4. namespace OpenErp\Modules\Sales;
  5.  
  6. class Draft extends Sales
  7. {
  8.  
  9.     /*
  10.      * BUSCAR Orden
  11.      * $data es un array que debe contener todas las condiciones necesarias que el producto debe cumplir para obtener su ID.
  12.      * Las condiciones pueden expresarse con un "=", "!=" o un "like" en un array con 3 valores: campo, tipo de condición y valor
  13.      * Por ejemplo: $data = array(array('email','like','%@correo.com'),array('is_company','=',1));
  14.      */
  15.  
  16.     public function search($data)
  17.     {
  18.         $limit = 2000;
  19.         $search = $this->erp->search('sale.order',$data,0,$limit);
  20.         return $search;
  21.     }
  22.  
  23.     /*
  24.      * OBTENER Orden POR ID
  25.      * $ids es un array, se tiene que indicar todas las órdenes que se necesitan según por su id.
  26.      * $fields también es un array pero es opcional, se puede indicar los campos necesarios que se requiere por cada llamada.
  27.      * Si no se indica el $fields, devolverá todos los campos de la orden,
  28.      * esto significaría una conexión más lenta si las órdenes fueran muchas.
  29.      */
  30.  
  31.     public function read($ids,$fields = array())
  32.     {
  33.         if (is_array($ids) && count($ids) > 0) {
  34.             $details = $this->erp->read('sale.order', $ids, $fields);
  35.             return $details;
  36.         }
  37.  
  38.         return false;
  39.     }
  40.  
  41.     /*
  42.      * CREAR Orden de venta en estado Draft
  43.      * $data son los parámetros necesarios para registrar una órden, normalmente son los mismos parámetros que
  44.      * la función READ te muestra. Con la diferencia, que no es necesario la ID y algunos campos con opcionales. Devuelve la id de la órden que se creó.
  45.      */
  46.  
  47.     public function create($data)
  48.     {
  49.         $create = $this->erp->create('sale.order', $data);
  50.         return $create;
  51.     }
  52.  
  53.     /*
  54.      * ACTUALIZAR Orden
  55.      * $id es un número entero existente de una orden del OpenERP
  56.      * $data son los parámetros necesarios para actualizar la órden, normalmente son los mismos parámetros que
  57.      * la función READ te muestra. Sin indicar la ID de la órden aquí.
  58.      */
  59.  
  60.     public function write($id, $fields = array())
  61.     {
  62.         $details = $this->erp->write('sale.order', array($id), $fields);
  63.         return $details[0];
  64.     }
  65. }
  66.  
  67.  
  68. class DraftLines extends Sales
  69. {
  70.  
  71.     public function create($data)
  72.     {
  73.         $create = $this->erp->create('sale.order.line', $data);
  74.         return $create;
  75.     }
  76.  
  77. }
  78.  
  79. ##################333
  80.  
  81. My script.php (De momento va a medias, la función de crear las órdenes se lanza en el momento de acceder al php, no al dar al boton
  82.  
  83. <?php
  84. /* Magento*/
  85.  
  86. require_once 'app/Mage.php';
  87. Mage::app();
  88. umask(0);
  89.  
  90. /* Libraries*/
  91. require_once 'vendor/autoload.php';
  92. require_once 'connector/config.php';
  93. use OpenErp\Modules\Sales\Draft;
  94. use OpenErp\Modules\Sales\DraftLines;
  95. use OpenErp\Modules\Sales\Product;
  96.  
  97.        
  98. ?> 
  99.  
  100.  
  101. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  102.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  103. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  104.  
  105. <head>
  106.     <title>SCTV-PUSH-MOTOSCOOT</title>
  107.     <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  108.     <meta name="generator" content="Geany 1.23.1" />
  109.     <style>
  110.         td {
  111.             border:1px solid black;
  112.             }
  113.     </style>
  114.    
  115. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
  116. <script type="text/javascript">
  117.    
  118.   function call_php()
  119.   {
  120.     var result="<?php pushorder();?>";
  121.     alert (result);
  122.     return false;
  123.  
  124.   }
  125. </script>
  126. </head>
  127.  
  128. <body>
  129. <?
  130.        $orders = Mage::getResourceModel('sales/order_collection')
  131.     ->addAttributeToSelect('*')
  132.    
  133.     ->addFieldToFilter('status', array("in" => array(
  134.             'complete',
  135.             'closed',
  136.             'pending',)
  137.             ))
  138.  
  139.     ->addAttributeToFilter('store_id', Mage::app()->getStore()->getId())
  140.     ->addAttributeToSort('created_at', 'desc')
  141.     ->load();
  142.    
  143. foreach($orders as $order):
  144.     echo '<table>';
  145.     echo '<tr>';
  146.         echo '<td>'.$order->getIncrementId().'</td>';
  147.         echo '<td>'.$order->getstate().'</td>';
  148.         echo '<td>'.$order->getstatus().'</td>';
  149.         echo '<td>'.$order->getcustomer_firstname().'</td>';
  150.         echo '<td>'.$order->getcustomer_lastname().'</td>';
  151.         echo '<td>'.$order->getcustomer_email().'</td>';
  152.         echo '<td>
  153.               <form  method="get">
  154.               <input type="button" onclick="call_php" value="Push Order">
  155.               </form>
  156.               </td>';
  157.         echo '<td>';
  158.         echo '<tr>';
  159.                 $items = $order->getAllVisibleItems();
  160.                 foreach($items as $i):
  161.                 echo '<td>'.$i->getSku(). '/' .$i->getname().'</td>';
  162.                 echo '<td>'.$i->getqty_ordered().'</td>';
  163.                 endforeach;
  164.                
  165.                
  166.     echo '</tr>';
  167.     echo '</td>';
  168.     echo '</table>';
  169.     echo '</br>';
  170.        
  171.  
  172. endforeach;
  173.  
  174. /* Openerp Functions */
  175.  
  176. function pushorder(){
  177.    
  178.    
  179.     $orders = Mage::getResourceModel('sales/order_collection')
  180.     ->addAttributeToSelect('*')
  181.     ->addFieldToFilter('status', array("in" => array(
  182.             'complete',
  183.             'closed',
  184.             'pending',)
  185.             ))
  186.  
  187.     ->addAttributeToFilter('store_id', Mage::app()->getStore()->getId())
  188.     ->addAttributeToSort('created_at', 'desc')
  189.     ->load();
  190.    
  191.     # For each Order, create new one and push all products in sale line
  192.     foreach($orders as $order):
  193.    
  194.         # Create Basic Order
  195.         $saleorder=array(
  196.         'partner_id'=>'4932', /* SCTV*/
  197.         'partner_invoice_id'=>'4933',  /*Julian*/
  198.         'partner_shipping_id'=>'4932', /*SCTV*/
  199.         'shop_id' =>'1',
  200.         'pricelist_id' =>'4',
  201.         );
  202.  
  203.  
  204.         $orderid = new Draft(USERNAME, PASSWORD, DATABASE, SERVER);
  205.         $result = $orderid->create($saleorder);
  206.        
  207.         ## Get Sale_id and fill with products
  208.         ## get product_fields(name)  by id
  209.         $product = new Product(USERNAME, PASSWORD, DATABASE, SERVER);
  210.         $items = $order->getAllVisibleItems();
  211.             foreach($items as $i):
  212.                 $sku = $i->getSku();
  213.                
  214.                
  215.                 # get product_id/Name from Openerp
  216.                 $prod_id = $product->search(array(array('default_code','like', $sku)));
  217.                 $prod_inf = $product->read(array($prod_id),array('pvp_fabricante','name','id','list_price'));
  218.                 $qty = $i->getqty_ordered();
  219.                
  220.                 $salelines =array(
  221.                 'order_id' => $result, /*Order in wich I want intro multiple lines */
  222.                 'product_uom_qty'=> $qty, /*Qty from magento*/
  223.                 'product_id'=>$prod_inf[0]['id'], /*product_id in Openerp*/
  224.                 'price_unit'=>(float)$prod_inf[0]['list_price'], /*Si no informo el producto queda sin precio pero debe ser el precio
  225.                                                                    /*que me debe calcular la tarifa asignada a ese cliente*/
  226.                 'name'=> $prod_inf[0]['name'],
  227.                
  228.                 );
  229.                 $orderlines = new DraftLines(USERNAME, PASSWORD, DATABASE, SERVER);
  230.                 $resultlines = $orderlines->create($salelines);
  231.                 endforeach;
  232.     endforeach;        
  233.                
  234.    
  235. }
  236.  
  237.  
  238.  
  239. ?>
  240.    
  241. </body>
  242.  
  243. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement