Current File : /home/karenpetzb/application/models/utils/PromoCalculator.php
<?php
class PromoCalculator {
	var $promoProduct;
	var $promoUser;
	var $promoCommand;

	function PromoCalculator() {
		$this->promoProduct = new PromoProduct();
		$this->promoUser = new PromoUser();
		$this->promoCommand = new PromoCommand();
	}

	public function calculEuro($prix, $remise) { return $prix - $remise; }
	public function calculPour($prix, $remise) { return $prix - (($prix * $remise) / 100); }
	public function isRemise() {

	}
	/*
	 * REMISES SUR PRODUITS
	 */

	/*
	 * Calcul du prix de la promotion selon la reference d'un produit
	 */
	private function computePrixProductByReference($factureLine) {
		if ($factureLine->isPromo()) {
			$myPromo = $this->promoProduct->getRemiseByProductReference($factureLine->item_reference);
			if ($myPromo['REMISEEURO'] > 0) {
				$factureLine->remise_euro = $myPromo['REMISEEURO'];
				//$prixPromo = $this->calculEuro($factureLine->item_prix, $myPromo['REMISEEURO']);
				//$factureLine->setPrixPromo($prixPromo);
			}
			if ($myPromo['REMISEPOUR'] > 0) {
				//$prixPromo = $this->calculPour($factureLine->item_prix, $myPromo['REMISEPOUR']);
				//$factureLine->setPrixPromo($prixPromo);
				$factureLine->remise_pour = $myPromo['REMISEPOUR'];
			}
			$factureLine->checkPrixPromo();
		}
	}

	/*
	 * Calcul du prix de la promotion selon la marque d'un produit
	 */
	private function computePrixProductByMarque($factureLine) {
		$myPromo = $this->promoProduct->getRemiseByProductBrend($factureLine->product_brend_id);
		if ($myPromo) {
			if ($myPromo['REMISEEURO'] > 0) {
				$factureLine->remise_euro = $myPromo['REMISEEURO'];
			}
			if ($myPromo['REMISEPOUR'] > 0) {
				$factureLine->remise_pour = $myPromo['REMISEPOUR'];
			}
			$factureLine->checkPrixPromo();
		}
	}


	/*
	 * Calcul du prix de la promotion selon la gamme d'un produit
	 */
	private function computePrixProductByCategory($factureLine) {
		$myPromo = $this->promoProduct->getRemiseByProductCategory($factureLine->product_category_id);
		if ($myPromo) {
			if ($myPromo['REMISEEURO'] > 0) {
				$factureLine->remise_euro = $myPromo['REMISEEURO'];
			}
			if ($myPromo['REMISEPOUR'] > 0) {
				$factureLine->remise_pour = $myPromo['REMISEPOUR'];
			}
			$factureLine->checkPrixPromo();
		}
	}

	/*
	 * Calcul du prix de la promotion d'un produit selon un nombre achete
	 */
	private function computePrixProductByQte($factureLine, $facture_lines) {
		$myPromo = $this->promoProduct->getRemisesByProductQte($factureLine->item_reference, $factureLine->item_qte);
		if ($myPromo) {
			foreach ($facture_lines AS $line) {
				foreach ($myPromo AS $promo) {
					if (($line->item_reference == $promo['CHILDREFBUY']) &&
					($line->item_qte >= $promo['CHILDREFBUYNB']) ) {
						if ($promo['REMISEEURO'] > 0) {
							$factureLine->remise_items = array(
								'NB' => $promo['CHILDREFPROMONB'],
								'REF' => $promo['CHILDREFPROMO'],
								'REMISE_EURO' => $promo['REMISEEURO'],
								'REMISE_POUR' => 0

							);
						}
						if ($promo['REMISEPOUR'] > 0) {
							$factureLine->remise_items = array(
								'NB' => $promo['CHILDREFPROMONB'],
								'REF' => $promo['CHILDREFPROMO'],
								'REMISE_POUR' => $promo['REMISEPOUR'],
								'REMISE_EURO' => 0 
							);
						}
					}
				}
			}
		}
	}

