403Webshell
Server IP : 210.245.233.93  /  Your IP : 216.73.216.226
Web Server : Apache/2.2.15 (CentOS)
System : Linux webserver2.onesolution.com.hk 2.6.32-754.35.1.el6.x86_64 #1 SMP Sat Nov 7 12:42:14 UTC 2020 x86_64
User : apache ( 48)
PHP Version : 5.3.3
Disable Function : exec, shell_exec, system, passthru, popen, proc_open, pcntl_exec
MySQL : ON  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /var/www/hkosl.com/innoutstorage/webadmin/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/hkosl.com/innoutstorage/webadmin/warehousing_step3_post_20161202.php
<?php
	require_once('check_login.php');

	global $dbh;

	try {

		$dbh->beginTransaction();

		$from_order_id       = NULL;
		$total_return_amount = 0;
		$location_info       = get_location($_SESSION["warehousing"][2]["location_id"]);

		//if switching order, update old order
		if (!empty($_SESSION["switch"])) {
			$last_order_id = $_SESSION["switch"]["order_info"]["id"];
			//update deposit, do by staff
			/*$sql        = "update deposit set status = ? where order_id = ? ";
			$parameters = array("RETURNED", $last_order_id);
			bind_pdo($sql, $parameters);*/

			//update invoice
			/*$sql        = "update invoice set status = ?, type = ? where order_id = ? and status != ?";
			$parameters = array("VOID", "RENT", $last_order_id, "SETTLED");
			bind_pdo($sql, $parameters);*/

			//update order
			$sql        = "update `order` set status = ? where id = ? and status != ?";
			$parameters = array("REVISED", $last_order_id, "COMPLETED");
			bind_pdo($sql, $parameters);

			//update room
			foreach ($_SESSION["switch"]["ordered_room_id"] as $room_id) {
				$sql        = "update room set status = ?, lastupby = ?, lastupdate = ?, reservedate_from = NULL, reservedate_to = NULL, rentaldate_from = NULL, rentaldate_to = NULL where id = ?";
				$parameters = array("SUSPENDED", $_SESSION['cmsloginid'], $nowdate, $room_id,);
				bind_pdo($sql, $parameters);
			}


			$from_order_id       = $last_order_id;
			$total_return_amount = $_SESSION["switch"]["total_return_amount"];

		}

		//insert customer (new customer)
		if($_SESSION["warehousing"][1]["new_customer"] == 1){
			//if new customer, create a customer record
			$hkid = NULL;
			$hkbr = NULL;

			if ($_SESSION["warehousing"][1]["identity_type"] == "PERSONAL") {
				//$code_start_with = "CP";
				$hkid            = rsa_crypt($_SESSION["warehousing"][1]["identity_id"], 1);
			}

			if ($_SESSION["warehousing"][1]["identity_type"] == "BUSINESS") {
				//$code_start_with = "CB";
				$hkbr            = rsa_crypt($_SESSION["warehousing"][1]["identity_id"], 1);
			}

			$total_num_of_customer = count(get_customer());
			$new_code_id       = $total_num_of_customer + 1;
			$password              = random_string(15);
			$encrypt_password      = rsa_crypt($password, 1);

			$customer_code = "C".date("y") . str_pad($new_code_id, 4, "0", STR_PAD_LEFT);

			$_SESSION["warehousing"][1]["customer_code"]          = $customer_code;

			$data                  = array(
				"code"        => $customer_code,
				"typeid"      => $_SESSION["warehousing"][1]["identity_type"],
				"title"       => $_SESSION["warehousing"][1]["title"],
				"firstname"   => rsa_crypt($_SESSION["warehousing"][1]["firstname"], 1),
				"lastname"    => rsa_crypt($_SESSION["warehousing"][1]["lastname"], 1),
				"companyname" => $_SESSION["warehousing"][1]["companyname"],
				"tel"         => rsa_crypt($_SESSION["warehousing"][1]["tel"], 1),
				"email"       => rsa_crypt($_SESSION["warehousing"][1]["email"], 1),
				"address"     => rsa_crypt($_SESSION["warehousing"][1]["address"], 1),
				"hkid"        => $hkid,
				"hkbr"        => $hkbr,
				"loginid"     => rsa_crypt($_SESSION["warehousing"][1]["email"], 1),
				"password"    => $encrypt_password,
				"createby"    => $_SESSION['cmsloginid'],
				"createdate"  => $nowdate,
				"lastupby"    => $_SESSION['cmsloginid'],
				"lastupdate"  => $nowdate
			);

			$result = insert_record("customer", $data);

			$_SESSION["warehousing"][1]["customer_id"] = $new_code_id;
			/*if($result){
				$customer_id = $dbh->lastInsertId();

				$_SESSION["warehousing"][1]["customer_id"] = $customer_id;
			}*/
		}

		//insert order
		//$order_num     = count(get_order());
		$sql2        = "select * from `order` ";
		$parameters2 = array();
		$order_info2 = bind_pdo($sql2, $parameters2, "selectall");
		$order_num = count($order_info2);

		$new_order_num = $order_num + 1;

		$order_code = $location_info["location_code"] . "-CT" . date("y") . str_pad($new_order_num, 4, "0", STR_PAD_LEFT);
		if (!empty($_SESSION["warehousing"][2]["delivery_service"])) {
			$transport_date        = $_SESSION["warehousing"][2]["delivery_date"];
			$transport_address     = $_SESSION["warehousing"][2]["delivery_from"];
			$transport_destination = $_SESSION["warehousing"][2]["delivery_to"];
		} else {
			$transport_date        = NULL;
			$transport_address     = NULL;
			$transport_destination = NULL;
		}

		if ($_SESSION["warehousing"][2]["paymethod"] == "CHEQUE") {
			$bank_name  = $_SESSION["warehousing"][2]["bank_name"];
			$cheque_num = $_SESSION["warehousing"][2]["cheque_num"];
		} else {
			$bank_name  = "";
			$cheque_num = "";
		}

		if ($_SESSION["warehousing"][2]["paymethod2"] == "CHEQUE") {
			$bank_name2  = $_SESSION["warehousing"][2]["bank_name2"];
			$cheque_num2 = $_SESSION["warehousing"][2]["cheque_num2"];
		} else {
			$bank_name2  = "";
			$cheque_num2 = "";
		}

		if ($_SESSION["warehousing"][2]["paymethod"] == "OTHER" || $_SESSION["warehousing"][2]["paymethod"] == "BANKIN" ) {
			$paymethod_other = $_SESSION["warehousing"][2]["paymethod_other"];
		} else {
			$paymethod_other = "";
		}

		if ($_SESSION["warehousing"][2]["paymethod2"] == "OTHER" || $_SESSION["warehousing"][2]["paymethod2"] == "BANKIN" ) {
			$paymethod_other2 = $_SESSION["warehousing"][2]["paymethod_other2"];
		} else {
			$paymethod_other2 = "";
		}

		$hkid = NULL;
		$hkbr = NULL;

		if ($_SESSION["warehousing"][1]["identity_type"] == "PERSONAL") {
			//$code_start_with = "CP";
			$hkid            = rsa_crypt($_SESSION["warehousing"][1]["identity_id"], 1);
		}

		if ($_SESSION["warehousing"][1]["identity_type"] == "BUSINESS") {
			//$code_start_with = "CB";
			$hkbr            = rsa_crypt($_SESSION["warehousing"][1]["identity_id"], 1);
		}


		$data = array(
			"company_id"              => $_SESSION["warehousing"][2]["company_id"],
			"branch_id"               => $_SESSION["warehousing"][2]["branch_id"],
			"location_id"             => $_SESSION["warehousing"][2]["location_id"],
			"customer_id"             => $_SESSION["warehousing"][1]["customer_id"],
			"status"                  => "NEW",
			"code"                    => $order_code,
			"total_retail_price"      => $_SESSION["warehousing"][2]["total_retail_price"],
			"total_discounted_price"  => $_SESSION["warehousing"][2]["total_discounted_price"], //each month rent
			"deposit"                 => $_SESSION["warehousing"][2]["deposit"],
			"deposit_penalty"         => 0,
			"rent_month"              => $_SESSION["warehousing"][2]["rent_month"],
			"prepaid_month"           => $_SESSION["warehousing"][2]["prepaid_month"],
			"prepaid_amount"          => $_SESSION["warehousing"][2]["prepaid_amount"],
			"original_amount"         => $_SESSION["warehousing"][2]["order_total"],
			"balance"                 => $_SESSION["warehousing"][2]["final_order_total"],
			"docdate"                 => date("Y-m-d H:i:s"),
			"transport_enable"        => $_SESSION["warehousing"][2]["delivery_service"],
			"transport_date"          => $transport_date,
			"transport_address"       => $transport_address,
			"transport_destination"   => $transport_destination,
			"startdate"               => $_SESSION["warehousing"][2]["rent_from"],
			"enddate"                 => $_SESSION["warehousing"][2]["rent_to"],
			"remark"                  => $_SESSION["warehousing"][2]["remark"],
			"warehousing_date"        => $_SESSION["warehousing"][2]["warehousing_date"],
			"roughly_size"            => $_SESSION["warehousing"][2]["roughly_size"],
			"free_first_month_deduct" => $_SESSION["warehousing"][2]["free_first_month_deduct"],
			"discount_deduct"         => $_SESSION["warehousing"][2]["discount_deduct"],
			"last_paid"               => $_SESSION["warehousing"][2]["last_paid"],
			"createby"                => $_SESSION['cmsloginid'],
			"createdate"              => $nowdate,
			"lastupby"                => $_SESSION['cmsloginid'],
			"lastupdate"              => $nowdate,

			//insert payment in to order
			"paymethod"               => $_SESSION["warehousing"][2]["paymethod"],
			"bank_name"               => $bank_name,
			"cheque_num"              => $cheque_num,
			"paymethod_other"         => $paymethod_other,
			"paymethod2"               => $_SESSION["warehousing"][2]["paymethod2"],
			"bank_name2"               => $bank_name2,
			"cheque_num2"              => $cheque_num2,
			"paymethod_other2"         => $paymethod_other2,
			"from_order_id"           => $from_order_id,
			"total_return_amount"     => $total_return_amount,
			"room_key"                => $_SESSION["warehousing"][2]["room_key"],
			"customer_code" => $_SESSION["warehousing"][1]["customer_code"],
			"customer_typeid" => $_SESSION["warehousing"][1]["identity_type"],
			"customer_title" =>  $_SESSION["warehousing"][1]["title"],
			"customer_firstname" =>  rsa_crypt($_SESSION["warehousing"][1]["firstname"], 1),
			"customer_lastname" =>  rsa_crypt($_SESSION["warehousing"][1]["lastname"], 1),
			"customer_companyname" =>  $_SESSION["warehousing"][1]["companyname"],
			"customer_address" =>  rsa_crypt($_SESSION["warehousing"][1]["address"], 1),
			"customer_tel" =>  rsa_crypt($_SESSION["warehousing"][1]["tel"], 1),
			"customer_email" =>  rsa_crypt($_SESSION["warehousing"][1]["email"], 1),
			"customer_hkid" =>  $hkid,
			"customer_hkbr" =>  $hkbr,

		);

		$result = insert_record("order", $data);

		$order_id = $dbh->lastInsertId();

		//insert order discount
		if (!empty($_SESSION["warehousing"][2]["order_discount"])) {
			foreach ($_SESSION["warehousing"][2]["order_discount"] as $discount) {

				$data = array(
					"order_id"                => $order_id,
					"master_room_discount_id" => $discount["master_room_discount_id"],
					"master_room_id"          => $discount["master_room_id"],
					"room_id"          => $discount["room_id"],
					"discount_type"           => $discount["discount_type"],
					"discount"                => $discount["discount"],
					"monthly_deduct"          => $discount["monthly_deduct"],
					"createby"                => $_SESSION['cmsloginid'],
					"createdate"              => $nowdate,
				);

				$result = insert_record("order_discount", $data);
			}
		}

		//if free first month fee
		if ($_SESSION["warehousing"][2]["free_first_month"] == 1) {
			$sql                   = "select * from master_room_discount where discount_type = ? and status = ? and deleted = ?";
			$parameters            = array("MO", 1, 0);
			$free_first_month_info = bind_pdo($sql, $parameters, "selectone");
			$data                  = array(
				"order_id"                => $order_id,
				"master_room_discount_id" => $free_first_month_info["id"],
				/*"room_id"          => $discount["room_id"],*/
				"discount_type"           => "MO",
				"monthly_deduct"          => round($_SESSION["warehousing"][2]["biggest_discounted_price"]-$_SESSION["warehousing"][2]["biggest_retail_price"]*0.5, 1),
				"createby"                => $_SESSION['cmsloginid'],
				"createdate"              => $nowdate,
			);

			$result = insert_record("order_discount", $data);
		}


		//insert order product
		$order_product_total = 0;

		if (!empty($_SESSION["warehousing"][2]["order_product"])) {

			foreach ($_SESSION["warehousing"][2]["order_product"] as $product) {

				$data = array(
					"order_id"    => $order_id,
					"customer_id" => $_SESSION["warehousing"][1]["customer_id"],
					"product_id"  => $product["product_id"],
					"qty"         => $product["qty"],
					"price"       => $product["price"],
					"amount"      => $product["price"] * $product["qty"],
					"createby"    => $_SESSION['cmsloginid'],
					"createdate"  => $nowdate,
					"lastupby"    => $_SESSION['cmsloginid'],
					"lastupdate"  => $nowdate,
				);

				$result = insert_record("order_product", $data);

				$order_product_total += $product["price"] * $product["qty"];
			}

			//update order
			$sql = "update `order` set total_order_product = ? where id = ?";
			$parameters = array($order_product_total, $order_id);
			$result = bind_pdo($sql, $parameters);

			/*if($_SESSION["warehousing"][2]["prepaid_month"] > 0){
				$sql = "update `order` set prepaid_amount=prepaid_amount+? where id = ?";
				$parameters = array($order_product_total, $order_id);
				$result = bind_pdo($sql, $parameters);
			}*/

		}


		//insert order room
		if (!empty($_SESSION["warehousing"][2]["order_room"])) {
			foreach ($_SESSION["warehousing"][2]["order_room"] as $room) {

				$data = array(
					"order_id"         => $order_id,
					"room_id"          => $room["room_id"],
					"retail_price"     => $room["retail_price"],
					"discounted_price" => $room["discounted_price"],
					"createby"         => $_SESSION['cmsloginid'],
					"createdate"       => $nowdate,

				);

				$result = insert_record("order_room", $data);

				//update room status
				$sql        = "update room set `status` = 'RENTED', rentaldate_from = ?, rentaldate_to = ? where id = ?";
				$parameters = array($_SESSION["warehousing"][2]["rent_from"], $_SESSION["warehousing"][2]["rent_to"], $room["room_id"]);
				bind_pdo($sql, $parameters);

				//_log(basename($_SERVER['PHP_SELF']), "39325088", $sql, $parameters);

			}
		}


		/*** DEPOSIT ***/
		$sql                  = "select max(id) as max_id from deposit";
		$new_deposit_code_num = bind_pdo($sql, NULL, "selectone");

		$deposit_code = $location_info["location_code"] . "-D" . date("y") . str_pad($new_deposit_code_num["max_id"] + 1, 4, "0", STR_PAD_LEFT);

		if($_SESSION["warehousing"][2]["paymethod"] == "UNPAID"){
			$deposit_paid_status = "NEW";
			$deposit_balance = $_SESSION["warehousing"][2]["deposit"];
		}else{
			$deposit_paid_status = "PAID";
			$deposit_balance = 0;
		}

		$data = array(
			"order_id"            => $order_id,
			"code"                => $deposit_code,
			//"status"              => "NEW",
			//"status"              => "PAID",
			"status"              => $deposit_paid_status,
			"amount"              => $_SESSION["warehousing"][2]["deposit"],
			//"balance"             => $_SESSION["warehousing"][2]["deposit"],
			"balance"             => $deposit_balance,
			"expected_returndate" => $_SESSION["warehousing"][2]["rent_to"],
			"docdate"             => $nowdate,
			"duedate"             => $nowdate,
			"createby"            => $_SESSION['cmsloginid'],
			"createdate"          => $nowdate,
			"lastupby"            => $_SESSION['cmsloginid'],
			"lastupdate"          => $nowdate,
			"customer_id"         => $_SESSION["warehousing"][1]["customer_id"]
		);

		$result = insert_record("deposit", $data);
		$deposit_id = $dbh->lastInsertId();
		/*** END DEPOSIT ***/


		/*** INVOICE AND PAYMENT ***/

		//generate a invoice, need to update when sum the total
		$sql                  = "select max(id) as max_id from invoice";
		$new_invoice_code_num = bind_pdo($sql, NULL, "selectone");

		$invoice_code = $location_info["location_code"] . "-I" . date("y") . str_pad($new_invoice_code_num["max_id"] + 1, 4, "0", STR_PAD_LEFT);

		$price = $_SESSION["warehousing"][2]["total_discounted_price"];

		if($_SESSION["warehousing"][2]["paymethod2"] == "UNPAID"){
			$invoice_paid_status = "NEW";
		}else{
			$invoice_paid_status = "SETTLED";
		}

		$data = array(
			"order_id"    => $order_id,
			"customer_id" => $_SESSION["warehousing"][1]["customer_id"],
			//"status"      => "NEW",
			//"status"      => "SETTLED",
			"status"      => $invoice_paid_status,
			"code"        => $invoice_code,
			"docdate"     => $nowdate,
			"duedate"     => $_SESSION["warehousing"][2]["warehousing_date"],
			"createby"    => $_SESSION['cmsloginid'],
			"createdate"  => $nowdate,
			"lastupby"    => $_SESSION['cmsloginid'],
			"lastupdate"  => $nowdate
		);

		$result     = insert_record("invoice", $data);
		$invoice_id = $dbh->lastInsertId();



		$last_paid_first_month_total = 0;
		//$last_paid_first_month       = 1;
		$last_paid_first_month       = $_SESSION["warehousing"][2]["rent_month"];

		$total_invoice_amount = $order_product_total;

		//generate a payment for invoice
		if($_SESSION["warehousing"][2]["free_first_month"] == 1 || (float)$_SESSION["warehousing"][2]["last_paid"] > 0){

			$total_payment = 0;

			/*$sql                  = "select max(id) as max_id from payment";
			$new_payment_code_num = bind_pdo($sql, NULL, "selectone");

			//$payment_code = $location_info["location_code"]."-P".date("y"). str_pad($new_payment_code_num["max_id"] + 1, 4, "0", STR_PAD_LEFT);
			$payment_code = "P" . date("y") . str_pad($new_payment_code_num["max_id"] + 1, 4, "0", STR_PAD_LEFT);
			$data         = array(
				"customer_id"     => $_SESSION["warehousing"][1]["customer_id"],
				"paymethod"       => $_SESSION["warehousing"][2]["paymethod"],
				"bank_name"       => $bank_name,
				"cheque_num"      => $cheque_num,
				"paymethod_other" => $paymethod_other,
				"status"          => "PAID",
				"code"            => $payment_code,
				"docdate"         => $nowdate,
				"amount"          => 0,
				"createby"        => $_SESSION['cmsloginid'],
				"createdate"      => $nowdate,
				"lastupby"        => $_SESSION['cmsloginid'],
				"lastupdate"      => $nowdate,
			);

			$result = insert_record("payment", $data);
			$payment_id = $dbh->lastInsertId();*/


			if($_SESSION["warehousing"][2]["free_first_month"] == 1){
				$total_payment += round($_SESSION["warehousing"][2]["total_discounted_price"]-$_SESSION["warehousing"][2]["biggest_discounted_price"]+$_SESSION["warehousing"][2]["biggest_retail_price"]*0.5, 1);

				//$total_invoice_amount += $_SESSION["warehousing"][2]["total_discounted_price"];
				$total_invoice_amount += round($_SESSION["warehousing"][2]["total_discounted_price"]-$_SESSION["warehousing"][2]["biggest_discounted_price"]+$_SESSION["warehousing"][2]["biggest_retail_price"]*0.5, 1);
				//invoice_dtl
				$data = array(
					"invoice_id" => $invoice_id,
					"type"       => "RENT",
					//"price"      => $price,
					"price"      => round($_SESSION["warehousing"][2]["total_discounted_price"]-$_SESSION["warehousing"][2]["biggest_discounted_price"]+$_SESSION["warehousing"][2]["biggest_retail_price"]*0.5, 1),
					"uom_price"  => "MO",
					"qty"        => "1",
					//"amount"     => $price * 1,
					"amount"     => round($_SESSION["warehousing"][2]["total_discounted_price"]-$_SESSION["warehousing"][2]["biggest_discounted_price"]+$_SESSION["warehousing"][2]["biggest_retail_price"]*0.5, 1),
					"remark"     => "首月半價",
					"month"      => 1,
					"createby"   => $_SESSION['cmsloginid'],
					"createdate" => $nowdate,
					"lastupby"   => $_SESSION['cmsloginid'],
					"lastupdate" => $nowdate
				);

				$result = insert_record("invoice_dtl", $data);

				//payment dtl
				/*$data = array(
					"payment_id"   => $payment_id,
					"payable_type" => "INVOICE",
					"payable_id"   => $invoice_id,
					"status"       => "PAID",
					"docdate"      => $nowdate,
					"amount"       => $_SESSION["warehousing"][2]["total_discounted_price"],
					"createby"     => $_SESSION['cmsloginid'],
					"createdate"   => $nowdate,
					"lastupby"     => $_SESSION['cmsloginid'],
					"lastupdate"   => $nowdate,
				);

				$result = insert_record("payment_dtl", $data);*/
			}

			if((float)$_SESSION["warehousing"][2]["last_paid"] > 0){
				$total_payment += (float)$_SESSION["warehousing"][2]["last_paid"];

				$last_paid = $_SESSION["warehousing"][2]["last_paid"];

				for ($i = 1; $i <= $_SESSION["warehousing"][2]["rent_month"]; $i++) {

					//if free first month
					if ($_SESSION["warehousing"][2]["free_first_month"] == 1 && $i == 1) {
						//skip it, already did it
						continue;
					}

					//each month rent need to deduct with last paid
					$balance = $_SESSION["warehousing"][2]["total_discounted_price"];

					$sql                  = "select max(id) as max_id from invoice";
					$new_invoice_code_num = bind_pdo($sql, NULL, "selectone");

					$invoice_code = $location_info["location_code"] . "-I" . date("y") . str_pad($new_invoice_code_num["max_id"] + 1, 4, "0", STR_PAD_LEFT);

					//$amount = $_SESSION["warehousing"][2]["total_discounted_price"];
					//$price  = $_SESSION["warehousing"][2]["total_discounted_price"];
					$remark = NULL;

					if ($last_paid > 0 && $last_paid >= $_SESSION["warehousing"][2]["total_discounted_price"]) {
						//$amount = 0;
						//$price  = $_SESSION["warehousing"][2]["total_discounted_price"];
						$last_paid -= $_SESSION["warehousing"][2]["total_discounted_price"];
						//$invoice_status = "SETTLED";
					} else {
						//$amount         = $_SESSION["warehousing"][2]["total_discounted_price"] - $last_paid;
						//$price          = $_SESSION["warehousing"][2]["total_discounted_price"] - $last_paid;
						//$invoice_status = "NEW";

						if($last_paid_first_month_total == 0){
							$last_paid_first_month_total = $_SESSION["warehousing"][2]["total_discounted_price"] - $last_paid;
							$last_paid_first_month       = $i;

							$last_paid = 0;
						}
					}

					//if last pay > 0, override previous
					if ($last_paid > 0) {
						$remark = "整數扣減已付款項";

						//$total_invoice_amount += $_SESSION["warehousing"][2]["total_discounted_price"];
						$total_invoice_amount += 0;

						//invoice_dtl
						$data = array(
							"invoice_id" => $invoice_id,
							"type"       => "RENT",
							"remark"     => $remark,
							"month"      => $i,
							//"price"      => $_SESSION["warehousing"][2]["total_discounted_price"],
							"price"      => 0,
							"uom_price"  => "MO",
							"qty"        => "1",
							//"amount"     => $_SESSION["warehousing"][2]["total_discounted_price"] * 1,
							"amount"     => 0,
							"createby"   => $_SESSION['cmsloginid'],
							"createdate" => $nowdate,
							"lastupby"   => $_SESSION['cmsloginid'],
							"lastupdate" => $nowdate
						);

						$result = insert_record("invoice_dtl", $data);
					} else {
						if ($i == $last_paid_first_month && $last_paid_first_month_total > 0) {
							$remark = "非整數扣減已付款項";

							//$total_invoice_amount += $_SESSION["warehousing"][2]["total_discounted_price"];
							$total_invoice_amount += $last_paid_first_month_total;
							//invoice_dtl
							$data = array(
								"invoice_id" => $invoice_id,
								"type"       => "RENT",
								"remark"     => $remark,
								"month"      => $i,
								//"price"      => $_SESSION["warehousing"][2]["total_discounted_price"],
								"price"      => $last_paid_first_month_total,
								"uom_price"  => "MO",
								"qty"        => "1",
								//"amount"     => $_SESSION["warehousing"][2]["total_discounted_price"] * 1,
								"amount"     => $last_paid_first_month_total,
								"createby"   => $_SESSION['cmsloginid'],
								"createdate" => $nowdate,
								"lastupby"   => $_SESSION['cmsloginid'],
								"lastupdate" => $nowdate
							);

							$result = insert_record("invoice_dtl", $data);
						}
					}


				}

				//payment_dtl
				/*$data = array(
					"payment_id"   => $payment_id,
					"payable_type" => "INVOICE",
					"payable_id"   => $invoice_id,
					"status"       => "PAID",
					"docdate"      => $nowdate,
					"amount"       => $_SESSION["warehousing"][2]["last_paid"],
					"createby"     => $_SESSION['cmsloginid'],
					"createdate"   => $nowdate,
					"lastupby"     => $_SESSION['cmsloginid'],
					"lastupdate"   => $nowdate,
				);

				$result = insert_record("payment_dtl", $data);*/

			}

			//update payment
			/*$sql = "update payment set amount = ? where id = ?";
			$parameters = array($total_payment, $payment_id);
			$result = bind_pdo($sql, $parameters);*/

		}else{
			//no free first month, no last paid, no prepaid

			if($_SESSION["warehousing"][2]["prepaid_month"] <= 0){
				$total_invoice_amount += $_SESSION["warehousing"][2]["total_discounted_price"];

				$data = array(
					"invoice_id" => $invoice_id,
					"type"       => "RENT",
					"remark"     => "",
					"month"      => 1,
					"price"      => $_SESSION["warehousing"][2]["total_discounted_price"],
					"uom_price"  => "MO",
					"qty"        => "1",
					"amount"     => $_SESSION["warehousing"][2]["total_discounted_price"] * 1,
					"createby"   => $_SESSION['cmsloginid'],
					"createdate" => $nowdate,
					"lastupby"   => $_SESSION['cmsloginid'],
					"lastupdate" => $nowdate
				);

				$result = insert_record("invoice_dtl", $data);
			}

		}


		$invoice_balance = 0;

		// 3. check if prepaid
		if ($_SESSION["warehousing"][2]["prepaid_month"] > 0) {

			//$invoice_balance = $order_product_total + $_SESSION["warehousing"][2]["total_discounted_price"] * $_SESSION["warehousing"][2]["rent_month"];

			//looping remain rent month
			if($_SESSION["warehousing"][2]["rent_month"] == $last_paid_first_month){
				//$remain_rent_month = $_SESSION["warehousing"][2]["rent_month"];

				if(!isset($i)){
					$i = 1;
				}

				for($j = $i; $j <= $_SESSION["warehousing"][2]["rent_month"]; $j++){

					if($_SESSION["warehousing"][2]["free_first_month"] == 1 && $j == 1){
						continue;
					}

					$total_invoice_amount += $_SESSION["warehousing"][2]["total_discounted_price"];


					$data = array(
						"invoice_id" => $invoice_id,
						"type"       => "RENT",
						"remark"     => "預繳租金",
						"month"      => $j,
						"price"      => $_SESSION["warehousing"][2]["total_discounted_price"],
						"uom_price"  => "MO",
						"qty"        => "1",
						"amount"     => $_SESSION["warehousing"][2]["total_discounted_price"] * 1,
						"createby"   => $_SESSION['cmsloginid'],
						"createdate" => $nowdate,
						"lastupby"   => $_SESSION['cmsloginid'],
						"lastupdate" => $nowdate
					);

					$result = insert_record("invoice_dtl", $data);
				}
			}else{
				//$remain_rent_month = $_SESSION["warehousing"][2]["rent_month"] - $last_paid_first_month;

				for($i = 1; $i <= $_SESSION["warehousing"][2]["rent_month"]; $i++){

					if($_SESSION["warehousing"][2]["free_first_month"] == 1 && $i == 1){
						continue;
					}

					if($i > $last_paid_first_month){
						$total_invoice_amount += $_SESSION["warehousing"][2]["total_discounted_price"];

						$data = array(
							"invoice_id" => $invoice_id,
							"type"       => "RENT",
							"remark"     => "預繳租金",
							"month"      => $i,
							"price"      => $_SESSION["warehousing"][2]["total_discounted_price"],
							"uom_price"  => "MO",
							"qty"        => "1",
							"amount"     => $_SESSION["warehousing"][2]["total_discounted_price"] * 1,
							"createby"   => $_SESSION['cmsloginid'],
							"createdate" => $nowdate,
							"lastupby"   => $_SESSION['cmsloginid'],
							"lastupdate" => $nowdate
						);

						$result = insert_record("invoice_dtl", $data);
					}
				}
			}
		}else{
			//no prepaid
			//$invoice_balance = $order_product_total + $_SESSION["warehousing"][2]["total_discounted_price"] * $_SESSION["warehousing"][2]["rent_month"];
		}


		//case 1: has free first month, last paid > 0
		if ($_SESSION["warehousing"][2]["free_first_month"] == 1 && (float)$_SESSION["warehousing"][2]["last_paid"] > 0) {
			$invoice_balance = $total_invoice_amount - $_SESSION["warehousing"][2]["total_discounted_price"] - (float)$_SESSION["warehousing"][2]["last_paid"];

			//var_dump("case 1");
		}

		//case 2: has free first month, last paid = 0
		if ($_SESSION["warehousing"][2]["free_first_month"] == 1 && (float)$_SESSION["warehousing"][2]["last_paid"] == 0) {
			$invoice_balance = $total_invoice_amount - $_SESSION["warehousing"][2]["total_discounted_price"];

			//var_dump("case 2");
		}

		//case 3: no free first month, last paid > 0
		if ($_SESSION["warehousing"][2]["free_first_month"] != 1 && (float)$_SESSION["warehousing"][2]["last_paid"] > 0) {

			$invoice_balance = $total_invoice_amount - (float)$_SESSION["warehousing"][2]["last_paid"];

			//var_dump("case 3");
		}

		//case 4: no free first month, last paid = 0
		if ($_SESSION["warehousing"][2]["free_first_month"] != 1 && (float)$_SESSION["warehousing"][2]["last_paid"] == 0) {

			//$invoice_balance = 0;

			//only first month
			$invoice_balance = $order_product_total + $_SESSION["warehousing"][2]["total_discounted_price"];

			//var_dump("case 4");
		}

		// end of 3


		// 4. other normal setting

		//insert order product invoice_dtl
		foreach ($_SESSION["warehousing"][2]["order_product"] as $order_product) {
			//insert order product invoice detail
			$data = array(
				"invoice_id"   => $invoice_id,
				"type"         => "ORDER_PRODUCT",
				"price"        => $order_product["price"],
				"uom_price"    => "PCS",
				"qty"          => $order_product["qty"],
				"amount"       => $order_product["price"] * $order_product["qty"],
				"order_dtl_id" => $order_product["product_id"],
				"createby"     => $_SESSION['cmsloginid'],
				"createdate"   => $nowdate,
				"lastupby"     => $_SESSION['cmsloginid'],
				"lastupdate"   => $nowdate
			);

			$result = insert_record("invoice_dtl", $data);
		}

		// end of 4

		if($_SESSION["warehousing"][2]["paymethod2"] != "UNPAID"){
			$invoice_balance = 0;
		}

		//update invoice
		$sql        = "update invoice set amount = ?, balance = ? where id = ?";
		$parameters = array($total_invoice_amount, $invoice_balance, $invoice_id);
		//$parameters = array($total_invoice_amount, 0, $invoice_id);
		$result     = bind_pdo($sql, $parameters);


		if($_SESSION["warehousing"][2]["paymethod"] != "UNPAID" || $_SESSION["warehousing"][2]["paymethod2"] != "UNPAID"){
			//PAYMENT for this invoice

			$amount = 0;
			if($_SESSION["warehousing"][2]["paymethod"] != "UNPAID"){
				$amount += $_SESSION["warehousing"][2]["deposit"];
			}

			if($_SESSION["warehousing"][2]["paymethod2"] != "UNPAID"){
				$amount += $total_invoice_amount;
			}

			if($amount > 0){
				$sql                  = "select max(id) as max_id from payment";
				$new_payment_code_num = bind_pdo($sql, NULL, "selectone");

				//$payment_code = $location_info["location_code"]."-P".date("y"). str_pad($new_payment_code_num["max_id"] + 1, 4, "0", STR_PAD_LEFT);
				$payment_code = "P" . date("y") . str_pad($new_payment_code_num["max_id"] + 1, 4, "0", STR_PAD_LEFT);


				$data         = array(
					"customer_id"     => $_SESSION["warehousing"][1]["customer_id"],
					"paymethod"       => $_SESSION["warehousing"][2]["paymethod"],
					"bank_name"       => $bank_name,
					"cheque_num"      => $cheque_num,
					"paymethod_other" => $paymethod_other,
					"paymethod2"       => $_SESSION["warehousing"][2]["paymethod2"],
					"bank_name2"       => $bank_name2,
					"cheque_num2"      => $cheque_num2,
					"paymethod_other2" => $paymethod_other2,
					"status"          => "PAID",
					"code"            => $payment_code,
					"docdate"         => $nowdate,
					//"amount"          => ($invoice_balance+$_SESSION["warehousing"][2]["deposit"]),
					//"amount"          => $_SESSION["warehousing"][2]["final_order_total"],
					//"amount"          => ($_SESSION["warehousing"][2]["deposit"]+$total_invoice_amount),
					"amount"          => $amount,
					"createby"        => $_SESSION['cmsloginid'],
					"createdate"      => $nowdate,
					"lastupby"        => $_SESSION['cmsloginid'],
					"lastupdate"      => $nowdate,
				);

				$result = insert_record("payment", $data);
				$payment_id = $dbh->lastInsertId();

				if($_SESSION["warehousing"][2]["paymethod"] != "UNPAID"){
					//payment_dtl
					$data = array(
						"payment_id"   => $payment_id,
						"payable_type" => "DEPOSIT",
						"payable_id"   => $deposit_id,
						"status"       => "PAID",
						"docdate"      => $nowdate,
						"amount"       => $_SESSION["warehousing"][2]["deposit"],
						"createby"     => $_SESSION['cmsloginid'],
						"createdate"   => $nowdate,
						"lastupby"     => $_SESSION['cmsloginid'],
						"lastupdate"   => $nowdate,
					);

					$result = insert_record("payment_dtl", $data);
				}

				if($_SESSION["warehousing"][2]["paymethod2"] != "UNPAID"){
					//payment_dtl
					$data = array(
						"payment_id"   => $payment_id,
						"payable_type" => "INVOICE",
						"payable_id"   => $invoice_id,
						"status"       => "PAID",
						"docdate"      => $nowdate,
						"amount"       => $total_invoice_amount,
						"createby"     => $_SESSION['cmsloginid'],
						"createdate"   => $nowdate,
						"lastupby"     => $_SESSION['cmsloginid'],
						"lastupdate"   => $nowdate,
					);

					$result = insert_record("payment_dtl", $data);
				}
			}

		}

		/*** END INVOICE AND PAYMENT ***/

		$dbh->commit();

		//unset session
		unset($_SESSION["warehousing"]);
		unset($_SESSION["switch"]);
		unset($_SESSION["selected_room_tr"]);
		unset($_SESSION["roughly_size"]);
		unset($_SESSION["total_discounted_price"]);
		unset($_SESSION["deposit"]);

		//auto generate report
		header("Location: order_modifyform.php?order_id=".$order_id."&print=1");
	} catch (Exception $e) {

		$dbh->rollBack();

		echo $e->getMessage();

		//header("Location: order_index.php");
	}


Youez - 2016 - github.com/yon3zu
LinuXploit