Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
Nabin Hait | 3237c75 | 2015-02-17 11:11:11 +0530 | [diff] [blame] | 6 | from frappe.utils import cint, flt, cstr, comma_or |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 7 | from erpnext.setup.utils import get_company_currency |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 8 | from frappe import _, throw |
Rushabh Mehta | b5e7689 | 2014-07-29 16:07:43 +0530 | [diff] [blame] | 9 | from erpnext.stock.get_item_details import get_available_qty |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 10 | |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 11 | from erpnext.controllers.stock_controller import StockController |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 12 | |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 13 | class SellingController(StockController): |
Rushabh Mehta | f84c240 | 2014-07-18 16:39:31 +0530 | [diff] [blame] | 14 | def __setup__(self): |
Nabin Hait | de9c8a9 | 2015-02-23 01:06:00 +0530 | [diff] [blame] | 15 | if hasattr(self, "taxes"): |
Rushabh Mehta | c567e8e | 2015-02-03 17:55:52 +0530 | [diff] [blame] | 16 | self.print_templates = { |
| 17 | "taxes": "templates/print_formats/includes/taxes.html" |
Rushabh Mehta | 5b51cc8 | 2014-07-21 18:25:45 +0530 | [diff] [blame] | 18 | } |
Rushabh Mehta | f84c240 | 2014-07-18 16:39:31 +0530 | [diff] [blame] | 19 | |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 20 | def get_feed(self): |
| 21 | return _("To {0} | {1} {2}").format(self.customer_name, self.currency, |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 22 | self.grand_total) |
Rushabh Mehta | c0bb453 | 2014-09-09 16:15:35 +0530 | [diff] [blame] | 23 | |
Rushabh Mehta | b5e7689 | 2014-07-29 16:07:43 +0530 | [diff] [blame] | 24 | def onload(self): |
| 25 | if self.doctype in ("Sales Order", "Delivery Note", "Sales Invoice"): |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 26 | for item in self.get("items"): |
Rushabh Mehta | b5e7689 | 2014-07-29 16:07:43 +0530 | [diff] [blame] | 27 | item.update(get_available_qty(item.item_code, |
| 28 | item.warehouse)) |
| 29 | |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 30 | def validate(self): |
| 31 | super(SellingController, self).validate() |
| 32 | self.validate_max_discount() |
| 33 | check_active_sales_items(self) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 34 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 35 | def set_missing_values(self, for_validate=False): |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 36 | super(SellingController, self).set_missing_values(for_validate) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 37 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 38 | # set contact and address details for customer, if they are not mentioned |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 39 | self.set_missing_lead_customer_details() |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 40 | self.set_price_list_and_item_details() |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 41 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 42 | def set_missing_lead_customer_details(self): |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 43 | if getattr(self, "customer", None): |
Anand Doshi | 4362443 | 2014-02-25 15:42:16 +0530 | [diff] [blame] | 44 | from erpnext.accounts.party import _get_party_details |
Rushabh Mehta | 2cc585f | 2015-02-26 15:01:23 +0530 | [diff] [blame] | 45 | party_details = _get_party_details(self.customer, |
| 46 | ignore_permissions=self.flags.ignore_permissions) |
Nabin Hait | 6b03914 | 2014-05-02 15:45:10 +0530 | [diff] [blame] | 47 | if not self.meta.get_field("sales_team"): |
| 48 | party_details.pop("sales_team") |
| 49 | |
| 50 | self.update_if_missing(party_details) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 51 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 52 | elif getattr(self, "lead", None): |
Rushabh Mehta | 60cfccb | 2015-02-17 10:36:54 +0530 | [diff] [blame] | 53 | from erpnext.crm.doctype.lead.lead import get_lead_details |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 54 | self.update_if_missing(get_lead_details(self.lead)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 55 | |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 56 | def set_price_list_and_item_details(self): |
| 57 | self.set_price_list_currency("Selling") |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 58 | self.set_missing_item_details() |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 59 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 60 | def apply_shipping_rule(self): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 61 | if self.shipping_rule: |
Rushabh Mehta | b385ecf | 2014-03-28 16:44:37 +0530 | [diff] [blame] | 62 | shipping_rule = frappe.get_doc("Shipping Rule", self.shipping_rule) |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 63 | value = self.base_net_total |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 64 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 65 | # TODO |
| 66 | # shipping rule calculation based on item's net weight |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 67 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 68 | shipping_amount = 0.0 |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 69 | for condition in shipping_rule.get("conditions"): |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 70 | if not condition.to_value or (flt(condition.from_value) <= value <= flt(condition.to_value)): |
| 71 | shipping_amount = condition.shipping_amount |
| 72 | break |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 73 | |
Anand Doshi | ac32bad | 2014-04-18 01:30:14 +0530 | [diff] [blame] | 74 | shipping_charge = { |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 75 | "doctype": "Sales Taxes and Charges", |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 76 | "charge_type": "Actual", |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 77 | "account_head": shipping_rule.account, |
Anand Doshi | ac32bad | 2014-04-18 01:30:14 +0530 | [diff] [blame] | 78 | "cost_center": shipping_rule.cost_center |
| 79 | } |
| 80 | |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 81 | existing_shipping_charge = self.get("taxes", filters=shipping_charge) |
Anand Doshi | ac32bad | 2014-04-18 01:30:14 +0530 | [diff] [blame] | 82 | if existing_shipping_charge: |
| 83 | # take the last record found |
Anand Doshi | ff8a854 | 2015-05-26 18:06:09 -0400 | [diff] [blame] | 84 | existing_shipping_charge[-1].tax_amount = shipping_amount |
Anand Doshi | ac32bad | 2014-04-18 01:30:14 +0530 | [diff] [blame] | 85 | else: |
Anand Doshi | ff8a854 | 2015-05-26 18:06:09 -0400 | [diff] [blame] | 86 | shipping_charge["tax_amount"] = shipping_amount |
Anand Doshi | ac32bad | 2014-04-18 01:30:14 +0530 | [diff] [blame] | 87 | shipping_charge["description"] = shipping_rule.label |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 88 | self.append("taxes", shipping_charge) |
Anand Doshi | ac32bad | 2014-04-18 01:30:14 +0530 | [diff] [blame] | 89 | |
| 90 | self.calculate_taxes_and_totals() |
| 91 | |
| 92 | def remove_shipping_charge(self): |
| 93 | if self.shipping_rule: |
| 94 | shipping_rule = frappe.get_doc("Shipping Rule", self.shipping_rule) |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 95 | existing_shipping_charge = self.get("taxes", { |
Anand Doshi | ac32bad | 2014-04-18 01:30:14 +0530 | [diff] [blame] | 96 | "doctype": "Sales Taxes and Charges", |
| 97 | "charge_type": "Actual", |
| 98 | "account_head": shipping_rule.account, |
| 99 | "cost_center": shipping_rule.cost_center |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 100 | }) |
Anand Doshi | ac32bad | 2014-04-18 01:30:14 +0530 | [diff] [blame] | 101 | if existing_shipping_charge: |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 102 | self.get("taxes").remove(existing_shipping_charge[-1]) |
Anand Doshi | ac32bad | 2014-04-18 01:30:14 +0530 | [diff] [blame] | 103 | self.calculate_taxes_and_totals() |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 104 | |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 105 | def set_total_in_words(self): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 106 | from frappe.utils import money_in_words |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 107 | company_currency = get_company_currency(self.company) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 108 | |
Nabin Hait | 04d244a | 2015-07-22 17:47:49 +0530 | [diff] [blame] | 109 | disable_rounded_total = cint(frappe.db.get_value("Global Defaults", None, "disable_rounded_total")) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 110 | |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 111 | if self.meta.get_field("base_in_words"): |
| 112 | self.base_in_words = money_in_words(disable_rounded_total and |
Nabin Hait | 04d244a | 2015-07-22 17:47:49 +0530 | [diff] [blame] | 113 | abs(self.base_grand_total) or abs(self.base_rounded_total), company_currency) |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 114 | if self.meta.get_field("in_words"): |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 115 | self.in_words = money_in_words(disable_rounded_total and |
Nabin Hait | 04d244a | 2015-07-22 17:47:49 +0530 | [diff] [blame] | 116 | abs(self.grand_total) or abs(self.rounded_total), self.currency) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 117 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 118 | def calculate_commission(self): |
| 119 | if self.meta.get_field("commission_rate"): |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 120 | self.round_floats_in(self, ["base_net_total", "commission_rate"]) |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 121 | if self.commission_rate > 100.0: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 122 | throw(_("Commission rate cannot be greater than 100")) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 123 | |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 124 | self.total_commission = flt(self.base_net_total * self.commission_rate / 100.0, |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 125 | self.precision("total_commission")) |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 126 | |
| 127 | def calculate_contribution(self): |
Anand Doshi | ac32bad | 2014-04-18 01:30:14 +0530 | [diff] [blame] | 128 | if not self.meta.get_field("sales_team"): |
| 129 | return |
| 130 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 131 | total = 0.0 |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 132 | sales_team = self.get("sales_team") |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 133 | for sales_person in sales_team: |
| 134 | self.round_floats_in(sales_person) |
| 135 | |
| 136 | sales_person.allocated_amount = flt( |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 137 | self.base_net_total * sales_person.allocated_percentage / 100.0, |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 138 | self.precision("allocated_amount", sales_person)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 139 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 140 | total += sales_person.allocated_percentage |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 141 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 142 | if sales_team and total != 100.0: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 143 | throw(_("Total allocated percentage for sales team should be 100")) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 144 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 145 | def validate_order_type(self): |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 146 | valid_types = ["Sales", "Maintenance", "Shopping Cart"] |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 147 | if not self.order_type: |
| 148 | self.order_type = "Sales" |
| 149 | elif self.order_type not in valid_types: |
Anand Doshi | 80a988c | 2014-07-07 11:35:54 +0530 | [diff] [blame] | 150 | throw(_("Order Type must be one of {0}").format(comma_or(valid_types))) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 151 | |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 152 | def validate_max_discount(self): |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 153 | for d in self.get("items"): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 154 | discount = flt(frappe.db.get_value("Item", d.item_code, "max_discount")) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 155 | |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 156 | if discount and flt(d.discount_percentage) > discount: |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 157 | frappe.throw(_("Maxiumm discount for Item {0} is {1}%").format(d.item_code, discount)) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 158 | |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 159 | def get_item_list(self): |
| 160 | il = [] |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 161 | for d in self.get("items"): |
nathando | 247e9ff | 2014-09-03 15:03:31 +0800 | [diff] [blame] | 162 | if d.qty is None: |
Nabin Hait | c41a948 | 2014-08-18 15:40:15 +0530 | [diff] [blame] | 163 | frappe.throw(_("Row {0}: Qty is mandatory").format(d.idx)) |
Nabin Hait | 1f94fa2 | 2015-08-24 14:32:38 +0530 | [diff] [blame] | 164 | |
Neil Trini Lasrado | ed8cecb | 2015-07-07 13:59:23 +0530 | [diff] [blame] | 165 | if self.has_product_bundle(d.item_code): |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 166 | for p in self.get("packed_items"): |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 167 | if p.parent_detail_docname == d.name and p.parent_item == d.item_code: |
| 168 | # the packing details table's qty is already multiplied with parent's qty |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 169 | il.append(frappe._dict({ |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 170 | 'warehouse': p.warehouse, |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 171 | 'item_code': p.item_code, |
| 172 | 'qty': flt(p.qty), |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 173 | 'uom': p.uom, |
| 174 | 'batch_no': cstr(p.batch_no).strip(), |
| 175 | 'serial_no': cstr(p.serial_no).strip(), |
| 176 | 'name': d.name |
| 177 | })) |
| 178 | else: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 179 | il.append(frappe._dict({ |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 180 | 'warehouse': d.warehouse, |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 181 | 'item_code': d.item_code, |
| 182 | 'qty': d.qty, |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 183 | 'uom': d.stock_uom, |
Nabin Hait | 1d21842 | 2015-07-17 15:19:02 +0530 | [diff] [blame] | 184 | 'stock_uom': d.stock_uom, |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 185 | 'batch_no': cstr(d.get("batch_no")).strip(), |
| 186 | 'serial_no': cstr(d.get("serial_no")).strip(), |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 187 | 'name': d.name |
| 188 | })) |
| 189 | return il |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 190 | |
Neil Trini Lasrado | ed8cecb | 2015-07-07 13:59:23 +0530 | [diff] [blame] | 191 | def has_product_bundle(self, item_code): |
| 192 | return frappe.db.sql("""select name from `tabProduct Bundle` |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 193 | where new_item_code=%s and docstatus != 2""", item_code) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 194 | |
Nabin Hait | 453cc37 | 2015-07-29 12:21:03 +0530 | [diff] [blame] | 195 | def get_already_delivered_qty(self, current_docname, so, so_detail): |
| 196 | delivered_via_dn = frappe.db.sql("""select sum(qty) from `tabDelivery Note Item` |
ankitjavalkarwork | e69a611 | 2014-10-08 12:46:02 +0530 | [diff] [blame] | 197 | where so_detail = %s and docstatus = 1 |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 198 | and against_sales_order = %s |
Nabin Hait | 453cc37 | 2015-07-29 12:21:03 +0530 | [diff] [blame] | 199 | and parent != %s""", (so_detail, so, current_docname)) |
| 200 | |
Nabin Hait | 1826791 | 2015-08-17 15:36:23 +0530 | [diff] [blame] | 201 | delivered_via_si = frappe.db.sql("""select sum(si_item.qty) |
| 202 | from `tabSales Invoice Item` si_item, `tabSales Invoice` si |
| 203 | where si_item.parent = si.name and ifnull(si.update_stock, 0) = 1 |
| 204 | and si_item.so_detail = %s and si.docstatus = 1 |
| 205 | and si_item.sales_order = %s |
| 206 | and si.name != %s""", (so_detail, so, current_docname)) |
Nabin Hait | 453cc37 | 2015-07-29 12:21:03 +0530 | [diff] [blame] | 207 | |
| 208 | total_delivered_qty = (flt(delivered_via_dn[0][0]) if delivered_via_dn else 0) \ |
| 209 | + (flt(delivered_via_si[0][0]) if delivered_via_si else 0) |
| 210 | |
| 211 | return total_delivered_qty |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 212 | |
| 213 | def get_so_qty_and_warehouse(self, so_detail): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 214 | so_item = frappe.db.sql("""select qty, warehouse from `tabSales Order Item` |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 215 | where name = %s and docstatus = 1""", so_detail, as_dict=1) |
| 216 | so_qty = so_item and flt(so_item[0]["qty"]) or 0.0 |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 217 | so_warehouse = so_item and so_item[0]["warehouse"] or "" |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 218 | return so_qty, so_warehouse |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 219 | |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 220 | def check_stop_sales_order(self, ref_fieldname): |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 221 | for d in self.get("items"): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 222 | if d.get(ref_fieldname): |
Rushabh Mehta | f2227d0 | 2014-03-31 23:37:40 +0530 | [diff] [blame] | 223 | status = frappe.db.get_value("Sales Order", d.get(ref_fieldname), "status") |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 224 | if status == "Stopped": |
Rushabh Mehta | 8a40c13 | 2014-04-15 16:30:55 +0530 | [diff] [blame] | 225 | frappe.throw(_("Sales Order {0} is stopped").format(d.get(ref_fieldname))) |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 226 | |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 227 | def check_active_sales_items(obj): |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 228 | for d in obj.get("items"): |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 229 | if d.item_code: |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 230 | item = frappe.db.sql("""select docstatus, is_sales_item, |
| 231 | is_service_item, income_account from tabItem where name = %s""", |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 232 | d.item_code, as_dict=True)[0] |
Rushabh Mehta | 1e8025b | 2015-07-24 15:16:25 +0530 | [diff] [blame] | 233 | if item.is_sales_item == 0 and item.is_service_item == 0: |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 234 | frappe.throw(_("Item {0} must be Sales or Service Item in {1}").format(d.item_code, d.idx)) |
Nabin Hait | 365ae27 | 2014-04-03 17:38:54 +0530 | [diff] [blame] | 235 | if getattr(d, "income_account", None) and not item.income_account: |
Anand Doshi | d294650 | 2014-04-08 20:10:03 +0530 | [diff] [blame] | 236 | frappe.db.set_value("Item", d.item_code, "income_account", |
Nabin Hait | 89ad6f2 | 2013-10-18 12:55:59 +0530 | [diff] [blame] | 237 | d.income_account) |