	/*
	 * Calcul du prix de la promotion selon la date d'un produit
	 */
	private function computePrixProductByDateBetween($factureLine) {
		$myPromo = $this->promoProduct->getRemiseByProductDateBetween($factureLine->product_promo_date);
		if ($myPromo) {
			if ($myPromo['REMISEEURO'] > 0) {
				$factureLine->remise_euro = $myPromo['REMISEEURO'];
			}
			if ($myPromo['REMISEPOUR'] > 0) {
				$factureLine->remise_pour = $myPromo['REMISEPOUR'];
			}
			$factureLine->checkPrixPromo();
		}
	}


	/*
	 * Calcul du prix de la remise selon la marque et l'user
	 */
	private function computePrixUserByMarque($factureLine, $userId) {
		$myPromo = $this->promoUser->getRemiseByMarque($factureLine->product_brend_id, $userId);
		if ($myPromo) {
			if ($myPromo['REMISEEURO'] > 0) {
				$factureLine->remise_euro = $myPromo['REMISEEURO'];
			}
			if ($myPromo['REMISEPOUR'] > 0) {
				$factureLine->remise_pour = $myPromo['REMISEPOUR'];
			}
			$factureLine->checkPrixPromo();
		}
	}

	/*
	 * Calcul du prix de la remise selon le code interne et la marque
	 */
	private function computePrixUserByCodeInternMarque($factureLine, $codeIntern) {
		$myPromo = $this->promoUser->getRemiseByCodeInternMarque($factureLine->product_brend_id, $codeIntern);
		if ($myPromo) {
			if ($myPromo['REMISEEURO'] > 0) {
				$factureLine->remise_euro = $myPromo['REMISEEURO'];
			}
			if ($myPromo['REMISEPOUR'] > 0) {
				$factureLine->remise_pour = $myPromo['REMISEPOUR'];
			}
			$factureLine->checkPrixPromo();
		}
	}


	/*
	 * REMISES GLOBALES
	 */

	/*
	 * Calcul du prix de la remise de la commande selon un nombre de produits
	 *
	 * CUMUL
	 */
	private function computePrixCommandByProductQte($factureLine, $facture) {
		$myPromo = $this->promoCommand->getRemiseCommandByProductQte($factureLine->item_reference, $factureLine->item_qte);
		if ($myPromo) {
			if ($myPromo['REMISEEURO'] > 0) {
				$facture->remise_command_euro += $myPromo['REMISEEURO'];
			}
			if ($myPromo['REMISEPOUR'] > 0) {
				$facture->remise_command_pour += $myPromo['REMISEPOUR'];
			}
		}
	}

	/*
	 * Calcul du prix de la remise de la commande selon le TOTAL HT
	 *
	 * CUMUL
	 */
	private function computePrixCommandByTotalHT($facture) {
		$myPromo = $this->promoCommand->getRemiseCommandByTotalHT($facture->total_HT_HR);
		if ($myPromo) {
			if ($myPromo['REMISEEURO'] > 0) {
				$facture->remise_command_euro += $myPromo['REMISEEURO'];
			}
			if ($myPromo['REMISEPOUR'] > 0) {
				$facture->remise_command_pour += $myPromo['REMISEPOUR'];
			}
		}
	}

	/*
	 * Calcul du prix de la remise de toute les commandes
	 *
	 * CUMUL
	 */
	private function computePrixCommandByAll($facture) {
		$myPromo = $this->promoCommand->getRemiseCommandByAll();
		if ($myPromo) {
			if ($myPromo['REMISEEURO'] > 0) {
				$facture->remise_command_euro += $myPromo['REMISEEURO'];
			}
			if ($myPromo['REMISEPOUR'] > 0) {
				$facture->remise_command_pour += $myPromo['REMISEPOUR'];
			}
		}
	}

	/*
	 * Calcul du prix de la remise de la commande selon la date anniversaire
	 *
	 * CUMUL
	 */
	private function computePrixUserByDateInscription($facture, $dateInsc) {
		$myPromo = $this->promoUser->getRemiseByDateInscription();
		if ($myPromo) {
			$date = new Zend_Date();
			$userDate = new Zend_Date(strtotime($dateInsc));
			if (($userDate->toString("MM") -  $date->toString("MM")) < 2 &&
			($userDate->toString("MM") -  $date->toString("MM")) > -2) {
					
				if ($myPromo['REMISEEURO'] > 0) {
					$facture->remise_command_euro += $myPromo['REMISEEURO'];
				}
				if ($myPromo['REMISEPOUR'] > 0) {
					$facture->remise_command_pour += $myPromo['REMISEPOUR'];
				}
			}
		}
	}


