Advertisement
fadlyshafa

Untitled

Jun 23rd, 2020
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 66.22 KB | None | 0 0
  1. <?php
  2.  
  3. defined('BASEPATH') OR exit('No direct script access allowed');
  4.  
  5. require_once APPPATH . '/../vendor/autoload.php';
  6.  
  7. class Sales extends CI_Controller {
  8.  
  9. function __construct() {
  10. parent::__construct();
  11. $this->load->model('sales_model');
  12. $this->load->model('quotation_model');
  13. $this->load->model('customer_model');
  14. $this->load->model('category_model');
  15. $this->load->model('brand_model');
  16. $this->load->model('bank_model');
  17. $this->load->model('purchase_model');
  18. $this->load->model('company_setting_model');
  19. $this->load->model('kurs_model');
  20. $this->load->model('signature_model');
  21. $this->load->model('general_model');
  22. $this->load->model('log_model');
  23. }
  24.  
  25. public function randomKey($length) {
  26. $key = '';
  27. $pool = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
  28. for ($i = 0; $i < $length; $i++) {
  29. $key .= $pool[mt_rand(0, count($pool) - 1)];
  30. }
  31. return $key;
  32. }
  33.  
  34. public function alert($msg) {
  35. echo "<script type='text/javascript'>system.log('$msg');</script>";
  36. }
  37.  
  38. public function index() {
  39. // get all sales to display list
  40. $filter = null;
  41. if(isset($_GET['filter'])) {
  42. if ($_GET['filter'] != null) {
  43. $filter = $_GET['filter'];
  44. }
  45. }
  46.  
  47. $data['quotation'] = $this->quotation_model->getQuotation();
  48. $data['data'] = $this->sales_model->getSales($filter);
  49. $this->load->view('sales/list', $data);
  50. }
  51.  
  52. /*
  53. call add view to add sales record
  54. */
  55.  
  56. public function add() {
  57. $kursdata = $this->company_setting_model->getCurrency();
  58.  
  59. $data['country'] = $this->db->get('countries')->result();
  60. $data['brands'] = $this->brand_model->getBrand();
  61. $data['warehouse'] = $this->sales_model->getWarehouse();
  62. $data['quotation'] = $this->quotation_model->getQuotation();
  63. $data['warehouse_products'] = $this->sales_model->getWarehouseProducts();
  64. $data['tax'] = $this->sales_model->getTax();
  65. $data['biller'] = $this->sales_model->getBiller();
  66. $data['customer'] = $this->sales_model->getCustomer();
  67. $data['quatation'] = $this->sales_model->getQuatation();
  68. $data['product'] = $this->sales_model->getProducts2();
  69. $data['service'] = $this->sales_model->getService();
  70. $data['discount'] = $this->sales_model->getDiscount();
  71. $data['kursdata'] = $kursdata;
  72. $data['reference_no'] = $this->sales_model->createReferenceNo();
  73. $data['so_number'] = $this->general_model->generateAutoNumber('sales_order');
  74. $this->load->view('sales/add', $data);
  75. }
  76.  
  77. /*
  78. this function is used to get discount data when discount is change
  79. */
  80.  
  81. public function getDiscountAjax($id) {
  82. $data = $this->sales_model->getDiscountAjax($id);
  83. echo json_encode($data);
  84. }
  85.  
  86. /* get all product warehouse wise */
  87.  
  88. public function getProducts($warehouse_id) {
  89. $data = $this->sales_model->getProducts($warehouse_id);
  90. echo json_encode($data);
  91. }
  92.  
  93. public function getPartNumber($brand_id) {
  94. $data = $this->sales_model->getPartNumber($brand_id);
  95. echo json_encode($data);
  96. }
  97.  
  98. public function getCodeProduct($code) {
  99. $data = $this->sales_model->getCodeProduct($code);
  100. echo json_encode($data);
  101. }
  102.  
  103. /* get single product */
  104.  
  105. public function getProduct($product_id,$sales_id) {
  106. $datas = $this->sales_model->getProducts21($product_id);
  107. // print_r(var_dump($this->db->last_query()));
  108. for ($i=0; $i < count($datas) ; $i++) {
  109.  
  110. if ($datas[$i]->product_grouping != NULL) {
  111. $arlist = json_encode(unserialize($datas[$i]->list));
  112. $arlist = json_decode($arlist);
  113. $max = array();
  114. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  115.  
  116. $qtyp = $this->db->select('quantity')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->quantity;
  117. $count = ($qtyp / intval($arlist[$ilist]->ql));
  118. array_push($max, $count);
  119. }
  120. $min = min($max);
  121. $datas[$i]->quantity = intval($min);
  122. }
  123. }
  124.  
  125. $data['items'] = $this->sales_model->getProducts99($sales_id);
  126.  
  127. $data['discount'] = $this->sales_model->getDiscount();
  128. $data['bonus'] = $this->sales_model->getBonus($product_id);
  129. $data['bonus1'] = $this->sales_model->getBonus12();
  130. $data['tax'] = $this->sales_model->getTax();
  131. echo json_encode($data);
  132. }
  133.  
  134. public function getProduct_old($product_id) {
  135. $data = $this->sales_model->getProducts21($product_id);
  136. // print_r(var_dump($this->db->last_query()));
  137. for ($i=0; $i < count($data) ; $i++) {
  138.  
  139. if ($data[$i]->product_grouping != NULL) {
  140. $arlist = json_encode(unserialize($data[$i]->list));
  141. $arlist = json_decode($arlist);
  142. $max = array();
  143. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  144.  
  145. $qtyp = $this->db->select('quantity')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->quantity;
  146. $count = ($qtyp / intval($arlist[$ilist]->ql));
  147. array_push($max, $count);
  148. }
  149. $min = min($max);
  150. $data[$i]->quantity = intval($min);
  151. }
  152. }
  153.  
  154. // $data['items'] = $this->sales_model->getProducts99($sales_id);
  155.  
  156. $data['discount'] = $this->sales_model->getDiscount();
  157. $data['bonus'] = $this->sales_model->getBonus($product_id);
  158. $data['bonus1'] = $this->sales_model->getBonus12();
  159. $data['tax'] = $this->sales_model->getTax();
  160. echo json_encode($data);
  161. }
  162.  
  163. public function getService($service_id) {
  164. $data = $this->sales_model->getServicetable($service_id);
  165. $data['discount'] = $this->sales_model->getDiscount();
  166. $data['tax'] = $this->sales_model->getTax();
  167. echo json_encode($data);
  168. }
  169.  
  170. /*
  171. this function is used to search product name / code in auto complite
  172. */
  173.  
  174. public function getAutoCodeName($code, $search_option, $warehouse) {
  175. //$code = strtolower($code);
  176. $p_code = $this->input->post('p_code');
  177. $p_search_option = $this->input->post('p_search_option');
  178. $data = $this->sales_model->getProductCodeName($p_code, $p_search_option, $warehouse);
  179. if ($search_option == "Code") {
  180. $list = "<ul class='auto-product'>";
  181. foreach ($data as $val) {
  182. $list .= "<li value=" . $val->code . ">" . $val->code . "</li>";
  183. }
  184. $list .= "</ul>";
  185. } else {
  186. $list = "<ul class='auto-product'>";
  187. foreach ($data as $val) {
  188. $list .= "<li value=" . $val->product_id . ">" . $val->name . "</li>";
  189. }
  190. $list .= "</ul>";
  191. }
  192.  
  193. echo $list;
  194. //echo json_encode($data);
  195. //print_r($data);
  196. }
  197.  
  198. /*
  199. this fucntion is used to add sales record in database
  200. */
  201.  
  202. public function addSales() {
  203. date_default_timezone_set("Asia/Bangkok");
  204.  
  205. $submit = $this->input->post('submit');
  206. if ($submit == "Publish") {
  207. $status = 0;
  208. } else {
  209. $status = 4;
  210. }
  211.  
  212. $this->form_validation->set_rules('customer', 'Customer', 'trim|required');
  213. if ($this->form_validation->run() == false) {
  214. $this->add();
  215. } else {
  216. $data = array(
  217. "po" => $this->input->post('so'),
  218. "po_cust" => $this->input->post('po_cust') ?: "",
  219. "cc_holder" => $this->input->post('taxx'),
  220. "currentdate" => $this->input->post('sales_date'),
  221. "date" => $this->input->post('cust_date'),
  222. "reference_no" => serialize($this->input->post('reference_no')),
  223. "ketnama" => $this->input->post('reference'),
  224. "warehouse_id" => "",
  225. "biller_id" => $this->input->post('biller_id'),
  226. "customer_id" => $this->input->post('customer'),
  227. "upcc" => $this->input->post('main'),
  228. "upcc2" => $this->input->post('cc'),
  229. "total" => $this->input->post('grand_total'),
  230. "discount_value" => $this->input->post('total_discount'),
  231. "tax_value" => $this->input->post('total_tax'),
  232. "note" => $this->input->post('note'),
  233. "shipping_city_id" => $this->input->post('city'),
  234. "shipping_state_id" => $this->input->post('state'),
  235. "shipping_country_id" => $this->input->post('country'),
  236. "shipping_address" => $this->input->post('address'),
  237. "shipping_charge" => $this->input->post('shipping_charge'),
  238. "internal_note" => $this->input->post('internal_note'),
  239. "mode_of_transport" => $this->input->post('mode_of_transport'),
  240. "transporter_name" => $this->input->post('transporter_name'),
  241. "transporter_code" => $this->input->post('transporter_code'),
  242. "vehicle_regn_no" => $this->input->post('vehicle_regn_no'),
  243. "l_r_no" => $this->input->post('l_r_no'),
  244. "chalan_no" => $this->input->post('chalan_no'),
  245. "indent_no" => $this->input->post('indent_no'),
  246. "credit_days" => $this->input->post('credit_days'),
  247. "date_of_supply" => $this->input->post('date_of_supply'),
  248. "electronic_ref_no" => $this->input->post('electronic_ref_no'),
  249. "gst_payable" => $this->input->post('gst_payable'),
  250. "broker" => $this->input->post('broker'),
  251. "user" => $this->session->userdata('user_id'),
  252. "term_and_condition" => $this->input->post('term_and_condition'),
  253. "user_name" => $this->session->userdata('identity'),
  254. "kurs_id" => $this->input->post('kursdata'),
  255. "nominalcurrency" => $this->input->post('grand_currency'),
  256. "ATTN" => $this->input->post('tax'),
  257. "shipto" => $this->input->post('shipto'),
  258. "billto" => $this->input->post('billto'),
  259. "dateestimate" => $this->input->post('dateestimate'),
  260. "salesdesc" => $this->input->post('others'),
  261. "paid_by" => $this->input->post('paymenterms') ?: 'cash',
  262. "diskonpersen" => $this->input->post('DiscountPersen'),
  263. "service_description" => $this->input->post('service_description'),
  264. "additional_description" => $this->input->post('additional_description'),
  265. "status_rev" => $status,
  266. "diskon" => $this->input->post('diskonfix')
  267. );
  268.  
  269. $invoice = array(
  270. "invoice_no" => $this->sales_model->generateInvoiceNo(),
  271. "sales_amount" => $this->input->post('grand_total'),
  272. "invoice_date" => date('Y-m-d')
  273. );
  274.  
  275. if ($sales_id = $this->sales_model->addModel($data, $invoice)) {
  276. $log_data = array(
  277. 'user_id' => $this->session->userdata('user_id'),
  278. 'table_id' => $sales_id,
  279. 'message' => 'Sales Inserted'
  280. );
  281. $this->log_model->insert_log($log_data);
  282. $jumlahtermin = $this->input->post('terminnumber');
  283.  
  284.  
  285. if (!empty($jumlahtermin)) {
  286. $termins = $this->input->post('terminvalue');
  287. $ket = $this->input->post('terminket');
  288. $tanggaltermins = $this->input->post('tanggalterminvalue');
  289. $i = 1;
  290. if (!empty($termins)) {
  291. $im = 0;
  292. $termino = 0;
  293. foreach ($termins as $quan) {
  294. $value = $this->input->post('grand_total');
  295. $nominal = ($value * $quan) / 100;
  296. $nominal = number_format((float) $nominal, 2, '.', '');
  297. if (!empty($tanggaltermins[$im])) {
  298. $datetermin = $tanggaltermins[$im];
  299. } else {
  300. $datetermin = date('Y-m-d');
  301. }
  302. $termino++;
  303. $datatermin = array(
  304. "sales_id" => $sales_id,
  305. "persen" => $quan,
  306. "nominal" => $nominal,
  307. "tanggalbayar" => $datetermin,
  308. "keterangan" => $ket[$i]
  309. );
  310.  
  311. $this->sales_model->addTerminModel($datatermin);
  312. $i++;
  313. $im++;
  314. }
  315. } else {
  316.  
  317. }
  318. }
  319.  
  320.  
  321. $js_data = json_decode($this->input->post('table_data'));
  322. $js_data1 = json_decode($this->input->post('tableadditional_data'));
  323. $js_data2 = json_decode($this->input->post('tableservice_data'));
  324. $js_data3 = json_decode($this->input->post('tableitemrequest'));
  325.  
  326. // if ($js_data) {
  327. // foreach ($js_data as $key => $value) {
  328. // if ($value != null || !empty($value->product_id)) {
  329.  
  330. // $data = array(
  331. // "product_id" => $value->product_id,
  332. // "quantity" => $value->quantity,
  333. // "price" => $value->price,
  334. // "gross_total" => $value->total,
  335. // "discount_id" => @$value->discount_id,
  336. // "discount_value" => @$value->discount_value,
  337. // "discount" => $value->discount,
  338. // "serial_no" => $value->desc,
  339. // "sales_id" => $sales_id
  340. // );
  341.  
  342. // $this->db->insert('sales_items' ,$data);
  343. // }
  344. // }
  345. // }
  346.  
  347. $product_id = $this->input->post('product_id');
  348.  
  349. foreach ($product_id as $e=>$pd) {
  350. $quantity = $this->input->post('qty'.$e);
  351. $price = $this->input->post('price'.$e);
  352. $gross_total = $this->input->post('subtotal'.$e);
  353. $discount_id = $this->input->post('discount_id'.$e);
  354. $discount_value = $this->input->post('discount_value'.$e);
  355. $discount = $this->input->post('discount'.$e);
  356. $serial_no = $this->input->post('serial_no'.$e);
  357. $sales_id = $sales_id;
  358.  
  359. $lines['quantity'] = $quantity;
  360. $lines['price'] = $price;
  361. $lines['gross_total'] = $gross_total;
  362. $lines['discount_id'] = $discount_id;
  363. $lines['discount_value'] = $discount_value;
  364. $lines['discount'] = $discount;
  365. $lines['serial_no'] = $serial_no;
  366. $lines['sales_id'] = $sales_id;
  367.  
  368. $this->db->insert('sales_items' ,$lines);
  369. }
  370.  
  371. if ($js_data1) {
  372.  
  373. foreach ($js_data1 as $key => $value) {
  374. if ($value != null || !empty($value->additional_id)) {
  375.  
  376. $data = array(
  377. "product_id" => $value->additional_id,
  378. "quantity" => $value->quantity,
  379. "price" => $value->price,
  380. "gross_total" => $value->total,
  381. "discount_id" => @$value->discount_id,
  382. "discount_value" => @$value->discount_value,
  383. "discount" => $value->discount,
  384. "desc" => $value->desc,
  385. "sales_id" => $sales_id
  386. );
  387.  
  388. $this->db->insert('sales_additional_items' ,$data);
  389. }
  390. }
  391. }
  392.  
  393. if ($js_data2) {
  394.  
  395. foreach ($js_data2 as $key => $value) {
  396. if ($value != null || !empty($value->service_id)) {
  397.  
  398. $data = array(
  399. "service_id" => $value->service_id,
  400. "quantity" => @$value->quantity?:0,
  401. "price" => @$value->price?:0,
  402. "gross_total" => $value->total,
  403. "discount_id" => @$value->discount_id,
  404. "discount_value" => @$value->discount_value,
  405. "discount" => $value->discount,
  406. "sales_id" => $sales_id
  407. );
  408.  
  409. $this->sales_model->addSalesServiceItem($data);
  410.  
  411. }
  412. }
  413. }
  414.  
  415. if ($js_data3) {
  416. var_dump($js_data3);
  417. foreach ($js_data3 as $value) {
  418. var_dump($value->request_date);
  419.  
  420. //random name item req
  421. $digits = 4;
  422. $rand = str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);
  423. $number = date("dm").$rand;
  424. $cek = $this->db->select('*')->from('item_request')->where('sales_id', $sales_id)->get()->result();
  425. var_dump($cek);
  426. if (empty($cek)) {
  427. $deadline = date('Y-m-d', strtotime('14 day', strtotime(date($value->request_date))));
  428.  
  429. $params = array(
  430. 'request_number' => $number,
  431. 'request_date' => $value->request_date,
  432. 'request_deadline' => $deadline,
  433. 'reference' => $value->reference,
  434. 'sales_id' => $sales_id,
  435. 'request_for' => 'Sales'
  436. );
  437.  
  438. $this->db->insert('item_request', $params);
  439. $id = $this->db->insert_id();
  440. } else {
  441. $id = $cek[0]->id_item_request;
  442. }
  443.  
  444. $params = array(
  445. 'item_request_id' => $id,
  446. 'product_id' => $value->id_product,
  447. 'price' => $value->price,
  448. 'qty' => $value->qty
  449. );
  450.  
  451.  
  452.  
  453. $this->db->insert('item_request_items', $params);
  454.  
  455. }
  456.  
  457. }
  458.  
  459.  
  460. redirect('sales/view/' . $sales_id);
  461. } else {
  462. redirect('sales', 'refresh');
  463. }
  464. }
  465. }
  466.  
  467. public function revisiSales() {
  468. $id = $this->input->post('sales_id');
  469. $status_old = $this->input->post('status_old');
  470. date_default_timezone_set("Asia/Bangkok");
  471.  
  472. $this->form_validation->set_rules('customer', 'Customer', 'trim|required');
  473.  
  474. $submit = $this->input->post('submit');
  475. if ($submit == "Publish") {
  476. $status = 1;
  477. } else {
  478. $status = 4;
  479. }
  480. if ($this->form_validation->run() == false) {
  481. $this->revisi($id);
  482. } else {
  483. $data = array(
  484. "po" => $this->input->post('po'),
  485. "cc_holder" => $this->input->post('taxx'),
  486. "po_cust" => $this->input->post('po_cust'),
  487. "po_new" => $this->input->post('po_new'),
  488. "currentdate" => $this->input->post('salesdate'),
  489. "date" => $this->input->post('cust_date'),
  490. "ketnama" => $this->input->post('reference'),
  491. "reference_no" => serialize($this->input->post('reference_no')),
  492. "warehouse_id" => "",
  493. "customer_id" => $this->input->post('customer'),
  494. "upcc" => $this->input->post('main'),
  495. "upcc2" => $this->input->post('cc'),
  496. "total" => $this->input->post('grand_total'),
  497. "discount_value" => $this->input->post('total_discount'),
  498. "tax_value" => $this->input->post('total_tax'),
  499. "note" => $this->input->post('note'),
  500. "shipping_city_id" => $this->input->post('city'),
  501. "shipping_state_id" => $this->input->post('state'),
  502. "shipping_country_id" => $this->input->post('country'),
  503. "shipping_address" => $this->input->post('address'),
  504. "shipping_charge" => $this->input->post('shipping_charge'),
  505. "internal_note" => $this->input->post('internal_note'),
  506. "mode_of_transport" => $this->input->post('mode_of_transport'),
  507. "transporter_name" => $this->input->post('transporter_name'),
  508. "transporter_code" => $this->input->post('transporter_code'),
  509. "vehicle_regn_no" => $this->input->post('vehicle_regn_no'),
  510. "l_r_no" => $this->input->post('l_r_no'),
  511. "chalan_no" => $this->input->post('chalan_no'),
  512. "indent_no" => $this->input->post('indent_no'),
  513. "credit_days" => $this->input->post('credit_days'),
  514. "date_of_supply" => $this->input->post('date_of_supply'),
  515. "electronic_ref_no" => $this->input->post('electronic_ref_no'),
  516. "gst_payable" => $this->input->post('gst_payable'),
  517. "broker" => $this->input->post('broker'),
  518. "user" => $this->session->userdata('user_id'),
  519. "term_and_condition" => $this->input->post('term_and_condition'),
  520. "user_name" => $this->session->userdata('identity'),
  521. "kurs_id" => $this->input->post('kursdata'),
  522. "nominalcurrency" => $this->input->post('grand_currency'),
  523. "ATTN" => $this->input->post('tax'),
  524. "shipto" => $this->input->post('shipto'),
  525. "billto" => $this->input->post('billto'),
  526. "dateestimate" => $this->input->post('dateestimate'),
  527. "salesdesc" => $this->input->post('others'),
  528. "paid_by" => $this->input->post('paymenterms') ?: 'advance',
  529. "additional_description" => $this->input->post('additional_description'),
  530. "service_description" => $this->input->post('service_description'),
  531. "diskonpersen" => $this->input->post('DiscountPersen'),
  532. "status_rev" => $status,
  533. "diskon" => $this->input->post('diskonfix')
  534. );
  535.  
  536. $invoice = array(
  537. "invoice_no" => $this->sales_model->generateInvoiceNo(),
  538. "sales_amount" => $this->input->post('grand_total'),
  539. "invoice_date" => date('Y-m-d')
  540. );
  541.  
  542. if ($sales_id = $this->sales_model->addModel($data, $invoice)) {
  543.  
  544. if ($status_old == 2) {
  545.  
  546. $product_items = $this->db->select('*')
  547. ->from('sales_items')
  548. ->where('sales_id', $id)
  549. ->get()
  550. ->result();
  551.  
  552. foreach ($product_items as $row) {
  553. $idp = $this->db->select('*')
  554. ->from('product_grouping')
  555. ->where('product_id', $row->product_id)
  556. ->get()
  557. ->result();
  558.  
  559. if ($idp) {
  560. $arlist = json_encode(unserialize($idp[0]->list));
  561. $arlist = json_decode($arlist);
  562.  
  563. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  564.  
  565. $quantity = $row->quantity * $arlist[$ilist]->ql;
  566.  
  567. $this->db->set('quantity', 'quantity'-$quantity)
  568. ->where('product_id', $arlist[$ilist]->pl)
  569. ->update('products');
  570. }
  571. } else {
  572. $this->db->query("UPDATE products SET quantity=quantity + $row->quantity WHERE product_id=$row->product_id");
  573. }
  574. }
  575.  
  576. $additional_items = $this->db->select('*')
  577. ->from('sales_additional_items')
  578. ->where('sales_id', $id)
  579. ->get()
  580. ->result();
  581.  
  582. foreach ($additional_items as $value) {
  583. $idp = $this->db->select('*')
  584. ->from('product_grouping')
  585. ->where('product_id', $value->product_id)
  586. ->get()
  587. ->result();
  588.  
  589. if ($idp) {
  590. $arlist = json_encode(unserialize($idp[0]->list));
  591. $arlist = json_decode($arlist);
  592.  
  593. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  594.  
  595. $quantity = $value->quantity * $arlist[$ilist]->ql;
  596.  
  597. $this->db->set('quantity', 'quantity'-$quantity)
  598. ->where('product_id', $arlist[$ilist]->pl)
  599. ->update('products');
  600. }
  601. } else {
  602. $this->db->query("UPDATE products SET quantity=quantity + $row->quantity WHERE product_id=$value->product_id");
  603. }
  604. }
  605. }
  606. $log_data = array(
  607. 'user_id' => $this->session->userdata('user_id'),
  608. 'table_id' => $sales_id,
  609. 'message' => 'Sales Inserted'
  610. );
  611. $this->log_model->insert_log($log_data);
  612. $jumlahtermin = $this->input->post('terminnumber');
  613.  
  614.  
  615. if (!empty($jumlahtermin)) {
  616. $termins = $this->input->post('terminvalue');
  617. $ket = $this->input->post('terminket');
  618. $tanggaltermins = $this->input->post('tanggalterminvalue');
  619. $i = 1;
  620. if (!empty($termins)) {
  621. $im = 0;
  622. $termino = 0;
  623. foreach ($termins as $quan) {
  624. $value = $this->input->post('grand_total');
  625. $nominal = ($value * $quan) / 100;
  626. $nominal = number_format((float) $nominal, 2, '.', '');
  627. if (!empty($tanggaltermins[$im])) {
  628. $datetermin = $tanggaltermins[$im];
  629. } else {
  630. $datetermin = date('Y-m-d');
  631. }
  632. $termino++;
  633. $datatermin = array(
  634. "sales_id" => $sales_id,
  635. "persen" => $quan,
  636. "nominal" => $nominal,
  637. "tanggalbayar" => $datetermin,
  638. "keterangan" => $ket[$i]
  639. );
  640.  
  641. $this->sales_model->addTerminModel($datatermin);
  642. $i++;
  643. $im++;
  644. }
  645. }
  646. }
  647.  
  648.  
  649. $js_data = json_decode($this->input->post('table_data'));
  650. $js_data1 = json_decode($this->input->post('tableadditional_data'));
  651. $js_data2 = json_decode($this->input->post('tableservice_data'));
  652.  
  653. foreach ($js_data as $key => $value) {
  654. if ($value != null || !empty($value->product_id)) {
  655.  
  656. $data = array(
  657. "product_id" => $value->product_id,
  658. "quantity" => $value->quantity,
  659. "price" => $value->price,
  660. "gross_total" => $value->total,
  661. "discount_id" => @$value->discount_id,
  662. "discount_value" => @$value->discount_value,
  663. "discount" => $value->discount,
  664. "serial_no" => $value->desc,
  665. "sales_id" => $sales_id
  666. );
  667.  
  668. $this->db->insert('sales_items', $data);
  669. }
  670. }
  671.  
  672. foreach ($js_data1 as $key => $value) {
  673. if ($value != null || !empty($value->additional_id)) {
  674.  
  675. $data = array(
  676. "product_id" => $value->additional_id,
  677. "quantity" => $value->quantity,
  678. "price" => $value->price,
  679. "gross_total" => $value->total,
  680. "discount_id" => @$value->discount_id,
  681. "discount_value" => @$value->discount_value,
  682. "discount" => $value->discount,
  683. "desc" => $value->desc,
  684. "sales_id" => $sales_id
  685. );
  686.  
  687. $this->db->insert('sales_additional_items' ,$data);
  688. }
  689. }
  690.  
  691. if (!empty($js_data2)) {
  692. foreach ($js_data2 as $key => $value) {
  693. if ($value != null || !empty($value->service_id)) {
  694.  
  695. $data = array(
  696. "service_id" => $value->service_id,
  697. "quantity" => $value->quantity,
  698. "price" => $value->price,
  699. "gross_total" => $value->total,
  700. "discount_id" => @$value->discount_id,
  701. "discount_value" => @$value->discount_value,
  702. "discount" => $value->discount,
  703. "sales_id" => $sales_id
  704. );
  705. $this->sales_model->addSalesServiceItem($data);
  706. }
  707. }
  708. }
  709.  
  710. $dataman = array(
  711. "status_rev" => 2
  712. );
  713.  
  714. if ($this->sales_model->editModel($id, $dataman)) {
  715.  
  716. $log_data = array(
  717. 'user_id' => $this->session->userdata('user_id'),
  718. 'table_id' => $id,
  719. 'message' => 'Change Revisi Sales Updated'
  720. );
  721. $this->log_model->insert_log($log_data);
  722. }
  723.  
  724.  
  725. redirect('sales/view/' . $sales_id);
  726. } else {
  727. redirect('sales', 'refresh');
  728. }
  729. }
  730. }
  731.  
  732. /*
  733. call edit view to edit sales record
  734. */
  735.  
  736. public function getCurrencyAjax($id) {
  737. $data = $this->sales_model->getCurrencyId($id);
  738. echo json_encode($data);
  739. //print_r($data);
  740. }
  741.  
  742. public function disetujui($id) {
  743. $data['data'] = $this->sales_model->getDetails($id);
  744. $data['quotation'] = $this->quotation_model->getQuotation();
  745. $data['check_invoice'] = $this->sales_model->checkInvoice($id);
  746. $data['items'] = $this->sales_model->getItems($id);
  747. $data['serviceitems'] = $this->sales_model->getServiceItems($id);
  748. $data['company'] = $this->purchase_model->getCompany();
  749. $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  750. $data['quotationadditional'] = $this->quotation_model->getAdditional($id);
  751. $this->load->view('sales/disetujui', $data);
  752. }
  753.  
  754. public function changeSetujui() {
  755. $id = $this->input->post('sales_id');
  756.  
  757. $this->form_validation->set_rules('status', 'Status', 'required');
  758. if ($this->form_validation->run() == false) {
  759. $this->disetujui($id);
  760. } else {
  761.  
  762. $data = array(
  763. 'status_rev' => 2
  764. );
  765.  
  766. $array['id_medrep'] = $this->input->post("id_medrep");
  767. $array['id_sales'] = $this->input->post("id_sales");
  768. $array['total_sales'] = $this->input->post("total_sales");
  769. $this->sales_model->insert('medrep_transection',$array);
  770.  
  771. $product_items = $this->db->select('*')
  772. ->from('sales_items')
  773. ->where('sales_id', $id)
  774. ->get()
  775. ->result();
  776.  
  777. foreach ($product_items as $row) {
  778. $idp = $this->db->select('*')
  779. ->from('product_grouping')
  780. ->where('product_id', $row->product_id)
  781. ->get()
  782. ->result();
  783.  
  784. if ($idp) {
  785. $arlist = json_encode(unserialize($idp[0]->list));
  786. $arlist = json_decode($arlist);
  787.  
  788. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  789.  
  790. $quantity = $row->quantity * $arlist[$ilist]->ql;
  791.  
  792. $this->db->set('quantity', 'quantity'-$quantity)
  793. ->where('product_id', $arlist[$ilist]->pl)
  794. ->update('products');
  795. }
  796. } else {
  797. $this->db->query("UPDATE products SET quantity=quantity - $row->quantity WHERE product_id=$row->product_id");
  798. }
  799. }
  800.  
  801. $additional_items = $this->db->select('*')
  802. ->from('sales_additional_items')
  803. ->where('sales_id', $id)
  804. ->get()
  805. ->result();
  806.  
  807. foreach ($additional_items as $value) {
  808. $idp = $this->db->select('*')
  809. ->from('product_grouping')
  810. ->where('product_id', $value->product_id)
  811. ->get()
  812. ->result();
  813.  
  814. if ($idp) {
  815. $arlist = json_encode(unserialize($idp[0]->list));
  816. $arlist = json_decode($arlist);
  817.  
  818. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  819.  
  820. $quantity = $value->quantity * $arlist[$ilist]->ql;
  821.  
  822. $this->db->set('quantity', 'quantity'-$quantity)
  823. ->where('product_id', $arlist[$ilist]->pl)
  824. ->update('products');
  825. }
  826. } else {
  827. $this->db->query("UPDATE products SET quantity=quantity - $row->quantity WHERE product_id=$value->product_id");
  828. }
  829. }
  830.  
  831. $update = $this->db->where('sales_id', $id)->update('sales', $data);
  832.  
  833. if ($update) {
  834.  
  835. $log_data = array(
  836. 'user_id' => $this->session->userdata('user_id'),
  837. 'table_id' => $id,
  838. 'message' => 'Change Setujui Sales Updated'
  839. );
  840. $this->log_model->insert_log($log_data);
  841.  
  842. redirect('sales', 'refresh');
  843. } else {
  844.  
  845. redirect('sales', 'refresh');
  846. }
  847.  
  848.  
  849.  
  850. }
  851. }
  852.  
  853.  
  854. public function ditolak($id) {
  855. $data['data'] = $this->sales_model->getDetails($id);
  856. $data['quotation'] = $this->quotation_model->getQuotation();
  857. $data['check_invoice'] = $this->sales_model->checkInvoice($id);
  858. $data['items'] = $this->sales_model->getItems($id);
  859. $data['serviceitems'] = $this->sales_model->getServiceItems($id);
  860. $data['company'] = $this->purchase_model->getCompany();
  861. $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  862. $data['quotationadditional'] = $this->quotation_model->getAdditional($id);
  863. $this->load->view('sales/ditolak', $data);
  864. }
  865.  
  866. public function changeTolak() {
  867. $id = $this->input->post('sales_id');
  868. $this->form_validation->set_rules('status', 'Status', 'required');
  869. $this->form_validation->set_rules('ditolak', 'Keterangan Ditolak', 'required');
  870. if ($this->form_validation->run() == false) {
  871. $this->ditolak($id);
  872. } else {
  873.  
  874. $data = array(
  875. 'status_rev' => 3
  876. );
  877.  
  878. $update = $this->db->where('sales_id', $id)->update('sales', $data);
  879.  
  880. if ($update) {
  881.  
  882. $log_data = array(
  883. 'user_id' => $this->session->userdata('user_id'),
  884. 'table_id' => $id,
  885. 'message' => 'Change Ditolak Sales Updated'
  886. );
  887.  
  888. $this->log_model->insert_log($log_data);
  889.  
  890. redirect('sales', 'refresh');
  891. } else {
  892.  
  893. redirect('sales', 'refresh');
  894. }
  895. }
  896. }
  897.  
  898. public function edit($id) {
  899.  
  900. $kursdata = $this->company_setting_model->getCurrency();
  901.  
  902. $data['brands'] = $this->brand_model->getBrand();
  903. $data['customer'] = $this->sales_model->getCustomer();
  904. $data['biller'] = $this->sales_model->getBiller();
  905. $data['product2'] = $this->sales_model->getProducts2();
  906. $data['service'] = $this->sales_model->getService();
  907. $data['additional'] = $this->sales_model->getAdditional($id);
  908. $data['discount'] = $this->sales_model->getDiscount();
  909. $data['bonus'] = $this->sales_model->getBonus($id);
  910. $data['tax'] = $this->sales_model->getTax();
  911. $data['data'] = $this->sales_model->getRecord($id);
  912. $data['country'] = $this->customer_model->getCountry();
  913. $data['kursdata'] = $kursdata;
  914. $data['state'] = $this->customer_model->getState($data['data'][0]->shipping_country_id);
  915. $data['city'] = $this->customer_model->getCity($data['data'][0]->shipping_state_id);
  916. $data['product'] = $this->sales_model->getProducts($data['data'][0]->warehouse_id);
  917. $data['items'] = $this->sales_model->getProducts99($id);
  918.  
  919. $data['items2_old'] = $this->sales_model->getProducts99($id);
  920. $data['total_items'] = count($data['items2_old']);
  921. // echo '<pre>' . var_export($data, true) . '</pre>';
  922. // die;
  923. $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  924. $data['termin'] = $this->sales_model->getTerminSalesId($data['data'][0]->sales_id);
  925.  
  926. $this->load->view('sales/edit', $data);
  927. }
  928. public function revisi($id) {
  929.  
  930. $kursdata = $this->company_setting_model->getCurrency();
  931.  
  932. $data['brands'] = $this->brand_model->getBrand();
  933. $data['biller'] = $this->sales_model->getBiller();
  934. $data['customer'] = $this->sales_model->getCustomer();
  935. $data['product2'] = $this->sales_model->getProducts2();
  936. $data['service'] = $this->sales_model->getService();
  937. $data['additional'] = $this->sales_model->getAdditional($id);
  938. $data['discount'] = $this->sales_model->getDiscount();
  939. $data['tax'] = $this->sales_model->getTax();
  940. $data['data'] = $this->sales_model->getRecord($id);
  941. $data['country'] = $this->customer_model->getCountry();
  942. $data['kursdata'] = $kursdata;
  943. $data['state'] = $this->customer_model->getState($data['data'][0]->shipping_country_id);
  944. $data['city'] = $this->customer_model->getCity($data['data'][0]->shipping_state_id);
  945. $data['product'] = $this->sales_model->getProducts($data['data'][0]->warehouse_id);
  946. $data['items'] = $this->sales_model->getSalesItems($data['data'][0]->sales_id);
  947. $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  948. $data['termin'] = $this->sales_model->getTerminSalesId($data['data'][0]->sales_id);
  949.  
  950. $this->load->view('sales/revisi', $data);
  951. }
  952.  
  953. /*
  954. this fucntion is to edit sales record and save in database
  955. */
  956.  
  957. public function editSales() {
  958. $id = $this->input->post('sales_id');
  959. $submit = $this->input->post('submit');
  960. if ($submit == "Update") {
  961. $status = 0;
  962. } else {
  963. $status = 4;
  964. }
  965.  
  966. $this->form_validation->set_rules('customer', 'Customer', 'trim|required');
  967. if ($this->form_validation->run() == false) {
  968. $this->edit($id);
  969. } else {
  970. $warehouse_id = $this->input->post('warehouse');
  971. $old_warehouse_id = $this->input->post('old_warehouse_id');
  972. $warehouse_change = $this->input->post('warehouse_change');
  973. $data = array(
  974. "currentdate" => $this->input->post('sales_date'),
  975. "date" => $this->input->post('cust_date'),
  976. "cc_holder" => $this->input->post('taxx'),
  977. "po_cust" => $this->input->post('po_cust'),
  978. "warehouse_id" => $this->input->post('warehouse'),
  979. "ketnama" => $this->input->post('reference'),
  980. "biller_id" => $this->input->post('biller_id'),
  981. "customer_id" => $this->input->post('customer'),
  982. "upcc" => $this->input->post('main'),
  983. "upcc2" => $this->input->post('cc'),
  984. "total" => $this->input->post('grand_total'),
  985. "discount_value" => $this->input->post('total_discount'),
  986. "tax_value" => $this->input->post('total_tax'),
  987. "note" => $this->input->post('note'),
  988. "shipping_city_id" => $this->input->post('city'),
  989. "shipping_state_id" => $this->input->post('state'),
  990. "shipping_country_id" => $this->input->post('country'),
  991. "shipping_address" => $this->input->post('address'),
  992. "shipping_charge" => $this->input->post('shipping_charge'),
  993. "internal_note" => $this->input->post('internal_note'),
  994. "mode_of_transport" => $this->input->post('mode_of_transport'),
  995. "transporter_name" => $this->input->post('transporter_name'),
  996. "transporter_code" => $this->input->post('transporter_code'),
  997. "vehicle_regn_no" => $this->input->post('vehicle_regn_no'),
  998. "l_r_no" => $this->input->post('l_r_no'),
  999. "chalan_no" => $this->input->post('chalan_no'),
  1000. "indent_no" => $this->input->post('indent_no'),
  1001. "credit_days" => $this->input->post('credit_days'),
  1002. "date_of_supply" => $this->input->post('date_of_supply'),
  1003. "electronic_ref_no" => $this->input->post('electronic_ref_no'),
  1004. "gst_payable" => $this->input->post('gst_payable'),
  1005. "broker" => $this->input->post('broker'),
  1006. "user" => $this->session->userdata('user_id'),
  1007. "term_and_condition" => $this->input->post('term_and_condition'),
  1008. "sales_id" => $this->input->post('sales_id'),
  1009. "user_name" => $this->session->userdata('identity'),
  1010. "kurs_id" => $this->input->post('kursdata'),
  1011. "nominalcurrency" => $this->input->post('grand_currency'),
  1012. "ATTN" => $this->input->post('tax'),
  1013. "shipto" => $this->input->post('shipto'),
  1014. "billto" => $this->input->post('billto'),
  1015. "dateestimate" => $this->input->post('dateestimate'),
  1016. "salesdesc" => $this->input->post('others'),
  1017. "paid_by" => $this->input->post('paymenterms') ?: 'advance',
  1018. "additional_description" => $this->input->post('additional_description'),
  1019. "service_description" => $this->input->post('service_description'),
  1020. "diskonpersen" => $this->input->post('DiscountPersen'),
  1021. "diskon" => $this->input->post('diskonfix'),
  1022. "status_rev" => $status
  1023. );
  1024.  
  1025. if ($this->sales_model->editModel($id, $data)) {
  1026. $log_data = array(
  1027. 'user_id' => $this->session->userdata('user_id'),
  1028. 'table_id' => $id,
  1029. 'message' => 'Sales Updated'
  1030. );
  1031. $this->log_model->insert_log($log_data);
  1032.  
  1033.  
  1034. $this->sales_model->deleteTerminModel($id);
  1035.  
  1036. $jumlahtermin = $this->input->post('terminnumber');
  1037.  
  1038. if (!empty($jumlahtermin)) {
  1039. $termins = $this->input->post('terminvalue');
  1040. $tanggaltermins = $this->input->post('tanggalterminvalue');
  1041. $ket = $this->input->post('terminket');
  1042. if (!empty($termins)) {
  1043. $im = 0;
  1044. $i = 1;
  1045. foreach ($termins as $quan) {
  1046. $value = $this->input->post('grand_total');
  1047. $nominal = ($value * $quan) / 100;
  1048. $nominal = number_format((float) $nominal, 2, '.', '');
  1049. if (!empty($tanggaltermins[$im])) {
  1050. $datetermin = $tanggaltermins[$im];
  1051. } else {
  1052. $datetermin = date('Y-m-d');
  1053. }
  1054.  
  1055. $datatermin = array(
  1056. "sales_id" => $id,
  1057. "persen" => $quan,
  1058. "nominal" => $nominal,
  1059. "tanggalbayar" => $datetermin,
  1060. "keterangan" => $ket[$i]
  1061. );
  1062.  
  1063. $this->sales_model->addTerminModel($datatermin);
  1064. $i++;
  1065. $im++;
  1066. }
  1067. } else {
  1068.  
  1069. }
  1070. }
  1071.  
  1072.  
  1073. // $this->sales_model->deleteSalesItemsAll($id);
  1074.  
  1075. // $product_id = $this->input->post('product_id');
  1076. // $quantity = $this->input->post('qty');
  1077. // $price = $this->input->post('price');
  1078. // $gross_total = $this->input->post('linetotal');
  1079. // $discount_id = $this->input->post('discount_id');
  1080. // $discount_value = $this->input->post('discount_value');
  1081. // $discount = $this->input->post('discount');
  1082. // $serial_no = $this->input->post('serial_no');
  1083. // $sales_id = $id;
  1084.  
  1085. // foreach ($product_id as $e => $pd) {
  1086.  
  1087. // $a['product_id'] = $pd;
  1088. // $a['quantity'] = $quantity[$e];
  1089. // $a['price'] = $price[$e];
  1090. // $a['gross_total'] = $gross_total[$e];
  1091. // $a['discount_id'] = $discount_id[$e];
  1092. // $a['discount_value'] = $discount_value[$e];
  1093. // $a['discount'] = $discount[$e];
  1094. // $a['serial_no'] = $serial_no[$e];
  1095. // $a['sales_id'] = $sales_id;
  1096.  
  1097. // $this->db->insert('sales_items', $a);
  1098.  
  1099. // }
  1100.  
  1101. $js_data = json_decode($this->input->post('table_data'));
  1102. $js_data1 = json_decode($this->input->post('tableadditional_data'));
  1103. $js_data2 = json_decode($this->input->post('tableservice_data'));
  1104.  
  1105. $this->sales_model->deleteSalesItemsAll($id);
  1106.  
  1107. $id_product = $this->input->post('idProducts');
  1108. $quantity = $this->input->post('quantitys');
  1109. $price = $this->input->post('prices');
  1110. $total = $this->input->post('totals');
  1111. // $discount_id = $this->input->post('hidden_discount');
  1112. $discount_value = $this->input->post('discount_values');
  1113. // $serial_no =
  1114. $sales_id = $id;
  1115.  
  1116. $patokan = $this->input->post('patokan');
  1117. // var_dump($patokan);
  1118. // die;
  1119.  
  1120. foreach ($patokan as $e => $pd) {
  1121. // var_dump($pd);
  1122. // die;
  1123. $a['product_id'] = $this->input->post('idProducts'.$pd);
  1124. // $a['quantity'] = $quantity[$e];
  1125. // $a['price'] = $price[$e];
  1126. // $a['gross_total'] = $total[$e];
  1127. // // $a['discount_id'] = $discount_id[$e];
  1128. // $a['discount_value'] = $discount_value[$e];
  1129. // $a['sales_id'] = $id;
  1130.  
  1131. $a['quantity'] = $this->input->post('qty'.$pd);
  1132. $a['price'] = $this->input->post('price'.$pd);
  1133. $a['gross_total'] = $this->input->post('linetotal'.$pd);
  1134. $a['serial_no'] = $this->input->post('description'.$pd);
  1135. $a['discount_id'] = $this->input->post('item_discount'.$pd);
  1136.  
  1137. if($a['discount_id'] != ''){
  1138. $dk = $this->db->where('discount_id = ',$a['discount_id'])->get('discount')->row();
  1139. $diskon = $dk->discount_value / 100 * $a['price'];
  1140.  
  1141. $a['discount_value'] = $dk->discount_value;
  1142. $a['discount'] = $diskon;
  1143. }
  1144.  
  1145. $a['sales_id'] = $id;
  1146.  
  1147. $this->db->insert('sales_items', $a);
  1148. }
  1149.  
  1150. // foreach ($js_data as $key => $value) {
  1151. // if (!empty($value) and ! empty($value->product_id)) {
  1152.  
  1153. // $data = array(
  1154. // "product_id" => $value->product_id,
  1155. // "quantity" => (!empty($value->quantity)) ? $value->quantity : 0,
  1156. // "price" => (!empty($value->price)) ? $value->price : 0,
  1157. // "gross_total" => (!empty($value->total)) ? $value->total : 0,
  1158. // "discount_id" => (!empty($value->discount_id)) ? $value->discount_id : 0,
  1159. // "discount_value" => (!empty($value->discount_value)) ? $value->discount_value : 0,
  1160. // "discount" => (!empty($value->discount)) ? $value->discount : 0,
  1161.  
  1162. // "serial_no" => (!empty($value->desc)) ? $value->desc : 0,
  1163. // "sales_id" => $id
  1164. // );
  1165.  
  1166. // $this->db->insert('sales_items', $data);
  1167. // }
  1168. // }
  1169.  
  1170. $this->sales_model->deleteSalesAdditionalAll($id);
  1171. foreach ($js_data1 as $key => $value) {
  1172. if ($value != null || !empty($value->additional_id)) {
  1173.  
  1174. $data = array(
  1175. "product_id" => $value->additional_id,
  1176. "quantity" => $value->quantity,
  1177. "price" => $value->price,
  1178. "gross_total" => $value->total,
  1179. "discount_id" => @$value->discount_id,
  1180. "discount_value" => @$value->discount_value,
  1181. "discount" => $value->discount,
  1182. "desc" => $value->desc,
  1183. "sales_id" => $id
  1184. );
  1185.  
  1186. $this->db->insert('sales_additional_items' ,$data);
  1187. }
  1188. }
  1189.  
  1190. $this->sales_model->deleteSalesServiceItemsAll($id);
  1191. if (!empty($js_data2)) {
  1192. foreach ($js_data2 as $key => $value) {
  1193. if ($value != null || !empty($value->service_id)) {
  1194.  
  1195. $data = array(
  1196. "service_id" => $value->service_id,
  1197. "quantity" => $value->quantity,
  1198. "price" => $value->price,
  1199. "gross_total" => $value->total,
  1200. "discount_id" => @$value->discount_id,
  1201. "discount_value" => @$value->discount_value,
  1202. "discount" => $value->discount,
  1203. "sales_id" => $id
  1204. );
  1205.  
  1206. $this->sales_model->addSalesServiceItem($data);
  1207. }
  1208. }
  1209. }
  1210.  
  1211. redirect('sales/view/' . $id);
  1212. } else {
  1213. redirect('sales', 'refresh');
  1214. }
  1215. }
  1216. }
  1217.  
  1218. /*
  1219. this function is used to delete sales record from database
  1220. */
  1221.  
  1222. public function delete($id) {
  1223. if ($this->sales_model->deleteModel($id)) {
  1224. $log_data = array(
  1225. 'user_id' => $this->session->userdata('user_id'),
  1226. 'table_id' => $id,
  1227. 'message' => 'Sales Deleted'
  1228. );
  1229. $this->log_model->insert_log($log_data);
  1230. redirect('sales', 'refresh');
  1231. } else {
  1232. redirect('sales', 'refresh');
  1233. }
  1234. }
  1235.  
  1236. /*
  1237. display data in dashboard calendar
  1238. */
  1239.  
  1240. public function calendar() {
  1241. log_message('debug', print_r($this->db->get('category')->result(), true));
  1242. $data = $this->sales_model->getCalendarData();
  1243. $total = 0;
  1244. foreach ($data as $value) {
  1245. $date = Date('Y-m-d');
  1246. if ($date == $value->date) {
  1247. $total += $value->total;
  1248. }
  1249. $temp = array(
  1250. "title" => $total,
  1251. "start" => "2017-04-05T00:01:00+05:30"
  1252. );
  1253. }
  1254. echo json_encode($temp);
  1255. }
  1256.  
  1257. /*
  1258. view Sales details
  1259. */
  1260.  
  1261. public function view($id) {
  1262. $data['quotation'] = $this->quotation_model->getQuotation();
  1263. $data['data'] = $this->sales_model->getDetails($id);
  1264. $data['items'] = $this->sales_model->getItems($id);
  1265. $data['symbol'] = $this->db->select('*')->from("currency")->where("kurs_id", $data['data'][0]->kurs_id)->get()->result()[0]->symbol;
  1266.  
  1267. for ($i=0; $i < count($data['items']) ; $i++) {
  1268.  
  1269. if ($data['items'][$i]->product_grouping != NULL) {
  1270. $arlist = json_encode(unserialize($data['items'][$i]->list));
  1271. $arlist = json_decode($arlist);
  1272. $max = array();
  1273. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1274.  
  1275. $qtyp = $this->db->select('quantity')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->quantity;
  1276. $count = ($qtyp / intval($arlist[$ilist]->ql));
  1277. array_push($max, $count);
  1278. }
  1279. $min = min($max);
  1280. $data['items'][$i]->quantity_product = intval($min);
  1281. }
  1282. }
  1283. $data['additional'] = $this->sales_model->getAdditional($id);
  1284. for ($i=0; $i < count($data['additional']) ; $i++) {
  1285.  
  1286. if ($data['additional'][$i]->product_grouping != NULL) {
  1287. $arlist = json_encode(unserialize($data['additional'][$i]->list));
  1288. $arlist = json_decode($arlist);
  1289. $max = array();
  1290. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1291.  
  1292. $qtyp = $this->db->select('quantity')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->quantity;
  1293. $count = ($qtyp / intval($arlist[$ilist]->ql));
  1294. array_push($max, $count);
  1295. }
  1296. $min = min($max);
  1297. $data['additional'][$i]->quantity_product = intval($min);
  1298. }
  1299. }
  1300. $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  1301. $data['company'] = $this->purchase_model->getCompany();
  1302. $data['salestermins'] = $this->sales_model->getTerminSalesId($id);
  1303. $this->load->view('sales/view', $data);
  1304. }
  1305.  
  1306. /*
  1307. generate pdf
  1308. */
  1309.  
  1310. public function pdf($id) {
  1311. $log_data = array(
  1312. 'user_id' => $this->session->userdata('user_id'),
  1313. 'table_id' => $id,
  1314. 'message' => 'Invoice Generated'
  1315. );
  1316. $this->log_model->insert_log($log_data);
  1317. ob_start();
  1318. $html = ob_get_clean();
  1319. $html = utf8_encode($html);
  1320. $data['quotation'] = $this->quotation_model->getQuotation();
  1321. $data['data'] = $this->sales_model->getDetails($id);
  1322. $data['items'] = $this->sales_model->getItems($id);
  1323. for ($i=0; $i < count($data['items']) ; $i++) {
  1324.  
  1325. if ($data['items'][$i]->product_grouping != NULL) {
  1326. $arlist = json_encode(unserialize($data['items'][$i]->list));
  1327. $arlist = json_decode($arlist);
  1328. $array = array();
  1329. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1330.  
  1331. $name = $this->db->select('name')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->name;
  1332. array_push($array, $name);
  1333. }
  1334. $list = implode(', ', $array);
  1335.  
  1336. $data['items'][$i]->name = $data['items'][$i]->name. ' : ' .$list;
  1337. }
  1338. }
  1339. $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  1340. $data['company'] = $this->purchase_model->getCompany();
  1341. $data['company_setting'] = $this->company_setting_model->getData();
  1342. $data['signature'] = $this->signature_model->getActiveSignature();
  1343. $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  1344. $data['customer_shipto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->shipto);
  1345. $data['additional'] = $this->sales_model->getAdditional($id);
  1346. $data['salestermins'] = $this->sales_model->getTerminSalesId($id);
  1347. for ($i=0; $i < count($data['additional']) ; $i++) {
  1348.  
  1349. if ($data['additional'][$i]->product_grouping != NULL) {
  1350. $arlist = json_encode(unserialize($data['additional'][$i]->list));
  1351. $arlist = json_decode($arlist);
  1352. $array = array();
  1353. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1354.  
  1355. $name = $this->db->select('name')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->name;
  1356. array_push($array, $name);
  1357. }
  1358. $list = implode(', ', $array);
  1359.  
  1360. $data['additional'][$i]->name = $data['additional'][$i]->name. ' : ' .$list;
  1361. }
  1362. }
  1363. $data['bank'] = $this->bank_model->getRecordAr();
  1364. $data['customer_billto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->billto);
  1365. $data['symbol'] = $this->db->select('*')->from("currency")->where("kurs_id", $data['data'][0]->kurs_id)->get()->result()[0]->symbol;
  1366.  
  1367. // UNTUK FONT
  1368. $t = 0;
  1369. foreach ($data['items'] as $value) { $t += ($value->price * $value->quantity); }
  1370. foreach ($data['additional'] as $value) { $t += $value->gross_total; }
  1371. foreach ($data['serviceitems'] as $value) { $t += ($value->price * $value->quantity); }
  1372.  
  1373. $r = $t - $data['data'][0]->discount_value + $data['data'][0]->shipping_charge + $data['data'][0]->tax_value;
  1374. if ($r > $t) {
  1375. $font = $r;
  1376. } else {
  1377. $font = $t;
  1378. }
  1379.  
  1380. if(strlen($font) <= 9) {
  1381. $data['style'] = 'style="font-size: 9pt"';
  1382. $data['style2'] = 'style="font-size: 8pt; font-style: italic; font-weight: bold;"';
  1383. } elseif (strlen($font) == 10) {
  1384. $data['style'] = 'style="font-size: 8pt"';
  1385. $data['style2'] = 'style="font-size: 7pt; font-style: italic; font-weight: bold;"';
  1386. } elseif (strlen($font) >= 11) {
  1387. $data['style'] = 'style="font-size: 7pt"';
  1388. $data['style2'] = 'style="font-size: 6pt; font-style: italic; font-weight: bold;"';
  1389. }
  1390.  
  1391. $html = $this->load->view('sales/pdf', $data, true);
  1392.  
  1393. $logo = base_url(header_logo('logo'));
  1394. $width = header_logo('width');
  1395. $height = header_logo('height');
  1396.  
  1397. $mpdf = new \Mpdf\Mpdf(['setAutoTopMargin' => 'stretch', 'format' => [216, 356]]);
  1398. $mpdf->SetHTMLHeader('
  1399.  
  1400. <table style=" vertical-align: bottom; font-family: serif; font-size: 9pt;" width="100%">
  1401. <tr>
  1402. <td width="40%" valign="top"><img src="'. $logo .'" width="'. $width .'" height="'. $height .'"></td>
  1403. <td style="text-align: right;" width="60%">
  1404. '.header_doc() . '
  1405.  
  1406. </td>
  1407. </tr>
  1408. </table>');
  1409. if ($data['data'][0]->status_rev == 4) {
  1410. $status = 'Draft';
  1411. $mpdf->SetWatermarkText($status);
  1412. $mpdf->showWatermarkText = true;
  1413. } else {
  1414. $mpdf->SetWatermarkImage(base_url('/assets/images/logo-glodisa.jpg'));
  1415. $mpdf->showWatermarkImage = true;
  1416. }
  1417. $mpdf->allow_charset_conversion = true;
  1418. $mpdf->setFooter('|page {PAGENO} of {nbpg}|');
  1419. $mpdf->charset_in = 'UTF-8';
  1420. $mpdf->use_kwt = true;
  1421. $mpdf->WriteHTML($html);
  1422. $mpdf->Output(($data['data'][0]->po_new ?: $data['data'][0]->po) . '.pdf', 'I');
  1423. }
  1424.  
  1425. public function word($id, $ida='1') {
  1426. $idar = str_replace('_', ',', $ida);
  1427. $log_data = array(
  1428. 'user_id' => $this->session->userdata('user_id'),
  1429. 'table_id' => $id,
  1430. 'message' => 'Invoice Generated'
  1431. );
  1432. $this->log_model->insert_log($log_data);
  1433. ob_start();
  1434. $html = ob_get_clean();
  1435. $html = utf8_encode($html);
  1436. $data['quotation'] = $this->quotation_model->getQuotation();
  1437. $data['data'] = $this->sales_model->getDetails($id);
  1438. $data['items'] = $this->sales_model->getItems($id);
  1439. for ($i=0; $i < count($data['items']) ; $i++) {
  1440.  
  1441. if ($data['items'][$i]->product_grouping != NULL) {
  1442. $arlist = json_encode(unserialize($data['items'][$i]->list));
  1443. $arlist = json_decode($arlist);
  1444. $array = array();
  1445. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1446.  
  1447. $name = $this->db->select('name')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->name;
  1448. array_push($array, $name);
  1449. }
  1450. $list = implode(', ', $array);
  1451.  
  1452. $data['items'][$i]->name = $data['items'][$i]->name. ' : ' .$list;
  1453. }
  1454. }
  1455. $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  1456. $data['company'] = $this->purchase_model->getCompany();
  1457. $data['company_setting'] = $this->company_setting_model->getData();
  1458. $data['signature'] = $this->signature_model->getActiveSignature();
  1459. $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  1460. $data['customer_shipto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->shipto);
  1461. $data['additional'] = $this->sales_model->getAdditional($id);
  1462. $data['salestermins'] = $this->sales_model->getTerminSalesId($id);
  1463. for ($i=0; $i < count($data['additional']) ; $i++) {
  1464.  
  1465. if ($data['additional'][$i]->product_grouping != NULL) {
  1466. $arlist = json_encode(unserialize($data['additional'][$i]->list));
  1467. $arlist = json_decode($arlist);
  1468. $array = array();
  1469. for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1470.  
  1471. $name = $this->db->select('name')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->name;
  1472. array_push($array, $name);
  1473. }
  1474. $list = implode(', ', $array);
  1475.  
  1476. $data['additional'][$i]->name = $data['additional'][$i]->name. ' : ' .$list;
  1477. }
  1478. }
  1479. $data['bank'] = $this->bank_model->getRecordAr();
  1480. $data['customer_billto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->billto);
  1481. $data['symbol'] = $this->db->select('*')->from("currency")->where("kurs_id", $data['data'][0]->kurs_id)->get()->result()[0]->symbol;
  1482.  
  1483. // UNTUK FONT
  1484. $t = 0;
  1485. foreach ($data['items'] as $value) { $t += ($value->price * $value->quantity); }
  1486. foreach ($data['additional'] as $value) { $t += $value->gross_total; }
  1487. foreach ($data['serviceitems'] as $value) { $t += ($value->price * $value->quantity); }
  1488.  
  1489. $r = $t - $data['data'][0]->discount_value + $data['data'][0]->shipping_charge + $data['data'][0]->tax_value;
  1490. if ($r > $t) {
  1491. $font = $r;
  1492. } else {
  1493. $font = $t;
  1494. }
  1495.  
  1496. if(strlen($font) <= 9) {
  1497. $data['style'] = 'style="font-size: 9pt"';
  1498. $data['style2'] = 'style="font-size: 8pt; font-style: italic; font-weight: bold;"';
  1499. } elseif (strlen($font) == 10) {
  1500. $data['style'] = 'style="font-size: 8pt"';
  1501. $data['style2'] = 'style="font-size: 7pt; font-style: italic; font-weight: bold;"';
  1502. } elseif (strlen($font) >= 11) {
  1503. $data['style'] = 'style="font-size: 7pt"';
  1504. $data['style2'] = 'style="font-size: 6pt; font-style: italic; font-weight: bold;"';
  1505. }
  1506.  
  1507. $this->load->library('lmsword');
  1508. $this->lmsword->page_setup('A4');
  1509. $this->lmsword->html($this->load->view('sales/pdf', $data, true));
  1510. $this->lmsword->create($data['data'][0]->po_new ?: $data['data'][0]->po);
  1511. }
  1512.  
  1513. public function print1($id, $idb='1') {
  1514. $idar = str_replace('_', ',', $idb);
  1515. $log_data = array(
  1516. 'user_id' => $this->session->userdata('user_id'),
  1517. 'table_id' => $id,
  1518. 'message' => 'Invoice Printed'
  1519. );
  1520. $this->log_model->insert_log($log_data);
  1521. $data['data'] = $this->sales_model->getDetails($id);
  1522. $data['items'] = $this->sales_model->getItems($id);
  1523. $data['company'] = $this->purchase_model->getCompany();
  1524. $data['quotation'] = $this->quotation_model->getQuotation();
  1525. $data['data'] = $this->sales_model->getDetails($id);
  1526. $data['bank'] = $this->bank_model->getRecordAr($idar);
  1527. $data['additional'] = $this->sales_model->getAdditional($id);
  1528. $data['items'] = $this->sales_model->getItems($id);
  1529. $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  1530. $data['company'] = $this->purchase_model->getCompany();
  1531. $data['company_setting'] = $this->company_setting_model->getData();
  1532. $data['signature'] = $this->signature_model->getActiveSignature();
  1533. $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  1534. $data['customer_shipto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->shipto);
  1535. $data['customer_billto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->billto);
  1536. $this->load->view('sales/pdf', $data);
  1537. }
  1538.  
  1539. /*
  1540. send email
  1541. */
  1542.  
  1543. public function email($id) {
  1544. $log_data = array(
  1545. 'user_id' => $this->session->userdata('user_id'),
  1546. 'table_id' => 0,
  1547. 'message' => 'Invoice Email Send'
  1548. );
  1549. $this->log_model->insert_log($log_data);
  1550. $email = $this->sales_model->getSmtpSetup();
  1551.  
  1552. $data = $this->sales_model->getCustomerEmail($id);
  1553. $company = $this->purchase_model->getCompany();
  1554. $this->load->view('class.phpmailer.php');
  1555.  
  1556. $mail = new PHPMailer();
  1557.  
  1558. $mail->IsSMTP();
  1559. $mail->Host = $email->smtp_host;
  1560.  
  1561. $mail->SMTPAuth = true;
  1562. //$mail->SMTPSecure = "ssl";
  1563. $mail->Port = $email->smtp_port;
  1564. $mail->Username = $email->smtp_username;
  1565. $mail->Password = $email->smtp_password;
  1566.  
  1567. $mail->From = $email->from_address;
  1568. $mail->FromName = $email->from_name;
  1569. $mail->AddAddress($data[0]->email);
  1570. //$mail->AddReplyTo("mail@mail.com");
  1571.  
  1572. /* $mail->IsHTML(true);
  1573.  
  1574. $mail->Subject = "Purchase order No : ".$data[0]->reference_no." From ".$company[0]->name;
  1575. $mail->Body = "Date : ".$data[0]->date."<br>Total : ".$data[0]->total;
  1576. //$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
  1577.  
  1578. if(!$mail->Send())
  1579. {
  1580. $message = "Email could not be sent";
  1581. }
  1582. else{
  1583. $message = "Email has been sent";
  1584. } */
  1585. $total = $data[0]->total + $data[0]->shipping_charge;
  1586. $this->load->library('email');
  1587.  
  1588. $this->email->from($email->from_address, $email->from_name);
  1589. $this->email->to($data[0]->email);
  1590. $this->email->subject("Sales order No : " . $data[0]->reference_no . " From " . $company[0]->name);
  1591. $this->email->message("Date : " . $data[0]->date . " \nTotal : " . $total . " \n\n\nComapany Name : " . $company[0]->name . "\nAddress : " . $company[0]->street . " " . $company[0]->country_name . "\nMobile No :" . $company[0]->phone);
  1592. //Send mail
  1593. if ($this->email->send())
  1594. $message = "Email sent successfully.";
  1595. else
  1596. $message = "Error in sending Email.";
  1597.  
  1598. $this->session->set_flashdata('message', $message);
  1599. redirect('sales', 'refresh');
  1600. }
  1601.  
  1602. /*
  1603. payment view
  1604. */
  1605.  
  1606. public function payment($id) {
  1607. $data['data'] = $this->sales_model->getDetailsPayment($id);
  1608. $data['company'] = $this->purchase_model->getCompany();
  1609. $data['p_reference_no'] = $this->sales_model->generateReferenceNo();
  1610.  
  1611. $this->load->view('sales/payment', $data);
  1612. }
  1613.  
  1614. /*
  1615. get payment details to view and send to model
  1616. */
  1617.  
  1618. public function addPayment() {
  1619. $id = $this->input->post('id');
  1620. $paying_by = $this->input->post('paying_by');
  1621. $this->form_validation->set_rules('date', 'Date', 'trim|required');
  1622. $this->form_validation->set_rules('paying_by', 'Paying By', 'trim|required');
  1623. if ($paying_by == "Cheque") {
  1624. $this->form_validation->set_rules('bank_name', 'Bank Name', 'trim|required|callback_alpha_dash_space');
  1625. $this->form_validation->set_rules('cheque_no', 'Cheque No', 'trim|required|numeric');
  1626. }
  1627. if ($this->form_validation->run() == false) {
  1628. $this->payment($id);
  1629. } else {
  1630. if ($paying_by == "Cheque") {
  1631. $bank_name = $this->input->post('bank_name');
  1632. $cheque_no = $this->input->post('cheque_no');
  1633. } else {
  1634. $bank_name = "";
  1635. $cheque_no = "";
  1636. }
  1637. $data = array(
  1638. "sales_id" => $id,
  1639. "date" => $this->input->post('date'),
  1640. "reference_no" => $this->input->post('reference_no'),
  1641. "amount" => $this->input->post('amount'),
  1642. "paying_by" => $this->input->post('paying_by'),
  1643. "bank_name" => $bank_name,
  1644. "cheque_no" => $cheque_no,
  1645. "description" => $this->input->post('note')
  1646. );
  1647.  
  1648. if ($this->sales_model->addPayment($data)) {
  1649. $log_data = array(
  1650. 'user_id' => $this->session->userdata('user_id'),
  1651. 'table_id' => $id,
  1652. 'message' => 'Sales Payable'
  1653. );
  1654. $this->log_model->insert_log($log_data);
  1655. redirect('sales', 'refresh');
  1656. } else {
  1657. redirect("sales", 'refresh');
  1658. }
  1659. }
  1660. }
  1661.  
  1662. /*
  1663. generate invoice
  1664. */
  1665.  
  1666. public function invoice() {
  1667. $data['data'] = $this->sales_model->invoice();
  1668. $this->load->view('invoice/list', $data);
  1669. }
  1670.  
  1671. /*
  1672.  
  1673. */
  1674.  
  1675. public function getCustomerData($id) {
  1676. $data['data'] = $this->customer_model->getRecord($id);
  1677. $data['country'] = $this->customer_model->getCountry();
  1678. $data['state'] = $this->customer_model->getState($data['data'][0]->country_id);
  1679. $data['city'] = $this->customer_model->getCity($data['data'][0]->state_id);
  1680. $data['address'] = $this->customer_model->getListAddressRecord($id);
  1681. echo json_encode($data);
  1682. }
  1683.  
  1684. public function getRecordAjax($id) {
  1685. $data['data'] = $this->quotation_model->getRecordAjax($id);
  1686. echo json_encode($data);
  1687. }
  1688.  
  1689. public function getCustomerAddressData($id) {
  1690. $data['data'] = $this->customer_model->getRecord($id);
  1691. $data['address'] = $this->customer_model->getCustomerAddressbyID($id);
  1692. echo json_encode($data);
  1693. }
  1694.  
  1695. /*
  1696. check character and space validation
  1697. */
  1698.  
  1699. function alpha_dash_space($str) {
  1700. if (!preg_match("/^([a-zA-Z ])+$/i", $str)) {
  1701. $this->form_validation->set_message('alpha_dash_space', 'The %s field may only contain alpha and spaces');
  1702. return FALSE;
  1703. } else {
  1704. return TRUE;
  1705. }
  1706. }
  1707.  
  1708. function currencyConverter2($currency_from, $currency_to) {
  1709. if ($currency_from == $currency_to) {
  1710. return 1;
  1711. } else {
  1712. //$currency_output = round(currency($currency_input, $currency_from,$currency_to, false),2);
  1713. //$currency_input=substr($currency_input, 0, -3);
  1714. $currency_input = filter_var(1, FILTER_SANITIZE_NUMBER_INT);
  1715. $from_Currency = urlencode($currency_from);
  1716. $to_Currency = urlencode($currency_to);
  1717. $get = @file_get_contents("https://finance.google.com/finance/converter?a=1&from=$from_Currency&to=$to_Currency");
  1718. $get = @explode("<span class=bld>", $get);
  1719. $get = @explode("</span>", @$get[1]);
  1720. $converted_currency = preg_replace("/[^0-9\.]/", null, @$get[0]);
  1721. $converted_currency = $converted_currency;
  1722. return $converted_currency;
  1723.  
  1724. //return $currency_output;
  1725. }
  1726. }
  1727.  
  1728. }
  1729.  
  1730. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement