actions.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. /**
  3. * kataoPeriod actions.
  4. *
  5. * @package www.katao.fr
  6. * @subpackage kataoPeriod
  7. * @author Your name here
  8. * @version SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
  9. */
  10. class kataoPeriodActions extends wpActions {
  11. public function executeIndex() {
  12. $criteria = new Criteria();
  13. $sort_method = $this->getCriteriaSortMethod();
  14. switch ($this->sort = $this->getRequestParameter('sort', 'name')) {
  15. case 'begin':
  16. $criteria->$sort_method(KataoPeriodPeer::BEGIN_AT);
  17. break;
  18. case 'end':
  19. $criteria->$sort_method(KataoPeriodPeer::FINISH_AT);
  20. break;
  21. case 'order':
  22. $criteria->$sort_method(KataoPeriodPeer::ORDER_ENDED_AT);
  23. break;
  24. case 'active':
  25. $criteria->$sort_method(KataoPeriodPeer::IS_ACTIVE);
  26. break;
  27. default:
  28. $criteria->$sort_method(KataoPeriodPeer::NAME);
  29. }
  30. if ('' != $this->filter_status = $this->getUser()->getAttribute('katao_period_status')) {
  31. $criteria->add(KataoPeriodPeer::STATUS, $this->filter_status);
  32. }
  33. $pager = new sfPropelPager('KataoPeriod', sfConfig::get('app_pager'));
  34. $pager->setCriteria($criteria);
  35. $pager->setPage($this->getRequestParameter('page', 1));
  36. $pager->init();
  37. $this->pager = $pager;
  38. $this->katao_periodList = $pager->getResults();
  39. }
  40. public function executeFilter($request) {
  41. $this->getUser()->setAttribute('katao_period_status', $request->getParameter('filter_status'));
  42. return $this->redirect('kataoPeriod/index');
  43. }
  44. public function executeFilterReset($request) {
  45. $this->getUser()->setAttribute('katao_period_status', '');
  46. return $this->redirect('kataoPeriod/index');
  47. }
  48. public function executeShow($request) {
  49. $this->katao_period = KataoPeriodPeer::retrieveByPk($request->getParameter('id'));
  50. $this->redirectWithErrorUnless($this->katao_period, 'Période #' . $request->getParameter('id') . ' inconnue.');
  51. }
  52. public function executeCreate() {
  53. $this->form = new KataoPeriodForm();
  54. $this->katao_suppliers = KataoSupplierPeer::getAllToSortArray();
  55. $this->products = KataoProductPeer::getSelectedProducts(array_keys($this->katao_suppliers['some_product']));
  56. $this->setTemplate('edit');
  57. }
  58. public function executeEdit($request) {
  59. $this->form = new KataoPeriodForm(KataoPeriodPeer::retrieveByPk($request->getParameter('id')));
  60. $this->katao_suppliers = KataoSupplierPeer::getAllToSortArray($request->getParameter('id'));
  61. $this->products = KataoProductPeer::getSelectedProducts(array_keys($this->katao_suppliers['some_product']), $request->getParameter('id'));
  62. }
  63. public function executeUpdate($request) {
  64. $this->redirectWithErrorUnless($request->isMethod('post'));
  65. $this->form = new KataoPeriodForm(KataoPeriodPeer::retrieveByPk($request->getParameter('id')));
  66. $this->form->bind($request->getParameter('katao_period'));
  67. if ($this->form->isValid()) {
  68. $katao_period = $this->form->save();
  69. wpFlashMessages::addConfirmation('Période "' . $katao_period->getName() . '" sauvée avec succès.');
  70. $this->redirect('kataoPeriod/index');
  71. }
  72. $this->katao_suppliers = KataoSupplierPeer::getAllToSortArray($request->getParameter('id'));
  73. $this->products = KataoProductPeer::getSelectedProducts(array_keys($this->katao_suppliers['some_product']), $request->getParameter('id'));
  74. $this->setTemplate('edit');
  75. }
  76. public function executeDelete($request) {
  77. $this->redirectWithErrorUnless($katao_period = KataoPeriodPeer::retrieveByPk($request->getParameter('id')), 'Période #' . $request->getParameter('id') . ' inconnue.');
  78. $this->redirectWithErrorIf(!$katao_period->isDeletable());
  79. $katao_period->delete();
  80. wpFlashMessages::addConfirmation('Période "' . $katao_period->getName() . '" supprimée avec succès.');
  81. $this->redirect('kataoPeriod/index');
  82. }
  83. public function executeDeleteMultiple($request) {
  84. if (!wpPersistenceManager::isEmpty('katao_period')) {
  85. foreach (KataoPeriodPeer::retrieveByPKs(array_keys(wpPersistenceManager::getSelectedItems('katao_period'))) as/*(KataoPeriod)*/ $katao_period) {
  86. if ($katao_period->isDeletable()) {
  87. $katao_period->delete();
  88. }
  89. }
  90. wpPersistenceManager::cleanSelection('katao_period');
  91. wpFlashMessages::addConfirmation('Les périodes sélectionnées ont été supprimées avec succès.');
  92. } else {
  93. wpFlashMessages::addWarning('Merci de sélectionner au moins une période.');
  94. }
  95. $this->redirect('kataoPeriod/index');
  96. }
  97. public function executeRefreshProducts($request) {
  98. return $this->renderText('(' . Utils::array_to_json_string(KataoProductPeer::getSelectedProducts(explode(',', $request->getParameter('suppliers_ids')), $request->getParameter('id'))) . ')');
  99. }
  100. public function executeActivate($request) {
  101. $this->redirectWithErrorUnless($katao_period = KataoPeriodPeer::retrieveByPk($request->getParameter('id')), 'Période #' . $request->getParameter('id') . ' inconnue.');
  102. $katao_period->activate();
  103. wpFlashMessages::addConfirmation('Période "' . $katao_period->getName() . '" activée avec succès.');
  104. $this->redirect('kataoPeriod/index');
  105. }
  106. public function executeExportAccounting($request) {
  107. $katao_periods = KataoPeriodPeer::getAllWithDates();
  108. $export_date_from = date('Y-m-d 00:00:00', Utils::getDateFromInput($request->getParameter('date_from')));
  109. $export_date_to = date('Y-m-d 23:59:59', Utils::getDateFromInput($request->getParameter('date_to')));
  110. $content = array();
  111. /* Liste des dépôts validés de la période en question */
  112. $criteria = new Criteria();
  113. $criteria->add(KataoMemberDepositPeer::PAYMENT_MODE, KataoMemberDeposit::PAYMENT_MODE_TRANSFER_SOL, Criteria::NOT_EQUAL);
  114. $criteria->add(KataoMemberDepositPeer::STATUS, KataoMemberDeposit::STATUS_VALIDATED);
  115. $criterion = $criteria->getNewCriterion(KataoMemberDepositPeer::VALUED_AT, $export_date_from, Criteria::GREATER_EQUAL);
  116. $criterion->addAnd($criteria->getNewCriterion(KataoMemberDepositPeer::VALUED_AT, $export_date_to, Criteria::LESS_EQUAL));
  117. $criteria->addAnd($criterion);
  118. $criteria->addAscendingOrderByColumn(KataoMemberDepositPeer::VALUED_AT);
  119. foreach (KataoMemberDepositPeer::doSelectJoinKataoMember($criteria) as/*(KataoMemberDeposit)*/ $katao_member_deposit) {
  120. $katao_period_id = KataoPeriodPeer::getIdFromRangeDate($katao_member_deposit->getValuedAt('Y-m-d'), $katao_periods);
  121. $katao_member = $katao_member_deposit->getKataoMember();
  122. $date_ecriture = $katao_member_deposit->getValuedAt('Ymd');
  123. $date_echeance = $katao_member_deposit->getValuedAt('Ymd');
  124. $num_piece = sprintf('DE%04d', $katao_member_deposit->getId());
  125. if (KataoMemberDeposit::PAYMENT_MODE_CHECK == $katao_member_deposit->getPaymentMode()) {
  126. $libelle_ecriture = sprintf('%s%s%s - %s', $katao_member_deposit->getCheckDeposit()?sprintf('%s-', $katao_member_deposit->getCheckDeposit()):'', $katao_member_deposit->getCheckBank(), $katao_member_deposit->getCheckNumber(), $katao_member->getLastName());
  127. } else {
  128. $libelle_ecriture = sprintf('%s - %s', $katao_member_deposit->getPaymentModeStr(), $katao_member->getLastName());
  129. }
  130. $num_pointage = '';
  131. $code_analytique_budgetaire = '';
  132. $libelle_compte = $katao_member->getFullName();
  133. if (KataoMemberDeposit::CURRENCY_SOL == $katao_member_deposit->getCurrency()) {
  134. $journal = 'SO';
  135. $num_mouvement = $katao_period_id . '4';
  136. $montant = $katao_member_deposit->getAmount() / CONST_EURO_TO_SOL;
  137. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, wpConfig::getAccountingCodeSolDiscount(), $libelle_ecriture, $montant, (KataoMemberDeposit::PAYMENT_MODE_WITHDRAWAL != $katao_member_deposit->getPaymentMode())?'D':'C', $num_pointage, $code_analytique_budgetaire, 'REMISE SOL');
  138. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, $katao_member->getAccountingCodeSol(), $libelle_ecriture, $montant, (KataoMemberDeposit::PAYMENT_MODE_WITHDRAWAL != $katao_member_deposit->getPaymentMode())?'C':'D', $num_pointage, $code_analytique_budgetaire, $libelle_compte . '-' . wpConfig::getAdditionalCurrencyName());
  139. } else {
  140. $journal = 'BQ';
  141. $num_mouvement = $katao_period_id . '3';
  142. $montant = $katao_member_deposit->getAmount();
  143. $num_compte = (KataoMemberDeposit::PAYMENT_MODE_CREDIT == $katao_member_deposit->getPaymentMode())?wpConfig::getAccountingCodeCredit():wpConfig::getAccountingCodeBank();
  144. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, $num_compte, $libelle_ecriture, $montant, (KataoMemberDeposit::PAYMENT_MODE_WITHDRAWAL != $katao_member_deposit->getPaymentMode())?'D':'C', $num_pointage, $code_analytique_budgetaire, 'CAISSE D\'EPARGNE');
  145. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, $katao_member->getAccountingCode(), $libelle_ecriture, $montant, (KataoMemberDeposit::PAYMENT_MODE_WITHDRAWAL != $katao_member_deposit->getPaymentMode())?'C':'D', $num_pointage, $code_analytique_budgetaire, $libelle_compte);
  146. }
  147. }
  148. /* Liste des factures fournisseurs de la période */
  149. $criteria = new Criteria();
  150. $criterion = $criteria->getNewCriterion(KataoSupplierInvoicePeer::INVOICE_DATE, $export_date_from, Criteria::GREATER_EQUAL);
  151. $criterion->addAnd($criteria->getNewCriterion(KataoSupplierInvoicePeer::INVOICE_DATE, $export_date_to, Criteria::LESS_EQUAL));
  152. $criteria->addAnd($criterion);
  153. $criteria->addAscendingOrderByColumn(KataoSupplierInvoicePeer::INVOICE_DATE);
  154. foreach (KataoSupplierInvoicePeer::doSelectJoinAll($criteria) as/*(KataoSupplierInvoice)*/ $katao_supplier_invoice) {
  155. $katao_period = $katao_supplier_invoice->getKataoPeriod();
  156. $katao_supplier = $katao_supplier_invoice->getKataoSupplier();
  157. $journal = 'HA';
  158. $num_mouvement = $katao_period->getId() . '2';
  159. $date_ecriture = $katao_supplier_invoice->getInvoiceDate('Ymd');
  160. $date_echeance = $katao_supplier_invoice->getDueDate('Ymd');
  161. $num_piece = $katao_supplier_invoice->getReference();
  162. $libelle_ecriture = $katao_period->getName();
  163. $num_pointage = '';
  164. $code_analytique_budgetaire = '';
  165. $libelle_compte = '';
  166. $total_sol = $katao_supplier_invoice->getPaymentAmountSol() / CONST_EURO_TO_SOL;
  167. // enlever la valeur en sol du montant total en €
  168. $total_euro = $katao_supplier_invoice->getTotalHT() + $katao_supplier_invoice->getTotalTva() - $total_sol;
  169. $total_credit = $total_euro;
  170. // pour vérifier que total débit = total crédit sans les sols
  171. $total_debit = - $total_sol;
  172. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, $katao_supplier->getAccountingCode(), $libelle_ecriture, $total_euro, 'C', $num_pointage, $code_analytique_budgetaire, $katao_supplier->getName());
  173. if ($total_sol) {
  174. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, $katao_supplier->getAccountingCodeSol(), $libelle_ecriture, $total_sol, 'C', $num_pointage, $code_analytique_budgetaire, $katao_supplier->getName() . '-' . wpConfig::getAdditionalCurrencyName());
  175. }
  176. $line_by_accounting_code = array();
  177. foreach ($katao_supplier_invoice->getKataoSupplierInvoiceProductsJoinKataoProduct() as/*(KataoSupplierInvoiceProduct)*/ $katao_supplier_invoice_product) {
  178. $katao_product = $katao_supplier_invoice_product->getKataoProduct();
  179. if (!isset($line_by_accounting_code[$katao_product->getAccountingCodePurchase()]['amount'])) {
  180. $line_by_accounting_code[$katao_product->getAccountingCodePurchase()]['amount'] = 0;
  181. }
  182. $line_by_accounting_code[$katao_product->getAccountingCodePurchase()]['amount'] += $katao_supplier_invoice_product->getProductPriceTotal();
  183. $line_by_accounting_code[$katao_product->getAccountingCodePurchase()]['category'] = sprintf('Ach : %s', $katao_product->getKataoProductFamily()->getKataoProductCategory()->getName());
  184. }
  185. foreach ($line_by_accounting_code as $accounting_code => $data) {
  186. if (!empty($data['amount'])) {
  187. $total_debit += $data['amount'];
  188. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, $accounting_code, $libelle_ecriture, $data['amount'], 'D', $num_pointage, $code_analytique_budgetaire, $data['category']);
  189. }
  190. }
  191. $discount = $katao_supplier_invoice->getTaux0DiscountHt() + $katao_supplier_invoice->getTaux1DiscountHt() + $katao_supplier_invoice->getTaux2DiscountHt();
  192. $tva_rates = array('0.196' => 1, '0.055' => 2, '0' => 0);
  193. $tva_5_5 = $katao_supplier_invoice->getTaux2Amount();
  194. $tva_19_6 = $katao_supplier_invoice->getTaux1Amount();
  195. if ($katao_supplier_invoice->getFeesBillingTva() && !empty($tva_rates[(string)$katao_supplier_invoice->getFeesBillingRate()])) {
  196. switch ($tva_rates[(string)$katao_supplier_invoice->getFeesBillingRate()]) {
  197. case 1:
  198. $tva_19_6 += $katao_supplier_invoice->getFeesBillingTva();
  199. break;
  200. case 2:
  201. $tva_5_5 += $katao_supplier_invoice->getFeesBillingTva();
  202. break;
  203. }
  204. }
  205. if ($katao_supplier_invoice->getFeesShippingTva() && !empty($tva_rates[(string)$katao_supplier_invoice->getFeesShippingRate()])) {
  206. switch ($tva_rates[(string)$katao_supplier_invoice->getFeesShippingRate()]) {
  207. case 1:
  208. $tva_19_6 += $katao_supplier_invoice->getFeesShippingTva();
  209. break;
  210. case 2:
  211. $tva_5_5 += $katao_supplier_invoice->getFeesShippingTva();
  212. break;
  213. }
  214. }
  215. if ($tva_19_6) {
  216. $total_debit += $tva_19_6;
  217. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, '445664', $libelle_ecriture, $tva_19_6, 'D', $num_pointage, $code_analytique_budgetaire, 'TVA 19.6%');
  218. }
  219. if ($tva_5_5) {
  220. $total_debit += $tva_5_5;
  221. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, '445662', $libelle_ecriture, $tva_5_5, 'D', $num_pointage, $code_analytique_budgetaire, 'TVA 5.5%');
  222. }
  223. if ($katao_supplier_invoice->getFeesBillingHt()) {
  224. $total_debit += $katao_supplier_invoice->getFeesBillingHt();
  225. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, wpConfig::getAccountingCodeFeesBilling(), sprintf('FRSDEFACT-%s', $katao_supplier->getName()), $katao_supplier_invoice->getFeesBillingHt(), 'D', $num_pointage, $code_analytique_budgetaire, $katao_supplier->getName());
  226. }
  227. if ($katao_supplier_invoice->getFeesShippingHt()) {
  228. $total_debit += $katao_supplier_invoice->getFeesShippingHt();
  229. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, wpConfig::getAccountingCodeFeesShipping(), sprintf('FRSDEPORT-%s', $katao_supplier->getName()), $katao_supplier_invoice->getFeesShippingHt(), 'D', $num_pointage, $code_analytique_budgetaire, $katao_supplier->getName());
  230. }
  231. if ($discount) {
  232. $total_credit += $discount;
  233. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, wpConfig::getAccountingCodeDiscount(), sprintf('REMISE-%s', $katao_supplier->getName()), $discount, 'C', $num_pointage, $code_analytique_budgetaire, $katao_supplier->getName());
  234. }
  235. if (round(abs($total_debit - $total_credit), 2)) {
  236. if ($total_debit > $total_credit) {
  237. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, wpConfig::getAccountingCodeErrorAdjustmentPositive(), $libelle_ecriture, $total_debit - $total_credit, 'C', $num_pointage, $code_analytique_budgetaire, 'Ecart de conversion');
  238. } elseif ($total_debit < $total_credit) {
  239. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, wpConfig::getAccountingCodeErrorAdjustmentNegative(), $libelle_ecriture, $total_credit - $total_debit, 'D', $num_pointage, $code_analytique_budgetaire, 'Ecart de conversion');
  240. }
  241. }
  242. }
  243. /* Liste des factures fournisseurs payées de la période */
  244. $criteria = new Criteria();
  245. $criterion = $criteria->getNewCriterion(KataoSupplierInvoicePeer::PAYMENT_DATE, $export_date_from, Criteria::GREATER_EQUAL);
  246. $criterion->addAnd($criteria->getNewCriterion(KataoSupplierInvoicePeer::PAYMENT_DATE, $export_date_to, Criteria::LESS_EQUAL));
  247. $criteria->addAnd($criterion);
  248. $criteria->addAscendingOrderByColumn(KataoSupplierInvoicePeer::INVOICE_DATE);
  249. foreach (KataoSupplierInvoicePeer::doSelectJoinAll($criteria) as/*(KataoSupplierInvoice)*/ $katao_supplier_invoice) {
  250. $katao_period = $katao_supplier_invoice->getKataoPeriod();
  251. $katao_supplier = $katao_supplier_invoice->getKataoSupplier();
  252. $num_mouvement = $katao_period->getId() . '5';
  253. $date_ecriture = $katao_supplier_invoice->getPaymentDate('Ymd');
  254. $date_echeance = $katao_supplier_invoice->getPaymentDate('Ymd');
  255. $num_piece = $katao_supplier_invoice->getReference();
  256. $libelle_ecriture = sprintf('%s - %s', (KataoMemberDeposit::PAYMENT_MODE_CHECK == $katao_supplier_invoice->getPaymentMode())?$katao_supplier_invoice->getPaymentDetails():$katao_supplier_invoice->getPaymentModeStr(), $katao_supplier->getName());
  257. $num_pointage = '';
  258. $code_analytique_budgetaire = '';
  259. $libelle_compte = '';
  260. $total_sol = $katao_supplier_invoice->getPaymentAmountSol() / CONST_EURO_TO_SOL;
  261. $total_euro = $katao_supplier_invoice->getTotalHT() + $katao_supplier_invoice->getTotalTva();
  262. $this->addAccountingLine($num_mouvement, 'BQ', $date_ecriture, $date_echeance, $num_piece, wpConfig::getAccountingCodeBank(), $libelle_ecriture, $total_euro, 'C', $num_pointage, $code_analytique_budgetaire, 'CAISSE D\'EPARGNE');
  263. if ($total_sol) {
  264. $this->addAccountingLine($num_mouvement, 'BQ', $date_ecriture, $date_echeance, $num_piece, wpConfig::getAccountingCodeBankSol(), $libelle_ecriture, $total_sol, 'C', $num_pointage, $code_analytique_budgetaire, 'COMPTE SOL');
  265. }
  266. $this->addAccountingLine($num_mouvement, 'BQ', $date_ecriture, $date_echeance, $num_piece, $katao_supplier->getAccountingCode(), $libelle_ecriture, $total_euro, 'D', $num_pointage, $code_analytique_budgetaire, $katao_supplier->getName());
  267. if ($total_sol) {
  268. $this->addAccountingLine($num_mouvement, 'BQ', $date_ecriture, $date_echeance, $num_piece, $katao_supplier->getAccountingCodeSol(), $libelle_ecriture, $total_sol, 'D', $num_pointage, $code_analytique_budgetaire, $katao_supplier->getName() . '-' . wpConfig::getAdditionalCurrencyName());
  269. }
  270. }
  271. /* Liste des factures adhérents envoyées de la période */
  272. $criteria = new Criteria();
  273. $criterion = $criteria->getNewCriterion(KataoInvoicePeer::CREATED_AT, $export_date_from, Criteria::GREATER_EQUAL);
  274. $criterion->addAnd($criteria->getNewCriterion(KataoInvoicePeer::CREATED_AT, $export_date_to, Criteria::LESS_EQUAL));
  275. $criteria->addAnd($criterion);
  276. $criteria->add(KataoInvoicePeer::STATUS, KataoInvoice::STATUS_SENT);
  277. $criteria->addAscendingOrderByColumn(KataoInvoicePeer::CREATED_AT);
  278. foreach (KataoInvoicePeer::doSelectJoinAllExceptKataoCart($criteria) as/*(KataoInvoice)*/ $katao_invoice) {
  279. $katao_period = $katao_invoice->getKataoPeriod();
  280. $katao_member = $katao_invoice->getKataoMember();
  281. $journal = 'VT';
  282. $num_mouvement = $katao_period->getId() . '1';
  283. $date_ecriture = $katao_invoice->getCreatedAt('Ymd');
  284. $date_echeance = $katao_invoice->getCreatedAt('Ymd');
  285. $num_piece = $katao_invoice->getNumber();
  286. $libelle_ecriture = sprintf('%s - %s', $katao_period->getName(), $katao_invoice->getKataoNode()->getCity());
  287. $num_pointage = '';
  288. $code_analytique_budgetaire = '';
  289. $libelle_compte = '';
  290. $total_by_tva_rate = array('0.196' => 0, '0.055' => 0, '0' => 0);
  291. $total_debit = $katao_invoice->sumProducts();
  292. $total_debit_sol = $katao_invoice->getSolAmount() / CONST_EURO_TO_SOL;
  293. $total_debit_euro = $total_debit - $total_debit_sol;
  294. $total_credit = 0;
  295. $libelle_compte = $katao_member->getFullName();
  296. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, $katao_member->getAccountingCode(), $libelle_ecriture, $total_debit_euro, 'D', $num_pointage, $code_analytique_budgetaire, $libelle_compte);
  297. if ($total_debit_sol) {
  298. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, $katao_member->getAccountingCodeSol(), $libelle_ecriture, $total_debit_sol, 'D', $num_pointage, $code_analytique_budgetaire, $libelle_compte . '-' . wpConfig::getAdditionalCurrencyName());
  299. }
  300. $libelle_compte = '';
  301. $amounts_by_accounting_code_and_tva = array();
  302. $line_by_accounting_code = array();
  303. foreach ($katao_invoice->getKataoInvoiceProductsJoinKataoProduct() as/*(KataoInvoiceProduct)*/ $katao_invoice_product) {
  304. $katao_product = $katao_invoice_product->getKataoProduct();
  305. $famille = $katao_product->getAccountingCodeSell();
  306. $tva = (string)$katao_invoice_product->getProductTvaRate();
  307. $amounts_by_accounting_code_and_tva[$famille]['name'] = $katao_product->getKataoProductFamily()->getKataoProductCategory()->getName();
  308. if (!isset($amounts_by_accounting_code_and_tva[$famille]['values'][$tva]['ttc'])) {
  309. $amounts_by_accounting_code_and_tva[$famille]['values'][$tva]['ttc'] = 0;
  310. }
  311. $amounts_by_accounting_code_and_tva[$famille]['values'][$tva]['ttc'] += round($katao_invoice_product->getQuantityDelivered() * round($katao_invoice_product->getProductPriceWithTaxes(), 2), 2);
  312. $amount = $katao_invoice_product->getQuantityDelivered() * $katao_invoice_product->getProductPriceWithoutTaxes();
  313. }
  314. $total_by_tva_rate = array();
  315. foreach ($amounts_by_accounting_code_and_tva as $amount_accounting_code => $amount_accounting_code_tvas) {
  316. foreach ($amount_accounting_code_tvas['values'] as $amount_tva => $amount_tva_values) {
  317. if (!isset($amount_tva_values['ttc'])) {
  318. $amount_tva_values['ttc'] = 0;
  319. $amounts_by_accounting_code_and_tva[$amount_accounting_code]['values'][$amount_tva]['ttc'] = 0;
  320. }
  321. $value = round($amount_tva_values['ttc'] / (1 + (float)$amount_tva), 2);
  322. $amounts_by_accounting_code_and_tva[$amount_accounting_code]['values'][$amount_tva]['ht'] = $value;
  323. if (!isset($total_by_tva_rate[$amount_tva])) {
  324. $total_by_tva_rate[$amount_tva] = 0;
  325. }
  326. $total_by_tva_rate[$amount_tva] += max(0, $amounts_by_accounting_code_and_tva[$amount_accounting_code]['values'][$amount_tva]['ttc'] - $amounts_by_accounting_code_and_tva[$amount_accounting_code]['values'][$amount_tva]['ht']);
  327. }
  328. }
  329. foreach ($amounts_by_accounting_code_and_tva as $amount_accounting_code => $amount_accounting_code_tvas) {
  330. $amount = 0;
  331. foreach ($amount_accounting_code_tvas['values'] as $amount_tva => $amount_tva_values) {
  332. $amount += $amount_tva_values['ht'];
  333. }
  334. $total_credit += round($amount, 2);
  335. $line_by_accounting_code[$amount_accounting_code]['amount'] = $amount;
  336. $line_by_accounting_code[$amount_accounting_code]['category'] = sprintf('Vts : %s', $amount_accounting_code_tvas['name']);
  337. }
  338. foreach ($line_by_accounting_code as $accounting_code => $data) {
  339. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, $accounting_code, $libelle_ecriture, $data['amount'], 'C', $num_pointage, $code_analytique_budgetaire, $data['category']);
  340. }
  341. if (!empty($total_by_tva_rate['0.196'])) {
  342. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, '445714', $libelle_ecriture, $total_by_tva_rate['0.196'], 'C', $num_pointage, $code_analytique_budgetaire, 'TVA 19.6%');
  343. $total_credit += round($total_by_tva_rate['0.196'], 2);
  344. }
  345. if (!empty($total_by_tva_rate['0.055'])) {
  346. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, '445712', $libelle_ecriture, $total_by_tva_rate['0.055'], 'C', $num_pointage, $code_analytique_budgetaire, 'TVA 5.5%');
  347. $total_credit += round($total_by_tva_rate['0.055'], 2);
  348. }
  349. if (round(abs($total_debit - $total_credit), 2)) {
  350. if ($total_debit > $total_credit) {
  351. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, wpConfig::getAccountingCodeErrorAdjustmentPositive(), $libelle_ecriture, $total_debit - $total_credit, 'C', $num_pointage, $code_analytique_budgetaire, 'Ecart de conversion');
  352. } elseif ($total_debit < $total_credit) {
  353. $this->addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, wpConfig::getAccountingCodeErrorAdjustmentNegative(), $libelle_ecriture, $total_credit - $total_debit, 'D', $num_pointage, $code_analytique_budgetaire, 'Ecart de conversion');
  354. }
  355. }
  356. }
  357. $content = implode("\n", $this->accounting_lines);
  358. $response = $this->getResponse();
  359. $response->setContentType('application/octet-stream');
  360. $response->setHttpHeader('Content-disposition', 'attachment; filename="XIMPORT.TXT"');
  361. $response->setHttpHeader('Content-Length', strlen($content));
  362. $response->setHttpHeader('Pragma', 'public');
  363. $response->setHttpHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
  364. $response->setHttpHeader('Expires', '0');
  365. $response->setContent($content);
  366. return sfView::NONE;
  367. }
  368. protected $accounting_lines = array();
  369. protected function addAccountingLine($num_mouvement, $journal, $date_ecriture, $date_echeance, $num_piece, $num_compte, $libelle_ecriture, $montant, $sens_operation, $num_pointage, $code_analytique_budgetaire, $libelle_compte) {
  370. $libelle_ecriture = Utils::removeAccents(utf8_decode($libelle_ecriture));
  371. $libelle_compte = Utils::removeAccents(utf8_decode($libelle_compte));
  372. $this->accounting_lines[] = sprintf('%s%s%s%s%s%s%s%s%s%s%s%s02003', Utils::completeWithSpaces($num_mouvement, 5, true), $journal,
  373. $date_ecriture, $date_echeance,
  374. Utils::completeWithSpaces($num_piece, 12), Utils::completeWithSpaces($num_compte, 11),
  375. Utils::completeWithSpaces($libelle_ecriture, 25), Utils::completeWithSpaces(sprintf('%0.2f', $montant), 13, true), $sens_operation,
  376. Utils::completeWithSpaces($num_pointage, 12), Utils::completeWithSpaces('', 6),
  377. Utils::completeWithSpaces($libelle_compte, 34));
  378. }
  379. public function executeCopy($request) {
  380. $this->redirectWithErrorUnless($katao_period = KataoPeriodPeer::retrieveByPk($request->getParameter('id')), 'Période #' . $request->getParameter('id') . ' inconnue.');
  381. if ($katao_period_id = $katao_period->makePeriodCopy()) {
  382. wpFlashMessages::addConfirmation('Période "' . $katao_period->getName() . '" copiée avec succès.');
  383. $this->redirect('kataoPeriod/edit?id=' . $katao_period_id);
  384. }
  385. wpFlashMessages::addError('Impossible de copier la période "' . $katao_period->getName() . '".');
  386. $this->redirect('kataoPeriod/index');
  387. }
  388. }