	/*
	 * Calcul du prix de la remise de la commande selon l'user
	 *
	 * CUMUL
	 */
	private function computePrixUserByUser($facture, $userID) {
		$myPromo = $this->promoUser->getRemiseByUserID($userID);
		if ($myPromo) {
			if ($myPromo['REMISEEURO'] > 0) {
				$facture->remise_command_euro += $myPromo['REMISEEURO'];
			}
			if ($myPromo['REMISEPOUR'] > 0) {
				$facture->remise_command_pour += $myPromo['REMISEPOUR'];
			}
		}
	}

	/*
	 * Calcul du prix de la remise de la commande si c'est la premiere
	 *
	 * CUMUL
	 */
	private function computePrixUserByIsFirstCommand($facture, $userID) {
		$myPromo = $this->promoUser->getRemiseByIsFirstCommand();
		if ($myPromo) {
			$commande = new Command();
			if (!$commande->isAlreadyBuySomething($userID)) {
				if ($myPromo['REMISEEURO'] > 0) {
					$facture->remise_command_euro += $myPromo['REMISEEURO'];
				}
				if ($myPromo['REMISEPOUR'] > 0) {
					$facture->remise_command_pour += $myPromo['REMISEPOUR'];
				}
			}
		}
	}

	/*
	 * Calcul du prix du code de reduction pour un produit
	 *
	 * UNIQUE
	 */
	private function computePrixCodeReduction($facture) {
		if (isset($facture->code_reduction) && !empty($facture->code_reduction)) {
			$codeReduction = new CodeReduction();
			$myPromo = $codeReduction->getVerifyCodeBy($facture->code_reduction['CODE']);
			if ($myPromo) {
				$facture->code_reduction = $myPromo;
				if ($facture->isCodeReduction_Product) {
					if (isset($myPromo) && !empty($myPromo) && $myPromo['isACTIF'] == 1 ) {
						if ($myPromo['EURO'] > 0) {
							$facture->remise_command_euro = $myPromo['EURO'];
						}
						if ($myPromo['POUR'] > 0) {
							$facture->remise_command_pour = $myPromo['POUR'];
						}
					}
				} else {
					if ($facture->total_HT_HR >= $myPromo['CMDTOTAL'] && $myPromo['CMDTOTAL'] > 0) {
						if ($myPromo['EURO'] > 0) {
							$facture->remise_command_euro = $myPromo['EURO'];
						}
						if ($myPromo['POUR'] > 0) {
							$facture->remise_command_pour = $myPromo['POUR'];
						}
					}
				}
				$facture->code_reduction['EURO'] = $facture->getPrixCodeReduction();
			} else { $facture->code_reduction = array(); }
		}
	}

	/*
	 * Calcul du prix restant a payer de la commande pour validation
	 *
	 * Return prix restant a payer
	 */
	public function getPrixCommandValidation($facture) {
		$result = 0;
		$myPromo = $this->promoCommand->getCommandValid();
		if ($myPromo) {
			$total_HT = $facture->getPrixTotalHT();
			if ($total_HT < $myPromo['MONTANTVALID']) {
				$result = sprintf("%.2f", $myPromo['MONTANTVALID'] - $total_HT);
			}
		}
		return $result;
	}
	public function isCommandValid($prixHT) {
		$myPromo = $this->promoCommand->getCommandValid();
		if ($myPromo) {
			if ($prixHT < $myPromo['MONTANTVALID']) {  return false ; }
		}
		return true;
	}



