Example #1
0
 /**
  *
  * @return array
  */
 public function updateAction()
 {
     $params = $this->getRequest()->getParams();
     //die("<pre>".print_r($params, true)."</pre>");
     $baseProduct = ProductQuery::create()->findByPK($params['id']);
     $products = ProductQuery::create()->whereAdd(Product::ITEM_CODE, substr($params['id'], 0, 9), ProductQuery::LIKE)->find();
     $brand = BrandQuery::create()->findByPKOrThrow($params["idBrand"], $this->i18n->_("The Brand with id {$params["idBrand"]} does not exist."));
     if ($params["supplier_id"] != '') {
         $supplier = SapSupplierQuery::create()->findByPKOrThrow($params["supplier_id"], $this->i18n->_("The Supplier with id {$params["supplier_id"]} does not exist."));
     } else {
         $supplier = new SapSupplier();
     }
     $path = 'product/' . $params["keyMother"] . '/';
     $savedFile = $this->saveFile('importationPermission', $path);
     if ($savedFile instanceof File) {
         $this->getFileCatalog()->beginTransaction();
         try {
             $this->getFileCatalog()->create($savedFile);
             $this->getFileCatalog()->commit();
         } catch (Exception $e) {
             $this->getFileCatalog()->rollBack();
             throw new \InvalidArgumentException($this->i18n->_("The Import Permit File can't be saved."));
         }
     } else {
         $savedFile = new File();
         $savedFile->setIdFile(0);
     }
     $colors = ColorQuery::create()->find();
     $test = 1;
     $responses = array();
     foreach ($params["colors"] as $item) {
         $error = false;
         $new = false;
         if (!empty($item["idItem"])) {
             $itemcode = $item["idItem"];
             $codPro = $params["keyMother"] . $brand->getName();
             $color = new Color();
         } else {
             $new = true;
             $color = $colors->getByPkOrElse($item["idColor"], new Color());
             $itemcode = ($codPro = $baseProduct->getKeyMother() . $brand->getName()) . $color->getCode();
             if (ProductGroupQuery::create()->whereAdd(ProductGroup::ID_PRODUCT_GROUP, $params["idProductGroup"])->count()) {
                 $productGroup = ProductGroupQuery::create()->findByPK($params["idProductGroup"]);
             } else {
                 $error = true;
                 $response[0] = false;
                 $response[1] = $itemcode;
                 $responses[] = $response;
             }
             if (FamilyQuery::create()->whereAdd(Family::ID_FAMILY, $params["IdCategory"])->count()) {
                 $family = FamilyQuery::create()->findByPK($params["IdCategory"]);
             } else {
                 $family = new Family();
                 $family->setName(0);
                 $family->setIdFamily(0);
             }
             if (SubfamilyQuery::create()->whereAdd(Subfamily::ID_SUBFAMILY, $params['idSubfamily'])->count()) {
                 $subfamily = SubfamilyQuery::create()->findByPK($params["idSubfamily"]);
             } else {
                 $subfamily = new Subfamily();
                 $subfamily->setCode(0);
             }
         }
         if (!empty($params["goodTrough"])) {
             $date = new Zend_Date(strtolower($params["goodTrough"]), "MMM dd yyyy");
         }
         if (!$error) {
             $productArray = array('CstGrpCode' => $params["idCustomsTariffCode"], 'FirmCode' => $brand->getCode(), 'U_Color' => $color->getCode(), 'U_Categoria' => 0, 'U_PCS_Id_Categoria' => 0, 'ItmsGrpCod' => 0, 'U_Id_Familia' => 0, 'U_Familia' => 0, 'U_id_subcategoria' => 0, 'U_PCS_Impor_per_id' => $savedFile ? $savedFile->getIdFile() : $products->current()->getImportPermit(), 'CardCode' => $supplier->getCardCode(), 'U_PCSid_npd_sheet' => $params["idNpd"] > 0 ? $params["idNpd"] : -1, 'ItemCode' => $itemcode, 'ItemName' => $item["itemName"], 'SWW' => $item["status"], 'Invntitem' => 1, 'PrchseItem' => 1, 'U_id_marca' => $brand->getName(), 'U_Marca' => $brand->getName(), 'SellItem' => 1, 'U_Madre_09' => $params["keyMother"], 'U_CODPRO' => $codPro, 'BuyUnitMsr' => "PZA", 'NumInBuy' => 1, 'SalUnitMsr' => "PZA", 'NumInSale' => 1, 'InvntryUom' => "PZA", 'U_MCFrente' => $params["mcFront"], 'U_MCAltura' => $params["mcHeight"], 'U_MCProfundida' => $params["mcDepth"], 'U_MCPeso' => $params["mcWeight"], 'U_PzasXCaja' => $params["mcParts"], 'U_PCS_Pieces_per_inn' => $params["piecesPerInner"], 'U_pzafrente' => $params["pzaFront"], 'U_pzaaltura' => $params["pzaHeight"], 'U_pzaprofu' => $params["pzaDepth"], 'U_PesoxPza' => $params["pzaWeight"], 'U_pzadiametro' => $params["pzaDiameter"], 'U_PCS_Individual_Pac' => $params["individualPackage"], 'U_PCS_Packing_Front' => $params["packingFront"], 'U_PCS_Packing_Height' => $params["packingHeight"], 'U_PCS_Packing_Depth' => $params["packingDepth"], 'U_PCS_Packing_Weight' => $params["packingWeight"], 'U_PCS_Packing_Diamet' => $params["packingDiameter"], 'U_PCS_Def_Freight_Fa' => $params["defaultFreightFare"], 'U_PCS_Important' => $params["important"] ? 1 : 0, 'U_PCS_Rescue' => $params["rescue"] ? 1 : 0, 'U_PCS_VIM' => $params["vim"], 'U_PCS_Individual_VIM' => $item["vim"], 'U_PCS_import_permit' => $params["remarks"], 'U_PCS_Put_trougth' => $date instanceof Zend_Date ? $date->get("yyyy-MM-dd") : "", 'U_PCS_Catalog_Year' => $params["catalogYear"], 'LeadTime' => $params["lead_time"], 'U_PCS_id_incoterm' => $params["incoterm"], 'U_PCS_manual_restock' => $params["manual_restock"] ? 1 : 0, 'Price' => $params["price"], 'Currency' => $params["currency"], 'U_ShortName' => $params['U_ShortName']);
             if ($new) {
                 $productArray['U_Categoria'] = $family->getName();
                 $productArray['U_PCS_Id_Categoria'] = $family->getIdFamily();
                 $productArray['ItmsGrpCod'] = $productGroup->getIdProductGroup();
                 $productArray['U_Id_Familia'] = $productGroup->getCode();
                 $productArray['U_Familia'] = $productGroup->getName();
                 $productArray['U_id_subcategoria'] = $subfamily->getCode();
             }
             if ($products->containsIndex($itemcode)) {
                 $product = $products->getByPK($itemcode);
                 ProductFactory::populate($product, $productArray);
             } else {
                 $product = ProductFactory::createFromArray($productArray);
             }
             $test++;
             $products->append($product);
         }
     }
     // 		die("<pre>".print_r($products, true)."</pre>");
     $SAPProductManager = new SapProductSaveManager();
     $responses += $SAPProductManager->save($products, $this->getUser()->getBean());
     foreach ($responses as $response) {
         if ($response[0] == 200) {
             $success .= "<br>" . $this->i18n->_("The Product with id {$response[1]} was updated correctly.");
         } else {
             $error .= "<br>" . $this->i18n->_("The Product with id {$response[1]} can't be updated.");
         }
     }
     if (strlen($success) > 0) {
         $this->setFlash('ok', $success);
     }
     if (strlen($error) > 0) {
         $this->setFlash('error', $error);
     }
     $this->_redirect('product/list');
 }