KataoProductForm.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * KataoProduct form.
  4. *
  5. * @package katao
  6. * @subpackage form
  7. * @author Your name here
  8. * @version SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
  9. */
  10. class KataoProductForm extends BaseKataoProductForm {
  11. public function configure() {
  12. unset($this->validatorSchema['url_identifier'], $this->validatorSchema['description'], $this->validatorSchema['created_at'], $this->validatorSchema['updated_at']);
  13. unset($this->widgetSchema['url_identifier'], $this->widgetSchema['description'], $this->widgetSchema['created_at'], $this->widgetSchema['updated_at']);
  14. $this->widgetSchema['reference'] = new sfWidgetFormInput(array(), array('style' => 'width: 250px'));
  15. $this->widgetSchema['name'] = new sfWidgetFormInput(array(), array('style' => 'width: 250px'));
  16. $this->widgetSchema['accounting_code_purchase'] = new sfWidgetFormInput(array(), array('style' => 'width: 40px'));
  17. $this->widgetSchema['accounting_code_sell'] = new sfWidgetFormInput(array(), array('style' => 'width: 40px'));
  18. $this->widgetSchema['min_order_number'] = new sfWidgetFormInput(array(), array('style' => 'width: 20px'));
  19. $this->widgetSchema['delivery_delay'] = new sfWidgetFormInput(array(), array('style' => 'width: 20px'));
  20. $this->widgetSchema['max_sol_amount'] = new sfWidgetFormInput(array(), array('style' => 'width: 54px'));
  21. $this->widgetSchema['unit_price_euro'] = new sfWidgetFormInput(array(), array('style' => 'width: 54px'));
  22. $this->widgetSchema['margin'] = new sfWidgetFormInput(array(), array('style' => 'width: 54px'));
  23. $this->widgetSchema['katao_product_family_id'] = new sfWidgetFormSelect(array('choices' => KataoProductFamilyPeer::getAllGroupedByCategory()));
  24. $this->validatorSchema['accounting_code_purchase'] = new sfValidatorPass();
  25. $this->validatorSchema['accounting_code_sell'] = new sfValidatorPass();
  26. $sf_user = sfContext::getInstance()->getUser();
  27. if ($sf_user->isJustReferer()) {
  28. $this->widgetSchema['katao_supplier_id'] = new sfWidgetFormSelect(array('choices' => KataoSupplierPeer::getAllByRefererId($sf_user->getInstance()->getKataoMemberId())));
  29. } elseif ($sf_user->isJustDelegate()) {
  30. $this->widgetSchema['katao_supplier_id'] = new sfWidgetFormSelect(array('choices' => KataoSupplierPeer::getAllLocalToNode($sf_user->getInstance()->getKataoMember()->getKataoNodeId())));
  31. } else {
  32. $this->widgetSchema['katao_supplier_id'] = new sfWidgetFormSelect(array('choices' => KataoSupplierPeer::getAllSimple()));
  33. }
  34. if ($sf_user->isJustSupplier()) {
  35. unset($this->validatorSchema['accounting_code_purchase'], $this->validatorSchema['accounting_code_sell']);
  36. unset($this->widgetSchema['accounting_code_purchase'], $this->widgetSchema['accounting_code_sell']);
  37. }
  38. if ($this->getObject()->isNew()) {
  39. $this->setDefault('margin', 0.15);
  40. }
  41. $this->widgetSchema['tva_rate'] = new sfWidgetFormSelect(array('choices' => array('0' => '0%', '0.055' => '5.5%', '0.196' => '19.6%')));
  42. $this->widgetSchema['is_archived'] = new wpWidgetFormInputCheckbox();
  43. $this->widgetSchema['is_archived']->setOption('value_attribute_value', 1);
  44. if ((bool)$this->getObject()->getIsArchived()) {
  45. $this->widgetSchema['is_archived']->setAttribute('checked', 'checked');
  46. }
  47. $this->widgetSchema['picture'] = new wpWidgetFormInputFile(array(), array('file_src' => $this->getObject()->getPicture()));
  48. $this->validatorSchema['picture'] = new sfValidatorFile(array('required' => false, 'mime_types' => array('image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif', 'image/jpeg; charset=binary')), array('mime_types' => 'Merci de choisir une image au format jpg, png ou gif (%mime_type%)'));
  49. $this->widgetSchema->setLabels(array(
  50. 'reference' => 'Référence',
  51. 'name' => 'Nom',
  52. 'katao_product_family_id' => 'Rayon/Famille',
  53. 'katao_supplier_id' => 'Fournisseur',
  54. 'max_sol_amount' => 'Montant maxi',
  55. 'unit_price_euro' => 'Prix d\'achat HT',
  56. 'tva_rate' => 'Taux de TVA',
  57. 'min_order_number' => 'Colisage',
  58. 'margin' => 'Marge',
  59. 'picture' => 'Photo',
  60. 'delivery_delay' => 'Délai de livraison',
  61. 'is_archived' => 'Archivé ?',
  62. 'accounting_code_purchase' => 'Achat',
  63. 'accounting_code_sell' => 'Vente',
  64. ));
  65. $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'checkMaxSolAmount')), array('invalid' => 'Le montant de la monnaie complémentaire est trop important.')));
  66. }
  67. public function checkMaxSolAmount($validator, $value) {
  68. $max_sol_amount = (int)sfContext::getInstance()->getRequest()->getParameter('katao_product[max_sol_amount]');
  69. $price = (float)sfContext::getInstance()->getRequest()->getParameter('katao_product[unit_price_euro]');
  70. $tva_rate = (float)sfContext::getInstance()->getRequest()->getParameter('katao_product[tva_rate]');
  71. $margin = (float)sfContext::getInstance()->getRequest()->getParameter('katao_product[margin]') / 100;
  72. if (10 * round($price * (1 + $margin) * (1 + $tva_rate), 2) < $max_sol_amount) {
  73. throw new sfValidatorError($validator, 'invalid');
  74. }
  75. return $value;
  76. }
  77. public function doSave($con = null) {
  78. $file = $this->getValue('picture');
  79. if ($file && is_object($file)) {
  80. if (is_file(sfConfig::get('sf_web_dir') . $this->getObject()->getPicture())) {
  81. unlink(sfConfig::get('sf_web_dir') . $this->getObject()->getPicture());
  82. }
  83. $filename = sha1($file->getOriginalName()) . $file->getExtension($file->getOriginalExtension());
  84. $file->save(sfConfig::get('sf_upload_dir') . '/repository/produits/' . $filename);
  85. } elseif (!empty($_REQUEST['RemovePicture'])) {
  86. if (is_file(sfConfig::get('sf_web_dir') . $this->getObject()->getPicture())) {
  87. unlink(sfConfig::get('sf_web_dir') . $this->getObject()->getPicture());
  88. }
  89. $this->getObject()->setPicture(null);
  90. }
  91. return parent::doSave($con);
  92. }
  93. public function updateObject($values = null) {
  94. $picture = $this->getObject()->getPicture();
  95. $object =/*(KataoProduct)*/ parent::updateObject($values);
  96. $file = $this->getValue('picture');
  97. if ($file && is_object($file)) {
  98. $object->setPicture(str_replace(sfConfig::get('sf_web_dir'), '', $object->getPicture()));
  99. } else {
  100. $object->setPicture($picture);
  101. }
  102. $object->setMargin($this->getValue('margin') / 100);
  103. return $object;
  104. }
  105. }