	public function computeAllPromos($facture, $user) {
		foreach ($facture->facture_lines AS $item) {
			if (!$item->isCaddyType) {
				if ($item->isSurDevis()) {
					$item->item_prix = 0;
				} else {
					$this->computePrixProductByReference($item);
					if (!$item->isRemiseChanged()) { $this->computePrixProductByMarque($item); }
					if (!$item->isRemiseChanged()) { $this->computePrixProductByCategory($item); }
					if (!$item->isRemiseChanged()) { $this->computePrixProductByQte($item, $facture->facture_lines); }
					if (!$item->isRemiseChanged()) { $this->computePrixProductByDateBetween($item); }


					if (isset($user) && !empty($user)) {
						$userId = $user['id'];
						$codeIntern = $user['cintern'];

						if (!empty($codeIntern)) { $this->computePrixUserByCodeInternMarque($item, $codeIntern); }
						if (!$item->isRemiseChanged()) { $this->computePrixUserByMarque($item, $userId); }
					}

					$this->computePrixCommandByProductQte($item, $facture);

					if (!empty($facture->code_reduction) && isset($facture->code_reduction['PRODUITREF']) && !empty($facture->code_reduction['PRODUITREF'])) {
						$facture->checkCodeReductionProduct($item->item_reference, $item->item_qte);
					}

					$poidsCurrent = $item->getPoidsTotal();
					if ($poidsCurrent > 0) { $facture->total_poids += $poidsCurrent ;
					} else { $facture->isAncienPoids = true;  }

					if ($item->isFrancoDenied()) { $facture->isFrancoDenied = true; }

					$facture->total_HT_HR += $item->getPrixTotalHT(true);
				}
			}
		}

		$this->computePrixCommandByTotalHT($facture);
		$this->computePrixCommandByAll($facture);
			
		if (isset($user) && !empty($user)) {
			$this->computePrixUserByDateInscription($facture, $user['dateinsc']);

			$this->computePrixUserByUser($facture, $user['id']);

			$this->computePrixUserByIsFirstCommand($facture, $user['id']);
		}
			
		$this->computePrixCodeReduction($facture);
	}

	public function computeItemsPromosCaddyType($caddyItems, $user) {
		foreach ($caddyItems AS $item) {
			if (!$item->isPromo()) {
				$promoProduct = new PromoProduct();
				$promoUser = new PromoUser();

				$myPromo = $promoProduct->getRemiseByProductReference($item->reference);
				if ($myPromo) { $item->setRemise($myPromo['REMISEEURO'], $myPromo['REMISEPOUR']); }
 
				if (!$item->isRemiseChanged()) { 
					$myPromo = $promoProduct->getRemiseByProductBrend($item->idBrend);
					if ($myPromo) { $item->setRemise($myPromo['REMISEEURO'], $myPromo['REMISEPOUR']); }
				}

				if (!$item->isRemiseChanged()) {
					$myPromo = $promoProduct->getRemiseByProductCategory($item->idcategory);
					if ($myPromo) { $item->setRemise($myPromo['REMISEEURO'], $myPromo['REMISEPOUR']); }
				}
				if (!$item->isRemiseChanged()) {
					$myPromo = $promoProduct->getRemisesByProductQte($item->reference, $item->qte);
					if ($myPromo) {
						foreach ($caddyItems AS $line) {
							foreach ($myPromo AS $promo) {
								if (($line->reference == $promo['CHILDREFBUY']) &&
								($line->qte >= $promo['CHILDREFBUYNB']) ) {
									$item->setRemise($promo['REMISEEURO'], $promo['REMISEPOUR']);
								}
							}
						}
					}
				}
				if (!$item->isRemiseChanged()) {
					$myPromo = $promoProduct->getRemiseByProductDateBetween($item->promo_date);
					if ($myPromo) { $item->setRemise($myPromo['REMISEEURO'], $myPromo['REMISEPOUR']); }
				}
				if (isset($user) && !empty($user)) {
					$userId = $user['id'];
					$codeIntern = $user['cintern'];

					if (!empty($codeIntern)) {
						$myPromo = $promoUser->getRemiseByCodeInternMarque($item->idBrend, $codeIntern);
						if ($myPromo) { $item->setRemise($myPromo['REMISEEURO'], $myPromo['REMISEPOUR']); }
					}
					if (!$item->isRemiseChanged()) {
						$myPromo = $promoUser->getRemiseByMarque($item->idBrend, $userId);
						if ($myPromo) { $item->setRemise($myPromo['REMISEEURO'], $myPromo['REMISEPOUR']); }
					}
				}
			}
		}
	}
}
?>